summaryrefslogtreecommitdiff
path: root/deps/jemalloc/test/unit/base.c
blob: 6b792cf21ad2b0c705d48cf70b84a12ef7f12235 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "test/jemalloc_test.h"

#include "test/extent_hooks.h"

static extent_hooks_t hooks_null = {
	extent_alloc_hook,
	NULL, /* dalloc */
	NULL, /* destroy */
	NULL, /* commit */
	NULL, /* decommit */
	NULL, /* purge_lazy */
	NULL, /* purge_forced */
	NULL, /* split */
	NULL /* merge */
};

static extent_hooks_t hooks_not_null = {
	extent_alloc_hook,
	extent_dalloc_hook,
	extent_destroy_hook,
	NULL, /* commit */
	extent_decommit_hook,
	extent_purge_lazy_hook,
	extent_purge_forced_hook,
	NULL, /* split */
	NULL /* merge */
};

TEST_BEGIN(test_base_hooks_default) {
	base_t *base;
	size_t allocated0, allocated1, resident, mapped, n_thp;

	tsdn_t *tsdn = tsd_tsdn(tsd_fetch());
	base = base_new(tsdn, 0, (extent_hooks_t *)&extent_hooks_default);

	if (config_stats) {
		base_stats_get(tsdn, base, &allocated0, &resident, &mapped,
		    &n_thp);
		assert_zu_ge(allocated0, sizeof(base_t),
		    "Base header should count as allocated");
		if (opt_metadata_thp == metadata_thp_always) {
			assert_zu_gt(n_thp, 0,
			    "Base should have 1 THP at least.");
		}
	}

	assert_ptr_not_null(base_alloc(tsdn, base, 42, 1),
	    "Unexpected base_alloc() failure");

	if (config_stats) {
		base_stats_get(tsdn, base, &allocated1, &resident, &mapped,
		    &n_thp);
		assert_zu_ge(allocated1 - allocated0, 42,
		    "At least 42 bytes were allocated by base_alloc()");
	}

	base_delete(tsdn, base);
}
TEST_END

TEST_BEGIN(test_base_hooks_null) {
	extent_hooks_t hooks_orig;
	base_t *base;
	size_t allocated0, allocated1, resident, mapped, n_thp;

	extent_hooks_prep();
	try_dalloc = false;
	try_destroy = true;
	try_decommit = false;
	try_purge_lazy = false;
	try_purge_forced = false;
	memcpy(&hooks_orig, &hooks, sizeof(extent_hooks_t));
	memcpy(&hooks, &hooks_null, sizeof(extent_hooks_t));

	tsdn_t *tsdn = tsd_tsdn(tsd_fetch());
	base = base_new(tsdn, 0, &hooks);
	assert_ptr_not_null(base, "Unexpected base_new() failure");

	if (config_stats) {
		base_stats_get(tsdn, base, &allocated0, &resident, &mapped,
		    &n_thp);
		assert_zu_ge(allocated0, sizeof(base_t),
		    "Base header should count as allocated");
		if (opt_metadata_thp == metadata_thp_always) {
			assert_zu_gt(n_thp, 0,
			    "Base should have 1 THP at least.");
		}
	}

	assert_ptr_not_null(base_alloc(tsdn, base, 42, 1),
	    "Unexpected base_alloc() failure");

	if (config_stats) {
		base_stats_get(tsdn, base, &allocated1, &resident, &mapped,
		    &n_thp);
		assert_zu_ge(allocated1 - allocated0, 42,
		    "At least 42 bytes were allocated by base_alloc()");
	}

	base_delete(tsdn, base);

	memcpy(&hooks, &hooks_orig, sizeof(extent_hooks_t));
}
TEST_END

TEST_BEGIN(test_base_hooks_not_null) {
	extent_hooks_t hooks_orig;
	base_t *base;
	void *p, *q, *r, *r_exp;

	extent_hooks_prep();
	try_dalloc = false;
	try_destroy = true;
	try_decommit = false;
	try_purge_lazy = false;
	try_purge_forced = false;
	memcpy(&hooks_orig, &hooks, sizeof(extent_hooks_t));
	memcpy(&hooks, &hooks_not_null, sizeof(extent_hooks_t));

	tsdn_t *tsdn = tsd_tsdn(tsd_fetch());
	did_alloc = false;
	base = base_new(tsdn, 0, &hooks);
	assert_ptr_not_null(base, "Unexpected base_new() failure");
	assert_true(did_alloc, "Expected alloc");

	/*
	 * Check for tight packing at specified alignment under simple
	 * conditions.
	 */
	{
		const size_t alignments[] = {
			1,
			QUANTUM,
			QUANTUM << 1,
			CACHELINE,
			CACHELINE << 1,
		};
		unsigned i;

		for (i = 0; i < sizeof(alignments) / sizeof(size_t); i++) {
			size_t alignment = alignments[i];
			size_t align_ceil = ALIGNMENT_CEILING(alignment,
			    QUANTUM);
			p = base_alloc(tsdn, base, 1, alignment);
			assert_ptr_not_null(p,
			    "Unexpected base_alloc() failure");
			assert_ptr_eq(p,
			    (void *)(ALIGNMENT_CEILING((uintptr_t)p,
			    alignment)), "Expected quantum alignment");
			q = base_alloc(tsdn, base, alignment, alignment);
			assert_ptr_not_null(q,
			    "Unexpected base_alloc() failure");
			assert_ptr_eq((void *)((uintptr_t)p + align_ceil), q,
			    "Minimal allocation should take up %zu bytes",
			    align_ceil);
			r = base_alloc(tsdn, base, 1, alignment);
			assert_ptr_not_null(r,
			    "Unexpected base_alloc() failure");
			assert_ptr_eq((void *)((uintptr_t)q + align_ceil), r,
			    "Minimal allocation should take up %zu bytes",
			    align_ceil);
		}
	}

	/*
	 * Allocate an object that cannot fit in the first block, then verify
	 * that the first block's remaining space is considered for subsequent
	 * allocation.
	 */
	assert_zu_ge(extent_bsize_get(&base->blocks->extent), QUANTUM,
	    "Remainder insufficient for test");
	/* Use up all but one quantum of block. */
	while (extent_bsize_get(&base->blocks->extent) > QUANTUM) {
		p = base_alloc(tsdn, base, QUANTUM, QUANTUM);
		assert_ptr_not_null(p, "Unexpected base_alloc() failure");
	}
	r_exp = extent_addr_get(&base->blocks->extent);
	assert_zu_eq(base->extent_sn_next, 1, "One extant block expected");
	q = base_alloc(tsdn, base, QUANTUM + 1, QUANTUM);
	assert_ptr_not_null(q, "Unexpected base_alloc() failure");
	assert_ptr_ne(q, r_exp, "Expected allocation from new block");
	assert_zu_eq(base->extent_sn_next, 2, "Two extant blocks expected");
	r = base_alloc(tsdn, base, QUANTUM, QUANTUM);
	assert_ptr_not_null(r, "Unexpected base_alloc() failure");
	assert_ptr_eq(r, r_exp, "Expected allocation from first block");
	assert_zu_eq(base->extent_sn_next, 2, "Two extant blocks expected");

	/*
	 * Check for proper alignment support when normal blocks are too small.
	 */
	{
		const size_t alignments[] = {
			HUGEPAGE,
			HUGEPAGE << 1
		};
		unsigned i;

		for (i = 0; i < sizeof(alignments) / sizeof(size_t); i++) {
			size_t alignment = alignments[i];
			p = base_alloc(tsdn, base, QUANTUM, alignment);
			assert_ptr_not_null(p,
			    "Unexpected base_alloc() failure");
			assert_ptr_eq(p,
			    (void *)(ALIGNMENT_CEILING((uintptr_t)p,
			    alignment)), "Expected %zu-byte alignment",
			    alignment);
		}
	}

	called_dalloc = called_destroy = called_decommit = called_purge_lazy =
	    called_purge_forced = false;
	base_delete(tsdn, base);
	assert_true(called_dalloc, "Expected dalloc call");
	assert_true(!called_destroy, "Unexpected destroy call");
	assert_true(called_decommit, "Expected decommit call");
	assert_true(called_purge_lazy, "Expected purge_lazy call");
	assert_true(called_purge_forced, "Expected purge_forced call");

	try_dalloc = true;
	try_destroy = true;
	try_decommit = true;
	try_purge_lazy = true;
	try_purge_forced = true;
	memcpy(&hooks, &hooks_orig, sizeof(extent_hooks_t));
}
TEST_END

int
main(void) {
	return test(
	    test_base_hooks_default,
	    test_base_hooks_null,
	    test_base_hooks_not_null);
}