Did arena and fixedarena changes (for reducing usage of member procs)

This commit is contained in:
Edward R. Gonzalez 2024-11-30 12:16:01 -05:00
parent 163ad0a511
commit 5958dd2055
5 changed files with 143 additions and 89 deletions

View File

@ -16,7 +16,7 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va )
local_persist local_persist
char tok_map_mem[ TokenFmt_TokenMap_MemSize ]; char tok_map_mem[ TokenFmt_TokenMap_MemSize ];
init_from_memory( tok_map_arena, tok_map_mem, sizeof(tok_map_mem) ); tok_map_arena = init_from_memory( tok_map_mem, sizeof(tok_map_mem) );
tok_map = HashTable<StrC>::init( tok_map_arena ); tok_map = HashTable<StrC>::init( tok_map_arena );
s32 left = num_tokens - 1; s32 left = num_tokens - 1;

View File

@ -713,7 +713,7 @@ Code parse_class_struct( TokType which, bool inplace_def = false )
local_persist local_persist
char interface_arr_mem[ kilobytes(4) ] {0}; char interface_arr_mem[ kilobytes(4) ] {0};
Array<CodeType> interfaces; { Array<CodeType> interfaces; {
Arena arena; init_from_memory(arena, interface_arr_mem, kilobytes(4) ); Arena arena = init_from_memory( interface_arr_mem, kilobytes(4) );
Array<CodeType>::init_reserve( arena, 4 ); Array<CodeType>::init_reserve( arena, 4 );
} }

View File

@ -1,5 +1,6 @@
#ifdef GEN_INTELLISENSE_DIRECTIVES #ifdef GEN_INTELLISENSE_DIRECTIVES
# pragma once # pragma once
# include "platform.hpp"
# include "macros.hpp" # include "macros.hpp"
#endif #endif

View File

@ -334,7 +334,7 @@ ssize virtual_memory_page_size( ssize* alignment_out )
#pragma endregion VirtualMemory #pragma endregion VirtualMemory
void* Arena::allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ) void* arena_allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags )
{ {
Arena* arena = rcast(Arena*, allocator_data); Arena* arena = rcast(Arena*, allocator_data);
void* ptr = NULL; void* ptr = NULL;

View File

@ -170,127 +170,180 @@ b32 gen_vm_purge( VirtualMemory vm );
//! Retrieve VM's page size and alignment. //! Retrieve VM's page size and alignment.
ssize gen_virtual_memory_page_size( ssize* alignment_out ); ssize gen_virtual_memory_page_size( ssize* alignment_out );
#pragma region Arena
struct Arena; struct Arena;
void init_from_memory( Arena& arena, void* start, ssize size );
struct Arena AllocatorInfo allocator_info( Arena& arena );
{
static
void* allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags );
//forceinline static // Remove static keyword and rename allocator_proc
//Arena init_from_memory( void* start, ssize size ) { void* arena_allocator_proc(void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags);
// Arena result; GEN_NS init_from_memory( result, start, size );
// return result;
//}
static // Add these declarations after the Arena struct
Arena init_from_allocator( AllocatorInfo backing, ssize size ) Arena init_from_allocator(AllocatorInfo backing, ssize size);
{ Arena init_from_memory( void* start, ssize size );
Arena result = Arena init_sub(Arena& parent, ssize size);
{ ssize alignment_of(Arena& arena, ssize alignment);
backing,
alloc( backing, size),
size,
0,
0
};
return result;
}
static
Arena init_sub( Arena& parent, ssize size )
{
return init_from_allocator( parent.Backing, size );
}
ssize alignment_of( ssize alignment )
{
ssize alignment_offset, result_pointer, mask;
GEN_ASSERT( is_power_of_two( alignment ) );
alignment_offset = 0;
result_pointer = (ssize) PhysicalStart + TotalUsed;
mask = alignment - 1;
if ( result_pointer & mask )
alignment_offset = alignment - ( result_pointer & mask );
return alignment_offset;
}
// This id is defined by Unreal for asserts // This id is defined by Unreal for asserts
#pragma push_macro("check") #pragma push_macro("check")
#undef check #undef check
void check() void check(Arena& arena);
{
GEN_ASSERT( TempCount == 0 );
}
#pragma pop_macro("check") #pragma pop_macro("check")
void free() void free(Arena& arena);
{ ssize size_remaining(Arena& arena, ssize alignment);
if ( Backing.Proc )
{
gen::free( Backing, PhysicalStart );
PhysicalStart = nullptr;
}
}
ssize size_remaining( ssize alignment )
{
ssize result = TotalSize - ( TotalUsed + alignment_of( alignment ) );
return result;
}
struct Arena
{
AllocatorInfo Backing; AllocatorInfo Backing;
void* PhysicalStart; void* PhysicalStart;
ssize TotalSize; ssize TotalSize;
ssize TotalUsed; ssize TotalUsed;
ssize TempCount; ssize TempCount;
operator AllocatorInfo() { return { allocator_proc, this }; } #if 1
#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 arena_allocator_proc( allocator_data, type, size, alignment, old_memory, old_size, flags ); }
forceinline static Arena init_from_memory( void* start, ssize size ) { return GEN_NS init_from_memory( start, size ); }
forceinline static Arena init_from_allocator( AllocatorInfo backing, ssize size ) { return GEN_NS init_from_allocator( backing, size ); }
forceinline static Arena init_sub( Arena& parent, ssize size ) { return GEN_NS init_from_allocator( parent.Backing, size ); }
forceinline ssize alignment_of( ssize alignment ) { return GEN_NS alignment_of(* this, alignment); }
forceinline void free() { return GEN_NS free(* this); }
forceinline ssize size_remaining( ssize alignment ) { return GEN_NS size_remaining(* this, alignment); }
// This id is defined by Unreal for asserts
#pragma push_macro("check")
#undef check
forceinline void check() { GEN_NS check(* this); }
#pragma pop_macro("check")
#pragma endregion Member Mapping
#endif
}; };
void init_from_memory( Arena& arena, void* start, ssize size ) inline
AllocatorInfo allocator_info( Arena& arena ) {
return { arena_allocator_proc, &arena };
}
inline
Arena init_from_memory( void* start, ssize size )
{ {
arena = Arena arena = {
{
{ nullptr, nullptr }, { nullptr, nullptr },
start, start,
size, size,
0, 0,
0 0
}; };
return arena;
} }
inline
Arena init_from_allocator(AllocatorInfo backing, ssize size)
{
Arena result =
{
backing,
alloc(backing, size),
size,
0,
0
};
return result;
}
inline
Arena init_sub(Arena& parent, ssize size)
{
return init_from_allocator(parent.Backing, size);
}
inline
ssize alignment_of(Arena& arena, ssize alignment)
{
ssize alignment_offset, result_pointer, mask;
GEN_ASSERT(is_power_of_two(alignment));
alignment_offset = 0;
result_pointer = (ssize)arena.PhysicalStart + arena.TotalUsed;
mask = alignment - 1;
if (result_pointer & mask)
alignment_offset = alignment - (result_pointer & mask);
return alignment_offset;
}
#pragma push_macro("check")
#undef check
inline
void check(Arena& arena)
{
GEN_ASSERT(arena.TempCount == 0);
}
#pragma pop_macro("check")
inline
void free(Arena& arena)
{
if (arena.Backing.Proc)
{
gen::free(arena.Backing, arena.PhysicalStart);
arena.PhysicalStart = nullptr;
}
}
inline
ssize size_remaining(Arena& arena, ssize alignment)
{
ssize result = arena.TotalSize - (arena.TotalUsed + alignment_of(arena, alignment));
return result;
}
#pragma endregion Arena
#pragma region FixedArena
template<s32 Size>
struct FixedArena;
template<s32 Size> AllocatorInfo allocator_info( FixedArena<Size>& fixed_arena );
template<s32 Size> FixedArena<Size> fixed_arena_init();
template<s32 Size> ssize size_remaining(FixedArena<Size>& fixed_arena, ssize alignment);
// Just a wrapper around using an arena with memory associated with its scope instead of from an allocator. // Just a wrapper around using an arena with memory associated with its scope instead of from an allocator.
// Used for static segment or stack allocations. // Used for static segment or stack allocations.
template< s32 Size > template< s32 Size >
struct FixedArena struct FixedArena
{ {
static char memory[Size];
FixedArena init()
{
FixedArena result = { Arena::init_from_memory( result.memory, Size ), {0} };
return result;
}
ssize size_remaining( ssize alignment )
{
return arena.size_remaining( alignment );
}
operator AllocatorInfo()
{
return { Arena::allocator_proc, &arena };
}
Arena arena; Arena arena;
char memory[ Size ];
#if 1
#pragma region Member Mapping
forceinline operator AllocatorInfo() { return GEN_NS allocator_info(* this); }
forceinline static FixedArena init() { FixedArena result; GEN_NS fixed_arena_init<Size>(result); return result; }
forceinline ssize size_remaining(ssize alignment) { GEN_NS size_remaining(*this, alignment); }
#pragma endregion Member Mapping
#endif
}; };
template<s32 Size> inline
AllocatorInfo allocator_info( FixedArena<Size>& fixed_arena ) { return { arena_allocator_proc, & fixed_arena.arena }; }
template<s32 Size>
void fixed_arena_init(FixedArena<Size>& result) {
zero_size(& result.memory[0], Size);
result.arena = init_from_memory(& result.memory[0], Size);
}
template<s32 Size> inline
ssize size_remaining(FixedArena<Size>& fixed_arena, ssize alignment) {
return size_remaining(fixed_arena.arena, alignment);
}
using Arena_1KB = FixedArena< kilobytes( 1 ) >; using Arena_1KB = FixedArena< kilobytes( 1 ) >;
using Arena_4KB = FixedArena< kilobytes( 4 ) >; using Arena_4KB = FixedArena< kilobytes( 4 ) >;
using Arena_8KB = FixedArena< kilobytes( 8 ) >; using Arena_8KB = FixedArena< kilobytes( 8 ) >;
@ -303,6 +356,7 @@ using Arena_512KB = FixedArena< kilobytes( 512 ) >;
using Arena_1MB = FixedArena< megabytes( 1 ) >; using Arena_1MB = FixedArena< megabytes( 1 ) >;
using Arena_2MB = FixedArena< megabytes( 2 ) >; using Arena_2MB = FixedArena< megabytes( 2 ) >;
using Arena_4MB = FixedArena< megabytes( 4 ) >; using Arena_4MB = FixedArena< megabytes( 4 ) >;
#pragma endregion FixedArena
struct Pool struct Pool
{ {
@ -342,7 +396,6 @@ struct Pool
} }
}; };
inline inline
b32 is_power_of_two( ssize x ) { b32 is_power_of_two( ssize x ) {
if ( x <= 0 ) if ( x <= 0 )