Ignore:
Timestamp:
May 11, 2020, 1:53:29 PM (5 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
504a7dc
Parents:
b7d6a36 (diff), a7b486b (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into relaxed_ready

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.hfa

    rb7d6a36 r6a490b2  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Feb  4 08:27:01 2020
    13 // Update Count     : 401
     12// Last Modified On : Thu Apr 16 22:44:05 2020
     13// Update Count     : 432
    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        size_t malloc_size( void * addr );                                      // CFA heap
     28        void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap
    2529        void * memset( void * dest, int fill, size_t size ); // string.h
    2630        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
     31        void * resize( void * oaddr, size_t size );                     // CFA heap
    2832} // extern "C"
    2933
     34void * resize( void * oaddr, size_t nalign, size_t size ); // CFA heap
    3035void * realloc( void * oaddr, size_t nalign, size_t size ); // CFA heap
    3136
     
    4045
    4146static inline forall( dtype T | sized(T) ) {
    42         // C dynamic allocation
     47        // Cforall safe equivalents, i.e., implicit size specification
    4348
    4449        T * malloc( void ) {
     
    7176                return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign
    7277        } // posix_memalign
    73 
    74         // Cforall dynamic allocation
     78} // distribution
     79
     80static inline forall( dtype T | sized(T) ) {
     81        // Cforall safe general allocation, fill, resize, array
    7582
    7683        T * alloc( void ) {
     
    8390        } // alloc
    8491
    85         T * alloc( T ptr[], size_t dim ) {                                      // realloc
    86                 return (T *)(void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
     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
    87111        } // alloc
    88112
     
    112136forall( dtype T | sized(T) ) {
    113137        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
    114139} // distribution
    115140
     
    125150        T * alloc_align( T ptr[], size_t align ) {                      // aligned realloc array
    126151                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
    127157        } // alloc_align
    128158
     
    155185
    156186forall( 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
    157189        T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); // aligned realloc array with fill
    158 } // distribution
    159 
    160 static inline forall( dtype T | sized(T) ) {
    161         // data, non-array types
     190        T * alloc_align_set( T ptr[], size_t align, size_t dim, T fill ); // aligned realloc array with fill
     191} // distribution
     192
     193static inline forall( dtype T | sized(T) ) {
     194        // Cforall safe initialization/copy, i.e., implicit size specification, non-array types
    162195        T * memset( T * dest, char fill ) {
    163196                return (T *)memset( dest, fill, sizeof(T) );
     
    170203
    171204static inline forall( dtype T | sized(T) ) {
    172         // data, array types
     205        // Cforall safe initialization/copy, i.e., implicit size specification, array types
    173206        T * amemset( T dest[], char fill, size_t dim ) {
    174207                return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
     
    180213} // distribution
    181214
    182 // allocation/deallocation and constructor/destructor, non-array types
     215// Cforall allocation/deallocation and constructor/destructor, non-array types
    183216forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
    184217forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );
    185218forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
    186219
    187 // allocation/deallocation and constructor/destructor, array types
     220// Cforall allocation/deallocation and constructor/destructor, array types
    188221forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
    189222forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
Note: See TracChangeset for help on using the changeset viewer.