1#include "test/jemalloc_test.h" 2 3JEMALLOC_INLINE_C void 4time_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 17void 18compare_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 43static void 44malloc_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 55static void 56mallocx_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 66TEST_BEGIN(test_malloc_vs_mallocx) 67{ 68 compare_funcs(10*1000*1000, 100*1000*1000, "malloc", 69 malloc_free, "mallocx", mallocx_free); 70} 71TEST_END 72 73static void 74malloc_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 84static void 85malloc_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 95TEST_BEGIN(test_free_vs_dallocx) 96{ 97 compare_funcs(10*1000*1000, 100*1000*1000, "free", malloc_free, 98 "dallocx", malloc_dallocx); 99} 100TEST_END 101 102TEST_BEGIN(test_dallocx_vs_sdallocx) 103{ 104 compare_funcs(10*1000*1000, 100*1000*1000, "dallocx", malloc_dallocx, 105 "sdallocx", malloc_sdallocx); 106} 107TEST_END 108 109static void 110malloc_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 123static void 124malloc_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 138TEST_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} 143TEST_END 144 145static void 146malloc_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 160TEST_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} 165TEST_END 166 167int 168main(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