implement free

This commit is contained in:
Mathieu Maret 2019-04-15 23:09:09 +02:00
parent 698490800f
commit 3f258485b9
3 changed files with 33 additions and 0 deletions

View File

@ -145,3 +145,32 @@ void *malloc(size_t size)
list_add_head(slubEntry, newSlab); list_add_head(slubEntry, newSlab);
return allocFromSlab(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");
}

View File

@ -14,3 +14,4 @@ int allocInit(void);
int allocBookSlab(size_t size, int selfContained); int allocBookSlab(size_t size, int selfContained);
void *malloc(size_t size); void *malloc(size_t size);
void free(void* ptr);

View File

@ -46,6 +46,9 @@ static void testAlloc(void)
void *malloc1 = malloc(sizeof(void *)); void *malloc1 = malloc(sizeof(void *));
void *malloc2 = malloc(sizeof(void *)); void *malloc2 = malloc(sizeof(void *));
assert((char *)malloc2 == ((char *)malloc1 + 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)); assert(malloc(1024));
assert(malloc(1024)); assert(malloc(1024));