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"
|
|
|
|
|
2015-09-18 06:27:28 +08:00
|
|
|
static unsigned
|
2017-01-16 08:56:30 +08:00
|
|
|
get_nsizes_impl(const char *cmd) {
|
2015-09-18 06:27:28 +08:00
|
|
|
unsigned ret;
|
|
|
|
size_t z;
|
|
|
|
|
|
|
|
z = sizeof(unsigned);
|
2016-10-28 12:31:25 +08:00
|
|
|
assert_d_eq(mallctl(cmd, (void *)&ret, &z, NULL, 0), 0,
|
2015-09-18 06:27:28 +08:00
|
|
|
"Unexpected mallctl(\"%s\", ...) failure", cmd);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2015-09-18 06:27:28 +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");
|
2015-09-18 06:27:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
get_size_impl(const char *cmd, size_t ind) {
|
2015-09-18 06:27:28 +08:00
|
|
|
size_t ret;
|
|
|
|
size_t z;
|
|
|
|
size_t mib[4];
|
|
|
|
size_t miblen = 4;
|
|
|
|
|
|
|
|
z = sizeof(size_t);
|
|
|
|
assert_d_eq(mallctlnametomib(cmd, mib, &miblen),
|
|
|
|
0, "Unexpected mallctlnametomib(\"%s\", ...) failure", cmd);
|
|
|
|
mib[2] = ind;
|
|
|
|
z = sizeof(size_t);
|
2016-10-28 12:31:25 +08:00
|
|
|
assert_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0),
|
2015-09-18 06:27:28 +08:00
|
|
|
0, "Unexpected mallctlbymib([\"%s\", %zu], ...) failure", cmd, ind);
|
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return ret;
|
2015-09-18 06:27:28 +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);
|
2015-09-18 06:27:28 +08:00
|
|
|
}
|
|
|
|
|
2016-10-29 02:23:24 +08:00
|
|
|
/*
|
|
|
|
* On systems which can't merge extents, tests that call this function generate
|
|
|
|
* a lot of dirty memory very quickly. Purging between cycles mitigates
|
|
|
|
* potential OOM on e.g. 32-bit Windows.
|
|
|
|
*/
|
|
|
|
static void
|
2017-01-16 08:56:30 +08:00
|
|
|
purge(void) {
|
2016-10-29 02:23:24 +08:00
|
|
|
assert_d_eq(mallctl("arena.0.purge", NULL, NULL, NULL, 0), 0,
|
|
|
|
"Unexpected mallctl error");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-06-01 05:50:21 +08:00
|
|
|
largemax = get_large_size(get_nlarge()-1);
|
2016-02-26 07:29:49 +08:00
|
|
|
|
2016-06-01 05:50:21 +08:00
|
|
|
assert_ptr_null(mallocx(largemax+1, 0),
|
|
|
|
"Expected OOM for mallocx(size=%#zx, 0)", largemax+1);
|
2016-02-26 07:29:49 +08:00
|
|
|
|
2016-02-26 08:40:24 +08:00
|
|
|
assert_ptr_null(mallocx(ZU(PTRDIFF_MAX)+1, 0),
|
|
|
|
"Expected OOM for mallocx(size=%#zx, 0)", ZU(PTRDIFF_MAX)+1);
|
2016-02-26 07:29:49 +08:00
|
|
|
|
|
|
|
assert_ptr_null(mallocx(SIZE_T_MAX, 0),
|
|
|
|
"Expected OOM for mallocx(size=%#zx, 0)", SIZE_T_MAX);
|
|
|
|
|
2016-02-26 08:40:24 +08:00
|
|
|
assert_ptr_null(mallocx(1, MALLOCX_ALIGN(ZU(PTRDIFF_MAX)+1)),
|
2016-02-26 07:29:49 +08:00
|
|
|
"Expected OOM for mallocx(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
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_oom) {
|
2016-06-01 05:50:21 +08:00
|
|
|
size_t largemax;
|
2016-05-04 00:37:54 +08:00
|
|
|
bool oom;
|
|
|
|
void *ptrs[3];
|
|
|
|
unsigned i;
|
2015-09-18 06:27:28 +08:00
|
|
|
|
2015-09-25 07:44:16 +08:00
|
|
|
/*
|
2016-05-04 00:37:54 +08:00
|
|
|
* It should be impossible to allocate three objects that each consume
|
|
|
|
* nearly half the virtual address space.
|
2015-09-25 07:44:16 +08:00
|
|
|
*/
|
2016-06-01 05:50:21 +08:00
|
|
|
largemax = get_large_size(get_nlarge()-1);
|
2016-05-04 00:37:54 +08:00
|
|
|
oom = false;
|
|
|
|
for (i = 0; i < sizeof(ptrs) / sizeof(void *); i++) {
|
2016-06-01 05:50:21 +08:00
|
|
|
ptrs[i] = mallocx(largemax, 0);
|
2017-01-16 08:56:30 +08:00
|
|
|
if (ptrs[i] == NULL) {
|
2016-05-04 00:37:54 +08:00
|
|
|
oom = true;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2016-05-04 00:37:54 +08:00
|
|
|
}
|
|
|
|
assert_true(oom,
|
|
|
|
"Expected OOM during series of calls to mallocx(size=%zu, 0)",
|
2016-06-01 05:50:21 +08:00
|
|
|
largemax);
|
2016-05-04 00:37:54 +08:00
|
|
|
for (i = 0; i < sizeof(ptrs) / sizeof(void *); i++) {
|
2017-01-16 08:56:30 +08:00
|
|
|
if (ptrs[i] != NULL) {
|
2016-05-04 00:37:54 +08:00
|
|
|
dallocx(ptrs[i], 0);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2015-09-25 07:44:16 +08:00
|
|
|
}
|
2016-10-29 02:23:24 +08:00
|
|
|
purge();
|
2015-09-18 06:27:28 +08:00
|
|
|
|
|
|
|
#if LG_SIZEOF_PTR == 3
|
2016-03-12 02:11:56 +08:00
|
|
|
assert_ptr_null(mallocx(0x8000000000000000ULL,
|
|
|
|
MALLOCX_ALIGN(0x8000000000000000ULL)),
|
|
|
|
"Expected OOM for mallocx()");
|
|
|
|
assert_ptr_null(mallocx(0x8000000000000000ULL,
|
|
|
|
MALLOCX_ALIGN(0x80000000)),
|
|
|
|
"Expected OOM for mallocx()");
|
2015-09-18 06:27:28 +08:00
|
|
|
#else
|
2016-03-12 02:11:56 +08:00
|
|
|
assert_ptr_null(mallocx(0x80000000UL, MALLOCX_ALIGN(0x80000000UL)),
|
|
|
|
"Expected OOM for mallocx()");
|
2015-09-18 06:27:28 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_basic) {
|
2017-01-20 13:41:41 +08:00
|
|
|
#define MAXSZ (((size_t)1) << 23)
|
2015-02-16 12:13:28 +08:00
|
|
|
size_t sz;
|
|
|
|
|
|
|
|
for (sz = 1; sz < MAXSZ; sz = nallocx(sz, 0) + 1) {
|
|
|
|
size_t nsz, rsz;
|
|
|
|
void *p;
|
|
|
|
nsz = nallocx(sz, 0);
|
|
|
|
assert_zu_ne(nsz, 0, "Unexpected nallocx() error");
|
|
|
|
p = mallocx(sz, 0);
|
2016-10-29 02:23:24 +08:00
|
|
|
assert_ptr_not_null(p,
|
|
|
|
"Unexpected mallocx(size=%zx, flags=0) error", sz);
|
2015-02-16 12:13:28 +08:00
|
|
|
rsz = sallocx(p, 0);
|
|
|
|
assert_zu_ge(rsz, sz, "Real size smaller than expected");
|
|
|
|
assert_zu_eq(nsz, rsz, "nallocx()/sallocx() size mismatch");
|
|
|
|
dallocx(p, 0);
|
|
|
|
|
|
|
|
p = mallocx(sz, 0);
|
2016-10-29 02:23:24 +08:00
|
|
|
assert_ptr_not_null(p,
|
|
|
|
"Unexpected mallocx(size=%zx, flags=0) error", sz);
|
2015-02-16 12:13:28 +08:00
|
|
|
dallocx(p, 0);
|
|
|
|
|
|
|
|
nsz = nallocx(sz, MALLOCX_ZERO);
|
|
|
|
assert_zu_ne(nsz, 0, "Unexpected nallocx() error");
|
|
|
|
p = mallocx(sz, MALLOCX_ZERO);
|
2016-10-29 02:23:24 +08:00
|
|
|
assert_ptr_not_null(p,
|
|
|
|
"Unexpected mallocx(size=%zx, flags=MALLOCX_ZERO) error",
|
|
|
|
nsz);
|
2015-02-16 12:13:28 +08:00
|
|
|
rsz = sallocx(p, 0);
|
|
|
|
assert_zu_eq(nsz, rsz, "nallocx()/sallocx() rsize mismatch");
|
|
|
|
dallocx(p, 0);
|
2016-10-29 02:23:24 +08:00
|
|
|
purge();
|
2015-02-16 12:13:28 +08:00
|
|
|
}
|
2015-06-24 09:47:07 +08:00
|
|
|
#undef MAXSZ
|
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
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_alignment_and_size) {
|
2017-01-20 13:41:41 +08:00
|
|
|
#define MAXALIGN (((size_t)1) << 23)
|
|
|
|
#define NITER 4
|
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 nsz, rsz, sz, alignment, total;
|
|
|
|
unsigned i;
|
|
|
|
void *ps[NITER];
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
for (i = 0; i < NITER; i++) {
|
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
|
|
|
ps[i] = NULL;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
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
|
|
|
|
|
|
|
for (alignment = 8;
|
|
|
|
alignment <= MAXALIGN;
|
|
|
|
alignment <<= 1) {
|
|
|
|
total = 0;
|
|
|
|
for (sz = 1;
|
|
|
|
sz < 3 * alignment && sz < (1U << 31);
|
|
|
|
sz += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
|
|
|
|
for (i = 0; i < NITER; i++) {
|
|
|
|
nsz = nallocx(sz, MALLOCX_ALIGN(alignment) |
|
|
|
|
MALLOCX_ZERO);
|
|
|
|
assert_zu_ne(nsz, 0,
|
|
|
|
"nallocx() error for alignment=%zu, "
|
|
|
|
"size=%zu (%#zx)", alignment, sz, sz);
|
|
|
|
ps[i] = mallocx(sz, MALLOCX_ALIGN(alignment) |
|
|
|
|
MALLOCX_ZERO);
|
|
|
|
assert_ptr_not_null(ps[i],
|
|
|
|
"mallocx() error for alignment=%zu, "
|
|
|
|
"size=%zu (%#zx)", alignment, sz, sz);
|
|
|
|
rsz = sallocx(ps[i], 0);
|
|
|
|
assert_zu_ge(rsz, sz,
|
|
|
|
"Real size smaller than expected for "
|
|
|
|
"alignment=%zu, size=%zu", alignment, sz);
|
|
|
|
assert_zu_eq(nsz, rsz,
|
|
|
|
"nallocx()/sallocx() size mismatch for "
|
|
|
|
"alignment=%zu, size=%zu", alignment, sz);
|
|
|
|
assert_ptr_null(
|
|
|
|
(void *)((uintptr_t)ps[i] & (alignment-1)),
|
|
|
|
"%p inadequately aligned for"
|
|
|
|
" alignment=%zu, size=%zu", ps[i],
|
|
|
|
alignment, sz);
|
|
|
|
total += rsz;
|
2017-01-16 08:56:30 +08:00
|
|
|
if (total >= (MAXALIGN << 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
|
|
|
break;
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
for (i = 0; i < NITER; i++) {
|
|
|
|
if (ps[i] != NULL) {
|
|
|
|
dallocx(ps[i], 0);
|
|
|
|
ps[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-29 02:23:24 +08:00
|
|
|
purge();
|
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
|
|
|
}
|
2015-06-24 09:47:07 +08:00
|
|
|
#undef MAXALIGN
|
|
|
|
#undef NITER
|
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
|
|
|
|
|
|
|
|
int
|
2017-01-16 08:56:30 +08:00
|
|
|
main(void) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return test(
|
2016-02-26 07:29:49 +08:00
|
|
|
test_overflow,
|
2015-09-18 06:27:28 +08:00
|
|
|
test_oom,
|
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_basic,
|
2017-01-20 10:15:45 +08:00
|
|
|
test_alignment_and_size);
|
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
|
|
|
}
|