1
mirror of https://code.videolan.org/videolan/vlc synced 2024-09-28 23:09:59 +02:00

compat: replace aligned_alloc() rather than posix_memalign()

This commit is contained in:
Rémi Denis-Courmont 2017-06-17 15:27:16 +03:00
parent 6d194c87e6
commit 34cd965645
4 changed files with 38 additions and 46 deletions

View File

@ -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>
int posix_memalign (void **ptr, size_t align, size_t size)
{
if (check_align (align))
return EINVAL;
int saved_errno = errno;
void *p = memalign (align, size);
if (p == NULL)
/* align must be a power of 2 */
/* size must be a multiple of align */
if ((align & (align - 1)) || (size & (align - 1)))
{
errno = saved_errno;
return ENOMEM;
errno = EINVAL;
return NULL;
}
*ptr = p;
return 0;
}
#ifdef HAVE_POSIX_MEMALIGN
if (align < sizeof (void *)) /* POSIX does not allow small alignment */
align = sizeof (void *);
void *ptr;
int err = posix_memalign(&ptr, align, size);
if (err)
{
errno = err;
ptr = NULL;
}
return ptr;
#elif !defined (_WIN32) && !defined (__APPLE__)
return memalign(align, size);
#else
int posix_memalign (void **ptr, size_t align, size_t size)
{
if (check_align (align))
return EINVAL;
*ptr = NULL;
return size ? ENOMEM : 0;
}
if (size > 0)
errno = ENOMEM;
return NULL;
#endif
}

View File

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

View File

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

View File

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