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);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_d_eq(mallctl("arenas.nlextents", (void *)&nlextents, &sz, NULL,
|
2016-10-28 12:31:25 +08:00
|
|
|
0), 0, "Unexpected mallctl() error");
|
2015-07-10 12:36:33 +08:00
|
|
|
|
|
|
|
miblen = sizeof(mib) / sizeof(size_t);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_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);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_d_eq(mallctlbymib(mib, miblen, (void *)&max_size_class, &sz,
|
2016-10-28 12:31:25 +08:00
|
|
|
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)) {
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_true(index < max_index,
|
2015-07-10 12:36:33 +08:00
|
|
|
"Loop conditionals should be equivalent; index=%u, "
|
|
|
|
"size_class=%zu (%#zx)", index, size_class, size_class);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_true(size_class < max_size_class,
|
2015-07-10 12:36:33 +08:00
|
|
|
"Loop conditionals should be equivalent; index=%u, "
|
|
|
|
"size_class=%zu (%#zx)", index, size_class, size_class);
|
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_u_eq(index, sz_size2index(size_class),
|
2017-05-31 01:45:37 +08:00
|
|
|
"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)));
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(size_class,
|
2017-05-31 01:45:37 +08:00
|
|
|
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)));
|
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_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");
|
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_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");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(size_class, sz_s2u(size_class-1),
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_s2u() does not round up to size class");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(size_class, sz_s2u(size_class),
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_s2u() does not compute same size class");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(sz_s2u(size_class+1), sz_index2size(index+1),
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_s2u() does not round up to next size class");
|
2015-07-10 12:36:33 +08:00
|
|
|
}
|
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_u_eq(index, sz_size2index(sz_index2size(index)),
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_size2index() does not reverse sz_index2size()");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(max_size_class, sz_index2size(
|
2017-05-31 01:45:37 +08:00
|
|
|
sz_size2index(max_size_class)),
|
|
|
|
"sz_index2size() does not reverse sz_size2index()");
|
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(size_class, sz_s2u(sz_index2size(index-1)+1),
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_s2u() does not round up to size class");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(size_class, sz_s2u(size_class-1),
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_s2u() does not round up to size class");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(size_class, sz_s2u(size_class),
|
2017-05-31 01:45:37 +08:00
|
|
|
"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)) {
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_true(pind < max_pind,
|
2016-04-18 07:16:11 +08:00
|
|
|
"Loop conditionals should be equivalent; pind=%u, "
|
|
|
|
"size_class=%zu (%#zx)", pind, size_class, size_class);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_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);
|
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_u_eq(pind, sz_psz2ind(size_class),
|
2017-05-31 01:45:37 +08:00
|
|
|
"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)));
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(size_class, sz_pind2sz(sz_psz2ind(size_class)),
|
2017-05-31 01:45:37 +08:00
|
|
|
"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) {
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_u_eq(SC_NPSIZES, sz_psz2ind(size_class + 1),
|
2018-07-20 08:08:10 +08:00
|
|
|
"Next size_class does not round up properly");
|
|
|
|
} else {
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_u_eq(pind + 1, sz_psz2ind(size_class + 1),
|
2018-07-20 08:08:10 +08:00
|
|
|
"Next size_class does not round up properly");
|
|
|
|
}
|
2016-04-18 07:16:11 +08:00
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_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");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(size_class, sz_psz2u(size_class-1),
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_psz2u() does not round up to size class");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(size_class, sz_psz2u(size_class),
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_psz2u() does not compute same size class");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(sz_psz2u(size_class+1), sz_pind2sz(pind+1),
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_psz2u() does not round up to next size class");
|
2016-04-18 07:16:11 +08:00
|
|
|
}
|
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_u_eq(pind, sz_psz2ind(sz_pind2sz(pind)),
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_psz2ind() does not reverse sz_pind2sz()");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(max_psz, sz_pind2sz(sz_psz2ind(max_psz)),
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_pind2sz() does not reverse sz_psz2ind()");
|
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(size_class, sz_psz2u(sz_pind2sz(pind-1)+1),
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_psz2u() does not round up to size class");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(size_class, sz_psz2u(size_class-1),
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_psz2u() does not round up to size class");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(size_class, sz_psz2u(size_class),
|
2017-05-31 01:45:37 +08:00
|
|
|
"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
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_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");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_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");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_u_eq(sz_size2index(SIZE_T_MAX), SC_NSIZES,
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_size2index() should return NSIZES on overflow");
|
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(sz_s2u(max_size_class+1), 0,
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_s2u() should return 0 for unsupported size");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(sz_s2u(ZU(PTRDIFF_MAX)+1), 0,
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_s2u() should return 0 for unsupported size");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(sz_s2u(SIZE_T_MAX), 0,
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_s2u() should return 0 on overflow");
|
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_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");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_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");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_u_eq(sz_psz2ind(SIZE_T_MAX), SC_NPSIZES,
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_psz2ind() should return NPSIZES on overflow");
|
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(sz_psz2u(max_size_class+1), max_psz,
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_psz2u() should return (LARGE_MAXCLASS + PAGE) for unsupported"
|
2016-11-04 12:18:50 +08:00
|
|
|
" size");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(sz_psz2u(ZU(PTRDIFF_MAX)+1), max_psz,
|
2017-05-31 01:45:37 +08:00
|
|
|
"sz_psz2u() should return (LARGE_MAXCLASS + PAGE) for unsupported "
|
2016-11-04 12:18:50 +08:00
|
|
|
"size");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_zu_eq(sz_psz2u(SIZE_T_MAX), max_psz,
|
2017-05-31 01:45:37 +08:00
|
|
|
"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
|
|
|
}
|