Skip to content
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

Make sure to align pointers in alloc_layout_slow #45

Merged
merged 1 commit into from
Nov 7, 2019
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
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,10 @@ impl Bump {
let current_layout = current_footer.as_ref().layout;
let new_footer =
Bump::new_chunk(Some((current_layout.size(), layout)), Some(current_footer));
debug_assert_eq!(
new_footer.as_ref().data.as_ptr() as usize % layout.align(),
0
);

// Set the new chunk as our new current chunk.
self.current_chunk_footer.set(new_footer);
Expand All @@ -646,6 +650,8 @@ impl Bump {
// this can't overflow because we successfully allocated a chunk of
// at least the requested size.
let ptr = new_footer.ptr.get().as_ptr() as usize - size;
// Round the pointer down to the requested alignment.
let ptr = ptr & !(layout.align() - 1);
debug_assert!(
ptr <= new_footer as *const _ as usize,
"{:#x} <= {:#x}",
Expand Down
12 changes: 12 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,15 @@ fn test_reset() {
);
assert_eq!(b.iter_allocated_chunks().count(), 1);
}

#[test]
fn test_alignment() {
let b = Bump::new();

let layout = std::alloc::Layout::from_size_align(64, 64).unwrap();

for _ in 0..1024 {
let ptr = b.alloc_layout(layout).as_ptr();
assert_eq!(ptr as *const u8 as usize % 64, 0);
}
}