simplify Valgrind wrapper

master
John Ericson 2015-08-24 19:50:50 -07:00 committed by edef
parent 5411f10460
commit 878a6fefc3
3 changed files with 51 additions and 52 deletions

View File

@ -1,27 +1,21 @@
// This file is part of libfringe, a low-level green threading library.
// Copyright (c) 2015, edef <edef@edef.eu>
// See the LICENSE file included in this distribution.
use stack;
pub use self::imp::*;
mod valgrind;
#[cfg(feature = "valgrind")]
#[path = "valgrind/mod.rs"]
mod imp;
#[derive(Debug)]
pub struct StackId(valgrind::stack_id_t);
impl StackId {
#[inline(always)]
pub fn register<Stack: stack::Stack>(stack: &mut Stack) -> StackId {
StackId(unsafe {
valgrind::stack_register(stack.limit(), stack.top())
})
}
}
impl Drop for StackId {
#[inline(always)]
fn drop(&mut self) {
unsafe {
valgrind::stack_deregister(self.0)
#[cfg(not(feature = "valgrind"))]
mod imp {
use stack;
#[derive(Debug)]
pub struct StackId;
/// No-op since no valgrind
impl StackId {
pub unsafe fn register<Stack: stack::Stack>(_stack: &mut Stack) -> StackId {
StackId
}
}
}

View File

@ -1,17 +1,44 @@
// This file is part of libfringe, a low-level green threading library.
// Copyright (c) 2015, edef <edef@edef.eu>
// See the LICENSE file included in this distribution.
pub use self::imp::*;
#![allow(non_camel_case_types)]
//! In order for Valgrind to keep track of stack overflows and such, it needs
//! a little help. That help unfortunately comes in the form of a set of C
//! macros. Calling out to un-inlineable C code for this is pointlessly slow,
//! but that's the way it is for now.
use stack;
#[cfg(feature = "valgrind")]
#[path = "native.rs"]
mod imp;
pub type stack_id_t = u32;
extern "C" {
#[link_name = "valgrind_stack_register"]
/// Register a stack with Valgrind. Returns an integer ID that can
/// be used to deregister the stack when it's deallocated.
/// `start < end`.
pub fn stack_register(start: *const u8, end: *const u8) -> stack_id_t;
#[cfg(not(feature = "valgrind"))]
mod imp {
//! Stub for the Valgrind functions
#![allow(non_camel_case_types)]
pub type stack_id_t = ();
pub unsafe fn stack_register(_start: *const u8, _end: *const u8) -> stack_id_t {}
pub unsafe fn stack_deregister(_id: stack_id_t) {}
#[link_name = "valgrind_stack_deregister"]
/// Deregister a stack from Valgrind. Takes the integer ID that was returned
/// on registration.
pub fn stack_deregister(id: stack_id_t);
}
#[derive(Debug)]
pub struct StackId(stack_id_t);
impl StackId {
#[inline(always)]
pub fn register<Stack: stack::Stack>(stack: &mut Stack) -> StackId {
StackId(unsafe {
stack_register(stack.limit(), stack.top())
})
}
}
impl Drop for StackId {
#[inline(always)]
fn drop(&mut self) {
unsafe {
stack_deregister(self.0)
}
}
}

View File

@ -1,22 +0,0 @@
// This file is part of libfringe, a low-level green threading library.
// Copyright (c) 2015, edef <edef@edef.eu>
// See the LICENSE file included in this distribution.
#![allow(non_camel_case_types)]
//! In order for Valgrind to keep track of stack overflows and such, it needs
//! a little help. That help unfortunately comes in the form of a set of C
//! macros. Calling out to un-inlineable C code for this is pointlessly slow,
//! but that's the way it is for now.
pub type stack_id_t = u32;
extern "C" {
#[link_name = "valgrind_stack_register"]
/// Register a stack with Valgrind. Returns an integer ID that can
/// be used to deregister the stack when it's deallocated.
/// `start < end`.
pub fn stack_register(start: *const u8, end: *const u8) -> stack_id_t;
#[link_name = "valgrind_stack_deregister"]
/// Deregister a stack from Valgrind. Takes the integer ID that was returned
/// on registration.
pub fn stack_deregister(id: stack_id_t);
}