[vlc-devel] [PATCH] core: add aligned_free to match the aligned_alloc used in the code
Steve Lhomme
robux4 at videolabs.io
Tue Jun 20 13:41:55 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
---
compat/aligned_alloc.c | 16 ++++++++++------
configure.ac | 2 +-
include/vlc_fixups.h | 18 ++++++++++++++++++
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, 34 insertions(+), 12 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)
}
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/configure.ac b/configure.ac
index 67fc7deb1a..6c9867b8ef 100644
--- a/configure.ac
+++ b/configure.ac
@@ -597,7 +597,7 @@ dnl Check for system libs needed
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_CHECK_FUNCS([aligned_free 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([gettimeofday])
AC_CHECK_FUNC(fdatasync,,
diff --git a/include/vlc_fixups.h b/include/vlc_fixups.h
index 44b8426f2c..6cb79219e2 100644
--- a/include/vlc_fixups.h
+++ b/include/vlc_fixups.h
@@ -305,6 +305,24 @@ int unsetenv (const char *);
#ifndef HAVE_ALIGNED_ALLOC
void *aligned_alloc(size_t, size_t);
#endif
+#ifndef HAVE_ALIGNED_FREE
+#if defined (_WIN32)
+#include <malloc.h>
+#endif
+static inline 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
+}
+
+#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..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