Skip to content

Commit ff1d93b

Browse files
committed
refactor(allocator): do not make Allocator::bump field public (#12936)
Follow-on after #12555. Avoid making `bump` field of `Allocator` public when `track_allocations` feature is enabled, by moving the field offset calculations to next to `Allocator`'s definition.
1 parent 0039523 commit ff1d93b

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

crates/oxc_allocator/src/alloc.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,10 @@ impl Alloc for Bump {
100100
// This will go away when we add a custom allocator to oxc.
101101
#[cfg(all(feature = "track_allocations", not(feature = "disable_track_allocations")))]
102102
unsafe {
103-
use crate::Allocator;
104-
use std::{
105-
mem::offset_of,
106-
ptr,
107-
sync::atomic::{AtomicUsize, Ordering},
108-
};
109-
#[expect(clippy::cast_possible_wrap)]
110-
const OFFSET: isize = (offset_of!(Allocator, num_alloc) as isize)
111-
- (offset_of!(Allocator, bump) as isize);
112-
let num_alloc_ptr = ptr::from_ref(self).byte_offset(OFFSET).cast::<AtomicUsize>();
103+
use crate::allocator::NUM_ALLOC_FIELD_OFFSET;
104+
use std::sync::atomic::{AtomicUsize, Ordering};
105+
let num_alloc_ptr =
106+
std::ptr::from_ref(self).byte_offset(NUM_ALLOC_FIELD_OFFSET).cast::<AtomicUsize>();
113107
let num_alloc = num_alloc_ptr.as_ref().unwrap_unchecked();
114108
num_alloc.fetch_add(1, Ordering::SeqCst);
115109
}
@@ -157,16 +151,11 @@ impl Alloc for Bump {
157151
// This will go away when we add a custom allocator to oxc.
158152
#[cfg(all(feature = "track_allocations", not(feature = "disable_track_allocations")))]
159153
unsafe {
160-
use crate::Allocator;
161-
use std::{
162-
mem::offset_of,
163-
ptr,
164-
sync::atomic::{AtomicUsize, Ordering},
165-
};
166-
#[expect(clippy::cast_possible_wrap)]
167-
const OFFSET: isize = (offset_of!(Allocator, num_realloc) as isize)
168-
- (offset_of!(Allocator, bump) as isize);
169-
let num_realloc_ptr = ptr::from_ref(self).byte_offset(OFFSET).cast::<AtomicUsize>();
154+
use crate::allocator::NUM_REALLOC_FIELD_OFFSET;
155+
use std::sync::atomic::{AtomicUsize, Ordering};
156+
let num_realloc_ptr = std::ptr::from_ref(self)
157+
.byte_offset(NUM_REALLOC_FIELD_OFFSET)
158+
.cast::<AtomicUsize>();
170159
let num_realloc = num_realloc_ptr.as_ref().unwrap_unchecked();
171160
num_realloc.fetch_add(1, Ordering::SeqCst);
172161
}

crates/oxc_allocator/src/allocator.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ use std::{
44
slice, str,
55
};
66

7+
#[cfg(all(feature = "track_allocations", not(feature = "disable_track_allocations")))]
8+
use std::mem::offset_of;
9+
710
use bumpalo::Bump;
811

912
use oxc_data_structures::assert_unchecked;
@@ -214,12 +217,7 @@ use oxc_data_structures::assert_unchecked;
214217
/// [`HashMap::new_in`]: crate::HashMap::new_in
215218
#[derive(Default)]
216219
pub struct Allocator {
217-
#[cfg(not(all(feature = "track_allocations", not(feature = "disable_track_allocations"))))]
218220
bump: Bump,
219-
// NOTE: We need to expose `bump` publicly here for calculating its field offset in memory.
220-
#[cfg(all(feature = "track_allocations", not(feature = "disable_track_allocations")))]
221-
#[doc(hidden)]
222-
pub bump: Bump,
223221
/// Used to track the total number of allocations made in this allocator when the `track_allocations` feature is enabled.
224222
#[cfg(all(feature = "track_allocations", not(feature = "disable_track_allocations")))]
225223
#[doc(hidden)]
@@ -230,6 +228,16 @@ pub struct Allocator {
230228
pub num_realloc: std::sync::atomic::AtomicUsize,
231229
}
232230

231+
// Consts used in `Alloc` trait for allocation tracking
232+
#[cfg(all(feature = "track_allocations", not(feature = "disable_track_allocations")))]
233+
#[expect(clippy::cast_possible_wrap)]
234+
pub const NUM_ALLOC_FIELD_OFFSET: isize =
235+
(offset_of!(Allocator, num_alloc) as isize) - (offset_of!(Allocator, bump) as isize);
236+
#[cfg(all(feature = "track_allocations", not(feature = "disable_track_allocations")))]
237+
#[expect(clippy::cast_possible_wrap)]
238+
pub const NUM_REALLOC_FIELD_OFFSET: isize =
239+
(offset_of!(Allocator, num_realloc) as isize) - (offset_of!(Allocator, bump) as isize);
240+
233241
impl Allocator {
234242
/// Create a new [`Allocator`] with no initial capacity.
235243
///

0 commit comments

Comments
 (0)