Mercurial > libjeffpc
changeset 76:73fd50074f8f v0.10-rc1
refcnt: make refcnt_t opaque
C compilers happily cast across typedefs. For example, if we have:
typedef foo_t bar_t;
void test(bar_t *);
Even though the intention is to have test() take a bar_t pointer, the compiler
will happily let you pass in a foo_t pointer. Wrapping the parent type in a
structure forces the compiler to error out.
To prevent accidental uses of atomic_*() functions on refcnt_t, we wrap the
atomic_t in a struct.
Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author | Josef 'Jeff' Sipek <jeffpc@josefsipek.net> |
---|---|
date | Tue, 05 Apr 2016 11:13:01 -0400 |
parents | 3b9cce8766bf |
children | 82c9e152777f |
files | include/jeffpc/refcnt.h |
diffstat | 1 files changed, 7 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/include/jeffpc/refcnt.h Tue Apr 05 10:49:17 2016 -0400 +++ b/include/jeffpc/refcnt.h Tue Apr 05 11:13:01 2016 -0400 @@ -26,28 +26,30 @@ #include <jeffpc/atomic.h> #include <jeffpc/error.h> -typedef atomic_t refcnt_t; +typedef struct { + atomic_t count; +} refcnt_t; static inline void refcnt_init(refcnt_t *x, uint32_t v) { - atomic_set(x, v); + atomic_set(&x->count, v); } static inline uint32_t refcnt_read(refcnt_t *x) { - return atomic_read(x); + return atomic_read(&x->count); } /* INTERNAL FUNCTION - DO NOT USE DIRECTLY */ static inline void __refcnt_inc(refcnt_t *x) { - atomic_inc(x); + atomic_inc(&x->count); } /* INTERNAL FUNCTION - DO NOT USE DIRECTLY */ static inline uint32_t __refcnt_dec(refcnt_t *x) { - return atomic_dec(x); + return atomic_dec(&x->count); } #define REFCNT_PROTOTYPES(type, name) \