1 #include "test/jemalloc_test.h"
2
3 static size_t
get_max_size_class(void)4 get_max_size_class(void)
5 {
6 unsigned nhchunks;
7 size_t mib[4];
8 size_t sz, miblen, max_size_class;
9
10 sz = sizeof(unsigned);
11 assert_d_eq(mallctl("arenas.nhchunks", &nhchunks, &sz, NULL, 0), 0,
12 "Unexpected mallctl() error");
13
14 miblen = sizeof(mib) / sizeof(size_t);
15 assert_d_eq(mallctlnametomib("arenas.hchunk.0.size", mib, &miblen), 0,
16 "Unexpected mallctlnametomib() error");
17 mib[2] = nhchunks - 1;
18
19 sz = sizeof(size_t);
20 assert_d_eq(mallctlbymib(mib, miblen, &max_size_class, &sz, NULL, 0), 0,
21 "Unexpected mallctlbymib() error");
22
23 return (max_size_class);
24 }
25
TEST_BEGIN(test_size_classes)26 TEST_BEGIN(test_size_classes)
27 {
28 size_t size_class, max_size_class;
29 szind_t index, max_index;
30
31 max_size_class = get_max_size_class();
32 max_index = size2index(max_size_class);
33
34 for (index = 0, size_class = index2size(index); index < max_index ||
35 size_class < max_size_class; index++, size_class =
36 index2size(index)) {
37 assert_true(index < max_index,
38 "Loop conditionals should be equivalent; index=%u, "
39 "size_class=%zu (%#zx)", index, size_class, size_class);
40 assert_true(size_class < max_size_class,
41 "Loop conditionals should be equivalent; index=%u, "
42 "size_class=%zu (%#zx)", index, size_class, size_class);
43
44 assert_u_eq(index, size2index(size_class),
45 "size2index() does not reverse index2size(): index=%u -->"
46 " size_class=%zu --> index=%u --> size_class=%zu", index,
47 size_class, size2index(size_class),
48 index2size(size2index(size_class)));
49 assert_zu_eq(size_class, index2size(size2index(size_class)),
50 "index2size() does not reverse size2index(): index=%u -->"
51 " size_class=%zu --> index=%u --> size_class=%zu", index,
52 size_class, size2index(size_class),
53 index2size(size2index(size_class)));
54
55 assert_u_eq(index+1, size2index(size_class+1),
56 "Next size_class does not round up properly");
57
58 assert_zu_eq(size_class, (index > 0) ?
59 s2u(index2size(index-1)+1) : s2u(1),
60 "s2u() does not round up to size class");
61 assert_zu_eq(size_class, s2u(size_class-1),
62 "s2u() does not round up to size class");
63 assert_zu_eq(size_class, s2u(size_class),
64 "s2u() does not compute same size class");
65 assert_zu_eq(s2u(size_class+1), index2size(index+1),
66 "s2u() does not round up to next size class");
67 }
68
69 assert_u_eq(index, size2index(index2size(index)),
70 "size2index() does not reverse index2size()");
71 assert_zu_eq(max_size_class, index2size(size2index(max_size_class)),
72 "index2size() does not reverse size2index()");
73
74 assert_zu_eq(size_class, s2u(index2size(index-1)+1),
75 "s2u() does not round up to size class");
76 assert_zu_eq(size_class, s2u(size_class-1),
77 "s2u() does not round up to size class");
78 assert_zu_eq(size_class, s2u(size_class),
79 "s2u() does not compute same size class");
80 }
81 TEST_END
82
TEST_BEGIN(test_overflow)83 TEST_BEGIN(test_overflow)
84 {
85 size_t max_size_class;
86
87 max_size_class = get_max_size_class();
88
89 assert_u_ge(size2index(max_size_class+1), NSIZES,
90 "size2index() should return >= NSIZES on overflow");
91 assert_u_ge(size2index(ZU(PTRDIFF_MAX)+1), NSIZES,
92 "size2index() should return >= NSIZES on overflow");
93 assert_u_ge(size2index(SIZE_T_MAX), NSIZES,
94 "size2index() should return >= NSIZES on overflow");
95
96 assert_zu_gt(s2u(max_size_class+1), HUGE_MAXCLASS,
97 "s2u() should return > HUGE_MAXCLASS for unsupported size");
98 assert_zu_gt(s2u(ZU(PTRDIFF_MAX)+1), HUGE_MAXCLASS,
99 "s2u() should return > HUGE_MAXCLASS for unsupported size");
100 assert_zu_eq(s2u(SIZE_T_MAX), 0,
101 "s2u() should return 0 on overflow");
102 }
103 TEST_END
104
105 int
main(void)106 main(void)
107 {
108
109 return (test(
110 test_size_classes,
111 test_overflow));
112 }
113