mirror of
https://github.com/Ed94/gencpp.git
synced 2024-12-22 07:44:45 -08:00
Did arena and fixedarena changes (for reducing usage of member procs)
This commit is contained in:
parent
163ad0a511
commit
5958dd2055
@ -16,7 +16,7 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va )
|
||||
local_persist
|
||||
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 );
|
||||
|
||||
s32 left = num_tokens - 1;
|
||||
|
@ -713,7 +713,7 @@ Code parse_class_struct( TokType which, bool inplace_def = false )
|
||||
local_persist
|
||||
char interface_arr_mem[ kilobytes(4) ] {0};
|
||||
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 );
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
#ifdef GEN_INTELLISENSE_DIRECTIVES
|
||||
# pragma once
|
||||
# include "platform.hpp"
|
||||
# include "macros.hpp"
|
||||
#endif
|
||||
|
||||
|
@ -334,7 +334,7 @@ ssize virtual_memory_page_size( ssize* alignment_out )
|
||||
|
||||
#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);
|
||||
void* ptr = NULL;
|
||||
|
@ -170,22 +170,78 @@ b32 gen_vm_purge( VirtualMemory vm );
|
||||
//! Retrieve VM's page size and alignment.
|
||||
ssize gen_virtual_memory_page_size( ssize* alignment_out );
|
||||
|
||||
|
||||
#pragma region 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
|
||||
{
|
||||
static
|
||||
void* allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags );
|
||||
AllocatorInfo Backing;
|
||||
void* PhysicalStart;
|
||||
ssize TotalSize;
|
||||
ssize TotalUsed;
|
||||
ssize TempCount;
|
||||
|
||||
//forceinline static
|
||||
//Arena init_from_memory( void* start, ssize size ) {
|
||||
// Arena result; GEN_NS init_from_memory( result, start, size );
|
||||
// return result;
|
||||
//}
|
||||
#if 1
|
||||
#pragma region Member Mapping
|
||||
forceinline operator AllocatorInfo() { return GEN_NS allocator_info(* this); }
|
||||
|
||||
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 result =
|
||||
@ -199,19 +255,20 @@ struct Arena
|
||||
return result;
|
||||
}
|
||||
|
||||
static
|
||||
inline
|
||||
Arena init_sub(Arena& parent, ssize 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;
|
||||
GEN_ASSERT(is_power_of_two(alignment));
|
||||
|
||||
alignment_offset = 0;
|
||||
result_pointer = (ssize) PhysicalStart + TotalUsed;
|
||||
result_pointer = (ssize)arena.PhysicalStart + arena.TotalUsed;
|
||||
mask = alignment - 1;
|
||||
|
||||
if (result_pointer & mask)
|
||||
@ -220,77 +277,73 @@ struct Arena
|
||||
return alignment_offset;
|
||||
}
|
||||
|
||||
// This id is defined by Unreal for asserts
|
||||
#pragma push_macro("check")
|
||||
#undef check
|
||||
void check()
|
||||
inline
|
||||
void check(Arena& arena)
|
||||
{
|
||||
GEN_ASSERT( TempCount == 0 );
|
||||
GEN_ASSERT(arena.TempCount == 0);
|
||||
}
|
||||
#pragma pop_macro("check")
|
||||
|
||||
void free()
|
||||
inline
|
||||
void free(Arena& arena)
|
||||
{
|
||||
if ( Backing.Proc )
|
||||
if (arena.Backing.Proc)
|
||||
{
|
||||
gen::free( Backing, PhysicalStart );
|
||||
PhysicalStart = nullptr;
|
||||
gen::free(arena.Backing, arena.PhysicalStart);
|
||||
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;
|
||||
}
|
||||
#pragma endregion Arena
|
||||
|
||||
AllocatorInfo Backing;
|
||||
void* PhysicalStart;
|
||||
ssize TotalSize;
|
||||
ssize TotalUsed;
|
||||
ssize TempCount;
|
||||
#pragma region FixedArena
|
||||
template<s32 Size>
|
||||
struct FixedArena;
|
||||
|
||||
operator AllocatorInfo() { return { allocator_proc, this }; }
|
||||
};
|
||||
|
||||
void init_from_memory( Arena& arena, void* start, ssize size )
|
||||
{
|
||||
arena =
|
||||
{
|
||||
{ nullptr, nullptr },
|
||||
start,
|
||||
size,
|
||||
0,
|
||||
0
|
||||
};
|
||||
}
|
||||
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.
|
||||
// Used for static segment or stack allocations.
|
||||
template< s32 Size >
|
||||
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];
|
||||
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_4KB = FixedArena< kilobytes( 4 ) >;
|
||||
using Arena_8KB = FixedArena< kilobytes( 8 ) >;
|
||||
@ -303,6 +356,7 @@ using Arena_512KB = FixedArena< kilobytes( 512 ) >;
|
||||
using Arena_1MB = FixedArena< megabytes( 1 ) >;
|
||||
using Arena_2MB = FixedArena< megabytes( 2 ) >;
|
||||
using Arena_4MB = FixedArena< megabytes( 4 ) >;
|
||||
#pragma endregion FixedArena
|
||||
|
||||
struct Pool
|
||||
{
|
||||
@ -342,7 +396,6 @@ struct Pool
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
inline
|
||||
b32 is_power_of_two( ssize x ) {
|
||||
if ( x <= 0 )
|
||||
|
Loading…
Reference in New Issue
Block a user