2015-07-10 12:36:33 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
|
|
|
static size_t
|
2017-01-16 08:56:30 +08:00
|
|
|
get_max_size_class(void) {
|
2016-06-01 05:50:21 +08:00
|
|
|
unsigned nlextents;
|
2015-07-10 12:36:33 +08:00
|
|
|
size_t mib[4];
|
|
|
|
size_t sz, miblen, max_size_class;
|
|
|
|
|
|
|
|
sz = sizeof(unsigned);
|
2016-10-28 12:31:25 +08:00
|
|
|
assert_d_eq(mallctl("arenas.nlextents", (void *)&nlextents, &sz, NULL,
|
|
|
|
0), 0, "Unexpected mallctl() error");
|
2015-07-10 12:36:33 +08:00
|
|
|
|
|
|
|
miblen = sizeof(mib) / sizeof(size_t);
|
2016-06-01 05:50:21 +08:00
|
|
|
assert_d_eq(mallctlnametomib("arenas.lextent.0.size", mib, &miblen), 0,
|
2015-07-10 12:36:33 +08:00
|
|
|
"Unexpected mallctlnametomib() error");
|
2016-06-01 05:50:21 +08:00
|
|
|
mib[2] = nlextents - 1;
|
2015-07-10 12:36:33 +08:00
|
|
|
|
|
|
|
sz = sizeof(size_t);
|
2016-10-28 12:31:25 +08:00
|
|
|
assert_d_eq(mallctlbymib(mib, miblen, (void *)&max_size_class, &sz,
|
|
|
|
NULL, 0), 0, "Unexpected mallctlbymib() error");
|
2015-07-10 12:36:33 +08:00
|
|
|
|
2017-01-20 10:15:45 +08:00
|
|
|
return max_size_class;
|
2015-07-10 12:36:33 +08:00
|
|
|
}
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_size_classes) {
|
2015-07-10 12:36:33 +08:00
|
|
|
size_t size_class, max_size_class;
|
2015-08-20 06:21:32 +08:00
|
|
|
szind_t index, max_index;
|
2015-07-10 12:36:33 +08:00
|
|
|
|
|
|
|
max_size_class = get_max_size_class();
|
2017-05-31 01:45:37 +08:00
|
|
|
max_index = sz_size2index(max_size_class);
|
2015-07-10 12:36:33 +08:00
|
|
|
|
2017-05-31 01:45:37 +08:00
|
|
|
for (index = 0, size_class = sz_index2size(index); index < max_index ||
|
2015-07-10 12:36:33 +08:00
|
|
|
size_class < max_size_class; index++, size_class =
|
2017-05-31 01:45:37 +08:00
|
|
|
sz_index2size(index)) {
|
2015-07-10 12:36:33 +08:00
|
|
|
assert_true(index < max_index,
|
|
|
|
"Loop conditionals should be equivalent; index=%u, "
|
|
|
|
"size_class=%zu (%#zx)", index, size_class, size_class);
|
|
|
|
assert_true(size_class < max_size_class,
|
|
|
|
"Loop conditionals should be equivalent; index=%u, "
|
|
|
|
"size_class=%zu (%#zx)", index, size_class, size_class);
|
|
|
|
|
2017-05-31 01:45:37 +08:00
|
|
|
assert_u_eq(index, sz_size2index(size_class),
|
|
|
|
"sz_size2index() does not reverse sz_index2size(): index=%u"
|
|
|
|
" --> size_class=%zu --> index=%u --> size_class=%zu",
|
|
|
|
index, size_class, sz_size2index(size_class),
|
|
|
|
sz_index2size(sz_size2index(size_class)));
|
|
|
|
assert_zu_eq(size_class,
|
|
|
|
sz_index2size(sz_size2index(size_class)),
|
|
|
|
"sz_index2size() does not reverse sz_size2index(): index=%u"
|
|
|
|
" --> size_class=%zu --> index=%u --> size_class=%zu",
|
|
|
|
index, size_class, sz_size2index(size_class),
|
|
|
|
sz_index2size(sz_size2index(size_class)));
|
|
|
|
|
|
|
|
assert_u_eq(index+1, sz_size2index(size_class+1),
|
2015-07-10 12:36:33 +08:00
|
|
|
"Next size_class does not round up properly");
|
|
|
|
|
|
|
|
assert_zu_eq(size_class, (index > 0) ?
|
2017-05-31 01:45:37 +08:00
|
|
|
sz_s2u(sz_index2size(index-1)+1) : sz_s2u(1),
|
|
|
|
"sz_s2u() does not round up to size class");
|
|
|
|
assert_zu_eq(size_class, sz_s2u(size_class-1),
|
|
|
|
"sz_s2u() does not round up to size class");
|
|
|
|
assert_zu_eq(size_class, sz_s2u(size_class),
|
|
|
|
"sz_s2u() does not compute same size class");
|
|
|
|
assert_zu_eq(sz_s2u(size_class+1), sz_index2size(index+1),
|
|
|
|
"sz_s2u() does not round up to next size class");
|
2015-07-10 12:36:33 +08:00
|
|
|
}
|
|
|
|
|
2017-05-31 01:45:37 +08:00
|
|
|
assert_u_eq(index, sz_size2index(sz_index2size(index)),
|
|
|
|
"sz_size2index() does not reverse sz_index2size()");
|
|
|
|
assert_zu_eq(max_size_class, sz_index2size(
|
|
|
|
sz_size2index(max_size_class)),
|
|
|
|
"sz_index2size() does not reverse sz_size2index()");
|
|
|
|
|
|
|
|
assert_zu_eq(size_class, sz_s2u(sz_index2size(index-1)+1),
|
|
|
|
"sz_s2u() does not round up to size class");
|
|
|
|
assert_zu_eq(size_class, sz_s2u(size_class-1),
|
|
|
|
"sz_s2u() does not round up to size class");
|
|
|
|
assert_zu_eq(size_class, sz_s2u(size_class),
|
|
|
|
"sz_s2u() does not compute same size class");
|
2015-07-10 12:36:33 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_psize_classes) {
|
2016-11-04 12:18:50 +08:00
|
|
|
size_t size_class, max_psz;
|
2016-04-18 07:16:11 +08:00
|
|
|
pszind_t pind, max_pind;
|
|
|
|
|
2016-11-04 12:18:50 +08:00
|
|
|
max_psz = get_max_size_class() + PAGE;
|
2017-05-31 01:45:37 +08:00
|
|
|
max_pind = sz_psz2ind(max_psz);
|
2016-04-18 07:16:11 +08:00
|
|
|
|
2017-05-31 01:45:37 +08:00
|
|
|
for (pind = 0, size_class = sz_pind2sz(pind);
|
|
|
|
pind < max_pind || size_class < max_psz;
|
|
|
|
pind++, size_class = sz_pind2sz(pind)) {
|
2016-04-18 07:16:11 +08:00
|
|
|
assert_true(pind < max_pind,
|
|
|
|
"Loop conditionals should be equivalent; pind=%u, "
|
|
|
|
"size_class=%zu (%#zx)", pind, size_class, size_class);
|
2016-11-04 12:18:50 +08:00
|
|
|
assert_true(size_class < max_psz,
|
2016-04-18 07:16:11 +08:00
|
|
|
"Loop conditionals should be equivalent; pind=%u, "
|
|
|
|
"size_class=%zu (%#zx)", pind, size_class, size_class);
|
|
|
|
|
2017-05-31 01:45:37 +08:00
|
|
|
assert_u_eq(pind, sz_psz2ind(size_class),
|
|
|
|
"sz_psz2ind() does not reverse sz_pind2sz(): pind=%u -->"
|
2016-04-18 07:16:11 +08:00
|
|
|
" size_class=%zu --> pind=%u --> size_class=%zu", pind,
|
2017-05-31 01:45:37 +08:00
|
|
|
size_class, sz_psz2ind(size_class),
|
|
|
|
sz_pind2sz(sz_psz2ind(size_class)));
|
|
|
|
assert_zu_eq(size_class, sz_pind2sz(sz_psz2ind(size_class)),
|
|
|
|
"sz_pind2sz() does not reverse sz_psz2ind(): pind=%u -->"
|
2016-04-18 07:16:11 +08:00
|
|
|
" size_class=%zu --> pind=%u --> size_class=%zu", pind,
|
2017-05-31 01:45:37 +08:00
|
|
|
size_class, sz_psz2ind(size_class),
|
|
|
|
sz_pind2sz(sz_psz2ind(size_class)));
|
2016-04-18 07:16:11 +08:00
|
|
|
|
2018-07-20 08:08:10 +08:00
|
|
|
if (size_class == SC_LARGE_MAXCLASS) {
|
|
|
|
assert_u_eq(SC_NPSIZES, sz_psz2ind(size_class + 1),
|
|
|
|
"Next size_class does not round up properly");
|
|
|
|
} else {
|
|
|
|
assert_u_eq(pind + 1, sz_psz2ind(size_class + 1),
|
|
|
|
"Next size_class does not round up properly");
|
|
|
|
}
|
2016-04-18 07:16:11 +08:00
|
|
|
|
|
|
|
assert_zu_eq(size_class, (pind > 0) ?
|
2017-05-31 01:45:37 +08:00
|
|
|
sz_psz2u(sz_pind2sz(pind-1)+1) : sz_psz2u(1),
|
|
|
|
"sz_psz2u() does not round up to size class");
|
|
|
|
assert_zu_eq(size_class, sz_psz2u(size_class-1),
|
|
|
|
"sz_psz2u() does not round up to size class");
|
|
|
|
assert_zu_eq(size_class, sz_psz2u(size_class),
|
|
|
|
"sz_psz2u() does not compute same size class");
|
|
|
|
assert_zu_eq(sz_psz2u(size_class+1), sz_pind2sz(pind+1),
|
|
|
|
"sz_psz2u() does not round up to next size class");
|
2016-04-18 07:16:11 +08:00
|
|
|
}
|
|
|
|
|
2017-05-31 01:45:37 +08:00
|
|
|
assert_u_eq(pind, sz_psz2ind(sz_pind2sz(pind)),
|
|
|
|
"sz_psz2ind() does not reverse sz_pind2sz()");
|
|
|
|
assert_zu_eq(max_psz, sz_pind2sz(sz_psz2ind(max_psz)),
|
|
|
|
"sz_pind2sz() does not reverse sz_psz2ind()");
|
|
|
|
|
|
|
|
assert_zu_eq(size_class, sz_psz2u(sz_pind2sz(pind-1)+1),
|
|
|
|
"sz_psz2u() does not round up to size class");
|
|
|
|
assert_zu_eq(size_class, sz_psz2u(size_class-1),
|
|
|
|
"sz_psz2u() does not round up to size class");
|
|
|
|
assert_zu_eq(size_class, sz_psz2u(size_class),
|
|
|
|
"sz_psz2u() does not compute same size class");
|
2016-04-18 07:16:11 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_overflow) {
|
2016-11-04 12:18:50 +08:00
|
|
|
size_t max_size_class, max_psz;
|
2016-02-26 07:29:49 +08:00
|
|
|
|
|
|
|
max_size_class = get_max_size_class();
|
2016-11-04 12:18:50 +08:00
|
|
|
max_psz = max_size_class + PAGE;
|
2016-02-26 07:29:49 +08:00
|
|
|
|
2017-12-15 04:46:39 +08:00
|
|
|
assert_u_eq(sz_size2index(max_size_class+1), SC_NSIZES,
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_size2index() should return NSIZES on overflow");
|
2017-12-15 04:46:39 +08:00
|
|
|
assert_u_eq(sz_size2index(ZU(PTRDIFF_MAX)+1), SC_NSIZES,
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_size2index() should return NSIZES on overflow");
|
2017-12-15 04:46:39 +08:00
|
|
|
assert_u_eq(sz_size2index(SIZE_T_MAX), SC_NSIZES,
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_size2index() should return NSIZES on overflow");
|
|
|
|
|
|
|
|
assert_zu_eq(sz_s2u(max_size_class+1), 0,
|
|
|
|
"sz_s2u() should return 0 for unsupported size");
|
|
|
|
assert_zu_eq(sz_s2u(ZU(PTRDIFF_MAX)+1), 0,
|
|
|
|
"sz_s2u() should return 0 for unsupported size");
|
|
|
|
assert_zu_eq(sz_s2u(SIZE_T_MAX), 0,
|
|
|
|
"sz_s2u() should return 0 on overflow");
|
|
|
|
|
2018-07-20 08:08:10 +08:00
|
|
|
assert_u_eq(sz_psz2ind(max_size_class+1), SC_NPSIZES,
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_psz2ind() should return NPSIZES on overflow");
|
2018-07-20 08:08:10 +08:00
|
|
|
assert_u_eq(sz_psz2ind(ZU(PTRDIFF_MAX)+1), SC_NPSIZES,
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_psz2ind() should return NPSIZES on overflow");
|
2018-07-20 08:08:10 +08:00
|
|
|
assert_u_eq(sz_psz2ind(SIZE_T_MAX), SC_NPSIZES,
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_psz2ind() should return NPSIZES on overflow");
|
|
|
|
|
|
|
|
assert_zu_eq(sz_psz2u(max_size_class+1), max_psz,
|
|
|
|
"sz_psz2u() should return (LARGE_MAXCLASS + PAGE) for unsupported"
|
2016-11-04 12:18:50 +08:00
|
|
|
" size");
|
2017-05-31 01:45:37 +08:00
|
|
|
assert_zu_eq(sz_psz2u(ZU(PTRDIFF_MAX)+1), max_psz,
|
|
|
|
"sz_psz2u() should return (LARGE_MAXCLASS + PAGE) for unsupported "
|
2016-11-04 12:18:50 +08:00
|
|
|
"size");
|
2017-05-31 01:45:37 +08:00
|
|
|
assert_zu_eq(sz_psz2u(SIZE_T_MAX), max_psz,
|
|
|
|
"sz_psz2u() should return (LARGE_MAXCLASS + PAGE) on overflow");
|
2016-02-26 07:29:49 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
2015-07-10 12:36:33 +08:00
|
|
|
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_size_classes,
|
2016-04-18 07:16:11 +08:00
|
|
|
test_psize_classes,
|
2017-01-20 10:15:45 +08:00
|
|
|
test_overflow);
|
2015-07-10 12:36:33 +08:00
|
|
|
}
|