From 5cf5edd58985c44f265f29b4f2bfda290618d6d5 Mon Sep 17 00:00:00 2001 From: Nathan Giddings Date: Fri, 3 Mar 2023 04:55:31 -0600 Subject: [PATCH] Rewrote heap.c to use libmalloc --- .gitignore | 0 README.md | 0 src/heap.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++-- src/x86/entry.S | 0 4 files changed, 73 insertions(+), 3 deletions(-) mode change 100755 => 100644 .gitignore mode change 100755 => 100644 README.md mode change 100755 => 100644 src/x86/entry.S diff --git a/.gitignore b/.gitignore old mode 100755 new mode 100644 diff --git a/README.md b/README.md old mode 100755 new mode 100644 diff --git a/src/heap.c b/src/heap.c index e194a60..1f28aa0 100644 --- a/src/heap.c +++ b/src/heap.c @@ -1,9 +1,13 @@ #include +#include #include "heap.h" #include "mmgr.h" #include "math.h" +#include "stdio.h" #include "types/status.h" +/* + #define AVAIL 0 #define UNAVAIL 1 #define ALLOCATED 2 @@ -83,6 +87,7 @@ int heap_contruct(struct heap_t *heap, void *base, void *start, size_t heap_size size_t header_size = (heap_size / block_size) << 1; for(size_t i = 1; i <= (heap_size / block_size) * 2; i++) { + //printf("%i\n",i); int flags = page_type((void*) heap->header + i); if((flags & PAGE_PRESENT) == 0) { @@ -146,17 +151,82 @@ void heap_free(struct heap_t *heap, void *ptr) } } +*/ + +bitmap_heap_descriptor_t system_heap; + +static int map_block(void *location, unsigned long size) +{ + for(int n = 0; n < size; n += page_size) + { + if(!(page_type(location + n) & PAGE_PRESENT)) + { + int status = map_page(location + n, reserve_page(), PAGE_RW); + if(status) + { + return status; + } + } + } + return ENONE; +} + +static int mmap_callback(void *location, unsigned long size) +{ + int status = ENONE; + for(unsigned long i = 0; i < size; i += page_size) + { + physaddr_t frame = reserve_page(); + if(frame == ENOMEM) + { + return ENOMEM; + } + else if((status = map_page(location + i, reserve_page(), PAGE_RW))) + { + return status; + } + } + return status; +} + int kminit(void *base, void* start, size_t heap_size, size_t block_size) { - return heap_contruct(&system_heap, base, start, heap_size, block_size); + static unsigned long heap_cache[16]; + system_heap.bitmap = NULL; + system_heap.cache = heap_cache; + system_heap.cache_capacity = 16; + system_heap.block_bits = 4; + system_heap.block_size = block_size; + system_heap.offset = (unsigned long)base; + memory_region_t map_array[8]; + memory_map_t map = { + .array = map_array, + .capacity = 8, + .size = 0 + }; + memmap_insert_region(&map, 0, heap_size, M_AVAILABLE); + memmap_insert_region(&map, 0, start - base, M_UNAVAILABLE); + return initialize_heap(&system_heap, &map, mmap_callback); } void *kmalloc(size_t size) { - return heap_allocate(&system_heap, size); + unsigned long loc = reserve_region(&system_heap, size); + if(loc == NOMEM) + { + return NULL; + } + else if(map_block((void*)loc, size)) + { + return NULL; + } + else + { + return (void*)loc; + } } void kfree(void *ptr) { - heap_free(&system_heap, ptr); + free_region(&system_heap, (unsigned long)ptr, 0); } diff --git a/src/x86/entry.S b/src/x86/entry.S old mode 100755 new mode 100644