From patchwork Wed Sep 28 10:48:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 38435 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:3b1c:b0:96:9ee8:5cfd with SMTP id c28csp121782pzh; Wed, 28 Sep 2022 03:49:38 -0700 (PDT) X-Google-Smtp-Source: AMsMyM41oWFfD8/H1PygtyP5D/zilaTQMlT9ZFKM+wwTG2SeiJFGkIEy12226OV4fuNdKExEh/xk X-Received: by 2002:a17:906:dac8:b0:741:545b:796a with SMTP id xi8-20020a170906dac800b00741545b796amr26569590ejb.240.1664362178503; Wed, 28 Sep 2022 03:49:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664362178; cv=none; d=google.com; s=arc-20160816; b=DsBZyAp7K0c8pIVix6JUd1ym0CeApASK+wBdWHR6gG/Pioc8ZlCjwkheZHH48gstUY 3x7o3fFzkq75VocES4a8xGl5gIsMCTpOdHDF4Ee7cHz+gV45+VVoKcmu2Ml9XfKlz4qt bMOkMR1BvKHHTQs3Q3Rzeaf3Y5Uq67q92PD9PNdCiRB6PqOgZ3BdlQ278caV9z5bPBby zIiuHRRJcx6e+AVYaAZCmFzCuJadE+QrSpStt6KNH3sZzYKjZexWhIQw2DJL+E1nwNsQ Mf15EH/sxHlRr9RLY8Mj7K4a3XeITnATJX+gUnr03CKfSYKt9DvrFZTebrCHIxvPt8RW 4EAg== 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:message-id:date:to:from :delivered-to; bh=pRUOmEyds2g3nFbrWrRscF6i5RmAfcS3w8Q4xxtNFL8=; b=XJ9NyPIUGc+VM2XxPq5LsfNVctjie0pea+Qy9rRmQyraysZvGBTGksLQXHmb6tXPqT wnDoWxZHuC8iThkp+LYKx1gbrjGbC0An+WhxUBKOhg7p/54Vla1yvFDSQSuJe1hjNVYx 461+NjMJ4kbQlROE6K1YSMnobMdTC0pAfeB+mZoZcyT1pK4J6JkaAUXLxmYQw8tF+hFN m91zZYk4Enkd4EXk+NelIcXeO7KTGY4KADluGNIxmfqQ2pbF27di933SRmnE5ZMM30It dmJZ+mZuZpDic5aQddLZf11gt7m9OyCLR3ITd1wQif7spy6JPSbcn4cXzGUuKBjX9X9Z WrPg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id p10-20020a50c94a000000b0043be773a90dsi4075586edh.45.2022.09.28.03.49.37; Wed, 28 Sep 2022 03:49:38 -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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 0E77968BC84; Wed, 28 Sep 2022 13:49:10 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 779FA68BB56 for ; Wed, 28 Sep 2022 13:49:00 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 159412404F8 for ; Wed, 28 Sep 2022 12:49:00 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id UOp5Ksr58Ojx for ; Wed, 28 Sep 2022 12:48:59 +0200 (CEST) Received: from libav.khirnov.net (libav.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.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id C5DEB2404E4 for ; Wed, 28 Sep 2022 12:48:57 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 96DDA3A014F for ; Wed, 28 Sep 2022 12:48:57 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Wed, 28 Sep 2022 12:48:49 +0200 Message-Id: <20220928104854.18629-1-anton@khirnov.net> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/6] lavu/mem: add av_realloc_reuse() as a replacement for av_fast_realloc() 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: VlGHEOMFWGUL It uses size_t rather than unsigned for the size and conforms to our standard naming scheme. --- configure | 5 ++++- doc/APIchanges | 3 +++ libavutil/mem.c | 30 ++++++++++++++++++++++++++++++ libavutil/mem.h | 37 +++++++++++++++++++++++++++++++++++-- libavutil/version.h | 3 ++- 5 files changed, 74 insertions(+), 4 deletions(-) diff --git a/configure b/configure index 6712d045d9..fd0e6ae032 100755 --- a/configure +++ b/configure @@ -4360,7 +4360,10 @@ case "$toolchain" in target_exec_default="valgrind" case "$toolchain" in valgrind-massif) - target_exec_args="--tool=massif --alloc-fn=av_malloc --alloc-fn=av_mallocz --alloc-fn=av_calloc --alloc-fn=av_fast_padded_malloc --alloc-fn=av_fast_malloc --alloc-fn=av_realloc_f --alloc-fn=av_fast_realloc --alloc-fn=av_realloc" + target_exec_args="--tool=massif" + for func in av_malloc av_mallocz av_calloc av_fast_padded_malloc av_fast_malloc av_realloc_f av_fast_realloc av_realloc av_realloc_reuse; do + target_exec_args="$target_exec_args --alloc-fn=$func" + done ;; valgrind-memcheck) target_exec_args="--error-exitcode=1 --malloc-fill=0x2a --track-origins=yes --leak-check=full --gen-suppressions=all --suppressions=$source_path/tests/fate-valgrind.supp" diff --git a/doc/APIchanges b/doc/APIchanges index b0a41c9e37..9a735c27e7 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -14,6 +14,9 @@ libavutil: 2021-04-27 API changes, most recent first: +2022-xx-xx - xxxxxxxxxx - lavu 57.38.100 - mem.h + Add av_realloc_reuse(), deprecate av_fast_realloc(). + 2022-09-26 - xxxxxxxxxx - lavc 59.48.100 - avcodec.h Deprecate avcodec_enum_to_chroma_pos() and avcodec_chroma_pos_to_enum(). Use av_chroma_location_enum_to_pos() or av_chroma_location_pos_to_enum() diff --git a/libavutil/mem.c b/libavutil/mem.c index 18aff5291f..51fdc9155f 100644 --- a/libavutil/mem.c +++ b/libavutil/mem.c @@ -502,6 +502,35 @@ void av_memcpy_backptr(uint8_t *dst, int back, int cnt) } } +void *av_realloc_reuse(void *ptr, size_t *size, size_t min_size) +{ + size_t max_size; + + if (min_size <= *size) + return ptr; + + max_size = atomic_load_explicit(&max_alloc_size, memory_order_relaxed); + + if (min_size > max_size) { + *size = 0; + return NULL; + } + + min_size = FFMIN(max_size, FFMAX(min_size + min_size / 16 + 32, min_size)); + + ptr = av_realloc(ptr, min_size); + /* we could set this to the unmodified min_size but this is safer + * if the user lost the ptr and uses NULL now + */ + if (!ptr) + min_size = 0; + + *size = min_size; + + return ptr; +} + +#if FF_API_FAST_ALLOC void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size) { size_t max_size; @@ -531,6 +560,7 @@ void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size) return ptr; } +#endif static inline void fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc) { diff --git a/libavutil/mem.h b/libavutil/mem.h index d91174196c..deb440ca1a 100644 --- a/libavutil/mem.h +++ b/libavutil/mem.h @@ -264,7 +264,7 @@ void *av_mallocz_array(size_t nmemb, size_t size) av_malloc_attrib av_alloc_size * @warning Unlike av_malloc(), the returned pointer is not guaranteed to be * correctly aligned. The returned pointer must be freed after even * if size is zero. - * @see av_fast_realloc() + * @see av_realloc_reuse() * @see av_reallocp() */ void *av_realloc(void *ptr, size_t size) av_alloc_size(2); @@ -346,6 +346,35 @@ av_alloc_size(2, 3) void *av_realloc_array(void *ptr, size_t nmemb, size_t size) */ int av_reallocp_array(void *ptr, size_t nmemb, size_t size); +/** + * Reallocate a data buffer, reusing the existing one if it is large enough. + * + * This function is similar to av_realloc(), but optimized for cases where the + * buffer may grow significantly and is not expected to shrink. + * + * @param[in] ptr Previously allocated buffer, or `NULL`. If `ptr` is `NULL`, a + * new uninitialized buffer is allocated. `ptr` is invalidated when this + * function returns non-`NULL` and must be replaced with its return value. + * + * @param[in,out] size Pointer to the allocated size of buffer `ptr`. This + * function updates `*size` to the new allocated size (which may be larger than + * `min_size`). `*size` is set to 0 on failure. + * + * @param[in] min_size Minimum size in bytes of the returned buffer. + * + * @return + * - An allocated buffer (to be freed with `av_free()`) that is large enough to + * hold at least `min_size` bytes. The first `*size` (value on entry to this + * function) bytes of the buffer remain the same as the data in `ptr`, the + * rest is uninitialized. + * - `NULL` on failure, then `*size` is set to 0 and ptr remains untouched. + * + * @see av_realloc() + * @see av_fast_malloc() + */ +void *av_realloc_reuse(void *ptr, size_t *size, size_t min_size); + +#if FF_API_FAST_ALLOC /** * Reallocate the given buffer if it is not large enough, otherwise do nothing. * @@ -377,13 +406,17 @@ int av_reallocp_array(void *ptr, size_t nmemb, size_t size); * error * @see av_realloc() * @see av_fast_malloc() + * + * @deprecated use av_realloc_reuse() */ +attribute_deprecated void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size); +#endif /** * Allocate a buffer, reusing the given one if large enough. * - * Contrary to av_fast_realloc(), the current buffer contents might not be + * Contrary to av_realloc_reuse(), the current buffer contents might not be * preserved and on error the old buffer is freed, thus no special handling to * avoid memleaks is necessary. * diff --git a/libavutil/version.h b/libavutil/version.h index 9c44cef6aa..285a32f3d6 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -79,7 +79,7 @@ */ #define LIBAVUTIL_VERSION_MAJOR 57 -#define LIBAVUTIL_VERSION_MINOR 37 +#define LIBAVUTIL_VERSION_MINOR 38 #define LIBAVUTIL_VERSION_MICRO 100 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ @@ -115,6 +115,7 @@ #define FF_API_OLD_CHANNEL_LAYOUT (LIBAVUTIL_VERSION_MAJOR < 58) #define FF_API_AV_FOPEN_UTF8 (LIBAVUTIL_VERSION_MAJOR < 58) #define FF_API_PKT_DURATION (LIBAVUTIL_VERSION_MAJOR < 58) +#define FF_API_FAST_ALLOC (LIBAVUTIL_VERSION_MAJOR < 58) /** * @}