about summary refs log tree commit diff
path: root/src/rt/rust_gc.cpp
blob: 26e48857ecbbda2c9b35d2f225c764dfbf126e8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Rust garbage collection.

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
#include <stdint.h>

#include "rust_abi.h"
#include "rust_debug.h"
#include "rust_gc.h"
#include "rust_internal.h"
#include "rust_shape.h"

#ifdef __WIN32__
#include <windows.h>
#else
#include <dlfcn.h>
#endif

using namespace stack_walk;

namespace gc {

weak_symbol<const uintptr_t> safe_point_data("rust_gc_safe_points");

struct root_info {
    intptr_t frame_offset;
    uintptr_t dynamic;  // 0 = static, 1 = dynamic
    const type_desc *tydesc;
};

struct root {
    const type_desc *tydesc;
    uint8_t *data;

    root(const root_info &info, const frame &frame)
    : tydesc(info.tydesc),
      data((uint8_t *)frame.bp + info.frame_offset) {}
};

struct safe_point {
    uintptr_t n_roots;
    root_info roots[0];
};

struct safe_point_index_entry {
    void (*ra)();                   // The return address.
    const struct safe_point *safe_point;   // The safe point.

    struct cmp {
        bool operator()(const safe_point_index_entry &entry, void (*ra)())
                const {
            return entry.ra < ra;
        }
        bool operator()(void (*ra)(), const safe_point_index_entry &entry)
                const {
            return ra < entry.ra;
        }
    };
};

class safe_point_map {
    uintptr_t n_safe_points;
    const safe_point_index_entry *index;
    const safe_point *safe_points;

public:
    safe_point_map() {
        const uintptr_t *data = *safe_point_data;
        n_safe_points = *data++;
        index = (const safe_point_index_entry *)data;
        data += n_safe_points * 2;
        safe_points = (const safe_point *)data;
    }

    const safe_point *get_safe_point(void (*addr)());
};

class gc {
private:
    rust_task *task;

    void mark(std::vector<root> &roots);
    void sweep();

public:
    gc(rust_task *in_task) : task(in_task) {}
    void run();
};

const safe_point *
safe_point_map::get_safe_point(void (*addr)()) {
    safe_point_index_entry::cmp cmp;
    const safe_point_index_entry *entry =
        std::lower_bound(index, index + n_safe_points, addr, cmp);
    return (entry && entry->ra == addr) ? entry->safe_point : NULL;
}

void
gc::mark(std::vector<root> &roots) {
    std::vector<root>::iterator ri = roots.begin(), rend = roots.end();
    while (ri < rend) {
        DPRINT("root: %p\n", ri->data);

        shape::arena arena;
        shape::type_param *params =
            shape::type_param::from_tydesc_and_data(ri->tydesc, ri->data,
                                                    arena);
        shape::log log(task, true, ri->tydesc->shape, params,
                       ri->tydesc->shape_tables, ri->data, std::cerr);
        log.walk();
        DPRINT("\n");

        ++ri;
    }
    // TODO
}

void
gc::sweep() {
    // TODO
}

void
gc::run() {
    safe_point_map map;

    // Find roots.
    std::vector<root> roots;
    std::vector<frame> call_stack = backtrace();
    for (unsigned i = 0; i < call_stack.size(); i++) {
        frame f = call_stack[i];
        const safe_point *sp = map.get_safe_point(f.ra);
        if (!sp)
            continue;

        DPRINT("%u: ra %p, ebp %p\n", i, call_stack[i].ra, call_stack[i].bp);
        for (unsigned j = 0; j < sp->n_roots; j++) {
            root r(sp->roots[j], f);
            roots.push_back(r);
        }
    }

    // Mark and sweep.
    mark(roots);
    sweep();
}

void
maybe_gc(rust_task *task) {
    if (*safe_point_data == NULL)
        return;

    static debug::flag zeal("RUST_GC_ZEAL");

    if (*zeal) {
        gc gc(task);
        gc.run();
    }
}

}