mirror of
https://github.com/Ed94/gencpp.git
synced 2025-07-02 11:51:02 -07:00
Alot (see description)
- Made a better global allocator for the process. - Some small fixes to gen.hpp, removed clear_code_memory as I'm designing this library to for now never free any memory. - Fixes to memory usage for cached strings - Added missing verification for attributes in some upfront constructors. Added attribute param for def_type procedure. - Started to use internal and global keywords in gen.cpp for associated definitions - Progress toward getting the parsing constructors to support operator definitions. - There was an *attempt* to get parse_type to support parsing function types. Its not tested yet.... - Its not an nice setup, there is no validation of parameters, problably will add that in the future.
This commit is contained in:
@ -6,33 +6,100 @@ namespace Memory
|
||||
{
|
||||
using namespace zpl;
|
||||
|
||||
Arena Global_Arena {};
|
||||
global AllocatorInfo GlobalAllocator;
|
||||
|
||||
global Array(Arena) Global_AllocatorBuckets;
|
||||
|
||||
void* Global_Allocator_Proc( void* allocator_data, AllocType type, sw size, sw alignment, void* old_memory, sw old_size, u64 flags )
|
||||
{
|
||||
Arena* last = & array_back( Global_AllocatorBuckets );
|
||||
|
||||
switch ( type )
|
||||
{
|
||||
case EAllocationALLOC:
|
||||
{
|
||||
if ( last->total_allocated + size > last->total_size )
|
||||
{
|
||||
Arena bucket;
|
||||
arena_init_from_allocator( & bucket, heap(), BucketSize );
|
||||
|
||||
if ( bucket.physical_start == nullptr )
|
||||
fatal( "Failed to create bucket for Global_AllocatorBuckets");
|
||||
|
||||
if ( ! array_append( Global_AllocatorBuckets, bucket ) )
|
||||
fatal( "Failed to append bucket to Global_AllocatorBuckets");
|
||||
|
||||
last = & array_back( Global_AllocatorBuckets );
|
||||
}
|
||||
|
||||
return alloc_align( arena_allocator( last), size, alignment );
|
||||
}
|
||||
case EAllocationFREE:
|
||||
{
|
||||
// Doesn't recycle.
|
||||
}
|
||||
case EAllocationFREE_ALL:
|
||||
{
|
||||
// Memory::cleanup instead.
|
||||
}
|
||||
case EAllocationRESIZE:
|
||||
{
|
||||
if ( last->total_allocated + size > last->total_size )
|
||||
{
|
||||
Arena bucket;
|
||||
arena_init_from_allocator( & bucket, heap(), BucketSize );
|
||||
|
||||
if ( bucket.physical_start == nullptr )
|
||||
fatal( "Failed to create bucket for Global_AllocatorBuckets");
|
||||
|
||||
if ( ! array_append( Global_AllocatorBuckets, bucket ) )
|
||||
fatal( "Failed to append bucket to Global_AllocatorBuckets");
|
||||
|
||||
last = & array_back( Global_AllocatorBuckets );
|
||||
}
|
||||
|
||||
void* result = alloc_align( arena_allocator( last), size, alignment );
|
||||
|
||||
if ( result != nullptr && old_memory != nullptr )
|
||||
{
|
||||
mem_copy( result, old_memory, size );
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
arena_init_from_allocator( & Global_Arena, heap(), Initial_Reserve );
|
||||
GlobalAllocator = AllocatorInfo { & Global_Allocator_Proc, nullptr };
|
||||
|
||||
if ( Global_Arena.total_size == 0 )
|
||||
{
|
||||
assert_crash( "Failed to reserve memory for Tests:: Global_Arena" );
|
||||
}
|
||||
}
|
||||
if ( ! array_init_reserve( Global_AllocatorBuckets, heap(), 128 ) )
|
||||
fatal( "Failed to reserve memory for Global_AllocatorBuckets");
|
||||
|
||||
void resize( uw new_size )
|
||||
{
|
||||
void* new_memory = resize( heap(), Global_Arena.physical_start, Global_Arena.total_size, new_size );
|
||||
Arena bucket;
|
||||
arena_init_from_allocator( & bucket, heap(), BucketSize );
|
||||
|
||||
if ( new_memory == nullptr )
|
||||
{
|
||||
fatal("Failed to resize global arena!");
|
||||
}
|
||||
if ( bucket.physical_start == nullptr )
|
||||
fatal( "Failed to create first bucket for Global_AllocatorBuckets");
|
||||
|
||||
Global_Arena.physical_start = new_memory;
|
||||
Global_Arena.total_size = new_size;
|
||||
array_append( Global_AllocatorBuckets, bucket );
|
||||
}
|
||||
|
||||
void cleanup()
|
||||
{
|
||||
arena_free( & Global_Arena);
|
||||
s32 index = 0;
|
||||
s32 left = array_count( Global_AllocatorBuckets );
|
||||
do
|
||||
{
|
||||
Arena* bucket = & Global_AllocatorBuckets[ index ];
|
||||
arena_free( bucket );
|
||||
index++;
|
||||
}
|
||||
while ( left--, left );
|
||||
|
||||
array_free( Global_AllocatorBuckets );
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user