diff --git a/core/alloc.c b/core/alloc.c index 1b1c3a9..1eeee22 100644 --- a/core/alloc.c +++ b/core/alloc.c @@ -145,3 +145,32 @@ void *malloc(size_t size) list_add_head(slubEntry, newSlab); return allocFromSlab(newSlab); } + +int slabFree(void *ptr, struct slabDesc *slab){ + struct slabDesc *slabEntry; + int slabIdx; + list_foreach(slab, slabEntry, slabIdx) + { + if ((slabEntry->page <= (vaddr_t)ptr) && + ((vaddr_t)ptr < (slabEntry->page + PAGE_SIZE))){ + pr_devel("free place! was %d is now %d\n", slabEntry->freeEl, ptr); + *((vaddr_t *)ptr) = (vaddr_t)slabEntry->freeEl; + slabEntry->freeEl = ptr; + slabEntry->full = 0; + return 1; + } + } + return 0; +} +void free(void *ptr){ + if(!ptr) + return; + + struct slabDesc *slab; + int slabIdx; + list_foreach(slub, slab, slabIdx){ + if(slabFree(ptr, slab)) + return; + } + pr_devel("free: slab not found\n"); +} diff --git a/core/alloc.h b/core/alloc.h index ee6ad3c..f339a9d 100644 --- a/core/alloc.h +++ b/core/alloc.h @@ -14,3 +14,4 @@ int allocInit(void); int allocBookSlab(size_t size, int selfContained); void *malloc(size_t size); +void free(void* ptr); diff --git a/tests/test.c b/tests/test.c index 7e27989..8bd13e9 100644 --- a/tests/test.c +++ b/tests/test.c @@ -46,6 +46,9 @@ static void testAlloc(void) void *malloc1 = malloc(sizeof(void *)); void *malloc2 = malloc(sizeof(void *)); assert((char *)malloc2 == ((char *)malloc1 + sizeof(void *))); + free(malloc2); + void *malloc3 = malloc(sizeof(void *)); + assertmsg((char *)malloc2 == (char *)malloc3, " %d %d\n", malloc2, malloc3); assert(malloc(1024)); assert(malloc(1024)); assert(malloc(1024));