Skip to main content

sieve/runtime/
arena.rs

1/*
2 * SPDX-FileCopyrightText: 2020 Stalwart Labs Ltd <hello@stalw.art>
3 *
4 * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL
5 */
6
7use bumpalo::Bump;
8
9#[derive(Default)]
10pub struct Arena {
11    pub(crate) bump: Bump,
12}
13
14impl Arena {
15    pub fn new() -> Self {
16        Self::default()
17    }
18
19    pub fn with_capacity(bytes: usize) -> Self {
20        Arena {
21            bump: Bump::with_capacity(bytes),
22        }
23    }
24
25    pub fn reset(&mut self) {
26        self.bump.reset();
27    }
28
29    pub(crate) fn prepare(&mut self, limit: usize) {
30        self.bump.reset();
31        if self.bump.allocated_bytes() > limit {
32            self.bump = Bump::new();
33        }
34        self.bump.set_allocation_limit(Some(limit));
35    }
36
37    pub fn allocated_bytes(&self) -> usize {
38        self.bump.allocated_bytes()
39    }
40}