Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.hfa

    re3fea42 rd6b03b7  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Feb  4 08:27:01 2020
    13 // Update Count     : 401
     12// Last Modified On : Tue Jul 23 14:14:59 2019
     13// Update Count     : 373
    1414//
    1515
     
    2525        void * memset( void * dest, int fill, size_t size ); // string.h
    2626        void * memcpy( void * dest, const void * src, size_t size ); // string.h
    27     void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap
     27    void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA
    2828} // extern "C"
    29 
    30 void * realloc( void * oaddr, size_t nalign, size_t size ); // CFA heap
    3129
    3230//---------------------------------------
     
    5250        } // calloc
    5351
    54         T * realloc( T * ptr, size_t size ) {                           // CFA realloc, eliminate return-type cast
    55                 return (T *)(void *)realloc( (void *)ptr, size ); // C realloc
     52        T * realloc( T * ptr, size_t size ) {
     53                if ( unlikely( ptr == 0 ) ) return malloc();
     54                return (T *)(void *)realloc( (void *)ptr, size );
    5655        } // realloc
    5756
    5857        T * memalign( size_t align ) {
    59                 return (T *)memalign( align, sizeof(T) );               // C memalign
     58                return (T *)memalign( align, sizeof(T) );
    6059        } // memalign
    6160
    62         T * cmemalign( size_t align, size_t dim  ) {
    63                 return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign
    64         } // cmemalign
    65 
    6661        T * aligned_alloc( size_t align ) {
    67                 return (T *)aligned_alloc( align, sizeof(T) );  // C aligned_alloc
     62                return (T *)aligned_alloc( align, sizeof(T) );
    6863        } // aligned_alloc
    6964
     
    7267        } // posix_memalign
    7368
     69
    7470        // Cforall dynamic allocation
    7571
     
    7874        } // alloc
    7975
     76        T * alloc( char fill ) {
     77                T * ptr;
     78                if ( _Alignof(T) <= libAlign() ) ptr = (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
     79                else ptr = (T *)memalign( _Alignof(T), sizeof(T) );
     80                return (T *)memset( ptr, (int)fill, sizeof(T) ); // initialize with fill value
     81        } // alloc
     82
    8083        T * alloc( size_t dim ) {
    81                 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( dim * (size_t)sizeof(T) );
     84                if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
    8285                else return (T *)memalign( _Alignof(T), dim * sizeof(T) );
    8386        } // alloc
    8487
    85         T * alloc( T ptr[], size_t dim ) {                                      // realloc
    86                 return (T *)(void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
    87         } // alloc
    88 
    89         T * alloc_set( char fill ) {
    90                 return (T *)memset( (T *)alloc(), (int)fill, sizeof(T) ); // initialize with fill value
    91         } // alloc
    92 
    93         T * alloc_set( T fill ) {
    94                 return (T *)memcpy( (T *)alloc(), &fill, sizeof(T) ); // initialize with fill value
    95         } // alloc
    96 
    97         T * alloc_set( size_t dim, char fill ) {
     88        T * alloc( size_t dim, char fill ) {
    9889                return (T *)memset( (T *)alloc( dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
    9990        } // alloc
    10091
    101         T * alloc_set( size_t dim, T fill ) {
    102                 T * r = (T *)alloc( dim );
    103                 for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value
    104                 return r;
    105         } // alloc
    106 
    107         T * alloc_set( size_t dim, const T fill[] ) {
    108                 return (T *)memcpy( (T *)alloc( dim ), fill, dim * sizeof(T) ); // initialize with fill value
    109         } // alloc
    110 } // distribution
    111 
    112 forall( dtype T | sized(T) ) {
    113         T * alloc_set( T ptr[], size_t dim, char fill );        // realloc array with fill
    114 } // distribution
    115 
    116 static inline forall( dtype T | sized(T) ) {
    117         T * alloc_align( size_t align ) {
     92        T * alloc( T ptr[], size_t dim ) {
     93                return realloc( ptr, dim * sizeof(T) );
     94        } // alloc
     95} // distribution
     96
     97
     98static inline forall( dtype T | sized(T) ) {
     99        T * align_alloc( size_t align ) {
    118100                return (T *)memalign( align, sizeof(T) );
    119         } // alloc_align
    120 
    121         T * alloc_align( size_t align, size_t dim ) {
     101        } // align_alloc
     102
     103        T * align_alloc( size_t align, char fill ) {
     104                T * ptr = (T *)memalign( align, sizeof(T) );
     105                return (T *)memset( ptr, (int)fill, sizeof(T) );
     106        } // align_alloc
     107
     108        T * align_alloc( size_t align, size_t dim ) {
    122109                return (T *)memalign( align, dim * sizeof(T) );
    123         } // alloc_align
    124 
    125         T * alloc_align( T ptr[], size_t align ) {                      // aligned realloc array
    126                 return (T *)(void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc
    127         } // alloc_align
    128 
    129         T * alloc_align( T ptr[], size_t align, size_t dim ) { // aligned realloc array
    130                 return (T *)(void *)realloc( (void *)ptr, align, dim * sizeof(T) ); // CFA realloc
    131         } // alloc_align
    132 
    133         T * alloc_align_set( size_t align, char fill ) {
    134                 return (T *)memset( (T *)alloc_align( align ), (int)fill, sizeof(T) ); // initialize with fill value
    135         } // alloc_align
    136 
    137         T * alloc_align_set( size_t align, T fill ) {
    138                 return (T *)memcpy( (T *)alloc_align( align ), &fill, sizeof(T) ); // initialize with fill value
    139         } // alloc_align
    140 
    141         T * alloc_align_set( size_t align, size_t dim, char fill ) {
    142                 return (T *)memset( (T *)alloc_align( align, dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
    143         } // alloc_align
    144 
    145         T * alloc_align_set( size_t align, size_t dim, T fill ) {
    146                 T * r = (T *)alloc_align( align, dim );
    147                 for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value
    148                 return r;
    149         } // alloc_align
    150 
    151         T * alloc_align_set( size_t align, size_t dim, const T fill[] ) {
    152                 return (T *)memcpy( (T *)alloc_align( align, dim ), fill, dim * sizeof(T) );
    153         } // alloc_align
    154 } // distribution
    155 
    156 forall( dtype T | sized(T) ) {
    157         T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); // aligned realloc array with fill
    158 } // distribution
     110        } // align_alloc
     111
     112        T * align_alloc( size_t align, size_t dim, char fill ) {
     113                if ( fill == '\0' ) {
     114                        return (T *)cmemalign( align, dim, sizeof(T) );
     115                } else {
     116                        return (T *)memset( (T *)memalign( align, dim * sizeof(T) ), (int)fill, dim * sizeof(T) );
     117                } // if
     118        } // align_alloc
     119} // distribution
     120
     121forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill );
     122
    159123
    160124static inline forall( dtype T | sized(T) ) {
    161125        // data, non-array types
     126
    162127        T * memset( T * dest, char fill ) {
    163128                return (T *)memset( dest, fill, sizeof(T) );
     
    171136static inline forall( dtype T | sized(T) ) {
    172137        // data, array types
     138
    173139        T * amemset( T dest[], char fill, size_t dim ) {
    174140                return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
     
    193159
    194160static inline {
    195         int strto( const char sptr[], char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
    196         unsigned int strto( const char sptr[], char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
    197         long int strto( const char sptr[], char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
    198         unsigned long int strto( const char sptr[], char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
    199         long long int strto( const char sptr[], char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
    200         unsigned long long int strto( const char sptr[], char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
    201 
    202         float strto( const char sptr[], char ** eptr ) { return strtof( sptr, eptr ); }
    203         double strto( const char sptr[], char ** eptr ) { return strtod( sptr, eptr ); }
    204         long double strto( const char sptr[], char ** eptr ) { return strtold( sptr, eptr ); }
    205 } // distribution
    206 
    207 float _Complex strto( const char sptr[], char ** eptr );
    208 double _Complex strto( const char sptr[], char ** eptr );
    209 long double _Complex strto( const char sptr[], char ** eptr );
     161        int strto( const char * sptr, char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
     162        unsigned int strto( const char * sptr, char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
     163        long int strto( const char * sptr, char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
     164        unsigned long int strto( const char * sptr, char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
     165        long long int strto( const char * sptr, char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
     166        unsigned long long int strto( const char * sptr, char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
     167
     168        float strto( const char * sptr, char ** eptr ) { return strtof( sptr, eptr ); }
     169        double strto( const char * sptr, char ** eptr ) { return strtod( sptr, eptr ); }
     170        long double strto( const char * sptr, char ** eptr ) { return strtold( sptr, eptr ); }
     171} // distribution
     172
     173float _Complex strto( const char * sptr, char ** eptr );
     174double _Complex strto( const char * sptr, char ** eptr );
     175long double _Complex strto( const char * sptr, char ** eptr );
    210176
    211177static inline {
    212         int ato( const char sptr[] ) { return (int)strtol( sptr, 0p, 10 ); }
    213         unsigned int ato( const char sptr[] ) { return (unsigned int)strtoul( sptr, 0p, 10 ); }
    214         long int ato( const char sptr[] ) { return strtol( sptr, 0p, 10 ); }
    215         unsigned long int ato( const char sptr[] ) { return strtoul( sptr, 0p, 10 ); }
    216         long long int ato( const char sptr[] ) { return strtoll( sptr, 0p, 10 ); }
    217         unsigned long long int ato( const char sptr[] ) { return strtoull( sptr, 0p, 10 ); }
    218 
    219         float ato( const char sptr[] ) { return strtof( sptr, 0p ); }
    220         double ato( const char sptr[] ) { return strtod( sptr, 0p ); }
    221         long double ato( const char sptr[] ) { return strtold( sptr, 0p ); }
    222 
    223         float _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
    224         double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
    225         long double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
     178        int ato( const char * sptr ) { return (int)strtol( sptr, 0, 10 ); }
     179        unsigned int ato( const char * sptr ) { return (unsigned int)strtoul( sptr, 0, 10 ); }
     180        long int ato( const char * sptr ) { return strtol( sptr, 0, 10 ); }
     181        unsigned long int ato( const char * sptr ) { return strtoul( sptr, 0, 10 ); }
     182        long long int ato( const char * sptr ) { return strtoll( sptr, 0, 10 ); }
     183        unsigned long long int ato( const char * sptr ) { return strtoull( sptr, 0, 10 ); }
     184
     185        float ato( const char * sptr ) { return strtof( sptr, 0 ); }
     186        double ato( const char * sptr ) { return strtod( sptr, 0 ); }
     187        long double ato( const char * sptr ) { return strtold( sptr, 0 ); }
     188
     189        float _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
     190        double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
     191        long double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
    226192} // distribution
    227193
Note: See TracChangeset for help on using the changeset viewer.