Coverage Report

Created: 2018-07-03 15:31

/home/travis/build/MoarVM/MoarVM/src/6model/reprs/SCRef.c
Line
Count
Source (jump to first uncovered line)
1
#include "moar.h"
2
3
/* This representation's function pointer table. */
4
static const MVMREPROps SCRef_this_repr;
5
6
/* Creates a new type object of this representation, and associates it with
7
 * the given HOW. */
8
144
static MVMObject * type_object_for(MVMThreadContext *tc, MVMObject *HOW) {
9
144
    MVMSTable *st  = MVM_gc_allocate_stable(tc, &SCRef_this_repr, HOW);
10
144
11
144
    MVMROOT(tc, st, {
12
144
        MVMObject *obj = MVM_gc_allocate_type_object(tc, st);
13
144
        MVM_ASSIGN_REF(tc, &(st->header), st->WHAT, obj);
14
144
        st->size = sizeof(MVMSerializationContext);
15
144
    });
16
144
17
144
    return st->WHAT;
18
144
}
19
20
/* Initializes a new instance. */
21
3.06k
static void initialize(MVMThreadContext *tc, MVMSTable *st, MVMObject *root, void *data) {
22
3.06k
    MVMObject *root_codes, *rep_indexes, *rep_scs, *owned_objects, *rm;
23
3.06k
24
3.06k
    MVMInstance       *instance     = tc->instance;
25
3.06k
    MVMObject         *BOOTIntArray = instance->boot_types.BOOTIntArray;
26
3.06k
    MVMSerializationContextBody *sc = ((MVMSerializationContext *)root)->body;
27
3.06k
28
3.06k
    MVM_gc_root_temp_push(tc, (MVMCollectable **)&root);
29
3.06k
30
3.06k
    rep_indexes = REPR(BOOTIntArray)->allocate(tc, STABLE(BOOTIntArray));
31
3.06k
    MVM_ASSIGN_REF(tc, &(root->header), sc->rep_indexes, rep_indexes);
32
3.06k
33
3.06k
    rm = MVM_repr_alloc_init(tc, tc->instance->boot_types.BOOTReentrantMutex);
34
3.06k
    MVM_ASSIGN_REF(tc, &(root->header), sc->mutex, rm);
35
3.06k
36
3.06k
    root_codes = REPR(instance->boot_types.BOOTArray)->allocate(tc, STABLE(instance->boot_types.BOOTArray));
37
3.06k
    MVM_ASSIGN_REF(tc, &(root->header), sc->root_codes, root_codes);
38
3.06k
39
3.06k
    rep_scs = REPR(instance->boot_types.BOOTArray)->allocate(tc, STABLE(instance->boot_types.BOOTArray));
40
3.06k
    MVM_ASSIGN_REF(tc, &(root->header), sc->rep_scs, rep_scs);
41
3.06k
42
3.06k
    owned_objects = REPR(instance->boot_types.BOOTArray)->allocate(tc, STABLE(instance->boot_types.BOOTArray));
43
3.06k
    MVM_ASSIGN_REF(tc, &(root->header), sc->owned_objects, owned_objects);
44
3.06k
45
3.06k
    MVM_gc_root_temp_pop(tc);
46
3.06k
}
47
48
/* Copies the body of one object to another. */
49
0
static void copy_to(MVMThreadContext *tc, MVMSTable *st, void *src, MVMObject *dest_root, void *dest) {
50
0
    MVM_exception_throw_adhoc(tc, "Cannot copy object with representation SCRef");
51
0
}
52
53
/* Called by the VM to mark any GCable items. */
54
3.84k
static void gc_mark(MVMThreadContext *tc, MVMSTable *st, void *data, MVMGCWorklist *worklist) {
55
3.84k
    MVMSerializationContextBody *sc = ((MVMSerializationContextBody **)data)[0];
56
3.84k
    MVMuint64 i;
57
3.84k
58
3.84k
    MVM_gc_worklist_add(tc, worklist, &sc->handle);
59
3.84k
    MVM_gc_worklist_add(tc, worklist, &sc->description);
60
3.84k
    MVM_gc_worklist_add(tc, worklist, &sc->root_codes);
61
3.84k
    MVM_gc_worklist_add(tc, worklist, &sc->rep_indexes);
62
3.84k
    MVM_gc_worklist_add(tc, worklist, &sc->rep_scs);
63
3.84k
    MVM_gc_worklist_add(tc, worklist, &sc->owned_objects);
64
3.84k
65
1.03M
    for (i = 0; i < sc->num_objects; i++)
66
1.02M
        MVM_gc_worklist_add(tc, worklist, &sc->root_objects[i]);
67
21.2k
    for (i = 0; i < sc->num_stables; i++)
68
17.4k
        MVM_gc_worklist_add(tc, worklist, &sc->root_stables[i]);
69
3.84k
70
3.84k
    MVM_gc_worklist_add(tc, worklist, &sc->sc);
71
3.84k
    MVM_gc_worklist_add(tc, worklist, &sc->mutex);
72
3.84k
73
3.84k
    /* Mark serialization reader, if we have one. */
74
3.84k
    if (sc->sr) {
75
951
        MVM_gc_worklist_add(tc, worklist, &(sc->sr->root.sc));
76
5.18k
        for (i = 0; i < sc->sr->root.num_dependencies; i++)
77
4.23k
            MVM_gc_worklist_add(tc, worklist, &(sc->sr->root.dependent_scs[i]));
78
951
        MVM_gc_worklist_add(tc, worklist, &(sc->sr->root.string_heap));
79
951
        MVM_gc_worklist_add(tc, worklist, &(sc->sr->root.string_comp_unit));
80
951
        MVM_gc_worklist_add(tc, worklist, &(sc->sr->codes_list));
81
951
        MVM_gc_worklist_add(tc, worklist, &(sc->sr->current_object));
82
2.11k
        for (i = 0; i < sc->sr->root.num_contexts; i++)
83
1.16k
            MVM_gc_worklist_add(tc, worklist, &(sc->sr->contexts[i]));
84
951
    }
85
3.84k
}
86
87
/* Called by the VM in order to free memory associated with this object. */
88
0
static void gc_free(MVMThreadContext *tc, MVMObject *obj) {
89
0
    MVMSerializationContext *sc = (MVMSerializationContext *)obj;
90
0
91
0
    if (sc->body == NULL)
92
0
        return;
93
0
94
0
    /* Remove from weakref lookup hash (which doesn't count as a root). */
95
0
    uv_mutex_lock(&tc->instance->mutex_sc_registry);
96
0
    HASH_DELETE(hash_handle, tc->instance->sc_weakhash, sc->body);
97
0
    tc->instance->all_scs[sc->body->sc_idx] = NULL;
98
0
    uv_mutex_unlock(&tc->instance->mutex_sc_registry);
99
0
100
0
    /* Free manually managed object and STable root list memory. */
101
0
    MVM_free(sc->body->root_objects);
102
0
    MVM_free(sc->body->root_stables);
103
0
104
0
    /* If we have a serialization reader, clean that up too. */
105
0
    if (sc->body->sr) {
106
0
        if (sc->body->sr->data_needs_free)
107
0
            MVM_free(sc->body->sr->data);
108
0
        MVM_free(sc->body->sr->root.dependent_scs);
109
0
        MVM_free(sc->body->sr->contexts);
110
0
        MVM_free(sc->body->sr->wl_objects.indexes);
111
0
        MVM_free(sc->body->sr->wl_stables.indexes);
112
0
        MVM_free(sc->body->sr);
113
0
    }
114
0
115
0
    /* Free body. */
116
0
    MVM_free(sc->body);
117
0
}
118
119
static const MVMStorageSpec storage_spec = {
120
    MVM_STORAGE_SPEC_REFERENCE, /* inlineable */
121
    0,                          /* bits */
122
    0,                          /* align */
123
    MVM_STORAGE_SPEC_BP_NONE,   /* boxed_primitive */
124
    0,                          /* can_box */
125
    0,                          /* is_unsigned */
126
};
127
128
/* Gets the storage specification for this representation. */
129
0
static const MVMStorageSpec * get_storage_spec(MVMThreadContext *tc, MVMSTable *st) {
130
0
    return &storage_spec;
131
0
}
132
133
/* Compose the representation. */
134
0
static void compose(MVMThreadContext *tc, MVMSTable *st, MVMObject *info) {
135
0
    /* Nothing to do for this REPR. */
136
0
}
137
138
1.17k
static MVMuint64 unmanaged_size(MVMThreadContext *tc, MVMSTable *st, void *data) {
139
1.17k
    MVMSerializationContextBody     *body      = ((MVMSerializationContextBody **)data)[0];
140
1.17k
    MVMuint64 size = 0;
141
1.17k
142
1.17k
    size += sizeof(MVMObject *) * body->num_objects;
143
1.17k
    size += sizeof(MVMSTable *) * body->num_stables;
144
1.17k
145
1.17k
    /* XXX probably have to measure the MVMSerializationReader, too */
146
1.17k
147
1.17k
    return size;
148
1.17k
}
149
150
0
static void describe_refs(MVMThreadContext *tc, MVMHeapSnapshotState *ss, MVMSTable *st, void *data) {
151
0
    MVMSerializationContextBody *body = ((MVMSerializationContextBody **)data)[0];
152
0
    MVMuint64 index;
153
0
154
0
    if (body->sr)
155
0
        return;
156
0
157
0
    for (index = 0; index < body->num_objects; index++)
158
0
        MVM_profile_heap_add_collectable_rel_const_cstr(tc, ss,
159
0
            (MVMCollectable *)body->root_objects[index], "Object root set");
160
0
    for (index = 0; index < body->num_stables; index++)
161
0
        MVM_profile_heap_add_collectable_rel_const_cstr(tc, ss,
162
0
            (MVMCollectable *)body->root_stables[index], "STable root set");
163
0
164
0
    MVM_profile_heap_add_collectable_rel_const_cstr(tc, ss,
165
0
        (MVMCollectable *)body->root_codes, "Root code refs");
166
0
    MVM_profile_heap_add_collectable_rel_const_cstr(tc, ss,
167
0
        (MVMCollectable *)body->rep_indexes, "Repossession indices");
168
0
    MVM_profile_heap_add_collectable_rel_const_cstr(tc, ss,
169
0
        (MVMCollectable *)body->rep_scs, "Repossession SCs");
170
0
    MVM_profile_heap_add_collectable_rel_const_cstr(tc, ss,
171
0
        (MVMCollectable *)body->owned_objects, "Owned Objects");
172
0
173
0
    MVM_profile_heap_add_collectable_rel_const_cstr(tc, ss,
174
0
        (MVMCollectable *)body->handle, "Handle");
175
0
    MVM_profile_heap_add_collectable_rel_const_cstr(tc, ss,
176
0
        (MVMCollectable *)body->description, "Description");
177
0
    MVM_profile_heap_add_collectable_rel_const_cstr(tc, ss,
178
0
        (MVMCollectable *)body->sc, "SC");
179
0
    MVM_profile_heap_add_collectable_rel_const_cstr(tc, ss,
180
0
        (MVMCollectable *)body->mutex, "Mutex");
181
0
182
0
    /* Mark serialization reader, if we have one. */
183
0
    if (body->sr) {
184
0
        MVM_profile_heap_add_collectable_rel_const_cstr(tc, ss,
185
0
            (MVMCollectable *)body->sr->root.sc, "Reader Root SC");
186
0
        for (index = 0; index < body->sr->root.num_dependencies; index++)
187
0
            MVM_profile_heap_add_collectable_rel_const_cstr(tc, ss,
188
0
                (MVMCollectable *)body->sr->root.dependent_scs[index], "SC Dependency (Reader)");
189
0
        MVM_profile_heap_add_collectable_rel_const_cstr(tc, ss,
190
0
            (MVMCollectable *)body->sr->root.string_heap, "String heap (Reader)");
191
0
        MVM_profile_heap_add_collectable_rel_const_cstr(tc, ss,
192
0
            (MVMCollectable *)body->sr->root.string_comp_unit, "String compilation unit (Reader)");
193
0
        MVM_profile_heap_add_collectable_rel_const_cstr(tc, ss,
194
0
            (MVMCollectable *)body->sr->codes_list, "Code objects list (Reader)");
195
0
        MVM_profile_heap_add_collectable_rel_const_cstr(tc, ss,
196
0
            (MVMCollectable *)body->sr->current_object, "Current object (Reader)");
197
0
    }
198
0
}
199
200
/* Initializes the representation. */
201
144
const MVMREPROps * MVMSCRef_initialize(MVMThreadContext *tc) {
202
144
    return &SCRef_this_repr;
203
144
}
204
205
static const MVMREPROps SCRef_this_repr = {
206
    type_object_for,
207
    MVM_gc_allocate_object,
208
    initialize,
209
    copy_to,
210
    MVM_REPR_DEFAULT_ATTR_FUNCS,
211
    MVM_REPR_DEFAULT_BOX_FUNCS,
212
    MVM_REPR_DEFAULT_POS_FUNCS,
213
    MVM_REPR_DEFAULT_ASS_FUNCS,
214
    MVM_REPR_DEFAULT_ELEMS,
215
    get_storage_spec,
216
    NULL, /* change_type */
217
    NULL, /* serialize */
218
    NULL, /* deserialize */
219
    NULL, /* serialize_repr_data */
220
    NULL, /* deserialize_repr_data */
221
    NULL, /* deserialize_stable_size */
222
    gc_mark,
223
    gc_free,
224
    NULL, /* gc_cleanup */
225
    NULL, /* gc_mark_repr_data */
226
    NULL, /* gc_free_repr_data */
227
    compose,
228
    NULL, /* spesh */
229
    "SCRef", /* name */
230
    MVM_REPR_ID_SCRef,
231
    unmanaged_size,
232
    describe_refs,
233
};