source: tests/alloc.cfa @ 9b026f1

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 9b026f1 was 6a25b8f, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

update allocation tests to reflect allocation changes, change from NULL to 0p, update expected values for changed tests

  • Property mode set to 100644
File size: 9.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
[6a25b8f]12// Last Modified On : Fri Nov 22 15:34:19 2019
13// Update Count     : 404
[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';
30        int * p, * p1;
[fab700b]31
32        // allocation, non-array types
33
[fc67d6f]34        p = (int *)malloc( sizeof(*p) );                                        // C malloc, type unsafe
[fab700b]35        *p = 0xdeadbeef;
[6065b3aa]36        printf( "C   malloc %#x\n", *p );
[2afec66]37        free( p );
[fab700b]38
[2afec66]39        p = malloc();                                       // CFA malloc, type safe
[6065b3aa]40        *p = 0xdeadbeef;
41        printf( "CFA malloc %#x\n", *p );
[2afec66]42        free( p );
[6065b3aa]43
[2afec66]44        p = alloc();                                        // CFA alloc, type safe
[6065b3aa]45        *p = 0xdeadbeef;
46        printf( "CFA alloc %#x\n", *p );
[2afec66]47        free( p );
[fab700b]48
[eb5a115]49        p = alloc_set( fill );                                                          // CFA alloc, fill
50        printf( "CFA array alloc, fill %#hhx\n", fill );
[6065b3aa]51        printf( "CFA alloc, fill %08x\n", *p );
[cf0de0e]52        free( p );
[fab700b]53
[eb5a115]54        p = alloc_set( 3 );                                                                     // CFA alloc, fill
55        printf( "CFA alloc, fill %d\n", *p );
56        free( p );
57
[fab700b]58
59        // allocation, array types
60        printf( "\n" );
61
[e672372]62        p = (int *)calloc( dim, sizeof( *p ) );                         // C array calloc, type unsafe
[6065b3aa]63        printf( "C   array calloc, fill 0\n" );
[8f34661]64        for ( i; dim ) { printf( "%#x ", p[i] ); }
[fab700b]65        printf( "\n" );
[2afec66]66        free( p );
[fab700b]67
[2afec66]68        p = calloc( dim );                                  // CFA array calloc, type safe
[6065b3aa]69        printf( "CFA array calloc, fill 0\n" );
[8f34661]70        for ( i; dim ) { printf( "%#x ", p[i] ); }
[fab700b]71        printf( "\n" );
[2afec66]72        free( p );
[fab700b]73
[2afec66]74        p = alloc( dim );                                   // CFA array alloc, type safe
[8f34661]75        for ( i; dim ) { p[i] = 0xdeadbeef; }
[6065b3aa]76        printf( "CFA array alloc, no fill\n" );
[8f34661]77        for ( i; dim ) { printf( "%#x ", p[i] ); }
[fab700b]78        printf( "\n" );
[2afec66]79        free( p );
[fab700b]80
[eb5a115]81        p = alloc_set( 2 * dim, fill );                                         // CFA array alloc, fill
[6ea0408]82        printf( "CFA array alloc, fill %#hhx\n", fill );
[8f34661]83        for ( i; 2 * dim ) { printf( "%#x ", p[i] ); }
[fab700b]84        printf( "\n" );
[eb5a115]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" );
[fab700b]91        // do not free
92
[eb5a115]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
[fab700b]99
100        // resize, non-array types
101        printf( "\n" );
102
[fc67d6f]103        p = (int *)realloc( p, dim * sizeof(*p) );                      // C realloc
[eb5a115]104        printf( "C realloc\n" );
[8f34661]105        for ( i; dim ) { printf( "%#x ", p[i] ); }
[fab700b]106        printf( "\n" );
[eb5a115]107        // do not free
[fab700b]108
[2afec66]109        p = realloc( p, 2 * dim * sizeof(*p) );             // CFA realloc
[8f34661]110        for ( i; dim ~ 2 * dim ) { p[i] = 0x1010101; }
[a4683611]111        printf( "CFA realloc\n" );
[8f34661]112        for ( i; 2 * dim ) { printf( "%#x ", p[i] ); }
[fab700b]113        printf( "\n" );
[6065b3aa]114        // do not free
[fab700b]115
116
[6065b3aa]117        // resize, array types
[fab700b]118        printf( "\n" );
119
[2afec66]120        p = alloc( p, dim );                                // CFA resize array alloc
[8f34661]121        for ( i; dim ) { p[i] = 0xdeadbeef; }
[eb5a115]122        printf( "CFA resize array alloc\n" );
[8f34661]123        for ( i; dim ) { printf( "%#x ", p[i] ); }
[fab700b]124        printf( "\n" );
[eb5a115]125        // do not free
[fab700b]126
[2afec66]127        p = alloc( p, 2 * dim );                            // CFA resize array alloc
[6a25b8f]128        for ( i; dim ~ 2 * dim ) { p[i] = 0x1010101; }          // fill upper part
[a4683611]129        printf( "CFA resize array alloc\n" );
[8f34661]130        for ( i; 2 * dim ) { printf( "%#x ", p[i] ); }
[fab700b]131        printf( "\n" );
[eb5a115]132        // do not free
[fab700b]133
[eb5a115]134        p = alloc( p, dim );                                // CFA resize array alloc
[a4683611]135        printf( "CFA resize array alloc\n" );
[8f34661]136        for ( i; dim ) { printf( "%#x ", p[i] ); }
[fab700b]137        printf( "\n" );
[eb5a115]138        // do not free
[fab700b]139
[eb5a115]140        p = alloc_set( p, 3 * dim, fill );                                      // CFA resize array alloc, fill
[6a25b8f]141        printf( "CFA resize array alloc\n" );
[eb5a115]142        for ( i; 3 * dim ) { printf( "%#x ", p[i] ); }
[fab700b]143        printf( "\n" );
[eb5a115]144        // do not free
[fab700b]145
[eb5a115]146        p = alloc_set( p, dim, fill );                                          // CFA resize array alloc, fill
[6a25b8f]147        printf( "CFA resize array alloc\n" );
[eb5a115]148        for ( i; dim ) { printf( "%#x ", p[i] ); }
[fab700b]149        printf( "\n" );
[eb5a115]150        // do not free
[fab700b]151
[eb5a115]152        p = alloc_set( p, 3 * dim, fill );                                      // CFA resize array alloc, fill
[a4683611]153        printf( "CFA resize array alloc, fill\n" );
[eb5a115]154        for ( i; 3 * dim ) { printf( "%#x ", p[i] );; }
[fab700b]155        printf( "\n" );
[6065b3aa]156        free( p );
[fab700b]157
158
[2afec66]159        struct Struct { int x; double y; };
160        Struct st, st1, sta[dim], sta1[dim], * stp, * stp1;
[fab700b]161
162        // alignment, non-array types
163        printf( "\n" );
164        enum { Alignment = 128 };
[6065b3aa]165
[6cfe8bb]166        stp = &(*(Struct*)memalign( Alignment, sizeof( *stp ) ) ){ 42, 42.5 }; // C memalign
[fab700b]167        assert( (uintptr_t)stp % Alignment == 0 );
[6065b3aa]168        printf( "C   memalign %d %g\n", stp->x, stp->y );
[2afec66]169        free( stp );
[fab700b]170
[6cfe8bb]171        stp = &(*memalign( Alignment )){ 42, 42.5 };          // CFA memalign
[fab700b]172        assert( (uintptr_t)stp % Alignment == 0 );
173        printf( "CFA memalign %d %g\n", stp->x, stp->y );
[2afec66]174        free( stp );
[fab700b]175
[2afec66]176        posix_memalign( (void **)&stp, Alignment, sizeof( *stp ) ); // C posix_memalign
[6065b3aa]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 );
[2afec66]180        free( stp );
[6065b3aa]181
[2afec66]182        posix_memalign( &stp, Alignment );                  // CFA posix_memalign
[fab700b]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 );
[2afec66]186        free( stp );
[fab700b]187
[eb5a115]188        stp = &(*alloc_align( Alignment)){ 42, 42.5 };          // CFA alloc_align
[fab700b]189        assert( (uintptr_t)stp % Alignment == 0 );
[eb5a115]190        printf( "CFA alloc_align %d %g\n", stp->x, stp->y );
[2afec66]191        free( stp );
[6065b3aa]192
[eb5a115]193        stp = &(*alloc_align( Alignment )){ 42, 42.5 };         // CFA alloc_align
[6065b3aa]194        assert( (uintptr_t)stp % Alignment == 0 );
[eb5a115]195        printf( "CFA alloc_align %d %g\n", stp->x, stp->y );
[2afec66]196        free( stp );
[6065b3aa]197
[eb5a115]198        stp = alloc_align_set( Alignment, fill );                       // CFA memalign, fill
[6065b3aa]199        assert( (uintptr_t)stp % Alignment == 0 );
[eb5a115]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 );
[2afec66]211        free( stp );
[fab700b]212
213
214        // alignment, array types
215        printf( "\n" );
216
[eb5a115]217        stp = alloc_align( Alignment, dim );                // CFA array memalign
[fab700b]218        assert( (uintptr_t)stp % Alignment == 0 );
[8f34661]219        for ( i; dim ) { stp[i] = (Struct){ 42, 42.5 }; }
[eb5a115]220        printf( "CFA array alloc_align\n" );
[8f34661]221        for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
[fab700b]222        printf( "\n" );
[2afec66]223        free( stp );
[fab700b]224
[eb5a115]225        stp = alloc_align_set( Alignment, dim, fill );          // CFA array memalign, fill
[fab700b]226        assert( (uintptr_t)stp % Alignment == 0 );
[eb5a115]227        printf( "CFA array alloc_align, fill\n" );
[8f34661]228        for ( i; dim ) { printf( "%#x %a, ", stp[i].x, stp[i].y ); }
[fab700b]229        printf( "\n" );
[2afec66]230        free( stp );
[fab700b]231
[eb5a115]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
[fab700b]254
255        // data, non-array types
256        printf( "\n" );
257
[2afec66]258        memset( &st, fill );                                // CFA memset, type safe
[6065b3aa]259        printf( "CFA memset %#x %a\n", st.x, st.y );
[2afec66]260        memcpy( &st1, &st );                                // CFA memcpy, type safe
[6065b3aa]261        printf( "CFA memcpy %#x %a\n", st1.x, st1.y );
[fab700b]262
263
264        // data, array types
265        printf( "\n" );
266
[b9c04946]267        amemset( sta, fill, dim );                                                      // CFA array memset, type safe
[fab700b]268        printf( "CFA array memset\n" );
[8f34661]269        for ( i; dim ) { printf( "%#x %a, ", sta[i].x, sta[i].y ); }
[fab700b]270        printf( "\n" );
271
[b9c04946]272        amemcpy( sta1, sta, dim );                                                      // CFA array memcpy, type safe
273        printf( "CFA array memcpy\n" );
[8f34661]274        for ( i; dim ) { printf( "%#x %a, ", sta1[i].x, sta1[i].y ); }
[6065b3aa]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" );
[8f34661]289        for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
[6065b3aa]290        printf( "\n" );
291        stp1 = anew( dim, 42, 42.5 );
[8f34661]292        for ( i; dim ) { printf( "%d %g, ", stp1[i].x, stp1[i].y ); }
[6065b3aa]293        printf( "\n" );
294        adelete( dim, stp, dim, stp1 );
295
296        // extras
[fab700b]297        printf( "\n" );
298
[2afec66]299        float * fp = malloc() + 1;
300        printf( "pointer arithmetic %d\n", fp == fp - 1 );
301        free( fp - 1 );
[fab700b]302
[2afec66]303        p = foo( bar( baz( malloc(), 0 ), 0 ), 0 );
[fab700b]304        *p = 0xdeadbeef;
[6065b3aa]305        printf( "CFA deep malloc %#x\n", *p );
[2afec66]306        free( p );
[fab700b]307
[fc67d6f]308#ifdef ERR1
[fab700b]309        stp = malloc();
310        printf( "\nSHOULD FAIL\n" );
[fc67d6f]311        p = realloc( stp, dim * sizeof( *stp ) );
312        p = alloc( stp, dim * sizeof( *stp ) );
[2afec66]313        p = memset( stp, 10 );
314        p = memcpy( &st1, &st );
[cdbfab0]315#endif
[fab700b]316} // main
317
318// Local Variables: //
319// tab-width: 4 //
[dc8511c]320// compile-command: "cfa alloc.cfa" //
[fab700b]321// End: //
Note: See TracBrowser for help on using the repository browser.