|
| 1 | +use crate::callconv::{BoxRet, FcInfo}; |
| 2 | +use crate::datum::{BorrowDatum, Datum}; |
| 3 | +use crate::layout::PassBy; |
| 4 | +use crate::memcx::MemCx; |
| 5 | +use crate::pg_sys; |
| 6 | +use core::marker::PhantomData; |
| 7 | +use core::ops::{Deref, DerefMut}; |
| 8 | +use core::ptr::NonNull; |
| 9 | + |
| 10 | +use pgrx_sql_entity_graph::metadata::{ |
| 11 | + ArgumentError, Returns, ReturnsError, SqlMapping, SqlTranslatable, |
| 12 | +}; |
| 13 | + |
| 14 | +/** As [`Box<T, A>`][stdbox] where `A` is a [`MemCx`] |
| 15 | +
|
| 16 | +
|
| 17 | +[stdbox]: alloc::boxed::Box |
| 18 | +*/ |
| 19 | +#[repr(transparent)] |
| 20 | +pub struct PBox<'mcx, T: ?Sized> { |
| 21 | + ptr: NonNull<T>, |
| 22 | + _cx: PhantomData<MemCx<'mcx>>, |
| 23 | +} |
| 24 | + |
| 25 | +impl<'mcx, T: ?Sized> PBox<'mcx, T> { |
| 26 | + // # Safety |
| 27 | + // The same constraints as [`Box::from_raw`], AND |
| 28 | + // - you assert the pointer was allocated in the `MemCx` |
| 29 | + // - you assert the pointer may be freed by `pfree` |
| 30 | + pub unsafe fn from_raw_in(ptr: NonNull<T>, _cx: &MemCx<'mcx>) -> PBox<'mcx, T> { |
| 31 | + PBox { ptr, _cx: PhantomData } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl<'mcx, T: Sized> PBox<'mcx, T> { |
| 36 | + pub fn new_in(val: T, memcx: &MemCx<'mcx>) -> Self { |
| 37 | + const { assert!(align_of::<T>() <= size_of::<pg_sys::Datum>()) }; |
| 38 | + let ptr = memcx.alloc_bytes(size_of::<T>()).cast(); |
| 39 | + // SAFETY: We were guaranteed an appropriately sized allocation to write to, |
| 40 | + // and we have asserted our alignment maximum was upheld |
| 41 | + unsafe { ptr.write(val) }; |
| 42 | + PBox { ptr, _cx: PhantomData } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +unsafe impl<'mcx, T> BoxRet for PBox<'mcx, T> |
| 47 | +where |
| 48 | + T: ?Sized + BorrowDatum, |
| 49 | +{ |
| 50 | + unsafe fn box_into<'fcx>(self, fcinfo: &mut FcInfo<'fcx>) -> Datum<'fcx> { |
| 51 | + let datum = match T::PASS { |
| 52 | + PassBy::Value => { |
| 53 | + // start with a zeroed Datum, just to minimize funny business |
| 54 | + let mut datum = pg_sys::Datum::null(); |
| 55 | + // SAFETY: Due to BorrowDatum, this type has a definite size less than a Datum, |
| 56 | + // and PBox must have an initialized pointee, so a copy is sound-by-construction. |
| 57 | + unsafe { |
| 58 | + let size = size_of_val(&*self.ptr.as_ptr()); |
| 59 | + debug_assert!(size <= size_of::<pg_sys::Datum>()); |
| 60 | + // using `BorrowDatum::point_from` handles endianness |
| 61 | + let datum_ptr = T::point_from(NonNull::from_mut(&mut datum).cast::<u8>()); |
| 62 | + datum_ptr.cast::<u8>().copy_from_nonoverlapping(self.ptr.cast(), size); |
| 63 | + } |
| 64 | + datum |
| 65 | + } |
| 66 | + PassBy::Ref => pg_sys::Datum::from(self.ptr.cast::<u8>().as_ptr()), |
| 67 | + }; |
| 68 | + // SAFETY: by proxy, BorrowDatum is an `unsafe trait` so the above impl must be correct |
| 69 | + unsafe { fcinfo.return_raw_datum(datum) } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/// SAFETY: SQL has no "pointers" so by-val and by-ref calling conventions are identical, |
| 74 | +/// and all `PBox` truly does is enable pass-by-ref returns of unsized values. |
| 75 | +unsafe impl<'mcx, T> SqlTranslatable for PBox<'mcx, T> |
| 76 | +where |
| 77 | + T: SqlTranslatable + ?Sized, |
| 78 | +{ |
| 79 | + fn argument_sql() -> Result<SqlMapping, ArgumentError> { |
| 80 | + T::argument_sql() |
| 81 | + } |
| 82 | + |
| 83 | + fn return_sql() -> Result<Returns, ReturnsError> { |
| 84 | + T::return_sql() |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl<'mcx, T> Deref for PBox<'mcx, T> |
| 89 | +where |
| 90 | + T: BorrowDatum + ?Sized, |
| 91 | +{ |
| 92 | + type Target = T; |
| 93 | + |
| 94 | + fn deref(&self) -> &Self::Target { |
| 95 | + // SAFETY: by construction |
| 96 | + unsafe { self.ptr.as_ref() } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl<'mcx, T> DerefMut for PBox<'mcx, T> |
| 101 | +where |
| 102 | + T: BorrowDatum + ?Sized, |
| 103 | +{ |
| 104 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 105 | + // SAFETY: by construction |
| 106 | + unsafe { self.ptr.as_mut() } |
| 107 | + } |
| 108 | +} |
0 commit comments