From 878a6fefc316ca54f37981029df6a77c94bd3910 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Mon, 24 Aug 2015 19:50:50 -0700 Subject: [PATCH] simplify Valgrind wrapper --- src/debug/mod.rs | 32 ++++++++++------------- src/debug/valgrind/mod.rs | 49 ++++++++++++++++++++++++++++-------- src/debug/valgrind/native.rs | 22 ---------------- 3 files changed, 51 insertions(+), 52 deletions(-) delete mode 100644 src/debug/valgrind/native.rs diff --git a/src/debug/mod.rs b/src/debug/mod.rs index 46540ba..6c01501 100644 --- a/src/debug/mod.rs +++ b/src/debug/mod.rs @@ -1,27 +1,21 @@ // This file is part of libfringe, a low-level green threading library. // Copyright (c) 2015, edef // 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: &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: &mut Stack) -> StackId { + StackId } } } diff --git a/src/debug/valgrind/mod.rs b/src/debug/valgrind/mod.rs index 525ab07..1b4156e 100644 --- a/src/debug/valgrind/mod.rs +++ b/src/debug/valgrind/mod.rs @@ -1,17 +1,44 @@ // This file is part of libfringe, a low-level green threading library. // Copyright (c) 2015, edef // 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: &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) + } + } } diff --git a/src/debug/valgrind/native.rs b/src/debug/valgrind/native.rs deleted file mode 100644 index 2d638a9..0000000 --- a/src/debug/valgrind/native.rs +++ /dev/null @@ -1,22 +0,0 @@ -// This file is part of libfringe, a low-level green threading library. -// Copyright (c) 2015, edef -// 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); -}