| [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
 | 
|---|
| [dc8511c] | 12 | // Last Modified On : Tue Nov  6 17:50:52 2018
 | 
|---|
 | 13 | // Update Count     : 339
 | 
|---|
| [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>
 | 
|---|
 | 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;
 | 
|---|
| [2dcb750] | 29 |         char fill = '\xff';
 | 
|---|
| [2afec66] | 30 |         int * p;
 | 
|---|
| [fab700b] | 31 | 
 | 
|---|
 | 32 |         // allocation, non-array types
 | 
|---|
 | 33 | 
 | 
|---|
| [fc67d6f] | 34 |         // int & r = malloc();
 | 
|---|
| [e672372] | 35 |         // r = 0xdeadbeef;
 | 
|---|
 | 36 |         // printf( "C   malloc %#x\n", r );
 | 
|---|
 | 37 |         // free( &r );
 | 
|---|
 | 38 | 
 | 
|---|
| [fc67d6f] | 39 |         p = (int *)malloc( sizeof(*p) );                                        // C malloc, type unsafe
 | 
|---|
| [fab700b] | 40 |         *p = 0xdeadbeef;
 | 
|---|
| [6065b3aa] | 41 |         printf( "C   malloc %#x\n", *p );
 | 
|---|
| [2afec66] | 42 |         free( p );
 | 
|---|
| [fab700b] | 43 | 
 | 
|---|
| [2afec66] | 44 |         p = malloc();                                       // CFA malloc, type safe
 | 
|---|
| [6065b3aa] | 45 |         *p = 0xdeadbeef;
 | 
|---|
 | 46 |         printf( "CFA malloc %#x\n", *p );
 | 
|---|
| [2afec66] | 47 |         free( p );
 | 
|---|
| [6065b3aa] | 48 | 
 | 
|---|
| [2afec66] | 49 |         p = alloc();                                        // CFA alloc, type safe
 | 
|---|
| [6065b3aa] | 50 |         *p = 0xdeadbeef;
 | 
|---|
 | 51 |         printf( "CFA alloc %#x\n", *p );
 | 
|---|
| [2afec66] | 52 |         free( p );
 | 
|---|
| [fab700b] | 53 | 
 | 
|---|
| [2afec66] | 54 |         p = alloc( fill );                                  // CFA alloc, fill
 | 
|---|
| [6065b3aa] | 55 |         printf( "CFA alloc, fill %08x\n", *p );
 | 
|---|
| [cf0de0e] | 56 |         free( p );
 | 
|---|
| [fab700b] | 57 | 
 | 
|---|
 | 58 | 
 | 
|---|
 | 59 |         // allocation, array types
 | 
|---|
 | 60 |         printf( "\n" );
 | 
|---|
 | 61 | 
 | 
|---|
| [e672372] | 62 |         p = (int *)calloc( dim, sizeof( *p ) );                         // C array calloc, type unsafe
 | 
|---|
| [6065b3aa] | 63 |         printf( "C   array calloc, fill 0\n" );
 | 
|---|
| [8f34661] | 64 |         for ( i; dim ) { printf( "%#x ", p[i] ); }
 | 
|---|
| [fab700b] | 65 |         printf( "\n" );
 | 
|---|
| [2afec66] | 66 |         free( p );
 | 
|---|
| [fab700b] | 67 | 
 | 
|---|
| [2afec66] | 68 |         p = calloc( dim );                                  // CFA array calloc, type safe
 | 
|---|
| [6065b3aa] | 69 |         printf( "CFA array calloc, fill 0\n" );
 | 
|---|
| [8f34661] | 70 |         for ( i; dim ) { printf( "%#x ", p[i] ); }
 | 
|---|
| [fab700b] | 71 |         printf( "\n" );
 | 
|---|
| [2afec66] | 72 |         free( p );
 | 
|---|
| [fab700b] | 73 | 
 | 
|---|
| [2afec66] | 74 |         p = alloc( dim );                                   // CFA array alloc, type safe
 | 
|---|
| [8f34661] | 75 |         for ( i; dim ) { p[i] = 0xdeadbeef; }
 | 
|---|
| [6065b3aa] | 76 |         printf( "CFA array alloc, no fill\n" );
 | 
|---|
| [8f34661] | 77 |         for ( i; dim ) { printf( "%#x ", p[i] ); }
 | 
|---|
| [fab700b] | 78 |         printf( "\n" );
 | 
|---|
| [2afec66] | 79 |         free( p );
 | 
|---|
| [fab700b] | 80 | 
 | 
|---|
| [2afec66] | 81 |         p = alloc( 2 * dim, fill );                         // CFA array alloc, fill
 | 
|---|
| [6ea0408] | 82 |         printf( "CFA array alloc, fill %#hhx\n", fill );
 | 
|---|
| [8f34661] | 83 |         for ( i; 2 * dim ) { printf( "%#x ", p[i] ); }
 | 
|---|
| [fab700b] | 84 |         printf( "\n" );
 | 
|---|
 | 85 |         // do not free
 | 
|---|
 | 86 | 
 | 
|---|
 | 87 | 
 | 
|---|
 | 88 |         // resize, non-array types
 | 
|---|
 | 89 |         printf( "\n" );
 | 
|---|
 | 90 | 
 | 
|---|
| [fc67d6f] | 91 |         p = (int *)realloc( p, dim * sizeof(*p) );                      // C realloc
 | 
|---|
| [8f34661] | 92 |         for ( i; dim ) { p[i] = 0xdeadbeef; }
 | 
|---|
| [a4683611] | 93 |         printf( "C   realloc\n" );
 | 
|---|
| [8f34661] | 94 |         for ( i; dim ) { printf( "%#x ", p[i] ); }
 | 
|---|
| [fab700b] | 95 |         printf( "\n" );
 | 
|---|
 | 96 | 
 | 
|---|
| [2afec66] | 97 |         p = realloc( p, 2 * dim * sizeof(*p) );             // CFA realloc
 | 
|---|
| [8f34661] | 98 |         for ( i; dim ~ 2 * dim ) { p[i] = 0x1010101; }
 | 
|---|
| [a4683611] | 99 |         printf( "CFA realloc\n" );
 | 
|---|
| [8f34661] | 100 |         for ( i; 2 * dim ) { printf( "%#x ", p[i] ); }
 | 
|---|
| [fab700b] | 101 |         printf( "\n" );
 | 
|---|
| [6065b3aa] | 102 |         // do not free
 | 
|---|
| [fab700b] | 103 | 
 | 
|---|
 | 104 | 
 | 
|---|
| [6065b3aa] | 105 |         // resize, array types
 | 
|---|
| [fab700b] | 106 |         printf( "\n" );
 | 
|---|
 | 107 | 
 | 
|---|
| [2afec66] | 108 |         p = alloc( p, dim );                                // CFA resize array alloc
 | 
|---|
| [8f34661] | 109 |         for ( i; dim ) { p[i] = 0xdeadbeef; }
 | 
|---|
| [a4683611] | 110 |         printf( "CFA resize alloc\n" );
 | 
|---|
| [8f34661] | 111 |         for ( i; dim ) { printf( "%#x ", p[i] ); }
 | 
|---|
| [fab700b] | 112 |         printf( "\n" );
 | 
|---|
 | 113 | 
 | 
|---|
| [2afec66] | 114 |         p = alloc( p, 2 * dim );                            // CFA resize array alloc
 | 
|---|
| [8f34661] | 115 |         for ( i; dim ~ 2 * dim ) { p[i] = 0x1010101; }
 | 
|---|
| [a4683611] | 116 |         printf( "CFA resize array alloc\n" );
 | 
|---|
| [8f34661] | 117 |         for ( i; 2 * dim ) { printf( "%#x ", p[i] ); }
 | 
|---|
| [fab700b] | 118 |         printf( "\n" );
 | 
|---|
 | 119 | 
 | 
|---|
| [2afec66] | 120 |         p = alloc( p, dim );                                // CFA array alloc
 | 
|---|
| [a4683611] | 121 |         printf( "CFA resize array alloc\n" );
 | 
|---|
| [8f34661] | 122 |         for ( i; dim ) { printf( "%#x ", p[i] ); }
 | 
|---|
| [fab700b] | 123 |         printf( "\n" );
 | 
|---|
 | 124 | 
 | 
|---|
| [6065b3aa] | 125 |         free( p );
 | 
|---|
 | 126 |         p = 0;
 | 
|---|
| [fab700b] | 127 | 
 | 
|---|
| [2afec66] | 128 |         p = alloc( p, dim, fill );                          // CFA array alloc, fill
 | 
|---|
| [a4683611] | 129 |         printf( "CFA resize array alloc, fill\n" );
 | 
|---|
| [8f34661] | 130 |         for ( i; dim ) { printf( "%#x ", p[i] ); }
 | 
|---|
| [fab700b] | 131 |         printf( "\n" );
 | 
|---|
 | 132 | 
 | 
|---|
| [2afec66] | 133 |         p = alloc( p, 2 * dim, fill );                      // CFA array alloc, fill
 | 
|---|
| [a4683611] | 134 |         printf( "CFA resize array alloc, fill\n" );
 | 
|---|
| [8f34661] | 135 |         for ( i; 2 * dim ) { printf( "%#x ", p[i] ); }
 | 
|---|
| [fab700b] | 136 |         printf( "\n" );
 | 
|---|
 | 137 | 
 | 
|---|
| [2afec66] | 138 |         p = alloc( p, dim, fill );                          // CFA array alloc, fill
 | 
|---|
| [a4683611] | 139 |         printf( "CFA resize array alloc, fill\n" );
 | 
|---|
| [8f34661] | 140 |         for ( i; dim ) { printf( "%#x ", p[i] );; }
 | 
|---|
| [fab700b] | 141 |         printf( "\n" );
 | 
|---|
| [6065b3aa] | 142 |         free( p );
 | 
|---|
| [fab700b] | 143 | 
 | 
|---|
 | 144 | 
 | 
|---|
| [2afec66] | 145 |         struct Struct { int x; double y; };
 | 
|---|
 | 146 |         Struct st, st1, sta[dim], sta1[dim], * stp, * stp1;
 | 
|---|
| [fab700b] | 147 | 
 | 
|---|
 | 148 |         // alignment, non-array types
 | 
|---|
 | 149 |         printf( "\n" );
 | 
|---|
 | 150 |         enum { Alignment = 128 };
 | 
|---|
| [6065b3aa] | 151 | 
 | 
|---|
| [6cfe8bb] | 152 |         stp = &(*(Struct*)memalign( Alignment, sizeof( *stp ) ) ){ 42, 42.5 }; // C memalign
 | 
|---|
| [fab700b] | 153 |         assert( (uintptr_t)stp % Alignment == 0 );
 | 
|---|
| [6065b3aa] | 154 |         printf( "C   memalign %d %g\n", stp->x, stp->y );
 | 
|---|
| [2afec66] | 155 |         free( stp );
 | 
|---|
| [fab700b] | 156 | 
 | 
|---|
| [6cfe8bb] | 157 |         stp = &(*memalign( Alignment )){ 42, 42.5 };          // CFA memalign
 | 
|---|
| [fab700b] | 158 |         assert( (uintptr_t)stp % Alignment == 0 );
 | 
|---|
 | 159 |         printf( "CFA memalign %d %g\n", stp->x, stp->y );
 | 
|---|
| [2afec66] | 160 |         free( stp );
 | 
|---|
| [fab700b] | 161 | 
 | 
|---|
| [2afec66] | 162 |         posix_memalign( (void **)&stp, Alignment, sizeof( *stp ) ); // C posix_memalign
 | 
|---|
| [6065b3aa] | 163 |         *stp = (Struct){ 42, 42.5 };
 | 
|---|
 | 164 |         assert( (uintptr_t)stp % Alignment == 0 );
 | 
|---|
 | 165 |         printf( "CFA posix_memalign %d %g\n", stp->x, stp->y );
 | 
|---|
| [2afec66] | 166 |         free( stp );
 | 
|---|
| [6065b3aa] | 167 | 
 | 
|---|
| [2afec66] | 168 |         posix_memalign( &stp, Alignment );                  // CFA posix_memalign
 | 
|---|
| [fab700b] | 169 |         *stp = (Struct){ 42, 42.5 };
 | 
|---|
 | 170 |         assert( (uintptr_t)stp % Alignment == 0 );
 | 
|---|
 | 171 |         printf( "CFA posix_memalign %d %g\n", stp->x, stp->y );
 | 
|---|
| [2afec66] | 172 |         free( stp );
 | 
|---|
| [fab700b] | 173 | 
 | 
|---|
| [6cfe8bb] | 174 |         stp = &(*aligned_alloc( Alignment )){ 42, 42.5 };     // CFA aligned_alloc
 | 
|---|
| [fab700b] | 175 |         assert( (uintptr_t)stp % Alignment == 0 );
 | 
|---|
| [6065b3aa] | 176 |         printf( "CFA aligned_alloc %d %g\n", stp->x, stp->y );
 | 
|---|
| [2afec66] | 177 |         free( stp );
 | 
|---|
| [6065b3aa] | 178 | 
 | 
|---|
| [6cfe8bb] | 179 |         stp = &(*align_alloc( Alignment )){ 42, 42.5 };       // CFA align_alloc
 | 
|---|
| [6065b3aa] | 180 |         assert( (uintptr_t)stp % Alignment == 0 );
 | 
|---|
 | 181 |         printf( "CFA align_alloc %d %g\n", stp->x, stp->y );
 | 
|---|
| [2afec66] | 182 |         free( stp );
 | 
|---|
| [6065b3aa] | 183 | 
 | 
|---|
| [2afec66] | 184 |         stp = align_alloc( Alignment, fill );               // CFA memalign, fill
 | 
|---|
| [6065b3aa] | 185 |         assert( (uintptr_t)stp % Alignment == 0 );
 | 
|---|
 | 186 |         printf( "CFA align_alloc fill %#x %a\n", stp->x, stp->y );
 | 
|---|
| [2afec66] | 187 |         free( stp );
 | 
|---|
| [fab700b] | 188 | 
 | 
|---|
 | 189 | 
 | 
|---|
 | 190 |         // alignment, array types
 | 
|---|
 | 191 |         printf( "\n" );
 | 
|---|
 | 192 | 
 | 
|---|
| [2afec66] | 193 |         stp = align_alloc( Alignment, dim );                // CFA array memalign
 | 
|---|
| [fab700b] | 194 |         assert( (uintptr_t)stp % Alignment == 0 );
 | 
|---|
| [8f34661] | 195 |         for ( i; dim ) { stp[i] = (Struct){ 42, 42.5 }; }
 | 
|---|
| [6065b3aa] | 196 |         printf( "CFA array align_alloc\n" );
 | 
|---|
| [8f34661] | 197 |         for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
 | 
|---|
| [fab700b] | 198 |         printf( "\n" );
 | 
|---|
| [2afec66] | 199 |         free( stp );
 | 
|---|
| [fab700b] | 200 | 
 | 
|---|
| [2afec66] | 201 |         stp = align_alloc( Alignment, dim, fill );          // CFA array memalign, fill
 | 
|---|
| [fab700b] | 202 |         assert( (uintptr_t)stp % Alignment == 0 );
 | 
|---|
| [6065b3aa] | 203 |         printf( "CFA array align_alloc, fill\n" );
 | 
|---|
| [8f34661] | 204 |         for ( i; dim ) { printf( "%#x %a, ", stp[i].x, stp[i].y ); }
 | 
|---|
| [fab700b] | 205 |         printf( "\n" );
 | 
|---|
| [2afec66] | 206 |         free( stp );
 | 
|---|
| [fab700b] | 207 | 
 | 
|---|
 | 208 | 
 | 
|---|
 | 209 |         // data, non-array types
 | 
|---|
 | 210 |         printf( "\n" );
 | 
|---|
 | 211 | 
 | 
|---|
| [2afec66] | 212 |         memset( &st, fill );                                // CFA memset, type safe
 | 
|---|
| [6065b3aa] | 213 |         printf( "CFA memset %#x %a\n", st.x, st.y );
 | 
|---|
| [2afec66] | 214 |         memcpy( &st1, &st );                                // CFA memcpy, type safe
 | 
|---|
| [6065b3aa] | 215 |         printf( "CFA memcpy %#x %a\n", st1.x, st1.y );
 | 
|---|
| [fab700b] | 216 | 
 | 
|---|
 | 217 | 
 | 
|---|
 | 218 |         // data, array types
 | 
|---|
 | 219 |         printf( "\n" );
 | 
|---|
 | 220 | 
 | 
|---|
| [b9c04946] | 221 |         amemset( sta, fill, dim );                                                      // CFA array memset, type safe
 | 
|---|
| [fab700b] | 222 |         printf( "CFA array memset\n" );
 | 
|---|
| [8f34661] | 223 |         for ( i; dim ) { printf( "%#x %a, ", sta[i].x, sta[i].y ); }
 | 
|---|
| [fab700b] | 224 |         printf( "\n" );
 | 
|---|
 | 225 | 
 | 
|---|
| [b9c04946] | 226 |         amemcpy( sta1, sta, dim );                                                      // CFA array memcpy, type safe
 | 
|---|
 | 227 |         printf( "CFA array memcpy\n" );
 | 
|---|
| [8f34661] | 228 |         for ( i; dim ) { printf( "%#x %a, ", sta1[i].x, sta1[i].y ); }
 | 
|---|
| [6065b3aa] | 229 |         printf( "\n" );
 | 
|---|
 | 230 | 
 | 
|---|
 | 231 | 
 | 
|---|
 | 232 |         // new, non-array types
 | 
|---|
 | 233 |         printf( "\n" );
 | 
|---|
 | 234 | 
 | 
|---|
 | 235 |         stp = new( 42, 42.5 );
 | 
|---|
 | 236 |         stp1 = new( 42, 42.5 );
 | 
|---|
 | 237 |         printf( "CFA new initialize\n%d %g %d %g\n", stp->x, stp->y, stp1->x, stp1->y );
 | 
|---|
 | 238 |         delete( stp, stp1 );
 | 
|---|
 | 239 | 
 | 
|---|
 | 240 |         // new, array types
 | 
|---|
 | 241 |         stp = anew( dim, 42, 42.5 );
 | 
|---|
 | 242 |         printf( "CFA array new initialize\n" );
 | 
|---|
| [8f34661] | 243 |         for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
 | 
|---|
| [6065b3aa] | 244 |         printf( "\n" );
 | 
|---|
 | 245 |         stp1 = anew( dim, 42, 42.5 );
 | 
|---|
| [8f34661] | 246 |         for ( i; dim ) { printf( "%d %g, ", stp1[i].x, stp1[i].y ); }
 | 
|---|
| [6065b3aa] | 247 |         printf( "\n" );
 | 
|---|
 | 248 |         adelete( dim, stp, dim, stp1 );
 | 
|---|
 | 249 | 
 | 
|---|
 | 250 |         // extras
 | 
|---|
| [fab700b] | 251 |         printf( "\n" );
 | 
|---|
 | 252 | 
 | 
|---|
| [2afec66] | 253 |         float * fp = malloc() + 1;
 | 
|---|
 | 254 |         printf( "pointer arithmetic %d\n", fp == fp - 1 );
 | 
|---|
 | 255 |         free( fp - 1 );
 | 
|---|
| [fab700b] | 256 | 
 | 
|---|
| [2afec66] | 257 |         p = foo( bar( baz( malloc(), 0 ), 0 ), 0 );
 | 
|---|
| [fab700b] | 258 |         *p = 0xdeadbeef;
 | 
|---|
| [6065b3aa] | 259 |         printf( "CFA deep malloc %#x\n", *p );
 | 
|---|
| [2afec66] | 260 |         free( p );
 | 
|---|
| [fab700b] | 261 | 
 | 
|---|
| [fc67d6f] | 262 | #ifdef ERR1
 | 
|---|
| [fab700b] | 263 |         stp = malloc();
 | 
|---|
 | 264 |         printf( "\nSHOULD FAIL\n" );
 | 
|---|
| [fc67d6f] | 265 |         p = realloc( stp, dim * sizeof( *stp ) );
 | 
|---|
 | 266 |         p = alloc( stp, dim * sizeof( *stp ) );
 | 
|---|
| [2afec66] | 267 |         p = memset( stp, 10 );
 | 
|---|
 | 268 |         p = memcpy( &st1, &st );
 | 
|---|
| [cdbfab0] | 269 | #endif
 | 
|---|
| [fab700b] | 270 | } // main
 | 
|---|
 | 271 | 
 | 
|---|
 | 272 | // Local Variables: //
 | 
|---|
 | 273 | // tab-width: 4 //
 | 
|---|
| [dc8511c] | 274 | // compile-command: "cfa alloc.cfa" //
 | 
|---|
| [fab700b] | 275 | // End: //
 | 
|---|