Skip to content
Open
Show file tree
Hide file tree
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
9 changes: 1 addition & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,11 @@ license = "MIT"
repository = "https://github.com/neoeinstein/stats_alloc"
documentation = "https://docs.rs/stats_alloc/"
readme = "README.md"
edition = "2021"
exclude = [
".gitignore",
".editorconfig",
"release.toml",
"rustfmt.toml",
"tests/**/*",
]

[features]
default = []
nightly = []

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "doc_cfg"]
19 changes: 3 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,18 @@ production loads to monitor for memory leaks.
```rust
extern crate stats_alloc;

use stats_alloc::{StatsAlloc, Region, INSTRUMENTED_SYSTEM};
use stats_alloc::{StatsAlloc, Region};
use std::alloc::System;

#[global_allocator]
static GLOBAL: &StatsAlloc<System> = &INSTRUMENTED_SYSTEM;
static GLOBAL: StatsAlloc<System> = StatsAlloc::system();

fn example_using_region() {
let reg = Region::new(&GLOBAL);
let x: Vec<u8> = Vec::with_capacity(1_024);
println!("Stats at 1: {:#?}", reg.change());
// Used here to ensure that the value is not
// dropped before we check the statistics
::std::mem::size_of_val(&x);
let _ = std::mem::size_of_val(&x);
}
```

## Custom allocators

Currenty wrapping a custom allocator requires the use of the nightly compiler
and compiling with the "nightly" feature due to the soon to stabilize use of
the unstable `const_fn_trait_bound` and the fact that the internals of the
instrumenting type are not public. If that's fine with you, a custom allocator
can be wrapped as follows:

```rust
#[global_allocator]
static GLOBAL: StatsAlloc<System> = StatsAlloc::new(MyCustomAllocator::new());
```
35 changes: 3 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
//!
//! An instrumenting middleware for global allocators in Rust, useful in testing
//! for validating assumptions regarding allocation patterns, and potentially in
//! production loads to monitor for memory leaks.
//!
//! ## Example
//!
//! ```
//! extern crate stats_alloc;
//!
//! use stats_alloc::{Region, StatsAlloc, INSTRUMENTED_SYSTEM};
//! use stats_alloc::{Region, StatsAlloc};
//! use std::alloc::System;
//!
//! #[global_allocator]
//! static GLOBAL: &StatsAlloc<System> = &INSTRUMENTED_SYSTEM;
//! static GLOBAL: StatsAlloc<System> = StatsAlloc::system();
//!
//! fn main() {
//! let reg = Region::new(&GLOBAL);
Expand Down Expand Up @@ -89,17 +86,6 @@ pub struct Stats {
pub bytes_reallocated: isize,
}

/// An instrumented instance of the system allocator.
pub static INSTRUMENTED_SYSTEM: StatsAlloc<System> = StatsAlloc {
allocations: AtomicUsize::new(0),
deallocations: AtomicUsize::new(0),
reallocations: AtomicUsize::new(0),
bytes_allocated: AtomicUsize::new(0),
bytes_deallocated: AtomicUsize::new(0),
bytes_reallocated: AtomicIsize::new(0),
inner: System,
};

impl StatsAlloc<System> {
/// Provides access to an instrumented instance of the system allocator.
pub const fn system() -> Self {
Expand All @@ -118,7 +104,6 @@ impl StatsAlloc<System> {
impl<T: GlobalAlloc> StatsAlloc<T> {
/// Provides access to an instrumented instance of the given global
/// allocator.
#[cfg(feature = "nightly")]
pub const fn new(inner: T) -> Self {
StatsAlloc {
allocations: AtomicUsize::new(0),
Expand All @@ -131,21 +116,6 @@ impl<T: GlobalAlloc> StatsAlloc<T> {
}
}

/// Provides access to an instrumented instance of the given global
/// allocator.
#[cfg(not(feature = "nightly"))]
pub fn new(inner: T) -> Self {
StatsAlloc {
allocations: AtomicUsize::new(0),
deallocations: AtomicUsize::new(0),
reallocations: AtomicUsize::new(0),
bytes_allocated: AtomicUsize::new(0),
bytes_deallocated: AtomicUsize::new(0),
bytes_reallocated: AtomicIsize::new(0),
inner,
}
}

/// Takes a snapshot of the current view of the allocator statistics.
pub fn stats(&self) -> Stats {
Stats {
Expand Down Expand Up @@ -267,6 +237,7 @@ unsafe impl<T: GlobalAlloc> GlobalAlloc for StatsAlloc<T> {
self.inner.alloc_zeroed(layout)
}

#[allow(clippy::comparison_chain)]
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
self.reallocations.fetch_add(1, Ordering::SeqCst);
if new_size > layout.size() {
Expand Down
17 changes: 0 additions & 17 deletions tests/test.rs

This file was deleted.

6 changes: 1 addition & 5 deletions tests/nightly.rs → tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#![cfg(feature = "nightly")]

extern crate stats_alloc;

use stats_alloc::{Region, StatsAlloc};
use std::alloc::System;

Expand All @@ -15,5 +11,5 @@ fn example_using_region() {
println!("Stats at 1: {:#?}", reg.change());
// Used here to ensure that the value is not
// dropped before we check the statistics
::std::mem::size_of_val(&x);
let _ = std::mem::size_of_val(&x);
}