source: tests/heap.cfa@ c8e4b23d

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since c8e4b23d was 2ff42f4, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

fix 32-bit heap-test warning for format codes

  • Property mode set to 100644
File size: 15.5 KB
RevLine 
[dc8511c]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
[2ff42f4]12// Last Modified On : Tue Aug 4 06:36:17 2020
13// Update Count : 56
[dc8511c]14//
15
[73abe95]16#include <thread.hfa>
[80228a7]17#include <kernel.hfa> // processor
18#include <stdlib.hfa> // *allocs
[5d4fa18]19#include <malloc.h> // malloc_*
20
[73abe95]21// #include <time.hfa>
[5d4fa18]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
29#define __U_DEFAULT_MMAP_START__ (512 * 1024 + 1)
30size_t default_mmap_start() __attribute__(( weak )) {
[3e91703d]31 return __U_DEFAULT_MMAP_START__;
[5d4fa18]32} // default_mmap_start
33
34thread Worker {
35}; // Worker
36
37void main( Worker & ) {
[3e91703d]38 enum { NoOfAllocs = 5000, NoOfMmaps = 10 };
39 char * locns[NoOfAllocs];
[58e280f4]40 size_t amount;
41 enum { limit = 64 * 1024 }; // check alignments up to here
[5d4fa18]42
[3e91703d]43 // check alloc/free
[5d4fa18]44
[3e91703d]45 for ( j; 40 ) {
[80228a7]46 for ( i; NoOfAllocs ) {
[5d4fa18]47 locns[i] = alloc( i );
[200fcb3]48 //sout | (void *)locns[i];
[80228a7]49 for ( k; i ) locns[i][k] = '\345';
[5d4fa18]50 } // for
[200fcb3]51 //sout | (char *)sbrk(0) - start | " bytes";
[5d4fa18]52
[80228a7]53 for ( i; NoOfAllocs ) {
[200fcb3]54 //sout | (void *)locns[i];
[80228a7]55 for ( k; i ) if ( locns[i][k] != '\345' ) abort( "new/delete corrupt storage1" );
[5d4fa18]56 free( locns[i] );
57 } // for
[200fcb3]58 //sout | (char *)sbrk(0) - start | " bytes";
[5d4fa18]59
[80228a7]60 for ( i; NoOfAllocs ) {
[5d4fa18]61 locns[i] = alloc( i );
[200fcb3]62 //sout | (void *)locns[i];
[80228a7]63 for ( k; i ) locns[i][k] = '\345';
[5d4fa18]64 } // for
[80228a7]65 for ( i; NoOfAllocs - 1 -~= 0 ) {
[200fcb3]66 //sout | (void *)locns[i];
[80228a7]67 for ( k; i ) if ( locns[i][k] != '\345' ) abort( "new/delete corrupt storage2" );
[5d4fa18]68 free( locns[i] );
69 } // for
[3e91703d]70 } // for
[5d4fa18]71
[3e91703d]72 // check malloc/free (sbrk)
[5d4fa18]73
[3e91703d]74 for ( i; NoOfAllocs ) {
[5d4fa18]75 size_t s = (i + 1) * 20;
[80228a7]76 char * area = (char *)malloc( s );
[5d4fa18]77 area[0] = '\345'; area[s - 1] = '\345'; // fill first/last
78 area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte
79 free( area );
[3e91703d]80 } // for
[5d4fa18]81
[3e91703d]82 for ( i; NoOfAllocs ) {
[80228a7]83 size_t s = i + 1; // +1 to make initialization simpler
[5d4fa18]84 locns[i] = (char *)malloc( s );
85 locns[i][0] = '\345'; locns[i][s - 1] = '\345'; // fill first/last
86 locns[i][malloc_usable_size( locns[i] ) - 1] = '\345'; // fill ultimate byte
[3e91703d]87 } // for
88 for ( i; NoOfAllocs ) {
[5d4fa18]89 size_t s = i + 1;
90 if ( locns[i][0] != '\345' || locns[i][s - 1] != '\345' ||
91 locns[i][malloc_usable_size( locns[i] ) - 1] != '\345' ) abort( "malloc/free corrupt storage" );
92 free( locns[i] );
[3e91703d]93 } // for
[5d4fa18]94
[3e91703d]95 // check malloc/free (mmap)
[5d4fa18]96
[3e91703d]97 for ( i; NoOfMmaps ) {
[80228a7]98 size_t s = i + default_mmap_start(); // cross over point
99 char * area = (char *)malloc( s );
[5d4fa18]100 area[0] = '\345'; area[s - 1] = '\345'; // fill first/last
101 area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte
102 free( area );
[3e91703d]103 } // for
[5d4fa18]104
[3e91703d]105 for ( i; NoOfMmaps ) {
[80228a7]106 size_t s = i + default_mmap_start(); // cross over point
[5d4fa18]107 locns[i] = (char *)malloc( s );
108 locns[i][0] = '\345'; locns[i][s - 1] = '\345'; // fill first/last
109 locns[i][malloc_usable_size( locns[i] ) - 1] = '\345'; // fill ultimate byte
[3e91703d]110 } // for
111 for ( i; NoOfMmaps ) {
[80228a7]112 size_t s = i + default_mmap_start(); // cross over point
[5d4fa18]113 if ( locns[i][0] != '\345' || locns[i][s - 1] != '\345' ||
114 locns[i][malloc_usable_size( locns[i] ) - 1] != '\345' ) abort( "malloc/free corrupt storage" );
115 free( locns[i] );
[3e91703d]116 } // for
[5d4fa18]117
[3e91703d]118 // check calloc/free (sbrk)
[5d4fa18]119
[3e91703d]120 for ( i; NoOfAllocs ) {
[5d4fa18]121 size_t s = (i + 1) * 20;
[80228a7]122 char * area = (char *)calloc( 5, s );
[5d4fa18]123 if ( area[0] != '\0' || area[s - 1] != '\0' ||
[25cbd99]124 area[malloc_size( area ) - 1] != '\0' ||
[5d4fa18]125 ! malloc_zero_fill( area ) ) abort( "calloc/free corrupt storage1" );
126 area[0] = '\345'; area[s - 1] = '\345'; // fill first/last
127 area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte
128 free( area );
[3e91703d]129 } // for
[5d4fa18]130
[3e91703d]131 for ( i; NoOfAllocs ) {
[5d4fa18]132 size_t s = i + 1;
133 locns[i] = (char *)calloc( 5, s );
134 if ( locns[i][0] != '\0' || locns[i][s - 1] != '\0' ||
[25cbd99]135 locns[i][malloc_size( locns[i] ) - 1] != '\0' ||
[5d4fa18]136 ! malloc_zero_fill( locns[i] ) ) abort( "calloc/free corrupt storage2" );
137 locns[i][0] = '\345'; locns[i][s - 1] = '\345'; // fill first/last
138 locns[i][malloc_usable_size( locns[i] ) - 1] = '\345'; // fill ultimate byte
[3e91703d]139 } // for
140 for ( i; NoOfAllocs ) {
[5d4fa18]141 size_t s = i + 1;
142 if ( locns[i][0] != '\345' || locns[i][s - 1] != '\345' ||
143 locns[i][malloc_usable_size( locns[i] ) - 1] != '\345' ) abort( "calloc/free corrupt storage3" );
144 free( locns[i] );
[3e91703d]145 } // for
[5d4fa18]146
[3e91703d]147 // check calloc/free (mmap)
[5d4fa18]148
[3e91703d]149 for ( i; NoOfMmaps ) {
[80228a7]150 size_t s = i + default_mmap_start(); // cross over point
151 char * area = (char *)calloc( 1, s );
[5d4fa18]152 if ( area[0] != '\0' || area[s - 1] != '\0' ) abort( "calloc/free corrupt storage4.1" );
[25cbd99]153 if ( area[malloc_size( area ) - 1] != '\0' ) abort( "calloc/free corrupt storage4.2" );
[5d4fa18]154 if ( ! malloc_zero_fill( area ) ) abort( "calloc/free corrupt storage4.3" );
155 area[0] = '\345'; area[s - 1] = '\345'; // fill first/last
156 area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte
157 free( area );
[3e91703d]158 } // for
[5d4fa18]159
[3e91703d]160 for ( i; NoOfMmaps ) {
[80228a7]161 size_t s = i + default_mmap_start(); // cross over point
[5d4fa18]162 locns[i] = (char *)calloc( 1, s );
163 if ( locns[i][0] != '\0' || locns[i][s - 1] != '\0' ||
[25cbd99]164 locns[i][malloc_size( locns[i] ) - 1] != '\0' ||
[5d4fa18]165 ! malloc_zero_fill( locns[i] ) ) abort( "calloc/free corrupt storage5" );
166 locns[i][0] = '\345'; locns[i][s - 1] = '\345'; // fill first/last
167 locns[i][malloc_usable_size( locns[i] ) - 1] = '\345'; // fill ultimate byte
[3e91703d]168 } // for
169 for ( i; NoOfMmaps ) {
[80228a7]170 size_t s = i + default_mmap_start(); // cross over point
[5d4fa18]171 if ( locns[i][0] != '\345' || locns[i][s - 1] != '\345' ||
172 locns[i][malloc_usable_size( locns[i] ) - 1] != '\345' ) abort( "calloc/free corrupt storage6" );
173 free( locns[i] );
[3e91703d]174 } // for
[5d4fa18]175
[3e91703d]176 // check memalign/free (sbrk)
[5d4fa18]177
[80228a7]178 for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2
[200fcb3]179 //sout | alignments[a];
[80228a7]180 for ( s; 1 ~ NoOfAllocs ) { // allocation of size 0 can return null
181 char * area = (char *)memalign( a, s );
[6a25b8f]182 //sout | i | area;
[5d4fa18]183 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
184 abort( "memalign/free bad alignment : memalign(%d,%d) = %p", (int)a, s, area );
185 } // if
[6a25b8f]186 area[0] = '\345'; area[s - 1] = '\345'; // fill first/last byte
[5d4fa18]187 area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte
188 free( area );
189 } // for
[3e91703d]190 } // for
[5d4fa18]191
[3e91703d]192 // check memalign/free (mmap)
[5d4fa18]193
[80228a7]194 for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2
[200fcb3]195 //sout | alignments[a];
[80228a7]196 for ( i; 1 ~ NoOfMmaps ) {
197 size_t s = i + default_mmap_start(); // cross over point
198 char * area = (char *)memalign( a, s );
[6a25b8f]199 //sout | i | area;
[5d4fa18]200 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
201 abort( "memalign/free bad alignment : memalign(%d,%d) = %p", (int)a, (int)s, area );
202 } // if
203 area[0] = '\345'; area[s - 1] = '\345'; // fill first/last byte
204 area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte
205 free( area );
206 } // for
[3e91703d]207 } // for
[5d4fa18]208
[3e91703d]209 // check calloc/realloc/free (sbrk)
[5d4fa18]210
[3e91703d]211 for ( i; 1 ~ 10_000 ~ 12 ) {
[5d4fa18]212 // initial N byte allocation
[80228a7]213 char * area = (char *)calloc( 5, i );
[5d4fa18]214 if ( area[0] != '\0' || area[i - 1] != '\0' ||
[25cbd99]215 area[malloc_size( area ) - 1] != '\0' ||
[5d4fa18]216 ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage1" );
217
218 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
[80228a7]219 for ( s; i ~ 256 * 1024 ~ 26 ) { // start at initial memory request
[5d4fa18]220 area = (char *)realloc( area, s ); // attempt to reuse storage
221 if ( area[0] != '\0' || area[s - 1] != '\0' ||
[25cbd99]222 area[malloc_size( area ) - 1] != '\0' ||
[5d4fa18]223 ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage2" );
224 } // for
225 free( area );
[3e91703d]226 } // for
[5d4fa18]227
[3e91703d]228 // check calloc/realloc/free (mmap)
[5d4fa18]229
[3e91703d]230 for ( i; 1 ~ 10_000 ~ 12 ) {
[5d4fa18]231 // initial N byte allocation
[80228a7]232 size_t s = i + default_mmap_start(); // cross over point
233 char * area = (char *)calloc( 1, s );
[25cbd99]234// if ( area == 0p ) abort( "calloc/realloc/free out of memory" );
[5d4fa18]235 if ( area[0] != '\0' || area[s - 1] != '\0' ||
[25cbd99]236 area[malloc_size( area ) - 1] != '\0' ||
237 ! malloc_zero_fill( area ) ) //abort( "calloc/realloc/free corrupt storage3" );
238 printf( "C %zd %d %d %d %d\n", s, area[0] != '\0', area[s - 1] != '\0', area[malloc_size( area ) - 1] != '\0', ! malloc_zero_fill( area ) );
[5d4fa18]239
240 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
[80228a7]241 for ( r; i ~ 256 * 1024 ~ 26 ) { // start at initial memory request
242 area = (char *)realloc( area, r ); // attempt to reuse storage
[25cbd99]243// if ( area == 0p ) abort( "calloc/realloc/free out of memory" );
[5d4fa18]244 if ( area[0] != '\0' || area[r - 1] != '\0' ||
[25cbd99]245 area[malloc_size( area ) - 1] != '\0' ||
246 ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage4" );
[5d4fa18]247 } // for
248 free( area );
[3e91703d]249 } // for
[5d4fa18]250
[3e91703d]251 // check memalign/realloc/free
[5d4fa18]252
[58e280f4]253 amount = 2;
[80228a7]254 for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2
[5d4fa18]255 // initial N byte allocation
[80228a7]256 char * area = (char *)memalign( a, amount ); // aligned N-byte allocation
[25cbd99]257// if ( area == 0p ) abort( "memalign/realloc/free out of memory" ); // no storage ?
[6a25b8f]258 //sout | alignments[a] | area;
[5d4fa18]259 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
260 abort( "memalign/realloc/free bad alignment : memalign(%d,%d) = %p", (int)a, (int)amount, area );
261 } // if
262 area[0] = '\345'; area[amount - 2] = '\345'; // fill first/penultimate byte
263
264 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
[80228a7]265 for ( s; amount ~ 256 * 1024 ) { // start at initial memory request
[5d4fa18]266 if ( area[0] != '\345' || area[s - 2] != '\345' ) abort( "memalign/realloc/free corrupt storage" );
267 area = (char *)realloc( area, s ); // attempt to reuse storage
[6a25b8f]268 //sout | i | area;
[5d4fa18]269 if ( (size_t)area % a != 0 ) { // check for initial alignment
270 abort( "memalign/realloc/free bad alignment %p", area );
271 } // if
272 area[s - 1] = '\345'; // fill last byte
273 } // for
274 free( area );
[3e91703d]275 } // for
[5d4fa18]276
[3e91703d]277 // check cmemalign/free
[5d4fa18]278
[80228a7]279 for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2
[200fcb3]280 //sout | alignments[a];
[80228a7]281 for ( s; 1 ~ limit ) { // allocation of size 0 can return null
282 char * area = (char *)cmemalign( a, 1, s );
[6a25b8f]283 //sout | i | area;
[5d4fa18]284 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
285 abort( "cmemalign/free bad alignment : cmemalign(%d,%d) = %p", (int)a, s, area );
286 } // if
287 if ( area[0] != '\0' || area[s - 1] != '\0' ||
[25cbd99]288 area[malloc_size( area ) - 1] != '\0' ||
[5d4fa18]289 ! malloc_zero_fill( area ) ) abort( "cmemalign/free corrupt storage" );
290 area[0] = '\345'; area[s - 1] = '\345'; // fill first/last byte
291 free( area );
292 } // for
[3e91703d]293 } // for
[5d4fa18]294
[3e91703d]295 // check cmemalign/realloc/free
[5d4fa18]296
[3e91703d]297 amount = 2;
[80228a7]298 for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2
[5d4fa18]299 // initial N byte allocation
[80228a7]300 char * area = (char *)cmemalign( a, 1, amount ); // aligned N-byte allocation
[6a25b8f]301 //sout | alignments[a] | area;
[5d4fa18]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' ||
[25cbd99]306 area[malloc_size( area ) - 1] != '\0' ||
[5d4fa18]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.
[80228a7]311 for ( s; amount ~ 256 * 1024 ) { // start at initial memory request
[5d4fa18]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
[6a25b8f]314 //sout | i | area;
[5d4fa18]315 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
316 abort( "cmemalign/realloc/free bad alignment %p", area );
317 } // if
[25cbd99]318 if ( area[0] != '\345' || area[s - 1] != '\0' ||
319 area[malloc_size( area ) - 1] != '\0' ||
[5d4fa18]320 ! malloc_zero_fill( area ) ) abort( "cmemalign/realloc/free corrupt storage3" );
321 area[s - 1] = '\345'; // fill last byte
322 } // for
323 free( area );
[3e91703d]324 } // for
[6a25b8f]325
326 // check memalign/realloc with align/free
327
328 amount = 2;
329 for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2
330 // initial N byte allocation
331 char * area = (char *)memalign( a, amount ); // aligned N-byte allocation
332 //sout | alignments[a] | area | endl;
333 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
334 abort( "memalign/realloc with align/free bad alignment : memalign(%d,%d) = %p", (int)a, (int)amount, area );
335 } // if
336 area[0] = '\345'; area[amount - 2] = '\345'; // fill first/penultimate byte
337
338 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
339 for ( s; amount ~ 256 * 1024 ) { // start at initial memory request
340 if ( area[0] != '\345' || area[s - 2] != '\345' ) abort( "memalign/realloc/free corrupt storage" );
341 area = (char *)realloc( area, a * 2, s ); // attempt to reuse storage
342 //sout | i | area | endl;
343 if ( (size_t)area % a * 2 != 0 ) { // check for initial alignment
344 abort( "memalign/realloc with align/free bad alignment %p", area );
345 } // if
346 area[s - 1] = '\345'; // fill last byte
347 } // for
348 free( area );
349 } // for
350
351 // check cmemalign/realloc with align/free
352
353 amount = 2;
354 for ( size_t a = libAlign() + libAlign(); a <= limit; a += a ) { // generate powers of 2
355 // initial N byte allocation
[2ff42f4]356 char * area = (char *)cmemalign( a, 1, amount ); // aligned N-byte allocation
[6a25b8f]357 //sout | alignments[a] | area | endl;
358 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
359 abort( "cmemalign/realloc with align/free bad alignment : cmemalign(%d,%d) = %p", (int)a, (int)amount, area );
360 } // if
361 if ( area[0] != '\0' || area[amount - 1] != '\0' ||
[25cbd99]362 area[malloc_size( area ) - 1] != '\0' ||
[6a25b8f]363 ! malloc_zero_fill( area ) ) abort( "cmemalign/realloc with align/free corrupt storage1" );
364 area[0] = '\345'; area[amount - 2] = '\345'; // fill first/penultimate byte
365
366 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
367 for ( int s = amount; s < 256 * 1024; s += 1 ) { // start at initial memory request
368 if ( area[0] != '\345' || area[s - 2] != '\345' ) abort( "cmemalign/realloc with align/free corrupt storage2" );
369 area = (char *)realloc( area, a * 2, s ); // attempt to reuse storage
370 //sout | i | area | endl;
371 if ( (size_t)area % a * 2 != 0 || malloc_alignment( area ) != a * 2 ) { // check for initial alignment
[2ff42f4]372 abort( "cmemalign/realloc with align/free bad alignment %p %zd %zd", area, malloc_alignment( area ), a * 2 );
[6a25b8f]373 } // if
374 if ( area[s - 1] != '\0' || area[s - 1] != '\0' ||
[25cbd99]375 area[malloc_size( area ) - 1] != '\0' ||
[6a25b8f]376 ! malloc_zero_fill( area ) ) abort( "cmemalign/realloc/free corrupt storage3" );
377 area[s - 1] = '\345'; // fill last byte
378 } // for
379 free( area );
380 } // for
[58e280f4]381
[200fcb3]382 //sout | "worker" | thisTask() | "successful completion";
[5d4fa18]383} // Worker main
384
385int main() {
[3e91703d]386 const unsigned int NoOfWorkers = 4;
387 {
[5d4fa18]388 processor processors[NoOfWorkers - 1] __attribute__(( unused )); // more than one processor
389 Worker workers[NoOfWorkers] __attribute__(( unused ));
[3e91703d]390 }
[5d4fa18]391 // checkFreeOn();
[3e91703d]392 // malloc_stats();
[5d4fa18]393}
394
395// Local Variables: //
396// tab-width: 4 //
[dc8511c]397// compile-command: "cfa -nodebug -O2 heap.cfa" //
[5d4fa18]398// End: //
Note: See TracBrowser for help on using the repository browser.