Reference count
include/types/refcount.h View source View on GitHubFunctions
Section titled “Functions”refcount_init
Section titled “refcount_init”void refcount_init(refcount_t *rc, uint32_t val) source refcount_inc
Section titled “refcount_inc”bool refcount_inc(refcount_t *rc) source refcount_inc_not_zero
Section titled “refcount_inc_not_zero”bool refcount_inc_not_zero(refcount_t *rc) source refcount_read
Section titled “refcount_read”uint32_t refcount_read(refcount_t *rc) source refcount_dec_and_test
Section titled “refcount_dec_and_test”bool refcount_dec_and_test(refcount_t *rc) source refcount_put_not_last
Section titled “refcount_put_not_last”void refcount_put_not_last(refcount_t *rc) source drop a reference that is known not to be the last one
Macros
Section titled “Macros”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) */ REFCOUNT_ASSERT_ZERO
Section titled “REFCOUNT_ASSERT_ZERO”#define REFCOUNT_ASSERT_ZERO(rc) source Expansion
#define REFCOUNT_ASSERT_ZERO(rc) (enforce(atomic_load(&rc) == 0))