From 901e7941a2b25a957f6aca2658a1ccdd0a5101bd Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sun, 11 Nov 2018 22:34:05 -0500 Subject: [PATCH] Final cleanup, time to commit --- Cargo.toml | 9 +++++++-- README.md | 15 +++++++++++---- qadapt-macro/src/lib.rs | 16 ++++++++-------- src/lib.rs | 2 +- tests/macros.rs | 5 +++-- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1807673..ae033b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,12 +5,17 @@ authors = ["Bradlee Speice "] description = "The Quick And Dirty Allocation Profiling Tool" license = "Apache-2.0" readme = "README.md" -categories = ["allocator", "nostd"] +categories = [ + "development-tools", + "development-tools::debugging", + "development-tools::profiling", + "development-tools::testing", + "memory-management" +] repository = "https://github.com/bspeice/qadapt.git" [dependencies] libc = "0.2" -log = "0.4" spin = "0.4" thread-id = "3.3" diff --git a/README.md b/README.md index ca91948..a759da9 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,14 @@ The Quick And Dirty Allocation Profiling Tool ============================================= -A simple attempt at an allocator that can let you know if allocations -are happening in places you didn't intend. This is primarily used for -guaranteeing that performance-critical code doesn't trigger an allocation -while running. +This allocator is a helper for writing high-performance code that is allocation/drop free; +for functions annotated with `#[allocate_panic]`, QADAPT will detect when allocations/drops +happen during their execution (and execution of any functions they call) and throw a +thread panic if this occurs. + +Because QADAPT panics on allocation and is rather slow (for an allocator) it is **strongly** +recommended that QADAPT (the allocator) be used only in code tests. Functions annotated with +`#[allocate_panic]` will have no side effects if the QADAPT allocator is not being used, +so the attribute is safe to leave everywhere. + +Currently this crate is Nightly-only, but will work once `const fn` is in Stable. \ No newline at end of file diff --git a/qadapt-macro/src/lib.rs b/qadapt-macro/src/lib.rs index 300c485..6470ff6 100644 --- a/qadapt-macro/src/lib.rs +++ b/qadapt-macro/src/lib.rs @@ -1,17 +1,17 @@ //! Helper macros to use with the QADAPT allocator system -//! +//! //! This crate is intended for managing the QADAPT allocator, //! and is unusable on its own. -//! +//! // TODO: This causes issues, but I can't track down why // #![deny(missing_docs)] extern crate proc_macro; -use proc_macro::TokenStream; -use proc_macro::TokenTree; +use proc_macro::Delimiter; use proc_macro::Spacing; use proc_macro::Span; -use proc_macro::Delimiter; +use proc_macro::TokenStream; +use proc_macro::TokenTree; use std::iter::FromIterator; type TT = proc_macro::TokenTree; @@ -46,7 +46,7 @@ fn protected_group(fn_body: G) -> TokenTree { /// Set up the QADAPT allocator to trigger a panic if any allocations happen during /// calls to this function. -/// +/// /// QADAPT will only track allocations in the thread that calls this function; /// if (for example) this function receives the results of an allocation in a /// separate thread, QADAPT will not trigger a panic. @@ -61,8 +61,8 @@ pub fn allocate_panic(_attr: TokenStream, item: TokenStream) -> TokenStream { TokenTree::Group(ref g) if g.delimiter() == Delimiter::Brace => { fn_body = Some(g.clone()); break; - }, - tt => ret.push(tt) + } + tt => ret.push(tt), } } ret.push(protected_group(fn_body.unwrap())); diff --git a/src/lib.rs b/src/lib.rs index e617732..31afcfe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -75,7 +75,7 @@ static INTERNAL_ALLOCATION: RwLock = RwLock::new(usize::max_value()); /// Get the current "protection level" in QADAPT: calls to enter_protected() - exit_protected() pub fn protection_level() -> usize { - PROTECTION_LEVEL.try_with(|v| *v.read() ).unwrap_or(0) + PROTECTION_LEVEL.try_with(|v| *v.read()).unwrap_or(0) } fn claim_internal_alloc() { diff --git a/tests/macros.rs b/tests/macros.rs index edb6d54..3d19ff1 100644 --- a/tests/macros.rs +++ b/tests/macros.rs @@ -1,7 +1,7 @@ extern crate qadapt; -use qadapt::QADAPT; use qadapt::allocate_panic; +use qadapt::QADAPT; #[global_allocator] static Q: QADAPT = QADAPT; @@ -9,7 +9,8 @@ static Q: QADAPT = QADAPT; #[allocate_panic] fn allocates() { assert_eq!(::qadapt::protection_level(), 1); - let mut v = Vec::new(); + // Without boxing, release profile can actually optimize out the allocation + let mut v = Box::new(Vec::new()); v.push(1); }