From: Pierre Habouzit Date: Sun, 13 Jan 2008 13:46:53 +0000 (+0100) Subject: add refcounted types X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=2b9f5916b3aebd8d34903d420ce8160121171534;p=apps%2Fmadmutt.git add refcounted types Signed-off-by: Pierre Habouzit --- diff --git a/lib-lib/mem.h b/lib-lib/mem.h index 8d904b3..d981a80 100644 --- a/lib-lib/mem.h +++ b/lib-lib/mem.h @@ -100,6 +100,27 @@ static inline void *xmemdupstr(const void *src, ssize_t len) { } +#define DO_REFCNT(type, prefix) \ + static inline type *prefix##_new(void) { \ + type *res = prefix##_init(p_new(type, 1)); \ + res->refcnt = 1; \ + return res; \ + } \ + static inline type *prefix##_dup(type *t) { \ + t->refcnt++; \ + return t; \ + } \ + static inline void prefix##_delete(type **tp) { \ + if (*tp) { \ + if (--(*tp)->refcnt > 0) { \ + *tp = NULL; \ + } else { \ + prefix##_wipe(*tp); \ + p_delete(tp); \ + } \ + } \ + } + #define DO_INIT(type, prefix) \ static inline type * prefix##_init(type *var) { \ p_clear(var, 1); \