From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.ffmpeg.org (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id 9359E4E52B for ; Tue, 10 Jun 2025 13:12:59 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id 9E24B68D25C; Tue, 10 Jun 2025 16:11:10 +0300 (EEST) Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id 8CD3468CFB5 for ; Tue, 10 Jun 2025 16:10:52 +0300 (EEST) Received: from haasn.dev (unknown [10.30.1.1]) by haasn.dev (Postfix) with UTF8SMTP id 10A29422C8; Tue, 10 Jun 2025 15:10:47 +0200 (CEST) From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Tue, 10 Jun 2025 15:04:57 +0200 Message-ID: <20250610131042.200918-7-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250610131042.200918-1-ffmpeg@haasn.xyz> References: <20250610131042.200918-1-ffmpeg@haasn.xyz> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v5 06/19] avutil/mem: add av_dynarray2_dup X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Niklas Haas Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: From: Niklas Haas 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-request@ffmpeg.org with subject "unsubscribe".