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