diff --git a/crates/metal2vulkan-rt/src/buffer.rs b/crates/metal2vulkan-rt/src/buffer.rs index 8714cf1ca..989a4407e 100644 --- a/crates/metal2vulkan-rt/src/buffer.rs +++ b/crates/metal2vulkan-rt/src/buffer.rs @@ -367,11 +367,17 @@ impl Device { } } -// SAFETY: the handle is its one `Arc` and nothing else, and its address is that `Arc`'s pointer. -unsafe impl crate::kept::Handle for Buffer { +impl crate::kept::Handle for Buffer { fn address(&self) -> usize { Arc::as_ptr(&self.inner) as *const () as usize } + + unsafe fn uncounted(&self) -> std::mem::ManuallyDrop { + // SAFETY: the pointer is a live `Arc`'s; the handle made of it is never dropped, and the + // caller keeps the object alive past its last use. + let inner = unsafe { Arc::from_raw(Arc::as_ptr(&self.inner)) }; + std::mem::ManuallyDrop::new(Buffer { inner }) + } } impl Buffer { diff --git a/crates/metal2vulkan-rt/src/kept.rs b/crates/metal2vulkan-rt/src/kept.rs index 815440394..6ee9f7b25 100644 --- a/crates/metal2vulkan-rt/src/kept.rs +++ b/crates/metal2vulkan-rt/src/kept.rs @@ -16,16 +16,19 @@ use std::hash::{BuildHasherDefault, Hasher}; use std::mem::ManuallyDrop; use std::ops::Deref; -/// A handle to a shared object that is one `Arc` and nothing else: a copy of it made bit for bit -/// is the same object, and dropping no copy but one keeps the count right. -/// -/// # Safety -/// -/// The type holds exactly one `Arc` and no other state, and `address` is that `Arc`'s pointer. -pub(crate) unsafe trait Handle: Clone { - /// The object's address: the same for every handle to it, and no other object's while it - /// lives. +/// A handle to a shared object that is one `Arc` and nothing else. +pub(crate) trait Handle: Clone { + /// The object's address: its `Arc`'s pointer, the same for every handle to it, and no other + /// object's while it lives. fn address(&self) -> usize; + + /// Another handle to the object, made of the same pointer, that took no count: dropping it + /// would give back one it never took, so it is never dropped. + /// + /// # Safety + /// + /// The object lives past the handle's last use. + unsafe fn uncounted(&self) -> ManuallyDrop; } /// A handle borrowed from what a recording keeps: the same object, which this copy neither @@ -40,10 +43,8 @@ impl Lent { /// /// The recording the borrow is used in keeps `object` until after the borrow's last use. unsafe fn of(object: &T) -> Lent { - // SAFETY: `T: Handle` is one `Arc`, so the copy is the same object; it is never dropped, - // so the count stays the kept handle's, and the caller keeps that one alive past the - // copy's last use. - Lent(ManuallyDrop::new(unsafe { std::ptr::read(object) })) + // SAFETY: the caller keeps the object alive past the borrow's last use. + Lent(unsafe { object.uncounted() }) } } diff --git a/crates/metal2vulkan-rt/src/pipeline.rs b/crates/metal2vulkan-rt/src/pipeline.rs index d18f99c8e..bd95856d6 100644 --- a/crates/metal2vulkan-rt/src/pipeline.rs +++ b/crates/metal2vulkan-rt/src/pipeline.rs @@ -1409,9 +1409,15 @@ impl Drop for Compiled { } } -// SAFETY: the handle is its one `Arc` and nothing else, and its address is that `Arc`'s pointer. -unsafe impl crate::kept::Handle for ComputePipelineState { +impl crate::kept::Handle for ComputePipelineState { fn address(&self) -> usize { Arc::as_ptr(&self.inner) as *const () as usize } + + unsafe fn uncounted(&self) -> std::mem::ManuallyDrop { + // SAFETY: the pointer is a live `Arc`'s; the handle made of it is never dropped, and the + // caller keeps the object alive past its last use. + let inner = unsafe { Arc::from_raw(Arc::as_ptr(&self.inner)) }; + std::mem::ManuallyDrop::new(ComputePipelineState { inner }) + } } diff --git a/crates/metal2vulkan-rt/src/queue.rs b/crates/metal2vulkan-rt/src/queue.rs index 3a8c89734..012597faa 100644 --- a/crates/metal2vulkan-rt/src/queue.rs +++ b/crates/metal2vulkan-rt/src/queue.rs @@ -47,7 +47,7 @@ struct Slots { free: Vec, /// The kept-object lists of retired command buffers, empty, with their capacity /// (`crate::kept`): at most one per slot. - objects: Vec>, + objects: Vec, } impl Slots { @@ -124,7 +124,7 @@ impl CommandQueue { Arc::clone(&self.inner), self.device.clone(), slot, - kept, + Box::new(kept), )) } @@ -179,6 +179,7 @@ impl QueueInner { /// holds. pub(crate) fn release(&self, slot: u32, kept: Box) { self.counters.fall(Gauge::CommandBuffers); + let kept = *kept; let mut slots = lock(&self.slots); slots.free.push(slot); slots.objects.push(kept); diff --git a/crates/metal2vulkan-rt/src/render.rs b/crates/metal2vulkan-rt/src/render.rs index 7937058d8..6a8085a9c 100644 --- a/crates/metal2vulkan-rt/src/render.rs +++ b/crates/metal2vulkan-rt/src/render.rs @@ -850,9 +850,15 @@ impl Drop for RenderCompiled { } } -// SAFETY: the handle is its one `Arc` and nothing else, and its address is that `Arc`'s pointer. -unsafe impl crate::kept::Handle for RenderPipelineState { +impl crate::kept::Handle for RenderPipelineState { fn address(&self) -> usize { Arc::as_ptr(&self.inner) as *const () as usize } + + unsafe fn uncounted(&self) -> std::mem::ManuallyDrop { + // SAFETY: the pointer is a live `Arc`'s; the handle made of it is never dropped, and the + // caller keeps the object alive past its last use. + let inner = unsafe { Arc::from_raw(Arc::as_ptr(&self.inner)) }; + std::mem::ManuallyDrop::new(RenderPipelineState { inner }) + } } diff --git a/crates/metal2vulkan-rt/src/sampler.rs b/crates/metal2vulkan-rt/src/sampler.rs index c61bf83f2..4b2667595 100644 --- a/crates/metal2vulkan-rt/src/sampler.rs +++ b/crates/metal2vulkan-rt/src/sampler.rs @@ -392,9 +392,15 @@ impl SamplerState { } } -// SAFETY: the handle is its one `Arc` and nothing else, and its address is that `Arc`'s pointer. -unsafe impl crate::kept::Handle for SamplerState { +impl crate::kept::Handle for SamplerState { fn address(&self) -> usize { Arc::as_ptr(&self.inner) as *const () as usize } + + unsafe fn uncounted(&self) -> std::mem::ManuallyDrop { + // SAFETY: the pointer is a live `Arc`'s; the handle made of it is never dropped, and the + // caller keeps the object alive past its last use. + let inner = unsafe { Arc::from_raw(Arc::as_ptr(&self.inner)) }; + std::mem::ManuallyDrop::new(SamplerState { inner }) + } } diff --git a/crates/metal2vulkan-rt/src/tests/kept.rs b/crates/metal2vulkan-rt/src/tests/kept.rs index 3103c5e64..36a1ae01d 100644 --- a/crates/metal2vulkan-rt/src/tests/kept.rs +++ b/crates/metal2vulkan-rt/src/tests/kept.rs @@ -2,6 +2,7 @@ //! taken; a later use and every borrow of it change no count; clearing releases every object. use crate::kept::{Handle, Kept}; +use std::mem::ManuallyDrop; use std::sync::Arc; /// A handle of one `Arc`, as the runtime's objects are. @@ -10,11 +11,16 @@ struct Probe { inner: Arc, } -// SAFETY: the handle is its one `Arc` and nothing else, and its address is that `Arc`'s pointer. -unsafe impl Handle for Probe { +impl Handle for Probe { fn address(&self) -> usize { Arc::as_ptr(&self.inner) as usize } + + unsafe fn uncounted(&self) -> ManuallyDrop { + // SAFETY: as the runtime's handles do. + let inner = unsafe { Arc::from_raw(Arc::as_ptr(&self.inner)) }; + ManuallyDrop::new(Probe { inner }) + } } /// An object kept, then used again, held again and borrowed many times, has one reference of the diff --git a/crates/metal2vulkan-rt/src/texture.rs b/crates/metal2vulkan-rt/src/texture.rs index de947d1fa..921150989 100644 --- a/crates/metal2vulkan-rt/src/texture.rs +++ b/crates/metal2vulkan-rt/src/texture.rs @@ -3105,9 +3105,15 @@ pub(crate) struct OverCopy { pub(crate) shared: bool, } -// SAFETY: the handle is its one `Arc` and nothing else, and its address is that `Arc`'s pointer. -unsafe impl crate::kept::Handle for Texture { +impl crate::kept::Handle for Texture { fn address(&self) -> usize { Arc::as_ptr(&self.inner) as *const () as usize } + + unsafe fn uncounted(&self) -> std::mem::ManuallyDrop { + // SAFETY: the pointer is a live `Arc`'s; the handle made of it is never dropped, and the + // caller keeps the object alive past its last use. + let inner = unsafe { Arc::from_raw(Arc::as_ptr(&self.inner)) }; + std::mem::ManuallyDrop::new(Texture { inner }) + } }