diff options
Diffstat (limited to 'libavutil')
-rw-r--r-- | libavutil/mem.c | 26 | ||||
-rw-r--r-- | libavutil/mem.h | 26 | ||||
-rw-r--r-- | libavutil/version.h | 2 |
3 files changed, 53 insertions, 1 deletions
diff --git a/libavutil/mem.c b/libavutil/mem.c index 66502eb467..1f2b0c3dd3 100644 --- a/libavutil/mem.c +++ b/libavutil/mem.c @@ -180,6 +180,32 @@ void *av_realloc_f(void *ptr, size_t nelem, size_t elsize) return r; } +void *av_realloc_array(void *ptr, size_t nmemb, size_t size) +{ + if (size <= 0 || nmemb >= INT_MAX / size) + return NULL; + return av_realloc(ptr, nmemb * size); +} + +int av_reallocp_array(void *ptr, size_t nmemb, size_t size) +{ + void **ptrptr = ptr; + void *ret; + if (size <= 0 || nmemb >= INT_MAX / size) + return AVERROR(ENOMEM); + if (nmemb <= 0) { + av_freep(ptr); + return 0; + } + ret = av_realloc(*ptrptr, nmemb * size); + if (!ret) { + av_freep(ptr); + return AVERROR(ENOMEM); + } + *ptrptr = ret; + return 0; +} + void av_free(void *ptr) { #if CONFIG_MEMALIGN_HACK diff --git a/libavutil/mem.h b/libavutil/mem.h index a3294690cf..fb23a69094 100644 --- a/libavutil/mem.h +++ b/libavutil/mem.h @@ -123,6 +123,32 @@ void *av_realloc(void *ptr, size_t size) av_alloc_size(2); void *av_realloc_f(void *ptr, size_t nelem, size_t elsize); /** + * Allocate or reallocate an array. + * If ptr is NULL and nmemb > 0, allocate a new block. If + * nmemb is zero, free the memory block pointed to by ptr. + * @param ptr Pointer to a memory block already allocated with + * av_malloc(z)() or av_realloc() or NULL. + * @param nmemb Number of elements + * @param size Size of the single element + * @return Pointer to a newly reallocated block or NULL if the block + * cannot be reallocated or the function is used to free the memory block. + */ +av_alloc_size(2, 3) void *av_realloc_array(void *ptr, size_t nmemb, size_t size); + +/** + * Allocate or reallocate an array. + * If *ptr is NULL and nmemb > 0, allocate a new block. If + * nmemb is zero, free the memory block pointed to by ptr. + * @param ptr Pointer to a pointer to a memory block already allocated + * with av_malloc(z)() or av_realloc(), or pointer to a pointer to NULL. + * The pointer is updated on success, or freed on failure. + * @param nmemb Number of elements + * @param size Size of the single element + * @return Zero on success, an AVERROR error code on failure. + */ +av_alloc_size(2, 3) int av_reallocp_array(void *ptr, size_t nmemb, size_t size); + +/** * Free a memory block which has been allocated with av_malloc(z)() or * av_realloc(). * @param ptr Pointer to the memory block which should be freed. diff --git a/libavutil/version.h b/libavutil/version.h index e234296b41..9941229bbc 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -75,7 +75,7 @@ */ #define LIBAVUTIL_VERSION_MAJOR 52 -#define LIBAVUTIL_VERSION_MINOR 34 +#define LIBAVUTIL_VERSION_MINOR 35 #define LIBAVUTIL_VERSION_MICRO 100 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ |