pool done (see previous commits for context)

This commit is contained in:
Edward R. Gonzalez 2024-11-30 12:27:54 -05:00
parent 5958dd2055
commit 4137ebfbd8
3 changed files with 47 additions and 35 deletions

View File

@ -37,7 +37,8 @@
"propidl.h": "c", "propidl.h": "c",
"android_native_app_glue.h": "c", "android_native_app_glue.h": "c",
"raylib.h": "c", "raylib.h": "c",
"*.m": "cpp" "*.m": "cpp",
"atomic": "cpp"
}, },
"C_Cpp.intelliSenseEngineFallback": "disabled", "C_Cpp.intelliSenseEngineFallback": "disabled",
"mesonbuild.configureOnOpen": true, "mesonbuild.configureOnOpen": true,

View File

@ -384,7 +384,7 @@ void* arena_allocator_proc( void* allocator_data, AllocType type, ssize size, ss
return ptr; 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); Pool* pool = rcast( Pool*, allocator_data);
void* ptr = NULL; void* ptr = NULL;
@ -457,7 +457,7 @@ void* Pool::allocator_proc( void* allocator_data, AllocType type, ssize size, ss
return ptr; 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 = {}; Pool pool = {};
@ -495,16 +495,16 @@ Pool Pool::init_align( AllocatorInfo backing, ssize num_blocks, ssize block_size
return pool; return pool;
} }
void Pool::clear() void clear(Pool& pool)
{ {
ssize actual_block_size, block_index; ssize actual_block_size, block_index;
void* curr; void* curr;
uptr* end; uptr* end;
actual_block_size = BlockSize + BlockAlign; actual_block_size = pool.BlockSize + pool.BlockAlign;
curr = PhysicalStart; curr = pool.PhysicalStart;
for ( block_index = 0; block_index < NumBlocks - 1; block_index++ ) for ( block_index = 0; block_index < pool.NumBlocks - 1; block_index++ )
{ {
uptr* next = ( uptr* ) curr; uptr* next = ( uptr* ) curr;
*next = ( uptr ) curr + actual_block_size; *next = ( uptr ) curr + actual_block_size;
@ -514,7 +514,7 @@ void Pool::clear()
end = ( uptr* ) curr; end = ( uptr* ) curr;
*end = ( uptr ) NULL; *end = ( uptr ) NULL;
FreeList = PhysicalStart; pool.FreeList = pool.PhysicalStart;
} }
#pragma endregion Memory #pragma endregion Memory

View File

@ -358,30 +358,18 @@ using Arena_2MB = FixedArena< megabytes( 2 ) >;
using Arena_4MB = FixedArena< megabytes( 4 ) >; using Arena_4MB = FixedArena< megabytes( 4 ) >;
#pragma endregion FixedArena #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 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; AllocatorInfo Backing;
void* PhysicalStart; void* PhysicalStart;
void* FreeList; void* FreeList;
@ -390,12 +378,35 @@ struct Pool
ssize TotalSize; ssize TotalSize;
ssize NumBlocks; ssize NumBlocks;
operator AllocatorInfo() #pragma region Member Mapping
{ forceinline operator AllocatorInfo() { return GEN_NS allocator_info(* this); }
return { allocator_proc, 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 inline
b32 is_power_of_two( ssize x ) { b32 is_power_of_two( ssize x ) {
if ( x <= 0 ) if ( x <= 0 )