percpu_macros 0.4.0

Macros to define and access a per-CPU data structure
Documentation
//! For single CPU use, we just make the per-CPU data a global variable.

use quote::quote;
use syn::{Ident, Type};

pub fn gen_symbol_vma(symbol: &Ident) -> proc_macro2::TokenStream {
    quote! {
        unsafe { ::core::ptr::addr_of!(#symbol) as usize }
    }
}

pub fn gen_offset(symbol: &Ident) -> proc_macro2::TokenStream {
    gen_symbol_vma(symbol)
}

pub fn gen_current_ptr(symbol: &Ident, _ty: &Type) -> proc_macro2::TokenStream {
    quote! {
        unsafe { ::core::ptr::addr_of!(#symbol) }
    }
}

pub fn gen_read_current_raw(_symbol: &Ident, _ty: &Type) -> proc_macro2::TokenStream {
    quote! {
        *self.current_ptr()
    }
}

pub fn gen_write_current_raw(_symbol: &Ident, val: &Ident, ty: &Type) -> proc_macro2::TokenStream {
    quote! {
        *(self.current_ptr() as *mut #ty) = #val
    }
}