microsoft/openvmm

Public

mirrored fromhttps://github.com/microsoft/openvmmAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
e6c778cbebacf3a70be1d39295b4f090134c4091

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

support/debug_ptr/src/lib.rs

29lines · modecode

1// Copyright (C) Microsoft Corporation. All rights reserved.
2
3//! A wrapper around AtomicPtr that automatically adds a blackbox hint to
4//! prevent it from being optimized out. Intended to be used as a write-
5//! only pointer used to easily find interested variables when debugging.
6
7#![warn(missing_docs)]
8#![forbid(unsafe_code)]
9
10use std::sync::atomic::AtomicPtr;
11use std::sync::atomic::Ordering;
12
13/// A pointer wrapper for debugging purposes
14pub struct DebugPtr<T>(AtomicPtr<T>);
15
16impl<T> DebugPtr<T> {
17 /// Creates a new debug pointer, initialized to null.
18 pub const fn new() -> Self {
19 DebugPtr(AtomicPtr::new(std::ptr::null_mut()))
20 }
21
22 /// Stores the provided reference as an AtomicPtr and uses a blackbox
23 /// hint to prevent it from being optimized out.
24 pub fn store(&self, ptr: &T) {
25 self.0
26 .store(std::ptr::from_ref(ptr).cast_mut(), Ordering::Relaxed);
27 std::hint::black_box(&self.0);
28 }
29}
30