themis-topology 0.11.1

Typed placement law for NUMA, HBM, memory-tier, and worker locality contracts
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Shared storage for the branded placement wrappers.
//!
//! The dynamic (`NumaPinned*`) and static (`ConstNumaPinned*`) families differ
//! only in how the NUMA node tag is carried: the dynamic family stores a
//! [`NumaNodeId`] value, the static family encodes it as a const generic on a
//! zero-sized [`ConstNodeTag`]. Both families are therefore one storage type,
//! [`PinnedStorage`], parameterised by that tag and by the container it holds —
//! an owned or borrowed cell, or an owned or borrowed slice of cells.

#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(not(feature = "std"))]
use alloc::{boxed::Box, vec::Vec};

#[cfg(feature = "std")]
use std::{boxed::Box, vec::Vec};

use melinoe::{collections::BrandedVec, MelinoeCell};

use super::cell::{ConstPinnedCell, ConstPinnedSlice, PinnedCell, PinnedSlice};
use crate::NumaNodeId;

/// A zero-sized NUMA node tag that encodes its node id in the type.
///
/// This is the tag parameter of [`PinnedStorage`] for the statically pinned
/// `ConstNumaPinned*` family; the dynamic `NumaPinned*` family uses a plain
/// [`NumaNodeId`] as its tag instead. Being a distinct type per `NODE_ID`, it
/// keeps a statically pinned cell from answering to any other tag.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ConstNodeTag<const NODE_ID: u32>;

/// A container that exposes exactly one branded cell by shared reference.
///
/// Implemented by an owned [`MelinoeCell`] and by an exclusive `&MelinoeCell`
/// borrow, so the owned and borrowed pinned-cell wrappers share one storage
/// type and one set of constructors.
pub trait CellContainer<'brand, T> {
    /// Returns the contained cell.
    fn as_cell(&self) -> &MelinoeCell<'brand, T>;
}

impl<'brand, T> CellContainer<'brand, T> for MelinoeCell<'brand, T> {
    #[inline]
    fn as_cell(&self) -> &MelinoeCell<'brand, T> {
        self
    }
}

impl<'brand, T> CellContainer<'brand, T> for &MelinoeCell<'brand, T> {
    #[inline]
    fn as_cell(&self) -> &MelinoeCell<'brand, T> {
        self
    }
}

/// A container that exposes a contiguous slice of branded cells.
///
/// Implemented by an owned boxed slice of cells and by an exclusive
/// `&[MelinoeCell]` borrow, so the owned and borrowed pinned-slice wrappers
/// share one storage type and one set of constructors.
pub trait CellSlice<'brand, T> {
    /// Returns the contained cells.
    fn as_cells(&self) -> &[MelinoeCell<'brand, T>];
}

impl<'brand, T> CellSlice<'brand, T> for Box<[MelinoeCell<'brand, T>]> {
    #[inline]
    fn as_cells(&self) -> &[MelinoeCell<'brand, T>] {
        self
    }
}

impl<'brand, T> CellSlice<'brand, T> for &[MelinoeCell<'brand, T>] {
    #[inline]
    fn as_cells(&self) -> &[MelinoeCell<'brand, T>] {
        self
    }
}

/// Storage shared by every pinned placement wrapper.
///
/// `Tag` is the node tag ([`NumaNodeId`] for the dynamic family,
/// [`ConstNodeTag`] for the static family) and `Storage` is the container: a
/// [`MelinoeCell`], a `&MelinoeCell` borrow, a boxed slice of cells, or a
/// borrowed slice of cells.
pub struct PinnedStorage<Tag, Storage> {
    tag: Tag,
    storage: Storage,
}

impl<Storage> PinnedStorage<NumaNodeId, Storage> {
    /// Returns the pinned NUMA node ID.
    #[must_use]
    #[inline]
    pub const fn node_id(&self) -> NumaNodeId {
        self.tag
    }
}

impl<T> PinnedStorage<NumaNodeId, MelinoeCell<'_, T>> {
    /// Creates a new cell pinned to the specified NUMA node.
    #[must_use]
    pub const fn new(node_id: NumaNodeId, value: T) -> Self {
        Self {
            tag: node_id,
            storage: MelinoeCell::new(value),
        }
    }
}

impl<const NODE_ID: u32, T> PinnedStorage<ConstNodeTag<NODE_ID>, MelinoeCell<'_, T>> {
    /// Creates a new cell pinned statically to the node ID.
    #[must_use]
    pub const fn new(value: T) -> Self {
        Self {
            tag: ConstNodeTag,
            storage: MelinoeCell::new(value),
        }
    }
}

impl<'a, 'brand, T> PinnedStorage<NumaNodeId, &'a MelinoeCell<'brand, T>> {
    /// Pins an exclusively borrowed cell to `node_id`.
    ///
    /// The `&mut` borrow is the placement proof and the reason this is safe:
    /// it is consumed for `'a`, so the compiler rejects any second reference to
    /// `cell` — and therefore any second node tag — while this one lives.
    ///
    /// Use this to place cells that live on the stack or inside a caller-owned
    /// buffer; the owned constructor covers the owned case.
    ///
    /// ```
    /// use themis::{NumaNodeId, NumaPinnedCellRef, sync_region_placement_scope};
    ///
    /// sync_region_placement_scope(|placement| {
    ///     let mut cell = placement.cell(7u32);
    ///     let pinned = NumaPinnedCellRef::from_unique(NumaNodeId::new(0), &mut cell);
    ///     assert_eq!(pinned.node_id(), NumaNodeId::new(0));
    /// });
    /// ```
    #[must_use]
    #[inline]
    pub fn from_unique(node_id: NumaNodeId, cell: &'a mut MelinoeCell<'brand, T>) -> Self {
        Self {
            tag: node_id,
            storage: cell,
        }
    }
}

impl<'a, 'brand, const NODE_ID: u32, T>
    PinnedStorage<ConstNodeTag<NODE_ID>, &'a MelinoeCell<'brand, T>>
{
    /// Pins an exclusively borrowed cell to `NODE_ID`.
    ///
    /// The `&mut` borrow is the placement proof: it is consumed for `'a`, so
    /// the compiler rejects a second reference to `cell` and therefore a second
    /// `NODE_ID` for it. Two capabilities minted by
    /// [`split_static`](crate::SyncRegionPlacement::split_static) can never
    /// both reach one cell.
    ///
    /// ```
    /// use themis::{ConstNumaPinnedCellRef, sync_region_placement_scope};
    ///
    /// sync_region_placement_scope(|region| {
    ///     let mut cell = region.cell(7u32);
    ///     let pinned = ConstNumaPinnedCellRef::<0, _>::from_unique(&mut cell);
    ///     let mut placement = region.project_static::<0>();
    ///     assert_eq!(*placement.read(&pinned), 7);
    /// });
    /// ```
    #[must_use]
    #[inline]
    pub fn from_unique(cell: &'a mut MelinoeCell<'brand, T>) -> Self {
        Self {
            tag: ConstNodeTag,
            storage: cell,
        }
    }
}

impl<'brand, T> PinnedStorage<NumaNodeId, Box<[MelinoeCell<'brand, T>]>> {
    /// Creates a new pinned slice from a vector of values.
    #[must_use]
    pub fn new(node_id: NumaNodeId, values: Vec<T>) -> Self {
        Self {
            tag: node_id,
            storage: BrandedVec::from_iter(values).into_boxed_cells(),
        }
    }

    /// Creates a new pinned slice by generating values in index order.
    ///
    /// Generation is performed directly through Melinoe's branded collection
    /// primitive; no intermediate unbranded value vector is required.
    #[must_use]
    pub fn from_fn<F>(node_id: NumaNodeId, len: usize, generate: F) -> Self
    where
        F: FnMut(usize) -> T,
    {
        Self {
            tag: node_id,
            storage: BrandedVec::from_fn(len, generate).into_boxed_cells(),
        }
    }

    /// Creates a new pinned slice directly from a boxed slice of cells.
    #[must_use]
    pub const fn from_cells(node_id: NumaNodeId, cells: Box<[MelinoeCell<'brand, T>]>) -> Self {
        Self {
            tag: node_id,
            storage: cells,
        }
    }
}

impl<'brand, const NODE_ID: u32, T>
    PinnedStorage<ConstNodeTag<NODE_ID>, Box<[MelinoeCell<'brand, T>]>>
{
    /// Creates a new pinned slice from a vector of values.
    #[must_use]
    pub fn new(values: Vec<T>) -> Self {
        Self {
            tag: ConstNodeTag,
            storage: BrandedVec::from_iter(values).into_boxed_cells(),
        }
    }

    /// Creates a new pinned slice by generating values in index order.
    ///
    /// Generation is performed directly through Melinoe's branded collection
    /// primitive; no intermediate unbranded value vector is required.
    #[must_use]
    pub fn from_fn<F>(len: usize, generate: F) -> Self
    where
        F: FnMut(usize) -> T,
    {
        Self {
            tag: ConstNodeTag,
            storage: BrandedVec::from_fn(len, generate).into_boxed_cells(),
        }
    }

    /// Creates a new pinned slice directly from a boxed slice of cells.
    #[must_use]
    pub const fn from_cells(cells: Box<[MelinoeCell<'brand, T>]>) -> Self {
        Self {
            tag: ConstNodeTag,
            storage: cells,
        }
    }
}

impl<'a, 'brand, T> PinnedStorage<NumaNodeId, &'a [MelinoeCell<'brand, T>]> {
    /// Pins an exclusively borrowed cell slice to `node_id`.
    ///
    /// The `&mut` borrow is the placement proof: it is consumed for `'a`, so no
    /// second wrapper — and no second node tag — can cover these cells while
    /// this one lives. Placing a stack array needs no allocation:
    ///
    /// ```
    /// use melinoe::MelinoeCell;
    /// use themis::{NumaNodeId, NumaPinnedSliceRef, sync_region_placement_scope};
    ///
    /// sync_region_placement_scope(|placement| {
    ///     let mut cells = [placement.cell(1u32), placement.cell(2u32)];
    ///     let pinned = NumaPinnedSliceRef::from_unique(NumaNodeId::new(0), &mut cells);
    ///     assert_eq!(pinned.node_id(), NumaNodeId::new(0));
    /// });
    /// ```
    #[must_use]
    #[inline]
    pub fn from_unique(node_id: NumaNodeId, cells: &'a mut [MelinoeCell<'brand, T>]) -> Self {
        Self {
            tag: node_id,
            storage: cells,
        }
    }
}

impl<'a, 'brand, const NODE_ID: u32, T>
    PinnedStorage<ConstNodeTag<NODE_ID>, &'a [MelinoeCell<'brand, T>]>
{
    /// Pins an exclusively borrowed cell slice to `NODE_ID`.
    ///
    /// The `&mut` borrow is the placement proof: it is consumed for `'a`, so no
    /// second wrapper — and no second `NODE_ID` — can cover these cells while
    /// this one lives. Placing a stack array needs no allocation.
    #[must_use]
    #[inline]
    pub fn from_unique(cells: &'a mut [MelinoeCell<'brand, T>]) -> Self {
        Self {
            tag: ConstNodeTag,
            storage: cells,
        }
    }
}

impl<'brand, Tag: Copy, T> PinnedStorage<Tag, MelinoeCell<'brand, T>> {
    /// Borrows this cell as a reference carrying the same pin.
    ///
    /// The tag is inherited from the owner rather than supplied by the caller,
    /// so the reference cannot relabel the cell onto another NUMA node.
    #[must_use]
    #[inline]
    pub const fn as_pinned_ref(&self) -> PinnedStorage<Tag, &MelinoeCell<'brand, T>> {
        PinnedStorage {
            tag: self.tag,
            storage: &self.storage,
        }
    }
}

impl<'brand, Tag, T> PinnedStorage<Tag, Box<[MelinoeCell<'brand, T>]>> {
    /// Borrow the underlying cells immutably.
    #[must_use]
    #[inline]
    pub fn cells(&self) -> &[MelinoeCell<'brand, T>] {
        &self.storage
    }
}

impl<'brand, Tag: Copy, T> PinnedStorage<Tag, Box<[MelinoeCell<'brand, T>]>> {
    /// Borrows these cells as a slice reference carrying the same pin.
    ///
    /// The tag is inherited from the owner rather than supplied by the caller.
    #[must_use]
    #[inline]
    pub const fn as_pinned_ref(&self) -> PinnedStorage<Tag, &[MelinoeCell<'brand, T>]> {
        PinnedStorage {
            tag: self.tag,
            storage: &self.storage,
        }
    }
}

#[cfg(feature = "std")]
impl<'brand, Tag, T> PinnedStorage<Tag, Box<[MelinoeCell<'brand, T>]>> {
    /// Access the uniquely owned branded cells for Themis's placement-gated
    /// Melinoe partition driver.
    pub(crate) fn cells_mut(&mut self) -> &mut [MelinoeCell<'brand, T>] {
        &mut self.storage
    }
}

// SAFETY — the dynamic (`NumaNodeId`) cell family, one impl covering both the
// owned and borrowed containers. An owned `MelinoeCell` is created by
// `PinnedStorage::new` and named by no other pinned wrapper, so its tag is
// exclusively this one. A `&MelinoeCell` container is only ever built by
// `from_unique`, which consumes an exclusive borrow for the wrapper's whole
// life, or by `as_pinned_ref`, which copies the owner's tag rather than
// accepting one. Either way the cell answers to exactly this `node_id`, which
// is a `Copy` value fixed at construction.
unsafe impl<'brand, T, Storage: CellContainer<'brand, T>> PinnedCell<'brand, T>
    for PinnedStorage<NumaNodeId, Storage>
{
    #[inline]
    fn node_id(&self) -> NumaNodeId {
        self.tag
    }

    #[inline]
    fn cell(&self) -> &MelinoeCell<'brand, T> {
        self.storage.as_cell()
    }
}

// SAFETY — as the `PinnedCell` impl, with `NODE_ID` as the tag: an owned cell
// is named by no other wrapper, a borrowed one reaches here only through
// `from_unique` (an exclusive borrow) or `as_pinned_ref` (an inherited tag),
// and `NODE_ID` is part of the type, so the tag cannot vary between calls.
unsafe impl<'brand, const NODE_ID: u32, T, Storage: CellContainer<'brand, T>>
    ConstPinnedCell<'brand, NODE_ID, T> for PinnedStorage<ConstNodeTag<NODE_ID>, Storage>
{
    #[inline]
    fn cell(&self) -> &MelinoeCell<'brand, T> {
        self.storage.as_cell()
    }
}

// SAFETY — the dynamic slice family, applied elementwise as `PinnedCell`: the
// cells are owned by this wrapper (and produced by its constructors, so no
// other wrapper can name them) or borrowed exclusively for the wrapper's whole
// life, and `node_id` is a `Copy` value fixed at construction.
unsafe impl<'brand, T, Storage: CellSlice<'brand, T>> PinnedSlice<'brand, T>
    for PinnedStorage<NumaNodeId, Storage>
{
    #[inline]
    fn node_id(&self) -> NumaNodeId {
        self.tag
    }

    #[inline]
    fn cells(&self) -> &[MelinoeCell<'brand, T>] {
        self.storage.as_cells()
    }
}

// SAFETY — as the `PinnedSlice` impl, with `NODE_ID` as the tag: ownership or an
// exclusive borrow keeps every cell reachable through exactly this wrapper, and
// `NODE_ID` being part of the type keeps the tag constant.
unsafe impl<'brand, const NODE_ID: u32, T, Storage: CellSlice<'brand, T>>
    ConstPinnedSlice<'brand, NODE_ID, T> for PinnedStorage<ConstNodeTag<NODE_ID>, Storage>
{
    #[inline]
    fn cells(&self) -> &[MelinoeCell<'brand, T>] {
        self.storage.as_cells()
    }
}