factor Valgrind support out and expose it

Now other Stack / StackSource implementations can use the same Valgrind
code. Ref #3.
This commit is contained in:
edef 2015-04-15 22:25:52 -04:00
parent 904da1a1c8
commit db6a2ff89b
5 changed files with 17 additions and 14 deletions

View File

@ -1,5 +1,5 @@
extern crate gcc; extern crate gcc;
fn main() { fn main() {
gcc::compile_library("libcontext.a", &["src/platform.c"]); gcc::compile_library("libvalgrind.a", &["src/valgrind.c"]);
} }

View File

@ -18,6 +18,7 @@ mod std { pub use core::*; }
pub mod context; pub mod context;
pub mod stack; pub mod stack;
pub mod valgrind;
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
mod arch; mod arch;

View File

@ -6,22 +6,14 @@ use self::std::io::Error as IoError;
use self::libc::{c_void, size_t}; use self::libc::{c_void, size_t};
use core::ptr; use core::ptr;
use stack; use stack;
use valgrind;
#[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);
}
#[allow(raw_pointer_derive)] #[allow(raw_pointer_derive)]
#[derive(Debug)] #[derive(Debug)]
pub struct Stack { pub struct Stack {
ptr: *mut u8, ptr: *mut u8,
len: usize, len: usize,
valgrind_id: stack_id_t valgrind_id: valgrind::stack_id_t
} }
pub struct StackSource; pub struct StackSource;
@ -69,7 +61,8 @@ impl Stack {
len, IoError::last_os_error()) len, IoError::last_os_error())
} }
let valgrind_id = stack_register(ptr.offset(len as isize) as *const _, let valgrind_id =
valgrind::stack_register(ptr.offset(len as isize) as *const _,
ptr as *const _); ptr as *const _);
Stack { ptr: ptr as *mut u8, len: len, valgrind_id: valgrind_id } Stack { ptr: ptr as *mut u8, len: len, valgrind_id: valgrind_id }
@ -95,7 +88,7 @@ impl Stack {
impl Drop for Stack { impl Drop for Stack {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { 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 { if libc::munmap(self.ptr as *mut c_void, self.len as size_t) != 0 {
panic!("munmap for stack {:p} of size {} failed: {:?}", panic!("munmap for stack {:p} of size {} failed: {:?}",
self.ptr, self.len, IoError::last_os_error()) self.ptr, self.len, IoError::last_os_error())

9
src/valgrind.rs Normal file
View File

@ -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);
}