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