1
0
mirror of https://github.com/bspeice/qadapt synced 2024-11-23 22:38:10 -05:00

Warning message if guards without allocator enabled

This commit is contained in:
Bradlee Speice 2018-11-22 11:19:31 -05:00
parent e68316527f
commit 095fd797c4
4 changed files with 40 additions and 2 deletions

View File

@ -19,7 +19,11 @@ maintenance = { status = "actively-developed" }
[dependencies]
libc = "0.2"
log = "0.4"
spin = "0.4"
thread-id = "3.3"
qadapt-macro = { version = "0.7.0", path = "./qadapt-macro" }
[dev-dependencies]
env_logger = "0.6"

View File

@ -14,6 +14,7 @@ fn does_allocate() -> Box<u8> {
fn main() {
// If you were to run `cargo run --example release_mode`, this program blows up.
// If, however, you ran `cargo run --release --example release_mode`,
// nothing interesting will happen.
// nothing interesting will happen since panic-related code is stripped
// for release builds.
does_allocate();
}

21
examples/setup_warning.rs Normal file
View File

@ -0,0 +1,21 @@
extern crate env_logger;
extern crate qadapt;
use qadapt::allocate_panic;
// Note that we're missing the `#[global_allocator]` attribute
#[allocate_panic]
fn does_allocate() -> Box<u8> {
Box::new(0)
}
fn main() {
// This code will warn that QADAPT isn't being used, but won't trigger a panic.
// Run with `RUST_LOG=warn cargo run --example setup_warning`
env_logger::init();
does_allocate();
// The warning will only trigger once though
does_allocate();
}

View File

@ -12,6 +12,8 @@
//! for some helper macros to make working with QADAPT a bit easier.
#![deny(missing_docs)]
extern crate libc;
#[macro_use]
extern crate log;
extern crate qadapt_macro;
extern crate spin;
// thread_id is necessary because `std::thread::current()` panics if we have not yet
@ -45,6 +47,11 @@ pub fn enter_protected() {
return;
}
if *IS_ACTIVE.read() == false {
*IS_ACTIVE.write() = true;
warn!("QADAPT not initialized when using allocation guards; please verify `#[global_allocator]` is set!");
}
PROTECTION_LEVEL
.try_with(|v| {
*v.write() += 1;
@ -76,6 +83,7 @@ pub fn exit_protected() {
}
}
static IS_ACTIVE: RwLock<bool> = RwLock::new(false);
static INTERNAL_ALLOCATION: RwLock<usize> = RwLock::new(usize::max_value());
/// Get the current "protection level" in QADAPT: calls to enter_protected() - exit_protected()
@ -115,6 +123,10 @@ fn alloc_immediate() -> bool {
unsafe impl GlobalAlloc for QADAPT {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
if *IS_ACTIVE.read() == false {
*IS_ACTIVE.write() = true;
}
// If we're attempting to allocate our PROTECTION_LEVEL thread local,
// just allow it through
if alloc_immediate() {