source: tests/alloc.cfa @ 6ca0dab

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 6ca0dab 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
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
[0fa04a04]12// Last Modified On : Fri Aug 14 16:59:59 2020
13// Update Count     : 430
[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
[2afec66]210        struct Struct { int x; double y; };
211        Struct st, st1, sta[dim], sta1[dim], * stp, * stp1;
[fab700b]212
213        // alignment, non-array types
214        printf( "\n" );
215        enum { Alignment = 128 };
[6065b3aa]216
[6cfe8bb]217        stp = &(*(Struct*)memalign( Alignment, sizeof( *stp ) ) ){ 42, 42.5 }; // C memalign
[fab700b]218        assert( (uintptr_t)stp % Alignment == 0 );
[6065b3aa]219        printf( "C   memalign %d %g\n", stp->x, stp->y );
[2afec66]220        free( stp );
[fab700b]221
[cfbc703d]222        stp = &(*memalign( Alignment )){ 42, 42.5 };            // CFA memalign
[fab700b]223        assert( (uintptr_t)stp % Alignment == 0 );
224        printf( "CFA memalign %d %g\n", stp->x, stp->y );
[2afec66]225        free( stp );
[fab700b]226
[2afec66]227        posix_memalign( (void **)&stp, Alignment, sizeof( *stp ) ); // C posix_memalign
[6065b3aa]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 );
[2afec66]231        free( stp );
[6065b3aa]232
[2afec66]233        posix_memalign( &stp, Alignment );                  // CFA posix_memalign
[fab700b]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 );
[2afec66]237        free( stp );
[fab700b]238
[191a190]239        stp = &(*alloc( Alignment`align)){ 42, 42.5 };          // CFA alloc_align
[fab700b]240        assert( (uintptr_t)stp % Alignment == 0 );
[eb5a115]241        printf( "CFA alloc_align %d %g\n", stp->x, stp->y );
[2afec66]242        free( stp );
[6065b3aa]243
[191a190]244        stp = &(*alloc( Alignment`align )){ 42, 42.5 };         // CFA alloc_align
[6065b3aa]245        assert( (uintptr_t)stp % Alignment == 0 );
[eb5a115]246        printf( "CFA alloc_align %d %g\n", stp->x, stp->y );
[2afec66]247        free( stp );
[6065b3aa]248
[191a190]249        stp = alloc( Alignment`align, fill`fill );                      // CFA memalign, fill
[6065b3aa]250        assert( (uintptr_t)stp % Alignment == 0 );
[eb5a115]251        printf( "CFA alloc_align fill %#x %a\n", stp->x, stp->y );
252        free( stp );
253
[191a190]254        stp = alloc( Alignment`align, (Struct){ 42, 42.5 }`fill ); // CFA memalign, fill
[eb5a115]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
[191a190]259        stp = &(*alloc( stp`realloc, 4096`align )){ 42, 42.5 };         // CFA realign
[eb5a115]260        assert( (uintptr_t)stp % 4096 == 0 );
261        printf( "CFA alloc_align %d %g\n", stp->x, stp->y );
[2afec66]262        free( stp );
[fab700b]263
264
265        // alignment, array types
266        printf( "\n" );
267
[191a190]268        stp = alloc( dim, Alignment`align );                // CFA array memalign
[fab700b]269        assert( (uintptr_t)stp % Alignment == 0 );
[8f34661]270        for ( i; dim ) { stp[i] = (Struct){ 42, 42.5 }; }
[eb5a115]271        printf( "CFA array alloc_align\n" );
[8f34661]272        for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
[fab700b]273        printf( "\n" );
[2afec66]274        free( stp );
[fab700b]275
[191a190]276        stp = alloc( dim, Alignment`align, fill`fill );         // CFA array memalign, fill
[fab700b]277        assert( (uintptr_t)stp % Alignment == 0 );
[eb5a115]278        printf( "CFA array alloc_align, fill\n" );
[8f34661]279        for ( i; dim ) { printf( "%#x %a, ", stp[i].x, stp[i].y ); }
[fab700b]280        printf( "\n" );
[2afec66]281        free( stp );
[fab700b]282
[191a190]283        stp = alloc( dim, Alignment`align, ((Struct){ 42, 42.5 })`fill ); // CFA array memalign, fill
[eb5a115]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
[191a190]290        stp1 = alloc( dim, Alignment`align, [stp, dim]`fill );  // CFA array memalign, fill
[eb5a115]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
[191a190]297        stp = alloc( dim, stp`realloc, 4096`align );                            // CFA aligned realloc array
[eb5a115]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
[fab700b]305
306        // data, non-array types
307        printf( "\n" );
308
[2afec66]309        memset( &st, fill );                                // CFA memset, type safe
[6065b3aa]310        printf( "CFA memset %#x %a\n", st.x, st.y );
[2afec66]311        memcpy( &st1, &st );                                // CFA memcpy, type safe
[6065b3aa]312        printf( "CFA memcpy %#x %a\n", st1.x, st1.y );
[fab700b]313
314
315        // data, array types
316        printf( "\n" );
317
[b9c04946]318        amemset( sta, fill, dim );                                                      // CFA array memset, type safe
[fab700b]319        printf( "CFA array memset\n" );
[8f34661]320        for ( i; dim ) { printf( "%#x %a, ", sta[i].x, sta[i].y ); }
[fab700b]321        printf( "\n" );
322
[b9c04946]323        amemcpy( sta1, sta, dim );                                                      // CFA array memcpy, type safe
324        printf( "CFA array memcpy\n" );
[8f34661]325        for ( i; dim ) { printf( "%#x %a, ", sta1[i].x, sta1[i].y ); }
[6065b3aa]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" );
[8f34661]339        for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
[6065b3aa]340        printf( "\n" );
341        stp1 = anew( dim, 42, 42.5 );
[8f34661]342        for ( i; dim ) { printf( "%d %g, ", stp1[i].x, stp1[i].y ); }
[6065b3aa]343        printf( "\n" );
344        adelete( dim, stp, dim, stp1 );
345
346        // extras
[fab700b]347        printf( "\n" );
348
[2afec66]349        float * fp = malloc() + 1;
350        printf( "pointer arithmetic %d\n", fp == fp - 1 );
351        free( fp - 1 );
[fab700b]352
[cfbc703d]353        ip = foo( bar( baz( malloc(), 0 ), 0 ), 0 );
354        *ip = 0xdeadbeef;
355        printf( "CFA deep malloc %#x\n", *ip );
356        free( ip );
[fab700b]357
[fc67d6f]358#ifdef ERR1
[fab700b]359        stp = malloc();
360        printf( "\nSHOULD FAIL\n" );
[cfbc703d]361        ip = realloc( stp, dim * sizeof( *stp ) );
362        ip = memset( stp, 10 );
363        ip = memcpy( &st1, &st );
[cdbfab0]364#endif
[fab700b]365} // main
366
367// Local Variables: //
368// tab-width: 4 //
[dc8511c]369// compile-command: "cfa alloc.cfa" //
[fab700b]370// End: //
Note: See TracBrowser for help on using the repository browser.