Re: [PATCH v10 2/3] rust: add dma coherent allocator abstraction.

From: Alice Ryhl
Date: Wed Jan 22 2025 - 05:16:06 EST


On Wed, Jan 22, 2025 at 11:14 AM Abdiel Janulgue
<abdiel.janulgue@xxxxxxxxx> wrote:
> >> + /// Returns the device, base address, dma handle, attributes and the size of the
> >> + /// allocated region.
> >> + ///
> >> + /// The caller takes ownership of the returned resources, i.e., will have the responsibility
> >> + /// in calling `bindings::dma_free_attrs`. The allocated region is valid as long as
> >> + /// the returned device exists.
> >> + pub fn into_parts(
> >> + self,
> >> + ) -> (
> >> + ARef<Device>,
> >> + *mut T,
> >> + bindings::dma_addr_t,
> >> + crate::ffi::c_ulong,
> >> + usize,
> >> + ) {
> >> + let size = self.count * core::mem::size_of::<T>();
> >> + let ret = (
> >> + self.dev.clone(),
> >
> > Calling clone here increments the refcount, but you don't want to do
> > that since it leaks a count.
> >
>
> You mean return the raw *mut bindings::device instead of ARef<Device>.
> Otherwise the compiler complains not being able to move out ARef<Device>
> because it doesn't implement Copy.

What I mean is that you should return an ARef<Device> without
incrementing the refcount. I acknowledge that this requires unsafe,
but it's the correct semantics and the unsafe is a consequence of
using mem::forget.

You can write `core::ptr::read(&self.dev)`.

Alice