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
use cratePageSize;
use NonZeroUsize;
/// Structured failure reason for [`RegionInit::try_init`](RegionInit::try_init).
///
/// These describe the *checkable* preconditions of initialisation. The remaining
/// unverifiable preconditions live in the `# Safety` contract instead.
/// Trait for physical frame allocators.
///
/// # Contract guarantees
///
/// [`allocate_physical`](Self::allocate_physical) is a safe method whose returned
/// address may later be passed to unsafe deallocation paths. Implementors must
/// guarantee that every successful allocation:
///
/// * returns the base of `count` contiguous frames of size `ps`;
/// * returns a base address aligned to `ps.bytes()`;
/// * represents the whole byte range `[base, base + count * ps.bytes())` without
/// arithmetic overflow;
/// * transfers exclusive ownership of that range to the caller until it is
/// returned with [`deallocate_physical`](Self::deallocate_physical); and
/// * never returns a range that overlaps any still-live allocation.
///
/// On `Err`, the allocator must not transfer ownership of any frame.
///
/// # Safety
///
/// This is an unsafe trait because safe callers and generic wrappers may rely on
/// successful allocations being real, unique physical frames. Implementors must
/// ensure that every successful allocation is backed by physical memory managed
/// by this allocator and exclusively owned until deallocation.
///
/// # Errors
///
/// Unsupported page sizes, unrepresentable requests, requests too large for the
/// allocator's configuration, and exhausted memory must be reported with
/// [`AllocError`] rather than by returning an address that violates the contract.
pub unsafe
/// A physically-contiguous, base-frame-aligned range of usable RAM.
/// Memory-map-aware initialisation interface for physical frame allocators.
///
/// [`try_init`](RegionInit::try_init) configures the allocator's metadata over the
/// whole *span* and frees only the `usable` ranges; holes between them stay
/// reserved (never handed out).
///
/// # Contract guarantees
///
/// * **On `Err`, the allocator is untouched** - it remains in the valid empty
/// state it had before the call, and a corrected retry is permitted.
/// * **At most one successful call.** A returned `Ok(())` consumes the one-time
/// initialisation; a returned `Err(_)` does not, so a caller may retry with
/// corrected arguments after any failure.
///
/// # Safety
///
/// These preconditions are unverifiable from the arguments and so remain the
/// caller's responsibility (violating any of them is undefined behaviour, not an
/// [`InitError`]):
///
/// * Backends reach the managed memory through a [`Provenance`](crate::Provenance)
/// strategy of their own, obtaining pointers for the physical addresses they
/// are given; the caller supplies no mapping pointer. Each backend's `Provenance`
/// `# Safety` contract states what it requires of that mapping.
/// * Each `usable` range's memory must be exclusively owned and not aliased while
/// registered.
/// * Must be called single-threaded, and the allocator must be published to
/// other threads with a happens-before edge (thread spawn, mutex, or a
/// Release store / Acquire load of a ready flag) before any concurrent use.
///
/// # Errors
///
/// The *checkable* preconditions are reported rather than assumed. See
/// [`InitError`] for the full list; in summary `try_init` returns:
///
/// * [`InitError::AlreadyInitialized`] if a previous call already succeeded;
/// * [`InitError::Misaligned`] if `phys_base` is not base-frame aligned (it need
/// not itself be usable RAM);
/// * [`InitError::InvalidSpan`] if `span_len` is zero, not a base-frame multiple,
/// or overflows the address space;
/// * [`InitError::InvalidUsable`] if any `usable` range is empty, misaligned, out
/// of order, overlapping, or escapes the span;
/// * [`InitError::MetadataWontFit`] if no usable range can host the allocator's
/// in-pool metadata.
///
/// An empty `usable` slice is permitted by this interface, but some
/// implementations may reject it with [`InitError::MetadataWontFit`]: an allocator
/// that keeps its metadata inside the managed pool needs enough usable RAM to host
/// that metadata.
pub unsafe
/// All three figures are in **bytes**. They are diagnostics - utilisation
/// reporting, fragmentation tracking, tests - not a basis for allocation
/// decisions: each is **non-linearizable** under concurrent use, so a value may be
/// stale the instant it is returned. Gated behind the `stats` feature.