Changes in libcfa/src/stdlib.hfa [cafb687:cfbc703d]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/stdlib.hfa
rcafb687 rcfbc703d 10 10 // Created On : Thu Jan 28 17:12:35 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Oct 20 22:57:33 201913 // Update Count : 39012 // Last Modified On : Wed Apr 1 18:38:41 2020 13 // Update Count : 429 14 14 // 15 15 … … 21 21 #include <stdlib.h> // *alloc, strto*, ato* 22 22 23 // Reduce includes by explicitly defining these routines. 23 24 extern "C" { 24 25 void * memalign( size_t align, size_t size ); // malloc.h 26 size_t malloc_usable_size( void * ptr ); // malloc.h 27 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap 25 28 void * memset( void * dest, int fill, size_t size ); // string.h 26 29 void * memcpy( void * dest, const void * src, size_t size ); // string.h 27 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize );// CFA heap30 void * resize( void * oaddr, size_t size ); // CFA heap 28 31 } // extern "C" 32 33 void * resize( void * oaddr, size_t nalign, size_t size ); // CFA heap 34 void * realloc( void * oaddr, size_t nalign, size_t size ); // CFA heap 29 35 30 36 //--------------------------------------- … … 38 44 39 45 static inline forall( dtype T | sized(T) ) { 40 // C dynamic allocation46 // Cforall safe equivalents, i.e., implicit size specification 41 47 42 48 T * malloc( void ) { … … 50 56 } // calloc 51 57 52 T * realloc( T * ptr, size_t size ) { 53 if ( unlikely( ptr == 0 ) ) return malloc(); 58 T * realloc( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast 54 59 return (T *)(void *)realloc( (void *)ptr, size ); // C realloc 55 60 } // realloc … … 59 64 } // memalign 60 65 66 T * cmemalign( size_t align, size_t dim ) { 67 return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign 68 } // cmemalign 69 61 70 T * aligned_alloc( size_t align ) { 62 71 return (T *)aligned_alloc( align, sizeof(T) ); // C aligned_alloc … … 66 75 return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign 67 76 } // posix_memalign 68 69 // Cforall dynamic allocation 77 } // distribution 78 79 static inline forall( dtype T | sized(T) ) { 80 // Cforall safe general allocation, fill, resize, array 70 81 71 82 T * alloc( void ) { … … 78 89 } // alloc 79 90 80 T * alloc( T ptr[], size_t dim ) { // realloc 81 return realloc( ptr, dim * sizeof(T) ); 91 forall( dtype S | sized(S) ) 92 T * alloc( S ptr[], size_t dim = 1 ) { // singleton/array resize 93 size_t len = malloc_usable_size( ptr ); // current bucket size 94 if ( sizeof(T) * dim > len ) { // not enough space ? 95 T * temp = alloc( dim ); // new storage 96 free( ptr ); // free old storage 97 return temp; 98 } else { 99 return (T *)ptr; 100 } // if 101 } // alloc 102 103 T * alloc( T ptr[], size_t dim, bool copy = true ) { 104 if ( copy ) { // realloc 105 return (T *)(void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc 106 } else { 107 struct __Unknown {}; 108 return alloc( (__Unknown *)ptr, dim ); // reuse, cheat making T/S different types 109 } // if 82 110 } // alloc 83 111 … … 107 135 forall( dtype T | sized(T) ) { 108 136 T * alloc_set( T ptr[], size_t dim, char fill ); // realloc array with fill 137 T * alloc_set( T ptr[], size_t dim, T fill ); // realloc array with fill 109 138 } // distribution 110 139 … … 116 145 T * alloc_align( size_t align, size_t dim ) { 117 146 return (T *)memalign( align, dim * sizeof(T) ); 147 } // alloc_align 148 149 T * alloc_align( T ptr[], size_t align ) { // aligned realloc array 150 return (T *)(void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc 151 } // alloc_align 152 153 forall( dtype S | sized(S) ) 154 T * alloc_align( S ptr[], size_t align ) { // aligned reuse array 155 return (T *)(void *)resize( (void *)ptr, align, sizeof(T) ); // CFA realloc 156 } // alloc_align 157 158 T * alloc_align( T ptr[], size_t align, size_t dim ) { // aligned realloc array 159 return (T *)(void *)realloc( (void *)ptr, align, dim * sizeof(T) ); // CFA realloc 118 160 } // alloc_align 119 161 … … 142 184 143 185 forall( dtype T | sized(T) ) { 144 T * alloc_align ( T ptr[], size_t align ); // realign145 T * alloc_align ( T ptr[], size_t align, size_t dim ); // aligned realloc array186 T * alloc_align_set( T ptr[], size_t align, char fill ); // aligned realloc with fill 187 T * alloc_align_set( T ptr[], size_t align, T fill ); // aligned realloc with fill 146 188 T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); // aligned realloc array with fill 147 } // distribution 148 149 static inline forall( dtype T | sized(T) ) { 150 // data, non-array types 189 T * alloc_align_set( T ptr[], size_t align, size_t dim, T fill ); // aligned realloc array with fill 190 } // distribution 191 192 static inline forall( dtype T | sized(T) ) { 193 // Cforall safe initialization/copy, i.e., implicit size specification, non-array types 151 194 T * memset( T * dest, char fill ) { 152 195 return (T *)memset( dest, fill, sizeof(T) ); … … 159 202 160 203 static inline forall( dtype T | sized(T) ) { 161 // data, array types204 // Cforall safe initialization/copy, i.e., implicit size specification, array types 162 205 T * amemset( T dest[], char fill, size_t dim ) { 163 206 return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset … … 169 212 } // distribution 170 213 171 // allocation/deallocation and constructor/destructor, non-array types214 // Cforall allocation/deallocation and constructor/destructor, non-array types 172 215 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p ); 173 216 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr ); 174 217 forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest ); 175 218 176 // allocation/deallocation and constructor/destructor, array types219 // Cforall allocation/deallocation and constructor/destructor, array types 177 220 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p ); 178 221 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] ); … … 182 225 183 226 static inline { 184 int strto( const char * sptr, char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }185 unsigned int strto( const char * sptr, char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }186 long int strto( const char * sptr, char ** eptr, int base ) { return strtol( sptr, eptr, base ); }187 unsigned long int strto( const char * sptr, char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }188 long long int strto( const char * sptr, char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }189 unsigned long long int strto( const char * sptr, char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }190 191 float strto( const char * sptr, char ** eptr ) { return strtof( sptr, eptr ); }192 double strto( const char * sptr, char ** eptr ) { return strtod( sptr, eptr ); }193 long double strto( const char * sptr, char ** eptr ) { return strtold( sptr, eptr ); }194 } // distribution 195 196 float _Complex strto( const char * sptr, char ** eptr );197 double _Complex strto( const char * sptr, char ** eptr );198 long double _Complex strto( const char * sptr, char ** eptr );227 int strto( const char sptr[], char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); } 228 unsigned int strto( const char sptr[], char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); } 229 long int strto( const char sptr[], char ** eptr, int base ) { return strtol( sptr, eptr, base ); } 230 unsigned long int strto( const char sptr[], char ** eptr, int base ) { return strtoul( sptr, eptr, base ); } 231 long long int strto( const char sptr[], char ** eptr, int base ) { return strtoll( sptr, eptr, base ); } 232 unsigned long long int strto( const char sptr[], char ** eptr, int base ) { return strtoull( sptr, eptr, base ); } 233 234 float strto( const char sptr[], char ** eptr ) { return strtof( sptr, eptr ); } 235 double strto( const char sptr[], char ** eptr ) { return strtod( sptr, eptr ); } 236 long double strto( const char sptr[], char ** eptr ) { return strtold( sptr, eptr ); } 237 } // distribution 238 239 float _Complex strto( const char sptr[], char ** eptr ); 240 double _Complex strto( const char sptr[], char ** eptr ); 241 long double _Complex strto( const char sptr[], char ** eptr ); 199 242 200 243 static inline { 201 int ato( const char * sptr ) { return (int)strtol( sptr, 0, 10 ); }202 unsigned int ato( const char * sptr ) { return (unsigned int)strtoul( sptr, 0, 10 ); }203 long int ato( const char * sptr ) { return strtol( sptr, 0, 10 ); }204 unsigned long int ato( const char * sptr ) { return strtoul( sptr, 0, 10 ); }205 long long int ato( const char * sptr ) { return strtoll( sptr, 0, 10 ); }206 unsigned long long int ato( const char * sptr ) { return strtoull( sptr, 0, 10 ); }207 208 float ato( const char * sptr ) { return strtof( sptr, 0); }209 double ato( const char * sptr ) { return strtod( sptr, 0); }210 long double ato( const char * sptr ) { return strtold( sptr, 0); }211 212 float _Complex ato( const char * sptr ) { return strto( sptr, NULL); }213 double _Complex ato( const char * sptr ) { return strto( sptr, NULL); }214 long double _Complex ato( const char * sptr ) { return strto( sptr, NULL); }244 int ato( const char sptr[] ) { return (int)strtol( sptr, 0p, 10 ); } 245 unsigned int ato( const char sptr[] ) { return (unsigned int)strtoul( sptr, 0p, 10 ); } 246 long int ato( const char sptr[] ) { return strtol( sptr, 0p, 10 ); } 247 unsigned long int ato( const char sptr[] ) { return strtoul( sptr, 0p, 10 ); } 248 long long int ato( const char sptr[] ) { return strtoll( sptr, 0p, 10 ); } 249 unsigned long long int ato( const char sptr[] ) { return strtoull( sptr, 0p, 10 ); } 250 251 float ato( const char sptr[] ) { return strtof( sptr, 0p ); } 252 double ato( const char sptr[] ) { return strtod( sptr, 0p ); } 253 long double ato( const char sptr[] ) { return strtold( sptr, 0p ); } 254 255 float _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); } 256 double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); } 257 long double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); } 215 258 } // distribution 216 259
Note:
See TracChangeset
for help on using the changeset viewer.