use core::cell::Cell;
use core::fmt;
use core::mem::{align_of, size_of};
use super::StrRef;
pub const SCRATCH_CAPACITY: usize = 4992;
pub struct ScratchArena<const CAP: usize = SCRATCH_CAPACITY> {
inner: bumpalo::Bump,
used: Cell<usize>,
}
impl<const CAP: usize> Default for ScratchArena<CAP> {
fn default() -> Self {
Self::new()
}
}
impl<const CAP: usize> ScratchArena<CAP> {
#[inline]
pub fn new() -> Self {
Self {
inner: bumpalo::Bump::with_capacity(CAP),
used: Cell::new(0),
}
}
#[inline]
pub fn try_alloc<'a, T>(&'a self, val: T) -> Option<&'a mut T> {
let align = align_of::<T>().max(1);
let size = size_of::<T>();
let cur = (self.used.get() + align - 1) & !(align - 1);
self.used.set(cur + size);
Some(self.inner.alloc(val))
}
#[inline(always)]
pub fn alloc<'a, T>(&'a self, val: T) -> &'a mut T {
self.try_alloc(val).expect("ScratchArena overflow")
}
#[inline]
pub fn try_alloc_temp<'a, T>(&'a self, val: T) -> Option<&'a mut T> {
self.try_alloc(val)
}
#[inline(always)]
pub fn alloc_temp<'a, T>(&'a self, val: T) -> &'a mut T {
self.try_alloc_temp(val)
.expect("ScratchArena temp overflow")
}
#[inline(always)]
pub(crate) unsafe fn reset_temp(&self) {
}
#[inline(always)]
pub(crate) fn save_temp_mark(&self) -> usize {
CAP
}
#[inline(always)]
pub(crate) fn restore_temp_mark(&mut self, mark: usize) {
let _ = mark;
}
#[inline(always)]
pub(crate) unsafe fn rollback_speculative_marks(&self, mark: usize, temp_mark: usize) {
let _ = temp_mark;
if mark <= self.used.get() {
self.used.set(mark);
}
}
#[inline(always)]
pub(crate) fn contains_temp_ptr(&self, ptr: *const u8) -> bool {
if ptr.is_null() {
return false;
}
self.used.get() > 0
}
#[inline(always)]
pub fn alloc_bytes<'a>(&'a self, len: usize) -> &'a mut [u8] {
if len == 0 {
return &mut [];
}
self.used.set(self.used.get() + len);
self.inner.alloc_slice_fill_copy(len, 0u8)
}
pub fn alloc_iter<'a, T, I>(&'a self, iter: I) -> &'a mut [T]
where
I: IntoIterator<Item = T>,
{
let mut iter = iter.into_iter();
let (lower, upper) = iter.size_hint();
let size = size_of::<T>();
if Some(lower) == upper {
let count = lower;
if count == 0 || size == 0 {
return &mut [];
}
self.used.set(self.used.get() + count.saturating_mul(size));
return self.inner.alloc_slice_fill_with(count, |_| {
iter.next().expect("Exact size_hint mismatch")
});
}
let mut vec = bumpalo::collections::Vec::with_capacity_in(lower, &self.inner);
for item in iter {
vec.push(item);
}
self.used.set(self.used.get() + vec.len().saturating_mul(size));
vec.into_bump_slice_mut()
}
#[inline]
pub fn alloc_str<'a>(&'a self, s: &str) -> StrRef<'a> {
if s.is_empty() {
return StrRef::Static("");
}
let copied = self.inner.alloc_str(s);
self.used.set(self.used.get() + copied.len());
StrRef::Scratch(copied)
}
pub fn alloc_fmt<'a>(&'a self, args: fmt::Arguments<'_>) -> StrRef<'a> {
if let Some(static_str) = args.as_str() {
return StrRef::Static(static_str);
}
let mut s = bumpalo::collections::String::new_in(&self.inner);
let _ = fmt::write(&mut s, args);
self.used.set(self.used.get() + s.len());
StrRef::Scratch(s.into_bump_str())
}
#[inline(always)]
pub fn rewind(&mut self) {
self.used.set(0);
self.inner.reset();
}
#[inline(always)]
pub fn save_mark(&self) -> usize {
self.used_bytes()
}
#[inline(always)]
pub fn restore_mark(&mut self, mark: usize) {
if mark <= self.used.get() {
self.used.set(mark);
}
}
#[inline(always)]
pub fn used_bytes(&self) -> usize {
self.used.get()
}
#[inline(always)]
pub(crate) fn real_allocated_bytes(&self) -> usize {
self.inner.allocated_bytes()
}
#[inline(always)]
pub fn can_alloc_slice<T>(&self, count: usize) -> bool {
let _ = count;
true
}
#[inline(always)]
pub fn can_alloc_temp_slice<T>(&self, count: usize) -> bool {
let _ = count;
true
}
pub fn alloc_temp_iter_exact<'a, T, I>(&'a self, count: usize, iter: I) -> &'a mut [T]
where
I: IntoIterator<Item = T>,
{
if count == 0 || size_of::<T>() == 0 {
return &mut [];
}
self.alloc_iter(iter.into_iter().take(count))
}
}