[fab700b] | 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 |
---|
[a4683611] | 12 | // Last Modified On : Fri Jun 2 15:13:03 2017 |
---|
| 13 | // Update Count : 316 |
---|
[fab700b] | 14 | // |
---|
| 15 | |
---|
| 16 | #include <assert> |
---|
| 17 | extern "C" { |
---|
| 18 | #include <malloc.h> // malloc_usable_size |
---|
| 19 | #include <stdint.h> // uintptr_t |
---|
[6065b3aa] | 20 | #include <stdlib.h> // posix_memalign |
---|
[fab700b] | 21 | } // extern |
---|
| 22 | #include <fstream> |
---|
| 23 | #include <stdlib> // access C malloc, realloc |
---|
| 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 ) { |
---|
| 30 | size_t dim = 10; |
---|
| 31 | int * p; |
---|
[6065b3aa] | 32 | char fill = '\1'; |
---|
[fab700b] | 33 | |
---|
| 34 | // allocation, non-array types |
---|
| 35 | |
---|
| 36 | p = (void *)malloc( sizeof(*p) ); // C malloc, type unsafe |
---|
| 37 | *p = 0xdeadbeef; |
---|
[6065b3aa] | 38 | printf( "C malloc %#x\n", *p ); |
---|
[fab700b] | 39 | free( p ); |
---|
| 40 | |
---|
| 41 | p = malloc(); // CFA malloc, type safe |
---|
[6065b3aa] | 42 | *p = 0xdeadbeef; |
---|
| 43 | printf( "CFA malloc %#x\n", *p ); |
---|
| 44 | free( p ); |
---|
| 45 | |
---|
| 46 | p = alloc(); // CFA alloc, type safe |
---|
| 47 | *p = 0xdeadbeef; |
---|
| 48 | printf( "CFA alloc %#x\n", *p ); |
---|
[fab700b] | 49 | free( p ); |
---|
| 50 | |
---|
[6065b3aa] | 51 | p = alloc( fill ); // CFA alloc, fill |
---|
| 52 | printf( "CFA alloc, fill %08x\n", *p ); |
---|
[fab700b] | 53 | |
---|
| 54 | |
---|
| 55 | // allocation, array types |
---|
| 56 | printf( "\n" ); |
---|
| 57 | |
---|
| 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" ); |
---|
| 62 | free( p ); |
---|
| 63 | |
---|
| 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" ); |
---|
| 68 | free( p ); |
---|
| 69 | |
---|
[6065b3aa] | 70 | p = alloc( dim ); // CFA array alloc, type safe |
---|
| 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" ); |
---|
| 75 | free( p ); |
---|
| 76 | |
---|
[6065b3aa] | 77 | p = alloc( 2 * dim, fill ); // CFA array alloc, fill |
---|
| 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 | |
---|
[6065b3aa] | 87 | p = (void *)realloc( p, dim * sizeof(*p) ); // C realloc |
---|
| 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 | |
---|
| 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 | |
---|
[6065b3aa] | 104 | p = alloc( p, dim ); // CFA resize array alloc |
---|
| 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 | |
---|
[6065b3aa] | 110 | p = alloc( p, 2 * dim ); // CFA resize array alloc |
---|
| 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 | |
---|
[6065b3aa] | 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 | |
---|
[6065b3aa] | 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 | |
---|
[6065b3aa] | 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 | |
---|
[6065b3aa] | 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 | |
---|
[6065b3aa] | 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 | |
---|
| 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 ); |
---|
[fab700b] | 151 | free( stp ); |
---|
| 152 | |
---|
[6065b3aa] | 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 ); |
---|
| 156 | free( stp ); |
---|
| 157 | |
---|
[6065b3aa] | 158 | posix_memalign( (void **)&stp, Alignment, sizeof( *stp ) ); // C posix_memalign |
---|
| 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 ); |
---|
| 162 | free( stp ); |
---|
| 163 | |
---|
[fab700b] | 164 | posix_memalign( &stp, Alignment ); // CFA posix_memalign |
---|
| 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 ); |
---|
| 168 | free( stp ); |
---|
| 169 | |
---|
[6065b3aa] | 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 ); |
---|
| 173 | free( stp ); |
---|
| 174 | |
---|
| 175 | stp = (align_alloc( Alignment )){ 42, 42.5 }; // CFA align_alloc |
---|
| 176 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
| 177 | printf( "CFA align_alloc %d %g\n", stp->x, stp->y ); |
---|
| 178 | free( stp ); |
---|
| 179 | |
---|
| 180 | stp = align_alloc( Alignment, fill ); // CFA memalign, fill |
---|
| 181 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
| 182 | printf( "CFA align_alloc fill %#x %a\n", stp->x, stp->y ); |
---|
[fab700b] | 183 | free( stp ); |
---|
| 184 | |
---|
| 185 | |
---|
| 186 | // alignment, array types |
---|
| 187 | printf( "\n" ); |
---|
| 188 | |
---|
[6065b3aa] | 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" ); |
---|
| 195 | free( stp ); |
---|
| 196 | |
---|
[6065b3aa] | 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" ); |
---|
| 202 | free( stp ); |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | // data, non-array types |
---|
| 206 | printf( "\n" ); |
---|
| 207 | |
---|
[6065b3aa] | 208 | memset( &st, fill ); // CFA memset, type safe |
---|
| 209 | printf( "CFA memset %#x %a\n", st.x, st.y ); |
---|
| 210 | memcpy( &st1, &st ); // CFA memcpy, type safe |
---|
| 211 | printf( "CFA memcpy %#x %a\n", st1.x, st1.y ); |
---|
[fab700b] | 212 | |
---|
| 213 | |
---|
| 214 | // data, array types |
---|
| 215 | printf( "\n" ); |
---|
| 216 | |
---|
[6065b3aa] | 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 | |
---|
[6065b3aa] | 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 | |
---|
| 249 | float * fp = malloc() + 1; |
---|
| 250 | printf( "pointer arithmetic %d\n", fp == fp - 1 ); |
---|
| 251 | free( fp - 1 ); |
---|
| 252 | |
---|
| 253 | p = foo( bar( baz( malloc(), 0 ), 0 ), 0 ); |
---|
| 254 | *p = 0xdeadbeef; |
---|
[6065b3aa] | 255 | printf( "CFA deep malloc %#x\n", *p ); |
---|
[fab700b] | 256 | free( p ); |
---|
| 257 | |
---|
| 258 | stp = malloc(); |
---|
| 259 | printf( "\nSHOULD FAIL\n" ); |
---|
[6065b3aa] | 260 | p = alloc( stp, dim * sizeof(*stp) ); |
---|
[fab700b] | 261 | p = memset( stp, 10 ); |
---|
| 262 | p = memcpy( &st1, &st ); |
---|
| 263 | } // main |
---|
| 264 | |
---|
| 265 | // Local Variables: // |
---|
| 266 | // tab-width: 4 // |
---|
| 267 | // compile-command: "cfa alloc.c" // |
---|
| 268 | // End: // |
---|