lpool: use offset to next, avoids pointer alignment requirments
This commit is contained in:
+3
-3
@@ -9,7 +9,7 @@ extern "C" {
|
|||||||
|
|
||||||
typedef union LPoolBlock LPoolBlock;
|
typedef union LPoolBlock LPoolBlock;
|
||||||
union LPoolBlock {
|
union LPoolBlock {
|
||||||
LPoolBlock *next; // next free block (when not allocated)
|
uint32_t next; // offset from start to next free block (when not allocated)
|
||||||
unsigned char data[0]; // data when allocated
|
unsigned char data[0]; // data when allocated
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -30,8 +30,8 @@ union LPoolBlock {
|
|||||||
typedef struct LPool LPool;
|
typedef struct LPool LPool;
|
||||||
struct LPool {
|
struct LPool {
|
||||||
LPoolBlock *free_list; // free list
|
LPoolBlock *free_list; // free list
|
||||||
LPoolBlock *start; // start of user buffer
|
uint8_t *start; // start of user buffer
|
||||||
LPoolBlock *end; // one past end of user buffer
|
uint8_t *end; // one past end of user buffer
|
||||||
uint32_t block_size; // aligned size of each block
|
uint32_t block_size; // aligned size of each block
|
||||||
uint32_t capacity; // number of blocks
|
uint32_t capacity; // number of blocks
|
||||||
uint32_t align; // alignment
|
uint32_t align; // alignment
|
||||||
|
|||||||
+33
-17
@@ -3,6 +3,23 @@
|
|||||||
#include "lpool.h"
|
#include "lpool.h"
|
||||||
|
|
||||||
#define ALIGN_UP(n, alignment) (((n) + (__typeof__(n))(alignment) - 1) & ~((__typeof__(n))(alignment) - 1))
|
#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)
|
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;
|
LPoolBlock *block = (LPoolBlock *)first_addr;
|
||||||
pool->free_list = block;
|
pool->free_list = block;
|
||||||
for (uint32_t i = 0; i < pool->capacity - 1; i++) {
|
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 = next;
|
||||||
block = next;
|
block = (LPoolBlock *)(first_addr + next);
|
||||||
}
|
}
|
||||||
block->next = NULL; // last element
|
block->next = NULL_BLOCK; // last element
|
||||||
}
|
}
|
||||||
pool->block_size = stride;
|
pool->block_size = stride;
|
||||||
pool->align = align;
|
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)
|
void *lpool_alloc(LPool *pool)
|
||||||
{
|
{
|
||||||
if (!pool->free_list) {
|
if (pool->free_list == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
LPoolBlock *block = pool->free_list;
|
LPoolBlock *block = pool->free_list;
|
||||||
pool->free_list = block->next;
|
LPoolBlock *next = lpool_next(pool, block);
|
||||||
|
pool->free_list = next;
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
memset(block, 0xCB, pool->block_size);
|
memset(block, 0xCB, pool->block_size);
|
||||||
@@ -70,22 +88,21 @@ void lpool_free(LPool *pool, void *p)
|
|||||||
}
|
}
|
||||||
LPoolBlock *block = p;
|
LPoolBlock *block = p;
|
||||||
#ifdef DEBUG
|
#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);
|
assert(((uintptr_t)p - (uintptr_t)pool->start) % pool->block_size == 0);
|
||||||
|
|
||||||
// double-free detection
|
// 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) {
|
if (f == p) {
|
||||||
assert(0 && "Double free detected in pool!");
|
assert(0 && "Double free detected in pool!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(p, 0xDD, pool->block_size);
|
memset(p, 0xDD, pool->block_size);
|
||||||
#endif
|
#endif
|
||||||
// When not in use, store pointer to next free element inline in the block
|
// When not in use, store pointer to next free element inline in the block
|
||||||
block->next = pool->free_list;
|
block->next = lpool_offset(pool, pool->free_list);
|
||||||
pool->free_list = p;
|
pool->free_list = block;
|
||||||
}
|
}
|
||||||
|
|
||||||
void lpool_reset(LPool *pool)
|
void lpool_reset(LPool *pool)
|
||||||
@@ -96,14 +113,13 @@ void lpool_reset(LPool *pool)
|
|||||||
uintptr_t first_addr = ALIGN_UP(start_addr, pool->align);
|
uintptr_t first_addr = ALIGN_UP(start_addr, pool->align);
|
||||||
|
|
||||||
// build intrusive free list
|
// 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) {
|
for (size_t i = 0; i < pool->capacity - 1; ++i) {
|
||||||
LPoolBlock *next = __builtin_assume_aligned(&p->data[0] + pool->block_size, _Alignof(LPoolBlock));
|
uint32_t next = i * pool->block_size;
|
||||||
p->next = next;
|
block->next = next;
|
||||||
p = next;
|
block = (LPoolBlock *)(first_addr + next);
|
||||||
}
|
}
|
||||||
|
|
||||||
p->next = NULL; // last element
|
block->next = NULL_BLOCK; // last element
|
||||||
|
|
||||||
pool->free_list = (LPoolBlock *)first_addr;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ int main(int argc, char *argv[])
|
|||||||
printf("Total allocs = %zu\n", allocs);
|
printf("Total allocs = %zu\n", allocs);
|
||||||
printf("alloc succeded = %d\n", lpool_alloc(&pool) != NULL);
|
printf("alloc succeded = %d\n", lpool_alloc(&pool) != NULL);
|
||||||
lpool_free(&pool, data);
|
lpool_free(&pool, data);
|
||||||
|
|
||||||
printf("alloc succeded = %d\n", lpool_alloc(&pool) != NULL);
|
printf("alloc succeded = %d\n", lpool_alloc(&pool) != NULL);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user