| [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
 | 
|---|
| [66812dd] | 12 | // Last Modified On : Fri Sep 25 15:21:52 2020
 | 
|---|
 | 13 | // Update Count     : 73
 | 
|---|
| [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)
 | 
|---|
 | 30 | size_t default_mmap_start() __attribute__(( weak )) {
 | 
|---|
| [3e91703d] | 31 |         return __U_DEFAULT_MMAP_START__;
 | 
|---|
| [5d4fa18] | 32 | } // default_mmap_start
 | 
|---|
 | 33 | 
 | 
|---|
 | 34 | thread Worker {
 | 
|---|
 | 35 | }; // Worker
 | 
|---|
 | 36 | 
 | 
|---|
 | 37 | void 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 | 
 | 
|---|
| [943a079] | 209 |         // check malloc/resize/free (sbrk)
 | 
|---|
 | 210 | 
 | 
|---|
 | 211 |         for ( i; 2 ~ NoOfAllocs ~ 12 ) {
 | 
|---|
 | 212 |                 // initial N byte allocation
 | 
|---|
 | 213 |                 char * area = (char *)malloc( i );
 | 
|---|
 | 214 |                 area[0] = '\345'; area[i - 1] = '\345';                 // fill first/penultimate byte
 | 
|---|
 | 215 | 
 | 
|---|
 | 216 |                 // Do not start this loop index at 0 because resize of 0 bytes frees the storage.
 | 
|---|
 | 217 |                 int prev = i;
 | 
|---|
 | 218 |                 for ( s; i ~ 256 * 1024 ~ 26 ) {                                // start at initial memory request
 | 
|---|
 | 219 |                         if ( area[0] != '\345' || area[prev - 1] != '\345' ) abort( "malloc/resize/free corrupt storage" );
 | 
|---|
 | 220 |                         area = (char *)resize( area, s );                       // attempt to reuse storage
 | 
|---|
 | 221 |                         area[0] = area[s - 1] = '\345';                         // fill last byte
 | 
|---|
 | 222 |                         prev = s;
 | 
|---|
 | 223 |                 } // for
 | 
|---|
 | 224 |                 free( area );
 | 
|---|
 | 225 |         } // for
 | 
|---|
 | 226 | 
 | 
|---|
 | 227 |         // check malloc/resize/free (mmap)
 | 
|---|
 | 228 | 
 | 
|---|
| [dfc13bb] | 229 |         for ( i; 2 ~ NoOfAllocs ~ 12 ) {
 | 
|---|
| [943a079] | 230 |                 // initial N byte allocation
 | 
|---|
 | 231 |                 size_t s = i + default_mmap_start();                    // cross over point
 | 
|---|
 | 232 |                 char * area = (char *)malloc( s );
 | 
|---|
 | 233 |                 area[0] = '\345'; area[s - 1] = '\345';                 // fill first/penultimate byte
 | 
|---|
 | 234 | 
 | 
|---|
 | 235 |                 // Do not start this loop index at 0 because resize of 0 bytes frees the storage.
 | 
|---|
 | 236 |                 int prev = s;
 | 
|---|
 | 237 |                 for ( r; s ~ 256 * 1024 ~ 26 ) {                                // start at initial memory request
 | 
|---|
 | 238 |                         if ( area[0] != '\345' || area[prev - 1] != '\345' ) abort( "malloc/resize/free corrupt storage" );
 | 
|---|
 | 239 |                         area = (char *)resize( area, s );                       // attempt to reuse storage
 | 
|---|
 | 240 |                         area[0] = area[r - 1] = '\345';                         // fill last byte
 | 
|---|
 | 241 |                         prev = r;
 | 
|---|
 | 242 |                 } // for
 | 
|---|
 | 243 |                 free( area );
 | 
|---|
 | 244 |         } // for
 | 
|---|
 | 245 | 
 | 
|---|
 | 246 |         // check malloc/realloc/free (sbrk)
 | 
|---|
 | 247 | 
 | 
|---|
 | 248 |         for ( i; 2 ~ NoOfAllocs ~ 12 ) {
 | 
|---|
 | 249 |                 // initial N byte allocation
 | 
|---|
 | 250 |                 char * area = (char *)malloc( i );
 | 
|---|
 | 251 |                 area[0] = '\345'; area[i - 1] = '\345';                 // fill first/penultimate byte
 | 
|---|
 | 252 | 
 | 
|---|
 | 253 |                 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
 | 
|---|
 | 254 |                 int prev = i;
 | 
|---|
 | 255 |                 for ( s; i ~ 256 * 1024 ~ 26 ) {                                // start at initial memory request
 | 
|---|
 | 256 |                         if ( area[0] != '\345' || area[prev - 1] != '\345' ) abort( "malloc/realloc/free corrupt storage" );
 | 
|---|
 | 257 |                         area = (char *)realloc( area, s );                      // attempt to reuse storage
 | 
|---|
 | 258 |                         area[s - 1] = '\345';                                           // fill last byte
 | 
|---|
 | 259 |                         prev = s;
 | 
|---|
 | 260 |                 } // for
 | 
|---|
 | 261 |                 free( area );
 | 
|---|
 | 262 |         } // for
 | 
|---|
 | 263 | 
 | 
|---|
 | 264 |         // check malloc/realloc/free (mmap)
 | 
|---|
 | 265 | 
 | 
|---|
| [dfc13bb] | 266 |         for ( i; 2 ~ NoOfAllocs ~ 12 ) {
 | 
|---|
| [943a079] | 267 |                 // initial N byte allocation
 | 
|---|
 | 268 |                 size_t s = i + default_mmap_start();                    // cross over point
 | 
|---|
 | 269 |                 char * area = (char *)malloc( s );
 | 
|---|
 | 270 |                 area[0] = '\345'; area[s - 1] = '\345';                 // fill first/penultimate byte
 | 
|---|
 | 271 | 
 | 
|---|
 | 272 |                 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
 | 
|---|
 | 273 |                 int prev = s;
 | 
|---|
 | 274 |                 for ( r; s ~ 256 * 1024 ~ 26 ) {                                // start at initial memory request
 | 
|---|
 | 275 |                         if ( area[0] != '\345' || area[prev - 1] != '\345' ) abort( "malloc/realloc/free corrupt storage" );
 | 
|---|
 | 276 |                         area = (char *)realloc( area, s );                      // attempt to reuse storage
 | 
|---|
 | 277 |                         area[r - 1] = '\345';                                           // fill last byte
 | 
|---|
 | 278 |                         prev = r;
 | 
|---|
 | 279 |                 } // for
 | 
|---|
 | 280 |                 free( area );
 | 
|---|
 | 281 |         } // for
 | 
|---|
 | 282 | 
 | 
|---|
| [3e91703d] | 283 |         // check calloc/realloc/free (sbrk)
 | 
|---|
| [5d4fa18] | 284 | 
 | 
|---|
| [3e91703d] | 285 |         for ( i; 1 ~ 10_000 ~ 12 ) {
 | 
|---|
| [5d4fa18] | 286 |                 // initial N byte allocation
 | 
|---|
| [80228a7] | 287 |                 char * area = (char *)calloc( 5, i );
 | 
|---|
| [5d4fa18] | 288 |                 if ( area[0] != '\0' || area[i - 1] != '\0' ||
 | 
|---|
| [25cbd99] | 289 |                          area[malloc_size( area ) - 1] != '\0' ||
 | 
|---|
| [5d4fa18] | 290 |                          ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage1" );
 | 
|---|
 | 291 | 
 | 
|---|
 | 292 |                 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
 | 
|---|
| [80228a7] | 293 |                 for ( s; i ~ 256 * 1024 ~ 26 ) {                                // start at initial memory request
 | 
|---|
| [5d4fa18] | 294 |                         area = (char *)realloc( area, s );                      // attempt to reuse storage
 | 
|---|
 | 295 |                         if ( area[0] != '\0' || area[s - 1] != '\0' ||
 | 
|---|
| [25cbd99] | 296 |                                  area[malloc_size( area ) - 1] != '\0' ||
 | 
|---|
| [5d4fa18] | 297 |                                  ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage2" );
 | 
|---|
 | 298 |                 } // for
 | 
|---|
 | 299 |                 free( area );
 | 
|---|
| [3e91703d] | 300 |         } // for
 | 
|---|
| [5d4fa18] | 301 | 
 | 
|---|
| [3e91703d] | 302 |         // check calloc/realloc/free (mmap)
 | 
|---|
| [5d4fa18] | 303 | 
 | 
|---|
| [3e91703d] | 304 |         for ( i; 1 ~ 10_000 ~ 12 ) {
 | 
|---|
| [5d4fa18] | 305 |                 // initial N byte allocation
 | 
|---|
| [80228a7] | 306 |                 size_t s = i + default_mmap_start();                    // cross over point
 | 
|---|
 | 307 |                 char * area = (char *)calloc( 1, s );
 | 
|---|
| [5d4fa18] | 308 |                 if ( area[0] != '\0' || area[s - 1] != '\0' ||
 | 
|---|
| [25cbd99] | 309 |                          area[malloc_size( area ) - 1] != '\0' ||
 | 
|---|
| [505450a] | 310 |                          ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage3" );
 | 
|---|
| [5d4fa18] | 311 | 
 | 
|---|
 | 312 |                 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
 | 
|---|
| [80228a7] | 313 |                 for ( r; i ~ 256 * 1024 ~ 26 ) {                                // start at initial memory request
 | 
|---|
 | 314 |                         area = (char *)realloc( area, r );                      // attempt to reuse storage
 | 
|---|
| [5d4fa18] | 315 |                         if ( area[0] != '\0' || area[r - 1] != '\0' ||
 | 
|---|
| [25cbd99] | 316 |                                  area[malloc_size( area ) - 1] != '\0' ||
 | 
|---|
 | 317 |                                  ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage4" );
 | 
|---|
| [5d4fa18] | 318 |                 } // for
 | 
|---|
 | 319 |                 free( area );
 | 
|---|
| [3e91703d] | 320 |         } // for
 | 
|---|
| [5d4fa18] | 321 | 
 | 
|---|
| [3e91703d] | 322 |         // check memalign/realloc/free
 | 
|---|
| [5d4fa18] | 323 | 
 | 
|---|
| [58e280f4] | 324 |         amount = 2;
 | 
|---|
| [80228a7] | 325 |         for ( a; libAlign() ~= limit ~ a ) {                            // generate powers of 2
 | 
|---|
| [5d4fa18] | 326 |                 // initial N byte allocation
 | 
|---|
| [80228a7] | 327 |                 char * area = (char *)memalign( a, amount );    // aligned N-byte allocation
 | 
|---|
| [6a25b8f] | 328 |                 //sout | alignments[a] | area;
 | 
|---|
| [5d4fa18] | 329 |                 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
 | 
|---|
 | 330 |                         abort( "memalign/realloc/free bad alignment : memalign(%d,%d) = %p", (int)a, (int)amount, area );
 | 
|---|
 | 331 |                 } // if
 | 
|---|
 | 332 |                 area[0] = '\345'; area[amount - 2] = '\345';    // fill first/penultimate byte
 | 
|---|
 | 333 | 
 | 
|---|
 | 334 |                 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
 | 
|---|
| [80228a7] | 335 |                 for ( s; amount ~ 256 * 1024 ) {                                // start at initial memory request
 | 
|---|
| [5d4fa18] | 336 |                         if ( area[0] != '\345' || area[s - 2] != '\345' ) abort( "memalign/realloc/free corrupt storage" );
 | 
|---|
 | 337 |                         area = (char *)realloc( area, s );                      // attempt to reuse storage
 | 
|---|
| [6a25b8f] | 338 |                         //sout | i | area;
 | 
|---|
| [5d4fa18] | 339 |                         if ( (size_t)area % a != 0 ) {                          // check for initial alignment
 | 
|---|
 | 340 |                                 abort( "memalign/realloc/free bad alignment %p", area );
 | 
|---|
 | 341 |                         } // if
 | 
|---|
 | 342 |                         area[s - 1] = '\345';                                           // fill last byte
 | 
|---|
 | 343 |                 } // for
 | 
|---|
 | 344 |                 free( area );
 | 
|---|
| [3e91703d] | 345 |         } // for
 | 
|---|
| [5d4fa18] | 346 | 
 | 
|---|
| [3e91703d] | 347 |         // check cmemalign/free
 | 
|---|
| [5d4fa18] | 348 | 
 | 
|---|
| [80228a7] | 349 |         for ( a; libAlign() ~= limit ~ a ) {                            // generate powers of 2
 | 
|---|
| [200fcb3] | 350 |                 //sout | alignments[a];
 | 
|---|
| [80228a7] | 351 |                 for ( s; 1 ~ limit ) {                                                  // allocation of size 0 can return null
 | 
|---|
 | 352 |                         char * area = (char *)cmemalign( a, 1, s );
 | 
|---|
| [6a25b8f] | 353 |                         //sout | i | area;
 | 
|---|
| [5d4fa18] | 354 |                         if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
 | 
|---|
 | 355 |                                 abort( "cmemalign/free bad alignment : cmemalign(%d,%d) = %p", (int)a, s, area );
 | 
|---|
 | 356 |                         } // if
 | 
|---|
 | 357 |                         if ( area[0] != '\0' || area[s - 1] != '\0' ||
 | 
|---|
| [25cbd99] | 358 |                                  area[malloc_size( area ) - 1] != '\0' ||
 | 
|---|
| [5d4fa18] | 359 |                                  ! malloc_zero_fill( area ) ) abort( "cmemalign/free corrupt storage" );
 | 
|---|
 | 360 |                         area[0] = '\345'; area[s - 1] = '\345';         // fill first/last byte
 | 
|---|
 | 361 |                         free( area );
 | 
|---|
 | 362 |                 } // for
 | 
|---|
| [3e91703d] | 363 |         } // for
 | 
|---|
| [5d4fa18] | 364 | 
 | 
|---|
| [3e91703d] | 365 |         // check cmemalign/realloc/free
 | 
|---|
| [5d4fa18] | 366 | 
 | 
|---|
| [3e91703d] | 367 |         amount = 2;
 | 
|---|
| [80228a7] | 368 |         for ( a; libAlign() ~= limit ~ a ) {                            // generate powers of 2
 | 
|---|
| [5d4fa18] | 369 |                 // initial N byte allocation
 | 
|---|
| [80228a7] | 370 |                 char * area = (char *)cmemalign( a, 1, amount ); // aligned N-byte allocation
 | 
|---|
| [6a25b8f] | 371 |                 //sout | alignments[a] | area;
 | 
|---|
| [5d4fa18] | 372 |                 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
 | 
|---|
 | 373 |                         abort( "cmemalign/realloc/free bad alignment : cmemalign(%d,%d) = %p", (int)a, (int)amount, area );
 | 
|---|
 | 374 |                 } // if
 | 
|---|
 | 375 |                 if ( area[0] != '\0' || area[amount - 1] != '\0' ||
 | 
|---|
| [25cbd99] | 376 |                          area[malloc_size( area ) - 1] != '\0' ||
 | 
|---|
| [5d4fa18] | 377 |                          ! malloc_zero_fill( area ) ) abort( "cmemalign/realloc/free corrupt storage1" );
 | 
|---|
 | 378 |                 area[0] = '\345'; area[amount - 2] = '\345';    // fill first/penultimate byte
 | 
|---|
 | 379 | 
 | 
|---|
 | 380 |                 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
 | 
|---|
| [80228a7] | 381 |                 for ( s; amount ~ 256 * 1024 ) {                                // start at initial memory request
 | 
|---|
| [5d4fa18] | 382 |                         if ( area[0] != '\345' || area[s - 2] != '\345' ) abort( "cmemalign/realloc/free corrupt storage2" );
 | 
|---|
 | 383 |                         area = (char *)realloc( area, s );                      // attempt to reuse storage
 | 
|---|
| [6a25b8f] | 384 |                         //sout | i | area;
 | 
|---|
| [5d4fa18] | 385 |                         if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
 | 
|---|
 | 386 |                                 abort( "cmemalign/realloc/free bad alignment %p", area );
 | 
|---|
 | 387 |                         } // if
 | 
|---|
| [25cbd99] | 388 |                         if ( area[0] != '\345' || area[s - 1] != '\0' ||
 | 
|---|
 | 389 |                                  area[malloc_size( area ) - 1] != '\0' ||
 | 
|---|
| [5d4fa18] | 390 |                                  ! malloc_zero_fill( area ) ) abort( "cmemalign/realloc/free corrupt storage3" );
 | 
|---|
 | 391 |                         area[s - 1] = '\345';                                           // fill last byte
 | 
|---|
 | 392 |                 } // for
 | 
|---|
 | 393 |                 free( area );
 | 
|---|
| [3e91703d] | 394 |         } // for
 | 
|---|
| [6a25b8f] | 395 | 
 | 
|---|
| [943a079] | 396 |         // check memalign/resize with align/free
 | 
|---|
 | 397 | 
 | 
|---|
 | 398 |         amount = 2;
 | 
|---|
 | 399 |         for ( a; libAlign() ~= limit ~ a ) {                            // generate powers of 2
 | 
|---|
 | 400 |                 // initial N byte allocation
 | 
|---|
 | 401 |                 char * area = (char *)memalign( a, amount );    // aligned N-byte allocation
 | 
|---|
 | 402 |                 //sout | alignments[a] | area | endl;
 | 
|---|
 | 403 |                 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
 | 
|---|
 | 404 |                         abort( "memalign/resize with align/free bad alignment : memalign(%d,%d) = %p", (int)a, (int)amount, area );
 | 
|---|
 | 405 |                 } // if
 | 
|---|
 | 406 |                 area[0] = '\345'; area[amount - 2] = '\345';    // fill first/penultimate byte
 | 
|---|
 | 407 | 
 | 
|---|
 | 408 |                 // Do not start this loop index at 0 because resize of 0 bytes frees the storage.
 | 
|---|
 | 409 |                 for ( s; amount ~ 256 * 1024 ) {                                // start at initial memory request
 | 
|---|
 | 410 |                         area = (char *)resize( area, a * 2, s );        // attempt to reuse storage
 | 
|---|
 | 411 |                         //sout | i | area | endl;
 | 
|---|
 | 412 |                         if ( (size_t)area % a * 2 != 0 ) {                      // check for initial alignment
 | 
|---|
 | 413 |                                 abort( "memalign/resize with align/free bad alignment %p", area );
 | 
|---|
 | 414 |                         } // if
 | 
|---|
 | 415 |                         area[s - 1] = '\345';                                           // fill last byte
 | 
|---|
 | 416 |                 } // for
 | 
|---|
 | 417 |                 free( area );
 | 
|---|
 | 418 |         } // for
 | 
|---|
 | 419 | 
 | 
|---|
| [6a25b8f] | 420 |         // check memalign/realloc with align/free
 | 
|---|
 | 421 | 
 | 
|---|
 | 422 |         amount = 2;
 | 
|---|
 | 423 |         for ( a; libAlign() ~= limit ~ a ) {                            // generate powers of 2
 | 
|---|
 | 424 |                 // initial N byte allocation
 | 
|---|
 | 425 |                 char * area = (char *)memalign( a, amount );    // aligned N-byte allocation
 | 
|---|
 | 426 |                 //sout | alignments[a] | area | endl;
 | 
|---|
 | 427 |                 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
 | 
|---|
 | 428 |                         abort( "memalign/realloc with align/free bad alignment : memalign(%d,%d) = %p", (int)a, (int)amount, area );
 | 
|---|
 | 429 |                 } // if
 | 
|---|
 | 430 |                 area[0] = '\345'; area[amount - 2] = '\345';    // fill first/penultimate byte
 | 
|---|
 | 431 | 
 | 
|---|
 | 432 |                 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
 | 
|---|
 | 433 |                 for ( s; amount ~ 256 * 1024 ) {                                // start at initial memory request
 | 
|---|
 | 434 |                         if ( area[0] != '\345' || area[s - 2] != '\345' ) abort( "memalign/realloc/free corrupt storage" );
 | 
|---|
 | 435 |                         area = (char *)realloc( area, a * 2, s );       // attempt to reuse storage
 | 
|---|
 | 436 |                         //sout | i | area | endl;
 | 
|---|
 | 437 |                         if ( (size_t)area % a * 2 != 0 ) {                      // check for initial alignment
 | 
|---|
 | 438 |                                 abort( "memalign/realloc with align/free bad alignment %p", area );
 | 
|---|
 | 439 |                         } // if
 | 
|---|
 | 440 |                         area[s - 1] = '\345';                                           // fill last byte
 | 
|---|
 | 441 |                 } // for
 | 
|---|
 | 442 |                 free( area );
 | 
|---|
 | 443 |         } // for
 | 
|---|
 | 444 | 
 | 
|---|
 | 445 |         // check cmemalign/realloc with align/free
 | 
|---|
 | 446 | 
 | 
|---|
 | 447 |         amount = 2;
 | 
|---|
 | 448 |         for ( size_t a = libAlign() + libAlign(); a <= limit; a += a ) { // generate powers of 2
 | 
|---|
 | 449 |                 // initial N byte allocation
 | 
|---|
| [2ff42f4] | 450 |                 char * area = (char *)cmemalign( a, 1, amount ); // aligned N-byte allocation
 | 
|---|
| [6a25b8f] | 451 |                 //sout | alignments[a] | area | endl;
 | 
|---|
 | 452 |                 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
 | 
|---|
 | 453 |                         abort( "cmemalign/realloc with align/free bad alignment : cmemalign(%d,%d) = %p", (int)a, (int)amount, area );
 | 
|---|
 | 454 |                 } // if
 | 
|---|
 | 455 |                 if ( area[0] != '\0' || area[amount - 1] != '\0' ||
 | 
|---|
| [25cbd99] | 456 |                          area[malloc_size( area ) - 1] != '\0' ||
 | 
|---|
| [6a25b8f] | 457 |                          ! malloc_zero_fill( area ) ) abort( "cmemalign/realloc with align/free corrupt storage1" );
 | 
|---|
 | 458 |                 area[0] = '\345'; area[amount - 2] = '\345';    // fill first/penultimate byte
 | 
|---|
 | 459 | 
 | 
|---|
 | 460 |                 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
 | 
|---|
 | 461 |                 for ( int s = amount; s < 256 * 1024; s += 1 ) { // start at initial memory request
 | 
|---|
 | 462 |                         if ( area[0] != '\345' || area[s - 2] != '\345' ) abort( "cmemalign/realloc with align/free corrupt storage2" );
 | 
|---|
 | 463 |                         area = (char *)realloc( area, a * 2, s );       // attempt to reuse storage
 | 
|---|
 | 464 |                         //sout | i | area | endl;
 | 
|---|
 | 465 |                         if ( (size_t)area % a * 2 != 0 || malloc_alignment( area ) != a * 2 ) { // check for initial alignment
 | 
|---|
| [2ff42f4] | 466 |                                 abort( "cmemalign/realloc with align/free bad alignment %p %zd %zd", area, malloc_alignment( area ), a * 2 );
 | 
|---|
| [6a25b8f] | 467 |                         } // if
 | 
|---|
 | 468 |                         if ( area[s - 1] != '\0' || area[s - 1] != '\0' ||
 | 
|---|
| [25cbd99] | 469 |                                  area[malloc_size( area ) - 1] != '\0' ||
 | 
|---|
| [6a25b8f] | 470 |                                  ! malloc_zero_fill( area ) ) abort( "cmemalign/realloc/free corrupt storage3" );
 | 
|---|
 | 471 |                         area[s - 1] = '\345';                                           // fill last byte
 | 
|---|
 | 472 |                 } // for
 | 
|---|
 | 473 |                 free( area );
 | 
|---|
 | 474 |         } // for
 | 
|---|
| [58e280f4] | 475 | 
 | 
|---|
| [200fcb3] | 476 |         //sout | "worker" | thisTask() | "successful completion";
 | 
|---|
| [5d4fa18] | 477 | } // Worker main
 | 
|---|
 | 478 | 
 | 
|---|
 | 479 | int main() {
 | 
|---|
| [3e91703d] | 480 |         const unsigned int NoOfWorkers = 4;
 | 
|---|
 | 481 |         {
 | 
|---|
| [5d4fa18] | 482 |                 processor processors[NoOfWorkers - 1] __attribute__(( unused )); // more than one processor
 | 
|---|
 | 483 |                 Worker workers[NoOfWorkers] __attribute__(( unused ));
 | 
|---|
| [3e91703d] | 484 |         }
 | 
|---|
| [5d4fa18] | 485 |         // checkFreeOn();
 | 
|---|
| [3e91703d] | 486 |         // malloc_stats();
 | 
|---|
| [66812dd] | 487 |         printf( "done\n" );                                                                     // non-empty .expect file
 | 
|---|
| [5d4fa18] | 488 | }
 | 
|---|
 | 489 | 
 | 
|---|
 | 490 | // Local Variables: //
 | 
|---|
 | 491 | // tab-width: 4 //
 | 
|---|
| [dc8511c] | 492 | // compile-command: "cfa -nodebug -O2 heap.cfa" //
 | 
|---|
| [5d4fa18] | 493 | // End: //
 | 
|---|