aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/include
diff options
context:
space:
mode:
Diffstat (limited to 'arch/x86_64/include')
-rw-r--r--arch/x86_64/include/arch/memory/frame_allocator.hpp45
1 files changed, 43 insertions, 2 deletions
diff --git a/arch/x86_64/include/arch/memory/frame_allocator.hpp b/arch/x86_64/include/arch/memory/frame_allocator.hpp
index a52cc46..fa22ce5 100644
--- a/arch/x86_64/include/arch/memory/frame_allocator.hpp
+++ b/arch/x86_64/include/arch/memory/frame_allocator.hpp
@@ -57,8 +57,40 @@ namespace teachos::arch::memory
};
/**
+ * @brief Iterator for memory areas
+ */
+ class memory_area_iterator
+ {
+ memory_area * ptr;
+
+ public:
+ std::size_t begin;
+ std::size_t end;
+
+ explicit memory_area_iterator(memory_area * p)
+ : ptr(p)
+ {
+ }
+
+ memory_area & operator*() const { return *ptr; }
+ memory_area_iterator & operator++()
+ {
+ ++ptr;
+ return *this;
+ }
+
+ memory_area_iterator operator++(int)
+ {
+ memory_area_iterator temp = *this;
+ ++(*this);
+ return temp;
+ }
+
+ bool operator==(const memory_area_iterator & other) const { return ptr == other.ptr; }
+ };
+
+ /**
* @brief Allocates memory using memory areas read from the multiboot2 information pointer
- *
*/
struct area_frame_allocator
{
@@ -70,6 +102,10 @@ namespace teachos::arch::memory
frame multiboot_start; //!< The start address of the multiboot code in memory
frame multiboot_end; //!< The end address of the multiboot code in memory
+ private:
+ uint8_t N;
+
+ public:
/**
* @brief Constructor
*
@@ -80,12 +116,13 @@ namespace teachos::arch::memory
* @param memory_areas Pointer to the first element of all memory areas
*/
area_frame_allocator(std::size_t kernel_start, std::size_t kernel_end, std::size_t multiboot_start,
- std::size_t multiboot_end, memory_area * memory_areas)
+ std::size_t multiboot_end, memory_area * memory_areas, uint8_t area_count)
: areas(memory_areas)
, kernel_start(frame{kernel_start})
, kernel_end(frame{kernel_end})
, multiboot_start(frame{multiboot_start})
, multiboot_end(frame{multiboot_end})
+ , N(area_count)
{
choose_next_area();
}
@@ -109,6 +146,10 @@ namespace teachos::arch::memory
*/
auto deallocate_frame(frame frame) -> void;
+ memory_area_iterator begin() { return memory_area_iterator(areas); }
+
+ memory_area_iterator end() { return memory_area_iterator(areas + N); }
+
private:
/**
* @brief Find the next memory area and write it into current_area