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