Skip to content

Commit cbef849

Browse files
committed
refactor(allocator/pool): move AllocatorPool into own directory
1 parent babbaca commit cbef849

File tree

6 files changed

+85
-120
lines changed

6 files changed

+85
-120
lines changed

.github/generated/ast_changes_watch_list.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ src:
55
- '.github/generated/ast_changes_watch_list.yml'
66
- 'crates/oxc_allocator/src/generated/assert_layouts.rs'
77
- 'crates/oxc_allocator/src/generated/fixed_size_constants.rs'
8-
- 'crates/oxc_allocator/src/pool_fixed_size.rs'
8+
- 'crates/oxc_allocator/src/pool/fixed_size.rs'
99
- 'crates/oxc_ast/src/ast/comment.rs'
1010
- 'crates/oxc_ast/src/ast/js.rs'
1111
- 'crates/oxc_ast/src/ast/jsx.rs'

crates/oxc_allocator/src/lib.rs

Lines changed: 7 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ mod convert;
5353
#[cfg(feature = "from_raw_parts")]
5454
mod from_raw_parts;
5555
pub mod hash_map;
56+
#[cfg(feature = "pool")]
57+
mod pool;
5658
mod string_builder;
5759
mod take_in;
5860
#[cfg(all(feature = "track_allocations", not(feature = "disable_track_allocations")))]
@@ -69,113 +71,16 @@ pub use boxed::Box;
6971
pub use clone_in::CloneIn;
7072
pub use convert::{FromIn, IntoIn};
7173
pub use hash_map::HashMap;
74+
#[cfg(feature = "pool")]
75+
pub use pool::*;
7276
pub use string_builder::StringBuilder;
7377
pub use take_in::{Dummy, TakeIn};
7478
pub use vec::Vec;
7579

76-
// Fixed size allocators are only supported on 64-bit little-endian platforms at present
77-
78-
#[cfg(all(
79-
feature = "pool",
80-
not(all(
81-
feature = "fixed_size",
82-
not(feature = "disable_fixed_size"),
83-
target_pointer_width = "64",
84-
target_endian = "little"
85-
))
86-
))]
87-
mod pool;
88-
89-
#[cfg(all(
90-
feature = "pool",
91-
feature = "fixed_size",
92-
not(feature = "disable_fixed_size"),
93-
target_pointer_width = "64",
94-
target_endian = "little"
95-
))]
96-
mod pool_fixed_size;
97-
#[cfg(all(
98-
feature = "pool",
99-
feature = "fixed_size",
100-
not(feature = "disable_fixed_size"),
101-
target_pointer_width = "64",
102-
target_endian = "little"
103-
))]
104-
use pool_fixed_size as pool;
105-
// Import here so `generated/assert_layouts.rs` can access it.
106-
// Add `debug_assertions` here because `assert_layouts` is only loaded in debug mode,
107-
// so this is required to avoid unused vars lint warning in release mode.
108-
#[cfg(all(
109-
debug_assertions,
110-
feature = "pool",
111-
feature = "fixed_size",
112-
not(feature = "disable_fixed_size"),
113-
target_pointer_width = "64",
114-
target_endian = "little"
115-
))]
116-
use pool_fixed_size::FixedSizeAllocatorMetadata;
117-
// Export so can be used in `napi/oxlint2`
80+
// Fixed size allocators are only supported on 64-bit little-endian platforms at present.
81+
//
82+
// Note: Importing the `fixed_size_constants` module would cause a compilation error on 32-bit systems.
11883
#[cfg(all(
119-
feature = "pool",
120-
feature = "fixed_size",
121-
not(feature = "disable_fixed_size"),
122-
target_pointer_width = "64",
123-
target_endian = "little"
124-
))]
125-
pub use pool_fixed_size::free_fixed_size_allocator;
126-
127-
#[cfg(feature = "pool")]
128-
pub use pool::{AllocatorGuard, AllocatorPool};
129-
130-
// Dummy implementations of interfaces from `pool_fixed_size`, just to stop clippy complaining.
131-
// Seems to be necessary due to feature unification.
132-
#[cfg(all(
133-
feature = "pool",
134-
not(all(
135-
feature = "fixed_size",
136-
not(feature = "disable_fixed_size"),
137-
target_pointer_width = "64",
138-
target_endian = "little"
139-
))
140-
))]
141-
#[allow(missing_docs, clippy::missing_safety_doc, clippy::unused_self, clippy::allow_attributes)]
142-
mod dummies {
143-
use std::{ptr::NonNull, sync::atomic::AtomicBool};
144-
145-
use super::Allocator;
146-
147-
#[doc(hidden)]
148-
pub struct FixedSizeAllocatorMetadata {
149-
pub id: u32,
150-
pub alloc_ptr: NonNull<u8>,
151-
pub is_double_owned: AtomicBool,
152-
}
153-
154-
#[doc(hidden)]
155-
pub unsafe fn free_fixed_size_allocator(_metadata_ptr: NonNull<FixedSizeAllocatorMetadata>) {
156-
unreachable!();
157-
}
158-
159-
#[doc(hidden)]
160-
impl Allocator {
161-
pub unsafe fn fixed_size_metadata_ptr(&self) -> NonNull<FixedSizeAllocatorMetadata> {
162-
unreachable!();
163-
}
164-
}
165-
}
166-
#[cfg(all(
167-
feature = "pool",
168-
not(all(
169-
feature = "fixed_size",
170-
not(feature = "disable_fixed_size"),
171-
target_pointer_width = "64",
172-
target_endian = "little"
173-
))
174-
))]
175-
pub use dummies::*;
176-
177-
#[cfg(all(
178-
feature = "pool",
17984
feature = "fixed_size",
18085
not(feature = "disable_fixed_size"),
18186
target_pointer_width = "64",
@@ -186,11 +91,3 @@ mod generated {
18691
mod assert_layouts;
18792
pub mod fixed_size_constants;
18893
}
189-
#[cfg(all(
190-
feature = "pool",
191-
feature = "fixed_size",
192-
not(feature = "disable_fixed_size"),
193-
target_pointer_width = "64",
194-
target_endian = "little"
195-
))]
196-
use generated::fixed_size_constants;

crates/oxc_allocator/src/pool_fixed_size.rs renamed to crates/oxc_allocator/src/pool/fixed_size.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use oxc_ast_macros::ast;
1313

1414
use crate::{
1515
Allocator,
16-
fixed_size_constants::{BLOCK_ALIGN, BLOCK_SIZE, RAW_METADATA_SIZE},
16+
generated::fixed_size_constants::{BLOCK_ALIGN, BLOCK_SIZE, RAW_METADATA_SIZE},
1717
};
1818

1919
const TWO_GIB: usize = 1 << 31;
@@ -103,16 +103,16 @@ impl Drop for AllocatorGuard<'_> {
103103
}
104104
}
105105

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

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

206206
impl FixedSizeAllocator {
207207
/// Create a new [`FixedSizeAllocator`].
208208
#[expect(clippy::items_after_statements)]
209-
pub fn new(id: u32) -> Self {
209+
fn new(id: u32) -> Self {
210210
// Only support little-endian systems. `Allocator::from_raw_parts` includes this same assertion.
211211
// This module is only compiled on 64-bit little-endian systems, so it should be impossible for
212212
// this panic to occur. But we want to make absolutely sure that if there's a mistake elsewhere,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Fixed size allocators are only supported on 64-bit little-endian platforms at present.
2+
// They are only enabled if `fixed_size` feature enabled, and `disable_fixed_size` feature is not enabled.
3+
//
4+
// Note: Importing the `fixed_size` module would cause a compilation error on 32-bit systems.
5+
#[cfg(all(
6+
feature = "fixed_size",
7+
not(feature = "disable_fixed_size"),
8+
target_pointer_width = "64",
9+
target_endian = "little"
10+
))]
11+
mod fixed_size;
12+
#[cfg(all(
13+
feature = "fixed_size",
14+
not(feature = "disable_fixed_size"),
15+
target_pointer_width = "64",
16+
target_endian = "little"
17+
))]
18+
pub use fixed_size::*;
19+
20+
#[cfg(not(all(
21+
feature = "fixed_size",
22+
not(feature = "disable_fixed_size"),
23+
target_pointer_width = "64",
24+
target_endian = "little"
25+
)))]
26+
mod standard;
27+
#[cfg(not(all(
28+
feature = "fixed_size",
29+
not(feature = "disable_fixed_size"),
30+
target_pointer_width = "64",
31+
target_endian = "little"
32+
)))]
33+
pub use standard::*;

crates/oxc_allocator/src/pool.rs renamed to crates/oxc_allocator/src/pool/standard.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,38 @@ impl Drop for AllocatorGuard<'_> {
7171
self.pool.add(allocator);
7272
}
7373
}
74+
75+
// Dummy implementations of interfaces from `fixed_size`, just to stop clippy complaining.
76+
// Seems to be necessary due to feature unification.
77+
#[allow(
78+
dead_code,
79+
missing_docs,
80+
clippy::missing_safety_doc,
81+
clippy::unused_self,
82+
clippy::allow_attributes
83+
)]
84+
mod dummies {
85+
use std::{ptr::NonNull, sync::atomic::AtomicBool};
86+
87+
use crate::Allocator;
88+
89+
#[doc(hidden)]
90+
pub struct FixedSizeAllocatorMetadata {
91+
pub id: u32,
92+
pub(crate) alloc_ptr: NonNull<u8>,
93+
pub is_double_owned: AtomicBool,
94+
}
95+
96+
#[doc(hidden)]
97+
pub unsafe fn free_fixed_size_allocator(_metadata_ptr: NonNull<FixedSizeAllocatorMetadata>) {
98+
unreachable!();
99+
}
100+
101+
#[doc(hidden)]
102+
impl Allocator {
103+
pub unsafe fn fixed_size_metadata_ptr(&self) -> NonNull<FixedSizeAllocatorMetadata> {
104+
unreachable!();
105+
}
106+
}
107+
}
108+
pub use dummies::*;

tasks/ast_tools/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ use utils::create_ident;
207207

208208
/// Paths to source files containing AST types
209209
static SOURCE_PATHS: &[&str] = &[
210-
"crates/oxc_allocator/src/pool_fixed_size.rs",
210+
"crates/oxc_allocator/src/pool/fixed_size.rs",
211211
"crates/oxc_ast/src/ast/js.rs",
212212
"crates/oxc_ast/src/ast/literal.rs",
213213
"crates/oxc_ast/src/ast/jsx.rs",

0 commit comments

Comments
 (0)