Implement ext_mem_balloc()

This commit is contained in:
mintsuki 2020-09-02 05:33:22 +02:00
parent 46f46ddd15
commit 2052d92b91
4 changed files with 31 additions and 5 deletions

Binary file not shown.

View File

@ -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)

View File

@ -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;

View File

@ -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);