From 2ece7cd76309a99519f496c675452770d6a5698c Mon Sep 17 00:00:00 2001 From: Laurence Tratt Date: Sat, 5 Dec 2020 17:28:20 +0000 Subject: [PATCH] Tweak comment and guarantee correct alignment. --- ykrt/src/mt.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/ykrt/src/mt.rs b/ykrt/src/mt.rs index addf40515..05bf1f3bd 100644 --- a/ykrt/src/mt.rs +++ b/ykrt/src/mt.rs @@ -1,6 +1,7 @@ #[cfg(test)] use std::time::Duration; use std::{ + alloc::{alloc, dealloc, Layout}, io, mem, panic::{catch_unwind, resume_unwind, UnwindSafe}, rc::Rc, @@ -311,9 +312,10 @@ impl MTThread { /// The innards of a meta-tracer thread. struct MTThreadInner { mt: MT, - /// A value that uniquely identifies a thread. For simplicities sake this is a pointer to a - /// malloc'd chunk of memory since that pointer's alignment means that we won't conflict with - /// the bits set in PHASE_TAG. + /// A value that uniquely identifies a thread. Since this ID needs to be ORable with PHASE_TAG, + /// we use a pointer to a malloc'd chunk of memory. We guarantee a) that chunk is aligned to a + /// machine word (or greater) b) that it is a non-zero chunk of memory (and thus guaranteed to + /// be a unique pointer). tid: *mut u8, hot_threshold: HotThreshold, #[allow(dead_code)] @@ -328,9 +330,14 @@ impl MTThreadInner { fn init(mt: MT) -> MTThread { let hot_threshold = mt.hot_threshold(); let tracing_kind = mt.tracing_kind(); + let tid = { + let layout = + Layout::from_size_align(mem::size_of::(), mem::size_of::()).unwrap(); + unsafe { alloc(layout) } + }; let inner = MTThreadInner { mt, - tid: Box::into_raw(Box::new(0u8)), + tid, hot_threshold, tracing_kind, tracer: None, @@ -343,7 +350,9 @@ impl MTThreadInner { impl Drop for MTThreadInner { fn drop(&mut self) { - unsafe { Box::from_raw(self.tid) }; + let layout = + Layout::from_size_align(mem::size_of::(), mem::size_of::()).unwrap(); + unsafe { dealloc(self.tid, layout) }; } }