From: hgn Date: Fri, 9 Jun 2023 17:01:32 +0000 (+0100) Subject: memlog X-Git-Url: https://harrygodden.com/git/?p=vg.git;a=commitdiff_plain;h=eaf607f6fce19fe5aaaddc91c6d3d26403456b3f memlog --- diff --git a/.gitignore b/.gitignore index 2019de1..a9d0441 100755 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ *.o *.so *.dll +*.swp bin/ diff --git a/vg_mem.h b/vg_mem.h index 49ab3e3..a69779e 100644 --- a/vg_mem.h +++ b/vg_mem.h @@ -16,8 +16,7 @@ VG_STATIC void vg_print_backtrace(void); typedef struct vg_linear_allocator vg_linear_allocator; typedef struct vg_allocation_meta vg_allocation_meta; -struct -{ +struct{ void *rtmemory, *scratch; @@ -28,10 +27,11 @@ static vg_mem; struct vg_allocation_meta { + const char *name; void *data; - enum allocation_type - { - k_allocation_type_block = 0, + u32 size; + enum allocation_type{ + k_allocation_type_block = 0, k_allocation_type_linear = 1 } type; @@ -90,7 +90,8 @@ static vg_linear_allocator *vg_linear_header( void *data ) /* allocate something from a linear allocator */ __attribute__((warn_unused_result)) -VG_STATIC void *vg_linear_alloc( void *buffer, u32 size ) +VG_STATIC void *_vg_linear_alloc( void *buffer, u32 size, + const char *constr_name ) { if( size % 8 ){ vg_error( "alloc(%u) is not 8 byte aligned\n", size ); @@ -124,6 +125,8 @@ VG_STATIC void *vg_linear_alloc( void *buffer, u32 size ) vg_allocation_meta *meta = &alloc->alloc_table[ alloc->allocation_count ]; meta->type = k_allocation_type_block; meta->data = data; + meta->size = size; + meta->name = constr_name; } else{ data = buffer + alloc->cur; @@ -212,10 +215,11 @@ VG_STATIC void vg_linear_del( void *buffer, void *data ) /* extend latest block of memory from linear */ __attribute__((warn_unused_result)) -VG_STATIC void *vg_linear_extend( void *buffer, void *data, u32 extra ) +VG_STATIC void *_vg_linear_extend( void *buffer, void *data, u32 extra, + const char *constr_name ) { if( !data ) - return vg_linear_alloc( buffer, extra ); + return _vg_linear_alloc( buffer, extra, constr_name ); vg_linear_allocator *alloc = vg_linear_header( buffer ); @@ -277,8 +281,8 @@ VG_STATIC void vg_linear_clear( void *buffer ) } /* allocate a FIXED SIZE linear allocator */ -VG_STATIC void *vg_create_linear_allocator( void *lin_alloc, u32 size, - u16 flags ) +VG_STATIC void *_vg_create_linear_allocator( void *lin_alloc, u32 size, + u16 flags, const char *constr_name) { assert( sizeof( vg_linear_allocator ) == 32 ); @@ -310,6 +314,8 @@ VG_STATIC void *vg_create_linear_allocator( void *lin_alloc, u32 size, meta->data = header+1; meta->type = k_allocation_type_linear; + meta->size = size; + meta->name = constr_name; } else{ header = lin_alloc + alloc->cur; @@ -355,10 +361,57 @@ VG_STATIC void vg_alloc_quota(void) u32 size_scratch = 10*1024*1024; 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, - size_scratch, - VG_MEMORY_SYSTEM ); + vg_mem.rtmemory = _vg_create_linear_allocator( NULL, size, VG_MEMORY_SYSTEM, + "VG Root" ); + vg_mem.scratch = _vg_create_linear_allocator( vg_mem.rtmemory, + size_scratch, + VG_MEMORY_SYSTEM, + "Scratch buffer" ); +} + +VG_STATIC void vg_mem_log( void *lin_alloc, int depth, const char *name ) +{ + if( vg_mem.use_libc_malloc ){ + vg_linear_allocator *alloc = vg_linear_header( lin_alloc ); + + u32 s = alloc->size; + f32 p = ((float)alloc->cur / (float)alloc->size) * 100.0f; + + for( int i=0; iflags & VG_MEMORY_SYSTEM ){ + for( u32 i=0; iallocation_count; i++ ){ + vg_allocation_meta *meta = &alloc->alloc_table[i]; + + if( meta->type == k_allocation_type_block ){ + for( int i=0; iname, meta->size ); + } + else{ + vg_mem_log( meta->data, depth +1, meta->name ); + } + } + } + else{ + for( int i=0; i (UNTRACKED)\n" ); + } + } + else{ + vg_error( "allocations are not tracked (turn on libc mode)\n" ); + } } +#define VG_MEM_MCSTR(S) VG_MEM_MCSTR2(S) +#define VG_MEM_MCSTR2(S) #S + +#define vg_linear_alloc(...) \ + _vg_linear_alloc( __VA_ARGS__, __FILE__":"VG_MEM_MCSTR(__LINE__) ) +#define vg_linear_extend(...) \ + _vg_linear_extend( __VA_ARGS__, __FILE__":"VG_MEM_MCSTR(__LINE__) ) +#define vg_create_linear_allocator(...) \ + _vg_create_linear_allocator( __VA_ARGS__, __FILE__":"VG_MEM_MCSTR(__LINE__) ) + + #endif /* VG_MEM_H */