@@ -126,33 +126,31 @@ type GetRandomFn = unsafe extern fn(*mut u8, libc::size_t, libc::c_uint)
126
126
// Instead the stable APIs are exposed via libc. Cache the result of the
127
127
// lookup for future calls. This is loosely modeled after the
128
128
// libstd::sys::unix::weak macro which unfortunately is not exported.
129
- fn fetch ( ) -> Option < & ' static GetRandomFn > {
129
+ fn fetch ( ) -> Option < GetRandomFn > {
130
130
static FPTR : AtomicUsize = AtomicUsize :: new ( 1 ) ;
131
131
132
+ if FPTR . load ( Ordering :: SeqCst ) == 1 {
133
+ let name = "getrandom\0 " ;
134
+ let addr = unsafe {
135
+ libc:: dlsym ( libc:: RTLD_DEFAULT , name. as_ptr ( ) as * const _ ) as usize
136
+ } ;
137
+ FPTR . store ( addr, Ordering :: SeqCst ) ;
138
+ }
139
+
140
+ let ptr = FPTR . load ( Ordering :: SeqCst ) ;
132
141
unsafe {
133
- if FPTR . load ( Ordering :: SeqCst ) == 1 {
134
- let name = "getrandom\0 " ;
135
- let addr = libc:: dlsym ( libc:: RTLD_DEFAULT ,
136
- name. as_ptr ( ) as * const _ ) as usize ;
137
- FPTR . store ( addr, Ordering :: SeqCst ) ;
138
- }
139
-
140
- if FPTR . load ( Ordering :: SeqCst ) == 0 {
141
- return None ;
142
- } else {
143
- mem:: transmute :: < & AtomicUsize , Option < & GetRandomFn > > ( & FPTR )
144
- }
145
- }
142
+ mem:: transmute :: < usize , Option < GetRandomFn > > ( ptr)
143
+ }
146
144
}
147
145
148
146
fn getrandom ( buf : & mut [ u8 ] , blocking : bool ) -> libc:: ssize_t {
149
147
const GRND_NONBLOCK : libc:: c_uint = 0x0001 ;
150
148
const GRND_RANDOM : libc:: c_uint = 0x0002 ;
151
149
152
150
if let Some ( rand) = fetch ( ) {
151
+ let flag = if blocking { 0 } else { GRND_NONBLOCK } | GRND_RANDOM ;
153
152
unsafe {
154
- rand ( buf. as_mut_ptr ( ) , buf. len ( ) ,
155
- if blocking { 0 } else { GRND_NONBLOCK } | GRND_RANDOM ) as libc:: ssize_t
153
+ rand ( buf. as_mut_ptr ( ) , buf. len ( ) , flag) as libc:: ssize_t
156
154
}
157
155
} else {
158
156
-1
0 commit comments