From a661bd8f0da6b0d37afeae9dce4a5988c5f13b76 Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 6 Oct 2011 20:46:01 +0200 Subject: video: use talloc for mp_image, abort if out of memory Make new_mp_image() allocate the struct with talloc and abort() if the av_malloc for image plane data fails. --- libmpcodecs/mp_image.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) (limited to 'libmpcodecs') diff --git a/libmpcodecs/mp_image.c b/libmpcodecs/mp_image.c index 88702dde60..4683f41174 100644 --- a/libmpcodecs/mp_image.c +++ b/libmpcodecs/mp_image.c @@ -22,9 +22,7 @@ #include #include -#if HAVE_MALLOC_H -#include -#endif +#include "talloc.h" #include "libmpcodecs/img_format.h" #include "libmpcodecs/mp_image.h" @@ -39,6 +37,8 @@ void mp_image_alloc_planes(mp_image_t *mpi) { mpi->chroma_width*mpi->chroma_height); } else mpi->planes[0]=av_malloc(mpi->bpp*mpi->width*(mpi->height+2)/8); + if (!mpi->planes[0]) + abort(); //out of memory if (mpi->flags&MP_IMGFLAG_PLANAR) { int bpp = IMGFMT_IS_YUVP16(mpi->imgfmt)? 2 : 1; // YV12/I420/YVU9/IF09. feel free to add other planar formats here... @@ -188,23 +188,29 @@ void mp_image_setfmt(mp_image_t* mpi,unsigned int out_fmt){ mpi->bpp=0; } +static int mp_image_destructor(void *ptr) +{ + mp_image_t *mpi = ptr; + + if(mpi->flags&MP_IMGFLAG_ALLOCATED){ + /* because we allocate the whole image at once */ + av_free(mpi->planes[0]); + if (mpi->flags & MP_IMGFLAG_RGB_PALETTE) + av_free(mpi->planes[1]); + } + + return 0; +} + mp_image_t* new_mp_image(int w,int h){ - mp_image_t* mpi = malloc(sizeof(mp_image_t)); - if(!mpi) return NULL; // error! - memset(mpi,0,sizeof(mp_image_t)); + mp_image_t* mpi = talloc_zero(NULL, mp_image_t); + talloc_set_destructor(mpi, mp_image_destructor); mpi->width=mpi->w=w; mpi->height=mpi->h=h; return mpi; } void free_mp_image(mp_image_t* mpi){ - if(!mpi) return; - if(mpi->flags&MP_IMGFLAG_ALLOCATED){ - /* becouse we allocate the whole image in once */ - av_free(mpi->planes[0]); - if (mpi->flags & MP_IMGFLAG_RGB_PALETTE) - av_free(mpi->planes[1]); - } - free(mpi); + talloc_free(mpi); } -- cgit v1.2.3