feat(ast): implement allocator_api2 for Allocator (#8043)

This commit is contained in:
Boshen 2024-12-20 13:26:48 +00:00
parent b3f38aeab9
commit 8547e02456
5 changed files with 52 additions and 6 deletions

1
Cargo.lock generated
View file

@ -1926,7 +1926,6 @@ name = "oxc_semantic"
version = "0.42.0"
dependencies = [
"assert-unchecked",
"bumpalo",
"hashbrown 0.15.2",
"insta",
"itertools",

View file

@ -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)
}
}

View file

@ -48,6 +48,7 @@ pub use bumpalo::collections::String;
use bumpalo::Bump;
mod address;
mod allocator_api2;
mod boxed;
mod clone_in;
mod convert;

View file

@ -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"] }

View file

@ -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::{
@ -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
@ -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(),
}),
}
@ -63,7 +63,7 @@ impl Default for ScopeTree {
self_cell::self_cell!(
pub(crate) struct ScopeTreeCell {
owner: Bump,
owner: Allocator,
#[covariant]
dependent: ScopeTreeInner,
}