From aafec768cad31ae1da2bd4535c9f90d1b45486f2 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sat, 22 Sep 2018 16:13:36 -0400 Subject: [PATCH] Takes us 576 allocations to get up and running I need a better way to handle initialization costs... --- Cargo.toml | 2 +- src/lib.rs | 12 ++++++++++-- tests/initial.rs | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 tests/initial.rs diff --git a/Cargo.toml b/Cargo.toml index 890b0b6..91f6340 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Bradlee Speice "] description = "The Quick And Dirty Allocation Profiling Tool" [dependencies] -lazy_static = "1.1" +backtrace = "0.3" [dependencies.libc] default-features = false diff --git a/src/lib.rs b/src/lib.rs index ca4a54f..c6f07c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,7 @@ +extern crate backtrace; extern crate libc; +use backtrace::Backtrace; use libc::c_void; use libc::free; use libc::malloc; @@ -11,12 +13,13 @@ use std::sync::atomic::Ordering; mod const_init; use const_init::ConstInit; +static mut INIT_ALLOCATIONS: u32 = 576; + pub struct QADAPT { pub has_allocated: AtomicBool } impl ConstInit for QADAPT { - const INIT: QADAPT = QADAPT { has_allocated: AtomicBool::new(false) }; @@ -25,7 +28,12 @@ impl ConstInit for QADAPT { unsafe impl GlobalAlloc for QADAPT { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { let block = malloc(layout.size()) as *mut u8; - self.has_allocated.store(true, Ordering::SeqCst); + + if INIT_ALLOCATIONS > 0 { + INIT_ALLOCATIONS -= 1; + } else { + self.has_allocated.store(true, Ordering::SeqCst); + } block } diff --git a/tests/initial.rs b/tests/initial.rs new file mode 100644 index 0000000..35856ba --- /dev/null +++ b/tests/initial.rs @@ -0,0 +1,14 @@ +extern crate qadapt; + +use qadapt::QADAPT; +use std::sync::atomic::Ordering; + +#[global_allocator] +static A: QADAPT = QADAPT::INIT; + +#[test] +fn init() { + // Make sure that we don't have any allocations at the start + // that pollute other tests + assert!(!A.has_allocated.load(Ordering::SeqCst)); +} \ No newline at end of file