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 compare_funcs(10*1000*1000, 100*1000*1000, "malloc",
69 malloc_free, "mallocx", mallocx_free);
70 }
71 TEST_END
72
73 static void
malloc_dallocx(void)74 malloc_dallocx(void)
75 {
76 void *p = malloc(1);
77 if (p == NULL) {
78 test_fail("Unexpected malloc() failure");
79 return;
80 }
81 dallocx(p, 0);
82 }
83
84 static void
malloc_sdallocx(void)85 malloc_sdallocx(void)
86 {
87 void *p = malloc(1);
88 if (p == NULL) {
89 test_fail("Unexpected malloc() failure");
90 return;
91 }
92 sdallocx(p, 1, 0);
93 }
94
TEST_BEGIN(test_free_vs_dallocx)95 TEST_BEGIN(test_free_vs_dallocx)
96 {
97 compare_funcs(10*1000*1000, 100*1000*1000, "free", malloc_free,
98 "dallocx", malloc_dallocx);
99 }
100 TEST_END
101
TEST_BEGIN(test_dallocx_vs_sdallocx)102 TEST_BEGIN(test_dallocx_vs_sdallocx)
103 {
104 compare_funcs(10*1000*1000, 100*1000*1000, "dallocx", malloc_dallocx,
105 "sdallocx", malloc_sdallocx);
106 }
107 TEST_END
108
109 static void
malloc_mus_free(void)110 malloc_mus_free(void)
111 {
112 void *p;
113
114 p = malloc(1);
115 if (p == NULL) {
116 test_fail("Unexpected malloc() failure");
117 return;
118 }
119 malloc_usable_size(p);
120 free(p);
121 }
122
123 static void
malloc_sallocx_free(void)124 malloc_sallocx_free(void)
125 {
126 void *p;
127
128 p = malloc(1);
129 if (p == NULL) {
130 test_fail("Unexpected malloc() failure");
131 return;
132 }
133 if (sallocx(p, 0) < 1)
134 test_fail("Unexpected sallocx() failure");
135 free(p);
136 }
137
TEST_BEGIN(test_mus_vs_sallocx)138 TEST_BEGIN(test_mus_vs_sallocx)
139 {
140 compare_funcs(10*1000*1000, 100*1000*1000, "malloc_usable_size",
141 malloc_mus_free, "sallocx", malloc_sallocx_free);
142 }
143 TEST_END
144
145 static void
malloc_nallocx_free(void)146 malloc_nallocx_free(void)
147 {
148 void *p;
149
150 p = malloc(1);
151 if (p == NULL) {
152 test_fail("Unexpected malloc() failure");
153 return;
154 }
155 if (nallocx(1, 0) < 1)
156 test_fail("Unexpected nallocx() failure");
157 free(p);
158 }
159
TEST_BEGIN(test_sallocx_vs_nallocx)160 TEST_BEGIN(test_sallocx_vs_nallocx)
161 {
162 compare_funcs(10*1000*1000, 100*1000*1000, "sallocx",
163 malloc_sallocx_free, "nallocx", malloc_nallocx_free);
164 }
165 TEST_END
166
167 int
main(void)168 main(void)
169 {
170 return (test(
171 test_malloc_vs_mallocx,
172 test_free_vs_dallocx,
173 test_dallocx_vs_sdallocx,
174 test_mus_vs_sallocx,
175 test_sallocx_vs_nallocx));
176 }
177