MCPcopy Create free account
hub / github.com/emscripten-core/emscripten / main

Function main

test/core/test_emmalloc_trim.c:21–67  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19}
20
21int main() {
22 int did_free;
23 printf("heap size: %zu\n", emscripten_get_heap_size());
24 print_stats("init");
25
26 void *ptr = malloc(32*1024*1024);
27 void *ptr2 = malloc(4*1024*1024);
28 assert(ptr);
29 assert(ptr2);
30 print_stats("after alloc");
31
32 // After having allocated memory, trim the heap. This may or may not actually
33 // trim (it is unspecified whether the heap is trimmed after initial mallocs)
34 did_free = emmalloc_trim(0);
35 // But print for posterity whether we did actually trim.
36 printf("1st trim: did_free=%d\n", did_free);
37 print_stats("1");
38
39 // Trimming again back-to-back after a trim should not trim more.
40 did_free = emmalloc_trim(0);
41 printf("2nd trim: did_free=%d\n", did_free);
42 print_stats("2");
43 assert(!did_free);
44
45 // Free one block. That should allow memory to be trimmed.
46 free(ptr2);
47 did_free = emmalloc_trim(0);
48 printf("3rd trim: did_free=%d\n", did_free);
49 print_stats("3");
50 assert(did_free);
51
52 // Free second block. That should also allow more memory to be trimmed.
53 // Try trimming by leaving a padding.
54 free(ptr);
55 did_free = emmalloc_trim(100000);
56 printf("4th trim: did_free=%d\n", did_free);
57 print_stats("4");
58 assert(did_free);
59 assert(emmalloc_free_dynamic_memory() >= 100000);
60
61 // Now try re-trimming by also discarding the padding. This should trim
62 // more.
63 did_free = emmalloc_trim(0);
64 printf("5th trim: did_free=%d\n", did_free);
65 print_stats("5");
66 assert(did_free);
67}

Callers

nothing calls this directly

Calls 8

printfFunction · 0.85
emscripten_get_heap_sizeFunction · 0.85
print_statsFunction · 0.85
emmalloc_trimFunction · 0.85
mallocFunction · 0.70
freeFunction · 0.70
assertFunction · 0.50

Tested by

no test coverage detected