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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use std::alloc::Layout;
use std::ptr::NonNull;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;

use parking_lot::lock_api::RawMutex;

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 = 0x1133224455667788;

pub struct ArenaSharedAtomicReservation<T>(NonNull<ArenaArcData<T>>);

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

/// Represents an atomic reference-counted pointer into an arena-allocated object.
pub struct ArenaArc<T> {
  ptr: NonNull<ArenaArcData<T>>,
}

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

  /// Converts a raw pointer to the data into a `NonNull` pointer to `ArenaArcData`.
  ///
  /// # Safety
  ///
  /// This function assumes that the input `ptr` points to the data within an `ArenaArc` object.
  /// Improper usage may result in undefined behavior.
  #[inline(always)]
  unsafe fn data_from_ptr(ptr: NonNull<T>) -> NonNull<ArenaArcData<T>> {
    ptr_byte_sub(ptr, Self::PTR_OFFSET)
  }

  /// Converts a `NonNull` pointer to `ArenaArcData` into a raw pointer to the data.
  ///
  /// # Safety
  ///
  /// This function assumes that the input `ptr` is a valid `NonNull` pointer to `ArenaArcData`.
  /// Improper usage may result in undefined behavior.
  #[inline(always)]
  unsafe fn ptr_from_data(ptr: NonNull<ArenaArcData<T>>) -> NonNull<T> {
    ptr_byte_add(ptr, Self::PTR_OFFSET)
  }

  /// Consumes the `ArenaArc`, forgetting it, and returns a raw pointer to the contained data.
  ///
  /// # 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(arc: ArenaArc<T>) -> NonNull<T> {
    let ptr = arc.ptr;
    std::mem::forget(arc);
    unsafe { Self::ptr_from_data(ptr) }
  }

  /// Clones the `ArenaArc` reference, increments its reference count, and returns a raw pointer to the contained data.
  ///
  /// This function increments the reference count of the `ArenaArc`.
  #[inline(always)]
  pub fn clone_into_raw(arc: &ArenaArc<T>) -> NonNull<T> {
    unsafe {
      arc.ptr.as_ref().ref_count.fetch_add(1, Ordering::Relaxed);
      Self::ptr_from_data(arc.ptr)
    }
  }

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

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

  /// Clones an `ArenaArc` reference from a raw pointer and increments its reference count.
  ///
  /// This method increments the reference count of the `ArenaArc` instance
  /// associated with the provided raw pointer, allowing multiple references
  /// to the same allocated data.
  ///
  /// # Safety
  ///
  /// This function assumes that the provided `ptr` is a valid raw pointer
  /// to the data within an `ArenaArc` object. Improper usage may lead
  /// to memory unsafety or data corruption.
  #[inline(always)]
  pub unsafe fn clone_from_raw(ptr: NonNull<T>) -> ArenaArc<T> {
    let ptr = Self::data_from_ptr(ptr);
    let this = ptr.as_ref();
    this.ref_count.fetch_add(1, Ordering::Relaxed);
    ArenaArc { ptr }
  }

  /// Increments the reference count associated with the raw pointer to an `ArenaArc`-managed data.
  ///
  /// This method manually increases the reference count of the `ArenaArc` instance
  /// associated with the provided raw pointer. It allows incrementing the reference count
  /// without constructing a full `ArenaArc` instance, ideal for scenarios where direct
  /// manipulation of raw pointers is required.
  ///
  /// # Safety
  ///
  /// This method bypasses some safety checks enforced by the `ArenaArc` type. Incorrect usage
  /// or mishandling of raw pointers might lead to memory unsafety or data corruption.
  /// Use with caution and ensure proper handling of associated data.
  #[inline(always)]
  pub unsafe fn clone_raw_from_raw(ptr: NonNull<T>) {
    let this = Self::data_from_ptr(ptr).as_ref();
    this.ref_count.fetch_add(1, Ordering::Relaxed);
  }

  /// Drops the `ArenaArc` reference pointed to by the raw pointer.
  ///
  /// If the reference count drops to zero, the associated data is returned to the arena.
  ///
  /// # Safety
  ///
  /// This function assumes that the provided `ptr` is a valid raw pointer
  /// to the data within an `ArenaArc` object. Improper usage may lead
  /// to memory unsafety or data corruption.
  #[inline(always)]
  pub unsafe fn drop_from_raw(ptr: NonNull<T>) {
    let ptr = Self::data_from_ptr(ptr);
    let this = ptr.as_ref();
    if this.ref_count.fetch_sub(1, Ordering::Relaxed) == 0 {
      ArenaSharedAtomic::delete(ptr);
    }
  }
}

unsafe impl<T: Send + Sync> Send for ArenaArc<T> {}
unsafe impl<T: Send + Sync> Sync for ArenaArc<T> {}

// T is Send + Sync, so ArenaArc is too
static_assertions::assert_impl_all!(ArenaArc<()>: Send, Sync);
// T is !Send & !Sync, so ArenaArc is too
static_assertions::assert_not_impl_any!(ArenaArc<*mut ()>: Send, Sync);

impl<T> ArenaArc<T> {}

impl<T> Drop for ArenaArc<T> {
  fn drop(&mut self) {
    unsafe {
      let this = self.ptr.as_ref();
      if this.ref_count.fetch_sub(1, Ordering::Relaxed) == 0 {
        ArenaSharedAtomic::delete(self.ptr);
      }
    }
  }
}

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

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

/// Data structure containing metadata and the actual data within the `ArenaArc`.
struct ArenaArcData<T> {
  #[cfg(debug_assertions)]
  signature: usize,
  ref_count: AtomicUsize,
  arena_data: NonNull<ArenaSharedAtomicData<T>>,
  data: T,
}

/// An atomic reference-counted pointer into an arena-allocated object
/// with thread-safe allocation and deallocation capabilities.
///
/// This structure ensures atomic access and safe sharing of allocated
/// data across multiple threads while maintaining reference counting
/// to manage the memory deallocation when no longer needed.
///
/// It combines a thread-safe `RawArena` for allocation and deallocation
/// and provides a mutex to guarantee exclusive access to the internal
/// data for safe multi-threaded operation.
///
/// The `ArenaSharedAtomic` allows multiple threads to allocate, share,
/// and deallocate objects within the arena, ensuring safety and atomicity
/// during these operations.
pub struct ArenaSharedAtomic<T> {
  ptr: NonNull<ArenaSharedAtomicData<T>>,
}

unsafe impl<T> Send for ArenaSharedAtomic<T> {}

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

/// Data structure containing a mutex and the `RawArena` for atomic access in the `ArenaSharedAtomic`.
struct ArenaSharedAtomicData<T> {
  /// A mutex ensuring thread-safe access to the internal raw arena and refcount.
  mutex: parking_lot::RawMutex,
  protected: ArenaSharedAtomicDataProtected<T>,
}

struct ArenaSharedAtomicDataProtected<T> {
  raw_arena: RawArena<ArenaArcData<T>>,
  ref_count: usize,
}

impl<T> ArenaSharedAtomic<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::<ArenaArcData<T>>::allocation_size()
  }

  pub fn with_capacity(capacity: usize) -> Self {
    unsafe {
      let ptr = alloc();
      std::ptr::write(
        ptr.as_ptr(),
        ArenaSharedAtomicData {
          mutex: parking_lot::RawMutex::INIT,
          protected: ArenaSharedAtomicDataProtected {
            raw_arena: RawArena::with_capacity(capacity),
            ref_count: 0,
          },
        },
      );
      Self { ptr }
    }
  }

  #[inline(always)]
  unsafe fn lock<'s>(
    arena: NonNull<ArenaSharedAtomicData<T>>,
  ) -> &'s mut ArenaSharedAtomicDataProtected<T> {
    let mutex = &*std::ptr::addr_of!((*arena.as_ptr()).mutex);
    while !mutex.try_lock() {
      std::thread::yield_now();
    }
    &mut *std::ptr::addr_of_mut!((*arena.as_ptr()).protected)
  }

  #[inline(always)]
  unsafe fn unlock(arena: NonNull<ArenaSharedAtomicData<T>>) {
    let mutex = &*std::ptr::addr_of!((*arena.as_ptr()).mutex);
    mutex.unlock()
  }

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

  #[inline(always)]
  unsafe fn delete(data: NonNull<ArenaArcData<T>>) {
    let ptr = (*data.as_ptr()).arena_data;
    // We cannot materialize a reference to arena_data until we have the lock
    let this = Self::lock(ptr);
    this.raw_arena.recycle(data);
    if this.ref_count == 0 {
      Self::drop_data(ptr);
    } else {
      this.ref_count -= 1;
      Self::unlock(ptr);
    }
  }

  /// Allocates a new object in the arena and returns an `ArenaArc` pointing to it.
  ///
  /// This method creates a new instance of type `T` within the `RawArena`. The provided `data`
  /// is initialized within the arena, and an `ArenaArc` is returned to manage this allocated data.
  /// The `ArenaArc` serves as an atomic, reference-counted pointer to the allocated data within
  /// the arena, ensuring safe concurrent access across multiple threads while maintaining the
  /// reference count for memory management.
  ///
  /// The allocation process employs a mutex to ensure thread-safe access to the arena, allowing
  /// only one thread at a time to modify the internal state, including allocating and deallocating memory.
  ///
  /// # Safety
  ///
  /// The provided `data` is allocated within the arena and managed by the `ArenaArc`. Improper handling
  /// or misuse of the returned `ArenaArc` pointer may lead to memory leaks or memory unsafety.
  ///
  /// # Example
  ///
  /// ```rust
  /// # use deno_core::arena::ArenaSharedAtomic;
  ///
  /// // Define a struct that will be allocated within the arena
  /// struct MyStruct {
  ///     data: usize,
  /// }
  ///
  /// // Create a new instance of ArenaSharedAtomic with a specified base capacity
  /// let arena: ArenaSharedAtomic<MyStruct> = ArenaSharedAtomic::with_capacity(16);
  ///
  /// // Allocate a new MyStruct instance within the arena
  /// let data_instance = MyStruct { data: 42 };
  /// let allocated_arc = arena.allocate(data_instance);
  ///
  /// // Now, allocated_arc can be used as a managed reference to the allocated data
  /// assert_eq!(allocated_arc.data, 42); // Validate the data stored in the allocated arc
  /// ```
  pub fn allocate(&self, data: T) -> ArenaArc<T> {
    let ptr = unsafe {
      let this = Self::lock(self.ptr);
      let ptr = this.raw_arena.allocate();
      this.ref_count += 1;
      std::ptr::write(
        ptr.as_ptr(),
        ArenaArcData {
          #[cfg(debug_assertions)]
          signature: SIGNATURE,
          arena_data: self.ptr,
          ref_count: AtomicUsize::default(),
          data,
        },
      );
      Self::unlock(self.ptr);
      ptr
    };

    ArenaArc { ptr }
  }

  /// Allocates a new object in the arena and returns an `ArenaArc` pointing to it. If no space
  /// is available, returns the original object.
  ///
  /// This method creates a new instance of type `T` within the `RawArena`. The provided `data`
  /// is initialized within the arena, and an `ArenaArc` is returned to manage this allocated data.
  /// The `ArenaArc` serves as an atomic, reference-counted pointer to the allocated data within
  /// the arena, ensuring safe concurrent access across multiple threads while maintaining the
  /// reference count for memory management.
  ///
  /// The allocation process employs a mutex to ensure thread-safe access to the arena, allowing
  /// only one thread at a time to modify the internal state, including allocating and deallocating memory.
  pub fn allocate_if_space(&self, data: T) -> Result<ArenaArc<T>, T> {
    let ptr = unsafe {
      let this = Self::lock(self.ptr);
      let Some(ptr) = this.raw_arena.allocate_if_space() else {
        return Err(data);
      };
      this.ref_count += 1;
      Self::unlock(self.ptr);

      std::ptr::write(
        ptr.as_ptr(),
        ArenaArcData {
          #[cfg(debug_assertions)]
          signature: SIGNATURE,
          arena_data: self.ptr,
          ref_count: AtomicUsize::default(),
          data,
        },
      );
      ptr
    };

    Ok(ArenaArc { 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<ArenaSharedAtomicReservation<T>> {
    let this = Self::lock(self.ptr);
    let ptr = this.raw_arena.allocate_if_space()?;
    this.ref_count += 1;
    Self::unlock(self.ptr);
    Some(ArenaSharedAtomicReservation(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: ArenaSharedAtomicReservation<T>,
  ) {
    let ptr = reservation.0;
    std::mem::forget(reservation);
    let this = Self::lock(self.ptr);
    this.ref_count -= 1;
    this.raw_arena.recycle_without_drop(ptr);
    Self::unlock(self.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: ArenaSharedAtomicReservation<T>,
    data: T,
  ) -> ArenaArc<T> {
    let ptr = reservation.0;
    std::mem::forget(reservation);
    let ptr = {
      std::ptr::write(
        ptr.as_ptr(),
        ArenaArcData {
          #[cfg(debug_assertions)]
          signature: SIGNATURE,
          arena_data: self.ptr,
          ref_count: AtomicUsize::default(),
          data,
        },
      );
      ptr
    };
    ArenaArc { ptr }
  }
}

impl<T> Drop for ArenaSharedAtomic<T> {
  fn drop(&mut self) {
    unsafe {
      let this = Self::lock(self.ptr);
      if this.ref_count == 0 {
        Self::drop_data(self.ptr);
      } else {
        this.ref_count -= 1;
        Self::unlock(self.ptr);
      }
    }
  }
}

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

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

  #[test]
  fn test_clone_into_raw() {
    let arena: ArenaSharedAtomic<RefCell<usize>> =
      ArenaSharedAtomic::with_capacity(16);
    let arc = arena.allocate(Default::default());
    let raw = ArenaArc::clone_into_raw(&arc);
    _ = unsafe { ArenaArc::from_raw(raw) };
  }

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

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

  #[test]
  fn test_threaded() {
    let arena: Arc<Mutex<ArenaSharedAtomic<RefCell<usize>>>> =
      Arc::new(Mutex::new(ArenaSharedAtomic::with_capacity(2000)));
    const THREADS: usize = 20;
    const ITERATIONS: usize = 100;

    let mut handles = Vec::new();
    let barrier = Arc::new(std::sync::Barrier::new(THREADS));

    for _ in 0..THREADS {
      let arena = Arc::clone(&arena);
      let barrier = Arc::clone(&barrier);

      let handle = std::thread::spawn(move || {
        barrier.wait();

        for _ in 0..ITERATIONS {
          let arc = arena.lock().unwrap().allocate(RefCell::new(0));
          *arc.borrow_mut() += 1;
          drop(arc); // Ensure the Arc is dropped at the end of the iteration
        }
      });

      handles.push(handle);
    }

    for handle in handles {
      handle.join().unwrap();
    }
  }
}