implement free
This commit is contained in:
parent
698490800f
commit
3f258485b9
29
core/alloc.c
29
core/alloc.c
@ -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");
|
||||||
|
}
|
||||||
|
@ -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);
|
||||||
|
@ -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));
|
||||||
|
Loading…
Reference in New Issue
Block a user