Skip to content

Commit

Permalink
Add macro for refcounting runtime structures.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
robarnold authored and Eric Holk committed Jul 1, 2011
1 parent 73cc624 commit 02a5949
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/rt/rust_internal.h
Expand Up @@ -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();
Expand Down

0 comments on commit 02a5949

Please sign in to comment.