Skip to content

Commit a4bf336

Browse files
committed
Use &mut pointers instead of Unique for hole linking
1 parent b009e84 commit a4bf336

File tree

2 files changed

+12
-24
lines changed

2 files changed

+12
-24
lines changed

src/hole.rs

+12-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use core::ptr::Unique;
21
use core::mem::size_of;
32
use alloc::allocator::{Layout, AllocErr};
43

@@ -32,7 +31,7 @@ impl HoleList {
3231
HoleList {
3332
first: Hole {
3433
size: 0,
35-
next: Some(Unique::new_unchecked(ptr)),
34+
next: Some(&mut *ptr),
3635
},
3736
}
3837
}
@@ -75,23 +74,21 @@ impl HoleList {
7574
/// Returns information about the first hole for test purposes.
7675
#[cfg(test)]
7776
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))
8178
}
8279
}
8380

8481
/// A block containing free memory. It points to the next hole and thus forms a linked list.
8582
#[cfg(not(test))]
8683
pub struct Hole {
8784
size: usize,
88-
next: Option<Unique<Hole>>,
85+
next: Option<&'static mut Hole>,
8986
}
9087

9188
#[cfg(test)]
9289
pub struct Hole {
9390
pub size: usize,
94-
pub next: Option<Unique<Hole>>,
91+
pub next: Option<&'static mut Hole>,
9592
}
9693

9794
impl Hole {
@@ -102,11 +99,6 @@ impl Hole {
10299
size: self.size,
103100
}
104101
}
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-
}
110102
}
111103

112104
/// Basic information about a hole.
@@ -193,17 +185,17 @@ fn split_hole(hole: HoleInfo, required_layout: Layout) -> Option<Allocation> {
193185
fn allocate_first_fit(mut previous: &mut Hole, layout: Layout) -> Result<Allocation, AllocErr> {
194186
loop {
195187
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())
197189
});
198190
match allocation {
199191
Some(allocation) => {
200192
// 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();
202194
return Ok(allocation);
203195
}
204196
None if previous.next.is_some() => {
205197
// try next hole
206-
previous = move_helper(previous).next_unwrap();
198+
previous = move_helper(previous).next.as_mut().unwrap();
207199
}
208200
None => {
209201
// 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) {
237229
);
238230

239231
// 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());
243233

244234
match next_hole_info {
245235
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) {
248238
// after: ___XXXFFFFYYYYY____ where F is the freed block
249239

250240
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
252242
}
253243
_ if hole_addr + hole.size == addr => {
254244
// 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) {
266256
// before: ___XXX______YYYYY____ where X is this hole and Y the next hole
267257
// after: ___XXX__FFFFYYYYY____ where F is the freed block
268258

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
270260
size += next.size; // free the merged F/Y block in next iteration
271261
continue;
272262
}
@@ -275,7 +265,7 @@ fn deallocate(mut hole: &mut Hole, addr: usize, mut size: usize) {
275265
// before: ___XXX__YYYYY________ where X is this hole and Y the next hole
276266
// after: ___XXX__YYYYY__FFFF__ where F is the freed block
277267

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
279269
continue;
280270
}
281271
_ => {
@@ -295,7 +285,7 @@ fn deallocate(mut hole: &mut Hole, addr: usize, mut size: usize) {
295285
let ptr = addr as *mut Hole;
296286
unsafe { ptr.write(new_hole) };
297287
// 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 });
299289
}
300290
}
301291
break;

src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
#![feature(unique)]
21
#![feature(const_fn)]
32
#![feature(alloc, allocator_api)]
43
#![feature(pointer_methods)]
54
#![no_std]
6-
#![feature(ptr_internals)]
75

86
extern crate alloc;
97

0 commit comments

Comments
 (0)