Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.hfa

    rcfbc703d rcafb687  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Apr  1 18:38:41 2020
    13 // Update Count     : 429
     12// Last Modified On : Sun Oct 20 22:57:33 2019
     13// Update Count     : 390
    1414//
    1515
     
    2121#include <stdlib.h>                                                                             // *alloc, strto*, ato*
    2222
    23 // Reduce includes by explicitly defining these routines.
    2423extern "C" {
    2524        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
    2825        void * memset( void * dest, int fill, size_t size ); // string.h
    2926        void * memcpy( void * dest, const void * src, size_t size ); // string.h
    30         void * resize( void * oaddr, size_t size );                     // CFA heap
     27    void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap
    3128} // extern "C"
    32 
    33 void * resize( void * oaddr, size_t nalign, size_t size ); // CFA heap
    34 void * realloc( void * oaddr, size_t nalign, size_t size ); // CFA heap
    3529
    3630//---------------------------------------
     
    4438
    4539static inline forall( dtype T | sized(T) ) {
    46         // Cforall safe equivalents, i.e., implicit size specification
     40        // C dynamic allocation
    4741
    4842        T * malloc( void ) {
     
    5650        } // calloc
    5751
    58         T * realloc( T * ptr, size_t size ) {                           // CFA realloc, eliminate return-type cast
     52        T * realloc( T * ptr, size_t size ) {
     53                if ( unlikely( ptr == 0 ) ) return malloc();
    5954                return (T *)(void *)realloc( (void *)ptr, size ); // C realloc
    6055        } // realloc
     
    6459        } // memalign
    6560
    66         T * cmemalign( size_t align, size_t dim  ) {
    67                 return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign
    68         } // cmemalign
    69 
    7061        T * aligned_alloc( size_t align ) {
    7162                return (T *)aligned_alloc( align, sizeof(T) );  // C aligned_alloc
     
    7566                return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign
    7667        } // posix_memalign
    77 } // distribution
    78 
    79 static inline forall( dtype T | sized(T) ) {
    80         // Cforall safe general allocation, fill, resize, array
     68
     69        // Cforall dynamic allocation
    8170
    8271        T * alloc( void ) {
     
    8978        } // alloc
    9079
    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
     80        T * alloc( T ptr[], size_t dim ) {                                      // realloc
     81                return realloc( ptr, dim * sizeof(T) );
    11082        } // alloc
    11183
     
    135107forall( dtype T | sized(T) ) {
    136108        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
    138109} // distribution
    139110
     
    145116        T * alloc_align( size_t align, size_t dim ) {
    146117                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
    160118        } // alloc_align
    161119
     
    184142
    185143forall( dtype T | sized(T) ) {
    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
     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
    188146        T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); // aligned realloc array with fill
    189         T * alloc_align_set( T ptr[], size_t align, size_t dim, T fill ); // aligned realloc array with fill
    190 } // distribution
    191 
    192 static inline forall( dtype T | sized(T) ) {
    193         // Cforall safe initialization/copy, i.e., implicit size specification, non-array types
     147} // distribution
     148
     149static inline forall( dtype T | sized(T) ) {
     150        // data, non-array types
    194151        T * memset( T * dest, char fill ) {
    195152                return (T *)memset( dest, fill, sizeof(T) );
     
    202159
    203160static inline forall( dtype T | sized(T) ) {
    204         // Cforall safe initialization/copy, i.e., implicit size specification, array types
     161        // data, array types
    205162        T * amemset( T dest[], char fill, size_t dim ) {
    206163                return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
     
    212169} // distribution
    213170
    214 // Cforall allocation/deallocation and constructor/destructor, non-array types
     171// allocation/deallocation and constructor/destructor, non-array types
    215172forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
    216173forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );
    217174forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
    218175
    219 // Cforall allocation/deallocation and constructor/destructor, array types
     176// allocation/deallocation and constructor/destructor, array types
    220177forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
    221178forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
     
    225182
    226183static inline {
    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 
    239 float _Complex strto( const char sptr[], char ** eptr );
    240 double _Complex strto( const char sptr[], char ** eptr );
    241 long double _Complex strto( const char sptr[], char ** eptr );
     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
     196float _Complex strto( const char * sptr, char ** eptr );
     197double _Complex strto( const char * sptr, char ** eptr );
     198long double _Complex strto( const char * sptr, char ** eptr );
    242199
    243200static inline {
    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 ); }
     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 ); }
    258215} // distribution
    259216
Note: See TracChangeset for help on using the changeset viewer.