Skip to content

Commit

Permalink
Make ScopedAllocCounter public and add documentation (#204)
Browse files Browse the repository at this point in the history
* Made ScopedAllocCounter public

* Added Doctest example

* Cargo fmt and remove test

---------

Co-authored-by: Guillaume Binet <[email protected]>
  • Loading branch information
Zac8668 and gbin authored Jan 15, 2025
1 parent 024b248 commit d5aa7e7
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions core/cu29_runtime/src/monitoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit d5aa7e7

Please sign in to comment.