Skip to content
Snippets Groups Projects
Commit 558353f4 authored by Cheng Shao's avatar Cheng Shao Committed by Marge Bot
Browse files

rts: use page sized mblocks on wasm

This patch changes mblock size to page size on wasm. It allows us to
simplify our wasi-libc fork, makes it much easier to test third party
libc allocators like emmalloc/mimalloc, as well as experimenting with
threaded RTS in wasm.
parent 4e36d3a3
No related branches found
No related tags found
No related merge requests found
[61420,61420,61420,60388,60132,60132,60132,60132,60132,60132,60132,60132,60132,60132,60132,60132,60132,60132,60132,60132,60132,60132,60132,60132,60132,60132,60132,60132,60132,60132,60132,60132,60132,60132]
......@@ -170,7 +170,11 @@
#define BLOCK_SHIFT 12
/* The size of a megablock (2^MBLOCK_SHIFT bytes) */
#if defined(wasm32_HOST_ARCH)
#define MBLOCK_SHIFT 16
#else
#define MBLOCK_SHIFT 20
#endif
/* -----------------------------------------------------------------------------
Bitmap/size fields (used in info tables)
......
......@@ -17,7 +17,12 @@
#include "BeginPrivate.h"
// Segments
#if defined(wasm32_HOST_ARCH)
#define NONMOVING_SEGMENT_BITS 14ULL // 2^14 = 16kByte
#else
#define NONMOVING_SEGMENT_BITS 15ULL // 2^15 = 32kByte
#endif
// Mask to find base of segment
#define NONMOVING_SEGMENT_MASK (((uintptr_t)1 << NONMOVING_SEGMENT_BITS) - 1)
// In bytes
......
......@@ -32,14 +32,8 @@
// libc allocator's certain invariants. But dlmalloc permits this
// behavior!
//
// Therefore, we bypass dlmalloc, and directly call memory.grow to
// allocate megablocks. We even patch dlmalloc in the libc sysroot
// shipped in our wasi-sdk release, so that whenever dlmalloc calls
// sbrk(), it extends the linear memory to align to the megablock
// size, so to avoid space waste as much as possible. Our wasi-libc
// patch doesn't impact ABI interoperability, and when stock clang
// compiles code that calls malloc() to wasm objects, those objects
// would just link fine with our build products.
// Therefore, we bypass dlmalloc, and directly call sbrk() to
// allocate megablocks.
//
// One remaining question is how to free a megablock. Wasm spec
// doesn't allow shrinking the linear memory, so the logic of
......@@ -49,12 +43,13 @@
// megablock on Wasm.
#include "Rts.h"
#include "RtsUtils.h"
#include "sm/OSMem.h"
#include "rts/storage/HeapAlloc.h"
#include <__macro_PAGESIZE.h>
#include <unistd.h>
#define PAGESIZE (0x10000)
GHC_STATIC_ASSERT(MBLOCK_SIZE == PAGESIZE, "MBLOCK_SIZE must be equal to wasm page size");
void osMemInit(void)
{
......@@ -63,13 +58,7 @@ void osMemInit(void)
void *
osGetMBlocks(uint32_t n)
{
size_t base = __builtin_wasm_memory_size(0) * PAGESIZE;
size_t start = MBLOCK_ROUND_UP(base);
size_t end = start + (n << MBLOCK_SHIFT);
ptrdiff_t delta = (end - base) / PAGESIZE;
if (__builtin_wasm_memory_grow(0, delta) == SIZE_MAX)
barf("osGetMBlocks failed");
return start;
return sbrk(PAGESIZE * n);
}
void osBindMBlocksToNode(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment