Skip to content

Commit

Permalink
feat(ast): implement allocator_api2 for Allocator (#8043)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 20, 2024
1 parent b3f38ae commit 8547e02
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions crates/oxc_allocator/src/allocator_api2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::{alloc::Layout, ptr::NonNull};

use allocator_api2::alloc::{AllocError, Allocator};

/// SAFETY:
/// <https://github.com/fitzgen/bumpalo/blob/4eeab8847c85d5cde135ca21ae14a54e56b05224/src/lib.rs#L1938>
unsafe impl Allocator for &crate::Allocator {
#[inline]
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
(&self.bump).allocate(layout)
}

#[inline]
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
(&self.bump).deallocate(ptr, layout);
}

#[inline]
unsafe fn shrink(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
(&self.bump).shrink(ptr, old_layout, new_layout)
}

#[inline]
unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
(&self.bump).grow(ptr, old_layout, new_layout)
}

#[inline]
unsafe fn grow_zeroed(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
(&self.bump).grow_zeroed(ptr, old_layout, new_layout)
}
}
1 change: 1 addition & 0 deletions crates/oxc_allocator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub use bumpalo::collections::String;
use bumpalo::Bump;

mod address;
mod allocator_api2;
mod boxed;
mod clone_in;
mod convert;
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_semantic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ oxc_span = { workspace = true }
oxc_syntax = { workspace = true }

assert-unchecked = { workspace = true }
bumpalo = { workspace = true, features = ["allocator-api2"] }
hashbrown = { workspace = true, features = ["allocator-api2"] }
itertools = { workspace = true }
phf = { workspace = true, features = ["macros"] }
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_semantic/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::mem;

use rustc_hash::{FxBuildHasher, FxHashMap};

use bumpalo::Bump;
use oxc_allocator::Allocator;
use oxc_index::IndexVec;
use oxc_span::CompactStr;
use oxc_syntax::{
Expand All @@ -12,7 +12,7 @@ use oxc_syntax::{
symbol::SymbolId,
};

pub(crate) type Bindings<'a> = hashbrown::HashMap<&'a str, SymbolId, FxBuildHasher, &'a Bump>;
pub(crate) type Bindings<'a> = hashbrown::HashMap<&'a str, SymbolId, FxBuildHasher, &'a Allocator>;
pub type UnresolvedReferences = FxHashMap<CompactStr, Vec<ReferenceId>>;

/// Scope Tree
Expand Down Expand Up @@ -54,7 +54,7 @@ impl Default for ScopeTree {
node_ids: IndexVec::new(),
flags: IndexVec::new(),
root_unresolved_references: UnresolvedReferences::default(),
cell: ScopeTreeCell::new(Bump::new(), |_bump| ScopeTreeInner {
cell: ScopeTreeCell::new(Allocator::default(), |_bump| ScopeTreeInner {
bindings: IndexVec::new(),
}),
}
Expand All @@ -63,7 +63,7 @@ impl Default for ScopeTree {

self_cell::self_cell!(
pub(crate) struct ScopeTreeCell {
owner: Bump,
owner: Allocator,
#[covariant]
dependent: ScopeTreeInner,
}
Expand Down

0 comments on commit 8547e02

Please sign in to comment.