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
//! Cross-file teardown & deep-clone mechanism for `Foreign<T>` fields — the
//! cross-file counterparts of [`teardown`](crate::io_core::teardown) /
//! [`clone`](crate::io_core::clone).
//!
//! Each helper runs a `Foreign<T>` field's per-kind teardown or clone in *whichever
//! file the target lives in*, selected entirely by the `alloc` handed in: the local
//! allocator for a `SELF` target, or a
//! [`ForeignHostAllocator`](crate::registry::ForeignHostAllocator) for a cross-file
//! one. Because the whole teardown/clone machinery (`OwnedRef` /
//! `BStackShared::drop_strong_ref` / `WeakRef`, `try_clone_in`, and the recursive
//! `__bstack_drop_children`) is generic over `A: BStackRaiiAllocator`, the same code
//! frees or copies the target in its own file with no duplication — reads/writes go
//! through `alloc.stack()`, and frees are tagged with `alloc.wal_file_id()`.
//!
//! The `BStackBlock` / `BStackShared` / `BStackWeakable` `foreign_drop_*` /
//! `foreign_clone_*` trait methods delegate straight here; the generated field code
//! picks the helper by the field's annotation (`#[bstack_ref]` has none — a foreign
//! ref owns nothing).
use io;
use BStackRange;
use crateBStackRaiiAllocator;
use crate;
use crateNonNullOffset;
use crate;
use crate;
use crate;
/// Tear down an `#[bstack_owned] Foreign<T>` target: free the block at `offset`
/// (and, recursively, its own children) in the file `alloc` addresses.
///
/// # Safety
/// `offset` names a live `T` block, in the file `alloc` addresses, exclusively owned
/// by this foreign pointer (freed exactly once).
pub unsafe
/// Tear down an `#[bstack_strong] Foreign<T>` target: decrement the strong count at
/// `offset` (the target's *data* block) and, at zero, free it in the file `alloc`
/// addresses. `T` must be a shared block.
///
/// # Safety
/// `offset` names a live shared `T` data block in the file `alloc` addresses, holding
/// one strong reference on behalf of this foreign pointer.
pub unsafe
/// Tear down a `#[bstack_weak] Foreign<T>` target: decrement the weak count in the
/// *control* block at `ctrl_offset` and, at zero, free the control block in the file
/// `alloc` addresses. The data block is never touched. `T` must be weakable, and the
/// foreign pointer stores the **control** offset (as an in-file weak field does).
///
/// # Safety
/// `ctrl_offset` names a live `T::Control` block in the file `alloc` addresses,
/// holding one weak reference on behalf of this foreign pointer.
pub unsafe
// ---------------------------------------------------------------------------
// Cross-file deep-clone helpers (the mirror of the teardown helpers above).
//
// A `Foreign<T>` field's clone acts on the target *in the target's own file* per its
// annotation: `#[bstack_owned]` deep-copies the target (a fresh block on that file,
// the pointer repointed); `#[bstack_strong]` / `#[bstack_weak]` share the same target
// and bump its count; `#[bstack_ref]` aliases (byte-copied — no helper). A `SELF`
// pointer instead folds into the *home* clone plan (atomic with the home commit); the
// generated code picks that path. These helpers cover the cross-file case.
//
// **Atomicity across files is best-effort (option-1):** the foreign side is touched
// eagerly (its own commit for `owned`, an atomic increment for `strong`/`weak`) BEFORE
// the home clone commits, and a home failure afterwards does not undo it. That always
// errs toward **over-provisioning** — an orphaned fresh block, or an over-count — which
// leaks, never toward an under-count (which would be a premature free / double-free).
// A target file that is not currently attached makes the clone **error** (aliasing an
// owning pointer would create a second owner ⇒ later double-free); the generated code
// enforces that before calling these.
// ---------------------------------------------------------------------------
/// Deep-clone an `#[bstack_owned] Foreign<T>` target at `offset` in the file `alloc`
/// addresses; returns the new copy's offset. Self-contained atomic commit on that
/// file; eager (a later home-clone failure leaks the new block).
///
/// # Safety
/// `offset` names a live `T` block, in the file `alloc` addresses, owned by this
/// foreign pointer.
pub unsafe
/// Bump the strong count of an `#[bstack_strong] Foreign<T>` target at `offset` (its
/// data block) in the file `alloc` addresses — the strong reference the clone
/// acquires. Eager atomic increment.
///
/// # Safety
/// `offset` names a live shared `T` data block in the file `alloc` addresses.
pub unsafe
/// Bump the weak count of a `#[bstack_weak] Foreign<T>` target's control block at
/// `ctrl_offset` in the file `alloc` addresses — the weak reference the clone
/// acquires. Eager atomic increment. (`T` documents the intended weakable target; the
/// increment needs only the offset.)
///
/// # Safety
/// `ctrl_offset` names a live `T::Control` block in the file `alloc` addresses.
// `T` documents the target and keeps the signature parallel to the other three
// helpers (and to `BStackWeakable::foreign_clone_weak`); the increment is offset-only.
pub unsafe