-VG_STATIC void vg_linear_clear( void *buffer )
-{
- vg_linear_allocator *alloc = vg_linear_header( buffer );
-
- /* libc mode we recursively free any allocations made */
- if( vg_mem.use_libc_malloc && (alloc->flags & VG_MEMORY_SYSTEM) ){
- for( u32 i=0; i<alloc->allocation_count; i++ ){
- vg_allocation_meta *meta = &alloc->alloc_table[i];
-
- if( meta->type == k_allocation_type_block ){
- free( meta->data );
- }
- else{
- vg_linear_clear( meta->data );
- vg_linear_allocator *sub = vg_linear_header( meta->data );
-
- free( sub->alloc_table );
- free( sub );
- }
- }
- }
-
- alloc->last_alloc = NULL;
- alloc->last_alloc_size = 0;
- alloc->allocation_count = 0;
- alloc->cur = 0;
-}
-
-/* allocate a FIXED SIZE linear allocator */
-VG_STATIC void *vg_create_linear_allocator( void *lin_alloc, u32 size,
- u16 flags )
-{
- assert( sizeof( vg_linear_allocator ) == 32 );
-
- vg_linear_allocator *header;
- u32 block_size = size + sizeof(vg_linear_allocator);
-
- /* Creating it inside an existing one */
- if( lin_alloc ){
- vg_linear_allocator *alloc = vg_linear_header( lin_alloc );
-
- if( alloc->cur + block_size > alloc->size )
- vg_fatal_exit_loop( "Out of memory" );
-
- if( alloc->allocation_count + 1 > VG_MAX_ALLOCATIONS )
- vg_fatal_exit_loop( "Max allocations in linear allocator" );
-
- if( (flags && VG_MEMORY_SYSTEM) && (alloc->flags & VG_MEMORY_REALTIME) )
- vg_fatal_exit_loop( "Cannot declare realtime allocator inside systems"
- " allocator" );
-
- if( vg_mem.use_libc_malloc ){
- vg_allocation_meta *meta =
- &alloc->alloc_table[ alloc->allocation_count ];
-
- if( flags & VG_MEMORY_REALTIME )
- header = malloc( block_size );
- else
- header = malloc( sizeof(vg_linear_allocator) );
-
- meta->data = header+1;
- meta->type = k_allocation_type_linear;
- }
- else{
- header = lin_alloc + alloc->cur;
- }
-
- alloc->cur += block_size;
- alloc->last_alloc = NULL; /* cant resize this block! */
- alloc->last_alloc_size = block_size;
-
- alloc->allocation_count ++;
- }
- else{
- if( vg_mem.use_libc_malloc && (flags & VG_MEMORY_SYSTEM) )
- header = malloc( sizeof(vg_linear_allocator) );
- else
- header = malloc( block_size );
- }
-
- header->allocation_count = 0;
- header->cur = 0;
- header->last_alloc = NULL;
- header->last_alloc_size = 0;
- header->size = size;
- header->flags = flags;
-
- if( vg_mem.use_libc_malloc && (flags & VG_MEMORY_SYSTEM) ){
- u32 table_size = sizeof(vg_allocation_meta)*VG_MAX_ALLOCATIONS;
- header->alloc_table = malloc( table_size );
- }
- else
- header->alloc_table = NULL;
-
- return header+1;
-}