Skip to content

Commit

Permalink
Add is_non_null to pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
workingjubilee committed Feb 5, 2024
1 parent 395c975 commit caac2ee
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
1 change: 1 addition & 0 deletions pgrx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub use once_cell;

/// Not ready for public exposure.
mod layout;
mod ptr;
mod slice;
mod toast;

Expand Down
9 changes: 5 additions & 4 deletions pgrx/src/list/flat_list.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 _);
Expand Down Expand Up @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions pgrx/src/list/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,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
Expand Down Expand Up @@ -328,7 +328,7 @@ unsafe fn cons_cell<T: Enlist>(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;
Expand Down Expand Up @@ -496,7 +496,7 @@ impl<T: Enlist> Iterator for RawCellIter<T> {

#[inline]
fn next(&mut self) -> Option<T> {
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 {
Expand Down
15 changes: 15 additions & 0 deletions pgrx/src/ptr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pub(crate) trait PointerExt {
fn is_non_null(&self) -> bool;
}

impl<T> PointerExt for *mut T {
fn is_non_null(&self) -> bool {
!self.is_null()
}
}

impl<T> PointerExt for *const T {
fn is_non_null(&self) -> bool {
!self.is_null()
}
}

0 comments on commit caac2ee

Please sign in to comment.