source: tests/alloc.cfa@ 1e2de89

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

remove unnecessary print

  • Property mode set to 100644
File size: 9.4 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 : Sun Feb 16 09:21:13 2020
13// Update Count : 405
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 * p, * p1;
31
32 // allocation, non-array types
33
34 p = (int *)malloc( sizeof(*p) ); // C malloc, type unsafe
35 *p = 0xdeadbeef;
36 printf( "C malloc %#x\n", *p );
37 free( p );
38
39 p = malloc(); // CFA malloc, type safe
40 *p = 0xdeadbeef;
41 printf( "CFA malloc %#x\n", *p );
42 free( p );
43
44 p = alloc(); // CFA alloc, type safe
45 *p = 0xdeadbeef;
46 printf( "CFA alloc %#x\n", *p );
47 free( p );
48
49 p = alloc_set( fill ); // CFA alloc, fill
50 printf( "CFA alloc, fill %08x\n", *p );
51 free( p );
52
53 p = alloc_set( 3 ); // CFA alloc, fill
54 printf( "CFA alloc, fill %d\n", *p );
55 free( p );
56
57
58 // allocation, array types
59 printf( "\n" );
60
61 p = (int *)calloc( dim, sizeof( *p ) ); // C array calloc, type unsafe
62 printf( "C array calloc, fill 0\n" );
63 for ( i; dim ) { printf( "%#x ", p[i] ); }
64 printf( "\n" );
65 free( p );
66
67 p = calloc( dim ); // CFA array calloc, type safe
68 printf( "CFA array calloc, fill 0\n" );
69 for ( i; dim ) { printf( "%#x ", p[i] ); }
70 printf( "\n" );
71 free( p );
72
73 p = alloc( dim ); // CFA array alloc, type safe
74 for ( i; dim ) { p[i] = 0xdeadbeef; }
75 printf( "CFA array alloc, no fill\n" );
76 for ( i; dim ) { printf( "%#x ", p[i] ); }
77 printf( "\n" );
78 free( p );
79
80 p = alloc_set( 2 * dim, fill ); // CFA array alloc, fill
81 printf( "CFA array alloc, fill %#hhx\n", fill );
82 for ( i; 2 * dim ) { printf( "%#x ", p[i] ); }
83 printf( "\n" );
84 free( p );
85
86 p = alloc_set( 2 * dim, 0xdeadbeef ); // CFA array alloc, fill
87 printf( "CFA array alloc, fill %#hhx\n", 0xdeadbeef );
88 for ( i; 2 * dim ) { printf( "%#x ", p[i] ); }
89 printf( "\n" );
90 // do not free
91
92 p1 = alloc_set( 2 * dim, p ); // CFA array alloc, fill
93 printf( "CFA array alloc, fill from array\n" );
94 for ( i; 2 * dim ) { printf( "%#x %#x, ", p[i], p1[i] ); }
95 free( p1 );
96 printf( "\n" );
97
98
99 // resize, non-array types
100 printf( "\n" );
101
102 p = (int *)realloc( p, dim * sizeof(*p) ); // C realloc
103 printf( "C realloc\n" );
104 for ( i; dim ) { printf( "%#x ", p[i] ); }
105 printf( "\n" );
106 // do not free
107
108 p = realloc( p, 2 * dim * sizeof(*p) ); // CFA realloc
109 for ( i; dim ~ 2 * dim ) { p[i] = 0x1010101; }
110 printf( "CFA realloc\n" );
111 for ( i; 2 * dim ) { printf( "%#x ", p[i] ); }
112 printf( "\n" );
113 // do not free
114
115
116 // resize, array types
117 printf( "\n" );
118
119 p = alloc( p, dim ); // CFA resize array alloc
120 for ( i; dim ) { p[i] = 0xdeadbeef; }
121 printf( "CFA resize array alloc\n" );
122 for ( i; dim ) { printf( "%#x ", p[i] ); }
123 printf( "\n" );
124 // do not free
125
126 p = alloc( p, 2 * dim ); // CFA resize array alloc
127 for ( i; dim ~ 2 * dim ) { p[i] = 0x1010101; } // fill upper part
128 printf( "CFA resize array alloc\n" );
129 for ( i; 2 * dim ) { printf( "%#x ", p[i] ); }
130 printf( "\n" );
131 // do not free
132
133 p = alloc( p, dim ); // CFA resize array alloc
134 printf( "CFA resize array alloc\n" );
135 for ( i; dim ) { printf( "%#x ", p[i] ); }
136 printf( "\n" );
137 // do not free
138
139 p = alloc_set( p, 3 * dim, fill ); // CFA resize array alloc, fill
140 printf( "CFA resize array alloc\n" );
141 for ( i; 3 * dim ) { printf( "%#x ", p[i] ); }
142 printf( "\n" );
143 // do not free
144
145 p = alloc_set( p, dim, fill ); // CFA resize array alloc, fill
146 printf( "CFA resize array alloc\n" );
147 for ( i; dim ) { printf( "%#x ", p[i] ); }
148 printf( "\n" );
149 // do not free
150
151 p = alloc_set( p, 3 * dim, fill ); // CFA resize array alloc, fill
152 printf( "CFA resize array alloc, fill\n" );
153 for ( i; 3 * dim ) { printf( "%#x ", p[i] );; }
154 printf( "\n" );
155 free( p );
156
157
158 struct Struct { int x; double y; };
159 Struct st, st1, sta[dim], sta1[dim], * stp, * stp1;
160
161 // alignment, non-array types
162 printf( "\n" );
163 enum { Alignment = 128 };
164
165 stp = &(*(Struct*)memalign( Alignment, sizeof( *stp ) ) ){ 42, 42.5 }; // C memalign
166 assert( (uintptr_t)stp % Alignment == 0 );
167 printf( "C memalign %d %g\n", stp->x, stp->y );
168 free( stp );
169
170 stp = &(*memalign( Alignment )){ 42, 42.5 }; // CFA memalign
171 assert( (uintptr_t)stp % Alignment == 0 );
172 printf( "CFA memalign %d %g\n", stp->x, stp->y );
173 free( stp );
174
175 posix_memalign( (void **)&stp, Alignment, sizeof( *stp ) ); // C posix_memalign
176 *stp = (Struct){ 42, 42.5 };
177 assert( (uintptr_t)stp % Alignment == 0 );
178 printf( "CFA posix_memalign %d %g\n", stp->x, stp->y );
179 free( stp );
180
181 posix_memalign( &stp, Alignment ); // CFA posix_memalign
182 *stp = (Struct){ 42, 42.5 };
183 assert( (uintptr_t)stp % Alignment == 0 );
184 printf( "CFA posix_memalign %d %g\n", stp->x, stp->y );
185 free( stp );
186
187 stp = &(*alloc_align( Alignment)){ 42, 42.5 }; // CFA alloc_align
188 assert( (uintptr_t)stp % Alignment == 0 );
189 printf( "CFA alloc_align %d %g\n", stp->x, stp->y );
190 free( stp );
191
192 stp = &(*alloc_align( Alignment )){ 42, 42.5 }; // CFA alloc_align
193 assert( (uintptr_t)stp % Alignment == 0 );
194 printf( "CFA alloc_align %d %g\n", stp->x, stp->y );
195 free( stp );
196
197 stp = alloc_align_set( Alignment, fill ); // CFA memalign, fill
198 assert( (uintptr_t)stp % Alignment == 0 );
199 printf( "CFA alloc_align fill %#x %a\n", stp->x, stp->y );
200 free( stp );
201
202 stp = alloc_align_set( Alignment, (Struct){ 42, 42.5 } ); // CFA memalign, fill
203 assert( (uintptr_t)stp % Alignment == 0 );
204 printf( "CFA alloc_align fill %d %g\n", stp->x, stp->y );
205 // do not free
206
207 stp = &(*alloc_align( stp, 4096 )){ 42, 42.5 }; // CFA realign
208 assert( (uintptr_t)stp % 4096 == 0 );
209 printf( "CFA alloc_align %d %g\n", stp->x, stp->y );
210 free( stp );
211
212
213 // alignment, array types
214 printf( "\n" );
215
216 stp = alloc_align( Alignment, dim ); // CFA array memalign
217 assert( (uintptr_t)stp % Alignment == 0 );
218 for ( i; dim ) { stp[i] = (Struct){ 42, 42.5 }; }
219 printf( "CFA array alloc_align\n" );
220 for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
221 printf( "\n" );
222 free( stp );
223
224 stp = alloc_align_set( Alignment, dim, fill ); // CFA array memalign, fill
225 assert( (uintptr_t)stp % Alignment == 0 );
226 printf( "CFA array alloc_align, fill\n" );
227 for ( i; dim ) { printf( "%#x %a, ", stp[i].x, stp[i].y ); }
228 printf( "\n" );
229 free( stp );
230
231 stp = alloc_align_set( Alignment, dim, (Struct){ 42, 42.5 } ); // CFA array memalign, fill
232 assert( (uintptr_t)stp % Alignment == 0 );
233 printf( "CFA array alloc_align, fill\n" );
234 for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
235 printf( "\n" );
236 // do not free
237
238 stp1 = alloc_align_set( Alignment, dim, stp ); // CFA array memalign, fill
239 assert( (uintptr_t)stp % Alignment == 0 );
240 printf( "CFA array alloc_align, fill array\n" );
241 for ( i; dim ) { printf( "%d %g, ", stp1[i].x, stp1[i].y ); }
242 printf( "\n" );
243 free( stp1 );
244
245 stp = alloc_align( stp, 4096, dim ); // CFA aligned realloc array
246 assert( (uintptr_t)stp % 4096 == 0 );
247 for ( i; dim ) { stp[i] = (Struct){ 42, 42.5 }; }
248 printf( "CFA realloc array alloc_align\n" );
249 for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
250 printf( "\n" );
251 free( stp );
252
253
254 // data, non-array types
255 printf( "\n" );
256
257 memset( &st, fill ); // CFA memset, type safe
258 printf( "CFA memset %#x %a\n", st.x, st.y );
259 memcpy( &st1, &st ); // CFA memcpy, type safe
260 printf( "CFA memcpy %#x %a\n", st1.x, st1.y );
261
262
263 // data, array types
264 printf( "\n" );
265
266 amemset( sta, fill, dim ); // CFA array memset, type safe
267 printf( "CFA array memset\n" );
268 for ( i; dim ) { printf( "%#x %a, ", sta[i].x, sta[i].y ); }
269 printf( "\n" );
270
271 amemcpy( sta1, sta, dim ); // CFA array memcpy, type safe
272 printf( "CFA array memcpy\n" );
273 for ( i; dim ) { printf( "%#x %a, ", sta1[i].x, sta1[i].y ); }
274 printf( "\n" );
275
276
277 // new, non-array types
278 printf( "\n" );
279
280 stp = new( 42, 42.5 );
281 stp1 = new( 42, 42.5 );
282 printf( "CFA new initialize\n%d %g %d %g\n", stp->x, stp->y, stp1->x, stp1->y );
283 delete( stp, stp1 );
284
285 // new, array types
286 stp = anew( dim, 42, 42.5 );
287 printf( "CFA array new initialize\n" );
288 for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
289 printf( "\n" );
290 stp1 = anew( dim, 42, 42.5 );
291 for ( i; dim ) { printf( "%d %g, ", stp1[i].x, stp1[i].y ); }
292 printf( "\n" );
293 adelete( dim, stp, dim, stp1 );
294
295 // extras
296 printf( "\n" );
297
298 float * fp = malloc() + 1;
299 printf( "pointer arithmetic %d\n", fp == fp - 1 );
300 free( fp - 1 );
301
302 p = foo( bar( baz( malloc(), 0 ), 0 ), 0 );
303 *p = 0xdeadbeef;
304 printf( "CFA deep malloc %#x\n", *p );
305 free( p );
306
307#ifdef ERR1
308 stp = malloc();
309 printf( "\nSHOULD FAIL\n" );
310 p = realloc( stp, dim * sizeof( *stp ) );
311 p = alloc( stp, dim * sizeof( *stp ) );
312 p = memset( stp, 10 );
313 p = memcpy( &st1, &st );
314#endif
315} // main
316
317// Local Variables: //
318// tab-width: 4 //
319// compile-command: "cfa alloc.cfa" //
320// End: //
Note: See TracBrowser for help on using the repository browser.