[vlc-commits] compat: restore posix_memalign()
Rémi Denis-Courmont
git at videolan.org
Fri Jul 26 21:20:52 CEST 2019
vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Fri Jul 26 20:54:25 2019 +0300| [cd80b6ab72d9ba54fa928be14e4a581da4005bd6] | committer: Rémi Denis-Courmont
compat: restore posix_memalign()
aligned_alloc() is not portably suitable for over-alignments.
posix_memalign()/memalign() are still needed.
This essentially reverts commit 34cd965645cb0246f3d74515bbd5e55367f7d884.
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=cd80b6ab72d9ba54fa928be14e4a581da4005bd6
---
compat/posix_memalign.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++
configure.ac | 4 +--
include/vlc_fixups.h | 5 ++++
3 files changed, 87 insertions(+), 2 deletions(-)
diff --git a/compat/posix_memalign.c b/compat/posix_memalign.c
new file mode 100644
index 0000000000..bdd8f612c5
--- /dev/null
+++ b/compat/posix_memalign.c
@@ -0,0 +1,80 @@
+/*****************************************************************************
+ * posix_memalign.c: POSIX posix_memalign() replacement
+ *****************************************************************************
+ * Copyright © 2012, 2019 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
+ * 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 <errno.h>
+#include <stdlib.h>
+#include <limits.h>
+
+#ifdef HAVE_MEMALIGN
+# include <malloc.h>
+#else
+
+static void *memalign(size_t align, size_t size)
+{
+ void *p = malloc(size);
+
+ if ((uintptr_t)p & (align - 1)) {
+ free(p);
+ p = NULL;
+ }
+
+ return p;
+}
+
+#endif
+
+static int check_align(size_t align)
+{
+ if (align & (align - 1)) /* must be a power of two */
+ return EINVAL;
+ if (align < sizeof (void *)) /* must be a multiple of sizeof (void *) */
+ return EINVAL;
+ return 0;
+}
+
+int posix_memalign(void **ptr, size_t align, size_t size)
+{
+ int val = check_align(align);
+ if (val)
+ return val;
+
+ /* Unlike posix_memalign(), legacy memalign() requires that size be a
+ * multiple of align.
+ */
+ if (size > (SIZE_MAX / 2))
+ return ENOMEM;
+
+ size += (-size) & (align - 1);
+
+ int saved_errno = errno;
+ void *p = memalign(align, size);
+ if (p == NULL) {
+ val = errno;
+ errno = saved_errno;
+ return val;
+ }
+
+ *ptr = p;
+ return 0;
+}
diff --git a/configure.ac b/configure.ac
index ac818d8b83..d1aef347c6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -633,8 +633,8 @@ dnl Check for system libs needed
need_libc=false
dnl Check for usual libc functions
-AC_CHECK_FUNCS([accept4 daemon fcntl flock fstatvfs fork getenv getpwuid_r isatty memalign mkostemp mmap open_memstream newlocale openat pipe2 pread posix_fadvise posix_madvise posix_memalign setlocale stricmp strnicmp strptime uselocale])
-AC_REPLACE_FUNCS([aligned_alloc atof atoll dirfd fdopendir flockfile fsync getdelim getpid lfind lldiv memrchr nrand48 poll recvmsg rewind sendmsg setenv strcasecmp strcasestr strdup strlcpy strndup strnlen strnstr strsep strtof strtok_r strtoll swab tdestroy tfind timegm timespec_get strverscmp pathconf])
+AC_CHECK_FUNCS([accept4 daemon fcntl flock fstatvfs fork getenv getpwuid_r isatty memalign mkostemp mmap open_memstream newlocale openat pipe2 pread posix_fadvise posix_madvise setlocale stricmp strnicmp strptime uselocale])
+AC_REPLACE_FUNCS([aligned_alloc atof atoll dirfd fdopendir flockfile fsync getdelim getpid lfind lldiv memrchr nrand48 poll posix_memalign recvmsg rewind sendmsg setenv strcasecmp strcasestr strdup strlcpy strndup strnlen strnstr strsep strtof strtok_r strtoll swab tdestroy 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 1518b84362..8fd01da0ce 100644
--- a/include/vlc_fixups.h
+++ b/include/vlc_fixups.h
@@ -90,6 +90,7 @@ typedef struct
#if !defined (HAVE_ALIGNED_ALLOC) || \
!defined (HAVE_MEMRCHR) || \
+ !defined (HAVE_POSIX_MEMALIGN) || \
!defined (HAVE_QSORT_R) || \
!defined (HAVE_STRLCPY) || \
!defined (HAVE_STRNDUP) || \
@@ -299,6 +300,10 @@ int setenv (const char *, const char *, int);
int unsetenv (const char *);
#endif
+#ifndef HAVE_POSIX_MEMALIGN
+int posix_memalign(void **, size_t, size_t);
+#endif
+
#ifndef HAVE_ALIGNED_ALLOC
void *aligned_alloc(size_t, size_t);
#endif
More information about the vlc-commits
mailing list