From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> To: ffmpeg-devel@ffmpeg.org Cc: Andreas Rheinhardt <andres.rheinhardt@outlook.com>, Andreas Rheinhardt <andreas.rheinhardt@outlook.com> Subject: [FFmpeg-devel] [PATCH 3/8] avutil/mem: Add av_fast_realloc_array() Date: Tue, 5 Jul 2022 22:26:45 +0200 Message-ID: <DB6PR0101MB22144C833D5F308F85DCE5588F819@DB6PR0101MB2214.eurprd01.prod.exchangelabs.com> (raw) In-Reply-To: <DB6PR0101MB2214034DCAA45C10037497958F819@DB6PR0101MB2214.eurprd01.prod.exchangelabs.com> From: Andreas Rheinhardt <andres.rheinhardt@outlook.com> This is an array-equivalent of av_fast_realloc(). Its advantages compared to using av_fast_realloc() for allocating arrays are as follows: a) It performs its own overflow checks for the multiplication that is implicit in array allocations. (And it only needs to perform these checks (as well as the multiplication itself) in case the array needs to be reallocated.) b) It allows to limit the number of elements to an upper bound given by the caller. This allows to restrict the number of allocated elements to fit into an int and therefore makes this function usable with counters of this type. It can also be used to avoid overflow checks in the caller: E.g. setting it to UINT_MAX - 1 elements makes it safe to increase the desired number of elements in steps of one. And it avoids overallocations in situations where one already has an upper bound. c) av_fast_realloc_array() will always allocate in multiples of array elements; no memory is wasted with partial elements. d) By returning an int, av_fast_realloc_array() can distinguish between ordinary allocation failures (meriting AVERROR(ENOMEM)) and failures because of allocation limits (by returning AVERROR(ERANGE)). e) It is no longer possible for the user to accidentally lose the pointer by using ptr = av_fast_realloc(ptr, ...). Because of e) there is no need to set the number of allocated elements to zero on failure. av_fast_realloc() usually allocates size + size / 16 + 32 bytes if size bytes are desired and if the already existing buffer isn't big enough. av_fast_realloc_array() instead allocates nb + (nb + 14) / 16. Rounding up is done in order not to reallocate in steps of one if the current number is < 16; adding 14 instead of 15 has the effect of only allocating one element if one element is desired. This is done with an eye towards applications where arrays might commonly only contain one element (as happens with the Matroska CueTrackPositions). Which of the two functions allocates faster depends upon the size of the elements. E.g. if the elements have a size of 32B and the desired size is incremented in steps of one, allocations happen at 1, 3, 5, 7, 9, 11, 13, 15, 17, 20, 23, 26 ... for av_fast_realloc(), 1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 21, 24 ... for av_fast_realloc_array(). For element sizes of 96B, the numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 21, 23, 25, 27, 30 ... for av_fast_realloc() whereas the pattern for av_fast_realloc_array() is unchanged. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> --- This patch (and some of the other patches of this patchset) are mostly the same as the one in these threads: https://ffmpeg.org/pipermail/ffmpeg-devel/2019-December/254836.html https://ffmpeg.org/pipermail/ffmpeg-devel/2020-January/255182.html doc/APIchanges | 3 +++ libavutil/mem.c | 33 +++++++++++++++++++++++++++++++++ libavutil/mem.h | 30 ++++++++++++++++++++++++++++++ libavutil/version.h | 2 +- 4 files changed, 67 insertions(+), 1 deletion(-) diff --git a/doc/APIchanges b/doc/APIchanges index 20b944933a..f633ae6fee 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -14,6 +14,9 @@ libavutil: 2021-04-27 API changes, most recent first: +2022-07-05 - xxxxxxxxxx - lavu 57.28.100 - mem.h + Add av_fast_realloc_array() to simplify reallocating of arrays. + 2022-06-12 - xxxxxxxxxx - lavf 59.25.100 - avio.h Add avio_vprintf(), similar to avio_printf() but allow to use it from within a function taking a variable argument list as input. diff --git a/libavutil/mem.c b/libavutil/mem.c index 18aff5291f..6e3942ae63 100644 --- a/libavutil/mem.c +++ b/libavutil/mem.c @@ -532,6 +532,39 @@ void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size) return ptr; } +int av_fast_realloc_array(void *ptr, size_t *nb_allocated, + size_t min_nb, size_t max_nb, size_t elsize) +{ + void *array; + size_t nb, max_alloc_size_bytes; + + if (min_nb <= *nb_allocated) + return 0; + + max_alloc_size_bytes = atomic_load_explicit(&max_alloc_size, memory_order_relaxed); + max_nb = FFMIN(max_nb, max_alloc_size_bytes / elsize); + + if (min_nb > max_nb) + return AVERROR(ERANGE); + + nb = min_nb + (min_nb + 14) / 16; + + /* If min_nb is so big that the above calculation overflowed, + * just allocate as much as we are allowed to. */ + nb = nb < min_nb ? max_nb : FFMIN(nb, max_nb); + + memcpy(&array, ptr, sizeof(array)); + + array = av_realloc(array, nb * elsize); + if (!array) + return AVERROR(ENOMEM); + + memcpy(ptr, &array, sizeof(array)); + *nb_allocated = nb; + + 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..f040de08fc 100644 --- a/libavutil/mem.h +++ b/libavutil/mem.h @@ -375,11 +375,41 @@ int av_reallocp_array(void *ptr, size_t nmemb, size_t size); * @return `ptr` if the buffer is large enough, a pointer to newly reallocated * buffer if the buffer was not large enough, or `NULL` in case of * error + * @see av_fast_realloc_array() * @see av_realloc() * @see av_fast_malloc() */ void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size); +/** + * Reallocate the given array if it is not large enough, otherwise do nothing. + * + * If `ptr` points to `NULL`, then a new uninitialized 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_fast_realloc_array(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. * diff --git a/libavutil/version.h b/libavutil/version.h index 2e9e02dda8..87178e9e9a 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -79,7 +79,7 @@ */ #define LIBAVUTIL_VERSION_MAJOR 57 -#define LIBAVUTIL_VERSION_MINOR 27 +#define LIBAVUTIL_VERSION_MINOR 28 #define LIBAVUTIL_VERSION_MICRO 100 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ -- 2.34.1 _______________________________________________ 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-07-05 20:27 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 ` Andreas Rheinhardt [this message] 2022-07-06 14:40 ` [FFmpeg-devel] [PATCH 3/8] avutil/mem: Add av_fast_realloc_array() 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 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=DB6PR0101MB22144C833D5F308F85DCE5588F819@DB6PR0101MB2214.eurprd01.prod.exchangelabs.com \ --to=andreas.rheinhardt@outlook.com \ --cc=andres.rheinhardt@outlook.com \ --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