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 · modepreview

// Copyright (C) Microsoft Corporation. All rights reserved.

//! A wrapper around AtomicPtr that automatically adds a blackbox hint to
//! prevent it from being optimized out. Intended to be used as a write-
//! only pointer used to easily find interested variables when debugging.

#![warn(missing_docs)]
#![forbid(unsafe_code)]

use std::sync::atomic::AtomicPtr;
use std::sync::atomic::Ordering;

/// A pointer wrapper for debugging purposes
pub struct DebugPtr<T>(AtomicPtr<T>);

impl<T> DebugPtr<T> {
    /// Creates a new debug pointer, initialized to null.
    pub const fn new() -> Self {
        DebugPtr(AtomicPtr::new(std::ptr::null_mut()))
    }

    /// Stores the provided reference as an AtomicPtr and uses a blackbox
    /// hint to prevent it from being optimized out.
    pub fn store(&self, ptr: &T) {
        self.0
            .store(std::ptr::from_ref(ptr).cast_mut(), Ordering::Relaxed);
        std::hint::black_box(&self.0);
    }
}