1 /* Some auxiliary stuff for using mmap & friends.
2 Copyright (C) 2002-2024 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 2 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 <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible. */
19 #if defined _WIN32 && !defined __CYGWIN__
21 /* ------------------------ Windows ------------------------ */
23 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
25 # include <winerror.h>
26 # define PROT_NONE PAGE_NOACCESS
27 # define PROT_READ PAGE_READONLY
28 # define PROT_READ_WRITE PAGE_READWRITE
31 mmap_zeromap (void *map_addr_hint
, size_t map_len
)
33 if (VirtualAlloc ((void *)((uintptr_t) map_addr_hint
& -0x10000),
34 (((uintptr_t) map_addr_hint
+ map_len
- 1) | 0xffff) + 1
35 - ((uintptr_t) map_addr_hint
& -0x10000),
36 MEM_RESERVE
, PAGE_NOACCESS
)
37 && VirtualAlloc (map_addr_hint
, map_len
, MEM_COMMIT
, PAGE_READWRITE
))
44 munmap (void *addr
, size_t len
)
46 if (VirtualFree (addr
, len
, MEM_DECOMMIT
))
53 mprotect (void *addr
, size_t len
, int prot
)
57 if (VirtualProtect (addr
, len
, prot
, &oldprot
))
65 /* ------------------------ Unix ------------------------ */
67 # include <sys/types.h>
68 # include <sys/mman.h>
74 # define PROT_READ_WRITE (PROT_READ|PROT_WRITE)
76 # if HAVE_MAP_ANONYMOUS
78 # define map_flags MAP_ANONYMOUS | MAP_PRIVATE
84 # define map_flags MAP_FILE | MAP_PRIVATE
88 mmap_zeromap (void *map_addr_hint
, size_t map_len
)
91 /* HP-UX 10 mmap() often fails when given a hint. So give the OS complete
92 freedom about the address range. */
93 return (void *) mmap ((void *) 0, map_len
, PROT_READ_WRITE
, map_flags
, zero_fd
, 0);
95 return (void *) mmap (map_addr_hint
, map_len
, PROT_READ_WRITE
, map_flags
, zero_fd
, 0);