|
22 | 22 | #include "mono-proclib.h" |
23 | 23 | #include <mono/utils/mono-threads.h> |
24 | 24 | #include <mono/utils/atomic.h> |
| 25 | +#include <mono/utils/options.h> |
| 26 | + |
| 27 | +#include "mono-wasm-pagemgr.h" |
25 | 28 |
|
26 | 29 | #define BEGIN_CRITICAL_SECTION do { \ |
27 | 30 | MonoThreadInfo *__info = mono_thread_info_current_unchecked (); \ |
|
34 | 37 | int |
35 | 38 | mono_pagesize (void) |
36 | 39 | { |
| 40 | + if (mono_opt_wasm_mmap) |
| 41 | + return MWPM_PAGE_SIZE; |
| 42 | + |
37 | 43 | static int saved_pagesize = 0; |
38 | 44 |
|
39 | 45 | if (saved_pagesize) |
@@ -108,7 +114,16 @@ valloc_impl (void *addr, size_t size, int flags, MonoMemAccountType type) |
108 | 114 | mflags |= MAP_PRIVATE; |
109 | 115 |
|
110 | 116 | BEGIN_CRITICAL_SECTION; |
111 | | - ptr = mmap (addr, size, prot, mflags, -1, 0); |
| 117 | + if (mono_opt_wasm_mmap) { |
| 118 | + // FIXME: Make this work if the requested address range is free |
| 119 | + if ((flags & MONO_MMAP_FIXED) && addr) |
| 120 | + return NULL; |
| 121 | + |
| 122 | + ptr = mwpm_alloc_range (size, 1); |
| 123 | + if (!ptr) |
| 124 | + return NULL; |
| 125 | + } else |
| 126 | + ptr = mmap (addr, size, prot, mflags, -1, 0); |
112 | 127 | END_CRITICAL_SECTION; |
113 | 128 |
|
114 | 129 | if (ptr == MAP_FAILED) |
@@ -142,6 +157,10 @@ typedef struct { |
142 | 157 | void* |
143 | 158 | mono_valloc_aligned (size_t size, size_t alignment, int flags, MonoMemAccountType type) |
144 | 159 | { |
| 160 | + // We don't need padding if the alignment is compatible with the page size |
| 161 | + if (mono_opt_wasm_mmap && ((MWPM_PAGE_SIZE % alignment) == 0)) |
| 162 | + return valloc_impl (NULL, size, flags, type); |
| 163 | + |
145 | 164 | /* Allocate twice the memory to be able to put the block on an aligned address */ |
146 | 165 | char *mem = (char *) valloc_impl (NULL, size + alignment, flags, type); |
147 | 166 | char *aligned; |
@@ -175,13 +194,22 @@ mono_vfree (void *addr, size_t length, MonoMemAccountType type) |
175 | 194 | * mono_valloc_align (), free the original mapping. |
176 | 195 | */ |
177 | 196 | BEGIN_CRITICAL_SECTION; |
178 | | - munmap (info->addr, info->size); |
| 197 | + if (mono_opt_wasm_mmap) |
| 198 | + mwpm_free_range (info->addr, info->size); |
| 199 | + else |
| 200 | + munmap (info->addr, info->size); |
179 | 201 | END_CRITICAL_SECTION; |
180 | 202 | g_free (info); |
181 | 203 | g_hash_table_remove (valloc_hash, addr); |
182 | 204 | } else { |
| 205 | + // FIXME: We could be trying to unmap part of an aligned mapping, in which case the |
| 206 | + // hash lookup failed because addr isn't exactly the start of the mapping. |
| 207 | + // Ideally if the custom page manager is enabled, we won't have done aligned alloc. |
183 | 208 | BEGIN_CRITICAL_SECTION; |
184 | | - munmap (addr, length); |
| 209 | + if (mono_opt_wasm_mmap) |
| 210 | + mwpm_free_range (addr, length); |
| 211 | + else |
| 212 | + munmap (addr, length); |
185 | 213 | END_CRITICAL_SECTION; |
186 | 214 | } |
187 | 215 |
|
|
0 commit comments