Fix `#ifdef HAVE_CONFIG_H' stanza in some stand-alone tests.
[bpt/guile.git] / m4 / memchr.m4
CommitLineData
0f00f2c3 1# memchr.m4 serial 10
49114fd4 2dnl Copyright (C) 2002-2004, 2009-2011 Free Software Foundation, Inc.
c4b681fd
LC
3dnl This file is free software; the Free Software Foundation
4dnl gives unlimited permission to copy and/or distribute it,
5dnl with or without modifications, as long as this notice is preserved.
6
7AC_DEFUN_ONCE([gl_FUNC_MEMCHR],
8[
9 dnl Check for prerequisites for memory fence checks.
10 gl_FUNC_MMAP_ANON
11 AC_CHECK_HEADERS_ONCE([sys/mman.h])
12 AC_CHECK_FUNCS_ONCE([mprotect])
13
14 dnl These days, we assume memchr is present. But just in case...
15 AC_REQUIRE([gl_HEADER_STRING_H_DEFAULTS])
a927b6c1 16 AC_CHECK_FUNCS_ONCE([memchr])
c4b681fd
LC
17 if test $ac_cv_func_memchr = yes; then
18 # Detect platform-specific bugs in some versions of glibc:
19 # memchr should not dereference anything with length 0
20 # http://bugzilla.redhat.com/499689
21 # memchr should not dereference overestimated length after a match
22 # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=521737
23 # http://sourceware.org/bugzilla/show_bug.cgi?id=10162
24 # Assume that memchr works on platforms that lack mprotect.
25 AC_CACHE_CHECK([whether memchr works], [gl_cv_func_memchr_works],
26 [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
27#include <string.h>
28#if HAVE_SYS_MMAN_H
29# include <fcntl.h>
30# include <unistd.h>
31# include <sys/types.h>
32# include <sys/mman.h>
33# ifndef MAP_FILE
34# define MAP_FILE 0
35# endif
36#endif
37]], [[
0f00f2c3 38 int result = 0;
c4b681fd
LC
39 char *fence = NULL;
40#if HAVE_SYS_MMAN_H && HAVE_MPROTECT
41# if HAVE_MAP_ANONYMOUS
42 const int flags = MAP_ANONYMOUS | MAP_PRIVATE;
43 const int fd = -1;
44# else /* !HAVE_MAP_ANONYMOUS */
45 const int flags = MAP_FILE | MAP_PRIVATE;
46 int fd = open ("/dev/zero", O_RDONLY, 0666);
47 if (fd >= 0)
48# endif
49 {
50 int pagesize = getpagesize ();
51 char *two_pages =
1cd4fffc
LC
52 (char *) mmap (NULL, 2 * pagesize, PROT_READ | PROT_WRITE,
53 flags, fd, 0);
c4b681fd 54 if (two_pages != (char *)(-1)
1cd4fffc
LC
55 && mprotect (two_pages + pagesize, pagesize, PROT_NONE) == 0)
56 fence = two_pages + pagesize;
c4b681fd
LC
57 }
58#endif
59 if (fence)
60 {
61 if (memchr (fence, 0, 0))
0f00f2c3 62 result |= 1;
c4b681fd
LC
63 strcpy (fence - 9, "12345678");
64 if (memchr (fence - 9, 0, 79) != fence - 1)
0f00f2c3
LC
65 result |= 2;
66 if (memchr (fence - 1, 0, 3) != fence - 1)
67 result |= 4;
c4b681fd 68 }
0f00f2c3 69 return result;
c4b681fd
LC
70]])], [gl_cv_func_memchr_works=yes], [gl_cv_func_memchr_works=no],
71 [dnl Be pessimistic for now.
72 gl_cv_func_memchr_works="guessing no"])])
73 if test "$gl_cv_func_memchr_works" != yes; then
c4b681fd 74 REPLACE_MEMCHR=1
c4b681fd 75 fi
a927b6c1
LC
76 else
77 HAVE_MEMCHR=0
78 fi
79 if test $HAVE_MEMCHR = 0 || test $REPLACE_MEMCHR = 1; then
80 AC_LIBOBJ([memchr])
81 gl_PREREQ_MEMCHR
c4b681fd
LC
82 fi
83])
84
85# Prerequisites of lib/memchr.c.
86AC_DEFUN([gl_PREREQ_MEMCHR], [
87 AC_CHECK_HEADERS([bp-sym.h])
88])