diff --git a/Cargo.toml b/Cargo.toml index 131beea..b1bba77 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ license = "MIT" repository = "https://github.com/neoeinstein/stats_alloc" documentation = "https://docs.rs/stats_alloc/" readme = "README.md" +edition = "2021" exclude = [ ".gitignore", ".editorconfig", @@ -15,11 +16,3 @@ exclude = [ "rustfmt.toml", "tests/**/*", ] - -[features] -default = [] -nightly = [] - -[package.metadata.docs.rs] -all-features = true -rustdoc-args = ["--cfg", "doc_cfg"] diff --git a/README.md b/README.md index 01292aa..4bb29d0 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,11 @@ 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 = &INSTRUMENTED_SYSTEM; +static GLOBAL: StatsAlloc = StatsAlloc::system(); fn example_using_region() { let reg = Region::new(&GLOBAL); @@ -21,19 +21,6 @@ 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); } -``` - -## 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 = StatsAlloc::new(MyCustomAllocator::new()); ``` diff --git a/src/lib.rs b/src/lib.rs index faa6225..abdfcf8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,3 @@ -//! //! 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. @@ -6,13 +5,11 @@ //! ## 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 = &INSTRUMENTED_SYSTEM; +//! static GLOBAL: StatsAlloc = StatsAlloc::system(); //! //! fn main() { //! let reg = Region::new(&GLOBAL); @@ -89,17 +86,6 @@ pub struct Stats { pub bytes_reallocated: isize, } -/// An instrumented instance of the system allocator. -pub static INSTRUMENTED_SYSTEM: StatsAlloc = 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 { /// Provides access to an instrumented instance of the system allocator. pub const fn system() -> Self { @@ -118,7 +104,6 @@ impl StatsAlloc { impl StatsAlloc { /// 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), @@ -131,21 +116,6 @@ impl StatsAlloc { } } - /// 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 { @@ -267,6 +237,7 @@ unsafe impl GlobalAlloc for StatsAlloc { 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() { diff --git a/tests/test.rs b/tests/test.rs deleted file mode 100644 index 30d6c5b..0000000 --- a/tests/test.rs +++ /dev/null @@ -1,17 +0,0 @@ -extern crate stats_alloc; - -use stats_alloc::{Region, StatsAlloc, INSTRUMENTED_SYSTEM}; -use std::alloc::System; - -#[global_allocator] -static GLOBAL: &StatsAlloc = &INSTRUMENTED_SYSTEM; - -#[test] -fn example_using_region() { - let reg = Region::new(&GLOBAL); - let x: Vec = 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); -} diff --git a/tests/nightly.rs b/tests/tests.rs similarity index 81% rename from tests/nightly.rs rename to tests/tests.rs index 5c480d1..aa36096 100644 --- a/tests/nightly.rs +++ b/tests/tests.rs @@ -1,7 +1,3 @@ -#![cfg(feature = "nightly")] - -extern crate stats_alloc; - use stats_alloc::{Region, StatsAlloc}; use std::alloc::System; @@ -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); }