From patchwork Tue Mar 13 23:48:56 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 7964 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.2.1.70 with SMTP id c67csp1368573jad; Tue, 13 Mar 2018 16:49:21 -0700 (PDT) X-Google-Smtp-Source: AG47ELvUytRNGvv6xgle3JX7wLfWY6jQcetwwvn3KBLtSG9Yk9l39vAq6HvDej0JydVnyIZVWKUU X-Received: by 10.223.134.136 with SMTP id 8mr2081327wrx.86.1520984961548; Tue, 13 Mar 2018 16:49:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520984961; cv=none; d=google.com; s=arc-20160816; b=0F1tyAqzV8Eead15GLRTD0aeop1ChYgO3Uc9Nh6AdQTs6wKO54jUdpxwQo0Jqfaqsq PGjF6NVw5JQZ7/kJK4hSss8kIyzXynlQLrl5/2nblQLV5qD61iawBNVFnjVhBOysu1Hi D4vQuSE6wl3xVeUTFASZ7Os3dCahIdIknqtut9vQ77d5x9OrLfAHTOXY6fO9iOeYkmCb tB8pP7L+2jnjjFRuLB6Adk4vVsB24L8pB/y3smtFQ/XxKzvCdE6ILr8/ylssgQcAdqOW ObqH5NWHdgJqdBffGikETWjAJ4gIxpn6Xoh2KmPapeYOVIPtlYpeOXgPBGw1CgD2WWje 9XCw== 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=qlCbtxk2g3iGvVHWzEY9WoqrHZNUTPHgrIK/jtRwHdU=; b=CEzE+OyvDzf328d0+Zl/Yr8ip0tjtm6wr34CL2J3lGfeWy/NG0sAxZbLc0i9ohIxwe MdVVyYpiHBH5ZnzfH9Ye2w8ceQJPyoV9/QWPEonFcjer5zNn0PpvmbF87EtgVPmxE639 iKmPx6C+Ql6rzrU9MJ/CTgp2R1yuH6ICklRK6SqzYxADj53FSJb69qYgSrU7Lfc0HQmu gGkMPPrcyMwACmKVaNhvw9KBHHw+e/fIsVZ2GCbL7xp7Mt3X9Nb3jyydEnsygKyAjme6 1Ae0xX4MhNQeLnTMzffQHjADAkV29b/+5OYwYapnqGnLMxrF0zzwmzIfadQtVa6vhZlp SphQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=jJJPk4i5; 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 o12si885900wrb.152.2018.03.13.16.49.20; Tue, 13 Mar 2018 16:49:21 -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=jJJPk4i5; 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 E69A0689E34; Wed, 14 Mar 2018 01:49:07 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qk0-f194.google.com (mail-qk0-f194.google.com [209.85.220.194]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6B299689BF7 for ; Wed, 14 Mar 2018 01:49:05 +0200 (EET) Received: by mail-qk0-f194.google.com with SMTP id s9so1604657qke.12 for ; Tue, 13 Mar 2018 16:49:16 -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=X6GmZrmXcneL5uPKjeQldlUP4SwtZgQxN8pxN78asLg=; b=jJJPk4i5GuMxETaW2h8knk6JtjFojRhm7eAm1bcHMZrKK9INODiwWoRmgJPkzWpY6Y 0kn41Q0el0lLIeK7NLJgmsww/mwtbIwrnSd7djBjhcA9hLWImON1eFH0HgEgZIL/nh4L E4zqmaq4Z3DpWTu+FRPaIeC9O+T+TBBFlkSrhUGjtVTbmHAl5dFDyL244E3hk8AvrNrd 5joTvfon8T4+xYp/XhNxJuKdfvSaI3rcq3U3Ptgb/E5PEIhHrdDRm47Th5Zbv0ZCWf8Y irRQomW04RD8VqzahIJdKzUDLLwBxurI2YSbu+KpniI3Ig2PNDmjeqFA3M/82Mj9FAIu n/mw== 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=X6GmZrmXcneL5uPKjeQldlUP4SwtZgQxN8pxN78asLg=; b=E1hXHEVWyo5c5TTcruUn+eES43GmGQNKAjiffWnrrEW76u//3vDUGPEzUoL9bbMWYi 5lkruK63n9G+zczV9M6VSCUFCNgoqBPHBlXJBzXySsGc71NUSJoNL8ZvTctP9MJwdh7D 5WWg2TZ5qwoo9foEjImC255UpuqZjOy+xPjRVywFozUPAJyQwQQ6Bye28WmLQhWiviwB k8Tht6r5fovHvgzVMMHtAg/goXTyhq5mOx5VR1x81mbwtrcYZOhGamsWpNhSpp8UCZsd hxPMYT6OUjHF8FP3Saduqvs1Jc3ZPJ3yf2daG0Heq5N/DOmCRzr2P1MNIuhwjmNrnvyw k/7g== X-Gm-Message-State: AElRT7HOnP4E+WbvhsUa97PHG+OQeQ/5cNvTn7A2r9Do3TrVZTloYBws /D84roIqf5QSMUc/6iQN97Re3g== X-Received: by 10.55.2.140 with SMTP id v12mr3687527qkg.251.1520984955507; Tue, 13 Mar 2018 16:49:15 -0700 (PDT) Received: from localhost.localdomain ([190.188.171.140]) by smtp.gmail.com with ESMTPSA id l126sm629730qkb.86.2018.03.13.16.49.14 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 13 Mar 2018 16:49:15 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Tue, 13 Mar 2018 20:48:56 -0300 Message-Id: <20180313234856.11108-1-jamrial@gmail.com> X-Mailer: git-send-email 2.16.2 Subject: [FFmpeg-devel] [PATCH] 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 concept 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 | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ libavutil/buffer.h | 42 ++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) diff --git a/libavutil/buffer.c b/libavutil/buffer.c index 8d1aa5fa84..16ce1b82e2 100644 --- a/libavutil/buffer.c +++ b/libavutil/buffer.c @@ -215,6 +215,69 @@ int av_buffer_realloc(AVBufferRef **pbuf, int size) return 0; } +static av_always_inline int buffer_fast_alloc(AVBufferRef **pbuf, int size, int zero_alloc) +{ + AVBufferRef *buf = *pbuf; + AVBuffer *b; + uint8_t *data; + + if (!buf || !av_buffer_is_writable(buf) || buf->data != buf->buffer->data) { + /* Buffer can't be reused, and neither can the entire AVBufferRef. + * Unref the latter and alloc a new one. */ + av_buffer_unref(pbuf); + + buf = av_buffer_alloc(size); + if (!buf) + return AVERROR(ENOMEM); + + if (zero_alloc) + memset(buf->data, 0, size); + + *pbuf = buf; + return 0; + } + b = buf->buffer; + + if (size <= b->size) { + /* Buffer can be reused. Update the size of AVBufferRef but leave the + * AVBuffer untouched. */ + buf->size = FFMAX(0, size); + return 0; + } + + /* Buffer can't be reused, but there's no need to alloc new AVBuffer and + * AVBufferRef structs. Free the existing buffer, allocate a new one, and + * reset AVBuffer and AVBufferRef to default values. */ + b->free(b->opaque, b->data); + b->free = av_buffer_default_free; + b->opaque = NULL; + b->flags = 0; + + data = av_malloc(size); + if (!data) { + av_buffer_unref(pbuf); + return AVERROR(ENOMEM); + } + + if (zero_alloc) + memset(data, 0, size); + + b->data = buf->data = data; + b->size = buf->size = size; + + return 0; +} + +int av_buffer_fast_alloc(AVBufferRef **pbuf, int size) +{ + return buffer_fast_alloc(pbuf, size, 0); +} + +int av_buffer_fast_allocz(AVBufferRef **pbuf, int size) +{ + return buffer_fast_alloc(pbuf, size, 1); +} + 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); + /** * @} */