File tree Expand file tree Collapse file tree 1 file changed +9
-15
lines changed Expand file tree Collapse file tree 1 file changed +9
-15
lines changed Original file line number Diff line number Diff line change 1
1
use core:: { alloc:: Layout , convert:: TryInto } ;
2
- use rand:: distributions:: { Distribution , Uniform } ;
2
+ use rand:: {
3
+ distributions:: { Distribution , Uniform } ,
4
+ seq:: IteratorRandom ,
5
+ } ;
3
6
use rand_chacha:: ChaCha20Rng ;
4
7
use usize_conversions:: IntoUsize ;
5
8
use x86_64:: {
@@ -121,23 +124,14 @@ impl UsedLevel4Entries {
121
124
. map ( |( idx, _) | idx) ;
122
125
123
126
// Choose the free entry index.
124
- let idx = if let Some ( rng) = self . rng . as_mut ( ) {
125
- // Count the entries and randomly choose an index in `[0..count)`.
126
- let count = free_entries. clone ( ) . count ( ) ;
127
- if count == 0 {
128
- panic ! ( "no usable level 4 entries found" )
129
- }
130
- let distribution = Uniform :: from ( 0 ..count) ;
131
- let idx = distribution. sample ( rng) ;
132
-
133
- // Get the index of the free entry.
134
- free_entries. nth ( idx) . unwrap ( )
127
+ let idx_opt = if let Some ( rng) = self . rng . as_mut ( ) {
128
+ // Randomly choose an index.
129
+ free_entries. choose ( rng)
135
130
} else {
136
131
// Choose the first index.
137
- free_entries
138
- . next ( )
139
- . expect ( "no usable level 4 entries found" )
132
+ free_entries. next ( )
140
133
} ;
134
+ let idx = idx_opt. expect ( "no usable level 4 entry found" ) ;
141
135
142
136
// Mark the entry as used.
143
137
self . entry_state [ idx] = true ;
You can’t perform that action at this time.
0 commit comments