Bump version number.
[libsigsegv/ericb.git] / tests / mmaputil.h
blob707b34c526b834854f9c718821fba12bb9506983
1 /* Some auxiliary stuff for using mmap & friends.
2 Copyright (C) 2002-2003 Bruno Haible <bruno@clisp.org>
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 2, or (at your option)
7 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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 #if defined _WIN32 && !defined __CYGWIN__
19 # define HAVE_WIN32_VM
20 #else
21 # include "config.h"
22 #endif
24 #ifdef HAVE_WIN32_VM
26 /* ------------------------ Windows ------------------------ */
28 #define WIN32_LEAN_AND_MEAN /* avoid including junk */
29 #include <windows.h>
30 #include <winerror.h>
31 #define PROT_NONE PAGE_NOACCESS
32 #define PROT_READ PAGE_READONLY
33 #define PROT_READ_WRITE PAGE_READWRITE
35 static void *
36 mmap_zeromap (void *map_addr_hint, unsigned long map_len)
38 if (VirtualAlloc ((void *)((unsigned long) map_addr_hint & -0x10000),
39 (((unsigned long) map_addr_hint + map_len - 1) | 0xffff) + 1
40 - ((unsigned long) map_addr_hint & -0x10000),
41 MEM_RESERVE, PAGE_NOACCESS)
42 && VirtualAlloc (map_addr_hint, map_len, MEM_COMMIT, PAGE_READWRITE))
43 return map_addr_hint;
44 else
45 return (void *)(-1);
48 int munmap (void *addr, unsigned long len)
50 if (VirtualFree (addr, len, MEM_DECOMMIT))
51 return 0;
52 else
53 return -1;
56 int mprotect (void *addr, unsigned long len, int prot)
58 DWORD oldprot;
60 if (VirtualProtect (addr, len, prot, &oldprot))
61 return 0;
62 else
63 return -1;
66 #else
68 /* ------------------------ Unix ------------------------ */
70 #include <sys/types.h>
71 #include <sys/mman.h>
73 #ifndef PROT_NONE
74 # define PROT_NONE 0
75 #endif
76 #define PROT_READ_WRITE (PROT_READ|PROT_WRITE)
78 #if HAVE_MMAP_ANON
79 # define zero_fd -1
80 # define map_flags MAP_ANON | MAP_PRIVATE
81 #elif HAVE_MMAP_ANONYMOUS
82 # define zero_fd -1
83 # define map_flags MAP_ANONYMOUS | MAP_PRIVATE
84 #elif HAVE_MMAP_DEVZERO
85 # include <fcntl.h>
86 # ifndef MAP_FILE
87 # define MAP_FILE 0
88 # endif
89 static int zero_fd;
90 # define map_flags MAP_FILE | MAP_PRIVATE
91 #endif
93 static void *
94 mmap_zeromap (void *map_addr_hint, unsigned long map_len)
96 #ifdef __hpux
97 /* HP-UX 10 mmap() often fails when given a hint. So give the OS complete
98 freedom about the address range. */
99 return (void *) mmap ((void *) 0, map_len, PROT_READ_WRITE, map_flags, zero_fd, 0);
100 #else
101 return (void *) mmap (map_addr_hint, map_len, PROT_READ_WRITE, map_flags, zero_fd, 0);
102 #endif
105 #endif