---
name: "refcount_init"
section: "9"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/refcount/9"
---

# refcount\_init(9)

manage a simple reference counter

Part of [refcount(9)](/refcount/9.md).

## NAME

**refcount**, **refcount\_init**, **refcount\_acquire**, **refcount\_release** – manage a simple reference counter

## SYNOPSIS

**#include \<sys/param.h\>**

**#include \<sys/refcount.h\>**

*void*\

**refcount\_init**(*volatile u\_int \*count*, *u\_int value*);

*u\_int*\

**refcount\_load**(*volatile u\_int \*count*);

*u\_int*\

**refcount\_acquire**(*volatile u\_int \*count*);

*bool*\

**refcount\_acquire\_checked**(*volatile u\_int \*count*);

*bool*\

**refcount\_acquire\_if\_not\_zero**(*volatile u\_int \*count*);

*bool*\

**refcount\_release**(*volatile u\_int \*count*);

*bool*\

**refcount\_release\_if\_last**(*volatile u\_int \*count*);

*bool*\

**refcount\_release\_if\_not\_last**(*volatile u\_int \*count*);

## DESCRIPTION

The **refcount** functions provide an API to manage a simple reference counter. The caller provides the storage for the counter in an unsigned integer. A pointer to this integer is passed via *count*. Usually the counter is used to manage the lifetime of an object and is stored as a member of the object.

Currently all functions are implemented as static inline.

The **refcount\_init**() function is used to set the initial value of the counter to *value*. It is normally used when creating a reference-counted object.

The **refcount\_load**() function returns a snapshot of the counter value. This value may immediately become out-of-date in the absence of external synchronization. **refcount\_load**() should be used instead of relying on the properties of the *volatile* qualifier.

The **refcount\_acquire**() function is used to acquire a new reference. It returns the counter value before the new reference was acquired. The caller is responsible for ensuring that it holds a valid reference while obtaining a new reference. For example, if an object is stored on a list and the list holds a reference on the object, then holding a lock that protects the list provides sufficient protection for acquiring a new reference.

The **refcount\_acquire\_checked**() variant performs the same operation as **refcount\_acquire**(), but additionally checks that the *count* value does not overflow as result of the operation. It returns true if the reference was successfully obtained, and false if it was not, due to the overflow.

The **refcount\_acquire\_if\_not\_zero**() function is yet another variant of **refcount\_acquire**(), which only obtains the reference when some reference already exists. In other words, *\*count* must be already greater than zero for the function to succeed, in which case the return value is true, otherwise false is returned.

The **refcount\_release**() function is used to release an existing reference. The function returns true if the reference being released was the last reference; otherwise, it returns false.

The **refcount\_release\_if\_last**() and **refcount\_release\_if\_not\_last**() functions are variants of **refcount\_release**() which only drop the reference when it is or is not the last reference, respectively. In other words, **refcount\_release\_if\_last**() returns true when *\*count* is equal to one, in which case it is decremented to zero. Otherwise, *\*count* is not modified and the function returns false. Similarly, **refcount\_release\_if\_not\_last**() returns true when *\*count* is greater than one, in which case *\*count* is decremented. Otherwise, if *\*count* is equal to one, the reference is not released and the function returns false.

Note that these routines do not provide any inter-CPU synchronization or data protection for managing the counter. The caller is responsible for any additional synchronization needed by consumers of any containing objects. In addition, the caller is also responsible for managing the life cycle of any containing objects including explicitly releasing any resources when the last reference is released.

The **refcount\_release**() unconditionally executes a release fence (see [atomic(9)](/atomic/9.md)) before releasing the reference, which synchronizes with an acquire fence executed right before returning the true value. This ensures that the destructor, supposedly executed by the caller after the last reference was dropped, sees all updates done during the lifetime of the object.

## RETURN VALUES

The **refcount\_release** function returns true when releasing the last reference and false when releasing any other reference.

## HISTORY

These functions were introduced in FreeBSD 6.0.
