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