Changes in libcfa/src/stdlib.hfa [cfbc703d:cafb687]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/stdlib.hfa
rcfbc703d rcafb687 10 10 // Created On : Thu Jan 28 17:12:35 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Apr 1 18:38:41 202013 // Update Count : 42912 // Last Modified On : Sun Oct 20 22:57:33 2019 13 // Update Count : 390 14 14 // 15 15 … … 21 21 #include <stdlib.h> // *alloc, strto*, ato* 22 22 23 // Reduce includes by explicitly defining these routines.24 23 extern "C" { 25 24 void * memalign( size_t align, size_t size ); // malloc.h 26 size_t malloc_usable_size( void * ptr ); // malloc.h27 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap28 25 void * memset( void * dest, int fill, size_t size ); // string.h 29 26 void * memcpy( void * dest, const void * src, size_t size ); // string.h 30 void * resize( void * oaddr, size_t size );// CFA heap27 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap 31 28 } // extern "C" 32 33 void * resize( void * oaddr, size_t nalign, size_t size ); // CFA heap34 void * realloc( void * oaddr, size_t nalign, size_t size ); // CFA heap35 29 36 30 //--------------------------------------- … … 44 38 45 39 static inline forall( dtype T | sized(T) ) { 46 // C forall safe equivalents, i.e., implicit size specification40 // C dynamic allocation 47 41 48 42 T * malloc( void ) { … … 56 50 } // calloc 57 51 58 T * realloc( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast 52 T * realloc( T * ptr, size_t size ) { 53 if ( unlikely( ptr == 0 ) ) return malloc(); 59 54 return (T *)(void *)realloc( (void *)ptr, size ); // C realloc 60 55 } // realloc … … 64 59 } // memalign 65 60 66 T * cmemalign( size_t align, size_t dim ) {67 return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign68 } // cmemalign69 70 61 T * aligned_alloc( size_t align ) { 71 62 return (T *)aligned_alloc( align, sizeof(T) ); // C aligned_alloc … … 75 66 return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign 76 67 } // posix_memalign 77 } // distribution 78 79 static inline forall( dtype T | sized(T) ) { 80 // Cforall safe general allocation, fill, resize, array 68 69 // Cforall dynamic allocation 81 70 82 71 T * alloc( void ) { … … 89 78 } // alloc 90 79 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 80 T * alloc( T ptr[], size_t dim ) { // realloc 81 return realloc( ptr, dim * sizeof(T) ); 110 82 } // alloc 111 83 … … 135 107 forall( dtype T | sized(T) ) { 136 108 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 fill138 109 } // distribution 139 110 … … 145 116 T * alloc_align( size_t align, size_t dim ) { 146 117 return (T *)memalign( align, dim * sizeof(T) ); 147 } // alloc_align148 149 T * alloc_align( T ptr[], size_t align ) { // aligned realloc array150 return (T *)(void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc151 } // alloc_align152 153 forall( dtype S | sized(S) )154 T * alloc_align( S ptr[], size_t align ) { // aligned reuse array155 return (T *)(void *)resize( (void *)ptr, align, sizeof(T) ); // CFA realloc156 } // alloc_align157 158 T * alloc_align( T ptr[], size_t align, size_t dim ) { // aligned realloc array159 return (T *)(void *)realloc( (void *)ptr, align, dim * sizeof(T) ); // CFA realloc160 118 } // alloc_align 161 119 … … 184 142 185 143 forall( dtype T | sized(T) ) { 186 T * alloc_align _set( T ptr[], size_t align, char fill ); // aligned realloc with fill187 T * alloc_align _set( T ptr[], size_t align, T fill ); // aligned realloc with fill144 T * alloc_align( T ptr[], size_t align ); // realign 145 T * alloc_align( T ptr[], size_t align, size_t dim ); // aligned realloc array 188 146 T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); // aligned realloc array with fill 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 147 } // distribution 148 149 static inline forall( dtype T | sized(T) ) { 150 // data, non-array types 194 151 T * memset( T * dest, char fill ) { 195 152 return (T *)memset( dest, fill, sizeof(T) ); … … 202 159 203 160 static inline forall( dtype T | sized(T) ) { 204 // Cforall safe initialization/copy, i.e., implicit size specification, array types161 // data, array types 205 162 T * amemset( T dest[], char fill, size_t dim ) { 206 163 return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset … … 212 169 } // distribution 213 170 214 // Cforallallocation/deallocation and constructor/destructor, non-array types171 // allocation/deallocation and constructor/destructor, non-array types 215 172 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p ); 216 173 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr ); 217 174 forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest ); 218 175 219 // Cforallallocation/deallocation and constructor/destructor, array types176 // allocation/deallocation and constructor/destructor, array types 220 177 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p ); 221 178 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] ); … … 225 182 226 183 static inline { 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 );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 ); 242 199 243 200 static inline { 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); }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 ); } 258 215 } // distribution 259 216
Note:
See TracChangeset
for help on using the changeset viewer.