aboutsummaryrefslogtreecommitdiff
path: root/kernel/src/memory/bitmap_allocator.cpp
blob: caaf5a454cb07dd2dee487eba9651a57c4b6aac8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "kernel/memory/bitmap_allocator.hpp"

#include "kapi/memory.hpp"

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <ranges>
#include <span>
#include <utility>

namespace kernel::memory
{

  bitmap_frame_allocator::bitmap_frame_allocator(std::span<std::uint64_t> storage, std::size_t frame_count) noexcept
      : m_bitmap{storage}
      , m_frame_count{frame_count}
      , m_last_index{}
  {
    constexpr auto bits_per_word = 64uz;
    auto to_fill = (frame_count + bits_per_word - 1) / bits_per_word;
    std::ranges::fill(std::views::take(m_bitmap, to_fill), ~0uz);
  }

  auto bitmap_frame_allocator::allocate_many(std::size_t count) noexcept
      -> std::optional<std::pair<kapi::memory::frame, std::size_t>>
  {
    if (count == 0)
    {
      return std::nullopt;
    }

    auto free_count = 0uz;
    auto first_free = 0uz;

    for (auto i = 0uz; i < m_frame_count; ++i)
    {
      auto const current = (m_last_index + i) % m_frame_count;
      if (!test(current))
      {
        if (free_count == 0)
        {
          first_free = current;
        }

        ++free_count;

        if (free_count == count)
        {
          for (auto j = 0uz; j < count; ++j)
          {
            set(first_free + j);
          }
          m_last_index = first_free + count;
          return std::make_pair(kapi::memory::frame{first_free}, count);
        }
      }
      else
      {
        free_count = 0;
      }
    }

    return std::nullopt;
  }

  auto bitmap_frame_allocator::release_many(std::pair<kapi::memory::frame, std::size_t> frame_set) -> void
  {
    auto const [start, count] = frame_set;
    for (auto i = 0uz; i < count; ++i)
    {
      clear(start.number() + i);
    }
  }

  auto bitmap_frame_allocator::mark_used(kapi::memory::frame frame) -> void
  {
    set(frame.number());
  }

  auto bitmap_frame_allocator::test(std::size_t index) const noexcept -> bool
  {
    constexpr auto bits_per_word = 64uz;
    auto entry_entry = index / bits_per_word;
    auto entry_offset = index % bits_per_word;
    return (m_bitmap[entry_entry] & (1uz << entry_offset));
  }

  auto bitmap_frame_allocator::set(std::size_t index) noexcept -> void
  {
    constexpr auto bits_per_word = 64uz;
    auto entry_entry = index / bits_per_word;
    auto entry_offset = index % bits_per_word;
    m_bitmap[entry_entry] |= (1uz << entry_offset);
  }

  auto bitmap_frame_allocator::clear(std::size_t index) noexcept -> void
  {
    constexpr auto bits_per_word = 64uz;
    auto entry_entry = index / bits_per_word;
    auto entry_offset = index % bits_per_word;
    m_bitmap[entry_entry] &= ~(1uz << entry_offset);
  }

}  // namespace kernel::memory