From f130a0c6b0f1f1d43a812872c08a45cda35a8cd1 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Mon, 14 Jul 2025 18:21:24 +0000 Subject: [PATCH] refactor(allocator): disable fixed size allocators on unsupported platforms (#12272) Do not compile code for fixed-sized allocators (for raw transfer) on unsupported systems (32 bit, or big-endian). This allows removing the workaround to prevent compilation failure on 32-bit systems due to `1 << 32` overflowing `usize`. This code is no longer compiled on 32-bit systems. --- crates/oxc_allocator/src/fixed_size.rs | 16 +--------------- crates/oxc_allocator/src/lib.rs | 9 ++++++++- crates/oxc_allocator/src/pool.rs | 14 ++++++++++++-- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/crates/oxc_allocator/src/fixed_size.rs b/crates/oxc_allocator/src/fixed_size.rs index 6fa043e6968ee..587e7315bd264 100644 --- a/crates/oxc_allocator/src/fixed_size.rs +++ b/crates/oxc_allocator/src/fixed_size.rs @@ -7,14 +7,8 @@ use std::{ use crate::Allocator; -// Only 64-bit little-endian platforms are supported at present -const IS_SUPPORTED_PLATFORM: bool = - cfg!(all(target_pointer_width = "64", target_endian = "little")); - const TWO_GIB: usize = 1 << 31; -// `1 << 32`. -// We use `IS_SUPPORTED_PLATFORM as usize * 32` to avoid compilation failure on 32-bit platforms. -const FOUR_GIB: usize = 1 << (IS_SUPPORTED_PLATFORM as usize * 32); +const FOUR_GIB: usize = 1 << 32; // What we ideally want is an allocation 2 GiB in size, aligned on 4 GiB. // But system allocator on Mac OS refuses allocations with 4 GiB alignment. @@ -61,15 +55,7 @@ pub struct FixedSizeAllocator { impl FixedSizeAllocator { /// Create a new [`FixedSizeAllocator`]. - /// - /// # Panics - /// Panics if not a 64-bit little-endian platform. pub fn new() -> Self { - assert!( - IS_SUPPORTED_PLATFORM, - "Fixed size allocators are only supported on 64-bit little-endian platforms" - ); - // Allocate block of memory. // SAFETY: Layout does not have zero size. let alloc_ptr = unsafe { System.alloc(ALLOC_LAYOUT) }; diff --git a/crates/oxc_allocator/src/lib.rs b/crates/oxc_allocator/src/lib.rs index b79cf06be2d15..ae96bbc0fd894 100644 --- a/crates/oxc_allocator/src/lib.rs +++ b/crates/oxc_allocator/src/lib.rs @@ -21,6 +21,7 @@ //! //! * `fixed_size` - Makes [`AllocatorPool`] create large fixed-size allocators, instead of //! flexibly-sized ones. +//! Only supported on 64-bit little-endian platforms at present. //! Usage of this feature is not advisable, and it will be removed as soon as we're able to. //! //! * `disable_fixed_size` - Disables `fixed_size` feature. @@ -36,7 +37,13 @@ mod allocator_api2; mod boxed; mod clone_in; mod convert; -#[cfg(all(feature = "fixed_size", not(feature = "disable_fixed_size")))] +// Fixed size allocators are only supported on 64-bit little-endian platforms at present +#[cfg(all( + feature = "fixed_size", + not(feature = "disable_fixed_size"), + target_pointer_width = "64", + target_endian = "little" +))] mod fixed_size; #[cfg(feature = "from_raw_parts")] mod from_raw_parts; diff --git a/crates/oxc_allocator/src/pool.rs b/crates/oxc_allocator/src/pool.rs index a8fb135914ad7..88d9e5461dfef 100644 --- a/crates/oxc_allocator/src/pool.rs +++ b/crates/oxc_allocator/src/pool.rs @@ -73,7 +73,12 @@ impl Drop for AllocatorGuard<'_> { } } -#[cfg(any(not(feature = "fixed_size"), feature = "disable_fixed_size"))] +#[cfg(not(all( + feature = "fixed_size", + not(feature = "disable_fixed_size"), + target_pointer_width = "64", + target_endian = "little" +)))] mod wrapper { use crate::Allocator; @@ -105,7 +110,12 @@ mod wrapper { } } -#[cfg(all(feature = "fixed_size", not(feature = "disable_fixed_size")))] +#[cfg(all( + feature = "fixed_size", + not(feature = "disable_fixed_size"), + target_pointer_width = "64", + target_endian = "little" +))] mod wrapper { use crate::{ Allocator,