From patchwork Mon Oct 23 17:25:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 44335 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1b28:b0:15d:8365:d4b8 with SMTP id ch40csp1556576pzb; Mon, 23 Oct 2023 10:25:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEgYntmXlVG8gNutA1TJbBxiUD9aa+LIin9AYOXyA4JWp3uKU7gPp2qwMM+dodLuaLoAKf8 X-Received: by 2002:a17:907:d1b:b0:9a1:aaae:8207 with SMTP id gn27-20020a1709070d1b00b009a1aaae8207mr12230537ejc.20.1698081957830; Mon, 23 Oct 2023 10:25:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698081957; cv=none; d=google.com; s=arc-20160816; b=HJurbB/5OowE104VzOfqNdhj0sCF3lv9QEuOTU/cbTYd4fU7VfYBptA4knEEzJDukU O+emGVOnrHUoT8cNoeUrRAFGXJTC0Y8ufy5bRyGlXdycXJAHtgGaRxr9JlRzENQpX0nA JbcRFPJwofKc9jWlbdhQ0fiXg1T2k9Pqx9qQZdDGo2OEG1SNF9uN5llTZmmC1X5dhngk hX25zLOaW8ZWvee2D0OUmMp/5d3oqIGqbbt0RktgHqH6yTBJRyNupTpkIgxRWOMugb6E AcsfsxdJ3fR5tXo2QZbnnUOuoPDFqvrBIaYuXZgRLMTF6me4QdWOGSx2i0/eZL3ajnpQ Hc7A== 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=2a3CPP48js/DZUkKvKeR/al4XDyyp0Wf/Cj6TmhfqUs=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=c6r6x66Lbs5I5ZeUzYc33JD5WnCmtELBFl7AjIcCfo03HqSJiGVOooEcjqcQLXd7BV viWE1kR8GQN0btSN5LENbBQOSa8Z+VNAJ+ICBK9pV0p0IYTujI4T2F4kSFwjjB7eiOcI d8ktmC/A7qnTQE3gjM0/GxWs1TnmOMVA/1p0hy9UDt63Ah2YWjvBu0UxMRC2Yscznc8Y eQVotl7r4QWW5RfRNFLDEprzkRvh7tpAHDEyX6FcHyj7MEitPLkDM+/v4E7Rhr0lvrCn aXy/VSQqnnwjqZT0KfP8OiBiap6CbM3dzx+b0MW/RKhM0GSAyO7x50+EOzifL2G2Hado ON1g== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=HV64xIlm; 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 n27-20020a170906379b00b009c55f040e2fsi3398594ejc.588.2023.10.23.10.25.57; Mon, 23 Oct 2023 10:25:57 -0700 (PDT) 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=HV64xIlm; 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 8A6F768CA29; Mon, 23 Oct 2023 20:25:53 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E38B468C077 for ; Mon, 23 Oct 2023 20:25:46 +0300 (EEST) Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-27d45f5658fso2805177a91.3 for ; Mon, 23 Oct 2023 10:25:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698081944; x=1698686744; 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=8aPTtjDpjeE7zZuDivxmaIMObjEIQPWLUc496SLawg0=; b=HV64xIlm20QXGtzacHu968pKON1RQk9ltW0apAetpCH0QIEWr8rx+xeenCES+6TchF vy3yuOEAFyXq0Hu+h23AtSJ7BzaRLTCb7T8um0Obk3+CCKuBWUtzf5fBAmPgnxnoEo1R oZX5Gt6t5ylF95oYxCGQI5ae5clVLSqvmyM0B9nmrTVuoP9elguGuHh9A4nLUdaWkuBu sg1qvT9WbNzT4TN9w0AgJ1qxoFrSIqpahUcLfXHQ6r5E76vh+E/6yFUswVqR+0hSuCjc CkGikKi6dC42Ndg8jgTQHKtatIBzxjkb4zJyINlRXH+TMASs3zv9wg8a4KQhuEnfHEaz O8Og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698081944; x=1698686744; 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=8aPTtjDpjeE7zZuDivxmaIMObjEIQPWLUc496SLawg0=; b=jAd8lpa2LnMnJkBmlZq05YsCtzKDntp80ES/cAPGFMsDHaVLWlUWbcdcJU0EIRnGg3 M/r7S8zHEnQ5FxFAwIDXOTQ3BX8ePwVpaLFdroOtEIH7sqUfsqQyfYi4wEUjIcX8ULWv YUjJoP1KBd6tovPCpN3wQghQPdpe0swG6DXRm5KLYT7/0UQo3dZYHmwE1sFsFR+0ZNW1 2R+qwUXZ5824vC+lulD2fwpVVpXJaHA1bnBZvIN7jEk1FPQD8NB91dz38AJ9HudLb/Vh VNSFkiiXfCBmBEDITLbMgb1SgWX/wRgB88aGXKZTuhbGJckVuMEqRL1OTgBCW3iFXlWk 7gjQ== X-Gm-Message-State: AOJu0YyiY/2Z7NrNUL/2cQHRSyPrTWvXN8tSFmoEUXUJB6bMrPEh2B64 OUa3+eliwxJLI8J3aIjriCdRSotmA5w= X-Received: by 2002:a17:90a:c202:b0:27d:af3:f15d with SMTP id e2-20020a17090ac20200b0027d0af3f15dmr9036643pjt.4.1698081944032; Mon, 23 Oct 2023 10:25:44 -0700 (PDT) Received: from localhost.localdomain (host197.190-225-105.telecom.net.ar. [190.225.105.197]) by smtp.gmail.com with ESMTPSA id ge13-20020a17090b0e0d00b00271c5811019sm5736552pjb.38.2023.10.23.10.25.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 10:25:43 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Mon, 23 Oct 2023 14:25:26 -0300 Message-ID: <20231023172527.4460-1-jamrial@gmail.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231023140851.2087-1-jamrial@gmail.com> References: <20231023140851.2087-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2] 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: +n4TTZBwEIV6 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); + /** * @} */