* two functions to handle allocation of static data more simple
authorZdenek Kabelac <kabi@informatics.muni.cz>
Tue, 3 Dec 2002 19:40:35 +0000 (19:40 +0000)
committerZdenek Kabelac <kabi@informatics.muni.cz>
Tue, 3 Dec 2002 19:40:35 +0000 (19:40 +0000)
  av_mallocz_static - called for every static data table
  av_free_static - called when ffmpeg is no longer needed and should free
                   all static resources
* simple usage shown in mpegaudiodec.c

Originally committed as revision 1301 to svn://svn.ffmpeg.org/ffmpeg/trunk

ffmpeg.c
libavcodec/avcodec.h
libavcodec/mpegaudiodec.c
libavcodec/utils.c

index 9defc3cf3b7daa5ad1101a2d6054f83eb5e51634..2e76f384ea38882cc98e5fc5ec82cff8eb759421 100644 (file)
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -2667,5 +2667,6 @@ int main(int argc, char **argv)
     for(i=0;i<nb_input_files;i++)
         av_close_input_file(input_files[i]);
 
+    av_free_static();
     return 0;
 }
index 963e5f100a2d72db53f2e4dbc5d8c4ed60741776..7bfdc99a27e42dfb1b0eb3d21c90cc87a31eed8e 100644 (file)
@@ -1061,5 +1061,10 @@ void *av_mallocz(unsigned int size);
 void av_free(void *ptr);
 void __av_freep(void **ptr);
 #define av_freep(p) __av_freep((void **)(p))
+/* for static data only */
+/* call av_free_static to release all staticaly allocated tables */
+void av_free_static();
+void *__av_mallocz_static(void** location, unsigned int size);
+#define av_mallocz_static(p, s) __av_mallocz_static((void **)(p), s)
 
 #endif /* AVCODEC_H */
index a119b481d534b01289b1d9ae763da66df3511de3..b2c0966aa08bf558afc18d099d475dc1dfdedefa 100644 (file)
@@ -379,17 +379,13 @@ static int decode_init(AVCodecContext * avctx)
             band_index_long[i][22] = k;
         }
 
-        /* compute n ^ (4/3) and store it in mantissa/exp format */
-        table_4_3_exp = av_mallocz(TABLE_4_3_SIZE * 
-                                   sizeof(table_4_3_exp[0]));
-        if (!table_4_3_exp)
+       /* compute n ^ (4/3) and store it in mantissa/exp format */
+       if (!av_mallocz_static(&table_4_3_exp,
+                              TABLE_4_3_SIZE * sizeof(table_4_3_exp[0])))
+           return -1;
+       if (!av_mallocz_static(&table_4_3_value,
+                              TABLE_4_3_SIZE * sizeof(table_4_3_value[0])))
             return -1;
-        table_4_3_value = av_mallocz(TABLE_4_3_SIZE * 
-                                     sizeof(table_4_3_value[0]));
-        if (!table_4_3_value) {
-            av_free(table_4_3_exp);
-            return -1;
-        }
         
         int_pow_init();
         for(i=1;i<TABLE_4_3_SIZE;i++) {
index 6797508e1b78eba42c6aa549e921090fd9f119ad..969507e5eaa1c115f71ab3f43bf8ca45f6ae9822 100644 (file)
@@ -30,6 +30,43 @@ void *av_mallocz(unsigned int size)
     return ptr;
 }
 
+/* allocation of static arrays - do not use for normal allocation */
+static unsigned int last_static = 0;
+static char*** array_static = NULL;
+static const unsigned int grow_static = 64; // ^2
+void *__av_mallocz_static(void** location, unsigned int size)
+{
+    int l = (last_static + grow_static) & ~(grow_static - 1);
+    void *ptr = av_mallocz(size);
+    if (!ptr)
+       return NULL;
+
+    if (location)
+    {
+       if (l > last_static)
+           array_static = realloc(array_static, l);
+       array_static[last_static++] = (char**) location;
+       *location = ptr;
+    }
+    return ptr;
+}
+/* free all static arrays and reset pointers to 0 */
+void av_free_static()
+{
+    if (array_static)
+    {
+       unsigned i;
+       for (i = 0; i < last_static; i++)
+       {
+           free(*array_static[i]);
+            *array_static[i] = NULL;
+       }
+       free(array_static);
+       array_static = 0;
+    }
+    last_static = 0;
+}
+
 /* cannot call it directly because of 'void **' casting is not automatic */
 void __av_freep(void **ptr)
 {