Skip to content

Commit a1f3929

Browse files
author
Jason Evans
committed
Thwart optimization of free(malloc(1)) in microbench.
1 parent c54f93f commit a1f3929

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

test/stress/microbench.c

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,46 +31,52 @@ compare_funcs(uint64_t nwarmup, uint64_t niter, const char *name_a,
3131
}
3232

3333
static void
34-
malloc_vs_mallocx_malloc(void)
34+
malloc_free(void)
3535
{
36-
37-
free(malloc(1));
36+
/* The compiler can optimize away free(malloc(1))! */
37+
void *p = malloc(1);
38+
if (p == NULL) {
39+
test_fail("Unexpected malloc() failure");
40+
return;
41+
}
42+
free(p);
3843
}
3944

4045
static void
41-
malloc_vs_mallocx_mallocx(void)
46+
mallocx_free(void)
4247
{
43-
44-
free(mallocx(1, 0));
48+
void *p = mallocx(1, 0);
49+
if (p == NULL) {
50+
test_fail("Unexpected mallocx() failure");
51+
return;
52+
}
53+
free(p);
4554
}
4655

4756
TEST_BEGIN(test_malloc_vs_mallocx)
4857
{
4958

5059
compare_funcs(10*1000*1000, 100*1000*1000, "malloc",
51-
malloc_vs_mallocx_malloc, "mallocx", malloc_vs_mallocx_mallocx);
60+
malloc_free, "mallocx", mallocx_free);
5261
}
5362
TEST_END
5463

5564
static void
56-
free_vs_dallocx_free(void)
57-
{
58-
59-
free(malloc(1));
60-
}
61-
62-
static void
63-
free_vs_dallocx_dallocx(void)
65+
malloc_dallocx(void)
6466
{
65-
66-
dallocx(malloc(1), 0);
67+
void *p = malloc(1);
68+
if (p == NULL) {
69+
test_fail("Unexpected malloc() failure");
70+
return;
71+
}
72+
dallocx(p, 0);
6773
}
6874

6975
TEST_BEGIN(test_free_vs_dallocx)
7076
{
7177

72-
compare_funcs(10*1000*1000, 100*1000*1000, "free", free_vs_dallocx_free,
73-
"dallocx", free_vs_dallocx_dallocx);
78+
compare_funcs(10*1000*1000, 100*1000*1000, "free", malloc_free,
79+
"dallocx", malloc_dallocx);
7480
}
7581
TEST_END
7682

0 commit comments

Comments
 (0)