mallocx.c revision ada8447cf6fb2c1f976b6311dade2e91026b3d83
1#include "test/jemalloc_test.h"
2
3#define	CHUNK 0x400000
4#define	MAXALIGN (((size_t)1) << 25)
5#define	NITER 4
6
7TEST_BEGIN(test_basic)
8{
9	size_t nsz, rsz, sz;
10	void *p;
11
12	sz = 42;
13	nsz = nallocx(sz, 0);
14	assert_zu_ne(nsz, 0, "Unexpected nallocx() error");
15	p = mallocx(sz, 0);
16	assert_ptr_not_null(p, "Unexpected mallocx() error");
17	rsz = sallocx(p, 0);
18	assert_zu_ge(rsz, sz, "Real size smaller than expected");
19	assert_zu_eq(nsz, rsz, "nallocx()/sallocx() size mismatch");
20	dallocx(p, 0);
21
22	p = mallocx(sz, 0);
23	assert_ptr_not_null(p, "Unexpected mallocx() error");
24	dallocx(p, 0);
25
26	nsz = nallocx(sz, MALLOCX_ZERO);
27	assert_zu_ne(nsz, 0, "Unexpected nallocx() error");
28	p = mallocx(sz, MALLOCX_ZERO);
29	assert_ptr_not_null(p, "Unexpected mallocx() error");
30	rsz = sallocx(p, 0);
31	assert_zu_eq(nsz, rsz, "nallocx()/sallocx() rsize mismatch");
32	dallocx(p, 0);
33}
34TEST_END
35
36TEST_BEGIN(test_alignment_and_size)
37{
38	size_t nsz, rsz, sz, alignment, total;
39	unsigned i;
40	void *ps[NITER];
41
42	for (i = 0; i < NITER; i++)
43		ps[i] = NULL;
44
45	for (alignment = 8;
46	    alignment <= MAXALIGN;
47	    alignment <<= 1) {
48		total = 0;
49		for (sz = 1;
50		    sz < 3 * alignment && sz < (1U << 31);
51		    sz += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
52			for (i = 0; i < NITER; i++) {
53				nsz = nallocx(sz, MALLOCX_ALIGN(alignment) |
54				    MALLOCX_ZERO);
55				assert_zu_ne(nsz, 0,
56				    "nallocx() error for alignment=%zu, "
57				    "size=%zu (%#zx)", alignment, sz, sz);
58				ps[i] = mallocx(sz, MALLOCX_ALIGN(alignment) |
59				    MALLOCX_ZERO);
60				assert_ptr_not_null(ps[i],
61				    "mallocx() error for alignment=%zu, "
62				    "size=%zu (%#zx)", alignment, sz, sz);
63				rsz = sallocx(ps[i], 0);
64				assert_zu_ge(rsz, sz,
65				    "Real size smaller than expected for "
66				    "alignment=%zu, size=%zu", alignment, sz);
67				assert_zu_eq(nsz, rsz,
68				    "nallocx()/sallocx() size mismatch for "
69				    "alignment=%zu, size=%zu", alignment, sz);
70				assert_ptr_null(
71				    (void *)((uintptr_t)ps[i] & (alignment-1)),
72				    "%p inadequately aligned for"
73				    " alignment=%zu, size=%zu", ps[i],
74				    alignment, sz);
75				total += rsz;
76				if (total >= (MAXALIGN << 1))
77					break;
78			}
79			for (i = 0; i < NITER; i++) {
80				if (ps[i] != NULL) {
81					dallocx(ps[i], 0);
82					ps[i] = NULL;
83				}
84			}
85		}
86	}
87}
88TEST_END
89
90int
91main(void)
92{
93
94	return (test(
95	    test_basic,
96	    test_alignment_and_size));
97}
98