|
| 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::types::Opaque; |
| 8 | + |
| 9 | +/// Atomic reference counter. |
| 10 | +/// |
| 11 | +/// This type is conceptually an atomic integer, but provides saturation semantics compared to |
| 12 | +/// normal atomic integers. Values in the negative range when viewed as a signed integer are |
| 13 | +/// saturation (bad) values. For details about the saturation semantics, please refer to top of |
| 14 | +/// [`include/linux/refcount.h`](srctree/include/refcount.h). |
| 15 | +/// |
| 16 | +/// Wraps the kernel's C `refcount_t`. |
| 17 | +#[repr(transparent)] |
| 18 | +pub struct Refcount(Opaque<bindings::refcount_t>); |
| 19 | + |
| 20 | +impl Refcount { |
| 21 | + /// Construct a new [`Refcount`] from an initial value. |
| 22 | + #[inline] |
| 23 | + pub fn new(value: i32) -> Self { |
| 24 | + // SAFETY: There are no safety requirements for this FFI call. |
| 25 | + Self(Opaque::new(unsafe { bindings::REFCOUNT_INIT(value) })) |
| 26 | + } |
| 27 | + |
| 28 | + #[inline] |
| 29 | + fn as_ptr(&self) -> *mut bindings::refcount_t { |
| 30 | + self.0.get() |
| 31 | + } |
| 32 | + |
| 33 | + /// Set a refcount's value. |
| 34 | + #[inline] |
| 35 | + pub fn set(&self, value: i32) { |
| 36 | + // SAFETY: `self.as_ptr()` is valid. |
| 37 | + unsafe { bindings::refcount_set(self.as_ptr(), value) } |
| 38 | + } |
| 39 | + |
| 40 | + /// Increment a refcount. |
| 41 | + /// |
| 42 | + /// It will saturate if overflows and `WARN`. It will also `WARN` if the refcount is 0, as this |
| 43 | + /// represents a possible use-after-free condition. |
| 44 | + /// |
| 45 | + /// Provides no memory ordering, it is assumed that caller already has a reference on the |
| 46 | + /// object. |
| 47 | + #[inline] |
| 48 | + pub fn inc(&self) { |
| 49 | + // SAFETY: self is valid. |
| 50 | + unsafe { bindings::refcount_inc(self.as_ptr()) } |
| 51 | + } |
| 52 | + |
| 53 | + /// Decrement a refcount. |
| 54 | + /// |
| 55 | + /// It will `WARN` on underflow and fail to decrement when saturated. |
| 56 | + /// |
| 57 | + /// Provides release memory ordering, such that prior loads and stores are done |
| 58 | + /// before. |
| 59 | + #[inline] |
| 60 | + pub fn dec(&self) { |
| 61 | + // SAFETY: `self.as_ptr()` is valid. |
| 62 | + unsafe { bindings::refcount_dec(self.as_ptr()) } |
| 63 | + } |
| 64 | + |
| 65 | + /// Decrement a refcount and test if it is 0. |
| 66 | + /// |
| 67 | + /// It will `WARN` on underflow and fail to decrement when saturated. |
| 68 | + /// |
| 69 | + /// Provides release memory ordering, such that prior loads and stores are done |
| 70 | + /// before, and provides an acquire ordering on success such that memory deallocation |
| 71 | + /// must come after. |
| 72 | + /// |
| 73 | + /// Returns true if the resulting refcount is 0, false otherwise. |
| 74 | + #[inline] |
| 75 | + #[must_use = "use `dec` instead you do not need to test if it is 0"] |
| 76 | + pub fn dec_and_test(&self) -> bool { |
| 77 | + // SAFETY: `self.as_ptr()` is valid. |
| 78 | + unsafe { bindings::refcount_dec_and_test(self.as_ptr()) } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// SAFETY: `refcount_t` is thread-safe. |
| 83 | +unsafe impl Send for Refcount {} |
| 84 | + |
| 85 | +// SAFETY: `refcount_t` is thread-safe. |
| 86 | +unsafe impl Sync for Refcount {} |
0 commit comments