:: commit 2052d92b919bf38bb22de485987cb0d1b64de2d0

mintsuki <mintsuki@protonmail.com> — 2020-09-02 03:33

parents: 46f46ddd15

Implement ext_mem_balloc()

diff --git a/limine.bin b/limine.bin
index d36a50a5..7b95fbce 100644
Binary files a/limine.bin and b/limine.bin differ
diff --git a/src/lib/blib.c b/src/lib/blib.c
index 35ea11a8..3165da38 100644
--- a/src/lib/blib.c
+++ b/src/lib/blib.c
@@ -59,11 +59,7 @@ void *balloc(size_t count) {
 
 // Only power of 2 alignments
 void *balloc_aligned(size_t count, size_t alignment) {
-    size_t new_base = bump_allocator_base;
-    if (new_base & (alignment - 1)) {
-        new_base &= ~(alignment - 1);
-        new_base += alignment;
-    }
+    size_t new_base = ALIGN_UP(bump_allocator_base, alignment);
     void *ret = (void *)new_base;
     new_base += count;
     if (new_base >= BUMP_ALLOCATOR_LIMIT)
diff --git a/src/lib/memmap.c b/src/lib/memmap.c
index 4ab1d69c..1cd14e3d 100644
--- a/src/lib/memmap.c
+++ b/src/lib/memmap.c
@@ -165,6 +165,34 @@ void init_memmap(void) {
     sanitise_entries();
 }
 
+static size_t ext_mem_balloc_base = 0x100000;
+
+void *ext_mem_balloc(size_t count) {
+    ext_mem_balloc_aligned(count, 4);
+}
+
+// TODO: this basically only works for the 1st extended memory entry in the
+//       memmap and allocates until the first hole following it. Fix that.
+void *ext_mem_balloc_aligned(size_t count, size_t alignment) {
+    uint64_t base = ALIGN_UP(ext_mem_balloc_base, alignment);
+    uint64_t top  = base + count;
+
+    for (size_t i = 0; i < memmap_entries; i++) {
+        if (memmap[i].type != 1)
+            continue;
+
+        uint64_t entry_base = memmap[i].base;
+        uint64_t entry_top  = memmap[i].base + memmap[i].length;
+        if (base >= entry_base && base < entry_top &&
+            top  >= entry_base && top  < entry_top) {
+            ext_mem_balloc_base = base + count;
+            return (void *)base;
+        }
+    }
+
+    panic("High memory allocator: Out of memory");
+}
+
 void memmap_alloc_range(uint64_t base, uint64_t length, uint32_t type) {
     uint64_t top = base + length;
 
diff --git a/src/lib/memmap.h b/src/lib/memmap.h
index da587d5d..9e252614 100644
--- a/src/lib/memmap.h
+++ b/src/lib/memmap.h
@@ -5,6 +5,8 @@
 #include <lib/e820.h>
 
 void init_memmap(void);
+void *ext_mem_balloc(size_t count);
+void *ext_mem_balloc_aligned(size_t count, size_t alignment);
 void memmap_alloc_range(uint64_t base, uint64_t length, uint32_t type);
 struct e820_entry_t *get_memmap(size_t *entries);
 void print_memmap(struct e820_entry_t *mm, size_t size);
tab: 248 wrap: offon