Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1bcf7d8ae6 | |||
| f805f4b49c |
+53
-34
@@ -2,44 +2,57 @@
|
|||||||
#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)
|
||||||
{
|
{
|
||||||
U32 aligned_size = AlignUpPow2(size, page_size);
|
U32 aligned_size = AlignUpPow2(opts->size, opts->page_size);
|
||||||
U32 aligned_commit_size = AlignUpPow2(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(aligned_size);
|
MemoryBlock *block = memory_reserve(aligned_size);
|
||||||
Assert(block != NULL);
|
Assert(block != NULL);
|
||||||
memory_commit(block, aligned_commit_size);
|
memory_commit(block, aligned_commit_now_size);
|
||||||
|
|
||||||
block->base = (U8 *)block;
|
block->base = (U8 *)block;
|
||||||
block->size = aligned_size;
|
block->size = aligned_size;
|
||||||
block->pos = MemoryBlock_Header_Size;
|
block->first_free = MemoryBlock_Header_Size;
|
||||||
block->base_pos = 0;
|
block->arena_offset = 0;
|
||||||
block->prev = NULL;
|
block->prev = NULL;
|
||||||
block->committed_end = aligned_commit_size;
|
block->committed_end = aligned_commit_now_size;
|
||||||
|
block->idx = idx++;
|
||||||
|
|
||||||
ASAN_POISON_MEMORY_REGION(block->base, block->size);
|
ASAN_POISON_MEMORY_REGION(block->base, block->size);
|
||||||
ASAN_UNPOISON_MEMORY_REGION(block, MemoryBlock_Header_Size);
|
ASAN_UNPOISON_MEMORY_REGION(block, MemoryBlock_Header_Size);
|
||||||
return block;
|
return block;
|
||||||
}
|
}
|
||||||
|
|
||||||
void arena_init(Arena *arena, const ArenaParams *params)
|
void arena_init(Arena *arena, const ArenaOptions *opts)
|
||||||
{
|
{
|
||||||
U32 page_size = platform_page_size();
|
U32 page_size = platform_page_size();
|
||||||
Assert(params->size > MemoryBlock_Header_Size);
|
Assert(opts->size > MemoryBlock_Header_Size);
|
||||||
Assert(params->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(params->commit_size > 0);
|
Assert(opts->commit_size > 0);
|
||||||
Assert(params->commit_size <= params->size);
|
Assert(opts->commit_size <= opts->size);
|
||||||
|
|
||||||
|
MemoryBlockOptions mb_opts;
|
||||||
|
mb_opts.page_size = page_size;
|
||||||
|
mb_opts.commit_size = opts->commit_size;
|
||||||
|
mb_opts.size = opts->size;
|
||||||
|
mb_opts.commit_now_size = opts->commit_size;
|
||||||
|
MemoryBlock *block = memoryblock_allocate(&mb_opts);
|
||||||
|
|
||||||
MemoryBlock *block = memoryblock_allocate(params->size, params->commit_size, page_size);
|
arena->flags = opts->flags;
|
||||||
|
|
||||||
arena->flags = params->flags;
|
|
||||||
arena->checkpoint_level = 0;
|
arena->checkpoint_level = 0;
|
||||||
arena->current = block;
|
arena->current = block;
|
||||||
arena->block_size = params->size;
|
arena->block_size = opts->size;
|
||||||
arena->commit_size = params->commit_size;
|
arena->commit_size = opts->commit_size;
|
||||||
arena->page_size = page_size;
|
arena->page_size = page_size;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -62,7 +75,7 @@ void *arena_push(Arena *arena, U32 size, U32 align)
|
|||||||
Assert(IsPow2(align));
|
Assert(IsPow2(align));
|
||||||
|
|
||||||
MemoryBlock *block = arena->current;
|
MemoryBlock *block = arena->current;
|
||||||
U32 start_pos = AlignUpPow2(block->pos, 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) {
|
||||||
@@ -72,15 +85,21 @@ void *arena_push(Arena *arena, U32 size, U32 align)
|
|||||||
block_size = AlignUpPow2(size + MemoryBlock_Header_Size,align);
|
block_size = AlignUpPow2(size + MemoryBlock_Header_Size,align);
|
||||||
new_commit_size = block_size;
|
new_commit_size = block_size;
|
||||||
}
|
}
|
||||||
|
U32 commit_now_size = Max(new_commit_size, size);
|
||||||
|
|
||||||
MemoryBlock *new_block = memoryblock_allocate(block_size, new_commit_size, arena->page_size);
|
MemoryBlockOptions mb_opts;
|
||||||
// base_pos tracks the virtual absolute position within an arena
|
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
|
||||||
// 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->base_pos = block->base_pos + 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 = AlignUpPow2(block->pos, align);
|
start_pos = AlignUpPow2(block->first_free, align);
|
||||||
}
|
}
|
||||||
|
|
||||||
U32 end_pos = start_pos + size;
|
U32 end_pos = start_pos + size;
|
||||||
@@ -97,30 +116,30 @@ void *arena_push(Arena *arena, U32 size, U32 align)
|
|||||||
block->committed_end += aligned_size;
|
block->committed_end += aligned_size;
|
||||||
Assert(block->committed_end <= block->size);
|
Assert(block->committed_end <= block->size);
|
||||||
}
|
}
|
||||||
block->pos = end_pos;
|
block->first_free = end_pos;
|
||||||
|
|
||||||
ASAN_UNPOISON_MEMORY_REGION(mem, size);
|
ASAN_UNPOISON_MEMORY_REGION(mem, size);
|
||||||
return mem;
|
return mem;
|
||||||
}
|
}
|
||||||
|
|
||||||
void arena_pop_to(Arena *arena, U64 abs_pos)
|
void arena_reset_to(Arena *arena, U64 total_offset)
|
||||||
{
|
{
|
||||||
MemoryBlock *block = arena->current;
|
MemoryBlock *block = arena->current;
|
||||||
abs_pos = Max(MemoryBlock_Header_Size, abs_pos);
|
total_offset = Max(MemoryBlock_Header_Size, total_offset);
|
||||||
while (block) {
|
while (block) {
|
||||||
MemoryBlock *prev = block->prev;
|
MemoryBlock *prev = block->prev;
|
||||||
|
|
||||||
if (block->base_pos >= abs_pos) {
|
if (block->arena_offset >= total_offset) {
|
||||||
ASAN_UNPOISON_MEMORY_REGION(block->base, block->size);
|
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 {
|
||||||
U64 new_pos = abs_pos - block->base_pos;
|
U64 new_pos = total_offset - block->arena_offset;
|
||||||
new_pos = Max(MemoryBlock_Header_Size, new_pos);
|
new_pos = Max(MemoryBlock_Header_Size, new_pos);
|
||||||
// Disallow arbirary 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);
|
||||||
block->pos = new_pos;
|
block->first_free = new_pos;
|
||||||
ASAN_POISON_MEMORY_REGION(&block->base[block->pos], memoryblock_available(block));
|
ASAN_POISON_MEMORY_REGION(&block->base[block->first_free], memoryblock_available(block));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,7 +151,7 @@ ArenaCheckpoint arena_temp_begin(Arena *arena)
|
|||||||
{
|
{
|
||||||
ArenaCheckpoint checkpoint = {};
|
ArenaCheckpoint checkpoint = {};
|
||||||
checkpoint.arena = arena;
|
checkpoint.arena = arena;
|
||||||
checkpoint.absolute_pos = arena_used(arena);
|
checkpoint.total_offset = arena_used(arena);
|
||||||
arena->checkpoint_level++;
|
arena->checkpoint_level++;
|
||||||
|
|
||||||
return checkpoint;
|
return checkpoint;
|
||||||
@@ -142,11 +161,11 @@ void arena_temp_end(ArenaCheckpoint *checkpoint)
|
|||||||
{
|
{
|
||||||
Arena *arena = checkpoint->arena;
|
Arena *arena = checkpoint->arena;
|
||||||
Assert(arena->checkpoint_level > 0);
|
Assert(arena->checkpoint_level > 0);
|
||||||
arena_pop_to(arena, checkpoint->absolute_pos);
|
arena_reset_to(arena, checkpoint->total_offset);
|
||||||
arena->checkpoint_level--;
|
arena->checkpoint_level--;
|
||||||
}
|
}
|
||||||
|
|
||||||
void arena_reset(Arena *arena)
|
void arena_reset(Arena *arena)
|
||||||
{
|
{
|
||||||
arena_pop_to(arena, 0);
|
arena_reset_to(arena, 0);
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-11
@@ -2,10 +2,9 @@
|
|||||||
#include "base_core.h"
|
#include "base_core.h"
|
||||||
|
|
||||||
|
|
||||||
typedef U32 ArenaFlags; // no flags defined yet
|
typedef U32 ArenaFlags;
|
||||||
|
typedef struct ArenaOptions ArenaOptions;
|
||||||
typedef struct ArenaParams ArenaParams;
|
struct ArenaOptions {
|
||||||
struct ArenaParams {
|
|
||||||
U32 size;
|
U32 size;
|
||||||
U32 commit_size;
|
U32 commit_size;
|
||||||
ArenaFlags flags; // No flags defined yet
|
ArenaFlags flags; // No flags defined yet
|
||||||
@@ -15,10 +14,11 @@ typedef struct MemoryBlock MemoryBlock;
|
|||||||
struct MemoryBlock {
|
struct MemoryBlock {
|
||||||
U8 *base;
|
U8 *base;
|
||||||
U32 size; // Block size, page aligned
|
U32 size; // Block size, page aligned
|
||||||
U32 pos; // start of next allocation
|
U32 first_free; // start of next allocation
|
||||||
U64 base_pos; // absolute position 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;
|
||||||
@@ -34,7 +34,7 @@ struct Arena {
|
|||||||
typedef struct ArenaCheckpoint ArenaCheckpoint;
|
typedef struct ArenaCheckpoint ArenaCheckpoint;
|
||||||
struct ArenaCheckpoint {
|
struct ArenaCheckpoint {
|
||||||
Arena *arena;
|
Arena *arena;
|
||||||
U64 absolute_pos;
|
U64 total_offset;
|
||||||
};
|
};
|
||||||
#ifndef ArenaParams_Default
|
#ifndef ArenaParams_Default
|
||||||
#define ArenaParams_Default { .size = MB(32), .commit_size = KB(64) }
|
#define ArenaParams_Default { .size = MB(32), .commit_size = KB(64) }
|
||||||
@@ -44,7 +44,7 @@ StaticAssert(sizeof(MemoryBlock) <= 64);
|
|||||||
// Try keep start of user memory at a cacheline
|
// Try keep start of user memory at a cacheline
|
||||||
#define MemoryBlock_Header_Size 64u
|
#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_free(Arena *arena);
|
||||||
void *arena_push(Arena *arena, U32 size, U32 align);
|
void *arena_push(Arena *arena, U32 size, U32 align);
|
||||||
|
|
||||||
@@ -54,16 +54,16 @@ void *arena_push(Arena *arena, U32 size, U32 align);
|
|||||||
|
|
||||||
ArenaCheckpoint arena_temp_begin(Arena *arena);
|
ArenaCheckpoint arena_temp_begin(Arena *arena);
|
||||||
void arena_temp_end(ArenaCheckpoint *checkpoint);
|
void arena_temp_end(ArenaCheckpoint *checkpoint);
|
||||||
void arena_pop_to(Arena *arena, U64 abs_pos);
|
void arena_reset_to(Arena *arena, U64 total_offset);
|
||||||
void arena_reset(Arena *arena);
|
void arena_reset(Arena *arena);
|
||||||
|
|
||||||
static INLINE U32 memoryblock_available(const MemoryBlock *block)
|
static INLINE U32 memoryblock_available(const MemoryBlock *block)
|
||||||
{
|
{
|
||||||
return block->size - block->pos;
|
return block->size - block->first_free;
|
||||||
}
|
}
|
||||||
|
|
||||||
static INLINE U64 arena_used(const Arena *arena)
|
static INLINE U64 arena_used(const Arena *arena)
|
||||||
{
|
{
|
||||||
const MemoryBlock *block = arena->current;
|
const MemoryBlock *block = arena->current;
|
||||||
return block->base_pos + block->pos;
|
return block->arena_offset + block->first_free;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -108,7 +108,7 @@ typedef unsigned __int128 U128;
|
|||||||
#define AlignDownPow2(val, align) ((val) & ~((align) - 1UL))
|
#define AlignDownPow2(val, align) ((val) & ~((align) - 1UL))
|
||||||
#define IsPow2(x) ((x) && !((x) & ((x) - 1UL)))
|
#define IsPow2(x) ((x) && !((x) & ((x) - 1UL)))
|
||||||
#define IsPow2OrZero(x) (!((x) & ((x) - 1UL)))
|
#define IsPow2OrZero(x) (!((x) & ((x) - 1UL)))
|
||||||
#define IsAlignedTo(x, y) (IsPow2(y) && !((x) & ((y) - 1UL)))
|
#define IsAlignedToPow2(x, y) (IsPow2(y) && !((x) & ((y) - 1UL)))
|
||||||
|
|
||||||
#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
|
||||||
|
|||||||
+8
-8
@@ -12,8 +12,8 @@ void arena_dump(const Arena *arena)
|
|||||||
puts("-----Arena-----");
|
puts("-----Arena-----");
|
||||||
printf("base: %p\n", block->base);
|
printf("base: %p\n", block->base);
|
||||||
printf("size: %u\n", block->size);
|
printf("size: %u\n", block->size);
|
||||||
printf("pos: %u\n", block->pos);
|
printf("first_free: %u\n", block->first_free);
|
||||||
printf("base_pos: %lu\n",block->base_pos);
|
printf("total_used: %lu\n",block->arena_offset);
|
||||||
printf("prev: %p\n", block->prev);
|
printf("prev: %p\n", block->prev);
|
||||||
puts("-----Arena end-");
|
puts("-----Arena end-");
|
||||||
block = block->prev;
|
block = block->prev;
|
||||||
@@ -22,7 +22,7 @@ void arena_dump(const Arena *arena)
|
|||||||
static void arena_test_1(void)
|
static void arena_test_1(void)
|
||||||
{
|
{
|
||||||
TRACEF("\n");
|
TRACEF("\n");
|
||||||
ArenaParams arena_params = {};
|
ArenaOptions arena_params = {};
|
||||||
U32 arena_size = MB(1) + MemoryBlock_Header_Size;
|
U32 arena_size = MB(1) + MemoryBlock_Header_Size;
|
||||||
U32 commit_size = KB(8);
|
U32 commit_size = KB(8);
|
||||||
arena_params.size = arena_size;
|
arena_params.size = arena_size;
|
||||||
@@ -50,7 +50,7 @@ static void arena_test_2(void)
|
|||||||
{
|
{
|
||||||
TRACEF("\n");
|
TRACEF("\n");
|
||||||
|
|
||||||
ArenaParams arena_params = {};
|
ArenaOptions arena_params = {};
|
||||||
U32 arena_size = MB(1) + MemoryBlock_Header_Size;
|
U32 arena_size = MB(1) + MemoryBlock_Header_Size;
|
||||||
U32 commit_size = KB(16);
|
U32 commit_size = KB(16);
|
||||||
U32 push_size = KB(32);
|
U32 push_size = KB(32);
|
||||||
@@ -81,7 +81,7 @@ static void arena_test_2(void)
|
|||||||
arena_temp_end(&checkpoint2);
|
arena_temp_end(&checkpoint2);
|
||||||
|
|
||||||
arena_temp_end(&checkpoint);
|
arena_temp_end(&checkpoint);
|
||||||
arena_pop_to(&arena, 1088);
|
arena_reset_to(&arena, 1088);
|
||||||
arena_dump(&arena);
|
arena_dump(&arena);
|
||||||
ptr = arena_push(&arena, 1, KB(16));
|
ptr = arena_push(&arena, 1, KB(16));
|
||||||
((U8*)ptr)[0] = 0x37;
|
((U8*)ptr)[0] = 0x37;
|
||||||
@@ -89,9 +89,9 @@ 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_pop_to(&arena, 1089);
|
arena_reset_to(&arena, 1089);
|
||||||
arena_dump(&arena);
|
arena_dump(&arena);
|
||||||
arena_pop_to(&arena, 0);
|
arena_reset_to(&arena, 0);
|
||||||
ptr = arena_push(&arena, MB(1), 8);
|
ptr = arena_push(&arena, MB(1), 8);
|
||||||
MemoryZero(ptr, MB(1));
|
MemoryZero(ptr, MB(1));
|
||||||
arena_free(&arena);
|
arena_free(&arena);
|
||||||
@@ -101,7 +101,7 @@ static void arena_test_3(void)
|
|||||||
{
|
{
|
||||||
TRACEF("\n");
|
TRACEF("\n");
|
||||||
|
|
||||||
ArenaParams arena_params = {};
|
ArenaOptions arena_params = {};
|
||||||
U32 arena_size = KB(97) + MemoryBlock_Header_Size;
|
U32 arena_size = KB(97) + MemoryBlock_Header_Size;
|
||||||
U32 commit_size = KB(97);
|
U32 commit_size = KB(97);
|
||||||
arena_params.size = arena_size;
|
arena_params.size = arena_size;
|
||||||
|
|||||||
+1
-1
@@ -6,7 +6,7 @@
|
|||||||
#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)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user