Skip to content

Align up the Hole initialization address #18

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 2 commits into from
Jan 14, 2020
Merged
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
10 changes: 7 additions & 3 deletions src/hole.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloc::alloc::{AllocErr, Layout};
use core::mem::size_of;
use core::mem::{align_of, size_of};
use core::ptr::NonNull;

use super::align_up;
Expand All @@ -23,12 +23,15 @@ impl HoleList {
/// Creates a `HoleList` that contains the given hole. This function is unsafe because it
/// creates a hole at the given `hole_addr`. This can cause undefined behavior if this address
/// is invalid or if memory from the `[hole_addr, hole_addr+size) range is used somewhere else.
///
/// The pointer to `hole_addr` is automatically aligned.
pub unsafe fn new(hole_addr: usize, hole_size: usize) -> HoleList {
assert!(size_of::<Hole>() == Self::min_size());

let ptr = hole_addr as *mut Hole;
let aligned_hole_addr = align_up(hole_addr, align_of::<Hole>());
let ptr = aligned_hole_addr as *mut Hole;
ptr.write(Hole {
size: hole_size,
size: hole_size.saturating_sub(aligned_hole_addr - hole_addr),
next: None,
});

Expand Down Expand Up @@ -290,6 +293,7 @@ fn deallocate(mut hole: &mut Hole, addr: usize, mut size: usize) {
next: hole.next.take(), // the reference to the Y block (if it exists)
};
// write the new hole to the freed memory
debug_assert_eq!(addr % align_of::<Hole>(), 0);
let ptr = addr as *mut Hole;
unsafe { ptr.write(new_hole) };
// add the F block as the next block of the X block
Expand Down