:: commit 3c562830d2708bf73a10f29f8aa034535e0fde76

mintsuki <mintsuki@protonmail.com> — 2020-09-30 20:51

parents: 5f00385847

pmm: Merge contiguous bootloader reclaimable entries

diff --git a/limine.bin b/limine.bin
index 79fd1a85..feb697a1 100644
Binary files a/limine.bin and b/limine.bin differ
diff --git a/stage2/mm/pmm.c b/stage2/mm/pmm.c
index 3ea2a44a..0a9a47d1 100644
--- a/stage2/mm/pmm.c
+++ b/stage2/mm/pmm.c
@@ -75,7 +75,7 @@ static int align_entry(uint64_t *base, uint64_t *length) {
 
 static void sanitise_entries(void) {
     for (size_t i = 0; i < memmap_entries; i++) {
-        if (memmap[i].type != 1)
+        if (memmap[i].type != MEMMAP_USABLE)
             continue;
 
         // Check if the entry overlaps other entries
@@ -133,6 +133,25 @@ static void sanitise_entries(void) {
         memmap[min_index] = memmap[p];
         memmap[p] = min_e;
     }
+
+    // Merge contiguous bootloader-reclaimable entries
+    for (size_t i = 0; i < memmap_entries - 1; i++) {
+        if (memmap[i].type != MEMMAP_BOOTLOADER_RECLAIMABLE)
+            continue;
+
+        if (memmap[i+1].type == MEMMAP_BOOTLOADER_RECLAIMABLE
+         && memmap[i+1].base == memmap[i].base + memmap[i].length) {
+            print("%u: Merging %X %X to %X %X\n", i, memmap[i].base, memmap[i].length, memmap[i+1].base, memmap[i].length + memmap[i+1].length);
+            memmap[i].length += memmap[i+1].length;
+
+            // Eradicate from memmap
+            for (size_t j = i+1; j < memmap_entries - 1; j++) {
+                memmap[j] = memmap[j+1];
+            }
+            memmap_entries--;
+            i--;
+        }
+    }
 }
 
 struct e820_entry_t *get_memmap(size_t *entries) {
@@ -202,6 +221,8 @@ void *ext_mem_alloc_aligned_type(size_t count, size_t alignment, uint32_t type)
         // Zero out allocated space
         memset(ret, 0, count);
 
+        sanitise_entries();
+
         return ret;
     }
 
tab: 248 wrap: offon