im_rc/vector/
pool.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5use crate::config::POOL_SIZE;
6use crate::nodes::chunk::Chunk;
7use crate::nodes::rrb::Node;
8use crate::util::Pool;
9
10/// A memory pool for `Vector`.
11pub struct RRBPool<A> {
12    pub(crate) node_pool: Pool<Chunk<Node<A>>>,
13    pub(crate) value_pool: Pool<Chunk<A>>,
14    pub(crate) size_pool: Pool<Chunk<usize>>,
15}
16
17impl<A> RRBPool<A> {
18    /// Create a new memory pool with the given size.
19    pub fn new(size: usize) -> Self {
20        Self::with_sizes(size, size, size)
21    }
22
23    /// Create a new memory pool with the given sizes for each subpool.
24    pub fn with_sizes(
25        node_pool_size: usize,
26        leaf_pool_size: usize,
27        size_table_pool_size: usize,
28    ) -> Self {
29        Self {
30            node_pool: Pool::new(node_pool_size),
31            value_pool: Pool::new(leaf_pool_size),
32            size_pool: Pool::new(size_table_pool_size),
33        }
34    }
35
36    /// Fill the memory pool with preallocated chunks.
37    pub fn fill(&self) {
38        self.node_pool.fill();
39        self.value_pool.fill();
40        self.size_pool.fill();
41    }
42
43    /// Get the size of the node subpool.
44    pub fn node_pool_size(&self) -> usize {
45        self.node_pool.get_pool_size()
46    }
47
48    /// Get the size of the leaf node subpool.
49    pub fn leaf_pool_size(&self) -> usize {
50        self.value_pool.get_pool_size()
51    }
52
53    /// Get the size of the size table subpool.
54    pub fn size_table_pool_size(&self) -> usize {
55        self.size_pool.get_pool_size()
56    }
57}
58
59impl<A> Default for RRBPool<A> {
60    /// Construct a pool with a reasonable default pool size.
61    fn default() -> Self {
62        Self::new(POOL_SIZE)
63    }
64}
65
66impl<A> Clone for RRBPool<A> {
67    fn clone(&self) -> Self {
68        Self {
69            node_pool: self.node_pool.clone(),
70            value_pool: self.value_pool.clone(),
71            size_pool: self.size_pool.clone(),
72        }
73    }
74}