mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(ast): implement allocator_api2 for Allocator (#8043)
This commit is contained in:
parent
b3f38aeab9
commit
8547e02456
5 changed files with 52 additions and 6 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -1926,7 +1926,6 @@ name = "oxc_semantic"
|
||||||
version = "0.42.0"
|
version = "0.42.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"assert-unchecked",
|
"assert-unchecked",
|
||||||
"bumpalo",
|
|
||||||
"hashbrown 0.15.2",
|
"hashbrown 0.15.2",
|
||||||
"insta",
|
"insta",
|
||||||
"itertools",
|
"itertools",
|
||||||
|
|
|
||||||
47
crates/oxc_allocator/src/allocator_api2.rs
Normal file
47
crates/oxc_allocator/src/allocator_api2.rs
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -48,6 +48,7 @@ pub use bumpalo::collections::String;
|
||||||
use bumpalo::Bump;
|
use bumpalo::Bump;
|
||||||
|
|
||||||
mod address;
|
mod address;
|
||||||
|
mod allocator_api2;
|
||||||
mod boxed;
|
mod boxed;
|
||||||
mod clone_in;
|
mod clone_in;
|
||||||
mod convert;
|
mod convert;
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,6 @@ oxc_span = { workspace = true }
|
||||||
oxc_syntax = { workspace = true }
|
oxc_syntax = { workspace = true }
|
||||||
|
|
||||||
assert-unchecked = { workspace = true }
|
assert-unchecked = { workspace = true }
|
||||||
bumpalo = { workspace = true, features = ["allocator-api2"] }
|
|
||||||
hashbrown = { workspace = true, features = ["allocator-api2"] }
|
hashbrown = { workspace = true, features = ["allocator-api2"] }
|
||||||
itertools = { workspace = true }
|
itertools = { workspace = true }
|
||||||
phf = { workspace = true, features = ["macros"] }
|
phf = { workspace = true, features = ["macros"] }
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use std::mem;
|
||||||
|
|
||||||
use rustc_hash::{FxBuildHasher, FxHashMap};
|
use rustc_hash::{FxBuildHasher, FxHashMap};
|
||||||
|
|
||||||
use bumpalo::Bump;
|
use oxc_allocator::Allocator;
|
||||||
use oxc_index::IndexVec;
|
use oxc_index::IndexVec;
|
||||||
use oxc_span::CompactStr;
|
use oxc_span::CompactStr;
|
||||||
use oxc_syntax::{
|
use oxc_syntax::{
|
||||||
|
|
@ -12,7 +12,7 @@ use oxc_syntax::{
|
||||||
symbol::SymbolId,
|
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>>;
|
pub type UnresolvedReferences = FxHashMap<CompactStr, Vec<ReferenceId>>;
|
||||||
|
|
||||||
/// Scope Tree
|
/// Scope Tree
|
||||||
|
|
@ -54,7 +54,7 @@ impl Default for ScopeTree {
|
||||||
node_ids: IndexVec::new(),
|
node_ids: IndexVec::new(),
|
||||||
flags: IndexVec::new(),
|
flags: IndexVec::new(),
|
||||||
root_unresolved_references: UnresolvedReferences::default(),
|
root_unresolved_references: UnresolvedReferences::default(),
|
||||||
cell: ScopeTreeCell::new(Bump::new(), |_bump| ScopeTreeInner {
|
cell: ScopeTreeCell::new(Allocator::default(), |_bump| ScopeTreeInner {
|
||||||
bindings: IndexVec::new(),
|
bindings: IndexVec::new(),
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
|
@ -63,7 +63,7 @@ impl Default for ScopeTree {
|
||||||
|
|
||||||
self_cell::self_cell!(
|
self_cell::self_cell!(
|
||||||
pub(crate) struct ScopeTreeCell {
|
pub(crate) struct ScopeTreeCell {
|
||||||
owner: Bump,
|
owner: Allocator,
|
||||||
#[covariant]
|
#[covariant]
|
||||||
dependent: ScopeTreeInner,
|
dependent: ScopeTreeInner,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue