Compare commits
9 Commits
ce0e998d2a
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 48dc3a1e6a | |||
| e5336056db | |||
| e306a9fed0 | |||
| 6185de0694 | |||
| d04b151279 | |||
| c89b4d1d3c | |||
| 0180ce47e5 | |||
| 24718b4956 | |||
| 7687ffa710 |
@@ -1,39 +1,38 @@
|
|||||||
CC = gcc
|
CC = gcc
|
||||||
LD = $(CC)
|
LD = $(CC)
|
||||||
CFLAGS = -Wall -Wextra -Wno-unused-parameter -ggdb
|
CFLAGS = -std=gnu17 -Wall -Wextra -Wno-unused-parameter -ggdb
|
||||||
CPPFLAGS += -Iinclude
|
CPPFLAGS += -Iinclude
|
||||||
LDFLAGS +=
|
LDFLAGS +=
|
||||||
ifndef DEBUG
|
ifndef DEBUG
|
||||||
CFLAGS += -O2
|
CFLAGS += -O2
|
||||||
CPPFLAGS += -DNDEBUG
|
CPPFLAGS += -DNDEBUG
|
||||||
else
|
else
|
||||||
CPPFLAGS += -DDEBUG
|
CPPFLAGS += -DDEBUG -fsanitize=address,undefined
|
||||||
|
LDFLAGS += -fsanitize=address,undefined
|
||||||
endif
|
endif
|
||||||
BUILDDIR = build
|
BUILDDIR = build
|
||||||
|
|
||||||
SOURCES = $(wildcard src/*.c)
|
SOURCES = $(wildcard src/*.c)
|
||||||
OBJECTS = $(addprefix $(BUILDDIR)/,$(SOURCES:.c=.o))
|
|
||||||
DEPS = $(addprefix $(BUILDDIR)/,$(SOURCES:.c=.d))
|
|
||||||
LIB = liblilalloc
|
|
||||||
TEST_SOURCES = $(wildcard test/*.c)
|
TEST_SOURCES = $(wildcard test/*.c)
|
||||||
|
OBJECTS = $(addprefix $(BUILDDIR)/,$(SOURCES:.c=.o))
|
||||||
|
DEPS = $(addprefix $(BUILDDIR)/,$(SOURCES:.c=.d)) $(addprefix $(BUILDDIR)/,$(TEST_SOURCES:.c=.d))
|
||||||
|
LIB = liblilalloc.a
|
||||||
TEST_BINS = $(patsubst test/%.c,$(BUILDDIR)/test/%,$(TEST_SOURCES))
|
TEST_BINS = $(patsubst test/%.c,$(BUILDDIR)/test/%,$(TEST_SOURCES))
|
||||||
|
|
||||||
.PHONY: all
|
.PHONY: all
|
||||||
all: $(BUILDDIR)/$(LIB).a
|
all: $(BUILDDIR)/$(LIB)
|
||||||
|
|
||||||
|
|
||||||
$(BUILDDIR)/%.o: %.c
|
$(BUILDDIR)/%.o: %.c Makefile
|
||||||
mkdir -p $(@D)
|
mkdir -p $(@D)
|
||||||
$(CC) $(CPPFLAGS) $(CFLAGS) -MMD -MP -c -o $@ $<
|
$(CC) $(CPPFLAGS) $(CFLAGS) -MMD -MP -c -o $@ $<
|
||||||
|
|
||||||
$(BUILDDIR)/$(LIB).a: $(OBJECTS)
|
$(BUILDDIR)/$(LIB): $(OBJECTS)
|
||||||
ar crs $@ $^
|
ar crs $@ $^
|
||||||
|
|
||||||
|
$(BUILDDIR)/test/%: test/%.c $(BUILDDIR)/$(LIB)
|
||||||
$(BUILDDIR)/test/%: test/%.c $(BUILDDIR)/$(LIB).a
|
|
||||||
mkdir -p $(@D)
|
mkdir -p $(@D)
|
||||||
$(CC) $(CPPFLAGS) $(CFLAGS) -MMD -MP -o $@ $< $(BUILDDIR)/$(LIB).a
|
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -MMD -MP -o $@ $< $(BUILDDIR)/$(LIB)
|
||||||
|
|
||||||
|
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
test: $(TEST_BINS)
|
test: $(TEST_BINS)
|
||||||
@@ -44,8 +43,6 @@ test: $(TEST_BINS)
|
|||||||
echo ; \
|
echo ; \
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(BUILDDIR)
|
rm -rf $(BUILDDIR)
|
||||||
|
|||||||
@@ -34,7 +34,6 @@ typedef struct LAlloc LAlloc;
|
|||||||
struct LAlloc {
|
struct LAlloc {
|
||||||
uint8_t *base; // backing store
|
uint8_t *base; // backing store
|
||||||
uint32_t size; // total size
|
uint32_t size; // total size
|
||||||
LBlockHeader *heap_start; // first block
|
|
||||||
LBlockHeader *next_alloc; // start of free block search
|
LBlockHeader *next_alloc; // start of free block search
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+9
-3
@@ -94,9 +94,14 @@ struct LArenaTemp {
|
|||||||
* struct foo *foo = LARENA_ALLOC_TYPE(&my_allocator, struct foo);
|
* struct foo *foo = LARENA_ALLOC_TYPE(&my_allocator, struct foo);
|
||||||
* // Array types can be used too:
|
* // Array types can be used too:
|
||||||
* struct Foo *str = LARENA_ALLOC_TYPE(&my_allocator, struct Foo[32]);
|
* struct Foo *str = LARENA_ALLOC_TYPE(&my_allocator, struct Foo[32]);
|
||||||
|
* // For arrays with runtime determined size, use:
|
||||||
|
* LARENA_ALLOC_ARRAY(&my_allocator, struct Foo, cnt_foos);
|
||||||
* @endcode
|
* @endcode
|
||||||
*/
|
*/
|
||||||
#define LARENA_ALLOC_TYPE(allocator, type_) larena_alloc_aligned((allocator), sizeof(type_), _Alignof(type_))
|
#define LARENA_ALLOC_TYPE(allocator, type_) larena_alloc_aligned((allocator), sizeof(type_), _Alignof(type_))
|
||||||
|
|
||||||
|
#define LARENA_ALLOC_ARRAY(allocator, type_, nr_elements_) larena_alloc_aligned((allocator), (nr_elements_) * sizeof(type_), _Alignof(type_))
|
||||||
|
|
||||||
// Arena size in bytes
|
// Arena size in bytes
|
||||||
static inline size_t larena_size(const LArena *arena)
|
static inline size_t larena_size(const LArena *arena)
|
||||||
{
|
{
|
||||||
@@ -152,16 +157,17 @@ static inline void *larena_alloc(LArena *arena, size_t sz)
|
|||||||
|
|
||||||
// Allocate @sz bytes from the LArena, returned pointer is aligned to @align
|
// Allocate @sz bytes from the LArena, returned pointer is aligned to @align
|
||||||
// Returns NULL if not enough space
|
// Returns NULL if not enough space
|
||||||
static inline void *larena_alloc_aligned(LArena *arena, size_t sz, unsigned int align) [[gnu::alloc_align(3)]]
|
[[gnu::alloc_align(3)]]
|
||||||
|
static inline void *larena_alloc_aligned(LArena *arena, size_t sz, unsigned int align)
|
||||||
{
|
{
|
||||||
assert(align != 0);
|
assert(align != 0);
|
||||||
assert((align & (align - 1)) == 0); // power of 2
|
assert((align & (align - 1)) == 0); // power of 2
|
||||||
|
|
||||||
uintptr_t curr = (uintptr_t)arena->curr;
|
uintptr_t curr = (uintptr_t)arena->;
|
||||||
uintptr_t aligned = (curr + (align - (uintptr_t)1)) & ~(align - (uintptr_t)1);
|
uintptr_t aligned = (curr + (align - (uintptr_t)1)) & ~(align - (uintptr_t)1);
|
||||||
uint8_t *start = (uint8_t *)aligned;
|
uint8_t *start = (uint8_t *)aligned;
|
||||||
|
|
||||||
if (sz > (larena_available(arena))) {
|
if (sz > (size_t)(arena->end - start)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-4
@@ -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,10 +30,10 @@ 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
|
|
||||||
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; // total number of blocks
|
||||||
|
uint32_t used; // used number of blocks
|
||||||
uint32_t align; // alignment
|
uint32_t align; // alignment
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+4
-6
@@ -70,7 +70,6 @@ static void lmaybe_split_block(LAlloc *allocator, LBlockHeader *block, uint32_t
|
|||||||
|
|
||||||
lblock_set_size(block, size);
|
lblock_set_size(block, size);
|
||||||
lblock_set_used(block);
|
lblock_set_used(block);
|
||||||
|
|
||||||
new_block->size_and_flags = new_size | BLOCK_FREE;
|
new_block->size_and_flags = new_size | BLOCK_FREE;
|
||||||
new_block->next = block->next;
|
new_block->next = block->next;
|
||||||
new_block->prev = lbh_offset(allocator, block);
|
new_block->prev = lbh_offset(allocator, block);
|
||||||
@@ -116,7 +115,6 @@ void lalloc_init(LAlloc *allocator, void *backing_store, uint32_t size)
|
|||||||
first->next = NULL_BLOCK;
|
first->next = NULL_BLOCK;
|
||||||
first->prev = NULL_BLOCK;
|
first->prev = NULL_BLOCK;
|
||||||
|
|
||||||
allocator->heap_start = first;
|
|
||||||
allocator->next_alloc = first;
|
allocator->next_alloc = first;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -127,7 +125,7 @@ void *lalloc(LAlloc *allocator, uint32_t size)
|
|||||||
}
|
}
|
||||||
size = ALIGN_UP(size);
|
size = ALIGN_UP(size);
|
||||||
|
|
||||||
LBlockHeader *start = allocator->next_alloc ? allocator->next_alloc : allocator->heap_start;
|
LBlockHeader *start = allocator->next_alloc ? allocator->next_alloc : (LBlockHeader *)allocator->base;
|
||||||
LBlockHeader *curr = start;
|
LBlockHeader *curr = start;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
@@ -135,11 +133,11 @@ void *lalloc(LAlloc *allocator, uint32_t size)
|
|||||||
lmaybe_split_block(allocator, curr, size);
|
lmaybe_split_block(allocator, curr, size);
|
||||||
lblock_set_used(curr);
|
lblock_set_used(curr);
|
||||||
LBlockHeader *next = lblock_from_offset(allocator, curr->next);
|
LBlockHeader *next = lblock_from_offset(allocator, curr->next);
|
||||||
allocator->next_alloc = next ? next : allocator->heap_start;
|
allocator->next_alloc = next ? next : (LBlockHeader *)allocator->base;
|
||||||
return (uint8_t *)curr + sizeof(LBlockHeader);
|
return (uint8_t *)curr + sizeof(LBlockHeader);
|
||||||
}
|
}
|
||||||
LBlockHeader *next = lblock_from_offset(allocator, curr->next);
|
LBlockHeader *next = lblock_from_offset(allocator, curr->next);
|
||||||
curr = next ? next : allocator->heap_start;
|
curr = next ? next : (LBlockHeader *)allocator->base;
|
||||||
} while (curr != start);
|
} while (curr != start);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -153,7 +151,7 @@ void lfree(LAlloc *allocator, void *ptr)
|
|||||||
|
|
||||||
LBlockHeader *block = (LBlockHeader *)((uint8_t *)ptr - sizeof(LBlockHeader));
|
LBlockHeader *block = (LBlockHeader *)((uint8_t *)ptr - sizeof(LBlockHeader));
|
||||||
|
|
||||||
assert((uint8_t *)ptr - sizeof(LBlockHeader) >= allocator->base && (uint8_t *)ptr < allocator->base + allocator->size);
|
assert((uint8_t *)block >= allocator->base && (uint8_t *)ptr < allocator->base + allocator->size);
|
||||||
// double free detection
|
// double free detection
|
||||||
assert(!lblock_is_free(block));
|
assert(!lblock_is_free(block));
|
||||||
|
|
||||||
|
|||||||
+59
-37
@@ -3,13 +3,30 @@
|
|||||||
#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)
|
||||||
{
|
{
|
||||||
assert(align > 0);
|
assert(align > 0);
|
||||||
assert((align & (align - 1)) == 0); // power of 2
|
assert((align & (align - 1)) == 0); // power of 2
|
||||||
|
|
||||||
// enforce minimum for storing free list pointers
|
// enforce minimum for storing free list offsets
|
||||||
|
|
||||||
if (align < _Alignof(LPoolBlock)) {
|
if (align < _Alignof(LPoolBlock)) {
|
||||||
align = _Alignof(LPoolBlock);
|
align = _Alignof(LPoolBlock);
|
||||||
@@ -19,91 +36,96 @@ void lpool_init(LPool *pool, void *buffer, size_t buffer_size, uint32_t block_si
|
|||||||
block_size = sizeof(LPoolBlock);
|
block_size = sizeof(LPoolBlock);
|
||||||
}
|
}
|
||||||
// ensure initial alignment
|
// ensure initial alignment
|
||||||
uint32_t stride = ALIGN_UP(block_size, align);
|
block_size = ALIGN_UP(block_size, align);
|
||||||
|
|
||||||
// align first usable block
|
// align first usable block
|
||||||
uintptr_t start_addr = (uintptr_t)buffer;
|
uintptr_t buffer_addr = (uintptr_t)buffer;
|
||||||
uintptr_t first_addr = ALIGN_UP(start_addr, align);
|
uintptr_t first_addr = ALIGN_UP(buffer_addr, align);
|
||||||
size_t padding = first_addr - start_addr;
|
size_t padding = first_addr - buffer_addr;
|
||||||
|
|
||||||
if (padding + stride > buffer_size) {
|
if (padding + block_size > buffer_size) {
|
||||||
pool->capacity = 0;
|
pool->capacity = 0;
|
||||||
pool->free_list = NULL;
|
pool->free_list = NULL;
|
||||||
} else {
|
} else {
|
||||||
size_t usable = buffer_size - padding;
|
size_t usable = buffer_size - padding;
|
||||||
pool->capacity = usable / stride;
|
pool->capacity = usable / block_size;
|
||||||
|
|
||||||
// build intrusive free list
|
// build intrusive free list
|
||||||
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 = 1; i < pool->capacity; i++) {
|
||||||
LPoolBlock *next = __builtin_assume_aligned(&block->data[0] + stride, _Alignof(LPoolBlock));
|
uint32_t next = i * block_size;
|
||||||
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 = block_size;
|
||||||
pool->align = align;
|
pool->align = align;
|
||||||
pool->start = buffer;
|
pool->start = buffer;
|
||||||
pool->end = buffer + buffer_size;
|
pool->used = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
#endif
|
#endif
|
||||||
|
pool->used++;
|
||||||
return block->data;
|
return block->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
void lpool_free(LPool *pool, void *p)
|
void lpool_free(LPool *pool, void *ptr)
|
||||||
{
|
{
|
||||||
if (p == NULL) {
|
if (ptr == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
LPoolBlock *block = p;
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
assert(block >= pool->start && block < pool->end);
|
assert(pool->used > 0);
|
||||||
assert(((uintptr_t)p - (uintptr_t)pool->start) % pool->block_size == 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
|
// double-free detection
|
||||||
for (LPoolBlock *f = pool->free_list; f; f = f->next) {
|
for (LPoolBlock *block = pool->free_list; block != NULL; block = lpool_next(pool, block)) {
|
||||||
if (f == p) {
|
if (block == ptr) {
|
||||||
assert(0 && "Double free detected in pool!");
|
assert(0 && "Double free detected in pool!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
memset(ptr, 0xDD, pool->block_size);
|
||||||
memset(p, 0xDD, pool->block_size);
|
|
||||||
#endif
|
#endif
|
||||||
|
LPoolBlock *block = ptr;
|
||||||
// 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;
|
||||||
|
pool->used--;
|
||||||
}
|
}
|
||||||
|
|
||||||
void lpool_reset(LPool *pool)
|
void lpool_reset(LPool *pool)
|
||||||
{
|
{
|
||||||
if (pool->capacity == 0) return;
|
if (pool->capacity == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
uintptr_t start_addr = (uintptr_t)pool->start;
|
uintptr_t first_addr = (uintptr_t)pool->start;
|
||||||
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;
|
||||||
for (size_t i = 0; i < pool->capacity - 1; ++i) {
|
pool->free_list = block;
|
||||||
LPoolBlock *next = __builtin_assume_aligned(&p->data[0] + pool->block_size, _Alignof(LPoolBlock));
|
for (size_t i = 1; i < pool->capacity; ++i) {
|
||||||
p->next = next;
|
uint32_t next = i * pool->block_size;
|
||||||
p = next;
|
block->next = next;
|
||||||
|
block = (LPoolBlock *)(first_addr + next);
|
||||||
}
|
}
|
||||||
|
|
||||||
p->next = NULL; // last element
|
block->next = NULL_BLOCK; // last element
|
||||||
|
pool->used = 0;
|
||||||
pool->free_list = (LPoolBlock *)first_addr;
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -7,7 +7,7 @@
|
|||||||
void debug_alloc(const LAlloc *allocator)
|
void debug_alloc(const LAlloc *allocator)
|
||||||
{
|
{
|
||||||
puts("--debug start--");
|
puts("--debug start--");
|
||||||
for (LBlockHeader *b = allocator->heap_start; b; b = lblock_from_offset(allocator, b->next)) {
|
for (LBlockHeader *b = (LBlockHeader *)allocator->base; b; b = lblock_from_offset(allocator, b->next)) {
|
||||||
printf("Block size %u free %d prev %u next %u \n", lblock_size(b), lblock_is_free(b), b->prev, b->next);
|
printf("Block size %u free %d prev %u next %u \n", lblock_size(b), lblock_is_free(b), b->prev, b->next);
|
||||||
}
|
}
|
||||||
puts("--debug end--");
|
puts("--debug end--");
|
||||||
|
|||||||
+18
-4
@@ -1,4 +1,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "lpool.h"
|
#include "lpool.h"
|
||||||
|
|
||||||
struct TestData {
|
struct TestData {
|
||||||
@@ -11,8 +13,10 @@ struct TestData {
|
|||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
_Alignas(16) uint8_t store[2048];
|
_Alignas(16) uint8_t store[2048];
|
||||||
|
// uint8_t *store = malloc(1024 * 1024 - 17);
|
||||||
|
|
||||||
LPool pool;
|
LPool pool;
|
||||||
lpool_init(&pool, store, sizeof store, sizeof(struct TestData), _Alignof(struct TestData));
|
lpool_init(&pool, store, 2048, sizeof(struct TestData), _Alignof(struct TestData));
|
||||||
printf("align=%u capacity=%u block_size=%u\n", pool.align, pool.capacity, pool.block_size);
|
printf("align=%u capacity=%u block_size=%u\n", pool.align, pool.capacity, pool.block_size);
|
||||||
struct TestData *data = lpool_alloc(&pool);
|
struct TestData *data = lpool_alloc(&pool);
|
||||||
size_t allocs = 1;
|
size_t allocs = 1;
|
||||||
@@ -23,10 +27,20 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
allocs++;
|
allocs++;
|
||||||
}
|
}
|
||||||
printf("Total allocs = %zu\n", allocs);
|
printf("Total allocs = %zu, used = %u\n", allocs, pool.used);
|
||||||
printf("alloc succeded = %d\n", lpool_alloc(&pool) != NULL);
|
printf("alloc succeded = %d used = %u\n", lpool_alloc(&pool) != NULL, pool.used);
|
||||||
lpool_free(&pool, data);
|
lpool_free(&pool, data);
|
||||||
printf("alloc succeded = %d\n", lpool_alloc(&pool) != NULL);
|
printf("alloc succeded = %d used = %u\n", lpool_alloc(&pool) != NULL, pool.used);
|
||||||
|
|
||||||
|
lpool_reset(&pool);
|
||||||
|
allocs = 0;
|
||||||
|
for (;;) {
|
||||||
|
struct TestData *t = lpool_alloc(&pool);
|
||||||
|
if (t == NULL) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
allocs++;
|
||||||
|
}
|
||||||
|
printf("Total allocs = %zu, used = %u\n", allocs, pool.used);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user