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 : Mon Apr 6 20:35:28 2020 |
---|
13 | // Update Count : 427 |
---|
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 | 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_set( fill ); // CFA alloc, fill |
---|
50 | printf( "CFA alloc, fill %08x\n", *ip ); |
---|
51 | free( ip ); |
---|
52 | |
---|
53 | ip = alloc_set( 3 ); // 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_set( 2 * dim, 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_set( 2 * dim, 0xdeadbeef ); // 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_set( 2 * dim, ip ); // 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( 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] ); } |
---|
136 | printf( "\n" ); |
---|
137 | // do not free |
---|
138 | |
---|
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] ); } |
---|
142 | printf( "\n" ); |
---|
143 | // do not free |
---|
144 | |
---|
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] ); } |
---|
148 | printf( "\n" ); |
---|
149 | // do not free |
---|
150 | |
---|
151 | ip = alloc_set( ip, 3 * dim, 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_set( ip, 5 * dim, 5 ); // 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_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] ); } |
---|
166 | printf( "\n" ); |
---|
167 | // do not free |
---|
168 | |
---|
169 | ip = alloc_set( ip, 5 * dim, 5 ); // 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 | free( ip ); |
---|
174 | #endif // 0 |
---|
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 ); |
---|
208 | |
---|
209 | |
---|
210 | struct Struct { int x; double y; }; |
---|
211 | Struct st, st1, sta[dim], sta1[dim], * stp, * stp1; |
---|
212 | |
---|
213 | // alignment, non-array types |
---|
214 | printf( "\n" ); |
---|
215 | enum { Alignment = 128 }; |
---|
216 | |
---|
217 | stp = &(*(Struct*)memalign( Alignment, sizeof( *stp ) ) ){ 42, 42.5 }; // C memalign |
---|
218 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
219 | printf( "C memalign %d %g\n", stp->x, stp->y ); |
---|
220 | free( stp ); |
---|
221 | |
---|
222 | stp = &(*memalign( Alignment )){ 42, 42.5 }; // CFA memalign |
---|
223 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
224 | printf( "CFA memalign %d %g\n", stp->x, stp->y ); |
---|
225 | free( stp ); |
---|
226 | |
---|
227 | posix_memalign( (void **)&stp, Alignment, sizeof( *stp ) ); // C posix_memalign |
---|
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 ); |
---|
231 | free( stp ); |
---|
232 | |
---|
233 | posix_memalign( &stp, Alignment ); // CFA posix_memalign |
---|
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 ); |
---|
237 | free( stp ); |
---|
238 | |
---|
239 | stp = &(*alloc_align( Alignment)){ 42, 42.5 }; // CFA alloc_align |
---|
240 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
241 | printf( "CFA alloc_align %d %g\n", stp->x, stp->y ); |
---|
242 | free( stp ); |
---|
243 | |
---|
244 | stp = &(*alloc_align( Alignment )){ 42, 42.5 }; // CFA alloc_align |
---|
245 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
246 | printf( "CFA alloc_align %d %g\n", stp->x, stp->y ); |
---|
247 | free( stp ); |
---|
248 | |
---|
249 | stp = alloc_align_set( Alignment, fill ); // CFA memalign, fill |
---|
250 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
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 ); |
---|
262 | free( stp ); |
---|
263 | |
---|
264 | |
---|
265 | // alignment, array types |
---|
266 | printf( "\n" ); |
---|
267 | |
---|
268 | stp = alloc_align( Alignment, dim ); // CFA array memalign |
---|
269 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
270 | for ( i; dim ) { stp[i] = (Struct){ 42, 42.5 }; } |
---|
271 | printf( "CFA array alloc_align\n" ); |
---|
272 | for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); } |
---|
273 | printf( "\n" ); |
---|
274 | free( stp ); |
---|
275 | |
---|
276 | stp = alloc_align_set( Alignment, dim, fill ); // CFA array memalign, fill |
---|
277 | assert( (uintptr_t)stp % Alignment == 0 ); |
---|
278 | printf( "CFA array alloc_align, fill\n" ); |
---|
279 | for ( i; dim ) { printf( "%#x %a, ", stp[i].x, stp[i].y ); } |
---|
280 | printf( "\n" ); |
---|
281 | free( stp ); |
---|
282 | |
---|
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 | |
---|
305 | |
---|
306 | // data, non-array types |
---|
307 | printf( "\n" ); |
---|
308 | |
---|
309 | memset( &st, fill ); // CFA memset, type safe |
---|
310 | printf( "CFA memset %#x %a\n", st.x, st.y ); |
---|
311 | memcpy( &st1, &st ); // CFA memcpy, type safe |
---|
312 | printf( "CFA memcpy %#x %a\n", st1.x, st1.y ); |
---|
313 | |
---|
314 | |
---|
315 | // data, array types |
---|
316 | printf( "\n" ); |
---|
317 | |
---|
318 | amemset( sta, fill, dim ); // CFA array memset, type safe |
---|
319 | printf( "CFA array memset\n" ); |
---|
320 | for ( i; dim ) { printf( "%#x %a, ", sta[i].x, sta[i].y ); } |
---|
321 | printf( "\n" ); |
---|
322 | |
---|
323 | amemcpy( sta1, sta, dim ); // CFA array memcpy, type safe |
---|
324 | printf( "CFA array memcpy\n" ); |
---|
325 | for ( i; dim ) { printf( "%#x %a, ", sta1[i].x, sta1[i].y ); } |
---|
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" ); |
---|
340 | for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); } |
---|
341 | printf( "\n" ); |
---|
342 | stp1 = anew( dim, 42, 42.5 ); |
---|
343 | for ( i; dim ) { printf( "%d %g, ", stp1[i].x, stp1[i].y ); } |
---|
344 | printf( "\n" ); |
---|
345 | adelete( dim, stp, dim, stp1 ); |
---|
346 | |
---|
347 | // extras |
---|
348 | printf( "\n" ); |
---|
349 | |
---|
350 | float * fp = malloc() + 1; |
---|
351 | printf( "pointer arithmetic %d\n", fp == fp - 1 ); |
---|
352 | free( fp - 1 ); |
---|
353 | |
---|
354 | ip = foo( bar( baz( malloc(), 0 ), 0 ), 0 ); |
---|
355 | *ip = 0xdeadbeef; |
---|
356 | printf( "CFA deep malloc %#x\n", *ip ); |
---|
357 | free( ip ); |
---|
358 | |
---|
359 | #ifdef ERR1 |
---|
360 | stp = malloc(); |
---|
361 | printf( "\nSHOULD FAIL\n" ); |
---|
362 | ip = realloc( stp, dim * sizeof( *stp ) ); |
---|
363 | ip = memset( stp, 10 ); |
---|
364 | ip = memcpy( &st1, &st ); |
---|
365 | #endif |
---|
366 | } // main |
---|
367 | |
---|
368 | // Local Variables: // |
---|
369 | // tab-width: 4 // |
---|
370 | // compile-command: "cfa alloc.cfa" // |
---|
371 | // End: // |
---|