/
HashAttrStore.c
181 lines (154 loc) · 6.77 KB
/
HashAttrStore.c
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
#include "moar.h"
/* This representation's function pointer table. */
static const MVMREPROps HashAttrStore_this_repr;
/* Creates a new type object of this representation, and associates it with
* the given HOW. */
static MVMObject * type_object_for(MVMThreadContext *tc, MVMObject *HOW) {
MVMSTable *st = MVM_gc_allocate_stable(tc, &HashAttrStore_this_repr, HOW);
MVMROOT(tc, st, {
MVMObject *obj = MVM_gc_allocate_type_object(tc, st);
MVM_ASSIGN_REF(tc, &(st->header), st->WHAT, obj);
st->size = sizeof(MVMHashAttrStore);
});
return st->WHAT;
}
/* Copies the body of one object to another. */
static void copy_to(MVMThreadContext *tc, MVMSTable *st, void *src, MVMObject *dest_root, void *dest) {
MVMHashAttrStoreBody *src_body = (MVMHashAttrStoreBody *)src;
MVMHashAttrStoreBody *dest_body = (MVMHashAttrStoreBody *)dest;
MVMStrHashTable *src_hashtable = &(src_body->hashtable);
MVMStrHashTable *dest_hashtable = &(dest_body->hashtable);
if (MVM_str_hash_entry_size(tc, dest_hashtable)) {
// XXX Is this a valid assumption?
MVM_oops(tc, "copy_to on MVMHash that is already initialized");
}
MVM_str_hash_build(tc, dest_hashtable, sizeof(MVMHashEntry),
MVM_str_hash_count(tc, src_hashtable));
MVMStrHashIterator iterator = MVM_str_hash_first(tc, src_hashtable);
MVMHashEntry *entry;
while ((entry = MVM_str_hash_current(tc, src_hashtable, iterator))) {
MVMHashEntry *new_entry = MVM_str_hash_insert_nt(tc, dest_hashtable, entry->hash_handle.key);
MVM_ASSIGN_REF(tc, &(dest_root->header), new_entry->value, entry->value);
MVM_gc_write_barrier(tc, &(dest_root->header), &(entry->hash_handle.key->common.header));
iterator = MVM_str_hash_next(tc, src_hashtable, iterator);
}
}
/* Adds held objects to the GC worklist. */
static void gc_mark(MVMThreadContext *tc, MVMSTable *st, void *data, MVMGCWorklist *worklist) {
MVMHashAttrStoreBody *body = (MVMHashAttrStoreBody *)data;
MVMStrHashTable *hashtable = &(body->hashtable);
MVM_gc_worklist_presize_for(tc, worklist, 2 * MVM_str_hash_count(tc, hashtable));
MVMStrHashIterator iterator = MVM_str_hash_first(tc, hashtable);
MVMHashEntry *current;
while ((current = MVM_str_hash_current(tc, hashtable, iterator))) {
MVM_gc_worklist_add(tc, worklist, ¤t->hash_handle.key);
MVM_gc_worklist_add(tc, worklist, ¤t->value);
iterator = MVM_str_hash_next(tc, hashtable, iterator);
}
}
/* Called by the VM in order to free memory associated with this object. */
static void gc_free(MVMThreadContext *tc, MVMObject *obj) {
MVMHashAttrStore *h = (MVMHashAttrStore *)obj;
MVMStrHashTable *hashtable = &(h->body.hashtable);
MVM_str_hash_demolish(tc, hashtable);
}
static void get_attribute(MVMThreadContext *tc, MVMSTable *st, MVMObject *root,
void *data, MVMObject *class_handle, MVMString *name, MVMint64 hint,
MVMRegister *result_reg, MVMuint16 kind) {
MVMHashAttrStoreBody *body = (MVMHashAttrStoreBody *)data;
MVMStrHashTable *hashtable = &(body->hashtable);
if (MVM_UNLIKELY(kind != MVM_reg_obj))
MVM_exception_throw_adhoc(tc,
"HashAttrStore representation does not support native attribute storage");
MVMHashEntry *entry = MVM_str_hash_fetch(tc, hashtable, name);
result_reg->o = entry != NULL ? entry->value : tc->instance->VMNull;
}
static void bind_attribute(MVMThreadContext *tc, MVMSTable *st, MVMObject *root,
void *data, MVMObject *class_handle, MVMString *name, MVMint64 hint,
MVMRegister value_reg, MVMuint16 kind) {
MVMHashAttrStoreBody *body = (MVMHashAttrStoreBody *)data;
MVMStrHashTable *hashtable = &(body->hashtable);
if (!MVM_str_hash_key_is_valid(tc, name)) {
MVM_str_hash_key_throw_invalid(tc, name);
}
if (MVM_UNLIKELY(kind != MVM_reg_obj))
MVM_exception_throw_adhoc(tc,
"HashAttrStore representation does not support native attribute storage");
if (!MVM_str_hash_entry_size(tc, hashtable)) {
MVM_str_hash_build(tc, hashtable, sizeof(MVMHashEntry), 0);
}
MVMHashEntry *entry = MVM_str_hash_lvalue_fetch_nt(tc, hashtable, name);
MVM_ASSIGN_REF(tc, &(root->header), entry->value, value_reg.o);
if (!entry->hash_handle.key) {
entry->hash_handle.key = name;
MVM_gc_write_barrier(tc, &(root->header), &(name->common.header));
}
}
static MVMint64 is_attribute_initialized(MVMThreadContext *tc, MVMSTable *st, void *data, MVMObject *class_handle, MVMString *name, MVMint64 hint) {
MVMHashAttrStoreBody *body = (MVMHashAttrStoreBody *)data;
MVMStrHashTable *hashtable = &(body->hashtable);
MVMHashEntry *entry = MVM_str_hash_fetch(tc, hashtable, name);
return entry != NULL;
}
static MVMint64 hint_for(MVMThreadContext *tc, MVMSTable *st, MVMObject *class_handle, MVMString *name) {
return MVM_NO_HINT;
}
static const MVMStorageSpec storage_spec = {
MVM_STORAGE_SPEC_REFERENCE, /* inlineable */
0, /* bits */
0, /* align */
MVM_STORAGE_SPEC_BP_NONE, /* boxed_primitive */
0, /* can_box */
0, /* is_unsigned */
};
/* Gets the storage specification for this representation. */
static const MVMStorageSpec * get_storage_spec(MVMThreadContext *tc, MVMSTable *st) {
return &storage_spec;
}
/* Compose the representation. */
static void compose(MVMThreadContext *tc, MVMSTable *st, MVMObject *info) {
/* Nothing to do for this REPR. */
}
/* Set the size of the STable. */
static void deserialize_stable_size(MVMThreadContext *tc, MVMSTable *st, MVMSerializationReader *reader) {
st->size = sizeof(MVMHashAttrStore);
}
/* Initializes the representation. */
const MVMREPROps * MVMHashAttrStore_initialize(MVMThreadContext *tc) {
return &HashAttrStore_this_repr;
}
static const MVMREPROps HashAttrStore_this_repr = {
type_object_for,
MVM_gc_allocate_object,
NULL, /* initialize */
copy_to,
{
get_attribute,
bind_attribute,
hint_for,
is_attribute_initialized,
MVM_REPR_DEFAULT_ATTRIBUTE_AS_ATOMIC
}, /* attr_funcs */
MVM_REPR_DEFAULT_BOX_FUNCS,
MVM_REPR_DEFAULT_POS_FUNCS,
MVM_REPR_DEFAULT_ASS_FUNCS,
MVM_REPR_DEFAULT_ELEMS,
get_storage_spec,
NULL, /* change_type */
NULL, /* serialize */
NULL, /* deserialize */
NULL, /* serialize_repr_data */
NULL, /* deserialize_repr_data */
deserialize_stable_size,
gc_mark,
gc_free,
NULL, /* gc_cleanup */
NULL, /* gc_mark_repr_data */
NULL, /* gc_free_repr_data */
compose,
NULL, /* spesh */
"HashAttrStore", /* name */
MVM_REPR_ID_HashAttrStore,
NULL, /* unmanaged_size */
NULL, /* describe_refs */
};