[SCM] libav/experimental: use av_malloc() functions - added av_strdup and av_realloc()

siretart at users.alioth.debian.org siretart at users.alioth.debian.org
Sun Jun 30 15:35:05 UTC 2013


The following commit has been merged in the experimental branch:
commit 8e1e6f31c142aeb27d3dd7df539c47b35d7eb903
Author: Fabrice Bellard <fabrice at bellard.org>
Date:   Thu Jan 23 23:03:09 2003 +0000

    use av_malloc() functions - added av_strdup and av_realloc()
    
    Originally committed as revision 1505 to svn://svn.ffmpeg.org/ffmpeg/trunk

diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 61ce24e..e2252f4 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -1233,7 +1233,9 @@ int avcodec(void* handle, avc_cmd_t cmd, void* pin, void* pout);
 /* memory */
 void *av_malloc(unsigned int size);
 void *av_mallocz(unsigned int size);
+void *av_realloc(void *ptr, unsigned int size);
 void av_free(void *ptr);
+char *av_strdup(const char *s);
 void __av_freep(void **ptr);
 #define av_freep(p) __av_freep((void **)(p))
 void *av_fast_realloc(void *ptr, int *size, int min_size);
diff --git a/libavcodec/common.c b/libavcodec/common.c
index a2580ba..4868bc0 100644
--- a/libavcodec/common.c
+++ b/libavcodec/common.c
@@ -171,8 +171,8 @@ static int alloc_table(VLC *vlc, int size)
     vlc->table_size += size;
     if (vlc->table_size > vlc->table_allocated) {
         vlc->table_allocated += (1 << vlc->bits);
-        vlc->table = realloc(vlc->table,
-                             sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
+        vlc->table = av_realloc(vlc->table,
+                                sizeof(VLC_TYPE) * 2 * vlc->table_allocated);
         if (!vlc->table)
             return -1;
     }
diff --git a/libavcodec/common.h b/libavcodec/common.h
index 31bfbb1..bdfa398 100644
--- a/libavcodec/common.h
+++ b/libavcodec/common.h
@@ -931,6 +931,11 @@ if((y)<(x)){\
 
 #define CLAMP_TO_8BIT(d) ((d > 0xff) ? 0xff : (d < 0) ? 0 : d)
 
+/* avoid usage of various functions */
+#define malloc please_use_av_malloc
+#define free please_use_av_free
+#define realloc please_use_av_realloc
+
 #endif /* HAVE_AV_CONFIG_H */
 
 #endif /* COMMON_H */
diff --git a/libavcodec/imgconvert.c b/libavcodec/imgconvert.c
index 218c844..40538c6 100644
--- a/libavcodec/imgconvert.c
+++ b/libavcodec/imgconvert.c
@@ -1071,7 +1071,7 @@ static int avpicture_alloc(AVPicture *picture,
 
 static void avpicture_free(AVPicture *picture)
 {
-    free(picture->data[0]);
+    av_free(picture->data[0]);
 }
 
 /* XXX: always use linesize. Return -1 if not supported */
diff --git a/libavcodec/mem.c b/libavcodec/mem.c
index e59b25a..a36952f 100644
--- a/libavcodec/mem.c
+++ b/libavcodec/mem.c
@@ -17,6 +17,12 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 #include "avcodec.h"
+
+/* here we can use OS dependant allocation functions */
+#undef malloc
+#undef free
+#undef realloc
+
 #ifdef HAVE_MALLOC_H
 #include <malloc.h>
 #endif
@@ -25,13 +31,15 @@
    memory allocator. You do not need to suppress this file because the
    linker will do it automatically */
 
-/* memory alloc */
+/** 
+ * Memory allocation of size byte with alignment suitable for all
+ * memory accesses (including vectors if available on the
+ * CPU). av_malloc(0) must return a non NULL pointer.
+ */
 void *av_malloc(unsigned int size)
 {
     void *ptr;
     
-//    if(size==0) return NULL;
-    
 #if defined (HAVE_MEMALIGN)
     ptr = memalign(16,size);
     /* Why 64? 
@@ -63,23 +71,17 @@ void *av_malloc(unsigned int size)
 #else
     ptr = malloc(size);
 #endif
-    if (!ptr)
-        return NULL;
-//fprintf(stderr, "%X %d\n", (int)ptr, size);
-    /* NOTE: this memset should not be present */
-    memset(ptr, 0, size);
     return ptr;
 }
 
 /**
- * realloc which does nothing if the block is large enogh
+ * av_realloc semantics (same as glibc): if ptr is NULL and size > 0,
+ * identical to malloc(size). If size is zero, it is identical to
+ * free(ptr) and NULL is returned.  
  */
-void *av_fast_realloc(void *ptr, int *size, int min_size){
-    if(min_size < *size) return ptr;
-    
-    *size= min_size + 10*1024;
-
-    return realloc(ptr, *size);
+void *av_realloc(void *ptr, unsigned int size)
+{
+    return realloc(ptr, size);
 }
 
 /* NOTE: ptr = NULL is explicetly allowed */
diff --git a/libavcodec/mpegaudio.c b/libavcodec/mpegaudio.c
index 7d2dd8d..28329de 100644
--- a/libavcodec/mpegaudio.c
+++ b/libavcodec/mpegaudio.c
@@ -770,6 +770,7 @@ int MPA_encode_frame(AVCodecContext *avctx,
 static int MPA_encode_close(AVCodecContext *avctx)
 {
     av_freep(&avctx->coded_frame);
+    return 0;
 }
 
 AVCodec mp2_encoder = {
diff --git a/libavcodec/ratecontrol.c b/libavcodec/ratecontrol.c
index bda408d..6bcbe1c 100644
--- a/libavcodec/ratecontrol.c
+++ b/libavcodec/ratecontrol.c
@@ -751,8 +751,8 @@ static int init_pass2(MpegEncContext *s)
     }
 //printf("%lld %lld %lld %lld\n", available_bits[I_TYPE], available_bits[P_TYPE], available_bits[B_TYPE], all_available_bits);
         
-    qscale= malloc(sizeof(double)*rcc->num_entries);
-    blured_qscale= malloc(sizeof(double)*rcc->num_entries);
+    qscale= av_malloc(sizeof(double)*rcc->num_entries);
+    blured_qscale= av_malloc(sizeof(double)*rcc->num_entries);
 
     for(step=256*256; step>0.0000001; step*=0.5){
         expected_bits=0;
@@ -809,8 +809,8 @@ static int init_pass2(MpegEncContext *s)
 //        printf("%f %d %f\n", expected_bits, (int)all_available_bits, rate_factor);
         if(expected_bits > all_available_bits) rate_factor-= step;
     }
-    free(qscale);
-    free(blured_qscale);
+    av_free(qscale);
+    av_free(blured_qscale);
 
     if(abs(expected_bits/all_available_bits - 1.0) > 0.01 ){
         fprintf(stderr, "Error: 2pass curve failed to converge\n");
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index ba47785..e9e6725 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -33,6 +33,32 @@ void *av_mallocz(unsigned int size)
     return ptr;
 }
 
+char *av_strdup(const char *s)
+{
+    char *ptr;
+    int len;
+    len = strlen(s) + 1;
+    ptr = av_malloc(len);
+    if (!ptr)
+        return NULL;
+    memcpy(ptr, s, len);
+    return ptr;
+}
+
+/**
+ * realloc which does nothing if the block is large enough
+ */
+void *av_fast_realloc(void *ptr, int *size, int min_size)
+{
+    if(min_size < *size) 
+        return ptr;
+    
+    *size= min_size + 10*1024;
+
+    return av_realloc(ptr, *size);
+}
+
+
 /* allocation of static arrays - do not use for normal allocation */
 static unsigned int last_static = 0;
 static char*** array_static = NULL;
@@ -47,7 +73,7 @@ void *__av_mallocz_static(void** location, unsigned int size)
     if (location)
     {
 	if (l > last_static)
-	    array_static = realloc(array_static, l);
+	    array_static = av_realloc(array_static, l);
 	array_static[last_static++] = (char**) location;
 	*location = ptr;
     }
@@ -61,10 +87,10 @@ void av_free_static()
 	unsigned i;
 	for (i = 0; i < last_static; i++)
 	{
-	    free(*array_static[i]);
+	    av_free(*array_static[i]);
             *array_static[i] = NULL;
 	}
-	free(array_static);
+	av_free(array_static);
 	array_static = 0;
     }
     last_static = 0;
diff --git a/libavcodec/wmadec.c b/libavcodec/wmadec.c
index a80e030..5305e1c 100644
--- a/libavcodec/wmadec.c
+++ b/libavcodec/wmadec.c
@@ -92,7 +92,7 @@ typedef struct WMADecodeContext {
     int16_t coefs1[MAX_CHANNELS][BLOCK_MAX_SIZE];
     float coefs[MAX_CHANNELS][BLOCK_MAX_SIZE] __attribute__((aligned(16)));
     MDCTContext mdct_ctx[BLOCK_NB_SIZES];
-    float *windows[BLOCK_NB_SIZES] __attribute__((aligned(16)));
+    float *windows[BLOCK_NB_SIZES];
     FFTSample mdct_tmp[BLOCK_MAX_SIZE] __attribute__((aligned(16))); /* temporary storage for imdct */
     /* output buffer for one frame and the last for IMDCT windowing */
     float frame_out[MAX_CHANNELS][BLOCK_MAX_SIZE * 2] __attribute__((aligned(16)));
@@ -212,8 +212,8 @@ static void init_coef_vlc(VLC *vlc,
 
     init_vlc(vlc, 9, n, table_bits, 1, 1, table_codes, 4, 4);
 
-    run_table = malloc(n * sizeof(uint16_t));
-    level_table = malloc(n * sizeof(uint16_t));
+    run_table = av_malloc(n * sizeof(uint16_t));
+    level_table = av_malloc(n * sizeof(uint16_t));
     p = levels_table;
     i = 2;
     level = 1;

-- 
Libav/FFmpeg packaging



More information about the pkg-multimedia-commits mailing list