Clean up lpool
This commit is contained in:
+25
-21
@@ -36,34 +36,34 @@ void lpool_init(LPool *pool, void *buffer, size_t buffer_size, uint32_t block_si
|
||||
block_size = sizeof(LPoolBlock);
|
||||
}
|
||||
// ensure initial alignment
|
||||
uint32_t stride = ALIGN_UP(block_size, align);
|
||||
block_size = 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;
|
||||
uintptr_t buffer_addr = (uintptr_t)buffer;
|
||||
uintptr_t first_addr = ALIGN_UP(buffer_addr, align);
|
||||
size_t padding = first_addr - buffer_addr;
|
||||
|
||||
if (padding + stride > buffer_size) {
|
||||
if (padding + block_size > buffer_size) {
|
||||
pool->capacity = 0;
|
||||
pool->free_list = NULL;
|
||||
} else {
|
||||
size_t usable = buffer_size - padding;
|
||||
pool->capacity = usable / stride;
|
||||
pool->capacity = usable / block_size;
|
||||
|
||||
// build intrusive free list
|
||||
LPoolBlock *block = (LPoolBlock *)first_addr;
|
||||
pool->free_list = block;
|
||||
for (uint32_t i = 0; i < pool->capacity - 1; i++) {
|
||||
uint32_t next = (i + 1) * stride;
|
||||
for (uint32_t i = 1; i < pool->capacity; i++) {
|
||||
uint32_t next = i * block_size;
|
||||
block->next = next;
|
||||
block = (LPoolBlock *)(first_addr + next);
|
||||
}
|
||||
block->next = NULL_BLOCK; // last element
|
||||
}
|
||||
pool->block_size = stride;
|
||||
pool->block_size = block_size;
|
||||
pool->align = align;
|
||||
pool->start = buffer;
|
||||
pool->end = buffer + buffer_size;
|
||||
pool->used = 0;
|
||||
}
|
||||
|
||||
void *lpool_alloc(LPool *pool)
|
||||
@@ -78,48 +78,52 @@ void *lpool_alloc(LPool *pool)
|
||||
#ifdef DEBUG
|
||||
memset(block, 0xCB, pool->block_size);
|
||||
#endif
|
||||
pool->used++;
|
||||
return block->data;
|
||||
}
|
||||
|
||||
void lpool_free(LPool *pool, void *p)
|
||||
void lpool_free(LPool *pool, void *ptr)
|
||||
{
|
||||
if (p == NULL) {
|
||||
if (ptr == NULL) {
|
||||
return;
|
||||
}
|
||||
LPoolBlock *block = p;
|
||||
#ifdef DEBUG
|
||||
assert((uint8_t *)p >= pool->start && (uint8_t *)p < pool->end);
|
||||
assert(((uintptr_t)p - (uintptr_t)pool->start) % pool->block_size == 0);
|
||||
assert(pool->used > 0);
|
||||
uint8_t *end = pool->start + (pool->capacity * pool->block_size);
|
||||
assert((uint8_t *)ptr >= pool->start && (uint8_t *)ptr < end);
|
||||
assert((((uintptr_t)ptr - (uintptr_t)pool->start) & (pool->block_size - 1)) == 0);
|
||||
|
||||
// double-free detection
|
||||
for (LPoolBlock *f = pool->free_list; f; f = lpool_next(pool, f)) {
|
||||
if (f == p) {
|
||||
for (LPoolBlock *block = pool->free_list; block != NULL; block = lpool_next(pool, block)) {
|
||||
if (block == ptr) {
|
||||
assert(0 && "Double free detected in pool!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
memset(p, 0xDD, pool->block_size);
|
||||
memset(ptr, 0xDD, pool->block_size);
|
||||
#endif
|
||||
LPoolBlock *block = ptr;
|
||||
// When not in use, store pointer to next free element inline in the block
|
||||
block->next = lpool_offset(pool, pool->free_list);
|
||||
pool->free_list = block;
|
||||
pool->used--;
|
||||
}
|
||||
|
||||
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);
|
||||
uintptr_t first_addr = (uintptr_t)pool->start;
|
||||
|
||||
// build intrusive free list
|
||||
LPoolBlock *block = (LPoolBlock *)first_addr;
|
||||
pool->free_list = block;
|
||||
for (size_t i = 0; i < pool->capacity - 1; ++i) {
|
||||
for (size_t i = 1; i < pool->capacity; ++i) {
|
||||
uint32_t next = i * pool->block_size;
|
||||
block->next = next;
|
||||
block = (LPoolBlock *)(first_addr + next);
|
||||
}
|
||||
|
||||
block->next = NULL_BLOCK; // last element
|
||||
pool->used = 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user