From patchwork Tue Jun 14 14:42:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Tomas_H=C3=A4rdin?= X-Patchwork-Id: 36220 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1114521pzb; Tue, 14 Jun 2022 07:42:18 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uDuw+IiUBoSkAohnqEK3EWwGQ1iN2YPkWrWAbQToPdvmbzrXtWD0MOgy5CK/5OtD7ZlMcH X-Received: by 2002:a17:906:3985:b0:70c:a5fe:d4fb with SMTP id h5-20020a170906398500b0070ca5fed4fbmr4668568eje.127.1655217738064; Tue, 14 Jun 2022 07:42:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655217738; cv=none; d=google.com; s=arc-20160816; b=SeItxSoDMK4+slDobC7of0cfSfWBd5HncHmAxlGG492fjwjZgzoWlgwtkNsjIU7+S9 0C552ukphiwoV27QD3fGqNUloADNBauyFtSFUSYe1JUeJz+xsKSYOmbFUx3Xy9H0YjMe GChzN/l5ZcmY+5Ter3RV504gkoTjvULH0ShByoOOGsitBQ/ojEGFSjxbDwqA3S8i4/2R 1oLnSNeI/vnCY+xwtZyh0wZ7/zy64S+MSuxFwo6ASTHcm3jSQZUtwG+ghLhgB57qoTGa GA8msqg3UZEce4gM6Kjj20x6M7zw5F+4CBSdMtWljdEfe5Tn+MyeGappaQKWwcYfsjgP 1QMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:user-agent:references:in-reply-to:date:to:from :message-id:dkim-signature:dkim-signature:delivered-to; bh=W7LoP4VqyYIokWlS1QmtfV5W7gMLqNtI9NlkPK6mH1M=; b=JCWysgD78+9OZCs8FtkzxCQ6wXOipE/a52ncpojagX3HSL1qHF6DIWvudvS4ceAazK hCw0uaRrn7gmT46NuovrzRJ1F90sdEJ4mzTKPH1hxUarDqa3Xf+0XNEx5otF6pSA6sUn bB7+ubYv7ltJMKEX5g6OF5qmBp8wWyW+05lKiLUfAnjjDIwnetTVLpHnTAPgMnDhzlfo T0kx7lh50YhvoNVt0TkbCJRi+RrGYB+4obzATMVhr+fgG3hEKYRO64Vr9dhXQtKCMoqL vDBw6gET/4sQIbqmTfrs/Mn0bTlDr3qxzNEw0R229xFP26wLKYo51FMrNCKVlwcBmEMt P93A== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@acc.umu.se header.s=mail1 header.b=HJ338Gt1; dkim=neutral (body hash did not verify) header.i=@acc.umu.se header.s=mail1 header.b=HJ338Gt1; 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=NONE dis=NONE) header.from=acc.umu.se Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id gt17-20020a1709072d9100b00704c106f27csi10940398ejc.714.2022.06.14.07.42.17; Tue, 14 Jun 2022 07:42:18 -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=@acc.umu.se header.s=mail1 header.b=HJ338Gt1; dkim=neutral (body hash did not verify) header.i=@acc.umu.se header.s=mail1 header.b=HJ338Gt1; 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=NONE dis=NONE) header.from=acc.umu.se Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 592BE68B682; Tue, 14 Jun 2022 17:42:15 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail.acc.umu.se (mail.acc.umu.se [130.239.18.156]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4F0F668B4D2 for ; Tue, 14 Jun 2022 17:42:08 +0300 (EEST) Received: from localhost (localhost.localdomain [127.0.0.1]) by amavisd-new (Postfix) with ESMTP id D848E44DCB for ; Tue, 14 Jun 2022 16:42:07 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=acc.umu.se; s=mail1; t=1655217727; bh=zKpItBREfg9ZzMZzcMhIJTzxNbA1EI1kjR152sNauL8=; h=Subject:From:To:Date:In-Reply-To:References:From; b=HJ338Gt1/aFSoTmu/YyqwS/x3d+nnSnZ/Tr1ofGQENgaptoCT3Tcj0NLM9wAIr38o yWpj72f5maND3Bl0W2jiDLoZGuz4nyk4JOKvkXR0PpW7OOy/NNPyG3jlJiqMn6Dyhx nv2xIr24BOabJof5mJ+CKMnc9UYMFn+xqWFkOOiE53RJOcWZ6AcYN9cVE75/TJJvVz pHXNBoULi0GLuLfAnhOaTobmqrQB5prVi2oMbmOcgfFLA55+vc/bSUNo/hR6ERYuRG YdSyFiTeUAvZj1i/JM0NFOrMdewSGfkFlU+yzWfkpGDHL5BqWIaMxkKdiAATmnvJwg /5KgMIUksoAIA== Received: from debian.lan (unknown [IPv6:2a00:66c0:a::72c]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: tjoppen) by mail.acc.umu.se (Postfix) with ESMTPSA id 3335644DC6 for ; Tue, 14 Jun 2022 16:42:07 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=acc.umu.se; s=mail1; t=1655217727; bh=zKpItBREfg9ZzMZzcMhIJTzxNbA1EI1kjR152sNauL8=; h=Subject:From:To:Date:In-Reply-To:References:From; b=HJ338Gt1/aFSoTmu/YyqwS/x3d+nnSnZ/Tr1ofGQENgaptoCT3Tcj0NLM9wAIr38o yWpj72f5maND3Bl0W2jiDLoZGuz4nyk4JOKvkXR0PpW7OOy/NNPyG3jlJiqMn6Dyhx nv2xIr24BOabJof5mJ+CKMnc9UYMFn+xqWFkOOiE53RJOcWZ6AcYN9cVE75/TJJvVz pHXNBoULi0GLuLfAnhOaTobmqrQB5prVi2oMbmOcgfFLA55+vc/bSUNo/hR6ERYuRG YdSyFiTeUAvZj1i/JM0NFOrMdewSGfkFlU+yzWfkpGDHL5BqWIaMxkKdiAATmnvJwg /5KgMIUksoAIA== Message-ID: <9fab6eb579616c28441096f3cecf827ff1262c88.camel@acc.umu.se> From: Tomas =?iso-8859-1?q?H=E4rdin?= To: FFmpeg development discussions and patches Date: Tue, 14 Jun 2022 16:42:06 +0200 In-Reply-To: <10ec51ef44325c2de6d5de7b994a9b6c8eb5e3a2.camel@acc.umu.se> References: <10ec51ef44325c2de6d5de7b994a9b6c8eb5e3a2.camel@acc.umu.se> User-Agent: Evolution 3.38.3-1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 06/13] lavu/mem: Add ff_fast_recalloc() 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: OZB8NNU97O9n Left this as an ff_ funtion for now since it's only used by the j2k code /Tomas From 5d36d431ffe4c8ba0f698d0c288ebc16b83f0bbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= Date: Tue, 14 Jun 2022 13:35:18 +0200 Subject: [PATCH 06/13] lavu/mem: Add ff_fast_recalloc() --- libavutil/mem.c | 24 +++++++++++++++++++++ libavutil/mem.h | 55 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/libavutil/mem.c b/libavutil/mem.c index a0c9a42849..7781b715a0 100644 --- a/libavutil/mem.c +++ b/libavutil/mem.c @@ -530,6 +530,30 @@ void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size) return ptr; } +int ff_fast_recalloc(void *ptr, unsigned int *size, size_t nelem, size_t elsize) +{ + void *val; + void *new_ptr; + unsigned int new_size = *size; + size_t product; + int ret; + memcpy(&val, ptr, sizeof(val)); + + if ((ret = av_size_mult(nelem, elsize, &product)) < 0) + return ret; + + if (!(new_ptr = av_fast_realloc(val, &new_size, product))) + return AVERROR(ENOMEM); + + if (new_size > *size) { + memset((uint8_t*)new_ptr + *size, 0, new_size - *size); + *size = new_size; + memcpy(ptr, &new_ptr, sizeof(new_ptr)); + } + + return 0; +} + static inline void fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc) { size_t max_size; diff --git a/libavutil/mem.h b/libavutil/mem.h index d91174196c..74abf3dce2 100644 --- a/libavutil/mem.h +++ b/libavutil/mem.h @@ -380,6 +380,61 @@ int av_reallocp_array(void *ptr, size_t nmemb, size_t size); */ void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size); +/** + * Reallocate the pointed-to buffer if it is not large enough, otherwise do + * nothing. Old data is memcpy()'d to the start of the new buffer. The newly + * allocated space at the end of the buffer is zero-initialized. In other + * words the buffer is expanded with zeroes when necessary. + * + * If the pointed-to buffer is `NULL`, then a new zero-initialized buffer is + * allocated. + * + * If the pointed-to buffer is not large enough, and reallocation fails, + * `AVERROR(ENOMEM)` is returned. + * + * If nelem*elsize is too large then `AVERROR(EINVAL)` is returned. + * + * Contrary to av_fast_malloc(), *ptr and *size are not touched in case of + * error, to allow for proper cleanup. + * + * *ptr is not guaranteed to be an exact multiple of elsize bytes. + * + * This function is intended for use with arrays of structures that contain + * pointers that are allowed to grow and typically don't shrink. + * + * A typical use pattern follows: + * + * @code{.c} + * int foo_work(SomeContext *s) { + * if (ff_fast_recalloc(&s->foo, &s->foo_size, s->nfoo, sizeof(Foo))) + * return AVERROR(ENOMEM); + * for (x = 0; x < s->nfoo; x++) + * do stuff with s->foo[x] + * return 0; + * } + * + * void foo_close(SomeContext *s) { + * // note the use of s->foo_size, not s->nfoo + * for (x = 0; x < s->foo_size/sizeof(Foo); x++) + * av_freep(&s->foo[x].bar); + * av_freep(&s->foo); + * } + * @endcode + * + * @param[in,out] ptr Pointer to pointer to an already allocated buffer. + * `*ptr` will be overwritten with pointer to new + * buffer on success and will be left alone on failure + * @param[in,out] size Pointer to the size of buffer `*ptr`. `*size` is + * updated to the new allocated size and will be left + * along on failure. + * @param[in] nelem Number of desired elements in *ptr + * @param[in] elsize Size of each element in *ptr + * @return Zero on success, <0 on error. + * @see av_fast_realloc() + * @see av_fast_malloc() + */ +int ff_fast_recalloc(void *ptr, unsigned int *size, size_t nelem, size_t elsize); + /** * Allocate a buffer, reusing the given one if large enough. * -- 2.30.2