Skip to content

Commit 5d385a3

Browse files
tamirdojeda
authored andcommitted
rust: arc: split unsafe block, add missing comment
The new SAFETY comment style is taken from existing comments in `deref` and `drop. Reviewed-by: Alice Ryhl <[email protected]> Reviewed-by: Andreas Hindborg <[email protected]> Signed-off-by: Tamir Duberstein <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent aa991a2 commit 5d385a3

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

rust/kernel/sync/arc.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,14 @@ impl<T: ?Sized> AsRef<T> for Arc<T> {
389389

390390
impl<T: ?Sized> Clone for Arc<T> {
391391
fn clone(&self) -> Self {
392+
// SAFETY: By the type invariant, there is necessarily a reference to the object, so it is
393+
// safe to dereference it.
394+
let refcount = unsafe { self.ptr.as_ref() }.refcount.get();
395+
392396
// INVARIANT: C `refcount_inc` saturates the refcount, so it cannot overflow to zero.
393397
// SAFETY: By the type invariant, there is necessarily a reference to the object, so it is
394398
// safe to increment the refcount.
395-
unsafe { bindings::refcount_inc(self.ptr.as_ref().refcount.get()) };
399+
unsafe { bindings::refcount_inc(refcount) };
396400

397401
// SAFETY: We just incremented the refcount. This increment is now owned by the new `Arc`.
398402
unsafe { Self::from_inner(self.ptr) }

0 commit comments

Comments
 (0)