Thwart optimization of free(malloc(1)) in microbench.

This commit is contained in:
Jason Evans 2014-09-08 16:23:48 -07:00
parent c54f93f186
commit a1f3929ffd

View File

@ -31,46 +31,52 @@ compare_funcs(uint64_t nwarmup, uint64_t niter, const char *name_a,
} }
static void static void
malloc_vs_mallocx_malloc(void) malloc_free(void)
{ {
/* The compiler can optimize away free(malloc(1))! */
free(malloc(1)); void *p = malloc(1);
if (p == NULL) {
test_fail("Unexpected malloc() failure");
return;
}
free(p);
} }
static void static void
malloc_vs_mallocx_mallocx(void) mallocx_free(void)
{ {
void *p = mallocx(1, 0);
free(mallocx(1, 0)); if (p == NULL) {
test_fail("Unexpected mallocx() failure");
return;
}
free(p);
} }
TEST_BEGIN(test_malloc_vs_mallocx) TEST_BEGIN(test_malloc_vs_mallocx)
{ {
compare_funcs(10*1000*1000, 100*1000*1000, "malloc", compare_funcs(10*1000*1000, 100*1000*1000, "malloc",
malloc_vs_mallocx_malloc, "mallocx", malloc_vs_mallocx_mallocx); malloc_free, "mallocx", mallocx_free);
} }
TEST_END TEST_END
static void static void
free_vs_dallocx_free(void) malloc_dallocx(void)
{ {
void *p = malloc(1);
free(malloc(1)); if (p == NULL) {
} test_fail("Unexpected malloc() failure");
return;
static void }
free_vs_dallocx_dallocx(void) dallocx(p, 0);
{
dallocx(malloc(1), 0);
} }
TEST_BEGIN(test_free_vs_dallocx) TEST_BEGIN(test_free_vs_dallocx)
{ {
compare_funcs(10*1000*1000, 100*1000*1000, "free", free_vs_dallocx_free, compare_funcs(10*1000*1000, 100*1000*1000, "free", malloc_free,
"dallocx", free_vs_dallocx_dallocx); "dallocx", malloc_dallocx);
} }
TEST_END TEST_END