Ignore:
Timestamp:
Apr 1, 2020, 9:32:21 PM (5 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:
6d43cdde
Parents:
5137f9f
Message:

add resize and more "alloc" routines

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.hfa

    r5137f9f rcfbc703d  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Mar  5 11:29:06 2020
    13 // Update Count     : 407
     12// Last Modified On : Wed Apr  1 18:38:41 2020
     13// Update Count     : 429
    1414//
    1515
     
    2424extern "C" {
    2525        void * memalign( size_t align, size_t size );           // malloc.h
    26     void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap
     26        size_t malloc_usable_size( void * ptr );                        // malloc.h
     27        void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap
    2728        void * memset( void * dest, int fill, size_t size ); // string.h
    2829        void * memcpy( void * dest, const void * src, size_t size ); // string.h
     30        void * resize( void * oaddr, size_t size );                     // CFA heap
    2931} // extern "C"
    3032
     33void * resize( void * oaddr, size_t nalign, size_t size ); // CFA heap
    3134void * realloc( void * oaddr, size_t nalign, size_t size ); // CFA heap
    3235
     
    7275                return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign
    7376        } // posix_memalign
    74 
     77} // distribution
     78
     79static inline forall( dtype T | sized(T) ) {
    7580        // Cforall safe general allocation, fill, resize, array
    7681
     
    8489        } // alloc
    8590
    86         T * alloc( T ptr[], size_t dim ) {                                      // realloc
    87                 return (T *)(void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
     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
    88110        } // alloc
    89111
     
    113135forall( dtype T | sized(T) ) {
    114136        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
    115138} // distribution
    116139
     
    126149        T * alloc_align( T ptr[], size_t align ) {                      // aligned realloc array
    127150                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
    128156        } // alloc_align
    129157
     
    156184
    157185forall( 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
    158188        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
    159190} // distribution
    160191
Note: See TracChangeset for help on using the changeset viewer.