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 : Fri Oct 14 09:31:39 2022 |
---|
13 | // Update Count : 491 |
---|
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 | |
---|
23 | int * foo( int * p, int c ) { return p; } |
---|
24 | int * bar( int * p, int c ) { return p; } |
---|
25 | int * baz( int * p, int c ) { return p; } |
---|
26 | |
---|
27 | int 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 | sout | "C malloc" | hex(*ip); |
---|
37 | free( ip ); |
---|
38 | |
---|
39 | ip = malloc(); // CFA malloc, type safe |
---|
40 | *ip = 0xdeadbeef; |
---|
41 | sout | "CFA malloc" | hex(*ip); |
---|
42 | free( ip ); |
---|
43 | |
---|
44 | ip = alloc(); // CFA alloc, type safe |
---|
45 | *ip = 0xdeadbeef; |
---|
46 | sout | "CFA alloc" | hex(*ip); |
---|
47 | free( ip ); |
---|
48 | |
---|
49 | ip = alloc( fill`fill ); // CFA alloc, fill |
---|
50 | sout | "CFA alloc, fill" | wd(8, nobase(hex(*ip))); |
---|
51 | free( ip ); |
---|
52 | |
---|
53 | ip = alloc( 3`fill ); // CFA alloc, fill |
---|
54 | sout | "CFA alloc, fill" | *ip; |
---|
55 | free( ip ); |
---|
56 | |
---|
57 | |
---|
58 | // allocation, array types |
---|
59 | sout | nl; |
---|
60 | |
---|
61 | ip = (int *)calloc( dim, sizeof( *ip ) ); // C array calloc, type unsafe |
---|
62 | sout | "C array calloc, fill 0"; |
---|
63 | for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; } |
---|
64 | sout | nl; |
---|
65 | free( ip ); |
---|
66 | |
---|
67 | ip = calloc( dim ); // CFA array calloc, type safe |
---|
68 | sout | "CFA array calloc, fill 0"; |
---|
69 | for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; } |
---|
70 | sout | nl; |
---|
71 | free( ip ); |
---|
72 | |
---|
73 | ip = alloc( dim ); // CFA array alloc, type safe |
---|
74 | for ( i; dim ) { ip[i] = 0xdeadbeef; } |
---|
75 | sout | "CFA array alloc, no fill"; |
---|
76 | for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; } |
---|
77 | sout | nl; |
---|
78 | free( ip ); |
---|
79 | |
---|
80 | ip = alloc( 2 * dim, fill`fill ); // CFA array alloc, fill |
---|
81 | sout | "CFA array alloc, fill" | hex(fill); |
---|
82 | for ( i; 2 * dim ) { sout | hex(ip[i]) | ' ' | nonl; } |
---|
83 | sout | nl; |
---|
84 | free( ip ); |
---|
85 | |
---|
86 | ip = alloc( 2 * dim, ((int)0xdeadbeef)`fill ); // CFA array alloc, fill |
---|
87 | sout | "CFA array alloc, fill" | hex((char)0xdeadbeef); |
---|
88 | for ( i; 2 * dim ) { sout | hex(ip[i]) | ' ' | nonl; } |
---|
89 | sout | nl; |
---|
90 | // do not free |
---|
91 | |
---|
92 | ip1 = alloc( 2 * dim, [ip, 2 * dim]`fill ); // CFA array alloc, fill |
---|
93 | sout | "CFA array alloc, fill from array"; |
---|
94 | for ( i; 2 * dim ) { sout | hex(ip[i]) | hex(ip1[i]) | ", " | nonl; } |
---|
95 | free( ip1 ); |
---|
96 | sout | nl; |
---|
97 | |
---|
98 | |
---|
99 | // realloc, non-array types |
---|
100 | sout | nl; |
---|
101 | |
---|
102 | ip = (int *)realloc( ip, dim * sizeof(*ip) ); // C realloc |
---|
103 | sout | "C realloc"; |
---|
104 | for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; } |
---|
105 | sout | nl; |
---|
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 | sout | "CFA realloc"; |
---|
111 | for ( i; 2 * dim ) { sout | hex(ip[i]) | ' ' | nonl; } |
---|
112 | sout | nl; |
---|
113 | // do not free |
---|
114 | |
---|
115 | |
---|
116 | // realloc, array types |
---|
117 | sout | nl; |
---|
118 | |
---|
119 | ip = alloc( dim, ip`realloc ); // CFA realloc array alloc |
---|
120 | for ( i; dim ) { ip[i] = 0xdeadbeef; } |
---|
121 | sout | "CFA realloc array alloc"; |
---|
122 | for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; } |
---|
123 | sout | nl; |
---|
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 | sout | "CFA realloc array alloc"; |
---|
129 | for ( i; 2 * dim ) { sout | hex(ip[i]) | ' ' | nonl; } |
---|
130 | sout | nl; |
---|
131 | // do not free |
---|
132 | |
---|
133 | ip = alloc( dim, ip`realloc ); // CFA realloc array alloc |
---|
134 | sout | "CFA realloc array alloc"; |
---|
135 | for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; } |
---|
136 | sout | nl; |
---|
137 | // do not free |
---|
138 | |
---|
139 | ip = alloc( 3 * dim, ip`realloc, fill`fill ); // CFA realloc array alloc, fill |
---|
140 | sout | "CFA realloc array alloc, fill"; |
---|
141 | for ( i; 3 * dim ) { sout | hex(ip[i]) | ' ' | nonl; } |
---|
142 | sout | nl; |
---|
143 | // do not free |
---|
144 | |
---|
145 | ip = alloc( dim, ip`realloc, fill`fill ); // CFA realloc array alloc, fill |
---|
146 | sout | "CFA realloc array alloc, fill"; |
---|
147 | for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; } |
---|
148 | sout | nl; |
---|
149 | // do not free |
---|
150 | |
---|
151 | ip = alloc( 3 * dim, ip`realloc, fill`fill ); // CFA realloc array alloc, fill |
---|
152 | sout | "CFA realloc array alloc, fill"; |
---|
153 | for ( i; 3 * dim ) { sout | hex(ip[i]) | ' ' | nonl; } |
---|
154 | sout | nl; |
---|
155 | // do not free |
---|
156 | |
---|
157 | ip = alloc( 5 * dim, ip`realloc, 5`fill ); // CFA realloc array alloc, 5 |
---|
158 | sout | "CFA realloc array alloc, 5"; |
---|
159 | for ( i; 5 * dim ) { sout | hex(ip[i]) | ' ' | nonl; } |
---|
160 | sout | nl; |
---|
161 | // do not free |
---|
162 | |
---|
163 | ip = alloc( dim, ip`realloc, 5`fill ); // CFA realloc array alloc, 5 |
---|
164 | sout | "CFA realloc array alloc, 5"; |
---|
165 | for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; } |
---|
166 | sout | nl; |
---|
167 | // do not free |
---|
168 | |
---|
169 | ip = alloc( 5 * dim, ip`realloc, 5`fill ); // CFA realloc array alloc, 5 |
---|
170 | sout | "CFA realloc array alloc, 5"; |
---|
171 | for ( i; 5 * dim ) { sout | hex(ip[i]) | ' ' | nonl; } |
---|
172 | sout | nl; |
---|
173 | |
---|
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 | sout | nl; |
---|
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 | sout | "C memalign " | 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 | sout | "CFA memalign" | 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 | sout | "CFA posix_memalign" | 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 | sout | "CFA posix_memalign" | 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 | sout | "CFA alloc_align" | 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 | sout | "CFA alloc_align" | 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 | sout | "CFA alloc_align fill" | hex(stp->x) | hex(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 | sout | "CFA alloc_align fill" | 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 | sout | "CFA alloc_align" | stp->x | stp->y; |
---|
271 | free( stp ); |
---|
272 | |
---|
273 | |
---|
274 | // alignment, array types |
---|
275 | sout | nl; |
---|
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 | sout | "CFA array alloc_align"; |
---|
281 | for ( i; dim ) { sout | stp[i].x | stp[i].y | ", " | nonl; } |
---|
282 | sout | nl; |
---|
283 | free( stp ); |
---|
284 | |
---|
285 | stp = alloc( dim, Alignment`align, fill`fill ); // CFA array memalign, fill |
---|
286 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
287 | sout | "CFA array alloc_align, fill"; |
---|
288 | for ( i; dim ) { sout | hex(stp[i].x) | hex(stp[i].y) | ", " | nonl; } |
---|
289 | sout | nl; |
---|
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 | sout | "CFA array alloc_align, fill"; |
---|
295 | for ( i; dim ) { sout | stp[i].x | stp[i].y | ", " | nonl; } |
---|
296 | sout | nl; |
---|
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 | sout | "CFA array alloc_align, fill array"; |
---|
302 | for ( i; dim ) { sout | stp1[i].x | stp1[i].y | ", " | nonl; } |
---|
303 | sout | nl; |
---|
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 | sout | "CFA realloc array alloc_align"; |
---|
310 | for ( i; dim ) { sout | stp[i].x | stp[i].y | ", " | nonl; } |
---|
311 | sout | nl; |
---|
312 | free( stp ); |
---|
313 | |
---|
314 | |
---|
315 | // data, non-array types |
---|
316 | sout | nl; |
---|
317 | |
---|
318 | memset( &st, fill ); // CFA memset, type safe |
---|
319 | sout | "CFA memset" | hex(st.x) | hex(st.y); |
---|
320 | memcpy( &st1, &st ); // CFA memcpy, type safe |
---|
321 | sout | "CFA memcpy" | hex(st1.x) | hex(st1.y); |
---|
322 | |
---|
323 | |
---|
324 | // data, array types |
---|
325 | sout | nl; |
---|
326 | |
---|
327 | amemset( sta, fill, dim ); // CFA array memset, type safe |
---|
328 | sout | "CFA array memset"; |
---|
329 | for ( i; dim ) { sout | hex(sta[i].x) | hex(sta[i].y) | ", " | nonl; } |
---|
330 | sout | nl; |
---|
331 | |
---|
332 | amemcpy( sta1, sta, dim ); // CFA array memcpy, type safe |
---|
333 | sout | "CFA array memcpy"; |
---|
334 | for ( i; dim ) { sout | hex(sta1[i].x) | hex(sta1[i].y) | ", " | nonl; } |
---|
335 | sout | nl; |
---|
336 | |
---|
337 | // new, non-array types |
---|
338 | sout | nl; |
---|
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 | sout | "CFA new initialize" | nl | 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 | sout | "CFA array new initialize"; |
---|
354 | for ( i; dim ) { sout | stp[i].x | stp[i].y | ", " | nonl; } |
---|
355 | sout | nl; |
---|
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 ) { sout | stp1[i].x | stp1[i].y | ", " | nonl; } |
---|
360 | sout | nl; |
---|
361 | adelete( stp, stp1 ); |
---|
362 | assert( const_count == 2 + 2 * dim && dest_count == 2 + 2 * dim); // assertion for testing |
---|
363 | |
---|
364 | // extras |
---|
365 | sout | nl; |
---|
366 | |
---|
367 | float * fp = malloc() + 1; |
---|
368 | sout | "pointer arithmetic" | fp == fp - 1; |
---|
369 | free( fp - 1 ); |
---|
370 | |
---|
371 | ip = foo( bar( baz( malloc(), 0 ), 0 ), 0 ); |
---|
372 | *ip = 0xdeadbeef; |
---|
373 | sout | "CFA deep malloc" | hex(*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 | sout | "\nSHOULD FAIL"; |
---|
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: // |
---|