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