diff --git a/freelist/freelist.c b/freelist/freelist.c index 7fd946e..380010f 100644 --- a/freelist/freelist.c +++ b/freelist/freelist.c @@ -4,10 +4,11 @@ #include #include #include +#include /* Initialize the FreeList */ int fl_init(FreeList *fl, uintptr_t start, uintptr_t end, size_t itemsize) { - size_t size = ALIGN(itemsize + sizeof(FreeListBlock)); + size_t size = ALIGN(itemsize); if (!fl || end <= start) return EXIT_FAILURE; @@ -39,7 +40,8 @@ void *fl_alloc(FreeList *fl) { fl->free = fl->free->next; fl->allocated++; - return ((void *)m) + sizeof(FreeListBlock); + memset((void *)m, 0, sizeof(FreeListBlock)); + return ((void *)m); } /* Return some memory to the FreeList */ @@ -48,7 +50,7 @@ int fl_free(FreeList *fl, void *ptr) { return EXIT_FAILURE; /* We cant free memory we do not own */ } - FreeListBlock *block = (FreeListBlock *)((uintptr_t)ptr - sizeof(FreeListBlock)); + FreeListBlock *block = (FreeListBlock *)(uintptr_t)ptr; block->next = fl->free; fl->free = block;