Allocate extra space to account for alignment losses

This commit is contained in:
John Kåre Alsaker 2023-03-05 06:26:51 +01:00
parent 67b1d2aec1
commit d5d72168fa

View File

@ -410,7 +410,11 @@ impl Default for DroplessArena {
} }
impl DroplessArena { impl DroplessArena {
fn grow(&self, additional: usize) { fn grow(&self, layout: Layout) {
// Add some padding so we can align `self.end` while
// stilling fitting in a `layout` allocation.
let additional = layout.size() + cmp::max(DROPLESS_ALIGNMENT, layout.align()) - 1;
unsafe { unsafe {
let mut chunks = self.chunks.borrow_mut(); let mut chunks = self.chunks.borrow_mut();
let mut new_cap; let mut new_cap;
@ -429,7 +433,7 @@ impl DroplessArena {
// Also ensure that this chunk can fit `additional`. // Also ensure that this chunk can fit `additional`.
new_cap = cmp::max(additional, new_cap); new_cap = cmp::max(additional, new_cap);
let mut chunk = ArenaChunk::new(new_cap); let mut chunk = ArenaChunk::new(align(new_cap, PAGE));
self.start.set(chunk.start()); self.start.set(chunk.start());
// Align the end to DROPLESS_ALIGNMENT // Align the end to DROPLESS_ALIGNMENT
@ -445,8 +449,8 @@ impl DroplessArena {
#[inline(never)] #[inline(never)]
#[cold] #[cold]
fn grow_and_alloc_raw(&self, layout: Layout) -> *mut u8 { fn grow_and_alloc_raw(&self, layout: Layout) -> *mut u8 {
self.grow(layout.size()); self.grow(layout);
self.alloc_raw(layout) self.alloc_raw_without_grow(layout).unwrap()
} }
#[inline(never)] #[inline(never)]