|
| 1 | +/* |
| 2 | +Copyright 2026 The Hyperlight Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +//! A simple fixed-size buffer recycler for H2G prefill entries. |
| 18 | +//! |
| 19 | +//! Unlike [`super::BufferPool`] which uses a bitmap allocator, this |
| 20 | +//! holds a fixed set of same-sized buffer addresses in a free list. |
| 21 | +//! Alloc and dealloc are O(1). Intended for H2G writable buffers |
| 22 | +//! that are pre-allocated once and recycled after each use. |
| 23 | +
|
| 24 | +use alloc::sync::Arc; |
| 25 | + |
| 26 | +use atomic_refcell::AtomicRefCell; |
| 27 | +use smallvec::SmallVec; |
| 28 | + |
| 29 | +use super::{AllocError, Allocation, BufferProvider}; |
| 30 | + |
| 31 | +/// A recycling buffer provider with fixed-size slots. |
| 32 | +#[derive(Clone)] |
| 33 | +pub struct RecyclePool { |
| 34 | + inner: Arc<AtomicRefCell<RecyclePoolInner>>, |
| 35 | +} |
| 36 | + |
| 37 | +struct RecyclePoolInner { |
| 38 | + base_addr: u64, |
| 39 | + slot_size: usize, |
| 40 | + count: usize, |
| 41 | + free: SmallVec<[u64; 64]>, |
| 42 | +} |
| 43 | + |
| 44 | +impl RecyclePool { |
| 45 | + /// Create a new recycling pool by carving `base..base+region_len` into slots of `slot_size` bytes. |
| 46 | + pub fn new(base_addr: u64, region_len: usize, slot_size: usize) -> Result<Self, AllocError> { |
| 47 | + if slot_size == 0 { |
| 48 | + return Err(AllocError::InvalidArg); |
| 49 | + } |
| 50 | + |
| 51 | + let count = region_len / slot_size; |
| 52 | + if count == 0 { |
| 53 | + return Err(AllocError::EmptyRegion); |
| 54 | + } |
| 55 | + |
| 56 | + let mut free = SmallVec::with_capacity(count); |
| 57 | + for i in 0..count { |
| 58 | + free.push(base_addr + (i * slot_size) as u64); |
| 59 | + } |
| 60 | + |
| 61 | + let inner = AtomicRefCell::new(RecyclePoolInner { |
| 62 | + base_addr, |
| 63 | + slot_size, |
| 64 | + count, |
| 65 | + free, |
| 66 | + }); |
| 67 | + |
| 68 | + Ok(Self { |
| 69 | + inner: inner.into(), |
| 70 | + }) |
| 71 | + } |
| 72 | + |
| 73 | + /// Number of free slots. |
| 74 | + pub fn num_free(&self) -> usize { |
| 75 | + self.inner.borrow().free.len() |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl BufferProvider for RecyclePool { |
| 80 | + fn alloc(&self, len: usize) -> Result<Allocation, AllocError> { |
| 81 | + let mut inner = self.inner.borrow_mut(); |
| 82 | + if len > inner.slot_size { |
| 83 | + return Err(AllocError::OutOfMemory); |
| 84 | + } |
| 85 | + |
| 86 | + let addr = inner.free.pop().ok_or(AllocError::OutOfMemory)?; |
| 87 | + |
| 88 | + Ok(Allocation { |
| 89 | + addr, |
| 90 | + len: inner.slot_size, |
| 91 | + }) |
| 92 | + } |
| 93 | + |
| 94 | + fn dealloc(&self, alloc: Allocation) -> Result<(), AllocError> { |
| 95 | + let mut inner = self.inner.borrow_mut(); |
| 96 | + inner.free.push(alloc.addr); |
| 97 | + Ok(()) |
| 98 | + } |
| 99 | + |
| 100 | + fn resize(&self, old: Allocation, new_len: usize) -> Result<Allocation, AllocError> { |
| 101 | + let inner = self.inner.borrow(); |
| 102 | + if new_len > inner.slot_size { |
| 103 | + return Err(AllocError::OutOfMemory); |
| 104 | + } |
| 105 | + Ok(old) |
| 106 | + } |
| 107 | + |
| 108 | + fn reset(&self) { |
| 109 | + let mut inner = self.inner.borrow_mut(); |
| 110 | + let base = inner.base_addr; |
| 111 | + let slot = inner.slot_size; |
| 112 | + let count = inner.count; |
| 113 | + |
| 114 | + inner.free.clear(); |
| 115 | + |
| 116 | + for i in 0..count { |
| 117 | + inner.free.push(base + (i * slot) as u64); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments