Skip to content

Make it possible to extend the size of the heap #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/hole.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ impl HoleList {
assert!(size_of::<Hole>() == Self::min_size());

let ptr = hole_addr as *mut Hole;
mem::forget(mem::replace(&mut *ptr,
Hole {
size: hole_size,
next: None,
}));
mem::replace(&mut *ptr,
Hole {
size: hole_size,
next: None,
});

HoleList {
first: Hole {
Expand Down Expand Up @@ -240,11 +240,15 @@ fn deallocate(mut hole: &mut Hole, addr: usize, mut size: usize) {
hole.size += size + next.size; // merge the F and Y blocks to this X block
hole.next = hole.next_unwrap().next.take(); // remove the Y block
}
Some(_) if hole_addr + hole.size == addr => {
_ if hole_addr + hole.size == addr => {
// block is right behind this hole but there is used memory after it
// before: ___XXX______YYYYY____ where X is this hole and Y the next hole
// after: ___XXXFFFF__YYYYY____ where F is the freed block

// or: block is right behind this hole and this is the last hole
// before: ___XXX_______________ where X is this hole and Y the next hole
// after: ___XXXFFFF___________ where F is the freed block

hole.size += size; // merge the F block to this X block
}
Some(next) if addr + size == next.addr => {
Expand Down Expand Up @@ -279,7 +283,7 @@ fn deallocate(mut hole: &mut Hole, addr: usize, mut size: usize) {
};
// write the new hole to the freed memory
let ptr = addr as *mut Hole;
mem::forget(mem::replace(unsafe { &mut *ptr }, new_hole));
mem::replace(unsafe { &mut *ptr }, new_hole);
// add the F block as the next block of the X block
hole.next = Some(unsafe { Unique::new(ptr) });
}
Expand Down
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@ impl Heap {
pub fn size(&self) -> usize {
self.size
}

/// Return the top address of the heap
pub fn top(&self) -> usize {
self.bottom + self.size
}

/// Extends the size of the heap by creating a new hole at the end
///
/// # Unsafety
///
/// The new extended area must be valid
pub unsafe fn extend(&mut self, by: usize) {
let top = self.top();
self.holes.deallocate(top as *mut u8, by);
self.size += by;
}
}

/// Align downwards. Returns the greatest x with alignment `align`
Expand Down
59 changes: 59 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ fn new_heap() -> Heap {
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 = unsafe { Heap::new(heap_space as usize, HEAP_SIZE) };
assert!(heap.bottom == heap_space as usize);
assert!(heap.size == HEAP_SIZE);
heap
}

#[test]
fn empty() {
let mut heap = Heap::empty();
Expand Down Expand Up @@ -201,3 +212,51 @@ fn align_from_small_to_big() {
// try to allocate a 8 byte aligned block
assert!(heap.allocate_first_fit(8, 8).is_some());
}

#[test]
fn extend_empty_heap() {
let mut heap = new_max_heap();

unsafe {
heap.extend(1024);
}

// Try to allocate full heap after extend
assert!(heap.allocate_first_fit(2048, 1).is_some());
}

#[test]
fn extend_full_heap() {
let mut heap = new_max_heap();

// Allocate full heap, extend and allocate again to the max
assert!(heap.allocate_first_fit(1024, 1).is_some());
unsafe {
heap.extend(1024);
}
assert!(heap.allocate_first_fit(1024, 1).is_some());
}

#[test]
fn extend_fragmented_heap() {
let mut heap = new_max_heap();

let alloc1 = heap.allocate_first_fit(512, 1);
let alloc2 = heap.allocate_first_fit(512, 1);

assert!(alloc1.is_some());
assert!(alloc2.is_some());

unsafe {
// Create a hole at the beginning of the heap
heap.deallocate(alloc1.unwrap(), 512, 1);
}

unsafe {
heap.extend(1024);
}

// We got additional 1024 bytes hole at the end of the heap
// Try to allocate there
assert!(heap.allocate_first_fit(1024, 1).is_some());
}