Coverage Report

Created: 2017-04-15 07:07

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