6 /* malloc might have been renamed as rpl_malloc. */
9 /* Thanks to Mike Haertel and Jim Avera for this test.
10 Here is a matrix of mmap possibilities:
11 mmap private not fixed
12 mmap private fixed at somewhere currently unmapped
13 mmap private fixed at somewhere already mapped
15 mmap shared fixed at somewhere currently unmapped
16 mmap shared fixed at somewhere already mapped
17 For private mappings, we should verify that changes cannot be read()
18 back from the file, nor mmap's back from the file at a different
19 address. (There have been systems where private was not correctly
20 implemented like the infamous i386 svr4.0, and systems where the
21 VM page cache was not coherent with the file system buffer cache
22 like early versions of FreeBSD and possibly contemporary NetBSD.)
23 For shared mappings, we should conversely verify that changes get
24 propagated back to all the places they're supposed to be.
26 Grep wants private fixed already mapped.
27 The main things grep needs to know about mmap are:
28 * does it exist and is it safe to write into the mmap'd area
29 * how to use it (BSD variants) */
34 #if !defined STDC_HEADERS && !defined HAVE_STDLIB_H
38 /* This mess was copied from the GNU getpagesize.h. */
39 #ifndef HAVE_GETPAGESIZE
41 # define getpagesize() sysconf(_SC_PAGESIZE)
42 # else /* no _SC_PAGESIZE */
43 # ifdef HAVE_SYS_PARAM_H
44 # include <sys/param.h>
46 # define getpagesize() EXEC_PAGESIZE
47 # else /* no EXEC_PAGESIZE */
49 # define getpagesize() NBPG * CLSIZE
52 # endif /* no CLSIZE */
55 # define getpagesize() NBPC
58 # define getpagesize() PAGESIZE
59 # endif /* PAGESIZE */
62 # endif /* no EXEC_PAGESIZE */
63 # else /* no HAVE_SYS_PARAM_H */
64 # define getpagesize() 8192 /* punt totally */
65 # endif /* no HAVE_SYS_PARAM_H */
66 # endif /* no _SC_PAGESIZE */
68 #endif /* no HAVE_GETPAGESIZE */
73 char *data
, *data2
, *data3
;
77 pagesize
= getpagesize ();
79 /* First, make a file with some known garbage in it. */
80 data
= (char *) malloc (pagesize
);
83 for (i
= 0; i
< pagesize
; ++i
)
84 *(data
+ i
) = rand ();
86 fd
= creat ("conftest.mmap", 0600);
89 if (write (fd
, data
, pagesize
) != pagesize
)
93 /* Next, check that the tail of a page is zero-filled. File must have
94 non-zero length, otherwise we risk SIGBUS for entire page. */
95 fd2
= open ("conftest.txt", O_RDWR
| O_CREAT
| O_TRUNC
, 0600);
99 if (write (fd2
, data2
, 1) != 1)
101 data2
= (char *) mmap (0, pagesize
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd2
, 0L);
102 if (data2
== MAP_FAILED
)
104 for (i
= 0; i
< pagesize
; ++i
)
108 if (munmap (data2
, pagesize
))
111 /* Next, try to mmap the file at a fixed address which already has
112 something else allocated at it. If we can, also make sure that
113 we see the same garbage. */
114 fd
= open ("conftest.mmap", O_RDWR
);
117 if (data2
!= mmap (data2
, pagesize
, PROT_READ
| PROT_WRITE
,
118 MAP_PRIVATE
| MAP_FIXED
, fd
, 0L))
120 for (i
= 0; i
< pagesize
; ++i
)
121 if (*(data
+ i
) != *(data2
+ i
))
124 /* Finally, make sure that changes to the mapped area do not
125 percolate back to the file as seen by read(). (This is a bug on
126 some variants of i386 svr4.0.) */
127 for (i
= 0; i
< pagesize
; ++i
)
128 *(data2
+ i
) = *(data2
+ i
) + 1;
129 data3
= (char *) malloc (pagesize
);
132 if (read (fd
, data3
, pagesize
) != pagesize
)
134 for (i
= 0; i
< pagesize
; ++i
)
135 if (*(data
+ i
) != *(data3
+ i
))