Changeset 6a490b2 for libcfa/src/stdlib.hfa
- Timestamp:
- May 11, 2020, 1:53:29 PM (5 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 504a7dc
- Parents:
- b7d6a36 (diff), a7b486b (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.hfa
rb7d6a36 r6a490b2 10 10 // Created On : Thu Jan 28 17:12:35 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : T ue Feb 4 08:27:01202013 // Update Count : 4 0112 // Last Modified On : Thu Apr 16 22:44:05 2020 13 // Update Count : 432 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 size_t malloc_size( void * addr ); // CFA heap 28 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); // CFA heap 25 29 void * memset( void * dest, int fill, size_t size ); // string.h 26 30 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 heap31 void * resize( void * oaddr, size_t size ); // CFA heap 28 32 } // extern "C" 29 33 34 void * resize( void * oaddr, size_t nalign, size_t size ); // CFA heap 30 35 void * realloc( void * oaddr, size_t nalign, size_t size ); // CFA heap 31 36 … … 40 45 41 46 static inline forall( dtype T | sized(T) ) { 42 // C dynamic allocation47 // Cforall safe equivalents, i.e., implicit size specification 43 48 44 49 T * malloc( void ) { … … 71 76 return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign 72 77 } // posix_memalign 73 74 // Cforall dynamic allocation 78 } // distribution 79 80 static inline forall( dtype T | sized(T) ) { 81 // Cforall safe general allocation, fill, resize, array 75 82 76 83 T * alloc( void ) { … … 83 90 } // alloc 84 91 85 T * alloc( T ptr[], size_t dim ) { // realloc 86 return (T *)(void *)realloc( (void *)ptr, dim * sizeof(T) ); // C realloc 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 87 111 } // alloc 88 112 … … 112 136 forall( dtype T | sized(T) ) { 113 137 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 fill 114 139 } // distribution 115 140 … … 125 150 T * alloc_align( T ptr[], size_t align ) { // aligned realloc array 126 151 return (T *)(void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA realloc 152 } // alloc_align 153 154 forall( dtype S | sized(S) ) 155 T * alloc_align( S ptr[], size_t align ) { // aligned reuse array 156 return (T *)(void *)resize( (void *)ptr, align, sizeof(T) ); // CFA realloc 127 157 } // alloc_align 128 158 … … 155 185 156 186 forall( dtype T | sized(T) ) { 187 T * alloc_align_set( T ptr[], size_t align, char fill ); // aligned realloc with fill 188 T * alloc_align_set( T ptr[], size_t align, T fill ); // aligned realloc with fill 157 189 T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); // aligned realloc array with fill 158 } // distribution 159 160 static inline forall( dtype T | sized(T) ) { 161 // data, non-array types 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 162 195 T * memset( T * dest, char fill ) { 163 196 return (T *)memset( dest, fill, sizeof(T) ); … … 170 203 171 204 static inline forall( dtype T | sized(T) ) { 172 // data, array types205 // Cforall safe initialization/copy, i.e., implicit size specification, array types 173 206 T * amemset( T dest[], char fill, size_t dim ) { 174 207 return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset … … 180 213 } // distribution 181 214 182 // allocation/deallocation and constructor/destructor, non-array types215 // Cforall allocation/deallocation and constructor/destructor, non-array types 183 216 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p ); 184 217 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr ); 185 218 forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest ); 186 219 187 // allocation/deallocation and constructor/destructor, array types220 // Cforall allocation/deallocation and constructor/destructor, array types 188 221 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p ); 189 222 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
Note:
See TracChangeset
for help on using the changeset viewer.