-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmem.xht
297 lines (265 loc) · 6.12 KB
/
cmem.xht
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset='UTF-8' />
<title>cmem-wasm debug</title>
<script>//<![CDATA[
window.cmemMod = null;
window.cmem = null;
window.view = null;
let test;
function reset(skipInit)
{
cmem = new WebAssembly.Instance(cmemMod).exports
view = new Uint8Array(cmem.memory.buffer);
if (!skipInit) cmem.cmem_init(0);
}
(() => {
let failCount = 0;
let totalCount = 0;
function check(cond)
{
if (!cond)
{
console.error("Test failure");
++failCount;
}
++totalCount;
}
function checkMem(expected)
{
if (!Uint8Array.from(expected).every((value, i) => value === view[i]))
{
console.error("Exp ", expected.toString(), '\nGot ', Array.from(view.slice(0, expected.length)).toString());
++failCount;
}
++totalCount;
}
test = function runChecks()
{
failCount = totalCount = 0;
let caught = false;
let ptr1, ptr2, ptr3, ptr4;
const begin = [0, 0, 0xFF, 0xFF, 6, 0];
// list_push
reset();
let list = cmem.cmem_list_push(0, 1);
cmem.cmem_list_push(list, 2);
checkMem(begin.concat(
[
1, 0, 0, 0, 13, 0,
0,
2, 0, 6, 0, 0, 0,
0, 0,
]));
// malloc
// First allocation
reset();
ptr1 = cmem.malloc(8);
check(ptr1 === 12);
checkMem(begin.concat([
8, 0, 0, 0, 0, 0,
]));
// Another allocation
ptr1 = cmem.malloc(8);
check(ptr1 === 26);
// Exact fit
cmem.malloc(8);
cmem.free(ptr1);
ptr2 = cmem.malloc(8)
check(ptr1 === ptr2);
// Just too short
cmem.free(ptr1);
ptr2 = cmem.malloc(9)
check(ptr1 !== ptr2);
// Gap longer than buffer
ptr2 = cmem.malloc(7)
check(ptr1 === ptr2);
// Best fit
reset();
ptr1 = cmem.malloc(2); // 6 0, 2, 6 1, 1,
cmem.malloc(1);
ptr2 = cmem.malloc(1);
cmem.malloc(1);
cmem.free(ptr1);
cmem.free(ptr2);
ptr3 = cmem.malloc(1);
check(ptr2 === ptr3);
// Exhaust page
reset();
try
{
cmem.malloc(65535 - 12);
} catch
{
++failCount;
}
++totalCount;
// Exceed page
reset();
try
{
cmem.malloc(65535 - 11);
} catch
{
caught = true;
}
++totalCount;
if (!caught)
++failCount;
caught = false;
// Swiss cheese
reset();
cmem.malloc(8);
cmem.malloc(8);
ptr1 = cmem.malloc(6);
ptr2 = cmem.malloc(6);
cmem.malloc(8);
ptr3 = cmem.malloc(32);
cmem.malloc(8);
cmem.free(ptr1);
cmem.free(ptr2);
cmem.free(ptr3);
ptr4 = cmem.malloc(18);
check(ptr1 === ptr4);
// realloc
// Shrink
reset();
cmem.malloc(32);
ptr1 = cmem.malloc(32);
cmem.malloc(32);
check(ptr1 === cmem.realloc(ptr1, 30));
check(cmem.cmem_list_get(ptr1 - 6) === 30);
// Grow behind self
reset();
cmem.malloc(32);
ptr1 = cmem.malloc(32);
ptr2 = cmem.malloc(2);
cmem.malloc(32);
cmem.free(ptr2);
check(ptr1 === cmem.realloc(ptr1, 40));
check(cmem.cmem_list_get(ptr1 - 6) === 40);
// Grow before self
reset();
cmem.malloc(32);
ptr1 = cmem.malloc(9);
ptr2 = cmem.malloc(32);
ptr3 = cmem.malloc(2);
cmem.malloc(32);
cmem.free(ptr1);
ptr4 = cmem.malloc(1);
cmem.free(ptr3);
check(ptr4 === ptr1);
ptr2 = cmem.realloc(ptr2, 48)
check(ptr2 === ptr1 + 7);
check(cmem.cmem_list_get(ptr2 - 6) === 48);
check(cmem.cmem_list_prev(ptr2 - 6) === ptr1 - 6);
check(cmem.cmem_list_next(ptr2 - 6) === ptr3 + 2);
// Overlapping heads
reset();
cmem.malloc(1);
ptr1 = cmem.malloc(2);
ptr2 = cmem.malloc(1);
ptr3 = cmem.malloc(1);
view[ptr2] = 2;
check(cmem.realloc(ptr1, 1) === ptr1);
ptr4 = cmem.realloc(ptr2, 2);
check(ptr4 === ptr1 + 1 + 6);
checkMem(begin.concat(
[
1, 0, 0, 0, 13, 0,
0,
1, 0, 6, 0, 20, 0,
0,
2, 0, 13, 0, 28, 0,
2, 2,
1, 0, 20, 0, 0, 0,
0,
0,
]));
// Insufficient space before self
reset();
cmem.malloc(32);
ptr1 = cmem.malloc(9);
ptr2 = cmem.malloc(1);
ptr3 = cmem.malloc(32);
cmem.realloc(ptr1, 1);
ptr2 = cmem.realloc(ptr2, 10);
check(ptr2 === ptr3 + 32 + 6);
// memset
reset(true);
cmem.memset(1, 1, 1);
cmem.memset(3, 2, 2);
checkMem([0, 1, 0, 2, 2, 0, 0]);
// memcpy
cmem.memcpy(6, 1, 1);
cmem.memcpy(8, 3, 2);
cmem.memcpy(11, 1, 4);
checkMem(
[
0, 1, 0, 2, 2, 0,
1,
0,
2, 2,
0,
1, 0, 2, 2,
0, 0
]);
// memmove
// Overlapping at start of dest
reset(true);
for (let i = 1; i <= 4; ++i) { view[i] = i; }
cmem.memmove(1, 0, 2);
checkMem([0, 0, 1, 3, 4, 0]);
// Overlapping at end of dest
cmem.memmove(2, 3, 2);
checkMem([0, 0, 3, 4, 4, 0]);
// src before dest without overlap
cmem.memmove(4, 2, 2);
checkMem([0, 0, 3, 4, 3, 4]);
// src after dest without overlap
cmem.memmove(0, 2, 2);
checkMem([3, 4, 3, 4, 3, 4]);
// end
reset();
let len = 6;
check(cmem.cmem_end() === len);
ptr1 = cmem.malloc(8);
len += 6 + 8;
check(cmem.cmem_end() === len);
cmem.calloc(4, 2);
len += 6 + 4 * 2;
check(cmem.cmem_end() === len);
ptr1 = cmem.realloc(ptr1, 16);
len += 6 + 16;
check(cmem.cmem_end() === len);
cmem.malloc(8);
check(cmem.cmem_end() === len);
cmem.free(ptr1);
len -= 6 + 16;
check(cmem.cmem_end() === len);
reset();
ptr1 = cmem.malloc(8);
ptr2 = cmem.malloc(8);
cmem.malloc(8);
check(cmem.cmem_end() === 48)
cmem.free(ptr1);
cmem.realloc(ptr2, 22);
check(cmem.cmem_end() === 48)
cmem.realloc(ptr2, 23);
check(cmem.cmem_end() === 77)
console.log(totalCount - failCount, "/", totalCount, "tests succeeded");
}
})();
fetch('out/cmem.wasm').then(response =>
response.arrayBuffer()
).then(buffer =>
{
cmemMod = new WebAssembly.Module(buffer);
cmem = new WebAssembly.Instance(cmemMod).exports;
console.log("cmem-wasm ready");
}).then(test, error => console.error(error));
//]]></script>
</head>
<body></body>
</html>