source: tests/alloc.cfa @ 66812dd

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 66812dd was 191a190, checked in by m3zulfiq <m3zulfiq@…>, 4 years ago

Removed a fill bug from alloc interface, changed pervious alloc tests (alloc.cfa) to comply with new alloc interface, added new tests for memory allocation (malloc.cfa and alloc2.cfa).

  • Property mode set to 100644
File size: 10.7 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 : Fri Aug 14 16:59:59 2020
13// Update Count     : 430
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        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( Alignment`align)){ 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( Alignment`align )){ 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( Alignment`align, fill`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( Alignment`align, (Struct){ 42, 42.5 }`fill ); // 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( stp`realloc, 4096`align )){ 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( dim, Alignment`align );                // 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( dim, Alignment`align, fill`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( dim, Alignment`align, ((Struct){ 42, 42.5 })`fill ); // 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( dim, Alignment`align, [stp, dim]`fill );  // 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( dim, stp`realloc, 4096`align );                            // 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        // new, non-array types
329        printf( "\n" );
330
331        stp = new( 42, 42.5 );
332        stp1 = new( 42, 42.5 );
333        printf( "CFA new initialize\n%d %g %d %g\n", stp->x, stp->y, stp1->x, stp1->y );
334        delete( stp, stp1 );
335
336        // new, array types
337        stp = anew( dim, 42, 42.5 );
338        printf( "CFA array new initialize\n" );
339        for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
340        printf( "\n" );
341        stp1 = anew( dim, 42, 42.5 );
342        for ( i; dim ) { printf( "%d %g, ", stp1[i].x, stp1[i].y ); }
343        printf( "\n" );
344        adelete( dim, stp, dim, stp1 );
345
346        // extras
347        printf( "\n" );
348
349        float * fp = malloc() + 1;
350        printf( "pointer arithmetic %d\n", fp == fp - 1 );
351        free( fp - 1 );
352
353        ip = foo( bar( baz( malloc(), 0 ), 0 ), 0 );
354        *ip = 0xdeadbeef;
355        printf( "CFA deep malloc %#x\n", *ip );
356        free( ip );
357
358#ifdef ERR1
359        stp = malloc();
360        printf( "\nSHOULD FAIL\n" );
361        ip = realloc( stp, dim * sizeof( *stp ) );
362        ip = memset( stp, 10 );
363        ip = memcpy( &st1, &st );
364#endif
365} // main
366
367// Local Variables: //
368// tab-width: 4 //
369// compile-command: "cfa alloc.cfa" //
370// End: //
Note: See TracBrowser for help on using the repository browser.