#include <stdlib.h>
#include <malloc.h>
-#define VG_MAX_ALLOCATIONS 64
+VG_STATIC void vg_print_backtrace(void);
+
+#define VG_MAX_ALLOCATIONS 128
#define VG_FUZZ_ALLOCATIONS
typedef struct vg_linear_allocator vg_linear_allocator;
};
#pragma pack(pop)
-VG_STATIC void vg_fatal_exit_loop( const char *error );
+VG_STATIC void vg_fatal_error( const char *fmt, ... );
VG_STATIC void vg_error(const char *fmt, ...);
VG_STATIC void vg_info(const char *fmt, ...);
return m << 3;
}
+VG_STATIC u32 vg_align4( u32 s )
+{
+ u32 m = (s + 3) >> 2;
+ return m << 2;
+}
+
/* Returns allocator structure from data pointer */
static vg_linear_allocator *vg_linear_header( void *data )
{
__attribute__((warn_unused_result))
VG_STATIC void *vg_linear_alloc( void *buffer, u32 size )
{
- if( size % 8 )
- {
- vg_error( "Requested size: %u\n", size );
- vg_fatal_exit_loop( "8 byte alignment required" );
+ if( size % 8 ){
+ vg_error( "alloc(%u) is not 8 byte aligned\n", size );
+ vg_print_backtrace();
+ size = vg_align8( size );
+ }
+#ifdef _WIN32
+ if( ((u32)buffer) % 8 ){
+#else
+ if( ((u64)buffer) % 8 ){
+#endif
+ vg_fatal_error( "unaligned buffer (%p)", buffer );
}
vg_linear_allocator *alloc = vg_linear_header( buffer );
- if( (alloc->cur + size) > alloc->size )
- {
- vg_error( "%u + %u > %u\n", alloc->cur, size, alloc->size );
- vg_fatal_exit_loop( "linear allocator overflow" );
+ if( (alloc->cur + size) > alloc->size ){
+ vg_fatal_error( "linear allocator overflow (%u + %u > %u)\n",
+ alloc->cur, size, alloc->size );
}
if( alloc->flags & VG_MEMORY_SYSTEM )
if( (alloc->allocation_count + 1) > VG_MAX_ALLOCATIONS )
- vg_fatal_exit_loop( "Max linear allocations reached" );
+ vg_fatal_error( "Max linear allocations reached" );
void *data;
- if( vg_mem.use_libc_malloc && (alloc->flags & VG_MEMORY_SYSTEM) )
- {
+ if( vg_mem.use_libc_malloc && (alloc->flags & VG_MEMORY_SYSTEM) ){
data = malloc( size );
vg_allocation_meta *meta = &alloc->alloc_table[ alloc->allocation_count ];
meta->type = k_allocation_type_block;
meta->data = data;
}
- else
- {
+ else{
data = buffer + alloc->cur;
}
+ u8 *bytes = data;
+ for( u32 i=0; i<size; i++ ){
+ bytes[i] = 0xfe;
+ }
+
alloc->allocation_count ++;
alloc->last_alloc = data;
alloc->last_alloc_size = size;
alloc->cur += size;
+#ifdef _WIN32
+ if( ((u32)data) % 8 ){
+#else
+ if( ((u64)data) % 8 ){
+#endif
+ vg_fatal_error( "unaligned" );
+ }
+
return data;
}
-
/* resize latest block of memory from linear */
__attribute__((warn_unused_result))
VG_STATIC void *vg_linear_resize( void *buffer, void *data, u32 newsize )
{
vg_linear_allocator *alloc = vg_linear_header( buffer );
+ if( newsize % 8 ){
+ vg_error( "alloc(%u) is not 8 byte aligned\n", newsize );
+ vg_print_backtrace();
+ newsize = vg_align8( newsize );
+ }
+
if( alloc->last_alloc != data )
- vg_fatal_exit_loop( "This block has been fixed!" );
+ vg_fatal_error( "This block has been fixed!" );
if( (alloc->cur - alloc->last_alloc_size + newsize) > alloc->size )
- vg_fatal_exit_loop( "Cannot resize, overflow" );
+ vg_fatal_error( "Cannot resize, overflow" );
alloc->cur -= alloc->last_alloc_size;
alloc->cur += newsize;
alloc->last_alloc_size = newsize;
- if( vg_mem.use_libc_malloc && (alloc->flags & VG_MEMORY_SYSTEM) )
- {
+ if( vg_mem.use_libc_malloc && (alloc->flags & VG_MEMORY_SYSTEM) ){
data = realloc( data, newsize );
if( !data )
- vg_fatal_exit_loop( "realloc failed" );
+ vg_fatal_error( "realloc failed" );
alloc->alloc_table[ alloc->allocation_count-1 ].data = data;
alloc->last_alloc = data;
return data;
}
- else
- {
+ else{
return data;
}
}
vg_linear_allocator *alloc = vg_linear_header( buffer );
if( alloc->last_alloc != data )
- vg_fatal_exit_loop( "This block has been fixed!" );
+ vg_fatal_error( "This block has been fixed!" );
- if( vg_mem.use_libc_malloc && (alloc->flags & VG_MEMORY_SYSTEM) )
- {
+ if( vg_mem.use_libc_malloc && (alloc->flags & VG_MEMORY_SYSTEM) ){
vg_allocation_meta *meta = &alloc->alloc_table[alloc->allocation_count-1];
if( meta->type == k_allocation_type_linear )
- vg_fatal_exit_loop( "Cannot free a linear allocator in this conext" );
+ vg_fatal_error( "Cannot free a linear allocator in this conext" );
free( data );
}
__attribute__((warn_unused_result))
VG_STATIC void *vg_linear_extend( void *buffer, void *data, u32 extra )
{
+ if( !data )
+ return vg_linear_alloc( buffer, extra );
+
vg_linear_allocator *alloc = vg_linear_header( buffer );
if( alloc->last_alloc != data )
- vg_fatal_exit_loop( "This block has been fixed!" );
+ vg_fatal_error( "This block has been fixed!" );
u32 new_size = alloc->last_alloc_size + extra;
return vg_linear_resize( buffer, data, new_size );
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++ )
- {
+ 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 )
- {
+ if( meta->type == k_allocation_type_block ){
free( meta->data );
}
- else
- {
+ else{
vg_linear_clear( meta->data );
vg_linear_allocator *sub = vg_linear_header( meta->data );
u32 block_size = size + sizeof(vg_linear_allocator);
/* Creating it inside an existing one */
- if( lin_alloc )
- {
+ 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" );
+ vg_fatal_error( "Out of memory" );
if( alloc->allocation_count + 1 > VG_MAX_ALLOCATIONS )
- vg_fatal_exit_loop( "Max allocations in linear allocator" );
+ vg_fatal_error( "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"
+ vg_fatal_error( "Cannot declare realtime allocator inside systems"
" allocator" );
- if( vg_mem.use_libc_malloc )
- {
+ if( vg_mem.use_libc_malloc ){
vg_allocation_meta *meta =
&alloc->alloc_table[ alloc->allocation_count ];
meta->data = header+1;
meta->type = k_allocation_type_linear;
}
- else
- {
+ else{
header = lin_alloc + alloc->cur;
}
alloc->allocation_count ++;
}
- else
- {
+ else{
if( vg_mem.use_libc_malloc && (flags & VG_MEMORY_SYSTEM) )
header = malloc( sizeof(vg_linear_allocator) );
else
header->size = size;
header->flags = flags;
- if( vg_mem.use_libc_malloc && (flags & VG_MEMORY_SYSTEM) )
- {
+ 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 );
}