Skip to main content

agave_jemalloc/
group.rs

1use {
2    crate::jemalloc::{Arena, Decay, Error, Jemalloc},
3    std::ops::Index,
4};
5
6#[derive(Debug)]
7pub struct ArenaGroup {
8    arenas: Box<[Arena]>,
9}
10
11impl ArenaGroup {
12    pub fn new(
13        arena_count: usize,
14        oversize_threshold: usize,
15        retain_grow_limit: usize,
16    ) -> Result<Self, Error> {
17        assert!(arena_count > 0, "arena count must be positive");
18
19        let arenas = (0..arena_count)
20            .map(|_| {
21                let arena = Jemalloc::create_arena()?;
22                arena.set_dirty_decay(Decay::Never)?;
23                arena.set_muzzy_decay(Decay::Never)?;
24                arena.set_oversize_threshold(oversize_threshold)?;
25                arena.set_retain_grow_limit(retain_grow_limit)?;
26                Ok(arena)
27            })
28            .collect::<Result<Vec<_>, Error>>()?
29            .into_boxed_slice();
30
31        Ok(Self { arenas })
32    }
33
34    #[allow(clippy::len_without_is_empty)]
35    pub fn len(&self) -> usize {
36        self.arenas.len()
37    }
38}
39
40impl Index<usize> for ArenaGroup {
41    type Output = Arena;
42
43    fn index(&self, index: usize) -> &Self::Output {
44        &self.arenas[index]
45    }
46}