lpool: use offset to next, avoids pointer alignment requirments
This commit is contained in:
+33
-17
@@ -3,6 +3,23 @@
|
||||
#include "lpool.h"
|
||||
|
||||
#define ALIGN_UP(n, alignment) (((n) + (__typeof__(n))(alignment) - 1) & ~((__typeof__(n))(alignment) - 1))
|
||||
#define NULL_BLOCK (0xffffffff)
|
||||
|
||||
static inline LPoolBlock *lpool_next(LPool *pool, LPoolBlock *block)
|
||||
{
|
||||
if (block->next == NULL_BLOCK) {
|
||||
return NULL;
|
||||
}
|
||||
return (LPoolBlock *)(pool->start + block->next);
|
||||
}
|
||||
|
||||
static inline uint32_t lpool_offset(LPool *pool, LPoolBlock *block)
|
||||
{
|
||||
if (block == NULL) {
|
||||
return NULL_BLOCK;
|
||||
}
|
||||
return (uint8_t *)block - pool->start;
|
||||
}
|
||||
|
||||
void lpool_init(LPool *pool, void *buffer, size_t buffer_size, uint32_t block_size, uint32_t align)
|
||||
{
|
||||
@@ -37,11 +54,11 @@ void lpool_init(LPool *pool, void *buffer, size_t buffer_size, uint32_t block_si
|
||||
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));
|
||||
uint32_t next = (i + 1) * stride;
|
||||
block->next = next;
|
||||
block = next;
|
||||
block = (LPoolBlock *)(first_addr + next);
|
||||
}
|
||||
block->next = NULL; // last element
|
||||
block->next = NULL_BLOCK; // last element
|
||||
}
|
||||
pool->block_size = stride;
|
||||
pool->align = align;
|
||||
@@ -51,11 +68,12 @@ void lpool_init(LPool *pool, void *buffer, size_t buffer_size, uint32_t block_si
|
||||
|
||||
void *lpool_alloc(LPool *pool)
|
||||
{
|
||||
if (!pool->free_list) {
|
||||
if (pool->free_list == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
LPoolBlock *block = pool->free_list;
|
||||
pool->free_list = block->next;
|
||||
LPoolBlock *next = lpool_next(pool, block);
|
||||
pool->free_list = next;
|
||||
|
||||
#ifdef DEBUG
|
||||
memset(block, 0xCB, pool->block_size);
|
||||
@@ -70,22 +88,21 @@ void lpool_free(LPool *pool, void *p)
|
||||
}
|
||||
LPoolBlock *block = p;
|
||||
#ifdef DEBUG
|
||||
assert(block >= pool->start && block < pool->end);
|
||||
assert((uint8_t *)p >= pool->start && (uint8_t *)p < 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) {
|
||||
for (LPoolBlock *f = pool->free_list; f; f = lpool_next(pool, f)) {
|
||||
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;
|
||||
block->next = lpool_offset(pool, pool->free_list);
|
||||
pool->free_list = block;
|
||||
}
|
||||
|
||||
void lpool_reset(LPool *pool)
|
||||
@@ -96,14 +113,13 @@ void lpool_reset(LPool *pool)
|
||||
uintptr_t first_addr = ALIGN_UP(start_addr, pool->align);
|
||||
|
||||
// build intrusive free list
|
||||
LPoolBlock *p = (LPoolBlock *)first_addr;
|
||||
LPoolBlock *block = (LPoolBlock *)first_addr;
|
||||
pool->free_list = block;
|
||||
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;
|
||||
uint32_t next = i * pool->block_size;
|
||||
block->next = next;
|
||||
block = (LPoolBlock *)(first_addr + next);
|
||||
}
|
||||
|
||||
p->next = NULL; // last element
|
||||
|
||||
pool->free_list = (LPoolBlock *)first_addr;
|
||||
block->next = NULL_BLOCK; // last element
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user