Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions ibverbs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,16 @@ impl<T> MemoryRegion<T> {
self.data
}

/// Make a lifetime-independent slice representation of this memory region.
/// Unlike `LocalMemorySlice`, this supports 64-bit memory region lengths.
pub fn as_slice(&self) -> MemoryRegionSlice {
MemoryRegionSlice {
addr: unsafe { *self.inner.mr }.addr as u64,
length: unsafe { *self.inner.mr }.length as u64,
lkey: unsafe { *self.inner.mr }.lkey,
}
}

/// Make a subslice of this memory region.
pub fn slice(&self, bounds: impl RangeBounds<usize>) -> LocalMemorySlice {
let (addr, length) = calc_addr_len(
Expand Down Expand Up @@ -1613,6 +1623,32 @@ impl LocalMemorySlice {
}
}

/// Lifetime-independent slice representation of a memory region.
/// Unlike `LocalMemorySlice`, this supports 64-bit memory region lengths.
#[derive(Debug, Default, Copy, Clone)]
pub struct MemoryRegionSlice {
/// Memory region base address
pub addr: u64,
/// Memory region length
pub length: u64,
/// Memory region lkey
pub lkey: u32,
}

impl MemoryRegionSlice {
/// Make a slice of this memory region.
pub fn slice(&self, bounds: impl RangeBounds<usize>) -> LocalMemorySlice {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is your use case for needing something lifetime independent?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be useful to move MemoryRegion into a generic (rdma-agnostic) custom buffer pool struct, which, on drop, will call the MemoryRegion's destructor, thus deregistering the region right before deallocating the application memory. Thus, we need to be able to create slices outside of the MemoryRegion's lifetime

let (addr, len) = calc_addr_len(bounds, self.addr, self.length as usize);
LocalMemorySlice {
_sge: ffi::ibv_sge {
addr,
length: len.try_into().unwrap(),
lkey: self.lkey,
},
}
}
}

/// Remote memory region.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize, Debug, Clone))]
pub struct RemoteMemorySlice {
Expand Down