use crate::{memory_region::LocalMemoryRegion, protection_domain::ProtectionDomain};
use clippy_utilities::OverflowArithmetic;
use rdma_sys::ibv_access_flags;
use std::{alloc::Layout, io, sync::Arc};
#[derive(Debug)]
pub(crate) struct MRAllocator {
_pd: Arc<ProtectionDomain>,
mr: Arc<LocalMemoryRegion>,
}
impl MRAllocator {
#[allow(clippy::unwrap_in_result)]
pub(crate) fn new(pd: Arc<ProtectionDomain>) -> io::Result<Self> {
let access = ibv_access_flags::IBV_ACCESS_LOCAL_WRITE
| ibv_access_flags::IBV_ACCESS_REMOTE_WRITE
| ibv_access_flags::IBV_ACCESS_REMOTE_READ
| ibv_access_flags::IBV_ACCESS_REMOTE_ATOMIC;
let mr = Arc::new(
pd.alloc_memory_region(
#[allow(clippy::unwrap_used)]
Layout::from_size_align(4096.overflow_mul(2), 4096).unwrap(),
access,
)?,
);
Ok(Self { _pd: pd, mr })
}
pub(crate) fn alloc(&self, layout: &Layout) -> io::Result<LocalMemoryRegion> {
self.mr.alloc(layout)
}
}
#[cfg(test)]
mod tests {
use crate::RdmaBuilder;
use std::{alloc::Layout, io};
#[tokio::test]
async fn test1() -> io::Result<()> {
let rdma = RdmaBuilder::default()
.set_port_num(1)
.set_gid_index(1)
.build()?;
let mut mrs = vec![];
for _ in 0_i32..2_i32 {
let mr = rdma.alloc_local_mr(Layout::new::<[u8; 4096]>())?;
mrs.push(mr);
}
Ok(())
}
}