On 2012-01-31 11:40:11 -0500, Justin Ruggles wrote:
> 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

This feels too hacky and sounds if it's just used to silence "depends
on uninitilized value" valgrind warnings.

> 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

just on growth

> +  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

also only after buffer growth

> + * be 0-initialized so that no uninitialized data will ever appear.

the old data which is preserved after a buffer shrink could be also
considered uninitialized data.

> + */
> +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;

I'm not sure if it's a good idea to include FF_INPUT_BUFFER_PADDING_SIZE
in the returned *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
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to