From 4137ebfbd8a07aeb16dfa7f56c901963e40189bb Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sat, 30 Nov 2024 12:27:54 -0500 Subject: [PATCH] pool done (see previous commits for context) --- .vscode/settings.json | 3 +- project/dependencies/memory.cpp | 16 ++++----- project/dependencies/memory.hpp | 63 +++++++++++++++++++-------------- 3 files changed, 47 insertions(+), 35 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 6e65a84..65611a9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -37,7 +37,8 @@ "propidl.h": "c", "android_native_app_glue.h": "c", "raylib.h": "c", - "*.m": "cpp" + "*.m": "cpp", + "atomic": "cpp" }, "C_Cpp.intelliSenseEngineFallback": "disabled", "mesonbuild.configureOnOpen": true, diff --git a/project/dependencies/memory.cpp b/project/dependencies/memory.cpp index e51fb09..f11cb47 100644 --- a/project/dependencies/memory.cpp +++ b/project/dependencies/memory.cpp @@ -384,7 +384,7 @@ void* arena_allocator_proc( void* allocator_data, AllocType type, ssize size, ss return ptr; } -void* Pool::allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ) +void* pool_allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ) { Pool* pool = rcast( Pool*, allocator_data); void* ptr = NULL; @@ -457,7 +457,7 @@ void* Pool::allocator_proc( void* allocator_data, AllocType type, ssize size, ss return ptr; } -Pool Pool::init_align( AllocatorInfo backing, ssize num_blocks, ssize block_size, ssize block_align ) +Pool pool_init_align( AllocatorInfo backing, ssize num_blocks, ssize block_size, ssize block_align ) { Pool pool = {}; @@ -495,16 +495,16 @@ Pool Pool::init_align( AllocatorInfo backing, ssize num_blocks, ssize block_size return pool; } -void Pool::clear() +void clear(Pool& pool) { - ssize actual_block_size, block_index; + ssize actual_block_size, block_index; void* curr; uptr* end; - actual_block_size = BlockSize + BlockAlign; + actual_block_size = pool.BlockSize + pool.BlockAlign; - curr = PhysicalStart; - for ( block_index = 0; block_index < NumBlocks - 1; block_index++ ) + curr = pool.PhysicalStart; + for ( block_index = 0; block_index < pool.NumBlocks - 1; block_index++ ) { uptr* next = ( uptr* ) curr; *next = ( uptr ) curr + actual_block_size; @@ -514,7 +514,7 @@ void Pool::clear() end = ( uptr* ) curr; *end = ( uptr ) NULL; - FreeList = PhysicalStart; + pool.FreeList = pool.PhysicalStart; } #pragma endregion Memory diff --git a/project/dependencies/memory.hpp b/project/dependencies/memory.hpp index 2a704c7..7e2d857 100644 --- a/project/dependencies/memory.hpp +++ b/project/dependencies/memory.hpp @@ -358,30 +358,18 @@ using Arena_2MB = FixedArena< megabytes( 2 ) >; using Arena_4MB = FixedArena< megabytes( 4 ) >; #pragma endregion FixedArena +#pragma region Pool +struct Pool; + +AllocatorInfo allocator_info(Pool& pool); +void* pool_allocator_proc(void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags); +Pool pool_init(AllocatorInfo backing, ssize num_blocks, ssize block_size); +Pool pool_init_align(AllocatorInfo backing, ssize num_blocks, ssize block_size, ssize block_align); +void clear(Pool& pool); +void free(Pool& pool); + struct Pool { - static - void* allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ); - - static - Pool init( AllocatorInfo backing, ssize num_blocks, ssize block_size ) - { - return init_align( backing, num_blocks, block_size, GEN_DEFAULT_MEMORY_ALIGNMENT ); - } - - static - Pool init_align( AllocatorInfo backing, ssize num_blocks, ssize block_size, ssize block_align ); - - void clear(); - - void free() - { - if ( Backing.Proc ) - { - gen::free( Backing, PhysicalStart ); - } - } - AllocatorInfo Backing; void* PhysicalStart; void* FreeList; @@ -390,12 +378,35 @@ struct Pool ssize TotalSize; ssize NumBlocks; - operator AllocatorInfo() - { - return { allocator_proc, this }; - } +#pragma region Member Mapping + forceinline operator AllocatorInfo() { return GEN_NS allocator_info(* this); } + + forceinline static void* allocator_proc(void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags) { return GEN_NS pool_allocator_proc(allocator_data, type, size, alignment, old_memory, old_size, flags); } + forceinline static Pool init(AllocatorInfo backing, ssize num_blocks, ssize block_size) { return GEN_NS pool_init(backing, num_blocks, block_size); } + forceinline static Pool init_align(AllocatorInfo backing, ssize num_blocks, ssize block_size, ssize block_align) { return GEN_NS pool_init_align(backing, num_blocks, block_size, block_align); } + forceinline void clear() { GEN_NS clear(* this); } + forceinline void free() { GEN_NS free(* this); } +#pragma endregion }; +inline +AllocatorInfo allocator_info(Pool& pool) { + return { pool_allocator_proc, &pool }; +} + +inline +Pool pool_init(AllocatorInfo backing, ssize num_blocks, ssize block_size) { + return pool_init_align(backing, num_blocks, block_size, GEN_DEFAULT_MEMORY_ALIGNMENT); +} + +inline +void free(Pool& pool) { + if(pool.Backing.Proc) { + GEN_NS free(pool.Backing, pool.PhysicalStart); + } +} +#pragma endregion Pool + inline b32 is_power_of_two( ssize x ) { if ( x <= 0 )