From: Reimar Döffinger <[email protected]> The same as av_fast_malloc but uses av_mallocz and keeps extra always-0 padding. This does not mean the memory will be 0-initialized after each call, but actually only after each growth of the buffer. However this makes sure that a) all data anywhere in the buffer is always initialized b) the padding is always 0 c) the user does not have to bother with adding the padding themselves
Signed-off-by: Reimar Döffinger <[email protected]> Signed-off-by: Justin Ruggles <[email protected]> --- doc/APIchanges | 4 ++++ libavcodec/avcodec.h | 9 +++++++++ libavcodec/utils.c | 24 +++++++++++++++++++++--- libavcodec/version.h | 2 +- 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/doc/APIchanges b/doc/APIchanges index 7545fa5..8872a22 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -13,6 +13,10 @@ libavutil: 2011-04-18 API changes, most recent first: +2012-xx-xx - xxxxxxx - lavc 54.1.0 + Add a new function, av_fast_padded_malloc(), which zeros memory on resizes + and adds FF_INPUT_BUFFER_PADDING_SIZE, which is zeroed at each call. + 2012-01-30 - xxxxxxx - lavu 51.22.0 - intfloat.h Add a new installed header libavutil/intfloat.h with int/float punning functions. diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index a6bb686..e511234 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -4082,6 +4082,15 @@ void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size); void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size); /** + * Same behaviour av_fast_malloc but the buffer has additional + * FF_INPUT_PADDING_SIZE at the end which will will always be 0. + * + * In addition the whole buffer will initially and after resizes + * be 0-initialized so that no uninitialized data will ever appear. + */ +void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size); + +/** * Copy image src to dst. Wraps av_picture_data_copy() above. */ void av_picture_copy(AVPicture *dst, const AVPicture *src, diff --git a/libavcodec/utils.c b/libavcodec/utils.c index 34a4122..e14ea4f 100644 --- a/libavcodec/utils.c +++ b/libavcodec/utils.c @@ -68,16 +68,34 @@ void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size) return ptr; } -void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size) +static inline int fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc) { void **p = ptr; if (min_size < *size) - return; + return 0; min_size= FFMAX(17*min_size/16 + 32, min_size); av_free(*p); - *p = av_malloc(min_size); + *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) +{ + fast_malloc(ptr, size, min_size, 0); +} + +void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size) +{ + uint8_t **p = ptr; + if (min_size > SIZE_MAX - FF_INPUT_BUFFER_PADDING_SIZE) { + *p = NULL; + *size = 0; + return; + } + if (!fast_malloc(p, size, min_size + FF_INPUT_BUFFER_PADDING_SIZE, 1)) + memset(*p + min_size, 0, FF_INPUT_BUFFER_PADDING_SIZE); } /* encoder management */ diff --git a/libavcodec/version.h b/libavcodec/version.h index c8ed77e..a360ed3 100644 --- a/libavcodec/version.h +++ b/libavcodec/version.h @@ -21,7 +21,7 @@ #define AVCODEC_VERSION_H #define LIBAVCODEC_VERSION_MAJOR 54 -#define LIBAVCODEC_VERSION_MINOR 0 +#define LIBAVCODEC_VERSION_MINOR 1 #define LIBAVCODEC_VERSION_MICRO 0 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ -- 1.7.1 _______________________________________________ libav-devel mailing list [email protected] https://lists.libav.org/mailman/listinfo/libav-devel
