Ignore:
Timestamp:
Oct 21, 2019, 10:18:15 AM (3 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
arm-eh, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
eb5a115
Parents:
1aa6ecb
Message:

start allocation updates for arrays and alignment

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.cfa

    r1aa6ecb rcafb687  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jun 24 17:34:44 2019
    13 // Update Count     : 462
     12// Last Modified On : Sun Oct 20 18:41:23 2019
     13// Update Count     : 473
    1414//
    1515
     
    2727//---------------------------------------
    2828
    29 // resize, non-array types
    30 forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ) {
    31         size_t olen = malloc_usable_size( ptr );                        // current allocation
    32     char * nptr = (void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
    33         size_t nlen = malloc_usable_size( nptr );                       // new allocation
    34         if ( nlen > olen ) {                                                            // larger ?
    35                 memset( nptr + olen, (int)fill, nlen - olen );  // initialize added storage
    36         } //
    37     return (T *)nptr;
    38 } // alloc
     29forall( dtype T | sized(T) ) {
     30        T * alloc_set( T ptr[], size_t dim, char fill ) {       // realloc array with fill
     31                size_t olen = malloc_usable_size( ptr );                // current allocation
     32                char * nptr = (char *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
     33                size_t nlen = malloc_usable_size( nptr );               // new allocation
     34                if ( nlen > olen ) {                                                    // larger ?
     35                        memset( nptr + olen, (int)fill, nlen - olen ); // initialize added storage
     36                } // if
     37                return (T *)nptr;
     38        } // alloc_set
     39
     40        T * alloc_align( T ptr[], size_t align ) {                      // aligned realloc array
     41                char * nptr;
     42                size_t alignment = malloc_alignment( ptr );
     43                if ( align != alignment ) {
     44                        size_t olen = malloc_usable_size( ptr );        // current allocation
     45                        nptr = (char *)memalign( align, olen );
     46                        size_t nlen = malloc_usable_size( nptr );       // new allocation
     47                        size_t lnth = olen < nlen ? olen : nlen;        // min
     48                        memcpy( nptr, ptr, lnth );                                      // initialize storage
     49                } else {
     50                        nptr = (char *)ptr;
     51                } // if
     52                return (T *)nptr;
     53        } // alloc_align
     54
     55        T * alloc_align( T ptr[], size_t align, size_t dim ) { // aligned realloc array
     56                char * nptr;
     57                size_t alignment = malloc_alignment( ptr );
     58                if ( align != alignment ) {
     59                        size_t olen = malloc_usable_size( ptr );        // current allocation
     60                        nptr = (char *)memalign( align, dim * sizeof(T) );
     61                        size_t nlen = malloc_usable_size( nptr );       // new allocation
     62                        size_t lnth = olen < nlen ? olen : nlen;        // min
     63                        memcpy( nptr, ptr, lnth );                                      // initialize storage
     64                } else {
     65                        nptr = (char *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc
     66                } // if
     67                return (T *)nptr;
     68        } // alloc_align
     69
     70        T * alloc_align_set( T ptr[], size_t align, char fill ) { // aligned realloc with fill
     71                size_t olen = malloc_usable_size( ptr );                // current allocation
     72                char * nptr = alloc_align( ptr, align );
     73                size_t nlen = malloc_usable_size( nptr );               // new allocation
     74                if ( nlen > olen ) {                                                    // larger ?
     75                        memset( nptr + olen, (int)fill, nlen - olen ); // initialize added storage
     76                } // if
     77                return (T *)nptr;
     78        } // alloc_align_set
     79} // distribution
    3980
    4081// allocation/deallocation and constructor/destructor, non-array types
    4182forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
    4283T * new( Params p ) {
    43         return &(*malloc()){ p };                                                               // run constructor
     84        return &(*malloc()){ p };                                                       // run constructor
    4485} // new
    4586
     
    4788void delete( T * ptr ) {
    4889        if ( ptr ) {                                                                            // ignore null
    49                 ^(*ptr){};                                                                                      // run destructor
     90                ^(*ptr){};                                                                              // run destructor
    5091                free( ptr );
    5192        } // if
     
    5596void delete( T * ptr, Params rest ) {
    5697        if ( ptr ) {                                                                            // ignore null
    57                 ^(*ptr){};                                                                                      // run destructor
     98                ^(*ptr){};                                                                              // run destructor
    5899                free( ptr );
    59100        } // if
Note: See TracChangeset for help on using the changeset viewer.