Ignore:
Timestamp:
Jan 7, 2021, 2:55:57 PM (5 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
58fe85a
Parents:
bdfc032 (diff), 44e37ef (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into dkobets-vector

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/stdlib.cfa

    rbdfc032 reef8dfb  
    1010// Created On       : Thu Jan 28 17:10:29 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Nov 20 17:22:47 2019
    13 // Update Count     : 485
     12// Last Modified On : Thu Nov 12 07:46:09 2020
     13// Update Count     : 503
    1414//
    1515
     
    2020#define _XOPEN_SOURCE 600                                                               // posix_memalign, *rand48
    2121#include <string.h>                                                                             // memcpy, memset
    22 #include <malloc.h>                                                                             // malloc_usable_size
    2322//#include <math.h>                                                                             // fabsf, fabs, fabsl
    2423#include <complex.h>                                                                    // _Complex_I
     
    2726//---------------------------------------
    2827
    29 forall( 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
    53 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
    54 T * new( Params p ) {
    55         return &(*malloc()){ p };                                                       // run constructor
    56 } // new
    57 
    58 forall( dtype T | sized(T) | { void ^?{}( T & ); } )
    59 void delete( T * ptr ) {
    60         if ( ptr ) {                                                                            // ignore null
    61                 ^(*ptr){};                                                                              // run destructor
    62                 free( ptr );
    63         } // if
    64 } // delete
    65 
    66 forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } )
    67 void 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
    77 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
    78 T * anew( size_t dim, Params p ) {
     28// Cforall allocation/deallocation and constructor/destructor, array types
     29
     30forall( dtype T | sized(T), ttype TT | { void ?{}( T &, TT ); } )
     31T * anew( size_t dim, TT p ) {
    7932        T * arr = alloc( dim );
    8033        for ( unsigned int i = 0; i < dim; i += 1 ) {
     
    8538
    8639forall( dtype T | sized(T) | { void ^?{}( T & ); } )
    87 void adelete( size_t dim, T arr[] ) {
     40void adelete( T arr[] ) {
    8841        if ( arr ) {                                                                            // ignore null
     42                size_t dim = malloc_size( arr ) / sizeof( T );
    8943                for ( int i = dim - 1; i >= 0; i -= 1 ) {               // reverse allocation order, must be unsigned
    9044                        ^(arr[i]){};                                                            // run destructor
     
    9448} // adelete
    9549
    96 forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } )
    97 void adelete( size_t dim, T arr[], Params rest ) {
     50forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype TT | { void adelete( TT ); } )
     51void adelete( T arr[], TT rest ) {
    9852        if ( arr ) {                                                                            // ignore null
     53                size_t dim = malloc_size( arr ) / sizeof( T );
    9954                for ( int i = dim - 1; i >= 0; i -= 1 ) {               // reverse allocation order, must be unsigned
    10055                        ^(arr[i]){};                                                            // run destructor
     
    10762//---------------------------------------
    10863
    109 float _Complex strto( const char * sptr, char ** eptr ) {
     64float _Complex strto( const char sptr[], char ** eptr ) {
    11065        float re, im;
    11166        char * eeptr;
     
    11873} // strto
    11974
    120 double _Complex strto( const char * sptr, char ** eptr ) {
     75double _Complex strto( const char sptr[], char ** eptr ) {
    12176        double re, im;
    12277        char * eeptr;
     
    12984} // strto
    13085
    131 long double _Complex strto( const char * sptr, char ** eptr ) {
     86long double _Complex strto( const char sptr[], char ** eptr ) {
    13287        long double re, im;
    13388        char * eeptr;
     
    255210extern "C" {                                                                                    // override C version
    256211        void srandom( unsigned int seed ) { srand48( (long int)seed ); }
    257         long int random( void ) { return mrand48(); }
     212        long int random( void ) { return mrand48(); }           // GENERATES POSITIVE AND NEGATIVE VALUES
    258213} // extern "C"
    259214
Note: See TracChangeset for help on using the changeset viewer.