Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ast): implement allocator_api2 for Allocator #8043

Merged
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
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
Loading