From 23d6c880279c7ceb61ca9f5e90ebf20e3eac3c51 Mon Sep 17 00:00:00 2001 From: Hendiadyoin1 Date: Fri, 12 Jan 2024 00:03:49 +0100 Subject: [PATCH] Kernel/MM: Don't allocate a temporary Vector when parsing the memory map Instead we can achieve the same by just using an optional. --- Kernel/Memory/MemoryManager.cpp | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/Kernel/Memory/MemoryManager.cpp b/Kernel/Memory/MemoryManager.cpp index a350acb1a8..fcf03b0c6d 100644 --- a/Kernel/Memory/MemoryManager.cpp +++ b/Kernel/Memory/MemoryManager.cpp @@ -295,8 +295,7 @@ UNMAP_AFTER_INIT void MemoryManager::parse_memory_map() PhysicalAddress upper; }; - Vector contiguous_physical_ranges; - + Optional last_contiguous_physical_range; for (auto* mmap = mmap_begin; mmap < mmap_end; mmap++) { // We have to copy these onto the stack, because we take a reference to these when printing them out, // and doing so on a packed struct field is UB. @@ -364,6 +363,8 @@ UNMAP_AFTER_INIT void MemoryManager::parse_memory_map() continue; } + // FIXME: This might have a nicer solution than slicing the ranges apart, + // to just put them back together when we dont find a used range in them for (PhysicalSize page_base = address; page_base <= (address + length); page_base += PAGE_SIZE) { auto addr = PhysicalAddress(page_base); @@ -372,24 +373,30 @@ UNMAP_AFTER_INIT void MemoryManager::parse_memory_map() for (auto& used_range : global_data.used_memory_ranges) { if (addr.get() >= used_range.start.get() && addr.get() <= used_range.end.get()) { should_skip = true; + page_base = used_range.end.get(); break; } } if (should_skip) continue; - if (contiguous_physical_ranges.is_empty() || contiguous_physical_ranges.last().upper.offset(PAGE_SIZE) != addr) { - contiguous_physical_ranges.append(ContiguousPhysicalVirtualRange { - .lower = addr, - .upper = addr, - }); + if (!last_contiguous_physical_range.has_value() || last_contiguous_physical_range->upper.offset(PAGE_SIZE) != addr) { + if (last_contiguous_physical_range.has_value()) { + auto range = last_contiguous_physical_range.release_value(); + // FIXME: OOM? + global_data.physical_regions.append(PhysicalRegion::try_create(range.lower, range.upper).release_nonnull()); + } + last_contiguous_physical_range = ContiguousPhysicalVirtualRange { .lower = addr, .upper = addr }; } else { - contiguous_physical_ranges.last().upper = addr; + last_contiguous_physical_range->upper = addr; } } } - for (auto& range : contiguous_physical_ranges) { + // FIXME: If this is ever false, theres a good chance that all physical memory is already spent + if (last_contiguous_physical_range.has_value()) { + auto range = last_contiguous_physical_range.release_value(); + // FIXME: OOM? global_data.physical_regions.append(PhysicalRegion::try_create(range.lower, range.upper).release_nonnull()); }