Changes in libcfa/src/stdlib.hfa [76e2113:e3fea42]
- File:
-
- 1 edited
-
libcfa/src/stdlib.hfa (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/stdlib.hfa
r76e2113 re3fea42 10 10 // Created On : Thu Jan 28 17:12:35 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : T hu Apr 16 22:44:05202013 // Update Count : 4 3212 // Last Modified On : Tue Feb 4 08:27:01 2020 13 // Update Count : 401 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 size_t malloc_size( void * addr ); // CFA heap28 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap29 25 void * memset( void * dest, int fill, size_t size ); // string.h 30 26 void * memcpy( void * dest, const void * src, size_t size ); // string.h 31 void * resize( void * oaddr, size_t size );// CFA heap27 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap 32 28 } // extern "C" 33 29 34 void * resize( void * oaddr, size_t nalign, size_t size ); // CFA heap35 30 void * realloc( void * oaddr, size_t nalign, size_t size ); // CFA heap 36 31 … … 45 40 46 41 static inline forall( dtype T | sized(T) ) { 47 // C forall safe equivalents, i.e., implicit size specification42 // C dynamic allocation 48 43 49 44 T * malloc( void ) { … … 76 71 return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign 77 72 } // posix_memalign 78 } // distribution 79 80 static inline forall( dtype T | sized(T) ) { 81 // Cforall safe general allocation, fill, resize, array 73 74 // Cforall dynamic allocation 82 75 83 76 T * alloc( void ) { … … 90 83 } // alloc 91 84 92 forall( dtype S | sized(S) ) 93 T * alloc( S ptr[], size_t dim = 1 ) { // singleton/array resize 94 size_t len = malloc_usable_size( ptr ); // current bucket size 95 if ( sizeof(T) * dim > len ) { // not enough space ? 96 T * temp = alloc( dim ); // new storage 97 free( ptr ); // free old storage 98 return temp; 99 } else { 100 return (T *)ptr; 101 } // if 102 } // alloc 103 104 T * alloc( T ptr[], size_t dim, bool copy = true ) { 105 if ( copy ) { // realloc 106 return (T *)(void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc 107 } else { 108 struct __Unknown {}; 109 return alloc( (__Unknown *)ptr, dim ); // reuse, cheat making T/S different types 110 } // if 85 T * alloc( T ptr[], size_t dim ) { // realloc 86 return (T *)(void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc 111 87 } // alloc 112 88 … … 136 112 forall( dtype T | sized(T) ) { 137 113 T * alloc_set( T ptr[], size_t dim, char fill ); // realloc array with fill 138 T * alloc_set( T ptr[], size_t dim, T fill ); // realloc array with fill139 114 } // distribution 140 115 … … 150 125 T * alloc_align( T ptr[], size_t align ) { // aligned realloc array 151 126 return (T *)(void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc 152 } // alloc_align153 154 forall( dtype S | sized(S) )155 T * alloc_align( S ptr[], size_t align ) { // aligned reuse array156 return (T *)(void *)resize( (void *)ptr, align, sizeof(T) ); // CFA realloc157 127 } // alloc_align 158 128 … … 185 155 186 156 forall( dtype T | sized(T) ) { 187 T * alloc_align_set( T ptr[], size_t align, char fill ); // aligned realloc with fill188 T * alloc_align_set( T ptr[], size_t align, T fill ); // aligned realloc with fill189 157 T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); // aligned realloc array with fill 190 T * alloc_align_set( T ptr[], size_t align, size_t dim, T fill ); // aligned realloc array with fill 191 } // distribution 192 193 static inline forall( dtype T | sized(T) ) { 194 // Cforall safe initialization/copy, i.e., implicit size specification, non-array types 158 } // distribution 159 160 static inline forall( dtype T | sized(T) ) { 161 // data, non-array types 195 162 T * memset( T * dest, char fill ) { 196 163 return (T *)memset( dest, fill, sizeof(T) ); … … 203 170 204 171 static inline forall( dtype T | sized(T) ) { 205 // Cforall safe initialization/copy, i.e., implicit size specification, array types172 // data, array types 206 173 T * amemset( T dest[], char fill, size_t dim ) { 207 174 return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset … … 213 180 } // distribution 214 181 215 // Cforallallocation/deallocation and constructor/destructor, non-array types182 // allocation/deallocation and constructor/destructor, non-array types 216 183 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p ); 217 184 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr ); 218 185 forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest ); 219 186 220 // Cforallallocation/deallocation and constructor/destructor, array types187 // allocation/deallocation and constructor/destructor, array types 221 188 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p ); 222 189 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
Note:
See TracChangeset
for help on using the changeset viewer.