From 2b9f5916b3aebd8d34903d420ce8160121171534 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Sun, 13 Jan 2008 14:46:53 +0100 Subject: [PATCH] add refcounted types Signed-off-by: Pierre Habouzit --- lib-lib/mem.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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); \ -- 2.20.1