Skip to content

Commit 5e87cce

Browse files
Change MemCx::alloc_bytes to return NonNull (pgcentralfoundation#2214)
Postgres will throw if it gets NULL here, so roll with that.
1 parent 5c0fc98 commit 5e87cce

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

pgrx/src/list/flat_list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl<'cx, T: Enlist> List<'cx, T> {
112112
// No silly reasoning, simply allocate ~2 cache lines for a list
113113
let list_size = 128;
114114
unsafe {
115-
let list: *mut pg_sys::List = mcx.alloc_bytes(list_size).cast();
115+
let list: *mut pg_sys::List = mcx.alloc_bytes(list_size).cast().as_ptr();
116116
assert!(list.is_non_null());
117117
(*list).type_ = T::LIST_TAG;
118118
(*list).max_length = ((list_size - mem::size_of::<pg_sys::List>())

pgrx/src/memcx.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ impl<'mcx> MemCx<'mcx> {
2929

3030
/// Allocate a raw byte buffer `size` bytes in length
3131
/// and returns a pointer to the new allocation.
32-
pub fn alloc_bytes(&self, size: usize) -> *mut u8 {
33-
unsafe { pg_sys::MemoryContextAlloc(self.ptr.as_ptr(), size).cast() }
32+
pub fn alloc_bytes(&self, size: usize) -> NonNull<u8> {
33+
let ptr = unsafe { pg_sys::MemoryContextAlloc(self.ptr.as_ptr(), size).cast() };
34+
// SAFETY: `pg_sys::MemoryContextAlloc` is not allowed to return NULL, it must error.
35+
unsafe { NonNull::new_unchecked(ptr) }
3436
}
3537

3638
/// Stores the current memory context, switches to *this* memory context,

0 commit comments

Comments
 (0)