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
//! Sequential fallbacks for [`BStackRaiiAllocator::alloc_many`] /
//! [`BStackRaiiAllocator::free_many`].
//!
//! ## How bulk dispatch works
//!
//! bstack provides atomic [`BStackBulkAllocator::alloc_bulk`] /
//! [`dealloc_bulk`](bstack::BStackBulkAllocator::dealloc_bulk), but only some
//! allocators implement it, and **all** of `bstack_raii` is generic over
//! `A: BStackRaiiAllocator`. On stable Rust a generic function cannot dispatch on
//! whether its concrete `A` *also* implements `BStackBulkAllocator` (that needs the
//! nightly `specialization` feature; autoref specialization collapses to the
//! fallback in generic code), so we don't try. Instead `alloc_many` / `free_many`
//! are **provided methods on [`BStackRaiiAllocator`]**: the default bodies are these
//! sequential helpers, and each bulk-capable allocator *overrides* them to call
//! `alloc_bulk` / `dealloc_bulk` (see the impls in [`crate::io_core::wal`]). Ordinary trait
//! dispatch then picks the override at monomorphization — the fast path flows
//! through generic code, and non-bulk allocators (e.g. `FirstFit`) keep the
//! sequential path — with no bound leaking onto the public API.
//!
//! Atomicity of the fallback:
//!
//! * each individual `alloc` / `dealloc` **is** crash-atomic (allocator contract);
//! * the *set* is not — a crash mid-sequence orphans the blocks done so far (a
//! leak, never a torn structure), which the WAL layer reclaims.
use Error;
use ;
use ;
use crateBStackRaiiAllocator;
use cratedealloc_range;
/// The error a partial [`free_many`](BStackRaiiAllocator::free_many) returns: the
/// first underlying failure, plus **every range whose free did not cleanly
/// complete**.
///
/// The sequential fallback frees every range it can and collects the ones whose
/// `dealloc` returned an error, rather than stopping at the first error and
/// leaving an unknown suffix allocated. [`unfreed`](Self::unfreed)
/// is a **superset** of the ranges still allocated: every range genuinely still
/// live is named (so the caller never silently leaks one), but a single
/// `dealloc` is not atomic against a mid-operation I/O fault, so a named range
/// may in fact have been reclaimed before a follow-up step failed. Treat the set
/// as "needs checking": account for these ranges, and verify liveness before
/// re-freeing (a blind retry of the whole set could double-free a
/// freed-but-reported one).
///
/// It is wrapped in the returned [`io::Error`] as the source, so a caller that
/// only propagates keeps the plain error and a caller that wants to recover
/// downcasts:
///
/// ```ignore
/// if let Some(fme) = err.get_ref().and_then(|e| e.downcast_ref::<FreeManyError>()) {
/// for r in fme.unfreed() { /* retry / account for r */ }
/// }
/// ```
/// Sequential fallback for [`BStackRaiiAllocator::alloc_many`]: allocate one block
/// per entry in `sizes`, in order. On any failure the blocks already allocated are
/// freed (reverse order) before the error is returned, so a partial allocation
/// never leaks within the call.
pub
/// Sequential fallback for [`BStackRaiiAllocator::free_many`]: free every range in
/// turn, **continuing past a failure** and collecting the ranges it could not
/// free rather than stopping at the first error. On any failure
/// it returns a [`FreeManyError`] (wrapped in the [`io::Error`]) naming every
/// unfreed range, so the caller can retry exactly those without double-freeing
/// the ones already reclaimed.
pub
/// The bulk override shared by every [`BStackBulkAllocator`]: allocate all `sizes`
/// as one atomic [`alloc_bulk`](BStackBulkAllocator::alloc_bulk) and hand back their
/// ranges. Either all blocks are allocated or none is (and the store is unchanged);
/// a crash mid-op is reclaimed by the allocator's own recovery, so this needs no WAL.
/// The `atomic_bulk`-path counterpart of [`seq_alloc_many`], for a
/// [`BStackBulkAllocator`]; wired in by `BStackRaiiAllocator`'s bulk overrides.
pub
/// The bulk override shared by every [`BStackBulkAllocator`]: free all `ranges` as
/// one atomic [`dealloc_bulk`](BStackBulkAllocator::dealloc_bulk). Reconstructs an
/// owned slice per range (as [`crate::io_core::teardown::dealloc_range`] does) and
/// frees them together; on failure the error's `source` is surfaced (the un-freed
/// handles it carries back are dropped — they are non-RAII, so dropping does not
/// double-free). The `atomic_bulk`-path counterpart of [`seq_free_many`].
pub