-
Notifications
You must be signed in to change notification settings - Fork 138
/
addrregistry.pmc
280 lines (182 loc) · 6.06 KB
/
addrregistry.pmc
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
Copyright (C) 2005-2012, Parrot Foundation.
=head1 NAME
src/pmc/addrregistry.pmc - A GC Registry PMC
=head1 DESCRIPTION
The AddrRegistry class provides the equivalence of reference counts
mainly for extenders and embedders of Parrot. The hash keys are the
addresses of the key PMC, values are reference counts, i.e. the
difference of (set_pmc_keyed - delete_pmc_keyed). If the reference
goes to zero, the entry is deleted physically.
Please note that you have to anchor an instance of AddrRegistry yourself with
C<Parrot_register_pmc> if it isn't visible to Parrot.
=head2 Functions
=over 4
=cut
*/
#include "parrot/hash.h"
#include "pmc/pmc_hash.h"
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* HEADERIZER END: static */
pmclass AddrRegistry provides hash auto_attrs {
ATTR Hash *pmc_registry;
ATTR Hash *str_registry;
/*
=item C<void init()>
Initializes the instance.
=cut
*/
VTABLE void init() {
Hash * const pmc_registry = Parrot_hash_create(INTERP,
enum_type_int,
Hash_key_type_PMC_ptr);
Hash * const str_registry = Parrot_hash_create(INTERP,
enum_type_int,
Hash_key_type_STRING);
SET_ATTR_pmc_registry(INTERP, SELF, pmc_registry);
SET_ATTR_str_registry(INTERP, SELF, str_registry);
PObj_custom_mark_destroy_SETALL(SELF);
}
/*
=item C<void mark()>
Mark any PMCs and STRINGs in this registry.
=cut
*/
VTABLE void mark() {
Hash *str_registry, *pmc_registry;
GET_ATTR_str_registry(INTERP, SELF, str_registry);
GET_ATTR_pmc_registry(INTERP, SELF, pmc_registry);
Parrot_hash_mark(interp, pmc_registry);
Parrot_hash_mark(interp, str_registry);
}
/*
=item C<void destroy()>
Destroy this PMC.
=cut
*/
VTABLE void destroy() {
Hash *str_registry, *pmc_registry;
GET_ATTR_str_registry(INTERP, SELF, str_registry);
GET_ATTR_pmc_registry(INTERP, SELF, pmc_registry);
Parrot_hash_destroy(interp, pmc_registry);
Parrot_hash_destroy(interp, str_registry);
SET_ATTR_str_registry(INTERP, SELF, NULL);
SET_ATTR_pmc_registry(INTERP, SELF, NULL);
}
/*
=item C<INTVAL get_integer_keyed(PMC *key)>
Returns the reference count for C<key> or 0 if the key doesn't exist.
=item C<INTVAL elements()>
Returns the number of elements in the hash.
=item C<INTVAL get_bool()>
Returns true if the hash size is not zero.
=cut
*/
VTABLE INTVAL get_integer_keyed(PMC *key) {
Hash *hash;
const void *value;
GET_ATTR_pmc_registry(INTERP, SELF, hash);
value = Parrot_hash_get(INTERP, hash, key);
if (value)
return (INTVAL)value;
return 0;
}
VTABLE INTVAL elements() {
const Hash *pmc_registry, *str_registry;
GET_ATTR_pmc_registry(INTERP, SELF, pmc_registry);
GET_ATTR_str_registry(INTERP, SELF, str_registry);
return Parrot_hash_size(INTERP, pmc_registry) +
Parrot_hash_size(INTERP, str_registry);
}
VTABLE INTVAL get_bool() {
const Hash *pmc_registry, *str_registry;
GET_ATTR_pmc_registry(INTERP, SELF, pmc_registry);
GET_ATTR_str_registry(INTERP, SELF, str_registry);
return (Parrot_hash_size(INTERP, pmc_registry) +
Parrot_hash_size(INTERP, str_registry)) != 0;
}
/*
=item C<void set_pmc_keyed(PMC *key, PMC *value)>
Increment the reference count of C<key>. If the entry doesn't exist
create it. The C<value> is always ignored.
=item C<void set_integer_keyed(PMC *key, INTVAL value)>
Set the given value.
=item C<void delete_keyed(PMC *key)>
=item C<void delete_keyed_str(STRING *key)>
Decrement the reference count of C<key>. If the reference count
reaches 0, delete the entry.
=cut
*/
VTABLE void set_pmc_keyed(PMC *key, PMC *value) {
UNUSED(value)
const void *oldval;
ptrcast_t newval = 1;
Hash *hash;
GET_ATTR_pmc_registry(INTERP, SELF, hash);
oldval = Parrot_hash_get(INTERP, hash, key);
if (oldval)
newval += (ptrcast_t)oldval;
Parrot_hash_put(INTERP, hash, key, (void *)newval);
}
VTABLE void set_pmc_keyed_str(STRING *key, PMC *value) {
UNUSED(value)
const void *oldval;
ptrcast_t newval = 1;
Hash *hash;
GET_ATTR_str_registry(INTERP, SELF, hash);
oldval = Parrot_hash_get(INTERP, hash, key);
if (oldval)
newval += (ptrcast_t)oldval;
Parrot_hash_put(INTERP, hash, key, (void *)newval);
}
VTABLE void set_integer_keyed(PMC *key, INTVAL value) {
Hash *hash;
GET_ATTR_pmc_registry(INTERP, SELF, hash);
Parrot_hash_put(INTERP, hash, key, (void *)value);
}
VTABLE void delete_keyed(PMC *key) {
Hash *hash;
void *value;
GET_ATTR_pmc_registry(INTERP, SELF, hash);
value = Parrot_hash_get(INTERP, hash, key);
/* these casts look bad, but they avoid type punning warnings with -O */
if (value) {
ptrcast_t val = (ptrcast_t)value;
if (val == 1)
Parrot_hash_delete(INTERP, hash, key);
else {
value = (void *)(--val);
Parrot_hash_put(INTERP, hash, key, value);
}
}
}
VTABLE void delete_keyed_str(STRING *key) {
Hash *hash;
void *value;
GET_ATTR_str_registry(INTERP, SELF, hash);
value = Parrot_hash_get(INTERP, hash, key);
/* these casts look bad, but they avoid type punning warnings with -O */
if (value) {
ptrcast_t val = (ptrcast_t)value;
if (val == 1)
Parrot_hash_delete(INTERP, hash, key);
else {
value = (void *)(--val);
Parrot_hash_put(INTERP, hash, key, value);
}
}
}
}
/*
=back
=head1 SEE ALSO
F<src/pmc.c:gc_register_pmc()>
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/