about summary refs log tree commit diff
path: root/src/rt/rust_box_annihilator.cpp
blob: 21f7b9d2dc37755062d1321ac9162ce50e6e4fd4 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "rust_globals.h"
#include "rust_task.h"
#include "rust_shape.h"

class annihilator : public shape::data<annihilator,shape::ptr> {
    friend class shape::data<annihilator,shape::ptr>;

    annihilator(const annihilator &other, const shape::ptr &in_dp)
        : shape::data<annihilator,shape::ptr>(other.task, other.align,
                                        other.sp, other.params,
                                        other.tables, in_dp) {}

    annihilator(const annihilator &other,
          const uint8_t *in_sp,
          const shape::type_param *in_params,
          const rust_shape_tables *in_tables = NULL)
        : shape::data<annihilator,shape::ptr>(other.task,
                                        other.align,
                                        in_sp,
                                        in_params,
                                        in_tables ? in_tables : other.tables,
                                        other.dp) {}

    annihilator(const annihilator &other,
          const uint8_t *in_sp,
          const shape::type_param *in_params,
          const rust_shape_tables *in_tables,
          shape::ptr in_dp)
        : shape::data<annihilator,shape::ptr>(other.task,
                                        other.align,
                                        in_sp,
                                        in_params,
                                        in_tables,
                                        in_dp) {}

    annihilator(rust_task *in_task,
          bool in_align,
          const uint8_t *in_sp,
          const shape::type_param *in_params,
          const rust_shape_tables *in_tables,
          uint8_t *in_data)
        : shape::data<annihilator,shape::ptr>(in_task, in_align, in_sp,
                                        in_params, in_tables, in_data) {}

    void walk_vec2(bool is_pod, uint16_t sp_size) {
        void *vec = shape::get_dp<void *>(dp);
        walk_vec2(is_pod, get_vec_data_range(dp));
        task->kernel->free(vec);
    }

    void walk_vec2(bool is_pod,
                  const std::pair<shape::ptr,shape::ptr> &data_range) {
        annihilator sub(*this, data_range.first);
        shape::ptr data_end = sub.end_dp = data_range.second;
        while (sub.dp < data_end) {
            sub.walk_reset();
            sub.align = true;
        }
    }

    void walk_tag2(shape::tag_info &tinfo, uint32_t tag_variant) {
        shape::data<annihilator,shape::ptr>
          ::walk_variant1(tinfo, tag_variant);
    }

    void walk_uniq2() {
        void *x = *((void **)dp);
        // free contents first:
        shape::data<annihilator,shape::ptr>::walk_uniq_contents1();
        // now free the ptr:
        task->kernel->free(x);
    }

    void walk_box2() {
        // In annihilator phase, do not walk the box contents.  There is an
        // outer loop walking all remaining boxes, and this box may well
        // have been freed already!
    }

    void walk_fn2(char code) {
        switch (code) {
          case shape::SHAPE_UNIQ_FN: {
              fn_env_pair pair = *(fn_env_pair*)dp;

              if (pair.env) {
                  // free closed over data:
                  shape::data<annihilator,shape::ptr>::walk_fn_contents1();

                  // now free the ptr:
                  task->kernel->free(pair.env);
              }
              break;
          }
          case shape::SHAPE_BOX_FN: {
              // the box will be visited separately:
              shape::bump_dp<void*>(dp); // skip over the code ptr
              walk_box2();               // walk over the environment ptr
              break;
          }
          case shape::SHAPE_BARE_FN:         // Does not close over data.
          case shape::SHAPE_STACK_FN: break; // Not reachable from heap.
          default: abort();
        }
    }

    void walk_obj2() {
        return;
    }

    void walk_iface2() {
        walk_box2();
    }

    void walk_tydesc2(char kind) {
        switch(kind) {
          case shape::SHAPE_TYDESC:
          case shape::SHAPE_SEND_TYDESC:
            break;
          default: abort();
        }
    }

    struct run_dtor_args {
        const shape::rust_fn *dtor;
        void *data;
    };

    typedef void (*dtor)(void **retptr, void *env, void *dptr);

    static void run_dtor(run_dtor_args *args) {
        dtor f = (dtor)args->dtor;
        f(NULL, args->dtor->env, args->data);
    }

    void walk_res2(const shape::rust_fn *dtor, unsigned n_params,
                   const shape::type_param *params, const uint8_t *end_sp,
                   bool live) {
        void *data = (void*)(uintptr_t)dp;
        // Switch back to the Rust stack to run the destructor
        run_dtor_args args = {dtor, data};
        task->call_on_rust_stack((void*)&args, (void*)run_dtor);

        while (this->sp != end_sp) {
            this->walk();
            align = true;
        }
    }

    void walk_subcontext2(annihilator &sub) { sub.walk(); }

    void walk_uniq_contents2(annihilator &sub) { sub.walk(); }

    void walk_struct2(const uint8_t *end_sp) {
        while (this->sp != end_sp) {
            this->walk();
            align = true;
        }
    }

    void walk_variant2(shape::tag_info &tinfo, uint32_t variant_id,
                      const std::pair<const uint8_t *,const uint8_t *>
                      variant_ptr_and_end) {
        annihilator sub(*this, variant_ptr_and_end.first, tinfo.params);

        const uint8_t *variant_end = variant_ptr_and_end.second;
        while (sub.sp < variant_end) {
            sub.walk();
            align = true;
        }
    }

    template<typename T>
    inline void walk_number2() { /* no-op */ }

public:
    static void do_annihilate(rust_task *task, rust_opaque_box *box);
};

void
annihilator::do_annihilate(rust_task *task, rust_opaque_box *box) {
    const type_desc *tydesc = box->td;
    uint8_t *p = (uint8_t*) box_body(box);
    shape::arena arena;
    shape::type_param *params =
        shape::type_param::from_tydesc_and_data(tydesc, p, arena);

    annihilator annihilator(task, true, tydesc->shape,
                            params, tydesc->shape_tables, p);
    annihilator.walk();
    task->boxed.free(box);
}

void
annihilate_box(rust_task *task, rust_opaque_box *box) {
    annihilator::do_annihilate(task, box);
}

void
annihilate_boxes(rust_task *task) {
    LOG(task, gc, "annihilating boxes for task %p", task);

    boxed_region *boxed = &task->boxed;
    rust_opaque_box *box = boxed->first_live_alloc();
    while (box != NULL) {
        rust_opaque_box *tmp = box;
        box = box->next;
        annihilate_box(task, tmp);
    }
}