// // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // alloc.cfa -- // // Author : Peter A. Buhr // Created On : Wed Feb 3 07:56:22 2016 // Last Modified By : Peter A. Buhr // Last Modified On : Fri Oct 14 09:31:39 2022 // Update Count : 491 // #include #include // malloc_usable_size #include // uintptr_t #include // posix_memalign #include #include // access C malloc, realloc int * foo( int * p, int c ) { return p; } int * bar( int * p, int c ) { return p; } int * baz( int * p, int c ) { return p; } int main( void ) { size_t dim = 10; char fill = '\xde'; int * ip, * ip1; // allocation, non-array types ip = (int *)malloc( sizeof(*ip) ); // C malloc, type unsafe *ip = 0xdeadbeef; sout | "C malloc" | hex(*ip); free( ip ); ip = malloc(); // CFA malloc, type safe *ip = 0xdeadbeef; sout | "CFA malloc" | hex(*ip); free( ip ); ip = alloc(); // CFA alloc, type safe *ip = 0xdeadbeef; sout | "CFA alloc" | hex(*ip); free( ip ); ip = alloc( fill`fill ); // CFA alloc, fill sout | "CFA alloc, fill" | wd(8, nobase(hex(*ip))); free( ip ); ip = alloc( 3`fill ); // CFA alloc, fill sout | "CFA alloc, fill" | *ip; free( ip ); // allocation, array types sout | nl; ip = (int *)calloc( dim, sizeof( *ip ) ); // C array calloc, type unsafe sout | "C array calloc, fill 0"; for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; } sout | nl; free( ip ); ip = calloc( dim ); // CFA array calloc, type safe sout | "CFA array calloc, fill 0"; for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; } sout | nl; free( ip ); ip = alloc( dim ); // CFA array alloc, type safe for ( i; dim ) { ip[i] = 0xdeadbeef; } sout | "CFA array alloc, no fill"; for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; } sout | nl; free( ip ); ip = alloc( 2 * dim, fill`fill ); // CFA array alloc, fill sout | "CFA array alloc, fill" | hex(fill); for ( i; 2 * dim ) { sout | hex(ip[i]) | ' ' | nonl; } sout | nl; free( ip ); ip = alloc( 2 * dim, ((int)0xdeadbeef)`fill ); // CFA array alloc, fill sout | "CFA array alloc, fill" | hex((char)0xdeadbeef); for ( i; 2 * dim ) { sout | hex(ip[i]) | ' ' | nonl; } sout | nl; // do not free ip1 = alloc( 2 * dim, [ip, 2 * dim]`fill ); // CFA array alloc, fill sout | "CFA array alloc, fill from array"; for ( i; 2 * dim ) { sout | hex(ip[i]) | hex(ip1[i]) | ", " | nonl; } free( ip1 ); sout | nl; // realloc, non-array types sout | nl; ip = (int *)realloc( ip, dim * sizeof(*ip) ); // C realloc sout | "C realloc"; for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; } sout | nl; // do not free ip = realloc( ip, 2 * dim * sizeof(*ip) ); // CFA realloc for ( i; dim ~ 2 * dim ) { ip[i] = 0x1010101; } sout | "CFA realloc"; for ( i; 2 * dim ) { sout | hex(ip[i]) | ' ' | nonl; } sout | nl; // do not free // realloc, array types sout | nl; ip = alloc( dim, ip`realloc ); // CFA realloc array alloc for ( i; dim ) { ip[i] = 0xdeadbeef; } sout | "CFA realloc array alloc"; for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; } sout | nl; // do not free ip = alloc( 2 * dim, ip`realloc ); // CFA realloc array alloc for ( i; dim ~ 2 * dim ) { ip[i] = 0x1010101; } // fill upper part sout | "CFA realloc array alloc"; for ( i; 2 * dim ) { sout | hex(ip[i]) | ' ' | nonl; } sout | nl; // do not free ip = alloc( dim, ip`realloc ); // CFA realloc array alloc sout | "CFA realloc array alloc"; for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; } sout | nl; // do not free ip = alloc( 3 * dim, ip`realloc, fill`fill ); // CFA realloc array alloc, fill sout | "CFA realloc array alloc, fill"; for ( i; 3 * dim ) { sout | hex(ip[i]) | ' ' | nonl; } sout | nl; // do not free ip = alloc( dim, ip`realloc, fill`fill ); // CFA realloc array alloc, fill sout | "CFA realloc array alloc, fill"; for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; } sout | nl; // do not free ip = alloc( 3 * dim, ip`realloc, fill`fill ); // CFA realloc array alloc, fill sout | "CFA realloc array alloc, fill"; for ( i; 3 * dim ) { sout | hex(ip[i]) | ' ' | nonl; } sout | nl; // do not free ip = alloc( 5 * dim, ip`realloc, 5`fill ); // CFA realloc array alloc, 5 sout | "CFA realloc array alloc, 5"; for ( i; 5 * dim ) { sout | hex(ip[i]) | ' ' | nonl; } sout | nl; // do not free ip = alloc( dim, ip`realloc, 5`fill ); // CFA realloc array alloc, 5 sout | "CFA realloc array alloc, 5"; for ( i; dim ) { sout | hex(ip[i]) | ' ' | nonl; } sout | nl; // do not free ip = alloc( 5 * dim, ip`realloc, 5`fill ); // CFA realloc array alloc, 5 sout | "CFA realloc array alloc, 5"; for ( i; 5 * dim ) { sout | hex(ip[i]) | ' ' | nonl; } sout | nl; free( ip ); // resize, non-array types struct S { int a[5]; }; ip = alloc(); *ip = 5; double * dp = alloc( ip`resize ); *dp = 5.5; S * sp = alloc( dp`resize ); *sp = (S){ {0, 1, 2, 3, 4} }; ip = alloc( sp`resize ); *ip = 3; free( ip ); // resize, array types ip = alloc( 5 ); for ( i; 5 ) { ip[i] = 5; } dp = alloc( 5, ip`resize ); for ( i; 5 ) { dp[i] = 5.5; } sp = alloc( 5, dp`resize ); for ( i; 5 ) { sp[i] = (S){ {0, 1, 2, 3, 4} }; } ip = alloc( 3, sp`resize ); for ( i; 3 ) { ip[i] = 3; } ip = alloc( 7, ip`realloc ); for ( i; 7 ) { ip[i] = 7; } ip = alloc( 7, ip`resize ); for ( i; 7 ) { ip[i] = 7; } free( ip ); int const_count, dest_count; struct Struct { int x; double y; }; void ?{}( Struct & a ) { // construct a.[ x, y ] = [ -1, -1.0 ]; } void ?{}( Struct & a, int x, double y ) { // initialize a.[ x, y ] = [ x, y ]; const_count++; } void ^?{}( Struct & a ) { dest_count++; } // destruct Struct st, st1, sta[dim], sta1[dim], * stp, * stp1; // alignment, non-array types sout | nl; enum { Alignment = 128 }; stp = &(*(Struct*)memalign( Alignment, sizeof( *stp ) ) ){ 42, 42.5 }; // C memalign assert( (uintptr_t)stp % Alignment == 0 ); sout | "C memalign " | stp->x | stp->y; free( stp ); stp = &(*memalign( Alignment )){ 42, 42.5 }; // CFA memalign assert( (uintptr_t)stp % Alignment == 0 ); sout | "CFA memalign" | stp->x | stp->y; free( stp ); posix_memalign( (void **)&stp, Alignment, sizeof( *stp ) ); // C posix_memalign *stp = (Struct){ 42, 42.5 }; assert( (uintptr_t)stp % Alignment == 0 ); sout | "CFA posix_memalign" | stp->x | stp->y; free( stp ); posix_memalign( &stp, Alignment ); // CFA posix_memalign *stp = (Struct){ 42, 42.5 }; assert( (uintptr_t)stp % Alignment == 0 ); sout | "CFA posix_memalign" | stp->x | stp->y; free( stp ); stp = &(*alloc( Alignment`align)){ 42, 42.5 }; // CFA alloc_align assert( (uintptr_t)stp % Alignment == 0 ); sout | "CFA alloc_align" | stp->x | stp->y; free( stp ); stp = &(*alloc( Alignment`align )){ 42, 42.5 }; // CFA alloc_align assert( (uintptr_t)stp % Alignment == 0 ); sout | "CFA alloc_align" | stp->x | stp->y; free( stp ); stp = alloc( Alignment`align, fill`fill ); // CFA memalign, fill assert( (uintptr_t)stp % Alignment == 0 ); sout | "CFA alloc_align fill" | hex(stp->x) | hex(stp->y); free( stp ); stp = alloc( Alignment`align, (Struct){ 42, 42.5 }`fill ); // CFA memalign, fill assert( (uintptr_t)stp % Alignment == 0 ); sout | "CFA alloc_align fill" | stp->x | stp->y; // do not free stp = &(*alloc( stp`realloc, 4096`align )){ 42, 42.5 }; // CFA realign assert( (uintptr_t)stp % 4096 == 0 ); sout | "CFA alloc_align" | stp->x | stp->y; free( stp ); // alignment, array types sout | nl; stp = alloc( dim, Alignment`align ); // CFA array memalign assert( (uintptr_t)stp % Alignment == 0 ); for ( i; dim ) { stp[i] = (Struct){ 42, 42.5 }; } sout | "CFA array alloc_align"; for ( i; dim ) { sout | stp[i].x | stp[i].y | ", " | nonl; } sout | nl; free( stp ); stp = alloc( dim, Alignment`align, fill`fill ); // CFA array memalign, fill assert( (uintptr_t)stp % Alignment == 0 ); sout | "CFA array alloc_align, fill"; for ( i; dim ) { sout | hex(stp[i].x) | hex(stp[i].y) | ", " | nonl; } sout | nl; free( stp ); stp = alloc( dim, Alignment`align, ((Struct){ 42, 42.5 })`fill ); // CFA array memalign, fill assert( (uintptr_t)stp % Alignment == 0 ); sout | "CFA array alloc_align, fill"; for ( i; dim ) { sout | stp[i].x | stp[i].y | ", " | nonl; } sout | nl; // do not free stp1 = alloc( dim, Alignment`align, [stp, dim]`fill ); // CFA array memalign, fill assert( (uintptr_t)stp % Alignment == 0 ); sout | "CFA array alloc_align, fill array"; for ( i; dim ) { sout | stp1[i].x | stp1[i].y | ", " | nonl; } sout | nl; free( stp1 ); stp = alloc( dim, stp`realloc, 4096`align ); // CFA aligned realloc array assert( (uintptr_t)stp % 4096 == 0 ); for ( i; dim ) { stp[i] = (Struct){ 42, 42.5 }; } sout | "CFA realloc array alloc_align"; for ( i; dim ) { sout | stp[i].x | stp[i].y | ", " | nonl; } sout | nl; free( stp ); // data, non-array types sout | nl; memset( &st, fill ); // CFA memset, type safe sout | "CFA memset" | hex(st.x) | hex(st.y); memcpy( &st1, &st ); // CFA memcpy, type safe sout | "CFA memcpy" | hex(st1.x) | hex(st1.y); // data, array types sout | nl; amemset( sta, fill, dim ); // CFA array memset, type safe sout | "CFA array memset"; for ( i; dim ) { sout | hex(sta[i].x) | hex(sta[i].y) | ", " | nonl; } sout | nl; amemcpy( sta1, sta, dim ); // CFA array memcpy, type safe sout | "CFA array memcpy"; for ( i; dim ) { sout | hex(sta1[i].x) | hex(sta1[i].y) | ", " | nonl; } sout | nl; // new, non-array types sout | nl; const_count = dest_count = 0; stp = new( 42, 42.5 ); assert( const_count == 1 && dest_count == 0 ); // assertion for testing stp1 = new( 42, 42.5 ); assert( const_count == 2 && dest_count == 0 ); // assertion for testing sout | "CFA new initialize" | nl | stp->x | stp->y | stp1->x | stp1->y; delete( stp, stp1 ); assert( const_count == 2 && dest_count == 2 ); // assertion for testing // new, array types stp = anew( dim, 42, 42.5 ); assert( const_count == 2 + dim && dest_count == 2 ); // assertion for testing sout | "CFA array new initialize"; for ( i; dim ) { sout | stp[i].x | stp[i].y | ", " | nonl; } sout | nl; stp1 = anew( dim, 42, 42.5 ); assert( const_count == 2 + 2 * dim && dest_count == 2 ); // assertion for testing for ( i; dim ) { sout | stp1[i].x | stp1[i].y | ", " | nonl; } sout | nl; adelete( stp, stp1 ); assert( const_count == 2 + 2 * dim && dest_count == 2 + 2 * dim); // assertion for testing // extras sout | nl; float * fp = malloc() + 1; sout | "pointer arithmetic" | fp == fp - 1; free( fp - 1 ); ip = foo( bar( baz( malloc(), 0 ), 0 ), 0 ); *ip = 0xdeadbeef; sout | "CFA deep malloc" | hex(*ip); dp = alloc(5.0`fill); // just for testing multiple free assert(*dp == 5.0); free( ip, dp, 0p ); #ifdef ERR1 stp = malloc(); sout | "\nSHOULD FAIL"; ip = realloc( stp, dim * sizeof( *stp ) ); ip = memset( stp, 10 ); ip = memcpy( &st1, &st ); #endif // ERR1 } // main // Local Variables: // // tab-width: 4 // // compile-command: "cfa alloc.cfa" // // End: //