diff --git a/crates/oxc_allocator/src/pool/fixed_size.rs b/crates/oxc_allocator/src/pool/fixed_size.rs index dbfbbfc1e9df8..8247e4261a87e 100644 --- a/crates/oxc_allocator/src/pool/fixed_size.rs +++ b/crates/oxc_allocator/src/pool/fixed_size.rs @@ -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`, - // and is `#[repr(transparent)]`, so the 2 are equivalent. - let allocator = - unsafe { mem::transmute::>(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(); } } } @@ -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`, + // and is `#[repr(transparent)]`, so the 2 are equivalent + let allocator = + unsafe { mem::transmute::>(self) }; + ManuallyDrop::into_inner(allocator) + } } impl Drop for FixedSizeAllocator {