source: tests/heap.cfa@ 4be0117

Last change on this file since 4be0117 was 4962741, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

fix for-control loop in heap test

  • Property mode set to 100644
File size: 18.8 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// heap.cfa --
8//
9// Author : Peter A. Buhr
10// Created On : Tue Nov 6 17:54:56 2018
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Wed Aug 10 09:57:01 2022
13// Update Count : 86
14//
15
16#include <thread.hfa>
17#include <kernel.hfa> // processor
18#include <stdlib.hfa> // *allocs
19#include <malloc.h> // malloc_*
20
21// #include <time.hfa>
22// #define __CFA_DEFAULT_PREEMPTION__ 1000`us
23// //#define __CFA_DEFAULT_PREEMPTION__ 0
24
25// Duration default_preemption() {
26// return __CFA_DEFAULT_PREEMPTION__;
27// }
28
29thread Worker {
30}; // Worker
31
32void main( Worker & ) {
33 enum { NoOfAllocs = 5000, NoOfMmaps = 10 };
34 char * locns[NoOfAllocs];
35 size_t amount;
36 enum { limit = 64 * 1024 }; // check alignments up to here
37
38 // check alloc/free
39
40 for ( j; 40 ) {
41 for ( i; NoOfAllocs ) {
42 locns[i] = alloc( i );
43 //sout | (void *)locns[i];
44 for ( k; i ) locns[i][k] = '\345';
45 } // for
46 //sout | (char *)sbrk(0) - start | " bytes";
47
48 for ( i; NoOfAllocs ) {
49 //sout | (void *)locns[i];
50 for ( k; i ) if ( locns[i][k] != '\345' ) abort( "new/delete corrupt storage1" );
51 free( locns[i] );
52 } // for
53 //sout | (char *)sbrk(0) - start | " bytes";
54
55 for ( i; NoOfAllocs ) {
56 locns[i] = alloc( i );
57 //sout | (void *)locns[i];
58 for ( k; i ) locns[i][k] = '\345';
59 } // for
60 for ( i; -~= NoOfAllocs - 1 ) {
61 //sout | (void *)locns[i];
62 for ( k; i ) if ( locns[i][k] != '\345' ) abort( "new/delete corrupt storage2" );
63 free( locns[i] );
64 } // for
65 } // for
66
67 // check malloc/free (sbrk)
68
69 for ( i; NoOfAllocs ) {
70 size_t s = (i + 1) * 20;
71 char * area = (char *)malloc( s );
72 area[0] = '\345'; area[s - 1] = '\345'; // fill first/last
73 area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte
74 free( area );
75 } // for
76
77 for ( i; NoOfAllocs ) {
78 size_t s = i + 1; // +1 to make initialization simpler
79 locns[i] = (char *)malloc( s );
80 locns[i][0] = '\345'; locns[i][s - 1] = '\345'; // fill first/last
81 locns[i][malloc_usable_size( locns[i] ) - 1] = '\345'; // fill ultimate byte
82 } // for
83 for ( i; NoOfAllocs ) {
84 size_t s = i + 1;
85 if ( locns[i][0] != '\345' || locns[i][s - 1] != '\345' ||
86 locns[i][malloc_usable_size( locns[i] ) - 1] != '\345' ) abort( "malloc/free corrupt storage" );
87 free( locns[i] );
88 } // for
89
90 // check malloc/free (mmap)
91
92 for ( i; NoOfMmaps ) {
93 size_t s = i + malloc_mmap_start(); // cross over point
94 char * area = (char *)malloc( s );
95 area[0] = '\345'; area[s - 1] = '\345'; // fill first/last
96 area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte
97 free( area );
98 } // for
99
100 for ( i; NoOfMmaps ) {
101 size_t s = i + malloc_mmap_start(); // cross over point
102 locns[i] = (char *)malloc( s );
103 locns[i][0] = '\345'; locns[i][s - 1] = '\345'; // fill first/last
104 locns[i][malloc_usable_size( locns[i] ) - 1] = '\345'; // fill ultimate byte
105 } // for
106 for ( i; NoOfMmaps ) {
107 size_t s = i + malloc_mmap_start(); // cross over point
108 if ( locns[i][0] != '\345' || locns[i][s - 1] != '\345' ||
109 locns[i][malloc_usable_size( locns[i] ) - 1] != '\345' ) abort( "malloc/free corrupt storage" );
110 free( locns[i] );
111 } // for
112
113 // check calloc/free (sbrk)
114
115 for ( i; NoOfAllocs ) {
116 size_t s = (i + 1) * 20;
117 char * area = (char *)calloc( 5, s );
118 if ( area[0] != '\0' || area[s - 1] != '\0' ||
119 area[malloc_size( area ) - 1] != '\0' ||
120 ! malloc_zero_fill( area ) ) abort( "calloc/free corrupt storage1" );
121 area[0] = '\345'; area[s - 1] = '\345'; // fill first/last
122 area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte
123 free( area );
124 } // for
125
126 for ( i; NoOfAllocs ) {
127 size_t s = i + 1;
128 locns[i] = (char *)calloc( 5, s );
129 if ( locns[i][0] != '\0' || locns[i][s - 1] != '\0' ||
130 locns[i][malloc_size( locns[i] ) - 1] != '\0' ||
131 ! malloc_zero_fill( locns[i] ) ) abort( "calloc/free corrupt storage2" );
132 locns[i][0] = '\345'; locns[i][s - 1] = '\345'; // fill first/last
133 locns[i][malloc_usable_size( locns[i] ) - 1] = '\345'; // fill ultimate byte
134 } // for
135 for ( i; NoOfAllocs ) {
136 size_t s = i + 1;
137 if ( locns[i][0] != '\345' || locns[i][s - 1] != '\345' ||
138 locns[i][malloc_usable_size( locns[i] ) - 1] != '\345' ) abort( "calloc/free corrupt storage3" );
139 free( locns[i] );
140 } // for
141
142 // check calloc/free (mmap)
143
144 for ( i; NoOfMmaps ) {
145 size_t s = i + malloc_mmap_start(); // cross over point
146 char * area = (char *)calloc( 1, s );
147 if ( area[0] != '\0' || area[s - 1] != '\0' ) abort( "calloc/free corrupt storage4.1" );
148 if ( area[malloc_size( area ) - 1] != '\0' ) abort( "calloc/free corrupt storage4.2" );
149 if ( ! malloc_zero_fill( area ) ) abort( "calloc/free corrupt storage4.3" );
150 area[0] = '\345'; area[s - 1] = '\345'; // fill first/last
151 area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte
152 free( area );
153 } // for
154
155 for ( i; NoOfMmaps ) {
156 size_t s = i + malloc_mmap_start(); // cross over point
157 locns[i] = (char *)calloc( 1, s );
158 if ( locns[i][0] != '\0' || locns[i][s - 1] != '\0' ||
159 locns[i][malloc_size( locns[i] ) - 1] != '\0' ||
160 ! malloc_zero_fill( locns[i] ) ) abort( "calloc/free corrupt storage5" );
161 locns[i][0] = '\345'; locns[i][s - 1] = '\345'; // fill first/last
162 locns[i][malloc_usable_size( locns[i] ) - 1] = '\345'; // fill ultimate byte
163 } // for
164 for ( i; NoOfMmaps ) {
165 size_t s = i + malloc_mmap_start(); // cross over point
166 if ( locns[i][0] != '\345' || locns[i][s - 1] != '\345' ||
167 locns[i][malloc_usable_size( locns[i] ) - 1] != '\345' ) abort( "calloc/free corrupt storage6" );
168 free( locns[i] );
169 } // for
170
171 // check memalign/free (sbrk)
172
173 for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2
174 //sout | alignments[a];
175 for ( s; 1 ~ NoOfAllocs ) { // allocation of size 0 can return null
176 char * area = (char *)memalign( a, s );
177 //sout | i | area;
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 ( a; libAlign() ~= limit ~ a ) { // generate powers of 2
190 //sout | alignments[a];
191 for ( i; 1 ~ NoOfMmaps ) {
192 size_t s = i + malloc_mmap_start(); // cross over point
193 char * area = (char *)memalign( a, s );
194 //sout | i | area;
195 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
196 abort( "memalign/free bad alignment : memalign(%d,%d) = %p", (int)a, (int)s, area );
197 } // if
198 area[0] = '\345'; area[s - 1] = '\345'; // fill first/last byte
199 area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte
200 free( area );
201 } // for
202 } // for
203
204 // check malloc/resize/free (sbrk)
205
206 for ( i; 2 ~ NoOfAllocs ~ 12 ) {
207 // initial N byte allocation
208 char * area = (char *)malloc( i );
209 area[0] = '\345'; area[i - 1] = '\345'; // fill first/penultimate byte
210
211 // Do not start this loop index at 0 because resize of 0 bytes frees the storage.
212 int prev = i;
213 for ( s; i ~ 256 * 1024 ~ 26 ) { // start at initial memory request
214 if ( area[0] != '\345' || area[prev - 1] != '\345' ) abort( "malloc/resize/free corrupt storage" );
215 area = (char *)resize( area, s ); // attempt to reuse storage
216 area[0] = area[s - 1] = '\345'; // fill last byte
217 prev = s;
218 } // for
219 free( area );
220 } // for
221
222 // check malloc/resize/free (mmap)
223
224 for ( i; 2 ~ NoOfAllocs ~ 12 ) {
225 // initial N byte allocation
226 size_t s = i + malloc_mmap_start(); // cross over point
227 char * area = (char *)malloc( s );
228 area[0] = '\345'; area[s - 1] = '\345'; // fill first/penultimate byte
229
230 // Do not start this loop index at 0 because resize of 0 bytes frees the storage.
231 int prev = s;
232 for ( r; s ~ 256 * 1024 ~ 26 ) { // start at initial memory request
233 if ( area[0] != '\345' || area[prev - 1] != '\345' ) abort( "malloc/resize/free corrupt storage" );
234 area = (char *)resize( area, s ); // attempt to reuse storage
235 area[0] = area[r - 1] = '\345'; // fill last byte
236 prev = r;
237 } // for
238 free( area );
239 } // for
240
241 // check malloc/realloc/free (sbrk)
242
243 for ( i; 2 ~ NoOfAllocs ~ 12 ) {
244 // initial N byte allocation
245 char * area = (char *)malloc( i );
246 area[0] = '\345'; area[i - 1] = '\345'; // fill first/penultimate byte
247
248 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
249 int prev = i;
250 for ( s; i ~ 256 * 1024 ~ 26 ) { // start at initial memory request
251 if ( area[0] != '\345' || area[prev - 1] != '\345' ) abort( "malloc/realloc/free corrupt storage" );
252 area = (char *)realloc( area, s ); // attempt to reuse storage
253 area[s - 1] = '\345'; // fill last byte
254 prev = s;
255 } // for
256 free( area );
257 } // for
258
259 // check malloc/realloc/free (mmap)
260
261 for ( i; 2 ~ NoOfAllocs ~ 12 ) {
262 // initial N byte allocation
263 size_t s = i + malloc_mmap_start(); // cross over point
264 char * area = (char *)malloc( s );
265 area[0] = '\345'; area[s - 1] = '\345'; // fill first/penultimate byte
266
267 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
268 int prev = s;
269 for ( r; s ~ 256 * 1024 ~ 26 ) { // start at initial memory request
270 if ( area[0] != '\345' || area[prev - 1] != '\345' ) abort( "malloc/realloc/free corrupt storage" );
271 area = (char *)realloc( area, s ); // attempt to reuse storage
272 area[r - 1] = '\345'; // fill last byte
273 prev = r;
274 } // for
275 free( area );
276 } // for
277
278 // check calloc/realloc/free (sbrk)
279
280 for ( i; 1 ~ 10_000 ~ 12 ) {
281 // initial N byte allocation
282 char * area = (char *)calloc( 5, i );
283 if ( area[0] != '\0' || area[i - 1] != '\0' ||
284 area[malloc_size( area ) - 1] != '\0' ||
285 ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage1" );
286
287 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
288 for ( s; i ~ 256 * 1024 ~ 26 ) { // start at initial memory request
289 area = (char *)realloc( area, s ); // attempt to reuse storage
290 if ( area[0] != '\0' || area[s - 1] != '\0' ||
291 area[malloc_size( area ) - 1] != '\0' ||
292 ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage2" );
293 } // for
294 free( area );
295 } // for
296
297 // check calloc/realloc/free (mmap)
298
299 for ( i; 1 ~ 10_000 ~ 12 ) {
300 // initial N byte allocation
301 size_t s = i + malloc_mmap_start(); // cross over point
302 char * area = (char *)calloc( 1, s );
303 if ( area[0] != '\0' || area[s - 1] != '\0' ||
304 area[malloc_size( area ) - 1] != '\0' ||
305 ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage3" );
306
307 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
308 for ( r; i ~ 256 * 1024 ~ 26 ) { // start at initial memory request
309 area = (char *)realloc( area, r ); // attempt to reuse storage
310 if ( area[0] != '\0' || area[r - 1] != '\0' ||
311 area[malloc_size( area ) - 1] != '\0' ||
312 ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage4" );
313 } // for
314 free( area );
315 } // for
316
317 // check memalign/realloc/free
318
319 amount = 2;
320 for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2
321 // initial N byte allocation
322 char * area = (char *)memalign( a, amount ); // aligned N-byte allocation
323 //sout | alignments[a] | area;
324 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
325 abort( "memalign/realloc/free bad alignment : memalign(%d,%d) = %p", (int)a, (int)amount, area );
326 } // if
327 area[0] = '\345'; area[amount - 2] = '\345'; // fill first/penultimate byte
328
329 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
330 for ( s; amount ~ 256 * 1024 ) { // start at initial memory request
331 if ( area[0] != '\345' || area[s - 2] != '\345' ) abort( "memalign/realloc/free corrupt storage" );
332 area = (char *)realloc( area, s ); // attempt to reuse storage
333 //sout | i | area;
334 if ( (size_t)area % a != 0 ) { // check for initial alignment
335 abort( "memalign/realloc/free bad alignment %p", area );
336 } // if
337 area[s - 1] = '\345'; // fill last byte
338 } // for
339 free( area );
340 } // for
341
342 // check cmemalign/free
343
344 for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2
345 //sout | alignments[a];
346 for ( s; 1 ~ limit ) { // allocation of size 0 can return null
347 char * area = (char *)cmemalign( a, 1, s );
348 //sout | i | area;
349 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
350 abort( "cmemalign/free bad alignment : cmemalign(%d,%d) = %p", (int)a, s, area );
351 } // if
352 if ( area[0] != '\0' || area[s - 1] != '\0' ||
353 area[malloc_size( area ) - 1] != '\0' ||
354 ! malloc_zero_fill( area ) ) abort( "cmemalign/free corrupt storage" );
355 area[0] = '\345'; area[s - 1] = '\345'; // fill first/last byte
356 free( area );
357 } // for
358 } // for
359
360 // check cmemalign/realloc/free
361
362 amount = 2;
363 for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2
364 // initial N byte allocation
365 char * area = (char *)cmemalign( a, 1, amount ); // aligned N-byte allocation
366 //sout | alignments[a] | area;
367 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
368 abort( "cmemalign/realloc/free bad alignment : cmemalign(%d,%d) = %p", (int)a, (int)amount, area );
369 } // if
370 if ( area[0] != '\0' || area[amount - 1] != '\0' ||
371 area[malloc_size( area ) - 1] != '\0' ||
372 ! malloc_zero_fill( area ) ) abort( "cmemalign/realloc/free corrupt storage1" );
373 area[0] = '\345'; area[amount - 2] = '\345'; // fill first/penultimate byte
374
375 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
376 for ( s; amount ~ 256 * 1024 ) { // start at initial memory request
377 if ( area[0] != '\345' || area[s - 2] != '\345' ) abort( "cmemalign/realloc/free corrupt storage2" );
378 area = (char *)realloc( area, s ); // attempt to reuse storage
379 //sout | i | area;
380 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
381 abort( "cmemalign/realloc/free bad alignment %p", area );
382 } // if
383 if ( area[0] != '\345' || area[s - 1] != '\0' ||
384 area[malloc_size( area ) - 1] != '\0' ||
385 ! malloc_zero_fill( area ) ) abort( "cmemalign/realloc/free corrupt storage3" );
386 area[s - 1] = '\345'; // fill last byte
387 } // for
388 free( area );
389 } // for
390
391 // check memalign/resize with align/free
392
393 amount = 2;
394 for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2
395 // initial N byte allocation
396 char * area = (char *)memalign( a, amount ); // aligned N-byte allocation
397 //sout | alignments[a] | area | endl;
398 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
399 abort( "memalign/resize with align/free bad alignment : memalign(%d,%d) = %p", (int)a, (int)amount, area );
400 } // if
401 area[0] = '\345'; area[amount - 2] = '\345'; // fill first/penultimate byte
402
403 // Do not start this loop index at 0 because resize of 0 bytes frees the storage.
404 for ( s; amount ~ 256 * 1024 ) { // start at initial memory request
405 area = (char *)resize( area, a * 2, s ); // attempt to reuse storage
406 //sout | i | area | endl;
407 if ( (size_t)area % a * 2 != 0 ) { // check for initial alignment
408 abort( "memalign/resize with align/free bad alignment %p", area );
409 } // if
410 area[s - 1] = '\345'; // fill last byte
411 } // for
412 free( area );
413 } // for
414
415 // check memalign/realloc with align/free
416
417 amount = 2;
418 for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2
419 // initial N byte allocation
420 char * area = (char *)memalign( a, amount ); // aligned N-byte allocation
421 //sout | alignments[a] | area | endl;
422 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
423 abort( "memalign/realloc with align/free bad alignment : memalign(%d,%d) = %p", (int)a, (int)amount, area );
424 } // if
425 area[0] = '\345'; area[amount - 2] = '\345'; // fill first/penultimate byte
426
427 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
428 for ( s; amount ~ 256 * 1024 ) { // start at initial memory request
429 if ( area[0] != '\345' || area[s - 2] != '\345' ) abort( "memalign/realloc/free corrupt storage" );
430 area = (char *)realloc( area, a * 2, s ); // attempt to reuse storage
431 //sout | i | area | endl;
432 if ( (size_t)area % a * 2 != 0 ) { // check for initial alignment
433 abort( "memalign/realloc with align/free bad alignment %p", area );
434 } // if
435 area[s - 1] = '\345'; // fill last byte
436 } // for
437 free( area );
438 } // for
439
440 // check cmemalign/realloc with align/free
441
442 amount = 2;
443 for ( size_t a = libAlign() + libAlign(); a <= limit; a += a ) { // generate powers of 2
444 // initial N byte allocation
445 char * area = (char *)cmemalign( a, 1, amount ); // aligned N-byte allocation
446 //sout | alignments[a] | area | endl;
447 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
448 abort( "cmemalign/realloc with align/free bad alignment : cmemalign(%d,%d) = %p", (int)a, (int)amount, area );
449 } // if
450 if ( area[0] != '\0' || area[amount - 1] != '\0' ||
451 area[malloc_size( area ) - 1] != '\0' ||
452 ! malloc_zero_fill( area ) ) abort( "cmemalign/realloc with align/free corrupt storage1" );
453 area[0] = '\345'; area[amount - 2] = '\345'; // fill first/penultimate byte
454
455 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
456 for ( int s = amount; s < 256 * 1024; s += 1 ) { // start at initial memory request
457 if ( area[0] != '\345' || area[s - 2] != '\345' ) abort( "cmemalign/realloc with align/free corrupt storage2" );
458 area = (char *)realloc( area, a * 2, s ); // attempt to reuse storage
459 //sout | i | area | endl;
460 if ( (size_t)area % a * 2 != 0 || malloc_alignment( area ) != a * 2 ) { // check for initial alignment
461 abort( "cmemalign/realloc with align/free bad alignment %p %zd %zd", area, malloc_alignment( area ), a * 2 );
462 } // if
463 if ( area[s - 1] != '\0' || area[s - 1] != '\0' ||
464 area[malloc_size( area ) - 1] != '\0' ||
465 ! malloc_zero_fill( area ) ) abort( "cmemalign/realloc/free corrupt storage3" );
466 area[s - 1] = '\345'; // fill last byte
467 } // for
468 free( area );
469 } // for
470
471 //sout | "worker" | thisTask() | "successful completion";
472} // Worker main
473
474int main() {
475 enum { NoOfWorkers = 4 };
476 {
477 processor processors[NoOfWorkers - 1] __attribute__(( unused )); // more than one processor
478 Worker workers[NoOfWorkers] __attribute__(( unused ));
479 }
480 // checkFreeOn();
481 // malloc_stats();
482 printf( "done\n" ); // non-empty .expect file
483}
484
485// Local Variables: //
486// tab-width: 4 //
487// compile-command: "cfa -nodebug -O2 heap.cfa" //
488// End: //
Note: See TracBrowser for help on using the repository browser.