From patchwork Wed Mar 14 15:30:04 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 7995 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.1.70 with SMTP id c67csp188744jad; Wed, 14 Mar 2018 08:31:39 -0700 (PDT) X-Google-Smtp-Source: AG47ELsSv4ZD908ePtrbmkoor4rW63+2vDJuTJoSPjc49soPioJjSEl1qnzQnEFB0r5md+v6g6k2 X-Received: by 10.28.72.132 with SMTP id v126mr1980141wma.150.1521041499497; Wed, 14 Mar 2018 08:31:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521041499; cv=none; d=google.com; s=arc-20160816; b=j46MYbENLTSTqMuWYg4H4NgXGNGogGfWCvWHnRaW6L2TtwbNwcNUJ7nTrbaQUvLEu3 J0OOLVOM6RbPrMNGYmKbMHfm1Nsmz9M4IvygUHfPr/yh2lXh0ivKIWDiV4SflyDXnd4V iZYxaq29ULXALSpUCdGbArbpAIHZrxGSInqHYFeZzjQBmQYli5Q3B4sun3GAi/UsB2Vt 5sXpQj4a4pMJDvYLm4bfrhremxIyF0mxL+DbHxU+/fBXRpPdmjF88N399Tso6eIKUElc Q8XJ4n4DBrgF1ZCUAegesoyQQqbXLjsAyFKCil2rVDWKZGWLV7b/+lhe4CK8p/eIFixn MrWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:message-id:date:to:from:dkim-signature :delivered-to:arc-authentication-results; bh=wLvjPKTa9X8T5C2bc9f/sCNksYoKDdLB9PVNLRI7A1Q=; b=GrY76rc5tnjyKK5/UHGtLkQNUS1M0iKBeOcRnj/ZBfEjfc35xY8kvobREP012shqtg KZh/AB+mRzAQQAoZye68q1JBOqmBMljCp18/RUR3vVbJV0ve7T4wW9Hsi12EpSxcj/PJ geAWsA0+29541lDsqNTWhMbspJorDTqurrtDDx9wxaBUSE12X19HGbCUWZ5u6OQGnN+4 sDzi1ZqyncIaMWcACwBMLALx3Gh+iPxsYM1dqc0uI4zvcJGdIMqJh83W+5Qqcb8Osasl TayQ1uMmZf9BgbwrRZ9pQpMyOi/al+OYh9UXGd5jhBEywnOJN54oNODkar9t93QqFWxQ eFeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=oFDS5pk+; 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 c127si1087744wme.63.2018.03.14.08.31.38; Wed, 14 Mar 2018 08:31:39 -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=20161025 header.b=oFDS5pk+; 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 603D868A1FC; Wed, 14 Mar 2018 17:31:26 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk0-f196.google.com (mail-qk0-f196.google.com [209.85.220.196]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id CE90C68995C for ; Wed, 14 Mar 2018 17:31:20 +0200 (EET) Received: by mail-qk0-f196.google.com with SMTP id f25so3913321qkm.0 for ; Wed, 14 Mar 2018 08:31:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id; bh=azqCR2AGSOZjYmOMJK0SPW3mTsqMKeu7GxYA9ja4eQ0=; b=oFDS5pk+ZGbF9a/eII7EGlyieoBTOVXXdNZiIny4LCSnWtMU65GHg7FlXesg46Zj80 1DrsYG0+LcReJ7hyTib1sQtzs0doFzk4NbeCd4YhCWfdaOiEygn3LuiV6S8E9CkgFeI5 ixEiG4MDnFBW3+3YGSxqafCGfaC0/HqSfQeOO05mTDrq77k/anpwhNADydLjNOgu7jNu qGC3JaiP0UF0dvdbZ3vx70A++4PVSm4neihY6dQAsDcf2N9xzVWpV5NDpN6ApS4edUKW N43AgIXFZALhW8ysRtysLzO2iQYrOWh8RTol9L0S4SyEItMnAz6RWfyqeJpfTNaAfpe0 7UZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=azqCR2AGSOZjYmOMJK0SPW3mTsqMKeu7GxYA9ja4eQ0=; b=TbmqFvLmHyH5NB3JcGyhZAiXx06+6L0dt7Y1iWnAbEbzPqqkFfBVLhZKJVM7taBLPp jwnam6KlK8sHfrq9Ug7HWoCRCzyeXkKiVomAIMilcpOEDi+l9Cv2ZNiGUWaS1W9Xfw7Z VBzkrGt+AfD5e2W8OlSuuUHRPIoWG9uON+NRF9IEwEcuvYjojZg713ailsD1uDzpbEUz HXUVWnyfCyckE7Op31orMWQNGN9L+meSyvrb9hV52pqFRpcTKLFY0+wDGCEiMG0c6LSI HOh7+u2RmBROr9D76mL2EydkimBy9BExC99sXhYceDF080tFghWvbBvqa8180pTBckoE Lcyg== X-Gm-Message-State: AElRT7EQL1ErhUYVFYSfYYY3L/qnWFYlAOtFdFeVnIQwj3EeLbQgI03s aUXyrxdQoxj+ws+aYuvSTrD8eg== X-Received: by 10.55.41.138 with SMTP id p10mr7586404qkp.22.1521041491309; Wed, 14 Mar 2018 08:31:31 -0700 (PDT) Received: from localhost.localdomain ([190.188.171.140]) by smtp.gmail.com with ESMTPSA id t28sm1684607qkj.61.2018.03.14.08.31.30 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 14 Mar 2018 08:31:30 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Wed, 14 Mar 2018 12:30:04 -0300 Message-Id: <20180314153004.6436-1-jamrial@gmail.com> X-Mailer: git-send-email 2.16.2 Subject: [FFmpeg-devel] [PATCH v2] avutil/buffer: add av_buffer_fast_alloc() 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 MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Same use case as av_fast_malloc(). If the buffer passed to it is writable and big enough it will be reused, otherwise a new one will be allocated instead. Signed-off-by: James Almer --- TODO: Changelog and APIChanges entries, version bump. libavutil/buffer.c | 33 +++++++++++++++++++++++++++++++++ libavutil/buffer.h | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/libavutil/buffer.c b/libavutil/buffer.c index 8d1aa5fa84..26e015d7ee 100644 --- a/libavutil/buffer.c +++ b/libavutil/buffer.c @@ -215,6 +215,39 @@ int av_buffer_realloc(AVBufferRef **pbuf, int size) return 0; } +static inline int buffer_fast_alloc(AVBufferRef **pbuf, int size, + AVBufferRef* (*buffer_alloc)(int size)) +{ + AVBufferRef *buf = *pbuf; + + if (buf && av_buffer_is_writable(buf) + && buf->data == buf->buffer->data + && size <= buf->buffer->size) { + buf->size = FFMAX(0, size); + return 0; + } + + av_buffer_unref(pbuf); + + buf = buffer_alloc(size); + if (!buf) + return AVERROR(ENOMEM); + + *pbuf = buf; + + return 0; +} + +int av_buffer_fast_alloc(AVBufferRef **pbuf, int size) +{ + return buffer_fast_alloc(pbuf, size, av_buffer_alloc); +} + +int av_buffer_fast_allocz(AVBufferRef **pbuf, int size) +{ + return buffer_fast_alloc(pbuf, size, av_buffer_allocz); +} + 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 73b6bd0b14..1166017d22 100644 --- a/libavutil/buffer.h +++ b/libavutil/buffer.h @@ -197,6 +197,48 @@ int av_buffer_make_writable(AVBufferRef **buf); */ int av_buffer_realloc(AVBufferRef **buf, int size); +/** + * Allocate a buffer, reusing the given one if writable and large enough. + * + * @code{.c} + * AVBufferRef *buf = ...; + * int ret = av_buffer_fast_alloc(&buf, size); + * if (ret < 0) { + * // Allocation failed; buf already freed + * return ret; + * } + * @endcode + * + * @param buf A buffer reference. *buf may be NULL. On success, a new buffer + * reference will be written in its place. On failure, it will be + * unreferenced and set to NULL. + * @param size Required buffer size. + * + * @return 0 on success, a negative AVERROR on failure. + * + * @see av_buffer_realloc() + * @see av_buffer_fast_allocz() + */ +int av_buffer_fast_alloc(AVBufferRef **buf, int size); + +/** + * Allocate and clear a buffer, reusing the given one if writable and large + * enough. + * + * Like av_buffer_fast_alloc(), but all newly allocated space is initially + * cleared. Reused buffer is not cleared. + * + * @param buf A buffer reference. *buf may be NULL. On success, a new buffer + * reference will be written in its place. On failure, it will be + * unreferenced and set to NULL. + * @param size Required buffer size. + * + * @return 0 on success, a negative AVERROR on failure. + * + * @see av_buffer_fast_alloc() + */ +int av_buffer_fast_allocz(AVBufferRef **buf, int size); + /** * @} */