2N/A# memchr.m4 serial 9
2N/Adnl Copyright (C) 2002-2004, 2009-2010 Free Software Foundation, Inc.
2N/Adnl This file is free software; the Free Software Foundation
2N/Adnl gives unlimited permission to copy and/or distribute it,
2N/Adnl with or without modifications, as long as this notice is preserved.
2N/A
2N/AAC_DEFUN_ONCE([gl_FUNC_MEMCHR],
2N/A[
2N/A dnl Check for prerequisites for memory fence checks.
2N/A gl_FUNC_MMAP_ANON
2N/A AC_CHECK_HEADERS_ONCE([sys/mman.h])
2N/A AC_CHECK_FUNCS_ONCE([mprotect])
2N/A
2N/A dnl These days, we assume memchr is present. But just in case...
2N/A AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
2N/A AC_CHECK_FUNCS_ONCE([memchr])
2N/A if test $ac_cv_func_memchr = yes; then
2N/A # Detect platform-specific bugs in some versions of glibc:
2N/A # memchr should not dereference anything with length 0
2N/A # http://bugzilla.redhat.com/499689
2N/A # memchr should not dereference overestimated length after a match
2N/A # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=521737
2N/A # http://sourceware.org/bugzilla/show_bug.cgi?id=10162
2N/A # Assume that memchr works on platforms that lack mprotect.
2N/A AC_CACHE_CHECK([whether memchr works], [gl_cv_func_memchr_works],
2N/A [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
2N/A#include <string.h>
2N/A#if HAVE_SYS_MMAN_H
2N/A# include <fcntl.h>
2N/A# include <unistd.h>
2N/A# include <sys/types.h>
2N/A# include <sys/mman.h>
2N/A# ifndef MAP_FILE
2N/A# define MAP_FILE 0
2N/A# endif
2N/A#endif
2N/A]], [[
2N/A char *fence = NULL;
2N/A#if HAVE_SYS_MMAN_H && HAVE_MPROTECT
2N/A# if HAVE_MAP_ANONYMOUS
2N/A const int flags = MAP_ANONYMOUS | MAP_PRIVATE;
2N/A const int fd = -1;
2N/A# else /* !HAVE_MAP_ANONYMOUS */
2N/A const int flags = MAP_FILE | MAP_PRIVATE;
2N/A int fd = open ("/dev/zero", O_RDONLY, 0666);
2N/A if (fd >= 0)
2N/A# endif
2N/A {
2N/A int pagesize = getpagesize ();
2N/A char *two_pages =
2N/A (char *) mmap (NULL, 2 * pagesize, PROT_READ | PROT_WRITE,
2N/A flags, fd, 0);
2N/A if (two_pages != (char *)(-1)
2N/A && mprotect (two_pages + pagesize, pagesize, PROT_NONE) == 0)
2N/A fence = two_pages + pagesize;
2N/A }
2N/A#endif
2N/A if (fence)
2N/A {
2N/A if (memchr (fence, 0, 0))
2N/A return 1;
2N/A strcpy (fence - 9, "12345678");
2N/A if (memchr (fence - 9, 0, 79) != fence - 1)
2N/A return 2;
2N/A if (memchr (fence - 1, 0, 3) != fence - 1)
2N/A return 3;
2N/A }
2N/A return 0;
2N/A]])], [gl_cv_func_memchr_works=yes], [gl_cv_func_memchr_works=no],
2N/A [dnl Be pessimistic for now.
2N/A gl_cv_func_memchr_works="guessing no"])])
2N/A if test "$gl_cv_func_memchr_works" != yes; then
2N/A REPLACE_MEMCHR=1
2N/A fi
2N/A else
2N/A HAVE_MEMCHR=0
2N/A fi
2N/A if test $HAVE_MEMCHR = 0 || test $REPLACE_MEMCHR = 1; then
2N/A AC_LIBOBJ([memchr])
2N/A gl_PREREQ_MEMCHR
2N/A fi
2N/A])
2N/A
2N/A# Prerequisites of lib/memchr.c.
2N/AAC_DEFUN([gl_PREREQ_MEMCHR], [
2N/A AC_CHECK_HEADERS([bp-sym.h])
2N/A])