Compare commits
18 Commits
bc78e4150b
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 207d141ddf | |||
| 332307c111 | |||
| f8d9c3bc7e | |||
| 368225ad66 | |||
| 530777fe8c | |||
| e5e0752c10 | |||
| e52051b859 | |||
| 1010541db7 | |||
| 4b61f589fd | |||
| 4a75eddd8f | |||
| 612b9f0e8a | |||
| 2fed5cbe1b | |||
| cec0cd966b | |||
| a21311de7b | |||
| 0fbe5e8255 | |||
| fe3d316b67 | |||
| ba471f41b7 | |||
| 99bcba9c4c |
+32
-12
@@ -12,15 +12,15 @@ static MemoryBlock *memoryblock_allocate(U32 size, U32 commit_size, U32 page_siz
|
||||
Assert(block != NULL);
|
||||
memory_commit(block, commit_size);
|
||||
|
||||
ASAN_POISON_MEMORY_REGION(block, size);
|
||||
ASAN_UNPOISON_MEMORY_REGION(block, MemoryBlock_Header_Size);
|
||||
|
||||
block->base = (U8 *)block;
|
||||
block->size = size;
|
||||
block->first_free = MemoryBlock_Header_Size;
|
||||
block->arena_offset = 0;
|
||||
block->prev = NULL;
|
||||
block->committed_end = commit_size;
|
||||
|
||||
ASAN_POISON_MEMORY_REGION(block->base, block->size);
|
||||
ASAN_UNPOISON_MEMORY_REGION(block, MemoryBlock_Header_Size);
|
||||
return block;
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ void arena_init(Arena *arena, const ArenaOptions *opts)
|
||||
MemoryBlock *block = memoryblock_allocate(aligned_size, aligned_commit_size, page_size);
|
||||
|
||||
arena->flags = opts->flags;
|
||||
arena->checkpoint_level = 0;
|
||||
arena->temp_level = 0;
|
||||
arena->current = block;
|
||||
arena->block_size = aligned_size;
|
||||
arena->commit_size = aligned_commit_size;
|
||||
@@ -48,7 +48,7 @@ void arena_init(Arena *arena, const ArenaOptions *opts)
|
||||
void arena_free(Arena *arena)
|
||||
{
|
||||
MemoryBlock *block = arena->current;
|
||||
Assert(arena->checkpoint_level == 0);
|
||||
Assert(arena->temp_level == 0);
|
||||
while (block != NULL) {
|
||||
MemoryBlock *prev = block->prev;
|
||||
memory_free(block->base, block->size);
|
||||
@@ -115,7 +115,7 @@ void arena_reset_to(Arena *arena, U64 total_offset)
|
||||
MemoryBlock *prev = block->prev;
|
||||
|
||||
if (block->arena_offset >= total_offset) {
|
||||
ASAN_UNPOISON_MEMORY_REGION(block->base, block->size);
|
||||
ASAN_UNPOISON_MEMORY_REGION(block->base, block->committed_end);
|
||||
memory_free(block->base, block->size);
|
||||
arena->current = prev;
|
||||
} else {
|
||||
@@ -123,8 +123,14 @@ void arena_reset_to(Arena *arena, U64 total_offset)
|
||||
new_pos = Max(MemoryBlock_Header_Size, new_pos);
|
||||
// Disallow arbitrary pops to positions that previously were not commited.
|
||||
Assert(new_pos <= block->committed_end);
|
||||
Assert(new_pos <= block->size);
|
||||
block->first_free = new_pos;
|
||||
ASAN_POISON_MEMORY_REGION(&block->base[block->first_free], memoryblock_available(block));
|
||||
|
||||
if ((arena->flags & ArenaFlag_NoDecommit) == 0) {
|
||||
arena_trim(arena);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -132,25 +138,39 @@ void arena_reset_to(Arena *arena, U64 total_offset)
|
||||
}
|
||||
}
|
||||
|
||||
ArenaCheckpoint arena_temp_begin(Arena *arena)
|
||||
void arena_trim(Arena *arena)
|
||||
{
|
||||
ArenaCheckpoint checkpoint = {};
|
||||
MemoryBlock *block = arena->current;
|
||||
U32 aligned_pos = RoundUpToMultiple(block->first_free, arena->commit_size);
|
||||
if (aligned_pos < block->committed_end) {
|
||||
void *trim_start = &block->base[aligned_pos];
|
||||
// Note(nos): Should we unpoision ?
|
||||
//ASAN_UNPOISON_MEMORY_REGION(trim_start, block->committed_end);
|
||||
memory_decommit(trim_start, block->committed_end - aligned_pos);
|
||||
block->committed_end = aligned_pos;
|
||||
}
|
||||
}
|
||||
|
||||
ATemp arena_temp_begin(Arena *arena)
|
||||
{
|
||||
ATemp checkpoint = {};
|
||||
checkpoint.arena = arena;
|
||||
checkpoint.total_offset = arena_used(arena);
|
||||
arena->checkpoint_level++;
|
||||
arena->temp_level++;
|
||||
|
||||
return checkpoint;
|
||||
}
|
||||
|
||||
void arena_temp_end(ArenaCheckpoint *checkpoint)
|
||||
void arena_temp_end(ATemp *checkpoint)
|
||||
{
|
||||
Arena *arena = checkpoint->arena;
|
||||
Assert(arena->checkpoint_level > 0);
|
||||
Assert(arena->temp_level > 0);
|
||||
arena_reset_to(arena, checkpoint->total_offset);
|
||||
arena->checkpoint_level--;
|
||||
arena->temp_level--;
|
||||
}
|
||||
|
||||
void arena_reset(Arena *arena)
|
||||
{
|
||||
arena_reset_to(arena, 0);
|
||||
}
|
||||
|
||||
|
||||
+39
-7
@@ -4,6 +4,9 @@
|
||||
|
||||
typedef U32 ArenaFlags;
|
||||
typedef struct ArenaOptions ArenaOptions;
|
||||
enum {
|
||||
ArenaFlag_NoDecommit, // arena_ temp_end/reset/reset_to doesn't decommit memory
|
||||
};
|
||||
struct ArenaOptions {
|
||||
U32 size;
|
||||
U32 commit_size;
|
||||
@@ -27,16 +30,16 @@ struct Arena {
|
||||
U32 commit_size; // requested commit size
|
||||
U32 page_size; // cached page size
|
||||
ArenaFlags flags;
|
||||
S32 checkpoint_level;
|
||||
S32 temp_level;
|
||||
};
|
||||
|
||||
typedef struct ArenaCheckpoint ArenaCheckpoint;
|
||||
struct ArenaCheckpoint {
|
||||
typedef struct ATemp ATemp;
|
||||
struct ATemp {
|
||||
Arena *arena;
|
||||
U64 total_offset;
|
||||
};
|
||||
#ifndef ArenaParams_Default
|
||||
#define ArenaParams_Default { .size = MB(32), .commit_size = KB(64) }
|
||||
#ifndef ArenaOptions_Default
|
||||
#define ArenaOptions_Default ((ArenaOptions){ .size = MB(32), .commit_size = KB(64) })
|
||||
#endif
|
||||
|
||||
StaticAssert(sizeof(MemoryBlock) <= 64);
|
||||
@@ -51,8 +54,10 @@ void *arena_push(Arena *arena, U32 size, U32 align);
|
||||
// Invalid for arrays > U32
|
||||
#define arena_push_array_of(arena, type, len) arena_push(arena, (U32) (sizeof(type) * (len)), alignof(type))
|
||||
|
||||
ArenaCheckpoint arena_temp_begin(Arena *arena);
|
||||
void arena_temp_end(ArenaCheckpoint *checkpoint);
|
||||
void arena_trim(Arena *arena);
|
||||
|
||||
ATemp arena_temp_begin(Arena *arena);
|
||||
void arena_temp_end(ATemp *checkpoint);
|
||||
void arena_reset_to(Arena *arena, U64 total_offset);
|
||||
void arena_reset(Arena *arena);
|
||||
|
||||
@@ -66,3 +71,30 @@ static INLINE U64 arena_used(const Arena *arena)
|
||||
const MemoryBlock *block = arena->current;
|
||||
return block->arena_offset + block->first_free;
|
||||
}
|
||||
|
||||
|
||||
static INLINE void *arena_allocator_func(Allocator *allocator, AllocOperation operation , void *memory, U32 size, U32 align)
|
||||
{
|
||||
switch (operation) {
|
||||
case Alloc_Op_Aligned:
|
||||
return arena_push(allocator->data, size, align);
|
||||
|
||||
case Alloc_Op_Free: // can't free from an marena
|
||||
return NULL;
|
||||
|
||||
case Alloc_Op_Free_All:
|
||||
arena_free(allocator->data);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Assert(false);
|
||||
}
|
||||
|
||||
static INLINE Allocator arena_allocator(Arena *arena)
|
||||
{
|
||||
Allocator allocator = {};
|
||||
allocator.op_func_ptr = arena_allocator_func;
|
||||
allocator.data = arena;
|
||||
|
||||
return allocator;
|
||||
}
|
||||
|
||||
+88
-8
@@ -104,12 +104,43 @@ typedef unsigned __int128 U128;
|
||||
(const type *)((const void *)(const unsigned char *)__mptr - offsetof(const type, member)); \
|
||||
})
|
||||
|
||||
// align needs to be pow2
|
||||
#define AlignUp(val, align) (((val) + (align) - 1UL) & ~((align) - 1UL))
|
||||
#define AlignDown(val, align) ((val) & ~((align) - 1UL))
|
||||
#define IsPow2(x) ((x) && !((x) & ((x) - 1UL)))
|
||||
#define IsPow2OrZero(x) (!((x) & ((x) - 1UL)))
|
||||
#define IsAlignedTo(x, align) (IsPow2(align) && !((x) & ((align) - 1UL)))
|
||||
// `align` must be a power of two.
|
||||
//
|
||||
// These keep the result in the *same integer type as the first argument*
|
||||
#define AlignUp(val, align) \
|
||||
({ \
|
||||
__typeof__(val) AlignUp_v_ = (val); \
|
||||
__typeof__(val) AlignUp_a_ = (__typeof__(val))(align); \
|
||||
(__typeof__(val))((AlignUp_v_ + (AlignUp_a_ - 1)) & \
|
||||
~(__typeof__(val))(AlignUp_a_ - 1)); \
|
||||
})
|
||||
|
||||
#define AlignDown(val, align) \
|
||||
({ \
|
||||
__typeof__(val) AlignDown_v_ = (val); \
|
||||
__typeof__(val) AlignDown_a_ = (__typeof__(val))(align); \
|
||||
(__typeof__(val))(AlignDown_v_ & ~(__typeof__(val))(AlignDown_a_ - 1));\
|
||||
})
|
||||
|
||||
#define IsPow2(x) \
|
||||
({ \
|
||||
__typeof__(x) IsPow2_x_ = (x); \
|
||||
IsPow2_x_ && !(IsPow2_x_ & (__typeof__(x))(IsPow2_x_ - 1)); \
|
||||
})
|
||||
|
||||
#define IsPow2OrZero(x) \
|
||||
({ \
|
||||
__typeof__(x) IsPow2OrZero_x_ = (x); \
|
||||
!(IsPow2OrZero_x_ & (__typeof__(x))(IsPow2OrZero_x_ - 1)); \
|
||||
})
|
||||
|
||||
#define IsAlignedTo(x, align) \
|
||||
({ \
|
||||
__typeof__(x) IsAlignedTo_x_ = (x); \
|
||||
__typeof__(x) IsAlignedTo_a_ = (__typeof__(x))(align); \
|
||||
IsPow2(IsAlignedTo_a_) && \
|
||||
!(IsAlignedTo_x_ & (__typeof__(x))(IsAlignedTo_a_ - 1)); \
|
||||
})
|
||||
|
||||
#define GetBit(val, idx) (((val) >> (idx)) & 1)
|
||||
// Get n_bits starting at idx going left
|
||||
@@ -146,7 +177,8 @@ typedef unsigned __int128 U128;
|
||||
#define CeilDiv(x, y) (((x) + ((y) - 1)) / (y))
|
||||
|
||||
/**
|
||||
* Round x up to nearest multiple of y
|
||||
* Round x up to nearest multiple of y:
|
||||
* (((x) + (y) - 1) - (((x) + (y) - 1) % (y)))
|
||||
* e.g. RoundUp(30,48) == 48
|
||||
* e.g. RoundUp(49,48) == 96
|
||||
*
|
||||
@@ -154,7 +186,15 @@ typedef unsigned __int128 U128;
|
||||
* @param y denominator
|
||||
* @return x rounded up to nearest multiple of y
|
||||
*/
|
||||
#define RoundUpToMultiple(x, y) (CeilDiv((x), (y)) * (y))
|
||||
#define RoundUpToMultiple(x, y) \
|
||||
({ \
|
||||
__typeof__(x) _x = (x); \
|
||||
__typeof__(y) _y = (y); \
|
||||
Assert(_y != 0); \
|
||||
__typeof__(x) _tmp = _x + _y - 1; \
|
||||
_tmp - _tmp % _y; \
|
||||
})
|
||||
|
||||
|
||||
/**
|
||||
* Round x down to nearest multiple of y
|
||||
@@ -304,6 +344,46 @@ static INLINE U64 SaturatingMult64(U64 a, U64 b)
|
||||
return SaturatingMult(a, b, U64, U64_Max);
|
||||
}
|
||||
|
||||
// Allocators
|
||||
typedef enum AllocOperation AllocOperation;
|
||||
enum AllocOperation {
|
||||
Alloc_Op_Aligned = 1,
|
||||
Alloc_Op_Free,
|
||||
Alloc_Op_Free_All,
|
||||
};
|
||||
|
||||
typedef struct Allocator Allocator;
|
||||
typedef void *(*AllocOpFunc)(Allocator *allocator, AllocOperation operation, void *memory, U32 size, U32 align);
|
||||
struct Allocator {
|
||||
AllocOpFunc op_func_ptr;
|
||||
void *data;
|
||||
};
|
||||
|
||||
static INLINE void *allocator_alloc_aligned(Allocator *allocator, U32 size, U32 align)
|
||||
{
|
||||
return allocator->op_func_ptr(allocator, Alloc_Op_Aligned, NULL, size, align);
|
||||
}
|
||||
|
||||
static INLINE void allocator_free(Allocator *allocator, void *memory)
|
||||
{
|
||||
allocator->op_func_ptr(allocator, Alloc_Op_Free, memory, 0, 0);
|
||||
}
|
||||
|
||||
static INLINE void allocator_free_all(Allocator *allocator)
|
||||
{
|
||||
allocator->op_func_ptr(allocator, Alloc_Op_Free_All, NULL, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
#define allocator_push_type(allocator, type_)\
|
||||
(allocator)->op_func_ptr((allocator), Alloc_Op_Aligned, NULL, sizeof(type_), _Alignof(type_))
|
||||
|
||||
#define allocator_push_array(allocator, type_, nr_elements)\
|
||||
(allocator)->op_func_ptr((allocator), Alloc_Op_Aligned, NULL, sizeof(type_) * (nr_elements), _Alignof(type_) )
|
||||
|
||||
#define allocator_push(allocator, size, align)\
|
||||
(allocator)->op_func_ptr((allocator), Alloc_Op_Aligned, NULL, size, align)
|
||||
|
||||
// address-sanitizer helpers (-fsanitize=address)
|
||||
#ifdef USE_ASAN
|
||||
#include <sanitizer/asan_interface.h>
|
||||
|
||||
+267
-11
@@ -1,6 +1,8 @@
|
||||
#include "base_core.h"
|
||||
#include "arena.h"
|
||||
#include "mini_arena.h"
|
||||
#include "platform.h"
|
||||
#include "pool.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -67,7 +69,7 @@ static void arena_test_2(void)
|
||||
printf("Allocated ptr %p\n", ptr);
|
||||
|
||||
arena_dump(&arena);
|
||||
ArenaCheckpoint checkpoint = arena_temp_begin(&arena);
|
||||
ATemp checkpoint = arena_temp_begin(&arena);
|
||||
ptr = arena_push(&arena, MB(4)+3, 8);
|
||||
MemoryZero(ptr, MB(2));
|
||||
|
||||
@@ -76,7 +78,7 @@ static void arena_test_2(void)
|
||||
ptr = arena_push(&arena, commit_size , 8);
|
||||
MemoryZero(ptr, commit_size );
|
||||
|
||||
ArenaCheckpoint checkpoint2 = arena_temp_begin(&arena);
|
||||
ATemp checkpoint2 = arena_temp_begin(&arena);
|
||||
ptr = arena_push(&arena, 64, 8);
|
||||
MemoryZero(ptr, 64);
|
||||
|
||||
@@ -91,12 +93,12 @@ static void arena_test_2(void)
|
||||
((U8*)ptr)[0] = 0x73;
|
||||
ptr = arena_push(&arena, 0, KB(4));
|
||||
//((U8*)ptr)[0] = 0x73;
|
||||
arena_reset_to(&arena, 1089);
|
||||
// arena_reset_to(&arena, 1089);
|
||||
arena_dump(&arena);
|
||||
arena_reset_to(&arena, 0);
|
||||
for (U64 i = 0; i < KB(1); i++) {
|
||||
ptr = arena_push(&arena, MB(1) + 3, 8);
|
||||
MemoryZero(ptr, MB(1));
|
||||
for (U64 i = 0; i < MB(4); i++) {
|
||||
ptr = arena_push(&arena, 3, 1);
|
||||
MemoryZero(ptr, 3);
|
||||
}
|
||||
arena_free(&arena);
|
||||
}
|
||||
@@ -162,11 +164,265 @@ static void arena_test_4(void)
|
||||
arena_free(&arena);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
static void arena_test_allocator_interface(void)
|
||||
{
|
||||
// arena_test_1();
|
||||
arena_test_2();
|
||||
// arena_test_3();
|
||||
arena_test_4();
|
||||
TRACEF("\n");
|
||||
|
||||
U32 page_size = platform_page_size();
|
||||
ArenaOptions arena_params = {};
|
||||
U32 arena_size = MB(1) + MemoryBlock_Header_Size;
|
||||
U32 commit_size = KB(64);
|
||||
arena_params.size = arena_size;
|
||||
arena_params.commit_size = commit_size;
|
||||
Arena arena;
|
||||
arena_init(&arena, &arena_params);
|
||||
Allocator allocator = arena_allocator(&arena);
|
||||
|
||||
// block_size is the page-aligned arena_size. Request nearly the whole block
|
||||
// but with page alignment, so the alignment gap (a full page) forces a grow.
|
||||
U32 block_size = arena.block_size;
|
||||
U32 push_size = block_size - MemoryBlock_Header_Size;
|
||||
|
||||
void *ptr = allocator_push(&allocator, push_size, page_size);
|
||||
Assert(IsAlignedTo((uintptr_t)ptr, page_size));
|
||||
MemoryZero(ptr, push_size);
|
||||
printf("test_4: allocated %u bytes page-aligned at %p\n", push_size, ptr);
|
||||
arena_dump(&arena);
|
||||
|
||||
// A second page-aligned push to make sure the cursor math still holds.
|
||||
ptr = allocator_push(&allocator, page_size, page_size);
|
||||
Assert(IsAlignedTo((uintptr_t)ptr, page_size));
|
||||
MemoryZero(ptr, page_size);
|
||||
printf("test_4: allocated %u bytes page-aligned at %p\n", page_size, ptr);
|
||||
|
||||
allocator_free_all(&allocator);
|
||||
arena_free(&arena);
|
||||
|
||||
}
|
||||
|
||||
static void arena_test_decommit(void)
|
||||
{
|
||||
TRACEF("\n");
|
||||
|
||||
U32 page_size = platform_page_size();
|
||||
ArenaOptions arena_params = {};
|
||||
U32 arena_size = MB(1) + MemoryBlock_Header_Size;
|
||||
U32 commit_size = KB(64);
|
||||
arena_params.size = arena_size;
|
||||
arena_params.commit_size = commit_size;
|
||||
Arena arena;
|
||||
arena_init(&arena, &arena_params);
|
||||
void *ptr = arena_push(&arena, commit_size * 10, 8);
|
||||
MemoryZero(ptr, commit_size * 10);
|
||||
arena_reset(&arena);
|
||||
|
||||
void *pre_ptr = arena_push(&arena, 5, 4);
|
||||
|
||||
ATemp tmp = arena_temp_begin(&arena);
|
||||
ptr = arena_push(&arena, commit_size * 2 + 13, 8);
|
||||
MemoryZero(ptr, commit_size * 2 + 13);
|
||||
arena_temp_end(&tmp);
|
||||
MemoryZero(pre_ptr, 5);
|
||||
|
||||
arena_free(&arena);
|
||||
}
|
||||
|
||||
void miniarena_test1(void)
|
||||
{
|
||||
TRACEF("\n");
|
||||
|
||||
#define arena_size 4096
|
||||
MARENA_DEFINE(arena, arena_size, 8);
|
||||
int i;
|
||||
for (i = 0; ; i++) {
|
||||
void *ptr = marena_push(&arena, 1);
|
||||
if (ptr == NULL) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
printf("miniarena_test1,size %u pushed %d bytes first_free %u\n", arena.size, i, arena.first_free);
|
||||
marena_reset(&arena);
|
||||
MTemp checkpoint = marena_temp_begin(&arena);
|
||||
|
||||
void *ptr = marena_push(&arena, arena_size);
|
||||
Assert(ptr != NULL);
|
||||
Assert(arena.first_free == arena_size);
|
||||
MemoryZero(ptr, arena_size);
|
||||
marena_temp_end(&checkpoint);
|
||||
Assert(arena.first_free == 0);
|
||||
U32 *val = marena_push_type(&arena, U32);
|
||||
*val = 0xa0b0c0d0;
|
||||
printf("marena val 0x%x\n", *val);
|
||||
|
||||
printf("miniarena_test1,size %u bytes first_free %u\n", arena.size, arena.first_free);
|
||||
Assert(arena.first_free == sizeof(U32));
|
||||
}
|
||||
|
||||
typedef struct PoolTest PoolTest;
|
||||
struct PoolTest{
|
||||
U32 n;
|
||||
PoolTest *next;
|
||||
char name[12];
|
||||
};
|
||||
|
||||
void apool_test(void)
|
||||
{
|
||||
TRACEF("\n");
|
||||
|
||||
Arena arena;
|
||||
arena_init(&arena, &ArenaOptions_Default);
|
||||
Allocator allocator = arena_allocator(&arena);
|
||||
APool *pool = apool_create_ex(&allocator, 16, sizeof(PoolTest), alignof(PoolTest));
|
||||
PoolTest *list = NULL;
|
||||
for (U32 i = 0; i < U32_Max; i++) {
|
||||
PoolTest *pt = apool_alloc(pool);
|
||||
if (pt == NULL){
|
||||
break;
|
||||
}
|
||||
pt->n = i;
|
||||
pt->next = list;
|
||||
list = pt;
|
||||
}
|
||||
for (PoolTest *p = list; p; p = p->next){
|
||||
printf("Pooltest %u\n",p->n);
|
||||
}
|
||||
|
||||
PoolTest *next;
|
||||
U32 count = 0;
|
||||
for (PoolTest *p = list; count < 7 && p; p = next){
|
||||
count++;
|
||||
next = p->next;
|
||||
printf("Pooltest freeing %u\n",p->n);
|
||||
apool_free(pool, p);
|
||||
}
|
||||
list = NULL;
|
||||
for (U32 i = 0; i < U32_Max; i++) {
|
||||
PoolTest *pt = apool_alloc(pool);
|
||||
if (pt == NULL){
|
||||
break;
|
||||
}
|
||||
pt->n = i;
|
||||
pt->next = list;
|
||||
list = pt;
|
||||
}
|
||||
for (PoolTest *p = list; p; p = p->next){
|
||||
printf("Pooltest %u\n",p->n);
|
||||
}
|
||||
allocator_free_all(&allocator);
|
||||
arena_free(&arena);
|
||||
}
|
||||
|
||||
void apool_test_miniarena(void)
|
||||
{
|
||||
TRACEF("\n");
|
||||
|
||||
MArena arena;
|
||||
U8 data[4096];
|
||||
marena_init(&arena, data, sizeof data);
|
||||
Allocator allocator = marena_allocator(&arena);
|
||||
APool *pool = apool_create_ex(&allocator, 16, sizeof(PoolTest), alignof(PoolTest));
|
||||
PoolTest *list = NULL;
|
||||
for (U32 i = 0; i < U32_Max; i++) {
|
||||
PoolTest *pt = apool_alloc(pool);
|
||||
if (pt == NULL){
|
||||
break;
|
||||
}
|
||||
pt->n = i;
|
||||
pt->next = list;
|
||||
list = pt;
|
||||
}
|
||||
for (PoolTest *p = list; p; p = p->next){
|
||||
printf("Pooltest %u\n",p->n);
|
||||
}
|
||||
|
||||
PoolTest *next;
|
||||
U32 count = 0;
|
||||
for (PoolTest *p = list; count < 7 && p; p = next){
|
||||
count++;
|
||||
next = p->next;
|
||||
printf("Pooltest freeing %u\n",p->n);
|
||||
apool_free(pool, p);
|
||||
}
|
||||
list = NULL;
|
||||
for (U32 i = 0; i < U32_Max; i++) {
|
||||
PoolTest *pt = apool_alloc(pool);
|
||||
if (pt == NULL){
|
||||
break;
|
||||
}
|
||||
pt->n = i;
|
||||
pt->next = list;
|
||||
list = pt;
|
||||
}
|
||||
for (PoolTest *p = list; p; p = p->next){
|
||||
printf("Pooltest %u\n",p->n);
|
||||
}
|
||||
allocator_free_all(&allocator);
|
||||
}
|
||||
|
||||
|
||||
void apool_test_autogrow(void)
|
||||
{
|
||||
TRACEF("\n");
|
||||
|
||||
Arena arena;
|
||||
arena_init(&arena, &ArenaOptions_Default);
|
||||
Allocator allocator = arena_allocator(&arena);
|
||||
|
||||
APool *pool = apool_create(&allocator, APOOL_AUTOGROW, PoolTest);
|
||||
PoolTest *list = NULL;
|
||||
for (U32 i = 0; i < 4; i++) {
|
||||
PoolTest *pt = apool_alloc(pool);
|
||||
if (pt == NULL){
|
||||
break;
|
||||
}
|
||||
pt->n = i;
|
||||
pt->next = list;
|
||||
list = pt;
|
||||
}
|
||||
for (PoolTest *p = list; p; p = p->next){
|
||||
printf("Pooltest %u\n",p->n);
|
||||
}
|
||||
|
||||
PoolTest *next;
|
||||
U32 count = 0;
|
||||
for (PoolTest *p = list; count < 3 && p; p = next){
|
||||
count++;
|
||||
next = p->next;
|
||||
printf("Pooltest freeing %u\n",p->n);
|
||||
apool_free(pool, p);
|
||||
}
|
||||
list = NULL;
|
||||
for (U32 i = 0; i < 5; i++) {
|
||||
PoolTest *pt = apool_alloc(pool);
|
||||
if (pt == NULL){
|
||||
break;
|
||||
}
|
||||
pt->n = i;
|
||||
pt->next = list;
|
||||
list = pt;
|
||||
}
|
||||
for (PoolTest *p = list; p; p = p->next){
|
||||
printf("Pooltest %u\n",p->n);
|
||||
}
|
||||
|
||||
allocator_free_all(&allocator);
|
||||
arena_free(&arena);
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
arena_test_1();
|
||||
arena_test_2();
|
||||
arena_test_3();
|
||||
arena_test_4();
|
||||
arena_test_decommit();
|
||||
arena_test_allocator_interface();
|
||||
miniarena_test1();
|
||||
apool_test();
|
||||
apool_test_autogrow();
|
||||
apool_test_miniarena();
|
||||
|
||||
}
|
||||
|
||||
|
||||
+84
-59
@@ -1,96 +1,121 @@
|
||||
#pragma once
|
||||
#include "base_core.h"
|
||||
|
||||
typedef struct MiniArena MiniArena;
|
||||
struct MiniArena {
|
||||
U8 *start; // start of user buffer
|
||||
U8 *curr; // next alloc point
|
||||
U8 *end; // one past end of user buffer
|
||||
int checkpoint_level; // level of miniarenatmp_begin stacks
|
||||
// Mini Arena for arena allocations from a supplied buffer
|
||||
|
||||
typedef struct MArena MArena;
|
||||
struct MArena {
|
||||
U8 *base; // start of buffer
|
||||
U32 first_free;
|
||||
U32 size;
|
||||
S32 temp_level; // level of marena_temp_begin nesting
|
||||
};
|
||||
|
||||
typedef struct MiniArenaCheckpoint MiniArenaCheckpoint;
|
||||
struct MiniArenaCheckpoint {
|
||||
U8 *alloc_point;
|
||||
MiniArena *arena;
|
||||
typedef struct MTemp MTemp;
|
||||
struct MTemp {
|
||||
U32 offset;
|
||||
MArena *arena;
|
||||
};
|
||||
|
||||
#define LARENA_STATIC_INITIALIZER(data) LARENA_INITIALIZER((data), sizeof(data))
|
||||
#define LARENA_INITIALIZER(data, sz) {.start = (data), .curr = (data), .end = (data) + sz, .checkpoint_level = 0}
|
||||
#define LARENA_DEFINE(var_name, size_, alignment_) \
|
||||
#define MARENA_STATIC_INITIALIZER(data) MARENA_INITIALIZER((data), sizeof(data))
|
||||
#define MARENA_INITIALIZER(data, size_) {.base = (data), .first_free = 0, .size = (U32)size_, .temp_level = 0}
|
||||
#define MARENA_DEFINE(var_name, size, alignment_) \
|
||||
struct { \
|
||||
_Alignas(alignment_) U8 memory[size_]; \
|
||||
} var_name##_arena_memory; \
|
||||
MiniArena var_name = LARENA_STATIC_INITIALIZER(var_name##_arena_memory.memory)
|
||||
#define LARENA_ALLOC_TYPE(allocator, type_) miniarenaalloc_aligned((allocator), sizeof(type_), _Alignof(type_))
|
||||
#define LARENA_ALLOC_ARRAY(allocator, type_, nr_elements_) miniarenaalloc_aligned((allocator), (nr_elements_) * sizeof(type_), _Alignof(type_))
|
||||
static INLINE size_t miniarena_size(const MiniArena *arena)
|
||||
_Alignas(alignment_) U8 memory[size];\
|
||||
} var_name##_marena_memory; \
|
||||
MArena var_name = MARENA_STATIC_INITIALIZER(var_name##_marena_memory.memory)
|
||||
|
||||
|
||||
|
||||
static INLINE void marena_init(MArena *arena, void *memory, U32 size)
|
||||
{
|
||||
return (size_t)(arena->end - arena->start);
|
||||
Assert(memory != NULL);
|
||||
arena->base = (U8 *)memory;
|
||||
arena->first_free = 0;
|
||||
arena->size = size;
|
||||
}
|
||||
|
||||
static INLINE size_t miniarena_used(const MiniArena *arena)
|
||||
static INLINE MTemp marena_temp_begin(MArena *arena)
|
||||
{
|
||||
return (size_t)(arena->curr - arena->start);
|
||||
}
|
||||
static INLINE size_t miniarena_available(const MiniArena *arena)
|
||||
{
|
||||
return miniarena_size(arena) - miniarena_used(arena);
|
||||
}
|
||||
|
||||
static INLINE void miniarena_init(MiniArena *arena, void *memory, size_t sz)
|
||||
{
|
||||
arena->start = arena->curr = (U8 *)memory;
|
||||
arena->end = arena->start + sz;
|
||||
}
|
||||
|
||||
static INLINE MiniArenaCheckpoint miniarenatmp_begin(MiniArena *arena)
|
||||
{
|
||||
MiniArenaCheckpoint checkpoint = {};
|
||||
MTemp checkpoint = {};
|
||||
|
||||
checkpoint.arena = arena;
|
||||
checkpoint.alloc_point = arena->curr;
|
||||
arena->checkpoint_level++;
|
||||
checkpoint.offset = arena->first_free;
|
||||
arena->temp_level++;
|
||||
|
||||
return checkpoint;
|
||||
}
|
||||
|
||||
static INLINE void miniarenatmp_end(MiniArenaCheckpoint *state)
|
||||
static INLINE void marena_temp_end(MTemp *state)
|
||||
{
|
||||
MiniArena *arena = state->arena;
|
||||
Assert(arena->checkpoint_level > 0);
|
||||
arena->curr = state->alloc_point;
|
||||
arena->checkpoint_level--;
|
||||
MArena *arena = state->arena;
|
||||
Assert(arena->temp_level > 0);
|
||||
arena->first_free = state->offset;
|
||||
arena->temp_level--;
|
||||
}
|
||||
static INLINE void *miniarena_push(MiniArena *arena, size_t sz)
|
||||
|
||||
static INLINE void *marena_push(MArena *arena, U32 size)
|
||||
{
|
||||
if (sz > miniarena_available(arena)) {
|
||||
if (size > arena->size - arena->first_free) {
|
||||
return NULL;
|
||||
}
|
||||
void *start = arena->curr;
|
||||
arena->curr += sz;
|
||||
return start;
|
||||
void *base = &arena->base[arena->first_free];
|
||||
arena->first_free += size;
|
||||
return base;
|
||||
}
|
||||
|
||||
[[gnu::alloc_align(3)]]
|
||||
static INLINE void *miniaren_push_aligned(MiniArena *arena, size_t sz, unsigned int align)
|
||||
static INLINE void *marena_push_aligned(MArena *arena, U32 size, U32 align)
|
||||
{
|
||||
Assert(IsPow2(align));
|
||||
|
||||
uintptr_t curr = (uintptr_t)arena->curr;
|
||||
uintptr_t aligned = AlignUp(curr, align);
|
||||
U8 *start = (U8 *)aligned;
|
||||
|
||||
if (sz > (size_t)(arena->end - start)) {
|
||||
U32 aligned = AlignUp(arena->first_free, align);
|
||||
if (aligned < arena->first_free) { // aligned wrapped around
|
||||
return NULL;
|
||||
}
|
||||
|
||||
arena->curr = start + sz;
|
||||
if (aligned > arena->size || size > arena->size - aligned) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return start;
|
||||
void *base = &arena->base[aligned];
|
||||
arena->first_free = aligned + size;
|
||||
|
||||
return base;
|
||||
}
|
||||
|
||||
static INLINE void miniarena_reset(MiniArena *arena)
|
||||
#define marena_push_type(arena, type_)\
|
||||
marena_push_aligned((arena), sizeof(type_), _Alignof(type_))
|
||||
#define marena_push_array(arena, type_, nr_elements_)\
|
||||
marena_push_aligned((arena), (nr_elements_) * (U32)sizeof(type_), _Alignof(type_))
|
||||
|
||||
|
||||
static INLINE void marena_reset(MArena *arena)
|
||||
{
|
||||
arena->curr = arena->start;
|
||||
arena->first_free = 0;
|
||||
}
|
||||
|
||||
static INLINE void *marena_allocator_func(Allocator *allocator, AllocOperation operation , void *memory, U32 size, U32 align)
|
||||
{
|
||||
switch (operation) {
|
||||
case Alloc_Op_Aligned:
|
||||
return marena_push_aligned(allocator->data, size, align);
|
||||
|
||||
case Alloc_Op_Free: // can't free/Free_all from an marena
|
||||
return NULL;
|
||||
|
||||
case Alloc_Op_Free_All:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Assert(false);
|
||||
}
|
||||
|
||||
static INLINE Allocator marena_allocator(MArena *arena)
|
||||
{
|
||||
Allocator allocator = {};
|
||||
allocator.op_func_ptr = marena_allocator_func;
|
||||
allocator.data = arena;
|
||||
|
||||
return allocator;
|
||||
}
|
||||
|
||||
@@ -23,6 +23,34 @@ void platform_free(void *mem)
|
||||
free(mem);
|
||||
}
|
||||
|
||||
|
||||
static void *platform_allocator_func(Allocator *allocator, AllocOperation operation , void *memory, U32 size, U32 align)
|
||||
{
|
||||
switch (operation) {
|
||||
case Alloc_Op_Aligned:
|
||||
return aligned_alloc(size, align);
|
||||
|
||||
case Alloc_Op_Free: // can't free/Free_all from an marena
|
||||
free(memory);
|
||||
return NULL;
|
||||
|
||||
case Alloc_Op_Free_All:
|
||||
NotImplemented;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Assert(false);
|
||||
}
|
||||
|
||||
Allocator platform_allocator(void)
|
||||
{
|
||||
Allocator allocator = {};
|
||||
allocator.op_func_ptr = platform_allocator_func;
|
||||
allocator.data = NULL;
|
||||
|
||||
return allocator;
|
||||
}
|
||||
|
||||
void *memory_allocate_and_commit(size_t size)
|
||||
{
|
||||
Assert(IsPageAligned(size));
|
||||
|
||||
+5
-1
@@ -1,12 +1,16 @@
|
||||
#pragma once
|
||||
#include <stdlib.h>
|
||||
#include "base_core.h"
|
||||
|
||||
void *platform_malloc(size_t size);
|
||||
void platform_free(void *mem);
|
||||
Allocator platform_allocator(void);
|
||||
|
||||
U32 platform_page_size(void);
|
||||
|
||||
// Memory functions expect mem and size to be page aligned
|
||||
void *memory_allocate_and_commit(size_t size);
|
||||
void *memory_reserve(size_t size);
|
||||
void memory_commit(void *mem, size_t size);
|
||||
void memory_decommit(void *mem, size_t size);
|
||||
|
||||
void memory_free(void *mem, size_t size);
|
||||
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
#include "pool.h"
|
||||
|
||||
APool *apool_create_ex(Allocator *allocator, U32 n_elements, U32 element_size, U32 element_align)
|
||||
{
|
||||
Assert(IsPow2(element_align));
|
||||
U32 align = element_align;
|
||||
if (element_align < alignof(APoolElement)) {
|
||||
element_align = alignof(APoolElement);
|
||||
}
|
||||
U32 size = AlignUp(element_size, element_align);
|
||||
|
||||
APool *pool = allocator_push_type(allocator, APool);
|
||||
Assert(pool != NULL);
|
||||
pool->align = align;
|
||||
pool->element_size = size;
|
||||
pool->allocator = allocator;
|
||||
pool->n_elements = n_elements;
|
||||
pool->free_list = NULL;
|
||||
pool->start = NULL;
|
||||
|
||||
if (size != APOOL_AUTOGROW) {
|
||||
U32 total_size = n_elements * size; // Note(nos): guard against unreasonable sizes
|
||||
U8 * start = allocator_push(allocator, total_size, align);
|
||||
for (U32 i = 0; i < total_size; i += size) {
|
||||
APoolElement *element = (APoolElement *)&start[i];
|
||||
element->next = pool->free_list;
|
||||
pool->free_list = element;
|
||||
}
|
||||
pool->start = start;
|
||||
}
|
||||
|
||||
return pool;
|
||||
}
|
||||
|
||||
void *apool_alloc(APool *pool)
|
||||
{
|
||||
APoolElement *element = pool->free_list;
|
||||
if (element != NULL) {
|
||||
pool->free_list = element->next;
|
||||
} else if(pool->n_elements == APOOL_AUTOGROW) {
|
||||
element = allocator_push(pool->allocator, pool->element_size, pool->align);
|
||||
}
|
||||
|
||||
return element;
|
||||
}
|
||||
|
||||
void apool_free(APool *pool, void *ptr)
|
||||
{
|
||||
if (ptr != NULL) {
|
||||
APoolElement *element = (APoolElement *)ptr;
|
||||
element->next = pool->free_list;
|
||||
pool->free_list = element;
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
#include "base_core.h"
|
||||
|
||||
typedef union APoolElement APoolElement;
|
||||
union APoolElement {
|
||||
APoolElement *next; // offset from start to next free block (when not allocated)
|
||||
};
|
||||
|
||||
typedef struct APool APool;
|
||||
struct APool {
|
||||
APoolElement *free_list; // free list
|
||||
U8 *start; // start of user buffer
|
||||
Allocator *allocator;
|
||||
U32 element_size; // aligned size of each block
|
||||
U32 n_elements; // total number of blocks
|
||||
U32 align; // alignment
|
||||
};
|
||||
enum {
|
||||
APOOL_AUTOGROW = 0
|
||||
};
|
||||
|
||||
#define apool_create(arena, n_elements, type) \
|
||||
apool_create_ex(arena, n_elements, sizeof(type), alignof(type));
|
||||
APool *apool_create_ex(Allocator *allocator, U32 n_elements, U32 element_size, uint32_t element_align);
|
||||
void *apool_alloc(APool *pool);
|
||||
void apool_free(APool *pool, void *element);
|
||||
Reference in New Issue
Block a user