Reference counting with weak pointers.
More...
Reference counting with weak pointers.
◆ REF_MAGIC
| #define REF_MAGIC 0x26CB6E4C |
Magic value used in debug builds to check for corruption or invalid use of the ref_t structure.
Definition at line 25 of file ref.h.
◆ REF_COUNT
Get current reference count.
Primarily intended to be used with RCU protected objects to check if they are still alive within a RCU read critical section.
- Parameters
-
| ptr | Pointer to the struct containing ref_t as its first member, can be NULL. |
- Returns
- Current reference count, or
0 if ptr is NULL.
Definition at line 71 of file ref.h.
◆ REF
Value: ({ \
ref_inc(ref); \
ptr; \
})
Reference counting structure.
Increment reference count.
Atomically increments the reference counter. Used to avoid the need for a typecast. The magic number checking makes sure we cant accidentally misuse this.
- Parameters
-
| ptr | Pointer to the struct containing ref_t as its first member, can be NULL. |
- Returns
- The
ptr passed as input
Definition at line 82 of file ref.h.
◆ REF_TRY
Value: ({ \
ref_inc_try(ref) !=
NULL ? ptr :
NULL; \
})
#define NULL
Pointer error value.
Increment reference count, but only if the current count is not zero.
- Parameters
-
| ptr | Pointer to the struct containing ref_t as its first member, can be NULL. |
- Returns
- The
ptr passed as input, or NULL if the count was zero.
Definition at line 95 of file ref.h.
◆ UNREF
Value:
Decrement reference count.
Atomically decrements the reference counter. Used to avoid the need for a typecast. The magic number checking makes sure we cant accidentally misuse this.
- Parameters
-
| ptr | Pointer to the struct containing ref_t as its first member, can be NULL. |
Definition at line 109 of file ref.h.
◆ UNREF_DEFER
RAII-style cleanup for scoped references.
Uses GCC's cleanup attribute to automatically call ref_dec when going out of scope.
- Parameters
-
| ptr | Pointer to the struct containing ref_t as its first member, can be NULL. |
Definition at line 122 of file ref.h.
◆ ref_init()
| static void ref_init |
( |
ref_t * |
ref, |
|
|
void * |
callback |
|
) |
| |
|
inlinestatic |
Initialize a reference counter.
- Parameters
-
| ref | Pointer to the reference counter structure |
| callback | Callback to call when count reaches zero |
Definition at line 130 of file ref.h.
◆ ref_inc()
| static void * ref_inc |
( |
void * |
ptr | ) |
|
|
inlinestatic |
Increment reference count.
- Parameters
-
| ptr | Pointer to the struct containing ref_t as its first member, can be NULL. |
- Returns
- The
ptr passed as input
Definition at line 147 of file ref.h.
◆ ref_inc_try()
| static void * ref_inc_try |
( |
void * |
ptr | ) |
|
|
inlinestatic |
Increment reference count, but only if the current count is not zero.
- Parameters
-
| ptr | Pointer to the struct containing ref_t as its first member, can be NULL. |
- Returns
- The
ptr passed as input, or NULL if the count was zero.
Definition at line 166 of file ref.h.
◆ ref_dec()
| static void ref_dec |
( |
void * |
ptr | ) |
|
|
inlinestatic |
Decrement reference count.
If count reaches zero it calls the registered cleanup function.
- Parameters
-
| ptr | Pointer to the struct containing ref_t as its first member, can be NULL. |
Definition at line 195 of file ref.h.
◆ ref_defer_cleanup()
| static void ref_defer_cleanup |
( |
void ** |
ptr | ) |
|
|
inlinestatic |
◆ weak_ptr_set()
| static void weak_ptr_set |
( |
weak_ptr_t * |
wp, |
|
|
ref_t * |
ref, |
|
|
void(*)(void *) |
callback, |
|
|
void * |
arg |
|
) |
| |
|
inlinestatic |
Set a weak pointer.
The provided callback must not attempt to access the weak ptr as that would cause a deadlock.
- Parameters
-
| wp | Pointer to the weak pointer structure |
| ref | Pointer to the reference counting structure to point to, can be NULL. |
| callback | Callback function to call when the strong reference count reaches zero. |
| arg | Argument to pass to the callback function. |
Definition at line 252 of file ref.h.
◆ weak_ptr_clear()
Clear a weak pointer.
Will not invoke the callback.
- Parameters
-
| wp | Pointer to the weak pointer structure. |
Definition at line 280 of file ref.h.
◆ weak_ptr_get()
Upgrade a weak pointer to a strong pointer.
If the strong reference count is zero, NULL is returned.
- Parameters
-
| wp | Pointer to the weak pointer structure |
- Returns
- On success, pointer to the struct containing
ref_t as its first member. On failure, NULL.
Definition at line 316 of file ref.h.