Use __wasm__ instead of __wasi__ for wasm targets

This commit is contained in:
Cheng Shao 2023-08-28 09:33:56 +00:00
parent 10efe291af
commit 1338edf2cf
11 changed files with 109 additions and 114 deletions

View file

@ -373,7 +373,7 @@ static inline void mi_atomic_yield(void) {
static inline void mi_atomic_yield(void) { static inline void mi_atomic_yield(void) {
smt_pause(); smt_pause();
} }
#elif defined(__wasi__) #elif defined(__wasm__)
#include <sched.h> #include <sched.h>
static inline void mi_atomic_yield(void) { static inline void mi_atomic_yield(void) {
sched_yield(); sched_yield();

View file

@ -40,10 +40,6 @@ terms of the MIT license. A copy of the license can be found in the file
#define mi_decl_cache_align #define mi_decl_cache_align
#endif #endif
#if defined(__EMSCRIPTEN__) && !defined(__wasi__)
#define __wasi__
#endif
#if defined(__cplusplus) #if defined(__cplusplus)
#define mi_decl_externc extern "C" #define mi_decl_externc extern "C"
#else #else
@ -51,7 +47,7 @@ terms of the MIT license. A copy of the license can be found in the file
#endif #endif
// pthreads // pthreads
#if !defined(_WIN32) && !defined(__wasi__) #if !defined(_WIN32) && !defined(__wasm__)
#define MI_USE_PTHREADS #define MI_USE_PTHREADS
#include <pthread.h> #include <pthread.h>
#endif #endif

View file

@ -12,7 +12,7 @@ terms of the MIT license. A copy of the license can be found in the file
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// This file specifies the primitive portability API. // This file specifies the primitive portability API.
// Each OS/host needs to implement these primitives, see `src/prim` // Each OS/host needs to implement these primitives, see `src/prim`
// for implementations on Window, macOS, WASI, and Linux/Unix. // for implementations on Window, macOS, wasm, and Linux/Unix.
// //
// note: on all primitive functions, we always have result parameters != NUL, and: // note: on all primitive functions, we always have result parameters != NUL, and:
// addr != NULL and page aligned // addr != NULL and page aligned

View file

@ -264,7 +264,7 @@ int reallocarr(void* p, size_t count, size_t size) { return mi_reallocarr(p
void* memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); } void* memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }
void* _aligned_malloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); } void* _aligned_malloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }
#if defined(__wasi__) #if defined(__wasm__)
// forward __libc interface (see PR #667) // forward __libc interface (see PR #667)
void* __libc_malloc(size_t size) MI_FORWARD1(mi_malloc, size) void* __libc_malloc(size_t size) MI_FORWARD1(mi_malloc, size)
void* __libc_calloc(size_t count, size_t size) MI_FORWARD2(mi_calloc, count, size) void* __libc_calloc(size_t count, size_t size) MI_FORWARD2(mi_calloc, count, size)

View file

@ -823,7 +823,7 @@ mi_decl_nodiscard mi_decl_restrict char* mi_strndup(const char* s, size_t n) mi_
return mi_heap_strndup(mi_prim_get_default_heap(),s,n); return mi_heap_strndup(mi_prim_get_default_heap(),s,n);
} }
#ifndef __wasi__ #ifndef __wasm__
// `realpath` using mi_malloc // `realpath` using mi_malloc
#ifdef _WIN32 #ifdef _WIN32
#ifndef PATH_MAX #ifndef PATH_MAX

View file

@ -13,7 +13,7 @@ threads and need to be accessed using atomic operations.
Arenas are used to for huge OS page (1GiB) reservations or for reserving Arenas are used to for huge OS page (1GiB) reservations or for reserving
OS memory upfront which can be improve performance or is sometimes needed OS memory upfront which can be improve performance or is sometimes needed
on embedded devices. We can also employ this with WASI or `sbrk` systems on embedded devices. We can also employ this with wasm or `sbrk` systems
to reserve large arenas upfront and be able to reuse the memory more effectively. to reserve large arenas upfront and be able to reuse the memory more effectively.
The arena allocation needs to be thread safe and we use an atomic bitmap to allocate. The arena allocation needs to be thread safe and we use an atomic bitmap to allocate.
@ -939,4 +939,3 @@ int mi_reserve_huge_os_pages(size_t pages, double max_secs, size_t* pages_reserv
if (err==0 && pages_reserved!=NULL) *pages_reserved = pages; if (err==0 && pages_reserved!=NULL) *pages_reserved = pages;
return err; return err;
} }

View file

@ -14,9 +14,9 @@ terms of the MIT license. A copy of the license can be found in the file
#elif defined(__APPLE__) #elif defined(__APPLE__)
#include "osx/prim.c" // macOSX (actually defers to mmap in unix/prim.c) #include "osx/prim.c" // macOSX (actually defers to mmap in unix/prim.c)
#elif defined(__wasi__) #elif defined(__wasm__)
#define MI_USE_SBRK #define MI_USE_SBRK
#include "wasi/prim.c" // memory-grow or sbrk (Wasm) #include "wasm/prim.c" // memory-grow or sbrk (Wasm)
#else #else
#include "unix/prim.c" // mmap() (Linux, macOSX, BSD, Illumnos, Haiku, DragonFly, etc.) #include "unix/prim.c" // mmap() (Linux, macOSX, BSD, Illumnos, Haiku, DragonFly, etc.)

View file

@ -3,7 +3,7 @@
This is the portability layer where all primitives needed from the OS are defined. This is the portability layer where all primitives needed from the OS are defined.
- `include/mimalloc/prim.h`: primitive portability API definition. - `include/mimalloc/prim.h`: primitive portability API definition.
- `prim.c`: Selects one of `unix/prim.c`, `wasi/prim.c`, or `windows/prim.c` depending on the host platform - `prim.c`: Selects one of `unix/prim.c`, `wasm/prim.c`, or `windows/prim.c` depending on the host platform
(and on macOS, `osx/prim.c` defers to `unix/prim.c`). (and on macOS, `osx/prim.c` defers to `unix/prim.c`).
Note: still work in progress, there may still be places in the sources that still depend on OS ifdef's. Note: still work in progress, there may still be places in the sources that still depend on OS ifdef's.

View file

@ -642,7 +642,7 @@ void _mi_prim_process_info(mi_process_info_t* pinfo)
#else #else
#ifndef __wasi__ #ifndef __wasm__
// WebAssembly instances are not processes // WebAssembly instances are not processes
#pragma message("define a way to get process info") #pragma message("define a way to get process info")
#endif #endif

View file

@ -30,7 +30,7 @@ void _mi_prim_mem_init( mi_os_mem_config_t* config ) {
int _mi_prim_free(void* addr, size_t size ) { int _mi_prim_free(void* addr, size_t size ) {
MI_UNUSED(addr); MI_UNUSED(size); MI_UNUSED(addr); MI_UNUSED(size);
// wasi heap cannot be shrunk // wasm linear memory cannot be shrunk
return 0; return 0;
} }
@ -43,12 +43,12 @@ int _mi_prim_free(void* addr, size_t size ) {
static void* mi_memory_grow( size_t size ) { static void* mi_memory_grow( size_t size ) {
void* p = sbrk(size); void* p = sbrk(size);
if (p == (void*)(-1)) return NULL; if (p == (void*)(-1)) return NULL;
#if !defined(__wasi__) // on wasi this is always zero initialized already (?) #if !defined(__wasm__) // on wasm this is always zero initialized already.
memset(p,0,size); memset(p,0,size);
#endif #endif
return p; return p;
} }
#elif defined(__wasi__) #elif defined(__wasm__)
static void* mi_memory_grow( size_t size ) { static void* mi_memory_grow( size_t size ) {
size_t base = (size > 0 ? __builtin_wasm_memory_grow(0,_mi_divide_up(size, _mi_os_page_size())) size_t base = (size > 0 ? __builtin_wasm_memory_grow(0,_mi_divide_up(size, _mi_os_page_size()))
: __builtin_wasm_memory_size(0)); : __builtin_wasm_memory_size(0));

View file

@ -175,7 +175,7 @@ static void mi_random_init_ex(mi_random_ctx_t* ctx, bool use_weak) {
if (use_weak || !_mi_prim_random_buf(key, sizeof(key))) { if (use_weak || !_mi_prim_random_buf(key, sizeof(key))) {
// if we fail to get random data from the OS, we fall back to a // if we fail to get random data from the OS, we fall back to a
// weak random source based on the current time // weak random source based on the current time
#if !defined(__wasi__) #if !defined(__wasm__)
if (!use_weak) { _mi_warning_message("unable to use secure randomness\n"); } if (!use_weak) { _mi_warning_message("unable to use secure randomness\n"); }
#endif #endif
uintptr_t x = _mi_os_random_weak(0); uintptr_t x = _mi_os_random_weak(0);