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)
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
26 /* ------------------------ Windows ------------------------ */
28 #define WIN32_LEAN_AND_MEAN /* avoid including junk */
31 #define PROT_NONE PAGE_NOACCESS
32 #define PROT_READ PAGE_READONLY
33 #define PROT_READ_WRITE PAGE_READWRITE
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
))
48 int munmap (void *addr
, unsigned long len
)
50 if (VirtualFree (addr
, len
, MEM_DECOMMIT
))
56 int mprotect (void *addr
, unsigned long len
, int prot
)
60 if (VirtualProtect (addr
, len
, prot
, &oldprot
))
68 /* ------------------------ Unix ------------------------ */
70 #include <sys/types.h>
76 #define PROT_READ_WRITE (PROT_READ|PROT_WRITE)
80 # define map_flags MAP_ANON | MAP_PRIVATE
81 #elif HAVE_MMAP_ANONYMOUS
83 # define map_flags MAP_ANONYMOUS | MAP_PRIVATE
84 #elif HAVE_MMAP_DEVZERO
90 # define map_flags MAP_FILE | MAP_PRIVATE
94 mmap_zeromap (void *map_addr_hint
, unsigned long map_len
)
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);
101 return (void *) mmap (map_addr_hint
, map_len
, PROT_READ_WRITE
, map_flags
, zero_fd
, 0);