[2afec66] | 1 | // |
---|
[fab700b] | 2 | // Cforall Version 1.0.0 Copyright (C) 2016 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 | // |
---|
[dc8511c] | 7 | // alloc.cfa -- |
---|
[2afec66] | 8 | // |
---|
[fab700b] | 9 | // Author : Peter A. Buhr |
---|
| 10 | // Created On : Wed Feb 3 07:56:22 2016 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[8725c74] | 12 | // Last Modified On : Sun Feb 16 09:21:13 2020 |
---|
| 13 | // Update Count : 405 |
---|
[2afec66] | 14 | // |
---|
[fab700b] | 15 | |
---|
[91c389a] | 16 | #include <assert.h> |
---|
[fab700b] | 17 | #include <malloc.h> // malloc_usable_size |
---|
| 18 | #include <stdint.h> // uintptr_t |
---|
[6065b3aa] | 19 | #include <stdlib.h> // posix_memalign |
---|
[73abe95] | 20 | #include <fstream.hfa> |
---|
[eb5a115] | 21 | #include <stdlib.hfa> // access C malloc, realloc |
---|
[fab700b] | 22 | |
---|
| 23 | int * foo( int * p, int c ) { return p; } |
---|
| 24 | int * bar( int * p, int c ) { return p; } |
---|
| 25 | int * baz( int * p, int c ) { return p; } |
---|
| 26 | |
---|
| 27 | int main( void ) { |
---|
[2afec66] | 28 | size_t dim = 10; |
---|
[eb5a115] | 29 | char fill = '\xde'; |
---|
| 30 | int * p, * p1; |
---|
[fab700b] | 31 | |
---|
| 32 | // allocation, non-array types |
---|
| 33 | |
---|
[fc67d6f] | 34 | p = (int *)malloc( sizeof(*p) ); // C malloc, type unsafe |
---|
[fab700b] | 35 | *p = 0xdeadbeef; |
---|
[6065b3aa] | 36 | printf( "C malloc %#x\n", *p ); |
---|
[2afec66] | 37 | free( p ); |
---|
[fab700b] | 38 | |
---|
[2afec66] | 39 | p = malloc(); // CFA malloc, type safe |
---|
[6065b3aa] | 40 | *p = 0xdeadbeef; |
---|
| 41 | printf( "CFA malloc %#x\n", *p ); |
---|
[2afec66] | 42 | free( p ); |
---|
[6065b3aa] | 43 | |
---|
[2afec66] | 44 | p = alloc(); // CFA alloc, type safe |
---|
[6065b3aa] | 45 | *p = 0xdeadbeef; |
---|
| 46 | printf( "CFA alloc %#x\n", *p ); |
---|
[2afec66] | 47 | free( p ); |
---|
[fab700b] | 48 | |
---|
[eb5a115] | 49 | p = alloc_set( fill ); // CFA alloc, fill |
---|
[6065b3aa] | 50 | printf( "CFA alloc, fill %08x\n", *p ); |
---|
[cf0de0e] | 51 | free( p ); |
---|
[fab700b] | 52 | |
---|
[eb5a115] | 53 | p = alloc_set( 3 ); // CFA alloc, fill |
---|
| 54 | printf( "CFA alloc, fill %d\n", *p ); |
---|
| 55 | free( p ); |
---|
| 56 | |
---|
[fab700b] | 57 | |
---|
| 58 | // allocation, array types |
---|
| 59 | printf( "\n" ); |
---|
| 60 | |
---|
[e672372] | 61 | p = (int *)calloc( dim, sizeof( *p ) ); // C array calloc, type unsafe |
---|
[6065b3aa] | 62 | printf( "C array calloc, fill 0\n" ); |
---|
[8f34661] | 63 | for ( i; dim ) { printf( "%#x ", p[i] ); } |
---|
[fab700b] | 64 | printf( "\n" ); |
---|
[2afec66] | 65 | free( p ); |
---|
[fab700b] | 66 | |
---|
[2afec66] | 67 | p = calloc( dim ); // CFA array calloc, type safe |
---|
[6065b3aa] | 68 | printf( "CFA array calloc, fill 0\n" ); |
---|
[8f34661] | 69 | for ( i; dim ) { printf( "%#x ", p[i] ); } |
---|
[fab700b] | 70 | printf( "\n" ); |
---|
[2afec66] | 71 | free( p ); |
---|
[fab700b] | 72 | |
---|
[2afec66] | 73 | p = alloc( dim ); // CFA array alloc, type safe |
---|
[8f34661] | 74 | for ( i; dim ) { p[i] = 0xdeadbeef; } |
---|
[6065b3aa] | 75 | printf( "CFA array alloc, no fill\n" ); |
---|
[8f34661] | 76 | for ( i; dim ) { printf( "%#x ", p[i] ); } |
---|
[fab700b] | 77 | printf( "\n" ); |
---|
[2afec66] | 78 | free( p ); |
---|
[fab700b] | 79 | |
---|
[eb5a115] | 80 | p = alloc_set( 2 * dim, fill ); // CFA array alloc, fill |
---|
[6ea0408] | 81 | printf( "CFA array alloc, fill %#hhx\n", fill ); |
---|
[8f34661] | 82 | for ( i; 2 * dim ) { printf( "%#x ", p[i] ); } |
---|
[fab700b] | 83 | printf( "\n" ); |
---|
[eb5a115] | 84 | free( p ); |
---|
| 85 | |
---|
| 86 | p = alloc_set( 2 * dim, 0xdeadbeef ); // CFA array alloc, fill |
---|
| 87 | printf( "CFA array alloc, fill %#hhx\n", 0xdeadbeef ); |
---|
| 88 | for ( i; 2 * dim ) { printf( "%#x ", p[i] ); } |
---|
| 89 | printf( "\n" ); |
---|
[fab700b] | 90 | // do not free |
---|
| 91 | |
---|
[eb5a115] | 92 | p1 = alloc_set( 2 * dim, p ); // CFA array alloc, fill |
---|
| 93 | printf( "CFA array alloc, fill from array\n" ); |
---|
| 94 | for ( i; 2 * dim ) { printf( "%#x %#x, ", p[i], p1[i] ); } |
---|
| 95 | free( p1 ); |
---|
| 96 | printf( "\n" ); |
---|
| 97 | |
---|
[fab700b] | 98 | |
---|
| 99 | // resize, non-array types |
---|
| 100 | printf( "\n" ); |
---|
| 101 | |
---|
[fc67d6f] | 102 | p = (int *)realloc( p, dim * sizeof(*p) ); // C realloc |
---|
[eb5a115] | 103 | printf( "C realloc\n" ); |
---|
[8f34661] | 104 | for ( i; dim ) { printf( "%#x ", p[i] ); } |
---|
[fab700b] | 105 | printf( "\n" ); |
---|
[eb5a115] | 106 | // do not free |
---|
[fab700b] | 107 | |
---|
[2afec66] | 108 | p = realloc( p, 2 * dim * sizeof(*p) ); // CFA realloc |
---|
[8f34661] | 109 | for ( i; dim ~ 2 * dim ) { p[i] = 0x1010101; } |
---|
[a4683611] | 110 | printf( "CFA realloc\n" ); |
---|
[8f34661] | 111 | for ( i; 2 * dim ) { printf( "%#x ", p[i] ); } |
---|
[fab700b] | 112 | printf( "\n" ); |
---|
[6065b3aa] | 113 | // do not free |
---|
[fab700b] | 114 | |
---|
| 115 | |
---|
[6065b3aa] | 116 | // resize, array types |
---|
[fab700b] | 117 | printf( "\n" ); |
---|
| 118 | |
---|
[2afec66] | 119 | p = alloc( p, dim ); // CFA resize array alloc |
---|
[8f34661] | 120 | for ( i; dim ) { p[i] = 0xdeadbeef; } |
---|
[eb5a115] | 121 | printf( "CFA resize array alloc\n" ); |
---|
[8f34661] | 122 | for ( i; dim ) { printf( "%#x ", p[i] ); } |
---|
[fab700b] | 123 | printf( "\n" ); |
---|
[eb5a115] | 124 | // do not free |
---|
[fab700b] | 125 | |
---|
[2afec66] | 126 | p = alloc( p, 2 * dim ); // CFA resize array alloc |
---|
[6a25b8f] | 127 | for ( i; dim ~ 2 * dim ) { p[i] = 0x1010101; } // fill upper part |
---|
[a4683611] | 128 | printf( "CFA resize array alloc\n" ); |
---|
[8f34661] | 129 | for ( i; 2 * dim ) { printf( "%#x ", p[i] ); } |
---|
[fab700b] | 130 | printf( "\n" ); |
---|
[eb5a115] | 131 | // do not free |
---|
[fab700b] | 132 | |
---|
[eb5a115] | 133 | p = alloc( p, dim ); // CFA resize array alloc |
---|
[a4683611] | 134 | printf( "CFA resize array alloc\n" ); |
---|
[8f34661] | 135 | for ( i; dim ) { printf( "%#x ", p[i] ); } |
---|
[fab700b] | 136 | printf( "\n" ); |
---|
[eb5a115] | 137 | // do not free |
---|
[fab700b] | 138 | |
---|
[eb5a115] | 139 | p = alloc_set( p, 3 * dim, fill ); // CFA resize array alloc, fill |
---|
[6a25b8f] | 140 | printf( "CFA resize array alloc\n" ); |
---|
[eb5a115] | 141 | for ( i; 3 * dim ) { printf( "%#x ", p[i] ); } |
---|
[fab700b] | 142 | printf( "\n" ); |
---|
[eb5a115] | 143 | // do not free |
---|
[fab700b] | 144 | |
---|
[eb5a115] | 145 | p = alloc_set( p, dim, fill ); // CFA resize array alloc, fill |
---|
[6a25b8f] | 146 | printf( "CFA resize array alloc\n" ); |
---|
[eb5a115] | 147 | for ( i; dim ) { printf( "%#x ", p[i] ); } |
---|
[fab700b] | 148 | printf( "\n" ); |
---|
[eb5a115] | 149 | // do not free |
---|
[fab700b] | 150 | |
---|
[eb5a115] | 151 | p = alloc_set( p, 3 * dim, fill ); // CFA resize array alloc, fill |
---|
[a4683611] | 152 | printf( "CFA resize array alloc, fill\n" ); |
---|
[eb5a115] | 153 | for ( i; 3 * dim ) { printf( "%#x ", p[i] );; } |
---|
[fab700b] | 154 | printf( "\n" ); |
---|
[6065b3aa] | 155 | free( p ); |
---|
[fab700b] | 156 | |
---|
| 157 | |
---|
[2afec66] | 158 | struct Struct { int x; double y; }; |
---|
| 159 | Struct st, st1, sta[dim], sta1[dim], * stp, * stp1; |
---|
[fab700b] | 160 | |
---|
| 161 | // alignment, non-array types |
---|
| 162 | printf( "\n" ); |
---|
| 163 | enum { Alignment = 128 }; |
---|
[6065b3aa] | 164 | |
---|
[6cfe8bb] | 165 | stp = &(*(Struct*)memalign( Alignment, sizeof( *stp ) ) ){ 42, 42.5 }; // C memalign |
---|
[fab700b] | 166 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
[6065b3aa] | 167 | printf( "C memalign %d %g\n", stp->x, stp->y ); |
---|
[2afec66] | 168 | free( stp ); |
---|
[fab700b] | 169 | |
---|
[6cfe8bb] | 170 | stp = &(*memalign( Alignment )){ 42, 42.5 }; // CFA memalign |
---|
[fab700b] | 171 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
| 172 | printf( "CFA memalign %d %g\n", stp->x, stp->y ); |
---|
[2afec66] | 173 | free( stp ); |
---|
[fab700b] | 174 | |
---|
[2afec66] | 175 | posix_memalign( (void **)&stp, Alignment, sizeof( *stp ) ); // C posix_memalign |
---|
[6065b3aa] | 176 | *stp = (Struct){ 42, 42.5 }; |
---|
| 177 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
| 178 | printf( "CFA posix_memalign %d %g\n", stp->x, stp->y ); |
---|
[2afec66] | 179 | free( stp ); |
---|
[6065b3aa] | 180 | |
---|
[2afec66] | 181 | posix_memalign( &stp, Alignment ); // CFA posix_memalign |
---|
[fab700b] | 182 | *stp = (Struct){ 42, 42.5 }; |
---|
| 183 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
| 184 | printf( "CFA posix_memalign %d %g\n", stp->x, stp->y ); |
---|
[2afec66] | 185 | free( stp ); |
---|
[fab700b] | 186 | |
---|
[eb5a115] | 187 | stp = &(*alloc_align( Alignment)){ 42, 42.5 }; // CFA alloc_align |
---|
[fab700b] | 188 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
[eb5a115] | 189 | printf( "CFA alloc_align %d %g\n", stp->x, stp->y ); |
---|
[2afec66] | 190 | free( stp ); |
---|
[6065b3aa] | 191 | |
---|
[eb5a115] | 192 | stp = &(*alloc_align( Alignment )){ 42, 42.5 }; // CFA alloc_align |
---|
[6065b3aa] | 193 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
[eb5a115] | 194 | printf( "CFA alloc_align %d %g\n", stp->x, stp->y ); |
---|
[2afec66] | 195 | free( stp ); |
---|
[6065b3aa] | 196 | |
---|
[eb5a115] | 197 | stp = alloc_align_set( Alignment, fill ); // CFA memalign, fill |
---|
[6065b3aa] | 198 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
[eb5a115] | 199 | printf( "CFA alloc_align fill %#x %a\n", stp->x, stp->y ); |
---|
| 200 | free( stp ); |
---|
| 201 | |
---|
| 202 | stp = alloc_align_set( Alignment, (Struct){ 42, 42.5 } ); // CFA memalign, fill |
---|
| 203 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
| 204 | printf( "CFA alloc_align fill %d %g\n", stp->x, stp->y ); |
---|
| 205 | // do not free |
---|
| 206 | |
---|
| 207 | stp = &(*alloc_align( stp, 4096 )){ 42, 42.5 }; // CFA realign |
---|
| 208 | assert( (uintptr_t)stp % 4096 == 0 ); |
---|
| 209 | printf( "CFA alloc_align %d %g\n", stp->x, stp->y ); |
---|
[2afec66] | 210 | free( stp ); |
---|
[fab700b] | 211 | |
---|
| 212 | |
---|
| 213 | // alignment, array types |
---|
| 214 | printf( "\n" ); |
---|
| 215 | |
---|
[eb5a115] | 216 | stp = alloc_align( Alignment, dim ); // CFA array memalign |
---|
[fab700b] | 217 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
[8f34661] | 218 | for ( i; dim ) { stp[i] = (Struct){ 42, 42.5 }; } |
---|
[eb5a115] | 219 | printf( "CFA array alloc_align\n" ); |
---|
[8f34661] | 220 | for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); } |
---|
[fab700b] | 221 | printf( "\n" ); |
---|
[2afec66] | 222 | free( stp ); |
---|
[fab700b] | 223 | |
---|
[eb5a115] | 224 | stp = alloc_align_set( Alignment, dim, fill ); // CFA array memalign, fill |
---|
[fab700b] | 225 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
[eb5a115] | 226 | printf( "CFA array alloc_align, fill\n" ); |
---|
[8f34661] | 227 | for ( i; dim ) { printf( "%#x %a, ", stp[i].x, stp[i].y ); } |
---|
[fab700b] | 228 | printf( "\n" ); |
---|
[2afec66] | 229 | free( stp ); |
---|
[fab700b] | 230 | |
---|
[eb5a115] | 231 | stp = alloc_align_set( Alignment, dim, (Struct){ 42, 42.5 } ); // CFA array memalign, fill |
---|
| 232 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
| 233 | printf( "CFA array alloc_align, fill\n" ); |
---|
| 234 | for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); } |
---|
| 235 | printf( "\n" ); |
---|
| 236 | // do not free |
---|
| 237 | |
---|
| 238 | stp1 = alloc_align_set( Alignment, dim, stp ); // CFA array memalign, fill |
---|
| 239 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
| 240 | printf( "CFA array alloc_align, fill array\n" ); |
---|
| 241 | for ( i; dim ) { printf( "%d %g, ", stp1[i].x, stp1[i].y ); } |
---|
| 242 | printf( "\n" ); |
---|
| 243 | free( stp1 ); |
---|
| 244 | |
---|
| 245 | stp = alloc_align( stp, 4096, dim ); // CFA aligned realloc array |
---|
| 246 | assert( (uintptr_t)stp % 4096 == 0 ); |
---|
| 247 | for ( i; dim ) { stp[i] = (Struct){ 42, 42.5 }; } |
---|
| 248 | printf( "CFA realloc array alloc_align\n" ); |
---|
| 249 | for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); } |
---|
| 250 | printf( "\n" ); |
---|
| 251 | free( stp ); |
---|
| 252 | |
---|
[fab700b] | 253 | |
---|
| 254 | // data, non-array types |
---|
| 255 | printf( "\n" ); |
---|
| 256 | |
---|
[2afec66] | 257 | memset( &st, fill ); // CFA memset, type safe |
---|
[6065b3aa] | 258 | printf( "CFA memset %#x %a\n", st.x, st.y ); |
---|
[2afec66] | 259 | memcpy( &st1, &st ); // CFA memcpy, type safe |
---|
[6065b3aa] | 260 | printf( "CFA memcpy %#x %a\n", st1.x, st1.y ); |
---|
[fab700b] | 261 | |
---|
| 262 | |
---|
| 263 | // data, array types |
---|
| 264 | printf( "\n" ); |
---|
| 265 | |
---|
[b9c04946] | 266 | amemset( sta, fill, dim ); // CFA array memset, type safe |
---|
[fab700b] | 267 | printf( "CFA array memset\n" ); |
---|
[8f34661] | 268 | for ( i; dim ) { printf( "%#x %a, ", sta[i].x, sta[i].y ); } |
---|
[fab700b] | 269 | printf( "\n" ); |
---|
| 270 | |
---|
[b9c04946] | 271 | amemcpy( sta1, sta, dim ); // CFA array memcpy, type safe |
---|
| 272 | printf( "CFA array memcpy\n" ); |
---|
[8f34661] | 273 | for ( i; dim ) { printf( "%#x %a, ", sta1[i].x, sta1[i].y ); } |
---|
[6065b3aa] | 274 | printf( "\n" ); |
---|
| 275 | |
---|
| 276 | |
---|
| 277 | // new, non-array types |
---|
| 278 | printf( "\n" ); |
---|
| 279 | |
---|
| 280 | stp = new( 42, 42.5 ); |
---|
| 281 | stp1 = new( 42, 42.5 ); |
---|
| 282 | printf( "CFA new initialize\n%d %g %d %g\n", stp->x, stp->y, stp1->x, stp1->y ); |
---|
| 283 | delete( stp, stp1 ); |
---|
| 284 | |
---|
| 285 | // new, array types |
---|
| 286 | stp = anew( dim, 42, 42.5 ); |
---|
| 287 | printf( "CFA array new initialize\n" ); |
---|
[8f34661] | 288 | for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); } |
---|
[6065b3aa] | 289 | printf( "\n" ); |
---|
| 290 | stp1 = anew( dim, 42, 42.5 ); |
---|
[8f34661] | 291 | for ( i; dim ) { printf( "%d %g, ", stp1[i].x, stp1[i].y ); } |
---|
[6065b3aa] | 292 | printf( "\n" ); |
---|
| 293 | adelete( dim, stp, dim, stp1 ); |
---|
| 294 | |
---|
| 295 | // extras |
---|
[fab700b] | 296 | printf( "\n" ); |
---|
| 297 | |
---|
[2afec66] | 298 | float * fp = malloc() + 1; |
---|
| 299 | printf( "pointer arithmetic %d\n", fp == fp - 1 ); |
---|
| 300 | free( fp - 1 ); |
---|
[fab700b] | 301 | |
---|
[2afec66] | 302 | p = foo( bar( baz( malloc(), 0 ), 0 ), 0 ); |
---|
[fab700b] | 303 | *p = 0xdeadbeef; |
---|
[6065b3aa] | 304 | printf( "CFA deep malloc %#x\n", *p ); |
---|
[2afec66] | 305 | free( p ); |
---|
[fab700b] | 306 | |
---|
[fc67d6f] | 307 | #ifdef ERR1 |
---|
[fab700b] | 308 | stp = malloc(); |
---|
| 309 | printf( "\nSHOULD FAIL\n" ); |
---|
[fc67d6f] | 310 | p = realloc( stp, dim * sizeof( *stp ) ); |
---|
| 311 | p = alloc( stp, dim * sizeof( *stp ) ); |
---|
[2afec66] | 312 | p = memset( stp, 10 ); |
---|
| 313 | p = memcpy( &st1, &st ); |
---|
[cdbfab0] | 314 | #endif |
---|
[fab700b] | 315 | } // main |
---|
| 316 | |
---|
| 317 | // Local Variables: // |
---|
| 318 | // tab-width: 4 // |
---|
[dc8511c] | 319 | // compile-command: "cfa alloc.cfa" // |
---|
[fab700b] | 320 | // End: // |
---|