Changes in libcfa/src/stdlib.hfa [919a6b2:aa8e24c3]
- File:
-
- 1 edited
-
libcfa/src/stdlib.hfa (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/stdlib.hfa
r919a6b2 raa8e24c3 10 10 // Created On : Thu Jan 28 17:12:35 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Jan 13 21:34:46 202213 // Update Count : 63612 // Last Modified On : Wed Dec 29 15:30:58 2021 13 // Update Count : 591 14 14 // 15 15 … … 21 21 #include <stdlib.h> // *alloc, strto*, ato* 22 22 #include <heap.hfa> 23 24 23 25 24 // Reduce includes by explicitly defining these routines. … … 44 43 //--------------------------------------- 45 44 45 // Macro because of returns 46 #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 46 50 static inline forall( T & | sized(T) ) { 47 51 // CFA safe equivalents, i.e., implicit size specification 48 52 49 53 T * malloc( void ) { 50 if ( _Alignof(T) <= libAlign() ) return (T *) malloc(sizeof(T) ); // C allocation54 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C allocation 51 55 else return (T *)memalign( _Alignof(T), sizeof(T) ); 52 56 } // malloc 53 57 54 58 T * aalloc( size_t dim ) { 55 if ( _Alignof(T) <= libAlign() ) return (T *)aalloc( dim, sizeof(T) ); // C allocation 56 else return (T *)amemalign( _Alignof(T), dim, sizeof(T) ); 59 ARRAY_ALLOC$( aalloc, amemalign, dim ); 57 60 } // aalloc 58 61 59 62 T * calloc( size_t dim ) { 60 if ( _Alignof(T) <= libAlign() ) return (T *)calloc( dim, sizeof(T) ); // C allocation 61 else return (T *)cmemalign( _Alignof(T), dim, sizeof(T) ); 63 ARRAY_ALLOC$( calloc, cmemalign, dim ); 62 64 } // calloc 63 65 64 66 T * resize( T * ptr, size_t size ) { // CFA resize, eliminate return-type cast 65 if ( _Alignof(T) <= libAlign() ) return (T *) resize( (void *)ptr, size ); // CFA resize66 else return (T *) resize( (void *)ptr, _Alignof(T), size ); // CFA resize67 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)resize( (void *)ptr, size ); // CFA resize 68 else return (T *)(void *)resize( (void *)ptr, _Alignof(T), size ); // CFA resize 67 69 } // resize 68 70 69 71 T * realloc( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast 70 if ( _Alignof(T) <= libAlign() ) return (T *) realloc( (void *)ptr, size ); // C realloc71 else return (T *) realloc( (void *)ptr, _Alignof(T), size ); // CFA realloc72 if ( _Alignof(T) <= libAlign() ) return (T *)(void *)realloc( (void *)ptr, size ); // C realloc 73 else return (T *)(void *)realloc( (void *)ptr, _Alignof(T), size ); // CFA realloc 72 74 } // realloc 73 75 … … 208 210 209 211 forall( TT... | { T * alloc_internal$( void *, T *, size_t, size_t, S_fill(T), TT ); } ) { 212 210 213 T * alloc_internal$( void * , T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill, T_resize Resize, TT rest) { 211 214 return alloc_internal$( Resize, (T*)0p, Align, Dim, Fill, rest); … … 231 234 return alloc_internal$( (void*)0p, (T*)0p, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), dim, (S_fill(T)){'0'}, all); 232 235 } 236 233 237 } // distribution TT 234 238 } // distribution T … … 384 388 //--------------------------------------- 385 389 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 seed390 //391 // Interface :392 // set_seed( sprng, 1009 ) - set starting seed for ALL kernel threads versus random seed393 // get_seed( sprng ) - read seed394 // 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 far398 //399 // Examples : generate random number between 5-21400 // prng( sprng ) % 17 + 5; values 0-16 + 5 = 5-21401 // prng( sprng, 16 + 1 ) + 5;402 // prng( sprng, 5, 21 );403 // calls( sprng );404 405 390 struct PRNG { 406 391 uint32_t callcnt; // call count … … 409 394 }; // PRNG 410 395 411 void set_seed( PRNG & prng, uint32_t seed_ ); 412 uint32_t prng( PRNG & prng ) __attribute__(( warn_unused_result )); // [0,UINT_MAX] 396 extern uint32_t prng( PRNG & prng ) __attribute__(( warn_unused_result )); // [0,UINT_MAX] 413 397 static inline { 398 void set_seed( PRNG & prng, uint32_t seed_ ) with( prng ) { state = seed = seed_; } // set seed 414 399 void ?{}( PRNG & prng ) { set_seed( prng, rdtscl() ); } // random seed 415 400 void ?{}( PRNG & prng, uint32_t seed ) { set_seed( prng, seed ); } // fixed seed … … 420 405 } // distribution 421 406 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] 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] 439 410 static inline { 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] 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] 442 415 } // distribution 443 416
Note:
See TracChangeset
for help on using the changeset viewer.