Skip to content

Commit 5cc614a

Browse files
committed
feat(allocator): replace allocator_ap2's Vec with Vec2 (#9656)
Just replace allocator_ap2's Vec with Vec2, and make some changes to make it compile successfully.
1 parent 6d079a9 commit 5cc614a

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

crates/oxc_allocator/src/clone_in.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ where
5555
}
5656
}
5757

58-
impl<'new_alloc, T, C> CloneIn<'new_alloc> for Vec<'_, T>
58+
impl<'new_alloc, T, C: 'new_alloc> CloneIn<'new_alloc> for Vec<'_, T>
5959
where
6060
T: CloneIn<'new_alloc, Cloned = C>,
6161
{

crates/oxc_allocator/src/string.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,23 @@ impl<'alloc> String<'alloc> {
106106
//
107107
// `#[inline(always)]` because this is a no-op at runtime
108108
#[inline(always)]
109-
pub unsafe fn from_utf8_unchecked(bytes: Vec<'alloc, u8>) -> String<'alloc> {
109+
pub unsafe fn from_utf8_unchecked(mut bytes: Vec<'alloc, u8>) -> String<'alloc> {
110110
// Cannot use `bumpalo::String::from_utf8_unchecked` because it takes a `bumpalo::collections::Vec`,
111-
// and our inner `Vec` type is `allocator_api2::vec::Vec`.
111+
// and our inner `Vec` type is our own `crate::vec2::Vec`.
112+
//
112113
// SAFETY: Conversion is safe because both types store data in arena in same way.
113114
// Lifetime of returned `String` is same as lifetime of original `Vec<u8>`.
115+
//
116+
// `into_raw_parts_with_alloc` consumed `bytes`, so it wasn't an issue before.
117+
// But `as_mut_ptr` doesn't consume `bytes`, so if `Vec` was `Drop`, it'd get
118+
// dropped at end of this function, which would free the memory which is now
119+
// backing the `String`. Ditto if it was `InnerVec` which is (currently) `Drop`.
120+
114121
unsafe {
115-
let inner = ManuallyDrop::into_inner(bytes.0);
116-
let (ptr, len, capacity, bump) = inner.into_raw_parts_with_alloc();
122+
let ptr = bytes.as_mut_ptr();
123+
let len = bytes.len();
124+
let capacity = bytes.capacity();
125+
let bump = bytes.bump();
117126
Self(ManuallyDrop::new(BumpaloString::from_raw_parts_in(ptr, len, capacity, bump)))
118127
}
119128
}

crates/oxc_allocator/src/vec.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ use std::{
1414
slice::SliceIndex,
1515
};
1616

17-
use allocator_api2::vec::Vec as InnerVec;
18-
use bumpalo::Bump;
17+
use crate::vec2::Vec as InnerVec;
1918
#[cfg(any(feature = "serialize", test))]
2019
use oxc_estree::{ESTree, Serializer as ESTreeSerializer};
2120
#[cfg(any(feature = "serialize", test))]
@@ -36,7 +35,7 @@ use crate::{Allocator, Box};
3635
/// Static checks make this impossible to do. [`Vec::new_in`] and all other methods which create
3736
/// a [`Vec`] will refuse to compile if called with a [`Drop`] type.
3837
#[derive(PartialEq, Eq)]
39-
pub struct Vec<'alloc, T>(pub(crate) ManuallyDrop<InnerVec<T, &'alloc Bump>>);
38+
pub struct Vec<'alloc, T>(pub(crate) ManuallyDrop<InnerVec<'alloc, T>>);
4039

4140
/// SAFETY: Not actually safe, but for enabling `Send` for downstream crates.
4241
unsafe impl<T> Send for Vec<'_, T> {}
@@ -169,7 +168,7 @@ impl<'alloc, T> Vec<'alloc, T> {
169168
}
170169

171170
impl<'alloc, T> ops::Deref for Vec<'alloc, T> {
172-
type Target = InnerVec<T, &'alloc Bump>;
171+
type Target = InnerVec<'alloc, T>;
173172

174173
#[inline]
175174
fn deref(&self) -> &Self::Target {
@@ -179,13 +178,13 @@ impl<'alloc, T> ops::Deref for Vec<'alloc, T> {
179178

180179
impl<'alloc, T> ops::DerefMut for Vec<'alloc, T> {
181180
#[inline]
182-
fn deref_mut(&mut self) -> &mut InnerVec<T, &'alloc Bump> {
181+
fn deref_mut(&mut self) -> &mut InnerVec<'alloc, T> {
183182
&mut self.0
184183
}
185184
}
186185

187186
impl<'alloc, T> IntoIterator for Vec<'alloc, T> {
188-
type IntoIter = <InnerVec<T, &'alloc Bump> as IntoIterator>::IntoIter;
187+
type IntoIter = <InnerVec<'alloc, T> as IntoIterator>::IntoIter;
189188
type Item = T;
190189

191190
#[inline(always)]

0 commit comments

Comments
 (0)