From a8d40f4e65926718480b64d23d947c9c0ac50cff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Thu, 18 Jan 2024 11:05:31 +0100 Subject: [PATCH 1/5] Remove the nightly feature --- Cargo.toml | 8 -------- src/lib.rs | 17 ----------------- tests/test.rs | 17 ----------------- tests/{nightly.rs => tests.rs} | 4 +--- 4 files changed, 1 insertion(+), 45 deletions(-) delete mode 100644 tests/test.rs rename tests/{nightly.rs => tests.rs} (87%) diff --git a/Cargo.toml b/Cargo.toml index 131beea..9ebf5d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,11 +15,3 @@ exclude = [ "rustfmt.toml", "tests/**/*", ] - -[features] -default = [] -nightly = [] - -[package.metadata.docs.rs] -all-features = true -rustdoc-args = ["--cfg", "doc_cfg"] diff --git a/src/lib.rs b/src/lib.rs index faa6225..da4ace6 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. @@ -118,7 +117,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 +129,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 { 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 87% rename from tests/nightly.rs rename to tests/tests.rs index 5c480d1..7cf37b2 100644 --- a/tests/nightly.rs +++ b/tests/tests.rs @@ -1,5 +1,3 @@ -#![cfg(feature = "nightly")] - extern crate stats_alloc; use stats_alloc::{Region, StatsAlloc}; @@ -15,5 +13,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); } From d258c25bf4ec80fceb41ea3ac9b478f93300c2aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Thu, 18 Jan 2024 11:11:55 +0100 Subject: [PATCH 2/5] Move to edition 2021 --- Cargo.toml | 1 + src/lib.rs | 2 -- tests/tests.rs | 2 -- 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9ebf5d2..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", diff --git a/src/lib.rs b/src/lib.rs index da4ace6..e7a1aeb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,8 +5,6 @@ //! ## Example //! //! ``` -//! extern crate stats_alloc; -//! //! use stats_alloc::{Region, StatsAlloc, INSTRUMENTED_SYSTEM}; //! use std::alloc::System; //! diff --git a/tests/tests.rs b/tests/tests.rs index 7cf37b2..aa36096 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,5 +1,3 @@ -extern crate stats_alloc; - use stats_alloc::{Region, StatsAlloc}; use std::alloc::System; From 1c1e3ac7e6924b29e491a76da8494efe6dbfab9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Thu, 18 Jan 2024 11:13:22 +0100 Subject: [PATCH 3/5] Remove the useless INSTRUMENTED_SYSTEM --- src/lib.rs | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e7a1aeb..360e238 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,11 +5,11 @@ //! ## Example //! //! ``` -//! 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); @@ -86,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 { From 6f83c52160c7d0550fdf770e1f73d239b0ff9a97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Thu, 18 Jan 2024 11:17:04 +0100 Subject: [PATCH 4/5] Make clippy happy --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index 360e238..abdfcf8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -237,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() { From 3d1f6f6ee25ed680e1fa77045a141aebddee2339 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Thu, 18 Jan 2024 16:14:13 +0100 Subject: [PATCH 5/5] Remove the warnings about custom allocator stability --- README.md | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) 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()); ```