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

Steve Lhomme robux4 at videolabs.io
Wed Jun 21 15:05:30 CEST 2017


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
replaces https://patches.videolan.org/patch/16997/
- check HAVE_MEMALIGN is defined in aligned_alloc
- always use a macro for aligned_free()
- remove vlc_memalign()
---
 compat/aligned_alloc.c         | 20 +++++++++++---------
 include/vlc_common.h           | 10 ----------
 include/vlc_fixups.h           |  8 ++++++++
 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 +-
 8 files changed, 24 insertions(+), 24 deletions(-)

diff --git a/compat/aligned_alloc.c b/compat/aligned_alloc.c
index 9ea352b0c5..02881eeeb0 100644
--- a/compat/aligned_alloc.c
+++ b/compat/aligned_alloc.c
@@ -25,7 +25,7 @@
 #include <assert.h>
 #include <stdlib.h>
 #include <errno.h>
-#if !defined (HAVE_POSIX_MEMALIGN) && !defined (_WIN32)
+#if !defined (HAVE_POSIX_MEMALIGN)
 # include <malloc.h>
 #endif
 
@@ -52,14 +52,16 @@ void *aligned_alloc(size_t align, size_t size)
     }
     return ptr;
 
-#elif !defined (_WIN32)
-
-   return memalign(align, size);
-
+#elif defined(HAVE_MEMALIGN)
+    return memalign(align, size);
+#elif defined (_WIN32) && defined(__MINGW32__)
+    return __mingw_aligned_malloc(size, align);
+#elif defined (_WIN32) && defined(_MSC_VER)
+    return _aligned_malloc(size, align);
 #else
-
-   if (size > 0)
-       errno = ENOMEM;
-   return NULL;
+#warning unsupported aligned allocation!
+    if (size > 0)
+        errno = ENOMEM;
+    return NULL;
 #endif
 }
diff --git a/include/vlc_common.h b/include/vlc_common.h
index 35655b1299..94ab07e7a1 100644
--- a/include/vlc_common.h
+++ b/include/vlc_common.h
@@ -845,16 +845,6 @@ VLC_API bool vlc_ureduce( unsigned *, unsigned *, uint64_t, uint64_t, uint64_t )
 #define container_of(ptr, type, member) \
     ((type *)(((char *)(ptr)) - offsetof(type, member)))
 
-/* Aligned memory allocator */
-
-#ifdef __MINGW32__
-# define vlc_memalign(align, size) (__mingw_aligned_malloc(size, align))
-#elif defined(_MSC_VER)
-# define vlc_memalign(align, size) (_aligned_malloc(size, align))
-#else
-# define vlc_memalign(align, size) aligned_alloc(align, size)
-#endif
-
 /*****************************************************************************
  * I18n stuff
  *****************************************************************************/
diff --git a/include/vlc_fixups.h b/include/vlc_fixups.h
index 44b8426f2c..eb0eb17278 100644
--- a/include/vlc_fixups.h
+++ b/include/vlc_fixups.h
@@ -306,6 +306,14 @@ int unsetenv (const char *);
 void *aligned_alloc(size_t, size_t);
 #endif
 
+#if defined (_WIN32) && defined(__MINGW32__)
+#define aligned_free(ptr)  __mingw_aligned_free(ptr)
+#elif defined (_WIN32) && defined(_MSC_VER)
+#define aligned_free(ptr)  _aligned_free(ptr)
+#else
+#define aligned_free(ptr)  free(ptr)
+#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)
-- 
2.12.1



More information about the vlc-devel mailing list