comment
[vg.git] / vg_mem.h
index 5edbd9c68ebc60560860f98a53a62687bfb6dc4b..49ab3e3061a8af2eb1cb87aaae01f51cd6877728 100644 (file)
--- a/vg_mem.h
+++ b/vg_mem.h
@@ -8,7 +8,9 @@
 #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;
@@ -47,37 +49,12 @@ struct vg_allocation_meta
 struct vg_linear_allocator
 {
    u32 size;
-   /*     */
-   /*     */
-   /*     */
    u32 cur;
-   /*    */
-   /*    */
-   /*    */
    u16 allocation_count;
-   /*                 */
    u16 flags;
-   /*      */
    u32 last_alloc_size;
-   /*                */
-   /*                */
-   /*                */
    void *last_alloc;
-   /*             */
-   /*             */
-   /*             */
-   /*             */
-   /*             */
-   /*             */
-   /*             */
    vg_allocation_meta *alloc_table;
-   /*                            */
-   /*                            */
-   /*                            */
-   /*                            */
-   /*                            */
-   /*                            */
-   /*                            */
 
 #ifdef _WIN32
    /* 32 bit pointers! */
@@ -86,7 +63,7 @@ struct 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, ...);
 
@@ -96,6 +73,12 @@ VG_STATIC u32 vg_align8( u32 s )
    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 )
 {
@@ -109,76 +92,96 @@ 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 )
-   {
+   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;
    }
 }
@@ -188,14 +191,15 @@ VG_STATIC void vg_linear_del( void *buffer, void *data )
 {
    vg_linear_allocator *alloc = vg_linear_header( buffer );
 
-   if( alloc->last_alloc != data )
-      vg_fatal_exit_loop( "This block has been fixed!" );
+   if( alloc->last_alloc != data ){
+      vg_fatal_error( "This block has been fixed! Last alloc: %p, this: %p\n",
+                      alloc->last_alloc, data );
+   }
 
-   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 );
    }
@@ -210,10 +214,13 @@ VG_STATIC void vg_linear_del( void *buffer, void *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 );
@@ -246,18 +253,14 @@ 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++ )
-      {
+   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 );
 
@@ -283,22 +286,20 @@ VG_STATIC void *vg_create_linear_allocator( void *lin_alloc, u32 size,
    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 ];
          
@@ -310,19 +311,16 @@ VG_STATIC void *vg_create_linear_allocator( void *lin_alloc, u32 size,
          meta->data = header+1;
          meta->type = k_allocation_type_linear;
       }
-      else
-      {
+      else{
          header = lin_alloc + alloc->cur;
       }
       
       alloc->cur += block_size;
-      alloc->last_alloc = NULL; /* cant resize this block! */
+      alloc->last_alloc = header;
       alloc->last_alloc_size = block_size;
-
       alloc->allocation_count ++;
    }
-   else
-   {
+   else{
       if( vg_mem.use_libc_malloc && (flags & VG_MEMORY_SYSTEM) )
          header = malloc( sizeof(vg_linear_allocator) );
       else
@@ -336,8 +334,7 @@ VG_STATIC void *vg_create_linear_allocator( void *lin_alloc, u32 size,
    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 );
    }