source: tests/alloc.cfa @ 657c36f

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 657c36f was fe23950, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

argument test of free with multiple values

  • Property mode set to 100644
File size: 11.5 KB
RevLine 
[2afec66]1//
[fab700b]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//
[dc8511c]7// alloc.cfa --
[2afec66]8//
[fab700b]9// Author           : Peter A. Buhr
10// Created On       : Wed Feb  3 07:56:22 2016
11// Last Modified By : Peter A. Buhr
[fe23950]12// Last Modified On : Thu Nov 12 10:02:18 2020
13// Update Count     : 432
[2afec66]14//
[fab700b]15
[91c389a]16#include <assert.h>
[fab700b]17#include <malloc.h>                                                                             // malloc_usable_size
18#include <stdint.h>                                                                             // uintptr_t
[6065b3aa]19#include <stdlib.h>                                                                             // posix_memalign
[73abe95]20#include <fstream.hfa>
[eb5a115]21#include <stdlib.hfa>                                                                   // access C malloc, realloc
[fab700b]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 ) {
[2afec66]28        size_t dim = 10;
[eb5a115]29        char fill = '\xde';
[cfbc703d]30        int * ip, * ip1;
[fab700b]31
32        // allocation, non-array types
33
[cfbc703d]34        ip = (int *)malloc( sizeof(*ip) );                                      // C malloc, type unsafe
35        *ip = 0xdeadbeef;
36        printf( "C   malloc %#x\n", *ip );
37        free( ip );
[fab700b]38
[cfbc703d]39        ip = malloc();                                                                          // CFA malloc, type safe
40        *ip = 0xdeadbeef;
41        printf( "CFA malloc %#x\n", *ip );
42        free( ip );
[6065b3aa]43
[cfbc703d]44        ip = alloc();                                                                           // CFA alloc, type safe
45        *ip = 0xdeadbeef;
46        printf( "CFA alloc %#x\n", *ip );
47        free( ip );
[fab700b]48
[191a190]49        ip = alloc( fill`fill );                                                                // CFA alloc, fill
[cfbc703d]50        printf( "CFA alloc, fill %08x\n", *ip );
51        free( ip );
[fab700b]52
[191a190]53        ip = alloc( 3`fill );                                                           // CFA alloc, fill
[cfbc703d]54        printf( "CFA alloc, fill %d\n", *ip );
55        free( ip );
[eb5a115]56
[fab700b]57
58        // allocation, array types
59        printf( "\n" );
60
[cfbc703d]61        ip = (int *)calloc( dim, sizeof( *ip ) );                       // C array calloc, type unsafe
[6065b3aa]62        printf( "C   array calloc, fill 0\n" );
[cfbc703d]63        for ( i; dim ) { printf( "%#x ", ip[i] ); }
[fab700b]64        printf( "\n" );
[cfbc703d]65        free( ip );
[fab700b]66
[cfbc703d]67        ip = calloc( dim );                                                                     // CFA array calloc, type safe
[6065b3aa]68        printf( "CFA array calloc, fill 0\n" );
[cfbc703d]69        for ( i; dim ) { printf( "%#x ", ip[i] ); }
[fab700b]70        printf( "\n" );
[cfbc703d]71        free( ip );
[fab700b]72
[cfbc703d]73        ip = alloc( dim );                                                                      // CFA array alloc, type safe
74        for ( i; dim ) { ip[i] = 0xdeadbeef; }
[6065b3aa]75        printf( "CFA array alloc, no fill\n" );
[cfbc703d]76        for ( i; dim ) { printf( "%#x ", ip[i] ); }
[fab700b]77        printf( "\n" );
[cfbc703d]78        free( ip );
[fab700b]79
[191a190]80        ip = alloc( 2 * dim, fill`fill );                                       // CFA array alloc, fill
[6ea0408]81        printf( "CFA array alloc, fill %#hhx\n", fill );
[cfbc703d]82        for ( i; 2 * dim ) { printf( "%#x ", ip[i] ); }
[fab700b]83        printf( "\n" );
[cfbc703d]84        free( ip );
[eb5a115]85
[191a190]86        ip = alloc( 2 * dim, ((int)0xdeadbeef)`fill );                          // CFA array alloc, fill
[eb5a115]87        printf( "CFA array alloc, fill %#hhx\n", 0xdeadbeef );
[cfbc703d]88        for ( i; 2 * dim ) { printf( "%#x ", ip[i] ); }
[eb5a115]89        printf( "\n" );
[fab700b]90        // do not free
91
[191a190]92        ip1 = alloc( 2 * dim, [ip, 2 * dim]`fill );                             // CFA array alloc, fill
[eb5a115]93        printf( "CFA array alloc, fill from array\n" );
[cfbc703d]94        for ( i; 2 * dim ) { printf( "%#x %#x, ", ip[i], ip1[i] ); }
95        free( ip1 );
[eb5a115]96        printf( "\n" );
97
[fab700b]98
[cfbc703d]99        // realloc, non-array types
[fab700b]100        printf( "\n" );
101
[cfbc703d]102        ip = (int *)realloc( ip, dim * sizeof(*ip) );           // C realloc
[eb5a115]103        printf( "C realloc\n" );
[cfbc703d]104        for ( i; dim ) { printf( "%#x ", ip[i] ); }
[fab700b]105        printf( "\n" );
[eb5a115]106        // do not free
[fab700b]107
[cfbc703d]108        ip = realloc( ip, 2 * dim * sizeof(*ip) );                      // CFA realloc
109        for ( i; dim ~ 2 * dim ) { ip[i] = 0x1010101; }
[a4683611]110        printf( "CFA realloc\n" );
[cfbc703d]111        for ( i; 2 * dim ) { printf( "%#x ", ip[i] ); }
[fab700b]112        printf( "\n" );
[6065b3aa]113        // do not free
[fab700b]114
115
[cfbc703d]116        // realloc, array types
117        printf( "\n" );
118
[191a190]119        ip = alloc( dim, ip`realloc );                                                          // CFA realloc array alloc
[cfbc703d]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
[191a190]126        ip = alloc( 2 * dim, ip`realloc );                                                      // CFA realloc array alloc
[cfbc703d]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
[191a190]133        ip = alloc( dim, ip`realloc );                                                          // CFA realloc array alloc
[cfbc703d]134        printf( "CFA realloc array alloc\n" );
135        for ( i; dim ) { printf( "%#x ", ip[i] ); }
[fab700b]136        printf( "\n" );
[cfbc703d]137        // do not free
[fab700b]138
[191a190]139        ip = alloc( 3 * dim, ip`realloc, fill`fill );                           // CFA realloc array alloc, fill
[cfbc703d]140        printf( "CFA realloc array alloc, fill\n" );
141        for ( i; 3 * dim ) { printf( "%#x ", ip[i] ); }
[fab700b]142        printf( "\n" );
[eb5a115]143        // do not free
[fab700b]144
[191a190]145        ip = alloc( dim, ip`realloc, fill`fill );                                       // CFA realloc array alloc, fill
[cfbc703d]146        printf( "CFA realloc array alloc, fill\n" );
147        for ( i; dim ) { printf( "%#x ", ip[i] ); }
[fab700b]148        printf( "\n" );
[eb5a115]149        // do not free
[fab700b]150
[191a190]151        ip = alloc( 3 * dim, ip`realloc, fill`fill );                           // CFA realloc array alloc, fill
[cfbc703d]152        printf( "CFA realloc array alloc, fill\n" );
[23ecea4]153        for ( i; 3 * dim ) { printf( "%#x ", ip[i] ); }
[fab700b]154        printf( "\n" );
[eb5a115]155        // do not free
[23ecea4]156#if 0 // FIX ME
[191a190]157        ip = alloc( 5 * dim, ip`realloc, 5`fill );                                      // CFA realloc array alloc, 5
[cfbc703d]158        printf( "CFA realloc array alloc, 5\n" );
[23ecea4]159        for ( i; 5 * dim ) { printf( "%#x ", ip[i] ); }
[fab700b]160        printf( "\n" );
[eb5a115]161        // do not free
[fab700b]162
[191a190]163        ip = alloc( dim, ip`realloc, 5`fill );                                          // CFA realloc array alloc, 5
[cfbc703d]164        printf( "CFA realloc array alloc, 5\n" );
165        for ( i; dim ) { printf( "%#x ", ip[i] ); }
[fab700b]166        printf( "\n" );
[eb5a115]167        // do not free
[fab700b]168
[191a190]169        ip = alloc( 5 * dim, ip`realloc, 5`fill );                                      // CFA realloc array alloc, 5
[cfbc703d]170        printf( "CFA realloc array alloc, 5\n" );
[23ecea4]171        for ( i; 5 * dim ) { printf( "%#x ", ip[i] ); }
[fab700b]172        printf( "\n" );
[23ecea4]173#endif // 0
[9d5d01f]174        free( ip );
[cfbc703d]175
176        // resize, non-array types
177
178        struct S {
179                int a[5];
180        };
181
182    ip = alloc();
183        *ip = 5;
[191a190]184    double * dp = alloc( ip`resize );
[cfbc703d]185        *dp = 5.5;
[191a190]186    S * sp = alloc( dp`resize );
[cfbc703d]187        *sp = (S){ {0, 1, 2, 3, 4} };
[191a190]188    ip = alloc( sp`resize );
[cfbc703d]189        *ip = 3;
190    free( ip );
191
192
193        // resize, array types
194
195    ip = alloc( 5 );
196        for ( i; 5 ) { ip[i] = 5; }
[191a190]197    dp = alloc( 5, ip`resize );
[cfbc703d]198        for ( i; 5 ) { dp[i] = 5.5; }
[191a190]199    sp = alloc( 5, dp`resize );
[cfbc703d]200        for ( i; 5 ) { sp[i] = (S){ {0, 1, 2, 3, 4} }; }
[191a190]201    ip = alloc( 3, sp`resize );
[cfbc703d]202        for ( i; 3 ) { ip[i] = 3; }
[191a190]203    ip = alloc( 7, ip`realloc );
[cfbc703d]204        for ( i; 7 ) { ip[i] = 7; }
[191a190]205    ip = alloc( 7, ip`resize );
[cfbc703d]206        for ( i; 7 ) { ip[i] = 7; }
207    free( ip );
[fab700b]208
209
[55acc3a]210        int const_count, dest_count;
[2afec66]211        struct Struct { int x; double y; };
[55acc3a]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
[2afec66]220        Struct st, st1, sta[dim], sta1[dim], * stp, * stp1;
[fab700b]221
222        // alignment, non-array types
223        printf( "\n" );
224        enum { Alignment = 128 };
[6065b3aa]225
[6cfe8bb]226        stp = &(*(Struct*)memalign( Alignment, sizeof( *stp ) ) ){ 42, 42.5 }; // C memalign
[fab700b]227        assert( (uintptr_t)stp % Alignment == 0 );
[6065b3aa]228        printf( "C   memalign %d %g\n", stp->x, stp->y );
[2afec66]229        free( stp );
[fab700b]230
[cfbc703d]231        stp = &(*memalign( Alignment )){ 42, 42.5 };            // CFA memalign
[fab700b]232        assert( (uintptr_t)stp % Alignment == 0 );
233        printf( "CFA memalign %d %g\n", stp->x, stp->y );
[2afec66]234        free( stp );
[fab700b]235
[2afec66]236        posix_memalign( (void **)&stp, Alignment, sizeof( *stp ) ); // C posix_memalign
[6065b3aa]237        *stp = (Struct){ 42, 42.5 };
238        assert( (uintptr_t)stp % Alignment == 0 );
239        printf( "CFA posix_memalign %d %g\n", stp->x, stp->y );
[2afec66]240        free( stp );
[6065b3aa]241
[2afec66]242        posix_memalign( &stp, Alignment );                  // CFA posix_memalign
[fab700b]243        *stp = (Struct){ 42, 42.5 };
244        assert( (uintptr_t)stp % Alignment == 0 );
245        printf( "CFA posix_memalign %d %g\n", stp->x, stp->y );
[2afec66]246        free( stp );
[fab700b]247
[191a190]248        stp = &(*alloc( Alignment`align)){ 42, 42.5 };          // CFA alloc_align
[fab700b]249        assert( (uintptr_t)stp % Alignment == 0 );
[eb5a115]250        printf( "CFA alloc_align %d %g\n", stp->x, stp->y );
[2afec66]251        free( stp );
[6065b3aa]252
[191a190]253        stp = &(*alloc( Alignment`align )){ 42, 42.5 };         // CFA alloc_align
[6065b3aa]254        assert( (uintptr_t)stp % Alignment == 0 );
[eb5a115]255        printf( "CFA alloc_align %d %g\n", stp->x, stp->y );
[2afec66]256        free( stp );
[6065b3aa]257
[191a190]258        stp = alloc( Alignment`align, fill`fill );                      // CFA memalign, fill
[6065b3aa]259        assert( (uintptr_t)stp % Alignment == 0 );
[eb5a115]260        printf( "CFA alloc_align fill %#x %a\n", stp->x, stp->y );
261        free( stp );
262
[191a190]263        stp = alloc( Alignment`align, (Struct){ 42, 42.5 }`fill ); // CFA memalign, fill
[eb5a115]264        assert( (uintptr_t)stp % Alignment == 0 );
265        printf( "CFA alloc_align fill %d %g\n", stp->x, stp->y );
266        // do not free
267
[191a190]268        stp = &(*alloc( stp`realloc, 4096`align )){ 42, 42.5 };         // CFA realign
[eb5a115]269        assert( (uintptr_t)stp % 4096 == 0 );
270        printf( "CFA alloc_align %d %g\n", stp->x, stp->y );
[2afec66]271        free( stp );
[fab700b]272
273
274        // alignment, array types
275        printf( "\n" );
276
[191a190]277        stp = alloc( dim, Alignment`align );                // CFA array memalign
[fab700b]278        assert( (uintptr_t)stp % Alignment == 0 );
[8f34661]279        for ( i; dim ) { stp[i] = (Struct){ 42, 42.5 }; }
[eb5a115]280        printf( "CFA array alloc_align\n" );
[8f34661]281        for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
[fab700b]282        printf( "\n" );
[2afec66]283        free( stp );
[fab700b]284
[191a190]285        stp = alloc( dim, Alignment`align, fill`fill );         // CFA array memalign, fill
[fab700b]286        assert( (uintptr_t)stp % Alignment == 0 );
[eb5a115]287        printf( "CFA array alloc_align, fill\n" );
[8f34661]288        for ( i; dim ) { printf( "%#x %a, ", stp[i].x, stp[i].y ); }
[fab700b]289        printf( "\n" );
[2afec66]290        free( stp );
[fab700b]291
[191a190]292        stp = alloc( dim, Alignment`align, ((Struct){ 42, 42.5 })`fill ); // CFA array memalign, fill
[eb5a115]293        assert( (uintptr_t)stp % Alignment == 0 );
294        printf( "CFA array alloc_align, fill\n" );
295        for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
296        printf( "\n" );
297        // do not free
298
[191a190]299        stp1 = alloc( dim, Alignment`align, [stp, dim]`fill );  // CFA array memalign, fill
[eb5a115]300        assert( (uintptr_t)stp % Alignment == 0 );
301        printf( "CFA array alloc_align, fill array\n" );
302        for ( i; dim ) { printf( "%d %g, ", stp1[i].x, stp1[i].y ); }
303        printf( "\n" );
304        free( stp1 );
305
[191a190]306        stp = alloc( dim, stp`realloc, 4096`align );                            // CFA aligned realloc array
[eb5a115]307        assert( (uintptr_t)stp % 4096 == 0 );
308        for ( i; dim ) { stp[i] = (Struct){ 42, 42.5 }; }
309        printf( "CFA realloc array alloc_align\n" );
310        for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
311        printf( "\n" );
312        free( stp );
313
[fab700b]314
315        // data, non-array types
316        printf( "\n" );
317
[2afec66]318        memset( &st, fill );                                // CFA memset, type safe
[6065b3aa]319        printf( "CFA memset %#x %a\n", st.x, st.y );
[2afec66]320        memcpy( &st1, &st );                                // CFA memcpy, type safe
[6065b3aa]321        printf( "CFA memcpy %#x %a\n", st1.x, st1.y );
[fab700b]322
323
324        // data, array types
325        printf( "\n" );
326
[b9c04946]327        amemset( sta, fill, dim );                                                      // CFA array memset, type safe
[fab700b]328        printf( "CFA array memset\n" );
[8f34661]329        for ( i; dim ) { printf( "%#x %a, ", sta[i].x, sta[i].y ); }
[fab700b]330        printf( "\n" );
331
[b9c04946]332        amemcpy( sta1, sta, dim );                                                      // CFA array memcpy, type safe
333        printf( "CFA array memcpy\n" );
[8f34661]334        for ( i; dim ) { printf( "%#x %a, ", sta1[i].x, sta1[i].y ); }
[6065b3aa]335        printf( "\n" );
336
337        // new, non-array types
338        printf( "\n" );
339
[55acc3a]340        const_count = dest_count = 0;
[6065b3aa]341        stp = new( 42, 42.5 );
[55acc3a]342        assert( const_count == 1 && dest_count == 0 );                                          // assertion for testing
[6065b3aa]343        stp1 = new( 42, 42.5 );
[55acc3a]344        assert( const_count == 2 && dest_count == 0 );                                          // assertion for testing
345
[6065b3aa]346        printf( "CFA new initialize\n%d %g %d %g\n", stp->x, stp->y, stp1->x, stp1->y );
347        delete( stp, stp1 );
[55acc3a]348        assert( const_count == 2 && dest_count == 2 );                                          // assertion for testing
[6065b3aa]349
350        // new, array types
351        stp = anew( dim, 42, 42.5 );
[55acc3a]352        assert( const_count == 2 + dim && dest_count == 2 );                            // assertion for testing
[6065b3aa]353        printf( "CFA array new initialize\n" );
[8f34661]354        for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
[6065b3aa]355        printf( "\n" );
[55acc3a]356
[6065b3aa]357        stp1 = anew( dim, 42, 42.5 );
[55acc3a]358        assert( const_count == 2 + 2 * dim && dest_count == 2 );                        // assertion for testing
[8f34661]359        for ( i; dim ) { printf( "%d %g, ", stp1[i].x, stp1[i].y ); }
[6065b3aa]360        printf( "\n" );
[45444c3]361        adelete( stp, stp1 );
[55acc3a]362        assert( const_count == 2 + 2 * dim && dest_count == 2 + 2 * dim);       // assertion for testing
[6065b3aa]363
364        // extras
[fab700b]365        printf( "\n" );
366
[2afec66]367        float * fp = malloc() + 1;
368        printf( "pointer arithmetic %d\n", fp == fp - 1 );
369        free( fp - 1 );
[fab700b]370
[cfbc703d]371        ip = foo( bar( baz( malloc(), 0 ), 0 ), 0 );
372        *ip = 0xdeadbeef;
373        printf( "CFA deep malloc %#x\n", *ip );
[55acc3a]374
375        dp = alloc(5.0`fill); // just for testing multiple free
376        assert(*dp == 5.0);
[fe23950]377        free( ip, dp, 0p );
[fab700b]378
[fc67d6f]379#ifdef ERR1
[fab700b]380        stp = malloc();
381        printf( "\nSHOULD FAIL\n" );
[cfbc703d]382        ip = realloc( stp, dim * sizeof( *stp ) );
383        ip = memset( stp, 10 );
384        ip = memcpy( &st1, &st );
[a33704d]385#endif // ERR1
[fab700b]386} // main
387
388// Local Variables: //
389// tab-width: 4 //
[dc8511c]390// compile-command: "cfa alloc.cfa" //
[fab700b]391// End: //
Note: See TracBrowser for help on using the repository browser.