#ifndef FREELIST_H #define FREELIST_H #include #include #include /* Fiddle these around according to your need. Delete or check makefile */ #ifdef FREELIST_NOALIGN #define ALIGN(x) (x) #else // FREELIST_NOALIGN /* Align to nearest multiple of sizeof(void*) */ static inline size_t align_up(size_t n) { return (n + sizeof(void *) - 1) & ~(sizeof(void *) - 1); } #define ALIGN(x) (align_up(x)) #endif // FREELIST_NOALIGN typedef struct FreeListBlock { struct FreeListBlock *next; } FreeListBlock; typedef struct { uintptr_t start; uintptr_t end; FreeListBlock *free; size_t size; size_t allocated; } FreeList; int fl_init(FreeList *fl, uintptr_t start, uintptr_t end, size_t itemsize); int fl_free(FreeList *fl, void *ptr); int fl_is_managed(FreeList *fl, void *ptr); void *fl_alloc(FreeList *fl); size_t fl_check(FreeList *fl); size_t fl_allocated(FreeList *fl); size_t fl_available(FreeList *fl); size_t fl_capacity(FreeList *fl); float fl_utilization(FreeList *fl, size_t itemsize); #endif // FREELIST_H