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