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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//! Shared macro generating the body of the `bytemuck` / `zerocopy`
//! integration modules.
//!
//! The two ecosystems expose nearly identical zero-init traits
//! ([`bytemuck::Zeroable`] and [`zerocopy::FromZeros`]); the views
//! that expose safe zero-initialized arena allocations to user code
//! have identical method bodies and only differ in the trait bound.
//! Rather than maintain two ~290-line copies that drift over time,
//! both `bytemuck.rs` and `zerocopy.rs` invoke this macro with their
//! ecosystem-specific names. Method bodies live in exactly one
//! place; the generated code is byte-identical to a hand-written
//! per-module implementation.
/// Emit the `<View>` struct, its `Arena` accessor, and the full set
/// of safe zero-init alloc methods (`alloc`, `try_alloc`,
/// `alloc_slice`, `try_alloc_slice`, scalar/slice × `box`/`rc`/`arc`
/// variants) parameterized over the marker trait.
///
/// Parameters:
/// * `$View` — the public view struct name (`BytemuckView` or
/// `ZerocopyView`).
/// * `$Marker` — the trait path users must satisfy
/// (`bytemuck::Zeroable` or `zerocopy::FromZeros`).
/// * `$panic_msg` — the panic message string used by the cold
/// alloc-failure helper.
macro_rules! zero_init_view {
($View:ident, $Marker:path, $panic_msg:literal) => {
/// Zero-cost view over an [`Arena`](crate::Arena) for safe zero-initialized allocation.
///
/// Exposes safe zero-initialized allocation methods for types implementing
/// the marker trait. Obtained via [`Arena`](crate::Arena)'s ecosystem-specific accessor.
#[derive(Debug)]
pub struct $View<'a, A: ::allocator_api2::alloc::Allocator + Clone = ::allocator_api2::alloc::Global> {
arena: &'a $crate::Arena<A>,
}
impl<'a, A: ::allocator_api2::alloc::Allocator + Clone> $View<'a, A> {
#[inline]
pub(crate) const fn new(arena: &'a $crate::Arena<A>) -> Self {
Self { arena }
}
/// Allocate a zero-initialized `T` and return a mutable reference into the arena.
///
/// The returned reference's lifetime is tied to the arena.
///
/// # Panics
///
/// Panics if the backing allocator fails or if `T` requires alignment of 64 KiB or greater (which exceeds the arena chunk alignment).
#[must_use]
#[inline]
pub fn alloc<T: $Marker>(&self) -> &'a mut T {
match self.try_alloc::<T>() {
Ok(v) => v,
Err(_) => panic_alloc(),
}
}
/// Fallible variant of [`Self::alloc`].
///
/// # Errors
///
/// Returns [`AllocError`](::allocator_api2::alloc::AllocError) if the backing allocator fails or if `T` requires alignment
/// >= 64 KiB.
#[inline]
pub fn try_alloc<T: $Marker>(&self) -> Result<&'a mut T, ::allocator_api2::alloc::AllocError> {
let uninit: &mut ::core::mem::MaybeUninit<T> = self.arena.try_alloc_with(::core::mem::MaybeUninit::<T>::zeroed)?;
// SAFETY: the marker trait guarantees the all-zero bit pattern is a valid `T`.
Ok(unsafe { ::core::mem::MaybeUninit::assume_init_mut(uninit) })
}
/// Allocate a zero-initialized slice of `T` and return a mutable slice into the arena.
///
/// # Panics
///
/// Panics if the backing allocator fails or if `T` requires alignment of 64 KiB or greater.
#[must_use]
#[inline]
pub fn alloc_slice<T: $Marker>(&self, len: usize) -> &'a mut [T] {
match self.try_alloc_slice::<T>(len) {
Ok(v) => v,
Err(_) => panic_alloc(),
}
}
/// Fallible variant of [`Self::alloc_slice`].
///
/// # Errors
///
/// Returns [`AllocError`](::allocator_api2::alloc::AllocError) if the backing allocator fails or if `T` requires alignment
/// >= 64 KiB.
#[inline]
pub fn try_alloc_slice<T: $Marker>(&self, len: usize) -> Result<&'a mut [T], ::allocator_api2::alloc::AllocError> {
let slice: &mut [::core::mem::MaybeUninit<T>] = self
.arena
.try_alloc_slice_fill_with(len, |_| ::core::mem::MaybeUninit::<T>::zeroed())?;
// SAFETY: the marker trait guarantees that the all-zero bit pattern is a valid `T`,
// and the fill closure produced a slice of `len` zeroed elements.
Ok(unsafe { &mut *(::core::ptr::from_mut::<[::core::mem::MaybeUninit<T>]>(slice) as *mut [T]) })
}
/// Allocate a zero-initialized `T` and return a [`Box<T, A>`](crate::Box).
///
/// # Panics
///
/// Panics if the backing allocator fails or if `T` requires alignment of 32 KiB or greater (smart-pointer paths cap alignment at half the chunk alignment).
#[must_use]
#[inline]
pub fn alloc_box<T: $Marker>(&self) -> $crate::Box<T, A> {
match self.try_alloc_box::<T>() {
Ok(v) => v,
Err(_) => panic_alloc(),
}
}
/// Fallible variant of [`Self::alloc_box`].
///
/// # Errors
///
/// Returns [`AllocError`](::allocator_api2::alloc::AllocError) if the backing allocator fails or if `T` requires alignment
/// >= 32 KiB (smart-pointer paths cap alignment at half the chunk alignment).
#[inline]
pub fn try_alloc_box<T: $Marker>(&self) -> Result<$crate::Box<T, A>, ::allocator_api2::alloc::AllocError> {
let b = self.arena.try_alloc_zeroed_box::<T>()?;
// SAFETY: the marker trait guarantees the all-zero bit pattern is a valid `T`.
Ok(unsafe { b.assume_init() })
}
/// Allocate a zero-initialized `T` and return an [`Rc<T, A>`](crate::Rc).
///
/// # Panics
///
/// Panics if the backing allocator fails or if `T` requires alignment of 32 KiB or greater (smart-pointer paths cap alignment at half the chunk alignment).
#[must_use]
#[inline]
pub fn alloc_rc<T: $Marker>(&self) -> $crate::Rc<T, A> {
match self.try_alloc_rc::<T>() {
Ok(v) => v,
Err(_) => panic_alloc(),
}
}
/// Fallible variant of [`Self::alloc_rc`].
///
/// # Errors
///
/// Returns [`AllocError`](::allocator_api2::alloc::AllocError) if the backing allocator fails or if `T` requires alignment
/// >= 32 KiB (smart-pointer paths cap alignment at half the chunk alignment).
#[inline]
pub fn try_alloc_rc<T: $Marker>(&self) -> Result<$crate::Rc<T, A>, ::allocator_api2::alloc::AllocError> {
let rc = self.arena.try_alloc_zeroed_rc::<T>()?;
// SAFETY: the marker trait guarantees the all-zero bit pattern is a valid `T`.
Ok(unsafe { rc.assume_init() })
}
/// Allocate a zero-initialized `T` and return an [`Arc<T, A>`](crate::Arc).
///
/// # Panics
///
/// Panics if the backing allocator fails or if `T` requires alignment of 32 KiB or greater (smart-pointer paths cap alignment at half the chunk alignment).
#[must_use]
#[inline]
pub fn alloc_arc<T: $Marker + Send + Sync>(&self) -> $crate::Arc<T, A>
where
A: Send + Sync,
{
match self.try_alloc_arc::<T>() {
Ok(v) => v,
Err(_) => panic_alloc(),
}
}
/// Fallible variant of [`Self::alloc_arc`].
///
/// # Errors
///
/// Returns [`AllocError`](::allocator_api2::alloc::AllocError) if the backing allocator fails or if `T` requires alignment
/// >= 32 KiB (smart-pointer paths cap alignment at half the chunk alignment).
#[inline]
pub fn try_alloc_arc<T: $Marker + Send + Sync>(&self) -> Result<$crate::Arc<T, A>, ::allocator_api2::alloc::AllocError>
where
A: Send + Sync,
{
let arc = self.arena.try_alloc_zeroed_arc::<T>()?;
// SAFETY: the marker trait guarantees the all-zero bit pattern is a valid `T`.
Ok(unsafe { arc.assume_init() })
}
/// Allocate a zero-initialized slice of `T` and return an [`Rc<[T], A>`](crate::Rc).
///
/// # Panics
///
/// Panics if the backing allocator fails or if `T` requires alignment of 32 KiB or greater (smart-pointer paths cap alignment at half the chunk alignment).
#[must_use]
#[inline]
pub fn alloc_slice_rc<T: $Marker>(&self, len: usize) -> $crate::Rc<[T], A> {
match self.try_alloc_slice_rc::<T>(len) {
Ok(v) => v,
Err(_) => panic_alloc(),
}
}
/// Fallible variant of [`Self::alloc_slice_rc`].
///
/// # Errors
///
/// Returns [`AllocError`](::allocator_api2::alloc::AllocError) if the backing allocator fails or if `T` requires alignment
/// >= 32 KiB (smart-pointer paths cap alignment at half the chunk alignment).
#[inline]
pub fn try_alloc_slice_rc<T: $Marker>(&self, len: usize) -> Result<$crate::Rc<[T], A>, ::allocator_api2::alloc::AllocError> {
let rc = self.arena.try_alloc_zeroed_slice_rc::<T>(len)?;
// SAFETY: the marker trait guarantees the all-zero bit pattern is valid for every element.
Ok(unsafe { rc.assume_init() })
}
/// Allocate a zero-initialized slice of `T` and return an [`Arc<[T], A>`](crate::Arc).
///
/// # Panics
///
/// Panics if the backing allocator fails or if `T` requires alignment of 32 KiB or greater (smart-pointer paths cap alignment at half the chunk alignment).
#[must_use]
#[inline]
pub fn alloc_slice_arc<T: $Marker + Send + Sync>(&self, len: usize) -> $crate::Arc<[T], A>
where
A: Send + Sync,
{
match self.try_alloc_slice_arc::<T>(len) {
Ok(v) => v,
Err(_) => panic_alloc(),
}
}
/// Fallible variant of [`Self::alloc_slice_arc`].
///
/// # Errors
///
/// Returns [`AllocError`](::allocator_api2::alloc::AllocError) if the backing allocator fails or if `T` requires alignment
/// >= 32 KiB (smart-pointer paths cap alignment at half the chunk alignment).
#[inline]
pub fn try_alloc_slice_arc<T: $Marker + Send + Sync>(
&self,
len: usize,
) -> Result<$crate::Arc<[T], A>, ::allocator_api2::alloc::AllocError>
where
A: Send + Sync,
{
let arc = self.arena.try_alloc_zeroed_slice_arc::<T>(len)?;
// SAFETY: the marker trait guarantees the all-zero bit pattern is valid for every element.
Ok(unsafe { arc.assume_init() })
}
/// Allocate a zero-initialized slice of `T` and return a [`Box<[T], A>`](crate::Box).
///
/// # Panics
///
/// Panics if the backing allocator fails or if `T` requires alignment of 32 KiB or greater (smart-pointer paths cap alignment at half the chunk alignment).
#[must_use]
#[inline]
pub fn alloc_slice_box<T: $Marker>(&self, len: usize) -> $crate::Box<[T], A> {
match self.try_alloc_slice_box::<T>(len) {
Ok(v) => v,
Err(_) => panic_alloc(),
}
}
/// Fallible variant of [`Self::alloc_slice_box`].
///
/// # Errors
///
/// Returns [`AllocError`](::allocator_api2::alloc::AllocError) if the backing allocator fails or if `T` requires alignment
/// >= 32 KiB (smart-pointer paths cap alignment at half the chunk alignment).
#[inline]
pub fn try_alloc_slice_box<T: $Marker>(&self, len: usize) -> Result<$crate::Box<[T], A>, ::allocator_api2::alloc::AllocError> {
let b = self.arena.try_alloc_zeroed_slice_box::<T>(len)?;
// SAFETY: the marker trait guarantees the all-zero bit pattern is valid for every element.
Ok(unsafe { b.assume_init() })
}
}
#[cold]
#[inline(never)]
#[allow(
clippy::panic,
reason = "intentional panic on allocation failure matching Arena's own panic_alloc"
)]
fn panic_alloc() -> ! {
panic!($panic_msg);
}
};
}
pub(crate) use zero_init_view;