From: "Tomas Härdin" <tjoppen@acc.umu.se>
To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Subject: Re: [FFmpeg-devel] [PATCH 3/8] avutil/mem: Add av_fast_realloc_array()
Date: Tue, 27 Sep 2022 17:23:41 +0200
Message-ID: <dfd0b08de363b552e6591682097ad4c3bddf8f40.camel@acc.umu.se> (raw)
In-Reply-To: <3324e761a5215915b99679c51a97c2f2ec5a2b05.camel@acc.umu.se>
[-- Attachment #1: Type: text/plain, Size: 2350 bytes --]
mån 2022-09-26 klockan 16:24 +0200 skrev Tomas Härdin:
> mån 2022-09-26 klockan 14:25 +0200 skrev Andreas Rheinhardt:
> > Anton Khirnov:
> > > Quoting Andreas Rheinhardt (2022-07-14 14:51:07)
> > > > Anton Khirnov:
> > > > > Quoting Andreas Rheinhardt (2022-07-12 16:12:16)
> > > > > > Anton really dislikes the av_fast_* naming and instead
> > > > > > wants
> > > > > > this to be
> > > > > > called av_realloc_array_reuse(). I don't care either way.
> > > > > > Any
> > > > > > more
> > > > > > opinions on this (or on the patch itself)?
> > > > >
> > > > > If people dislike _reuse(), I am open to other reasonable
> > > > > suggestions.
> > > > > This 'fast' naming sucks because
> > > > > - it tells you nothing about how this function is "fast"
> > > > > - it is added at the beginning rather than the end, which is
> > > > > against standard namespacing conventions
> > > > >
> > > >
> > > > Isn't reusing the basic modus operandi for a reallocation
> > > > function? So
> > > > your suggested name doesn't seem to fit either.
> > >
> > > Ordinary realloc just keeps the data, I wouldn't call that
> > > "reuse"
> > > since
> > > it will often be a copy. This "fast" realloc OTOH reuses the
> > > actual
> > > buffer, same as all the other "fast" mem.h functions.
> > >
> > > But feel free to suggest another naming pattern if you can think
> > > of
> > > one.
> > >
> >
> > I see two differences between this function and ordinary realloc:
> > It
> > never shrinks the buffer and it overallocates. These two properties
> > make
> > it more likely that these functions can avoid copies more often
> > than
> > plain realloc (but in contrast to realloc, we can not grow the
> > buffer
> > in
> > case there is free space after it), but it is nevertheless the same
> > as
> > realloc.
> >
> > But I don't really care that much about the name and will therefore
> > use
> > your name as I can't come up with anything better.
> > (Of course, I am still open to alternative suggestions.)
> >
> > - Andreas
>
> So this means av_realloc_array_reuse()? Eh, it works. I will add a
> function that also zeroes the newly allocated space, what should we
> call that? av_realloc_array_reusez()?
> av_realloc_array_reuse_zerofill()?
Here's a draft patch that calls it av_reallocz_array_reuse(). Needs a
minor version bump of course
/Tomas
[-- Attachment #2: 0001-avutil-mem-Add-av_reallocz_array_reuse.patch --]
[-- Type: text/x-patch, Size: 3735 bytes --]
From 75e0ca71f782b2b18242815b1fbb079a7bfdb5ba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= <git@haerdin.se>
Date: Tue, 27 Sep 2022 15:37:41 +0200
Subject: [PATCH] avutil/mem: Add av_reallocz_array_reuse()
Like av_realloc_array_reuse() but zeroes the newly allocated memory.
---
libavutil/mem.c | 24 ++++++++++++++++++++++++
libavutil/mem.h | 31 +++++++++++++++++++++++++++++++
2 files changed, 55 insertions(+)
diff --git a/libavutil/mem.c b/libavutil/mem.c
index c315a38672..3dd34fcf1b 100644
--- a/libavutil/mem.c
+++ b/libavutil/mem.c
@@ -565,6 +565,30 @@ int av_realloc_array_reuse(void *ptr, size_t *nb_allocated,
return 0;
}
+int av_reallocz_array_reuse(void *ptr, size_t *nb_allocated,
+ size_t min_nb, size_t max_nb, size_t elsize)
+{
+ size_t nb_initial, nb_allocated2 = *nb_allocated;
+ void *array;
+ int ret;
+
+ // check if *ptr is NULL. nb_allocated is not guaranteed to have an initial value
+ memcpy(&array, ptr, sizeof(array));
+ nb_initial = array ? *nb_allocated : 0;
+
+ if ((ret = av_realloc_array_reuse(ptr, &nb_allocated2, min_nb, max_nb, elsize)))
+ return ret;
+
+ if (nb_allocated2 > nb_initial) {
+ // new space allocated - zero it!
+ memcpy(&array, ptr, sizeof(array));
+ memset((char*)array + nb_initial*elsize, 0, (nb_allocated2 - nb_initial)*elsize);
+ *nb_allocated = nb_allocated2;
+ }
+
+ return ret;
+}
+
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 3638329108..cfcec37e73 100644
--- a/libavutil/mem.h
+++ b/libavutil/mem.h
@@ -410,6 +410,37 @@ void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size);
int av_realloc_array_reuse(void *ptr, size_t *nb_allocated,
size_t min_nb, size_t max_nb, size_t elsize);
+/**
+ * Reallocate the given array if it is not large enough and fill the newly
+ * allocated space with zeroes, otherwise do nothing.
+ * In other words, like av_realloc_array_reuse() but zeroes the new space.
+ *
+ * If `ptr` points to `NULL`, then a new zeroed array is allocated.
+ *
+ * @param[in,out] ptr Pointer to `NULL` or pointer to an already
+ * allocated array. `*ptr` will be set to point
+ * to the new array on success.
+ * @param[in,out] nb_allocated Pointer to the number of elements of the array
+ * `*ptr`. `*nb_allocated` is updated to the new
+ * number of allocated elements.
+ * @param[in] min_nb Desired minimal number of elements in array `*ptr`
+ * @param[in] max_nb Maximal number of elements to allocate.
+ * @param[in] elsize Size of a single element of the array.
+ * Must not be zero.
+ * @return 0 on success, < 0 on failure. On failure, `*ptr` is not freed and
+ * `*ptr` as well as `*nb_allocated` are unchanged.
+ * @note `max_nb` can be used to limit allocations and make this function
+ * usable with counters of types other than size_t. It can also be used
+ * to avoid overflow checks in callers: E.g. setting it to `UINT_MAX - 1`
+ * means that incrementing an unsigned int in steps of one need not be
+ * checked for overflow.
+ * @see av_fast_realloc()
+ * @see av_realloc()
+ * @see av_fast_malloc()
+ */
+int av_reallocz_array_reuse(void *ptr, size_t *nb_allocated,
+ size_t min_nb, size_t max_nb, size_t elsize);
+
/**
* Allocate a buffer, reusing the given one if large enough.
*
--
2.30.2
[-- Attachment #3: Type: text/plain, Size: 251 bytes --]
_______________________________________________
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".
next prev parent reply other threads:[~2022-09-27 15:23 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-05 20:09 [FFmpeg-devel] [PATCH 1/8] avutil/mem: Handle fast allocations near UINT_MAX properly Andreas Rheinhardt
2022-07-05 20:26 ` [FFmpeg-devel] [PATCH 2/8] avformat/flvenc: Add deinit function Andreas Rheinhardt
2022-07-06 2:28 ` Steven Liu
2022-07-05 20:26 ` [FFmpeg-devel] [PATCH 3/8] avutil/mem: Add av_fast_realloc_array() Andreas Rheinhardt
2022-07-06 14:40 ` Tomas Härdin
2022-07-06 14:46 ` Andreas Rheinhardt
2022-07-06 14:54 ` Tomas Härdin
2022-07-12 14:12 ` Andreas Rheinhardt
2022-07-14 8:14 ` Anton Khirnov
2022-07-14 12:51 ` Andreas Rheinhardt
2022-07-17 8:30 ` Anton Khirnov
2022-09-26 12:25 ` Andreas Rheinhardt
2022-09-26 14:21 ` Andreas Rheinhardt
2022-09-26 14:24 ` Tomas Härdin
2022-09-27 15:23 ` Tomas Härdin [this message]
2022-09-28 9:35 ` Tomas Härdin
2022-09-28 11:06 ` Andreas Rheinhardt
2022-09-28 11:41 ` Tomas Härdin
2022-07-21 21:23 ` Tomas Härdin
2022-08-17 15:29 ` Anton Khirnov
2022-07-05 20:26 ` [FFmpeg-devel] [PATCH 4/8] avformat/flvenc: Use array instead of linked list for index Andreas Rheinhardt
2022-07-06 14:58 ` Tomas Härdin
2022-07-06 15:03 ` Andreas Rheinhardt
2022-07-05 20:26 ` [FFmpeg-devel] [PATCH 5/8] avformat/matroskaenc: Use av_fast_realloc_array for index entries Andreas Rheinhardt
2022-07-06 15:03 ` Tomas Härdin
2022-07-06 15:10 ` Andreas Rheinhardt
2022-07-06 15:21 ` Tomas Härdin
2022-07-05 20:26 ` [FFmpeg-devel] [PATCH 6/8] avcodec/movtextenc: Use av_fast_realloc_array Andreas Rheinhardt
2022-07-06 15:06 ` Tomas Härdin
2022-07-05 20:26 ` [FFmpeg-devel] [PATCH 7/8] avutil/fifo: Simplify growing FIFO Andreas Rheinhardt
2022-07-05 20:26 ` [FFmpeg-devel] [PATCH 8/8] avutil/fifo: Grow FIFO faster when growing automatically Andreas Rheinhardt
2022-07-06 13:02 ` [FFmpeg-devel] [PATCH 1/8] avutil/mem: Handle fast allocations near UINT_MAX properly Anton Khirnov
2022-07-06 13:08 ` Andreas Rheinhardt
2022-07-06 13:17 ` Anton Khirnov
2022-07-06 14:24 ` Tomas Härdin
2022-07-06 14:40 ` Andreas Rheinhardt
2022-08-17 14:31 ` Tomas Härdin
2022-09-26 11:50 ` Tomas Härdin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=dfd0b08de363b552e6591682097ad4c3bddf8f40.camel@acc.umu.se \
--to=tjoppen@acc.umu.se \
--cc=ffmpeg-devel@ffmpeg.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git