From 3b77ba6b04b4fb30343581b8b89170c46a3060b9 Mon Sep 17 00:00:00 2001 From: apio Date: Mon, 5 Dec 2022 12:49:01 +0100 Subject: [PATCH] EVERYTHING IS CONSTANT IN THIS UNIVERSE Here's one advantage of Rust over C++: Immutability by default. In Rust, you have to mark a variable as mutable, whereas in C++ you have to mark it as immutable. What's the problem here? Usually, most variables don't need mutability. Thus we end up with const all over the place. --- kernel/src/memory/MemoryManager.cpp | 32 +++++++++++++---------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/kernel/src/memory/MemoryManager.cpp b/kernel/src/memory/MemoryManager.cpp index 2a91c5d4..768997f4 100644 --- a/kernel/src/memory/MemoryManager.cpp +++ b/kernel/src/memory/MemoryManager.cpp @@ -9,10 +9,10 @@ #include #include -extern u8 start_of_kernel_rodata[1]; -extern u8 end_of_kernel_rodata[1]; -extern u8 start_of_kernel_data[1]; -extern u8 end_of_kernel_data[1]; +extern const u8 start_of_kernel_rodata[1]; +extern const u8 end_of_kernel_rodata[1]; +extern const u8 start_of_kernel_data[1]; +extern const u8 end_of_kernel_data[1]; static u64 free_mem = 0; static u64 used_mem = 0; @@ -27,7 +27,7 @@ static Bitmap g_frame_bitmap; static usize get_physical_address_space_size() { MemoryMapIterator iter; - MemoryMapEntry entry = iter.highest(); + 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. @@ -53,27 +53,23 @@ namespace MemoryManager MemoryMapIterator iter; MemoryMapEntry entry; - auto largest_free = iter.largest_free(); + const auto largest_free = iter.largest_free(); - expect(largest_free.free, "We were given a largest free block that isn't even free!"); + expect(largest_free.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 // usable addresses will be scattered across it. - usize physical_address_space_size = get_physical_address_space_size(); + 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* frame_bitmap_addr = (char*)largest_free.ptr; + char* const frame_bitmap_addr = (char*)largest_free.ptr; - usize frame_bitmap_size = get_blocks_from_size(physical_address_space_size / ARCH_PAGE_SIZE, 8UL); + 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. - if (frame_bitmap_size >= largest_free.size) [[unlikely]] - { - kerrorln("ERROR: No single memory block is enough to hold the frame bitmap"); - CPU::efficient_halt(); - } + 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); @@ -82,8 +78,8 @@ namespace MemoryManager iter.rewind(); while (iter.next().try_set_value(entry)) { - u64 index = entry.ptr / ARCH_PAGE_SIZE; - u64 pages = entry.size / ARCH_PAGE_SIZE; + const u64 index = entry.ptr / ARCH_PAGE_SIZE; + const u64 pages = entry.size / ARCH_PAGE_SIZE; if (!entry.free) { reserved_mem += entry.size; } else { @@ -104,7 +100,7 @@ namespace MemoryManager void lock_frame(u64 frame) { - const u64 index = ((u64)frame) / ARCH_PAGE_SIZE; + const u64 index = frame / ARCH_PAGE_SIZE; if (g_frame_bitmap.get(index)) return; g_frame_bitmap.set(index, true); used_mem += ARCH_PAGE_SIZE;