Skip to main content

ax_percpu/
region.rs

1use core::{num::NonZeroU32, ptr::NonNull};
2
3/// Platform-owned raw storage geometry for runtime per-CPU areas.
4#[derive(Clone, Copy, Debug, Eq, PartialEq)]
5pub struct PerCpuRegion {
6    runtime_base: NonNull<u8>,
7    area_stride: usize,
8    area_count: NonZeroU32,
9}
10
11// SAFETY: this value describes externally synchronized permanent storage; it
12// does not itself grant access to any bytes.
13unsafe impl Send for PerCpuRegion {}
14// SAFETY: see Send; shared geometry is immutable.
15unsafe impl Sync for PerCpuRegion {}
16
17impl PerCpuRegion {
18    /// Creates raw runtime geometry to be validated by `initialize_layout`.
19    pub const fn new(
20        runtime_base: NonNull<u8>,
21        area_stride: usize,
22        area_count: NonZeroU32,
23    ) -> Self {
24        Self {
25            runtime_base,
26            area_stride,
27            area_count,
28        }
29    }
30
31    /// Returns CPU zero's runtime base.
32    pub fn runtime_base(self) -> usize {
33        self.runtime_base.as_ptr() as usize
34    }
35
36    /// Returns the byte stride between adjacent areas.
37    pub const fn area_stride(self) -> usize {
38        self.area_stride
39    }
40
41    /// Returns the number of runtime areas.
42    pub const fn area_count(self) -> NonZeroU32 {
43        self.area_count
44    }
45}