Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
93 lines (84 loc) · 2.18 KB

File metadata and controls

93 lines (84 loc) · 2.18 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* Copyright 2013 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/
// Emscripten tests
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <dlfcn.h>
typedef void *(*mallocer)(int n);
typedef void (*freeer)(void *p);
void *lib_handle;
int handles = 0;
mallocer mallocproxy = NULL;
freeer freeproxy = NULL;
void get_lib() {
//printf("get lib\n");
lib_handle = dlopen("liblib.so", RTLD_NOW);
assert(lib_handle != NULL);
handles++;
mallocproxy = (mallocer)dlsym(lib_handle, "mallocproxy");
assert(mallocproxy!= NULL);
freeproxy = (freeer)dlsym(lib_handle, "freeproxy");
assert(freeproxy!= NULL);
}
void unget_lib() {
//printf("unget lib\n");
assert(lib_handle);
dlclose(lib_handle);
handles--;
if (handles == 0) lib_handle = NULL;
}
int main() {
int n = 0, total = 0, l = 0;
void *allocs[50];
allocs[n++] = malloc(10); // pull in real malloc
for (int i = 0; i < 1000; i++) {
//printf("%d: total ever %d MB, current MB %d, total libs %d\n", i, total, n, l);
assert(n < 50);
if (i % 5 == 0) {
if (handles < 10) {
get_lib();
l++;
}
}
if (i % 7 == 0) {
if (handles > 0) unget_lib();
}
if (i % 3 == 0) {
if (handles > 0) {
if (n < 10) {
if (i % 2 == 0) {
//printf("%d: alloc\n", i);
allocs[n++] = mallocproxy(1024*1024);
} else {
//printf("%d: real alloc\n", i);
allocs[n++] = malloc(1024*1024);
}
total++;
} else {
//printf("real free\n");
free(allocs[--n]); // real free
}
}
}
if (i % 4 == 0) {
if (handles > 0 && n > 0) {
//printf("free\n");
if (i % 2 == 0) {
//printf("free\n");
freeproxy(allocs[--n]);
} else {
//printf("real free\n");
free(allocs[--n]);
}
}
}
}
while (n > 0) free(allocs[--n]); // real free
while (handles > 0) unget_lib();
printf("*%d,%d*\n", total, l);
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.