Based on a patch by Clément Bœsch. See thread: From: Clément Bœsch <ubitux@gmail.com> Subject: [FFmpeg-devel] [PATCH 1/5] lavu: add av_dynarray_alloc_elem(). Date: Sun, 14 Apr 2013 03:07:54 +0200
Unified · Split
| @@ -15,6 +15,9 @@ libavutil: 2012-10-22 · 14 unmodified lines | ||
| 15 | 15 | |
| 16 | 16 | API changes, most recent first: |
| 17 | 17 | |
| 18 | +2013-05-13 - xxxxxxx - lavu 52.31.100 - mem.h | |
| 19 | + Add av_dynarray2_add(). | |
| 20 | + | |
| 18 | 21 | 2013-05-12 - xxxxxxx - lavfi 3.65.100 |
| 19 | 22 | Add AVFILTER_FLAG_SUPPORT_TIMELINE* filter flags. |
| 20 | 23 | |
| @@ -270,6 +270,39 @@ fail: · 269 unmodified lines | ||
| 270 | 270 | *nb_ptr = 0; |
| 271 | 271 | } |
| 272 | 272 | |
| 273 | +void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size, | |
| 274 | + const uint8_t *elem_data) | |
| 275 | +{ | |
| 276 | + int nb = *nb_ptr, nb_alloc; | |
| 277 | + uint8_t *tab = *tab_ptr, *tab_elem_data; | |
| 278 | + | |
| 279 | + if ((nb & (nb - 1)) == 0) { | |
| 280 | + if (nb == 0) { | |
| 281 | + nb_alloc = 1; | |
| 282 | + } else { | |
| 283 | + if (nb > INT_MAX / (2 * elem_size)) | |
| 284 | + goto fail; | |
| 285 | + nb_alloc = nb * 2; | |
| 286 | + } | |
| 287 | + tab = av_realloc(tab, nb_alloc * elem_size); | |
| 288 | + if (!tab) | |
| 289 | + goto fail; | |
| 290 | + *tab_ptr = tab; | |
| 291 | + } | |
| 292 | + *nb_ptr = nb + 1; | |
| 293 | + tab_elem_data = tab + nb*elem_size; | |
| 294 | + if (elem_data) | |
| 295 | + memcpy(tab_elem_data, elem_data, elem_size); | |
| 296 | + else if (CONFIG_MEMORY_POISONING) | |
| 297 | + memset(tab_elem_data, FF_MEMORY_POISON, elem_size); | |
| 298 | + return tab_elem_data; | |
| 299 | + | |
| 300 | +fail: | |
| 301 | + av_freep(tab_ptr); | |
| 302 | + *nb_ptr = 0; | |
| 303 | + return NULL; | |
| 304 | +} | |
| 305 | + | |
| 273 | 306 | static void fill16(uint8_t *dst, int len) |
| 274 | 307 | { |
| 275 | 308 | uint32_t v = AV_RN16(dst - 2); |
| @@ -215,9 +215,34 @@ void av_freep(void *ptr); · 214 unmodified lines | ||
| 215 | 215 | * @param tab_ptr pointer to the array to grow |
| 216 | 216 | * @param nb_ptr pointer to the number of elements in the array |
| 217 | 217 | * @param elem element to add |
| 218 | + * @see av_dynarray2_add() | |
| 218 | 219 | */ |
| 219 | 220 | void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem); |
| 220 | 221 | |
| 222 | +/** | |
| 223 | + * Add an element of size elem_size to a dynamic array. | |
| 224 | + * | |
| 225 | + * The array is reallocated when its number of elements reaches powers of 2. | |
| 226 | + * Therefore, the amortized cost of adding an element is constant. | |
| 227 | + * | |
| 228 | + * In case of success, the pointer to the array is updated in order to | |
| 229 | + * point to the new grown array, and the number pointed to by nb_ptr | |
| 230 | + * is incremented. | |
| 231 | + * In case of failure, the array is freed, *tab_ptr is set to NULL and | |
| 232 | + * *nb_ptr is set to 0. | |
| 233 | + * | |
| 234 | + * @param tab_ptr pointer to the array to grow | |
| 235 | + * @param nb_ptr pointer to the number of elements in the array | |
| 236 | + * @param elem_size size in bytes of the elements in the array | |
| 237 | + * @param elem_data pointer to the data of the element to add. If NULL, the space of | |
| 238 | + * the new added element is not filled. | |
| 239 | + * @return pointer to the data of the element to copy in the new allocated space. | |
| 240 | + * If NULL, the new allocated space is left uninitialized." | |
| 241 | + * @see av_dynarray_add() | |
| 242 | + */ | |
| 243 | +void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size, | |
| 244 | + const uint8_t *elem_data); | |
| 245 | + | |
| 221 | 246 | /** |
| 222 | 247 | * Multiply two size_t values checking for overflow. |
| 223 | 248 | * @return 0 if success, AVERROR(EINVAL) if overflow. |
| @@ -75,7 +75,7 @@ · 74 unmodified lines | ||
| 75 | 75 | */ |
| 76 | 76 | |
| 77 | 77 | #define LIBAVUTIL_VERSION_MAJOR 52 |
| 78 | −#define LIBAVUTIL_VERSION_MINOR 30 | |
| 78 | +#define LIBAVUTIL_VERSION_MINOR 31 | |
| 79 | 79 | #define LIBAVUTIL_VERSION_MICRO 100 |
| 80 | 80 | |
| 81 | 81 | #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ |