Skip to content

Commit 30c731c

Browse files
committed
refactor(allocator): disable fixed size allocators on unsupported platforms
1 parent c86b147 commit 30c731c

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

crates/oxc_allocator/src/fixed_size.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,8 @@ use std::{
77

88
use crate::Allocator;
99

10-
// Only 64-bit little-endian platforms are supported at present
11-
const IS_SUPPORTED_PLATFORM: bool =
12-
cfg!(all(target_pointer_width = "64", target_endian = "little"));
13-
1410
const TWO_GIB: usize = 1 << 31;
15-
// `1 << 32`.
16-
// We use `IS_SUPPORTED_PLATFORM as usize * 32` to avoid compilation failure on 32-bit platforms.
17-
const FOUR_GIB: usize = 1 << (IS_SUPPORTED_PLATFORM as usize * 32);
11+
const FOUR_GIB: usize = 1 << 32;
1812

1913
// What we ideally want is an allocation 2 GiB in size, aligned on 4 GiB.
2014
// But system allocator on Mac OS refuses allocations with 4 GiB alignment.
@@ -61,15 +55,7 @@ pub struct FixedSizeAllocator {
6155

6256
impl FixedSizeAllocator {
6357
/// Create a new [`FixedSizeAllocator`].
64-
///
65-
/// # Panics
66-
/// Panics if not a 64-bit little-endian platform.
6758
pub fn new() -> Self {
68-
assert!(
69-
IS_SUPPORTED_PLATFORM,
70-
"Fixed size allocators are only supported on 64-bit little-endian platforms"
71-
);
72-
7359
// Allocate block of memory.
7460
// SAFETY: Layout does not have zero size.
7561
let alloc_ptr = unsafe { System.alloc(ALLOC_LAYOUT) };

crates/oxc_allocator/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
//!
2222
//! * `fixed_size` - Makes [`AllocatorPool`] create large fixed-size allocators, instead of
2323
//! flexibly-sized ones.
24+
//! Only supported on 64-bit little-endian platforms at present.
2425
//! Usage of this feature is not advisable, and it will be removed as soon as we're able to.
2526
//!
2627
//! * `disable_fixed_size` - Disables `fixed_size` feature.
@@ -36,7 +37,13 @@ mod allocator_api2;
3637
mod boxed;
3738
mod clone_in;
3839
mod convert;
39-
#[cfg(all(feature = "fixed_size", not(feature = "disable_fixed_size")))]
40+
// Fixed size allocators are only supported on 64-bit little-endian platforms at present
41+
#[cfg(all(
42+
feature = "fixed_size",
43+
not(feature = "disable_fixed_size"),
44+
target_pointer_width = "64",
45+
target_endian = "little"
46+
))]
4047
mod fixed_size;
4148
#[cfg(feature = "from_raw_parts")]
4249
mod from_raw_parts;

crates/oxc_allocator/src/pool.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,12 @@ impl Drop for AllocatorGuard<'_> {
7373
}
7474
}
7575

76-
#[cfg(any(not(feature = "fixed_size"), feature = "disable_fixed_size"))]
76+
#[cfg(not(all(
77+
feature = "fixed_size",
78+
not(feature = "disable_fixed_size"),
79+
target_pointer_width = "64",
80+
target_endian = "little"
81+
)))]
7782
mod wrapper {
7883
use crate::Allocator;
7984

@@ -105,7 +110,12 @@ mod wrapper {
105110
}
106111
}
107112

108-
#[cfg(all(feature = "fixed_size", not(feature = "disable_fixed_size")))]
113+
#[cfg(all(
114+
feature = "fixed_size",
115+
not(feature = "disable_fixed_size"),
116+
target_pointer_width = "64",
117+
target_endian = "little"
118+
))]
109119
mod wrapper {
110120
use crate::{
111121
Allocator,

0 commit comments

Comments
 (0)