Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/dlmalloc_test.c
4128 views
1
/*
2
* Copyright 2011 The Emscripten Authors. All rights reserved.
3
* Emscripten is available under two separate licenses, the MIT license and the
4
* University of Illinois/NCSA Open Source License. Both these licenses can be
5
* found in the LICENSE file.
6
*/
7
8
// Emscripten tests
9
10
#include <assert.h>
11
#include <emscripten/console.h>
12
#include <stdlib.h>
13
14
int main(int ac, char **av) {
15
int NUM = ac > 1 ? atoi(av[1]) : 0;
16
int REPS = ac > 2 ? atoi(av[2]) : 0;
17
int c1 = 0, c2 = 0;
18
for (int x = 0; x < REPS; x++) {
19
char* allocations[NUM];
20
for (int i = 0; i < NUM/2; i++) {
21
allocations[i] = (char*)malloc((11*i)%1024 + x);
22
assert(allocations[i]);
23
if (i > 10 && i%4 == 1 && allocations[i-10]) {
24
free(allocations[i-10]);
25
allocations[i-10] = NULL;
26
}
27
}
28
for (int i = NUM/2; i < NUM; i++) {
29
allocations[i] = (char*)malloc(1024*(i+1));
30
assert(allocations[i]);
31
if (i > 10 && i%4 != 1 && allocations[i-10]) {
32
free(allocations[i-10]);
33
allocations[i-10] = NULL;
34
}
35
}
36
char* first = allocations[0];
37
for (int i = 0; i < NUM; i++) {
38
if (allocations[i]) {
39
free(allocations[i]);
40
}
41
}
42
char *last = (char*)malloc(512); // should be identical, as we free'd it all
43
char *newer = (char*)malloc(512); // should be different
44
c1 += first == last;
45
c2 += first == newer;
46
}
47
emscripten_console_logf("*%d,%d*\n", c1, c2);
48
}
49
50