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,22 +170,78 @@ 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 );
AllocatorInfo allocator_info( Arena& arena );
// Remove static keyword and rename allocator_proc
void* arena_allocator_proc(void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags);
// Add these declarations after the Arena struct
Arena init_from_allocator(AllocatorInfo backing, ssize size);
Arena init_from_memory( void* start, ssize size );
Arena init_sub(Arena& parent, ssize size);
ssize alignment_of(Arena& arena, ssize alignment);
// This id is defined by Unreal for asserts
#pragma push_macro("check")
#undef check
void check(Arena& arena);
#pragma pop_macro("check")
void free(Arena& arena);
ssize size_remaining(Arena& arena, ssize alignment);
struct Arena struct Arena
{ {
static AllocatorInfo Backing;
void* allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ); void* PhysicalStart;
ssize TotalSize;
ssize TotalUsed;
ssize TempCount;
//forceinline static #if 1
//Arena init_from_memory( void* start, ssize size ) { #pragma region Member Mapping
// Arena result; GEN_NS init_from_memory( result, start, size ); forceinline operator AllocatorInfo() { return GEN_NS allocator_info(* this); }
// return result;
//}
static 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
};
inline
AllocatorInfo allocator_info( Arena& arena ) {
return { arena_allocator_proc, &arena };
}
inline
Arena init_from_memory( void* start, ssize size )
{
Arena arena = {
{ nullptr, nullptr },
start,
size,
0,
0
};
return arena;
}
inline
Arena init_from_allocator(AllocatorInfo backing, ssize size) Arena init_from_allocator(AllocatorInfo backing, ssize size)
{ {
Arena result = Arena result =
@ -199,19 +255,20 @@ struct Arena
return result; return result;
} }
static inline
Arena init_sub(Arena& parent, ssize size) Arena init_sub(Arena& parent, ssize size)
{ {
return init_from_allocator(parent.Backing, size); return init_from_allocator(parent.Backing, size);
} }
ssize alignment_of( ssize alignment ) inline
ssize alignment_of(Arena& arena, ssize alignment)
{ {
ssize alignment_offset, result_pointer, mask; ssize alignment_offset, result_pointer, mask;
GEN_ASSERT(is_power_of_two(alignment)); GEN_ASSERT(is_power_of_two(alignment));
alignment_offset = 0; alignment_offset = 0;
result_pointer = (ssize) PhysicalStart + TotalUsed; result_pointer = (ssize)arena.PhysicalStart + arena.TotalUsed;
mask = alignment - 1; mask = alignment - 1;
if (result_pointer & mask) if (result_pointer & mask)
@ -220,77 +277,73 @@ struct Arena
return alignment_offset; return alignment_offset;
} }
// This id is defined by Unreal for asserts
#pragma push_macro("check") #pragma push_macro("check")
#undef check #undef check
void check() inline
void check(Arena& arena)
{ {
GEN_ASSERT( TempCount == 0 ); GEN_ASSERT(arena.TempCount == 0);
} }
#pragma pop_macro("check") #pragma pop_macro("check")
void free() inline
void free(Arena& arena)
{ {
if ( Backing.Proc ) if (arena.Backing.Proc)
{ {
gen::free( Backing, PhysicalStart ); gen::free(arena.Backing, arena.PhysicalStart);
PhysicalStart = nullptr; arena.PhysicalStart = nullptr;
} }
} }
ssize size_remaining( ssize alignment ) inline
ssize size_remaining(Arena& arena, ssize alignment)
{ {
ssize result = TotalSize - ( TotalUsed + alignment_of( alignment ) ); ssize result = arena.TotalSize - (arena.TotalUsed + alignment_of(arena, alignment));
return result; return result;
} }
#pragma endregion Arena
AllocatorInfo Backing; #pragma region FixedArena
void* PhysicalStart; template<s32 Size>
ssize TotalSize; struct FixedArena;
ssize TotalUsed;
ssize TempCount;
operator AllocatorInfo() { return { allocator_proc, this }; } 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);
void init_from_memory( Arena& arena, void* start, ssize size )
{
arena =
{
{ nullptr, nullptr },
start,
size,
0,
0
};
}
// 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
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;
char memory[Size]; char memory[Size];
Arena arena;
#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 )