Debian Codegraph @

src/prim/wasi/prim.c

raw
1 /* ----------------------------------------------------------------------------
2 Copyright (c) 2018-2023, Microsoft Research, Daan Leijen
3 This is free software; you can redistribute it and/or modify it under the
4 terms of the MIT license. A copy of the license can be found in the file
5 "LICENSE" at the root of this distribution.
6 -----------------------------------------------------------------------------*/
7
8 // This file is included in `src/prim/prim.c`
9
10 #include "mimalloc.h"
11 #include "mimalloc/internal.h"
12 #include "mimalloc/prim.h"
13
14 #include <stdio.h> // fputs
15 #include <stdlib.h> // getenv
16 #include <unistd.h> // sbrk, sleep
17
18 //---------------------------------------------
19 // Initialize
20 //---------------------------------------------
21
22 void _mi_prim_mem_init( mi_os_mem_config_t* config ) {
23 config->page_size = 64*MI_KiB; // WebAssembly has a fixed page size: 64KiB
24 config->alloc_granularity = 16;
25 config->has_overcommit = false;
26 config->has_partial_free = false;
27 config->has_virtual_reserve = false;
28 }
29
30 //---------------------------------------------
31 // Free
32 //---------------------------------------------
33
34 int _mi_prim_free(void* addr, size_t size ) {
35 MI_UNUSED(addr); MI_UNUSED(size);
36 // wasi theap cannot be shrunk
37 return 0;
38 }
39
40
41 //---------------------------------------------
42 // Allocation: sbrk or memory_grow
43 //---------------------------------------------
44
45 #if defined(MI_USE_SBRK)
46 static void* mi_memory_grow( size_t size ) {
47 void* p = sbrk(size);
48 if (p == (void*)(-1)) return NULL;
49 #if !defined(__wasi__) // on wasi this is always zero initialized already (?)
50 memset(p,0,size);
51 #endif
52 return p;
53 }
54 #elif defined(__wasi__)
55 static void* mi_memory_grow( size_t size ) {
56 size_t base = (size > 0 ? __builtin_wasm_memory_grow(0,_mi_divide_up(size, _mi_os_page_size()))
57 : __builtin_wasm_memory_size(0));
58 if (base == SIZE_MAX) return NULL;
59 return (void*)(base * _mi_os_page_size());
60 }
61 #endif
62
63 #if defined(MI_USE_PTHREADS)
64 static pthread_mutex_t mi_theap_grow_mutex = PTHREAD_MUTEX_INITIALIZER;
65 #endif
66
67 static void* mi_prim_mem_grow(size_t size, size_t try_alignment) {
68 void* p = NULL;
69 if (try_alignment <= 1) {
70 // `sbrk` is not thread safe in general so try to protect it (we could skip this on WASM but leave it in for now)
71 #if defined(MI_USE_PTHREADS)
72 pthread_mutex_lock(&mi_theap_grow_mutex);
73 #endif
74 p = mi_memory_grow(size);
75 #if defined(MI_USE_PTHREADS)
76 pthread_mutex_unlock(&mi_theap_grow_mutex);
77 #endif
78 }
79 else {
80 void* base = NULL;
81 size_t alloc_size = 0;
82 // to allocate aligned use a lock to try to avoid thread interaction
83 // between getting the current size and actual allocation
84 // (also, `sbrk` is not thread safe in general)
85 #if defined(MI_USE_PTHREADS)
86 pthread_mutex_lock(&mi_theap_grow_mutex);
87 #endif
88 {
89 void* current = mi_memory_grow(0); // get current size
90 if (current != NULL) {
91 void* aligned_current = _mi_align_up_ptr(current, try_alignment); // and align from there to minimize wasted space
92 alloc_size = _mi_align_up( ((uint8_t*)aligned_current - (uint8_t*)current) + size, _mi_os_page_size());
93 base = mi_memory_grow(alloc_size);
94 }
95 }
96 #if defined(MI_USE_PTHREADS)
97 pthread_mutex_unlock(&mi_theap_grow_mutex);
98 #endif
99 if (base != NULL) {
100 p = _mi_align_up_ptr(base, try_alignment);
101 if ((uint8_t*)p + size > (uint8_t*)base + alloc_size) {
102 // another thread used wasm_memory_grow/sbrk in-between and we do not have enough
103 // space after alignment. Give up (and waste the space as we cannot shrink :-( )
104 // (in `mi_os_mem_alloc_aligned` this will fall back to overallocation to align)
105 p = NULL;
106 }
107 }
108 }
109 /*
110 if (p == NULL) {
111 _mi_warning_message("unable to allocate sbrk/wasm_memory_grow OS memory (%zu bytes, %zu alignment)\n", size, try_alignment);
112 errno = ENOMEM;
113 return NULL;
114 }
115 */
116 mi_assert_internal( p == NULL || try_alignment == 0 || (uintptr_t)p % try_alignment == 0 );
117 return p;
118 }
119
120 // Note: the `try_alignment` is just a hint and the returned pointer is not guaranteed to be aligned.
121 int _mi_prim_alloc(void* hint_addr, size_t size, size_t try_alignment, bool commit, bool allow_large, bool* is_large, bool* is_zero, void** addr) {
122 MI_UNUSED(allow_large); MI_UNUSED(commit); MI_UNUSED(hint_addr);
123 *is_large = false;
124 *is_zero = false;
125 *addr = mi_prim_mem_grow(size, try_alignment);
126 return (*addr != NULL ? 0 : ENOMEM);
127 }
128
129
130 //---------------------------------------------
131 // Commit/Reset/Protect
132 //---------------------------------------------
133
134 int _mi_prim_commit(void* addr, size_t size, bool* is_zero) {
135 MI_UNUSED(addr); MI_UNUSED(size);
136 *is_zero = false;
137 return 0;
138 }
139
140 int _mi_prim_decommit(void* addr, size_t size, bool* needs_recommit) {
141 MI_UNUSED(addr); MI_UNUSED(size);
142 *needs_recommit = false;
143 return 0;
144 }
145
146 int _mi_prim_reset(void* addr, size_t size) {
147 MI_UNUSED(addr); MI_UNUSED(size);
148 return 0;
149 }
150
151 int _mi_prim_reuse(void* addr, size_t size) {
152 MI_UNUSED(addr); MI_UNUSED(size);
153 return 0;
154 }
155
156 int _mi_prim_protect(void* addr, size_t size, bool protect) {
157 MI_UNUSED(addr); MI_UNUSED(size); MI_UNUSED(protect);
158 return 0;
159 }
160
161
162 //---------------------------------------------
163 // Huge pages and NUMA nodes
164 //---------------------------------------------
165
166 int _mi_prim_alloc_huge_os_pages(void* hint_addr, size_t size, int numa_node, bool* is_zero, void** addr) {
167 MI_UNUSED(hint_addr); MI_UNUSED(size); MI_UNUSED(numa_node);
168 *is_zero = true;
169 *addr = NULL;
170 return ENOSYS;
171 }
172
173 size_t _mi_prim_numa_node(void) {
174 return 0;
175 }
176
177 size_t _mi_prim_numa_node_count(void) {
178 return 1;
179 }
180
181
182 //----------------------------------------------------------------
183 // Clock
184 //----------------------------------------------------------------
185
186 #include <time.h>
187
188 // low resolution timer
189 static mi_msecs_t mi_prim_clock_now_lowres(void) {
190 const int64_t ticks = (int64_t)clock();
191 #if !defined(CLOCKS_PER_SEC)
192 return ticks;
193 #else
194 if (CLOCKS_PER_SEC <= 0 || CLOCKS_PER_SEC == 1000) {
195 return ticks;
196 }
197 else if (CLOCKS_PER_SEC > 0 && CLOCKS_PER_SEC < 1000) {
198 return ticks * (1000 / (mi_msecs_t)CLOCKS_PER_SEC);
199 }
200 else {
201 return ticks / ((mi_msecs_t)CLOCKS_PER_SEC / 1000);
202 }
203 #endif
204 }
205
206 mi_msecs_t _mi_prim_clock_now(void) {
207 #if defined(CLOCK_REALTIME) || defined(CLOCK_MONOTONIC)
208 #ifdef CLOCK_MONOTONIC
209 const clockid_t clockid = CLOCK_MONOTONIC;
210 #else
211 const clockid_t clockid = CLOCK_REALTIME;
212 #endif
213 struct timespec t;
214 if (clock_gettime(clockid,&t) == 0) {
215 return ((mi_msecs_t)t.tv_sec * 1000) + ((mi_msecs_t)t.tv_nsec / 1000000L);
216 }
217 #endif
218 return mi_prim_clock_now_lowres();
219 }
220
221
222 //----------------------------------------------------------------
223 // Process info
224 //----------------------------------------------------------------
225
226 void _mi_prim_process_info(mi_process_info_t* pinfo)
227 {
228 // use defaults
229 MI_UNUSED(pinfo);
230 }
231
232
233 //----------------------------------------------------------------
234 // Output
235 //----------------------------------------------------------------
236
237 void _mi_prim_out_stderr( const char* msg ) {
238 fputs(msg,stderr);
239 }
240
241
242 //----------------------------------------------------------------
243 // Environment
244 //----------------------------------------------------------------
245
246 int _mi_prim_getenv(const char* name, char* result, size_t result_size) {
247 // cannot call getenv() when still initializing the C runtime.
248 if (_mi_preloading()) return -1; // error, try again later
249 const char* s = getenv(name);
250 if (s == NULL) {
251 // we check the upper case name too.
252 char buf[64+1];
253 size_t len = _mi_strnlen(name,sizeof(buf)-1);
254 for (size_t i = 0; i < len; i++) {
255 buf[i] = _mi_toupper(name[i]);
256 }
257 buf[len] = 0;
258 s = getenv(buf);
259 }
260 if (s == NULL || _mi_strnlen(s,result_size) >= result_size) return 0; // not found
261 _mi_strlcpy(result, s, result_size);
262 return 1; // found
263 }
264
265
266 //----------------------------------------------------------------
267 // Random
268 //----------------------------------------------------------------
269
270 bool _mi_prim_random_buf(void* buf, size_t buf_len) {
271 return false;
272 }
273
274
275 //----------------------------------------------------------------
276 // Thread init/done
277 //----------------------------------------------------------------
278
279 void _mi_prim_thread_init_auto_done(void) {
280 // nothing
281 }
282
283 void _mi_prim_thread_done_auto_done(void) {
284 // nothing
285 }
286
287 void _mi_prim_thread_associate_default_theap(mi_theap_t* theap) {
288 MI_UNUSED(theap);
289 }
290
291 bool _mi_prim_thread_is_in_threadpool(void) {
292 return false;
293 }
294
295 void _mi_prim_thread_yield(void) {
296 sleep(0);
297 }