use rdma_mummy_sys::{ibv_dealloc_pd, ibv_pd};
use std::ptr::NonNull;
use std::sync::Arc;
use super::{
device_context::DeviceContext,
memory_region::{MemoryRegion, RegisterMemoryRegionError},
queue_pair::QueuePairBuilder,
AccessFlags,
};
#[derive(Debug)]
pub struct ProtectionDomain {
pub(crate) pd: NonNull<ibv_pd>,
pub(crate) _dev_ctx: Arc<DeviceContext>,
}
unsafe impl Send for ProtectionDomain {}
unsafe impl Sync for ProtectionDomain {}
impl Drop for ProtectionDomain {
fn drop(&mut self) {
unsafe {
ibv_dealloc_pd(self.pd.as_mut());
}
}
}
impl ProtectionDomain {
pub(crate) fn new(dev_ctx: Arc<DeviceContext>, pd: NonNull<ibv_pd>) -> Self {
ProtectionDomain {
pd,
_dev_ctx: dev_ctx,
}
}
pub unsafe fn reg_mr(
self: &Arc<Self>, ptr: usize, len: usize, access: AccessFlags,
) -> Result<Arc<MemoryRegion>, RegisterMemoryRegionError> {
Ok(Arc::new(MemoryRegion::reg_mr(Arc::clone(self), ptr, len, access)?))
}
pub fn create_qp_builder(self: &Arc<Self>) -> QueuePairBuilder {
QueuePairBuilder::new(self)
}
pub unsafe fn pd(&self) -> NonNull<ibv_pd> {
self.pd
}
}