Skip to content

Reference count

include/types/refcount.h View source View on GitHub
void refcount_init(refcount_t *rc, uint32_t val)
source
bool refcount_inc(refcount_t *rc)
source
bool refcount_inc_not_zero(refcount_t *rc)
source
uint32_t refcount_read(refcount_t *rc)
source
bool refcount_dec_and_test(refcount_t *rc)
source
void refcount_put_not_last(refcount_t *rc)
source

drop a reference that is known not to be the last one

REFCOUNT_GENERATE_GETREF_FOR_STRUCT_WITH_FAILURE_COND

Section titled “REFCOUNT_GENERATE_GETREF_FOR_STRUCT_WITH_FAILURE_COND”
#define REFCOUNT_GENERATE_GETREF_FOR_STRUCT_WITH_FAILURE_COND( \ __struct, __refcount_member, __failure_member, __failure_state)
source

generate a _getref function for a structure type, given a refcount member name and a failure condition. if the failure condition (e.g. object being cleaned up) is met, then the _getref will return false. will panic if refcount drops to zero before we get a chance to decrement it.

Expansion
#define REFCOUNT_GENERATE_GETREF_FOR_STRUCT_WITH_FAILURE_COND( \
    __struct, __refcount_member, __failure_member, __failure_state) \
    static inline bool __struct##_getref(struct __struct *obj) { \
        uint32_t old; \
        while (true) { \
            old = atomic_load_acq(&obj->__refcount_member); \
 \
            /* panic if refcount is zero (possible UAF) */
#define REFCOUNT_ASSERT_ZERO(rc)
source
Expansion
#define REFCOUNT_ASSERT_ZERO(rc) (enforce(atomic_load(&rc) == 0))