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