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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//! Uninitialized / zeroed allocation API on [`Arena`].
//!
//! All public methods are documented on [`Arena`] itself; this file
//! groups the `alloc_uninit_*` / `alloc_zeroed_*` family together to
//! keep the central `mod.rs` smaller.
use core::mem::MaybeUninit;
use core::ptr::NonNull;
use allocator_api2::alloc::{AllocError, Allocator};
use super::{AllocFlavor, Arena, expect_alloc};
use crate::arc::Arc;
use crate::r#box::Box;
use crate::internal::drop_list::noop_drop_shim;
use crate::rc::Rc;
impl<A: Allocator + Clone> Arena<A> {
/// Allocate uninitialized space for a `T` and return an
/// [`Box<MaybeUninit<T>, A>`](crate::Box). The caller must
/// initialize the value (e.g., via [`MaybeUninit::write`]) before
/// calling [`Box::<MaybeUninit<T>, A>::assume_init`].
///
/// For types that need drop, this allocation reserves a `DropEntry`
/// slot in addition to the value bytes. Dropping the box without
/// `assume_init` is sound (no destructor runs).
///
/// # Panics
///
/// Panics if the backing allocator fails or if the data alignment is at least 32 KiB.
/// Use [`Self::try_alloc_uninit_box`] for a fallible variant.
#[must_use]
#[inline]
pub fn alloc_uninit_box<T>(&self) -> Box<MaybeUninit<T>, A> {
// Reuse the slice path so drop-aware types still reserve a noop entry.
// The closure-based init keeps over-aligned T's stack-materialized
// value out of the caller's frame, unlike a `try_alloc_inner_value`
// fast path which would require passing `MaybeUninit<T>` by value
// (and on Windows-under-coverage that materializes a 64 KiB-aligned
// slot in the caller's 1 MiB stack — see the `OverAligned` notes in
// `tests/bytemuck_integration.rs`).
let drop_fn = const { core::mem::needs_drop::<T>() }.then_some(noop_drop_shim as unsafe fn(*mut u8, usize));
let slice = self.alloc_slice_local_with_or_panic(1, AllocFlavor::Box, drop_fn, |_, dst: &mut MaybeUninit<MaybeUninit<T>>| {
dst.write(MaybeUninit::uninit());
});
let ptr = slice.as_ptr().cast::<MaybeUninit<T>>();
// SAFETY: helper allocated one MaybeUninit<T> and bumped the chunk refcount for this Box.
unsafe { Box::from_raw(NonNull::new_unchecked(ptr)) }
}
/// Fallible variant of [`Self::alloc_uninit_box`].
///
/// # Errors
///
/// Returns [`AllocError`] if the backing allocator fails or if the data alignment
/// is at least 32 KiB.
#[inline]
pub fn try_alloc_uninit_box<T>(&self) -> Result<Box<MaybeUninit<T>, A>, AllocError> {
// Reuse the slice path with `len = 1` so drop-aware types reserve
// the noop entry that `assume_init().into_rc()` later retargets.
// See `alloc_uninit_box` for why the value-path fast path is unsafe
// for over-aligned T on Windows.
let drop_fn = const { core::mem::needs_drop::<T>() }.then_some(noop_drop_shim as unsafe fn(*mut u8, usize));
let slice =
self.try_alloc_slice_local_with::<_, _, false>(1, AllocFlavor::Box, drop_fn, |_, dst: &mut MaybeUninit<MaybeUninit<T>>| {
dst.write(MaybeUninit::uninit());
})?;
let ptr = slice.as_ptr().cast::<MaybeUninit<T>>();
// SAFETY: helper allocated one MaybeUninit<T> and bumped the chunk refcount for this Box.
Ok(unsafe { Box::from_raw(NonNull::new_unchecked(ptr)) })
}
/// Like [`Self::alloc_uninit_box`] but the value bytes are zeroed.
///
/// # Panics
///
/// Panics if the backing allocator fails or if the data alignment is at least 32 KiB.
/// Use [`Self::try_alloc_zeroed_box`] for a fallible variant.
#[must_use]
#[inline]
pub fn alloc_zeroed_box<T>(&self) -> Box<MaybeUninit<T>, A> {
let drop_fn = const { core::mem::needs_drop::<T>() }.then_some(noop_drop_shim as unsafe fn(*mut u8, usize));
let slice = self.alloc_slice_local_with_or_panic(1, AllocFlavor::Box, drop_fn, |_, dst: &mut MaybeUninit<MaybeUninit<T>>| {
dst.write(MaybeUninit::zeroed());
});
let ptr = slice.as_ptr().cast::<MaybeUninit<T>>();
// SAFETY: helper allocated one MaybeUninit<T> and bumped the chunk refcount for this Box.
unsafe { Box::from_raw(NonNull::new_unchecked(ptr)) }
}
/// Fallible variant of [`Self::alloc_zeroed_box`].
///
/// # Errors
///
/// Returns [`AllocError`] if the backing allocator fails or if the data alignment
/// is at least 32 KiB.
#[inline]
pub fn try_alloc_zeroed_box<T>(&self) -> Result<Box<MaybeUninit<T>, A>, AllocError> {
let drop_fn = const { core::mem::needs_drop::<T>() }.then_some(noop_drop_shim as unsafe fn(*mut u8, usize));
let slice =
self.try_alloc_slice_local_with::<_, _, false>(1, AllocFlavor::Box, drop_fn, |_, dst: &mut MaybeUninit<MaybeUninit<T>>| {
dst.write(MaybeUninit::zeroed());
})?;
let ptr = slice.as_ptr().cast::<MaybeUninit<T>>();
// SAFETY: helper allocated one MaybeUninit<T> and bumped the chunk refcount for this Box.
Ok(unsafe { Box::from_raw(NonNull::new_unchecked(ptr)) })
}
/// Allocate uninitialized space for a `T` and return an
/// [`Rc<MaybeUninit<T>, A>`](crate::Rc).
///
/// For types that need drop, this allocation reserves a `DropEntry`
/// slot wired to a placeholder shim. Dropping the
/// `Rc<MaybeUninit<T>>` without
/// `Rc::<MaybeUninit<T>, A>::assume_init` is sound — the
/// placeholder shim is a no-op, so `T::drop` does not run on
/// uninitialized memory. `assume_init` rewrites the shim to
/// `drop_shim::<T>` so a subsequent `Rc<T>` drops the value
/// correctly.
///
/// # Panics
///
/// Panics if the backing allocator fails or if the data alignment is at least 32 KiB.
/// Use [`Self::try_alloc_uninit_rc`] for a fallible variant.
#[must_use]
#[inline]
pub fn alloc_uninit_rc<T>(&self) -> Rc<MaybeUninit<T>, A> {
// Reuse the slice path so drop-aware types still reserve a noop entry.
// See `alloc_uninit_box` for why the value-path fast path is unsafe
// for over-aligned T on Windows-under-coverage.
let drop_fn = const { core::mem::needs_drop::<T>() }.then_some(noop_drop_shim as unsafe fn(*mut u8, usize));
let slice = self.alloc_slice_local_with_or_panic(1, AllocFlavor::Rc, drop_fn, |_, dst: &mut MaybeUninit<MaybeUninit<T>>| {
dst.write(MaybeUninit::uninit());
});
let ptr = slice.as_ptr().cast::<MaybeUninit<T>>();
// SAFETY: helper initialized one MaybeUninit<T> and bumped the refcount for this Rc.
unsafe { Rc::from_value_ptr(NonNull::new_unchecked(ptr)) }
}
/// Fallible variant of [`Self::alloc_uninit_rc`].
///
/// # Errors
///
/// Returns [`AllocError`] if the backing allocator fails or if the data alignment
/// is at least 32 KiB.
#[inline]
pub fn try_alloc_uninit_rc<T>(&self) -> Result<Rc<MaybeUninit<T>, A>, AllocError> {
let drop_fn = const { core::mem::needs_drop::<T>() }.then_some(noop_drop_shim as unsafe fn(*mut u8, usize));
let slice =
self.try_alloc_slice_local_with::<_, _, false>(1, AllocFlavor::Rc, drop_fn, |_, dst: &mut MaybeUninit<MaybeUninit<T>>| {
dst.write(MaybeUninit::uninit());
})?;
let ptr = slice.as_ptr().cast::<MaybeUninit<T>>();
// SAFETY: helper initialized one MaybeUninit<T> and bumped the refcount for this Rc.
Ok(unsafe { Rc::from_value_ptr(NonNull::new_unchecked(ptr)) })
}
/// Like [`Self::alloc_uninit_rc`] but the value bytes are zeroed.
///
/// # Panics
///
/// Panics if the backing allocator fails or if the data alignment is at least 32 KiB.
/// Use [`Self::try_alloc_zeroed_rc`] for a fallible variant.
#[must_use]
#[inline]
pub fn alloc_zeroed_rc<T>(&self) -> Rc<MaybeUninit<T>, A> {
let drop_fn = const { core::mem::needs_drop::<T>() }.then_some(noop_drop_shim as unsafe fn(*mut u8, usize));
let slice = self.alloc_slice_local_with_or_panic(1, AllocFlavor::Rc, drop_fn, |_, dst: &mut MaybeUninit<MaybeUninit<T>>| {
dst.write(MaybeUninit::zeroed());
});
let ptr = slice.as_ptr().cast::<MaybeUninit<T>>();
// SAFETY: helper initialized one MaybeUninit<T> and bumped the refcount for this Rc.
unsafe { Rc::from_value_ptr(NonNull::new_unchecked(ptr)) }
}
/// Fallible variant of [`Self::alloc_zeroed_rc`].
///
/// # Errors
///
/// Returns [`AllocError`] if the backing allocator fails or if the data alignment
/// is at least 32 KiB.
#[inline]
pub fn try_alloc_zeroed_rc<T>(&self) -> Result<Rc<MaybeUninit<T>, A>, AllocError> {
let drop_fn = const { core::mem::needs_drop::<T>() }.then_some(noop_drop_shim as unsafe fn(*mut u8, usize));
let slice =
self.try_alloc_slice_local_with::<_, _, false>(1, AllocFlavor::Rc, drop_fn, |_, dst: &mut MaybeUninit<MaybeUninit<T>>| {
dst.write(MaybeUninit::zeroed());
})?;
let ptr = slice.as_ptr().cast::<MaybeUninit<T>>();
// SAFETY: helper initialized one MaybeUninit<T> and bumped the refcount for this Rc.
Ok(unsafe { Rc::from_value_ptr(NonNull::new_unchecked(ptr)) })
}
/// Allocate uninitialized space for a `T` and return an
/// [`Arc<MaybeUninit<T>, A>`](crate::Arc).
///
/// For types that need drop, this allocation reserves a `DropEntry`
/// slot wired to a placeholder shim. Dropping the
/// `Arc<MaybeUninit<T>>` without
/// `Arc::<MaybeUninit<T>, A>::assume_init` is sound — the
/// placeholder shim is a no-op, so `T::drop` does not run on
/// uninitialized memory. `assume_init` rewrites the shim to
/// `drop_shim::<T>` so a subsequent `Arc<T>` drops the value
/// correctly.
///
/// # Panics
///
/// Panics if the backing allocator fails or if the data alignment is at least 32 KiB.
/// Use [`Self::try_alloc_uninit_arc`] for a fallible variant.
#[must_use]
#[inline]
pub fn alloc_uninit_arc<T>(&self) -> Arc<MaybeUninit<T>, A>
where
A: Send + Sync,
{
if const { core::mem::needs_drop::<T>() } {
let drop_fn = Some(noop_drop_shim as unsafe fn(*mut u8, usize));
let slice = self.alloc_slice_shared_with_or_panic(1, drop_fn, |_, dst: &mut MaybeUninit<MaybeUninit<T>>| {
dst.write(MaybeUninit::uninit());
});
let ptr = slice.as_ptr().cast::<MaybeUninit<T>>();
// SAFETY: helper initialized one MaybeUninit<T> and accounted for this Arc.
unsafe { Arc::from_value_ptr(NonNull::new_unchecked(ptr)) }
} else {
// No-drop T: bypass the slice path's per-slot loop by reusing
// the Arc value path. `needs_drop::<MaybeUninit<T>>() == false`
// ⇒ no entry reserved; the closure's `MaybeUninit::uninit()`
// construction compiles to a no-op.
let ptr = self.alloc_inner_arc_with_or_panic::<MaybeUninit<T>, _>(MaybeUninit::uninit);
// SAFETY: helper initialized one MaybeUninit<T> and accounted for this Arc.
unsafe { Arc::from_value_ptr(ptr) }
}
}
/// Fallible variant of [`Self::alloc_uninit_arc`].
///
/// # Errors
///
/// Returns [`AllocError`] if the backing allocator fails or if the data alignment
/// is at least 32 KiB.
#[inline]
pub fn try_alloc_uninit_arc<T>(&self) -> Result<Arc<MaybeUninit<T>, A>, AllocError>
where
A: Send + Sync,
{
if const { core::mem::needs_drop::<T>() } {
let drop_fn = Some(noop_drop_shim as unsafe fn(*mut u8, usize));
let slice = self.try_alloc_slice_shared_with::<_, _, false>(1, drop_fn, |_, dst: &mut MaybeUninit<MaybeUninit<T>>| {
dst.write(MaybeUninit::uninit());
})?;
let ptr = slice.as_ptr().cast::<MaybeUninit<T>>();
// SAFETY: helper initialized one MaybeUninit<T> and accounted for this Arc.
Ok(unsafe { Arc::from_value_ptr(NonNull::new_unchecked(ptr)) })
} else {
let ptr = self.try_alloc_inner_arc_with::<MaybeUninit<T>, _>(MaybeUninit::uninit)?;
// SAFETY: helper initialized one MaybeUninit<T> and accounted for this Arc.
Ok(unsafe { Arc::from_value_ptr(ptr) })
}
}
/// Like [`Self::alloc_uninit_arc`] but the value bytes are zeroed.
///
/// # Panics
///
/// Panics if the backing allocator fails or if the data alignment is at least 32 KiB.
/// Use [`Self::try_alloc_zeroed_arc`] for a fallible variant.
#[must_use]
#[inline]
pub fn alloc_zeroed_arc<T>(&self) -> Arc<MaybeUninit<T>, A>
where
A: Send + Sync,
{
if const { core::mem::needs_drop::<T>() } {
let drop_fn = Some(noop_drop_shim as unsafe fn(*mut u8, usize));
let slice = self.alloc_slice_shared_with_or_panic(1, drop_fn, |_, dst: &mut MaybeUninit<MaybeUninit<T>>| {
dst.write(MaybeUninit::zeroed());
});
let ptr = slice.as_ptr().cast::<MaybeUninit<T>>();
// SAFETY: helper initialized one MaybeUninit<T> and accounted for this Arc.
unsafe { Arc::from_value_ptr(NonNull::new_unchecked(ptr)) }
} else {
let ptr = self.alloc_inner_arc_with_or_panic::<MaybeUninit<T>, _>(MaybeUninit::zeroed);
// SAFETY: helper initialized one MaybeUninit<T> and accounted for this Arc.
unsafe { Arc::from_value_ptr(ptr) }
}
}
/// Fallible variant of [`Self::alloc_zeroed_arc`].
///
/// # Errors
///
/// Returns [`AllocError`] if the backing allocator fails or if the data alignment
/// is at least 32 KiB.
#[inline]
pub fn try_alloc_zeroed_arc<T>(&self) -> Result<Arc<MaybeUninit<T>, A>, AllocError>
where
A: Send + Sync,
{
if const { core::mem::needs_drop::<T>() } {
let drop_fn = Some(noop_drop_shim as unsafe fn(*mut u8, usize));
let slice = self.try_alloc_slice_shared_with::<_, _, false>(1, drop_fn, |_, dst: &mut MaybeUninit<MaybeUninit<T>>| {
dst.write(MaybeUninit::zeroed());
})?;
let ptr = slice.as_ptr().cast::<MaybeUninit<T>>();
// SAFETY: helper initialized one MaybeUninit<T> and accounted for this Arc.
Ok(unsafe { Arc::from_value_ptr(NonNull::new_unchecked(ptr)) })
} else {
let ptr = self.try_alloc_inner_arc_with::<MaybeUninit<T>, _>(MaybeUninit::zeroed)?;
// SAFETY: helper initialized one MaybeUninit<T> and accounted for this Arc.
Ok(unsafe { Arc::from_value_ptr(ptr) })
}
}
/// Allocate `len` uninitialized `T` slots and return an
/// [`Rc<[MaybeUninit<T>], A>`](crate::Rc).
///
/// For element types that need drop, this allocation reserves a
/// slice-`DropEntry` slot wired to a placeholder shim. Dropping the
/// `Rc<[MaybeUninit<T>]>` without
/// [`Rc::<[MaybeUninit<T>], A>::assume_init`] is sound — the
/// placeholder shim is a no-op, so `T::drop` does not run on
/// uninitialized elements. `assume_init` rewrites the shim to
/// `slice_drop_shim::<T>` so the elements drop correctly via the
/// resulting `Rc<[T]>`.
///
/// # Panics
///
/// Panics if the backing allocator fails or if the data alignment is at least 32 KiB.
/// Use [`Self::try_alloc_uninit_slice_rc`] for a fallible variant.
#[must_use]
#[inline]
pub fn alloc_uninit_slice_rc<T>(&self, len: usize) -> Rc<[MaybeUninit<T>], A> {
expect_alloc(self.try_alloc_uninit_slice_rc(len))
}
/// Fallible variant of [`Self::alloc_uninit_slice_rc`].
///
/// # Errors
///
/// Returns [`AllocError`] if the backing allocator fails or if the data alignment
/// is at least 32 KiB.
#[inline]
pub fn try_alloc_uninit_slice_rc<T>(&self, len: usize) -> Result<Rc<[MaybeUninit<T>], A>, AllocError> {
let drop_fn = const { core::mem::needs_drop::<T>() }.then_some(noop_drop_shim as unsafe fn(*mut u8, usize));
let ptr =
self.try_alloc_slice_local_with::<_, _, false>(len, AllocFlavor::Rc, drop_fn, |_, dst: &mut MaybeUninit<MaybeUninit<T>>| {
dst.write(MaybeUninit::uninit());
})?;
// SAFETY: helper initialized all MaybeUninit slots and bumped the refcount for this Rc.
Ok(unsafe { Rc::from_value_ptr(ptr) })
}
/// Like [`Self::alloc_uninit_slice_rc`] but the slice bytes are zeroed.
///
/// # Panics
///
/// Panics if the backing allocator fails or if the data alignment is at least 32 KiB.
/// Use [`Self::try_alloc_zeroed_slice_rc`] for a fallible variant.
#[must_use]
#[inline]
pub fn alloc_zeroed_slice_rc<T>(&self, len: usize) -> Rc<[MaybeUninit<T>], A> {
expect_alloc(self.try_alloc_zeroed_slice_rc(len))
}
/// Fallible variant of [`Self::alloc_zeroed_slice_rc`].
///
/// # Errors
///
/// Returns [`AllocError`] if the backing allocator fails or if the data alignment
/// is at least 32 KiB.
#[inline]
pub fn try_alloc_zeroed_slice_rc<T>(&self, len: usize) -> Result<Rc<[MaybeUninit<T>], A>, AllocError> {
let drop_fn = const { core::mem::needs_drop::<T>() }.then_some(noop_drop_shim as unsafe fn(*mut u8, usize));
let ptr =
self.try_alloc_slice_local_with::<_, _, false>(len, AllocFlavor::Rc, drop_fn, |_, dst: &mut MaybeUninit<MaybeUninit<T>>| {
dst.write(MaybeUninit::zeroed());
})?;
// SAFETY: helper initialized all MaybeUninit slots and bumped the refcount for this Rc.
Ok(unsafe { Rc::from_value_ptr(ptr) })
}
/// Allocate `len` uninitialized `T` slots and return an
/// [`Arc<[MaybeUninit<T>], A>`](crate::Arc).
///
/// For element types that need drop, this allocation reserves a
/// slice-`DropEntry` slot wired to a placeholder shim. Dropping the
/// `Arc<[MaybeUninit<T>]>` without
/// [`Arc::<[MaybeUninit<T>], A>::assume_init`] is sound — the
/// placeholder shim is a no-op, so `T::drop` does not run on
/// uninitialized elements. `assume_init` rewrites the shim to
/// `slice_drop_shim::<T>` so the elements drop correctly via the
/// resulting `Arc<[T]>`.
///
/// # Panics
///
/// Panics if the backing allocator fails or if the data alignment is at least 32 KiB.
/// Use [`Self::try_alloc_uninit_slice_arc`] for a fallible variant.
#[must_use]
#[inline]
pub fn alloc_uninit_slice_arc<T>(&self, len: usize) -> Arc<[MaybeUninit<T>], A>
where
A: Send + Sync,
{
expect_alloc(self.try_alloc_uninit_slice_arc(len))
}
/// Fallible variant of [`Self::alloc_uninit_slice_arc`].
///
/// # Errors
///
/// Returns [`AllocError`] if the backing allocator fails or if the data alignment
/// is at least 32 KiB.
#[inline]
pub fn try_alloc_uninit_slice_arc<T>(&self, len: usize) -> Result<Arc<[MaybeUninit<T>], A>, AllocError>
where
A: Send + Sync,
{
let drop_fn = const { core::mem::needs_drop::<T>() }.then_some(noop_drop_shim as unsafe fn(*mut u8, usize));
let ptr = self.try_alloc_slice_shared_with::<_, _, false>(len, drop_fn, |_, dst: &mut MaybeUninit<MaybeUninit<T>>| {
dst.write(MaybeUninit::uninit());
})?;
// SAFETY: helper initialized all MaybeUninit slots and accounted for this Arc.
Ok(unsafe { Arc::from_value_ptr(ptr) })
}
/// Like [`Self::alloc_uninit_slice_arc`] but the slice bytes are zeroed.
///
/// # Panics
///
/// Panics if the backing allocator fails or if the data alignment is at least 32 KiB.
/// Use [`Self::try_alloc_zeroed_slice_arc`] for a fallible variant.
#[must_use]
#[inline]
pub fn alloc_zeroed_slice_arc<T>(&self, len: usize) -> Arc<[MaybeUninit<T>], A>
where
A: Send + Sync,
{
expect_alloc(self.try_alloc_zeroed_slice_arc(len))
}
/// Fallible variant of [`Self::alloc_zeroed_slice_arc`].
///
/// # Errors
///
/// Returns [`AllocError`] if the backing allocator fails or if the data alignment
/// is at least 32 KiB.
#[inline]
pub fn try_alloc_zeroed_slice_arc<T>(&self, len: usize) -> Result<Arc<[MaybeUninit<T>], A>, AllocError>
where
A: Send + Sync,
{
let drop_fn = const { core::mem::needs_drop::<T>() }.then_some(noop_drop_shim as unsafe fn(*mut u8, usize));
let ptr = self.try_alloc_slice_shared_with::<_, _, false>(len, drop_fn, |_, dst: &mut MaybeUninit<MaybeUninit<T>>| {
dst.write(MaybeUninit::zeroed());
})?;
// SAFETY: helper initialized all MaybeUninit slots and accounted for this Arc.
Ok(unsafe { Arc::from_value_ptr(ptr) })
}
/// Allocate `len` uninitialized `T` slots and return an
/// [`Box<[MaybeUninit<T>], A>`](crate::Box).
///
/// For element types that need drop, this allocation reserves a
/// slice-`DropEntry` slot wired to a placeholder shim. Dropping the
/// `Box<[MaybeUninit<T>]>` without
/// [`Box::<[MaybeUninit<T>], A>::assume_init`] is sound — the
/// placeholder shim is a no-op, so `T::drop` does not run on
/// uninitialized elements. `assume_init` rewrites the shim to
/// `slice_drop_shim::<T>` so the elements drop correctly via the
/// resulting `Box<[T]>`.
///
/// # Panics
///
/// Panics if the backing allocator fails or if the data alignment is at least 32 KiB.
/// Use [`Self::try_alloc_uninit_slice_box`] for a fallible variant.
#[must_use]
#[inline]
pub fn alloc_uninit_slice_box<T>(&self, len: usize) -> Box<[MaybeUninit<T>], A> {
expect_alloc(self.try_alloc_uninit_slice_box(len))
}
/// Fallible variant of [`Self::alloc_uninit_slice_box`].
///
/// # Errors
///
/// Returns [`AllocError`] if the backing allocator fails or if the data alignment
/// is at least 32 KiB.
#[inline]
pub fn try_alloc_uninit_slice_box<T>(&self, len: usize) -> Result<Box<[MaybeUninit<T>], A>, AllocError> {
// Reserve a `noop_drop_shim` slot for `T: needs_drop` so that
// `Box<[MaybeUninit<T>]>::assume_init().into_rc()` has an entry
// to retarget. See `try_alloc_uninit_box` for full rationale.
let drop_fn = const { core::mem::needs_drop::<T>() }.then_some(noop_drop_shim as unsafe fn(*mut u8, usize));
let ptr =
self.try_alloc_slice_local_with::<_, _, false>(len, AllocFlavor::Box, drop_fn, |_, dst: &mut MaybeUninit<MaybeUninit<T>>| {
dst.write(MaybeUninit::uninit());
})?;
// SAFETY: helper initialized all MaybeUninit slots and bumped the refcount for this Box.
Ok(unsafe { Box::from_raw_unsized(ptr) })
}
/// Like [`Self::alloc_uninit_slice_box`] but the slice bytes are zeroed.
///
/// # Panics
///
/// Panics if the backing allocator fails or if the data alignment is at least 32 KiB.
/// Use [`Self::try_alloc_zeroed_slice_box`] for a fallible variant.
#[must_use]
#[inline]
pub fn alloc_zeroed_slice_box<T>(&self, len: usize) -> Box<[MaybeUninit<T>], A> {
expect_alloc(self.try_alloc_zeroed_slice_box(len))
}
/// Fallible variant of [`Self::alloc_zeroed_slice_box`].
///
/// # Errors
///
/// Returns [`AllocError`] if the backing allocator fails or if the data alignment
/// is at least 32 KiB.
#[inline]
pub fn try_alloc_zeroed_slice_box<T>(&self, len: usize) -> Result<Box<[MaybeUninit<T>], A>, AllocError> {
// See `try_alloc_uninit_slice_box` for rationale.
let drop_fn = const { core::mem::needs_drop::<T>() }.then_some(noop_drop_shim as unsafe fn(*mut u8, usize));
let ptr =
self.try_alloc_slice_local_with::<_, _, false>(len, AllocFlavor::Box, drop_fn, |_, dst: &mut MaybeUninit<MaybeUninit<T>>| {
dst.write(MaybeUninit::zeroed());
})?;
// SAFETY: helper initialized all MaybeUninit slots and bumped the refcount for this Box.
Ok(unsafe { Box::from_raw_unsized(ptr) })
}
}
impl<A: Allocator + Clone> Arena<A> {
/// Allocate an uninitialized `MaybeUninit<T>` slot and return a
/// [`Pin<Box<MaybeUninit<T>, A>>`](core::pin::Pin). Pair with
/// [`Box::assume_init_pin`](crate::Box::assume_init_pin) once
/// initialization completes to obtain `Pin<Box<T, A>>` without
/// ever moving the value off-arena.
///
/// # Panics
///
/// Panics if the backing allocator fails or if `align_of::<T>()`
/// is at least 32 KiB.
#[must_use]
#[inline]
pub fn alloc_uninit_box_pin<T>(&self) -> core::pin::Pin<Box<MaybeUninit<T>, A>>
where
A: 'static,
{
Box::into_pin(self.alloc_uninit_box::<T>())
}
/// Fallible variant of [`Self::alloc_uninit_box_pin`].
///
/// # Errors
///
/// Returns [`AllocError`] if the backing allocator fails or if
/// `align_of::<T>()` is at least 32 KiB.
#[inline]
pub fn try_alloc_uninit_box_pin<T>(&self) -> Result<core::pin::Pin<Box<MaybeUninit<T>, A>>, AllocError>
where
A: 'static,
{
self.try_alloc_uninit_box::<T>().map(Box::into_pin)
}
/// Zeroed pinned uninit variant of [`Self::alloc_uninit_box_pin`].
///
/// # Panics
///
/// See [`Self::alloc_uninit_box_pin`].
#[must_use]
#[inline]
pub fn alloc_zeroed_box_pin<T>(&self) -> core::pin::Pin<Box<MaybeUninit<T>, A>>
where
A: 'static,
{
Box::into_pin(self.alloc_zeroed_box::<T>())
}
/// Fallible variant of [`Self::alloc_zeroed_box_pin`].
///
/// # Errors
///
/// See [`Self::alloc_uninit_box_pin`].
#[inline]
pub fn try_alloc_zeroed_box_pin<T>(&self) -> Result<core::pin::Pin<Box<MaybeUninit<T>, A>>, AllocError>
where
A: 'static,
{
self.try_alloc_zeroed_box::<T>().map(Box::into_pin)
}
/// `Rc` mirror of [`Self::alloc_uninit_box_pin`].
///
/// # Panics
///
/// See [`Self::alloc_uninit_box_pin`].
#[must_use]
#[inline]
pub fn alloc_uninit_rc_pin<T>(&self) -> core::pin::Pin<Rc<MaybeUninit<T>, A>>
where
A: 'static,
{
Rc::into_pin(self.alloc_uninit_rc::<T>())
}
/// Fallible variant of [`Self::alloc_uninit_rc_pin`].
///
/// # Errors
///
/// See [`Self::alloc_uninit_box_pin`].
#[inline]
pub fn try_alloc_uninit_rc_pin<T>(&self) -> Result<core::pin::Pin<Rc<MaybeUninit<T>, A>>, AllocError>
where
A: 'static,
{
self.try_alloc_uninit_rc::<T>().map(Rc::into_pin)
}
/// `Rc` mirror of [`Self::alloc_zeroed_box_pin`].
///
/// # Panics
///
/// See [`Self::alloc_uninit_box_pin`].
#[must_use]
#[inline]
pub fn alloc_zeroed_rc_pin<T>(&self) -> core::pin::Pin<Rc<MaybeUninit<T>, A>>
where
A: 'static,
{
Rc::into_pin(self.alloc_zeroed_rc::<T>())
}
/// Fallible variant of [`Self::alloc_zeroed_rc_pin`].
///
/// # Errors
///
/// See [`Self::alloc_uninit_box_pin`].
#[inline]
pub fn try_alloc_zeroed_rc_pin<T>(&self) -> Result<core::pin::Pin<Rc<MaybeUninit<T>, A>>, AllocError>
where
A: 'static,
{
self.try_alloc_zeroed_rc::<T>().map(Rc::into_pin)
}
/// `Arc` mirror of [`Self::alloc_uninit_box_pin`].
///
/// # Panics
///
/// See [`Self::alloc_uninit_box_pin`].
#[must_use]
#[inline]
pub fn alloc_uninit_arc_pin<T>(&self) -> core::pin::Pin<Arc<MaybeUninit<T>, A>>
where
A: Send + Sync + 'static,
{
Arc::into_pin(self.alloc_uninit_arc::<T>())
}
/// Fallible variant of [`Self::alloc_uninit_arc_pin`].
///
/// # Errors
///
/// See [`Self::alloc_uninit_box_pin`].
#[inline]
pub fn try_alloc_uninit_arc_pin<T>(&self) -> Result<core::pin::Pin<Arc<MaybeUninit<T>, A>>, AllocError>
where
A: Send + Sync + 'static,
{
self.try_alloc_uninit_arc::<T>().map(Arc::into_pin)
}
/// `Arc` mirror of [`Self::alloc_zeroed_box_pin`].
///
/// # Panics
///
/// See [`Self::alloc_uninit_box_pin`].
#[must_use]
#[inline]
pub fn alloc_zeroed_arc_pin<T>(&self) -> core::pin::Pin<Arc<MaybeUninit<T>, A>>
where
A: Send + Sync + 'static,
{
Arc::into_pin(self.alloc_zeroed_arc::<T>())
}
/// Fallible variant of [`Self::alloc_zeroed_arc_pin`].
///
/// # Errors
///
/// See [`Self::alloc_uninit_box_pin`].
#[inline]
pub fn try_alloc_zeroed_arc_pin<T>(&self) -> Result<core::pin::Pin<Arc<MaybeUninit<T>, A>>, AllocError>
where
A: Send + Sync + 'static,
{
self.try_alloc_zeroed_arc::<T>().map(Arc::into_pin)
}
}