Commit 3f258485 authored by Mathieu Maret's avatar Mathieu Maret

implement free

parent 69849080
......@@ -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");
}
......@@ -14,3 +14,4 @@ int allocInit(void);
int allocBookSlab(size_t size, int selfContained);
void *malloc(size_t size);
void free(void* ptr);
......@@ -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));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment