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
//! Segments and spans — where a pointer's identity comes from.
//!
//! A **segment** is a 4 MiB region mapped at a 4 MiB-aligned address. It
//! is cut into 64 **spans** of 64 KiB; span 0 holds the segment header
//! and the other 63 serve allocations, one size class each.
//!
//! That geometry is the whole reason there are no per-allocation
//! headers. Masking a pointer with `!(SEGMENT_BYTES - 1)` gives the
//! segment, the offset gives the span index, and the span's metadata
//! gives the class — so `dealloc` recovers everything it needs from the
//! address itself. glibc has to store a size beside every chunk because
//! C's `free` is not told one; we are, and even when we were not, the
//! address would answer.
//!
//! Reference: mimalloc's segment/page split (`segment.c`), and Go's
//! `mheap` arena indexing. The divergence from both is that a segment
//! here is owned by exactly one shard for its whole life — kevy pins a
//! shard per core, so ownership never has to be negotiated.
//!
//! # Examples
//!
//! ```
//! use kevy_alloc::{Heap, segment};
//! let mut heap = Heap::new(0);
//! let p = heap.alloc(100, 8).ok_or("no mapping")?;
//! // everything `dealloc` needs comes from the address: segment, span, class
//! // SAFETY: `p` is a live small slot, so it lies inside a segment.
//! let seg = unsafe { segment::segment_of(p).as_ref() };
//! let span = &seg.spans()[segment::span_index_of(p)];
//! assert_eq!(kevy_alloc::class::size_of(usize::from(span.class())), 104);
//! // SAFETY: `p` came from this heap with this size and alignment.
//! unsafe { heap.dealloc(p, 100, 8) };
//! # Ok::<(), &str>(())
//! ```
use NonNull;
use ;
use crate;
pub use crate;
pub use ForeignTally;
/// Bytes per segment. Power of two: the mask is the lookup.
///
/// ```
/// use kevy_alloc::{Heap, segment::SEGMENT_BYTES};
/// let mut heap = Heap::new(0);
/// let p = heap.alloc(64, 8).ok_or("no mapping")?;
/// // masking off the low bits finds the segment base
/// let base = p.as_ptr() as usize & !(SEGMENT_BYTES - 1);
/// assert_eq!(base % SEGMENT_BYTES, 0);
/// assert!(p.as_ptr() as usize - base < SEGMENT_BYTES);
/// // SAFETY: `p` came from this heap with this size and alignment.
/// unsafe { heap.dealloc(p, 64, 8) };
/// # Ok::<(), &str>(())
/// ```
pub const SEGMENT_BYTES: usize = 4 * 1024 * 1024;
/// Spans in a segment, including the header span.
///
/// ```
/// use kevy_alloc::{class::SPAN_BYTES, segment::{SEGMENT_BYTES, SPANS_PER_SEGMENT}};
/// assert_eq!(SPANS_PER_SEGMENT, 64);
/// assert_eq!(SPANS_PER_SEGMENT * SPAN_BYTES, SEGMENT_BYTES);
/// ```
pub const SPANS_PER_SEGMENT: usize = SEGMENT_BYTES / SPAN_BYTES;
/// Span index 0 is the header; allocation spans start at 1.
///
/// ```
/// use kevy_alloc::{Heap, segment::{FIRST_DATA_SPAN, span_index_of}};
/// let mut heap = Heap::new(0);
/// let p = heap.alloc(64, 8).ok_or("no mapping")?;
/// // span 0 is the header, so no slot is ever handed out there
/// assert!(span_index_of(p) >= FIRST_DATA_SPAN);
/// // SAFETY: `p` came from this heap with this size and alignment.
/// unsafe { heap.dealloc(p, 64, 8) };
/// # Ok::<(), &str>(())
/// ```
pub const FIRST_DATA_SPAN: usize = 1;
/// Identifies a live segment header. A pointer that masks to something
/// without this word is not ours, and that is a bug in the caller
/// rather than something to paper over.
const MAGIC: u64 = 0x6b65_7679_616c_6c63; // "kevyallc"
/// The header at the base of every segment.
///
/// ```
/// use kevy_alloc::{Heap, segment::{Segment, segment_of}};
/// let mut heap = Heap::new(3);
/// let p = heap.alloc(64, 8).ok_or("no mapping")?;
/// // SAFETY: `p` is a live small slot, so it lies inside a segment.
/// let seg: &Segment = unsafe { segment_of(p).as_ref() };
/// assert!(seg.is_valid());
/// assert_eq!(seg.owner(), 3);
/// // SAFETY: `p` came from this heap with this size and alignment.
/// unsafe { heap.dealloc(p, 64, 8) };
/// # Ok::<(), &str>(())
/// ```
/// Recover the segment owning `ptr`.
///
/// # Safety
/// `ptr` must be a slot address previously handed out by a segment of
/// this allocator (that is, not from the direct-mapping path).
///
/// ```
/// use kevy_alloc::{Heap, segment::{SEGMENT_BYTES, segment_of}};
/// let mut heap = Heap::new(0);
/// let p = heap.alloc(64, 8).ok_or("no mapping")?;
/// // SAFETY: `p` came from a segment, not from the direct-mapping path.
/// let seg = unsafe { segment_of(p) };
/// assert_eq!(seg.as_ptr() as usize, p.as_ptr() as usize & !(SEGMENT_BYTES - 1));
/// // SAFETY: `p` came from this heap with this size and alignment.
/// unsafe { heap.dealloc(p, 64, 8) };
/// # Ok::<(), &str>(())
/// ```
pub unsafe
/// The span index within a segment holding `ptr`.
///
/// ```
/// use kevy_alloc::{Heap, segment::{FIRST_DATA_SPAN, SPANS_PER_SEGMENT, span_index_of}};
/// let mut heap = Heap::new(0);
/// let p = heap.alloc(64, 8).ok_or("no mapping")?;
/// assert!((FIRST_DATA_SPAN..SPANS_PER_SEGMENT).contains(&span_index_of(p)));
/// // SAFETY: `p` came from this heap with this size and alignment.
/// unsafe { heap.dealloc(p, 64, 8) };
/// # Ok::<(), &str>(())
/// ```
/// The slot index within its span holding `ptr`, for a given class.
///
/// ```
/// use kevy_alloc::{Heap, class::index_of, segment::slot_index_of};
/// let mut heap = Heap::new(0);
/// let c = index_of(64, 8).ok_or("class")?;
/// let a = heap.alloc(64, 8).ok_or("no mapping")?;
/// let b = heap.alloc(64, 8).ok_or("no mapping")?;
/// // a fresh span hands out neighbouring slots, lowest first
/// assert_eq!(slot_index_of(b, c), slot_index_of(a, c) + 1);
/// // SAFETY: both came from this heap with this size and alignment.
/// unsafe { heap.dealloc(a, 64, 8); heap.dealloc(b, 64, 8) };
/// # Ok::<(), &str>(())
/// ```
/// Where [`Segment::splice_foreign`] stores the requested size inside a free slot,
/// clear of the link that occupies the first word.
///
/// ```
/// use kevy_alloc::{class::CLASSES, segment::FOREIGN_SIZE_OFFSET};
/// // the link word comes first; the size (a u32) must fit in every class
/// assert_eq!(FOREIGN_SIZE_OFFSET, core::mem::size_of::<*mut u8>());
/// assert!(FOREIGN_SIZE_OFFSET + 4 <= CLASSES[0] as usize);
/// ```
pub const FOREIGN_SIZE_OFFSET: usize = ;
/// Read back the requested size a foreign free recorded.
///
/// # Safety
/// `slot` must still be on a foreign list, untouched since
/// [`Segment::splice_foreign`] wrote it.
///
/// ```
/// use kevy_alloc::{Heap, segment::{self, FOREIGN_SIZE_OFFSET, foreign_requested}};
/// let mut owner = Heap::new(1);
/// let p = owner.alloc(100, 8).ok_or("no mapping")?;
/// // SAFETY: `p` is a live 104-byte slot, ours to overwrite.
/// unsafe { p.as_ptr().add(FOREIGN_SIZE_OFFSET).cast::<u32>().write(100) };
/// // SAFETY: `p` is a live small slot, so it lies inside a segment.
/// let seg = unsafe { segment::segment_of(p).as_ref() };
/// // SAFETY: a one-slot chain of this segment's slot, with its sums.
/// unsafe { seg.splice_foreign(p.as_ptr(), p.as_ptr(), 100, 104) };
/// // SAFETY: `p` is queued and untouched since the splice.
/// assert_eq!(unsafe { foreign_requested(p) }, 100);
/// owner.drain_foreign();
/// # Ok::<(), &str>(())
/// ```
pub unsafe