From: Niklas Haas <g...@haasn.dev>

Like av_memdup() but correctly rounds up to the nearest power of two so that
av_dynarray2_add() will continue to work on the duplicated list.
---
 libavutil/mem.c |  9 +++++++++
 libavutil/mem.h | 14 ++++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/libavutil/mem.c b/libavutil/mem.c
index b205d3fb25..2ed4553069 100644
--- a/libavutil/mem.c
+++ b/libavutil/mem.c
@@ -39,6 +39,7 @@
 
 #include "attributes.h"
 #include "avassert.h"
+#include "common.h"
 #include "dynarray.h"
 #include "error.h"
 #include "internal.h"
@@ -358,6 +359,14 @@ void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t 
elem_size,
     return tab_elem_data;
 }
 
+void *av_dynarray2_dup(void *tab, int nb, size_t elem_size)
+{
+    if (nb)
+        nb = 1 << av_ceil_log2(nb);
+
+    return av_memdup(tab, nb * elem_size);
+}
+
 static void fill16(uint8_t *dst, int len)
 {
     uint32_t v = AV_RN16(dst - 2);
diff --git a/libavutil/mem.h b/libavutil/mem.h
index ab7648ac57..7815327a88 100644
--- a/libavutil/mem.h
+++ b/libavutil/mem.h
@@ -562,6 +562,20 @@ int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, 
void *elem);
 void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size,
                        const uint8_t *elem_data);
 
+/**
+ * Duplicate an array allocated by `av_dynarray2_add`. Preserves the size of
+ * the underlying allocation, so that the returned array can be again modified
+ * using `av_dynarray2_add`.
+ *
+ * @param[in] tab       Pointer to the array to duplicate
+ * @param[in] nb        Number of elements in the array
+ * @param[in] elem_size Size in bytes of an element in the array
+ *
+ * @return Pointer to the newly allocated array, or `NULL` on failure.
+ * @see av_dynarray2_add()
+ */
+void *av_dynarray2_dup(void *tab, int nb, size_t elem_size);
+
 /**
  * @}
  */
-- 
2.49.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".

Reply via email to