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