Skip to content

Commit ff25a12

Browse files
authored
[test] Improve test_emmalloc_trim output. NFC (emscripten-core#23342)
Also, add some assertions.
1 parent 90f2c57 commit ff25a12

File tree

2 files changed

+66
-59
lines changed

2 files changed

+66
-59
lines changed

test/core/test_emmalloc_trim.c

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
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

1718
int 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
}

test/core/test_emmalloc_trim.out

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
11
heap size: 134217728
2-
dynamic heap 0: 4096
3-
free dynamic memory 0: 4096
4-
unclaimed heap memory 0: 2147348480
5-
sbrk 0: 0x12000
6-
1
7-
dynamic heap 1: 37752832
8-
free dynamic memory 1: 4096
9-
unclaimed heap memory 1: 2109599744
10-
sbrk 1: 0x2412000
11-
1st trim: 1
12-
dynamic heap 1: 37752832
13-
free dynamic memory 1: 0
14-
unclaimed heap memory 1: 2109599744
15-
sbrk 1: 0x2412000
16-
2nd trim: 0
17-
dynamic heap 2: 37752832
18-
free dynamic memory 2: 0
19-
unclaimed heap memory 2: 2109599744
20-
sbrk 2: 0x2412000
21-
3rd trim: 1
22-
dynamic heap 3: 33656832
23-
free dynamic memory 3: 102400
24-
unclaimed heap memory 3: 2109599744
25-
sbrk 3: 0x2412000
26-
4th trim: 0
27-
dynamic heap 4: 33656832
28-
free dynamic memory 4: 102400
29-
unclaimed heap memory 4: 2109599744
30-
sbrk 4: 0x2412000
31-
5th trim: 1
32-
dynamic heap 5: 33558528
33-
free dynamic memory 5: 0
34-
unclaimed heap memory 5: 2109599744
35-
sbrk 5: 0x2412000
2+
init: dynamic heap: 4096
3+
init: free dynamic memory: 4096
4+
init: unclaimed heap memory: 2147348480
5+
init: sbrk: 0x12000
6+
after alloc: dynamic heap: 37752832
7+
after alloc: free dynamic memory: 4096
8+
after alloc: unclaimed heap memory: 2109599744
9+
after alloc: sbrk: 0x2412000
10+
1st trim: did_free=1
11+
1: dynamic heap: 37752832
12+
1: free dynamic memory: 0
13+
1: unclaimed heap memory: 2109599744
14+
1: sbrk: 0x2412000
15+
2nd trim: did_free=0
16+
2: dynamic heap: 37752832
17+
2: free dynamic memory: 0
18+
2: unclaimed heap memory: 2109599744
19+
2: sbrk: 0x2412000
20+
3rd trim: did_free=1
21+
3: dynamic heap: 33656832
22+
3: free dynamic memory: 102400
23+
3: unclaimed heap memory: 2109599744
24+
3: sbrk: 0x2412000
25+
4th trim: did_free=0
26+
4: dynamic heap: 33656832
27+
4: free dynamic memory: 102400
28+
4: unclaimed heap memory: 2109599744
29+
4: sbrk: 0x2412000
30+
5th trim: did_free=1
31+
5: dynamic heap: 33558528
32+
5: free dynamic memory: 0
33+
5: unclaimed heap memory: 2109599744
34+
5: sbrk: 0x2412000

0 commit comments

Comments
 (0)