Skip to content
Merged
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
30 changes: 19 additions & 11 deletions crates/oxc_allocator/src/pool/fixed_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,24 @@ impl FixedSizeAllocatorPool {
/// # Panics
/// * Panics if the underlying mutex is poisoned.
pub fn get(&self) -> Allocator {
fn into_allocator(allocator: FixedSizeAllocator) -> Allocator {
// SAFETY: `FixedSizeAllocator` is just a wrapper around `ManuallyDrop<Allocator>`,
// and is `#[repr(transparent)]`, so the 2 are equivalent.
let allocator =
unsafe { mem::transmute::<FixedSizeAllocator, ManuallyDrop<Allocator>>(allocator) };
ManuallyDrop::into_inner(allocator)
}

// Try to get an allocator from the pool
{
let maybe_allocator = self.allocators.lock().unwrap().pop();
if let Some(allocator) = maybe_allocator {
return into_allocator(allocator);
return allocator.into_inner();
}
}

// Pool is empty. Try to create a new allocator.
if let Some(Ok(allocator)) = self.create_new_allocator() {
return into_allocator(allocator);
return allocator.into_inner();
}

// Pool cannot produce another allocator. Wait for an existing allocator to be returned to the pool.
loop {
let mut maybe_allocator = self.available.wait(self.allocators.lock().unwrap()).unwrap();
if let Some(allocator) = maybe_allocator.pop() {
return into_allocator(allocator);
return allocator.into_inner();
}
}
}
Expand Down Expand Up @@ -337,6 +332,19 @@ impl FixedSizeAllocator {
self.allocator.set_data_ptr(data_ptr);
}
}

/// Unwrap a [`FixedSizeAllocator`] into the [`Allocator`] it contains.
///
/// Caller must ensure that the returned `Allocator` is not dropped.
/// It must be wrapped in another type to ensure that it's dropped correctly.
#[inline] // Because this function is a no-op
fn into_inner(self) -> Allocator {
// SAFETY: `FixedSizeAllocator` is just a wrapper around `ManuallyDrop<Allocator>`,
// and is `#[repr(transparent)]`, so the 2 are equivalent
let allocator =
unsafe { mem::transmute::<FixedSizeAllocator, ManuallyDrop<Allocator>>(self) };
ManuallyDrop::into_inner(allocator)
}
}

impl Drop for FixedSizeAllocator {
Expand Down
Loading