diff --git a/crates/oxc_allocator/src/alloc.rs b/crates/oxc_allocator/src/alloc.rs index 3a817db0d03b2..138f0d1500959 100644 --- a/crates/oxc_allocator/src/alloc.rs +++ b/crates/oxc_allocator/src/alloc.rs @@ -100,16 +100,10 @@ impl Alloc for Bump { // This will go away when we add a custom allocator to oxc. #[cfg(all(feature = "track_allocations", not(feature = "disable_track_allocations")))] unsafe { - use crate::Allocator; - use std::{ - mem::offset_of, - ptr, - sync::atomic::{AtomicUsize, Ordering}, - }; - #[expect(clippy::cast_possible_wrap)] - const OFFSET: isize = (offset_of!(Allocator, num_alloc) as isize) - - (offset_of!(Allocator, bump) as isize); - let num_alloc_ptr = ptr::from_ref(self).byte_offset(OFFSET).cast::(); + use crate::allocator::NUM_ALLOC_FIELD_OFFSET; + use std::sync::atomic::{AtomicUsize, Ordering}; + let num_alloc_ptr = + std::ptr::from_ref(self).byte_offset(NUM_ALLOC_FIELD_OFFSET).cast::(); let num_alloc = num_alloc_ptr.as_ref().unwrap_unchecked(); num_alloc.fetch_add(1, Ordering::SeqCst); } @@ -157,16 +151,11 @@ impl Alloc for Bump { // This will go away when we add a custom allocator to oxc. #[cfg(all(feature = "track_allocations", not(feature = "disable_track_allocations")))] unsafe { - use crate::Allocator; - use std::{ - mem::offset_of, - ptr, - sync::atomic::{AtomicUsize, Ordering}, - }; - #[expect(clippy::cast_possible_wrap)] - const OFFSET: isize = (offset_of!(Allocator, num_realloc) as isize) - - (offset_of!(Allocator, bump) as isize); - let num_realloc_ptr = ptr::from_ref(self).byte_offset(OFFSET).cast::(); + use crate::allocator::NUM_REALLOC_FIELD_OFFSET; + use std::sync::atomic::{AtomicUsize, Ordering}; + let num_realloc_ptr = std::ptr::from_ref(self) + .byte_offset(NUM_REALLOC_FIELD_OFFSET) + .cast::(); let num_realloc = num_realloc_ptr.as_ref().unwrap_unchecked(); num_realloc.fetch_add(1, Ordering::SeqCst); } diff --git a/crates/oxc_allocator/src/allocator.rs b/crates/oxc_allocator/src/allocator.rs index 156d6cf949948..50f43ced9de33 100644 --- a/crates/oxc_allocator/src/allocator.rs +++ b/crates/oxc_allocator/src/allocator.rs @@ -4,6 +4,9 @@ use std::{ slice, str, }; +#[cfg(all(feature = "track_allocations", not(feature = "disable_track_allocations")))] +use std::mem::offset_of; + use bumpalo::Bump; use oxc_data_structures::assert_unchecked; @@ -214,12 +217,7 @@ use oxc_data_structures::assert_unchecked; /// [`HashMap::new_in`]: crate::HashMap::new_in #[derive(Default)] pub struct Allocator { - #[cfg(not(all(feature = "track_allocations", not(feature = "disable_track_allocations"))))] bump: Bump, - // NOTE: We need to expose `bump` publicly here for calculating its field offset in memory. - #[cfg(all(feature = "track_allocations", not(feature = "disable_track_allocations")))] - #[doc(hidden)] - pub bump: Bump, /// Used to track the total number of allocations made in this allocator when the `track_allocations` feature is enabled. #[cfg(all(feature = "track_allocations", not(feature = "disable_track_allocations")))] #[doc(hidden)] @@ -230,6 +228,16 @@ pub struct Allocator { pub num_realloc: std::sync::atomic::AtomicUsize, } +// Consts used in `Alloc` trait for allocation tracking +#[cfg(all(feature = "track_allocations", not(feature = "disable_track_allocations")))] +#[expect(clippy::cast_possible_wrap)] +pub const NUM_ALLOC_FIELD_OFFSET: isize = + (offset_of!(Allocator, num_alloc) as isize) - (offset_of!(Allocator, bump) as isize); +#[cfg(all(feature = "track_allocations", not(feature = "disable_track_allocations")))] +#[expect(clippy::cast_possible_wrap)] +pub const NUM_REALLOC_FIELD_OFFSET: isize = + (offset_of!(Allocator, num_realloc) as isize) - (offset_of!(Allocator, bump) as isize); + impl Allocator { /// Create a new [`Allocator`] with no initial capacity. ///