Skip to content

Commit 855b651

Browse files
committed
Add Heap constructor, utilize in testing
1 parent 059c706 commit 855b651

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use core::alloc::GlobalAlloc;
1717
use core::alloc::Layout;
1818
#[cfg(feature = "alloc_ref")]
1919
use core::alloc::{AllocError, Allocator};
20+
use core::mem::MaybeUninit;
2021
#[cfg(feature = "use_spin")]
2122
use core::ops::Deref;
2223
use core::ptr::NonNull;
@@ -89,9 +90,9 @@ impl Heap {
8990
/// This method panics if the heap is already initialized.
9091
pub fn init_from_slice(&mut self, mem: &'static mut [MaybeUninit<u8>]) {
9192
assert!(self.bottom == 0, "The heap has already been initialized.");
92-
let size = mem.size();
93+
let size = mem.len();
9394
let address = mem.as_ptr() as usize;
94-
// Safety: All initialization requires the bottom address to be valid, which implies it
95+
// SAFETY: All initialization requires the bottom address to be valid, which implies it
9596
// must not be 0. Initially the address is 0. The assertion above ensures that no
9697
// initialization had been called before.
9798
// The given address and size is valid according to the safety invariants of the mutable
@@ -116,6 +117,15 @@ impl Heap {
116117
}
117118
}
118119

120+
/// Crates a new heap from a slice of raw memory.
121+
pub fn with_memory(mem: &'static mut [MaybeUninit<u8>]) -> Heap {
122+
let size = mem.len();
123+
let address = mem.as_ptr() as usize;
124+
// SAFETY: The given address and size is valid according to the safety invariants of the
125+
// mutable reference handed to us by the caller.
126+
unsafe { Self::new(address, size) }
127+
}
128+
119129
/// Allocates a chunk of the given size with the given alignment. Returns a pointer to the
120130
/// beginning of that chunk if it was successful. Else it returns `None`.
121131
/// This function scans the list of free memory blocks and uses the first block that is big

src/test.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
use super::*;
22
use core::alloc::Layout;
3-
use std::mem::{align_of, size_of};
3+
use std::mem::{align_of, size_of, MaybeUninit};
44
use std::prelude::v1::*;
55

66
fn new_heap() -> Heap {
77
const HEAP_SIZE: usize = 1000;
8-
let heap_space = Box::into_raw(Box::new([0u8; HEAP_SIZE]));
8+
let heap_space = Box::leak(Box::new([MaybeUninit::uninit(); HEAP_SIZE]));
9+
let assumed_location = heap_space.as_ptr() as usize;
910

10-
let heap = unsafe { Heap::new(heap_space as usize, HEAP_SIZE) };
11-
assert!(heap.bottom == heap_space as usize);
11+
let heap = Heap::with_memory(heap_space);
12+
assert!(heap.bottom == assumed_location);
1213
assert!(heap.size == HEAP_SIZE);
1314
heap
1415
}
1516

1617
fn new_max_heap() -> Heap {
1718
const HEAP_SIZE: usize = 1024;
1819
const HEAP_SIZE_MAX: usize = 2048;
19-
let heap_space = Box::into_raw(Box::new([0u8; HEAP_SIZE_MAX]));
20+
let heap_space = Box::leak(Box::new([MaybeUninit::<u8>::uninit(); HEAP_SIZE_MAX]));
21+
let assumed_location = heap_space.as_ptr() as usize;
2022

21-
let heap = unsafe { Heap::new(heap_space as usize, HEAP_SIZE) };
22-
assert!(heap.bottom == heap_space as usize);
23+
// Unsafe so that we have provenance over the whole allocation.
24+
let heap = unsafe { Heap::new(assumed_location, HEAP_SIZE) };
25+
assert!(heap.bottom == assumed_location);
2326
assert!(heap.size == HEAP_SIZE);
2427
heap
2528
}

0 commit comments

Comments
 (0)