Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/stdlib.c

    rf3fc631f r6065b3aa  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue May 30 09:07:56 2017
    13 // Update Count     : 237
     12// Last Modified On : Thu Jun  1 21:52:57 2017
     13// Update Count     : 280
    1414//
    1515
     
    2828
    2929// resize, non-array types
    30 forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, char fill ) { // alternative realloc with fill value
    31         //printf( "X6\n" );
     30forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ) {
    3231        size_t olen = malloc_usable_size( ptr );                        // current allocation
    33     char * nptr = (void *)realloc( (void *)ptr, size ); // C realloc
     32    char * nptr = (void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
    3433        size_t nlen = malloc_usable_size( nptr );                       // new allocation
    3534        if ( nlen > olen ) {                                                            // larger ?
     
    3736        } //
    3837    return (T *)nptr;
    39 } // realloc
    40 
    41 // allocation/deallocation and constructor/destructor
    42 forall( dtype T, ttype Params | sized(T) | { void ?{}( T *, Params ); } )
     38} // alloc
     39
     40// allocation/deallocation and constructor/destructor, non-array types
     41forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } )
    4342T * new( Params p ) {
    44         return ((T *)malloc()){ p };
     43        return (malloc()){ p };                                                         // run constructor
    4544} // new
    4645
    4746forall( dtype T | { void ^?{}( T * ); } )
    4847void delete( T * ptr ) {
    49         if ( ptr ) {
     48        if ( ptr ) {                                                                            // ignore null
    5049                ^ptr{};                                                                                 // run destructor
    5150                free( ptr );
     
    5554forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } )
    5655void delete( T * ptr, Params rest ) {
    57         if ( ptr ) {
     56        if ( ptr ) {                                                                            // ignore null
    5857                ^ptr{};                                                                                 // run destructor
    5958                free( ptr );
     
    6160        delete( rest );
    6261} // delete
     62
     63
     64// allocation/deallocation and constructor/destructor, array types
     65forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } )
     66T * anew( size_t dim, Params p ) {
     67        T *arr = alloc( dim );
     68        for ( unsigned int i = 0; i < dim; i += 1 ) {
     69                (&arr[i]){ p };                                                                 // run constructor
     70        } // for
     71        return arr;
     72} // anew
     73
     74forall( dtype T | sized(T) | { void ^?{}( T * ); } )
     75void adelete( size_t dim, T arr[] ) {
     76        if ( arr ) {                                                                            // ignore null
     77                for ( int i = dim - 1; i >= 0; i -= 1 ) {               // reverse allocation order, must be unsigned
     78                        ^(&arr[i]){};                                                           // run destructor
     79                } // for
     80                free( arr );
     81        } // if
     82} // adelete
     83
     84forall( dtype T | sized(T) | { void ^?{}( T * ); }, ttype Params | { void adelete( Params ); } )
     85void adelete( size_t dim, T arr[], Params rest ) {
     86        if ( arr ) {                                                                            // ignore null
     87                for ( int i = dim - 1; i >= 0; i -= 1 ) {               // reverse allocation order, must be unsigned
     88                        ^(&arr[i]){};                                                           // run destructor
     89                } // for
     90                free( arr );
     91        } // if
     92        adelete( rest );
     93} // adelete
    6394
    6495//---------------------------------------
Note: See TracChangeset for help on using the changeset viewer.