From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 9FAC843080 for ; Thu, 13 Jan 2022 17:04:44 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E313E68B407; Thu, 13 Jan 2022 19:04:41 +0200 (EET) Received: from mail-qt1-f182.google.com (mail-qt1-f182.google.com [209.85.160.182]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7A05768B1C5 for ; Thu, 13 Jan 2022 19:04:35 +0200 (EET) Received: by mail-qt1-f182.google.com with SMTP id y17so7588955qtx.9 for ; Thu, 13 Jan 2022 09:04:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :references:from:in-reply-to:content-transfer-encoding; bh=cTUMvrPJuWs62aJmU96njnIs/UoxdgfN4cdLF1fMv7E=; b=Q8xEjCg8qmDAdWwkTqYIxgneNF5y1AiSQjnI93Iy5K4GUDbUU8aRUYm6aIg+vqRpJm WtmPgeRnrRyW/oq7npmHLo4f0jdwzFORdJFywg2auKCqmFk1x91hc8ca+AS+0XfYqh6I 46+GvJDUK9OjQx3yFRYGouMQSU2N5sy/KV9aORS8CUdJGJQW4zJwz6fCJxcKGyi73SEf VP6lzYrHD4WZBvBRqX29RdFPhPQ7U0yAg609Btf7cxFVgRzQ0jWOqRXGn5bCQ4NIZAjF moCdZCFkJnWdQGyCDKHngk9eVs3ZaJfoXhEdnJEjaLx8J7gyc20AM/CTeG0FgpvRfcY1 g87g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=cTUMvrPJuWs62aJmU96njnIs/UoxdgfN4cdLF1fMv7E=; b=csEsd329Q7hvHXfGfF+X1ZU/YBDNb9apeKicL0U6H3U6RpwLBS5NASR/w0Tc82A/DY QTZaHydrFjdVXX5Czv3pbEDRp4sA/fEhA/9uvVdcwqsN3fhcA7fTeiI9LGsvi9T5+oTj MncDYDB3GiKv7QBbHO9M+SReggicEgvmmW3gZJrIXkGPpSaq/PK4YTink64AufALseQZ mFbR4H+Us0TteKQdaeUU4hlOeUR4Y2oqg9w3/1yqBobVA79uAYbtXpYGXk2kSqsLXDND oHp6eaGvq1gSqhuM0mBr+MEXHDJWfMJfHrOK9gOD2/x06wGU4Z7E/ELu0Z0ep36GJ8cG rcjg== X-Gm-Message-State: AOAM532JziVSGp6KsGbFD1TvFlY5AJpSZ4sshrKVbLywunq80rVVFMDU SYT+K+sPTsEmT+Pi5YFNcwipmaNlbfo= X-Google-Smtp-Source: ABdhPJw8I9ozRLnPqtCqiLFUmdSyRH4EQIsP74uJbTrcLrz3L8n0e6t+kVccH4v5bDgEyCluqFPcgQ== X-Received: by 2002:a05:622a:53:: with SMTP id y19mr4263756qtw.96.1642093473894; Thu, 13 Jan 2022 09:04:33 -0800 (PST) Received: from [192.168.0.13] ([186.136.131.95]) by smtp.gmail.com with ESMTPSA id q20sm1835188qtx.63.2022.01.13.09.04.32 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 13 Jan 2022 09:04:33 -0800 (PST) Message-ID: <0f4152e3-041d-8758-b7eb-2289639905de@gmail.com> Date: Thu, 13 Jan 2022 14:04:31 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Content-Language: en-US To: ffmpeg-devel@ffmpeg.org References: <20220111204610.14262-1-anton@khirnov.net> <20220111204610.14262-5-anton@khirnov.net> From: James Almer In-Reply-To: <20220111204610.14262-5-anton@khirnov.net> Subject: Re: [FFmpeg-devel] [PATCH 05/35] lavu/fifo: add a new FIFO grow function 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 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: On 1/11/2022 5:45 PM, Anton Khirnov wrote: > Consistently use size_t for sizes. > > Unlike av_fifo_grow(), which addds to the currently used size, this > function adds to the allocated size. > > No new function is provided for a generic realloc, since the current code > only supports increasing the FIFO size. > --- > doc/APIchanges | 3 ++- > libavutil/fifo.c | 61 +++++++++++++++++++++++++++--------------------- > libavutil/fifo.h | 14 +++++++++++ > 3 files changed, 50 insertions(+), 28 deletions(-) > > diff --git a/doc/APIchanges b/doc/APIchanges > index 9400c5147a..98eae55719 100644 > --- a/doc/APIchanges > +++ b/doc/APIchanges > @@ -18,7 +18,8 @@ API changes, most recent first: > Add av_fifo_alloc2(), which allows setting a FIFO element size. > Operations on FIFOs created with this function on these elements > rather than bytes. > - Add av_fifo_elem_size(), av_fifo_can_read(), av_fifo_can_write(). > + Add av_fifo_elem_size(), av_fifo_can_read(), av_fifo_can_write(), > + av_fifo_grow2(). > > 2022-01-xx - xxxxxxxxxx - lavu fifo.h > Access to all AVFifoBuffer members is deprecated. The struct will > diff --git a/libavutil/fifo.c b/libavutil/fifo.c > index 8cde2c20e1..28d6d1b2e6 100644 > --- a/libavutil/fifo.c > +++ b/libavutil/fifo.c > @@ -150,41 +150,48 @@ int av_fifo_space(const AVFifoBuffer *f) > return av_fifo_can_write(f); > } > > -int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size) > +int av_fifo_grow2(AVFifoBuffer *f, size_t inc) > { > FifoBuffer *fb = (FifoBuffer*)f; > + uint8_t *tmp; > > - if (new_size > FIFO_SIZE_MAX) > + if (inc > FIFO_SIZE_MAX - fb->nb_elems) > return AVERROR(EINVAL); > > - if (fb->nb_elems < new_size) { > - uint8_t *tmp; > - > - tmp = av_realloc_array(f->buffer, new_size, fb->elem_size); > - if (!tmp) > - return AVERROR(ENOMEM); > - > - // move the data from the beginning of the ring buffer > - // to the newly allocated space > - if (fb->offset_w <= fb->offset_r && !fb->is_empty) { > - const size_t copy = FFMIN(new_size - fb->nb_elems, fb->offset_w); > - memcpy(tmp + fb->nb_elems * fb->elem_size, tmp, copy * fb->elem_size); > - if (copy < fb->offset_w) { > - memmove(tmp, tmp + copy * fb->elem_size, > - (fb->offset_w - copy) * fb->elem_size); > - fb->offset_w -= copy; > - } else > - fb->offset_w = fb->nb_elems + copy; > - } > + tmp = av_realloc_array(f->buffer, fb->nb_elems + inc, fb->elem_size); This sounds like a good opportunity to introduce av_fast_realloc_array() in mem.h, with FifoBuffer being a private struct where you can safely store the required buffer size field. av_realloc_array() can be slow in an auto-grow scenario if there's a lot of buffering before stuff starts being drained. > + if (!tmp) > + return AVERROR(ENOMEM); > + > + // move the data from the beginning of the ring buffer > + // to the newly allocated space > + if (fb->offset_w <= fb->offset_r && !fb->is_empty) { > + const size_t copy = FFMIN(inc, fb->offset_w); > + memcpy(tmp + fb->nb_elems * fb->elem_size, tmp, copy * fb->elem_size); > + if (copy < fb->offset_w) { > + memmove(tmp, tmp + copy * fb->elem_size, > + (fb->offset_w - copy) * fb->elem_size); > + fb->offset_w -= copy; > + } else > + fb->offset_w = fb->nb_elems + copy; > + } > + > + f->buffer = tmp; > + fb->nb_elems += inc; > > - f->buffer = tmp; > #if FF_API_FIFO_PUBLIC > - f->end = f->buffer + new_size; > - f->rptr = f->buffer + fb->offset_r * fb->elem_size; > - f->wptr = f->buffer + fb->offset_w * fb->elem_size; > + f->end = f->buffer + fb->nb_elems * fb->elem_size; > + f->rptr = f->buffer + fb->offset_r * fb->elem_size; > + f->wptr = f->buffer + fb->offset_w * fb->elem_size; > #endif > - fb->nb_elems = new_size; > - } > + > + return 0; > +} > + > +int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size) > +{ > + FifoBuffer *fb = (FifoBuffer*)f; > + if (fb->nb_elems < new_size) > + return av_fifo_grow2(f, new_size - fb->nb_elems); > return 0; > } > > diff --git a/libavutil/fifo.h b/libavutil/fifo.h > index 9e78082b3b..375d0d133b 100644 > --- a/libavutil/fifo.h > +++ b/libavutil/fifo.h > @@ -129,6 +129,20 @@ size_t av_fifo_can_read(const AVFifoBuffer *f); > */ > size_t av_fifo_can_write(const AVFifoBuffer *f); > > +/** > + * Enlarge an AVFifoBuffer. > + * > + * On success, the FIFO will be large enough to hold exactly > + * inc + av_fifo_can_read() + av_fifo_can_write() > + * elements. In case of failure, the old FIFO is kept unchanged. > + * > + * @param f AVFifoBuffer to resize > + * @param inc number of elements to allocate for, in addition to the current > + * allocated size > + * @return a non-negative number on success, a negative error code on failure > + */ > +int av_fifo_grow2(AVFifoBuffer *f, size_t inc); > + > /** > * Feed data at specific position from an AVFifoBuffer to a user-supplied callback. > * Similar as av_fifo_gereric_read but without discarding data. _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".