fix Vec leak with 0 capacity

This commit is contained in:
SparkyPotato 2022-04-06 01:32:26 +05:30
parent f262ca12aa
commit 31e7990145
1 changed files with 7 additions and 0 deletions

View File

@ -170,6 +170,13 @@ impl<T, A: Allocator> RawVec<T, A> {
fn allocate_in(capacity: usize, init: AllocInit, alloc: A) -> Self {
if mem::size_of::<T>() == 0 {
Self::new_in(alloc)
} else if capacity == 0 {
// Don't allocate here because `Drop` will not deallocate when `capacity` is 0.
Self {
ptr: unsafe { Unique::new_unchecked(NonNull::dangling().as_ptr()) },
cap: capacity,
alloc,
}
} else {
// We avoid `unwrap_or_else` here because it bloats the amount of
// LLVM IR generated.