1 #include "test/jemalloc_test.h"
2
3 /* Tests je_iterate added by src/android_je_iterate.c */
4
5 int je_iterate(uintptr_t, size_t, void (*)(uintptr_t, size_t, void*), void*);
6
7 static size_t alloc_count;
8 static size_t alloc_size;
9 static uintptr_t alloc_find;
10 static size_t alloc_find_size;
11 static bool alloc_found;
12
callback(uintptr_t ptr,size_t size,void * arg)13 static void callback(uintptr_t ptr, size_t size, void* arg) {
14 alloc_count++;
15 alloc_size += size;
16 if (ptr <= alloc_find && alloc_find < ptr + size) {
17 assert(alloc_find + alloc_find_size <= ptr + size);
18 alloc_found = true;
19 }
20 }
21
TEST_BEGIN(test_iterate_alloc)22 TEST_BEGIN(test_iterate_alloc)
23 {
24
25 #define MAXSZ (((size_t)1) << 26)
26 size_t sz;
27
28 for (sz = 1; sz < MAXSZ; sz <<= 1) {
29 void *ptr;
30 ptr = malloc(sz);
31 assert_ptr_not_null(ptr, "malloc() failed for size %zu", sz);
32
33 alloc_count = 0;
34 alloc_size = 0;
35 alloc_find = (uintptr_t)ptr;
36 alloc_find_size = sz;
37 alloc_found = false;
38
39 mallctl("thread.tcache.flush", NULL, NULL, NULL, 0);
40
41 assert(je_iterate((uintptr_t)ptr, sz, callback, NULL) == 0);
42
43 assert(alloc_found);
44
45 free(ptr);
46 }
47 #undef MAXSZ
48 }
49 TEST_END
50
TEST_BEGIN(test_iterate_dalloc)51 TEST_BEGIN(test_iterate_dalloc)
52 {
53
54 #define MAXSZ (((size_t)1) << 26)
55 size_t sz;
56
57 for (sz = 1; sz < MAXSZ; sz <<= 1) {
58 void *ptr;
59 ptr = malloc(sz);
60 free(ptr);
61 assert_ptr_not_null(ptr, "malloc() failed for size %zu", sz);
62
63 alloc_count = 0;
64 alloc_size = 0;
65 alloc_find = (uintptr_t)ptr;
66 alloc_find_size = sz;
67 alloc_found = false;
68
69 mallctl("thread.tcache.flush", NULL, NULL, NULL, 0);
70
71 je_iterate((uintptr_t)ptr, sz, callback, NULL);
72
73 assert(!alloc_found);
74 }
75 #undef MAXSZ
76 }
77 TEST_END
78
TEST_BEGIN(test_iterate_free_first)79 TEST_BEGIN(test_iterate_free_first)
80 {
81 #define MAXSZ (((size_t)1) << 26)
82 size_t sz;
83
84 for (sz = 1; sz < MAXSZ; sz <<= 1) {
85 void *ptr;
86 void *ptr2;
87 ptr2 = malloc(sz);
88 assert_ptr_not_null(ptr2, "malloc() failed for size %zu", sz);
89
90 ptr = malloc(sz);
91 assert_ptr_not_null(ptr, "malloc() failed for size %zu", sz);
92
93 free(ptr2);
94
95 alloc_count = 0;
96 alloc_size = 0;
97 alloc_find = (uintptr_t)ptr;
98 alloc_find_size = sz;
99 alloc_found = false;
100
101 mallctl("thread.tcache.flush", NULL, NULL, NULL, 0);
102
103 assert(je_iterate((uintptr_t)ptr, sz, callback, NULL) == 0);
104
105 assert(alloc_found);
106
107 free(ptr);
108 }
109 #undef MAXSZ
110 }
111 TEST_END
112
113 int
main(void)114 main(void)
115 {
116
117 return (test(
118 test_iterate_alloc,
119 test_iterate_dalloc,
120 test_iterate_free_first));
121 }
122