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

Make ScopedAllocCounter public and add documentation #204

Merged
merged 5 commits into from
Jan 15, 2025
Merged
Changes from all commits
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
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
Loading