From 9b4edc67d315a640a9d22f84919b0dd81e201f1d Mon Sep 17 00:00:00 2001 From: tcmal Date: Sun, 25 Aug 2024 17:44:24 +0100 Subject: refactor(skeleton): add alloc and free to context #7 --- stockton-skeleton/src/context.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'stockton-skeleton') 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 { Ok(self.existing_memory_pool::

().unwrap()) } + /// Allocate memory from the given pool. + /// See [`crate::mem::MemoryPool::alloc`] + pub fn alloc(&mut self, size: u64, align: u64) -> Result { + self.ensure_memory_pool::

()?; + + let device = self.lock_device()?; + let mut pool = self + .existing_memory_pool::

() + .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(&mut self, block: P::Block) -> Result<()> { + self.ensure_memory_pool::

()?; + + let device = self.lock_device()?; + let mut pool = self + .existing_memory_pool::

() + .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(&mut self) -> Result<()> { -- cgit v1.2.3