From 18d1c3e6d14e93f78ab6d39bcfbe16d8043f2cd1 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Tue, 6 Nov 2018 23:04:26 -0500 Subject: [PATCH] Start work on a procedural macro Turns out the system still needs some work. --- Cargo.toml | 4 +++- qadapt-macro/.gitignore | 3 +++ qadapt-macro/Cargo.toml | 9 +++++++++ qadapt-macro/src/lib.rs | 31 +++++++++++++++++++++++++++++ src/lib.rs | 4 ++++ tests/{simple.rs => allocations.rs} | 1 - 6 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 qadapt-macro/.gitignore create mode 100644 qadapt-macro/Cargo.toml create mode 100644 qadapt-macro/src/lib.rs rename tests/{simple.rs => allocations.rs} (99%) diff --git a/Cargo.toml b/Cargo.toml index 68ce1f5..8b19f40 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,6 @@ repository = "https://github.com/bspeice/qadapt.git" backtrace = "0.3" libc = "0.2" log = "0.4" -spin = "0.4" \ No newline at end of file +spin = "0.4" + +qadapt-macro = { path = "./qadapt-macro" } \ No newline at end of file diff --git a/qadapt-macro/.gitignore b/qadapt-macro/.gitignore new file mode 100644 index 0000000..6936990 --- /dev/null +++ b/qadapt-macro/.gitignore @@ -0,0 +1,3 @@ +/target +**/*.rs.bk +Cargo.lock diff --git a/qadapt-macro/Cargo.toml b/qadapt-macro/Cargo.toml new file mode 100644 index 0000000..0342b5a --- /dev/null +++ b/qadapt-macro/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "qadapt-macro" +version = "0.1.0" +authors = ["Bradlee Speice "] + +[lib] +proc-macro = true + +[dependencies] diff --git a/qadapt-macro/src/lib.rs b/qadapt-macro/src/lib.rs new file mode 100644 index 0000000..47512b4 --- /dev/null +++ b/qadapt-macro/src/lib.rs @@ -0,0 +1,31 @@ +//! Helper macros to use with the QADAPT allocator system +//! +//! This crate is unusable on its own, but because `proc_macro` crates +//! can't export non-proc-macro symbols, we have to go through an extra step +//! to move these up. +//! +//! Ultimately, this does actually work because we don't need to actually use +//! references to the underlying functionality here, we just need to know +//! where they will ultimately end up at. +// TODO: This causes issues, but I can't track down why +// #![deny(missing_docs)] +extern crate proc_macro; + +use proc_macro::TokenTree; +use proc_macro::TokenStream; + +/// Set up the QADAPT allocator to trigger a panic if any allocations happen during +/// this function. Race conditions between threads are not checked, this macro will +/// only work as intended in a single-threaded or otherwise synchronized environment. +#[proc_macro_attribute] +pub fn allocate_panic(_attr: TokenStream, item: TokenStream) -> TokenStream { + let ret = item.clone(); + + let mut token_iter = item.into_iter(); + match token_iter.next() { + Some(TokenTree::Ident(ref i)) if i.to_string() == "fn" => (), + _ => panic!("#[allocate_panic] macro can only be applied to functions") + } + + ret +} diff --git a/src/lib.rs b/src/lib.rs index b8251f8..ccfdfd4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,13 @@ extern crate backtrace; extern crate libc; +extern crate qadapt_macro; #[macro_use] extern crate log; extern crate spin; +// Re-export the proc macros to use by other code +pub use qadapt_macro::*; + use backtrace::Backtrace; use libc::c_void; use libc::free; diff --git a/tests/simple.rs b/tests/allocations.rs similarity index 99% rename from tests/simple.rs rename to tests/allocations.rs index bc09bb1..327444d 100644 --- a/tests/simple.rs +++ b/tests/allocations.rs @@ -1,5 +1,4 @@ #![feature(asm)] - extern crate qadapt; use qadapt::QADAPT;