add refcounted types
authorPierre Habouzit <madcoder@debian.org>
Sun, 13 Jan 2008 13:46:53 +0000 (14:46 +0100)
committerPierre Habouzit <madcoder@debian.org>
Sun, 13 Jan 2008 13:46:53 +0000 (14:46 +0100)
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
lib-lib/mem.h

index 8d904b3..d981a80 100644 (file)
@@ -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);                                    \
 #define DO_INIT(type, prefix) \
     static inline type * prefix##_init(type *var) {         \
         p_clear(var, 1);                                    \