Skip to main content

dope_core/driver/storage/
mod.rs

1use std::{mem, pin};
2
3use o3::{
4    alloc,
5    base::cell::region,
6    buffer::{
7        hold::limits,
8        resident::{Budget, Reuse},
9    },
10};
11
12use crate::{
13    BranchError, driver,
14    driver::{
15        life,
16        retained::flight,
17        route::{App, lease, queues},
18        schedule::{clock, clock::timer},
19        settings,
20    },
21};
22
23mod cleanup;
24mod failure;
25mod mapping;
26pub mod path;
27mod release;
28mod removal;
29pub mod resident;
30mod scope;
31
32pub use cleanup::Cleanup;
33pub use failure::Failure;
34pub use mapping::Mapping;
35pub use release::Release;
36pub use removal::Removal;
37pub use scope::Scope;
38
39#[derive(Clone, Copy, Debug, PartialEq, Eq)]
40#[repr(transparent)]
41pub struct Address(*mut core::ffi::c_void);
42
43#[derive(Clone, Copy, Debug, PartialEq, Eq)]
44#[repr(transparent)]
45pub struct Bytes(usize);
46
47#[derive(Clone, Copy, Debug, PartialEq, Eq)]
48#[repr(transparent)]
49pub struct Count(usize);
50
51impl Address {
52    pub(crate) const fn new(address: *mut core::ffi::c_void) -> Self {
53        Self(address)
54    }
55}
56
57impl Bytes {
58    pub(crate) const fn new(bytes: usize) -> Self {
59        Self(bytes)
60    }
61}
62
63impl Count {
64    pub(crate) const fn new(count: usize) -> Self {
65        Self(count)
66    }
67}
68
69/// Resident bytes requested for one driver-lifetime accounting domain.
70#[derive(Clone, Copy, Eq, PartialEq)]
71#[repr(transparent)]
72pub struct Resident(usize);
73
74/// Failure to issue one resident domain from a bounded allocator.
75#[derive(Clone, Copy, Debug, Eq, PartialEq)]
76pub enum Exhausted {
77    Domains { capacity: usize },
78    Retained { index: usize, cause: Reuse },
79}
80
81const _: () = assert!(size_of::<Exhausted>() == size_of::<usize>() * 4);
82
83impl Resident {
84    pub const fn bytes(bytes: usize) -> Self {
85        Self(bytes)
86    }
87
88    pub const fn from_limit(limit: limits::Bound) -> Self {
89        Self(limit.bytes())
90    }
91
92    const fn get(self) -> usize {
93        self.0
94    }
95}
96
97/// Projects one exact leaf from a generative storage build output.
98pub trait Path<Root, Leaf>: Copy
99where
100    Root: Plan,
101    Leaf: Plan,
102{
103    fn get<'life, 'p, 'd>(self, root: &'life Root::Output<'p, 'd>) -> &'life Leaf::Output<'p, 'd>
104    where
105        'd: 'p,
106        'p: 'life,
107        'd: 'life,
108        Self: 'd,
109        Root: 'd,
110        Leaf: 'd;
111}
112
113/// Restricted construction context for driver-domain storage.
114///
115/// Exposes stable identities and route reservation without operation submission.
116/// A build cannot publish into its output before the runtime pins that output.
117///
118#[repr(transparent)]
119pub struct Context<'a, 'd>(pub(crate) &'a mut life::Local<'d>);
120
121impl<'a, 'd> Context<'a, 'd> {
122    pub(super) fn new(local: &'a mut life::Local<'d>) -> Self {
123        Self(local)
124    }
125
126    pub fn driver(&self) -> driver::Reference<'d> {
127        self.0.driver
128    }
129
130    /// Exact receive-pool geometry of this driver generation.
131    pub fn receive(&self) -> settings::Receive {
132        self.0.driver.layout()
133    }
134
135    pub fn region(&self) -> &region::Token<region::Identity<'d>> {
136        &self.0.region.0
137    }
138
139    pub fn state(&self) -> &region::Token<region::Identity<'d, 1>> {
140        &self.0.region.1
141    }
142
143    pub fn timer(&self) -> timer::Timer<'d> {
144        self.0.driver.timer()
145    }
146
147    pub fn clock(&self) -> clock::Clock<'_, 'd> {
148        self.0.driver.scheduler().clock()
149    }
150
151    /// Copies the resident-domain authority for this exact driver generation.
152    pub fn allocator(&self) -> resident::Alloc<'d> {
153        self.0.resident
154    }
155
156    /// Claims a lifetime-branded resident accounting domain for storage owned
157    /// by the build output.
158    pub fn budget(&self, cap: Resident) -> Result<Budget<'d>, Exhausted> {
159        self.0.resident.try_budget(cap.get())
160    }
161
162    /// Lends one affine construction step while retaining this outer authority.
163    pub fn reborrow(&mut self) -> Context<'_, 'd> {
164        Context(&mut *self.0)
165    }
166
167    /// Stages fixed submission groups without changing the live flight arena.
168    pub fn try_flights<Tags, const GROUPS: usize, B>(
169        self,
170        need: flight::Need<Tags, GROUPS, B>,
171    ) -> Result<flight::Prepared<'a, 'd, Tags, GROUPS, B>, alloc::Error> {
172        let Self(local) = self;
173        flight::raw::prepare(local, need)
174    }
175
176    pub fn try_reserve<B: App>(self) -> Result<lease::Claim<'d, B>, lease::Error> {
177        let Self(local) = self;
178        lease::Claim::try_new(local)
179    }
180
181    pub(in crate::driver) fn release(&mut self, retired: queues::Retired<'d>) {
182        lease::Registry::release(self.0, retired);
183    }
184}
185
186/// One generative storage output family.
187///
188/// A plan may borrow from its enclosing owner. Its output is available only
189/// for driver generations which the consumed plan itself outlives.
190pub trait Plan {
191    type Output<'p, 'd>: 'p
192    where
193        Self: 'd,
194        'd: 'p;
195}
196
197/// Pinned storage retaining its plan, backing loan, and driver generation.
198#[repr(transparent)]
199pub struct Loan<'a, 'p: 'a, 'd: 'p, S: Plan + 'd> {
200    value: pin::Pin<&'a S::Output<'p, 'd>>,
201}
202
203impl<'a, 'p: 'a, 'd: 'p, S: Plan + 'd> Loan<'a, 'p, 'd, S> {
204    pub(in crate::driver) fn new(value: pin::Pin<&'a S::Output<'p, 'd>>) -> Self {
205        Self { value }
206    }
207
208    /// Preserves the source borrow when this temporary view is consumed.
209    ///
210    ///
211    pub fn pinned(&self) -> pin::Pin<&'a S::Output<'p, 'd>> {
212        self.value
213    }
214}
215
216const _: () = {
217    assert!(mem::size_of::<Loan<'static, 'static, 'static, ()>>() == mem::size_of::<usize>());
218    assert!(mem::align_of::<Loan<'static, 'static, 'static, ()>>() == mem::align_of::<usize>());
219};
220
221/// Fallibly constructs driver-domain storage through the non-submitting [`Context`].
222pub trait Build: Plan {
223    type Error;
224
225    type Store<'d>
226    where
227        Self: 'd;
228
229    type Owned<'d>
230    where
231        Self: 'd;
232
233    fn try_store<'d>(&self) -> Result<(Self::Store<'d>, Self::Owned<'d>), Self::Error>
234    where
235        Self: 'd;
236
237    fn try_build<'p, 'd>(
238        self,
239        owned: Self::Owned<'d>,
240        store: &'p mut Self::Store<'d>,
241        ctx: Context<'_, 'd>,
242    ) -> Result<Self::Output<'p, 'd>, Self::Error>
243    where
244        Self: 'd,
245        'd: 'p;
246}
247
248/// Constructs driver-domain storage that owns all of its backing.
249pub trait Create: Plan {
250    type Error;
251
252    fn try_build<'p, 'd>(self, ctx: Context<'_, 'd>) -> Result<Self::Output<'p, 'd>, Self::Error>
253    where
254        Self: 'd,
255        'd: 'p;
256}
257
258impl<T: Create> Build for T {
259    type Owned<'d>
260        = ()
261    where
262        Self: 'd;
263    type Error = <T as Create>::Error;
264    type Store<'d>
265        = ()
266    where
267        Self: 'd;
268
269    fn try_store<'d>(&self) -> Result<(Self::Store<'d>, Self::Owned<'d>), Self::Error>
270    where
271        Self: 'd,
272    {
273        Ok(((), ()))
274    }
275
276    fn try_build<'p, 'd>(
277        self,
278        _: Self::Owned<'d>,
279        _: &'p mut Self::Store<'d>,
280        ctx: Context<'_, 'd>,
281    ) -> Result<Self::Output<'p, 'd>, Self::Error>
282    where
283        Self: 'd,
284        'd: 'p,
285    {
286        Create::try_build(self, ctx)
287    }
288}
289
290/// Builds storage without contributing driver-resource claims.
291pub trait Claimless: Plan {}
292
293/// Constructs storage without introducing a recoverable failure path.
294pub trait Direct: Plan {
295    fn build<'p, 'd>(self) -> Self::Output<'p, 'd>
296    where
297        Self: 'd,
298        'd: 'p;
299}
300
301#[repr(transparent)]
302pub struct Value<T>(T);
303
304impl<T> Value<T> {
305    pub const fn new(value: T) -> Self {
306        Self(value)
307    }
308}
309
310impl<T> Plan for Value<T> {
311    type Output<'p, 'd>
312        = T
313    where
314        'd: 'p,
315        Self: 'd;
316}
317
318impl<T> Claimless for Value<T> {}
319
320impl<T> Direct for Value<T> {
321    fn build<'p, 'd>(self) -> Self::Output<'p, 'd>
322    where
323        'd: 'p,
324        Self: 'd,
325    {
326        self.0
327    }
328}
329
330impl Plan for () {
331    type Output<'p, 'd>
332        = ()
333    where
334        'd: 'p,
335        Self: 'd;
336}
337
338impl Claimless for () {}
339
340impl Direct for () {
341    fn build<'p, 'd>(self) -> Self::Output<'p, 'd>
342    where
343        'd: 'p,
344        Self: 'd,
345    {
346        self
347    }
348}
349
350impl<A: Plan, B: Plan> Plan for (A, B) {
351    type Output<'p, 'd>
352        = (A::Output<'p, 'd>, B::Output<'p, 'd>)
353    where
354        'd: 'p,
355        Self: 'd;
356}
357
358impl<A: Build, B: Build> Build for (A, B) {
359    type Owned<'d>
360        = (A::Owned<'d>, B::Owned<'d>)
361    where
362        Self: 'd;
363    type Error = BranchError<A::Error, B::Error>;
364    type Store<'d>
365        = (A::Store<'d>, B::Store<'d>)
366    where
367        Self: 'd;
368
369    fn try_store<'d>(&self) -> Result<(Self::Store<'d>, Self::Owned<'d>), Self::Error>
370    where
371        Self: 'd,
372    {
373        let (first, first_owned) = self.0.try_store().map_err(BranchError::First)?;
374        let (second, second_owned) = self.1.try_store().map_err(BranchError::Second)?;
375        Ok(((first, second), (first_owned, second_owned)))
376    }
377
378    fn try_build<'p, 'd>(
379        self,
380        owned: Self::Owned<'d>,
381        store: &'p mut Self::Store<'d>,
382        mut ctx: Context<'_, 'd>,
383    ) -> Result<Self::Output<'p, 'd>, Self::Error>
384    where
385        'd: 'p,
386        Self: 'd,
387    {
388        let (first, second) = self;
389        let (first_owned, second_owned) = owned;
390        let first = first
391            .try_build(first_owned, &mut store.0, ctx.reborrow())
392            .map_err(BranchError::First)?;
393        let second = second
394            .try_build(second_owned, &mut store.1, ctx)
395            .map_err(BranchError::Second)?;
396        Ok((first, second))
397    }
398}
399
400impl<A: Claimless, B: Claimless> Claimless for (A, B) {}
401
402impl<A: Direct, B: Direct> Direct for (A, B) {
403    fn build<'p, 'd>(self) -> Self::Output<'p, 'd>
404    where
405        'd: 'p,
406        Self: 'd,
407    {
408        let first = Direct::build(self.0);
409        let second = Direct::build(self.1);
410        (first, second)
411    }
412}
413
414const _: () = {
415    assert!(std::mem::size_of::<Context<'static, 'static>>() == std::mem::size_of::<usize>());
416    assert!(std::mem::align_of::<Context<'static, 'static>>() == std::mem::align_of::<usize>());
417    assert!(std::mem::size_of::<Value<usize>>() == std::mem::size_of::<usize>());
418    assert!(std::mem::align_of::<Value<usize>>() == std::mem::align_of::<usize>());
419    assert!(std::mem::needs_drop::<Value<String>>() == std::mem::needs_drop::<String>());
420};