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