diff options
author | Michael Niedermayer <michaelni@gmx.at> | 2013-11-14 15:04:04 +0100 |
---|---|---|
committer | Michael Niedermayer <michaelni@gmx.at> | 2013-11-14 15:04:04 +0100 |
commit | 7c888ae746da0d9f6a11bab9758c01d641b49d7b (patch) | |
tree | 22862c9f615cb2e636feec9ffd1d5e2a675295ed /libavutil | |
parent | 2925571278bf09702564dfedd5e7731a112dd1f0 (diff) | |
parent | cce3e0a49f0dd030262c28d9c53de0bd2fd909c4 (diff) |
Merge commit 'cce3e0a49f0dd030262c28d9c53de0bd2fd909c4'
* commit 'cce3e0a49f0dd030262c28d9c53de0bd2fd909c4':
Move av_fast_{m,re}alloc from lavc to lavu.
Conflicts:
libavcodec/avcodec.h
libavcodec/utils.c
libavutil/mem.c
libavutil/version.h
Merged-by: Michael Niedermayer <michaelni@gmx.at>
Diffstat (limited to 'libavutil')
-rw-r--r-- | libavutil/mem.c | 39 | ||||
-rw-r--r-- | libavutil/mem.h | 21 | ||||
-rw-r--r-- | libavutil/version.h | 2 |
3 files changed, 61 insertions, 1 deletions
diff --git a/libavutil/mem.c b/libavutil/mem.c index 5aad97aa93..10b0137a7e 100644 --- a/libavutil/mem.c +++ b/libavutil/mem.c @@ -38,6 +38,7 @@ #include "avassert.h" #include "avutil.h" +#include "common.h" #include "intreadwrite.h" #include "mem.h" @@ -463,3 +464,41 @@ void av_memcpy_backptr(uint8_t *dst, int back, int cnt) } } +void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size) +{ + if (min_size < *size) + return ptr; + + min_size = FFMAX(17 * min_size / 16 + 32, min_size); + + ptr = av_realloc(ptr, min_size); + /* we could set this to the unmodified min_size but this is safer + * if the user lost the ptr and uses NULL now + */ + if (!ptr) + min_size = 0; + + *size = min_size; + + return ptr; +} + +static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc) +{ + void **p = ptr; + if (min_size < *size) + return 0; + min_size = FFMAX(17 * min_size / 16 + 32, min_size); + av_free(*p); + *p = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size); + if (!*p) + min_size = 0; + *size = min_size; + return 1; +} + +void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size) +{ + ff_fast_malloc(ptr, size, min_size, 0); +} + diff --git a/libavutil/mem.h b/libavutil/mem.h index 77b7adc373..703ce81936 100644 --- a/libavutil/mem.h +++ b/libavutil/mem.h @@ -336,6 +336,27 @@ void av_max_alloc(size_t max); void av_memcpy_backptr(uint8_t *dst, int back, int cnt); /** + * Reallocate the given block if it is not large enough, otherwise do nothing. + * + * @see av_realloc + */ +void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size); + +/** + * Allocate a buffer, reusing the given one if large enough. + * + * Contrary to av_fast_realloc the current buffer contents might not be + * preserved and on error the old buffer is freed, thus no special + * handling to avoid memleaks is necessary. + * + * @param ptr pointer to pointer to already allocated buffer, overwritten with pointer to new buffer + * @param size size of the buffer *ptr points to + * @param min_size minimum size of *ptr buffer after returning, *ptr will be NULL and + * *size 0 if an error occurred. + */ +void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size); + +/** * @} */ diff --git a/libavutil/version.h b/libavutil/version.h index bd5e7b88e3..05c79fac1a 100644 --- a/libavutil/version.h +++ b/libavutil/version.h @@ -75,7 +75,7 @@ */ #define LIBAVUTIL_VERSION_MAJOR 52 -#define LIBAVUTIL_VERSION_MINOR 52 +#define LIBAVUTIL_VERSION_MINOR 53 #define LIBAVUTIL_VERSION_MICRO 100 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ |