| [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 | 
|---|
| [58e280f4] | 12 | // Last Modified On : Sun Nov 24 12:34:51 2019 | 
|---|
|  | 13 | // Update Count     : 28 | 
|---|
| [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 ); | 
|---|
| [6a25b8f] | 77 | if ( area == 0p ) abort( "malloc/free out of memory" ); | 
|---|
| [5d4fa18] | 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 ); | 
|---|
| [3e91703d] | 81 | } // for | 
|---|
| [5d4fa18] | 82 |  | 
|---|
| [3e91703d] | 83 | for ( i; NoOfAllocs ) { | 
|---|
| [80228a7] | 84 | size_t s = i + 1;                                                               // +1 to make initialization simpler | 
|---|
| [5d4fa18] | 85 | locns[i] = (char *)malloc( s ); | 
|---|
| [6a25b8f] | 86 | if ( locns[i] == 0p ) abort( "malloc/free out of memory" ); | 
|---|
| [5d4fa18] | 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 | 
|---|
| [3e91703d] | 89 | } // for | 
|---|
|  | 90 | for ( i; NoOfAllocs ) { | 
|---|
| [5d4fa18] | 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] ); | 
|---|
| [3e91703d] | 95 | } // for | 
|---|
| [5d4fa18] | 96 |  | 
|---|
| [3e91703d] | 97 | // check malloc/free (mmap) | 
|---|
| [5d4fa18] | 98 |  | 
|---|
| [3e91703d] | 99 | for ( i; NoOfMmaps ) { | 
|---|
| [80228a7] | 100 | size_t s = i + default_mmap_start();                    // cross over point | 
|---|
|  | 101 | char * area = (char *)malloc( s ); | 
|---|
| [6a25b8f] | 102 | if ( area == 0p ) abort( "malloc/free out of memory" ); | 
|---|
| [5d4fa18] | 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 ); | 
|---|
| [3e91703d] | 106 | } // for | 
|---|
| [5d4fa18] | 107 |  | 
|---|
| [3e91703d] | 108 | for ( i; NoOfMmaps ) { | 
|---|
| [80228a7] | 109 | size_t s = i + default_mmap_start();                    // cross over point | 
|---|
| [5d4fa18] | 110 | locns[i] = (char *)malloc( s ); | 
|---|
| [6a25b8f] | 111 | if ( locns[i] == 0p ) abort( "malloc/free out of memory" ); | 
|---|
| [5d4fa18] | 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 | 
|---|
| [3e91703d] | 114 | } // for | 
|---|
|  | 115 | for ( i; NoOfMmaps ) { | 
|---|
| [80228a7] | 116 | size_t s = i + default_mmap_start();                    // cross over point | 
|---|
| [5d4fa18] | 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] ); | 
|---|
| [3e91703d] | 120 | } // for | 
|---|
| [5d4fa18] | 121 |  | 
|---|
| [3e91703d] | 122 | // check calloc/free (sbrk) | 
|---|
| [5d4fa18] | 123 |  | 
|---|
| [3e91703d] | 124 | for ( i; NoOfAllocs ) { | 
|---|
| [5d4fa18] | 125 | size_t s = (i + 1) * 20; | 
|---|
| [80228a7] | 126 | char * area = (char *)calloc( 5, s ); | 
|---|
| [6a25b8f] | 127 | if ( area == 0p ) abort( "calloc/free out of memory" ); | 
|---|
| [5d4fa18] | 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 ); | 
|---|
| [3e91703d] | 134 | } // for | 
|---|
| [5d4fa18] | 135 |  | 
|---|
| [3e91703d] | 136 | for ( i; NoOfAllocs ) { | 
|---|
| [5d4fa18] | 137 | size_t s = i + 1; | 
|---|
|  | 138 | locns[i] = (char *)calloc( 5, s ); | 
|---|
| [6a25b8f] | 139 | if ( locns[i] == 0p ) abort( "calloc/free out of memory" ); | 
|---|
| [5d4fa18] | 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 | 
|---|
| [3e91703d] | 145 | } // for | 
|---|
|  | 146 | for ( i; NoOfAllocs ) { | 
|---|
| [5d4fa18] | 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] ); | 
|---|
| [3e91703d] | 151 | } // for | 
|---|
| [5d4fa18] | 152 |  | 
|---|
| [3e91703d] | 153 | // check calloc/free (mmap) | 
|---|
| [5d4fa18] | 154 |  | 
|---|
| [3e91703d] | 155 | for ( i; NoOfMmaps ) { | 
|---|
| [80228a7] | 156 | size_t s = i + default_mmap_start();                    // cross over point | 
|---|
|  | 157 | char * area = (char *)calloc( 1, s ); | 
|---|
| [6a25b8f] | 158 | if ( area == 0p ) abort( "calloc/free out of memory" ); | 
|---|
| [5d4fa18] | 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 ); | 
|---|
| [3e91703d] | 165 | } // for | 
|---|
| [5d4fa18] | 166 |  | 
|---|
| [3e91703d] | 167 | for ( i; NoOfMmaps ) { | 
|---|
| [80228a7] | 168 | size_t s = i + default_mmap_start();                    // cross over point | 
|---|
| [5d4fa18] | 169 | locns[i] = (char *)calloc( 1, s ); | 
|---|
| [6a25b8f] | 170 | if ( locns[i] == 0p ) abort( "calloc/free out of memory" ); | 
|---|
| [5d4fa18] | 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 | 
|---|
| [3e91703d] | 176 | } // for | 
|---|
|  | 177 | for ( i; NoOfMmaps ) { | 
|---|
| [80228a7] | 178 | size_t s = i + default_mmap_start();                    // cross over point | 
|---|
| [5d4fa18] | 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] ); | 
|---|
| [3e91703d] | 182 | } // for | 
|---|
| [5d4fa18] | 183 |  | 
|---|
| [3e91703d] | 184 | // check memalign/free (sbrk) | 
|---|
| [5d4fa18] | 185 |  | 
|---|
| [80228a7] | 186 | for ( a; libAlign() ~= limit ~ a ) {                            // generate powers of 2 | 
|---|
| [200fcb3] | 187 | //sout | alignments[a]; | 
|---|
| [80228a7] | 188 | for ( s; 1 ~ NoOfAllocs ) {                                             // allocation of size 0 can return null | 
|---|
|  | 189 | char * area = (char *)memalign( a, s ); | 
|---|
| [6a25b8f] | 190 | if ( area == 0p ) abort( "memalign/free out of memory" ); | 
|---|
|  | 191 | //sout | i | area; | 
|---|
| [5d4fa18] | 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 | 
|---|
| [6a25b8f] | 195 | area[0] = '\345'; area[s - 1] = '\345';         // fill first/last byte | 
|---|
| [5d4fa18] | 196 | area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte | 
|---|
|  | 197 | free( area ); | 
|---|
|  | 198 | } // for | 
|---|
| [3e91703d] | 199 | } // for | 
|---|
| [5d4fa18] | 200 |  | 
|---|
| [3e91703d] | 201 | // check memalign/free (mmap) | 
|---|
| [5d4fa18] | 202 |  | 
|---|
| [80228a7] | 203 | for ( a; libAlign() ~= limit ~ a ) {                            // generate powers of 2 | 
|---|
| [200fcb3] | 204 | //sout | alignments[a]; | 
|---|
| [80228a7] | 205 | for ( i; 1 ~ NoOfMmaps ) { | 
|---|
|  | 206 | size_t s = i + default_mmap_start();            // cross over point | 
|---|
|  | 207 | char * area = (char *)memalign( a, s ); | 
|---|
| [6a25b8f] | 208 | if ( area == 0p ) abort( "memalign/free out of memory" ); | 
|---|
|  | 209 | //sout | i | area; | 
|---|
| [5d4fa18] | 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 | 
|---|
| [3e91703d] | 217 | } // for | 
|---|
| [5d4fa18] | 218 |  | 
|---|
| [3e91703d] | 219 | // check calloc/realloc/free (sbrk) | 
|---|
| [5d4fa18] | 220 |  | 
|---|
| [3e91703d] | 221 | for ( i; 1 ~ 10_000 ~ 12 ) { | 
|---|
| [5d4fa18] | 222 | // initial N byte allocation | 
|---|
| [80228a7] | 223 | char * area = (char *)calloc( 5, i ); | 
|---|
| [6a25b8f] | 224 | if ( area == 0p ) abort( "calloc/realloc/free out of memory" ); | 
|---|
| [5d4fa18] | 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. | 
|---|
| [80228a7] | 230 | for ( s; i ~ 256 * 1024 ~ 26 ) {                                // start at initial memory request | 
|---|
| [5d4fa18] | 231 | area = (char *)realloc( area, s );                      // attempt to reuse storage | 
|---|
| [6a25b8f] | 232 | if ( area == 0p ) abort( "calloc/realloc/free out of memory" ); | 
|---|
| [5d4fa18] | 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 ); | 
|---|
| [3e91703d] | 238 | } // for | 
|---|
| [5d4fa18] | 239 |  | 
|---|
| [3e91703d] | 240 | // check calloc/realloc/free (mmap) | 
|---|
| [5d4fa18] | 241 |  | 
|---|
| [3e91703d] | 242 | for ( i; 1 ~ 10_000 ~ 12 ) { | 
|---|
| [5d4fa18] | 243 | // initial N byte allocation | 
|---|
| [80228a7] | 244 | size_t s = i + default_mmap_start();                    // cross over point | 
|---|
|  | 245 | char * area = (char *)calloc( 1, s ); | 
|---|
| [6a25b8f] | 246 | if ( area == 0p ) abort( "calloc/realloc/free out of memory" ); | 
|---|
| [5d4fa18] | 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. | 
|---|
| [80228a7] | 252 | for ( r; i ~ 256 * 1024 ~ 26 ) {                                // start at initial memory request | 
|---|
|  | 253 | area = (char *)realloc( area, r );                      // attempt to reuse storage | 
|---|
| [6a25b8f] | 254 | if ( area == 0p ) abort( "calloc/realloc/free out of memory" ); | 
|---|
| [5d4fa18] | 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 ); | 
|---|
| [3e91703d] | 260 | } // for | 
|---|
| [5d4fa18] | 261 |  | 
|---|
| [3e91703d] | 262 | // check memalign/realloc/free | 
|---|
| [5d4fa18] | 263 |  | 
|---|
| [58e280f4] | 264 | amount = 2; | 
|---|
| [80228a7] | 265 | for ( a; libAlign() ~= limit ~ a ) {                            // generate powers of 2 | 
|---|
| [5d4fa18] | 266 | // initial N byte allocation | 
|---|
| [80228a7] | 267 | char * area = (char *)memalign( a, amount );    // aligned N-byte allocation | 
|---|
| [6a25b8f] | 268 | if ( area == 0p ) abort( "memalign/realloc/free out of memory" ); // no storage ? | 
|---|
|  | 269 | //sout | alignments[a] | area; | 
|---|
| [5d4fa18] | 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. | 
|---|
| [80228a7] | 276 | for ( s; amount ~ 256 * 1024 ) {                                // start at initial memory request | 
|---|
| [5d4fa18] | 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 | 
|---|
| [6a25b8f] | 279 | if ( area == 0p ) abort( "memalign/realloc/free out of memory" ); // no storage ? | 
|---|
|  | 280 | //sout | i | area; | 
|---|
| [5d4fa18] | 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 ); | 
|---|
| [3e91703d] | 287 | } // for | 
|---|
| [5d4fa18] | 288 |  | 
|---|
| [3e91703d] | 289 | // check cmemalign/free | 
|---|
| [5d4fa18] | 290 |  | 
|---|
| [80228a7] | 291 | for ( a; libAlign() ~= limit ~ a ) {                            // generate powers of 2 | 
|---|
| [200fcb3] | 292 | //sout | alignments[a]; | 
|---|
| [80228a7] | 293 | for ( s; 1 ~ limit ) {                                                  // allocation of size 0 can return null | 
|---|
|  | 294 | char * area = (char *)cmemalign( a, 1, s ); | 
|---|
| [6a25b8f] | 295 | if ( area == 0p ) abort( "cmemalign/free out of memory" ); | 
|---|
|  | 296 | //sout | i | area; | 
|---|
| [5d4fa18] | 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 | 
|---|
| [3e91703d] | 306 | } // for | 
|---|
| [5d4fa18] | 307 |  | 
|---|
| [3e91703d] | 308 | // check cmemalign/realloc/free | 
|---|
| [5d4fa18] | 309 |  | 
|---|
| [3e91703d] | 310 | amount = 2; | 
|---|
| [80228a7] | 311 | for ( a; libAlign() ~= limit ~ a ) {                            // generate powers of 2 | 
|---|
| [5d4fa18] | 312 | // initial N byte allocation | 
|---|
| [80228a7] | 313 | char * area = (char *)cmemalign( a, 1, amount ); // aligned N-byte allocation | 
|---|
| [6a25b8f] | 314 | if ( area == 0p ) abort( "cmemalign/realloc/free out of memory" ); // no storage ? | 
|---|
|  | 315 | //sout | alignments[a] | area; | 
|---|
| [5d4fa18] | 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. | 
|---|
| [80228a7] | 325 | for ( s; amount ~ 256 * 1024 ) {                                // start at initial memory request | 
|---|
| [5d4fa18] | 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 | 
|---|
| [6a25b8f] | 328 | if ( area == 0p ) abort( "cmemalign/realloc/free out of memory" ); // no storage ? | 
|---|
|  | 329 | //sout | i | area; | 
|---|
| [5d4fa18] | 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 ); | 
|---|
| [3e91703d] | 339 | } // for | 
|---|
| [6a25b8f] | 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 | 
|---|
| [58e280f4] | 400 |  | 
|---|
| [200fcb3] | 401 | //sout | "worker" | thisTask() | "successful completion"; | 
|---|
| [5d4fa18] | 402 | } // Worker main | 
|---|
|  | 403 |  | 
|---|
|  | 404 | int main() { | 
|---|
| [3e91703d] | 405 | const unsigned int NoOfWorkers = 4; | 
|---|
|  | 406 | { | 
|---|
| [5d4fa18] | 407 | processor processors[NoOfWorkers - 1] __attribute__(( unused )); // more than one processor | 
|---|
|  | 408 | Worker workers[NoOfWorkers] __attribute__(( unused )); | 
|---|
| [3e91703d] | 409 | } | 
|---|
| [5d4fa18] | 410 | // checkFreeOn(); | 
|---|
| [3e91703d] | 411 | // malloc_stats(); | 
|---|
| [5d4fa18] | 412 | } | 
|---|
|  | 413 |  | 
|---|
|  | 414 | // Local Variables: // | 
|---|
|  | 415 | // tab-width: 4 // | 
|---|
| [dc8511c] | 416 | // compile-command: "cfa -nodebug -O2 heap.cfa" // | 
|---|
| [5d4fa18] | 417 | // End: // | 
|---|