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
29 changes: 9 additions & 20 deletions crates/oxc_allocator/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<AtomicUsize>();
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::<AtomicUsize>();
let num_alloc = num_alloc_ptr.as_ref().unwrap_unchecked();
num_alloc.fetch_add(1, Ordering::SeqCst);
}
Expand Down Expand Up @@ -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::<AtomicUsize>();
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::<AtomicUsize>();
let num_realloc = num_realloc_ptr.as_ref().unwrap_unchecked();
num_realloc.fetch_add(1, Ordering::SeqCst);
}
Expand Down
18 changes: 13 additions & 5 deletions crates/oxc_allocator/src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)]
Expand All @@ -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.
///
Expand Down
Loading