Changeset 55acc3a


Ignore:
Timestamp:
Nov 6, 2020, 4:48:34 PM (3 years ago)
Author:
m3zulfiq <m3zulfiq@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
139775e
Parents:
ea3fa25
Message:

Added free for multiple frees in the same call. Added adelete tests.

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.hfa

    rea3fa25 r55acc3a  
    101101                return (T *)pvalloc( sizeof(T) );                               // C pvalloc
    102102        } // pvalloc
     103
     104        void free( T * addr ) {
     105                free( (void *) addr );                                                  // C free
     106        } // free
     107} // distribution
     108
     109static inline forall( ttype TT | { void free( TT ); } ) {
     110        // T* does not take void* and vice-versa
     111
     112        void free( void * addr, TT rest ) {
     113                free( addr );
     114                free( rest );
     115        } // free
     116
     117        forall( dtype T | sized(T) )
     118        void free( T * addr, TT rest ) {
     119                free( addr );
     120                free( rest );
     121        } // free
    103122} // distribution
    104123
  • tests/alloc.cfa

    rea3fa25 r55acc3a  
    208208
    209209
     210        int const_count, dest_count;
    210211        struct Struct { int x; double y; };
     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
    211220        Struct st, st1, sta[dim], sta1[dim], * stp, * stp1;
    212221
     
    329338        printf( "\n" );
    330339
     340        const_count = dest_count = 0;
    331341        stp = new( 42, 42.5 );
     342        assert( const_count == 1 && dest_count == 0 );                                          // assertion for testing
    332343        stp1 = new( 42, 42.5 );
     344        assert( const_count == 2 && dest_count == 0 );                                          // assertion for testing
     345
    333346        printf( "CFA new initialize\n%d %g %d %g\n", stp->x, stp->y, stp1->x, stp1->y );
    334347        delete( stp, stp1 );
     348        assert( const_count == 2 && dest_count == 2 );                                          // assertion for testing
    335349
    336350        // new, array types
    337351        stp = anew( dim, 42, 42.5 );
     352        assert( const_count == 2 + dim && dest_count == 2 );                            // assertion for testing
    338353        printf( "CFA array new initialize\n" );
    339354        for ( i; dim ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
    340355        printf( "\n" );
     356
    341357        stp1 = anew( dim, 42, 42.5 );
     358        assert( const_count == 2 + 2 * dim && dest_count == 2 );                        // assertion for testing
    342359        for ( i; dim ) { printf( "%d %g, ", stp1[i].x, stp1[i].y ); }
    343360        printf( "\n" );
    344361        adelete( stp, stp1 );
     362        assert( const_count == 2 + 2 * dim && dest_count == 2 + 2 * dim);       // assertion for testing
    345363
    346364        // extras
     
    354372        *ip = 0xdeadbeef;
    355373        printf( "CFA deep malloc %#x\n", *ip );
    356         free( ip );
     374
     375        dp = alloc(5.0`fill); // just for testing multiple free
     376        assert(*dp == 5.0);
     377        free( ip, dp );
    357378
    358379#ifdef ERR1
Note: See TracChangeset for help on using the changeset viewer.