hop-2012/experiments/cmsg/ref.h

42 lines
862 B
C

/* Copyright (C) 2010, 2011 Tony Garnock-Jones. All rights reserved. */
#ifndef cmsg_ref_h
#define cmsg_ref_h
typedef struct refcount_t_ {
unsigned int count;
} refcount_t;
#define ZERO_REFCOUNT() ((refcount_t) { .count = 0 })
#define INCREF(x) ({ \
typeof(x) __x = (x); \
if (__x != NULL) { \
__x->refcount.count++; \
} \
__x; \
})
#define UNGRAB(x) ({ \
typeof(x) __x = (x); \
if (__x != NULL) { \
assert(__x->refcount.count); \
__x->refcount.count--; \
} \
__x; \
})
#define DECREF(x, dtor) ({ \
typeof(x) __x = (x); \
if (__x != NULL) { \
assert(__x->refcount.count); \
(__x->refcount.count)--; \
if (__x->refcount.count == 0) { \
(dtor)(__x); \
} \
} \
(typeof(__x)) 0; \
})
#endif