Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : *
3 : * Permission is hereby granted, free of charge, to any person obtaining
4 : * a copy of this software and associated documentation files (the
5 : * "Software"), to deal in the Software without restriction, including
6 : * without limitation the rights to use, copy, modify, merge, publish,
7 : * distribute, sublicense, and/or sell copies of the Software, and to
8 : * permit persons to whom the Software is furnished to do so, subject to
9 : * the following conditions:
10 : *
11 : * The above copyright notice and this permission notice shall be included
12 : * in all copies or substantial portions of the Software.
13 : *
14 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 : * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 : */
22 :
23 : #include "precompiled.h"
24 :
25 : #include "lib/sysdep/vm.h"
26 :
27 : #include "lib/debug.h"
28 : #include "lib/posix/posix_mman.h"
29 : #include "lib/status.h"
30 :
31 : #include <cerrno>
32 : #include <cstddef>
33 : #include <cstdint>
34 :
35 : // "anonymous" effectively means mapping /dev/zero, but is more efficient.
36 : // MAP_ANONYMOUS is not in SUSv3, but is a very common extension.
37 : // unfortunately, MacOS X only defines MAP_ANON, which Solaris says is
38 : // deprecated. workaround there: define MAP_ANONYMOUS in terms of MAP_ANON.
39 : #ifndef MAP_ANONYMOUS
40 : # define MAP_ANONYMOUS MAP_ANON
41 : #endif
42 :
43 : static const int mmap_flags = MAP_PRIVATE|MAP_ANONYMOUS;
44 :
45 : namespace vm {
46 :
47 1 : void* ReserveAddressSpace(size_t size, size_t /*commitSize*/, PageType /*pageType*/, int /*prot*/)
48 : {
49 1 : errno = 0;
50 1 : void* p = mmap(0, size, PROT_NONE, mmap_flags|MAP_NORESERVE, -1, 0);
51 1 : if(p == MAP_FAILED)
52 0 : return 0;
53 : return p;
54 : }
55 :
56 1 : void ReleaseAddressSpace(void* p, size_t size)
57 : {
58 1 : ENSURE(size != 0);
59 :
60 1 : errno = 0;
61 1 : if(munmap(p, size) != 0)
62 0 : DEBUG_WARN_ERR(StatusFromErrno());
63 1 : }
64 :
65 :
66 1 : bool Commit(uintptr_t address, size_t size, PageType, int prot)
67 : {
68 1 : if(prot == PROT_NONE) // would be understood as a request to decommit
69 : {
70 0 : DEBUG_WARN_ERR(ERR::INVALID_PARAM);
71 0 : return false;
72 : }
73 :
74 1 : errno = 0;
75 1 : if(mmap((void*)address, size, prot, mmap_flags|MAP_FIXED, -1, 0) == MAP_FAILED)
76 : return false;
77 :
78 1 : if(prot != (PROT_READ|PROT_WRITE))
79 0 : (void)Protect(address, size, prot);
80 :
81 : return true;
82 : }
83 :
84 0 : bool Decommit(uintptr_t address, size_t size)
85 : {
86 0 : errno = 0;
87 0 : if(mmap((void*)address, size, PROT_NONE, mmap_flags|MAP_NORESERVE|MAP_FIXED, -1, 0) == MAP_FAILED)
88 0 : return false;
89 : return true;
90 : }
91 :
92 :
93 0 : bool Protect(uintptr_t address, size_t size, int prot)
94 : {
95 0 : errno = 0;
96 0 : if(mprotect((void*)address, size, prot) != 0)
97 : {
98 0 : DEBUG_WARN_ERR(ERR::FAIL);
99 0 : return false;
100 : }
101 : return true;
102 : }
103 :
104 :
105 0 : void* Allocate(size_t size, PageType pageType, int prot)
106 : {
107 0 : void* p = ReserveAddressSpace(size);
108 0 : if(!p)
109 : return 0;
110 :
111 0 : if(!Commit(uintptr_t(p), size, pageType, prot))
112 : {
113 0 : ReleaseAddressSpace(p, size);
114 0 : return 0;
115 : }
116 :
117 : return p;
118 : }
119 :
120 0 : void Free(void* p, size_t size)
121 : {
122 : // (only the Windows implementation distinguishes between Free and ReleaseAddressSpace)
123 0 : vm::ReleaseAddressSpace(p, size);
124 0 : }
125 :
126 :
127 0 : void BeginOnDemandCommits()
128 : {
129 : // not yet implemented, but possible with a signal handler
130 0 : }
131 :
132 0 : void EndOnDemandCommits()
133 : {
134 : // not yet implemented, but possible with a signal handler
135 0 : }
136 :
137 : } // namespace vm
|