Reference counting.
More...
Reference counting.
|
| #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.
|
| |
|
| 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) |
| |
◆ 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
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 54 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 65 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 80 of file ref.h.
◆ ref_init()
| static void ref_init |
( |
ref_t * |
ref, |
|
|
void * |
free |
|
) |
| |
|
inlinestatic |
Initialize a reference counter.
- Parameters
-
| ref | Pointer to the reference counter structure |
| free | Cleanup function to call when count reaches zero |
Definition at line 92 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 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
-
| ptr | Pointer to the struct containing ref_t as its first member, can be NULL. |
Definition at line 127 of file ref.h.
◆ ref_defer_cleanup()
| static void ref_defer_cleanup |
( |
void ** |
ptr | ) |
|
|
inlinestatic |