From d9fc50f1ea18c571a83625249a5b31723a3fe2f3 Mon Sep 17 00:00:00 2001 From: apio Date: Mon, 5 Dec 2022 13:23:01 +0100 Subject: [PATCH] Make MemoryMapEntry have private fields with public getters --- kernel/src/memory/MemoryManager.cpp | 20 +++++++-------- kernel/src/memory/MemoryMap.cpp | 38 ++++++++++++++++++----------- kernel/src/memory/MemoryMap.h | 35 +++++++++++++++++++++++--- 3 files changed, 65 insertions(+), 28 deletions(-) diff --git a/kernel/src/memory/MemoryManager.cpp b/kernel/src/memory/MemoryManager.cpp index 768997f4..34cc8f91 100644 --- a/kernel/src/memory/MemoryManager.cpp +++ b/kernel/src/memory/MemoryManager.cpp @@ -29,8 +29,8 @@ static usize get_physical_address_space_size() MemoryMapIterator iter; const MemoryMapEntry entry = iter.highest(); - return entry.ptr + entry.size; // This is the address at the end of the last (highest) entry, thus the whole - // address space that was passed to us. + return entry.address() + entry.size(); // This is the address at the end of the last (highest) entry, thus the whole + // address space that was passed to us. } namespace MemoryManager @@ -55,7 +55,7 @@ namespace MemoryManager const auto largest_free = iter.largest_free(); - expect(largest_free.free, "We were given a largest free memory region that isn't even free!"); + expect(largest_free.is_free(), "We were given a largest free memory region that isn't even free!"); // The entire physical address space. May contain inexistent memory holes, thus differs from total_mem which // only counts existent memory. Our bitmap needs to have space for all of the physical address space, since @@ -63,13 +63,13 @@ namespace MemoryManager const usize physical_address_space_size = get_physical_address_space_size(); // We store our frame bitmap at the beginning of the largest free memory block. - char* const frame_bitmap_addr = (char*)largest_free.ptr; + char* const frame_bitmap_addr = (char*)largest_free.ptr(); const usize frame_bitmap_size = get_blocks_from_size(physical_address_space_size / ARCH_PAGE_SIZE, 8UL); // This should never happen, unless memory is very fragmented. Usually there is always a very big block of // usable memory and then some tiny blocks around it. - expect(frame_bitmap_size < largest_free.size, "No single memory region is enough to hold the frame bitmap"); + expect(frame_bitmap_size < largest_free.size(), "No single memory region is enough to hold the frame bitmap"); g_frame_bitmap.initialize(frame_bitmap_addr, frame_bitmap_size); @@ -78,18 +78,18 @@ namespace MemoryManager iter.rewind(); while (iter.next().try_set_value(entry)) { - const u64 index = entry.ptr / ARCH_PAGE_SIZE; - const u64 pages = entry.size / ARCH_PAGE_SIZE; - if (!entry.free) { reserved_mem += entry.size; } + const u64 index = entry.address() / ARCH_PAGE_SIZE; + const u64 pages = entry.size() / ARCH_PAGE_SIZE; + if (!entry.is_free()) { reserved_mem += entry.size(); } else { - free_mem += entry.size; + free_mem += entry.size(); g_frame_bitmap.clear_region(index, pages, false); } } // Make sure that the physical frames used by the bitmap aren't handed out to anyone else. - lock_frames(largest_free.ptr, get_blocks_from_size(frame_bitmap_size, ARCH_PAGE_SIZE)); + lock_frames(largest_free.address(), get_blocks_from_size(frame_bitmap_size, ARCH_PAGE_SIZE)); } void init() diff --git a/kernel/src/memory/MemoryMap.cpp b/kernel/src/memory/MemoryMap.cpp index 5a97443a..9573a7d7 100644 --- a/kernel/src/memory/MemoryMap.cpp +++ b/kernel/src/memory/MemoryMap.cpp @@ -2,19 +2,29 @@ extern const BOOTBOOT bootboot; -static MemoryMapEntry memory_map_entry_from_mmapent(const MMapEnt* ent) +MemoryMapEntry::MemoryMapEntry(u64 address, u64 size, bool is_free) + : m_address(address), m_size(size), m_is_free(is_free) { - MemoryMapEntry result; - result.ptr = MMapEnt_Ptr(ent); - result.size = MMapEnt_Size(ent); - result.free = MMapEnt_IsFree(ent); - return result; } -MemoryMapIterator::MemoryMapIterator() +MemoryMapEntry& MemoryMapEntry::operator=(const MemoryMapEntry& other) +{ + if (&other == this) return *this; + + m_address = other.address(); + m_size = other.size(); + m_is_free = other.is_free(); + + return *this; +} + +static MemoryMapEntry memory_map_entry_from_mmapent(const MMapEnt* ent) +{ + return {MMapEnt_Ptr(ent), MMapEnt_Size(ent), MMapEnt_IsFree(ent)}; +} + +MemoryMapIterator::MemoryMapIterator() : m_mmap_entries((bootboot.size - 128) / 16), m_base_ent(&bootboot.mmap) { - m_mmap_entries = (bootboot.size - 128) / 16; - m_base_ent = &bootboot.mmap; rewind(); } @@ -44,9 +54,9 @@ MemoryMapEntry MemoryMapIterator::largest_free() MemoryMapEntry entry; while (next().try_set_value(entry)) { - if (entry.free && entry.size > largest_size) + if (entry.is_free() && entry.size() > largest_size) { - largest_size = entry.size; + largest_size = entry.size(); largest_index = m_cur_ent - 1; } } @@ -56,7 +66,7 @@ MemoryMapEntry MemoryMapIterator::largest_free() MemoryMapEntry MemoryMapIterator::highest() { - usize highest_ptr = 0; + usize highest_address = 0; usize highest_index = 0; rewind(); @@ -64,9 +74,9 @@ MemoryMapEntry MemoryMapIterator::highest() MemoryMapEntry entry; while (next().try_set_value(entry)) { - if (entry.ptr > highest_ptr) + if (entry.address() > highest_address) { - highest_ptr = entry.ptr; + highest_address = entry.address(); highest_index = m_cur_ent - 1; } } diff --git a/kernel/src/memory/MemoryMap.h b/kernel/src/memory/MemoryMap.h index 26f86518..05b30b8d 100644 --- a/kernel/src/memory/MemoryMap.h +++ b/kernel/src/memory/MemoryMap.h @@ -5,9 +5,36 @@ struct MemoryMapEntry { - u64 ptr; - u64 size; - bool free; + MemoryMapEntry() = default; + MemoryMapEntry(const MemoryMapEntry& other) = default; + MemoryMapEntry(u64 address, u64 size, bool is_free); + + u64 address() const + { + return m_address; + } + + void* ptr() const + { + return (void*)m_address; + } + + u64 size() const + { + return m_size; + } + + bool is_free() const + { + return m_is_free; + } + + MemoryMapEntry& operator=(const MemoryMapEntry& other); + + private: + u64 m_address; + u64 m_size; + bool m_is_free; }; class MemoryMapIterator @@ -31,7 +58,7 @@ class MemoryMapIterator } private: - usize m_mmap_entries; + const usize m_mmap_entries; const MMapEnt* m_base_ent; usize m_cur_ent; }; \ No newline at end of file