Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.cfa

    r94429f8 rd74369b  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Nov 12 07:46:09 2020
    13 // Update Count     : 503
     12// Last Modified On : Wed Nov 20 17:22:47 2019
     13// Update Count     : 485
    1414//
    1515
     
    2020#define _XOPEN_SOURCE 600                                                               // posix_memalign, *rand48
    2121#include <string.h>                                                                             // memcpy, memset
     22#include <malloc.h>                                                                             // malloc_usable_size
    2223//#include <math.h>                                                                             // fabsf, fabs, fabsl
    2324#include <complex.h>                                                                    // _Complex_I
     
    2627//---------------------------------------
    2728
    28 // Cforall allocation/deallocation and constructor/destructor, array types
    29 
    30 forall( dtype T | sized(T), ttype TT | { void ?{}( T &, TT ); } )
    31 T * anew( size_t dim, TT p ) {
     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                void * nptr = (void *)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( (char *)nptr + olen, (int)fill, nlen - olen ); // initialize added storage
     36                } // if
     37                return (T *)nptr;
     38        } // alloc_set
     39
     40        T * alloc_align_set( T ptr[], size_t align, char fill ) { // aligned realloc with fill
     41                size_t olen = malloc_usable_size( ptr );                // current allocation
     42                void * nptr = (void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc
     43                // char * nptr = alloc_align( ptr, align );
     44                size_t nlen = malloc_usable_size( nptr );               // new allocation
     45                if ( nlen > olen ) {                                                    // larger ?
     46                        memset( (char *)nptr + olen, (int)fill, nlen - olen ); // initialize added storage
     47                } // if
     48                return (T *)nptr;
     49        } // alloc_align_set
     50} // distribution
     51
     52// allocation/deallocation and constructor/destructor, non-array types
     53forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
     54T * new( Params p ) {
     55        return &(*malloc()){ p };                                                       // run constructor
     56} // new
     57
     58forall( dtype T | sized(T) | { void ^?{}( T & ); } )
     59void delete( T * ptr ) {
     60        if ( ptr ) {                                                                            // ignore null
     61                ^(*ptr){};                                                                              // run destructor
     62                free( ptr );
     63        } // if
     64} // delete
     65
     66forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } )
     67void delete( T * ptr, Params rest ) {
     68        if ( ptr ) {                                                                            // ignore null
     69                ^(*ptr){};                                                                              // run destructor
     70                free( ptr );
     71        } // if
     72        delete( rest );
     73} // delete
     74
     75
     76// allocation/deallocation and constructor/destructor, array types
     77forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
     78T * anew( size_t dim, Params p ) {
    3279        T * arr = alloc( dim );
    3380        for ( unsigned int i = 0; i < dim; i += 1 ) {
     
    3885
    3986forall( dtype T | sized(T) | { void ^?{}( T & ); } )
    40 void adelete( T arr[] ) {
     87void adelete( size_t dim, T arr[] ) {
    4188        if ( arr ) {                                                                            // ignore null
    42                 size_t dim = malloc_size( arr ) / sizeof( T );
    4389                for ( int i = dim - 1; i >= 0; i -= 1 ) {               // reverse allocation order, must be unsigned
    4490                        ^(arr[i]){};                                                            // run destructor
     
    4894} // adelete
    4995
    50 forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype TT | { void adelete( TT ); } )
    51 void adelete( T arr[], TT rest ) {
     96forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } )
     97void adelete( size_t dim, T arr[], Params rest ) {
    5298        if ( arr ) {                                                                            // ignore null
    53                 size_t dim = malloc_size( arr ) / sizeof( T );
    5499                for ( int i = dim - 1; i >= 0; i -= 1 ) {               // reverse allocation order, must be unsigned
    55100                        ^(arr[i]){};                                                            // run destructor
     
    62107//---------------------------------------
    63108
    64 float _Complex strto( const char sptr[], char ** eptr ) {
     109float _Complex strto( const char * sptr, char ** eptr ) {
    65110        float re, im;
    66111        char * eeptr;
     
    73118} // strto
    74119
    75 double _Complex strto( const char sptr[], char ** eptr ) {
     120double _Complex strto( const char * sptr, char ** eptr ) {
    76121        double re, im;
    77122        char * eeptr;
     
    84129} // strto
    85130
    86 long double _Complex strto( const char sptr[], char ** eptr ) {
     131long double _Complex strto( const char * sptr, char ** eptr ) {
    87132        long double re, im;
    88133        char * eeptr;
     
    210255extern "C" {                                                                                    // override C version
    211256        void srandom( unsigned int seed ) { srand48( (long int)seed ); }
    212         long int random( void ) { return mrand48(); }           // GENERATES POSITIVE AND NEGATIVE VALUES
     257        long int random( void ) { return mrand48(); }
    213258} // extern "C"
    214259
Note: See TracChangeset for help on using the changeset viewer.