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

Thomas Guillem thomas at gllm.fr
Tue Jun 20 12:36:20 CEST 2017



On Tue, Jun 20, 2017, at 12:20, Steve Lhomme wrote:
> 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()
> ---
>  compat/aligned_alloc.c         | 16 ++++++++++------
>  compat/aligned_free.c          | 43
>  ++++++++++++++++++++++++++++++++++++++++++
>  configure.ac                   |  2 +-
>  include/vlc_fixups.h           |  3 +++
>  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 +-
>  9 files changed, 62 insertions(+), 12 deletions(-)
>  create mode 100644 compat/aligned_free.c
> 
> 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)
>      }
>      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/compat/aligned_free.c b/compat/aligned_free.c
> new file mode 100644
> index 0000000000..380125f20d
> --- /dev/null
> +++ b/compat/aligned_free.c
> @@ -0,0 +1,43 @@
> +/*****************************************************************************
> + * aligned_free.c: C11 aligned_alloc() replacement counterpart
> +
> *****************************************************************************
> + * Copyright © 2012, 2017 Steve Lhomme
> + *
> + * This program is free software; you can redistribute it and/or modify
> it
> + * under the terms of the GNU Lesser General Public License as published
> by
> + * the Free Software Foundation; either version 2.1 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> + * GNU Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> License
> + * along with this program; if not, write to the Free Software
> Foundation,
> + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
> +
> *****************************************************************************/
> +
> +#ifdef HAVE_CONFIG_H
> +# include <config.h>
> +#endif
> +
> +#include <assert.h>
> +#include <stdlib.h>
> +#include <errno.h>
> +#if !defined (HAVE_POSIX_MEMALIGN) && !defined (_WIN32)
> +# include <malloc.h>
> +#endif
> +
> +void aligned_free(void *ptr)
> +{
> +#if defined (_WIN32)
> +#ifdef __MINGW32__
> +    return __mingw_aligned_free(ptr);
> +#elif defined(_MSC_VER)
> +    return _aligned_free(ptr);
> +#endif
> +#else
> +    free(ptr);
> +#endif
> +}
> diff --git a/configure.ac b/configure.ac
> index 67fc7deb1a..633dd8a034 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -598,7 +598,7 @@ need_libc=false
>  
>  dnl Check for usual libc functions
>  AC_CHECK_FUNCS([daemon fcntl flock fstatvfs fork getenv getpwuid_r
>  isatty lstat memalign mkostemp mmap open_memstream openat pread
>  posix_fadvise posix_madvise posix_memalign setlocale stricmp strnicmp
>  strptime tdestroy uselocale])
> -AC_REPLACE_FUNCS([aligned_alloc atof atoll dirfd fdopendir ffsll
> flockfile fsync getdelim getpid lldiv memrchr nrand48 poll recvmsg rewind
> sendmsg setenv strcasecmp strcasestr strdup strlcpy strndup strnlen
> strnstr strsep strtof strtok_r strtoll swab tfind timegm timespec_get
> strverscmp pathconf])
> +AC_REPLACE_FUNCS([aligned_alloc aligned_free atof atoll dirfd fdopendir
> ffsll flockfile fsync getdelim getpid lldiv memrchr nrand48 poll recvmsg
> rewind sendmsg setenv strcasecmp strcasestr strdup strlcpy strndup
> strnlen strnstr strsep strtof strtok_r strtoll swab tfind timegm
> timespec_get strverscmp pathconf])
>  AC_REPLACE_FUNCS([gettimeofday])
>  AC_CHECK_FUNC(fdatasync,,
>    [AC_DEFINE(fdatasync, fsync, [Alias fdatasync() to fsync() if
>    missing.])
> diff --git a/include/vlc_fixups.h b/include/vlc_fixups.h
> index 44b8426f2c..7037fe2f85 100644
> --- a/include/vlc_fixups.h
> +++ b/include/vlc_fixups.h
> @@ -305,6 +305,9 @@ int unsetenv (const char *);
>  #ifndef HAVE_ALIGNED_ALLOC
>  void *aligned_alloc(size_t, size_t);
>  #endif
> +#ifndef HAVE_ALIGNED_FREE
> +void aligned_free(void*);
> +#endif
>  
>  #if defined(__native_client__) && defined(__cplusplus)
>  # define HAVE_USELOCALE
> 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..d31d4b7ca6 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] );
> +        align_free( sys->p_buffers[i].p[0] );

*aligned_free*

>      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);
>  }

Otherwise, LGTM

>  
>  void picture_pool_Release(picture_pool_t *pool)
> -- 
> 2.12.1
> 
> _______________________________________________
> vlc-devel mailing list
> To unsubscribe or modify your subscription options:
> https://mailman.videolan.org/listinfo/vlc-devel


More information about the vlc-devel mailing list