Rustfmt run

pull/3/head
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

@ -26,8 +26,8 @@ use libc::c_void;
use libc::free;
use libc::malloc;
use spin::RwLock;
use std::alloc::Layout;
use std::alloc::GlobalAlloc;
use std::alloc::Layout;
use std::thread;
thread_local! {
@ -41,22 +41,25 @@ pub struct QADAPT;
/// panics should be triggered if allocations/drops happen while we are running.
pub fn enter_protected() {
if thread::panicking() {
return
return;
}
PROTECTION_LEVEL.try_with(|v| {
PROTECTION_LEVEL
.try_with(|v| {
*v.write() += 1;
}).unwrap_or_else(|_e| ());
})
.unwrap_or_else(|_e| ());
}
/// Let QADAPT know that we are exiting a protected region. Will panic
/// if we attempt to [`exit_protected`] more times than we [`enter_protected`].
pub fn exit_protected() {
if thread::panicking() {
return
return;
}
PROTECTION_LEVEL.try_with(|v| {
PROTECTION_LEVEL
.try_with(|v| {
let val = { *v.read() };
match val {
v if v == 0 => panic!("Attempt to exit protected too many times"),
@ -64,7 +67,8 @@ pub fn exit_protected() {
*v.write() -= 1;
}
}
}).unwrap_or_else(|_e| ());
})
.unwrap_or_else(|_e| ());
}
static INTERNAL_ALLOCATION: RwLock<usize> = RwLock::new(usize::max_value());
@ -74,9 +78,9 @@ unsafe fn claim_internal_alloc() {
match INTERNAL_ALLOCATION.write() {
ref mut lock if **lock == usize::max_value() => {
**lock = thread_id::get();
break
},
_ => ()
break;
}
_ => (),
}
}
}
@ -84,7 +88,7 @@ unsafe fn claim_internal_alloc() {
unsafe fn release_internal_alloc() {
match INTERNAL_ALLOCATION.write() {
ref mut lock if **lock == thread_id::get() => **lock = usize::max_value(),
_ => panic!("Internal allocation tracking error")
_ => panic!("Internal allocation tracking error"),
}
}
@ -103,7 +107,8 @@ unsafe impl GlobalAlloc for QADAPT {
// 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.
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();
match protection_level {
@ -112,9 +117,13 @@ unsafe impl GlobalAlloc for QADAPT {
// Tripped a bad allocation, but make sure further memory access during unwind
// doesn't have issues
PROTECTION_LEVEL.with(|v| *v.write() = 0);
panic!("Unexpected allocation for size {}, protection level: {}", layout.size(), v)
},
Err(_) => unreachable!()
panic!(
"Unexpected allocation for size {}, protection level: {}",
layout.size(),
v
)
}
Err(_) => unreachable!(),
}
}
@ -124,7 +133,8 @@ unsafe impl GlobalAlloc for QADAPT {
}
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();
// 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
// doesn't have issues
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,9 +1,9 @@
#![feature(asm)]
extern crate qadapt;
use qadapt::QADAPT;
use qadapt::enter_protected;
use qadapt::exit_protected;
use qadapt::QADAPT;
#[global_allocator]
static Q: QADAPT = QADAPT;
@ -41,10 +41,14 @@ fn unit_result(b: bool) -> Result<(), ()> {
fn test_unit_result() {
enter_protected();
#[allow(unused)]
{ black_box(unit_result(true)); }
{
black_box(unit_result(true));
}
black_box(unit_result(true)).unwrap();
#[allow(unused)]
{ black_box(unit_result(false)); }
{
black_box(unit_result(false));
}
black_box(unit_result(false)).unwrap_err();
exit_protected();
}

View File

@ -2,7 +2,6 @@ extern crate qadapt;
use qadapt::allocate_panic;
#[allocate_panic]
fn allocates() {
let _v: Vec<()> = Vec::with_capacity(1);