|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +//! Atomic reference counting. |
| 4 | +//! |
| 5 | +//! C header: [`include/linux/refcount.h`](srctree/include/linux/refcount.h) |
| 6 | +
|
| 7 | +use crate::build_assert; |
| 8 | +use crate::types::Opaque; |
| 9 | + |
| 10 | +/// Atomic reference counter. |
| 11 | +/// |
| 12 | +/// This type is conceptually an atomic integer, but provides saturation semantics compared to |
| 13 | +/// normal atomic integers. Values in the negative range when viewed as a signed integer are |
| 14 | +/// saturation (bad) values. For details about the saturation semantics, please refer to top of |
| 15 | +/// [`include/linux/refcount.h`](srctree/include/linux/refcount.h). |
| 16 | +/// |
| 17 | +/// Wraps the kernel's C `refcount_t`. |
| 18 | +#[repr(transparent)] |
| 19 | +pub struct Refcount(Opaque<bindings::refcount_t>); |
| 20 | + |
| 21 | +impl Refcount { |
| 22 | + /// Construct a new [`Refcount`] from an initial value. |
| 23 | + /// |
| 24 | + /// The initial value should be non-saturated. |
| 25 | + #[inline] |
| 26 | + pub fn new(value: i32) -> Self { |
| 27 | + build_assert!(value >= 0, "initial value saturated"); |
| 28 | + // SAFETY: There are no safety requirements for this FFI call. |
| 29 | + Self(Opaque::new(unsafe { bindings::REFCOUNT_INIT(value) })) |
| 30 | + } |
| 31 | + |
| 32 | + #[inline] |
| 33 | + fn as_ptr(&self) -> *mut bindings::refcount_t { |
| 34 | + self.0.get() |
| 35 | + } |
| 36 | + |
| 37 | + /// Set a refcount's value. |
| 38 | + #[inline] |
| 39 | + pub fn set(&self, value: i32) { |
| 40 | + // SAFETY: `self.as_ptr()` is valid. |
| 41 | + unsafe { bindings::refcount_set(self.as_ptr(), value) } |
| 42 | + } |
| 43 | + |
| 44 | + /// Increment a refcount. |
| 45 | + /// |
| 46 | + /// It will saturate if overflows and `WARN`. It will also `WARN` if the refcount is 0, as this |
| 47 | + /// represents a possible use-after-free condition. |
| 48 | + /// |
| 49 | + /// Provides no memory ordering, it is assumed that caller already has a reference on the |
| 50 | + /// object. |
| 51 | + #[inline] |
| 52 | + pub fn inc(&self) { |
| 53 | + // SAFETY: self is valid. |
| 54 | + unsafe { bindings::refcount_inc(self.as_ptr()) } |
| 55 | + } |
| 56 | + |
| 57 | + /// Decrement a refcount. |
| 58 | + /// |
| 59 | + /// It will `WARN` on underflow and fail to decrement when saturated. |
| 60 | + /// |
| 61 | + /// Provides release memory ordering, such that prior loads and stores are done |
| 62 | + /// before. |
| 63 | + #[inline] |
| 64 | + pub fn dec(&self) { |
| 65 | + // SAFETY: `self.as_ptr()` is valid. |
| 66 | + unsafe { bindings::refcount_dec(self.as_ptr()) } |
| 67 | + } |
| 68 | + |
| 69 | + /// Decrement a refcount and test if it is 0. |
| 70 | + /// |
| 71 | + /// It will `WARN` on underflow and fail to decrement when saturated. |
| 72 | + /// |
| 73 | + /// Provides release memory ordering, such that prior loads and stores are done |
| 74 | + /// before, and provides an acquire ordering on success such that memory deallocation |
| 75 | + /// must come after. |
| 76 | + /// |
| 77 | + /// Returns true if the resulting refcount is 0, false otherwise. |
| 78 | + /// |
| 79 | + /// # Notes |
| 80 | + /// |
| 81 | + /// A common pattern of using `Refcount` is to free memory when the reference count reaches |
| 82 | + /// zero. This means that the reference to `Refcount` could become invalid after calling this |
| 83 | + /// function. This is fine as long as the reference to `Refcount` is no longer used when this |
| 84 | + /// function returns `false`. It is not necessary to use raw pointers in this scenario, see |
| 85 | + /// <https://github.com/rust-lang/rust/issues/55005>. |
| 86 | + #[inline] |
| 87 | + #[must_use = "use `dec` instead if you do not need to test if it is 0"] |
| 88 | + pub fn dec_and_test(&self) -> bool { |
| 89 | + // SAFETY: `self.as_ptr()` is valid. |
| 90 | + unsafe { bindings::refcount_dec_and_test(self.as_ptr()) } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// SAFETY: `refcount_t` is thread-safe. |
| 95 | +unsafe impl Send for Refcount {} |
| 96 | + |
| 97 | +// SAFETY: `refcount_t` is thread-safe. |
| 98 | +unsafe impl Sync for Refcount {} |
0 commit comments