compat: restore posix_memalign()

aligned_alloc() is not portably suitable for over-alignments.
posix_memalign()/memalign() are still needed.

This essentially reverts commit 34cd965645.
This commit is contained in:
Rémi Denis-Courmont 2019-07-26 20:54:25 +03:00
parent 577ba74815
commit cd80b6ab72
3 changed files with 87 additions and 2 deletions

80
compat/posix_memalign.c Normal file
View File

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

View File

@ -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.])

View File

@ -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