[x264-devel] Use calloc instead of malloc + memset

Henrik Gramner git at videolan.org
Wed Oct 30 21:18:32 CET 2013


x264 | branch: master | Henrik Gramner <henrik at gramner.com> | Fri Oct 18 22:21:38 2013 +0200| [1bb5232ac99bb87049ca9c6f6c357f5f5943fcf2] | committer: Jason Garrett-Glaser

Use calloc instead of malloc + memset

> http://git.videolan.org/gitweb.cgi/x264.git/?a=commit;h=1bb5232ac99bb87049ca9c6f6c357f5f5943fcf2
---

 output/flv.c            |    3 +--
 output/flv_bytestream.c |    4 +---
 output/matroska.c       |    7 +------
 output/matroska_ebml.c  |    7 ++-----
 output/mp4.c            |    7 ++-----
 5 files changed, 7 insertions(+), 21 deletions(-)

diff --git a/output/flv.c b/output/flv.c
index 6f972f7..46e46b3 100644
--- a/output/flv.c
+++ b/output/flv.c
@@ -75,11 +75,10 @@ static int write_header( flv_buffer *c )
 
 static int open_file( char *psz_filename, hnd_t *p_handle, cli_output_opt_t *opt )
 {
-    flv_hnd_t *p_flv = malloc( sizeof(*p_flv) );
     *p_handle = NULL;
+    flv_hnd_t *p_flv = calloc( 1, sizeof(flv_hnd_t) );
     if( !p_flv )
         return -1;
-    memset( p_flv, 0, sizeof(*p_flv) );
 
     p_flv->b_dts_compress = opt->use_dts_compress;
 
diff --git a/output/flv_bytestream.c b/output/flv_bytestream.c
index 38a4ca9..b6c29a2 100644
--- a/output/flv_bytestream.c
+++ b/output/flv_bytestream.c
@@ -87,11 +87,9 @@ void flv_put_amf_double( flv_buffer *c, double d )
 
 flv_buffer *flv_create_writer( const char *filename )
 {
-    flv_buffer *c = malloc( sizeof(*c) );
-
+    flv_buffer *c = calloc( 1, sizeof(flv_buffer) );
     if( !c )
         return NULL;
-    memset( c, 0, sizeof(*c) );
 
     if( !strcmp( filename, "-" ) )
         c->fp = stdout;
diff --git a/output/matroska.c b/output/matroska.c
index 9b83284..00a13f5 100644
--- a/output/matroska.c
+++ b/output/matroska.c
@@ -44,16 +44,11 @@ typedef struct
 
 static int open_file( char *psz_filename, hnd_t *p_handle, cli_output_opt_t *opt )
 {
-    mkv_hnd_t *p_mkv;
-
     *p_handle = NULL;
-
-    p_mkv  = malloc( sizeof(*p_mkv) );
+    mkv_hnd_t *p_mkv = calloc( 1, sizeof(mkv_hnd_t) );
     if( !p_mkv )
         return -1;
 
-    memset( p_mkv, 0, sizeof(*p_mkv) );
-
     p_mkv->w = mk_create_writer( psz_filename );
     if( !p_mkv->w )
     {
diff --git a/output/matroska_ebml.c b/output/matroska_ebml.c
index 12c43d5..943c65e 100644
--- a/output/matroska_ebml.c
+++ b/output/matroska_ebml.c
@@ -74,10 +74,9 @@ static mk_context *mk_create_context( mk_writer *w, mk_context *parent, unsigned
     }
     else
     {
-        c = malloc( sizeof(*c) );
+        c = calloc( 1, sizeof(mk_context) );
         if( !c )
             return NULL;
-        memset( c, 0, sizeof(*c) );
     }
 
     c->parent = parent;
@@ -291,12 +290,10 @@ static int mk_write_float( mk_context *c, unsigned id, float f )
 
 mk_writer *mk_create_writer( const char *filename )
 {
-    mk_writer *w = malloc( sizeof(*w) );
+    mk_writer *w = calloc( 1, sizeof(mk_writer) );
     if( !w )
         return NULL;
 
-    memset( w, 0, sizeof(*w) );
-
     w->root = mk_create_context( w, NULL, 0 );
     if( !w->root )
     {
diff --git a/output/mp4.c b/output/mp4.c
index f5471b9..8093b2b 100644
--- a/output/mp4.c
+++ b/output/mp4.c
@@ -165,8 +165,6 @@ static int close_file( hnd_t handle, int64_t largest_pts, int64_t second_largest
 
 static int open_file( char *psz_filename, hnd_t *p_handle, cli_output_opt_t *opt )
 {
-    mp4_hnd_t *p_mp4;
-
     *p_handle = NULL;
     FILE *fh = x264_fopen( psz_filename, "w" );
     if( !fh )
@@ -174,11 +172,10 @@ static int open_file( char *psz_filename, hnd_t *p_handle, cli_output_opt_t *opt
     FAIL_IF_ERR( !x264_is_regular_file( fh ), "mp4", "MP4 output is incompatible with non-regular file `%s'\n", psz_filename )
     fclose( fh );
 
-    if( !(p_mp4 = malloc( sizeof(mp4_hnd_t) )) )
+    mp4_hnd_t *p_mp4 = calloc( 1, sizeof(mp4_hnd_t) );
+    if( !p_mp4 )
         return -1;
 
-    memset( p_mp4, 0, sizeof(mp4_hnd_t) );
-
 #ifdef _WIN32
     /* GPAC doesn't support Unicode filenames. */
     char ansi_filename[MAX_PATH];



More information about the x264-devel mailing list