Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.hfa

    r76e2113 re3fea42  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Apr 16 22:44:05 2020
    13 // Update Count     : 432
     12// Last Modified On : Tue Feb  4 08:27:01 2020
     13// Update Count     : 401
    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         size_t malloc_size( void * addr );                                      // CFA heap
    28         void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap
    2925        void * memset( void * dest, int fill, size_t size ); // string.h
    3026        void * memcpy( void * dest, const void * src, size_t size ); // string.h
    31         void * resize( void * oaddr, size_t size );                     // CFA heap
     27    void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap
    3228} // extern "C"
    3329
    34 void * resize( void * oaddr, size_t nalign, size_t size ); // CFA heap
    3530void * realloc( void * oaddr, size_t nalign, size_t size ); // CFA heap
    3631
     
    4540
    4641static inline forall( dtype T | sized(T) ) {
    47         // Cforall safe equivalents, i.e., implicit size specification
     42        // C dynamic allocation
    4843
    4944        T * malloc( void ) {
     
    7671                return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign
    7772        } // posix_memalign
    78 } // distribution
    79 
    80 static inline forall( dtype T | sized(T) ) {
    81         // Cforall safe general allocation, fill, resize, array
     73
     74        // Cforall dynamic allocation
    8275
    8376        T * alloc( void ) {
     
    9083        } // alloc
    9184
    92         forall( dtype S | sized(S) )
    93         T * alloc( S ptr[], size_t dim = 1 ) {                          // singleton/array resize
    94                 size_t len = malloc_usable_size( ptr );                 // current bucket size
    95                 if ( sizeof(T) * dim > len ) {                                  // not enough space ?
    96                         T * temp = alloc( dim );                                        // new storage
    97                         free( ptr );                                                            // free old storage
    98                         return temp;
    99                 } else {
    100                         return (T *)ptr;
    101                 } // if
    102         } // alloc
    103 
    104         T * alloc( T ptr[], size_t dim, bool copy = true ) {
    105                 if ( copy ) {                                                                   // realloc
    106                         return (T *)(void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
    107                 } else {
    108                         struct __Unknown {};
    109                         return alloc( (__Unknown *)ptr, dim );          // reuse, cheat making T/S different types
    110                 } // if
     85        T * alloc( T ptr[], size_t dim ) {                                      // realloc
     86                return (T *)(void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
    11187        } // alloc
    11288
     
    136112forall( dtype T | sized(T) ) {
    137113        T * alloc_set( T ptr[], size_t dim, char fill );        // realloc array with fill
    138         T * alloc_set( T ptr[], size_t dim, T fill );           // realloc array with fill
    139114} // distribution
    140115
     
    150125        T * alloc_align( T ptr[], size_t align ) {                      // aligned realloc array
    151126                return (T *)(void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc
    152         } // alloc_align
    153 
    154         forall( dtype S | sized(S) )
    155         T * alloc_align( S ptr[], size_t align ) {                      // aligned reuse array
    156                 return (T *)(void *)resize( (void *)ptr, align, sizeof(T) ); // CFA realloc
    157127        } // alloc_align
    158128
     
    185155
    186156forall( dtype T | sized(T) ) {
    187         T * alloc_align_set( T ptr[], size_t align, char fill ); // aligned realloc with fill
    188         T * alloc_align_set( T ptr[], size_t align, T fill ); // aligned realloc with fill
    189157        T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); // aligned realloc array with fill
    190         T * alloc_align_set( T ptr[], size_t align, size_t dim, T fill ); // aligned realloc array with fill
    191 } // distribution
    192 
    193 static inline forall( dtype T | sized(T) ) {
    194         // Cforall safe initialization/copy, i.e., implicit size specification, non-array types
     158} // distribution
     159
     160static inline forall( dtype T | sized(T) ) {
     161        // data, non-array types
    195162        T * memset( T * dest, char fill ) {
    196163                return (T *)memset( dest, fill, sizeof(T) );
     
    203170
    204171static inline forall( dtype T | sized(T) ) {
    205         // Cforall safe initialization/copy, i.e., implicit size specification, array types
     172        // data, array types
    206173        T * amemset( T dest[], char fill, size_t dim ) {
    207174                return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
     
    213180} // distribution
    214181
    215 // Cforall allocation/deallocation and constructor/destructor, non-array types
     182// allocation/deallocation and constructor/destructor, non-array types
    216183forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
    217184forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );
    218185forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
    219186
    220 // Cforall allocation/deallocation and constructor/destructor, array types
     187// allocation/deallocation and constructor/destructor, array types
    221188forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
    222189forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
Note: See TracChangeset for help on using the changeset viewer.