Skip to content

Commit

Permalink
Tweak comment and guarantee correct alignment.
Browse files Browse the repository at this point in the history
  • Loading branch information
ltratt committed Dec 5, 2020
1 parent e65104e commit 2ece7cd
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions ykrt/src/mt.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)]
Expand All @@ -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::<usize>(), mem::size_of::<usize>()).unwrap();
unsafe { alloc(layout) }
};
let inner = MTThreadInner {
mt,
tid: Box::into_raw(Box::new(0u8)),
tid,
hot_threshold,
tracing_kind,
tracer: None,
Expand All @@ -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::<usize>(), mem::size_of::<usize>()).unwrap();
unsafe { dealloc(self.tid, layout) };
}
}

Expand Down

0 comments on commit 2ece7cd

Please sign in to comment.