1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
mod raw_arena;
mod shared_arena;
mod shared_atomic_arena;
mod unique_arena;

use std::alloc::handle_alloc_error;
use std::alloc::Layout;
use std::ptr::NonNull;

pub use raw_arena::*;
pub use shared_arena::*;
pub use shared_atomic_arena::*;
pub use unique_arena::*;

const unsafe fn ptr_byte_add<T, U>(
  ptr: NonNull<T>,
  offset: usize,
) -> NonNull<U> {
  NonNull::new_unchecked((ptr.as_ptr() as *mut u8).add(offset) as _)
}

const unsafe fn ptr_byte_sub<T, U>(
  ptr: NonNull<T>,
  offset: usize,
) -> NonNull<U> {
  NonNull::new_unchecked((ptr.as_ptr() as *mut u8).sub(offset) as _)
}

#[inline(always)]
fn alloc_layout<T>(layout: Layout) -> NonNull<T> {
  // Layout of size zero is UB
  assert!(std::mem::size_of::<T>() > 0);
  let alloc = unsafe { std::alloc::alloc(layout) } as *mut _;
  let Some(alloc) = NonNull::new(alloc) else {
    handle_alloc_error(layout);
  };
  alloc
}

#[inline(always)]
fn alloc<T>() -> NonNull<T> {
  // Layout of size zero is UB
  assert!(std::mem::size_of::<T>() > 0);
  let alloc = unsafe { std::alloc::alloc(Layout::new::<T>()) } as *mut _;
  let Some(alloc) = NonNull::new(alloc) else {
    handle_alloc_error(Layout::new::<T>());
  };
  alloc
}