|
| 1 | +//! A pointer type for heap allocation using the Zend memory manager. |
| 2 | +//! |
| 3 | +//! Heap memory in PHP is usually request-bound and allocated inside [memory arenas], which are cleared |
| 4 | +//! at the start and end of a PHP request. Allocating and freeing memory that is allocated on the Zend |
| 5 | +//! heap is done through two separate functions [`efree`] and [`emalloc`]. |
| 6 | +//! |
| 7 | +//! As such, most heap-allocated PHP types **cannot** be allocated on the stack, such as [`ZendStr`], which |
| 8 | +//! is a dynamically-sized type, and therefore must be allocated on the heap. A regular [`Box`] would not |
| 9 | +//! work in this case, as the memory needs to be freed from a separate function `zend_string_release`. The |
| 10 | +//! [`ZBox`] type provides a wrapper which calls the relevant release functions based on the type and what is |
| 11 | +//! inside the implementation of [`ZBoxable`]. |
| 12 | +//! |
| 13 | +//! This type is not created directly, but rather through a function implemented on the downstream type. For |
| 14 | +//! example, [`ZendStr`] has a function `new` which returns a [`ZBox<ZendStr>`]. |
| 15 | +//! |
| 16 | +//! [memory arenas]: https://en.wikipedia.org/wiki/Region-based_memory_management |
| 17 | +//! [`ZendStr`]: super::types::string::ZendStr |
| 18 | +//! [`emalloc`]: super::alloc::efree |
| 19 | +
|
| 20 | +use std::{ |
| 21 | + borrow::Borrow, |
| 22 | + fmt::Debug, |
| 23 | + mem::ManuallyDrop, |
| 24 | + ops::{Deref, DerefMut}, |
| 25 | + ptr::NonNull, |
| 26 | +}; |
| 27 | + |
| 28 | +use super::alloc::efree; |
| 29 | + |
| 30 | +/// A pointer type for heap allocation using the Zend memory manager. |
| 31 | +/// |
| 32 | +/// See the [module level documentation](../index.html) for more. |
| 33 | +pub struct ZBox<T: ZBoxable>(NonNull<T>); |
| 34 | + |
| 35 | +impl<T: ZBoxable> ZBox<T> { |
| 36 | + /// Creates a new box from a given pointer. |
| 37 | + /// |
| 38 | + /// # Parameters |
| 39 | + /// |
| 40 | + /// * `ptr` - A non-null, well-aligned pointer to a `T`. |
| 41 | + /// |
| 42 | + /// # Safety |
| 43 | + /// |
| 44 | + /// Caller must ensure that `ptr` is non-null, well-aligned and pointing to a `T`. |
| 45 | + pub unsafe fn from_raw(ptr: *mut T) -> Self { |
| 46 | + Self(NonNull::new_unchecked(ptr)) |
| 47 | + } |
| 48 | + |
| 49 | + /// Returns the pointer contained by the box, dropping the box in the process. The data pointed to by |
| 50 | + /// the returned pointer is not released. |
| 51 | + /// |
| 52 | + /// # Safety |
| 53 | + /// |
| 54 | + /// The caller is responsible for managing the memory pointed to by the returned pointer, including |
| 55 | + /// freeing the memory. |
| 56 | + pub fn into_raw(self) -> &'static mut T { |
| 57 | + let mut this = ManuallyDrop::new(self); |
| 58 | + // SAFETY: All constructors ensure the contained pointer is well-aligned and dereferencable. |
| 59 | + unsafe { this.0.as_mut() } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl<T: ZBoxable> Drop for ZBox<T> { |
| 64 | + #[inline] |
| 65 | + fn drop(&mut self) { |
| 66 | + self.deref_mut().free() |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl<T: ZBoxable> Deref for ZBox<T> { |
| 71 | + type Target = T; |
| 72 | + |
| 73 | + #[inline] |
| 74 | + fn deref(&self) -> &Self::Target { |
| 75 | + // SAFETY: All constructors ensure the contained pointer is well-aligned and dereferencable. |
| 76 | + unsafe { self.0.as_ref() } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl<T: ZBoxable> DerefMut for ZBox<T> { |
| 81 | + #[inline] |
| 82 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 83 | + // SAFETY: All constructors ensure the contained pointer is well-aligned and dereferencable. |
| 84 | + unsafe { self.0.as_mut() } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl<T: ZBoxable + Debug> Debug for ZBox<T> { |
| 89 | + #[inline] |
| 90 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 91 | + (&**self).fmt(f) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl<T: ZBoxable> Borrow<T> for ZBox<T> { |
| 96 | + #[inline] |
| 97 | + fn borrow(&self) -> &T { |
| 98 | + &**self |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl<T: ZBoxable> AsRef<T> for ZBox<T> { |
| 103 | + #[inline] |
| 104 | + fn as_ref(&self) -> &T { |
| 105 | + self |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/// Implemented on types that can be heap allocated using the Zend memory manager. These types are stored |
| 110 | +/// inside a [`ZBox`] when heap-allocated, and the [`free`] method is called when the box is dropped. |
| 111 | +/// |
| 112 | +/// # Safety |
| 113 | +/// |
| 114 | +/// The default implementation of the [`free`] function uses the [`efree`] function to free the memory without |
| 115 | +/// calling any destructors. |
| 116 | +/// |
| 117 | +/// The implementor must ensure that any time a pointer to the implementor is passed into a [`ZBox`] that the |
| 118 | +/// memory pointed to was allocated by the Zend memory manager. |
| 119 | +/// |
| 120 | +/// [`free`]: #method.free |
| 121 | +pub unsafe trait ZBoxable { |
| 122 | + /// Frees the memory pointed to by `self`, calling any destructors required in the process. |
| 123 | + fn free(&mut self) { |
| 124 | + unsafe { efree(self as *mut _ as *mut u8) }; |
| 125 | + } |
| 126 | +} |
0 commit comments