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
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 ---------------------------------------------------------------------------
29 dnl This function is copied over from autoconf sources, but fixed to work with
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. */
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
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) */
65 #if !defined STDC_HEADERS && !defined HAVE_STDLIB_H
69 /* This mess was copied from the GNU getpagesize.h. */
70 #ifndef HAVE_GETPAGESIZE
72 # define getpagesize() sysconf(_SC_PAGESIZE)
73 # else /* no _SC_PAGESIZE */
74 # ifdef HAVE_SYS_PARAM_H
75 # include <sys/param.h>
77 # define getpagesize() EXEC_PAGESIZE
78 # else /* no EXEC_PAGESIZE */
80 # define getpagesize() NBPG * CLSIZE
83 # endif /* no CLSIZE */
86 # define getpagesize() NBPC
89 # define getpagesize() PAGESIZE
90 # endif /* PAGESIZE */
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 */
104 char *data, *data2, *data3;
108 pagesize = getpagesize ();
110 /* First, make a file with some known garbage in it. */
111 data = (char *) malloc (pagesize);
114 for (i = 0; i < pagesize; ++i)
115 *(data + i) = rand ();
117 fd = creat ("conftest.mmap", 0600);
120 if (write (fd, data, pagesize) != pagesize)
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);
130 if (write (fd2, data2, 1) != 1)
132 data2 = (char *) mmap (0, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd2, 0L);
133 if (data2 == MAP_FAILED)
135 for (i = 0; i < pagesize; ++i)
139 if (munmap (data2, pagesize))
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);
148 if (data2 != mmap (data2, pagesize, PROT_READ | PROT_WRITE,
149 MAP_PRIVATE | MAP_FIXED, fd, 0L))
151 for (i = 0; i < pagesize; ++i)
152 if (*(data + i) != *(data2 + i))
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);
163 if (read (fd, data3, pagesize) != pagesize)
165 for (i = 0; i < pagesize; ++i)
166 if (*(data + i) != *(data3 + i))
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 ---------------------------------------------------------------------------
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])
196 AC_CHECK_FUNCS([munmap sysconf])
197 AS_IF([test $ac_cv_func_sysconf = yes], [
198 AC_MSG_CHECKING([for pagesize constant for sysconf])
203 return sysconf(_SC_PAGESIZE);
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>])
213 return sysconf(_SC_PAGE_SIZE);
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])
219 AC_MSG_RESULT([none])