4
4
#include " ds/flaglock.h"
5
5
#include " pal_plain.h"
6
6
7
- #include < array>
8
7
#ifdef WASM_ENV // wasi-libc/libc-bottom-half/headers/public/__header_*
8
+
9
+ #include < stdio.h>
10
+ #include < unistd.h>
11
+ #include < array>
12
+
9
13
extern " C" void *memset (void *dst, int c, size_t n);
10
14
extern " C" [[noreturn]] void w_abort ();
11
15
@@ -14,26 +18,11 @@ namespace snmalloc
14
18
{
15
19
class PALWASI
16
20
{
17
- // / Base of wlm heap
18
- static inline void * heap_base = nullptr ;
19
-
20
- // / Size of heap
21
- static inline size_t heap_size;
22
-
23
21
// This is infrequently used code, a spin lock simplifies the code
24
22
// considerably, and should never be on the fast path.
25
23
static inline std::atomic_flag spin_lock;
26
24
27
25
public:
28
- /* *
29
- * This will be called by oe_allocator_init to set up wasm sandbox heap bounds.
30
- */
31
- static void setup_initial_range (void * base, void * end)
32
- {
33
- heap_size = pointer_diff (base, end);
34
- heap_base = base;
35
- }
36
-
37
26
/* *
38
27
* Bitmap of PalFeatures flags indicating the optional features that this
39
28
* PAL supports.
@@ -44,22 +33,20 @@ namespace snmalloc
44
33
45
34
[[noreturn]] static void error (const char * const str)
46
35
{
47
- UNUSED ( str);
48
- w_abort ();
36
+ fprintf (stderr, " %s \n " , str);
37
+ abort ();
49
38
}
50
39
51
40
static std::pair<void *, size_t >
52
41
reserve_at_least (size_t request_size) noexcept
53
42
{
54
- // First call returns the entire address space
55
- // subsequent calls return {nullptr, 0}
56
43
FlagLock lock (spin_lock);
57
- if (request_size > heap_size)
44
+ intptr_t actual_size = ((request_size + PAGESIZE - 1 ) / PAGESIZE) * PAGESIZE;
45
+ void *start = sbrk (actual_size);
46
+ if (start == (void *)-1 )
58
47
return {nullptr , 0 };
59
48
60
- auto result = std::make_pair (heap_base, heap_size);
61
- heap_size = 0 ;
62
- return result;
49
+ return std::make_pair (start, actual_size);
63
50
}
64
51
65
52
template <bool page_aligned = false >
@@ -69,4 +56,17 @@ namespace snmalloc
69
56
}
70
57
};
71
58
}
59
+
60
+ // WASI does not support pthreads and thus neither can it offer __cxa_thread_atexit.
61
+ // Delegating to __cxa_atexit.
62
+ // Should be changed once pthreads support it live and desired.
63
+ extern " C"
64
+ {
65
+ int __cxa_atexit (void (*func) (void *), void* arg, void* dso_handle);
66
+ int __cxa_thread_atexit (void (*func) (void *), void* arg, void* dso_symbol)
67
+ {
68
+ return __cxa_atexit (func, arg, dso_symbol);
69
+ }
70
+ }
71
+
72
72
#endif
0 commit comments