Fix wasm backend compilation

This commit is contained in:
Cheng Shao 2023-08-28 10:01:50 +00:00
parent 1338edf2cf
commit c019eee52a
3 changed files with 45 additions and 50 deletions

View file

@ -307,6 +307,14 @@ static inline uintptr_t _mi_align_down(uintptr_t sz, size_t alignment) {
} }
} }
__attribute__((__unused__)) static void* mi_align_up_ptr(void* p, size_t alignment) {
return (void*)_mi_align_up((uintptr_t)p, alignment);
}
__attribute__((__unused__)) static void* mi_align_down_ptr(void* p, size_t alignment) {
return (void*)_mi_align_down((uintptr_t)p, alignment);
}
// Divide upwards: `s <= _mi_divide_up(s,d)*d < s+d`. // Divide upwards: `s <= _mi_divide_up(s,d)*d < s+d`.
static inline uintptr_t _mi_divide_up(uintptr_t size, size_t divider) { static inline uintptr_t _mi_divide_up(uintptr_t size, size_t divider) {
mi_assert_internal(divider != 0); mi_assert_internal(divider != 0);

View file

@ -73,14 +73,6 @@ void _mi_os_init(void) {
bool _mi_os_decommit(void* addr, size_t size, mi_stats_t* stats); bool _mi_os_decommit(void* addr, size_t size, mi_stats_t* stats);
bool _mi_os_commit(void* addr, size_t size, bool* is_zero, mi_stats_t* tld_stats); bool _mi_os_commit(void* addr, size_t size, bool* is_zero, mi_stats_t* tld_stats);
static void* mi_align_up_ptr(void* p, size_t alignment) {
return (void*)_mi_align_up((uintptr_t)p, alignment);
}
static void* mi_align_down_ptr(void* p, size_t alignment) {
return (void*)_mi_align_down((uintptr_t)p, alignment);
}
/* ----------------------------------------------------------- /* -----------------------------------------------------------
aligned hinting aligned hinting

View file

@ -7,6 +7,10 @@ terms of the MIT license. A copy of the license can be found in the file
// This file is included in `src/prim/prim.c` // This file is included in `src/prim/prim.c`
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "mimalloc.h" #include "mimalloc.h"
#include "mimalloc/internal.h" #include "mimalloc/internal.h"
#include "mimalloc/atomic.h" #include "mimalloc/atomic.h"
@ -36,10 +40,9 @@ int _mi_prim_free(void* addr, size_t size ) {
//--------------------------------------------- //---------------------------------------------
// Allocation: sbrk or memory_grow // Allocation: sbrk
//--------------------------------------------- //---------------------------------------------
#if defined(MI_USE_SBRK)
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;
@ -48,14 +51,6 @@ int _mi_prim_free(void* addr, size_t size ) {
#endif #endif
return p; return p;
} }
#elif defined(__wasm__)
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()))
: __builtin_wasm_memory_size(0));
if (base == SIZE_MAX) return NULL;
return (void*)(base * _mi_os_page_size());
}
#endif
#if defined(MI_USE_PTHREADS) #if defined(MI_USE_PTHREADS)
static pthread_mutex_t mi_heap_grow_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t mi_heap_grow_mutex = PTHREAD_MUTEX_INITIALIZER;
@ -96,7 +91,7 @@ static void* mi_prim_mem_grow(size_t size, size_t try_alignment) {
if (base != NULL) { if (base != NULL) {
p = mi_align_up_ptr(base, try_alignment); p = mi_align_up_ptr(base, try_alignment);
if ((uint8_t*)p + size > (uint8_t*)base + alloc_size) { if ((uint8_t*)p + size > (uint8_t*)base + alloc_size) {
// another thread used wasm_memory_grow/sbrk in-between and we do not have enough // another thread used sbrk in-between and we do not have enough
// space after alignment. Give up (and waste the space as we cannot shrink :-( ) // space after alignment. Give up (and waste the space as we cannot shrink :-( )
// (in `mi_os_mem_alloc_aligned` this will fall back to overallocation to align) // (in `mi_os_mem_alloc_aligned` this will fall back to overallocation to align)
p = NULL; p = NULL;
@ -105,7 +100,7 @@ static void* mi_prim_mem_grow(size_t size, size_t try_alignment) {
} }
/* /*
if (p == NULL) { if (p == NULL) {
_mi_warning_message("unable to allocate sbrk/wasm_memory_grow OS memory (%zu bytes, %zu alignment)\n", size, try_alignment); _mi_warning_message("unable to allocate sbrk OS memory (%zu bytes, %zu alignment)\n", size, try_alignment);
errno = ENOMEM; errno = ENOMEM;
return NULL; return NULL;
} }
@ -114,7 +109,7 @@ static void* mi_prim_mem_grow(size_t size, size_t try_alignment) {
return p; return p;
} }
// Note: the `try_alignment` is just a hint and the returned pointer is not guaranteed to be aligned. // Note: the `try_alignment` argument is respected by over-allocating.
int _mi_prim_alloc(size_t size, size_t try_alignment, bool commit, bool allow_large, bool* is_large, bool* is_zero, void** addr) { int _mi_prim_alloc(size_t size, size_t try_alignment, bool commit, bool allow_large, bool* is_large, bool* is_zero, void** addr) {
MI_UNUSED(allow_large); MI_UNUSED(commit); MI_UNUSED(allow_large); MI_UNUSED(commit);
*is_large = false; *is_large = false;