Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .github/generated/ast_changes_watch_list.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ src:
- '.github/generated/ast_changes_watch_list.yml'
- 'crates/oxc_allocator/src/generated/assert_layouts.rs'
- 'crates/oxc_allocator/src/generated/fixed_size_constants.rs'
- 'crates/oxc_allocator/src/pool_fixed_size.rs'
- 'crates/oxc_allocator/src/pool/fixed_size.rs'
- 'crates/oxc_ast/src/ast/comment.rs'
- 'crates/oxc_ast/src/ast/js.rs'
- 'crates/oxc_ast/src/ast/jsx.rs'
Expand Down
117 changes: 7 additions & 110 deletions crates/oxc_allocator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ mod convert;
#[cfg(feature = "from_raw_parts")]
mod from_raw_parts;
pub mod hash_map;
#[cfg(feature = "pool")]
mod pool;
mod string_builder;
mod take_in;
#[cfg(all(feature = "track_allocations", not(feature = "disable_track_allocations")))]
Expand All @@ -69,113 +71,16 @@ pub use boxed::Box;
pub use clone_in::CloneIn;
pub use convert::{FromIn, IntoIn};
pub use hash_map::HashMap;
#[cfg(feature = "pool")]
pub use pool::*;
pub use string_builder::StringBuilder;
pub use take_in::{Dummy, TakeIn};
pub use vec::Vec;

// Fixed size allocators are only supported on 64-bit little-endian platforms at present

#[cfg(all(
feature = "pool",
not(all(
feature = "fixed_size",
not(feature = "disable_fixed_size"),
target_pointer_width = "64",
target_endian = "little"
))
))]
mod pool;

#[cfg(all(
feature = "pool",
feature = "fixed_size",
not(feature = "disable_fixed_size"),
target_pointer_width = "64",
target_endian = "little"
))]
mod pool_fixed_size;
#[cfg(all(
feature = "pool",
feature = "fixed_size",
not(feature = "disable_fixed_size"),
target_pointer_width = "64",
target_endian = "little"
))]
use pool_fixed_size as pool;
// Import here so `generated/assert_layouts.rs` can access it.
// Add `debug_assertions` here because `assert_layouts` is only loaded in debug mode,
// so this is required to avoid unused vars lint warning in release mode.
#[cfg(all(
debug_assertions,
feature = "pool",
feature = "fixed_size",
not(feature = "disable_fixed_size"),
target_pointer_width = "64",
target_endian = "little"
))]
use pool_fixed_size::FixedSizeAllocatorMetadata;
// Export so can be used in `napi/oxlint2`
// Fixed size allocators are only supported on 64-bit little-endian platforms at present.
//
// Note: Importing the `fixed_size_constants` module would cause a compilation error on 32-bit systems.
#[cfg(all(
feature = "pool",
feature = "fixed_size",
not(feature = "disable_fixed_size"),
target_pointer_width = "64",
target_endian = "little"
))]
pub use pool_fixed_size::free_fixed_size_allocator;

#[cfg(feature = "pool")]
pub use pool::{AllocatorGuard, AllocatorPool};

// Dummy implementations of interfaces from `pool_fixed_size`, just to stop clippy complaining.
// Seems to be necessary due to feature unification.
#[cfg(all(
feature = "pool",
not(all(
feature = "fixed_size",
not(feature = "disable_fixed_size"),
target_pointer_width = "64",
target_endian = "little"
))
))]
#[allow(missing_docs, clippy::missing_safety_doc, clippy::unused_self, clippy::allow_attributes)]
mod dummies {
use std::{ptr::NonNull, sync::atomic::AtomicBool};

use super::Allocator;

#[doc(hidden)]
pub struct FixedSizeAllocatorMetadata {
pub id: u32,
pub alloc_ptr: NonNull<u8>,
pub is_double_owned: AtomicBool,
}

#[doc(hidden)]
pub unsafe fn free_fixed_size_allocator(_metadata_ptr: NonNull<FixedSizeAllocatorMetadata>) {
unreachable!();
}

#[doc(hidden)]
impl Allocator {
pub unsafe fn fixed_size_metadata_ptr(&self) -> NonNull<FixedSizeAllocatorMetadata> {
unreachable!();
}
}
}
#[cfg(all(
feature = "pool",
not(all(
feature = "fixed_size",
not(feature = "disable_fixed_size"),
target_pointer_width = "64",
target_endian = "little"
))
))]
pub use dummies::*;

#[cfg(all(
feature = "pool",
feature = "fixed_size",
not(feature = "disable_fixed_size"),
target_pointer_width = "64",
Expand All @@ -186,11 +91,3 @@ mod generated {
mod assert_layouts;
pub mod fixed_size_constants;
}
#[cfg(all(
feature = "pool",
feature = "fixed_size",
not(feature = "disable_fixed_size"),
target_pointer_width = "64",
target_endian = "little"
))]
use generated::fixed_size_constants;
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use oxc_ast_macros::ast;

use crate::{
Allocator,
fixed_size_constants::{BLOCK_ALIGN, BLOCK_SIZE, RAW_METADATA_SIZE},
generated::fixed_size_constants::{BLOCK_ALIGN, BLOCK_SIZE, RAW_METADATA_SIZE},
};

const TWO_GIB: usize = 1 << 31;
Expand Down Expand Up @@ -103,16 +103,16 @@ impl Drop for AllocatorGuard<'_> {
}
}

/// Metadata about a [`FixedSizeAllocator`].
/// Metadata about a `FixedSizeAllocator`.
///
/// Is stored in the memory backing the [`FixedSizeAllocator`], after `RawTransferMetadata`,
/// which is after the section of the allocation which [`Allocator`] uses for its chunk.
/// Is stored in the memory backing the `FixedSizeAllocator`, after `RawTransferMetadata`,
/// which is after the section of the allocation which `Allocator` uses for its chunk.
#[ast]
pub struct FixedSizeAllocatorMetadata {
/// ID of this allocator
pub id: u32,
/// Pointer to start of original allocation backing the `FixedSizeAllocator`
pub alloc_ptr: NonNull<u8>,
pub(crate) alloc_ptr: NonNull<u8>,
/// `true` if both Rust and JS currently hold references to this `FixedSizeAllocator`.
///
/// * `false` initially.
Expand Down Expand Up @@ -143,7 +143,7 @@ const ALLOC_SIZE: usize = BLOCK_SIZE + TWO_GIB;
const ALLOC_ALIGN: usize = TWO_GIB;

/// Layout of backing allocations for fixed-size allocators.
pub const ALLOC_LAYOUT: Layout = match Layout::from_size_align(ALLOC_SIZE, ALLOC_ALIGN) {
const ALLOC_LAYOUT: Layout = match Layout::from_size_align(ALLOC_SIZE, ALLOC_ALIGN) {
Ok(layout) => layout,
Err(_) => unreachable!(),
};
Expand Down Expand Up @@ -198,15 +198,15 @@ pub const ALLOC_LAYOUT: Layout = match Layout::from_size_align(ALLOC_SIZE, ALLOC
/// * `BLOCK_SIZE` is a multiple of 16.
/// * `RawTransferMetadata` is 16 bytes.
/// * Size of `FixedSizeAllocatorMetadata` is rounded up to a multiple of 16.
pub struct FixedSizeAllocator {
struct FixedSizeAllocator {
/// `Allocator` which utilizes part of the original allocation
allocator: ManuallyDrop<Allocator>,
}

impl FixedSizeAllocator {
/// Create a new [`FixedSizeAllocator`].
#[expect(clippy::items_after_statements)]
pub fn new(id: u32) -> Self {
fn new(id: u32) -> Self {
// Only support little-endian systems. `Allocator::from_raw_parts` includes this same assertion.
// This module is only compiled on 64-bit little-endian systems, so it should be impossible for
// this panic to occur. But we want to make absolutely sure that if there's a mistake elsewhere,
Expand Down
33 changes: 33 additions & 0 deletions crates/oxc_allocator/src/pool/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Fixed size allocators are only supported on 64-bit little-endian platforms at present.
// They are only enabled if `fixed_size` feature enabled, and `disable_fixed_size` feature is not enabled.
//
// Note: Importing the `fixed_size` module would cause a compilation error on 32-bit systems.
#[cfg(all(
feature = "fixed_size",
not(feature = "disable_fixed_size"),
target_pointer_width = "64",
target_endian = "little"
))]
mod fixed_size;
#[cfg(all(
feature = "fixed_size",
not(feature = "disable_fixed_size"),
target_pointer_width = "64",
target_endian = "little"
))]
pub use fixed_size::*;

#[cfg(not(all(
feature = "fixed_size",
not(feature = "disable_fixed_size"),
target_pointer_width = "64",
target_endian = "little"
)))]
mod standard;
#[cfg(not(all(
feature = "fixed_size",
not(feature = "disable_fixed_size"),
target_pointer_width = "64",
target_endian = "little"
)))]
pub use standard::*;
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,38 @@ impl Drop for AllocatorGuard<'_> {
self.pool.add(allocator);
}
}

// Dummy implementations of interfaces from `fixed_size`, just to stop clippy complaining.
// Seems to be necessary due to feature unification.
#[allow(
dead_code,
missing_docs,
clippy::missing_safety_doc,
clippy::unused_self,
clippy::allow_attributes
)]
mod dummies {
use std::{ptr::NonNull, sync::atomic::AtomicBool};

use crate::Allocator;

#[doc(hidden)]
pub struct FixedSizeAllocatorMetadata {
pub id: u32,
pub(crate) alloc_ptr: NonNull<u8>,
pub is_double_owned: AtomicBool,
}

#[doc(hidden)]
pub unsafe fn free_fixed_size_allocator(_metadata_ptr: NonNull<FixedSizeAllocatorMetadata>) {
unreachable!();
}

#[doc(hidden)]
impl Allocator {
pub unsafe fn fixed_size_metadata_ptr(&self) -> NonNull<FixedSizeAllocatorMetadata> {
unreachable!();
}
}
}
pub use dummies::*;
2 changes: 1 addition & 1 deletion tasks/ast_tools/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ use utils::create_ident;

/// Paths to source files containing AST types
static SOURCE_PATHS: &[&str] = &[
"crates/oxc_allocator/src/pool_fixed_size.rs",
"crates/oxc_allocator/src/pool/fixed_size.rs",
"crates/oxc_ast/src/ast/js.rs",
"crates/oxc_ast/src/ast/literal.rs",
"crates/oxc_ast/src/ast/jsx.rs",
Expand Down
Loading