From 02a5949abaa4e888ecb470023ea2f29cd6bde43b Mon Sep 17 00:00:00 2001 From: Rob Arnold Date: Thu, 30 Jun 2011 19:24:45 -0700 Subject: [PATCH] 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. --- src/rt/rust_internal.h | 21 ++++++++++----------- 1 file 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. + +#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; } } template struct rc_base { - intptr_t ref_count; - - void ref() { - ++ref_count; - } - - void deref() { - if (--ref_count == 0) { - delete (T*)this; - } - } + RUST_REFCOUNTED(T) rc_base(); ~rc_base();