[vlc-devel] [PATCH] core: add aligned_free to match the aligned_alloc used in the code

Rémi Denis-Courmont remi at remlab.net
Tue Jun 20 18:37:08 CEST 2017


Le tiistaina 20. kesäkuuta 2017, 15.00.39 EEST Steve Lhomme a écrit :
> On Windows if you allocate aligned memory you need to free it with an
> aligned version of free.
> 
> This is similar to the old vlc_memalign() + vlc_free()
> 
> --
> replaces https://patches.videolan.org/patch/16993/
> - fix align_free call instead of aligned_free
> replaces https://patches.videolan.org/patch/16994/
> - inline the call to aligned_free() in vlc_fixups.h
> replaces https://patches.videolan.org/patch/16996/
> - don't test in configure.ac since aligned_free doesn't exist
> ---
>  compat/aligned_alloc.c         | 16 ++++++++++------
>  include/vlc_fixups.h           | 16 ++++++++++++++++
>  modules/video_chroma/copy.c    |  2 +-
>  modules/video_filter/gradfun.c |  2 +-
>  modules/video_output/evas.c    |  2 +-
>  src/misc/picture.c             |  2 +-
>  src/misc/picture_pool.c        |  2 +-
>  7 files changed, 31 insertions(+), 11 deletions(-)
> 
> diff --git a/compat/aligned_alloc.c b/compat/aligned_alloc.c
> index 9ea352b0c5..28fad07f79 100644
> --- a/compat/aligned_alloc.c
> +++ b/compat/aligned_alloc.c
> @@ -52,14 +52,18 @@ void *aligned_alloc(size_t align, size_t size)

Might as well remove the stray !WIN32 around malloc.h then.

>      }
>      return ptr;
> 
> -#elif !defined (_WIN32)
> +#elif defined (_WIN32)
> +#ifdef __MINGW32__
> +    return __mingw_aligned_malloc(size, align);
> +#elif defined(_MSC_VER)
> +    return _aligned_malloc(size, align);
> +#endif
> +    if (size > 0)
> +        errno = ENOMEM;
> +    return NULL;
> +#else
> 
>     return memalign(align, size);
> 
> -#else
> -
> -   if (size > 0)
> -       errno = ENOMEM;
> -   return NULL;
>  #endif
>  }
> diff --git a/include/vlc_fixups.h b/include/vlc_fixups.h
> index 44b8426f2c..b9a9146ac9 100644
> --- a/include/vlc_fixups.h
> +++ b/include/vlc_fixups.h
> @@ -306,6 +306,22 @@ int unsetenv (const char *);
>  void *aligned_alloc(size_t, size_t);
>  #endif
> 
> +#if defined (_WIN32)
> +#include <malloc.h>
> +static inline void aligned_free(void *ptr)
> +{
> +#ifdef __MINGW32__
> +    return __mingw_aligned_free(ptr);
> +#elif defined(_MSC_VER)
> +    return _aligned_free(ptr);
> +#else
> +    free(ptr);

This presumably dead code would not compile, since <stdlib.h> was not *yet* 
included. Probably better use a macro in all cases.

> +#endif
> +}
> +#else
> +#define aligned_free(x)  free(x)
> +#endif
> +
>  #if defined(__native_client__) && defined(__cplusplus)
>  # define HAVE_USELOCALE
>  #endif
> diff --git a/modules/video_chroma/copy.c b/modules/video_chroma/copy.c
> index 4bc2e91c45..6c20c132b0 100644
> --- a/modules/video_chroma/copy.c
> +++ b/modules/video_chroma/copy.c
> @@ -49,7 +49,7 @@ int CopyInitCache(copy_cache_t *cache, unsigned width)
>  void CopyCleanCache(copy_cache_t *cache)
>  {
>  #ifdef CAN_COMPILE_SSE2
> -    free(cache->buffer);
> +    aligned_free(cache->buffer);
>      cache->buffer = NULL;
>      cache->size   = 0;
>  #else
> diff --git a/modules/video_filter/gradfun.c b/modules/video_filter/gradfun.c
> index 317c604f7b..8ebb95d0aa 100644
> --- a/modules/video_filter/gradfun.c
> +++ b/modules/video_filter/gradfun.c
> @@ -168,7 +168,7 @@ static void Close(vlc_object_t *object)
> 
>      var_DelCallback(filter, CFG_PREFIX "radius",   Callback, NULL);
>      var_DelCallback(filter, CFG_PREFIX "strength", Callback, NULL);
> -    free(sys->cfg.buf);
> +    aligned_free(sys->cfg.buf);
>      vlc_mutex_destroy(&sys->lock);
>      free(sys);
>  }
> diff --git a/modules/video_output/evas.c b/modules/video_output/evas.c
> index 348a681de3..880c9e413a 100644
> --- a/modules/video_output/evas.c
> +++ b/modules/video_output/evas.c
> @@ -951,7 +951,7 @@ EvasImageBuffersFree( vout_display_t *vd )
>      vout_display_sys_t *sys = vd->sys;
> 
>      for( unsigned int i = 0; i < sys->i_nb_buffers; i++ )
> -        free( sys->p_buffers[i].p[0] );
> +        aligned_free( sys->p_buffers[i].p[0] );
>      free( sys->p_buffers );
>      sys->p_buffers = NULL;
>      sys->i_nb_buffers = 0;
> diff --git a/src/misc/picture.c b/src/misc/picture.c
> index 3bac926714..7c170f0fba 100644
> --- a/src/misc/picture.c
> +++ b/src/misc/picture.c
> @@ -110,7 +110,7 @@ static void picture_DestroyFromResource( picture_t
> *p_picture ) */
>  static void picture_Destroy( picture_t *p_picture )
>  {
> -    free( p_picture->p[0].p_pixels );
> +    aligned_free( p_picture->p[0].p_pixels );
>      free( p_picture );
>  }
> 
> diff --git a/src/misc/picture_pool.c b/src/misc/picture_pool.c
> index 0c5ea39822..17bf3b7b82 100644
> --- a/src/misc/picture_pool.c
> +++ b/src/misc/picture_pool.c
> @@ -58,7 +58,7 @@ static void picture_pool_Destroy(picture_pool_t *pool)
> 
>      vlc_cond_destroy(&pool->wait);
>      vlc_mutex_destroy(&pool->lock);
> -    free(pool);
> +    aligned_free(pool);
>  }
> 
>  void picture_pool_Release(picture_pool_t *pool)


-- 
雷米‧德尼-库尔蒙
https://www.remlab.net/



More information about the vlc-devel mailing list