File: wasmstubs.cpp

package info (click to toggle)
meshoptimizer 0.18%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,256 kB
  • sloc: cpp: 12,819; ansic: 7,249; javascript: 292; makefile: 144; python: 24
file content (112 lines) | stat: -rw-r--r-- 1,864 bytes parent folder | download
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
#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);
}

void operator delete(void* ptr) throw()
{
	void* brk = sbrk(0);
	assert(ptr <= brk);

	sbrk((char*)ptr - (char*)brk);
}