Mercurial > gnulib
annotate tests/zerosize-ptr.h @ 40186:8964917f9574
autoupdate
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Mon, 18 Feb 2019 08:02:49 -0800 |
parents | b06060465f09 |
children |
rev | line source |
---|---|
11570
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
1 /* Return a pointer to a zero-size object in memory. |
40057
b06060465f09
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
19661
diff
changeset
|
2 Copyright (C) 2009-2019 Free Software Foundation, Inc. |
11570
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
3 |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
4 This program is free software: you can redistribute it and/or modify |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
5 it under the terms of the GNU General Public License as published by |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
6 the Free Software Foundation; either version 3 of the License, or |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
7 (at your option) any later version. |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
8 |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
9 This program is distributed in the hope that it will be useful, |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
12 GNU General Public License for more details. |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
13 |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
14 You should have received a copy of the GNU General Public License |
19190 | 15 along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
11570
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
16 |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
17 /* ISO C 99 does not allow memcmp(), memchr() etc. to be invoked with a NULL |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
18 argument. Therefore this file produces a non-NULL pointer which cannot |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
19 be dereferenced, if possible. */ |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
20 |
19661
bd78788aeebd
Avoid compilation error due to 'mmap' on Android.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
21 /* On Android, when targeting Android 4.4 or older with a GCC toolchain, |
bd78788aeebd
Avoid compilation error due to 'mmap' on Android.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
22 prevent a compilation error |
bd78788aeebd
Avoid compilation error due to 'mmap' on Android.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
23 "error: call to 'mmap' declared with attribute error: mmap is not |
bd78788aeebd
Avoid compilation error due to 'mmap' on Android.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
24 available with _FILE_OFFSET_BITS=64 when using GCC until android-21. |
bd78788aeebd
Avoid compilation error due to 'mmap' on Android.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
25 Either raise your minSdkVersion, disable _FILE_OFFSET_BITS=64, or |
bd78788aeebd
Avoid compilation error due to 'mmap' on Android.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
26 switch to Clang." |
bd78788aeebd
Avoid compilation error due to 'mmap' on Android.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
27 The files that we access in this compilation unit are less than 2 GB |
bd78788aeebd
Avoid compilation error due to 'mmap' on Android.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
28 large. */ |
bd78788aeebd
Avoid compilation error due to 'mmap' on Android.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
29 #if defined __ANDROID__ |
bd78788aeebd
Avoid compilation error due to 'mmap' on Android.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
30 # undef _FILE_OFFSET_BITS |
bd78788aeebd
Avoid compilation error due to 'mmap' on Android.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
31 # undef __USE_FILE_OFFSET64 |
bd78788aeebd
Avoid compilation error due to 'mmap' on Android.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
32 #endif |
bd78788aeebd
Avoid compilation error due to 'mmap' on Android.
Bruno Haible <bruno@clisp.org>
parents:
19484
diff
changeset
|
33 |
11570
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
34 #include <stdlib.h> |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
35 |
11580
89e9741f299f
Make zeroptr.h work on mingw.
Bruno Haible <bruno@clisp.org>
parents:
11570
diff
changeset
|
36 /* Test whether mmap() and mprotect() are available. |
89e9741f299f
Make zeroptr.h work on mingw.
Bruno Haible <bruno@clisp.org>
parents:
11570
diff
changeset
|
37 We don't use HAVE_MMAP, because AC_FUNC_MMAP would not define it on HP-UX. |
89e9741f299f
Make zeroptr.h work on mingw.
Bruno Haible <bruno@clisp.org>
parents:
11570
diff
changeset
|
38 HAVE_MPROTECT is not enough, because mingw does not have mmap() but has an |
89e9741f299f
Make zeroptr.h work on mingw.
Bruno Haible <bruno@clisp.org>
parents:
11570
diff
changeset
|
39 mprotect() function in libgcc.a. */ |
89e9741f299f
Make zeroptr.h work on mingw.
Bruno Haible <bruno@clisp.org>
parents:
11570
diff
changeset
|
40 #if HAVE_SYS_MMAN_H && HAVE_MPROTECT |
11570
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
41 # include <fcntl.h> |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
42 # include <unistd.h> |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
43 # include <sys/types.h> |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
44 # include <sys/mman.h> |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
45 /* Define MAP_FILE when it isn't otherwise. */ |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
46 # ifndef MAP_FILE |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
47 # define MAP_FILE 0 |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
48 # endif |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
49 #endif |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
50 |
11598
58b1732d052a
Check that memchr does not read past the first occurrence of the byte.
Bruno Haible <bruno@clisp.org>
parents:
11580
diff
changeset
|
51 /* Return a pointer to a zero-size object in memory (that is, actually, a |
58b1732d052a
Check that memchr does not read past the first occurrence of the byte.
Bruno Haible <bruno@clisp.org>
parents:
11580
diff
changeset
|
52 pointer to a page boundary where the previous page is readable and writable |
58b1732d052a
Check that memchr does not read past the first occurrence of the byte.
Bruno Haible <bruno@clisp.org>
parents:
11580
diff
changeset
|
53 and the next page is neither readable not writable), if possible. |
11570
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
54 Return NULL otherwise. */ |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
55 |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
56 static void * |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
57 zerosize_ptr (void) |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
58 { |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
59 /* Use mmap and mprotect when they exist. Don't test HAVE_MMAP, because it is |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
60 not defined on HP-UX 11 (since it does not support MAP_FIXED). */ |
11580
89e9741f299f
Make zeroptr.h work on mingw.
Bruno Haible <bruno@clisp.org>
parents:
11570
diff
changeset
|
61 #if HAVE_SYS_MMAN_H && HAVE_MPROTECT |
11570
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
62 # if HAVE_MAP_ANONYMOUS |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
63 const int flags = MAP_ANONYMOUS | MAP_PRIVATE; |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
64 const int fd = -1; |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
65 # else /* !HAVE_MAP_ANONYMOUS */ |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
66 const int flags = MAP_FILE | MAP_PRIVATE; |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
67 int fd = open ("/dev/zero", O_RDONLY, 0666); |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
68 if (fd >= 0) |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
69 # endif |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
70 { |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
71 int pagesize = getpagesize (); |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
72 char *two_pages = |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11598
diff
changeset
|
73 (char *) mmap (NULL, 2 * pagesize, PROT_READ | PROT_WRITE, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11598
diff
changeset
|
74 flags, fd, 0); |
11570
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
75 if (two_pages != (char *)(-1) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11598
diff
changeset
|
76 && mprotect (two_pages + pagesize, pagesize, PROT_NONE) == 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11598
diff
changeset
|
77 return two_pages + pagesize; |
11570
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
78 } |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
79 #endif |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
80 return NULL; |
1e84e9b3384a
Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
81 } |