Changeset 12c1eef for libcfa/src/stdlib.hfa
- Timestamp:
- Jan 19, 2022, 2:36:56 PM (4 years ago)
- Branches:
- ADT, ast-experimental, enum, master, pthread-emulation, qualifiedEnum
- Children:
- 97c215f
- Parents:
- 5235d49 (diff), 6a33e40 (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
-
libcfa/src/stdlib.hfa (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/stdlib.hfa
r5235d49 r12c1eef 10 10 // Created On : Thu Jan 28 17:12:35 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Dec 29 15:30:58 202113 // Update Count : 59112 // Last Modified On : Thu Jan 13 21:34:46 2022 13 // Update Count : 636 14 14 // 15 15 … … 21 21 #include <stdlib.h> // *alloc, strto*, ato* 22 22 #include <heap.hfa> 23 23 24 24 25 // Reduce includes by explicitly defining these routines. … … 43 44 //--------------------------------------- 44 45 45 // Macro because of returns46 #define ARRAY_ALLOC$( allocation, alignment, dim ) \47 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)allocation( dim, (size_t)sizeof(T) ); /* C allocation */ \48 else return (T *)alignment( _Alignof(T), dim, sizeof(T) )49 50 46 static inline forall( T & | sized(T) ) { 51 47 // CFA safe equivalents, i.e., implicit size specification 52 48 53 49 T * malloc( void ) { 54 if ( _Alignof(T) <= libAlign() ) return (T *) (void *)malloc( (size_t)sizeof(T) ); // C allocation50 if ( _Alignof(T) <= libAlign() ) return (T *)malloc( sizeof(T) ); // C allocation 55 51 else return (T *)memalign( _Alignof(T), sizeof(T) ); 56 52 } // malloc 57 53 58 54 T * aalloc( size_t dim ) { 59 ARRAY_ALLOC$( aalloc, amemalign, dim ); 55 if ( _Alignof(T) <= libAlign() ) return (T *)aalloc( dim, sizeof(T) ); // C allocation 56 else return (T *)amemalign( _Alignof(T), dim, sizeof(T) ); 60 57 } // aalloc 61 58 62 59 T * calloc( size_t dim ) { 63 ARRAY_ALLOC$( calloc, cmemalign, dim ); 60 if ( _Alignof(T) <= libAlign() ) return (T *)calloc( dim, sizeof(T) ); // C allocation 61 else return (T *)cmemalign( _Alignof(T), dim, sizeof(T) ); 64 62 } // calloc 65 63 66 64 T * resize( T * ptr, size_t size ) { // CFA resize, eliminate return-type cast 67 if ( _Alignof(T) <= libAlign() ) return (T *) (void *)resize( (void *)ptr, size ); // CFA resize68 else return (T *) (void *)resize( (void *)ptr, _Alignof(T), size ); // CFA resize65 if ( _Alignof(T) <= libAlign() ) return (T *)resize( (void *)ptr, size ); // CFA resize 66 else return (T *)resize( (void *)ptr, _Alignof(T), size ); // CFA resize 69 67 } // resize 70 68 71 69 T * realloc( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast 72 if ( _Alignof(T) <= libAlign() ) return (T *) (void *)realloc( (void *)ptr, size ); // C realloc73 else return (T *) (void *)realloc( (void *)ptr, _Alignof(T), size ); // CFA realloc70 if ( _Alignof(T) <= libAlign() ) return (T *)realloc( (void *)ptr, size ); // C realloc 71 else return (T *)realloc( (void *)ptr, _Alignof(T), size ); // CFA realloc 74 72 } // realloc 75 73 … … 210 208 211 209 forall( TT... | { T * alloc_internal$( void *, T *, size_t, size_t, S_fill(T), TT ); } ) { 212 213 210 T * alloc_internal$( void * , T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill, T_resize Resize, TT rest) { 214 211 return alloc_internal$( Resize, (T*)0p, Align, Dim, Fill, rest); … … 234 231 return alloc_internal$( (void*)0p, (T*)0p, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), dim, (S_fill(T)){'0'}, all); 235 232 } 236 237 233 } // distribution TT 238 234 } // distribution T … … 388 384 //--------------------------------------- 389 385 386 // Sequential Pseudo Random-Number Generator : generate repeatable sequence of values that appear random. 387 // 388 // Declaration : 389 // PRNG sprng = { 1009 } - set starting seed versus random seed 390 // 391 // Interface : 392 // set_seed( sprng, 1009 ) - set starting seed for ALL kernel threads versus random seed 393 // get_seed( sprng ) - read seed 394 // prng( sprng ) - generate random value in range [0,UINT_MAX] 395 // prng( sprng, u ) - generate random value in range [0,u) 396 // prng( sprng, l, u ) - generate random value in range [l,u] 397 // calls( sprng ) - number of generated random value so far 398 // 399 // Examples : generate random number between 5-21 400 // prng( sprng ) % 17 + 5; values 0-16 + 5 = 5-21 401 // prng( sprng, 16 + 1 ) + 5; 402 // prng( sprng, 5, 21 ); 403 // calls( sprng ); 404 390 405 struct PRNG { 391 406 uint32_t callcnt; // call count … … 394 409 }; // PRNG 395 410 396 extern uint32_t prng( PRNG & prng ) __attribute__(( warn_unused_result )); // [0,UINT_MAX] 411 void set_seed( PRNG & prng, uint32_t seed_ ); 412 uint32_t prng( PRNG & prng ) __attribute__(( warn_unused_result )); // [0,UINT_MAX] 397 413 static inline { 398 void set_seed( PRNG & prng, uint32_t seed_ ) with( prng ) { state = seed = seed_; } // set seed399 414 void ?{}( PRNG & prng ) { set_seed( prng, rdtscl() ); } // random seed 400 415 void ?{}( PRNG & prng, uint32_t seed ) { set_seed( prng, seed ); } // fixed seed … … 405 420 } // distribution 406 421 407 extern void set_seed( uint32_t seed ); // set per thread seed 408 extern uint32_t get_seed(); // get seed 409 extern uint32_t prng( void ) __attribute__(( warn_unused_result )); // [0,UINT_MAX] 422 // Concurrent Pseudo Random-Number Generator : generate repeatable sequence of values that appear random. 423 // 424 // Interface : 425 // set_seed( 1009 ) - fixed seed for all kernel threads versus random seed 426 // get_seed() - read seed 427 // prng() - generate random value in range [0,UINT_MAX] 428 // prng( u ) - generate random value in range [0,u) 429 // prng( l, u ) - generate random value in range [l,u] 430 // 431 // Examples : generate random number between 5-21 432 // prng() % 17 + 5; values 0-16 + 5 = 5-21 433 // prng( 16 + 1 ) + 5; 434 // prng( 5, 21 ); 435 436 void set_seed( uint32_t seed_ ) OPTIONAL_THREAD; 437 uint32_t get_seed() __attribute__(( warn_unused_result )); 438 uint32_t prng( void ) __attribute__(( warn_unused_result )) OPTIONAL_THREAD; // [0,UINT_MAX] 410 439 static inline { 411 uint32_t prng( uint32_t u ) __attribute__(( warn_unused_result )); 412 uint32_t prng( uint32_t u ) { return prng() % u; } // [0,u) 413 uint32_t prng( uint32_t l, uint32_t u ) __attribute__(( warn_unused_result )); 414 uint32_t prng( uint32_t l, uint32_t u ) { return prng( u - l + 1 ) + l; } // [l,u] 440 uint32_t prng( uint32_t u ) __attribute__(( warn_unused_result )) { return prng() % u; } // [0,u) 441 uint32_t prng( uint32_t l, uint32_t u ) __attribute__(( warn_unused_result )) { return prng( u - l + 1 ) + l; } // [l,u] 415 442 } // distribution 416 443
Note:
See TracChangeset
for help on using the changeset viewer.