Skip to content

Increase front_padding to min_size instead of skipping the whole hole #1

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
May 17, 2016
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "linked_list_allocator"
version = "0.2.1"
version = "0.2.2"
authors = ["Philipp Oppermann <[email protected]>"]
license = "Apache-2.0/MIT"

Expand Down
33 changes: 15 additions & 18 deletions src/hole.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,32 +130,29 @@ struct Allocation {
/// padding occurs if the required size is smaller than the size of the aligned hole. All padding
/// must be at least `HoleList::min_size()` big or the hole is unusable.
fn split_hole(hole: HoleInfo, required_size: usize, required_align: usize) -> Option<Allocation> {
let (aligned_addr, front_padding) = if hole.addr == align_up(hole.addr, required_align) {
// hole has already the required alignment
(hole.addr, None)
} else {
// the required alignment causes some padding before the allocation
let aligned_addr = align_up(hole.addr + HoleList::min_size(), required_align);
(aligned_addr, Some(HoleInfo {
addr: hole.addr,
size: aligned_addr - hole.addr,
}))
};

let aligned_hole = {
let aligned_hole_addr = align_up(hole.addr, required_align);
if aligned_hole_addr + required_size > hole.addr + hole.size {
if aligned_addr + required_size > hole.addr + hole.size {
// hole is too small
return None;
}
HoleInfo {
addr: aligned_hole_addr,
size: hole.size - (aligned_hole_addr - hole.addr),
addr: aligned_addr,
size: hole.size - (aligned_addr - hole.addr),
}
};

let front_padding = if aligned_hole.addr == hole.addr {
// hole has already the required alignment
None
} else if aligned_hole.addr < hole.addr + HoleList::min_size() {
// we can't use this hole because the required padding would create a new, too small hole
return None;
} else {
// the required alignment causes some padding before the allocation
Some(HoleInfo {
addr: hole.addr,
size: aligned_hole.addr - hole.addr,
})
};

let back_padding = if aligned_hole.size == required_size {
// the aligned hole has exactly the size that's needed, no padding accrues
None
Expand Down
12 changes: 11 additions & 1 deletion src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ fn allocate_usize() {
assert!(heap.allocate_first_fit(size_of::<usize>(), 1).is_some());
}


#[test]
fn allocate_usize_in_bigger_block() {
let mut heap = new_heap();
Expand All @@ -192,3 +191,14 @@ fn allocate_usize_in_bigger_block() {
heap.deallocate(z, size_of::<usize>(), 1);
}
}

#[test]
// see https://github.com/phil-opp/blog_os/issues/160
fn align_from_small_to_big() {
let mut heap = new_heap();

// allocate 28 bytes so that the heap end is only 4 byte aligned
assert!(heap.allocate_first_fit(28, 4).is_some());
// try to allocate a 8 byte aligned block
assert!(heap.allocate_first_fit(8, 8).is_some());
}