1 /* Return a pointer to a zero-size object in memory.
2 Copyright (C) 2009 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* ISO C 99 does not allow memcmp(), memchr() etc. to be invoked with a NULL
18 argument. Therefore this file produces a non-NULL pointer which cannot
19 be dereferenced, if possible. */
26 # include <sys/types.h>
27 # include <sys/mman.h>
28 /* Define MAP_FILE when it isn't otherwise. */
34 /* Return a pointer to a zero-size object in memory, if possible.
35 Return NULL otherwise. */
40 /* Use mmap and mprotect when they exist. Don't test HAVE_MMAP, because it is
41 not defined on HP-UX 11 (since it does not support MAP_FIXED). */
43 # if HAVE_MAP_ANONYMOUS
44 const int flags = MAP_ANONYMOUS | MAP_PRIVATE;
46 # else /* !HAVE_MAP_ANONYMOUS */
47 const int flags = MAP_FILE | MAP_PRIVATE;
48 int fd = open ("/dev/zero", O_RDONLY, 0666);
52 int pagesize = getpagesize ();
54 (char *) mmap (NULL, 2 * pagesize, PROT_READ | PROT_WRITE,
56 if (two_pages != (char *)(-1)
57 && mprotect (two_pages + pagesize, pagesize, PROT_NONE) == 0)
58 return two_pages + pagesize;