PatchworkOS  da8a090
A non-POSIX operating system.
Loading...
Searching...
No Matches
Reference counting

Reference counting. More...

Collaboration diagram for Reference counting:

Detailed Description

Reference counting.

Data Structures

struct  ref_t
 Reference counting structure. More...
 

Macros

#define REF_MAGIC   0x26CB6E4C
 Magic value used in debug builds to check for corruption or invalid use of the ref_t structure.
 
#define UNREF_DEFER(ptr)   __attribute__((cleanup(ref_defer_cleanup))) void* CONCAT(p, __COUNTER__) = (ptr)
 RAII-style cleanup for scoped references.
 
#define REF(ptr)
 Increment reference count.
 
#define UNREF(ptr)
 Decrement reference count.
 

Functions

static void ref_init (ref_t *ref, void *free)
 Initialize a reference counter.
 
static void * ref_inc (void *ptr)
 Increment reference count.
 
static void ref_dec (void *ptr)
 Decrement reference count.
 
static void ref_defer_cleanup (void **ptr)
 

Macro Definition Documentation

◆ 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 19 of file ref.h.

◆ UNREF_DEFER

#define UNREF_DEFER (   ptr)    __attribute__((cleanup(ref_defer_cleanup))) void* CONCAT(p, __COUNTER__) = (ptr)

RAII-style cleanup for scoped references.

Uses GCC's cleanup attribute to automatically call ref_dec when going out of scope.

Parameters
ptrPointer to the struct containing ref_t as its first member, can be NULL.

Definition at line 54 of file ref.h.

◆ REF

#define REF (   ptr)
Value:
({ \
ref_t* ref = (ref_t*)ptr; \
ref_inc(ref); \
ptr; \
})
Reference counting structure.
Definition ref.h:30

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
ptrPointer to the struct containing ref_t as its first member, can be NULL.
Returns
The ptr passed as input

Definition at line 65 of file ref.h.

◆ UNREF

#define UNREF (   ptr)
Value:
({ \
ref_t* ref = (ref_t*)ptr; \
ref_dec(ref); \
})

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
ptrPointer to the struct containing ref_t as its first member, can be NULL.

Definition at line 80 of file ref.h.

Function Documentation

◆ ref_init()

static void ref_init ( ref_t ref,
void *  free 
)
inlinestatic

Initialize a reference counter.

Parameters
refPointer to the reference counter structure
freeCleanup function to call when count reaches zero

Definition at line 92 of file ref.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ref_inc()

static void * ref_inc ( void *  ptr)
inlinestatic

Increment reference count.

Parameters
ptrPointer to the struct containing ref_t as its first member, can be NULL.
Returns
The ptr passed as input

Definition at line 107 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
ptrPointer to the struct containing ref_t as its first member, can be NULL.

Definition at line 127 of file ref.h.

Here is the caller graph for this function:

◆ ref_defer_cleanup()

static void ref_defer_cleanup ( void **  ptr)
inlinestatic

Definition at line 155 of file ref.h.

Here is the call graph for this function: