From c45e485bcafd05509de3c03b6e92c749b527c5f9 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Mon, 5 Feb 2024 13:19:41 -0800 Subject: [PATCH 1/2] Add is_non_null to pointers --- pgrx/src/lib.rs | 1 + pgrx/src/list/flat_list.rs | 9 +++++---- pgrx/src/list/linked_list.rs | 7 ++++--- pgrx/src/ptr.rs | 15 +++++++++++++++ 4 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 pgrx/src/ptr.rs diff --git a/pgrx/src/lib.rs b/pgrx/src/lib.rs index 6f74fd548..6c3c78e3a 100644 --- a/pgrx/src/lib.rs +++ b/pgrx/src/lib.rs @@ -88,6 +88,7 @@ pub use once_cell; /// Not ready for public exposure. mod layout; +mod ptr; mod slice; mod toast; diff --git a/pgrx/src/list/flat_list.rs b/pgrx/src/list/flat_list.rs index 59c135172..c414dc60f 100644 --- a/pgrx/src/list/flat_list.rs +++ b/pgrx/src/list/flat_list.rs @@ -1,6 +1,7 @@ use super::{Enlist, List, ListCell, ListHead}; use crate::memcx::MemCx; use crate::pg_sys; +use crate::ptr::PointerExt; use crate::seal::Sealed; use core::cmp; use core::ffi; @@ -195,7 +196,7 @@ impl<'cx, T: Enlist> List<'cx, T> { }; // Remember to check that our raw ptr is non-null - if raw != ptr::null_mut() { + if raw.is_non_null() { // Shorten the list to prohibit interaction with List's state after drain_start. // Note this breaks List repr invariants in the `drain_start == 0` case, but // we only consider returning the list ptr to `&mut self` if Drop is completed @@ -332,11 +333,11 @@ unsafe fn grow_list(list: &mut pg_sys::List, target: usize) { if list.elements == ptr::addr_of_mut!(list.initial_elements).cast() { // first realloc, we can't dealloc the elements ptr, as it isn't its own alloc let context = pg_sys::GetMemoryChunkContext(list as *mut pg_sys::List as *mut _); - if context == ptr::null_mut() { + if context.is_null() { panic!("Context free list?"); } let buf = pg_sys::MemoryContextAlloc(context, alloc_size); - if buf == ptr::null_mut() { + if buf.is_null() { panic!("List allocation failure"); } ptr::copy_nonoverlapping(list.elements, buf.cast(), list.length as _); @@ -382,7 +383,7 @@ pub struct Drain<'a, 'cx, T> { impl<'a, 'cx, T> Drop for Drain<'a, 'cx, T> { fn drop(&mut self) { - if self.raw == ptr::null_mut() { + if self.raw.is_null() { return; } diff --git a/pgrx/src/list/linked_list.rs b/pgrx/src/list/linked_list.rs index 820bda235..07fa49ae9 100644 --- a/pgrx/src/list/linked_list.rs +++ b/pgrx/src/list/linked_list.rs @@ -1,6 +1,7 @@ use super::{Enlist, List, ListCell, ListHead}; use crate::memcx::MemCx; use crate::pg_sys; +use crate::ptr::PointerExt; use crate::seal::Sealed; use core::cmp; use core::ffi; @@ -209,7 +210,7 @@ impl<'cx, T: Enlist> List<'cx, T> { }; // Remember to check that our raw ptr is non-null - if raw != ptr::null_mut() { + if raw.is_non_null() { // Shorten the list to prohibit interaction with List's state after drain_start. // Note this breaks List repr invariants in the `drain_start == 0` case, but // we only consider returning the list ptr to `&mut self` if Drop is completed @@ -328,7 +329,7 @@ unsafe fn cons_cell(list: &mut pg_sys::List, value: T) -> *mut pg_sys unsafe fn destroy_list(list: *mut pg_sys::List) { let mut cell = (*list).head; - while cell != ptr::null_mut() { + while cell.is_non_null() { let next = (*cell).next; pg_sys::pfree(cell.cast()); cell = next; @@ -496,7 +497,7 @@ impl Iterator for RawCellIter { #[inline] fn next(&mut self) -> Option { - if self.ptr != ptr::null_mut() { + if self.ptr.is_non_null() { let ptr = self.ptr; // SAFETY: It's assumed that the pointers are valid on construction unsafe { diff --git a/pgrx/src/ptr.rs b/pgrx/src/ptr.rs new file mode 100644 index 000000000..50a24b093 --- /dev/null +++ b/pgrx/src/ptr.rs @@ -0,0 +1,15 @@ +pub(crate) trait PointerExt { + fn is_non_null(&self) -> bool; +} + +impl PointerExt for *mut T { + fn is_non_null(&self) -> bool { + !self.is_null() + } +} + +impl PointerExt for *const T { + fn is_non_null(&self) -> bool { + !self.is_null() + } +} From 6a4d2e501953f92b57d1057508fe7c9bf0d3d1e8 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Mon, 5 Feb 2024 13:57:40 -0800 Subject: [PATCH 2/2] is_non_null in planner_rt_fetch --- pgrx/src/pg_sys.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pgrx/src/pg_sys.rs b/pgrx/src/pg_sys.rs index 1da93a6ce..e432bc97e 100644 --- a/pgrx/src/pg_sys.rs +++ b/pgrx/src/pg_sys.rs @@ -2,6 +2,7 @@ // Flatten out the contents into here. use crate::memcx; +use crate::ptr::PointerExt; pub use pgrx_pg_sys::*; // Interposing here can allow extensions like ZomboDB to skip the cshim, @@ -39,7 +40,7 @@ pub unsafe fn rt_fetch(index: Index, range_table: *mut List) -> *mut RangeTblEnt #[inline] pub unsafe fn planner_rt_fetch(index: Index, root: *mut PlannerInfo) -> *mut RangeTblEntry { unsafe { - if (*root).simple_rte_array != core::ptr::null_mut() { + if (*root).simple_rte_array.is_non_null() { *(*root).simple_rte_array.add(index as _) } else { rt_fetch(index, (*(*root).parse).rtable).cast()