source: tests/alloc.cfa@ 4e74466

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 4e74466 was 9d5d01f, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

second attempt for temporary patch to fix build for alloc nodebug

  • Property mode set to 100644
File size: 10.5 KB
RevLine 
[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//
[dc8511c]7// alloc.cfa --
[2afec66]8//
[fab700b]9// Author : Peter A. Buhr
10// Created On : Wed Feb 3 07:56:22 2016
11// Last Modified By : Peter A. Buhr
[9d5d01f]12// Last Modified On : Mon Apr 6 21:08:23 2020
13// Update Count : 428
[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
[73abe95]20#include <fstream.hfa>
[eb5a115]21#include <stdlib.hfa> // access C malloc, realloc
[fab700b]22
23int * foo( int * p, int c ) { return p; }
24int * bar( int * p, int c ) { return p; }
25int * baz( int * p, int c ) { return p; }
26
27int main( void ) {
[2afec66]28 size_t dim = 10;
[eb5a115]29 char fill = '\xde';
[cfbc703d]30 int * ip, * ip1;
[fab700b]31
32 // allocation, non-array types
33
[cfbc703d]34 ip = (int *)malloc( sizeof(*ip) ); // C malloc, type unsafe
35 *ip = 0xdeadbeef;
36 printf( "C malloc %#x\n", *ip );
37 free( ip );
[fab700b]38
[cfbc703d]39 ip = malloc(); // CFA malloc, type safe
40 *ip = 0xdeadbeef;
41 printf( "CFA malloc %#x\n", *ip );
42 free( ip );
[6065b3aa]43
[cfbc703d]44 ip = alloc(); // CFA alloc, type safe
45 *ip = 0xdeadbeef;
46 printf( "CFA alloc %#x\n", *ip );
47 free( ip );
[fab700b]48
[cfbc703d]49 ip = alloc_set( fill ); // CFA alloc, fill
50 printf( "CFA alloc, fill %08x\n", *ip );
51 free( ip );
[fab700b]52
[cfbc703d]53 ip = alloc_set( 3 ); // CFA alloc, fill
54 printf( "CFA alloc, fill %d\n", *ip );
55 free( ip );
[eb5a115]56
[fab700b]57
58 // allocation, array types
59 printf( "\n" );
60
[cfbc703d]61 ip = (int *)calloc( dim, sizeof( *ip ) ); // C array calloc, type unsafe
[6065b3aa]62 printf( "C array calloc, fill 0\n" );
[cfbc703d]63 for ( i; dim ) { printf( "%#x ", ip[i] ); }
[fab700b]64 printf( "\n" );
[cfbc703d]65 free( ip );
[fab700b]66
[cfbc703d]67 ip = calloc( dim ); // CFA array calloc, type safe
[6065b3aa]68 printf( "CFA array calloc, fill 0\n" );
[cfbc703d]69 for ( i; dim ) { printf( "%#x ", ip[i] ); }
[fab700b]70 printf( "\n" );
[cfbc703d]71 free( ip );
[fab700b]72
[cfbc703d]73 ip = alloc( dim ); // CFA array alloc, type safe
74 for ( i; dim ) { ip[i] = 0xdeadbeef; }
[6065b3aa]75 printf( "CFA array alloc, no fill\n" );
[cfbc703d]76 for ( i; dim ) { printf( "%#x ", ip[i] ); }
[fab700b]77 printf( "\n" );
[cfbc703d]78 free( ip );
[fab700b]79
[cfbc703d]80 ip = alloc_set( 2 * dim, fill ); // CFA array alloc, fill
[6ea0408]81 printf( "CFA array alloc, fill %#hhx\n", fill );
[cfbc703d]82 for ( i; 2 * dim ) { printf( "%#x ", ip[i] ); }
[fab700b]83 printf( "\n" );
[cfbc703d]84 free( ip );
[eb5a115]85
[cfbc703d]86 ip = alloc_set( 2 * dim, 0xdeadbeef ); // CFA array alloc, fill
[eb5a115]87 printf( "CFA array alloc, fill %#hhx\n", 0xdeadbeef );
[cfbc703d]88 for ( i; 2 * dim ) { printf( "%#x ", ip[i] ); }
[eb5a115]89 printf( "\n" );
[fab700b]90 // do not free
91
[cfbc703d]92 ip1 = alloc_set( 2 * dim, ip ); // CFA array alloc, fill
[eb5a115]93 printf( "CFA array alloc, fill from array\n" );
[cfbc703d]94 for ( i; 2 * dim ) { printf( "%#x %#x, ", ip[i], ip1[i] ); }
95 free( ip1 );
[eb5a115]96 printf( "\n" );
97
[fab700b]98
[cfbc703d]99 // realloc, non-array types
[fab700b]100 printf( "\n" );
101
[cfbc703d]102 ip = (int *)realloc( ip, dim * sizeof(*ip) ); // C realloc
[eb5a115]103 printf( "C realloc\n" );
[cfbc703d]104 for ( i; dim ) { printf( "%#x ", ip[i] ); }
[fab700b]105 printf( "\n" );
[eb5a115]106 // do not free
[fab700b]107
[cfbc703d]108 ip = realloc( ip, 2 * dim * sizeof(*ip) ); // CFA realloc
109 for ( i; dim ~ 2 * dim ) { ip[i] = 0x1010101; }
[a4683611]110 printf( "CFA realloc\n" );
[cfbc703d]111 for ( i; 2 * dim ) { printf( "%#x ", ip[i] ); }
[fab700b]112 printf( "\n" );
[6065b3aa]113 // do not free
[fab700b]114
115
[cfbc703d]116 // realloc, array types
117 printf( "\n" );
118
119 ip = alloc( ip, dim ); // CFA realloc array alloc
120 for ( i; dim ) { ip[i] = 0xdeadbeef; }
121 printf( "CFA realloc array alloc\n" );
122 for ( i; dim ) { printf( "%#x ", ip[i] ); }
123 printf( "\n" );
124 // do not free
125
126 ip = alloc( ip, 2 * dim ); // CFA realloc array alloc
127 for ( i; dim ~ 2 * dim ) { ip[i] = 0x1010101; } // fill upper part
128 printf( "CFA realloc array alloc\n" );
129 for ( i; 2 * dim ) { printf( "%#x ", ip[i] ); }
130 printf( "\n" );
131 // do not free
132
133 ip = alloc( ip, dim ); // CFA realloc array alloc
134 printf( "CFA realloc array alloc\n" );
135 for ( i; dim ) { printf( "%#x ", ip[i] ); }
[fab700b]136 printf( "\n" );
[cfbc703d]137 // do not free
[fab700b]138
[cfbc703d]139 ip = alloc_set( ip, 3 * dim, fill ); // CFA realloc array alloc, fill
140 printf( "CFA realloc array alloc, fill\n" );
141 for ( i; 3 * dim ) { printf( "%#x ", ip[i] ); }
[fab700b]142 printf( "\n" );
[eb5a115]143 // do not free
[fab700b]144
[cfbc703d]145 ip = alloc_set( ip, dim, fill ); // CFA realloc array alloc, fill
146 printf( "CFA realloc array alloc, fill\n" );
147 for ( i; dim ) { printf( "%#x ", ip[i] ); }
[fab700b]148 printf( "\n" );
[eb5a115]149 // do not free
[fab700b]150
[cfbc703d]151 ip = alloc_set( ip, 3 * dim, fill ); // CFA realloc array alloc, fill
152 printf( "CFA realloc array alloc, fill\n" );
[23ecea4]153 for ( i; 3 * dim ) { printf( "%#x ", ip[i] ); }
[fab700b]154 printf( "\n" );
[eb5a115]155 // do not free
[23ecea4]156#if 0 // FIX ME
157 ip = alloc_set( ip, 5 * dim, 5 ); // CFA realloc array alloc, 5
[cfbc703d]158 printf( "CFA realloc array alloc, 5\n" );
[23ecea4]159 for ( i; 5 * dim ) { printf( "%#x ", ip[i] ); }
[fab700b]160 printf( "\n" );
[eb5a115]161 // do not free
[fab700b]162
[cfbc703d]163 ip = alloc_set( ip, dim, 5 ); // CFA realloc array alloc, 5
164 printf( "CFA realloc array alloc, 5\n" );
165 for ( i; dim ) { printf( "%#x ", ip[i] ); }
[fab700b]166 printf( "\n" );
[eb5a115]167 // do not free
[fab700b]168
[23ecea4]169 ip = alloc_set( ip, 5 * dim, 5 ); // CFA realloc array alloc, 5
[cfbc703d]170 printf( "CFA realloc array alloc, 5\n" );
[23ecea4]171 for ( i; 5 * dim ) { printf( "%#x ", ip[i] ); }
[fab700b]172 printf( "\n" );
[23ecea4]173#endif // 0
[9d5d01f]174 free( ip );
[cfbc703d]175
176 // resize, non-array types
177
178 struct S {
179 int a[5];
180 };
181
182 ip = alloc();
183 *ip = 5;
184 double * dp = alloc( ip );
185 *dp = 5.5;
186 S * sp = alloc( dp );
187 *sp = (S){ {0, 1, 2, 3, 4} };
188 ip = alloc( sp );
189 *ip = 3;
190 free( ip );
191
192
193 // resize, array types
194
195 ip = alloc( 5 );
196 for ( i; 5 ) { ip[i] = 5; }
197 dp = alloc( ip, 5 );
198 for ( i; 5 ) { dp[i] = 5.5; }
199 sp = alloc( dp, 5 );
200 for ( i; 5 ) { sp[i] = (S){ {0, 1, 2, 3, 4} }; }
201 ip = alloc( sp, 3 );
202 for ( i; 3 ) { ip[i] = 3; }
203 ip = alloc( ip, 7 );
204 for ( i; 7 ) { ip[i] = 7; }
205 ip = alloc( ip, 7, false );
206 for ( i; 7 ) { ip[i] = 7; }
207 free( ip );
[fab700b]208
209
[2afec66]210 struct Struct { int x; double y; };
211 Struct st, st1, sta[dim], sta1[dim], * stp, * stp1;
[fab700b]212
213 // alignment, non-array types
214 printf( "\n" );
215 enum { Alignment = 128 };
[6065b3aa]216
[6cfe8bb]217 stp = &(*(Struct*)memalign( Alignment, sizeof( *stp ) ) ){ 42, 42.5 }; // C memalign
[fab700b]218 assert( (uintptr_t)stp % Alignment == 0 );
[6065b3aa]219 printf( "C memalign %d %g\n", stp->x, stp->y );
[2afec66]220 free( stp );
[fab700b]221
[cfbc703d]222 stp = &(*memalign( Alignment )){ 42, 42.5 }; // CFA memalign
[fab700b]223 assert( (uintptr_t)stp % Alignment == 0 );
224 printf( "CFA memalign %d %g\n", stp->x, stp->y );
[2afec66]225 free( stp );
[fab700b]226
[2afec66]227 posix_memalign( (void **)&stp, Alignment, sizeof( *stp ) ); // C posix_memalign
[6065b3aa]228 *stp = (Struct){ 42, 42.5 };
229 assert( (uintptr_t)stp % Alignment == 0 );
230 printf( "CFA posix_memalign %d %g\n", stp->x, stp->y );
[2afec66]231 free( stp );
[6065b3aa]232
[2afec66]233 posix_memalign( &stp, Alignment ); // CFA posix_memalign
[fab700b]234 *stp = (Struct){ 42, 42.5 };
235 assert( (uintptr_t)stp % Alignment == 0 );
236 printf( "CFA posix_memalign %d %g\n", stp->x, stp->y );
[2afec66]237 free( stp );
[fab700b]238
[eb5a115]239 stp = &(*alloc_align( Alignment)){ 42, 42.5 }; // CFA alloc_align
[fab700b]240 assert( (uintptr_t)stp % Alignment == 0 );
[eb5a115]241 printf( "CFA alloc_align %d %g\n", stp->x, stp->y );
[2afec66]242 free( stp );
[6065b3aa]243
[eb5a115]244 stp = &(*alloc_align( Alignment )){ 42, 42.5 }; // CFA alloc_align
[6065b3aa]245 assert( (uintptr_t)stp % Alignment == 0 );
[eb5a115]246 printf( "CFA alloc_align %d %g\n", stp->x, stp->y );
[2afec66]247 free( stp );
[6065b3aa]248
[eb5a115]249 stp = alloc_align_set( Alignment, fill ); // CFA memalign, fill
[6065b3aa]250 assert( (uintptr_t)stp % Alignment == 0 );
[eb5a115]251 printf( "CFA alloc_align fill %#x %a\n", stp->x, stp->y );
252 free( stp );
253
254 stp = alloc_align_set( Alignment, (Struct){ 42, 42.5 } ); // CFA memalign, fill
255 assert( (uintptr_t)stp % Alignment == 0 );
256 printf( "CFA alloc_align fill %d %g\n", stp->x, stp->y );
257 // do not free
258
259 stp = &(*alloc_align( stp, 4096 )){ 42, 42.5 }; // CFA realign
260 assert( (uintptr_t)stp % 4096 == 0 );
261 printf( "CFA alloc_align %d %g\n", stp->x, stp->y );
[2afec66]262 free( stp );
[fab700b]263
264
265 // alignment, array types
266 printf( "\n" );
267
[eb5a115]268 stp = alloc_align( Alignment, dim ); // CFA array memalign
[fab700b]269 assert( (uintptr_t)stp % Alignment == 0 );
[8f34661]270 for ( i; dim ) { stp[i] = (Struct){ 42, 42.5 }; }
[eb5a115]271 printf( "CFA array alloc_align\n" );
[8f34661]272 for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
[fab700b]273 printf( "\n" );
[2afec66]274 free( stp );
[fab700b]275
[eb5a115]276 stp = alloc_align_set( Alignment, dim, fill ); // CFA array memalign, fill
[fab700b]277 assert( (uintptr_t)stp % Alignment == 0 );
[eb5a115]278 printf( "CFA array alloc_align, fill\n" );
[8f34661]279 for ( i; dim ) { printf( "%#x %a, ", stp[i].x, stp[i].y ); }
[fab700b]280 printf( "\n" );
[2afec66]281 free( stp );
[fab700b]282
[eb5a115]283 stp = alloc_align_set( Alignment, dim, (Struct){ 42, 42.5 } ); // CFA array memalign, fill
284 assert( (uintptr_t)stp % Alignment == 0 );
285 printf( "CFA array alloc_align, fill\n" );
286 for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
287 printf( "\n" );
288 // do not free
289
290 stp1 = alloc_align_set( Alignment, dim, stp ); // CFA array memalign, fill
291 assert( (uintptr_t)stp % Alignment == 0 );
292 printf( "CFA array alloc_align, fill array\n" );
293 for ( i; dim ) { printf( "%d %g, ", stp1[i].x, stp1[i].y ); }
294 printf( "\n" );
295 free( stp1 );
296
297 stp = alloc_align( stp, 4096, dim ); // CFA aligned realloc array
298 assert( (uintptr_t)stp % 4096 == 0 );
299 for ( i; dim ) { stp[i] = (Struct){ 42, 42.5 }; }
300 printf( "CFA realloc array alloc_align\n" );
301 for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
302 printf( "\n" );
303 free( stp );
304
[fab700b]305
306 // data, non-array types
307 printf( "\n" );
308
[2afec66]309 memset( &st, fill ); // CFA memset, type safe
[6065b3aa]310 printf( "CFA memset %#x %a\n", st.x, st.y );
[2afec66]311 memcpy( &st1, &st ); // CFA memcpy, type safe
[6065b3aa]312 printf( "CFA memcpy %#x %a\n", st1.x, st1.y );
[fab700b]313
314
315 // data, array types
316 printf( "\n" );
317
[b9c04946]318 amemset( sta, fill, dim ); // CFA array memset, type safe
[fab700b]319 printf( "CFA array memset\n" );
[8f34661]320 for ( i; dim ) { printf( "%#x %a, ", sta[i].x, sta[i].y ); }
[fab700b]321 printf( "\n" );
322
[b9c04946]323 amemcpy( sta1, sta, dim ); // CFA array memcpy, type safe
324 printf( "CFA array memcpy\n" );
[8f34661]325 for ( i; dim ) { printf( "%#x %a, ", sta1[i].x, sta1[i].y ); }
[6065b3aa]326 printf( "\n" );
327
328
329 // new, non-array types
330 printf( "\n" );
331
332 stp = new( 42, 42.5 );
333 stp1 = new( 42, 42.5 );
334 printf( "CFA new initialize\n%d %g %d %g\n", stp->x, stp->y, stp1->x, stp1->y );
335 delete( stp, stp1 );
336
337 // new, array types
338 stp = anew( dim, 42, 42.5 );
339 printf( "CFA array new initialize\n" );
[8f34661]340 for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
[6065b3aa]341 printf( "\n" );
342 stp1 = anew( dim, 42, 42.5 );
[8f34661]343 for ( i; dim ) { printf( "%d %g, ", stp1[i].x, stp1[i].y ); }
[6065b3aa]344 printf( "\n" );
345 adelete( dim, stp, dim, stp1 );
346
347 // extras
[fab700b]348 printf( "\n" );
349
[2afec66]350 float * fp = malloc() + 1;
351 printf( "pointer arithmetic %d\n", fp == fp - 1 );
352 free( fp - 1 );
[fab700b]353
[cfbc703d]354 ip = foo( bar( baz( malloc(), 0 ), 0 ), 0 );
355 *ip = 0xdeadbeef;
356 printf( "CFA deep malloc %#x\n", *ip );
357 free( ip );
[fab700b]358
[fc67d6f]359#ifdef ERR1
[fab700b]360 stp = malloc();
361 printf( "\nSHOULD FAIL\n" );
[cfbc703d]362 ip = realloc( stp, dim * sizeof( *stp ) );
363 ip = memset( stp, 10 );
364 ip = memcpy( &st1, &st );
[cdbfab0]365#endif
[fab700b]366} // main
367
368// Local Variables: //
369// tab-width: 4 //
[dc8511c]370// compile-command: "cfa alloc.cfa" //
[fab700b]371// End: //
Note: See TracBrowser for help on using the repository browser.