Commit 34cd9656 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

compat: replace aligned_alloc() rather than posix_memalign()

parent 6d194c87
/*****************************************************************************
* 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
}
......@@ -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.])
......
......@@ -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
......
......@@ -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)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment