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