Compare commits

...

21 Commits

Author SHA1 Message Date
Nils O. Selåsdal 207d141ddf Formatting issue 2026-06-22 21:03:02 +02:00
Nils O. Selåsdal 332307c111 Merge 2026-06-22 10:12:57 +02:00
Nils O. Selåsdal f8d9c3bc7e Fix 2026-06-22 00:25:17 +02:00
Nils O. Selåsdal 368225ad66 Fix marena macros 2026-06-22 00:22:05 +02:00
Nils O. Selåsdal 530777fe8c Use allocator_free_all 2026-06-21 23:52:02 +02:00
Nils O. Selåsdal e5e0752c10 Wrap malloc 2026-06-21 23:50:16 +02:00
Nils O. Selåsdal e52051b859 Add allocator interface 2026-06-21 23:41:27 +02:00
Nils O. Selåsdal 1010541db7 Formatting 2026-06-16 23:55:36 +02:00
Nils O. Selåsdal 4b61f589fd Refactor decommit to arena_trim 2026-06-16 23:53:30 +02:00
Nils O. Selåsdal 4a75eddd8f Implement memory decommit 2026-06-16 23:46:52 +02:00
Nils O. Selåsdal 612b9f0e8a Fix roundtomultiple 2026-06-15 20:29:01 +02:00
Nils O. Selåsdal 2fed5cbe1b Cleanups 2026-06-15 10:50:54 +02:00
Nils O. Selåsdal cec0cd966b Add pool alocator based on arena 2026-06-12 21:39:16 +02:00
Nils O. Selåsdal a21311de7b Typesafe align macros 2026-06-10 20:27:02 +02:00
Nils O. Selåsdal 0fbe5e8255 Shorter names 2026-06-10 20:23:14 +02:00
Nils O. Selåsdal fe3d316b67 Fix miniarena 2026-06-10 20:16:41 +02:00
Nils O. Selåsdal ba471f41b7 Update mini arena 2026-06-10 16:43:22 +02:00
Nils O. Selåsdal 99bcba9c4c Fix poison 2026-06-10 16:38:10 +02:00
Nils O. Selåsdal bc78e4150b Start mini arena 2026-06-10 11:59:42 +02:00
Nils O. Selåsdal 627c6d16ca Fix potential overflow when we're nearing 4G blocks.. 2026-06-09 23:24:57 +02:00
Nils O. Selåsdal 2b4660f7d5 Perform proper memory commit. Many bug fizes 2026-06-09 20:15:12 +02:00
10 changed files with 897 additions and 153 deletions
+1
View File
@@ -0,0 +1 @@
vim.opt.makeprg = "./build.sh"
+99 -51
View File
@@ -3,100 +3,134 @@
#include "platform.h"
MemoryBlock *memoryblock_allocate(size_t size)
static MemoryBlock *memoryblock_allocate(U32 size, U32 commit_size, U32 page_size)
{
void *base = memory_allocate(size);
Assert(base != NULL);
Assert(IsAlignedTo(size, page_size));
Assert(IsAlignedTo(commit_size, page_size));
MemoryBlock *block = (MemoryBlock *)base;
block->base = (U8 *)base;
block->size = size;
block->pos = MemoryBlock_Header_Size;
block->base_pos = 0;
block->prev = NULL;
MemoryBlock *block = memory_reserve(size);
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;
return block;
}
void arena_init(Arena *arena, const ArenaParams *params)
void arena_init(Arena *arena, const ArenaOptions *opts)
{
Assert(params->size > MemoryBlock_Header_Size);
Assert(params->size < (U32_Max - MemoryBlock_Header_Size - 1));
U32 page_size = platform_page_size();
Assert(opts->size > MemoryBlock_Header_Size);
Assert(opts->size < (U32_Max - page_size)); // Reserving last piece so our math doesn't overflow
Assert(opts->commit_size > 0);
MemoryBlock *block = memoryblock_allocate(params->size);
ASAN_UNPOISON_MEMORY_REGION(block, MemoryBlock_Header_Size);
U32 aligned_commit_size = AlignUp(opts->commit_size, page_size);
U32 aligned_size = AlignUp(opts->size, page_size);
Assert(aligned_commit_size <= aligned_size);
arena->flags = params->flags;
arena->checkpoint_level = 0;
MemoryBlock *block = memoryblock_allocate(aligned_size, aligned_commit_size, page_size);
arena->flags = opts->flags;
arena->temp_level = 0;
arena->current = block;
arena->requested_size = params->size;
ASAN_POISON_MEMORY_REGION(&block->base[block->pos], memoryblock_available(block));
arena->block_size = aligned_size;
arena->commit_size = aligned_commit_size;
arena->page_size = page_size;
}
void arena_free(Arena *arena)
{
MemoryBlock *block = arena->current;
Assert(arena->temp_level == 0);
while (block != NULL) {
MemoryBlock *prev = block->prev;
memory_free(block->base, block->size);
block = prev;
}
arena->current = NULL;
}
void *arena_push(Arena *arena, U32 size, U32 align)
{
Assert(size < (U32_Max - align - MemoryBlock_Header_Size - 1));
Assert(IsPow2(align));
MemoryBlock *block = arena->current;
U32 start_pos = AlignUpPow2(block->pos, align);
U32 available = memoryblock_available(block);
if (available < size) {
U32 new_size = arena->requested_size; // original user requeted size
if (new_size < size) {
new_size = AlignUpPow2(size + MemoryBlock_Header_Size,align);
MemoryBlock *block = arena->current;
U32 start_pos = AlignUp(block->first_free, align); // might align past block->size, handled in below check
U32 available = block->size - start_pos;
if (start_pos > block->size || available < size) {
U32 pad = AlignUp(MemoryBlock_Header_Size, align);
Assert(size <= (U32_Max - arena->page_size) - pad);
U32 needed = pad + size;
U32 new_block_size = arena->block_size;
U32 new_commit_size = arena->commit_size;
if (needed > new_block_size) {
new_block_size = AlignUp(needed, arena->page_size);
new_commit_size = new_block_size;
}
MemoryBlock *new_block = memoryblock_allocate(new_size);
new_block->base_pos = block->base_pos + block->size;
MemoryBlock *new_block = memoryblock_allocate(new_block_size, new_commit_size, arena->page_size);
// arena_offset tracks the virtual absolute position within an arena
// This is not a contiguous space, we use it to free blocks when popping the arena
new_block->arena_offset = block->arena_offset + block->size;
new_block->prev = block;
arena->current = new_block;
block = new_block;
start_pos = AlignUpPow2(block->pos, align);
start_pos = AlignUp(block->first_free, align);
}
U32 end_pos = start_pos + size;
Assert(end_pos <= block->size);
U8 *mem = &block->base[start_pos];
block->pos = end_pos;
U8 *mem = &block->base[start_pos];
if (block->committed_end < end_pos) {
// Commit more memory
U32 aligned_end_pos = AlignUp(start_pos + size, arena->page_size);
U32 aligned_size = RoundUpToMultiple(aligned_end_pos - block->committed_end, arena->commit_size);
aligned_size = Min(aligned_size, block->size - block->committed_end);
memory_commit(&block->base[block->committed_end], aligned_size);
block->committed_end += aligned_size;
Assert(block->committed_end <= block->size);
}
block->first_free = end_pos;
ASAN_UNPOISON_MEMORY_REGION(mem, size);
return mem;
}
void arena_pop_to(Arena *arena, U32 abs_pos)
void arena_reset_to(Arena *arena, U64 total_offset)
{
MemoryBlock *block = arena->current;
abs_pos = Max(MemoryBlock_Header_Size, abs_pos);
total_offset = Max(MemoryBlock_Header_Size, total_offset);
while (block) {
MemoryBlock *prev = block->prev;
if (block->base_pos >= abs_pos) {
ASAN_UNPOISON_MEMORY_REGION(block->base, block->size);
if (block->arena_offset >= total_offset) {
ASAN_UNPOISON_MEMORY_REGION(block->base, block->committed_end);
memory_free(block->base, block->size);
arena->current = prev;
} else {
U32 new_pos = abs_pos - block->base_pos;
U64 new_pos = total_offset - block->arena_offset;
new_pos = Max(MemoryBlock_Header_Size, new_pos);
// Note, when doing commit/release, new_pos <= current->pos
// is the right thing . shouldn't be able to pop to larger than what's already commited
// Disallow arbitrary pops to positions that previously were not commited.
Assert(new_pos <= block->committed_end);
Assert(new_pos <= block->size);
block->pos = new_pos;
ASAN_POISON_MEMORY_REGION(&block->base[block->pos], memoryblock_available(block));
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;
}
@@ -104,25 +138,39 @@ void arena_pop_to(Arena *arena, U32 abs_pos)
}
}
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.absolute_pos = arena_used(arena);
arena->checkpoint_level++;
checkpoint.total_offset = arena_used(arena);
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);
arena_pop_to(arena, checkpoint->absolute_pos);
arena->checkpoint_level--;
Assert(arena->temp_level > 0);
arena_reset_to(arena, checkpoint->total_offset);
arena->temp_level--;
}
void arena_reset(Arena *arena)
{
arena_pop_to(arena, 0);
arena_reset_to(arena, 0);
}
+59 -23
View File
@@ -2,63 +2,99 @@
#include "base_core.h"
typedef U32 ArenaFlags; // no flags defined yets
typedef struct ArenaParams ArenaParams;
struct ArenaParams {
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;
ArenaFlags flags; // No flags defined yet
};
typedef struct MemoryBlock MemoryBlock;
struct MemoryBlock {
U8 *base;
U32 size;
U32 base_pos; // absolute position of base relative to the first linked arena
U32 pos; // start of next allocation
U32 size; // Block size, page aligned
U32 first_free; // start of next allocation
U64 arena_offset; // absolute offset of base relative to the first linked arena
U32 committed_end; // end of committed region. Will be page aligned
MemoryBlock *prev;
};
typedef struct Arena Arena;
struct Arena {
MemoryBlock *current;
U32 requested_size;
U32 block_size; // requested block size
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;
U32 absolute_pos;
U64 total_offset;
};
#ifndef ArenaOptions_Default
#define ArenaOptions_Default ((ArenaOptions){ .size = MB(32), .commit_size = KB(64) })
#endif
StaticAssert(sizeof(MemoryBlock) <= 64);
// Try keep start of user memory at a cacheline
#define MemoryBlock_Header_Size 64u
void arena_init(Arena *arena, const ArenaParams *params);
void arena_init(Arena *arena, const ArenaOptions *params);
void arena_free(Arena *arena);
void *arena_push(Arena *arena, U32 size, U32 align);
#define arena_push_type(arena, type) arena_push(arena, (U32)sizeof(type), alignof(type))
// 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_pop_to(Arena *arena, U32 abs_pos);
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);
MemoryBlock *memoryblock_allocate(size_t size);
static INLINE U32 memoryblock_available(const MemoryBlock *block)
{
return block->size - block->pos;
return block->size - block->first_free;
}
static INLINE U32 arena_used(const Arena *arena)
static INLINE U64 arena_used(const Arena *arena)
{
const MemoryBlock *block = arena->current;
return block->base_pos + block->pos;
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;
}
+96 -13
View File
@@ -41,10 +41,11 @@ typedef unsigned __int128 U128;
#define U32_Max ((U32)0xffffffff)
#define U64_Max ((U64)0xffffffffffffffffULL)
#define U8_Min ((U8) 0x80)
#define U16_Min ((U16)0x8000)
#define U32_Min ((U32)0x80000000)
#define U64_Min ((U64)0x8000000000000000ULL)
// Duh ..
#define U8_Min ((U8)0x0)
#define U16_Min ((U16)0x0)
#define U32_Min ((U32)0x0)
#define U64_Min ((U64)0x0)
#define S8_Max ((S8) 0x7f)
#define S16_Max ((S16)0x7fff)
@@ -103,10 +104,43 @@ typedef unsigned __int128 U128;
(const type *)((const void *)(const unsigned char *)__mptr - offsetof(const type, member)); \
})
#define AlignUpPow2(val, align) (((val) + (align) - 1UL) & ~((align) - 1UL))
#define AlignDownPow2(val, align) ((val) & ~((align) - 1UL))
#define IsPow2(x) ((x) && !((x) & ((x) - 1UL)))
#define IsPow2OrZero(x) (!((x) & ((x) - 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
@@ -132,7 +166,7 @@ typedef unsigned __int128 U128;
#endif
/**
* Round up x / y
* Ceiling of X / y , or round up x / y
* (e.g 100/9 == 12, 3/2 == 2)
* x + y must not overflow.
*
@@ -140,10 +174,11 @@ typedef unsigned __int128 U128;
* @param y denominator
* @return x/y rounded up
*/
#define DivRoundUp(x, y) (((x) + ((y) - 1)) / (y))
#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
*
@@ -151,7 +186,15 @@ typedef unsigned __int128 U128;
* @param y denominator
* @return x rounded up to nearest multiple of y
*/
#define RoundUp(x, y) (DIV_ROUND_UP((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
@@ -162,7 +205,7 @@ typedef unsigned __int128 U128;
* @param y denominator
* @return x rounded down nearest multiple of y
*/
#define RoundDown(x, y) ((x) / (y) * (y))
#define RoundDownToMultiple(x, y) ((x) / (y) * (y))
#define Clamp(val, min, max) (((val) < (min)) ? (min) : ((val) > (max)) ? (max) : (val))
#ifdef __GNUC__
@@ -301,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>
+360 -19
View File
@@ -1,18 +1,23 @@
#include "base_core.h"
#include "arena.h"
#include "mini_arena.h"
#include "platform.h"
#include "pool.h"
#include <stdio.h>
#include <stdlib.h>
void arena_dump(const Arena *arena)
{
return;
MemoryBlock *block = arena->current;
puts("@@@@@@@@@@");
while (block) {
puts("-----Arena-----");
printf("base: %p\n", block->base);
printf("size: %u\n", block->size);
printf("pos: %u\n", block->pos);
printf("base_pos: %u\n", block->base_pos);
printf("first_free: %u\n", block->first_free);
printf("total_used: %lu\n",block->arena_offset);
printf("prev: %p\n", block->prev);
puts("-----Arena end-");
block = block->prev;
@@ -20,8 +25,12 @@ void arena_dump(const Arena *arena)
}
static void arena_test_1(void)
{
ArenaParams arena_params = {};
arena_params.size = 1024+MemoryBlock_Header_Size;
TRACEF("\n");
ArenaOptions arena_params = {};
U32 arena_size = MB(1) + MemoryBlock_Header_Size;
U32 commit_size = KB(8);
arena_params.size = arena_size;
arena_params.commit_size = commit_size;
Arena arena;
arena_init(&arena, &arena_params);
arena_dump(&arena);
@@ -40,48 +49,380 @@ static void arena_test_1(void)
arena_dump(&arena);
arena_free(&arena);
}
static void arena_test_2(void)
{
ArenaParams arena_params = {};
arena_params.size = 1024+MemoryBlock_Header_Size;
TRACEF("\n");
ArenaOptions arena_params = {};
U32 arena_size = MB(3) + MemoryBlock_Header_Size;
U32 commit_size = KB(72);
U32 push_size = KB(32);
arena_params.size = arena_size;
arena_params.commit_size = commit_size;
Arena arena;
arena_init(&arena, &arena_params);
void *ptr;
ptr = arena_push(&arena, 1024, 8);
MemoryZero(ptr, 1024);
ptr = arena_push(&arena, push_size, 8);
MemoryZero(ptr, push_size);
printf("Allocated ptr %p\n", ptr);
arena_dump(&arena);
ArenaCheckPoint checkpoint = arena_temp_begin(&arena);
ptr = arena_push(&arena, 4096, 8);
MemoryZero(ptr, 4096);
ATemp checkpoint = arena_temp_begin(&arena);
ptr = arena_push(&arena, MB(4)+3, 8);
MemoryZero(ptr, MB(2));
ptr = arena_push(&arena, 4096, 8);
MemoryZero(ptr, 4096);
ptr = arena_push(&arena, commit_size * 3, 8);
MemoryZero(ptr, commit_size * 3 );
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);
arena_temp_end(&checkpoint2);
arena_temp_end(&checkpoint);
arena_pop_to(&arena, 1088);
arena_reset_to(&arena, 1088);
arena_dump(&arena);
ptr = arena_push(&arena, 1, 1);
ptr = arena_push(&arena, 1, KB(16));
((U8*)ptr)[0] = 0x37;
ptr = arena_push(&arena, 1, 1);
ptr = arena_push(&arena, 1, KB(16));
((U8*)ptr)[0] = 0x73;
arena_pop_to(&arena, 1089);
ptr = arena_push(&arena, 0, KB(4));
//((U8*)ptr)[0] = 0x73;
// arena_reset_to(&arena, 1089);
arena_dump(&arena);
arena_pop_to(&arena, 0);
arena_reset_to(&arena, 0);
for (U64 i = 0; i < MB(4); i++) {
ptr = arena_push(&arena, 3, 1);
MemoryZero(ptr, 3);
}
arena_free(&arena);
}
static void arena_test_3(void)
{
TRACEF("\n");
ArenaOptions arena_params = {};
U32 arena_size = KB(97) + MemoryBlock_Header_Size;
U32 commit_size = KB(97);
arena_params.size = arena_size;
arena_params.commit_size = commit_size;
Arena arena;
arena_init(&arena, &arena_params);
arena_dump(&arena);
void *ptr = arena_push(&arena, KB(64), 32);
MemoryZero(ptr, KB(64));
printf("Allocated ptr %p\n", ptr);
arena_dump(&arena);
ptr = arena_push(&arena, KB(97) - KB(64), 32);
MemoryZero(ptr, KB(97) - KB(64));
printf("Allocated ptr %p\n", ptr);
arena_dump(&arena);
arena_free(&arena);
}
// Exercises the alignment-padding edge case: a fresh block has first_free at
// MemoryBlock_Header_Size (64), so a large alignment pushes the user pointer
// well past the header. Requesting (block_size - 64) bytes with page alignment
// previously tripped the end_pos <= block->size assert because the alignment
// gap was not accounted for. After the fix this should allocate a larger block.
static void arena_test_4(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);
// 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 = arena_push(&arena, 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 = arena_push(&arena, 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);
arena_free(&arena);
}
static void arena_test_allocator_interface(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);
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();
}
+121
View File
@@ -0,0 +1,121 @@
#pragma once
#include "base_core.h"
// 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 MTemp MTemp;
struct MTemp {
U32 offset;
MArena *arena;
};
#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##_marena_memory; \
MArena var_name = MARENA_STATIC_INITIALIZER(var_name##_marena_memory.memory)
static INLINE void marena_init(MArena *arena, void *memory, U32 size)
{
Assert(memory != NULL);
arena->base = (U8 *)memory;
arena->first_free = 0;
arena->size = size;
}
static INLINE MTemp marena_temp_begin(MArena *arena)
{
MTemp checkpoint = {};
checkpoint.arena = arena;
checkpoint.offset = arena->first_free;
arena->temp_level++;
return checkpoint;
}
static INLINE void marena_temp_end(MTemp *state)
{
MArena *arena = state->arena;
Assert(arena->temp_level > 0);
arena->first_free = state->offset;
arena->temp_level--;
}
static INLINE void *marena_push(MArena *arena, U32 size)
{
if (size > arena->size - arena->first_free) {
return NULL;
}
void *base = &arena->base[arena->first_free];
arena->first_free += size;
return base;
}
[[gnu::alloc_align(3)]]
static INLINE void *marena_push_aligned(MArena *arena, U32 size, U32 align)
{
Assert(IsPow2(align));
U32 aligned = AlignUp(arena->first_free, align);
if (aligned < arena->first_free) { // aligned wrapped around
return NULL;
}
if (aligned > arena->size || size > arena->size - aligned) {
return NULL;
}
void *base = &arena->base[aligned];
arena->first_free = aligned + size;
return base;
}
#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->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;
}
+72 -44
View File
@@ -1,29 +1,16 @@
#include "base_core.h"
#include "platform.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#define IsPageAligned(x) (IsAlignedTo(x, platform_page_size()))
U32 platform_page_size(void)
{
return getpagesize();
}
static INLINE size_t AlignToPageSize(size_t size)
{
U32 page_size = platform_page_size();
size_t aligned_size = AlignUpPow2(size, page_size);
return aligned_size;
}
static INLINE size_t AlignToPageStart(size_t size)
{
U32 page_size = platform_page_size();
size_t aligned_size = AlignDownPow2(size, page_size);
return aligned_size;
return sysconf(_SC_PAGESIZE);
}
void *platform_malloc(size_t size)
@@ -37,53 +24,94 @@ void platform_free(void *mem)
}
void *memory_allocate(size_t size)
static void *platform_allocator_func(Allocator *allocator, AllocOperation operation , void *memory, U32 size, U32 align)
{
if (size == 0) {
size = 1;
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;
}
size_t aligned_size = AlignToPageSize(size);
void *mem = mmap(0, aligned_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
TRACEF("memory_allocate size=%zu aligned_size=%zu mem=%p\n", size, aligned_size, mem);
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));
void *mem = mmap(0, size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
TRACEF("memory_allocate size=%7zu mem=%p\n", size, mem);
if (mem == MAP_FAILED) {
mem = NULL;
} else {
int rc = madvise(mem, size, MADV_POPULATE_WRITE);
if (rc != 0) {
munmap(mem, size);
mem = NULL;
}
}
return mem;
}
void memory_free(void *mem, size_t size)
{
TRACEF(" memory_free size=%7zu mem=%p\n", size, mem);
Assert(IsPageAligned((uintptr_t)mem));
Assert(IsPageAligned(size));
int rc = munmap(mem, size);
Assert(rc == 0);
}
void *memory_reserve(size_t size)
{
Assert(IsPageAligned(size));
void *mem = mmap(0, size, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
TRACEF("memory_reserve size=%7zu mem=%p\n", size, mem);
if (mem == MAP_FAILED) {
mem = NULL;
}
return mem;
}
void memory_free(void *mem, size_t size)
{
size_t aligned_size = AlignToPageSize(size);
TRACEF(" memory_free size=%zu aligned_size=%zu mem=%p\n", size, aligned_size, mem);
int rc = munmap(mem, aligned_size);
Assert(rc == 0);
}
void memory_commit(void *mem, size_t size)
{
size_t aligned_size = AlignToPageSize(size);
void *aligned_mem = (void *)AlignToPageStart((uintptr_t)mem);
TRACEF("memory_commit size=%zu aligned_size=%zu ptr=%p aligned_mem=%p\n", size, aligned_size, mem, aligned_mem);
Assert(IsPageAligned((uintptr_t)mem));
Assert(IsPageAligned(size));
TRACEF("memory_commit size=%7zu mem=%p\n", size, mem);
int rc;
rc = mprotect(aligned_mem, aligned_size, PROT_READ | PROT_WRITE);
rc = mprotect(mem, size, PROT_READ | PROT_WRITE);
Assert(rc == 0);
rc = madvise(aligned_mem, aligned_size, MADV_WILLNEED);
rc = madvise(mem, size, MADV_WILLNEED);
Assert(rc == 0);
}
void memory_decommit(void *mem, size_t size)
{
size_t aligned_size = AlignToPageSize(size);
void *aligned_mem = (void *)AlignToPageStart((uintptr_t)mem);
TRACEF("memory_commit size=%zu aligned_size=%zu ptr=%p aligned_mem=%p\n", size, aligned_size, mem, aligned_mem);
TRACEF("memory_decommit size=%7zu mem=%p\n", size, mem);
Assert(IsPageAligned((uintptr_t)mem));
Assert(IsPageAligned(size));
int rc;
rc = mprotect(aligned_mem, aligned_size, PROT_NONE);
rc = mprotect(mem, size, PROT_NONE);
Assert(rc == 0);
rc = madvise(aligned_mem, aligned_size, MADV_DONTNEED);
rc = madvise(mem, size, MADV_DONTNEED);
Assert(rc == 0);
}
+9 -3
View File
@@ -1,10 +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);
void *memory_allocate(size_t size);
void *memory_free(void *mem, size_t size);
// 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
View File
@@ -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
View File
@@ -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);