History

lavu/mem: add av_dynarray2_add()

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

Stefano Sabatini committed 13 years ago

Commit e3984161 parent 84be8064 files · +62 −1Browse files

Unified · Split

4 files
  1. doc/APIchanges +3 −0
  2. libavutil/mem.c +33 −0
  3. libavutil/mem.h +25 −0
  4. libavutil/version.h +1 −1
doc/APIchanges+3−0
@@ -15,6 +15,9 @@ libavutil: 2012-10-22 · 14 unmodified lines
1515
1616 API changes, most recent first:
1717
18+2013-05-13 - xxxxxxx - lavu 52.31.100 - mem.h
19+ Add av_dynarray2_add().
20+
1821 2013-05-12 - xxxxxxx - lavfi 3.65.100
1922 Add AVFILTER_FLAG_SUPPORT_TIMELINE* filter flags.
2023
libavutil/mem.c+33−0
@@ -270,6 +270,39 @@ fail: · 269 unmodified lines
270270 *nb_ptr = 0;
271271 }
272272
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+
273306 static void fill16(uint8_t *dst, int len)
274307 {
275308 uint32_t v = AV_RN16(dst - 2);
libavutil/mem.h+25−0
@@ -215,9 +215,34 @@ void av_freep(void *ptr); · 214 unmodified lines
215215 * @param tab_ptr pointer to the array to grow
216216 * @param nb_ptr pointer to the number of elements in the array
217217 * @param elem element to add
218+ * @see av_dynarray2_add()
218219 */
219220 void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem);
220221
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+
221246 /**
222247 * Multiply two size_t values checking for overflow.
223248 * @return 0 if success, AVERROR(EINVAL) if overflow.
libavutil/version.h+1−1
@@ -75,7 +75,7 @@ · 74 unmodified lines
7575 */
7676
7777 #define LIBAVUTIL_VERSION_MAJOR 52
78−#define LIBAVUTIL_VERSION_MINOR 30
78+#define LIBAVUTIL_VERSION_MINOR 31
7979 #define LIBAVUTIL_VERSION_MICRO 100
8080
8181 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t