Changeset d3e4d6c for src/tests/alloc.c
- Timestamp:
- Aug 23, 2017, 6:22:07 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 87e08e24, cb811ac
- Parents:
- 9f07232 (diff), bd37119 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/tests/alloc.c
r9f07232 rd3e4d6c 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 … … 12 12 // Last Modified On : Thu Jul 20 16:01:10 2017 13 13 // Update Count : 318 14 // 14 // 15 15 16 16 #include <assert.h> … … 19 19 #include <stdlib.h> // posix_memalign 20 20 #include <fstream> 21 #include <stdlib> // access C malloc, realloc21 #include <stdlib> // access C malloc, realloc 22 22 23 23 int * foo( int * p, int c ) { return p; } … … 26 26 27 27 int main( void ) { 28 29 28 size_t dim = 10; 29 int * p; 30 30 char fill = '\1'; 31 31 32 32 // allocation, non-array types 33 33 34 p = (void *)malloc( sizeof(*p) );// C malloc, type unsafe34 p = (void *)malloc( sizeof(*p) ); // C malloc, type unsafe 35 35 *p = 0xdeadbeef; 36 36 printf( "C malloc %#x\n", *p ); 37 38 39 p = malloc();// CFA malloc, type safe37 free( p ); 38 39 p = malloc(); // CFA malloc, type safe 40 40 *p = 0xdeadbeef; 41 41 printf( "CFA malloc %#x\n", *p ); 42 43 44 p = alloc();// CFA alloc, type safe42 free( p ); 43 44 p = alloc(); // CFA alloc, type safe 45 45 *p = 0xdeadbeef; 46 46 printf( "CFA alloc %#x\n", *p ); 47 48 49 p = alloc( fill );// CFA alloc, fill47 free( p ); 48 49 p = alloc( fill ); // CFA alloc, fill 50 50 printf( "CFA alloc, fill %08x\n", *p ); 51 51 … … 54 54 printf( "\n" ); 55 55 56 p = calloc( dim, sizeof( *p ) );// C array calloc, type unsafe56 p = calloc( dim, sizeof( *p ) ); // C array calloc, type unsafe 57 57 printf( "C array calloc, fill 0\n" ); 58 58 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); } 59 59 printf( "\n" ); 60 61 62 p = calloc( dim );// CFA array calloc, type safe60 free( p ); 61 62 p = calloc( dim ); // CFA array calloc, type safe 63 63 printf( "CFA array calloc, fill 0\n" ); 64 64 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); } 65 65 printf( "\n" ); 66 67 68 p = alloc( dim );// CFA array alloc, type safe66 free( p ); 67 68 p = alloc( dim ); // CFA array alloc, type safe 69 69 for ( int i = 0; i < dim; i += 1 ) { p[i] = 0xdeadbeef; } 70 70 printf( "CFA array alloc, no fill\n" ); 71 71 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); } 72 72 printf( "\n" ); 73 74 75 p = alloc( 2 * dim, fill );// CFA array alloc, fill73 free( p ); 74 75 p = alloc( 2 * dim, fill ); // CFA array alloc, fill 76 76 printf( "CFA array alloc, fill %#x\n", fill ); 77 77 for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%#x ", p[i] ); } … … 83 83 printf( "\n" ); 84 84 85 p = (void *)realloc( p, dim * sizeof(*p) );// C realloc85 p = (void *)realloc( p, dim * sizeof(*p) ); // C realloc 86 86 for ( int i = 0; i < dim; i += 1 ) { p[i] = 0xdeadbeef; } 87 87 printf( "C realloc\n" ); … … 89 89 printf( "\n" ); 90 90 91 p = realloc( p, 2 * dim * sizeof(*p) );// CFA realloc91 p = realloc( p, 2 * dim * sizeof(*p) ); // CFA realloc 92 92 for ( int i = dim; i < 2 * dim; i += 1 ) { p[i] = 0x1010101; } 93 93 printf( "CFA realloc\n" ); … … 100 100 printf( "\n" ); 101 101 102 p = alloc( p, dim );// CFA resize array alloc102 p = alloc( p, dim ); // CFA resize array alloc 103 103 for ( int i = 0; i < dim; i += 1 ) { p[i] = 0xdeadbeef; } 104 104 printf( "CFA resize alloc\n" ); … … 106 106 printf( "\n" ); 107 107 108 p = alloc( p, 2 * dim );// CFA resize array alloc108 p = alloc( p, 2 * dim ); // CFA resize array alloc 109 109 for ( int i = dim; i < 2 * dim; i += 1 ) { p[i] = 0x1010101; } 110 110 printf( "CFA resize array alloc\n" ); … … 112 112 printf( "\n" ); 113 113 114 p = alloc( p, dim );// CFA array alloc114 p = alloc( p, dim ); // CFA array alloc 115 115 printf( "CFA resize array alloc\n" ); 116 116 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); } … … 120 120 p = 0; 121 121 122 p = alloc( p, dim, fill );// CFA array alloc, fill122 p = alloc( p, dim, fill ); // CFA array alloc, fill 123 123 printf( "CFA resize array alloc, fill\n" ); 124 124 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); } 125 125 printf( "\n" ); 126 126 127 p = alloc( p, 2 * dim, fill );// CFA array alloc, fill127 p = alloc( p, 2 * dim, fill ); // CFA array alloc, fill 128 128 printf( "CFA resize array alloc, fill\n" ); 129 129 for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%#x ", p[i] ); } 130 130 printf( "\n" ); 131 131 132 p = alloc( p, dim, fill );// CFA array alloc, fill132 p = alloc( p, dim, fill ); // CFA array alloc, fill 133 133 printf( "CFA resize array alloc, fill\n" ); 134 134 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] );; } … … 137 137 138 138 139 140 139 struct Struct { int x; double y; }; 140 Struct st, st1, sta[dim], sta1[dim], * stp, * stp1; 141 141 142 142 // alignment, non-array types … … 144 144 enum { Alignment = 128 }; 145 145 146 stp = (memalign( Alignment, sizeof( *stp ) ) ){ 42, 42.5 }; // C memalign146 stp = &(*(Struct*)memalign( Alignment, sizeof( *stp ) ) ){ 42, 42.5 }; // C memalign 147 147 assert( (uintptr_t)stp % Alignment == 0 ); 148 148 printf( "C memalign %d %g\n", stp->x, stp->y ); 149 150 151 stp = (memalign( Alignment )){ 42, 42.5 };// CFA memalign149 free( stp ); 150 151 stp = &(*memalign( Alignment )){ 42, 42.5 }; // CFA memalign 152 152 assert( (uintptr_t)stp % Alignment == 0 ); 153 153 printf( "CFA memalign %d %g\n", stp->x, stp->y ); 154 155 156 posix_memalign( (void **)&stp, Alignment, sizeof( *stp ) );// C posix_memalign154 free( stp ); 155 156 posix_memalign( (void **)&stp, Alignment, sizeof( *stp ) ); // C posix_memalign 157 157 *stp = (Struct){ 42, 42.5 }; 158 158 assert( (uintptr_t)stp % Alignment == 0 ); 159 159 printf( "CFA posix_memalign %d %g\n", stp->x, stp->y ); 160 161 162 posix_memalign( &stp, Alignment );// CFA posix_memalign160 free( stp ); 161 162 posix_memalign( &stp, Alignment ); // CFA posix_memalign 163 163 *stp = (Struct){ 42, 42.5 }; 164 164 assert( (uintptr_t)stp % Alignment == 0 ); 165 165 printf( "CFA posix_memalign %d %g\n", stp->x, stp->y ); 166 167 168 stp = (aligned_alloc( Alignment )){ 42, 42.5 };// CFA aligned_alloc166 free( stp ); 167 168 stp = &(*aligned_alloc( Alignment )){ 42, 42.5 }; // CFA aligned_alloc 169 169 assert( (uintptr_t)stp % Alignment == 0 ); 170 170 printf( "CFA aligned_alloc %d %g\n", stp->x, stp->y ); 171 172 173 stp = (align_alloc( Alignment )){ 42, 42.5 };// CFA align_alloc171 free( stp ); 172 173 stp = &(*align_alloc( Alignment )){ 42, 42.5 }; // CFA align_alloc 174 174 assert( (uintptr_t)stp % Alignment == 0 ); 175 175 printf( "CFA align_alloc %d %g\n", stp->x, stp->y ); 176 177 178 stp = align_alloc( Alignment, fill );// CFA memalign, fill176 free( stp ); 177 178 stp = align_alloc( Alignment, fill ); // CFA memalign, fill 179 179 assert( (uintptr_t)stp % Alignment == 0 ); 180 180 printf( "CFA align_alloc fill %#x %a\n", stp->x, stp->y ); 181 181 free( stp ); 182 182 183 183 … … 185 185 printf( "\n" ); 186 186 187 stp = align_alloc( Alignment, dim );// CFA array memalign187 stp = align_alloc( Alignment, dim ); // CFA array memalign 188 188 assert( (uintptr_t)stp % Alignment == 0 ); 189 189 for ( int i = 0; i < dim; i += 1 ) { stp[i] = (Struct){ 42, 42.5 }; } … … 191 191 for ( int i = 0; i < dim; i += 1 ) { printf( "%d %g, ", stp[i].x, stp[i].y ); } 192 192 printf( "\n" ); 193 194 195 stp = align_alloc( Alignment, dim, fill );// CFA array memalign, fill193 free( stp ); 194 195 stp = align_alloc( Alignment, dim, fill ); // CFA array memalign, fill 196 196 assert( (uintptr_t)stp % Alignment == 0 ); 197 197 printf( "CFA array align_alloc, fill\n" ); 198 198 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x %a, ", stp[i].x, stp[i].y ); } 199 199 printf( "\n" ); 200 200 free( stp ); 201 201 202 202 … … 204 204 printf( "\n" ); 205 205 206 memset( &st, fill );// CFA memset, type safe206 memset( &st, fill ); // CFA memset, type safe 207 207 printf( "CFA memset %#x %a\n", st.x, st.y ); 208 memcpy( &st1, &st );// CFA memcpy, type safe208 memcpy( &st1, &st ); // CFA memcpy, type safe 209 209 printf( "CFA memcpy %#x %a\n", st1.x, st1.y ); 210 210 … … 213 213 printf( "\n" ); 214 214 215 memset( sta, dim, fill );// CFA array memset, type safe215 memset( sta, dim, fill ); // CFA array memset, type safe 216 216 printf( "CFA array memset\n" ); 217 217 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x %a, ", sta[i].x, sta[i].y ); } 218 218 printf( "\n" ); 219 219 220 memcpy( sta1, sta, dim );// CFA array memcpy, type safe220 memcpy( sta1, sta, dim ); // CFA array memcpy, type safe 221 221 printf( "CFA memcpy\n" ); 222 222 for ( int i = 0; i < dim; i += 1 ) { printf( "%#x %a, ", sta1[i].x, sta1[i].y ); } … … 245 245 printf( "\n" ); 246 246 247 248 249 250 251 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 ); 252 252 *p = 0xdeadbeef; 253 253 printf( "CFA deep malloc %#x\n", *p ); 254 254 free( p ); 255 255 256 256 stp = malloc(); 257 257 printf( "\nSHOULD FAIL\n" ); 258 259 260 258 p = alloc( stp, dim * sizeof(*stp) ); 259 p = memset( stp, 10 ); 260 p = memcpy( &st1, &st ); 261 261 } // main 262 262
Note:
See TracChangeset
for help on using the changeset viewer.