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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use std::alloc::Layout;
use std::future::Future;
use std::pin::Pin;
use std::ptr::NonNull;

use crate::arena::raw_arena::RawArena;

use super::alloc;
use super::ptr_byte_add;
use super::ptr_byte_sub;

/// In debug mode we use a signature to ensure that raw pointers are pointing to the correct
/// shape of arena object.
#[cfg(debug_assertions)]
const SIGNATURE: usize = 0x8877665544332211;

pub struct ArenaUniqueReservation<T>(NonNull<ArenaBoxData<T>>);

impl<T> Drop for ArenaUniqueReservation<T> {
  fn drop(&mut self) {
    panic!("A reservation must be completed or forgotten")
  }
}

pub struct ArenaBox<T: 'static> {
  ptr: NonNull<ArenaBoxData<T>>,
}

impl<T> Unpin for ArenaBox<T> {}

struct ArenaBoxData<T> {
  #[cfg(debug_assertions)]
  signature: usize,
  arena_data: NonNull<ArenaUniqueData<T>>,
  data: T,
}

impl<T: 'static> ArenaBox<T> {
  /// Offset of the `ptr` field within the `ArenaBox` struct.
  const PTR_OFFSET: usize = memoffset::offset_of!(ArenaBox<T>, ptr);

  /// Constructs a `NonNull` reference to `ArenaBoxData` from a raw pointer to `T`.
  #[inline(always)]
  unsafe fn data_from_ptr(ptr: NonNull<T>) -> NonNull<ArenaBoxData<T>> {
    ptr_byte_sub(ptr, Self::PTR_OFFSET)
  }

  /// Obtains a raw pointer to `T` from a `NonNull` reference to `ArenaBoxData`.  
  #[inline(always)]
  unsafe fn ptr_from_data(ptr: NonNull<ArenaBoxData<T>>) -> NonNull<T> {
    ptr_byte_add(ptr, Self::PTR_OFFSET)
  }

  /// Transforms an `ArenaBox` into a raw pointer to `T` and forgets it.
  ///
  /// # Safety
  ///
  /// This function returns a raw pointer without managing the memory, potentially leading to
  /// memory leaks if the pointer is not properly handled or deallocated.
  #[inline(always)]
  pub fn into_raw(mut alloc: ArenaBox<T>) -> NonNull<T> {
    let ptr = NonNull::from(alloc.data_mut());
    std::mem::forget(alloc);
    unsafe { Self::ptr_from_data(ptr) }
  }

  /// Constructs an `ArenaBox` from a raw pointer to the contained data.
  ///
  /// # Safety
  ///
  /// This function safely constructs an `ArenaBox` from a raw pointer, assuming the pointer is
  /// valid and properly aligned. Misuse may lead to undefined behavior, memory unsafety, or data corruption.
  #[inline(always)]
  pub unsafe fn from_raw(ptr: NonNull<T>) -> ArenaBox<T> {
    let ptr = Self::data_from_ptr(ptr);

    #[cfg(debug_assertions)]
    debug_assert_eq!(ptr.as_ref().signature, SIGNATURE);
    ArenaBox { ptr }
  }
}

// This Box cannot be sent between threads
static_assertions::assert_not_impl_any!(ArenaBox<()>: Send, Sync);

impl<T> ArenaBox<T> {
  #[inline(always)]
  fn data(&self) -> &ArenaBoxData<T> {
    unsafe { self.ptr.as_ref() }
  }

  #[inline(always)]
  fn data_mut(&mut self) -> &mut ArenaBoxData<T> {
    unsafe { self.ptr.as_mut() }
  }
}

impl<T> Drop for ArenaBox<T> {
  #[inline(always)]
  fn drop(&mut self) {
    unsafe {
      ArenaUnique::delete(self.ptr);
    }
  }
}

impl<T> std::ops::Deref for ArenaBox<T> {
  type Target = T;
  #[inline(always)]
  fn deref(&self) -> &Self::Target {
    &self.data().data
  }
}

impl<T> std::convert::AsRef<T> for ArenaBox<T> {
  #[inline(always)]
  fn as_ref(&self) -> &T {
    &self.data().data
  }
}

impl<T> std::ops::DerefMut for ArenaBox<T> {
  #[inline(always)]
  fn deref_mut(&mut self) -> &mut Self::Target {
    &mut self.data_mut().data
  }
}

impl<T> std::convert::AsMut<T> for ArenaBox<T> {
  #[inline(always)]
  fn as_mut(&mut self) -> &mut T {
    &mut self.data_mut().data
  }
}

impl<F, R> std::future::Future for ArenaBox<F>
where
  F: Future<Output = R>,
{
  type Output = R;

  #[inline(always)]
  fn poll(
    mut self: Pin<&mut Self>,
    cx: &mut std::task::Context<'_>,
  ) -> std::task::Poll<Self::Output> {
    unsafe { F::poll(Pin::new_unchecked(&mut self.data_mut().data), cx) }
  }
}

/// An arena-based unique ownership container allowing allocation
/// and deallocation of objects with exclusive ownership semantics.
///
/// `ArenaUnique` provides exclusive ownership semantics similar to
/// a `Box`. It utilizes a `RawArena` for allocation and
/// deallocation of objects, maintaining the sole ownership of the
/// allocated data and enabling safe cleanup when the `ArenaUnique`
/// instance is dropped.
///
/// This container guarantees exclusive access to the allocated data
/// within the arena, allowing single-threaded operations while
/// efficiently managing memory and ensuring cleanup on drop.
pub struct ArenaUnique<T> {
  ptr: NonNull<ArenaUniqueData<T>>,
}

// The arena itself may not be shared so that we can guarantee all [`RawArena`]
// access happens on the owning thread.
static_assertions::assert_not_impl_any!(ArenaUnique<()>: Send, Sync);

struct ArenaUniqueData<T> {
  raw_arena: RawArena<ArenaBoxData<T>>,
  alive: bool,
}

impl<T> ArenaUnique<T> {
  /// Returns the constant overhead per allocation to assist with making allocations
  /// page-aligned.
  pub const fn overhead() -> usize {
    Self::allocation_size() - std::mem::size_of::<T>()
  }

  /// Returns the size of each allocation.
  pub const fn allocation_size() -> usize {
    RawArena::<ArenaBoxData<T>>::allocation_size()
  }

  pub fn with_capacity(capacity: usize) -> Self {
    unsafe {
      let ptr = alloc();
      std::ptr::write(
        ptr.as_ptr(),
        ArenaUniqueData {
          raw_arena: RawArena::with_capacity(capacity),
          alive: true,
        },
      );
      Self { ptr }
    }
  }

  #[cold]
  #[inline(never)]
  unsafe fn drop_data(data: NonNull<ArenaUniqueData<T>>) {
    let data = data.as_ptr();
    std::ptr::drop_in_place(data);
    std::alloc::dealloc(data as _, Layout::new::<ArenaUniqueData<T>>());
  }

  /// Deletes the data associated with an `ArenaBox` from the arena. If this is the last
  /// allocation for the arena and the arena has been dropped, de-allocate everything.
  #[inline(always)]
  unsafe fn delete(data: NonNull<ArenaBoxData<T>>) {
    let arena_data = data.as_ref().arena_data;
    let arena = arena_data.as_ref();
    if arena.raw_arena.recycle(data) && !arena.alive {
      Self::drop_data(arena_data)
    }
  }

  /// Allocates a new data instance of type `T` within the arena, encapsulating it within an `ArenaBox`.
  ///
  /// This method creates a new instance of type `T` within the `RawArena`. The provided `data`
  /// is initialized within the arena, and an `ArenaBox` is returned to manage this allocated data.
  /// The `ArenaBox` serves as a reference to the allocated data within the arena, providing safe access
  /// and management of the stored value.
  ///
  /// # Safety
  ///
  /// The provided `data` is allocated within the arena and managed by the `ArenaBox`. Improper handling
  /// or misuse of the returned `ArenaBox` pointer may lead to memory leaks or memory unsafety.
  ///
  /// # Example
  ///
  /// ```rust
  /// # use deno_core::arena::ArenaUnique;
  ///
  /// // Define a struct that will be allocated within the arena
  /// struct MyStruct {
  ///   data: usize,
  /// }
  ///
  /// // Create a new instance of ArenaUnique with a specified base capacity
  /// let arena: ArenaUnique<MyStruct> = ArenaUnique::with_capacity(16);
  ///
  /// // Allocate a new MyStruct instance within the arena
  /// let data_instance = MyStruct { data: 42 };
  /// let allocated_box = arena.allocate(data_instance);
  ///
  /// // Now, allocated_box can be used as a managed reference to the allocated data
  /// assert_eq!(allocated_box.data, 42); // Validate the data stored in the allocated box
  /// ```
  pub fn allocate(&self, data: T) -> ArenaBox<T> {
    let ptr = unsafe {
      let this = self.ptr.as_ptr();
      let ptr = (*this).raw_arena.allocate();
      std::ptr::write(
        ptr.as_ptr(),
        ArenaBoxData {
          #[cfg(debug_assertions)]
          signature: SIGNATURE,
          arena_data: self.ptr,
          data,
        },
      );
      ptr
    };

    ArenaBox { ptr }
  }

  /// Attempt to reserve space in this arena.
  ///
  /// # Safety
  ///
  /// Reservations must be either completed or forgotten, and must be provided to the same
  /// arena that created them.
  #[inline(always)]
  pub unsafe fn reserve_space(&self) -> Option<ArenaUniqueReservation<T>> {
    let this = &mut *self.ptr.as_ptr();
    let ptr = this.raw_arena.allocate_if_space()?;
    Some(ArenaUniqueReservation(ptr))
  }

  /// Forget a reservation.
  ///
  /// # Safety
  ///
  /// Reservations must be either completed or forgotten, and must be provided to the same
  /// arena that created them.
  pub unsafe fn forget_reservation(
    &self,
    reservation: ArenaUniqueReservation<T>,
  ) {
    let ptr = reservation.0;
    std::mem::forget(reservation);
    let this = self.ptr.as_ptr();
    (*this).raw_arena.recycle_without_drop(ptr);
  }

  /// Complete a reservation.
  ///
  /// # Safety
  ///
  /// Reservations must be either completed or forgotten, and must be provided to the same
  /// arena that created them.
  #[inline(always)]
  pub unsafe fn complete_reservation(
    &self,
    reservation: ArenaUniqueReservation<T>,
    data: T,
  ) -> ArenaBox<T> {
    let ptr = reservation.0;
    std::mem::forget(reservation);
    let ptr = {
      std::ptr::write(
        ptr.as_ptr(),
        ArenaBoxData {
          #[cfg(debug_assertions)]
          signature: SIGNATURE,
          arena_data: self.ptr,
          data,
        },
      );
      ptr
    };

    ArenaBox { ptr }
  }
}

impl<T> Drop for ArenaUnique<T> {
  fn drop(&mut self) {
    unsafe {
      let this = self.ptr.as_mut();
      if this.raw_arena.allocated() == 0 {
        Self::drop_data(self.ptr);
      } else {
        this.alive = false;
      }
    }
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use std::cell::RefCell;

  #[test]
  fn test_raw() {
    let arena: ArenaUnique<RefCell<usize>> = ArenaUnique::with_capacity(16);
    let arc = arena.allocate(Default::default());
    let raw = ArenaBox::into_raw(arc);
    _ = unsafe { ArenaBox::from_raw(raw) };
  }

  #[test]
  fn test_allocate_drop_box_first() {
    let arena: ArenaUnique<RefCell<usize>> = ArenaUnique::with_capacity(16);
    let alloc = arena.allocate(Default::default());
    *alloc.borrow_mut() += 1;
    drop(alloc);
    drop(arena);
  }

  #[test]
  fn test_allocate_drop_arena_first() {
    let arena: ArenaUnique<RefCell<usize>> = ArenaUnique::with_capacity(16);
    let alloc = arena.allocate(Default::default());
    *alloc.borrow_mut() += 1;
    drop(arena);
    drop(alloc);
  }
}