Changes in src/tests/alloc.c [2afec66:91c389a]
- File:
-
- 1 edited
-
src/tests/alloc.c (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/tests/alloc.c
r2afec66 r91c389a 1 // 1 // 2 2 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo 3 3 // … … 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // alloc.c -- 8 // 7 // alloc.c -- 8 // 9 9 // Author : Peter A. Buhr 10 10 // Created On : Wed Feb 3 07:56:22 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Jun 2 15:13:03 2017 13 // Update Count : 316 14 // 15 16 #include <assert> 17 extern "C" { 18 #include <malloc.h> // malloc_usable_size 19 #include <stdint.h> // uintptr_t 20 #include <stdlib.h> // posix_memalign 21 } // extern 12 // Last Modified On : Thu Jul 20 16:01:10 2017 13 // Update Count : 318 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 22 20 #include <fstream> 23 #include <stdlib> // access C malloc, realloc21 #include <stdlib> // access C malloc, realloc 24 22 25 23 int * foo( int * p, int c ) { return p; } … … 28 26 29 27 int main( void ) { 30 size_t dim = 10;31 int * p;28 size_t dim = 10; 29 int * p; 32 30 char fill = '\1'; 33 31 34 32 // allocation, non-array types 35 33 36 p = (void *)malloc( sizeof(*p) );// C malloc, type unsafe34 p = (void *)malloc( sizeof(*p) ); // C malloc, type unsafe 37 35 *p = 0xdeadbeef; 38 36 printf( "C malloc %#x\n", *p ); 39 free( p );40 41 p = malloc();// CFA malloc, type safe37 free( p ); 38 39 p = malloc(); // CFA malloc, type safe 42 40 *p = 0xdeadbeef; 43 41 printf( "CFA malloc %#x\n", *p ); 44 free( p );45 46 p = alloc();// CFA alloc, type safe42 free( p ); 43 44 p = alloc(); // CFA alloc, type safe 47 45 *p = 0xdeadbeef; 48 46 printf( "CFA alloc %#x\n", *p ); 49 free( p );50 51 p = alloc( fill );// CFA alloc, fill47 free( p ); 48 49 p = alloc( fill ); // CFA alloc, fill 52 50 printf( "CFA alloc, fill %08x\n", *p ); 53 51 … … 56 54 printf( "\n" ); 57 55 58 p = calloc( dim, sizeof( *p ) );// C array calloc, type unsafe56 p = calloc( dim, sizeof( *p ) ); // C array calloc, type unsafe 59 57 printf( "C array calloc, fill 0\n" ); 60 58 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); } 61 59 printf( "\n" ); 62 free( p );63 64 p = calloc( dim );// CFA array calloc, type safe60 free( p ); 61 62 p = calloc( dim ); // CFA array calloc, type safe 65 63 printf( "CFA array calloc, fill 0\n" ); 66 64 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); } 67 65 printf( "\n" ); 68 free( p );69 70 p = alloc( dim );// CFA array alloc, type safe66 free( p ); 67 68 p = alloc( dim ); // CFA array alloc, type safe 71 69 for ( int i = 0; i < dim; i += 1 ) { p[i] = 0xdeadbeef; } 72 70 printf( "CFA array alloc, no fill\n" ); 73 71 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); } 74 72 printf( "\n" ); 75 free( p );76 77 p = alloc( 2 * dim, fill );// CFA array alloc, fill73 free( p ); 74 75 p = alloc( 2 * dim, fill ); // CFA array alloc, fill 78 76 printf( "CFA array alloc, fill %#x\n", fill ); 79 77 for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%#x ", p[i] ); } … … 85 83 printf( "\n" ); 86 84 87 p = (void *)realloc( p, dim * sizeof(*p) );// C realloc85 p = (void *)realloc( p, dim * sizeof(*p) ); // C realloc 88 86 for ( int i = 0; i < dim; i += 1 ) { p[i] = 0xdeadbeef; } 89 87 printf( "C realloc\n" ); … … 91 89 printf( "\n" ); 92 90 93 p = realloc( p, 2 * dim * sizeof(*p) );// CFA realloc91 p = realloc( p, 2 * dim * sizeof(*p) ); // CFA realloc 94 92 for ( int i = dim; i < 2 * dim; i += 1 ) { p[i] = 0x1010101; } 95 93 printf( "CFA realloc\n" ); … … 102 100 printf( "\n" ); 103 101 104 p = alloc( p, dim );// CFA resize array alloc102 p = alloc( p, dim ); // CFA resize array alloc 105 103 for ( int i = 0; i < dim; i += 1 ) { p[i] = 0xdeadbeef; } 106 104 printf( "CFA resize alloc\n" ); … … 108 106 printf( "\n" ); 109 107 110 p = alloc( p, 2 * dim );// CFA resize array alloc108 p = alloc( p, 2 * dim ); // CFA resize array alloc 111 109 for ( int i = dim; i < 2 * dim; i += 1 ) { p[i] = 0x1010101; } 112 110 printf( "CFA resize array alloc\n" ); … … 114 112 printf( "\n" ); 115 113 116 p = alloc( p, dim );// CFA array alloc114 p = alloc( p, dim ); // CFA array alloc 117 115 printf( "CFA resize array alloc\n" ); 118 116 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); } … … 122 120 p = 0; 123 121 124 p = alloc( p, dim, fill );// CFA array alloc, fill122 p = alloc( p, dim, fill ); // CFA array alloc, fill 125 123 printf( "CFA resize array alloc, fill\n" ); 126 124 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); } 127 125 printf( "\n" ); 128 126 129 p = alloc( p, 2 * dim, fill );// CFA array alloc, fill127 p = alloc( p, 2 * dim, fill ); // CFA array alloc, fill 130 128 printf( "CFA resize array alloc, fill\n" ); 131 129 for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%#x ", p[i] ); } 132 130 printf( "\n" ); 133 131 134 p = alloc( p, dim, fill );// CFA array alloc, fill132 p = alloc( p, dim, fill ); // CFA array alloc, fill 135 133 printf( "CFA resize array alloc, fill\n" ); 136 134 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] );; } … … 139 137 140 138 141 struct Struct { int x; double y; };142 Struct st, st1, sta[dim], sta1[dim], * stp, * stp1;139 struct Struct { int x; double y; }; 140 Struct st, st1, sta[dim], sta1[dim], * stp, * stp1; 143 141 144 142 // alignment, non-array types … … 146 144 enum { Alignment = 128 }; 147 145 148 stp = (memalign( Alignment, sizeof( *stp ) ) ){ 42, 42.5 }; // C memalign146 stp = (memalign( Alignment, sizeof( *stp ) ) ){ 42, 42.5 }; // C memalign 149 147 assert( (uintptr_t)stp % Alignment == 0 ); 150 148 printf( "C memalign %d %g\n", stp->x, stp->y ); 151 free( stp );152 153 stp = (memalign( Alignment )){ 42, 42.5 };// CFA memalign149 free( stp ); 150 151 stp = (memalign( Alignment )){ 42, 42.5 }; // CFA memalign 154 152 assert( (uintptr_t)stp % Alignment == 0 ); 155 153 printf( "CFA memalign %d %g\n", stp->x, stp->y ); 156 free( stp );157 158 posix_memalign( (void **)&stp, Alignment, sizeof( *stp ) );// C posix_memalign154 free( stp ); 155 156 posix_memalign( (void **)&stp, Alignment, sizeof( *stp ) ); // C posix_memalign 159 157 *stp = (Struct){ 42, 42.5 }; 160 158 assert( (uintptr_t)stp % Alignment == 0 ); 161 159 printf( "CFA posix_memalign %d %g\n", stp->x, stp->y ); 162 free( stp );163 164 posix_memalign( &stp, Alignment );// CFA posix_memalign160 free( stp ); 161 162 posix_memalign( &stp, Alignment ); // CFA posix_memalign 165 163 *stp = (Struct){ 42, 42.5 }; 166 164 assert( (uintptr_t)stp % Alignment == 0 ); 167 165 printf( "CFA posix_memalign %d %g\n", stp->x, stp->y ); 168 free( stp );169 170 stp = (aligned_alloc( Alignment )){ 42, 42.5 };// CFA aligned_alloc166 free( stp ); 167 168 stp = (aligned_alloc( Alignment )){ 42, 42.5 }; // CFA aligned_alloc 171 169 assert( (uintptr_t)stp % Alignment == 0 ); 172 170 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_alloc171 free( stp ); 172 173 stp = (align_alloc( Alignment )){ 42, 42.5 }; // CFA align_alloc 176 174 assert( (uintptr_t)stp % Alignment == 0 ); 177 175 printf( "CFA align_alloc %d %g\n", stp->x, stp->y ); 178 free( stp );179 180 stp = align_alloc( Alignment, fill );// CFA memalign, fill176 free( stp ); 177 178 stp = align_alloc( Alignment, fill ); // CFA memalign, fill 181 179 assert( (uintptr_t)stp % Alignment == 0 ); 182 180 printf( "CFA align_alloc fill %#x %a\n", stp->x, stp->y ); 183 free( stp );181 free( stp ); 184 182 185 183 … … 187 185 printf( "\n" ); 188 186 189 stp = align_alloc( Alignment, dim );// CFA array memalign187 stp = align_alloc( Alignment, dim ); // CFA array memalign 190 188 assert( (uintptr_t)stp % Alignment == 0 ); 191 189 for ( int i = 0; i < dim; i += 1 ) { stp[i] = (Struct){ 42, 42.5 }; } … … 193 191 for ( int i = 0; i < dim; i += 1 ) { printf( "%d %g, ", stp[i].x, stp[i].y ); } 194 192 printf( "\n" ); 195 free( stp );196 197 stp = align_alloc( Alignment, dim, fill );// CFA array memalign, fill193 free( stp ); 194 195 stp = align_alloc( Alignment, dim, fill ); // CFA array memalign, fill 198 196 assert( (uintptr_t)stp % Alignment == 0 ); 199 197 printf( "CFA array align_alloc, fill\n" ); 200 198 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x %a, ", stp[i].x, stp[i].y ); } 201 199 printf( "\n" ); 202 free( stp );200 free( stp ); 203 201 204 202 … … 206 204 printf( "\n" ); 207 205 208 memset( &st, fill );// CFA memset, type safe206 memset( &st, fill ); // CFA memset, type safe 209 207 printf( "CFA memset %#x %a\n", st.x, st.y ); 210 memcpy( &st1, &st );// CFA memcpy, type safe208 memcpy( &st1, &st ); // CFA memcpy, type safe 211 209 printf( "CFA memcpy %#x %a\n", st1.x, st1.y ); 212 210 … … 215 213 printf( "\n" ); 216 214 217 memset( sta, dim, fill );// CFA array memset, type safe215 memset( sta, dim, fill ); // CFA array memset, type safe 218 216 printf( "CFA array memset\n" ); 219 217 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x %a, ", sta[i].x, sta[i].y ); } 220 218 printf( "\n" ); 221 219 222 memcpy( sta1, sta, dim );// CFA array memcpy, type safe220 memcpy( sta1, sta, dim ); // CFA array memcpy, type safe 223 221 printf( "CFA memcpy\n" ); 224 222 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x %a, ", sta1[i].x, sta1[i].y ); } … … 247 245 printf( "\n" ); 248 246 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 );247 float * fp = malloc() + 1; 248 printf( "pointer arithmetic %d\n", fp == fp - 1 ); 249 free( fp - 1 ); 250 251 p = foo( bar( baz( malloc(), 0 ), 0 ), 0 ); 254 252 *p = 0xdeadbeef; 255 253 printf( "CFA deep malloc %#x\n", *p ); 256 free( p );254 free( p ); 257 255 258 256 stp = malloc(); 259 257 printf( "\nSHOULD FAIL\n" ); 260 p = alloc( stp, dim * sizeof(*stp) );261 p = memset( stp, 10 );262 p = memcpy( &st1, &st );258 p = alloc( stp, dim * sizeof(*stp) ); 259 p = memset( stp, 10 ); 260 p = memcpy( &st1, &st ); 263 261 } // main 264 262
Note:
See TracChangeset
for help on using the changeset viewer.