Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replicate #2098 on master #2110

Merged
merged 1 commit into from
Dec 20, 2022
Merged
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
44 changes: 44 additions & 0 deletions vulkano/src/memory/allocator/suballocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,12 @@ impl MemoryAlloc {
/// [`shift`]: Self::shift
#[inline]
pub unsafe fn set_offset(&mut self, new_offset: DeviceSize) {
if let Some(ptr) = self.mapped_ptr.as_mut() {
*ptr = NonNull::new_unchecked(
ptr.as_ptr()
.offset(new_offset as isize - self.offset as isize),
);
}
self.offset = new_offset;
}

Expand Down Expand Up @@ -2591,6 +2597,44 @@ mod tests {
..DUMMY_INFO
};

#[test]
fn memory_alloc_set_offset() {
let (device, _) = gfx_dev_and_queue!();
let memory_type_index = device
.physical_device()
.memory_properties()
.memory_types
.iter()
.position(|memory_type| {
memory_type
.property_flags
.contains(MemoryPropertyFlags::HOST_VISIBLE)
})
.unwrap() as u32;
let mut alloc = MemoryAlloc::new(
DeviceMemory::allocate(
device,
MemoryAllocateInfo {
memory_type_index,
allocation_size: 1024,
..Default::default()
},
)
.unwrap(),
)
.unwrap();
let ptr = alloc.mapped_ptr().unwrap().as_ptr();

unsafe {
alloc.set_offset(16);
assert_eq!(alloc.mapped_ptr().unwrap().as_ptr(), ptr.offset(16));
alloc.set_offset(0);
assert_eq!(alloc.mapped_ptr().unwrap().as_ptr(), ptr.offset(0));
alloc.set_offset(32);
assert_eq!(alloc.mapped_ptr().unwrap().as_ptr(), ptr.offset(32));
}
}

#[test]
fn free_list_allocator_capacity() {
const THREADS: DeviceSize = 12;
Expand Down