Auto merge of #78058 - bugadani:arena2, r=lcnr

Make sure arenas don't allocate bigger than HUGE_PAGE

Right now, arenas allocate based on the size of the last chunk. It is possible for a `grow` call to allocate a chunk that is not a multiple of `PAGE`, and this size is doubled for each subsequent allocation. This means, instead of `HUGE_PAGE`, the biggest page possible is actually unknown.

This change fixes this, and also removes an unnecessary checked multiplication. It is still possible to allocate bigger than `HUGE_PAGE` pages, but this will only happen as many times as absolutely necessary.
This commit is contained in:
bors 2020-10-18 11:19:14 +00:00
commit 98e16884b1

View File

@ -223,10 +223,8 @@ impl<T> TypedArena<T> {
// If the previous chunk's len is less than HUGE_PAGE
// bytes, then this chunk will be least double the previous
// chunk's size.
new_cap = last_chunk.storage.len();
if new_cap < HUGE_PAGE / elem_size {
new_cap = new_cap.checked_mul(2).unwrap();
}
new_cap = last_chunk.storage.len().min(HUGE_PAGE / elem_size / 2);
new_cap = new_cap * 2;
} else {
new_cap = PAGE / elem_size;
}
@ -343,10 +341,8 @@ impl DroplessArena {
// If the previous chunk's len is less than HUGE_PAGE
// bytes, then this chunk will be least double the previous
// chunk's size.
new_cap = last_chunk.storage.len();
if new_cap < HUGE_PAGE {
new_cap = new_cap.checked_mul(2).unwrap();
}
new_cap = last_chunk.storage.len().min(HUGE_PAGE / 2);
new_cap = new_cap * 2;
} else {
new_cap = PAGE;
}