Compare commits
12 Commits
main
..
1bcf7d8ae6
| Author | SHA1 | Date | |
|---|---|---|---|
| 1bcf7d8ae6 | |||
| f805f4b49c | |||
| f4cf833339 | |||
| 41530a2a53 | |||
| 78eb30d67f | |||
| b06b78aeef | |||
| a092599a1a | |||
| 3de3206706 | |||
| 7dfe547e19 | |||
| 6937d066a5 | |||
| 81300eca52 | |||
| 1477f5b396 |
+54
-59
@@ -2,25 +2,34 @@
|
|||||||
#include "base_core.h"
|
#include "base_core.h"
|
||||||
#include "platform.h"
|
#include "platform.h"
|
||||||
|
|
||||||
|
typedef struct MemoryBlockOptions MemoryBlockOptions;
|
||||||
static MemoryBlock *memoryblock_allocate(U32 size, U32 commit_size, U32 page_size)
|
struct MemoryBlockOptions {
|
||||||
|
U32 size;
|
||||||
|
U32 commit_size;
|
||||||
|
U32 commit_now_size;
|
||||||
|
U32 page_size;
|
||||||
|
};
|
||||||
|
static U32 idx;
|
||||||
|
static MemoryBlock *memoryblock_allocate(const MemoryBlockOptions *opts)
|
||||||
{
|
{
|
||||||
Assert(IsAlignedTo(size, page_size));
|
U32 aligned_size = AlignUpPow2(opts->size, opts->page_size);
|
||||||
Assert(IsAlignedTo(commit_size, page_size));
|
U32 aligned_commit_size = AlignUpPow2(opts->commit_size, opts->page_size);
|
||||||
|
U32 aligned_commit_now_size = AlignUpPow2(opts->commit_now_size, opts->page_size);
|
||||||
|
|
||||||
MemoryBlock *block = memory_reserve(size);
|
MemoryBlock *block = memory_reserve(aligned_size);
|
||||||
Assert(block != NULL);
|
Assert(block != NULL);
|
||||||
memory_commit(block, commit_size);
|
memory_commit(block, aligned_commit_now_size);
|
||||||
|
|
||||||
ASAN_POISON_MEMORY_REGION(block, size);
|
|
||||||
ASAN_UNPOISON_MEMORY_REGION(block, MemoryBlock_Header_Size);
|
|
||||||
|
|
||||||
block->base = (U8 *)block;
|
block->base = (U8 *)block;
|
||||||
block->size = size;
|
block->size = aligned_size;
|
||||||
block->first_free = MemoryBlock_Header_Size;
|
block->first_free = MemoryBlock_Header_Size;
|
||||||
block->arena_offset = 0;
|
block->arena_offset = 0;
|
||||||
block->prev = NULL;
|
block->prev = NULL;
|
||||||
block->committed_end = commit_size;
|
block->committed_end = aligned_commit_now_size;
|
||||||
|
block->idx = idx++;
|
||||||
|
|
||||||
|
ASAN_POISON_MEMORY_REGION(block->base, block->size);
|
||||||
|
ASAN_UNPOISON_MEMORY_REGION(block, MemoryBlock_Header_Size);
|
||||||
return block;
|
return block;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,25 +39,28 @@ void arena_init(Arena *arena, const ArenaOptions *opts)
|
|||||||
Assert(opts->size > MemoryBlock_Header_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->size < (U32_Max - page_size)); // Reserving last piece so our math doesn't overflow
|
||||||
Assert(opts->commit_size > 0);
|
Assert(opts->commit_size > 0);
|
||||||
|
Assert(opts->commit_size <= opts->size);
|
||||||
|
|
||||||
U32 aligned_commit_size = AlignUp(opts->commit_size, page_size);
|
MemoryBlockOptions mb_opts;
|
||||||
U32 aligned_size = AlignUp(opts->size, page_size);
|
mb_opts.page_size = page_size;
|
||||||
Assert(aligned_commit_size <= aligned_size);
|
mb_opts.commit_size = opts->commit_size;
|
||||||
|
mb_opts.size = opts->size;
|
||||||
MemoryBlock *block = memoryblock_allocate(aligned_size, aligned_commit_size, page_size);
|
mb_opts.commit_now_size = opts->commit_size;
|
||||||
|
MemoryBlock *block = memoryblock_allocate(&mb_opts);
|
||||||
|
|
||||||
arena->flags = opts->flags;
|
arena->flags = opts->flags;
|
||||||
arena->temp_level = 0;
|
arena->checkpoint_level = 0;
|
||||||
arena->current = block;
|
arena->current = block;
|
||||||
arena->block_size = aligned_size;
|
arena->block_size = opts->size;
|
||||||
arena->commit_size = aligned_commit_size;
|
arena->commit_size = opts->commit_size;
|
||||||
arena->page_size = page_size;
|
arena->page_size = page_size;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void arena_free(Arena *arena)
|
void arena_free(Arena *arena)
|
||||||
{
|
{
|
||||||
MemoryBlock *block = arena->current;
|
MemoryBlock *block = arena->current;
|
||||||
Assert(arena->temp_level == 0);
|
Assert(arena->checkpoint_level == 0);
|
||||||
while (block != NULL) {
|
while (block != NULL) {
|
||||||
MemoryBlock *prev = block->prev;
|
MemoryBlock *prev = block->prev;
|
||||||
memory_free(block->base, block->size);
|
memory_free(block->base, block->size);
|
||||||
@@ -59,32 +71,35 @@ void arena_free(Arena *arena)
|
|||||||
|
|
||||||
void *arena_push(Arena *arena, U32 size, U32 align)
|
void *arena_push(Arena *arena, U32 size, U32 align)
|
||||||
{
|
{
|
||||||
|
Assert(size < (U32_Max - platform_page_size()));
|
||||||
Assert(IsPow2(align));
|
Assert(IsPow2(align));
|
||||||
|
|
||||||
|
|
||||||
MemoryBlock *block = arena->current;
|
MemoryBlock *block = arena->current;
|
||||||
U32 start_pos = AlignUp(block->first_free, align); // might align past block->size, handled in below check
|
U32 start_pos = AlignUpPow2(block->first_free, align); // might align past block->size, handled in below check
|
||||||
U32 available = block->size - start_pos;
|
U32 available = block->size - start_pos;
|
||||||
|
|
||||||
if (start_pos > block->size || available < size) {
|
if (start_pos > block->size || available < size) {
|
||||||
U32 pad = AlignUp(MemoryBlock_Header_Size, align);
|
U32 block_size = arena->block_size; // original user requested size
|
||||||
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;
|
U32 new_commit_size = arena->commit_size;
|
||||||
if (needed > new_block_size) {
|
if (size > block_size - MemoryBlock_Header_Size) {
|
||||||
new_block_size = AlignUp(needed, arena->page_size);
|
block_size = AlignUpPow2(size + MemoryBlock_Header_Size,align);
|
||||||
new_commit_size = new_block_size;
|
new_commit_size = block_size;
|
||||||
}
|
}
|
||||||
|
U32 commit_now_size = Max(new_commit_size, size);
|
||||||
|
|
||||||
MemoryBlock *new_block = memoryblock_allocate(new_block_size, new_commit_size, arena->page_size);
|
MemoryBlockOptions mb_opts;
|
||||||
|
mb_opts.page_size = arena->page_size;
|
||||||
|
mb_opts.commit_size = new_commit_size;
|
||||||
|
mb_opts.size = block_size;
|
||||||
|
mb_opts.commit_now_size = commit_now_size;
|
||||||
|
MemoryBlock *new_block = memoryblock_allocate(&mb_opts);
|
||||||
// arena_offset tracks the virtual absolute position within an arena
|
// 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
|
// 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->arena_offset = block->arena_offset + block->size;
|
||||||
new_block->prev = block;
|
new_block->prev = block;
|
||||||
arena->current = new_block;
|
arena->current = new_block;
|
||||||
block = new_block;
|
block = new_block;
|
||||||
start_pos = AlignUp(block->first_free, align);
|
start_pos = AlignUpPow2(block->first_free, align);
|
||||||
}
|
}
|
||||||
|
|
||||||
U32 end_pos = start_pos + size;
|
U32 end_pos = start_pos + size;
|
||||||
@@ -93,7 +108,7 @@ void *arena_push(Arena *arena, U32 size, U32 align)
|
|||||||
U8 *mem = &block->base[start_pos];
|
U8 *mem = &block->base[start_pos];
|
||||||
if (block->committed_end < end_pos) {
|
if (block->committed_end < end_pos) {
|
||||||
// Commit more memory
|
// Commit more memory
|
||||||
U32 aligned_end_pos = AlignUp(start_pos + size, arena->page_size);
|
U32 aligned_end_pos = AlignUpPow2(start_pos + size, arena->page_size);
|
||||||
U32 aligned_size = RoundUpToMultiple(aligned_end_pos - block->committed_end, arena->commit_size);
|
U32 aligned_size = RoundUpToMultiple(aligned_end_pos - block->committed_end, arena->commit_size);
|
||||||
aligned_size = Min(aligned_size, block->size - block->committed_end);
|
aligned_size = Min(aligned_size, block->size - block->committed_end);
|
||||||
|
|
||||||
@@ -115,7 +130,7 @@ void arena_reset_to(Arena *arena, U64 total_offset)
|
|||||||
MemoryBlock *prev = block->prev;
|
MemoryBlock *prev = block->prev;
|
||||||
|
|
||||||
if (block->arena_offset >= total_offset) {
|
if (block->arena_offset >= total_offset) {
|
||||||
ASAN_UNPOISON_MEMORY_REGION(block->base, block->committed_end);
|
ASAN_UNPOISON_MEMORY_REGION(block->base, block->size);
|
||||||
memory_free(block->base, block->size);
|
memory_free(block->base, block->size);
|
||||||
arena->current = prev;
|
arena->current = prev;
|
||||||
} else {
|
} else {
|
||||||
@@ -123,14 +138,8 @@ void arena_reset_to(Arena *arena, U64 total_offset)
|
|||||||
new_pos = Max(MemoryBlock_Header_Size, new_pos);
|
new_pos = Max(MemoryBlock_Header_Size, new_pos);
|
||||||
// Disallow arbitrary pops to positions that previously were not commited.
|
// Disallow arbitrary pops to positions that previously were not commited.
|
||||||
Assert(new_pos <= block->committed_end);
|
Assert(new_pos <= block->committed_end);
|
||||||
Assert(new_pos <= block->size);
|
|
||||||
block->first_free = new_pos;
|
block->first_free = new_pos;
|
||||||
ASAN_POISON_MEMORY_REGION(&block->base[block->first_free], memoryblock_available(block));
|
ASAN_POISON_MEMORY_REGION(&block->base[block->first_free], memoryblock_available(block));
|
||||||
|
|
||||||
if ((arena->flags & ArenaFlag_NoDecommit) == 0) {
|
|
||||||
arena_trim(arena);
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,39 +147,25 @@ void arena_reset_to(Arena *arena, U64 total_offset)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void arena_trim(Arena *arena)
|
ArenaCheckpoint arena_temp_begin(Arena *arena)
|
||||||
{
|
{
|
||||||
MemoryBlock *block = arena->current;
|
ArenaCheckpoint checkpoint = {};
|
||||||
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.arena = arena;
|
||||||
checkpoint.total_offset = arena_used(arena);
|
checkpoint.total_offset = arena_used(arena);
|
||||||
arena->temp_level++;
|
arena->checkpoint_level++;
|
||||||
|
|
||||||
return checkpoint;
|
return checkpoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
void arena_temp_end(ATemp *checkpoint)
|
void arena_temp_end(ArenaCheckpoint *checkpoint)
|
||||||
{
|
{
|
||||||
Arena *arena = checkpoint->arena;
|
Arena *arena = checkpoint->arena;
|
||||||
Assert(arena->temp_level > 0);
|
Assert(arena->checkpoint_level > 0);
|
||||||
arena_reset_to(arena, checkpoint->total_offset);
|
arena_reset_to(arena, checkpoint->total_offset);
|
||||||
arena->temp_level--;
|
arena->checkpoint_level--;
|
||||||
}
|
}
|
||||||
|
|
||||||
void arena_reset(Arena *arena)
|
void arena_reset(Arena *arena)
|
||||||
{
|
{
|
||||||
arena_reset_to(arena, 0);
|
arena_reset_to(arena, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+8
-39
@@ -4,9 +4,6 @@
|
|||||||
|
|
||||||
typedef U32 ArenaFlags;
|
typedef U32 ArenaFlags;
|
||||||
typedef struct ArenaOptions ArenaOptions;
|
typedef struct ArenaOptions ArenaOptions;
|
||||||
enum {
|
|
||||||
ArenaFlag_NoDecommit, // arena_ temp_end/reset/reset_to doesn't decommit memory
|
|
||||||
};
|
|
||||||
struct ArenaOptions {
|
struct ArenaOptions {
|
||||||
U32 size;
|
U32 size;
|
||||||
U32 commit_size;
|
U32 commit_size;
|
||||||
@@ -21,6 +18,7 @@ struct MemoryBlock {
|
|||||||
U64 arena_offset; // absolute offset of base relative to the first linked arena
|
U64 arena_offset; // absolute offset of base relative to the first linked arena
|
||||||
U32 committed_end; // end of committed region. Will be page aligned
|
U32 committed_end; // end of committed region. Will be page aligned
|
||||||
MemoryBlock *prev;
|
MemoryBlock *prev;
|
||||||
|
U32 idx;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct Arena Arena;
|
typedef struct Arena Arena;
|
||||||
@@ -30,16 +28,16 @@ struct Arena {
|
|||||||
U32 commit_size; // requested commit size
|
U32 commit_size; // requested commit size
|
||||||
U32 page_size; // cached page size
|
U32 page_size; // cached page size
|
||||||
ArenaFlags flags;
|
ArenaFlags flags;
|
||||||
S32 temp_level;
|
S32 checkpoint_level;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct ATemp ATemp;
|
typedef struct ArenaCheckpoint ArenaCheckpoint;
|
||||||
struct ATemp {
|
struct ArenaCheckpoint {
|
||||||
Arena *arena;
|
Arena *arena;
|
||||||
U64 total_offset;
|
U64 total_offset;
|
||||||
};
|
};
|
||||||
#ifndef ArenaOptions_Default
|
#ifndef ArenaParams_Default
|
||||||
#define ArenaOptions_Default ((ArenaOptions){ .size = MB(32), .commit_size = KB(64) })
|
#define ArenaParams_Default { .size = MB(32), .commit_size = KB(64) }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
StaticAssert(sizeof(MemoryBlock) <= 64);
|
StaticAssert(sizeof(MemoryBlock) <= 64);
|
||||||
@@ -54,10 +52,8 @@ void *arena_push(Arena *arena, U32 size, U32 align);
|
|||||||
// Invalid for arrays > U32
|
// Invalid for arrays > U32
|
||||||
#define arena_push_array_of(arena, type, len) arena_push(arena, (U32) (sizeof(type) * (len)), alignof(type))
|
#define arena_push_array_of(arena, type, len) arena_push(arena, (U32) (sizeof(type) * (len)), alignof(type))
|
||||||
|
|
||||||
void arena_trim(Arena *arena);
|
ArenaCheckpoint arena_temp_begin(Arena *arena);
|
||||||
|
void arena_temp_end(ArenaCheckpoint *checkpoint);
|
||||||
ATemp arena_temp_begin(Arena *arena);
|
|
||||||
void arena_temp_end(ATemp *checkpoint);
|
|
||||||
void arena_reset_to(Arena *arena, U64 total_offset);
|
void arena_reset_to(Arena *arena, U64 total_offset);
|
||||||
void arena_reset(Arena *arena);
|
void arena_reset(Arena *arena);
|
||||||
|
|
||||||
@@ -71,30 +67,3 @@ static INLINE U64 arena_used(const Arena *arena)
|
|||||||
const MemoryBlock *block = arena->current;
|
const MemoryBlock *block = arena->current;
|
||||||
return block->arena_offset + block->first_free;
|
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;
|
|
||||||
}
|
|
||||||
|
|||||||
+7
-88
@@ -104,43 +104,11 @@ typedef unsigned __int128 U128;
|
|||||||
(const type *)((const void *)(const unsigned char *)__mptr - offsetof(const type, member)); \
|
(const type *)((const void *)(const unsigned char *)__mptr - offsetof(const type, member)); \
|
||||||
})
|
})
|
||||||
|
|
||||||
// `align` must be a power of two.
|
#define AlignUpPow2(val, align) (((val) + (align) - 1UL) & ~((align) - 1UL))
|
||||||
//
|
#define AlignDownPow2(val, align) ((val) & ~((align) - 1UL))
|
||||||
// These keep the result in the *same integer type as the first argument*
|
#define IsPow2(x) ((x) && !((x) & ((x) - 1UL)))
|
||||||
#define AlignUp(val, align) \
|
#define IsPow2OrZero(x) (!((x) & ((x) - 1UL)))
|
||||||
({ \
|
#define IsAlignedToPow2(x, y) (IsPow2(y) && !((x) & ((y) - 1UL)))
|
||||||
__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)
|
#define GetBit(val, idx) (((val) >> (idx)) & 1)
|
||||||
// Get n_bits starting at idx going left
|
// Get n_bits starting at idx going left
|
||||||
@@ -177,8 +145,7 @@ typedef unsigned __int128 U128;
|
|||||||
#define CeilDiv(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(30,48) == 48
|
||||||
* e.g. RoundUp(49,48) == 96
|
* e.g. RoundUp(49,48) == 96
|
||||||
*
|
*
|
||||||
@@ -186,15 +153,7 @@ typedef unsigned __int128 U128;
|
|||||||
* @param y denominator
|
* @param y denominator
|
||||||
* @return x rounded up to nearest multiple of y
|
* @return x rounded up to nearest multiple of y
|
||||||
*/
|
*/
|
||||||
#define RoundUpToMultiple(x, y) \
|
#define RoundUpToMultiple(x, y) (CeilDiv((x), (y)) * (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
|
* Round x down to nearest multiple of y
|
||||||
@@ -344,46 +303,6 @@ static INLINE U64 SaturatingMult64(U64 a, U64 b)
|
|||||||
return SaturatingMult(a, b, U64, U64_Max);
|
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)
|
// address-sanitizer helpers (-fsanitize=address)
|
||||||
#ifdef USE_ASAN
|
#ifdef USE_ASAN
|
||||||
#include <sanitizer/asan_interface.h>
|
#include <sanitizer/asan_interface.h>
|
||||||
|
|||||||
+8
-308
@@ -1,12 +1,8 @@
|
|||||||
#include "base_core.h"
|
#include "base_core.h"
|
||||||
#include "arena.h"
|
#include "arena.h"
|
||||||
#include "mini_arena.h"
|
|
||||||
#include "platform.h"
|
|
||||||
#include "pool.h"
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
void arena_dump(const Arena *arena)
|
void arena_dump(const Arena *arena)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@@ -55,8 +51,8 @@ static void arena_test_2(void)
|
|||||||
TRACEF("\n");
|
TRACEF("\n");
|
||||||
|
|
||||||
ArenaOptions arena_params = {};
|
ArenaOptions arena_params = {};
|
||||||
U32 arena_size = MB(3) + MemoryBlock_Header_Size;
|
U32 arena_size = MB(1) + MemoryBlock_Header_Size;
|
||||||
U32 commit_size = KB(72);
|
U32 commit_size = KB(16);
|
||||||
U32 push_size = KB(32);
|
U32 push_size = KB(32);
|
||||||
arena_params.size = arena_size;
|
arena_params.size = arena_size;
|
||||||
arena_params.commit_size = commit_size;
|
arena_params.commit_size = commit_size;
|
||||||
@@ -69,8 +65,8 @@ static void arena_test_2(void)
|
|||||||
printf("Allocated ptr %p\n", ptr);
|
printf("Allocated ptr %p\n", ptr);
|
||||||
|
|
||||||
arena_dump(&arena);
|
arena_dump(&arena);
|
||||||
ATemp checkpoint = arena_temp_begin(&arena);
|
ArenaCheckpoint checkpoint = arena_temp_begin(&arena);
|
||||||
ptr = arena_push(&arena, MB(4)+3, 8);
|
ptr = arena_push(&arena, MB(2), 8);
|
||||||
MemoryZero(ptr, MB(2));
|
MemoryZero(ptr, MB(2));
|
||||||
|
|
||||||
ptr = arena_push(&arena, commit_size * 3, 8);
|
ptr = arena_push(&arena, commit_size * 3, 8);
|
||||||
@@ -78,7 +74,7 @@ static void arena_test_2(void)
|
|||||||
ptr = arena_push(&arena, commit_size , 8);
|
ptr = arena_push(&arena, commit_size , 8);
|
||||||
MemoryZero(ptr, commit_size );
|
MemoryZero(ptr, commit_size );
|
||||||
|
|
||||||
ATemp checkpoint2 = arena_temp_begin(&arena);
|
ArenaCheckpoint checkpoint2 = arena_temp_begin(&arena);
|
||||||
ptr = arena_push(&arena, 64, 8);
|
ptr = arena_push(&arena, 64, 8);
|
||||||
MemoryZero(ptr, 64);
|
MemoryZero(ptr, 64);
|
||||||
|
|
||||||
@@ -93,13 +89,11 @@ static void arena_test_2(void)
|
|||||||
((U8*)ptr)[0] = 0x73;
|
((U8*)ptr)[0] = 0x73;
|
||||||
ptr = arena_push(&arena, 0, KB(4));
|
ptr = arena_push(&arena, 0, KB(4));
|
||||||
//((U8*)ptr)[0] = 0x73;
|
//((U8*)ptr)[0] = 0x73;
|
||||||
// arena_reset_to(&arena, 1089);
|
arena_reset_to(&arena, 1089);
|
||||||
arena_dump(&arena);
|
arena_dump(&arena);
|
||||||
arena_reset_to(&arena, 0);
|
arena_reset_to(&arena, 0);
|
||||||
for (U64 i = 0; i < MB(4); i++) {
|
ptr = arena_push(&arena, MB(1), 8);
|
||||||
ptr = arena_push(&arena, 3, 1);
|
MemoryZero(ptr, MB(1));
|
||||||
MemoryZero(ptr, 3);
|
|
||||||
}
|
|
||||||
arena_free(&arena);
|
arena_free(&arena);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,303 +120,9 @@ static void arena_test_3(void)
|
|||||||
arena_free(&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[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
arena_test_1();
|
arena_test_1();
|
||||||
arena_test_2();
|
arena_test_2();
|
||||||
arena_test_3();
|
arena_test_3();
|
||||||
arena_test_4();
|
|
||||||
arena_test_decommit();
|
|
||||||
arena_test_allocator_interface();
|
|
||||||
miniarena_test1();
|
|
||||||
apool_test();
|
|
||||||
apool_test_autogrow();
|
|
||||||
apool_test_miniarena();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,121 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
||||||
+2
-30
@@ -6,11 +6,11 @@
|
|||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
|
|
||||||
|
|
||||||
#define IsPageAligned(x) (IsAlignedTo(x, platform_page_size()))
|
#define IsPageAligned(x) (IsAlignedToPow2(x, platform_page_size()))
|
||||||
|
|
||||||
U32 platform_page_size(void)
|
U32 platform_page_size(void)
|
||||||
{
|
{
|
||||||
return sysconf(_SC_PAGESIZE);
|
return getpagesize();
|
||||||
}
|
}
|
||||||
|
|
||||||
void *platform_malloc(size_t size)
|
void *platform_malloc(size_t size)
|
||||||
@@ -23,34 +23,6 @@ void platform_free(void *mem)
|
|||||||
free(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)
|
void *memory_allocate_and_commit(size_t size)
|
||||||
{
|
{
|
||||||
Assert(IsPageAligned(size));
|
Assert(IsPageAligned(size));
|
||||||
|
|||||||
+1
-5
@@ -1,16 +1,12 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "base_core.h"
|
|
||||||
|
|
||||||
void *platform_malloc(size_t size);
|
void *platform_malloc(size_t size);
|
||||||
void platform_free(void *mem);
|
|
||||||
Allocator platform_allocator(void);
|
|
||||||
|
|
||||||
U32 platform_page_size(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_allocate_and_commit(size_t size);
|
||||||
void *memory_reserve(size_t size);
|
void *memory_reserve(size_t size);
|
||||||
void memory_commit(void *mem, size_t size);
|
void memory_commit(void *mem, size_t size);
|
||||||
void memory_decommit(void *mem, size_t size);
|
void memory_decommit(void *mem, size_t size);
|
||||||
|
|
||||||
void memory_free(void *mem, size_t size);
|
void memory_free(void *mem, size_t size);
|
||||||
|
|||||||
-54
@@ -1,54 +0,0 @@
|
|||||||
#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
@@ -1,26 +0,0 @@
|
|||||||
#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