Native wasm32 expand_heap implementation

This commit is contained in:
Yury Delendik 2018-09-27 08:22:28 -05:00
parent edeb5004e6
commit 19296cd280

View File

@ -0,0 +1,28 @@
#include <limits.h>
#include <stdint.h>
#include <errno.h>
#include <sys/mman.h>
#include "libc.h"
#include "syscall.h"
#define WASM_PAGE_SIZE 65536
/* Expand the heap in-place use WebAssembly grow_memory operators.
* The caller is responsible for locking to prevent concurrent calls. */
void *__expand_heap(size_t *pn)
{
size_t n = *pn;
n += -n & WASM_PAGE_SIZE-1;
unsigned delta = n / WASM_PAGE_SIZE;
unsigned res = __builtin_wasm_memory_grow(0, delta);
if (res == (unsigned)-1) {
errno = ENOMEM;
return 0;
}
void *area = (void*)(WASM_PAGE_SIZE * res);
*pn = n;
return area;
}