[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 | // |
---|
[2afec66] | 7 | // alloc.c -- |
---|
| 8 | // |
---|
[fab700b] | 9 | // Author : Peter A. Buhr |
---|
| 10 | // Created On : Wed Feb 3 07:56:22 2016 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[fc67d6f] | 12 | // Last Modified On : Mon Jan 22 21:26:40 2018 |
---|
| 13 | // Update Count : 326 |
---|
[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 |
---|
[fab700b] | 20 | #include <fstream> |
---|
[e672372] | 21 | #include <stdlib> // 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; |
---|
| 29 | int * p; |
---|
[6065b3aa] | 30 | char fill = '\1'; |
---|
[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 ); |
---|
[fab700b] | 56 | |
---|
| 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" ); |
---|
| 63 | for ( int i = 0; i < dim; i += 1 ) { 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" ); |
---|
| 69 | for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); } |
---|
[fab700b] | 70 | printf( "\n" ); |
---|
[2afec66] | 71 | free( p ); |
---|
[fab700b] | 72 | |
---|
[2afec66] | 73 | p = alloc( dim ); // CFA array alloc, type safe |
---|
[6065b3aa] | 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] ); } |
---|
[fab700b] | 77 | printf( "\n" ); |
---|
[2afec66] | 78 | free( p ); |
---|
[fab700b] | 79 | |
---|
[2afec66] | 80 | p = alloc( 2 * dim, fill ); // CFA array alloc, fill |
---|
[6065b3aa] | 81 | printf( "CFA array alloc, fill %#x\n", fill ); |
---|
| 82 | for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%#x ", p[i] ); } |
---|
[fab700b] | 83 | printf( "\n" ); |
---|
| 84 | // do not free |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | // resize, non-array types |
---|
| 88 | printf( "\n" ); |
---|
| 89 | |
---|
[fc67d6f] | 90 | p = (int *)realloc( p, dim * sizeof(*p) ); // C realloc |
---|
[6065b3aa] | 91 | for ( int i = 0; i < dim; i += 1 ) { p[i] = 0xdeadbeef; } |
---|
[a4683611] | 92 | printf( "C realloc\n" ); |
---|
[6065b3aa] | 93 | for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); } |
---|
[fab700b] | 94 | printf( "\n" ); |
---|
| 95 | |
---|
[2afec66] | 96 | p = realloc( p, 2 * dim * sizeof(*p) ); // CFA realloc |
---|
[6065b3aa] | 97 | for ( int i = dim; i < 2 * dim; i += 1 ) { p[i] = 0x1010101; } |
---|
[a4683611] | 98 | printf( "CFA realloc\n" ); |
---|
[6065b3aa] | 99 | for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%#x ", p[i] ); } |
---|
[fab700b] | 100 | printf( "\n" ); |
---|
[6065b3aa] | 101 | // do not free |
---|
[fab700b] | 102 | |
---|
| 103 | |
---|
[6065b3aa] | 104 | // resize, array types |
---|
[fab700b] | 105 | printf( "\n" ); |
---|
| 106 | |
---|
[2afec66] | 107 | p = alloc( p, dim ); // CFA resize array alloc |
---|
[6065b3aa] | 108 | for ( int i = 0; i < dim; i += 1 ) { p[i] = 0xdeadbeef; } |
---|
[a4683611] | 109 | printf( "CFA resize alloc\n" ); |
---|
[6065b3aa] | 110 | for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); } |
---|
[fab700b] | 111 | printf( "\n" ); |
---|
| 112 | |
---|
[2afec66] | 113 | p = alloc( p, 2 * dim ); // CFA resize array alloc |
---|
[6065b3aa] | 114 | for ( int i = dim; i < 2 * dim; i += 1 ) { p[i] = 0x1010101; } |
---|
[a4683611] | 115 | printf( "CFA resize array alloc\n" ); |
---|
[6065b3aa] | 116 | for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%#x ", p[i] ); } |
---|
[fab700b] | 117 | printf( "\n" ); |
---|
| 118 | |
---|
[2afec66] | 119 | p = alloc( p, dim ); // CFA array alloc |
---|
[a4683611] | 120 | printf( "CFA resize array alloc\n" ); |
---|
[6065b3aa] | 121 | for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); } |
---|
[fab700b] | 122 | printf( "\n" ); |
---|
| 123 | |
---|
[6065b3aa] | 124 | free( p ); |
---|
| 125 | p = 0; |
---|
[fab700b] | 126 | |
---|
[2afec66] | 127 | p = alloc( p, dim, fill ); // CFA array alloc, fill |
---|
[a4683611] | 128 | printf( "CFA resize array alloc, fill\n" ); |
---|
[6065b3aa] | 129 | for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); } |
---|
[fab700b] | 130 | printf( "\n" ); |
---|
| 131 | |
---|
[2afec66] | 132 | p = alloc( p, 2 * dim, fill ); // CFA array alloc, fill |
---|
[a4683611] | 133 | printf( "CFA resize array alloc, fill\n" ); |
---|
[6065b3aa] | 134 | for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%#x ", p[i] ); } |
---|
[fab700b] | 135 | printf( "\n" ); |
---|
| 136 | |
---|
[2afec66] | 137 | p = alloc( p, dim, fill ); // CFA array alloc, fill |
---|
[a4683611] | 138 | printf( "CFA resize array alloc, fill\n" ); |
---|
[6065b3aa] | 139 | for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] );; } |
---|
[fab700b] | 140 | printf( "\n" ); |
---|
[6065b3aa] | 141 | free( p ); |
---|
[fab700b] | 142 | |
---|
| 143 | |
---|
[2afec66] | 144 | struct Struct { int x; double y; }; |
---|
| 145 | Struct st, st1, sta[dim], sta1[dim], * stp, * stp1; |
---|
[fab700b] | 146 | |
---|
| 147 | // alignment, non-array types |
---|
| 148 | printf( "\n" ); |
---|
| 149 | enum { Alignment = 128 }; |
---|
[6065b3aa] | 150 | |
---|
[6cfe8bb] | 151 | stp = &(*(Struct*)memalign( Alignment, sizeof( *stp ) ) ){ 42, 42.5 }; // C memalign |
---|
[fab700b] | 152 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
[6065b3aa] | 153 | printf( "C memalign %d %g\n", stp->x, stp->y ); |
---|
[2afec66] | 154 | free( stp ); |
---|
[fab700b] | 155 | |
---|
[6cfe8bb] | 156 | stp = &(*memalign( Alignment )){ 42, 42.5 }; // CFA memalign |
---|
[fab700b] | 157 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
| 158 | printf( "CFA memalign %d %g\n", stp->x, stp->y ); |
---|
[2afec66] | 159 | free( stp ); |
---|
[fab700b] | 160 | |
---|
[2afec66] | 161 | posix_memalign( (void **)&stp, Alignment, sizeof( *stp ) ); // C posix_memalign |
---|
[6065b3aa] | 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 ); |
---|
[2afec66] | 165 | free( stp ); |
---|
[6065b3aa] | 166 | |
---|
[2afec66] | 167 | posix_memalign( &stp, Alignment ); // CFA posix_memalign |
---|
[fab700b] | 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 ); |
---|
[2afec66] | 171 | free( stp ); |
---|
[fab700b] | 172 | |
---|
[6cfe8bb] | 173 | stp = &(*aligned_alloc( Alignment )){ 42, 42.5 }; // CFA aligned_alloc |
---|
[fab700b] | 174 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
[6065b3aa] | 175 | printf( "CFA aligned_alloc %d %g\n", stp->x, stp->y ); |
---|
[2afec66] | 176 | free( stp ); |
---|
[6065b3aa] | 177 | |
---|
[6cfe8bb] | 178 | stp = &(*align_alloc( Alignment )){ 42, 42.5 }; // CFA align_alloc |
---|
[6065b3aa] | 179 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
| 180 | printf( "CFA align_alloc %d %g\n", stp->x, stp->y ); |
---|
[2afec66] | 181 | free( stp ); |
---|
[6065b3aa] | 182 | |
---|
[2afec66] | 183 | stp = align_alloc( Alignment, fill ); // CFA memalign, fill |
---|
[6065b3aa] | 184 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
| 185 | printf( "CFA align_alloc fill %#x %a\n", stp->x, stp->y ); |
---|
[2afec66] | 186 | free( stp ); |
---|
[fab700b] | 187 | |
---|
| 188 | |
---|
| 189 | // alignment, array types |
---|
| 190 | printf( "\n" ); |
---|
| 191 | |
---|
[2afec66] | 192 | stp = align_alloc( Alignment, dim ); // CFA array memalign |
---|
[fab700b] | 193 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
[6065b3aa] | 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 ); } |
---|
[fab700b] | 197 | printf( "\n" ); |
---|
[2afec66] | 198 | free( stp ); |
---|
[fab700b] | 199 | |
---|
[2afec66] | 200 | stp = align_alloc( Alignment, dim, fill ); // CFA array memalign, fill |
---|
[fab700b] | 201 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
[6065b3aa] | 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 ); } |
---|
[fab700b] | 204 | printf( "\n" ); |
---|
[2afec66] | 205 | free( stp ); |
---|
[fab700b] | 206 | |
---|
| 207 | |
---|
| 208 | // data, non-array types |
---|
| 209 | printf( "\n" ); |
---|
| 210 | |
---|
[2afec66] | 211 | memset( &st, fill ); // CFA memset, type safe |
---|
[6065b3aa] | 212 | printf( "CFA memset %#x %a\n", st.x, st.y ); |
---|
[2afec66] | 213 | memcpy( &st1, &st ); // CFA memcpy, type safe |
---|
[6065b3aa] | 214 | printf( "CFA memcpy %#x %a\n", st1.x, st1.y ); |
---|
[fab700b] | 215 | |
---|
| 216 | |
---|
| 217 | // data, array types |
---|
| 218 | printf( "\n" ); |
---|
| 219 | |
---|
[2afec66] | 220 | memset( sta, dim, fill ); // CFA array memset, type safe |
---|
[fab700b] | 221 | printf( "CFA array memset\n" ); |
---|
[6065b3aa] | 222 | for ( int i = 0; i < dim; i += 1 ) { printf( "%#x %a, ", sta[i].x, sta[i].y ); } |
---|
[fab700b] | 223 | printf( "\n" ); |
---|
| 224 | |
---|
[2afec66] | 225 | memcpy( sta1, sta, dim ); // CFA array memcpy, type safe |
---|
[fab700b] | 226 | printf( "CFA memcpy\n" ); |
---|
[6065b3aa] | 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 |
---|
[fab700b] | 250 | printf( "\n" ); |
---|
| 251 | |
---|
[2afec66] | 252 | float * fp = malloc() + 1; |
---|
| 253 | printf( "pointer arithmetic %d\n", fp == fp - 1 ); |
---|
| 254 | free( fp - 1 ); |
---|
[fab700b] | 255 | |
---|
[2afec66] | 256 | p = foo( bar( baz( malloc(), 0 ), 0 ), 0 ); |
---|
[fab700b] | 257 | *p = 0xdeadbeef; |
---|
[6065b3aa] | 258 | printf( "CFA deep malloc %#x\n", *p ); |
---|
[2afec66] | 259 | free( p ); |
---|
[fab700b] | 260 | |
---|
[fc67d6f] | 261 | #ifdef ERR1 |
---|
[fab700b] | 262 | stp = malloc(); |
---|
| 263 | printf( "\nSHOULD FAIL\n" ); |
---|
[fc67d6f] | 264 | p = realloc( stp, dim * sizeof( *stp ) ); |
---|
| 265 | p = alloc( stp, dim * sizeof( *stp ) ); |
---|
[2afec66] | 266 | p = memset( stp, 10 ); |
---|
| 267 | p = memcpy( &st1, &st ); |
---|
[cdbfab0] | 268 | #endif |
---|
[fab700b] | 269 | } // main |
---|
| 270 | |
---|
| 271 | // Local Variables: // |
---|
| 272 | // tab-width: 4 // |
---|
| 273 | // compile-command: "cfa alloc.c" // |
---|
| 274 | // End: // |
---|