Skip to content

Commit e26b188

Browse files
committed
rt: Stub code for the cycle collector
1 parent c616911 commit e26b188

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

mk/rt.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ RUNTIME_CS := rt/sync/timer.cpp \
2727
rt/rust_obstack.cpp \
2828
rt/rust_gc.cpp \
2929
rt/rust_abi.cpp \
30+
rt/rust_cc.cpp \
3031
rt/memory_region.cpp \
3132
rt/test/rust_test_harness.cpp \
3233
rt/test/rust_test_runtime.cpp \

src/rt/rust_cc.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Rust cycle collector. Temporary, but will probably stick around for some
2+
// time until LLVM's GC infrastructure is more mature.
3+
4+
#include <cstdio>
5+
#include <cstdlib>
6+
#include <map>
7+
#include <vector>
8+
#include "rust_gc.h"
9+
#include "rust_internal.h"
10+
#include "rust_shape.h"
11+
#include "rust_task.h"
12+
13+
#undef DPRINT
14+
#define DPRINT(fmt,...) fprintf(stderr, fmt, ##__VA_ARGS__)
15+
16+
namespace cc {
17+
18+
void
19+
do_cc(rust_task *task) {
20+
std::map<void *,type_desc *>::iterator begin(task->local_allocs.begin());
21+
std::map<void *,type_desc *>::iterator end(task->local_allocs.end());
22+
while (begin != end) {
23+
void *p = begin->first;
24+
type_desc *tydesc = begin->second;
25+
26+
DPRINT("marking allocation: %p, tydesc=%p\n", p, tydesc);
27+
28+
// Prevents warnings for now
29+
(void)p;
30+
(void)tydesc;
31+
#if 0
32+
shape::arena arena;
33+
shape::type_param *params =
34+
shape::type_param::from_tydesc(tydesc, arena);
35+
mark mark(task, true, tydesc->shape, params, tydesc->shape_tables, p);
36+
mark.walk();
37+
#endif
38+
39+
++begin;
40+
}
41+
}
42+
43+
void
44+
maybe_cc(rust_task *task) {
45+
// FIXME: We ought to lock this.
46+
static int zeal = -1;
47+
if (zeal == -1) {
48+
char *ev = getenv("RUST_CC_ZEAL");
49+
zeal = ev && ev[0] != '\0' && ev[0] != '0';
50+
}
51+
52+
if (zeal)
53+
do_cc(task);
54+
}
55+
56+
} // end namespace cc
57+

src/rt/rust_cc.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Rust cycle collector. Temporary, but will probably stick around for some
2+
// time until LLVM's GC infrastructure is more mature.
3+
4+
#ifndef RUST_CC_H
5+
#define RUST_CC_H
6+
7+
struct rust_task;
8+
9+
namespace cc {
10+
11+
void do_cc(rust_task *task);
12+
void maybe_cc(rust_task *task);
13+
14+
} // end namespace cc
15+
16+
#endif
17+

src/rt/rust_upcall.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "rust_cc.h"
12
#include "rust_gc.h"
23
#include "rust_internal.h"
34
#include "rust_unwind.h"
@@ -61,6 +62,7 @@ upcall_malloc(rust_task *task, size_t nbytes, type_desc *td) {
6162
nbytes, td);
6263

6364
gc::maybe_gc(task);
65+
cc::maybe_cc(task);
6466

6567
// TODO: Maybe use dladdr here to find a more useful name for the
6668
// type_desc.

0 commit comments

Comments
 (0)