Changeset ceb7db8 for libcfa/src/stdlib.hfa
- Timestamp:
- Aug 25, 2020, 6:08:45 PM (4 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- f7fac4b
- Parents:
- d119d613
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/stdlib.hfa
rd119d613 rceb7db8 114 114 } // distribution 115 115 116 /* 117 FIX ME : fix alloc interface after Ticker Number 214 is resolved, define and add union to S_fill. Then, modify postfix-fill functions to support T * with nmemb, char, and T object of any size. Finally, change alloc_internal. 118 Or, just follow the instructions below for that. 119 120 1. Replace the current forall-block that contains defintions of S_fill and S_realloc with following: 121 forall( dtype T | sized(T) ) { 122 union U_fill { char c; T * a; T t; }; 123 struct S_fill { char tag; char c; size_t size; T * at; char t[50]; }; 124 struct S_realloc { inline T *; }; 125 } 126 127 2. Replace all current postfix-fill functions with following for updated S_fill: 128 S_fill(T) ?`fill( char a ) { S_fill(T) ret = {'c'}; ret.fill.c = a; return ret; } 129 S_fill(T) ?`fill( T a ) { S_fill(T) ret = {'t'}; memcpy(&ret.fill.t, &a, sizeof(T)); return ret; } 130 S_fill(T) ?`fill( T a[], size_t nmemb ) { S_fill(T) ret = {'a', nmemb}; ret.fill.a = a; return ret; } 131 132 3. Replace the $alloc_internal function which is outside ttype forall-block with following function: 133 T * $alloc_internal( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill) { 134 T * ptr = NULL; 135 size_t size = sizeof(T); 136 size_t copy_end = 0; 137 138 if(Resize) { 139 ptr = (T*) (void *) resize( (int *)Resize, Align, Dim * size ); 140 } else if (Realloc) { 141 if (Fill.tag != '0') copy_end = min(malloc_size( Realloc ), Dim * size); 142 ptr = (T*) (void *) realloc( (int *)Realloc, Align, Dim * size ); 143 } else { 144 ptr = (T*) (void *) memalign( Align, Dim * size ); 145 } 146 147 if(Fill.tag == 'c') { 148 memset( (char *)ptr + copy_end, (int)Fill.fill.c, Dim * size - copy_end ); 149 } else if(Fill.tag == 't') { 150 for ( int i = copy_end; i <= Dim * size - size ; i += size ) { 151 memcpy( (char *)ptr + i, &Fill.fill.t, size ); 152 } 153 } else if(Fill.tag == 'a') { 154 memcpy( (char *)ptr + copy_end, Fill.fill.a, min(Dim * size - copy_end, size * Fill.nmemb) ); 155 } 156 157 return ptr; 158 } // $alloc_internal 159 */ 160 161 typedef struct S_align { inline size_t; } T_align; 162 typedef struct S_resize { inline void *; } T_resize; 163 164 forall( dtype T ) { 165 struct S_fill { char tag; char c; size_t size; T * at; char t[50]; }; 166 struct S_realloc { inline T *; }; 167 } 168 169 static inline T_align ?`align ( size_t a ) { return (T_align){a}; } 170 static inline T_resize ?`resize ( void * a ) { return (T_resize){a}; } 116 171 static inline forall( dtype T | sized(T) ) { 117 // Cforall safe general allocation, fill, resize, array 118 119 T * alloc( void ) { 120 return malloc(); 121 } // alloc 122 123 T * alloc( size_t dim ) { 124 return aalloc( dim ); 125 } // alloc 126 127 forall( dtype S | sized(S) ) 128 T * alloc( S ptr[], size_t dim = 1 ) { // singleton/array resize 129 return resize( (T *)ptr, dim * sizeof(T) ); // CFA resize 130 } // alloc 131 132 T * alloc( T ptr[], size_t dim = 1, bool copy = true ) { 133 if ( copy ) { 134 return realloc( ptr, dim * sizeof(T) ); // CFA realloc 172 173 S_fill(T) ?`fill ( T t ) { 174 S_fill(T) ret = { 't' }; 175 size_t size = sizeof(T); 176 if(size > sizeof(ret.t)) { printf("ERROR: const object of size greater than 50 bytes given for dynamic memory fill\n"); exit(1); } 177 memcpy( &ret.t, &t, size ); 178 return ret; 179 } 180 S_fill(T) ?`fill ( char c ) { return (S_fill(T)){ 'c', c }; } 181 S_fill(T) ?`fill ( T * a ) { return (S_fill(T)){ 'T', '0', 0, a }; } 182 S_fill(T) ?`fill ( T a[], size_t nmemb ) { return (S_fill(T)){ 'a', '0', nmemb * sizeof(T), a }; } 183 184 S_realloc(T) ?`realloc ( T * a ) { return (S_realloc(T)){a}; } 185 186 T * $alloc_internal( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill) { 187 T * ptr = NULL; 188 size_t size = sizeof(T); 189 size_t copy_end = 0; 190 191 if(Resize) { 192 ptr = (T*) (void *) resize( (int *)Resize, Align, Dim * size ); 193 } else if (Realloc) { 194 if (Fill.tag != '0') copy_end = min(malloc_size( Realloc ), Dim * size); 195 ptr = (T*) (void *) realloc( (int *)Realloc, Align, Dim * size ); 135 196 } else { 136 return resize( ptr, dim * sizeof(T) ); // CFA resize 137 } // if 138 } // alloc 139 140 T * alloc_set( char fill ) { 141 return (T *)memset( (T *)alloc(), (int)fill, sizeof(T) ); // initialize with fill value 142 } // alloc_set 143 144 T * alloc_set( const T & fill ) { 145 return (T *)memcpy( (T *)alloc(), &fill, sizeof(T) ); // initialize with fill value 146 } // alloc_set 147 148 T * alloc_set( size_t dim, char fill ) { 149 return (T *)memset( (T *)alloc( dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value 150 } // alloc_set 151 152 T * alloc_set( size_t dim, const T & fill ) { 153 T * r = (T *)alloc( dim ); 154 for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value 155 return r; 156 } // alloc_set 157 158 T * alloc_set( size_t dimNew, const T fill[], size_t dimOld ) { 159 return (T *)memcpy( (T *)alloc( dimNew ), fill, min( dimNew, dimOld ) * sizeof(T) ); // initialize with fill value 160 } // alloc_set 161 162 T * alloc_set( T ptr[], size_t dim, char fill ) { // realloc array with fill 163 size_t osize = malloc_size( ptr ); // current allocation 164 size_t nsize = dim * sizeof(T); // new allocation 165 T * nptr = realloc( ptr, nsize ); // CFA realloc 166 if ( nsize > osize ) { // larger ? 167 memset( (char *)nptr + osize, (int)fill, nsize - osize ); // initialize added storage 168 } // if 169 return nptr; 170 } // alloc_set 171 172 T * alloc_set( T ptr[], size_t dim, const T & fill ) { // realloc array with fill 173 size_t odim = malloc_size( ptr ) / sizeof(T); // current dimension 174 size_t nsize = dim * sizeof(T); // new allocation 175 size_t ndim = nsize / sizeof(T); // new dimension 176 T * nptr = realloc( ptr, nsize ); // CFA realloc 177 if ( ndim > odim ) { // larger ? 178 for ( i; odim ~ ndim ) { 179 memcpy( &nptr[i], &fill, sizeof(T) ); // initialize with fill value 180 } // for 181 } // if 182 return nptr; 183 } // alloc_set 184 } // distribution 185 186 static inline forall( dtype T | sized(T) ) { 187 T * alloc_align( size_t align ) { 188 return (T *)memalign( align, sizeof(T) ); 189 } // alloc_align 190 191 T * alloc_align( size_t align, size_t dim ) { 192 return (T *)memalign( align, dim * sizeof(T) ); 193 } // alloc_align 194 195 T * alloc_align( T * ptr, size_t align ) { // aligned realloc array 196 return (T *)(void *)realloc( (void *)ptr, align, sizeof(T) ); // CFA C realloc 197 } // alloc_align 198 199 forall( dtype S | sized(S) ) 200 T * alloc_align( S ptr[], size_t align ) { // aligned reuse array 201 return (T *)(void *)resize( (void *)ptr, align, sizeof(T) ); // CFA realloc 202 } // alloc_align 203 204 T * alloc_align( T ptr[], size_t align, size_t dim ) { // aligned realloc array 205 return (T *)(void *)realloc( (void *)ptr, align, dim * sizeof(T) ); // CFA realloc 206 } // alloc_align 207 208 T * alloc_align_set( size_t align, char fill ) { 209 return (T *)memset( (T *)alloc_align( align ), (int)fill, sizeof(T) ); // initialize with fill value 210 } // alloc_align_set 211 212 T * alloc_align_set( size_t align, const T & fill ) { 213 return (T *)memcpy( (T *)alloc_align( align ), &fill, sizeof(T) ); // initialize with fill value 214 } // alloc_align_set 215 216 T * alloc_align_set( size_t align, size_t dim, char fill ) { 217 return (T *)memset( (T *)alloc_align( align, dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value 218 } // alloc_align_set 219 220 T * alloc_align_set( size_t align, size_t dim, const T & fill ) { 221 T * r = (T *)alloc_align( align, dim ); 222 for ( i; dim ) { memcpy( &r[i], &fill, sizeof(T) ); } // initialize with fill value 223 return r; 224 } // alloc_align_set 225 226 T * alloc_align_set( size_t align, size_t dimNew, const T fill[], size_t dimOld ) { 227 return (T *)memcpy( (T *)alloc_align( align, dimNew ), fill, min( dimNew, dimOld ) * sizeof(T) ); 228 } // alloc_align_set 229 230 T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ) { 231 size_t osize = malloc_size( ptr ); // current allocation 232 size_t nsize = dim * sizeof(T); // new allocation 233 T * nptr = alloc_align( ptr, align, nsize ); 234 if ( nsize > osize ) { // larger ? 235 memset( (char *)nptr + osize, (int)fill, nsize - osize ); // initialize added storage 236 } // if 237 return nptr; 238 } // alloc_align_set 239 240 T * alloc_align_set( T ptr[], size_t align, size_t dim, const T & fill ) { 241 size_t odim = malloc_size( ptr ) / sizeof(T); // current dimension 242 size_t nsize = dim * sizeof(T); // new allocation 243 size_t ndim = nsize / sizeof(T); // new dimension 244 T * nptr = alloc_align( ptr, align, nsize ); 245 if ( ndim > odim ) { // larger ? 246 for ( i; odim ~ ndim ) { 247 memcpy( &nptr[i], &fill, sizeof(T) ); // initialize with fill value 248 } // for 249 } // if 250 return nptr; 251 } // alloc_align_set 252 } // distribution 197 ptr = (T*) (void *) memalign( Align, Dim * size ); 198 } 199 200 if(Fill.tag == 'c') { 201 memset( (char *)ptr + copy_end, (int)Fill.c, Dim * size - copy_end ); 202 } else if(Fill.tag == 't') { 203 for ( int i = copy_end; i <= Dim * size - size ; i += size ) { 204 memcpy( (char *)ptr + i, &Fill.t, size ); 205 } 206 } else if(Fill.tag == 'a') { 207 memcpy( (char *)ptr + copy_end, Fill.at, min(Dim * size - copy_end, Fill.size) ); 208 } else if(Fill.tag == 'T') { 209 for ( int i = copy_end; i <= Dim * size - size ; i += size ) { 210 memcpy( (char *)ptr + i, Fill.at, size ); 211 } 212 } 213 214 return ptr; 215 } // $alloc_internal 216 217 forall( ttype TT | { T * $alloc_internal( void *, T *, size_t, size_t, S_fill(T), TT ); } ) { 218 219 T * $alloc_internal( void * , T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill, T_resize Resize, TT rest) { 220 return $alloc_internal( Resize, (T*)0p, Align, Dim, Fill, rest); 221 } 222 223 T * $alloc_internal( void * Resize, T * , size_t Align, size_t Dim, S_fill(T) Fill, S_realloc(T) Realloc, TT rest) { 224 return $alloc_internal( (void*)0p, Realloc, Align, Dim, Fill, rest); 225 } 226 227 T * $alloc_internal( void * Resize, T * Realloc, size_t , size_t Dim, S_fill(T) Fill, T_align Align, TT rest) { 228 return $alloc_internal( Resize, Realloc, Align, Dim, Fill, rest); 229 } 230 231 T * $alloc_internal( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T) , S_fill(T) Fill, TT rest) { 232 return $alloc_internal( Resize, Realloc, Align, Dim, Fill, rest); 233 } 234 235 T * alloc( TT all ) { 236 return $alloc_internal( (void*)0p, (T*)0p, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), (size_t)1, (S_fill(T)){'0'}, all); 237 } 238 239 T * alloc( size_t dim, TT all ) { 240 return $alloc_internal( (void*)0p, (T*)0p, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), dim, (S_fill(T)){'0'}, all); 241 } 242 243 } // distribution TT 244 245 } // distribution T 253 246 254 247 static inline forall( dtype T | sized(T) ) {
Note: See TracChangeset
for help on using the changeset viewer.