- Timestamp:
- Apr 22, 2024, 11:37:28 PM (8 months ago)
- Branches:
- master
- Children:
- 15215f02
- Parents:
- c0363be
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/stdlib.hfa
rc0363be r0fe07be 10 10 // Created On : Thu Jan 28 17:12:35 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Apr 19 09:47:55202413 // Update Count : 82612 // Last Modified On : Mon Apr 22 23:23:58 2024 13 // Update Count : 958 14 14 // 15 15 … … 121 121 122 122 /* 123 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. 123 FIX ME : fix alloc interface after Ticker Number 214 is resolved, define and add union to S_fill. Then, modify 124 postfix-fill functions to support T * with nmemb, char, and T object of any size. Finally, change alloc_internal. 124 125 Or, just follow the instructions below for that. 125 126 … … 165 166 */ 166 167 167 typedef struct S_align { inline size_t; } T_align; 168 typedef struct S_resize { inline void *; } T_resize; 169 170 forall( T & ) { 171 struct S_fill { char tag; char c; size_t size; T * at; char t[50]; }; 172 struct S_realloc { inline T *; }; 168 #pragma GCC diagnostic push 169 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" 170 #pragma GCC diagnostic ignored "-Wuninitialized" 171 172 struct T_align { size_t align; }; 173 struct T_resize { void * addr; }; 174 struct T_realloc { void * addr; }; 175 forall( T & ) struct T_fill { 176 // 'N' => no fill, 'c' => fill with character c, 'a' => fill first N array elements from another array, 177 // 'A' => fill all array elements from another array, 'T' => fill using a T value. 178 char tag; 179 size_t nelem; // number of elements copied from "at" (used with tag 'a') 180 // union { 181 char c; 182 T * at; 183 char t[64]; // T t; 184 // }; 185 }; 186 187 #pragma GCC diagnostic pop 188 189 static inline { 190 T_align ?`align( size_t a ) { return (T_align){ a }; } 191 T_resize ?`resize( void * a ) { return (T_resize){ a }; } 192 T_realloc ?`realloc( void * a ) { return (T_realloc){ a }; } 173 193 } 174 194 175 static inline T_align ?`align( size_t a ) { return (T_align){a}; }176 static inline T_resize ?`resize( void * a ) { return (T_resize){a}; }177 178 195 static inline forall( T & | sized(T) ) { 179 S_fill(T) ?`fill( T t ) { 180 S_fill(T) ret = { 't' }; 196 T_fill(T) ?`fill( char c ) { return (T_fill(T)){ 'c', 0, c }; } 197 T_fill(T) ?`fill( T t ) { 198 T_fill(T) ret = { 'T' }; 181 199 size_t size = sizeof(T); 182 200 if ( size > sizeof(ret.t) ) { … … 186 204 return ret; 187 205 } 188 S_fill(T) ?`fill( zero_t ) = void; // FIX ME: remove this once ticket 214 is resolved 189 S_fill(T) ?`fill( T * a ) { return (S_fill(T)){ 'T', '0', 0, a }; } // FIX ME: remove this once ticket 214 is resolved 190 S_fill(T) ?`fill( char c ) { return (S_fill(T)){ 'c', c }; } 191 S_fill(T) ?`fill( T a[], size_t nmemb ) { return (S_fill(T)){ 'a', '0', nmemb * sizeof(T), a }; } 192 193 S_realloc(T) ?`realloc ( T * a ) { return (S_realloc(T)){a}; } 194 195 T * alloc_internal$( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill ) { 196 T * ptr = NULL; 197 size_t size = sizeof(T); 206 T_fill(T) ?`fill( T a[] ) { return (T_fill(T)){ 'A', 0, '\0', a }; } // FIX ME: remove this once ticket 214 is resolved 207 T_fill(T) ?`fill( T a[], size_t nelem ) { return (T_fill(T)){ 'a', nelem * sizeof(T), '\0', a }; } 208 209 // private interface 210 T * alloc_internal$( size_t Dim, T_resize Resize, T_realloc Realloc, size_t Align, T_fill(T) Fill ) { 211 T * ptr; 212 size_t tsize = sizeof(T); 198 213 size_t copy_end = 0; 199 214 200 if ( Resize ) {201 ptr = (T *)(void *)resize( (void *)Resize, Align, Dim *size );202 } else if ( Realloc ) {203 if ( Fill.tag != ' 0' ) copy_end = min(malloc_size( Realloc ), Dim *size );204 ptr = (T *)(void *)realloc( (void *)Realloc, Align, Dim *size );215 if ( Resize.addr ) { 216 ptr = (T *)(void *)resize( Resize.addr, Align, Dim * tsize ); 217 } else if ( Realloc.addr ) { 218 if ( Fill.tag != 'N' ) copy_end = min(malloc_size( Realloc.addr ), Dim * tsize ); 219 ptr = (T *)(void *)realloc( Realloc.addr, Align, Dim * tsize ); 205 220 } else { 206 ptr = (T *)(void *) memalign( Align, Dim *size );207 } 221 ptr = (T *)(void *)memalign( Align, Dim * tsize ); 222 } // if 208 223 209 224 if ( Fill.tag == 'c' ) { 210 memset( (char *)ptr + copy_end, (int)Fill.c, Dim * size - copy_end ); 211 } else if ( Fill.tag == 't' ) { 212 for ( i; copy_end ~ Dim * size ~ size ) { 213 #pragma GCC diagnostic push 214 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" 215 assert( size <= sizeof(Fill.t) ); 216 memcpy( (char *)ptr + i, &Fill.t, size ); 217 #pragma GCC diagnostic pop 218 } 225 memset( (char *)ptr + copy_end, (int)Fill.c, Dim * tsize - copy_end ); 226 } else if ( Fill.tag == 'T' ) { 227 for ( i; copy_end ~ Dim * tsize ~ tsize ) { 228 assert( tsize <= sizeof(Fill.t) ); 229 memcpy( (char *)ptr + i, &Fill.t, tsize ); 230 } // for 219 231 } else if ( Fill.tag == 'a' ) { 220 memcpy( (char *)ptr + copy_end, Fill.at, min(Dim * size - copy_end, Fill.size) ); 221 } else if ( Fill.tag == 'T' ) { 222 memcpy( (char *)ptr + copy_end, Fill.at, Dim * size ); 223 } 224 232 memcpy( (char *)ptr + copy_end, Fill.at, min( Dim * tsize - copy_end, Fill.nelem ) ); 233 } else if ( Fill.tag == 'A' ) { 234 memcpy( (char *)ptr + copy_end, Fill.at, Dim * tsize ); 235 } // if 225 236 return ptr; 226 237 } // alloc_internal$ 227 238 228 forall( List ... | { T * alloc_internal$( void *, T *, size_t, size_t, S_fill(T), List ); } ) { 229 T * alloc_internal$( void *, T *, size_t Align, size_t Dim, S_fill(T) Fill, T_resize Resize, List rest ) { 230 return alloc_internal$( Resize, (T*)0p, Align, Dim, Fill, rest); 239 // Dim is a fixed (optional first) parameter, and hence is not set using a postfix function. A dummy parameter is 240 // being overwritten by the postfix argument in the ttype. 241 forall( List ... | { T * alloc_internal$( size_t Dim, T_resize Resize, T_realloc Realloc, size_t Align, T_fill(T) Fill, List ); } ) { 242 // middle interface 243 T * alloc_internal$( size_t Dim, T_resize dummy, T_realloc Realloc, size_t Align, T_fill(T) Fill, T_resize Resize, List rest ) { 244 return alloc_internal$( Dim, Resize, (T_realloc){0p}, Align, Fill, rest ); 231 245 } 232 233 T * alloc_internal$( void *, T *, size_t Align, size_t Dim, S_fill(T) Fill, S_realloc(T) Realloc, List rest ) { 234 return alloc_internal$( (void*)0p, Realloc, Align, Dim, Fill, rest); 246 T * alloc_internal$( size_t Dim, T_resize Resize, T_realloc dummy, size_t Align, T_fill(T) Fill, T_realloc Realloc, List rest ) { 247 return alloc_internal$( Dim, (T_resize){0p}, Realloc, Align, Fill, rest ); 235 248 } 236 237 T * alloc_internal$( void * Resize, T * Realloc, size_t, size_t Dim, S_fill(T) Fill, T_align Align, List rest ) { 238 return alloc_internal$( Resize, Realloc, Align, Dim, Fill, rest); 249 T * alloc_internal$( size_t Dim, T_resize Resize, T_realloc Realloc, size_t dummy, T_fill(T) Fill, T_align Align, List rest ) { 250 return alloc_internal$( Dim, Resize, Realloc, Align.align, Fill, rest ); 239 251 } 240 241 T * alloc_internal$( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T), S_fill(T) Fill, List rest ) { 242 return alloc_internal$( Resize, Realloc, Align, Dim, Fill, rest ); 252 T * alloc_internal$( size_t Dim, T_resize Resize, T_realloc Realloc, size_t Align, T_fill(T) dummy, T_fill(T) Fill, List rest ) { 253 return alloc_internal$( Dim, Resize, Realloc, Align, Fill, rest ); 243 254 } 244 245 T * alloc( List all) {246 return alloc_internal$( ( void*)0p, (T*)0p, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), (size_t)1, (S_fill(T)){'0'}, all);255 // public interface 256 T * alloc( List rest ) { 257 return alloc_internal$( (size_t)1, (T_resize){0p}, (T_realloc){0p}, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), (T_fill(T)){'N'}, rest ); 247 258 } 248 249 T * alloc( size_t dim, List all ) { 250 return alloc_internal$( (void*)0p, (T*)0p, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), dim, (S_fill(T)){'0'}, all ); 259 T * alloc( size_t Dim, List rest ) { 260 return alloc_internal$( Dim, (T_resize){0p}, (T_realloc){0p}, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), (T_fill(T)){'N'}, rest ); 251 261 } 252 262 } // distribution List
Note: See TracChangeset
for help on using the changeset viewer.