]>
git.proxmox.com Git - rustc.git/blob - src/jemalloc/test/integration/mallocx.c
1 #include "test/jemalloc_test.h"
4 get_nsizes_impl(const char *cmd
)
10 assert_d_eq(mallctl(cmd
, &ret
, &z
, NULL
, 0), 0,
11 "Unexpected mallctl(\"%s\", ...) failure", cmd
);
20 return (get_nsizes_impl("arenas.nhchunks"));
24 get_size_impl(const char *cmd
, size_t ind
)
32 assert_d_eq(mallctlnametomib(cmd
, mib
, &miblen
),
33 0, "Unexpected mallctlnametomib(\"%s\", ...) failure", cmd
);
36 assert_d_eq(mallctlbymib(mib
, miblen
, &ret
, &z
, NULL
, 0),
37 0, "Unexpected mallctlbymib([\"%s\", %zu], ...) failure", cmd
, ind
);
43 get_huge_size(size_t ind
)
46 return (get_size_impl("arenas.hchunk.0.size", ind
));
49 TEST_BEGIN(test_overflow
)
53 hugemax
= get_huge_size(get_nhuge()-1);
55 assert_ptr_null(mallocx(hugemax
+1, 0),
56 "Expected OOM for mallocx(size=%#zx, 0)", hugemax
+1);
58 assert_ptr_null(mallocx(ZU(PTRDIFF_MAX
)+1, 0),
59 "Expected OOM for mallocx(size=%#zx, 0)", ZU(PTRDIFF_MAX
)+1);
61 assert_ptr_null(mallocx(SIZE_T_MAX
, 0),
62 "Expected OOM for mallocx(size=%#zx, 0)", SIZE_T_MAX
);
64 assert_ptr_null(mallocx(1, MALLOCX_ALIGN(ZU(PTRDIFF_MAX
)+1)),
65 "Expected OOM for mallocx(size=1, MALLOCX_ALIGN(%#zx))",
72 size_t hugemax
, size
, alignment
;
74 hugemax
= get_huge_size(get_nhuge()-1);
77 * It should be impossible to allocate two objects that each consume
78 * more than half the virtual address space.
83 p
= mallocx(hugemax
, 0);
85 assert_ptr_null(mallocx(hugemax
, 0),
86 "Expected OOM for mallocx(size=%#zx, 0)", hugemax
);
91 #if LG_SIZEOF_PTR == 3
92 size
= ZU(0x8000000000000000);
93 alignment
= ZU(0x8000000000000000);
95 size
= ZU(0x80000000);
96 alignment
= ZU(0x80000000);
98 assert_ptr_null(mallocx(size
, MALLOCX_ALIGN(alignment
)),
99 "Expected OOM for mallocx(size=%#zx, MALLOCX_ALIGN(%#zx)", size
,
104 TEST_BEGIN(test_basic
)
106 #define MAXSZ (((size_t)1) << 26)
109 for (sz
= 1; sz
< MAXSZ
; sz
= nallocx(sz
, 0) + 1) {
112 nsz
= nallocx(sz
, 0);
113 assert_zu_ne(nsz
, 0, "Unexpected nallocx() error");
115 assert_ptr_not_null(p
, "Unexpected mallocx() error");
117 assert_zu_ge(rsz
, sz
, "Real size smaller than expected");
118 assert_zu_eq(nsz
, rsz
, "nallocx()/sallocx() size mismatch");
122 assert_ptr_not_null(p
, "Unexpected mallocx() error");
125 nsz
= nallocx(sz
, MALLOCX_ZERO
);
126 assert_zu_ne(nsz
, 0, "Unexpected nallocx() error");
127 p
= mallocx(sz
, MALLOCX_ZERO
);
128 assert_ptr_not_null(p
, "Unexpected mallocx() error");
130 assert_zu_eq(nsz
, rsz
, "nallocx()/sallocx() rsize mismatch");
137 TEST_BEGIN(test_alignment_and_size
)
139 #define MAXALIGN (((size_t)1) << 25)
141 size_t nsz
, rsz
, sz
, alignment
, total
;
145 for (i
= 0; i
< NITER
; i
++)
149 alignment
<= MAXALIGN
;
153 sz
< 3 * alignment
&& sz
< (1U << 31);
154 sz
+= (alignment
>> (LG_SIZEOF_PTR
-1)) - 1) {
155 for (i
= 0; i
< NITER
; i
++) {
156 nsz
= nallocx(sz
, MALLOCX_ALIGN(alignment
) |
159 "nallocx() error for alignment=%zu, "
160 "size=%zu (%#zx)", alignment
, sz
, sz
);
161 ps
[i
] = mallocx(sz
, MALLOCX_ALIGN(alignment
) |
163 assert_ptr_not_null(ps
[i
],
164 "mallocx() error for alignment=%zu, "
165 "size=%zu (%#zx)", alignment
, sz
, sz
);
166 rsz
= sallocx(ps
[i
], 0);
167 assert_zu_ge(rsz
, sz
,
168 "Real size smaller than expected for "
169 "alignment=%zu, size=%zu", alignment
, sz
);
170 assert_zu_eq(nsz
, rsz
,
171 "nallocx()/sallocx() size mismatch for "
172 "alignment=%zu, size=%zu", alignment
, sz
);
174 (void *)((uintptr_t)ps
[i
] & (alignment
-1)),
175 "%p inadequately aligned for"
176 " alignment=%zu, size=%zu", ps
[i
],
179 if (total
>= (MAXALIGN
<< 1))
182 for (i
= 0; i
< NITER
; i
++) {
203 test_alignment_and_size
));