Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
2016-02-26 07:29:49 +08:00
|
|
|
static unsigned
|
2017-01-16 08:56:30 +08:00
|
|
|
get_nsizes_impl(const char *cmd) {
|
2016-02-26 07:29:49 +08:00
|
|
|
unsigned ret;
|
|
|
|
size_t z;
|
|
|
|
|
|
|
|
z = sizeof(unsigned);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_d_eq(mallctl(cmd, (void *)&ret, &z, NULL, 0), 0,
|
2016-02-26 07:29:49 +08:00
|
|
|
"Unexpected mallctl(\"%s\", ...) failure", cmd);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2016-02-26 07:29:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned
|
2017-01-16 08:56:30 +08:00
|
|
|
get_nlarge(void) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return get_nsizes_impl("arenas.nlextents");
|
2016-02-26 07:29:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
get_size_impl(const char *cmd, size_t ind) {
|
2016-02-26 07:29:49 +08:00
|
|
|
size_t ret;
|
|
|
|
size_t z;
|
|
|
|
size_t mib[4];
|
|
|
|
size_t miblen = 4;
|
|
|
|
|
|
|
|
z = sizeof(size_t);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_d_eq(mallctlnametomib(cmd, mib, &miblen),
|
2016-02-26 07:29:49 +08:00
|
|
|
0, "Unexpected mallctlnametomib(\"%s\", ...) failure", cmd);
|
|
|
|
mib[2] = ind;
|
|
|
|
z = sizeof(size_t);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0),
|
2016-02-26 07:29:49 +08:00
|
|
|
0, "Unexpected mallctlbymib([\"%s\", %zu], ...) failure", cmd, ind);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2016-02-26 07:29:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
get_large_size(size_t ind) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return get_size_impl("arenas.lextent.0.size", ind);
|
2016-02-26 07:29:49 +08:00
|
|
|
}
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_grow_and_shrink) {
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
void *p, *q;
|
|
|
|
size_t tsz;
|
2017-01-20 13:41:41 +08:00
|
|
|
#define NCYCLES 3
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
unsigned i, j;
|
2017-01-20 13:41:41 +08:00
|
|
|
#define NSZS 1024
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
size_t szs[NSZS];
|
2017-01-20 13:41:41 +08:00
|
|
|
#define MAXSZ ZU(12 * 1024 * 1024)
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
|
|
|
|
p = mallocx(1, 0);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_not_null(p, "Unexpected mallocx() error");
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
szs[0] = sallocx(p, 0);
|
|
|
|
|
|
|
|
for (i = 0; i < NCYCLES; i++) {
|
|
|
|
for (j = 1; j < NSZS && szs[j-1] < MAXSZ; j++) {
|
|
|
|
q = rallocx(p, szs[j-1]+1, 0);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_not_null(q,
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
"Unexpected rallocx() error for size=%zu-->%zu",
|
|
|
|
szs[j-1], szs[j-1]+1);
|
|
|
|
szs[j] = sallocx(q, 0);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_ne(szs[j], szs[j-1]+1,
|
2015-09-12 07:18:53 +08:00
|
|
|
"Expected size to be at least: %zu", szs[j-1]+1);
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
p = q;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j--; j > 0; j--) {
|
|
|
|
q = rallocx(p, szs[j-1], 0);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_not_null(q,
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
"Unexpected rallocx() error for size=%zu-->%zu",
|
|
|
|
szs[j], szs[j-1]);
|
|
|
|
tsz = sallocx(q, 0);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(tsz, szs[j-1],
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
"Expected size=%zu, got size=%zu", szs[j-1], tsz);
|
|
|
|
p = q;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dallocx(p, 0);
|
2013-12-16 07:54:18 +08:00
|
|
|
#undef MAXSZ
|
|
|
|
#undef NSZS
|
|
|
|
#undef NCYCLES
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
static bool
|
2017-01-16 08:56:30 +08:00
|
|
|
validate_fill(const void *p, uint8_t c, size_t offset, size_t len) {
|
2013-12-16 07:54:18 +08:00
|
|
|
bool ret = false;
|
|
|
|
const uint8_t *buf = (const uint8_t *)p;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
uint8_t b = buf[offset+i];
|
|
|
|
if (b != c) {
|
2015-07-25 09:18:03 +08:00
|
|
|
test_fail("Allocation at %p (len=%zu) contains %#x "
|
|
|
|
"rather than %#x at offset %zu", p, len, b, c,
|
|
|
|
offset+i);
|
2013-12-16 07:54:18 +08:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2013-12-16 07:54:18 +08:00
|
|
|
}
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_zero) {
|
2013-12-16 07:54:18 +08:00
|
|
|
void *p, *q;
|
|
|
|
size_t psz, qsz, i, j;
|
|
|
|
size_t start_sizes[] = {1, 3*1024, 63*1024, 4095*1024};
|
2017-01-20 13:41:41 +08:00
|
|
|
#define FILL_BYTE 0xaaU
|
|
|
|
#define RANGE 2048
|
2013-12-16 07:54:18 +08:00
|
|
|
|
|
|
|
for (i = 0; i < sizeof(start_sizes)/sizeof(size_t); i++) {
|
|
|
|
size_t start_size = start_sizes[i];
|
|
|
|
p = mallocx(start_size, MALLOCX_ZERO);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_not_null(p, "Unexpected mallocx() error");
|
2013-12-16 07:54:18 +08:00
|
|
|
psz = sallocx(p, 0);
|
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_false(validate_fill(p, 0, 0, psz),
|
2013-12-16 07:54:18 +08:00
|
|
|
"Expected zeroed memory");
|
|
|
|
memset(p, FILL_BYTE, psz);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_false(validate_fill(p, FILL_BYTE, 0, psz),
|
2013-12-16 07:54:18 +08:00
|
|
|
"Expected filled memory");
|
|
|
|
|
|
|
|
for (j = 1; j < RANGE; j++) {
|
|
|
|
q = rallocx(p, start_size+j, MALLOCX_ZERO);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_not_null(q, "Unexpected rallocx() error");
|
2013-12-16 07:54:18 +08:00
|
|
|
qsz = sallocx(q, 0);
|
|
|
|
if (q != p || qsz != psz) {
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_false(validate_fill(q, FILL_BYTE, 0,
|
2013-12-16 07:54:18 +08:00
|
|
|
psz), "Expected filled memory");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_false(validate_fill(q, 0, psz, qsz-psz),
|
2013-12-16 07:54:18 +08:00
|
|
|
"Expected zeroed memory");
|
|
|
|
}
|
|
|
|
if (psz != qsz) {
|
2014-05-28 13:17:01 +08:00
|
|
|
memset((void *)((uintptr_t)q+psz), FILL_BYTE,
|
2014-05-21 17:13:21 +08:00
|
|
|
qsz-psz);
|
2013-12-16 07:54:18 +08:00
|
|
|
psz = qsz;
|
|
|
|
}
|
|
|
|
p = q;
|
|
|
|
}
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_false(validate_fill(p, FILL_BYTE, 0, psz),
|
2013-12-16 07:54:18 +08:00
|
|
|
"Expected filled memory");
|
|
|
|
dallocx(p, 0);
|
|
|
|
}
|
|
|
|
#undef FILL_BYTE
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_align) {
|
2013-12-16 07:54:18 +08:00
|
|
|
void *p, *q;
|
|
|
|
size_t align;
|
2017-01-20 13:41:41 +08:00
|
|
|
#define MAX_ALIGN (ZU(1) << 25)
|
2013-12-16 07:54:18 +08:00
|
|
|
|
|
|
|
align = ZU(1);
|
|
|
|
p = mallocx(1, MALLOCX_ALIGN(align));
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_not_null(p, "Unexpected mallocx() error");
|
2013-12-16 07:54:18 +08:00
|
|
|
|
|
|
|
for (align <<= 1; align <= MAX_ALIGN; align <<= 1) {
|
|
|
|
q = rallocx(p, 1, MALLOCX_ALIGN(align));
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_not_null(q,
|
2013-12-16 07:54:18 +08:00
|
|
|
"Unexpected rallocx() error for align=%zu", align);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_null(
|
2013-12-16 07:54:18 +08:00
|
|
|
(void *)((uintptr_t)q & (align-1)),
|
|
|
|
"%p inadequately aligned for align=%zu",
|
|
|
|
q, align);
|
|
|
|
p = q;
|
|
|
|
}
|
|
|
|
dallocx(p, 0);
|
|
|
|
#undef MAX_ALIGN
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2020-08-11 06:39:16 +08:00
|
|
|
TEST_BEGIN(test_align_enum) {
|
|
|
|
/* Span both small sizes and large sizes. */
|
|
|
|
#define LG_MIN 12
|
|
|
|
#define LG_MAX 15
|
|
|
|
for (size_t lg_align = LG_MIN; lg_align <= LG_MAX; ++lg_align) {
|
|
|
|
for (size_t lg_size = LG_MIN; lg_size <= LG_MAX; ++lg_size) {
|
|
|
|
size_t size = 1 << lg_size;
|
|
|
|
for (size_t lg_align_next = LG_MIN;
|
|
|
|
lg_align_next <= LG_MAX; ++lg_align_next) {
|
|
|
|
int flags = MALLOCX_LG_ALIGN(lg_align);
|
|
|
|
void *p = mallocx(1, flags);
|
|
|
|
assert_ptr_not_null(p,
|
|
|
|
"Unexpected mallocx() error");
|
|
|
|
assert_zu_eq(nallocx(1, flags),
|
2021-02-18 04:40:11 +08:00
|
|
|
TEST_MALLOC_SIZE(p),
|
2020-08-11 06:39:16 +08:00
|
|
|
"Wrong mallocx() usable size");
|
|
|
|
int flags_next =
|
|
|
|
MALLOCX_LG_ALIGN(lg_align_next);
|
|
|
|
p = rallocx(p, size, flags_next);
|
|
|
|
assert_ptr_not_null(p,
|
|
|
|
"Unexpected rallocx() error");
|
|
|
|
expect_zu_eq(nallocx(size, flags_next),
|
2021-02-18 04:40:11 +08:00
|
|
|
TEST_MALLOC_SIZE(p),
|
2020-08-11 06:39:16 +08:00
|
|
|
"Wrong rallocx() usable size");
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#undef LG_MAX
|
|
|
|
#undef LG_MIN
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_lg_align_and_zero) {
|
2013-12-16 07:54:18 +08:00
|
|
|
void *p, *q;
|
2016-02-25 04:42:23 +08:00
|
|
|
unsigned lg_align;
|
|
|
|
size_t sz;
|
2017-01-20 13:41:41 +08:00
|
|
|
#define MAX_LG_ALIGN 25
|
|
|
|
#define MAX_VALIDATE (ZU(1) << 22)
|
2013-12-16 07:54:18 +08:00
|
|
|
|
2016-02-25 04:42:23 +08:00
|
|
|
lg_align = 0;
|
2013-12-17 05:37:21 +08:00
|
|
|
p = mallocx(1, MALLOCX_LG_ALIGN(lg_align)|MALLOCX_ZERO);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_not_null(p, "Unexpected mallocx() error");
|
2013-12-16 07:54:18 +08:00
|
|
|
|
|
|
|
for (lg_align++; lg_align <= MAX_LG_ALIGN; lg_align++) {
|
2013-12-17 05:37:21 +08:00
|
|
|
q = rallocx(p, 1, MALLOCX_LG_ALIGN(lg_align)|MALLOCX_ZERO);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_not_null(q,
|
2016-02-25 04:42:23 +08:00
|
|
|
"Unexpected rallocx() error for lg_align=%u", lg_align);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_null(
|
2013-12-16 07:54:18 +08:00
|
|
|
(void *)((uintptr_t)q & ((ZU(1) << lg_align)-1)),
|
2016-02-25 04:42:23 +08:00
|
|
|
"%p inadequately aligned for lg_align=%u", q, lg_align);
|
2013-12-17 05:37:21 +08:00
|
|
|
sz = sallocx(q, 0);
|
|
|
|
if ((sz << 1) <= MAX_VALIDATE) {
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_false(validate_fill(q, 0, 0, sz),
|
2013-12-17 05:37:21 +08:00
|
|
|
"Expected zeroed memory");
|
|
|
|
} else {
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_false(validate_fill(q, 0, 0, MAX_VALIDATE),
|
2013-12-17 05:37:21 +08:00
|
|
|
"Expected zeroed memory");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_false(validate_fill(
|
2014-05-28 13:17:01 +08:00
|
|
|
(void *)((uintptr_t)q+sz-MAX_VALIDATE),
|
2014-05-21 17:13:21 +08:00
|
|
|
0, 0, MAX_VALIDATE), "Expected zeroed memory");
|
2013-12-17 05:37:21 +08:00
|
|
|
}
|
2013-12-16 07:54:18 +08:00
|
|
|
p = q;
|
|
|
|
}
|
|
|
|
dallocx(p, 0);
|
2013-12-17 05:37:21 +08:00
|
|
|
#undef MAX_VALIDATE
|
2013-12-16 07:54:18 +08:00
|
|
|
#undef MAX_LG_ALIGN
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2018-05-03 17:40:53 +08:00
|
|
|
/*
|
|
|
|
* GCC "-Walloc-size-larger-than" warning detects when one of the memory
|
|
|
|
* allocation functions is called with a size larger than the maximum size that
|
|
|
|
* they support. Here we want to explicitly test that the allocation functions
|
|
|
|
* do indeed fail properly when this is the case, which triggers the warning.
|
|
|
|
* Therefore we disable the warning for these tests.
|
|
|
|
*/
|
|
|
|
JEMALLOC_DIAGNOSTIC_PUSH
|
|
|
|
JEMALLOC_DIAGNOSTIC_IGNORE_ALLOC_SIZE_LARGER_THAN
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_overflow) {
|
2016-06-01 05:50:21 +08:00
|
|
|
size_t largemax;
|
2016-02-26 07:29:49 +08:00
|
|
|
void *p;
|
|
|
|
|
2016-06-01 05:50:21 +08:00
|
|
|
largemax = get_large_size(get_nlarge()-1);
|
2016-02-26 07:29:49 +08:00
|
|
|
|
|
|
|
p = mallocx(1, 0);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_not_null(p, "Unexpected mallocx() failure");
|
2016-02-26 07:29:49 +08:00
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_null(rallocx(p, largemax+1, 0),
|
2016-06-01 05:50:21 +08:00
|
|
|
"Expected OOM for rallocx(p, size=%#zx, 0)", largemax+1);
|
2016-02-26 07:29:49 +08:00
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_null(rallocx(p, ZU(PTRDIFF_MAX)+1, 0),
|
2016-02-26 08:40:24 +08:00
|
|
|
"Expected OOM for rallocx(p, size=%#zx, 0)", ZU(PTRDIFF_MAX)+1);
|
2016-02-26 07:29:49 +08:00
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_null(rallocx(p, SIZE_T_MAX, 0),
|
2016-02-26 07:29:49 +08:00
|
|
|
"Expected OOM for rallocx(p, size=%#zx, 0)", SIZE_T_MAX);
|
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_null(rallocx(p, 1, MALLOCX_ALIGN(ZU(PTRDIFF_MAX)+1)),
|
2016-02-26 07:29:49 +08:00
|
|
|
"Expected OOM for rallocx(p, size=1, MALLOCX_ALIGN(%#zx))",
|
2016-02-26 08:40:24 +08:00
|
|
|
ZU(PTRDIFF_MAX)+1);
|
2016-02-26 07:29:49 +08:00
|
|
|
|
|
|
|
dallocx(p, 0);
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2018-05-03 17:40:53 +08:00
|
|
|
/* Re-enable the "-Walloc-size-larger-than=" warning */
|
|
|
|
JEMALLOC_DIAGNOSTIC_POP
|
|
|
|
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
int
|
2017-01-16 08:56:30 +08:00
|
|
|
main(void) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return test(
|
2013-12-16 07:54:18 +08:00
|
|
|
test_grow_and_shrink,
|
|
|
|
test_zero,
|
|
|
|
test_align,
|
2020-08-11 06:39:16 +08:00
|
|
|
test_align_enum,
|
2016-02-26 07:29:49 +08:00
|
|
|
test_lg_align_and_zero,
|
2017-01-20 10:15:45 +08:00
|
|
|
test_overflow);
|
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
2013-12-13 14:35:52 +08:00
|
|
|
}
|