From 98c808ece63e8e7a45e9e0d99f6508058cb0365c Mon Sep 17 00:00:00 2001 From: Sage Griffin Date: Thu, 1 Oct 2026 11:41:31 -0600 Subject: [PATCH] Add `Owned::memory_allocated` This function returns the number of bytes allocated onto the memory context owned by that node. I'm not entirely clear if this is the amount of memory managed by the arena, or if its the amount of memory that's be palloc'd onto it, but this is literally the only thing resembling memory allocation stats that `libpg_query` links other than a function to print stats to stdout which we don't want --- src/mem.rs | 5 +++++ src/owned.rs | 8 ++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/mem.rs b/src/mem.rs index a495a44..96fdc75 100644 --- a/src/mem.rs +++ b/src/mem.rs @@ -44,6 +44,11 @@ impl MemoryContext { unsafe { raw::MemoryContextSwitchTo(prev) }; result } + + pub(crate) fn memory_allocated(&self) -> usize { + // SAFETY: `self.0` is always a valid memory context + unsafe { &*self.0 }.mem_allocated + } } // SAFETY: This will never have any parents, so we don't need to worry about diff --git a/src/owned.rs b/src/owned.rs index 761abdc..0f07c0d 100644 --- a/src/owned.rs +++ b/src/owned.rs @@ -6,7 +6,7 @@ use std::ops::Deref; /// An owned version of a node pub struct Owned { /// The memory context the node is allocated onto - _mem: mem::MemoryContext, + mem: mem::MemoryContext, /// A pointer to a node allocated onto [_mem] ptr: *mut raw::Node, _marker: PhantomData, @@ -21,11 +21,15 @@ unsafe impl Sync for Owned {} impl Owned { pub(crate) fn new(mem: mem::MemoryContext, ptr: *mut raw::Node) -> Self { Self { - _mem: mem, + mem, ptr, _marker: PhantomData, } } + + pub fn memory_allocated(&self) -> usize { + self.mem.memory_allocated() + } } impl Deref for Owned