From 75e0ca71f782b2b18242815b1fbb079a7bfdb5ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20H=C3=A4rdin?= 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