Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: drm: Add gem::impl_aref_for_gem_obj!

In the future we're going to be introducing more GEM object types in rust
then just gem::Object<T>. Since all types of GEM objects have refcounting,
let's introduce a macro that we can use in the gem crate in order to copy
this boilerplate implementation for each type: impl_aref_for_gem_obj!().

Signed-off-by: Lyude Paul <lyude@redhat.com>
Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
Reviewed-by: Janne Grunau <j@jananu.net>
Tested-by: Deborah Brouwer <deborah.brouwer@collabora.com>
Link: https://patch.msgid.link/20260316211646.650074-2-lyude@redhat.com
[ Resolve merge conflicts. - Danilo ]
Signed-off-by: Danilo Krummrich <dakr@kernel.org>

authored by

Lyude Paul and committed by
Danilo Krummrich
e64b9cc2 dff8302c

+36 -15
+36 -15
rust/kernel/drm/gem/mod.rs
··· 26 26 ptr::NonNull, // 27 27 }; 28 28 29 + /// A macro for implementing [`AlwaysRefCounted`] for any GEM object type. 30 + /// 31 + /// Since all GEM objects use the same refcounting scheme. 32 + #[macro_export] 33 + macro_rules! impl_aref_for_gem_obj { 34 + ( 35 + impl $( <$( $tparam_id:ident ),+> )? for $type:ty 36 + $( 37 + where 38 + $( $bind_param:path : $bind_trait:path ),+ 39 + )? 40 + ) => { 41 + // SAFETY: All GEM objects are refcounted. 42 + unsafe impl $( <$( $tparam_id ),+> )? $crate::types::AlwaysRefCounted for $type 43 + where 44 + Self: IntoGEMObject, 45 + $( $( $bind_param : $bind_trait ),+ )? 46 + { 47 + fn inc_ref(&self) { 48 + // SAFETY: The existence of a shared reference guarantees that the refcount is 49 + // non-zero. 50 + unsafe { bindings::drm_gem_object_get(self.as_raw()) }; 51 + } 52 + 53 + unsafe fn dec_ref(obj: core::ptr::NonNull<Self>) { 54 + // SAFETY: `obj` is a valid pointer to an `Object<T>`. 55 + let obj = unsafe { obj.as_ref() }.as_raw(); 56 + 57 + // SAFETY: The safety requirements guarantee that the refcount is non-zero. 58 + unsafe { bindings::drm_gem_object_put(obj) }; 59 + } 60 + } 61 + }; 62 + } 63 + 29 64 /// A type alias for retrieving a [`Driver`]s [`DriverFile`] implementation from its 30 65 /// [`DriverObject`] implementation. 31 66 /// ··· 298 263 } 299 264 } 300 265 301 - // SAFETY: Instances of `Object<T>` are always reference-counted. 302 - unsafe impl<T: DriverObject> crate::sync::aref::AlwaysRefCounted for Object<T> { 303 - fn inc_ref(&self) { 304 - // SAFETY: The existence of a shared reference guarantees that the refcount is non-zero. 305 - unsafe { bindings::drm_gem_object_get(self.as_raw()) }; 306 - } 307 - 308 - unsafe fn dec_ref(obj: NonNull<Self>) { 309 - // SAFETY: `obj` is a valid pointer to an `Object<T>`. 310 - let obj = unsafe { obj.as_ref() }; 311 - 312 - // SAFETY: The safety requirements guarantee that the refcount is non-zero. 313 - unsafe { bindings::drm_gem_object_put(obj.as_raw()) } 314 - } 315 - } 266 + impl_aref_for_gem_obj!(impl<T> for Object<T> where T: DriverObject); 316 267 317 268 impl<T: DriverObject> super::private::Sealed for Object<T> {} 318 269