]> git.sesse.net Git - vlc/commitdiff
aout_BufferAlloc() : remove stack allocation
authorRafaël Carré <rafael.carre@gmail.com>
Thu, 3 Sep 2009 13:03:07 +0000 (15:03 +0200)
committerRafaël Carré <rafael.carre@gmail.com>
Thu, 3 Sep 2009 13:03:07 +0000 (15:03 +0200)
alloca() was not used anyway on OSX and BSD, due to smaller stack sizes,
and we can't assume a default stack size anyway

I expect the performance loss to be minimal, but worth the code
simplification anyway (i didn't benchmark)

aout_BufferAlloc() is moved into its own function in a .c file instead
of being a macro

Since there is now 2 types of allocations (HEAP and NONE), make
i_alloc_type a boolean (true = HEAP alloc, false = NO alloc)

make aout_BufferFree() a static inline function in the same process.
Prototype doesn't change since the provided buffer doesn't need to be
set to NULL (I checked all the callers)

include/vlc_aout.h
modules/audio_mixer/spdif.c
src/audio_output/aout_internal.h
src/audio_output/common.c
src/audio_output/dec.c
src/audio_output/filters.c
src/audio_output/input.c
src/audio_output/mixer.c
src/audio_output/output.c

index f3eec569a4a46cd652cce1088c7fcb8da0389848..cdae6d484148e0e10d2495bb115939c08bd18de4 100644 (file)
@@ -129,7 +129,7 @@ typedef int32_t vlc_fixed_t;
 struct aout_buffer_t
 {
     uint8_t *               p_buffer;
-    int                     i_alloc_type;
+    bool                    b_alloc;
     /* i_size is the real size of the buffer (used for debug ONLY), i_nb_bytes
      * is the number of significative bytes in it. */
     size_t                  i_size, i_nb_bytes;
@@ -147,12 +147,11 @@ struct aout_buffer_t
     void (*pf_release)( aout_buffer_t * );
 };
 
-#define aout_BufferFree( p_buffer ) do {                                    \
-    if( p_buffer != NULL && (p_buffer)->i_alloc_type == AOUT_ALLOC_HEAP )   \
-    {                                                                       \
-        free( p_buffer );                                                   \
-    }                                                                       \
-    p_buffer = NULL; } while(0)
+static inline void aout_BufferFree( aout_buffer_t *buffer )
+{
+    if( buffer && buffer->b_alloc )
+        free( buffer );
+}
 
 /* Size of a frame for S/PDIF output. */
 #define AOUT_SPDIF_SIZE 6144
@@ -166,14 +165,10 @@ struct aout_buffer_t
 /** allocation of memory in the audio output */
 typedef struct aout_alloc_t
 {
-    int                     i_alloc_type;
+    bool                    b_alloc;
     int                     i_bytes_per_sec;
 } aout_alloc_t;
 
-#define AOUT_ALLOC_NONE     0
-#define AOUT_ALLOC_STACK    1
-#define AOUT_ALLOC_HEAP     2
-
 /** audio output buffer FIFO */
 struct aout_fifo_t
 {
index 4005f831b5bd8f3109cb263a4b3a6851054e1ff6..0af800f33fc328cdbfbad2dd220512300abedc18 100644 (file)
@@ -66,7 +66,7 @@ static int Create( vlc_object_t *p_this )
     p_mixer->mix = DoWork;
     /* This is a bit kludgy - do not ask for a new buffer, since the one
      * provided by the first input will be good enough. */
-    p_mixer->allocation.i_alloc_type = AOUT_ALLOC_NONE;
+    p_mixer->allocation.b_alloc = false;
 
     return 0;
 }
index d18929a234ba5362d7a0a80dcefda5be46d01851..103f730e2216ef58953d2b63f368c39f12e4a819 100644 (file)
 #ifndef __LIBVLC_AOUT_INTERNAL_H
 # define __LIBVLC_AOUT_INTERNAL_H 1
 
-#include <assert.h>
-
-#if defined( __APPLE__ ) || defined( SYS_BSD )
-#undef HAVE_ALLOCA
-#endif
-
-#ifdef HAVE_ALLOCA
-#   define ALLOCA_TEST( p_alloc, p_new_buffer )                             \
-        if ( (p_alloc)->i_alloc_type == AOUT_ALLOC_STACK )                  \
-        {                                                                   \
-            (p_new_buffer) = alloca( i_alloc_size + sizeof(aout_buffer_t) );\
-            i_alloc_type = AOUT_ALLOC_STACK;                                \
-        }                                                                   \
-        else
-#else
-#   define ALLOCA_TEST( p_alloc, p_new_buffer )
-#endif
-
-#define aout_BufferAlloc( p_alloc, i_nb_usec, p_previous_buffer,            \
-                          p_new_buffer )                                    \
-    if ( (p_alloc)->i_alloc_type == AOUT_ALLOC_NONE )                       \
-    {                                                                       \
-        (p_new_buffer) = p_previous_buffer;                                 \
-    }                                                                       \
-    else                                                                    \
-    {                                                                       \
-        int i_alloc_size, i_alloc_type;                                     \
-        i_alloc_size = (int)( (uint64_t)(p_alloc)->i_bytes_per_sec          \
-                                            * (i_nb_usec) / 1000000 + 1 );  \
-        ALLOCA_TEST( p_alloc, p_new_buffer )                                \
-        {                                                                   \
-            (p_new_buffer) = malloc( i_alloc_size + sizeof(aout_buffer_t) );\
-            i_alloc_type = AOUT_ALLOC_HEAP;                                 \
-        }                                                                   \
-        if ( p_new_buffer != NULL )                                         \
-        {                                                                   \
-            (p_new_buffer)->i_alloc_type = i_alloc_type;                    \
-            (p_new_buffer)->i_size = i_alloc_size;                          \
-            (p_new_buffer)->p_buffer = (uint8_t *)(p_new_buffer)            \
-                                         + sizeof(aout_buffer_t);           \
-            (p_new_buffer)->b_discontinuity = false;                        \
-            if ( (p_previous_buffer) != NULL )                              \
-            {                                                               \
-                (p_new_buffer)->start_date =                                \
-                           ((aout_buffer_t *)p_previous_buffer)->start_date;\
-                (p_new_buffer)->end_date =                                  \
-                           ((aout_buffer_t *)p_previous_buffer)->end_date;  \
-            }                                                               \
-        }                                                                   \
-        /* we'll keep that for a while --Meuuh */                           \
-        /* else printf("%s:%d\n", __FILE__, __LINE__); */                   \
-    }
+void aout_BufferAlloc(aout_alloc_t *allocation, mtime_t microseconds,
+        aout_buffer_t *old_buffer, aout_buffer_t **new_buffer);
 
 struct aout_filter_owner_sys_t
 {
index b153f0c4de4ecfdb31bcced56ae354b8285d0f30..dbdead4d07c5ecc5fc2439f8075f8b37017c394d 100644 (file)
@@ -696,3 +696,43 @@ bool aout_CheckChannelExtraction( int *pi_selection,
     }
     return i_out == i_channels;
 }
+
+/*****************************************************************************
+ * aout_BufferAlloc:
+ *****************************************************************************/
+
+void aout_BufferAlloc(aout_alloc_t *allocation, mtime_t microseconds,
+        aout_buffer_t *old_buffer, aout_buffer_t **new_buffer)
+{
+    if ( !allocation->b_alloc )
+    {
+        *new_buffer = old_buffer;
+        return;
+    }
+
+    aout_buffer_t *buffer;
+    int i_alloc_size;
+
+    i_alloc_size = (int)( (uint64_t)allocation->i_bytes_per_sec
+                                        * (microseconds) / 1000000 + 1 );
+
+    buffer = malloc( i_alloc_size + sizeof(aout_buffer_t) );
+    if ( !buffer )
+    {
+        *new_buffer = NULL;
+        return;
+    }
+
+    buffer->b_alloc = true;
+    buffer->i_size = i_alloc_size;
+    buffer->p_buffer = (uint8_t *)buffer + sizeof(aout_buffer_t);
+    buffer->b_discontinuity = false;
+
+    if ( old_buffer )
+    {
+        buffer->start_date = old_buffer->start_date;
+        buffer->end_date = old_buffer->end_date;
+    }
+
+    *new_buffer = buffer;
+}
index f3bfe3e0caac7ae9ae193634cfee9d2ee9961e83..a804144d9c8e983e42c239a4e841f908439b7fed 100644 (file)
@@ -267,7 +267,7 @@ aout_buffer_t * aout_DecNewBuffer( aout_input_t * p_input,
     duration = (1000000 * (mtime_t)i_nb_samples) / p_input->input.i_rate;
 
     /* This necessarily allocates in the heap. */
-    aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_buffer );
+    aout_BufferAlloc( &p_input->input_alloc, duration, NULL, &p_buffer );
     if( p_buffer != NULL )
         p_buffer->i_nb_bytes = i_nb_samples * p_input->input.i_bytes_per_frame
                                   / p_input->input.i_frame_length;
@@ -326,7 +326,7 @@ int aout_DecPlay( aout_instance_t * p_aout, aout_input_t * p_input,
         mtime_t duration = (1000000 * (mtime_t)p_buffer->i_nb_samples)
                             / p_input->input.i_rate;
 
-        aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_new_buffer );
+        aout_BufferAlloc( &p_input->input_alloc, duration, NULL, &p_new_buffer );
         vlc_memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
                     p_buffer->i_nb_bytes );
         p_new_buffer->i_nb_samples = p_buffer->i_nb_samples;
index ce88e9df9371f73cc3de3fea0225d84fdc423a56..d6972fc3b74b864d56109d5d3241f922aa887fe0 100644 (file)
@@ -317,14 +317,14 @@ void aout_FiltersHintBuffers( aout_instance_t * p_aout,
             p_first_alloc->i_bytes_per_sec = __MAX(
                                          p_first_alloc->i_bytes_per_sec,
                                          i_input_size );
-            p_filter->output_alloc.i_alloc_type = AOUT_ALLOC_NONE;
+            p_filter->output_alloc.b_alloc = false;
         }
         else
         {
             /* We're gonna need a buffer allocation. */
             memcpy( &p_filter->output_alloc, p_first_alloc,
                     sizeof(aout_alloc_t) );
-            p_first_alloc->i_alloc_type = AOUT_ALLOC_STACK;
+            p_first_alloc->b_alloc = true;
             p_first_alloc->i_bytes_per_sec = i_input_size;
         }
     }
@@ -350,7 +350,7 @@ void aout_FiltersPlay( aout_instance_t * p_aout,
         aout_BufferAlloc( &p_filter->output_alloc,
                           ((mtime_t)(*pp_input_buffer)->i_nb_samples + 2)
                           * 1000000 / p_filter->input.i_rate,
-                          *pp_input_buffer, p_output_buffer );
+                          *pp_input_buffer, &p_output_buffer );
         if( p_output_buffer == NULL )
             return;
 
@@ -373,8 +373,6 @@ void aout_FiltersPlay( aout_instance_t * p_aout,
             *pp_input_buffer = p_output_buffer;
         }
     }
-
-    assert( (*pp_input_buffer) == NULL || (*pp_input_buffer)->i_alloc_type != AOUT_ALLOC_STACK );
 }
 
 /*****************************************************************************
index 1f11ae0d888cad5a299871af5b28944edbc7646e..7ddbee884308245a6062326e87f430cabfd1d4ec 100644 (file)
@@ -406,7 +406,7 @@ int aout_InputNew( aout_instance_t * p_aout, aout_input_t * p_input, const aout_
     }
 
     /* Prepare hints for the buffer allocator. */
-    p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
+    p_input->input_alloc.b_alloc = true;
     p_input->input_alloc.i_bytes_per_sec = -1;
 
     /* Create resamplers. */
@@ -432,7 +432,7 @@ int aout_InputNew( aout_instance_t * p_aout, aout_input_t * p_input, const aout_
         aout_FiltersHintBuffers( p_aout, p_input->pp_resamplers,
                                  p_input->i_nb_resamplers,
                                  &p_input->input_alloc );
-        p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
+        p_input->input_alloc.b_alloc = true;
 
         /* Setup the initial rate of the resampler */
         p_input->pp_resamplers[0]->input.i_rate = p_input->input.i_rate;
@@ -447,7 +447,7 @@ int aout_InputNew( aout_instance_t * p_aout, aout_input_t * p_input, const aout_
     aout_FiltersHintBuffers( p_aout, p_input->pp_filters,
                              p_input->i_nb_filters,
                              &p_input->input_alloc );
-    p_input->input_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
+    p_input->input_alloc.b_alloc = true;
 
     /* i_bytes_per_sec is still == -1 if no filters */
     p_input->input_alloc.i_bytes_per_sec = __MAX(
index 0a74b0a4861a1af3346190400139a1140c54a8ea..7857dfa86ee6734a9a04ca487d3b01bfcc8861b4 100644 (file)
@@ -339,14 +339,14 @@ static int MixBuffer( aout_instance_t * p_aout )
                       /* This is a bit kludgy, but is actually only used
                        * for the S/PDIF dummy mixer : */
                       p_aout->pp_inputs[i_first_input]->mixer.fifo.p_first,
-                      p_output_buffer );
+                      &p_output_buffer );
     if ( p_output_buffer == NULL )
     {
         aout_unlock_input_fifos( p_aout );
         return -1;
     }
     /* This is again a bit kludgy - for the S/PDIF mixer. */
-    if ( p_aout->p_mixer->allocation.i_alloc_type != AOUT_ALLOC_NONE )
+    if ( p_aout->p_mixer->allocation.b_alloc )
     {
         p_output_buffer->i_nb_samples = p_aout->output.i_nb_samples;
         p_output_buffer->i_nb_bytes = p_aout->output.i_nb_samples
index c481778820a2c2ed72012acc3cdb7db95b323ba4..d15d9909af0d133acc04d7855c281394bd0fcc76 100644 (file)
@@ -199,7 +199,7 @@ int aout_OutputNew( aout_instance_t * p_aout,
     }
 
     /* Prepare hints for the buffer allocator. */
-    p_aout->mixer_allocation.i_alloc_type = AOUT_ALLOC_HEAP;
+    p_aout->mixer_allocation.b_alloc = true;
     p_aout->mixer_allocation.i_bytes_per_sec
                         = p_aout->mixer_format.i_bytes_per_frame
                            * p_aout->mixer_format.i_rate