139 lines
3.9 KiB
C
139 lines
3.9 KiB
C
#include "freelist.h"
|
|
|
|
/* Header block used to keep track of all the free blocks */
|
|
struct __attribute__((packed)) FreeListBlock {
|
|
uint16_t next_offset; /* May be 8, 16, 32, 64, whatever */
|
|
};
|
|
|
|
/* Align to nearest multiple of align */
|
|
static inline size_t align_up_to(size_t n, size_t align) {
|
|
return (n + align - 1) & ~(align - 1);
|
|
}
|
|
|
|
/* Convert pointer -> 1-based offset (0 means NULL) */
|
|
static inline uint32_t ptr_to_offset(FreeList *fl, void *ptr) {
|
|
if (!ptr)
|
|
return 0; // NULL maps to 0
|
|
uintptr_t diff = (uintptr_t)ptr - fl->start;
|
|
return (uint32_t)(diff / fl->size) + 1;
|
|
}
|
|
|
|
/* Convert 1-based offset -> pointer (0 means NULL) */
|
|
static inline void *offset_to_ptr(FreeList *fl, uint32_t offset) {
|
|
if (offset == 0)
|
|
return NULL; // 0 = invalid/null
|
|
return (void *)(fl->start + (uintptr_t)(offset - 1) * fl->size);
|
|
}
|
|
|
|
/* Initialize the FreeList */
|
|
int fl_init(FreeList *fl, uintptr_t start, size_t size_bytes, size_t itemsize) {
|
|
/* Fiddle around according to your need, (void *) alignment seems to be enough,
|
|
* but MAX_ALIGN_T is also an option. Especially for allocator implementation. */
|
|
size_t size = align_up_to(itemsize, sizeof(void *));
|
|
|
|
if (size < sizeof(FreeListBlock) || !fl)
|
|
return 0;
|
|
|
|
fl->start = start;
|
|
fl->end = start + size_bytes;
|
|
fl->size = size;
|
|
fl->allocated = 0;
|
|
|
|
FreeListBlock *block = (FreeListBlock *)start;
|
|
|
|
for (size_t offset = 1; offset < fl_capacity(fl) + 1; offset++) {
|
|
block->next_offset = (int32_t)offset;
|
|
block = offset_to_ptr(fl, offset);
|
|
}
|
|
|
|
block->next_offset = 0; /* Last block */
|
|
fl->free = (FreeListBlock *)start;
|
|
|
|
return 1;
|
|
}
|
|
|
|
/* Allocate some memory from the FreeList */
|
|
void *fl_alloc(FreeList *fl) {
|
|
if (!fl->free)
|
|
return NULL;
|
|
|
|
FreeListBlock *m = fl->free;
|
|
|
|
fl->free = offset_to_ptr(fl, fl->free->next_offset); /* May be null, which is fine */
|
|
fl->allocated++;
|
|
|
|
/* Wipe it before sending it out, could use memset
|
|
* here, or even wiping the entire block */
|
|
m->next_offset = 0;
|
|
|
|
/* For reference: */
|
|
// memset((void *)m, 0, fl->size); /* Wipe entire block */
|
|
// memset((void *)m, 0, sizeof(FreeListBlock)); /* Wipe only header */
|
|
|
|
return ((void *)m);
|
|
}
|
|
|
|
/* Return some memory to the FreeList */
|
|
int fl_free(FreeList *fl, void *ptr) {
|
|
if (!fl_is_managed(fl, ptr))
|
|
return 0; /* We cant free memory we do not own */
|
|
|
|
FreeListBlock *block = (FreeListBlock *)ptr;
|
|
|
|
block->next_offset = ptr_to_offset(fl, fl->free); /* May be null, which is fine */
|
|
fl->free = block;
|
|
fl->allocated--;
|
|
|
|
return 1;
|
|
}
|
|
|
|
/* Returns how many slots are occupied */
|
|
size_t fl_allocated(FreeList *fl) {
|
|
return fl->allocated;
|
|
}
|
|
|
|
/* Returns how many free slots are available (O(1)) */
|
|
size_t fl_available(FreeList *fl) {
|
|
return fl_capacity(fl) - fl->allocated;
|
|
}
|
|
|
|
/* Returns the total amount of items the freelist will hold */
|
|
size_t fl_capacity(FreeList *fl) {
|
|
return (fl->end - fl->start) / fl->size;
|
|
}
|
|
|
|
/* Check if a piece of memory is managed by the FreeList */
|
|
int fl_is_managed(FreeList *fl, void *ptr) {
|
|
uintptr_t p = (uintptr_t)ptr;
|
|
|
|
if (p < fl->start || p >= fl->end) {
|
|
return 0; // outside pool
|
|
}
|
|
|
|
return ((p - fl->start) % fl->size) == 0; // aligned to block
|
|
}
|
|
|
|
/* Returns the ratio of metadata versus data as a scalar in range 0..1 */
|
|
float fl_utilization(FreeList *fl, size_t itemsize) {
|
|
return (float)itemsize / fl->size;
|
|
}
|
|
|
|
/* Walks the free pages/slots, returns total count (O(n), given no cycles) */
|
|
size_t fl_check(FreeList *fl) {
|
|
int avail = 0;
|
|
FreeListBlock *cursor = fl->free;
|
|
|
|
while (cursor) {
|
|
avail++;
|
|
|
|
if (!fl_is_managed(fl, cursor) || avail > fl_capacity(fl))
|
|
return 0;
|
|
|
|
if (cursor->next_offset == 0)
|
|
break;
|
|
|
|
cursor = offset_to_ptr(fl, cursor->next_offset);
|
|
}
|
|
|
|
return avail;
|
|
}
|