1+ #include <assert.h>
12#include <stdio.h>
23#include <unistd.h>
34#include <emscripten/emmalloc.h>
@@ -7,40 +8,47 @@ size_t round_to_4k(size_t val){
78 return (val + 4095 ) & ~4095 ;
89}
910
10- void print_stats (int cnt ) {
11- printf ("dynamic heap %d: %zu\n" , cnt , round_to_4k (emmalloc_dynamic_heap_size ()));
12- printf ("free dynamic memory %d: %zu\n" , cnt , round_to_4k (emmalloc_free_dynamic_memory ()));
13- printf ("unclaimed heap memory %d : %zu\n" , cnt , round_to_4k (emmalloc_unclaimed_heap_memory ()));
14- printf ("sbrk %d: %#zx\n" , cnt , round_to_4k ((size_t )sbrk (0 )));
11+ void print_stats (const char * title ) {
12+ printf ("%s: dynamic heap: %zu\n" , title , round_to_4k (emmalloc_dynamic_heap_size ()));
13+ printf ("%s: free dynamic memory: %zu\n" , title , round_to_4k (emmalloc_free_dynamic_memory ()));
14+ printf ("%s: unclaimed heap memory: %zu\n" , title , round_to_4k (emmalloc_unclaimed_heap_memory ()));
15+ printf ("%s: sbrk: %#zx\n" , title , round_to_4k ((size_t )sbrk (0 )));
1516}
1617
1718int main () {
19+ int did_free ;
1820 printf ("heap size: %zu\n" , emscripten_get_heap_size ());
19- print_stats (0 );
21+ print_stats ("init" );
2022
2123 void * ptr = malloc (32 * 1024 * 1024 );
2224 void * ptr2 = malloc (4 * 1024 * 1024 );
23- printf ("%d\n" , (int )(ptr && ptr2 ));
24- print_stats (1 );
25-
26- int success = emmalloc_trim (0 );
27- printf ("1st trim: %d\n" , success );
28- print_stats (1 );
29-
30- success = emmalloc_trim (0 );
31- printf ("2nd trim: %d\n" , success );
32- print_stats (2 );
25+ assert (ptr );
26+ assert (ptr2 );
27+ print_stats ("after alloc" );
28+
29+ did_free = emmalloc_trim (0 );
30+ assert (did_free );
31+ printf ("1st trim: did_free=%d\n" , did_free );
32+ print_stats ("1" );
33+
34+ did_free = emmalloc_trim (0 );
35+ assert (!did_free );
36+ printf ("2nd trim: did_free=%d\n" , did_free );
37+ print_stats ("2" );
3338 free (ptr2 );
3439
35- success = emmalloc_trim (100000 );
36- printf ("3rd trim: %d\n" , success );
37- print_stats (3 );
40+ did_free = emmalloc_trim (100000 );
41+ assert (did_free );
42+ printf ("3rd trim: did_free=%d\n" , did_free );
43+ print_stats ("3" );
3844
39- success = emmalloc_trim (100000 );
40- printf ("4th trim: %d\n" , success );
41- print_stats (4 );
45+ did_free = emmalloc_trim (100000 );
46+ assert (!did_free );
47+ printf ("4th trim: did_free=%d\n" , did_free );
48+ print_stats ("4" );
4249
43- success = emmalloc_trim (0 );
44- printf ("5th trim: %d\n" , success );
45- print_stats (5 );
50+ did_free = emmalloc_trim (0 );
51+ assert (did_free );
52+ printf ("5th trim: did_free=%d\n" , did_free );
53+ print_stats ("5" );
4654}
0 commit comments