Changeset 6065b3aa for src/libcfa


Ignore:
Timestamp:
Jun 1, 2017, 10:58:18 PM (8 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
49c9773
Parents:
6016c87
Message:

second attempt at memory-allocation routines

Location:
src/libcfa
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/libcfa/stdlib

    r6016c87 r6065b3aa  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue May 30 09:07:35 2017
    13 // Update Count     : 164
     12// Last Modified On : Thu Jun  1 22:46:43 2017
     13// Update Count     : 216
    1414//
    1515
     
    2828//---------------------------------------
    2929
    30 extern "C" { void * memset( void * dest, int c, size_t size ); } // use default C routine for void *
    31 
    3230// allocation, non-array types
    3331static inline forall( dtype T | sized(T) ) T * malloc( void ) {
     
    3533        return (T *)(void *)malloc( (size_t)sizeof(T) );        // C malloc
    3634} // malloc
    37 static inline forall( dtype T | sized(T) ) T * malloc( char fill ) {
     35
     36extern "C" { void * calloc( size_t dim, size_t size ); } // default C routine
     37static inline forall( dtype T | sized(T) ) T * calloc( size_t dim ) {
    3838        //printf( "X2\n" );
     39        return (T *)(void *)calloc( dim, sizeof(T) );           // C cmalloc
     40}
     41
     42extern "C" { void * realloc( void * ptr, size_t size ); } // default C routine for void *
     43static inline forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) {
     44        //printf( "X3\n" );
     45        return (T *)(void *)realloc( (void *)ptr, size );
     46}
     47
     48extern "C" { void * memalign( size_t align, size_t size ); } // use default C routine for void *
     49static inline forall( dtype T | sized(T) ) T * memalign( size_t align ) {
     50        //printf( "X4\n" );
     51        return (T *)memalign( align, sizeof(T) );
     52} // memalign
     53
     54static inline forall( dtype T | sized(T) ) T * aligned_alloc( size_t align ) {
     55        //printf( "X5\n" );
     56        return (T *)memalign( align, sizeof(T) );
     57} // aligned_alloc
     58
     59extern "C" { int posix_memalign( void ** ptr, size_t align, size_t size ); } // use default C routine for void *
     60static inline forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t align ) {
     61        //printf( "X6\n" );
     62        return posix_memalign( (void **)ptr, align, sizeof(T) );
     63} // posix_memalign
     64
     65
     66extern "C" { void * memset( void * dest, int c, size_t size ); } // use default C routine for void *
     67
     68static inline forall( dtype T | sized(T) ) T * alloc( void ) {
     69        //printf( "X7\n" );
     70        return (T *)(void *)malloc( (size_t)sizeof(T) );        // C malloc
     71} // alloc
     72static inline forall( dtype T | sized(T) ) T * alloc( char fill ) {
     73        //printf( "X8\n" );
    3974        T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) );     // C malloc
    4075    return memset( ptr, (int)fill, sizeof(T) );                 // initial with fill value
    41 } // malloc
    42 
    43 // allocation, array types
    44 extern "C" { void * calloc( size_t dim, size_t size ); } // use default C routine for void *
    45 static inline forall( dtype T | sized(T) ) T * calloc( size_t dim ) {
    46         //printf( "X3\n" );
    47         return (T *)(void *)calloc( dim, sizeof(T) );           // C cmalloc
    48 }
    49 static inline forall( dtype T | sized(T) ) T * amalloc( size_t dim ) { // alternative name
    50         //printf( "X4\n" );
     76} // alloc
     77
     78static inline forall( dtype T | sized(T) ) T * alloc( size_t dim ) {
     79        //printf( "X9\n" );
    5180        return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
    52 } // amalloc
    53 static inline forall( dtype T | sized(T) ) T * amalloc( size_t dim, char fill ) { // alternative name
    54         //printf( "X5\n" );
     81} // alloc
     82static inline forall( dtype T | sized(T) ) T * alloc( size_t dim, char fill ) {
     83        //printf( "X10\n" );
    5584        T * ptr = (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
    5685    return memset( ptr, (int)fill, dim * sizeof(T) );
    57 } // amalloc
    58 
    59 // resize, non-array types
    60 extern "C" { void * realloc( void * ptr, size_t size ); } // use default C routine for void *
    61 static inline forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) {
    62         //printf( "X5.5\n" );
    63         return (T *)(void *)realloc( (void *)ptr, size );
    64 }
    65 forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, char fill );
    66 static inline forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size ) { // alternative name
    67         //printf( "X7\n" );
    68         return realloc( ptr, size );
    69 } // malloc
    70 static inline forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size, char fill ) { // alternative name
    71         //printf( "X8\n" );
    72         return realloc( ptr, size, fill );
    73 } // malloc
    74 
    75 // resize, array types
    76 static inline forall( dtype T | sized(T) ) T * amalloc( T * ptr, size_t dim ) {
    77         //printf( "X9\n" );
    78         return malloc( ptr, dim * (size_t)sizeof(T) );
    79 } // amalloc
    80 static inline forall( dtype T | sized(T) ) T * amalloc( T * ptr, size_t dim, char fill ) {
    81         //printf( "X10\n" );
    82         return malloc( ptr, dim * (size_t)sizeof(T), fill );
    83 } // amalloc
    84 
    85 // alignment, non-array types
    86 extern "C" { void * memalign( size_t alignment, size_t size ); } // use default C routine for void *
    87 static inline forall( dtype T | sized(T) ) T * memalign( size_t alignment ) {
     86} // alloc
     87
     88static inline forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim ) {
    8889        //printf( "X11\n" );
    89         return (T *)memalign( alignment, sizeof(T) );
    90 } // memalign
    91 static inline forall( dtype T | sized(T) ) T * memalign( size_t alignment, char fill ) {
    92         //printf( "X12\n" );
    93     T * ptr = (T *)memalign( alignment, sizeof(T) );
     90        return (void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
     91} // alloc
     92forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill );
     93
     94static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align ) {
     95        //printf( "X13\n" );
     96        return (T *)memalign( align, sizeof(T) );
     97} // align_alloc
     98static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, char fill ) {
     99        //printf( "X14\n" );
     100    T * ptr = (T *)memalign( align, sizeof(T) );
    94101    return memset( ptr, (int)fill, sizeof(T) );
    95 } // memalign
    96 static inline forall( dtype T | sized(T) ) T * aligned_alloc( size_t alignment ) {
    97         //printf( "X13\n" );
    98         return (T *)memalign( alignment, sizeof(T) );
    99 } // aligned_alloc
    100 extern "C" { int posix_memalign( void ** ptr, size_t alignment, size_t size ); } // use default C routine for void *
    101 static inline forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t alignment ) {
    102         //printf( "X14\n" );
    103         return posix_memalign( (void **)ptr, alignment, sizeof(T) );
    104 } // posix_memalign
    105 
    106 // alignment, array types
    107 static inline forall( dtype T | sized(T) ) T * amemalign( size_t alignment, size_t dim ) {
     102} // align_alloc
     103
     104static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim ) {
    108105        //printf( "X15\n" );
    109         return (T *)memalign( alignment, dim * sizeof(T) );
    110 } // amemalign
    111 static inline forall( dtype T | sized(T) ) T * amemalign( size_t alignment, size_t dim, char fill ) {
     106        return (T *)memalign( align, dim * sizeof(T) );
     107} // align_alloc
     108static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim, char fill ) {
    112109        //printf( "X16\n" );
    113     T * ptr = (T *)memalign( alignment, dim * sizeof(T) );
     110    T * ptr = (T *)memalign( align, dim * sizeof(T) );
    114111    return memset( ptr, (int)fill, dim * sizeof(T) );
    115 } // amemalign
     112} // align_alloc
     113
    116114
    117115// data, non-array types
     
    127125
    128126// data, array types
    129 static inline forall( dtype T | sized(T) ) T * amemset( T * dest, size_t dim, char c ) {
     127static inline forall( dtype T | sized(T) ) T * memset( T dest[], size_t dim, char c ) {
    130128        //printf( "X19\n" );
    131129        return memset( dest, c, dim * sizeof(T) );
    132 } // amemset
    133 static inline forall( dtype T | sized(T) ) T * amemcpy( T * dest, const T * src, size_t dim ) {
     130} // memset
     131static inline forall( dtype T | sized(T) ) T * memcpy( T dest[], const T src[], size_t dim ) {
    134132        //printf( "X20\n" );
    135         return memcpy( dest, src, dim * sizeof(T) );
    136 } // amemcpy
    137 
    138 // allocation/deallocation and constructor/destructor
    139 forall( dtype T, ttype Params | sized(T) | { void ?{}(T *, Params); } ) T * new( Params p );
     133        return (void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
     134} // memcpy
     135
     136// allocation/deallocation and constructor/destructor, non-array types
     137forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } ) T * new( Params p );
    140138forall( dtype T | { void ^?{}( T * ); } ) void delete( T * ptr );
    141139forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } ) void delete( T * ptr, Params rest );
     140
     141// allocation/deallocation and constructor/destructor, array types
     142forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } ) T * anew( size_t dim, Params p );
     143forall( dtype T | sized(T) | { void ^?{}( T * ); } ) void adelete( size_t dim, T arr[] );
     144forall( dtype T | sized(T) | { void ^?{}( T * ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );
    142145
    143146//---------------------------------------
  • TabularUnified src/libcfa/stdlib.c

    r6016c87 r6065b3aa  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue May 30 09:07:56 2017
    13 // Update Count     : 237
     12// Last Modified On : Thu Jun  1 21:52:57 2017
     13// Update Count     : 280
    1414//
    1515
     
    2828
    2929// resize, non-array types
    30 forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, char fill ) { // alternative realloc with fill value
    31         //printf( "X6\n" );
     30forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ) {
    3231        size_t olen = malloc_usable_size( ptr );                        // current allocation
    33     char * nptr = (void *)realloc( (void *)ptr, size ); // C realloc
     32    char * nptr = (void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
    3433        size_t nlen = malloc_usable_size( nptr );                       // new allocation
    3534        if ( nlen > olen ) {                                                            // larger ?
     
    3736        } //
    3837    return (T *)nptr;
    39 } // realloc
    40 
    41 // allocation/deallocation and constructor/destructor
    42 forall( dtype T, ttype Params | sized(T) | { void ?{}( T *, Params ); } )
     38} // alloc
     39
     40// allocation/deallocation and constructor/destructor, non-array types
     41forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } )
    4342T * new( Params p ) {
    44         return ((T *)malloc()){ p };
     43        return (malloc()){ p };                                                         // run constructor
    4544} // new
    4645
    4746forall( dtype T | { void ^?{}( T * ); } )
    4847void delete( T * ptr ) {
    49         if ( ptr ) {
     48        if ( ptr ) {                                                                            // ignore null
    5049                ^ptr{};                                                                                 // run destructor
    5150                free( ptr );
     
    5554forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } )
    5655void delete( T * ptr, Params rest ) {
    57         if ( ptr ) {
     56        if ( ptr ) {                                                                            // ignore null
    5857                ^ptr{};                                                                                 // run destructor
    5958                free( ptr );
     
    6160        delete( rest );
    6261} // delete
     62
     63
     64// allocation/deallocation and constructor/destructor, array types
     65forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } )
     66T * anew( size_t dim, Params p ) {
     67        T *arr = alloc( dim );
     68        for ( unsigned int i = 0; i < dim; i += 1 ) {
     69                (&arr[i]){ p };                                                                 // run constructor
     70        } // for
     71        return arr;
     72} // anew
     73
     74forall( dtype T | sized(T) | { void ^?{}( T * ); } )
     75void adelete( size_t dim, T arr[] ) {
     76        if ( arr ) {                                                                            // ignore null
     77                for ( int i = dim - 1; i >= 0; i -= 1 ) {               // reverse allocation order, must be unsigned
     78                        ^(&arr[i]){};                                                           // run destructor
     79                } // for
     80                free( arr );
     81        } // if
     82} // adelete
     83
     84forall( dtype T | sized(T) | { void ^?{}( T * ); }, ttype Params | { void adelete( Params ); } )
     85void adelete( size_t dim, T arr[], Params rest ) {
     86        if ( arr ) {                                                                            // ignore null
     87                for ( int i = dim - 1; i >= 0; i -= 1 ) {               // reverse allocation order, must be unsigned
     88                        ^(&arr[i]){};                                                           // run destructor
     89                } // for
     90                free( arr );
     91        } // if
     92        adelete( rest );
     93} // adelete
    6394
    6495//---------------------------------------
Note: See TracChangeset for help on using the changeset viewer.