Porting to WASM

This commit is contained in:
Krzosa Karol
2025-11-24 22:59:11 +01:00
parent 166f06d1fb
commit 56cdb9557d
19 changed files with 414 additions and 654 deletions
+27 -6
View File
@@ -17,6 +17,8 @@
#elif defined(__linux__)
#define OS_POSIX 1
#define OS_LINUX 1
#elif defined(__EMSCRIPTEN__)
#define OS_WASM 1
#else
#error Unsupported platform
#endif
@@ -31,6 +33,10 @@
#error Unsupported compiler
#endif
#ifndef OS_WASM
#define OS_WASM 0
#endif
#ifndef OS_MAC
#define OS_MAC 0
#endif
@@ -60,14 +66,18 @@
#endif
#if OS_WINDOWS
#define DebugBreak() __debugbreak()
#define BREAK() __debugbreak()
#elif OS_LINUX
#define DebugBreak() raise(SIGTRAP)
#define BREAK() raise(SIGTRAP)
#elif OS_WASM
#include <emscripten.h>
extern "C" void JS_Breakpoint(void);
#define BREAK() JS_Breakpoint()
#endif
#define Assert(x) \
if (!(x)) { \
DebugBreak(); \
BREAK(); \
}
#define InvalidCodepath() Assert(!"invalid codepath")
#define ElseInvalidCodepath() else {InvalidCodepath()}
@@ -1035,8 +1045,8 @@ bool VCommit(void *p, size_t size);
bool VRelease(void *p, size_t size);
bool VDecommit(void *p, size_t size);
void InitArena(Arena *arena, size_t reserve = GiB(4));
Arena *AllocArena(size_t reserve = GiB(4));
void InitArena(Arena *arena, size_t reserve = MiB(256));
Arena *AllocArena(size_t reserve = MiB(256));
Arena *AllocArena(Allocator allocator, size_t size);
void *PushSize(Arena *arena, size_t size);
void Release(Arena *arena);
@@ -1707,11 +1717,22 @@ void *ArenaAllocatorProc(void *object, int kind, void *p, size_t size) {
}
thread_local Arena *ScratchArenaPool[4];
void InitScratch() {
#if OS_WASM
void InitScratch() {
Allocator sys_allocator = GetSystemAllocator();
ScratchArenaPool[0] = AllocArena(sys_allocator, MiB(8));
ScratchArenaPool[1] = AllocArena(sys_allocator, MiB(2));
ScratchArenaPool[3] = AllocArena(sys_allocator, MiB(1));
ScratchArenaPool[3] = AllocArena(sys_allocator, KiB(512));
}
#else
void InitScratch() {
for (int i = 0; i < Lengthof(ScratchArenaPool); i += 1) {
ScratchArenaPool[i] = AllocArena();
}
}
#endif
TempArena GetScratchEx(Arena **conflicts, int conflict_count) {
Arena *unoccupied = 0;