1
- use core:: ptr:: Unique ;
2
1
use core:: mem:: size_of;
3
2
use alloc:: allocator:: { Layout , AllocErr } ;
4
3
@@ -32,7 +31,7 @@ impl HoleList {
32
31
HoleList {
33
32
first : Hole {
34
33
size : 0 ,
35
- next : Some ( Unique :: new_unchecked ( ptr) ) ,
34
+ next : Some ( & mut * ptr) ,
36
35
} ,
37
36
}
38
37
}
@@ -75,23 +74,21 @@ impl HoleList {
75
74
/// Returns information about the first hole for test purposes.
76
75
#[ cfg( test) ]
77
76
pub fn first_hole ( & self ) -> Option < ( usize , usize ) > {
78
- self . first . next . as_ref ( ) . map ( |hole| {
79
- ( hole. as_ptr ( ) as usize , unsafe { hole. as_ref ( ) . size } )
80
- } )
77
+ self . first . next . as_ref ( ) . map ( |hole| ( ( * hole) as * const Hole as usize , hole. size ) )
81
78
}
82
79
}
83
80
84
81
/// A block containing free memory. It points to the next hole and thus forms a linked list.
85
82
#[ cfg( not( test) ) ]
86
83
pub struct Hole {
87
84
size : usize ,
88
- next : Option < Unique < Hole > > ,
85
+ next : Option < & ' static mut Hole > ,
89
86
}
90
87
91
88
#[ cfg( test) ]
92
89
pub struct Hole {
93
90
pub size : usize ,
94
- pub next : Option < Unique < Hole > > ,
91
+ pub next : Option < & ' static mut Hole > ,
95
92
}
96
93
97
94
impl Hole {
@@ -102,11 +99,6 @@ impl Hole {
102
99
size : self . size ,
103
100
}
104
101
}
105
-
106
- /// Returns a reference to the next hole. Panics if this is the last hole.
107
- fn next_unwrap ( & mut self ) -> & mut Hole {
108
- unsafe { self . next . as_mut ( ) . unwrap ( ) . as_mut ( ) }
109
- }
110
102
}
111
103
112
104
/// Basic information about a hole.
@@ -193,17 +185,17 @@ fn split_hole(hole: HoleInfo, required_layout: Layout) -> Option<Allocation> {
193
185
fn allocate_first_fit ( mut previous : & mut Hole , layout : Layout ) -> Result < Allocation , AllocErr > {
194
186
loop {
195
187
let allocation: Option < Allocation > = previous. next . as_mut ( ) . and_then ( |current| {
196
- split_hole ( unsafe { current. as_ref ( ) } . info ( ) , layout. clone ( ) )
188
+ split_hole ( current. info ( ) , layout. clone ( ) )
197
189
} ) ;
198
190
match allocation {
199
191
Some ( allocation) => {
200
192
// hole is big enough, so remove it from the list by updating the previous pointer
201
- previous. next = previous. next_unwrap ( ) . next . take ( ) ;
193
+ previous. next = previous. next . as_mut ( ) . unwrap ( ) . next . take ( ) ;
202
194
return Ok ( allocation) ;
203
195
}
204
196
None if previous. next . is_some ( ) => {
205
197
// try next hole
206
- previous = move_helper ( previous) . next_unwrap ( ) ;
198
+ previous = move_helper ( previous) . next . as_mut ( ) . unwrap ( ) ;
207
199
}
208
200
None => {
209
201
// this was the last hole, so no hole is big enough -> allocation not possible
@@ -237,9 +229,7 @@ fn deallocate(mut hole: &mut Hole, addr: usize, mut size: usize) {
237
229
) ;
238
230
239
231
// get information about the next block
240
- let next_hole_info = hole. next
241
- . as_ref ( )
242
- . map ( |next| unsafe { next. as_ref ( ) . info ( ) } ) ;
232
+ let next_hole_info = hole. next . as_ref ( ) . map ( |next| next. info ( ) ) ;
243
233
244
234
match next_hole_info {
245
235
Some ( next) if hole_addr + hole. size == addr && addr + size == next. addr => {
@@ -248,7 +238,7 @@ fn deallocate(mut hole: &mut Hole, addr: usize, mut size: usize) {
248
238
// after: ___XXXFFFFYYYYY____ where F is the freed block
249
239
250
240
hole. size += size + next. size ; // merge the F and Y blocks to this X block
251
- hole. next = hole. next_unwrap ( ) . next . take ( ) ; // remove the Y block
241
+ hole. next = hole. next . as_mut ( ) . unwrap ( ) . next . take ( ) ; // remove the Y block
252
242
}
253
243
_ if hole_addr + hole. size == addr => {
254
244
// block is right behind this hole but there is used memory after it
@@ -266,7 +256,7 @@ fn deallocate(mut hole: &mut Hole, addr: usize, mut size: usize) {
266
256
// before: ___XXX______YYYYY____ where X is this hole and Y the next hole
267
257
// after: ___XXX__FFFFYYYYY____ where F is the freed block
268
258
269
- hole. next = hole. next_unwrap ( ) . next . take ( ) ; // remove the Y block
259
+ hole. next = hole. next . as_mut ( ) . unwrap ( ) . next . take ( ) ; // remove the Y block
270
260
size += next. size ; // free the merged F/Y block in next iteration
271
261
continue ;
272
262
}
@@ -275,7 +265,7 @@ fn deallocate(mut hole: &mut Hole, addr: usize, mut size: usize) {
275
265
// before: ___XXX__YYYYY________ where X is this hole and Y the next hole
276
266
// after: ___XXX__YYYYY__FFFF__ where F is the freed block
277
267
278
- hole = move_helper ( hole) . next_unwrap ( ) ; // start next iteration at next hole
268
+ hole = move_helper ( hole) . next . as_mut ( ) . unwrap ( ) ; // start next iteration at next hole
279
269
continue ;
280
270
}
281
271
_ => {
@@ -295,7 +285,7 @@ fn deallocate(mut hole: &mut Hole, addr: usize, mut size: usize) {
295
285
let ptr = addr as * mut Hole ;
296
286
unsafe { ptr. write ( new_hole) } ;
297
287
// add the F block as the next block of the X block
298
- hole. next = Some ( unsafe { Unique :: new_unchecked ( ptr) } ) ;
288
+ hole. next = Some ( unsafe { & mut * ptr } ) ;
299
289
}
300
290
}
301
291
break ;
0 commit comments