source: tests/alloc.cfa@ df91e15

Last change on this file since df91e15 was 4a3eb1c, checked in by Peter A. Buhr <pabuhr@…>, 17 months ago

add combinations of pointer/reference for memset and memcpy

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