]> git.saurik.com Git - redis.git/blob - deps/jemalloc/test/allocm.c
80be673b8fdb50cc46d5b99fe34495d26431fb67
[redis.git] / deps / jemalloc / test / allocm.c
1 #define JEMALLOC_MANGLE
2 #include "jemalloc_test.h"
3
4 #define CHUNK 0x400000
5 /* #define MAXALIGN ((size_t)UINT64_C(0x80000000000)) */
6 #define MAXALIGN ((size_t)0x2000000LU)
7 #define NITER 4
8
9 int
10 main(void)
11 {
12 int r;
13 void *p;
14 size_t nsz, rsz, sz, alignment, total;
15 unsigned i;
16 void *ps[NITER];
17
18 malloc_printf("Test begin\n");
19
20 sz = 42;
21 nsz = 0;
22 r = nallocm(&nsz, sz, 0);
23 if (r != ALLOCM_SUCCESS) {
24 malloc_printf("Unexpected nallocm() error\n");
25 abort();
26 }
27 rsz = 0;
28 r = allocm(&p, &rsz, sz, 0);
29 if (r != ALLOCM_SUCCESS) {
30 malloc_printf("Unexpected allocm() error\n");
31 abort();
32 }
33 if (rsz < sz)
34 malloc_printf("Real size smaller than expected\n");
35 if (nsz != rsz)
36 malloc_printf("nallocm()/allocm() rsize mismatch\n");
37 if (dallocm(p, 0) != ALLOCM_SUCCESS)
38 malloc_printf("Unexpected dallocm() error\n");
39
40 r = allocm(&p, NULL, sz, 0);
41 if (r != ALLOCM_SUCCESS) {
42 malloc_printf("Unexpected allocm() error\n");
43 abort();
44 }
45 if (dallocm(p, 0) != ALLOCM_SUCCESS)
46 malloc_printf("Unexpected dallocm() error\n");
47
48 nsz = 0;
49 r = nallocm(&nsz, sz, ALLOCM_ZERO);
50 if (r != ALLOCM_SUCCESS) {
51 malloc_printf("Unexpected nallocm() error\n");
52 abort();
53 }
54 rsz = 0;
55 r = allocm(&p, &rsz, sz, ALLOCM_ZERO);
56 if (r != ALLOCM_SUCCESS) {
57 malloc_printf("Unexpected allocm() error\n");
58 abort();
59 }
60 if (nsz != rsz)
61 malloc_printf("nallocm()/allocm() rsize mismatch\n");
62 if (dallocm(p, 0) != ALLOCM_SUCCESS)
63 malloc_printf("Unexpected dallocm() error\n");
64
65 #if LG_SIZEOF_PTR == 3
66 alignment = UINT64_C(0x8000000000000000);
67 sz = UINT64_C(0x8000000000000000);
68 #else
69 alignment = 0x80000000LU;
70 sz = 0x80000000LU;
71 #endif
72 nsz = 0;
73 r = nallocm(&nsz, sz, ALLOCM_ALIGN(alignment));
74 if (r == ALLOCM_SUCCESS) {
75 malloc_printf(
76 "Expected error for nallocm(&nsz, %zu, %#x)\n",
77 sz, ALLOCM_ALIGN(alignment));
78 }
79 rsz = 0;
80 r = allocm(&p, &rsz, sz, ALLOCM_ALIGN(alignment));
81 if (r == ALLOCM_SUCCESS) {
82 malloc_printf(
83 "Expected error for allocm(&p, %zu, %#x)\n",
84 sz, ALLOCM_ALIGN(alignment));
85 }
86 if (nsz != rsz)
87 malloc_printf("nallocm()/allocm() rsize mismatch\n");
88
89 #if LG_SIZEOF_PTR == 3
90 alignment = UINT64_C(0x4000000000000000);
91 sz = UINT64_C(0x8400000000000001);
92 #else
93 alignment = 0x40000000LU;
94 sz = 0x84000001LU;
95 #endif
96 nsz = 0;
97 r = nallocm(&nsz, sz, ALLOCM_ALIGN(alignment));
98 if (r != ALLOCM_SUCCESS)
99 malloc_printf("Unexpected nallocm() error\n");
100 rsz = 0;
101 r = allocm(&p, &rsz, sz, ALLOCM_ALIGN(alignment));
102 if (r == ALLOCM_SUCCESS) {
103 malloc_printf(
104 "Expected error for allocm(&p, %zu, %#x)\n",
105 sz, ALLOCM_ALIGN(alignment));
106 }
107
108 alignment = 0x10LU;
109 #if LG_SIZEOF_PTR == 3
110 sz = UINT64_C(0xfffffffffffffff0);
111 #else
112 sz = 0xfffffff0LU;
113 #endif
114 nsz = 0;
115 r = nallocm(&nsz, sz, ALLOCM_ALIGN(alignment));
116 if (r == ALLOCM_SUCCESS) {
117 malloc_printf(
118 "Expected error for nallocm(&nsz, %zu, %#x)\n",
119 sz, ALLOCM_ALIGN(alignment));
120 }
121 rsz = 0;
122 r = allocm(&p, &rsz, sz, ALLOCM_ALIGN(alignment));
123 if (r == ALLOCM_SUCCESS) {
124 malloc_printf(
125 "Expected error for allocm(&p, %zu, %#x)\n",
126 sz, ALLOCM_ALIGN(alignment));
127 }
128 if (nsz != rsz)
129 malloc_printf("nallocm()/allocm() rsize mismatch\n");
130
131 for (i = 0; i < NITER; i++)
132 ps[i] = NULL;
133
134 for (alignment = 8;
135 alignment <= MAXALIGN;
136 alignment <<= 1) {
137 total = 0;
138 malloc_printf("Alignment: %zu\n", alignment);
139 for (sz = 1;
140 sz < 3 * alignment && sz < (1U << 31);
141 sz += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
142 for (i = 0; i < NITER; i++) {
143 nsz = 0;
144 r = nallocm(&nsz, sz,
145 ALLOCM_ALIGN(alignment) | ALLOCM_ZERO);
146 if (r != ALLOCM_SUCCESS) {
147 malloc_printf(
148 "nallocm() error for size %zu"
149 " (%#zx): %d\n",
150 sz, sz, r);
151 exit(1);
152 }
153 rsz = 0;
154 r = allocm(&ps[i], &rsz, sz,
155 ALLOCM_ALIGN(alignment) | ALLOCM_ZERO);
156 if (r != ALLOCM_SUCCESS) {
157 malloc_printf(
158 "allocm() error for size %zu"
159 " (%#zx): %d\n",
160 sz, sz, r);
161 exit(1);
162 }
163 if (rsz < sz) {
164 malloc_printf(
165 "Real size smaller than"
166 " expected\n");
167 }
168 if (nsz != rsz) {
169 malloc_printf(
170 "nallocm()/allocm() rsize"
171 " mismatch\n");
172 }
173 if ((uintptr_t)p & (alignment-1)) {
174 malloc_printf(
175 "%p inadequately aligned for"
176 " alignment: %zu\n", p, alignment);
177 }
178 sallocm(ps[i], &rsz, 0);
179 total += rsz;
180 if (total >= (MAXALIGN << 1))
181 break;
182 }
183 for (i = 0; i < NITER; i++) {
184 if (ps[i] != NULL) {
185 dallocm(ps[i], 0);
186 ps[i] = NULL;
187 }
188 }
189 }
190 }
191
192 malloc_printf("Test end\n");
193 return (0);
194 }