]>
Commit | Line | Data |
---|---|---|
a78e148b | 1 | #define JEMALLOC_MANGLE |
2 | #include "jemalloc_test.h" | |
3 | ||
4 | #define CHUNK 0x400000 | |
4934f93d | 5 | /* #define MAXALIGN ((size_t)UINT64_C(0x80000000000)) */ |
6 | #define MAXALIGN ((size_t)0x2000000LU) | |
a78e148b | 7 | #define NITER 4 |
8 | ||
9 | int | |
10 | main(void) | |
11 | { | |
12 | int r; | |
13 | void *p; | |
4934f93d | 14 | size_t nsz, rsz, sz, alignment, total; |
a78e148b | 15 | unsigned i; |
16 | void *ps[NITER]; | |
17 | ||
4934f93d | 18 | malloc_printf("Test begin\n"); |
a78e148b | 19 | |
4934f93d | 20 | sz = 42; |
21 | nsz = 0; | |
22 | r = nallocm(&nsz, sz, 0); | |
a78e148b | 23 | if (r != ALLOCM_SUCCESS) { |
4934f93d | 24 | malloc_printf("Unexpected nallocm() error\n"); |
a78e148b | 25 | abort(); |
26 | } | |
4934f93d | 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"); | |
a78e148b | 39 | |
4934f93d | 40 | r = allocm(&p, NULL, sz, 0); |
a78e148b | 41 | if (r != ALLOCM_SUCCESS) { |
4934f93d | 42 | malloc_printf("Unexpected allocm() error\n"); |
a78e148b | 43 | abort(); |
44 | } | |
4934f93d | 45 | if (dallocm(p, 0) != ALLOCM_SUCCESS) |
46 | malloc_printf("Unexpected dallocm() error\n"); | |
a78e148b | 47 | |
4934f93d | 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); | |
a78e148b | 56 | if (r != ALLOCM_SUCCESS) { |
4934f93d | 57 | malloc_printf("Unexpected allocm() error\n"); |
a78e148b | 58 | abort(); |
59 | } | |
4934f93d | 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"); | |
a78e148b | 64 | |
65 | #if LG_SIZEOF_PTR == 3 | |
4934f93d | 66 | alignment = UINT64_C(0x8000000000000000); |
67 | sz = UINT64_C(0x8000000000000000); | |
a78e148b | 68 | #else |
69 | alignment = 0x80000000LU; | |
70 | sz = 0x80000000LU; | |
71 | #endif | |
4934f93d | 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)); | |
a78e148b | 81 | if (r == ALLOCM_SUCCESS) { |
4934f93d | 82 | malloc_printf( |
83 | "Expected error for allocm(&p, %zu, %#x)\n", | |
a78e148b | 84 | sz, ALLOCM_ALIGN(alignment)); |
85 | } | |
4934f93d | 86 | if (nsz != rsz) |
87 | malloc_printf("nallocm()/allocm() rsize mismatch\n"); | |
a78e148b | 88 | |
89 | #if LG_SIZEOF_PTR == 3 | |
4934f93d | 90 | alignment = UINT64_C(0x4000000000000000); |
91 | sz = UINT64_C(0x8400000000000001); | |
a78e148b | 92 | #else |
93 | alignment = 0x40000000LU; | |
94 | sz = 0x84000001LU; | |
95 | #endif | |
4934f93d | 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)); | |
a78e148b | 102 | if (r == ALLOCM_SUCCESS) { |
4934f93d | 103 | malloc_printf( |
104 | "Expected error for allocm(&p, %zu, %#x)\n", | |
a78e148b | 105 | sz, ALLOCM_ALIGN(alignment)); |
106 | } | |
107 | ||
4934f93d | 108 | alignment = 0x10LU; |
a78e148b | 109 | #if LG_SIZEOF_PTR == 3 |
4934f93d | 110 | sz = UINT64_C(0xfffffffffffffff0); |
a78e148b | 111 | #else |
4934f93d | 112 | sz = 0xfffffff0LU; |
a78e148b | 113 | #endif |
4934f93d | 114 | nsz = 0; |
115 | r = nallocm(&nsz, sz, ALLOCM_ALIGN(alignment)); | |
a78e148b | 116 | if (r == ALLOCM_SUCCESS) { |
4934f93d | 117 | malloc_printf( |
118 | "Expected error for nallocm(&nsz, %zu, %#x)\n", | |
a78e148b | 119 | sz, ALLOCM_ALIGN(alignment)); |
120 | } | |
4934f93d | 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"); | |
a78e148b | 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; | |
4934f93d | 138 | malloc_printf("Alignment: %zu\n", alignment); |
a78e148b | 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++) { | |
4934f93d | 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, | |
a78e148b | 155 | ALLOCM_ALIGN(alignment) | ALLOCM_ZERO); |
156 | if (r != ALLOCM_SUCCESS) { | |
4934f93d | 157 | malloc_printf( |
158 | "allocm() error for size %zu" | |
159 | " (%#zx): %d\n", | |
a78e148b | 160 | sz, sz, r); |
161 | exit(1); | |
162 | } | |
4934f93d | 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 | } | |
a78e148b | 173 | if ((uintptr_t)p & (alignment-1)) { |
4934f93d | 174 | malloc_printf( |
a78e148b | 175 | "%p inadequately aligned for" |
176 | " alignment: %zu\n", p, alignment); | |
177 | } | |
4934f93d | 178 | sallocm(ps[i], &rsz, 0); |
179 | total += rsz; | |
a78e148b | 180 | if (total >= (MAXALIGN << 1)) |
181 | break; | |
182 | } | |
183 | for (i = 0; i < NITER; i++) { | |
184 | if (ps[i] != NULL) { | |
4934f93d | 185 | dallocm(ps[i], 0); |
a78e148b | 186 | ps[i] = NULL; |
187 | } | |
188 | } | |
189 | } | |
190 | } | |
191 | ||
4934f93d | 192 | malloc_printf("Test end\n"); |
a78e148b | 193 | return (0); |
194 | } |