1
0
mirror of https://github.com/bspeice/qadapt synced 2024-11-24 06:48:09 -05:00

Rustfmt run

This commit is contained in:
Bradlee Speice 2018-11-10 21:59:39 -05:00
parent 1faa353706
commit 4e16b79486
3 changed files with 54 additions and 37 deletions

View File

@ -1,15 +1,15 @@
//! The Quick And Dirty Allocation Profiling Tool //! The Quick And Dirty Allocation Profiling Tool
//! //!
//! This allocator is a helper for writing high-performance code that is allocation/drop free; //! 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 //! 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 //! happen during their execution (and execution of any functions they call) and throw a
//! thread panic if this occurs. //! thread panic if this occurs.
//! //!
//! Because QADAPT panics on allocation and is rather slow (for an allocator) it is **strongly** //! 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 //! 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, //! `#[allocate_panic]` will have no side effects if the QADAPT allocator is not being used,
//! so the attribute is safe to leave everywhere. //! so the attribute is safe to leave everywhere.
//! //!
//! Currently this crate is Nightly-only, but will work once `const fn` is in Stable. //! Currently this crate is Nightly-only, but will work once `const fn` is in Stable.
#![deny(missing_docs)] #![deny(missing_docs)]
extern crate libc; extern crate libc;
@ -26,8 +26,8 @@ use libc::c_void;
use libc::free; use libc::free;
use libc::malloc; use libc::malloc;
use spin::RwLock; use spin::RwLock;
use std::alloc::Layout;
use std::alloc::GlobalAlloc; use std::alloc::GlobalAlloc;
use std::alloc::Layout;
use std::thread; use std::thread;
thread_local! { thread_local! {
@ -41,30 +41,34 @@ pub struct QADAPT;
/// panics should be triggered if allocations/drops happen while we are running. /// panics should be triggered if allocations/drops happen while we are running.
pub fn enter_protected() { pub fn enter_protected() {
if thread::panicking() { if thread::panicking() {
return return;
} }
PROTECTION_LEVEL.try_with(|v| { PROTECTION_LEVEL
*v.write() += 1; .try_with(|v| {
}).unwrap_or_else(|_e| ()); *v.write() += 1;
})
.unwrap_or_else(|_e| ());
} }
/// Let QADAPT know that we are exiting a protected region. Will panic /// Let QADAPT know that we are exiting a protected region. Will panic
/// if we attempt to [`exit_protected`] more times than we [`enter_protected`]. /// if we attempt to [`exit_protected`] more times than we [`enter_protected`].
pub fn exit_protected() { pub fn exit_protected() {
if thread::panicking() { if thread::panicking() {
return return;
} }
PROTECTION_LEVEL.try_with(|v| { PROTECTION_LEVEL
let val = { *v.read() }; .try_with(|v| {
match val { let val = { *v.read() };
v if v == 0 => panic!("Attempt to exit protected too many times"), match val {
_ => { v if v == 0 => panic!("Attempt to exit protected too many times"),
*v.write() -= 1; _ => {
*v.write() -= 1;
}
} }
} })
}).unwrap_or_else(|_e| ()); .unwrap_or_else(|_e| ());
} }
static INTERNAL_ALLOCATION: RwLock<usize> = RwLock::new(usize::max_value()); static INTERNAL_ALLOCATION: RwLock<usize> = RwLock::new(usize::max_value());
@ -74,9 +78,9 @@ unsafe fn claim_internal_alloc() {
match INTERNAL_ALLOCATION.write() { match INTERNAL_ALLOCATION.write() {
ref mut lock if **lock == usize::max_value() => { ref mut lock if **lock == usize::max_value() => {
**lock = thread_id::get(); **lock = thread_id::get();
break break;
}, }
_ => () _ => (),
} }
} }
} }
@ -84,7 +88,7 @@ unsafe fn claim_internal_alloc() {
unsafe fn release_internal_alloc() { unsafe fn release_internal_alloc() {
match INTERNAL_ALLOCATION.write() { match INTERNAL_ALLOCATION.write() {
ref mut lock if **lock == thread_id::get() => **lock = usize::max_value(), ref mut lock if **lock == thread_id::get() => **lock = usize::max_value(),
_ => panic!("Internal allocation tracking error") _ => panic!("Internal allocation tracking error"),
} }
} }
@ -100,10 +104,11 @@ unsafe impl GlobalAlloc for QADAPT {
return malloc(layout.size()) as *mut u8; return malloc(layout.size()) as *mut u8;
} }
// Because accessing PROTECTION_LEVEL has the potential to trigger an allocation, // Because accessing PROTECTION_LEVEL has the potential to trigger an allocation,
// we need to spin until we can claim the INTERNAL_ALLOCATION lock for our thread. // we need to spin until we can claim the INTERNAL_ALLOCATION lock for our thread.
claim_internal_alloc(); claim_internal_alloc();
let protection_level: Result<usize, ()> = PROTECTION_LEVEL.try_with(|v| *v.read()).or(Ok(0)); let protection_level: Result<usize, ()> =
PROTECTION_LEVEL.try_with(|v| *v.read()).or(Ok(0));
release_internal_alloc(); release_internal_alloc();
match protection_level { match protection_level {
@ -112,9 +117,13 @@ unsafe impl GlobalAlloc for QADAPT {
// Tripped a bad allocation, but make sure further memory access during unwind // Tripped a bad allocation, but make sure further memory access during unwind
// doesn't have issues // doesn't have issues
PROTECTION_LEVEL.with(|v| *v.write() = 0); PROTECTION_LEVEL.with(|v| *v.write() = 0);
panic!("Unexpected allocation for size {}, protection level: {}", layout.size(), v) panic!(
}, "Unexpected allocation for size {}, protection level: {}",
Err(_) => unreachable!() layout.size(),
v
)
}
Err(_) => unreachable!(),
} }
} }
@ -124,7 +133,8 @@ unsafe impl GlobalAlloc for QADAPT {
} }
claim_internal_alloc(); claim_internal_alloc();
let protection_level: Result<usize, ()> = PROTECTION_LEVEL.try_with(|v| *v.read()).or(Ok(0)); let protection_level: Result<usize, ()> =
PROTECTION_LEVEL.try_with(|v| *v.read()).or(Ok(0));
release_internal_alloc(); release_internal_alloc();
// Free before checking panic to make sure we avoid leaks // Free before checking panic to make sure we avoid leaks
@ -134,9 +144,13 @@ unsafe impl GlobalAlloc for QADAPT {
// Tripped a bad dealloc, but make sure further memory access during unwind // Tripped a bad dealloc, but make sure further memory access during unwind
// doesn't have issues // doesn't have issues
PROTECTION_LEVEL.with(|v| *v.write() = 0); PROTECTION_LEVEL.with(|v| *v.write() = 0);
panic!("Unexpected deallocation for size {}, protection level: {}", layout.size(), v) panic!(
}, "Unexpected deallocation for size {}, protection level: {}",
_ => () layout.size(),
v
)
}
_ => (),
} }
} }
} }

View File

@ -1,16 +1,16 @@
#![feature(asm)] #![feature(asm)]
extern crate qadapt; extern crate qadapt;
use qadapt::QADAPT;
use qadapt::enter_protected; use qadapt::enter_protected;
use qadapt::exit_protected; use qadapt::exit_protected;
use qadapt::QADAPT;
#[global_allocator] #[global_allocator]
static Q: QADAPT = QADAPT; static Q: QADAPT = QADAPT;
pub fn black_box<T>(dummy: T) -> T { pub fn black_box<T>(dummy: T) -> T {
// Taken from test lib, need to mark the arg as non-introspectable // Taken from test lib, need to mark the arg as non-introspectable
unsafe {asm!("" : : "r"(&dummy))} unsafe { asm!("" : : "r"(&dummy)) }
dummy dummy
} }
@ -41,10 +41,14 @@ fn unit_result(b: bool) -> Result<(), ()> {
fn test_unit_result() { fn test_unit_result() {
enter_protected(); enter_protected();
#[allow(unused)] #[allow(unused)]
{ black_box(unit_result(true)); } {
black_box(unit_result(true));
}
black_box(unit_result(true)).unwrap(); black_box(unit_result(true)).unwrap();
#[allow(unused)] #[allow(unused)]
{ black_box(unit_result(false)); } {
black_box(unit_result(false));
}
black_box(unit_result(false)).unwrap_err(); black_box(unit_result(false)).unwrap_err();
exit_protected(); exit_protected();
} }
@ -94,4 +98,4 @@ fn exit_too_often() {
enter_protected(); enter_protected();
exit_protected(); exit_protected();
exit_protected(); exit_protected();
} }

View File

@ -2,7 +2,6 @@ extern crate qadapt;
use qadapt::allocate_panic; use qadapt::allocate_panic;
#[allocate_panic] #[allocate_panic]
fn allocates() { fn allocates() {
let _v: Vec<()> = Vec::with_capacity(1); let _v: Vec<()> = Vec::with_capacity(1);
@ -24,4 +23,4 @@ fn test_no_allocate() {
fn test_allocates() { fn test_allocates() {
allocates(); allocates();
} }
*/ */