Fix compilation with wxWidgets 2.8.12
[amule.git] / m4 / mmap.m4
blob92a134cf3b977dadbd00c2caa7b35c514fbd1f15
1 #                                                       -*- Autoconf -*-
2 # This file is part of the aMule Project.
4 # Copyright (c) 2014 aMule Team ( admin@amule.org / http://www.amule.org )
6 # Any parts of this program derived from the xMule, lMule or eMule project,
7 # or contributed by third-party developers are copyrighted by their
8 # respective authors.
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
26 dnl ---------------------------------------------------------------------------
27 dnl MULE_FUNC_MMAP
28 dnl
29 dnl This function is copied over from autoconf sources, but fixed to work with
30 dnl C++.
31 dnl ---------------------------------------------------------------------------
32 AC_DEFUN([MULE_FUNC_MMAP],
33 [AC_CHECK_HEADERS_ONCE([stdlib.h unistd.h sys/param.h])
34 AC_CHECK_FUNCS([getpagesize])
35 AC_CACHE_CHECK([for working mmap], [ac_cv_func_mmap_fixed_mapped],
36 [AC_RUN_IFELSE([AC_LANG_SOURCE([AC_INCLUDES_DEFAULT]
37 [[/* malloc might have been renamed as rpl_malloc. */
38 #undef malloc
40 /* Thanks to Mike Haertel and Jim Avera for this test.
41    Here is a matrix of mmap possibilities:
42         mmap private not fixed
43         mmap private fixed at somewhere currently unmapped
44         mmap private fixed at somewhere already mapped
45         mmap shared not fixed
46         mmap shared fixed at somewhere currently unmapped
47         mmap shared fixed at somewhere already mapped
48    For private mappings, we should verify that changes cannot be read()
49    back from the file, nor mmap's back from the file at a different
50    address.  (There have been systems where private was not correctly
51    implemented like the infamous i386 svr4.0, and systems where the
52    VM page cache was not coherent with the file system buffer cache
53    like early versions of FreeBSD and possibly contemporary NetBSD.)
54    For shared mappings, we should conversely verify that changes get
55    propagated back to all the places they're supposed to be.
57    Grep wants private fixed already mapped.
58    The main things grep needs to know about mmap are:
59    * does it exist and is it safe to write into the mmap'd area
60    * how to use it (BSD variants)  */
62 #include <fcntl.h>
63 #include <sys/mman.h>
65 #if !defined STDC_HEADERS && !defined HAVE_STDLIB_H
66 char *malloc ();
67 #endif
69 /* This mess was copied from the GNU getpagesize.h.  */
70 #ifndef HAVE_GETPAGESIZE
71 # ifdef _SC_PAGESIZE
72 #  define getpagesize() sysconf(_SC_PAGESIZE)
73 # else /* no _SC_PAGESIZE */
74 #  ifdef HAVE_SYS_PARAM_H
75 #   include <sys/param.h>
76 #   ifdef EXEC_PAGESIZE
77 #    define getpagesize() EXEC_PAGESIZE
78 #   else /* no EXEC_PAGESIZE */
79 #    ifdef NBPG
80 #     define getpagesize() NBPG * CLSIZE
81 #     ifndef CLSIZE
82 #      define CLSIZE 1
83 #     endif /* no CLSIZE */
84 #    else /* no NBPG */
85 #     ifdef NBPC
86 #      define getpagesize() NBPC
87 #     else /* no NBPC */
88 #      ifdef PAGESIZE
89 #       define getpagesize() PAGESIZE
90 #      endif /* PAGESIZE */
91 #     endif /* no NBPC */
92 #    endif /* no NBPG */
93 #   endif /* no EXEC_PAGESIZE */
94 #  else /* no HAVE_SYS_PARAM_H */
95 #   define getpagesize() 8192   /* punt totally */
96 #  endif /* no HAVE_SYS_PARAM_H */
97 # endif /* no _SC_PAGESIZE */
99 #endif /* no HAVE_GETPAGESIZE */
102 main ()
104   char *data, *data2, *data3;
105   int i, pagesize;
106   int fd, fd2;
108   pagesize = getpagesize ();
110   /* First, make a file with some known garbage in it. */
111   data = (char *) malloc (pagesize);
112   if (!data)
113     return 1;
114   for (i = 0; i < pagesize; ++i)
115     *(data + i) = rand ();
116   umask (0);
117   fd = creat ("conftest.mmap", 0600);
118   if (fd < 0)
119     return 2;
120   if (write (fd, data, pagesize) != pagesize)
121     return 3;
122   close (fd);
124   /* Next, check that the tail of a page is zero-filled.  File must have
125      non-zero length, otherwise we risk SIGBUS for entire page.  */
126   fd2 = open ("conftest.txt", O_RDWR | O_CREAT | O_TRUNC, 0600);
127   if (fd2 < 0)
128     return 4;
129   data2 = (char *) "";
130   if (write (fd2, data2, 1) != 1)
131     return 5;
132   data2 = (char *) mmap (0, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd2, 0L);
133   if (data2 == MAP_FAILED)
134     return 6;
135   for (i = 0; i < pagesize; ++i)
136     if (*(data2 + i))
137       return 7;
138   close (fd2);
139   if (munmap (data2, pagesize))
140     return 8;
142   /* Next, try to mmap the file at a fixed address which already has
143      something else allocated at it.  If we can, also make sure that
144      we see the same garbage.  */
145   fd = open ("conftest.mmap", O_RDWR);
146   if (fd < 0)
147     return 9;
148   if (data2 != mmap (data2, pagesize, PROT_READ | PROT_WRITE,
149                      MAP_PRIVATE | MAP_FIXED, fd, 0L))
150     return 10;
151   for (i = 0; i < pagesize; ++i)
152     if (*(data + i) != *(data2 + i))
153       return 11;
155   /* Finally, make sure that changes to the mapped area do not
156      percolate back to the file as seen by read().  (This is a bug on
157      some variants of i386 svr4.0.)  */
158   for (i = 0; i < pagesize; ++i)
159     *(data2 + i) = *(data2 + i) + 1;
160   data3 = (char *) malloc (pagesize);
161   if (!data3)
162     return 12;
163   if (read (fd, data3, pagesize) != pagesize)
164     return 13;
165   for (i = 0; i < pagesize; ++i)
166     if (*(data + i) != *(data3 + i))
167       return 14;
168   close (fd);
169   return 0;
170 }]])],
171                [ac_cv_func_mmap_fixed_mapped=yes],
172                [ac_cv_func_mmap_fixed_mapped=no],
173                [ac_cv_func_mmap_fixed_mapped=no])])
174 if test $ac_cv_func_mmap_fixed_mapped = yes; then
175   AC_DEFINE([HAVE_MMAP], [1],
176             [Define to 1 if you have a working `mmap' system call.])
178 rm -f conftest.mmap conftest.txt
182 dnl ---------------------------------------------------------------------------
183 dnl MULE_CHECK_MMAP
185 dnl Checks for mmap() and makes use of it when found.
186 dnl ---------------------------------------------------------------------------
187 AC_DEFUN([MULE_CHECK_MMAP],
189         MULE_ARG_ENABLE([mmap], [no], [enable using mapped memory if supported])
191         AH_TEMPLATE([ENABLE_MMAP], [Define this variable to 1 if using mapped memory was requested. Note that defining it will alone not allow usage of mmap(), but unsetting it will completely disable its usage.])
193         MULE_IF_ENABLED([mmap], [
194                 AC_DEFINE([ENABLE_MMAP], [1])
195                 MULE_FUNC_MMAP
196                 AC_CHECK_FUNCS([munmap sysconf])
197                 AS_IF([test $ac_cv_func_sysconf = yes], [
198                         AC_MSG_CHECKING([for pagesize constant for sysconf])
199                         AC_LINK_IFELSE([
200                                 AC_LANG_PROGRAM([[
201                                         #include <unistd.h>
202                                 ]], [[
203                                         return sysconf(_SC_PAGESIZE);
204                                 ]])
205                         ], [
206                                 AC_MSG_RESULT([_SC_PAGESIZE])
207                                 AC_DEFINE([HAVE__SC_PAGESIZE], [1], [Define to 1 if you have the _SC_PAGESIZE constant in <unistd.h>])
208                         ], [
209                                 AC_LINK_IFELSE([
210                                         AC_LANG_PROGRAM([[
211                                                 #include <unistd.h>
212                                         ]], [[
213                                                 return sysconf(_SC_PAGE_SIZE);
214                                         ]])
215                                 ], [
216                                         AC_MSG_RESULT([_SC_PAGE_SIZE])
217                                         AC_DEFINE([HAVE__SC_PAGE_SIZE], [1], [Define to 1 if you have the _SC_PAGE_SIZE constant in <unistd.h>, but not _SC_PAGESIZE])
218                                 ], [
219                                         AC_MSG_RESULT([none])
220                                 ])
221                         ])
222                 ])
223         ])