#ifndef VG_MEM_H
#define VG_MEM_H
+#include "vg.h"
#include "vg_stdint.h"
#include "vg_platform.h"
#define VG_MAX_ALLOCATIONS 64
#define VG_FUZZ_ALLOCATIONS
-#define VG_DEBUG_ALLOCATIONS
typedef struct vg_linear_allocator vg_linear_allocator;
typedef struct vg_allocation_meta vg_allocation_meta;
*scratch;
int use_libc_malloc;
+ u32 quota;
}
static vg_mem;
/* */
/* */
/* */
+
+#ifdef _WIN32
+ /* 32 bit pointers! */
+ u8 padding[ 8 ];
+#endif
};
#pragma pack(pop)
+VG_STATIC void vg_fatal_exit_loop( const char *error );
VG_STATIC void vg_error(const char *fmt, ...);
VG_STATIC void vg_info(const char *fmt, ...);
{
data = malloc( size );
-#ifdef VG_DEBUG_ALLOCATIONS
- vg_info( "malloc: %p[%u]\n", data, size );
-#endif
-
vg_allocation_meta *meta = &alloc->alloc_table[ alloc->allocation_count ];
meta->type = k_allocation_type_block;
meta->data = data;
alloc->alloc_table[ alloc->allocation_count-1 ].data = data;
alloc->last_alloc = data;
-
-#ifdef VG_DEBUG_ALLOCATIONS
- vg_info( "realloc: %p (->%u)\n", data, newsize );
-#endif
return data;
}
else
return alloc->cur;
}
-/* get the capacity of allocator. TODO, should return free space? */
+/* get the capacity of allocator. */
VG_STATIC u32 vg_linear_get_capacity( void *buffer )
{
vg_linear_allocator *alloc = vg_linear_header( buffer );
return alloc->size;
}
+/* get the remaining size of the allocator */
+VG_STATIC u32 vg_linear_remaining( void *buffer )
+{
+ vg_linear_allocator *alloc = vg_linear_header( buffer );
+ return alloc->size - alloc->cur;
+}
+
/* yeet all memory from linear allocator */
VG_STATIC void vg_linear_clear( void *buffer )
{
vg_linear_allocator *alloc = vg_linear_header( buffer );
-#ifdef VG_DEBUG_ALLOCATIONS
- if( alloc->flags & VG_MEMORY_SYSTEM )
- vg_info( "linear_clear on %p\n", alloc );
-#endif
-
/* libc mode we recursively free any allocations made */
if( vg_mem.use_libc_malloc && (alloc->flags & VG_MEMORY_SYSTEM) )
{
if( meta->type == k_allocation_type_block )
{
free( meta->data );
-
-#ifdef VG_DEBUG_ALLOCATIONS
- vg_info( "free(%p) [%d]\n", meta->data, meta->type );
-#endif
}
else
{
vg_linear_clear( meta->data );
vg_linear_allocator *sub = vg_linear_header( meta->data );
-#ifdef VG_DEBUG_ALLOCATIONS
- vg_info( "free(%p) [alloc_table]\n", sub->alloc_table );
- vg_info( "free(%p) [%d]\n", sub, meta->type );
-#endif
-
free( sub->alloc_table );
free( sub );
}
else
header = malloc( sizeof(vg_linear_allocator) );
-#ifdef VG_DEBUG_ALLOCATIONS
- vg_info( "linear_create(sub) malloc(%p)\n", header );
-#endif
-
meta->data = header+1;
meta->type = k_allocation_type_linear;
}
header = malloc( sizeof(vg_linear_allocator) );
else
header = malloc( block_size );
-
-#ifdef VG_DEBUG_ALLOCATIONS
- vg_info( "linear_create malloc(%p)\n", header );
-#endif
}
header->allocation_count = 0;
{
u32 table_size = sizeof(vg_allocation_meta)*VG_MAX_ALLOCATIONS;
header->alloc_table = malloc( table_size );
-#ifdef VG_DEBUG_ALLOCATIONS
- vg_info( "linear_create(%p) [alloc_table]\n", header->alloc_table );
-#endif
}
else
header->alloc_table = NULL;
}
/* request all the memory we need in advance */
-VG_STATIC void vg_prealloc_quota( u32 size )
+VG_STATIC void vg_set_mem_quota( u32 size )
+{
+ vg_mem.quota = size;
+}
+
+VG_STATIC void vg_alloc_quota(void)
{
u32 size_scratch = 10*1024*1024;
- size = VG_MAX( size, size_scratch );
+ u32 size = VG_MAX( vg_mem.quota, size_scratch );
vg_mem.rtmemory = vg_create_linear_allocator( NULL, size, VG_MEMORY_SYSTEM );
vg_mem.scratch = vg_create_linear_allocator( vg_mem.rtmemory,
}
#endif /* VG_MEM_H */
-
-#if 0
-
-#ifndef VG_MEM_H
-#define VG_MEM_H
-
-#include "vg_stdint.h"
-#include "vg_platform.h"
-
-#include <stdlib.h>
-#include <malloc.h>
-
-typedef struct vg_linear_allocator vg_linear_allocator;
-
-struct
-{
- void *rtmemory,
- *scratch;
-}
-static vg_mem;
-
-struct vg_linear_allocator
-{
- u32 size, cur;
-
- /* allows temporarily extendable buffers */
- void *last_alloc;
- u32 last_alloc_size;
-};
-
-/*
- * TODO: Fallback on libc
- */
-
-//#define VG_USE_MALLOC
-
-VG_STATIC void vg_error(const char *fmt, ...);
-
-/* allocate something from a linear allocator */
-__attribute__((warn_unused_result))
-VG_STATIC void *vg_linear_alloc( void *allocator, u32 size )
-{
- size += 7;
- size >>= 3;
- size <<= 3;
-
- if( allocator == NULL )
- vg_fatal_exit_loop( "Null allocator" );
-
- vg_linear_allocator *allocptr = allocator;
- allocptr --;
-
- if( allocptr->cur + size > allocptr->size )
- {
- vg_error( "%u(current) + %u(alloc) > %u(max)\n",
- allocptr->cur, size, allocptr->size );
-
- vg_fatal_exit_loop( "Linear allocator out of memory" );
- }
-
-#ifdef VG_USE_MALLOC
- void *data = malloc(size);
-#else
- void *data = allocator + allocptr->cur;
-#endif
-
- allocptr->cur += size;
- allocptr->last_alloc_size = size;
- allocptr->last_alloc = data;
-
- return data;
-}
-
-/* resize latest block of memory from linear */
-__attribute__((warn_unused_result))
-VG_STATIC void *vg_linear_resize( void *allocator, void *data, u32 newsize )
-{
- vg_linear_allocator *allocptr = allocator;
- allocptr --;
-
- if( allocptr->last_alloc == data )
- {
- allocptr->cur -= allocptr->last_alloc_size;
-
-#ifdef VG_USE_MALLOC
- allocptr->cur += newsize;
- void *newdata = realloc( data, newsize );
- allocptr->last_alloc = newdata;
- allocptr->last_alloc_size = newsize;
-
- return newdata;
-#else
- return vg_linear_alloc( allocator, newsize );
-#endif
- }
- else
- {
- vg_error( "%p != %p\n", allocptr->last_alloc, data );
- vg_fatal_exit_loop( "Cannot resize this buffer anymore" );
- }
-
- return NULL;
-}
-
-VG_STATIC void vg_linear_del( void *allocator, void *data )
-{
-#ifdef VG_USE_MALLOC
- free(data);
-#else
- void *ignore = vg_linear_resize( allocator, data, 0 );
-#endif
-
- vg_linear_allocator *allocptr = allocator;
- allocptr --;
-
- allocptr->last_alloc = NULL;
- allocptr->last_alloc_size = 0;
-}
-
-/* extend latest block of memory from linear */
-__attribute__((warn_unused_result))
-VG_STATIC void *vg_linear_extend( void *allocator, void *data, u32 extra )
-{
- vg_linear_allocator *allocptr = allocator;
- allocptr --;
-
- return vg_linear_resize( allocator, data, allocptr->last_alloc_size+extra );
-}
-
-/* get the current usage of allocator */
-VG_STATIC u32 vg_linear_get_cur( void *allocator )
-{
- vg_linear_allocator *allocptr = allocator;
- allocptr --;
-
- return allocptr->cur;
-}
-
-/* get the capacity of allocator */
-VG_STATIC u32 vg_linear_get_capacity( void *allocator )
-{
- vg_linear_allocator *allocptr = allocator;
- allocptr --;
-
- return allocptr->size;
-}
-
-#if 0
-/* get the size of the last allocated thing */
-VG_STATIC u32 vg_linear_last_size( void *allocator )
-{
- vg_linear_allocator *allocptr = allocator;
- allocptr --;
-
- return allocptr->last_alloc_size;
-}
-#endif
-
-/* yeet all memory from linear allocator */
-VG_STATIC void vg_linear_clear( void *allocator )
-{
- vg_linear_allocator *allocptr = allocator;
- allocptr --;
-
- allocptr->last_alloc = NULL;
- allocptr->last_alloc_size = 0;
- allocptr->cur = 0;
-}
-
-/* allocate a FIXED SIZE linear allocator */
-VG_STATIC void *vg_create_linear_allocator( void *lin_alloc, u32 size )
-{
- u32 total = size + sizeof(vg_linear_allocator);
- vg_linear_allocator *allocptr;
-
- if( lin_alloc == NULL )
- {
- allocptr = malloc( total );
-
- if( allocptr == NULL )
- vg_fatal_exit_loop( "Create linear: Malloc failed" );
- }
- else
- {
- vg_linear_allocator *parent = lin_alloc;
- parent --;
-
- allocptr = vg_linear_alloc( lin_alloc, total );
-
-#if 0
- parent->last_alloc = allocptr+1;
- parent->last_alloc_size = total;
-#else
- parent->last_alloc = NULL;
- parent->last_alloc_size = total;
-#endif
- }
-
- allocptr->size = size;
- allocptr->cur = 0;
- allocptr->last_alloc = NULL;
- allocptr->last_alloc_size = 0;
-
- void *data = allocptr+1;
- return data;
-}
-
-/* request all the memory we need in advance */
-VG_STATIC void vg_prealloc_quota( u32 size )
-{
- size = VG_MAX( size, 20*1024*1024 );
- vg_mem.rtmemory = vg_create_linear_allocator( NULL, size );
- vg_mem.scratch = vg_create_linear_allocator( vg_mem.rtmemory, 10*1024*1024 );
-}
-
-#endif /* VG_MEM_H */
-
-#endif