From db6a2ff89b37b01de48f1f090405282c84859567 Mon Sep 17 00:00:00 2001 From: edef Date: Wed, 15 Apr 2015 22:25:52 -0400 Subject: [PATCH] factor Valgrind support out and expose it Now other Stack / StackSource implementations can use the same Valgrind code. Ref #3. --- build.rs | 2 +- src/lib.rs | 1 + src/os.rs | 19 ++++++------------- src/{platform.c => valgrind.c} | 0 src/valgrind.rs | 9 +++++++++ 5 files changed, 17 insertions(+), 14 deletions(-) rename src/{platform.c => valgrind.c} (100%) create mode 100644 src/valgrind.rs diff --git a/build.rs b/build.rs index 366c091..d7a9096 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,5 @@ extern crate gcc; fn main() { - gcc::compile_library("libcontext.a", &["src/platform.c"]); + gcc::compile_library("libvalgrind.a", &["src/valgrind.c"]); } diff --git a/src/lib.rs b/src/lib.rs index 23e8e07..0cf7d3e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,7 @@ mod std { pub use core::*; } pub mod context; pub mod stack; +pub mod valgrind; #[cfg(target_arch = "x86_64")] mod arch; diff --git a/src/os.rs b/src/os.rs index fd15961..8cfbccb 100644 --- a/src/os.rs +++ b/src/os.rs @@ -6,22 +6,14 @@ use self::std::io::Error as IoError; use self::libc::{c_void, size_t}; use core::ptr; use stack; - -#[allow(non_camel_case_types)] -type stack_id_t = u32; -extern "C" { - #[link_name = "lwt_stack_register"] - fn stack_register(start: *const u8, end: *const u8) -> stack_id_t; - #[link_name = "lwt_stack_deregister"] - fn stack_deregister(id: stack_id_t); -} +use valgrind; #[allow(raw_pointer_derive)] #[derive(Debug)] pub struct Stack { ptr: *mut u8, len: usize, - valgrind_id: stack_id_t + valgrind_id: valgrind::stack_id_t } pub struct StackSource; @@ -69,8 +61,9 @@ impl Stack { len, IoError::last_os_error()) } - let valgrind_id = stack_register(ptr.offset(len as isize) as *const _, - ptr as *const _); + let valgrind_id = + valgrind::stack_register(ptr.offset(len as isize) as *const _, + ptr as *const _); Stack { ptr: ptr as *mut u8, len: len, valgrind_id: valgrind_id } }; @@ -95,7 +88,7 @@ impl Stack { impl Drop for Stack { fn drop(&mut self) { unsafe { - stack_deregister(self.valgrind_id); + valgrind::stack_deregister(self.valgrind_id); if libc::munmap(self.ptr as *mut c_void, self.len as size_t) != 0 { panic!("munmap for stack {:p} of size {} failed: {:?}", self.ptr, self.len, IoError::last_os_error()) diff --git a/src/platform.c b/src/valgrind.c similarity index 100% rename from src/platform.c rename to src/valgrind.c diff --git a/src/valgrind.rs b/src/valgrind.rs new file mode 100644 index 0000000..8aa71fb --- /dev/null +++ b/src/valgrind.rs @@ -0,0 +1,9 @@ +#![allow(non_camel_case_types)] + +pub type stack_id_t = u32; +extern "C" { + #[link_name = "lwt_stack_register"] + pub fn stack_register(start: *const u8, end: *const u8) -> stack_id_t; + #[link_name = "lwt_stack_deregister"] + pub fn stack_deregister(id: stack_id_t); +}