source: tests/heap.cfa@ 0fba0d4

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum
Last change on this file since 0fba0d4 was 5dc5da7, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

set default heap expansion to 10M

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