Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.cfa

    re310238 r76e2113  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Jul 19 15:05:28 2020
    13 // Update Count     : 501
     12// Last Modified On : Thu Apr 16 22:43:33 2020
     13// Update Count     : 498
    1414//
    1515
     
    2626//---------------------------------------
    2727
     28forall( dtype T | sized(T) ) {
     29        T * alloc_set( T ptr[], size_t dim, char fill ) {       // realloc array with fill
     30                size_t olen = malloc_usable_size( ptr );                // current allocation
     31                void * nptr = (void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
     32                size_t nlen = malloc_usable_size( nptr );               // new allocation
     33                if ( nlen > olen ) {                                                    // larger ?
     34                        memset( (char *)nptr + olen, (int)fill, nlen - olen ); // initialize added storage
     35                } // if
     36                return (T *)nptr;
     37        } // alloc_set
     38
     39        T * alloc_set( T ptr[], size_t dim, T fill ) {          // realloc array with fill
     40                size_t olen = malloc_usable_size( ptr );                // current allocation
     41                void * nptr = (void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
     42                size_t nlen = malloc_usable_size( nptr );               // new allocation
     43                if ( nlen > olen ) {                                                    // larger ?
     44                        for ( i; malloc_size( ptr ) / sizeof(T) ~ dim ) {
     45                                memcpy( &ptr[i], &fill, sizeof(T) );    // initialize with fill value
     46                        } // for
     47                } // if
     48                return (T *)nptr;
     49        } // alloc_align_set
     50
     51        T * alloc_align_set( T ptr[], size_t align, char fill ) { // aligned realloc with fill
     52                size_t olen = malloc_usable_size( ptr );                // current allocation
     53                void * nptr = (void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc
     54                // char * nptr = alloc_align( ptr, align );
     55                size_t nlen = malloc_usable_size( nptr );               // new allocation
     56                if ( nlen > olen ) {                                                    // larger ?
     57                        memset( (char *)nptr + olen, (int)fill, nlen - olen ); // initialize added storage
     58                } // if
     59                return (T *)nptr;
     60        } // alloc_align_set
     61
     62        T * alloc_align_set( T ptr[], size_t align, size_t dim, T fill ) { // aligned realloc with fill
     63                size_t olen = malloc_usable_size( ptr );                // current allocation
     64                void * nptr = (void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc
     65                // char * nptr = alloc_align( ptr, align );
     66                size_t nlen = malloc_usable_size( nptr );               // new allocation
     67                if ( nlen > olen ) {                                                    // larger ?
     68                        for ( i; dim ) { memcpy( &ptr[i], &fill, sizeof(T) ); } // initialize with fill value
     69                } // if
     70                return (T *)nptr;
     71        } // alloc_align_set
     72} // distribution
     73
    2874// allocation/deallocation and constructor/destructor, non-array types
    2975forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
     
    3278} // new
    3379
    34 forall( dtype T | { void ^?{}( T & ); } )
     80forall( dtype T | sized(T) | { void ^?{}( T & ); } )
    3581void delete( T * ptr ) {
    3682        if ( ptr ) {                                                                            // ignore null
     
    4086} // delete
    4187
    42 forall( dtype T, ttype Params | { void ^?{}( T & ); void delete( Params ); } )
     88forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } )
    4389void delete( T * ptr, Params rest ) {
    44         delete( ptr );
     90        if ( ptr ) {                                                                            // ignore null
     91                ^(*ptr){};                                                                              // run destructor
     92                free( ptr );
     93        } // if
    4594        delete( rest );
    4695} // delete
     
    228277extern "C" {                                                                                    // override C version
    229278        void srandom( unsigned int seed ) { srand48( (long int)seed ); }
    230         long int random( void ) { return mrand48(); }           // GENERATES POSITIVE AND NEGATIVE VALUES
     279        long int random( void ) { return mrand48(); }
    231280} // extern "C"
    232281
Note: See TracChangeset for help on using the changeset viewer.