comment
[vg.git] / vg_mem.h
index 820fa6d19aa0d701349de4a4bd7df5c02f9e4352..49ab3e3061a8af2eb1cb87aaae01f51cd6877728 100644 (file)
--- a/vg_mem.h
+++ b/vg_mem.h
@@ -49,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! */
@@ -88,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, ...);
 
@@ -122,21 +97,24 @@ VG_STATIC void *vg_linear_alloc( void *buffer, u32 size )
       vg_print_backtrace();
       size = vg_align8( size );
    }
+#ifdef _WIN32
+   if( ((u32)buffer) % 8 ){
+#else
    if( ((u64)buffer) % 8 ){
-      vg_error( "buffer: %p\n", buffer );
-      vg_fatal_exit_loop( "unaligned buffer" );
+#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" );
+      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;
 
@@ -153,7 +131,7 @@ VG_STATIC void *vg_linear_alloc( void *buffer, u32 size )
 
    u8 *bytes = data;
    for( u32 i=0; i<size; i++ ){
-      bytes[i] = 0xae;
+      bytes[i] = 0xfe;
    }
 
    alloc->allocation_count ++;
@@ -161,8 +139,12 @@ VG_STATIC void *vg_linear_alloc( void *buffer, u32 size )
    alloc->last_alloc_size = size;
    alloc->cur += size;
 
+#ifdef _WIN32
+   if( ((u32)data) % 8 ){
+#else
    if( ((u64)data) % 8 ){
-      vg_fatal_exit_loop( "unaligned" );
+#endif
+      vg_fatal_error( "unaligned" );
    }
 
    return data;
@@ -181,10 +163,10 @@ VG_STATIC void *vg_linear_resize( void *buffer, void *data, u32 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;
@@ -193,7 +175,7 @@ VG_STATIC void *vg_linear_resize( void *buffer, void *data, u32 newsize )
    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;
@@ -209,13 +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) ){
       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 );
    }
@@ -236,7 +220,7 @@ VG_STATIC void *vg_linear_extend( void *buffer, void *data, u32 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 );
@@ -306,13 +290,13 @@ VG_STATIC void *vg_create_linear_allocator( void *lin_alloc, u32 size,
       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 ){
@@ -332,9 +316,8 @@ VG_STATIC void *vg_create_linear_allocator( void *lin_alloc, u32 size,
       }
       
       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{