Initial
This commit is contained in:
+109
@@ -0,0 +1,109 @@
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include "lpool.h"
|
||||
|
||||
#define ALIGN_UP(n, alignment) (((n) + (__typeof__(n))(alignment) - 1) & ~((__typeof__(n))(alignment) - 1))
|
||||
|
||||
void lpool_init(LPool *pool, void *buffer, size_t buffer_size, uint32_t block_size, uint32_t align)
|
||||
{
|
||||
assert(align > 0);
|
||||
assert((align & (align - 1)) == 0); // power of 2
|
||||
|
||||
// enforce minimum for storing free list pointers
|
||||
|
||||
if (align < _Alignof(LPoolBlock)) {
|
||||
align = _Alignof(LPoolBlock);
|
||||
}
|
||||
|
||||
if (block_size < sizeof(LPoolBlock)) {
|
||||
block_size = sizeof(LPoolBlock);
|
||||
}
|
||||
// ensure initial alignment
|
||||
uint32_t stride = ALIGN_UP(block_size, align);
|
||||
|
||||
// align first usable block
|
||||
uintptr_t start_addr = (uintptr_t)buffer;
|
||||
uintptr_t first_addr = ALIGN_UP(start_addr, align);
|
||||
size_t padding = first_addr - start_addr;
|
||||
|
||||
if (padding + stride > buffer_size) {
|
||||
pool->capacity = 0;
|
||||
pool->free_list = NULL;
|
||||
} else {
|
||||
size_t usable = buffer_size - padding;
|
||||
pool->capacity = usable / stride;
|
||||
|
||||
// build intrusive free list
|
||||
LPoolBlock *block = (LPoolBlock *)first_addr;
|
||||
pool->free_list = block;
|
||||
for (uint32_t i = 0; i < pool->capacity - 1; i++) {
|
||||
LPoolBlock *next = __builtin_assume_aligned(&block->data[0] + stride, _Alignof(LPoolBlock));
|
||||
block->next = next;
|
||||
block = next;
|
||||
}
|
||||
block->next = NULL; // last element
|
||||
}
|
||||
pool->block_size = stride;
|
||||
pool->align = align;
|
||||
pool->start = buffer;
|
||||
pool->end = buffer + buffer_size;
|
||||
}
|
||||
|
||||
void *lpool_alloc(LPool *pool)
|
||||
{
|
||||
if (!pool->free_list) {
|
||||
return NULL;
|
||||
}
|
||||
LPoolBlock *block = pool->free_list;
|
||||
pool->free_list = block->next;
|
||||
|
||||
#ifdef DEBUG
|
||||
memset(block, 0xCB, pool->block_size);
|
||||
#endif
|
||||
return block->data;
|
||||
}
|
||||
|
||||
void lpool_free(LPool *pool, void *p)
|
||||
{
|
||||
if (p == NULL) {
|
||||
return;
|
||||
}
|
||||
LPoolBlock *block = p;
|
||||
#ifdef DEBUG
|
||||
assert(block >= pool->start && block < pool->end);
|
||||
assert(((uintptr_t)p - (uintptr_t)pool->start) % pool->block_size == 0);
|
||||
|
||||
// double-free detection
|
||||
for (LPoolBlock *f = pool->free_list; f; f = f->next) {
|
||||
if (f == p) {
|
||||
assert(0 && "Double free detected in pool!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
memset(p, 0xDD, pool->block_size);
|
||||
#endif
|
||||
// When not in use, store pointer to next free element inline in the block
|
||||
block->next = pool->free_list;
|
||||
pool->free_list = p;
|
||||
}
|
||||
|
||||
void lpool_reset(LPool *pool)
|
||||
{
|
||||
if (pool->capacity == 0) return;
|
||||
|
||||
uintptr_t start_addr = (uintptr_t)pool->start;
|
||||
uintptr_t first_addr = ALIGN_UP(start_addr, pool->align);
|
||||
|
||||
// build intrusive free list
|
||||
LPoolBlock *p = (LPoolBlock *)first_addr;
|
||||
for (size_t i = 0; i < pool->capacity - 1; ++i) {
|
||||
LPoolBlock *next = __builtin_assume_aligned(&p->data[0] + pool->block_size, _Alignof(LPoolBlock));
|
||||
p->next = next;
|
||||
p = next;
|
||||
}
|
||||
|
||||
p->next = NULL; // last element
|
||||
|
||||
pool->free_list = (LPoolBlock *)first_addr;
|
||||
}
|
||||
Reference in New Issue
Block a user