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 f3eec56..cdae6d4 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 4005f83..0af800f 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 d18929a..103f730 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 b153f0c..dbdead4 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 f3bfe3e..a804144 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 ce88e9d..d6972fc 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 1f11ae0..7ddbee8 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 0a74b0a..7857dfa 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 c481778..d15d990 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