[FFmpeg-devel] [PATCH] Avoid pointless check before calling free

Clément Bœsch ubitux
Thu Feb 3 02:23:02 CET 2011


Hi,

We recently tracked down and annihilated most of (all?) the if (p) free(p) in
MPlayer, and I thought it could be a good idea to do the same with FFmpeg. So
here is two patches:

- the first one in order to delete a pointless free made on each av_free call,

  I checked source of the older ?libc, dietlibc and gblic version I was able to
  find, and the free pointer check was present. And btw:

    The free() function frees the memory space pointed to by ptr, which must have
    been returned by a previous call to malloc(), calloc()  or  realloc().
    Otherwise, or if free(ptr) has already been called before, undefined behavior
    occurs.  If ptr is NULL, no operation is performed.

    [...]

    CONFORMING TO
           C89, C99.

- the second to remove the few remaining if (p) av_free(p) I was able to find.

Note that I didn't touch the av_freep ones since I'm not sure of the real
benefit.

Regards,

-- 
Cl?ment B.
-------------- next part --------------
>From 2cb516fc22780c8ac3032488549d2cd14c2a78ad Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= <ubitux at gmail.com>
Date: Thu, 3 Feb 2011 01:40:35 +0100
Subject: [PATCH 1/2] Avoid pointless check before calling free

---
 libavutil/mem.c |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/libavutil/mem.c b/libavutil/mem.c
index 18fe28b..7ffd6cb 100644
--- a/libavutil/mem.c
+++ b/libavutil/mem.c
@@ -138,12 +138,11 @@ void *av_realloc(void *ptr, FF_INTERNAL_MEM_TYPE size)
 
 void av_free(void *ptr)
 {
-    /* XXX: this test should not be needed on most libcs */
-    if (ptr)
 #if CONFIG_MEMALIGN_HACK
+    if (ptr)
         free((char*)ptr - ((char*)ptr)[-1]);
 #else
-        free(ptr);
+    free(ptr);
 #endif
 }
 
-- 
1.7.4

-------------- next part --------------
>From 63e2aac6c0bcb4d5662e3827cbe746f1273f228a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Cl=C3=A9ment=20B=C5=93sch?= <ubitux at gmail.com>
Date: Thu, 3 Feb 2011 02:09:36 +0100
Subject: [PATCH 2/2] Remove a few if (p) av_free(p) forms

---
 libavcodec/dvbsubdec.c     |    7 ++-----
 libavcodec/flashsv.c       |    6 ++----
 libavcodec/libxvidff.c     |    9 +++------
 libavcodec/mpegaudiodec.c  |    3 +--
 libavcodec/smacker.c       |    9 +++------
 libavcodec/truemotion2.c   |   12 ++++--------
 libavcodec/tta.c           |    3 +--
 libavformat/rtpdec_mpeg4.c |    3 +--
 libavformat/smacker.c      |    9 +++------
 9 files changed, 20 insertions(+), 41 deletions(-)

diff --git a/libavcodec/dvbsubdec.c b/libavcodec/dvbsubdec.c
index 0f1e004..fe98798 100644
--- a/libavcodec/dvbsubdec.c
+++ b/libavcodec/dvbsubdec.c
@@ -332,9 +332,7 @@ static void delete_state(DVBSubContext *ctx)
         ctx->region_list = region->next;
 
         delete_region_display_list(ctx, region);
-        if (region->pbuf)
-            av_free(region->pbuf);
-
+        av_free(region->pbuf);
         av_free(region);
     }
 
@@ -1032,8 +1030,7 @@ static void dvbsub_parse_region_segment(AVCodecContext *avctx,
     buf += 2;
 
     if (region->width * region->height != region->buf_size) {
-        if (region->pbuf)
-            av_free(region->pbuf);
+        av_free(region->pbuf);
 
         region->buf_size = region->width * region->height;
 
diff --git a/libavcodec/flashsv.c b/libavcodec/flashsv.c
index 08748c6..f5ffca0 100644
--- a/libavcodec/flashsv.c
+++ b/libavcodec/flashsv.c
@@ -133,8 +133,7 @@ static int flashsv_decode_frame(AVCodecContext *avctx,
     /* the block size could change between frames, make sure the buffer
      * is large enough, if not, get a larger one */
     if(s->block_size < s->block_width*s->block_height) {
-        if (s->tmpblock != NULL)
-            av_free(s->tmpblock);
+        av_free(s->tmpblock);
         if ((s->tmpblock = av_malloc(3*s->block_width*s->block_height)) == NULL) {
             av_log(avctx, AV_LOG_ERROR, "Can't allocate decompression buffer.\n");
             return -1;
@@ -243,8 +242,7 @@ static av_cold int flashsv_decode_end(AVCodecContext *avctx)
         avctx->release_buffer(avctx, &s->frame);
 
     /* free the tmpblock */
-    if (s->tmpblock != NULL)
-        av_free(s->tmpblock);
+    av_free(s->tmpblock);
 
     return 0;
 }
diff --git a/libavcodec/libxvidff.c b/libavcodec/libxvidff.c
index d95ddee..771fc2b 100644
--- a/libavcodec/libxvidff.c
+++ b/libavcodec/libxvidff.c
@@ -533,12 +533,9 @@ static av_cold int xvid_encode_close(AVCodecContext *avctx) {
         av_free(x->twopassbuffer);
         av_free(x->old_twopassbuffer);
     }
-    if( x->twopassfile != NULL )
-        av_free(x->twopassfile);
-    if( x->intra_matrix != NULL )
-        av_free(x->intra_matrix);
-    if( x->inter_matrix != NULL )
-        av_free(x->inter_matrix);
+    av_free(x->twopassfile);
+    av_free(x->intra_matrix);
+    av_free(x->inter_matrix);
 
     return 0;
 }
diff --git a/libavcodec/mpegaudiodec.c b/libavcodec/mpegaudiodec.c
index 0cd0b68..7b296c4 100644
--- a/libavcodec/mpegaudiodec.c
+++ b/libavcodec/mpegaudiodec.c
@@ -2227,8 +2227,7 @@ static av_cold int decode_close_mp3on4(AVCodecContext * avctx)
     int i;
 
     for (i = 0; i < s->frames; i++)
-        if (s->mp3decctx[i])
-            av_free(s->mp3decctx[i]);
+        av_free(s->mp3decctx[i]);
 
     return 0;
 }
diff --git a/libavcodec/smacker.c b/libavcodec/smacker.c
index 2fcc335..fa8b4e3 100644
--- a/libavcodec/smacker.c
+++ b/libavcodec/smacker.c
@@ -677,12 +677,9 @@ static int smka_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
     for(i = 0; i < 4; i++) {
         if(vlc[i].table)
             free_vlc(&vlc[i]);
-        if(h[i].bits)
-            av_free(h[i].bits);
-        if(h[i].lengths)
-            av_free(h[i].lengths);
-        if(h[i].values)
-            av_free(h[i].values);
+        av_free(h[i].bits);
+        av_free(h[i].lengths);
+        av_free(h[i].values);
     }
 
     *data_size = unp_size;
diff --git a/libavcodec/truemotion2.c b/libavcodec/truemotion2.c
index d20ee94..86454ec 100644
--- a/libavcodec/truemotion2.c
+++ b/libavcodec/truemotion2.c
@@ -185,8 +185,7 @@ static int tm2_build_huff_table(TM2Context *ctx, TM2Codes *code)
 
 static void tm2_free_codes(TM2Codes *code)
 {
-    if(code->recode)
-        av_free(code->recode);
+    av_free(code->recode);
     if(code->vlc.table)
         free_vlc(&code->vlc);
 }
@@ -859,13 +858,10 @@ static av_cold int decode_end(AVCodecContext *avctx){
     AVFrame *pic = &l->pic;
     int i;
 
-    if(l->last)
-        av_free(l->last);
-    if(l->clast)
-        av_free(l->clast);
+    av_free(l->last);
+    av_free(l->clast);
     for(i = 0; i < TM2_NUM_STREAMS; i++)
-        if(l->tokens[i])
-            av_free(l->tokens[i]);
+        av_free(l->tokens[i]);
     if(l->Y1){
         av_free(l->Y1);
         av_free(l->U1);
diff --git a/libavcodec/tta.c b/libavcodec/tta.c
index 936dff2..eb4d71f 100644
--- a/libavcodec/tta.c
+++ b/libavcodec/tta.c
@@ -449,8 +449,7 @@ static int tta_decode_frame(AVCodecContext *avctx,
 static av_cold int tta_decode_close(AVCodecContext *avctx) {
     TTAContext *s = avctx->priv_data;
 
-    if (s->decode_buffer)
-        av_free(s->decode_buffer);
+    av_free(s->decode_buffer);
     av_freep(&s->ch_ctx);
 
     return 0;
diff --git a/libavformat/rtpdec_mpeg4.c b/libavformat/rtpdec_mpeg4.c
index 137dbd2..5498d1c 100644
--- a/libavformat/rtpdec_mpeg4.c
+++ b/libavformat/rtpdec_mpeg4.c
@@ -111,8 +111,7 @@ static int parse_fmtp_config(AVCodecContext * codec, char *value)
 {
     /* decode the hexa encoded parameter */
     int len = ff_hex_to_data(NULL, value);
-    if (codec->extradata)
-        av_free(codec->extradata);
+    av_free(codec->extradata);
     codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
     if (!codec->extradata)
         return AVERROR(ENOMEM);
diff --git a/libavformat/smacker.c b/libavformat/smacker.c
index 71a968e..92da91e 100644
--- a/libavformat/smacker.c
+++ b/libavformat/smacker.c
@@ -334,12 +334,9 @@ static int smacker_read_close(AVFormatContext *s)
     int i;
 
     for(i = 0; i < 7; i++)
-        if(smk->bufs[i])
-            av_free(smk->bufs[i]);
-    if(smk->frm_size)
-        av_free(smk->frm_size);
-    if(smk->frm_flags)
-        av_free(smk->frm_flags);
+        av_free(smk->bufs[i]);
+    av_free(smk->frm_size);
+    av_free(smk->frm_flags);
 
     return 0;
 }
-- 
1.7.4

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20110203/abcef19e/attachment.pgp>



More information about the ffmpeg-devel mailing list