[vlc-devel] commit: embedded snapshot: avoid an unnecessary double malloc ( Olivier Aubert )

git version control git at videolan.org
Mon Sep 8 15:07:05 CEST 2008


vlc | branch: master | Olivier Aubert <olivier.aubert at liris.cnrs.fr> | Mon Sep  8 15:10:06 2008 +0200| [37485818653989b5f4b8fbabfdc75198dfc84a05] | committer: Olivier Aubert 

embedded snapshot: avoid an unnecessary double malloc

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=37485818653989b5f4b8fbabfdc75198dfc84a05
---

 src/control/mediacontrol_audio_video.c |   13 +++++++------
 src/control/mediacontrol_util.c        |   32 +++++++++++++++-----------------
 2 files changed, 22 insertions(+), 23 deletions(-)

diff --git a/src/control/mediacontrol_audio_video.c b/src/control/mediacontrol_audio_video.c
index 2cc9a09..e0a6f7c 100644
--- a/src/control/mediacontrol_audio_video.c
+++ b/src/control/mediacontrol_audio_video.c
@@ -100,15 +100,16 @@ mediacontrol_snapshot( mediacontrol_Instance *self,
 
     if( p_snapshot )
     {
+        /* Note: p_snapshot->p_data is directly used, not copied. Thus
+           do not free it here. */
         p_pic = private_mediacontrol_createRGBPicture( p_snapshot->i_width,
-                               p_snapshot->i_height,
-                               VLC_FOURCC( 'p','n','g',' ' ),
-                               p_snapshot->date,
-                               p_snapshot->p_data,
-                               p_snapshot->i_datasize );
+                                                       p_snapshot->i_height,
+                                                       VLC_FOURCC( 'p','n','g',' ' ),
+                                                       p_snapshot->date,
+                                                       p_snapshot->p_data,
+                                                       p_snapshot->i_datasize );
         if( !p_pic )
         {
-            free( p_snapshot->p_data );
             free( p_snapshot );
             RAISE_NULL( mediacontrol_InternalException, "Out of memory" );
         }
diff --git a/src/control/mediacontrol_util.c b/src/control/mediacontrol_util.c
index e86646f..09e5b74 100644
--- a/src/control/mediacontrol_util.c
+++ b/src/control/mediacontrol_util.c
@@ -176,20 +176,6 @@ private_mediacontrol_position2microsecond( libvlc_media_player_t * p_media_playe
     return 0;
 }
 
-mediacontrol_RGBPicture*
-private_mediacontrol_RGBPicture__alloc( int datasize )
-{
-    mediacontrol_RGBPicture* pic;
-
-    pic = ( mediacontrol_RGBPicture * )malloc( sizeof( mediacontrol_RGBPicture ) );
-    if( ! pic )
-        return NULL;
-
-    pic->size = datasize;
-    pic->data = ( char* )malloc( datasize * sizeof( char ) );
-    return pic;
-}
-
 void
 mediacontrol_RGBPicture__free( mediacontrol_RGBPicture* pic )
 {
@@ -245,13 +231,25 @@ mediacontrol_exception_free( mediacontrol_Exception *exception )
     free( exception );
 }
 
+/**
+ * Allocates and initializes a mediacontrol_RGBPicture object.
+ *
+ * @param i_width: picture width
+ * @param i_height: picture width
+ * @param i_chroma: picture chroma
+ * @param l_date: picture timestamp
+ * @param p_data: pointer to the data. The data will be directly used, not copied.
+ * @param i_datasize: data size in bytes
+ *
+ * @return the new object, or NULL on error.
+ */
 mediacontrol_RGBPicture*
 private_mediacontrol_createRGBPicture( int i_width, int i_height, long i_chroma, int64_t l_date,
-                                char* p_data, int i_datasize )
+				       char* p_data, int i_datasize )
 {
     mediacontrol_RGBPicture *retval;
 
-    retval = private_mediacontrol_RGBPicture__alloc( i_datasize );
+    retval = ( mediacontrol_RGBPicture * )malloc( sizeof( mediacontrol_RGBPicture ) );
     if( retval )
     {
         retval->width  = i_width;
@@ -259,7 +257,7 @@ private_mediacontrol_createRGBPicture( int i_width, int i_height, long i_chroma,
         retval->type   = i_chroma;
         retval->date   = l_date;
         retval->size   = i_datasize;
-        memcpy( retval->data, p_data, i_datasize );
+        retval->data   = p_data;
     }
     return retval;
 }




More information about the vlc-devel mailing list