xref: /aosp_15_r20/external/jemalloc_new/test/unit/base.c (revision 1208bc7e437ced7eb82efac44ba17e3beba411da)
1*1208bc7eSAndroid Build Coastguard Worker #include "test/jemalloc_test.h"
2*1208bc7eSAndroid Build Coastguard Worker 
3*1208bc7eSAndroid Build Coastguard Worker #include "test/extent_hooks.h"
4*1208bc7eSAndroid Build Coastguard Worker 
5*1208bc7eSAndroid Build Coastguard Worker static extent_hooks_t hooks_null = {
6*1208bc7eSAndroid Build Coastguard Worker 	extent_alloc_hook,
7*1208bc7eSAndroid Build Coastguard Worker 	NULL, /* dalloc */
8*1208bc7eSAndroid Build Coastguard Worker 	NULL, /* destroy */
9*1208bc7eSAndroid Build Coastguard Worker 	NULL, /* commit */
10*1208bc7eSAndroid Build Coastguard Worker 	NULL, /* decommit */
11*1208bc7eSAndroid Build Coastguard Worker 	NULL, /* purge_lazy */
12*1208bc7eSAndroid Build Coastguard Worker 	NULL, /* purge_forced */
13*1208bc7eSAndroid Build Coastguard Worker 	NULL, /* split */
14*1208bc7eSAndroid Build Coastguard Worker 	NULL /* merge */
15*1208bc7eSAndroid Build Coastguard Worker };
16*1208bc7eSAndroid Build Coastguard Worker 
17*1208bc7eSAndroid Build Coastguard Worker static extent_hooks_t hooks_not_null = {
18*1208bc7eSAndroid Build Coastguard Worker 	extent_alloc_hook,
19*1208bc7eSAndroid Build Coastguard Worker 	extent_dalloc_hook,
20*1208bc7eSAndroid Build Coastguard Worker 	extent_destroy_hook,
21*1208bc7eSAndroid Build Coastguard Worker 	NULL, /* commit */
22*1208bc7eSAndroid Build Coastguard Worker 	extent_decommit_hook,
23*1208bc7eSAndroid Build Coastguard Worker 	extent_purge_lazy_hook,
24*1208bc7eSAndroid Build Coastguard Worker 	extent_purge_forced_hook,
25*1208bc7eSAndroid Build Coastguard Worker 	NULL, /* split */
26*1208bc7eSAndroid Build Coastguard Worker 	NULL /* merge */
27*1208bc7eSAndroid Build Coastguard Worker };
28*1208bc7eSAndroid Build Coastguard Worker 
TEST_BEGIN(test_base_hooks_default)29*1208bc7eSAndroid Build Coastguard Worker TEST_BEGIN(test_base_hooks_default) {
30*1208bc7eSAndroid Build Coastguard Worker 	base_t *base;
31*1208bc7eSAndroid Build Coastguard Worker 	size_t allocated0, allocated1, resident, mapped, n_thp;
32*1208bc7eSAndroid Build Coastguard Worker 
33*1208bc7eSAndroid Build Coastguard Worker 	tsdn_t *tsdn = tsd_tsdn(tsd_fetch());
34*1208bc7eSAndroid Build Coastguard Worker 	base = base_new(tsdn, 0, (extent_hooks_t *)&extent_hooks_default);
35*1208bc7eSAndroid Build Coastguard Worker 
36*1208bc7eSAndroid Build Coastguard Worker 	if (config_stats) {
37*1208bc7eSAndroid Build Coastguard Worker 		base_stats_get(tsdn, base, &allocated0, &resident, &mapped,
38*1208bc7eSAndroid Build Coastguard Worker 		    &n_thp);
39*1208bc7eSAndroid Build Coastguard Worker 		assert_zu_ge(allocated0, sizeof(base_t),
40*1208bc7eSAndroid Build Coastguard Worker 		    "Base header should count as allocated");
41*1208bc7eSAndroid Build Coastguard Worker 		if (opt_metadata_thp == metadata_thp_always) {
42*1208bc7eSAndroid Build Coastguard Worker 			assert_zu_gt(n_thp, 0,
43*1208bc7eSAndroid Build Coastguard Worker 			    "Base should have 1 THP at least.");
44*1208bc7eSAndroid Build Coastguard Worker 		}
45*1208bc7eSAndroid Build Coastguard Worker 	}
46*1208bc7eSAndroid Build Coastguard Worker 
47*1208bc7eSAndroid Build Coastguard Worker 	assert_ptr_not_null(base_alloc(tsdn, base, 42, 1),
48*1208bc7eSAndroid Build Coastguard Worker 	    "Unexpected base_alloc() failure");
49*1208bc7eSAndroid Build Coastguard Worker 
50*1208bc7eSAndroid Build Coastguard Worker 	if (config_stats) {
51*1208bc7eSAndroid Build Coastguard Worker 		base_stats_get(tsdn, base, &allocated1, &resident, &mapped,
52*1208bc7eSAndroid Build Coastguard Worker 		    &n_thp);
53*1208bc7eSAndroid Build Coastguard Worker 		assert_zu_ge(allocated1 - allocated0, 42,
54*1208bc7eSAndroid Build Coastguard Worker 		    "At least 42 bytes were allocated by base_alloc()");
55*1208bc7eSAndroid Build Coastguard Worker 	}
56*1208bc7eSAndroid Build Coastguard Worker 
57*1208bc7eSAndroid Build Coastguard Worker 	base_delete(tsdn, base);
58*1208bc7eSAndroid Build Coastguard Worker }
59*1208bc7eSAndroid Build Coastguard Worker TEST_END
60*1208bc7eSAndroid Build Coastguard Worker 
TEST_BEGIN(test_base_hooks_null)61*1208bc7eSAndroid Build Coastguard Worker TEST_BEGIN(test_base_hooks_null) {
62*1208bc7eSAndroid Build Coastguard Worker 	extent_hooks_t hooks_orig;
63*1208bc7eSAndroid Build Coastguard Worker 	base_t *base;
64*1208bc7eSAndroid Build Coastguard Worker 	size_t allocated0, allocated1, resident, mapped, n_thp;
65*1208bc7eSAndroid Build Coastguard Worker 
66*1208bc7eSAndroid Build Coastguard Worker 	extent_hooks_prep();
67*1208bc7eSAndroid Build Coastguard Worker 	try_dalloc = false;
68*1208bc7eSAndroid Build Coastguard Worker 	try_destroy = true;
69*1208bc7eSAndroid Build Coastguard Worker 	try_decommit = false;
70*1208bc7eSAndroid Build Coastguard Worker 	try_purge_lazy = false;
71*1208bc7eSAndroid Build Coastguard Worker 	try_purge_forced = false;
72*1208bc7eSAndroid Build Coastguard Worker 	memcpy(&hooks_orig, &hooks, sizeof(extent_hooks_t));
73*1208bc7eSAndroid Build Coastguard Worker 	memcpy(&hooks, &hooks_null, sizeof(extent_hooks_t));
74*1208bc7eSAndroid Build Coastguard Worker 
75*1208bc7eSAndroid Build Coastguard Worker 	tsdn_t *tsdn = tsd_tsdn(tsd_fetch());
76*1208bc7eSAndroid Build Coastguard Worker 	base = base_new(tsdn, 0, &hooks);
77*1208bc7eSAndroid Build Coastguard Worker 	assert_ptr_not_null(base, "Unexpected base_new() failure");
78*1208bc7eSAndroid Build Coastguard Worker 
79*1208bc7eSAndroid Build Coastguard Worker 	if (config_stats) {
80*1208bc7eSAndroid Build Coastguard Worker 		base_stats_get(tsdn, base, &allocated0, &resident, &mapped,
81*1208bc7eSAndroid Build Coastguard Worker 		    &n_thp);
82*1208bc7eSAndroid Build Coastguard Worker 		assert_zu_ge(allocated0, sizeof(base_t),
83*1208bc7eSAndroid Build Coastguard Worker 		    "Base header should count as allocated");
84*1208bc7eSAndroid Build Coastguard Worker 		if (opt_metadata_thp == metadata_thp_always) {
85*1208bc7eSAndroid Build Coastguard Worker 			assert_zu_gt(n_thp, 0,
86*1208bc7eSAndroid Build Coastguard Worker 			    "Base should have 1 THP at least.");
87*1208bc7eSAndroid Build Coastguard Worker 		}
88*1208bc7eSAndroid Build Coastguard Worker 	}
89*1208bc7eSAndroid Build Coastguard Worker 
90*1208bc7eSAndroid Build Coastguard Worker 	assert_ptr_not_null(base_alloc(tsdn, base, 42, 1),
91*1208bc7eSAndroid Build Coastguard Worker 	    "Unexpected base_alloc() failure");
92*1208bc7eSAndroid Build Coastguard Worker 
93*1208bc7eSAndroid Build Coastguard Worker 	if (config_stats) {
94*1208bc7eSAndroid Build Coastguard Worker 		base_stats_get(tsdn, base, &allocated1, &resident, &mapped,
95*1208bc7eSAndroid Build Coastguard Worker 		    &n_thp);
96*1208bc7eSAndroid Build Coastguard Worker 		assert_zu_ge(allocated1 - allocated0, 42,
97*1208bc7eSAndroid Build Coastguard Worker 		    "At least 42 bytes were allocated by base_alloc()");
98*1208bc7eSAndroid Build Coastguard Worker 	}
99*1208bc7eSAndroid Build Coastguard Worker 
100*1208bc7eSAndroid Build Coastguard Worker 	base_delete(tsdn, base);
101*1208bc7eSAndroid Build Coastguard Worker 
102*1208bc7eSAndroid Build Coastguard Worker 	memcpy(&hooks, &hooks_orig, sizeof(extent_hooks_t));
103*1208bc7eSAndroid Build Coastguard Worker }
104*1208bc7eSAndroid Build Coastguard Worker TEST_END
105*1208bc7eSAndroid Build Coastguard Worker 
TEST_BEGIN(test_base_hooks_not_null)106*1208bc7eSAndroid Build Coastguard Worker TEST_BEGIN(test_base_hooks_not_null) {
107*1208bc7eSAndroid Build Coastguard Worker 	extent_hooks_t hooks_orig;
108*1208bc7eSAndroid Build Coastguard Worker 	base_t *base;
109*1208bc7eSAndroid Build Coastguard Worker 	void *p, *q, *r, *r_exp;
110*1208bc7eSAndroid Build Coastguard Worker 
111*1208bc7eSAndroid Build Coastguard Worker 	extent_hooks_prep();
112*1208bc7eSAndroid Build Coastguard Worker 	try_dalloc = false;
113*1208bc7eSAndroid Build Coastguard Worker 	try_destroy = true;
114*1208bc7eSAndroid Build Coastguard Worker 	try_decommit = false;
115*1208bc7eSAndroid Build Coastguard Worker 	try_purge_lazy = false;
116*1208bc7eSAndroid Build Coastguard Worker 	try_purge_forced = false;
117*1208bc7eSAndroid Build Coastguard Worker 	memcpy(&hooks_orig, &hooks, sizeof(extent_hooks_t));
118*1208bc7eSAndroid Build Coastguard Worker 	memcpy(&hooks, &hooks_not_null, sizeof(extent_hooks_t));
119*1208bc7eSAndroid Build Coastguard Worker 
120*1208bc7eSAndroid Build Coastguard Worker 	tsdn_t *tsdn = tsd_tsdn(tsd_fetch());
121*1208bc7eSAndroid Build Coastguard Worker 	did_alloc = false;
122*1208bc7eSAndroid Build Coastguard Worker 	base = base_new(tsdn, 0, &hooks);
123*1208bc7eSAndroid Build Coastguard Worker 	assert_ptr_not_null(base, "Unexpected base_new() failure");
124*1208bc7eSAndroid Build Coastguard Worker 	assert_true(did_alloc, "Expected alloc");
125*1208bc7eSAndroid Build Coastguard Worker 
126*1208bc7eSAndroid Build Coastguard Worker 	/*
127*1208bc7eSAndroid Build Coastguard Worker 	 * Check for tight packing at specified alignment under simple
128*1208bc7eSAndroid Build Coastguard Worker 	 * conditions.
129*1208bc7eSAndroid Build Coastguard Worker 	 */
130*1208bc7eSAndroid Build Coastguard Worker 	{
131*1208bc7eSAndroid Build Coastguard Worker 		const size_t alignments[] = {
132*1208bc7eSAndroid Build Coastguard Worker 			1,
133*1208bc7eSAndroid Build Coastguard Worker 			QUANTUM,
134*1208bc7eSAndroid Build Coastguard Worker 			QUANTUM << 1,
135*1208bc7eSAndroid Build Coastguard Worker 			CACHELINE,
136*1208bc7eSAndroid Build Coastguard Worker 			CACHELINE << 1,
137*1208bc7eSAndroid Build Coastguard Worker 		};
138*1208bc7eSAndroid Build Coastguard Worker 		unsigned i;
139*1208bc7eSAndroid Build Coastguard Worker 
140*1208bc7eSAndroid Build Coastguard Worker 		for (i = 0; i < sizeof(alignments) / sizeof(size_t); i++) {
141*1208bc7eSAndroid Build Coastguard Worker 			size_t alignment = alignments[i];
142*1208bc7eSAndroid Build Coastguard Worker 			size_t align_ceil = ALIGNMENT_CEILING(alignment,
143*1208bc7eSAndroid Build Coastguard Worker 			    QUANTUM);
144*1208bc7eSAndroid Build Coastguard Worker 			p = base_alloc(tsdn, base, 1, alignment);
145*1208bc7eSAndroid Build Coastguard Worker 			assert_ptr_not_null(p,
146*1208bc7eSAndroid Build Coastguard Worker 			    "Unexpected base_alloc() failure");
147*1208bc7eSAndroid Build Coastguard Worker 			assert_ptr_eq(p,
148*1208bc7eSAndroid Build Coastguard Worker 			    (void *)(ALIGNMENT_CEILING((uintptr_t)p,
149*1208bc7eSAndroid Build Coastguard Worker 			    alignment)), "Expected quantum alignment");
150*1208bc7eSAndroid Build Coastguard Worker 			q = base_alloc(tsdn, base, alignment, alignment);
151*1208bc7eSAndroid Build Coastguard Worker 			assert_ptr_not_null(q,
152*1208bc7eSAndroid Build Coastguard Worker 			    "Unexpected base_alloc() failure");
153*1208bc7eSAndroid Build Coastguard Worker 			assert_ptr_eq((void *)((uintptr_t)p + align_ceil), q,
154*1208bc7eSAndroid Build Coastguard Worker 			    "Minimal allocation should take up %zu bytes",
155*1208bc7eSAndroid Build Coastguard Worker 			    align_ceil);
156*1208bc7eSAndroid Build Coastguard Worker 			r = base_alloc(tsdn, base, 1, alignment);
157*1208bc7eSAndroid Build Coastguard Worker 			assert_ptr_not_null(r,
158*1208bc7eSAndroid Build Coastguard Worker 			    "Unexpected base_alloc() failure");
159*1208bc7eSAndroid Build Coastguard Worker 			assert_ptr_eq((void *)((uintptr_t)q + align_ceil), r,
160*1208bc7eSAndroid Build Coastguard Worker 			    "Minimal allocation should take up %zu bytes",
161*1208bc7eSAndroid Build Coastguard Worker 			    align_ceil);
162*1208bc7eSAndroid Build Coastguard Worker 		}
163*1208bc7eSAndroid Build Coastguard Worker 	}
164*1208bc7eSAndroid Build Coastguard Worker 
165*1208bc7eSAndroid Build Coastguard Worker 	/*
166*1208bc7eSAndroid Build Coastguard Worker 	 * Allocate an object that cannot fit in the first block, then verify
167*1208bc7eSAndroid Build Coastguard Worker 	 * that the first block's remaining space is considered for subsequent
168*1208bc7eSAndroid Build Coastguard Worker 	 * allocation.
169*1208bc7eSAndroid Build Coastguard Worker 	 */
170*1208bc7eSAndroid Build Coastguard Worker 	assert_zu_ge(extent_bsize_get(&base->blocks->extent), QUANTUM,
171*1208bc7eSAndroid Build Coastguard Worker 	    "Remainder insufficient for test");
172*1208bc7eSAndroid Build Coastguard Worker 	/* Use up all but one quantum of block. */
173*1208bc7eSAndroid Build Coastguard Worker 	while (extent_bsize_get(&base->blocks->extent) > QUANTUM) {
174*1208bc7eSAndroid Build Coastguard Worker 		p = base_alloc(tsdn, base, QUANTUM, QUANTUM);
175*1208bc7eSAndroid Build Coastguard Worker 		assert_ptr_not_null(p, "Unexpected base_alloc() failure");
176*1208bc7eSAndroid Build Coastguard Worker 	}
177*1208bc7eSAndroid Build Coastguard Worker 	r_exp = extent_addr_get(&base->blocks->extent);
178*1208bc7eSAndroid Build Coastguard Worker 	assert_zu_eq(base->extent_sn_next, 1, "One extant block expected");
179*1208bc7eSAndroid Build Coastguard Worker 	q = base_alloc(tsdn, base, QUANTUM + 1, QUANTUM);
180*1208bc7eSAndroid Build Coastguard Worker 	assert_ptr_not_null(q, "Unexpected base_alloc() failure");
181*1208bc7eSAndroid Build Coastguard Worker 	assert_ptr_ne(q, r_exp, "Expected allocation from new block");
182*1208bc7eSAndroid Build Coastguard Worker 	assert_zu_eq(base->extent_sn_next, 2, "Two extant blocks expected");
183*1208bc7eSAndroid Build Coastguard Worker 	r = base_alloc(tsdn, base, QUANTUM, QUANTUM);
184*1208bc7eSAndroid Build Coastguard Worker 	assert_ptr_not_null(r, "Unexpected base_alloc() failure");
185*1208bc7eSAndroid Build Coastguard Worker 	assert_ptr_eq(r, r_exp, "Expected allocation from first block");
186*1208bc7eSAndroid Build Coastguard Worker 	assert_zu_eq(base->extent_sn_next, 2, "Two extant blocks expected");
187*1208bc7eSAndroid Build Coastguard Worker 
188*1208bc7eSAndroid Build Coastguard Worker 	/*
189*1208bc7eSAndroid Build Coastguard Worker 	 * Check for proper alignment support when normal blocks are too small.
190*1208bc7eSAndroid Build Coastguard Worker 	 */
191*1208bc7eSAndroid Build Coastguard Worker 	{
192*1208bc7eSAndroid Build Coastguard Worker 		const size_t alignments[] = {
193*1208bc7eSAndroid Build Coastguard Worker 			HUGEPAGE,
194*1208bc7eSAndroid Build Coastguard Worker 			HUGEPAGE << 1
195*1208bc7eSAndroid Build Coastguard Worker 		};
196*1208bc7eSAndroid Build Coastguard Worker 		unsigned i;
197*1208bc7eSAndroid Build Coastguard Worker 
198*1208bc7eSAndroid Build Coastguard Worker 		for (i = 0; i < sizeof(alignments) / sizeof(size_t); i++) {
199*1208bc7eSAndroid Build Coastguard Worker 			size_t alignment = alignments[i];
200*1208bc7eSAndroid Build Coastguard Worker 			p = base_alloc(tsdn, base, QUANTUM, alignment);
201*1208bc7eSAndroid Build Coastguard Worker 			assert_ptr_not_null(p,
202*1208bc7eSAndroid Build Coastguard Worker 			    "Unexpected base_alloc() failure");
203*1208bc7eSAndroid Build Coastguard Worker 			assert_ptr_eq(p,
204*1208bc7eSAndroid Build Coastguard Worker 			    (void *)(ALIGNMENT_CEILING((uintptr_t)p,
205*1208bc7eSAndroid Build Coastguard Worker 			    alignment)), "Expected %zu-byte alignment",
206*1208bc7eSAndroid Build Coastguard Worker 			    alignment);
207*1208bc7eSAndroid Build Coastguard Worker 		}
208*1208bc7eSAndroid Build Coastguard Worker 	}
209*1208bc7eSAndroid Build Coastguard Worker 
210*1208bc7eSAndroid Build Coastguard Worker 	called_dalloc = called_destroy = called_decommit = called_purge_lazy =
211*1208bc7eSAndroid Build Coastguard Worker 	    called_purge_forced = false;
212*1208bc7eSAndroid Build Coastguard Worker 	base_delete(tsdn, base);
213*1208bc7eSAndroid Build Coastguard Worker 	assert_true(called_dalloc, "Expected dalloc call");
214*1208bc7eSAndroid Build Coastguard Worker 	assert_true(!called_destroy, "Unexpected destroy call");
215*1208bc7eSAndroid Build Coastguard Worker 	assert_true(called_decommit, "Expected decommit call");
216*1208bc7eSAndroid Build Coastguard Worker 	assert_true(called_purge_lazy, "Expected purge_lazy call");
217*1208bc7eSAndroid Build Coastguard Worker 	assert_true(called_purge_forced, "Expected purge_forced call");
218*1208bc7eSAndroid Build Coastguard Worker 
219*1208bc7eSAndroid Build Coastguard Worker 	try_dalloc = true;
220*1208bc7eSAndroid Build Coastguard Worker 	try_destroy = true;
221*1208bc7eSAndroid Build Coastguard Worker 	try_decommit = true;
222*1208bc7eSAndroid Build Coastguard Worker 	try_purge_lazy = true;
223*1208bc7eSAndroid Build Coastguard Worker 	try_purge_forced = true;
224*1208bc7eSAndroid Build Coastguard Worker 	memcpy(&hooks, &hooks_orig, sizeof(extent_hooks_t));
225*1208bc7eSAndroid Build Coastguard Worker }
226*1208bc7eSAndroid Build Coastguard Worker TEST_END
227*1208bc7eSAndroid Build Coastguard Worker 
228*1208bc7eSAndroid Build Coastguard Worker int
main(void)229*1208bc7eSAndroid Build Coastguard Worker main(void) {
230*1208bc7eSAndroid Build Coastguard Worker 	return test(
231*1208bc7eSAndroid Build Coastguard Worker 	    test_base_hooks_default,
232*1208bc7eSAndroid Build Coastguard Worker 	    test_base_hooks_null,
233*1208bc7eSAndroid Build Coastguard Worker 	    test_base_hooks_not_null);
234*1208bc7eSAndroid Build Coastguard Worker }
235