source: tests/alloc.cfa@ 1389810

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

argument test of free with multiple values

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