From patchwork Sun Nov 26 01:28:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 44795 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:bca6:b0:181:818d:5e7f with SMTP id fx38csp2451795pzb; Sat, 25 Nov 2023 17:29:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IHrh8eueH7s6uvG6wlCKWhPvuv/2liH64iaHYrgMDebjTRhinFs3UjCppWtVZL5TTmmzTZ3 X-Received: by 2002:a05:6402:254d:b0:548:5671:b6c5 with SMTP id l13-20020a056402254d00b005485671b6c5mr5665515edb.0.1700962182641; Sat, 25 Nov 2023 17:29:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700962182; cv=none; d=google.com; s=arc-20160816; b=BBfyJke9k5j8WOI5k0X3dapVNAOYW+NTb+LofKk8IaixRob6OZz9Q6zvA82K3B8+os O8zxkt/OpEuJ+1e1kRZAAbkpl2XaUBh6x4WsxSRPac3g++5sDA5mcw1JFMDJcZIcWOPR T/L9Ke6w/57QKAgYXACl23npxvxKkOm9RnaoggWAxCaxOmc8l7z6XOV6YIs4RUdGtKJ8 vo0moY9KyDFwQVcd4BZQ5aApdz75AR29+3v/PCwUIuRug190NlXbVBX15ckWSUUQb5ld 7sEiE4lARWGc5OKdlp+gyW78ENv3l3HmxQBIPso6JDRccDH0O9Ur05fTSifYLGV4wOUa Tvmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=aN4oYknymk6o9F1EZjxeX2g1SIZRWj70Xdf1ShppncE=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=unT5Me57pZkZaP6ghAE0KidrQg8se669nVGBfZdhxz2Wk2ES6igWMIhLySPkE9YmKJ 4r0snDpygRArMIOwkBhAUisFg9P+LrD5I4ILBp7bmMZL7sEdwFZOr3mL80VC3daI7Xtj lTIha8WceHgB8smCQyYSMoYHl+I/sJA6FHxcRFamJ1LycaqmU8zTEcT/HKlBS0TeSfWJ P48f0Np/j9VBbQUCpIPfsFKuB83bE11mzqFXSo3H53VmLOFNSTMWyPsfYEnHGkZqtbGX FVY/zui5Vy857NRv+8Y40Q6M5f+rKlBqZgA3+xyMQujZAgB4xYJd19R0T8dl/lYWLRJg dNlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=AMRK3Zgd; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id n14-20020a05640205ce00b00548483895d4si3647450edx.610.2023.11.25.17.29.42; Sat, 25 Nov 2023 17:29:42 -0800 (PST) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=AMRK3Zgd; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 76C4D68CF50; Sun, 26 Nov 2023 03:29:30 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.169]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2945868C98A for ; Sun, 26 Nov 2023 03:29:23 +0200 (EET) Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-6c115026985so3040114b3a.1 for ; Sat, 25 Nov 2023 17:29:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700962160; x=1701566960; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=TSNmKZwoCMkjxuj7+W7roudmvrQ+hNf3DTrFKouFeZ4=; b=AMRK3ZgdIboWaIerTtHc7eFE9ph8w0G/NrudLAYz+09Y9f9uvvUq1j2jdGgdHmdmtX 42b5lwl07izS2m3bGdjSt5EcWCcPzi989ZgBKS+l4vCtOwt/r8PCfUnjBCZvbhvfWOwe O1DTwqKd8596gSOCR6hi7EM5t6CfODDi26/H3biVm+mVeKOyIs3wQla/6E9IKIJy3J9Z 7P6gzq063FM1rqSaC1Jr/cCbLEN5Tmtu+Zty9Kb8NthA/cyvV71f5YTdnKFNNolffqFk lMI3TuCKWDe+uW4vhUgxc7zhrcbSYU37L3o5qbTjjU6moev+FJFSie3lgoxMjlhfFyBZ Rl8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700962160; x=1701566960; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TSNmKZwoCMkjxuj7+W7roudmvrQ+hNf3DTrFKouFeZ4=; b=BgRjts8coNcoVTTVNb30PEmyA4X7Hce6Oz/+7AsACYjFpjh2s+u66LJi1Zc+qlQwep T0kNm2DVayhFBY9JTx3E/Xvo7gV/8UltTy0o/8Za1M8wmBAm7/Q81eWBx/IQ6r1dAghm xyQUptdLL6aQSVtNJFtpmdTSkiTSzMKuCuBaH3dJkEWKwajGMUaZcPS415npZdEgiy7P YrXs0ZWNmagBZw5lZ6j9JiZjiYvLIQ7nrTqO1KxqDz4qWYxplwBWygX+w+fc0XgEnm+Y v1heUhAzSSe/CJXVKCP1F1PRBRFCzr8Dfs7BfCxmApDBJSgpKd+gYVquin0h7su4bP61 G2jg== X-Gm-Message-State: AOJu0YwIIMGWOQR39x3JFVNXraLhzGDffrD6y572nJRoupCoeBQlpxWV Umfd0ydCncoeXCpGlA358Y5awcUPsvs= X-Received: by 2002:a05:6a20:7352:b0:18c:2d7:c4b0 with SMTP id v18-20020a056a20735200b0018c02d7c4b0mr7491456pzc.4.1700962160480; Sat, 25 Nov 2023 17:29:20 -0800 (PST) Received: from localhost.localdomain (host197.190-225-105.telecom.net.ar. [190.225.105.197]) by smtp.gmail.com with ESMTPSA id g3-20020a62e303000000b0068a13b0b300sm5049519pfh.11.2023.11.25.17.29.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Nov 2023 17:29:20 -0800 (PST) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Sat, 25 Nov 2023 22:28:50 -0300 Message-ID: <20231126012858.40388-2-jamrial@gmail.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20231126012858.40388-1-jamrial@gmail.com> References: <20231126012858.40388-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/9] avutil/mem: add av_dynarray2_add_nofree 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: dlF9uXlIZbDg Signed-off-by: James Almer --- libavutil/mem.c | 17 +++++++++++++++++ libavutil/mem.h | 32 +++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/libavutil/mem.c b/libavutil/mem.c index 36b8940a0c..bd37710968 100644 --- a/libavutil/mem.c +++ b/libavutil/mem.c @@ -356,6 +356,23 @@ void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size, return tab_elem_data; } +void *av_dynarray2_add_nofree(void **tab_ptr, int *nb_ptr, size_t elem_size, + const uint8_t *elem_data) +{ + uint8_t *tab_elem_data = NULL; + + FF_DYNARRAY_ADD(INT_MAX, elem_size, *tab_ptr, *nb_ptr, { + tab_elem_data = (uint8_t *)*tab_ptr + (*nb_ptr) * elem_size; + if (elem_data) + memcpy(tab_elem_data, elem_data, elem_size); + else if (CONFIG_MEMORY_POISONING) + memset(tab_elem_data, FF_MEMORY_POISON, elem_size); + }, { + return NULL; + }); + return tab_elem_data; +} + 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..c0161be243 100644 --- a/libavutil/mem.h +++ b/libavutil/mem.h @@ -519,7 +519,7 @@ void av_memcpy_backptr(uint8_t *dst, int back, int cnt); * @param[in,out] tab_ptr Pointer to the array to grow * @param[in,out] nb_ptr Pointer to the number of elements in the array * @param[in] elem Element to add - * @see av_dynarray_add_nofree(), av_dynarray2_add() + * @see av_dynarray_add_nofree(), av_dynarray2_add(), av_dynarray2_add_nofree() */ void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem); @@ -531,7 +531,7 @@ void av_dynarray_add(void *tab_ptr, int *nb_ptr, void *elem); * instead and leave current buffer untouched. * * @return >=0 on success, negative otherwise - * @see av_dynarray_add(), av_dynarray2_add() + * @see av_dynarray_add(), av_dynarray2_add(), av_dynarray2_add_nofree() */ av_warn_unused_result int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem); @@ -557,11 +557,37 @@ int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem); * * @return Pointer to the data of the element to copy in the newly allocated * space - * @see av_dynarray_add(), av_dynarray_add_nofree() + * @see av_dynarray2_add_nofree(), av_dynarray_add(), av_dynarray_add_nofree() */ void *av_dynarray2_add(void **tab_ptr, int *nb_ptr, size_t elem_size, const uint8_t *elem_data); +/** + * Add an element of size `elem_size` to a dynamic array. + * + * The array is reallocated when its number of elements reaches powers of 2. + * Therefore, the amortized cost of adding an element is constant. + * + * In case of success, the pointer to the array is updated in order to + * point to the new grown array, and the number pointed to by `nb_ptr` + * is incremented. + * In case of failure, the array and `nb_ptr` are left untouched, and NULL + * is returned. + * + * @param[in,out] tab_ptr Pointer to the array to grow + * @param[in,out] nb_ptr Pointer to the number of elements in the array + * @param[in] elem_size Size in bytes of an element in the array + * @param[in] elem_data Pointer to the data of the element to add. If + * `NULL`, the space of the newly added element is + * allocated but left uninitialized. + * + * @return Pointer to the data of the element to copy in the newly allocated + * space on success, NULL otherwise. + * @see av_dynarray2_add(), av_dynarray_add(), av_dynarray_add_nofree() + */ +void *av_dynarray2_add_nofree(void **tab_ptr, int *nb_ptr, size_t elem_size, + const uint8_t *elem_data); + /** * @} */