From c45e485bcafd05509de3c03b6e92c749b527c5f9 Mon Sep 17 00:00:00 2001 From: Jubilee Young Date: Mon, 5 Feb 2024 13:19:41 -0800 Subject: [PATCH] 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 6f74fd548d..6c3c78e3ae 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 59c135172a..c414dc60f1 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 820bda2354..07fa49ae9e 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 0000000000..50a24b0936 --- /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() + } +}