Changeset eef8dfb for libcfa/src/stdlib.cfa
- Timestamp:
- Jan 7, 2021, 2:55:57 PM (5 years ago)
- 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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/stdlib.cfa
rbdfc032 reef8dfb 10 10 // Created On : Thu Jan 28 17:10:29 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Nov 20 17:22:47 201913 // Update Count : 48512 // Last Modified On : Thu Nov 12 07:46:09 2020 13 // Update Count : 503 14 14 // 15 15 … … 20 20 #define _XOPEN_SOURCE 600 // posix_memalign, *rand48 21 21 #include <string.h> // memcpy, memset 22 #include <malloc.h> // malloc_usable_size23 22 //#include <math.h> // fabsf, fabs, fabsl 24 23 #include <complex.h> // _Complex_I … … 27 26 //--------------------------------------- 28 27 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 30 forall( dtype T | sized(T), ttype TT | { void ?{}( T &, TT ); } ) 31 T * anew( size_t dim, TT p ) { 79 32 T * arr = alloc( dim ); 80 33 for ( unsigned int i = 0; i < dim; i += 1 ) { … … 85 38 86 39 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) 87 void adelete( size_t dim,T arr[] ) {40 void adelete( T arr[] ) { 88 41 if ( arr ) { // ignore null 42 size_t dim = malloc_size( arr ) / sizeof( T ); 89 43 for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned 90 44 ^(arr[i]){}; // run destructor … … 94 48 } // adelete 95 49 96 forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params); } )97 void adelete( size_t dim, T arr[], Paramsrest ) {50 forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype TT | { void adelete( TT ); } ) 51 void adelete( T arr[], TT rest ) { 98 52 if ( arr ) { // ignore null 53 size_t dim = malloc_size( arr ) / sizeof( T ); 99 54 for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned 100 55 ^(arr[i]){}; // run destructor … … 107 62 //--------------------------------------- 108 63 109 float _Complex strto( const char * sptr, char ** eptr ) {64 float _Complex strto( const char sptr[], char ** eptr ) { 110 65 float re, im; 111 66 char * eeptr; … … 118 73 } // strto 119 74 120 double _Complex strto( const char * sptr, char ** eptr ) {75 double _Complex strto( const char sptr[], char ** eptr ) { 121 76 double re, im; 122 77 char * eeptr; … … 129 84 } // strto 130 85 131 long double _Complex strto( const char * sptr, char ** eptr ) {86 long double _Complex strto( const char sptr[], char ** eptr ) { 132 87 long double re, im; 133 88 char * eeptr; … … 255 210 extern "C" { // override C version 256 211 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 258 213 } // extern "C" 259 214
Note:
See TracChangeset
for help on using the changeset viewer.