1 | #include <thread.hfa> |
---|
2 | #include <kernel.hfa> // processor |
---|
3 | #include <stdlib.hfa> // *allocs |
---|
4 | #include <malloc.h> // malloc_* |
---|
5 | |
---|
6 | // #include <time.hfa> |
---|
7 | // #define __CFA_DEFAULT_PREEMPTION__ 1000`us |
---|
8 | // //#define __CFA_DEFAULT_PREEMPTION__ 0 |
---|
9 | |
---|
10 | // Duration default_preemption() { |
---|
11 | // return __CFA_DEFAULT_PREEMPTION__; |
---|
12 | // } |
---|
13 | |
---|
14 | #define __U_DEFAULT_MMAP_START__ (512 * 1024 + 1) |
---|
15 | size_t default_mmap_start() __attribute__(( weak )) { |
---|
16 | return __U_DEFAULT_MMAP_START__; |
---|
17 | } // default_mmap_start |
---|
18 | |
---|
19 | thread Worker { |
---|
20 | }; // Worker |
---|
21 | |
---|
22 | void main( Worker & ) { |
---|
23 | enum { NoOfAllocs = 5000, NoOfMmaps = 10 }; |
---|
24 | char *locns[NoOfAllocs]; |
---|
25 | int i; |
---|
26 | |
---|
27 | // check alloc/free |
---|
28 | |
---|
29 | for ( int j = 0; j < 40; j += 1 ) { |
---|
30 | for ( i = 0; i < NoOfAllocs; i += 1 ) { |
---|
31 | locns[i] = alloc( i ); |
---|
32 | //sout | (void *)locns[i] | endl; |
---|
33 | for ( int k = 0; k < i; k += 1 ) locns[i][k] = '\345'; |
---|
34 | } // for |
---|
35 | //sout | (char *)sbrk(0) - start | " bytes" | endl; |
---|
36 | |
---|
37 | for ( i = 0; i < NoOfAllocs; i += 1 ) { |
---|
38 | //sout | (void *)locns[i] | endl; |
---|
39 | for ( int k = 0; k < i; k += 1 ) if ( locns[i][k] != '\345' ) abort( "new/delete corrupt storage1" ); |
---|
40 | free( locns[i] ); |
---|
41 | } // for |
---|
42 | //sout | (char *)sbrk(0) - start | " bytes" | endl; |
---|
43 | |
---|
44 | for ( i = 0; i < NoOfAllocs; i += 1 ) { |
---|
45 | locns[i] = alloc( i ); |
---|
46 | //sout | (void *)locns[i] | endl; |
---|
47 | for ( int k = 0; k < i; k += 1 ) locns[i][k] = '\345'; |
---|
48 | } // for |
---|
49 | for ( i = NoOfAllocs - 1; i >=0 ; i -= 1 ) { |
---|
50 | //sout | (void *)locns[i] | endl; |
---|
51 | for ( int k = 0; k < i; k += 1 ) if ( locns[i][k] != '\345' ) abort( "new/delete corrupt storage2" ); |
---|
52 | free( locns[i] ); |
---|
53 | } // for |
---|
54 | } // for |
---|
55 | |
---|
56 | // check malloc/free (sbrk) |
---|
57 | |
---|
58 | for ( i = 0; i < NoOfAllocs; i += 1 ) { |
---|
59 | size_t s = (i + 1) * 20; |
---|
60 | char *area = (char *)malloc( s ); |
---|
61 | if ( area == 0 ) abort( "malloc/free out of memory" ); |
---|
62 | area[0] = '\345'; area[s - 1] = '\345'; // fill first/last |
---|
63 | area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte |
---|
64 | free( area ); |
---|
65 | } // for |
---|
66 | |
---|
67 | for ( i = 0; i < NoOfAllocs; i += 1 ) { |
---|
68 | size_t s = i + 1; // +1 to make initialization simpler |
---|
69 | locns[i] = (char *)malloc( s ); |
---|
70 | if ( locns[i] == 0 ) abort( "malloc/free out of memory" ); |
---|
71 | locns[i][0] = '\345'; locns[i][s - 1] = '\345'; // fill first/last |
---|
72 | locns[i][malloc_usable_size( locns[i] ) - 1] = '\345'; // fill ultimate byte |
---|
73 | } // for |
---|
74 | for ( i = 0; i < NoOfAllocs; i += 1 ) { |
---|
75 | size_t s = i + 1; |
---|
76 | if ( locns[i][0] != '\345' || locns[i][s - 1] != '\345' || |
---|
77 | locns[i][malloc_usable_size( locns[i] ) - 1] != '\345' ) abort( "malloc/free corrupt storage" ); |
---|
78 | free( locns[i] ); |
---|
79 | } // for |
---|
80 | |
---|
81 | // check malloc/free (mmap) |
---|
82 | |
---|
83 | for ( i = 0; i < NoOfMmaps; i += 1 ) { |
---|
84 | size_t s = i + default_mmap_start(); // cross over point |
---|
85 | char *area = (char *)malloc( s ); |
---|
86 | if ( area == 0 ) abort( "malloc/free out of memory" ); |
---|
87 | area[0] = '\345'; area[s - 1] = '\345'; // fill first/last |
---|
88 | area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte |
---|
89 | free( area ); |
---|
90 | } // for |
---|
91 | |
---|
92 | for ( i = 0; i < NoOfMmaps; i += 1 ) { |
---|
93 | size_t s = i + default_mmap_start(); // cross over point |
---|
94 | locns[i] = (char *)malloc( s ); |
---|
95 | if ( locns[i] == 0 ) abort( "malloc/free out of memory" ); |
---|
96 | locns[i][0] = '\345'; locns[i][s - 1] = '\345'; // fill first/last |
---|
97 | locns[i][malloc_usable_size( locns[i] ) - 1] = '\345'; // fill ultimate byte |
---|
98 | } // for |
---|
99 | for ( i = 0; i < NoOfMmaps; i += 1 ) { |
---|
100 | size_t s = i + default_mmap_start(); // cross over point |
---|
101 | if ( locns[i][0] != '\345' || locns[i][s - 1] != '\345' || |
---|
102 | locns[i][malloc_usable_size( locns[i] ) - 1] != '\345' ) abort( "malloc/free corrupt storage" ); |
---|
103 | free( locns[i] ); |
---|
104 | } // for |
---|
105 | |
---|
106 | // check calloc/free (sbrk) |
---|
107 | |
---|
108 | for ( i = 0; i < NoOfAllocs; i += 1 ) { |
---|
109 | size_t s = (i + 1) * 20; |
---|
110 | char *area = (char *)calloc( 5, s ); |
---|
111 | if ( area == 0 ) abort( "calloc/free out of memory" ); |
---|
112 | if ( area[0] != '\0' || area[s - 1] != '\0' || |
---|
113 | area[malloc_usable_size( area ) - 1] != '\0' || |
---|
114 | ! malloc_zero_fill( area ) ) abort( "calloc/free corrupt storage1" ); |
---|
115 | area[0] = '\345'; area[s - 1] = '\345'; // fill first/last |
---|
116 | area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte |
---|
117 | free( area ); |
---|
118 | } // for |
---|
119 | |
---|
120 | for ( i = 0; i < NoOfAllocs; i += 1 ) { |
---|
121 | size_t s = i + 1; |
---|
122 | locns[i] = (char *)calloc( 5, s ); |
---|
123 | if ( locns[i] == 0 ) abort( "calloc/free out of memory" ); |
---|
124 | if ( locns[i][0] != '\0' || locns[i][s - 1] != '\0' || |
---|
125 | locns[i][malloc_usable_size( locns[i] ) - 1] != '\0' || |
---|
126 | ! malloc_zero_fill( locns[i] ) ) abort( "calloc/free corrupt storage2" ); |
---|
127 | locns[i][0] = '\345'; locns[i][s - 1] = '\345'; // fill first/last |
---|
128 | locns[i][malloc_usable_size( locns[i] ) - 1] = '\345'; // fill ultimate byte |
---|
129 | } // for |
---|
130 | for ( i = 0; i < NoOfAllocs; i += 1 ) { |
---|
131 | size_t s = i + 1; |
---|
132 | if ( locns[i][0] != '\345' || locns[i][s - 1] != '\345' || |
---|
133 | locns[i][malloc_usable_size( locns[i] ) - 1] != '\345' ) abort( "calloc/free corrupt storage3" ); |
---|
134 | free( locns[i] ); |
---|
135 | } // for |
---|
136 | |
---|
137 | // check calloc/free (mmap) |
---|
138 | |
---|
139 | for ( i = 0; i < NoOfMmaps; i += 1 ) { |
---|
140 | size_t s = i + default_mmap_start(); // cross over point |
---|
141 | char *area = (char *)calloc( 1, s ); |
---|
142 | if ( area == 0 ) abort( "calloc/free out of memory" ); |
---|
143 | if ( area[0] != '\0' || area[s - 1] != '\0' ) abort( "calloc/free corrupt storage4.1" ); |
---|
144 | if ( area[malloc_usable_size( area ) - 1] != '\0' ) abort( "calloc/free corrupt storage4.2" ); |
---|
145 | if ( ! malloc_zero_fill( area ) ) abort( "calloc/free corrupt storage4.3" ); |
---|
146 | area[0] = '\345'; area[s - 1] = '\345'; // fill first/last |
---|
147 | area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte |
---|
148 | free( area ); |
---|
149 | } // for |
---|
150 | |
---|
151 | for ( i = 0; i < NoOfMmaps; i += 1 ) { |
---|
152 | size_t s = i + default_mmap_start(); // cross over point |
---|
153 | locns[i] = (char *)calloc( 1, s ); |
---|
154 | if ( locns[i] == 0 ) abort( "calloc/free out of memory" ); |
---|
155 | if ( locns[i][0] != '\0' || locns[i][s - 1] != '\0' || |
---|
156 | locns[i][malloc_usable_size( locns[i] ) - 1] != '\0' || |
---|
157 | ! malloc_zero_fill( locns[i] ) ) abort( "calloc/free corrupt storage5" ); |
---|
158 | locns[i][0] = '\345'; locns[i][s - 1] = '\345'; // fill first/last |
---|
159 | locns[i][malloc_usable_size( locns[i] ) - 1] = '\345'; // fill ultimate byte |
---|
160 | } // for |
---|
161 | for ( i = 0; i < NoOfMmaps; i += 1 ) { |
---|
162 | size_t s = i + default_mmap_start(); // cross over point |
---|
163 | if ( locns[i][0] != '\345' || locns[i][s - 1] != '\345' || |
---|
164 | locns[i][malloc_usable_size( locns[i] ) - 1] != '\345' ) abort( "calloc/free corrupt storage6" ); |
---|
165 | free( locns[i] ); |
---|
166 | } // for |
---|
167 | |
---|
168 | // check memalign/free (sbrk) |
---|
169 | |
---|
170 | enum { limit = 64 * 1024 }; // check alignments up to here |
---|
171 | |
---|
172 | for ( size_t a = libAlign(); a <= limit; a += a ) { // generate powers of 2 |
---|
173 | //sout | alignments[a] | endl; |
---|
174 | for ( int s = 1; s < NoOfAllocs; s += 1 ) { // allocation of size 0 can return null |
---|
175 | char *area = (char *)memalign( a, s ); |
---|
176 | if ( area == 0 ) abort( "memalign/free out of memory" ); |
---|
177 | //sout | i | " " | area | endl; |
---|
178 | if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment |
---|
179 | abort( "memalign/free bad alignment : memalign(%d,%d) = %p", (int)a, s, area ); |
---|
180 | } // if |
---|
181 | area[0] = '\345'; area[s - 1] = '\345'; // fill first/last byte |
---|
182 | area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte |
---|
183 | free( area ); |
---|
184 | } // for |
---|
185 | } // for |
---|
186 | |
---|
187 | // check memalign/free (mmap) |
---|
188 | |
---|
189 | for ( size_t a = libAlign(); a <= limit; a += a ) { // generate powers of 2 |
---|
190 | //sout | alignments[a] | endl; |
---|
191 | for ( i = 1; i < NoOfMmaps; i += 1 ) { |
---|
192 | size_t s = i + default_mmap_start(); // cross over point |
---|
193 | char *area = (char *)memalign( a, s ); |
---|
194 | if ( area == 0 ) abort( "memalign/free out of memory" ); |
---|
195 | //sout | i | " " | area | endl; |
---|
196 | if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment |
---|
197 | abort( "memalign/free bad alignment : memalign(%d,%d) = %p", (int)a, (int)s, area ); |
---|
198 | } // if |
---|
199 | area[0] = '\345'; area[s - 1] = '\345'; // fill first/last byte |
---|
200 | area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte |
---|
201 | free( area ); |
---|
202 | } // for |
---|
203 | } // for |
---|
204 | |
---|
205 | // check calloc/realloc/free (sbrk) |
---|
206 | |
---|
207 | for ( i = 1; i < 10000; i += 12 ) { |
---|
208 | // initial N byte allocation |
---|
209 | char *area = (char *)calloc( 5, i ); |
---|
210 | if ( area == 0 ) abort( "calloc/realloc/free out of memory" ); |
---|
211 | if ( area[0] != '\0' || area[i - 1] != '\0' || |
---|
212 | area[malloc_usable_size( area ) - 1] != '\0' || |
---|
213 | ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage1" ); |
---|
214 | |
---|
215 | // Do not start this loop index at 0 because realloc of 0 bytes frees the storage. |
---|
216 | for ( int s = i; s < 256 * 1024; s += 26 ) { // start at initial memory request |
---|
217 | area = (char *)realloc( area, s ); // attempt to reuse storage |
---|
218 | if ( area == 0 ) abort( "calloc/realloc/free out of memory" ); |
---|
219 | if ( area[0] != '\0' || area[s - 1] != '\0' || |
---|
220 | area[malloc_usable_size( area ) - 1] != '\0' || |
---|
221 | ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage2" ); |
---|
222 | } // for |
---|
223 | free( area ); |
---|
224 | } // for |
---|
225 | |
---|
226 | // check calloc/realloc/free (mmap) |
---|
227 | |
---|
228 | for ( i = 1; i < 1000; i += 12 ) { |
---|
229 | // initial N byte allocation |
---|
230 | size_t s = i + default_mmap_start(); // cross over point |
---|
231 | char *area = (char *)calloc( 1, s ); |
---|
232 | if ( area == 0 ) abort( "calloc/realloc/free out of memory" ); |
---|
233 | if ( area[0] != '\0' || area[s - 1] != '\0' || |
---|
234 | area[malloc_usable_size( area ) - 1] != '\0' || |
---|
235 | ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage1" ); |
---|
236 | |
---|
237 | // Do not start this loop index at 0 because realloc of 0 bytes frees the storage. |
---|
238 | for ( int r = i; r < 256 * 1024; r += 26 ) { // start at initial memory request |
---|
239 | area = (char *)realloc( area, r ); // attempt to reuse storage |
---|
240 | if ( area == 0 ) abort( "calloc/realloc/free out of memory" ); |
---|
241 | if ( area[0] != '\0' || area[r - 1] != '\0' || |
---|
242 | area[malloc_usable_size( area ) - 1] != '\0' || |
---|
243 | ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage2" ); |
---|
244 | } // for |
---|
245 | free( area ); |
---|
246 | } // for |
---|
247 | |
---|
248 | // check memalign/realloc/free |
---|
249 | |
---|
250 | size_t amount = 2; |
---|
251 | for ( size_t a = libAlign(); a <= limit; a += a ) { // generate powers of 2 |
---|
252 | // initial N byte allocation |
---|
253 | char *area = (char *)memalign( a, amount ); // aligned N-byte allocation |
---|
254 | if ( area == 0 ) abort( "memalign/realloc/free out of memory" ); // no storage ? |
---|
255 | //sout | alignments[a] | " " | area | endl; |
---|
256 | if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment |
---|
257 | abort( "memalign/realloc/free bad alignment : memalign(%d,%d) = %p", (int)a, (int)amount, area ); |
---|
258 | } // if |
---|
259 | area[0] = '\345'; area[amount - 2] = '\345'; // fill first/penultimate byte |
---|
260 | |
---|
261 | // Do not start this loop index at 0 because realloc of 0 bytes frees the storage. |
---|
262 | for ( int s = amount; s < 256 * 1024; s += 1 ) { // start at initial memory request |
---|
263 | if ( area[0] != '\345' || area[s - 2] != '\345' ) abort( "memalign/realloc/free corrupt storage" ); |
---|
264 | area = (char *)realloc( area, s ); // attempt to reuse storage |
---|
265 | if ( area == 0 ) abort( "memalign/realloc/free out of memory" ); // no storage ? |
---|
266 | //sout | i | " " | area | endl; |
---|
267 | if ( (size_t)area % a != 0 ) { // check for initial alignment |
---|
268 | abort( "memalign/realloc/free bad alignment %p", area ); |
---|
269 | } // if |
---|
270 | area[s - 1] = '\345'; // fill last byte |
---|
271 | } // for |
---|
272 | free( area ); |
---|
273 | } // for |
---|
274 | |
---|
275 | // check cmemalign/free |
---|
276 | |
---|
277 | for ( size_t a = libAlign(); a <= limit; a += a ) { // generate powers of 2 |
---|
278 | //sout | alignments[a] | endl; |
---|
279 | for ( int s = 1; s < limit; s += 1 ) { // allocation of size 0 can return null |
---|
280 | char *area = (char *)cmemalign( a, 1, s ); |
---|
281 | if ( area == 0 ) abort( "cmemalign/free out of memory" ); |
---|
282 | //sout | i | " " | area | endl; |
---|
283 | if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment |
---|
284 | abort( "cmemalign/free bad alignment : cmemalign(%d,%d) = %p", (int)a, s, area ); |
---|
285 | } // if |
---|
286 | if ( area[0] != '\0' || area[s - 1] != '\0' || |
---|
287 | area[malloc_usable_size( area ) - 1] != '\0' || |
---|
288 | ! malloc_zero_fill( area ) ) abort( "cmemalign/free corrupt storage" ); |
---|
289 | area[0] = '\345'; area[s - 1] = '\345'; // fill first/last byte |
---|
290 | free( area ); |
---|
291 | } // for |
---|
292 | } // for |
---|
293 | |
---|
294 | // check cmemalign/realloc/free |
---|
295 | |
---|
296 | amount = 2; |
---|
297 | for ( size_t a = libAlign() + libAlign(); a <= limit; a += a ) { // generate powers of 2 |
---|
298 | // initial N byte allocation |
---|
299 | char *area = (char *)cmemalign( a, 1, amount ); // aligned N-byte allocation |
---|
300 | if ( area == 0 ) abort( "cmemalign/realloc/free out of memory" ); // no storage ? |
---|
301 | //sout | alignments[a] | " " | area | endl; |
---|
302 | if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment |
---|
303 | abort( "cmemalign/realloc/free bad alignment : cmemalign(%d,%d) = %p", (int)a, (int)amount, area ); |
---|
304 | } // if |
---|
305 | if ( area[0] != '\0' || area[amount - 1] != '\0' || |
---|
306 | area[malloc_usable_size( area ) - 1] != '\0' || |
---|
307 | ! malloc_zero_fill( area ) ) abort( "cmemalign/realloc/free corrupt storage1" ); |
---|
308 | area[0] = '\345'; area[amount - 2] = '\345'; // fill first/penultimate byte |
---|
309 | |
---|
310 | // Do not start this loop index at 0 because realloc of 0 bytes frees the storage. |
---|
311 | for ( int s = amount; s < 256 * 1024; s += 1 ) { // start at initial memory request |
---|
312 | if ( area[0] != '\345' || area[s - 2] != '\345' ) abort( "cmemalign/realloc/free corrupt storage2" ); |
---|
313 | area = (char *)realloc( area, s ); // attempt to reuse storage |
---|
314 | if ( area == 0 ) abort( "cmemalign/realloc/free out of memory" ); // no storage ? |
---|
315 | //sout | i | " " | area | endl; |
---|
316 | if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment |
---|
317 | abort( "cmemalign/realloc/free bad alignment %p", area ); |
---|
318 | } // if |
---|
319 | if ( area[s - 1] != '\0' || area[s - 1] != '\0' || |
---|
320 | area[malloc_usable_size( area ) - 1] != '\0' || |
---|
321 | ! malloc_zero_fill( area ) ) abort( "cmemalign/realloc/free corrupt storage3" ); |
---|
322 | area[s - 1] = '\345'; // fill last byte |
---|
323 | } // for |
---|
324 | free( area ); |
---|
325 | } // for |
---|
326 | //sout | "worker" | thisTask() | "successful completion" | endl; |
---|
327 | } // Worker main |
---|
328 | |
---|
329 | int main() { |
---|
330 | const unsigned int NoOfWorkers = 4; |
---|
331 | { |
---|
332 | processor processors[NoOfWorkers - 1] __attribute__(( unused )); // more than one processor |
---|
333 | Worker workers[NoOfWorkers] __attribute__(( unused )); |
---|
334 | } |
---|
335 | // checkFreeOn(); |
---|
336 | // malloc_stats(); |
---|
337 | } |
---|
338 | |
---|
339 | // Local Variables: // |
---|
340 | // tab-width: 4 // |
---|
341 | // compile-command: "cfa -nodebug -O2 heap.c" // |
---|
342 | // End: // |
---|