From d5aa7e7b2e1480471d1559e99c33791e4667a87b Mon Sep 17 00:00:00 2001 From: Isaac Turci <78173025+Zac8668@users.noreply.github.com> Date: Wed, 15 Jan 2025 01:47:55 -0300 Subject: [PATCH] Make ScopedAllocCounter public and add documentation (#204) * Made ScopedAllocCounter public * Added Doctest example * Cargo fmt and remove test --------- Co-authored-by: Guillaume Binet --- core/cu29_runtime/src/monitoring.rs | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/core/cu29_runtime/src/monitoring.rs b/core/cu29_runtime/src/monitoring.rs index aaa816b8d..b44bf04e2 100644 --- a/core/cu29_runtime/src/monitoring.rs +++ b/core/cu29_runtime/src/monitoring.rs @@ -141,6 +141,37 @@ impl ScopedAllocCounter { bf_deallocated: GLOBAL.get_deallocated(), } } + + /// Returns the total number of bytes allocated in the current scope + /// since the creation of this `ScopedAllocCounter`. + /// + /// # Example + /// ``` + /// use cu29_runtime::monitoring::ScopedAllocCounter; + /// + /// let counter = ScopedAllocCounter::new(); + /// let _vec = vec![0u8; 1024]; + /// println!("Bytes allocated: {}", counter.get_allocated()); + /// ``` + pub fn get_allocated(&self) -> usize { + GLOBAL.get_allocated() - self.bf_allocated + } + + /// Returns the total number of bytes deallocated in the current scope + /// since the creation of this `ScopedAllocCounter`. + /// + /// # Example + /// ``` + /// use cu29_runtime::monitoring::ScopedAllocCounter; + /// + /// let counter = ScopedAllocCounter::new(); + /// let _vec = vec![0u8; 1024]; + /// drop(_vec); + /// println!("Bytes deallocated: {}", counter.get_deallocated()); + /// ``` + pub fn get_deallocated(&self) -> usize { + GLOBAL.get_deallocated() - self.bf_deallocated + } } /// Build a difference between the number of bytes allocated and deallocated in the scope at drop time.