about summary refs log tree commit diff
path: root/src/rt
diff options
context:
space:
mode:
authorRob Arnold <robarnold@cs.cmu.edu>2011-06-30 19:24:45 -0700
committerEric Holk <eholk@mozilla.com>2011-07-01 16:59:10 -0700
commit02a5949abaa4e888ecb470023ea2f29cd6bde43b (patch)
tree21604562b2a29e2d0b63124a318c28800b77c813 /src/rt
parent73cc624e8e326f54eb0ea8bff70388d62dccd3cb (diff)
downloadrust-02a5949abaa4e888ecb470023ea2f29cd6bde43b.tar.gz
rust-02a5949abaa4e888ecb470023ea2f29cd6bde43b.zip
Add macro for refcounting runtime structures.
The macro with the extra dtor parameter is intended for structures like
rust_chan which may not necessarily delete themselves when the ref count
becomes 0. This functionality will be used in an upcoming changeset.
Diffstat (limited to 'src/rt')
-rw-r--r--src/rt/rust_internal.h21
1 files changed, 10 insertions, 11 deletions
diff --git a/src/rt/rust_internal.h b/src/rt/rust_internal.h
index d5c4f459f1c..9d7d714064f 100644
--- a/src/rt/rust_internal.h
+++ b/src/rt/rust_internal.h
@@ -97,19 +97,18 @@ static intptr_t const CONST_REFCOUNT = 0x7badface;
 static size_t const BUF_BYTES = 2048;
 
 // Every reference counted object should derive from this base class.
+// Or use this macro. The macro is preferred as the base class will be
+// disappearing.
 
-template <typename T> struct rc_base {
-    intptr_t ref_count;
-
-    void ref() {
-        ++ref_count;
-    }
+#define RUST_REFCOUNTED(T) \
+  RUST_REFCOUNTED_WITH_DTOR(T, delete (T*)this)
+#define RUST_REFCOUNTED_WITH_DTOR(T, dtor) \
+  intptr_t ref_count;      \
+  void ref() { ++ref_count; } \
+  void deref() { if (--ref_count == 0) { dtor; } }
 
-    void deref() {
-        if (--ref_count == 0) {
-            delete (T*)this;
-        }
-    }
+template <typename T> struct rc_base {
+    RUST_REFCOUNTED(T)
 
     rc_base();
     ~rc_base();