aboutsummaryrefslogtreecommitdiff
path: root/stockton-skeleton/src/context.rs
diff options
context:
space:
mode:
authortcmal <me@aria.rip>2024-08-25 17:44:24 +0100
committertcmal <me@aria.rip>2024-08-25 17:44:24 +0100
commit9b4edc67d315a640a9d22f84919b0dd81e201f1d (patch)
treef0056af9fed09afa397e083ab39b6b31bf7ff3bf /stockton-skeleton/src/context.rs
parent4f068467c4954fb79e6ce297ae1ac0fdd2bdf16a (diff)
refactor(skeleton): add alloc and free to context
#7
Diffstat (limited to 'stockton-skeleton/src/context.rs')
-rw-r--r--stockton-skeleton/src/context.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/stockton-skeleton/src/context.rs b/stockton-skeleton/src/context.rs
index ea9810f..72b15c7 100644
--- a/stockton-skeleton/src/context.rs
+++ b/stockton-skeleton/src/context.rs
@@ -274,6 +274,38 @@ impl StatefulRenderingContext<Normal> {
Ok(self.existing_memory_pool::<P>().unwrap())
}
+ /// Allocate memory from the given pool.
+ /// See [`crate::mem::MemoryPool::alloc`]
+ pub fn alloc<P: MemoryPool>(&mut self, size: u64, align: u64) -> Result<P::Block> {
+ self.ensure_memory_pool::<P>()?;
+
+ let device = self.lock_device()?;
+ let mut pool = self
+ .existing_memory_pool::<P>()
+ .unwrap()
+ .write()
+ .map_err(|_| LockPoisoned::MemoryPool)?;
+
+ Ok(pool.alloc(&device, size, align)?.0)
+ }
+
+ /// Free memory from the given pool.
+ /// See [`crate::mem::MemoryPool::free`]
+ pub fn free<P: MemoryPool>(&mut self, block: P::Block) -> Result<()> {
+ self.ensure_memory_pool::<P>()?;
+
+ let device = self.lock_device()?;
+ let mut pool = self
+ .existing_memory_pool::<P>()
+ .unwrap()
+ .write()
+ .map_err(|_| LockPoisoned::MemoryPool)?;
+
+ pool.free(&device, block);
+
+ Ok(())
+ }
+
/// Ensure the specified memory pool is initialised.
#[allow(clippy::map_entry)] // We can't follow the suggestion because of a borrowing issue
pub fn ensure_memory_pool<P: MemoryPool>(&mut self) -> Result<()> {