Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.hfa

    rd8d8f20 raabb846  
    99// Author           : Peter A. Buhr
    1010// Created On       : Thu Jan 28 17:12:35 2016
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Jul 21 07:58:05 2020
    13 // Update Count     : 475
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Tue Jun  2 16:47:00 2020
     13// Update Count     : 451
    1414//
    1515
     
    3939//---------------------------------------
    4040
    41 // Macro because of returns
    42 #define $VAR_ALLOC( allocation, alignment ) \
    43         if ( _Alignof(T) <= libAlign() ) return (T *)(void *)allocation( (size_t)sizeof(T) ); /* C allocation */ \
    44         else return (T *)alignment( _Alignof(T), sizeof(T) )
    45 
    46 #define $ARRAY_ALLOC( allocation, alignment, dim ) \
    47         if ( _Alignof(T) <= libAlign() ) return (T *)(void *)allocation( dim, (size_t)sizeof(T) ); /* C allocation */ \
    48         else return (T *)alignment( _Alignof(T), dim, sizeof(T) )
    49 
    50 #define $RE_SPECIALS( ptr, size, allocation, alignment ) \
    51         if ( unlikely( size == 0 ) || unlikely( ptr == 0p ) ) { \
    52                 if ( unlikely( size == 0 ) ) free( ptr ); \
    53                 $VAR_ALLOC( malloc, memalign ); \
    54         } /* if */
    55 
    5641static inline forall( dtype T | sized(T) ) {
    5742        // Cforall safe equivalents, i.e., implicit size specification
    5843
    5944        T * malloc( void ) {
    60                 $VAR_ALLOC( malloc, memalign );
     45                if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
     46                else return (T *)memalign( _Alignof(T), sizeof(T) );
    6147        } // malloc
    6248
    6349        T * aalloc( size_t dim ) {
    64                 $ARRAY_ALLOC( aalloc, amemalign, dim );
     50                if ( _Alignof(T) <= libAlign() ) return (T *)(void *)aalloc( dim, (size_t)sizeof(T) ); // CFA aalloc
     51                else return (T *)amemalign( _Alignof(T), dim, sizeof(T) );
    6552        } // aalloc
    6653
    6754        T * calloc( size_t dim ) {
    68                 $ARRAY_ALLOC( calloc, cmemalign, dim );
     55                if ( _Alignof(T) <= libAlign() )return (T *)(void *)calloc( dim, sizeof(T) ); // C calloc
     56                else return (T *)cmemalign( _Alignof(T), dim, sizeof(T) );
    6957        } // calloc
    7058
    71         T * resize( T * ptr, size_t size ) {                            // CFA resize, eliminate return-type cast
    72                 $RE_SPECIALS( ptr, size, malloc, memalign );
    73                 return (T *)(void *)resize( (void *)ptr, size ); // CFA resize
     59        T * resize( T * ptr, size_t size ) {                            // CFA realloc, eliminate return-type cast
     60                return (T *)(void *)resize( (void *)ptr, size ); // C realloc
    7461        } // resize
    7562
    7663        T * realloc( T * ptr, size_t size ) {                           // CFA realloc, eliminate return-type cast
    77                 $RE_SPECIALS( ptr, size, malloc, memalign );
    7864                return (T *)(void *)realloc( (void *)ptr, size ); // C realloc
    7965        } // realloc
     
    132118
    133119        T * alloc( T ptr[], size_t dim, bool copy = true ) {
    134                 if ( copy ) {
    135                         return realloc( ptr, dim * sizeof(T) );         // CFA realloc
     120                if ( copy ) {                                                                   // realloc
     121                        return (T *)(void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
    136122                } else {
    137                         return resize( ptr, dim * sizeof(T) );          // CFA resize
     123                        return resize( ptr, dim * sizeof(T) );          // resize
    138124                } // if
    139125        } // alloc
     
    160146                return (T *)memcpy( (T *)alloc( dim ), fill, dim * sizeof(T) ); // initialize with fill value
    161147        } // alloc
    162 
    163         T * alloc_set( T ptr[], size_t dim, char fill ) {       // realloc array with fill
    164                 size_t osize = malloc_size( ptr );                              // current allocation
    165                 size_t nsize = dim * sizeof(T);                                 // new allocation
    166                 T * nptr = realloc( ptr, nsize );                               // CFA realloc
    167                 if ( nsize > osize ) {                                                  // larger ?
    168                         memset( (char *)nptr + osize, (int)fill, nsize - osize ); // initialize added storage
    169                 } // if
    170                 return (T *)nptr;
    171         } // alloc_set
    172 
    173         T * alloc_set( T ptr[], size_t dim, T & fill ) {        // realloc array with fill
    174                 size_t odim = malloc_size( ptr ) / sizeof(T);   // current dimension
    175                 size_t nsize = dim * sizeof(T);                                 // new allocation
    176                 size_t ndim = nsize / sizeof(T);                                // new dimension
    177                 T * nptr = realloc( ptr, nsize );                               // CFA realloc
    178                 if ( ndim > odim ) {                                                    // larger ?
    179                         for ( i; odim ~ ndim ) {
    180                                 memcpy( &nptr[i], &fill, sizeof(T) );   // initialize with fill value
    181                         } // for
    182                 } // if
    183                 return (T *)nptr;
    184         } // alloc_align_set
     148} // distribution
     149
     150forall( dtype T | sized(T) ) {
     151        T * alloc_set( T ptr[], size_t dim, char fill );        // realloc array with fill
     152        T * alloc_set( T ptr[], size_t dim, T fill );           // realloc array with fill
    185153} // distribution
    186154
     
    228196                return (T *)memcpy( (T *)alloc_align( align, dim ), fill, dim * sizeof(T) );
    229197        } // alloc_align
    230 
    231         T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ) {
    232                 size_t osize = malloc_size( ptr );                              // current allocation
    233                 size_t nsize = dim * sizeof(T);                                 // new allocation
    234                 T * nptr = realloc( ptr, align, nsize );                // CFA realloc
    235                 if ( nsize > osize ) {                                                  // larger ?
    236                         memset( (char *)nptr + osize, (int)fill, nsize - osize ); // initialize added storage
    237                 } // if
    238                 return (T *)nptr;
    239         } // alloc_align_set
    240 
    241         T * alloc_align_set( T ptr[], size_t align, size_t dim, T & fill ) {
    242                 size_t odim = malloc_size( ptr ) / sizeof(T);   // current dimension
    243                 size_t nsize = dim * sizeof(T);                                 // new allocation
    244                 size_t ndim = nsize / sizeof(T);                                // new dimension
    245                 T * nptr = realloc( ptr, align, nsize );                // CFA realloc
    246                 if ( ndim > odim ) {                                                    // larger ?
    247                         for ( i; odim ~ ndim ) {
    248                                 memcpy( &nptr[i], &fill, sizeof(T) );   // initialize with fill value
    249                         } // for
    250                 } // if
    251                 return (T *)nptr;
    252         } // alloc_align_set
     198} // distribution
     199
     200forall( dtype T | sized(T) ) {
     201        T * alloc_align_set( T ptr[], size_t align, char fill ); // aligned realloc with fill
     202        T * alloc_align_set( T ptr[], size_t align, T fill ); // aligned realloc with fill
     203        T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); // aligned realloc array with fill
     204        T * alloc_align_set( T ptr[], size_t align, size_t dim, T fill ); // aligned realloc array with fill
    253205} // distribution
    254206
Note: See TracChangeset for help on using the changeset viewer.