use std::ffi::CStr;
use std::sync::Arc;
use crate::ffi;
use crate::Allocator;
use crate::PoolCreateInfo;
use ash::prelude::VkResult;
#[derive(Clone, Copy)]
pub struct PoolHandle(pub(crate) ffi::VmaPool);
pub struct AllocatorPool {
pub(crate) allocator: Arc<Allocator>,
pub(crate) pool: PoolHandle,
}
unsafe impl Send for AllocatorPool {}
unsafe impl Sync for AllocatorPool {}
impl AllocatorPool {
pub fn new(allocator: Arc<Allocator>, create_info: &PoolCreateInfo) -> VkResult<Self> {
unsafe {
let mut ffi_pool: ffi::VmaPool = std::mem::zeroed();
ffi::vmaCreatePool(allocator.internal, &create_info.inner, &mut ffi_pool).result()?;
Ok(AllocatorPool {
pool: PoolHandle(ffi_pool),
allocator,
})
}
}
pub fn set_name(&self, name: Option<&CStr>) {
if self.pool.0.is_null() {
return;
}
unsafe {
ffi::vmaSetPoolName(
self.allocator.internal,
self.pool.0,
name.map_or(std::ptr::null(), CStr::as_ptr),
);
}
}
pub fn name(&self) -> Option<&CStr> {
if self.pool.0.is_null() {
return None;
}
let mut ptr: *const ::std::os::raw::c_char = std::ptr::null();
unsafe {
ffi::vmaGetPoolName(self.allocator.internal, self.pool.0, &mut ptr);
if ptr.is_null() {
return None;
}
Some(CStr::from_ptr(ptr))
}
}
pub fn get_statistics(&self) -> VkResult<ffi::VmaStatistics> {
unsafe {
let mut pool_stats: ffi::VmaStatistics = std::mem::zeroed();
ffi::vmaGetPoolStatistics(self.allocator.internal, self.pool.0, &mut pool_stats);
Ok(pool_stats)
}
}
pub fn calculate_statistics(&self) -> VkResult<ffi::VmaDetailedStatistics> {
unsafe {
let mut pool_stats: ffi::VmaDetailedStatistics = std::mem::zeroed();
ffi::vmaCalculatePoolStatistics(self.allocator.internal, self.pool.0, &mut pool_stats);
Ok(pool_stats)
}
}
pub fn check_corruption(&self) -> VkResult<()> {
unsafe { ffi::vmaCheckPoolCorruption(self.allocator.internal, self.pool.0).result() }
}
}
impl Drop for AllocatorPool {
fn drop(&mut self) {
unsafe {
ffi::vmaDestroyPool(self.allocator.internal, self.pool.0);
}
}
}