Skip to main content

bump_scope/
bump_align_guard.rs

1use crate::{BumpScope, align_pos, settings::BumpAllocatorSettings};
2
3/// Aligns the bump pointer on drop.
4///
5/// This is useful in unsafe contexts where the alignment is changed and we have to change it back.
6/// The `BumpScope` is in an invalid state when the bump pointer alignment does not match `MIN_ALIGN`.
7/// So `drop` ***must*** be called to return the bump scope to a valid state.
8pub(crate) struct BumpAlignGuard<'b, 'a, A, S>
9where
10    S: BumpAllocatorSettings,
11{
12    pub(crate) scope: &'b mut BumpScope<'a, A, S>,
13}
14
15impl<A, S> Drop for BumpAlignGuard<'_, '_, A, S>
16where
17    S: BumpAllocatorSettings,
18{
19    #[inline(always)]
20    fn drop(&mut self) {
21        if let Some(chunk) = self.scope.raw.chunk.get().as_non_dummy() {
22            let pos = chunk.pos().addr().get();
23            let addr = align_pos(S::UP, S::MIN_ALIGN, pos);
24            unsafe { chunk.set_pos_addr(addr) };
25        }
26    }
27}
28
29impl<'b, 'a, A, S> BumpAlignGuard<'b, 'a, A, S>
30where
31    S: BumpAllocatorSettings,
32{
33    #[inline(always)]
34    pub(crate) fn new(scope: &'b mut BumpScope<'a, A, S>) -> Self {
35        Self { scope }
36    }
37}