Skip to content

Commit

Permalink
Add file I forgot to commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tbodt committed Feb 17, 2020
1 parent dcb19ff commit ba78d23
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions util/refcount.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef UTIL_REFCOUNT_H
#define UTIL_REFCOUNT_H
#include <stdatomic.h>

// An industrial-strength refcounting implementation.
// Safety first! Make sure to:
// - Write a release for every retain
// - Get exclusive access to the reference before retaining or releasing it

struct refcount {
atomic_uint rc;
};

static inline void __refcount_init(struct refcount *refcount) {
refcount->rc = 1;
}
#define refcount_init(obj) __refcount_init(&(obj)->refcount)

static inline int __refcount_get(struct refcount *refcount) {
return refcount->rc;
}
#define refcount_get(obj) __refcount_get(&(obj)->refcount)

#define DECLARE_REFCOUNT(type) \
void type##_retain(struct type *obj); \
void type##_release(struct type *obj)

#define __DEFINE_REFCOUNT(type, qualifiers) \
qualifiers struct type *type##_retain(struct type *obj) { \
obj->refcount.rc++; \
return obj; \
} \
static void type##_cleanup(struct type *obj); \
qualifiers void type##_release(struct type *obj) { \
if (--obj->refcount.rc == 0) \
type##_cleanup(obj); \
}

#define DEFINE_REFCOUNT(type) __DEFINE_REFCOUNT(type, )
#define DEFINE_REFCOUNT_STATIC(type) __DEFINE_REFCOUNT(type, static)

#endif

0 comments on commit ba78d23

Please sign in to comment.