------------------------------------------------------------------------
dnl @synopsis AX_FUNC_MALLOC_ALIGNED
dnl
dnl Some operating systems (generally, BSD Unix variants) lack a
dnl posix_memalign function, a memalign function, and a working (meaning,
dnl the memory can be freed) valloc function. To make up for it, the
dnl malloc function promises to return page-aligned addresses if more than
dnl one page's worth of memory is allocated. AX_FUNC_MALLOC_ALIGNED
dnl checks for this condition and defines HAVE_PAGE_ALIGNED_MALLOC if the
dnl condition holds.
dnl
dnl As an aside, note that valloc'd memory cannot safely be freed on all
dnl operating systems. (Again, some flavors of BSD are the troublemakers.)
dnl It's best to avoid using valloc in favor of posix_memalign, memalign,
dnl or an aligned malloc as detected by AX_FUNC_MALLOC_ALIGNED.
dnl
dnl Caveat: AX_FUNC_MALLOC_ALIGNED takes a probabalistic approach. If 100
dnl calls to malloc all return page-aligned addresses, it assumes that all
dnl calls will behave likewise. It is therefore possible -- albeit
dnl extremely unlikely -- that AX_FUNC_MALLOC_ALIGNED can return a false
dnl positive.
dnl
dnl @version $Id: $
dnl @author Scott Pakin <address@hidden>
dnl
AC_DEFUN([AX_FUNC_MALLOC_ALIGNED],
[AC_CACHE_CHECK([if large mallocs guarantee page-alignment],
[ac_cv_func_malloc_aligned],
[AC_TRY_RUN([
#include "confdefs.h"
int main()
{
int pagesize = getpagesize();
int i;
for (i=0; i<100; i++)
if ((unsigned long)malloc(pagesize+1) & (pagesize-1))
return 1;
return 0;
}
],
[ac_cv_func_malloc_aligned=yes],
[ac_cv_func_malloc_aligned=no],
[ac_cv_func_malloc_aligned=no])
])
if test "$ac_cv_func_malloc_aligned" = yes ; then
AC_DEFINE([HAVE_PAGE_ALIGNED_MALLOC], [1],
[Define if `malloc'ing more than one page always returns a page-aligned
address.])
fi
])
------------------------------------------------------------------------
dnl @synopsis AX_FUNC_POSIX_MEMALIGN_WORKS
dnl
dnl
dnl Some versions of posix_memalign (notably glibc 2.2.5) incorrectly
dnl apply their power-of-two check to the size argument, not the alignment
dnl argument. AX_FUNC_POSIX_MEMALIGN_WORKS defines POSIX_MEMALIGN_WORKS
dnl if the power-of-two check is correctly applied to the alignment
dnl argument.
dnl
dnl @version $Id: $
dnl @author Scott Pakin <address@hidden>
dnl
AC_DEFUN([AX_FUNC_POSIX_MEMALIGN_WORKS],
[AC_CACHE_CHECK([if posix_memalign works],
[ac_cv_func_posix_memalign_works],
[AC_TRY_RUN([
#include <stdlib.h>
int
main ()
{
void *buffer;
/* Some versions of glibc incorrectly perform the alignment check on
* the size word. */
return posix_memalign (&buffer, sizeof(void *), 123);
}
],
[ac_cv_func_posix_memalign_works=yes],
[ac_cv_func_posix_memalign_works=no],
[ac_cv_func_posix_memalign_works=no])])
if test "$ac_cv_func_posix_memalign_works" = "yes" ; then
AC_DEFINE([POSIX_MEMALIGN_WORKS], [1],
[Define if the `posix_memalign' function correctly checks its `alignment'
and `size' parameters.])
fi
])
------------------------------------------------------------------------
dnl @synopsis AX_REQUIRE_ONE_FUNC (FUNCTION..., [ACTION-IF-ANY-FOUND],
[ACTION-IF-NONE-FOUND])
dnl
dnl AX_REQUIRE_ONE_FUNC is a simple wrapper for AC_CHECK_FUNCS. It calls
dnl AC_CHECK_FUNCS on the list of functions named in the first argument,
dnl then invokes ACTION-IF-ANY-FOUND if at least one of the functions
dnl exists or ACTION-IF-NONE-FOUND if none of the functions exist.
dnl
dnl Here's an example:
dnl
dnl AX_REQUIRE_ONE_FUNC([posix_memalign memalign valloc], ,
dnl [AC_MSG_ERROR([unable to allocate page-aligned memory])])
dnl
dnl @version $Id: $
dnl @author Scott Pakin <address@hidden>
dnl
AC_DEFUN([AX_REQUIRE_ONE_FUNC],
[m4_define([ac_1func_cv], [AS_TR_SH(ac_cv_func_any_$1)])
AC_CACHE_VAL([ac_1func_cv],
[ac_1func_cv=no
AC_CHECK_FUNCS([$1], [ac_1func_cv="$ac_1func_cv $ac_func"])])
AS_IF([test "$ac_1func_cv" = "no"],
[$3],
[ac_1func_cv=`echo $ac_1func_cv | sed 's/^no //'`
$2])
])