Slice from the top pool, sizing up if necessary.
(&mut self, count: usize)
| 153 | } |
| 154 | /// Slice from the top pool, sizing up if necessary. |
| 155 | pub fn acquire<T>(&mut self, count: usize) -> StackAlloc<T> { |
| 156 | let pools = &mut self.pools; |
| 157 | let mut prev_used = 0; |
| 158 | let mut next_pool = 0; |
| 159 | if let Some(top) = self.top { |
| 160 | let pool = &pools[top]; |
| 161 | if pool.usable_size::<T>() > count { |
| 162 | return self.get_slice(count, top); |
| 163 | } |
| 164 | prev_used = pool.bytes_used(); |
| 165 | next_pool = top + 1; |
| 166 | } |
| 167 | // The choices are to specify an alignment, or to make sure that the allocation |
| 168 | // is large enough to accommodate alignment padding. Choosing the latter. |
| 169 | let min_bytes = prev_used + size_of::<T>() * count + align_of::<T>(); |
| 170 | for i in next_pool..NUM_POOLS { |
| 171 | let size = size_for_i(i); |
| 172 | if size_for_i(i) >= min_bytes { |
| 173 | pools[i] = PoolAlloc::new(size); |
| 174 | self.top = Some(i); |
| 175 | return self.get_slice(count, i); |
| 176 | } |
| 177 | } |
| 178 | panic!("Allocation size too large"); |
| 179 | } |
| 180 | |
| 181 | /// Release a previously acquired pointer. |
| 182 | pub fn release<T>(&mut self, ptr: &StackAlloc<T>) { |
no test coverage detected