Skip to content

Commit d93f94f

Browse files
committed
Investigate miri issues
1 parent 1f848ba commit d93f94f

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

src/hole.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use super::align_up;
1010

1111
/// A sorted list of holes. It uses the the holes itself to store its nodes.
1212
pub struct HoleList {
13-
first: Hole, // dummy
13+
pub(crate) first: Hole, // dummy
1414
}
1515

1616
impl HoleList {
@@ -138,14 +138,7 @@ impl HoleList {
138138
}
139139

140140
/// A block containing free memory. It points to the next hole and thus forms a linked list.
141-
#[cfg(not(test))]
142-
struct Hole {
143-
size: usize,
144-
next: Option<&'static mut Hole>,
145-
}
146-
147-
#[cfg(test)]
148-
pub struct Hole {
141+
pub(crate) struct Hole {
149142
pub size: usize,
150143
pub next: Option<&'static mut Hole>,
151144
}

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ impl Heap {
185185
self.size - self.used
186186
}
187187

188+
pub(crate) fn holes(&self) -> &HoleList {
189+
&self.holes
190+
}
191+
188192
/// Extends the size of the heap by creating a new hole at the end
189193
///
190194
/// # Unsafety

src/test.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,26 @@ use core::alloc::Layout;
33
use std::mem::{align_of, size_of, MaybeUninit};
44
use std::prelude::v1::*;
55

6+
#[repr(align(128))]
7+
struct Chonk<const N: usize> {
8+
data: [MaybeUninit<u8>; N]
9+
}
10+
11+
impl<const N: usize> Chonk<N> {
12+
pub fn new() -> Self {
13+
Self {
14+
data: [MaybeUninit::uninit(); N],
15+
}
16+
}
17+
}
18+
619
fn new_heap() -> Heap {
720
const HEAP_SIZE: usize = 1000;
8-
let heap_space = Box::leak(Box::new([MaybeUninit::uninit(); HEAP_SIZE]));
9-
let assumed_location = heap_space.as_mut_ptr().cast();
21+
let heap_space = Box::leak(Box::new(Chonk::<HEAP_SIZE>::new()));
22+
let data = &mut heap_space.data;
23+
let assumed_location = data.as_mut_ptr().cast();
1024

11-
let heap = Heap::from_slice(heap_space);
25+
let heap = Heap::from_slice(data);
1226
assert!(heap.bottom == assumed_location);
1327
assert!(heap.size == HEAP_SIZE);
1428
heap
@@ -73,8 +87,10 @@ fn allocate_and_free_double_usize() {
7387
*(x.as_ptr() as *mut (usize, usize)) = (0xdeafdeadbeafbabe, 0xdeafdeadbeafbabe);
7488

7589
heap.deallocate(x, layout.clone());
76-
assert_eq!((*(heap.bottom as *const Hole)).size, heap.size);
77-
assert!((*(heap.bottom as *const Hole)).next.is_none());
90+
let real_first = heap.holes().first.next.as_ref().unwrap();
91+
92+
assert_eq!(real_first.size, heap.size);
93+
assert!(real_first.next.is_none());
7894
}
7995
}
8096

0 commit comments

Comments
 (0)