1 #include "test/jemalloc_test.h"
2
3 JEMALLOC_INLINE_C void
time_func(timedelta_t * timer,uint64_t nwarmup,uint64_t niter,void (* func)(void))4 time_func(timedelta_t *timer, uint64_t nwarmup, uint64_t niter,
5 void (*func)(void))
6 {
7 uint64_t i;
8
9 for (i = 0; i < nwarmup; i++)
10 func();
11 timer_start(timer);
12 for (i = 0; i < niter; i++)
13 func();
14 timer_stop(timer);
15 }
16
17 void
compare_funcs(uint64_t nwarmup,uint64_t niter,const char * name_a,void (* func_a),const char * name_b,void (* func_b))18 compare_funcs(uint64_t nwarmup, uint64_t niter, const char *name_a,
19 void (*func_a), const char *name_b, void (*func_b))
20 {
21 timedelta_t timer_a, timer_b;
22 char ratio_buf[6];
23 void *p;
24
25 p = mallocx(1, 0);
26 if (p == NULL) {
27 test_fail("Unexpected mallocx() failure");
28 return;
29 }
30
31 time_func(&timer_a, nwarmup, niter, func_a);
32 time_func(&timer_b, nwarmup, niter, func_b);
33
34 timer_ratio(&timer_a, &timer_b, ratio_buf, sizeof(ratio_buf));
35 malloc_printf("%"FMTu64" iterations, %s=%"FMTu64"us, "
36 "%s=%"FMTu64"us, ratio=1:%s\n",
37 niter, name_a, timer_usec(&timer_a), name_b, timer_usec(&timer_b),
38 ratio_buf);
39
40 dallocx(p, 0);
41 }
42
43 static void
malloc_free(void)44 malloc_free(void)
45 {
46 /* The compiler can optimize away free(malloc(1))! */
47 void *p = malloc(1);
48 if (p == NULL) {
49 test_fail("Unexpected malloc() failure");
50 return;
51 }
52 free(p);
53 }
54
55 static void
mallocx_free(void)56 mallocx_free(void)
57 {
58 void *p = mallocx(1, 0);
59 if (p == NULL) {
60 test_fail("Unexpected mallocx() failure");
61 return;
62 }
63 free(p);
64 }
65
TEST_BEGIN(test_malloc_vs_mallocx)66 TEST_BEGIN(test_malloc_vs_mallocx)
67 {
68
69 compare_funcs(10*1000*1000, 100*1000*1000, "malloc",
70 malloc_free, "mallocx", mallocx_free);
71 }
72 TEST_END
73
74 static void
malloc_dallocx(void)75 malloc_dallocx(void)
76 {
77 void *p = malloc(1);
78 if (p == NULL) {
79 test_fail("Unexpected malloc() failure");
80 return;
81 }
82 dallocx(p, 0);
83 }
84
85 static void
malloc_sdallocx(void)86 malloc_sdallocx(void)
87 {
88 void *p = malloc(1);
89 if (p == NULL) {
90 test_fail("Unexpected malloc() failure");
91 return;
92 }
93 sdallocx(p, 1, 0);
94 }
95
TEST_BEGIN(test_free_vs_dallocx)96 TEST_BEGIN(test_free_vs_dallocx)
97 {
98
99 compare_funcs(10*1000*1000, 100*1000*1000, "free", malloc_free,
100 "dallocx", malloc_dallocx);
101 }
102 TEST_END
103
TEST_BEGIN(test_dallocx_vs_sdallocx)104 TEST_BEGIN(test_dallocx_vs_sdallocx)
105 {
106
107 compare_funcs(10*1000*1000, 100*1000*1000, "dallocx", malloc_dallocx,
108 "sdallocx", malloc_sdallocx);
109 }
110 TEST_END
111
112 static void
malloc_mus_free(void)113 malloc_mus_free(void)
114 {
115 void *p;
116
117 p = malloc(1);
118 if (p == NULL) {
119 test_fail("Unexpected malloc() failure");
120 return;
121 }
122 malloc_usable_size(p);
123 free(p);
124 }
125
126 static void
malloc_sallocx_free(void)127 malloc_sallocx_free(void)
128 {
129 void *p;
130
131 p = malloc(1);
132 if (p == NULL) {
133 test_fail("Unexpected malloc() failure");
134 return;
135 }
136 if (sallocx(p, 0) < 1)
137 test_fail("Unexpected sallocx() failure");
138 free(p);
139 }
140
TEST_BEGIN(test_mus_vs_sallocx)141 TEST_BEGIN(test_mus_vs_sallocx)
142 {
143
144 compare_funcs(10*1000*1000, 100*1000*1000, "malloc_usable_size",
145 malloc_mus_free, "sallocx", malloc_sallocx_free);
146 }
147 TEST_END
148
149 static void
malloc_nallocx_free(void)150 malloc_nallocx_free(void)
151 {
152 void *p;
153
154 p = malloc(1);
155 if (p == NULL) {
156 test_fail("Unexpected malloc() failure");
157 return;
158 }
159 if (nallocx(1, 0) < 1)
160 test_fail("Unexpected nallocx() failure");
161 free(p);
162 }
163
TEST_BEGIN(test_sallocx_vs_nallocx)164 TEST_BEGIN(test_sallocx_vs_nallocx)
165 {
166
167 compare_funcs(10*1000*1000, 100*1000*1000, "sallocx",
168 malloc_sallocx_free, "nallocx", malloc_nallocx_free);
169 }
170 TEST_END
171
172 int
main(void)173 main(void)
174 {
175
176 return (test(
177 test_malloc_vs_mallocx,
178 test_free_vs_dallocx,
179 test_dallocx_vs_sdallocx,
180 test_mus_vs_sallocx,
181 test_sallocx_vs_nallocx));
182 }
183