Skip to content

Commit

Permalink
Fix free logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmunns committed Jul 1, 2022
1 parent cf6a569 commit f55f7f9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
51 changes: 29 additions & 22 deletions src/hole.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ impl HoleList {
}
}

#[cfg(test)]
#[allow(dead_code)]
pub(crate) fn debug(&mut self) {
if let Some(cursor) = self.cursor() {
Expand Down Expand Up @@ -421,36 +420,44 @@ impl Cursor {
}

fn try_insert_after(&mut self, mut node: NonNull<Hole>) -> Result<(), ()> {
if self.hole < node {
let node_u8 = node.as_ptr().cast::<u8>();
let node_size = unsafe { node.as_ref().size };
let hole_u8 = self.hole.as_ptr().cast::<u8>();
let hole_size = self.current().size;
let node_u8 = node.as_ptr().cast::<u8>();
let node_size = unsafe { node.as_ref().size };

// Does hole overlap node?
assert!(
hole_u8.wrapping_add(hole_size) <= node_u8,
"Freed node aliases existing hole! Bad free?",
);

// If we have a next, does the node overlap next?
if let Some(next) = self.current().next.as_ref() {
// If we have a next, does the node overlap next?
if let Some(next) = self.current().next.as_ref() {
if node < *next {
let node_u8 = node_u8 as *const u8;
assert!(
node_u8.wrapping_add(node_size) <= next.as_ptr().cast::<u8>(),
"Freed node aliases existing hole! Bad free?",
);
} else {
// The new hole isn't between current and next.
return Err(());
}
}

// All good! Let's insert that after.
unsafe {
let maybe_next = self.hole.as_mut().next.replace(node);
node.as_mut().next = maybe_next;
}
Ok(())
} else {
Err(())
// At this point, we either have no "next" pointer, or the hole is
// between current and "next". The following assert can only trigger
// if we've gotten our list out of order.
debug_assert!(self.hole < node, "Hole list out of order?");

let hole_u8 = self.hole.as_ptr().cast::<u8>();
let hole_size = self.current().size;

// Does hole overlap node?
assert!(
hole_u8.wrapping_add(hole_size) <= node_u8,
"Freed node aliases existing hole! Bad free?",
);

// All good! Let's insert that after.
unsafe {
let maybe_next = self.hole.as_mut().next.replace(node);
node.as_mut().next = maybe_next;
}

Ok(())
}

// Merge the current node with up to n following nodes
Expand Down
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
)]
#![no_std]

#[cfg(test)]
#[macro_use]
extern crate std;

Expand Down Expand Up @@ -160,10 +159,14 @@ impl Heap {
pub fn allocate_first_fit(&mut self, layout: Layout) -> Result<NonNull<u8>, ()> {
match self.holes.allocate_first_fit(layout) {
Ok((ptr, aligned_layout)) => {
self.holes.debug();
self.used += aligned_layout.size();
Ok(ptr)
}
Err(err) => Err(err),
Err(err) => {
println!("ERR");
Err(err)
}
}
}

Expand All @@ -180,6 +183,7 @@ impl Heap {
/// identical layout. Undefined behavior may occur for invalid arguments.
pub unsafe fn deallocate(&mut self, ptr: NonNull<u8>, layout: Layout) {
self.used -= self.holes.deallocate(ptr, layout).size();
self.holes.debug();
}

/// Returns the bottom address of the heap.
Expand Down

0 comments on commit f55f7f9

Please sign in to comment.