From patchwork Mon Aug 3 09:46:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 21465 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id CC8BF4494F0 for ; Mon, 3 Aug 2020 12:46:30 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id B0B2F68BA01; Mon, 3 Aug 2020 12:46:30 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.red.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 22DD368B6BA for ; Mon, 3 Aug 2020 12:46:25 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail.red.khirnov.net (Postfix) with ESMTP id CB96728BFED for ; Mon, 3 Aug 2020 11:46:24 +0200 (CEST) Received: from mail.red.khirnov.net ([IPv6:::1]) by localhost (mail.red.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id iOuy9qWCbzox for ; Mon, 3 Aug 2020 11:46:23 +0200 (CEST) Received: from libav.daenerys.khirnov.net (libav.daenerys.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.daenerys.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail.red.khirnov.net (Postfix) with ESMTPS id 88FC628BFE0 for ; Mon, 3 Aug 2020 11:46:23 +0200 (CEST) Received: by libav.daenerys.khirnov.net (Postfix, from userid 1000) id 20D5120E0089; Mon, 3 Aug 2020 11:46:23 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 3 Aug 2020 11:46:22 +0200 Message-Id: <20200803094622.2745-1-anton@khirnov.net> X-Mailer: git-send-email 2.27.0 In-Reply-To: References: MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] lavu/buffer: add a convenience function for replacing buffers X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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" A common pattern e.g. in libavcodec is replacing/updating buffer references: unref old one, ref new one. This function allows simplifying such code an avoiding unnecessary refs+unrefs if the references are already equivalent. --- Updated to not touch dst on failure. Still not using buffer_replace(), since it does not seem to make the code simpler. --- doc/APIchanges | 3 +++ libavutil/buffer.c | 26 ++++++++++++++++++++++++++ libavutil/buffer.h | 16 ++++++++++++++++ libavutil/version.h | 2 +- 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/doc/APIchanges b/doc/APIchanges index 72a4833fbc..5a3f0165a8 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -15,6 +15,9 @@ libavutil: 2017-10-21 API changes, most recent first: +2020-xx-xx - xxxxxxxxxx - lavu 56.51.100 - buffer.h + Add a av_buffer_replace() convenience function. + 2020-07-xx - xxxxxxxxxx - lavu 56.57.100 - cpu.h Add AV_CPU_FLAG_MMI and AV_CPU_FLAG_MSA. diff --git a/libavutil/buffer.c b/libavutil/buffer.c index 38a554208a..39539f99e9 100644 --- a/libavutil/buffer.c +++ b/libavutil/buffer.c @@ -215,6 +215,32 @@ int av_buffer_realloc(AVBufferRef **pbuf, int size) return 0; } +int av_buffer_replace(AVBufferRef **pdst, AVBufferRef *src) +{ + AVBufferRef *dst = *pdst; + AVBufferRef *tmp; + + if (!src) { + av_buffer_unref(pdst); + return 0; + } + + if (dst && dst->buffer == src->buffer) { + /* make sure the data pointers match */ + dst->data = src->data; + dst->size = src->size; + return 0; + } + + tmp = av_buffer_ref(src); + if (!tmp) + return AVERROR(ENOMEM); + + av_buffer_unref(pdst); + *pdst = tmp; + return 0; +} + AVBufferPool *av_buffer_pool_init2(int size, void *opaque, AVBufferRef* (*alloc)(void *opaque, int size), void (*pool_free)(void *opaque)) diff --git a/libavutil/buffer.h b/libavutil/buffer.h index c0f3f6cc9a..fd4e381efa 100644 --- a/libavutil/buffer.h +++ b/libavutil/buffer.h @@ -197,6 +197,22 @@ int av_buffer_make_writable(AVBufferRef **buf); */ int av_buffer_realloc(AVBufferRef **buf, int size); +/** + * Ensure dst refers to the same data as src. + * + * When *dst is already equivalent to src, do nothing. Otherwise unreference dst + * and replace it with a new reference to src. + * + * @param dst Pointer to either a valid buffer reference or NULL. On success, + * this will point to a buffer reference equivalent to src. On + * failure, dst will be left untouched. + * @param src A buffer reference to replace dst with. May be NULL, then this + * function is equivalent to av_buffer_unref(dst). + * @return 0 on success + * AVERROR(ENOMEM) on memory allocation failure. + */ +int av_buffer_replace(AVBufferRef **dst, AVBufferRef *src); + /** * @} */ diff --git a/libavutil/version.h b/libavutil/version.h index 975fb87f31..3e7e1f410b 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -79,7 +79,7 @@ */ #define LIBAVUTIL_VERSION_MAJOR 56 -#define LIBAVUTIL_VERSION_MINOR 57 +#define LIBAVUTIL_VERSION_MINOR 58 #define LIBAVUTIL_VERSION_MICRO 100 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \