[vlc-commits] compat: replace aligned_alloc() rather than posix_memalign()

Rémi Denis-Courmont git at videolan.org
Sat Jun 17 14:35:07 CEST 2017


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sat Jun 17 15:27:16 2017 +0300| [34cd965645cb0246f3d74515bbd5e55367f7d884] | committer: Rémi Denis-Courmont

compat: replace aligned_alloc() rather than posix_memalign()

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=34cd965645cb0246f3d74515bbd5e55367f7d884
---

 compat/{posix_memalign.c => aligned_alloc.c} | 62 ++++++++++++++--------------
 configure.ac                                 |  4 +-
 include/vlc_common.h                         |  8 +---
 include/vlc_fixups.h                         |  6 +--
 4 files changed, 36 insertions(+), 44 deletions(-)

diff --git a/compat/posix_memalign.c b/compat/aligned_alloc.c
similarity index 55%
rename from compat/posix_memalign.c
rename to compat/aligned_alloc.c
index 3c6ffdfe28..9a61c0010f 100644
--- a/compat/posix_memalign.c
+++ b/compat/aligned_alloc.c
@@ -1,7 +1,7 @@
 /*****************************************************************************
- * posix_memalign.c: POSIX posix_memalign() replacement
+ * aligned_alloc.c: C11 aligned_alloc() replacement
  *****************************************************************************
- * Copyright © 2012 Rémi Denis-Courmont
+ * Copyright © 2012, 2017 Rémi Denis-Courmont
  *
  * 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
@@ -22,46 +22,44 @@
 # include <config.h>
 #endif
 
+#include <assert.h>
 #include <stdlib.h>
 #include <errno.h>
+#if !defined (HAVE_POSIX_MEMALIGN) && !defined (_WIN32) && !defined (__APPLE__)
+# include <malloc.h>
+#endif
 
-static int check_align (size_t align)
+void *aligned_alloc(size_t align, size_t size)
 {
-    for (size_t i = sizeof (void *); i != 0; i *= 2)
-        if (align == i)
-            return 0;
-    return EINVAL;
-}
-
-#if !defined (_WIN32) && !defined (__APPLE__)
-#include <malloc.h>
+    /* align must be a power of 2 */
+    /* size must be a multiple of align */
+    if ((align & (align - 1)) || (size & (align - 1)))
+    {
+        errno = EINVAL;
+        return NULL;
+    }
 
-int posix_memalign (void **ptr, size_t align, size_t size)
-{
-    if (check_align (align))
-        return EINVAL;
+#ifdef HAVE_POSIX_MEMALIGN
+    if (align < sizeof (void *)) /* POSIX does not allow small alignment */
+        align = sizeof (void *);
 
-    int saved_errno = errno;
-    void *p = memalign (align, size);
-    if (p == NULL)
+    void *ptr;
+    int err = posix_memalign(&ptr, align, size);
+    if (err)
     {
-        errno = saved_errno;
-        return ENOMEM;
+        errno = err;
+        ptr = NULL;
     }
+    return ptr;
 
-    *ptr = p;
-    return 0;
-}
+#elif !defined (_WIN32) && !defined (__APPLE__)
 
-#else
-
-int posix_memalign (void **ptr, size_t align, size_t size)
-{
-    if (check_align (align))
-        return EINVAL;
+   return memalign(align, size);
 
-    *ptr = NULL;
-    return size ? ENOMEM : 0;
-}
+#else
 
+   if (size > 0)
+       errno = ENOMEM;
+   return NULL;
 #endif
+}
diff --git a/configure.ac b/configure.ac
index b1883a18b7..67fc7deb1a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -597,8 +597,8 @@ 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 setlocale stricmp strnicmp strptime tdestroy uselocale])
-AC_REPLACE_FUNCS([atof atoll dirfd fdopendir ffsll flockfile fsync getdelim getpid lldiv memrchr nrand48 poll posix_memalign recvmsg rewind sendmsg setenv strcasecmp strcasestr strdup strlcpy strndup strnlen strnstr strsep strtof strtok_r strtoll swab tfind timegm timespec_get strverscmp pathconf])
+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([gettimeofday])
 AC_CHECK_FUNC(fdatasync,,
   [AC_DEFINE(fdatasync, fsync, [Alias fdatasync() to fsync() if missing.])
diff --git a/include/vlc_common.h b/include/vlc_common.h
index e1cf885379..0bfb1b9b04 100644
--- a/include/vlc_common.h
+++ b/include/vlc_common.h
@@ -854,13 +854,7 @@ VLC_API bool vlc_ureduce( unsigned *, unsigned *, uint64_t, uint64_t, uint64_t )
 # define vlc_memalign(align, size) (_aligned_malloc(size, align))
 # define vlc_free(base)            (_aligned_free(base))
 #else
-static inline void *vlc_memalign(size_t align, size_t size)
-{
-    void *base;
-    if (unlikely(posix_memalign(&base, align, size)))
-        base = NULL;
-    return base;
-}
+# define vlc_memalign(align, size) aligned_alloc(align, size)
 # define vlc_free(base) free(base)
 #endif
 
diff --git a/include/vlc_fixups.h b/include/vlc_fixups.h
index 8bfb76888f..44b8426f2c 100644
--- a/include/vlc_fixups.h
+++ b/include/vlc_fixups.h
@@ -88,7 +88,7 @@ typedef struct
 # include <stdio.h> /* FILE */
 #endif
 
-#if !defined (HAVE_POSIX_MEMALIGN) || \
+#if !defined (HAVE_ALIGNED_ALLOC) || \
     !defined (HAVE_MEMRCHR) || \
     !defined (HAVE_STRLCPY) || \
     !defined (HAVE_STRNDUP) || \
@@ -302,8 +302,8 @@ int setenv (const char *, const char *, int);
 int unsetenv (const char *);
 #endif
 
-#ifndef HAVE_POSIX_MEMALIGN
-int posix_memalign (void **, size_t, size_t);
+#ifndef HAVE_ALIGNED_ALLOC
+void *aligned_alloc(size_t, size_t);
 #endif
 
 #if defined(__native_client__) && defined(__cplusplus)



More information about the vlc-commits mailing list