Changeset f3fc631f for src/libcfa


Ignore:
Timestamp:
May 30, 2017, 9:13:53 AM (7 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:
fab700b
Parents:
2ab67b9
Message:

first attempt new storage management routines

Location:
src/libcfa
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/stdlib

    r2ab67b9 rf3fc631f  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed May 24 18:06:27 2017
    13 // Update Count     : 115
     12// Last Modified On : Tue May 30 09:07:35 2017
     13// Update Count     : 164
    1414//
    1515
     
    2828//---------------------------------------
    2929
    30 forall( dtype T | sized(T) ) T * malloc( void );
    31 forall( dtype T | sized(T) ) T * malloc( char fill );
    32 forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size );
    33 forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size, unsigned char fill );
    34 extern "C" { void * calloc( size_t nmemb, size_t size ); } // use default C routine for void *
    35 forall( dtype T | sized(T) ) T * calloc( size_t nmemb );
     30extern "C" { void * memset( void * dest, int c, size_t size ); } // use default C routine for void *
     31
     32// allocation, non-array types
     33static inline forall( dtype T | sized(T) ) T * malloc( void ) {
     34        //printf( "X1\n" );
     35        return (T *)(void *)malloc( (size_t)sizeof(T) );        // C malloc
     36} // malloc
     37static inline forall( dtype T | sized(T) ) T * malloc( char fill ) {
     38        //printf( "X2\n" );
     39        T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) );     // C malloc
     40    return memset( ptr, (int)fill, sizeof(T) );                 // initial with fill value
     41} // malloc
     42
     43// allocation, array types
     44extern "C" { void * calloc( size_t dim, size_t size ); } // use default C routine for void *
     45static 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}
     49static inline forall( dtype T | sized(T) ) T * amalloc( size_t dim ) { // alternative name
     50        //printf( "X4\n" );
     51        return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
     52} // amalloc
     53static inline forall( dtype T | sized(T) ) T * amalloc( size_t dim, char fill ) { // alternative name
     54        //printf( "X5\n" );
     55        T * ptr = (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
     56    return memset( ptr, (int)fill, dim * sizeof(T) );
     57} // amalloc
     58
     59// resize, non-array types
    3660extern "C" { void * realloc( void * ptr, size_t size ); } // use default C routine for void *
    37 forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size );
    38 forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, unsigned char fill );
    39 
    40 forall( dtype T | sized(T) ) T * aligned_alloc( size_t alignment );
    41 forall( dtype T | sized(T) ) T * memalign( size_t alignment );          // deprecated
    42 forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t alignment );
    43 
     61static 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}
     65forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, char fill );
     66static 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
     70static 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
     76static 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
     80static 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
     86extern "C" { void * memalign( size_t alignment, size_t size ); } // use default C routine for void *
     87static inline forall( dtype T | sized(T) ) T * memalign( size_t alignment ) {
     88        //printf( "X11\n" );
     89        return (T *)memalign( alignment, sizeof(T) );
     90} // memalign
     91static 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) );
     94    return memset( ptr, (int)fill, sizeof(T) );
     95} // memalign
     96static 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
     100extern "C" { int posix_memalign( void ** ptr, size_t alignment, size_t size ); } // use default C routine for void *
     101static 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
     107static inline forall( dtype T | sized(T) ) T * amemalign( size_t alignment, size_t dim ) {
     108        //printf( "X15\n" );
     109        return (T *)memalign( alignment, dim * sizeof(T) );
     110} // amemalign
     111static inline forall( dtype T | sized(T) ) T * amemalign( size_t alignment, size_t dim, char fill ) {
     112        //printf( "X16\n" );
     113    T * ptr = (T *)memalign( alignment, dim * sizeof(T) );
     114    return memset( ptr, (int)fill, dim * sizeof(T) );
     115} // amemalign
     116
     117// data, non-array types
     118static inline forall( dtype T | sized(T) ) T * memset( T * dest, char c ) {
     119        //printf( "X17\n" );
     120        return memset( dest, c, sizeof(T) );
     121} // memset
     122extern "C" { void * memcpy( void * dest, const void * src, size_t size ); } // use default C routine for void *
     123static inline forall( dtype T | sized(T) ) T * memcpy( T * dest, const T * src ) {
     124        //printf( "X18\n" );
     125        return memcpy( dest, src, sizeof(T) );
     126} // memcpy
     127
     128// data, array types
     129static inline forall( dtype T | sized(T) ) T * amemset( T * dest, size_t dim, char c ) {
     130        //printf( "X19\n" );
     131        return memset( dest, c, dim * sizeof(T) );
     132} // amemset
     133static inline forall( dtype T | sized(T) ) T * amemcpy( T * dest, const T * src, size_t dim ) {
     134        //printf( "X20\n" );
     135        return memcpy( dest, src, dim * sizeof(T) );
     136} // amemcpy
     137
     138// allocation/deallocation and constructor/destructor
    44139forall( dtype T, ttype Params | sized(T) | { void ?{}(T *, Params); } ) T * new( Params p );
    45 forall( dtype T | { void ^?{}(T *); } ) void delete( T * ptr );
    46 forall( dtype T, ttype Params | { void ^?{}(T *); void delete(Params); } ) void delete( T * ptr, Params rest );
     140forall( dtype T | { void ^?{}( T * ); } ) void delete( T * ptr );
     141forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } ) void delete( T * ptr, Params rest );
    47142
    48143//---------------------------------------
     
    77172
    78173forall( otype T | { int ?<?( T, T ); } )
    79 T * bsearch( T key, const T * arr, size_t dimension );
    80 
    81 forall( otype T | { int ?<?( T, T ); } )
    82 unsigned int bsearch( T key, const T * arr, size_t dimension );
    83 
    84 
    85 forall( otype T | { int ?<?( T, T ); } )
    86 void qsort( const T * arr, size_t dimension );
     174T * bsearch( T key, const T * arr, size_t dim );
     175
     176forall( otype T | { int ?<?( T, T ); } )
     177unsigned int bsearch( T key, const T * arr, size_t dim );
     178
     179
     180forall( otype T | { int ?<?( T, T ); } )
     181void qsort( const T * arr, size_t dim );
    87182
    88183//---------------------------------------
  • src/libcfa/stdlib.c

    r2ab67b9 rf3fc631f  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed May 24 18:13:15 2017
    13 // Update Count     : 198
     12// Last Modified On : Tue May 30 09:07:56 2017
     13// Update Count     : 237
    1414//
    1515
     
    2121#define _XOPEN_SOURCE 600                                                               // posix_memalign, *rand48
    2222#include <stdlib.h>                                                                             // malloc, free, calloc, realloc, memalign, posix_memalign, bsearch
    23 #include <string.h>                                                                             // memset
     23#include <string.h>                                                                             // memcpy, memset
    2424#include <malloc.h>                                                                             // malloc_usable_size
    2525#include <math.h>                                                                               // fabsf, fabs, fabsl
     
    2727} // extern "C"
    2828
    29 forall( dtype T | sized(T) ) T * malloc( void ) {               // type-safe
    30     return (T *)(void *)malloc( (size_t)sizeof(T) );
    31 } // malloc
    32 
    33 forall( dtype T | sized(T) ) T * malloc( char fill ) {  // initial with fill value (like calloc)
    34         T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) );
    35     return memset( ptr, (int)fill, sizeof(T) );
    36 } // malloc
    37 
    38 forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size ) { // alternative realloc
    39     return (T *)realloc( ptr, size );
    40 } // malloc
    41 
    42 forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size, unsigned char fill ) { // alternative realloc with fill value
    43     return (T *)realloc( ptr, size, fill );
    44 } // malloc
    45 
    46 
    47 forall( dtype T | sized(T) ) T * calloc( size_t nmemb ) { // type-safe array initialization with fill 0
    48     return (T *)calloc( nmemb, sizeof(T) );
    49 } // calloc
    50 
    51 
    52 forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) { // type-safe
    53     return (T *)(void *)realloc( (void *)ptr, size );
     29// resize, non-array types
     30forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, char fill ) { // alternative realloc with fill value
     31        //printf( "X6\n" );
     32        size_t olen = malloc_usable_size( ptr );                        // current allocation
     33    char * nptr = (void *)realloc( (void *)ptr, size ); // C realloc
     34        size_t nlen = malloc_usable_size( nptr );                       // new allocation
     35        if ( nlen > olen ) {                                                            // larger ?
     36                memset( nptr + olen, (int)fill, nlen - olen );  // initialize added storage
     37        } //
     38    return (T *)nptr;
    5439} // realloc
    5540
    56 forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, unsigned char fill ) { // alternative realloc with fill value
    57     char * nptr = (T *)(void *)realloc( (void *)ptr, size );
    58     size_t unused = malloc_usable_size( nptr );
    59     memset( nptr + size - unused, (int)fill, unused );  // initialize any new storage
    60     return nptr;
    61 } // realloc
    62 
    63 
    64 forall( dtype T | sized(T) ) T * aligned_alloc( size_t alignment ) { // aligned allocation
    65     return (T *)memalign( alignment, sizeof(T) );
    66 } // aligned_alloc
    67 
    68 forall( dtype T | sized(T) ) T * memalign( size_t alignment ) {
    69     return (T *)memalign( alignment, sizeof(T) );
    70 } // memalign
    71 
    72 forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t alignment ) {
    73     return posix_memalign( (void **)ptr, alignment, sizeof(T) );
    74 } // posix_memalign
    75 
    76 
    77 forall( dtype T, ttype Params | sized(T) | { void ?{}( T *, Params ); } ) //  new
     41// allocation/deallocation and constructor/destructor
     42forall( dtype T, ttype Params | sized(T) | { void ?{}( T *, Params ); } )
    7843T * new( Params p ) {
    7944        return ((T *)malloc()){ p };
    8045} // new
    8146
    82 forall( dtype T | { void ^?{}(T *); } )                                 // delete
     47forall( dtype T | { void ^?{}( T * ); } )
    8348void delete( T * ptr ) {
    8449        if ( ptr ) {
    85                 ^ptr{};
     50                ^ptr{};                                                                                 // run destructor
    8651                free( ptr );
    87         }
     52        } // if
    8853} // delete
    8954
    90 forall( dtype T, ttype Params | { void ^?{}(T *); void delete(Params); } )
     55forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } )
    9156void delete( T * ptr, Params rest ) {
    9257        if ( ptr ) {
    93                 ^ptr{};
     58                ^ptr{};                                                                                 // run destructor
    9459                free( ptr );
    95         }
     60        } // if
    9661        delete( rest );
    9762} // delete
     
    242207
    243208forall( otype T | { int ?<?( T, T ); } )
    244 T * bsearch( T key, const T * arr, size_t dimension ) {
     209T * bsearch( T key, const T * arr, size_t dim ) {
    245210        int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
    246         return (T *)bsearch( &key, arr, dimension, sizeof(T), comp );
     211        return (T *)bsearch( &key, arr, dim, sizeof(T), comp );
    247212} // bsearch
    248213
    249214forall( otype T | { int ?<?( T, T ); } )
    250 unsigned int bsearch( T key, const T * arr, size_t dimension ) {
    251         T *result = bsearch( key, arr, dimension );
    252         return result ? result - arr : dimension;                       // pointer subtraction includes sizeof(T)
     215unsigned int bsearch( T key, const T * arr, size_t dim ) {
     216        T *result = bsearch( key, arr, dim );
     217        return result ? result - arr : dim;                                     // pointer subtraction includes sizeof(T)
    253218} // bsearch
    254219
    255220forall( otype T | { int ?<?( T, T ); } )
    256 void qsort( const T * arr, size_t dimension ) {
     221void qsort( const T * arr, size_t dim ) {
    257222        int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
    258         qsort( arr, dimension, sizeof(T), comp );
     223        qsort( arr, dim, sizeof(T), comp );
    259224} // qsort
    260225
Note: See TracChangeset for help on using the changeset viewer.