Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up library with is_non_null #1528

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
7 changes: 4 additions & 3 deletions pgrx/src/list/linked_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 @@ -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
Expand Down Expand Up @@ -328,7 +329,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 +497,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()
}
}
Loading