[FFmpeg-devel] [PATCH 1/4] lavu: add simple array implementation
Nicolas George
george at nsup.org
Wed Mar 5 21:15:09 CET 2014
Le quintidi 15 ventôse, an CCXXII, Nicolas George a écrit :
> For that issue, I would conclude: let us not discuss without actually seeing
> the code.
And since it is always better to have something concrete to discuss about,
here is a first draft:
#define AV_DYNARRAY_ADD(av_size_max, av_array, av_size, av_success, av_failure) \
do { \
size_t av_size_new = av_size; \
if (!(av_size & (av_size - 1))) { \
av_size_new = av_size ? av_size << 1 : 1; \
if (av_size_new > av_size_max) { \
av_size_new = 0; \
} else { \
void *av_array_new = \
av_realloc(tab, av_size_new * sizeof(*av_array)); \
if (!av_array_new) \
av_size_new = 0; \
else \
av_array = av_array_new; \
} \
} \
if (av_size_new) { \
av_success; \
av_size++; \
} else { \
av_failure; \
} \
} while (0)
I realize that the types (of the size and of the elements) were actually not
needed, even with type-safe code.
This is not tested, I have no time to do it right now, but it can still
serve as a base for discussion.
Possible enhancements:
* Check for (size & (size + 1)) == 0 and resize to 2*size+1: that would give
successive sizes 1, 3, 5, 7, 15, ... 0x7FFFFFxx instead of 1, 2, 4, ...
0x40000000.
* Add a size_min argument, to avoid allocating tiny arrays.
Regards,
--
Nicolas George
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20140305/2abe0d59/attachment.asc>
More information about the ffmpeg-devel
mailing list