Struct Refcount

Source
pub struct Refcount(/* private fields */);
Expand description

Atomic reference counter.

This type is conceptually an atomic integer, but provides saturation semantics compared to normal atomic integers. Values in the negative range when viewed as a signed integer are saturation (bad) values. For details about the saturation semantics, please refer to top of include/linux/refcount.h.

Wraps the kernel’s C refcount_t.

Implementations§

Source§

impl Refcount

Source

pub fn new(value: i32) -> Self

Construct a new Refcount from an initial value.

The initial value should be non-saturated.

Source

pub fn as_atomic(&self) -> &Atomic<i32>

Get the underlying atomic counter that backs the refcount.

NOTE: Usage of this function is discouraged as it can circumvent the protections offered by refcount.h. If there is no way to achieve the result using APIs in refcount.h, then this function can be used. Otherwise consider adding a binding for the required API.

Source

pub fn set(&self, value: i32)

Set a refcount’s value.

Source

pub fn inc(&self)

Increment a refcount.

It will saturate if overflows and WARN. It will also WARN if the refcount is 0, as this represents a possible use-after-free condition.

Provides no memory ordering, it is assumed that caller already has a reference on the object.

Source

pub fn dec(&self)

Decrement a refcount.

It will WARN on underflow and fail to decrement when saturated.

Provides release memory ordering, such that prior loads and stores are done before.

Source

pub fn dec_and_test(&self) -> bool

Decrement a refcount and test if it is 0.

It will WARN on underflow and fail to decrement when saturated.

Provides release memory ordering, such that prior loads and stores are done before, and provides an acquire ordering on success such that memory deallocation must come after.

Returns true if the resulting refcount is 0, false otherwise.

§Notes

A common pattern of using Refcount is to free memory when the reference count reaches zero. This means that the reference to Refcount could become invalid after calling this function. This is fine as long as the reference to Refcount is no longer used when this function returns false. It is not necessary to use raw pointers in this scenario, see https://github.com/rust-lang/rust/issues/55005.

Trait Implementations§

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Init<T> for T

Source§

unsafe fn __init(self, slot: *mut T) -> Result<(), Infallible>

Initializes slot. Read more
Source§

fn chain<F>(self, f: F) -> ChainInit<Self, F, T, E>
where F: FnOnce(&mut T) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PinInit<T> for T

Source§

unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), Infallible>

Initializes slot. Read more
Source§

fn pin_chain<F>(self, f: F) -> ChainPinInit<Self, F, T, E>
where F: FnOnce(Pin<&mut T>) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.