PatchworkOS
Loading...
Searching...
No Matches
Reference counting

Reference counting. More...

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 DEREF_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 DEREF(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)
 

Detailed Description

Reference counting.

Macro Definition Documentation

◆ DEREF

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

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.

◆ DEREF_DEFER

#define DEREF_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; \
})

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.

◆ 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.

Function Documentation

◆ 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.

References assert, atomic_fetch_sub_explicit, atomic_thread_fence, ref_t::count, count, ref_t::free, ref_t::magic, memory_order_acquire, memory_order_relaxed, NULL, and REF_MAGIC.

Referenced by init_process_spawn(), and ref_defer_cleanup().

◆ ref_defer_cleanup()

static void ref_defer_cleanup ( void **  ptr)
inlinestatic

Definition at line 155 of file ref.h.

References ref_dec().

◆ 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.

References assert, atomic_fetch_add_explicit, ref_t::count, ref_t::magic, memory_order_relaxed, NULL, and REF_MAGIC.

◆ 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.

References atomic_init, ref_t::count, free(), ref_t::free, ref_t::magic, and REF_MAGIC.

Referenced by aml_object_new(), dentry_new(), file_new(), inode_new(), local_conn_new(), local_listen_new(), mount_new(), process_init(), shmem_object_new(), socket_new(), and superblock_new().