Skip to content

Commit 02a5949

Browse files
robarnoldEric Holk
authored andcommitted
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.
1 parent 73cc624 commit 02a5949

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/rt/rust_internal.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,18 @@ static intptr_t const CONST_REFCOUNT = 0x7badface;
9797
static size_t const BUF_BYTES = 2048;
9898

9999
// Every reference counted object should derive from this base class.
100+
// Or use this macro. The macro is preferred as the base class will be
101+
// disappearing.
100102

101-
template <typename T> struct rc_base {
102-
intptr_t ref_count;
103-
104-
void ref() {
105-
++ref_count;
106-
}
103+
#define RUST_REFCOUNTED(T) \
104+
RUST_REFCOUNTED_WITH_DTOR(T, delete (T*)this)
105+
#define RUST_REFCOUNTED_WITH_DTOR(T, dtor) \
106+
intptr_t ref_count; \
107+
void ref() { ++ref_count; } \
108+
void deref() { if (--ref_count == 0) { dtor; } }
107109

108-
void deref() {
109-
if (--ref_count == 0) {
110-
delete (T*)this;
111-
}
112-
}
110+
template <typename T> struct rc_base {
111+
RUST_REFCOUNTED(T)
113112

114113
rc_base();
115114
~rc_base();

0 commit comments

Comments
 (0)