Changeset b89c7c2
- Timestamp:
- Jul 19, 2020, 8:59:27 PM (4 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:
- eacf82c
- Parents:
- e310238
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/stdlib.hfa
re310238 rb89c7c2 9 9 // Author : Peter A. Buhr 10 10 // Created On : Thu Jan 28 17:12:35 2016 11 // Last Modified By : Andrew Beach12 // Last Modified On : Tue Jun 2 16:47:00202013 // Update Count : 4 5111 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Jul 19 18:29:34 2020 13 // Update Count : 463 14 14 // 15 15 … … 57 57 } // calloc 58 58 59 T * resize( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast 60 return (T *)(void *)resize( (void *)ptr, size ); // C realloc 59 T * resize( T * ptr, size_t size ) { // CFA resize, eliminate return-type cast 60 if ( unlikely( size == 0 ) || unlikely( ptr == 0p ) ) { // special cases 61 if ( unlikely( size == 0 ) ) free( ptr ); 62 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc 63 else return (T *)memalign( _Alignof(T), sizeof(T) ); 64 } // if 65 return (T *)(void *)resize( (void *)ptr, size ); // CFA resize 61 66 } // resize 62 67 63 68 T * realloc( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast 69 if ( unlikely( size == 0 ) || unlikely( ptr == 0p ) ) { // special cases 70 if ( unlikely( size == 0 ) ) free( ptr ); 71 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc 72 else return (T *)memalign( _Alignof(T), sizeof(T) ); 73 } // if 64 74 return (T *)(void *)realloc( (void *)ptr, size ); // C realloc 65 75 } // realloc … … 118 128 119 129 T * alloc( T ptr[], size_t dim, bool copy = true ) { 120 if ( copy ) { // realloc121 return (T *)(void *)realloc( (void *)ptr, dim * sizeof(T) ); // Crealloc130 if ( copy ) { 131 return realloc( ptr, dim * sizeof(T) ); // CFA realloc 122 132 } else { 123 return resize( ptr, dim * sizeof(T) ); // resize133 return resize( ptr, dim * sizeof(T) ); // CFA resize 124 134 } // if 125 135 } // alloc … … 146 156 return (T *)memcpy( (T *)alloc( dim ), fill, dim * sizeof(T) ); // initialize with fill value 147 157 } // alloc 148 } // distribution 149 150 forall( dtype T | sized(T) ) { 151 T * alloc_set( T ptr[], size_t dim, char fill ); // realloc array with fill 152 T * alloc_set( T ptr[], size_t dim, T fill ); // realloc array with fill 158 159 T * alloc_set( T ptr[], size_t dim, char fill ) { // realloc array with fill 160 size_t osize = malloc_size( ptr ); // current allocation 161 T * nptr = realloc( ptr, dim * sizeof(T) ); // CFA realloc 162 size_t nsize = malloc_size( nptr ); // new allocation 163 if ( nsize > osize ) { // larger ? 164 memset( (char *)nptr + osize, (int)fill, nsize - osize ); // initialize added storage 165 } // if 166 return (T *)nptr; 167 } // alloc_set 168 169 T * alloc_set( T ptr[], size_t dim, T & fill ) { // realloc array with fill 170 size_t odim = malloc_size( ptr ) / sizeof(T); // current allocation 171 T * nptr = realloc( ptr, dim * sizeof(T) ); // CFA realloc 172 size_t ndim = malloc_size( nptr ) / sizeof(T); // new allocation 173 if ( ndim > odim ) { // larger ? 174 for ( i; odim ~ ndim ) { 175 memcpy( &nptr[i], &fill, sizeof(T) ); // initialize with fill value 176 } // for 177 } // if 178 return (T *)nptr; 179 } // alloc_align_set 153 180 } // distribution 154 181 … … 196 223 return (T *)memcpy( (T *)alloc_align( align, dim ), fill, dim * sizeof(T) ); 197 224 } // alloc_align 198 } // distribution 199 200 forall( dtype T | sized(T) ) { 201 T * alloc_align_set( T ptr[], size_t align, char fill ); // aligned realloc with fill 202 T * alloc_align_set( T ptr[], size_t align, T fill ); // aligned realloc with fill 203 T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); // aligned realloc array with fill 204 T * alloc_align_set( T ptr[], size_t align, size_t dim, T fill ); // aligned realloc array with fill 225 226 T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ) { 227 size_t osize = malloc_size( ptr ); // current allocation 228 T * nptr = realloc( ptr, align, dim * sizeof(T) ); // CFA realloc 229 size_t nsize = malloc_size( nptr ); // new allocation 230 if ( nsize > osize ) { // larger ? 231 memset( (char *)nptr + osize, (int)fill, nsize - osize ); // initialize added storage 232 } // if 233 return (T *)nptr; 234 } // alloc_align_set 235 236 T * alloc_align_set( T ptr[], size_t align, size_t dim, T & fill ) { 237 size_t odim = malloc_size( ptr ) / sizeof(T); // current allocation 238 T * nptr = realloc( ptr, align, dim * sizeof(T) ); // CFA realloc 239 size_t ndim = malloc_size( nptr ); // new allocation 240 if ( ndim > odim ) { // larger ? 241 for ( i; odim ~ ndim ) { 242 memcpy( &nptr[i], &fill, sizeof(T) ); // initialize with fill value 243 } // for 244 } // if 245 return (T *)nptr; 246 } // alloc_align_set 205 247 } // distribution 206 248
Note: See TracChangeset
for help on using the changeset viewer.