the mega audio-memory-profiler-patch TM
authorhgn <hgodden00@gmail.com>
Fri, 28 Oct 2022 02:15:37 +0000 (03:15 +0100)
committerhgn <hgodden00@gmail.com>
Fri, 28 Oct 2022 02:15:37 +0000 (03:15 +0100)
src/vg/vg.h
src/vg/vg_audio.h
src/vg/vg_mem.h
src/vg/vg_profiler.h

index 08c18b34c7c59eb455fbbd88b747e41ee54c2279..f3456f3199887c8e76c4f0222cefe55732978292 100644 (file)
@@ -397,8 +397,14 @@ VG_STATIC void vg_enter( int argc, char *argv[], const char *window_name )
       {
          vg.samples = VG_MAX( 0, VG_MIN( 8, atoi( arg ) ) );
       }
-   }
 
+      if( (arg = vg_long_opt_arg( "use-libc-malloc" )) )
+      {
+         vg_mem.use_libc_malloc = atoi( arg );
+      }
+   }
+   
+   vg_alloc_quota();
    vg_log_init();
    vg_console_init();
 
index 0b4fc3ea87264e22b1a3980f0e714c7fadce92d0..18d560d6203d1638cca027696d26f002ac87c6b7 100644 (file)
 #include <time.h>
 
 #ifdef __GNUC__
-#ifndef __clang__
-  #pragma GCC push_options
-  #pragma GCC optimize ("O3")
-#endif
+  #ifndef __clang__
+    #pragma GCC push_options
+    #pragma GCC optimize ("O3")
+  #endif
 #endif
 
 #pragma GCC diagnostic push
@@ -43,9 +43,9 @@
 #pragma GCC diagnostic pop 
 
 #ifdef __GNUC__
-#ifndef __clang__
-  #pragma GCC pop_options
-#endif
+  #ifndef __clang__
+    #pragma GCC pop_options
+  #endif
 #endif
 
 #define SFX_MAX_SYSTEMS       32
@@ -765,8 +765,11 @@ VG_STATIC void audio_mixer_callback( ma_device *pDevice, void *pOutBuf,
    audio_unlock();
 }
 
-VG_STATIC void audio_clip_load( audio_clip *clip )
+VG_STATIC void audio_clip_load( audio_clip *clip, void *lin_alloc )
 {
+   if( lin_alloc == NULL )
+      lin_alloc = vg_audio.audio_pool;
+
    if( clip->source_mode == k_audio_source_mono )
    {
       vg_linear_clear( vg_mem.scratch );
@@ -795,7 +798,7 @@ VG_STATIC void audio_clip_load( audio_clip *clip )
           data_size      = length_samples * sizeof(i16);
 
       audio_lock();
-      clip->data = vg_linear_alloc( vg_audio.audio_pool, data_size );
+      clip->data = vg_linear_alloc( lin_alloc, data_size );
       clip->size = length_samples;
       audio_unlock();
 
@@ -814,7 +817,7 @@ VG_STATIC void audio_clip_load( audio_clip *clip )
    else if( clip->source_mode == k_audio_source_compressed )
    {
       audio_lock();
-      clip->data = vg_file_read( vg_audio.audio_pool, clip->path, &clip->size );
+      clip->data = vg_file_read( lin_alloc, clip->path, &clip->size );
       audio_unlock();
 
       if( !clip->data )
@@ -825,10 +828,10 @@ VG_STATIC void audio_clip_load( audio_clip *clip )
    }
 }
 
-VG_STATIC void audio_clip_loadn( audio_clip *arr, int count )
+VG_STATIC void audio_clip_loadn( audio_clip *arr, int count, void *lin_alloc )
 {
    for( int i=0; i<count; i++ )
-      audio_clip_load( &arr[i] );
+      audio_clip_load( &arr[i], lin_alloc );
 }
 
 /* Mark change to be uploaded through queue system */
index 47e9c93886ea67e68b668484700e1cb9eadd8b81..2309707437d97d02a649a102f0c13610f26994c7 100644 (file)
@@ -9,7 +9,6 @@
 
 #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;
@@ -20,6 +19,7 @@ struct
         *scratch;
 
    int   use_libc_malloc;
+   u32   quota;
 }
 static vg_mem;
 
@@ -114,10 +114,6 @@ VG_STATIC void *vg_linear_alloc( void *buffer, u32 size )
    {
       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;
@@ -160,10 +156,6 @@ VG_STATIC void *vg_linear_resize( void *buffer, void *data, u32 newsize )
 
       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
@@ -215,23 +207,25 @@ VG_STATIC u32 vg_linear_get_cur( void *buffer )
    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) )
    {
@@ -242,21 +236,12 @@ VG_STATIC void vg_linear_clear( void *buffer )
          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 );
          }
@@ -303,10 +288,6 @@ VG_STATIC void *vg_create_linear_allocator( void *lin_alloc, u32 size,
          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;
       }
@@ -327,10 +308,6 @@ VG_STATIC void *vg_create_linear_allocator( void *lin_alloc, u32 size,
          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;
@@ -344,9 +321,6 @@ VG_STATIC void *vg_create_linear_allocator( void *lin_alloc, u32 size,
    {
       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;
@@ -355,10 +329,15 @@ VG_STATIC void *vg_create_linear_allocator( void *lin_alloc, u32 size,
 }
 
 /* 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, 
index fded3302523be9442f8c161b62e4df5b42d7241c..87de34f53dae041f4a9be1dd465c656c9ef3f222 100644 (file)
@@ -121,7 +121,7 @@ VG_STATIC void vg_profile_drawn( struct vg_profile **profiles, u32 count,
 
    for( int i=0; i<count; i++ )
    {
-      snprintf( infbuf, 64, "%.1fms %s", 
+      snprintf( infbuf, 64, "%.4fms %s", 
                         avgs[i] * (1.0f/(VG_PROFILE_SAMPLE_COUNT-1)),
                         profiles[i]->name );