Skip to main content

ax_percpu/
lib.rs

1#![cfg_attr(not(feature = "host-test"), no_std)]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3#![doc = include_str!("../README.md")]
4
5extern crate ax_percpu_macros;
6extern crate self as ax_percpu;
7
8mod alignment;
9mod area;
10mod descriptor;
11mod error;
12mod ffi;
13#[cfg(feature = "host-test")]
14pub mod host_test;
15mod initialization;
16mod layout;
17mod region;
18mod template;
19mod value;
20
21pub(crate) use alignment::required_area_alignment;
22pub use ax_percpu_macros::def_percpu;
23pub use cpu_local::{CpuAreaRef, CpuIndex, CpuPin, ExclusiveCpu, with_cpu_pin, with_exclusive_cpu};
24pub(crate) use template::{template_base, template_size};
25
26pub use self::{
27    area::{PerCpuArea, area, current_area, current_cpu_index, layout},
28    error::PerCpuError,
29    initialization::initialize_layout,
30    layout::PerCpuLayout,
31    region::PerCpuRegion,
32    value::PerCpu,
33};
34
35#[doc(hidden)]
36pub mod __priv {
37    pub use crate::{
38        descriptor::{PerCpuInitDescriptor, PerCpuInitRegistration},
39        value::{PerCpuObjectSymbol, PerCpuPrimitiveSymbol, PerCpuSymbol},
40    };
41
42    /// Calculates one symbol's offset from the template prefix.
43    pub fn symbol_offset(symbol_address: usize) -> usize {
44        symbol_address
45            .checked_sub(crate::template_base())
46            .expect("per-CPU symbol must follow the loaded template prefix")
47    }
48
49    /// Calculates a symbol pointer covered by an explicit CPU pin.
50    pub fn current_symbol_ptr<T>(pin: &crate::CpuPin<'_>, offset: usize) -> core::ptr::NonNull<T> {
51        // SAFETY: macro-generated offsets were validated before layout
52        // publication and CpuPin carries an initialized permanent area.
53        unsafe { core::ptr::NonNull::new_unchecked((pin.area().base() + offset) as *mut T) }
54    }
55
56    /// Calculates a symbol pointer in an explicit remote area.
57    pub fn remote_symbol_ptr<T>(area: crate::PerCpuArea, offset: usize) -> core::ptr::NonNull<T> {
58        crate::area::symbol_ptr(area, offset)
59    }
60}
61
62/// Example per-CPU data for documentation only.
63#[cfg(doc)]
64#[cfg_attr(docsrs, doc(cfg(doc)))]
65#[def_percpu]
66pub static EXAMPLE_PERCPU_DATA: usize = 0;