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
use core::{
alloc::Layout,
ffi::CStr,
fmt::{self, Debug},
marker::PhantomData,
mem::MaybeUninit,
panic::{RefUnwindSafe, UnwindSafe},
ptr::NonNull,
};
#[cfg(feature = "nightly-clone-to-uninit")]
use core::clone::CloneToUninit;
use crate::{
BaseAllocator, BumpBox, BumpClaimGuard, BumpScopeGuard, Checkpoint, ErrorBehavior, NoDrop, SizedTypeProperties,
alloc::{AllocError, Allocator},
allocator_impl, down_align_usize, maybe_default_allocator,
owned_slice::OwnedSlice,
polyfill::{non_null, transmute_mut, transmute_ref, transmute_value},
raw_bump::RawBump,
settings::{BumpAllocatorSettings, BumpSettings, MinimumAlignment, SupportedMinimumAlignment},
stats::{AnyStats, Stats},
traits::{
self, BumpAllocator, BumpAllocatorCore, BumpAllocatorScope, BumpAllocatorTyped, BumpAllocatorTypedScope,
MutBumpAllocatorTypedScope,
},
up_align_usize_unchecked,
};
#[cfg(feature = "alloc")]
use crate::alloc::Global;
#[cfg(feature = "panic-on-alloc")]
use crate::panic_on_error;
macro_rules! make_type {
($($allocator_parameter:tt)*) => {
/// A bump allocation scope.
///
/// A `BumpScope`'s allocations are live for `'a`, which is the lifetime of its associated `BumpScopeGuard` or `scoped` closure.
///
/// `BumpScope` has mostly same api as [`Bump`].
///
/// This type is provided as a parameter to the closure of [`scoped`], or created
/// by [`BumpScopeGuard::scope`]. A [`Bump`] can also be turned into a `BumpScope` using
/// [`as_scope`], [`as_mut_scope`] or `from` / `into`.
///
/// [`scoped`]: crate::traits::BumpAllocator::scoped
/// [`BumpScopeGuard::scope`]: crate::BumpScopeGuard::scope
/// [`Bump`]: crate::Bump
/// [`as_scope`]: crate::Bump::as_scope
/// [`as_mut_scope`]: crate::Bump::as_mut_scope
/// [`reset`]: crate::Bump::reset
#[repr(transparent)]
pub struct BumpScope<'a, $($allocator_parameter)*, S = BumpSettings>
where
S: BumpAllocatorSettings,
{
pub(crate) raw: RawBump<A, S>,
/// Marks the lifetime of the mutably borrowed `BumpScopeGuard`.
pub(crate) marker: PhantomData<&'a ()>,
}
};
}
maybe_default_allocator!(make_type);
impl<A, S> UnwindSafe for BumpScope<'_, A, S>
where
A: RefUnwindSafe,
S: BumpAllocatorSettings,
{
}
impl<A, S> RefUnwindSafe for BumpScope<'_, A, S>
where
A: RefUnwindSafe,
S: BumpAllocatorSettings,
{
}
impl<A, S> Debug for BumpScope<'_, A, S>
where
S: BumpAllocatorSettings,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
AnyStats::from(self.stats()).debug_format("BumpScope", f)
}
}
impl<A, S> BumpScope<'_, A, S>
where
A: BaseAllocator<S::GuaranteedAllocated>,
S: BumpAllocatorSettings,
{
/// Converts this `&mut BumpScope` into an owned `BumpScope`.
///
/// Allocates a chunk if none has been allocated yet.
///
/// This can be used to remove an indirection, simplify type signatures and allow
/// for more settings conversions via [`with_settings`].
///
/// # Panics
/// Panics if the bump allocator is currently [claimed].
///
/// Panics if the allocation fails.
///
/// [claimed]: crate::traits::BumpAllocatorScope::claim
/// [`with_settings`]: BumpScope::with_settings
/// [`borrow_mut_with_settings`]: BumpScope::borrow_mut_with_settings
#[must_use]
#[inline(always)]
#[cfg(feature = "panic-on-alloc")]
pub fn by_value(&mut self) -> BumpScope<'_, A, S> {
panic_on_error(self.raw.make_allocated());
BumpScope {
raw: self.raw.clone(),
marker: PhantomData,
}
}
/// Converts this `&mut BumpScope` into an owned `BumpScope`.
///
/// Allocates a chunk if none has been allocated yet.
///
/// This can be used to remove an indirection, simplify type signatures and allow
/// for more settings conversions via [`with_settings`].
///
/// # Errors
/// Errors if the bump allocator is currently [claimed].
///
/// Errors if the allocation fails.
///
/// [claimed]: crate::traits::BumpAllocatorScope::claim
/// [`with_settings`]: BumpScope::with_settings
/// [`borrow_mut_with_settings`]: BumpScope::borrow_mut_with_settings
#[inline(always)]
pub fn try_by_value(&mut self) -> Result<BumpScope<'_, A, S>, AllocError> {
self.raw.make_allocated::<AllocError>()?;
Ok(BumpScope {
raw: self.raw.clone(),
marker: PhantomData,
})
}
}
impl<'a, A, S> BumpScope<'a, A, S>
where
S: BumpAllocatorSettings,
{
/// Returns a type which provides statistics about the memory usage of the bump allocator.
#[must_use]
#[inline(always)]
pub fn stats(&self) -> Stats<'a, A, S> {
self.raw.stats()
}
#[inline(always)]
pub(crate) fn align<const ALIGN: usize>(&self)
where
MinimumAlignment<ALIGN>: SupportedMinimumAlignment,
{
self.raw.align::<ALIGN>();
}
/// Converts this `BumpScope` into a `BumpScope` with new settings.
///
/// This function will fail to compile if:
/// - `NewS::MIN_ALIGN < S::MIN_ALIGN`
/// - `NewS::UP != S::UP`
///
/// # Panics
/// Panics if `!NewS::CLAIMABLE` and the bump allocator is currently [claimed].
///
/// [claimed]: crate::traits::BumpAllocatorScope::claim
#[inline]
pub fn with_settings<NewS>(self) -> BumpScope<'a, A, NewS>
where
NewS: BumpAllocatorSettings,
{
self.raw.ensure_scope_satisfies_settings::<NewS>();
unsafe { transmute_value(self) }
}
/// Borrows this `BumpScope` with new settings.
///
/// This function will fail to compile if:
/// - `NewS::MIN_ALIGN != S::MIN_ALIGN`
/// - `NewS::UP != S::UP`
/// - `NewS::CLAIMABLE != S::CLAIMABLE`
/// - `NewS::GUARANTEED_ALLOCATED > S::GUARANTEED_ALLOCATED`
#[inline]
pub fn borrow_with_settings<NewS>(&self) -> &BumpScope<'a, A, NewS>
where
NewS: BumpAllocatorSettings,
{
self.raw.ensure_satisfies_settings_for_borrow::<NewS>();
unsafe { transmute_ref(self) }
}
/// Borrows this `BumpScope` mutably with new settings.
///
/// This function will fail to compile if:
/// - `NewS::MIN_ALIGN < S::MIN_ALIGN`
/// - `NewS::UP != S::UP`
/// - `NewS::GUARANTEED_ALLOCATED != S::GUARANTEED_ALLOCATED`
/// - `NewS::CLAIMABLE != S::CLAIMABLE`
#[inline]
pub fn borrow_mut_with_settings<NewS>(&mut self) -> &mut BumpScope<'a, A, NewS>
where
NewS: BumpAllocatorSettings,
{
self.raw.ensure_satisfies_settings_for_borrow_mut::<NewS>();
unsafe { transmute_mut(self) }
}
}
#[cfg(feature = "alloc")]
impl<S> BumpScope<'_, Global, S>
where
S: BumpAllocatorSettings,
{
/// Converts this `BumpScope` into a raw pointer.
#[inline]
#[must_use]
pub fn into_raw(self) -> NonNull<()> {
self.raw.into_raw()
}
/// Converts the raw pointer that was created with [`into_raw`](Self::into_raw) back into a `BumpScope`.
///
/// # Safety
/// This is highly unsafe, due to the number of invariants that aren't checked:
/// - `ptr` must have been created with `Self::into_raw`.
/// - This function must only be called once with this `ptr`.
/// - Nothing must have been allocated since then.
/// - The lifetime must match the original one.
/// - The settings must match the original ones.
#[inline]
#[must_use]
pub unsafe fn from_raw(ptr: NonNull<()>) -> Self {
Self {
raw: unsafe { RawBump::from_raw(ptr) },
marker: PhantomData,
}
}
}
impl<A, S> NoDrop for BumpScope<'_, A, S> where S: BumpAllocatorSettings {}
/// Methods that forward to traits.
// Documentation is in the forwarded to methods.
#[allow(clippy::missing_errors_doc, clippy::missing_safety_doc)]
impl<'a, A, S> BumpScope<'a, A, S>
where
A: BaseAllocator<S::GuaranteedAllocated>,
S: BumpAllocatorSettings,
{
traits::forward_methods! {
self: self
access: {self}
access_mut: {self}
lifetime: 'a
}
}
/// Additional `alloc` methods that are not available in traits.
impl<'a, A, S> BumpScope<'a, A, S>
where
A: BaseAllocator<S::GuaranteedAllocated>,
S: BumpAllocatorSettings,
{
/// Allocates the result of `f` in the bump allocator, then moves `E` out of it and deallocates the space it took up.
///
/// This can be more performant than allocating `T` after the fact, as `Result<T, E>` may be constructed in the bump allocators memory instead of on the stack and then copied over.
///
/// There is also [`alloc_try_with_mut`](Self::alloc_try_with_mut), optimized for a mutable reference.
///
/// # Panics
/// Panics if the allocation fails.
///
/// # Examples
#[cfg_attr(feature = "nightly-tests", doc = "```")]
#[cfg_attr(not(feature = "nightly-tests"), doc = "```ignore")]
/// # #![feature(offset_of_enum)]
/// # use core::mem::offset_of;
/// # use bump_scope::Bump;
/// # let bump: Bump = Bump::new();
/// let result = bump.alloc_try_with(|| -> Result<i32, i32> { Ok(123) });
/// assert_eq!(result.unwrap(), 123);
/// assert_eq!(bump.stats().allocated(), offset_of!(Result<i32, i32>, Ok.0) + size_of::<i32>());
/// ```
#[cfg_attr(feature = "nightly-tests", doc = "```")]
#[cfg_attr(not(feature = "nightly-tests"), doc = "```ignore")]
/// # use bump_scope::Bump;
/// # let bump: Bump = Bump::new();
/// let result = bump.alloc_try_with(|| -> Result<i32, i32> { Err(123) });
/// assert_eq!(result.unwrap_err(), 123);
/// assert_eq!(bump.stats().allocated(), 0);
/// ```
#[inline(always)]
#[cfg(feature = "panic-on-alloc")]
#[expect(clippy::missing_errors_doc)]
pub fn alloc_try_with<T, E>(&self, f: impl FnOnce() -> Result<T, E>) -> Result<BumpBox<'a, T>, E> {
panic_on_error(self.generic_alloc_try_with(f))
}
/// Allocates the result of `f` in the bump allocator, then moves `E` out of it and deallocates the space it took up.
///
/// This can be more performant than allocating `T` after the fact, as `Result<T, E>` may be constructed in the bump allocators memory instead of on the stack and then copied over.
///
/// There is also [`try_alloc_try_with_mut`](Self::try_alloc_try_with_mut), optimized for a mutable reference.
///
/// # Errors
/// Errors if the allocation fails.
///
/// # Examples
#[cfg_attr(feature = "nightly-tests", doc = "```")]
#[cfg_attr(not(feature = "nightly-tests"), doc = "```ignore")]
/// # #![feature(offset_of_enum)]
/// # use core::mem::offset_of;
/// # use bump_scope::Bump;
/// # let bump: Bump = Bump::new();
/// let result = bump.try_alloc_try_with(|| -> Result<i32, i32> { Ok(123) })?;
/// assert_eq!(result.unwrap(), 123);
/// assert_eq!(bump.stats().allocated(), offset_of!(Result<i32, i32>, Ok.0) + size_of::<i32>());
/// # Ok::<(), bump_scope::alloc::AllocError>(())
/// ```
#[cfg_attr(feature = "nightly-tests", doc = "```")]
#[cfg_attr(not(feature = "nightly-tests"), doc = "```ignore")]
/// # use bump_scope::Bump;
/// # let bump: Bump = Bump::new();
/// let result = bump.try_alloc_try_with(|| -> Result<i32, i32> { Err(123) })?;
/// assert_eq!(result.unwrap_err(), 123);
/// assert_eq!(bump.stats().allocated(), 0);
/// # Ok::<(), bump_scope::alloc::AllocError>(())
/// ```
#[inline(always)]
pub fn try_alloc_try_with<T, E>(
&self,
f: impl FnOnce() -> Result<T, E>,
) -> Result<Result<BumpBox<'a, T>, E>, AllocError> {
self.generic_alloc_try_with(f)
}
#[inline(always)]
pub(crate) fn generic_alloc_try_with<B: ErrorBehavior, T, E>(
&self,
f: impl FnOnce() -> Result<T, E>,
) -> Result<Result<BumpBox<'a, T>, E>, B> {
if T::IS_ZST {
return match f() {
Ok(value) => Ok(Ok(BumpBox::zst(value))),
Err(error) => Ok(Err(error)),
};
}
let checkpoint_before_alloc = self.checkpoint();
let uninit = self.generic_alloc_uninit::<B, Result<T, E>>()?;
let ptr = BumpBox::into_raw(uninit).cast::<Result<T, E>>();
// When bumping downwards the chunk's position is the same as `ptr`.
// Using `ptr` is faster so we use that.
let pos = if S::UP { self.raw.chunk.get().pos() } else { ptr.cast() };
Ok(unsafe {
non_null::write_with(ptr, f);
// If `f` made allocations on this bump allocator we can't shrink the allocation.
let can_shrink = pos == self.raw.chunk.get().pos();
match non_null::result(ptr) {
Ok(value) => Ok({
if can_shrink {
let new_pos = if S::UP {
let pos = value.add(1).addr().get();
up_align_usize_unchecked(pos, S::MIN_ALIGN)
} else {
let pos = value.addr().get();
down_align_usize(pos, S::MIN_ALIGN)
};
// The allocation of was successful, so our chunk must be allocated.
let chunk = self.raw.chunk.get().as_non_dummy_unchecked();
chunk.set_pos_addr(new_pos);
}
BumpBox::from_raw(value)
}),
Err(error) => Err({
let error = error.read();
if can_shrink {
self.reset_to(checkpoint_before_alloc);
}
error
}),
}
})
}
/// Allocates the result of `f` in the bump allocator, then moves `E` out of it and deallocates the space it took up.
///
/// This can be more performant than allocating `T` after the fact, as `Result<T, E>` may be constructed in the bump allocators memory instead of on the stack and then copied over.
///
/// This is just like [`alloc_try_with`](Self::alloc_try_with), but optimized for a mutable reference.
///
/// # Panics
/// Panics if the allocation fails.
///
/// # Examples
#[cfg_attr(feature = "nightly-tests", doc = "```")]
#[cfg_attr(not(feature = "nightly-tests"), doc = "```ignore")]
/// # #![feature(offset_of_enum)]
/// # use core::mem::offset_of;
/// # use bump_scope::Bump;
/// # let mut bump: Bump = Bump::new();
/// let result = bump.alloc_try_with_mut(|| -> Result<i32, i32> { Ok(123) });
/// assert_eq!(result.unwrap(), 123);
/// assert_eq!(bump.stats().allocated(), offset_of!(Result<i32, i32>, Ok.0) + size_of::<i32>());
/// ```
#[cfg_attr(feature = "nightly-tests", doc = "```")]
#[cfg_attr(not(feature = "nightly-tests"), doc = "```ignore")]
/// # use bump_scope::Bump;
/// # let mut bump: Bump = Bump::new();
/// let result = bump.alloc_try_with_mut(|| -> Result<i32, i32> { Err(123) });
/// assert_eq!(result.unwrap_err(), 123);
/// assert_eq!(bump.stats().allocated(), 0);
/// ```
#[inline(always)]
#[cfg(feature = "panic-on-alloc")]
#[expect(clippy::missing_errors_doc)]
pub fn alloc_try_with_mut<T, E>(&mut self, f: impl FnOnce() -> Result<T, E>) -> Result<BumpBox<'a, T>, E> {
panic_on_error(self.generic_alloc_try_with_mut(f))
}
/// Allocates the result of `f` in the bump allocator, then moves `E` out of it and deallocates the space it took up.
///
/// This can be more performant than allocating `T` after the fact, as `Result<T, E>` may be constructed in the bump allocators memory instead of on the stack and then copied over.
///
/// This is just like [`try_alloc_try_with`](Self::try_alloc_try_with), but optimized for a mutable reference.
///
/// # Errors
/// Errors if the allocation fails.
///
/// # Examples
#[cfg_attr(feature = "nightly-tests", doc = "```")]
#[cfg_attr(not(feature = "nightly-tests"), doc = "```ignore")]
/// # #![feature(offset_of_enum)]
/// # use core::mem::offset_of;
/// # use bump_scope::Bump;
/// # let mut bump: Bump = Bump::new();
/// let result = bump.try_alloc_try_with_mut(|| -> Result<i32, i32> { Ok(123) })?;
/// assert_eq!(result.unwrap(), 123);
/// assert_eq!(bump.stats().allocated(), offset_of!(Result<i32, i32>, Ok.0) + size_of::<i32>());
/// # Ok::<(), bump_scope::alloc::AllocError>(())
/// ```
#[cfg_attr(feature = "nightly-tests", doc = "```")]
#[cfg_attr(not(feature = "nightly-tests"), doc = "```ignore")]
/// # use bump_scope::Bump;
/// # let mut bump: Bump = Bump::new();
/// let result = bump.try_alloc_try_with_mut(|| -> Result<i32, i32> { Err(123) })?;
/// assert_eq!(result.unwrap_err(), 123);
/// assert_eq!(bump.stats().allocated(), 0);
/// # Ok::<(), bump_scope::alloc::AllocError>(())
/// ```
#[inline(always)]
pub fn try_alloc_try_with_mut<T, E>(
&mut self,
f: impl FnOnce() -> Result<T, E>,
) -> Result<Result<BumpBox<'a, T>, E>, AllocError> {
self.generic_alloc_try_with_mut(f)
}
#[inline(always)]
pub(crate) fn generic_alloc_try_with_mut<B: ErrorBehavior, T, E>(
&mut self,
f: impl FnOnce() -> Result<T, E>,
) -> Result<Result<BumpBox<'a, T>, E>, B> {
if T::IS_ZST {
return match f() {
Ok(value) => Ok(Ok(BumpBox::zst(value))),
Err(error) => Ok(Err(error)),
};
}
let checkpoint = self.checkpoint();
let ptr = self.raw.prepare_sized_allocation::<B, Result<T, E>>()?;
Ok(unsafe {
non_null::write_with(ptr, f);
// There is no need for `can_shrink` checks, because we have a mutable reference
// so there's no way anyone else has allocated in `f`.
match non_null::result(ptr) {
Ok(value) => Ok({
let new_pos = if S::UP {
let pos = value.add(1).addr().get();
up_align_usize_unchecked(pos, S::MIN_ALIGN)
} else {
let pos = value.addr().get();
down_align_usize(pos, S::MIN_ALIGN)
};
// The allocation was successful, so our chunk must be allocated.
let chunk = self.raw.chunk.get().as_non_dummy_unchecked();
chunk.set_pos_addr(new_pos);
BumpBox::from_raw(value)
}),
Err(error) => Err({
let error = error.read();
self.reset_to(checkpoint);
error
}),
}
})
}
#[inline(always)]
pub(crate) fn generic_alloc_uninit<B: ErrorBehavior, T>(&self) -> Result<BumpBox<'a, MaybeUninit<T>>, B> {
if T::IS_ZST {
return Ok(BumpBox::zst(MaybeUninit::uninit()));
}
let ptr = self.raw.alloc_sized::<B, T>()?.cast::<MaybeUninit<T>>();
unsafe { Ok(BumpBox::from_raw(ptr)) }
}
}
unsafe impl<A, S> Allocator for BumpScope<'_, A, S>
where
A: BaseAllocator<S::GuaranteedAllocated>,
S: BumpAllocatorSettings,
{
#[inline(always)]
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
allocator_impl::allocate(&self.raw, layout)
}
#[inline(always)]
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
unsafe { allocator_impl::deallocate(&self.raw, ptr, layout) };
}
#[inline(always)]
unsafe fn grow(&self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
unsafe { allocator_impl::grow(&self.raw, ptr, old_layout, new_layout) }
}
#[inline(always)]
unsafe fn grow_zeroed(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
unsafe { allocator_impl::grow_zeroed(&self.raw, ptr, old_layout, new_layout) }
}
#[inline(always)]
unsafe fn shrink(&self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
unsafe { allocator_impl::shrink(&self.raw, ptr, old_layout, new_layout) }
}
}