Changeset b0a0ee4


Ignore:
Timestamp:
Jul 20, 2020, 8:20:21 PM (4 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
2b23d78
Parents:
92850ef
Message:

refactor duplicate code into cpp macros

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.hfa

    r92850ef rb0a0ee4  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jul 20 14:29:21 2020
    13 // Update Count     : 464
     12// Last Modified On : Mon Jul 20 19:04:33 2020
     13// Update Count     : 472
    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
    4156static inline forall( dtype T | sized(T) ) {
    4257        // Cforall safe equivalents, i.e., implicit size specification
    4358
    4459        T * malloc( void ) {
    45                 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
    46                 else return (T *)memalign( _Alignof(T), sizeof(T) );
     60                $VAR_ALLOC( malloc, memalign );
    4761        } // malloc
    4862
    4963        T * aalloc( size_t 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) );
     64                $ARRAY_ALLOC( aalloc, amemalign, dim );
    5265        } // aalloc
    5366
    5467        T * calloc( size_t 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) );
     68                $ARRAY_ALLOC( calloc, cmemalign, dim );
    5769        } // calloc
    5870
    5971        T * resize( T * ptr, size_t size ) {                            // CFA resize, eliminate return-type cast
    60                 if ( unlikely( size == 0 ) || unlikely( ptr == 0p ) ) { // special cases
    61                         if ( unlikely( size == 0 ) ) free( ptr );
    62                         if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( size ); // C malloc
    63                         else return (T *)memalign( _Alignof(T), size ); // C memalign
    64                 } // if
     72                $RE_SPECIALS( ptr, size, malloc, memalign );
    6573                return (T *)(void *)resize( (void *)ptr, size ); // CFA resize
    6674        } // resize
    6775
    6876        T * realloc( T * ptr, size_t size ) {                           // CFA realloc, eliminate return-type cast
    69                 if ( unlikely( size == 0 ) || unlikely( ptr == 0p ) ) { // special cases
    70                         if ( unlikely( size == 0 ) ) free( ptr );
    71                         if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( size ); // C malloc
    72                         else return (T *)memalign( _Alignof(T), size ); // C memalign
    73                 } // if
     77                $RE_SPECIALS( ptr, size, malloc, memalign );
    7478                return (T *)(void *)realloc( (void *)ptr, size ); // C realloc
    7579        } // realloc
Note: See TracChangeset for help on using the changeset viewer.