[FFmpeg-devel] [PATCH] avutil/mem: add av_memdup()

Michael Niedermayer michaelni at gmx.at
Tue Apr 30 11:17:25 CEST 2013


On Sun, Apr 28, 2013 at 02:52:30PM +0200, Nicolas George wrote:
> Le nonidi 9 floréal, an CCXXI, Michael Niedermayer a écrit :
> >  /**
> > + * Duplicate the buffer p.
> > + * @param p buffer to be duplicated
> > + * @return Pointer to a newly allocated buffer containing a
> > + * copy of p or NULL if the buffer cannot be allocated.
> > + */
> > +void *av_memdup(const void *p, size_t size);
> 
> I like the idea. Possibly slightly more practical API:
> 
> int av_memdup(void **dst, const void *src, size_t size);
> 
> Returns 0 or AVERROR(ENOMEM) -> avoids "ret = AVERROR(ENOMEM);" before "goto
> fail;".
> 
> dst is unchanged if allocation failed -> avoids using a temp variable.
> 
> Optionally: free *dst before overwriting it.

the cases where i want to use this copy a structure and then
duplicate fields that where allocated so the whole struct becomes a
clone.
code that leaves dst in case of failure as well as code to free
it are quite inconvenient for that (both would need to be worked
around)


> 
> Second option: "if (size == (size_t)-1) size = strlen(src) + 1;".
> 
> Regards,
> 
> -- 
>   Nicolas George



> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel


-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

The real ebay dictionary, page 2
"100% positive feedback" - "All either got their money back or didnt complain"
"Best seller ever, very honest" - "Seller refunded buyer after failed scam"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20130430/85cda373/attachment.asc>


More information about the ffmpeg-devel mailing list