forked from zeux/meshoptimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwasmstubs.cpp
116 lines (93 loc) · 1.96 KB
/
wasmstubs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#ifndef __wasi__
#error This file contains libc stubs for WASI SDK and should only be used in non-Emscripten WebAssembly builds
#endif
#include <stddef.h>
#include <stdint.h>
#include <assert.h>
extern unsigned char __heap_base;
static intptr_t sbrkp = intptr_t(&__heap_base);
static const int WASM_PAGE_SIZE = 64 * 1024;
extern "C" void* sbrk(intptr_t increment)
{
intptr_t sbrko = sbrkp;
increment = (increment + 3) & ~3;
sbrkp += increment;
size_t heap_size = __builtin_wasm_memory_size(0) * WASM_PAGE_SIZE;
if (sbrkp > heap_size)
{
size_t diff = (sbrkp - heap_size + WASM_PAGE_SIZE - 1) / WASM_PAGE_SIZE;
if (__builtin_wasm_memory_grow(0, diff) == size_t(-1))
return (void*)-1;
}
return (void*)sbrko;
}
extern "C" void* memcpy(void* destination, const void* source, size_t num)
{
char* d = (char*)destination;
const char* s = (const char*)source;
if (((uintptr_t(d) | uintptr_t(s)) & 3) == 0)
{
while (num > 15)
{
((uint32_t*)d)[0] = ((uint32_t*)s)[0];
((uint32_t*)d)[1] = ((uint32_t*)s)[1];
((uint32_t*)d)[2] = ((uint32_t*)s)[2];
((uint32_t*)d)[3] = ((uint32_t*)s)[3];
d += 16;
s += 16;
num -= 16;
}
while (num > 3)
{
((uint32_t*)d)[0] = ((uint32_t*)s)[0];
d += 4;
s += 4;
num -= 4;
}
}
while (num > 0)
{
*d++ = *s++;
num--;
}
return destination;
}
extern "C" void* memset(void* ptr, int value, size_t num)
{
uint32_t v32 = ~0u / 255 * uint8_t(value);
char* d = (char*)ptr;
if ((uintptr_t(d) & 3) == 0)
{
while (num > 15)
{
((uint32_t*)d)[0] = v32;
((uint32_t*)d)[1] = v32;
((uint32_t*)d)[2] = v32;
((uint32_t*)d)[3] = v32;
d += 16;
num -= 16;
}
while (num > 3)
{
((uint32_t*)d)[0] = v32;
d += 4;
num -= 4;
}
}
while (num > 0)
{
*d++ = char(value);
num--;
}
return ptr;
}
void* operator new(size_t size)
{
return sbrk((size + 7) & ~7);
}
void operator delete(void* ptr) throw()
{
void* brk = sbrk(0);
assert(ptr <= brk);
sbrk((char*)ptr - (char*)brk);
}