[FFmpeg-devel] [PATCH] avutil/mem: add av_memdup()
Paul B Mahol
onemda at gmail.com
Wed May 1 02:04:48 CEST 2013
On 4/30/13, Michael Niedermayer <michaelni at gmx.at> wrote:
> On Sun, Apr 28, 2013 at 02:52:30PM +0200, Nicolas George wrote:
>> Le nonidi 9 floreal, an CCXXI, Michael Niedermayer a ecrit :
>> > /**
>> > + * 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
>> 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
> 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
I like original simple code which is "clone" of av_strdup.
>> Second option: "if (size == (size_t)-1) size = strlen(src) + 1;".
>> Nicolas George
More information about the ffmpeg-devel