Takes us 576 allocations to get up and running

I need a better way to handle initialization costs...
pull/3/head
Bradlee Speice 2018-09-22 16:13:36 -04:00
parent a489f71ae2
commit aafec768ca
3 changed files with 25 additions and 3 deletions

View File

@ -5,7 +5,7 @@ authors = ["Bradlee Speice <bradlee@speice.io>"]
description = "The Quick And Dirty Allocation Profiling Tool"
[dependencies]
lazy_static = "1.1"
backtrace = "0.3"
[dependencies.libc]
default-features = false

View File

@ -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
}

14
tests/initial.rs Normal file
View File

@ -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));
}