From patchwork Tue Nov 20 11:38:37 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 11086 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 C95BD44C8B7 for ; Tue, 20 Nov 2018 13:44:54 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4660D689EE3; Tue, 20 Nov 2018 13:44:55 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9419F6883B3 for ; Tue, 20 Nov 2018 13:44:49 +0200 (EET) Received: by mail-wr1-f65.google.com with SMTP id q18so1614307wrx.9 for ; Tue, 20 Nov 2018 03:44:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=3/TOZ0c0A2af4rkghz8aKKWB8+V0Y+zf9mJL0yas9Ks=; b=sQeB0URWN3h79M0f3z/Ij4cg9uuxB7pZfb/jWyIHcglX3CHOXylulztPn2Hl4swbOo IU9oyziefg/quRgktxcqjMD2F+kk5KBlh8E7Od47nNGkiA2OxJIl07px0VZSyVfHdATM 0vAJxmH7JE9gTeA5lf0clCU683x/J8Ax5nn4R8laVAnccjz6+z2MwhCut7bQP3q7swaP dPXrSVvYDzH1sbE+gbWMRY7sOT/5/Gx0xAvQYdJe7objRo8jWTeQS8XZxjIWrxXlQN2J HabtqzW4G9UuyjMWkh/XtwASvAHHNTMRe1TJdLffDkXrPF4V2gIXPcroyxxVS94zHaya wQsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=3/TOZ0c0A2af4rkghz8aKKWB8+V0Y+zf9mJL0yas9Ks=; b=tiepqeF0g7wzcoIRgksVyuTcSYMblfWxWWYwcYkiubXckdpuHKA6l3CAvSDN58tyL+ Drg7IbvnohxnfchL1SvRHVkB8D/lYilxfsOoGqQNl0DnU5jTlJO03+J9yn+NlK/xsk7U gpRhviYPx892iNFSVAIPD+ybOurjNP+3n1orOHUBQGhTHVSY2UkY1W+APQ7f+4GsBoQG QrmZyCPbxA2VHn0Uq2xLWjC8d8RBsJNUYRccbY2erwCCjzDS25XGvFhpb7QDhhO/XkeN npP1m4QCx7iJZLdRp1ezcrGsuCoICGa4Faq7ao/zhLMh8kxgASR4xnFlicGEAOKzy2bu tFyw== X-Gm-Message-State: AA+aEWZBLilMUmi6fS0v5Sg6CwwJoWHdGp/C72jl+8ZxSCA+EZBsKP9j QbA37LjJp81HFG8wMPx/tI3UwNzv X-Google-Smtp-Source: AFSGD/UOPcbUXI0RpFfVMUVOIIT6vTar5h/jlTYLlzxaTuYDRxIQdVDnLCQBJMa58WGRXX4cBzhqNQ== X-Received: by 2002:adf:90d0:: with SMTP id i74-v6mr1649488wri.181.1542713959808; Tue, 20 Nov 2018 03:39:19 -0800 (PST) Received: from localhost.localdomain (ipbcc08c44.dynamic.kabel-deutschland.de. [188.192.140.68]) by smtp.googlemail.com with ESMTPSA id b13sm13007389wrn.28.2018.11.20.03.39.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Nov 2018 03:39:19 -0800 (PST) From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 20 Nov 2018 12:38:37 +0100 Message-Id: <20181120113839.4512-3-andreas.rheinhardt@googlemail.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181120113839.4512-1-andreas.rheinhardt@googlemail.com> References: <20181120113839.4512-1-andreas.rheinhardt@googlemail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/4] avutil/buffer: Add av_fast_malloc equivalent 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" A new function, avpriv_buffer_fast_alloc, is added. Its distinguishing feature is that the content of the buffer is considered expendable for the given reference, so that no data copying is performed. Signed-off-by: Andreas Rheinhardt --- libavutil/buffer.c | 37 +++++++++++++++++++++++++++++++++++++ libavutil/buffer.h | 19 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/libavutil/buffer.c b/libavutil/buffer.c index 8d1aa5fa84..8303f2fc80 100644 --- a/libavutil/buffer.c +++ b/libavutil/buffer.c @@ -215,6 +215,43 @@ int av_buffer_realloc(AVBufferRef **pbuf, int size) return 0; } +int avpriv_buffer_fast_alloc(AVBufferRef **pbuf, int min_size) +{ + AVBufferRef *buf = *pbuf; + + if (!buf || !av_buffer_is_writable(buf)) { + av_buffer_unref(pbuf); + + buf = av_buffer_alloc(FFMAX(min_size + (min_size >> 4) + 32, + min_size)); + if (!buf) + return AVERROR(ENOMEM); + + *pbuf = buf; + return 0; + } + + if (min_size <= buf->buffer->size) { + buf->data = buf->buffer->data; + buf->size = buf->buffer->size; + return 0; + } + + min_size = FFMAX(min_size + (min_size >> 4) + 32, min_size); + av_free(buf->buffer->data); + buf->buffer->data = av_malloc(min_size); + if (!buf->buffer->data) { + av_free(buf->buffer); + av_freep(pbuf); + return AVERROR(ENOMEM); + } + + buf->data = buf->buffer->data; + buf->size = buf->buffer->size = min_size; + + 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 73b6bd0b14..26715275c4 100644 --- a/libavutil/buffer.h +++ b/libavutil/buffer.h @@ -197,6 +197,25 @@ int av_buffer_make_writable(AVBufferRef **buf); */ int av_buffer_realloc(AVBufferRef **buf, int size); +/** + * Create a writable buffer with given minimum size, reusing the given buffer + * if possible. The content of the buffer is considered expendable for the + * given reference and might not be preserved at all if the buffer is writable. + * The underlying buffer must be compatible with av_free; furthermore, only the + * default free callback may be used. + * + * @param buf Pointer to NULL or a pointer to a pointer to a buffer reference. + * On success, **buf will be an AVBufferRef whose size is the + * allocated size. On failure the reference is unreferenced. + * If *buf is NULL, a new buffer is allocated. + * @param min_size Desired minimal buffer size in bytes. + * @return 0 on success, a negative AVERROR on failure. + * + * @note The function intentionally overallocates in order to reduce the need + * for future allocations. + */ +int avpriv_buffer_fast_alloc(AVBufferRef **buf, int min_size); + /** * @} */