annotate tests/zerosize-ptr.h @ 17249:e542fd46ad6f

maint: update all copyright year number ranges Run "make update-copyright". Compare to commit 1602f0a from last year. Signed-off-by: Eric Blake <eblake@redhat.com>
author Eric Blake <eblake@redhat.com>
date Tue, 01 Jan 2013 00:50:58 +0000
parents 8250f2777afc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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.
17249
e542fd46ad6f maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents: 16201
diff changeset
2 Copyright (C) 2009-2013 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
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include <stdlib.h>
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
11580
89e9741f299f Make zeroptr.h work on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11570
diff changeset
23 /* Test whether mmap() and mprotect() are available.
89e9741f299f Make zeroptr.h work on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11570
diff changeset
24 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
25 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
26 mprotect() function in libgcc.a. */
89e9741f299f Make zeroptr.h work on mingw.
Bruno Haible <bruno@clisp.org>
parents: 11570
diff changeset
27 #if HAVE_SYS_MMAN_H && HAVE_MPROTECT
11570
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 # include <fcntl.h>
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 # include <unistd.h>
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 # include <sys/types.h>
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 # include <sys/mman.h>
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 /* Define MAP_FILE when it isn't otherwise. */
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 # ifndef MAP_FILE
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 # define MAP_FILE 0
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 # endif
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 #endif
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37
11598
58b1732d052a Check that memchr does not read past the first occurrence of the byte.
Bruno Haible <bruno@clisp.org>
parents: 11580
diff changeset
38 /* 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
39 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
40 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
41 Return NULL otherwise. */
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 static void *
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 zerosize_ptr (void)
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 {
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 /* 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
47 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
48 #if HAVE_SYS_MMAN_H && HAVE_MPROTECT
11570
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 # if HAVE_MAP_ANONYMOUS
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 const int flags = MAP_ANONYMOUS | MAP_PRIVATE;
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 const int fd = -1;
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 # else /* !HAVE_MAP_ANONYMOUS */
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 const int flags = MAP_FILE | MAP_PRIVATE;
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 int fd = open ("/dev/zero", O_RDONLY, 0666);
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 if (fd >= 0)
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 # endif
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 {
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 int pagesize = getpagesize ();
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 char *two_pages =
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11598
diff changeset
60 (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
61 flags, fd, 0);
11570
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 if (two_pages != (char *)(-1)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11598
diff changeset
63 && mprotect (two_pages + pagesize, pagesize, PROT_NONE) == 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11598
diff changeset
64 return two_pages + pagesize;
11570
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 }
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 #endif
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 return NULL;
1e84e9b3384a Make some tests ISO C 99 compliant.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 }