From 0d10c98477422880f6909f07bfdec7f6258b976a Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 4 Dec 2022 15:50:21 +0100 Subject: [PATCH] SImplify init_physical_frame_allocator() even more --- kernel/src/memory/MemoryManager.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/kernel/src/memory/MemoryManager.cpp b/kernel/src/memory/MemoryManager.cpp index fcae5467..a2a03927 100644 --- a/kernel/src/memory/MemoryManager.cpp +++ b/kernel/src/memory/MemoryManager.cpp @@ -54,29 +54,26 @@ namespace MemoryManager void init_physical_frame_allocator() { - u64 total_mem = 0; - MemoryMapIterator iter; - MemoryMapEntry entry; - while (iter.next().try_set_value(entry)) { total_mem += entry.size; } auto largest_free = iter.largest_free(); - void* biggest_usable_memory_block = (void*)largest_free.ptr; - u64 biggest_usable_memory_block_size = largest_free.size; + expect(largest_free.free, "We were given a largest free block 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 // usable addresses will be scattered across it. usize physical_address_space_size = get_physical_address_space_size(); - char* frame_bitmap_addr = (char*)biggest_usable_memory_block; + // We store our frame bitmap at the beginning of the largest free memory block. + char* frame_bitmap_addr = (char*)largest_free.ptr; + usize frame_bitmap_size = physical_address_space_size / ARCH_PAGE_SIZE / 8 + 1; // 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. - if (frame_bitmap_size >= biggest_usable_memory_block_size) [[unlikely]] + if (frame_bitmap_size >= largest_free.size) [[unlikely]] { kerrorln("ERROR: No single memory block is enough to hold the frame bitmap"); CPU::efficient_halt();