Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.hfa

    rcafb687 rcfbc703d  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Oct 20 22:57:33 2019
    13 // Update Count     : 390
     12// Last Modified On : Wed Apr  1 18:38:41 2020
     13// Update Count     : 429
    1414//
    1515
     
    2121#include <stdlib.h>                                                                             // *alloc, strto*, ato*
    2222
     23// Reduce includes by explicitly defining these routines.
    2324extern "C" {
    2425        void * memalign( size_t align, size_t size );           // malloc.h
     26        size_t malloc_usable_size( void * ptr );                        // malloc.h
     27        void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap
    2528        void * memset( void * dest, int fill, size_t size ); // string.h
    2629        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
     30        void * resize( void * oaddr, size_t size );                     // CFA heap
    2831} // extern "C"
     32
     33void * resize( void * oaddr, size_t nalign, size_t size ); // CFA heap
     34void * realloc( void * oaddr, size_t nalign, size_t size ); // CFA heap
    2935
    3036//---------------------------------------
     
    3844
    3945static inline forall( dtype T | sized(T) ) {
    40         // C dynamic allocation
     46        // Cforall safe equivalents, i.e., implicit size specification
    4147
    4248        T * malloc( void ) {
     
    5056        } // calloc
    5157
    52         T * realloc( T * ptr, size_t size ) {
    53                 if ( unlikely( ptr == 0 ) ) return malloc();
     58        T * realloc( T * ptr, size_t size ) {                           // CFA realloc, eliminate return-type cast
    5459                return (T *)(void *)realloc( (void *)ptr, size ); // C realloc
    5560        } // realloc
     
    5964        } // memalign
    6065
     66        T * cmemalign( size_t align, size_t dim  ) {
     67                return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign
     68        } // cmemalign
     69
    6170        T * aligned_alloc( size_t align ) {
    6271                return (T *)aligned_alloc( align, sizeof(T) );  // C aligned_alloc
     
    6675                return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign
    6776        } // posix_memalign
    68 
    69         // Cforall dynamic allocation
     77} // distribution
     78
     79static inline forall( dtype T | sized(T) ) {
     80        // Cforall safe general allocation, fill, resize, array
    7081
    7182        T * alloc( void ) {
     
    7889        } // alloc
    7990
    80         T * alloc( T ptr[], size_t dim ) {                                      // realloc
    81                 return realloc( ptr, dim * sizeof(T) );
     91        forall( dtype S | sized(S) )
     92        T * alloc( S ptr[], size_t dim = 1 ) {                          // singleton/array resize
     93                size_t len = malloc_usable_size( ptr );                 // current bucket size
     94                if ( sizeof(T) * dim > len ) {                                  // not enough space ?
     95                        T * temp = alloc( dim );                                        // new storage
     96                        free( ptr );                                                            // free old storage
     97                        return temp;
     98                } else {
     99                        return (T *)ptr;
     100                } // if
     101        } // alloc
     102
     103        T * alloc( T ptr[], size_t dim, bool copy = true ) {
     104                if ( copy ) {                                                                   // realloc
     105                        return (T *)(void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
     106                } else {
     107                        struct __Unknown {};
     108                        return alloc( (__Unknown *)ptr, dim );          // reuse, cheat making T/S different types
     109                } // if
    82110        } // alloc
    83111
     
    107135forall( dtype T | sized(T) ) {
    108136        T * alloc_set( T ptr[], size_t dim, char fill );        // realloc array with fill
     137        T * alloc_set( T ptr[], size_t dim, T fill );           // realloc array with fill
    109138} // distribution
    110139
     
    116145        T * alloc_align( size_t align, size_t dim ) {
    117146                return (T *)memalign( align, dim * sizeof(T) );
     147        } // alloc_align
     148
     149        T * alloc_align( T ptr[], size_t align ) {                      // aligned realloc array
     150                return (T *)(void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc
     151        } // alloc_align
     152
     153        forall( dtype S | sized(S) )
     154        T * alloc_align( S ptr[], size_t align ) {                      // aligned reuse array
     155                return (T *)(void *)resize( (void *)ptr, align, sizeof(T) ); // CFA realloc
     156        } // alloc_align
     157
     158        T * alloc_align( T ptr[], size_t align, size_t dim ) { // aligned realloc array
     159                return (T *)(void *)realloc( (void *)ptr, align, dim * sizeof(T) ); // CFA realloc
    118160        } // alloc_align
    119161
     
    142184
    143185forall( dtype T | sized(T) ) {
    144         T * alloc_align( T ptr[], size_t align );                       // realign
    145         T * alloc_align( T ptr[], size_t align, size_t dim ); // aligned realloc array
     186        T * alloc_align_set( T ptr[], size_t align, char fill ); // aligned realloc with fill
     187        T * alloc_align_set( T ptr[], size_t align, T fill ); // aligned realloc with fill
    146188        T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); // aligned realloc array with fill
    147 } // distribution
    148 
    149 static inline forall( dtype T | sized(T) ) {
    150         // data, non-array types
     189        T * alloc_align_set( T ptr[], size_t align, size_t dim, T fill ); // aligned realloc array with fill
     190} // distribution
     191
     192static inline forall( dtype T | sized(T) ) {
     193        // Cforall safe initialization/copy, i.e., implicit size specification, non-array types
    151194        T * memset( T * dest, char fill ) {
    152195                return (T *)memset( dest, fill, sizeof(T) );
     
    159202
    160203static inline forall( dtype T | sized(T) ) {
    161         // data, array types
     204        // Cforall safe initialization/copy, i.e., implicit size specification, array types
    162205        T * amemset( T dest[], char fill, size_t dim ) {
    163206                return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
     
    169212} // distribution
    170213
    171 // allocation/deallocation and constructor/destructor, non-array types
     214// Cforall allocation/deallocation and constructor/destructor, non-array types
    172215forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
    173216forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );
    174217forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
    175218
    176 // allocation/deallocation and constructor/destructor, array types
     219// Cforall allocation/deallocation and constructor/destructor, array types
    177220forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
    178221forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
     
    182225
    183226static inline {
    184         int strto( const char * sptr, char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
    185         unsigned int strto( const char * sptr, char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
    186         long int strto( const char * sptr, char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
    187         unsigned long int strto( const char * sptr, char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
    188         long long int strto( const char * sptr, char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
    189         unsigned long long int strto( const char * sptr, char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
    190 
    191         float strto( const char * sptr, char ** eptr ) { return strtof( sptr, eptr ); }
    192         double strto( const char * sptr, char ** eptr ) { return strtod( sptr, eptr ); }
    193         long double strto( const char * sptr, char ** eptr ) { return strtold( sptr, eptr ); }
    194 } // distribution
    195 
    196 float _Complex strto( const char * sptr, char ** eptr );
    197 double _Complex strto( const char * sptr, char ** eptr );
    198 long double _Complex strto( const char * sptr, char ** eptr );
     227        int strto( const char sptr[], char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
     228        unsigned int strto( const char sptr[], char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
     229        long int strto( const char sptr[], char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
     230        unsigned long int strto( const char sptr[], char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
     231        long long int strto( const char sptr[], char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
     232        unsigned long long int strto( const char sptr[], char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
     233
     234        float strto( const char sptr[], char ** eptr ) { return strtof( sptr, eptr ); }
     235        double strto( const char sptr[], char ** eptr ) { return strtod( sptr, eptr ); }
     236        long double strto( const char sptr[], char ** eptr ) { return strtold( sptr, eptr ); }
     237} // distribution
     238
     239float _Complex strto( const char sptr[], char ** eptr );
     240double _Complex strto( const char sptr[], char ** eptr );
     241long double _Complex strto( const char sptr[], char ** eptr );
    199242
    200243static inline {
    201         int ato( const char * sptr ) { return (int)strtol( sptr, 0, 10 ); }
    202         unsigned int ato( const char * sptr ) { return (unsigned int)strtoul( sptr, 0, 10 ); }
    203         long int ato( const char * sptr ) { return strtol( sptr, 0, 10 ); }
    204         unsigned long int ato( const char * sptr ) { return strtoul( sptr, 0, 10 ); }
    205         long long int ato( const char * sptr ) { return strtoll( sptr, 0, 10 ); }
    206         unsigned long long int ato( const char * sptr ) { return strtoull( sptr, 0, 10 ); }
    207 
    208         float ato( const char * sptr ) { return strtof( sptr, 0 ); }
    209         double ato( const char * sptr ) { return strtod( sptr, 0 ); }
    210         long double ato( const char * sptr ) { return strtold( sptr, 0 ); }
    211 
    212         float _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
    213         double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
    214         long double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
     244        int ato( const char sptr[] ) { return (int)strtol( sptr, 0p, 10 ); }
     245        unsigned int ato( const char sptr[] ) { return (unsigned int)strtoul( sptr, 0p, 10 ); }
     246        long int ato( const char sptr[] ) { return strtol( sptr, 0p, 10 ); }
     247        unsigned long int ato( const char sptr[] ) { return strtoul( sptr, 0p, 10 ); }
     248        long long int ato( const char sptr[] ) { return strtoll( sptr, 0p, 10 ); }
     249        unsigned long long int ato( const char sptr[] ) { return strtoull( sptr, 0p, 10 ); }
     250
     251        float ato( const char sptr[] ) { return strtof( sptr, 0p ); }
     252        double ato( const char sptr[] ) { return strtod( sptr, 0p ); }
     253        long double ato( const char sptr[] ) { return strtold( sptr, 0p ); }
     254
     255        float _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
     256        double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
     257        long double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
    215258} // distribution
    216259
Note: See TracChangeset for help on using the changeset viewer.