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