]>
git.proxmox.com Git - rustc.git/blob - src/jemalloc/test/integration/rallocx.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_grow_and_shrink
)
57 #define MAXSZ ZU(12 * 1024 * 1024)
60 assert_ptr_not_null(p
, "Unexpected mallocx() error");
61 szs
[0] = sallocx(p
, 0);
63 for (i
= 0; i
< NCYCLES
; i
++) {
64 for (j
= 1; j
< NSZS
&& szs
[j
-1] < MAXSZ
; j
++) {
65 q
= rallocx(p
, szs
[j
-1]+1, 0);
66 assert_ptr_not_null(q
,
67 "Unexpected rallocx() error for size=%zu-->%zu",
68 szs
[j
-1], szs
[j
-1]+1);
69 szs
[j
] = sallocx(q
, 0);
70 assert_zu_ne(szs
[j
], szs
[j
-1]+1,
71 "Expected size to be at least: %zu", szs
[j
-1]+1);
75 for (j
--; j
> 0; j
--) {
76 q
= rallocx(p
, szs
[j
-1], 0);
77 assert_ptr_not_null(q
,
78 "Unexpected rallocx() error for size=%zu-->%zu",
81 assert_zu_eq(tsz
, szs
[j
-1],
82 "Expected size=%zu, got size=%zu", szs
[j
-1], tsz
);
95 validate_fill(const void *p
, uint8_t c
, size_t offset
, size_t len
)
98 const uint8_t *buf
= (const uint8_t *)p
;
101 for (i
= 0; i
< len
; i
++) {
102 uint8_t b
= buf
[offset
+i
];
104 test_fail("Allocation at %p (len=%zu) contains %#x "
105 "rather than %#x at offset %zu", p
, len
, b
, c
,
114 TEST_BEGIN(test_zero
)
117 size_t psz
, qsz
, i
, j
;
118 size_t start_sizes
[] = {1, 3*1024, 63*1024, 4095*1024};
119 #define FILL_BYTE 0xaaU
122 for (i
= 0; i
< sizeof(start_sizes
)/sizeof(size_t); i
++) {
123 size_t start_size
= start_sizes
[i
];
124 p
= mallocx(start_size
, MALLOCX_ZERO
);
125 assert_ptr_not_null(p
, "Unexpected mallocx() error");
128 assert_false(validate_fill(p
, 0, 0, psz
),
129 "Expected zeroed memory");
130 memset(p
, FILL_BYTE
, psz
);
131 assert_false(validate_fill(p
, FILL_BYTE
, 0, psz
),
132 "Expected filled memory");
134 for (j
= 1; j
< RANGE
; j
++) {
135 q
= rallocx(p
, start_size
+j
, MALLOCX_ZERO
);
136 assert_ptr_not_null(q
, "Unexpected rallocx() error");
138 if (q
!= p
|| qsz
!= psz
) {
139 assert_false(validate_fill(q
, FILL_BYTE
, 0,
140 psz
), "Expected filled memory");
141 assert_false(validate_fill(q
, 0, psz
, qsz
-psz
),
142 "Expected zeroed memory");
145 memset((void *)((uintptr_t)q
+psz
), FILL_BYTE
,
151 assert_false(validate_fill(p
, FILL_BYTE
, 0, psz
),
152 "Expected filled memory");
159 TEST_BEGIN(test_align
)
163 #define MAX_ALIGN (ZU(1) << 25)
166 p
= mallocx(1, MALLOCX_ALIGN(align
));
167 assert_ptr_not_null(p
, "Unexpected mallocx() error");
169 for (align
<<= 1; align
<= MAX_ALIGN
; align
<<= 1) {
170 q
= rallocx(p
, 1, MALLOCX_ALIGN(align
));
171 assert_ptr_not_null(q
,
172 "Unexpected rallocx() error for align=%zu", align
);
174 (void *)((uintptr_t)q
& (align
-1)),
175 "%p inadequately aligned for align=%zu",
184 TEST_BEGIN(test_lg_align_and_zero
)
189 #define MAX_LG_ALIGN 25
190 #define MAX_VALIDATE (ZU(1) << 22)
193 p
= mallocx(1, MALLOCX_LG_ALIGN(lg_align
)|MALLOCX_ZERO
);
194 assert_ptr_not_null(p
, "Unexpected mallocx() error");
196 for (lg_align
++; lg_align
<= MAX_LG_ALIGN
; lg_align
++) {
197 q
= rallocx(p
, 1, MALLOCX_LG_ALIGN(lg_align
)|MALLOCX_ZERO
);
198 assert_ptr_not_null(q
,
199 "Unexpected rallocx() error for lg_align=%u", lg_align
);
201 (void *)((uintptr_t)q
& ((ZU(1) << lg_align
)-1)),
202 "%p inadequately aligned for lg_align=%u", q
, lg_align
);
204 if ((sz
<< 1) <= MAX_VALIDATE
) {
205 assert_false(validate_fill(q
, 0, 0, sz
),
206 "Expected zeroed memory");
208 assert_false(validate_fill(q
, 0, 0, MAX_VALIDATE
),
209 "Expected zeroed memory");
210 assert_false(validate_fill(
211 (void *)((uintptr_t)q
+sz
-MAX_VALIDATE
),
212 0, 0, MAX_VALIDATE
), "Expected zeroed memory");
222 TEST_BEGIN(test_overflow
)
227 hugemax
= get_huge_size(get_nhuge()-1);
230 assert_ptr_not_null(p
, "Unexpected mallocx() failure");
232 assert_ptr_null(rallocx(p
, hugemax
+1, 0),
233 "Expected OOM for rallocx(p, size=%#zx, 0)", hugemax
+1);
235 assert_ptr_null(rallocx(p
, ZU(PTRDIFF_MAX
)+1, 0),
236 "Expected OOM for rallocx(p, size=%#zx, 0)", ZU(PTRDIFF_MAX
)+1);
238 assert_ptr_null(rallocx(p
, SIZE_T_MAX
, 0),
239 "Expected OOM for rallocx(p, size=%#zx, 0)", SIZE_T_MAX
);
241 assert_ptr_null(rallocx(p
, 1, MALLOCX_ALIGN(ZU(PTRDIFF_MAX
)+1)),
242 "Expected OOM for rallocx(p, size=1, MALLOCX_ALIGN(%#zx))",
254 test_grow_and_shrink
,
257 test_lg_align_and_zero
,