Skip to content

Commit

Permalink
Add Heap constructor, utilize in testing
Browse files Browse the repository at this point in the history
  • Loading branch information
HeroicKatora committed Oct 6, 2021
1 parent 059c706 commit 855b651
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
14 changes: 12 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use core::alloc::GlobalAlloc;
use core::alloc::Layout;
#[cfg(feature = "alloc_ref")]
use core::alloc::{AllocError, Allocator};
use core::mem::MaybeUninit;
#[cfg(feature = "use_spin")]
use core::ops::Deref;
use core::ptr::NonNull;
Expand Down Expand Up @@ -89,9 +90,9 @@ impl Heap {
/// This method panics if the heap is already initialized.
pub fn init_from_slice(&mut self, mem: &'static mut [MaybeUninit<u8>]) {
assert!(self.bottom == 0, "The heap has already been initialized.");
let size = mem.size();
let size = mem.len();
let address = mem.as_ptr() as usize;
// Safety: All initialization requires the bottom address to be valid, which implies it
// SAFETY: All initialization requires the bottom address to be valid, which implies it
// must not be 0. Initially the address is 0. The assertion above ensures that no
// initialization had been called before.
// The given address and size is valid according to the safety invariants of the mutable
Expand All @@ -116,6 +117,15 @@ impl Heap {
}
}

/// Crates a new heap from a slice of raw memory.
pub fn with_memory(mem: &'static mut [MaybeUninit<u8>]) -> Heap {
let size = mem.len();
let address = mem.as_ptr() as usize;
// SAFETY: The given address and size is valid according to the safety invariants of the
// mutable reference handed to us by the caller.
unsafe { Self::new(address, size) }
}

/// Allocates a chunk of the given size with the given alignment. Returns a pointer to the
/// beginning of that chunk if it was successful. Else it returns `None`.
/// This function scans the list of free memory blocks and uses the first block that is big
Expand Down
17 changes: 10 additions & 7 deletions src/test.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
use super::*;
use core::alloc::Layout;
use std::mem::{align_of, size_of};
use std::mem::{align_of, size_of, MaybeUninit};
use std::prelude::v1::*;

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

let heap = unsafe { Heap::new(heap_space as usize, HEAP_SIZE) };
assert!(heap.bottom == heap_space as usize);
let heap = Heap::with_memory(heap_space);
assert!(heap.bottom == assumed_location);
assert!(heap.size == HEAP_SIZE);
heap
}

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

let heap = unsafe { Heap::new(heap_space as usize, HEAP_SIZE) };
assert!(heap.bottom == heap_space as usize);
// Unsafe so that we have provenance over the whole allocation.
let heap = unsafe { Heap::new(assumed_location, HEAP_SIZE) };
assert!(heap.bottom == assumed_location);
assert!(heap.size == HEAP_SIZE);
heap
}
Expand Down

0 comments on commit 855b651

Please sign in to comment.