|
| 1 | +// Copyright (c) 2025 PHPER Framework Team |
| 2 | +// PHPER is licensed under Mulan PSL v2. |
| 3 | +// You can use this software according to the terms and conditions of the Mulan |
| 4 | +// PSL v2. You may obtain a copy of Mulan PSL v2 at: |
| 5 | +// http://license.coscl.org.cn/MulanPSL2 |
| 6 | +// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY |
| 7 | +// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO |
| 8 | +// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. |
| 9 | +// See the Mulan PSL v2 for more details. |
| 10 | + |
| 11 | +//! Memory allocation utilities and boxed types for PHP values. |
| 12 | +
|
| 13 | +pub use phper_alloc::{RefClone, ToRefOwned}; |
| 14 | +use std::{ |
| 15 | + borrow::{Borrow, BorrowMut}, |
| 16 | + fmt::{self}, |
| 17 | + mem::ManuallyDrop, |
| 18 | + ops::{Deref, DerefMut}, |
| 19 | +}; |
| 20 | + |
| 21 | +/// A smart pointer for PHP values allocated in the Zend Engine memory. |
| 22 | +/// |
| 23 | +/// `EBox<T>` provides owned access to values allocated in PHP's memory |
| 24 | +/// management system. It automatically handles deallocation when dropped, |
| 25 | +/// ensuring proper cleanup of PHP resources. |
| 26 | +pub struct EBox<T> { |
| 27 | + ptr: *mut T, |
| 28 | +} |
| 29 | + |
| 30 | +impl<T> EBox<T> { |
| 31 | + /// Constructs from a raw pointer. |
| 32 | + /// |
| 33 | + /// # Safety |
| 34 | + /// |
| 35 | + /// Make sure the pointer is from `into_raw`, or created from `emalloc`. |
| 36 | + pub unsafe fn from_raw(raw: *mut T) -> Self { |
| 37 | + Self { ptr: raw } |
| 38 | + } |
| 39 | + |
| 40 | + /// Consumes and returning a wrapped raw pointer. |
| 41 | + /// |
| 42 | + /// Will leak memory. |
| 43 | + pub fn into_raw(b: EBox<T>) -> *mut T { |
| 44 | + ManuallyDrop::new(b).ptr |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl<T: fmt::Debug> fmt::Debug for EBox<T> { |
| 49 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 50 | + fmt::Debug::fmt(&**self, f) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl<T> Deref for EBox<T> { |
| 55 | + type Target = T; |
| 56 | + |
| 57 | + fn deref(&self) -> &Self::Target { |
| 58 | + unsafe { self.ptr.as_ref().unwrap() } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl<T> DerefMut for EBox<T> { |
| 63 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 64 | + unsafe { self.ptr.as_mut().unwrap() } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl<T> Drop for EBox<T> { |
| 69 | + fn drop(&mut self) { |
| 70 | + unsafe { |
| 71 | + self.ptr.drop_in_place(); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl<T> Borrow<T> for EBox<T> { |
| 77 | + fn borrow(&self) -> &T { |
| 78 | + unsafe { self.ptr.as_ref().unwrap() } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl<T> BorrowMut<T> for EBox<T> { |
| 83 | + fn borrow_mut(&mut self) -> &mut T { |
| 84 | + unsafe { self.ptr.as_mut().unwrap() } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl<T> AsRef<T> for EBox<T> { |
| 89 | + fn as_ref(&self) -> &T { |
| 90 | + unsafe { self.ptr.as_ref().unwrap() } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl<T> AsMut<T> for EBox<T> { |
| 95 | + fn as_mut(&mut self) -> &mut T { |
| 96 | + unsafe { self.ptr.as_mut().unwrap() } |
| 97 | + } |
| 98 | +} |
0 commit comments