mirror of
https://github.com/bspeice/qadapt
synced 2024-11-21 13:28:11 -05:00
Takes us 576 allocations to get up and running
I need a better way to handle initialization costs...
This commit is contained in:
parent
a489f71ae2
commit
aafec768ca
@ -5,7 +5,7 @@ authors = ["Bradlee Speice <bradlee@speice.io>"]
|
|||||||
description = "The Quick And Dirty Allocation Profiling Tool"
|
description = "The Quick And Dirty Allocation Profiling Tool"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
lazy_static = "1.1"
|
backtrace = "0.3"
|
||||||
|
|
||||||
[dependencies.libc]
|
[dependencies.libc]
|
||||||
default-features = false
|
default-features = false
|
||||||
|
12
src/lib.rs
12
src/lib.rs
@ -1,5 +1,7 @@
|
|||||||
|
extern crate backtrace;
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
|
|
||||||
|
use backtrace::Backtrace;
|
||||||
use libc::c_void;
|
use libc::c_void;
|
||||||
use libc::free;
|
use libc::free;
|
||||||
use libc::malloc;
|
use libc::malloc;
|
||||||
@ -11,12 +13,13 @@ use std::sync::atomic::Ordering;
|
|||||||
mod const_init;
|
mod const_init;
|
||||||
use const_init::ConstInit;
|
use const_init::ConstInit;
|
||||||
|
|
||||||
|
static mut INIT_ALLOCATIONS: u32 = 576;
|
||||||
|
|
||||||
pub struct QADAPT {
|
pub struct QADAPT {
|
||||||
pub has_allocated: AtomicBool
|
pub has_allocated: AtomicBool
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConstInit for QADAPT {
|
impl ConstInit for QADAPT {
|
||||||
|
|
||||||
const INIT: QADAPT = QADAPT {
|
const INIT: QADAPT = QADAPT {
|
||||||
has_allocated: AtomicBool::new(false)
|
has_allocated: AtomicBool::new(false)
|
||||||
};
|
};
|
||||||
@ -25,7 +28,12 @@ impl ConstInit for QADAPT {
|
|||||||
unsafe impl GlobalAlloc for QADAPT {
|
unsafe impl GlobalAlloc for QADAPT {
|
||||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||||
let block = malloc(layout.size()) as *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
|
block
|
||||||
}
|
}
|
||||||
|
14
tests/initial.rs
Normal file
14
tests/initial.rs
Normal 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));
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user