/
pmc_freeze.c
282 lines (198 loc) · 6.19 KB
/
pmc_freeze.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
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
281
282
/*
Copyright (C) 2001-2010, Parrot Foundation.
$Id$
=head1 NAME
src/pmc_freeze.c - Freeze and thaw functionality
=head1 DESCRIPTION
Thawing PMCs uses a list with (maximum) size of the amount of PMCs to
keep track of retrieved PMCs.
The individual information of PMCs is frozen/thawed by their vtables.
To avoid recursion, the whole functionality is driven by
C<< pmc->vtable->visit >>, which is called for the first PMC initially.
Container PMCs call a "todo-callback" for all contained PMCs. The
individual action vtable (freeze/thaw) is then called for all todo-PMCs.
=cut
*/
#include "parrot/parrot.h"
#include "pmc/pmc_callcontext.h"
#include "pmc_freeze.str"
/* when thawing a string longer then this size, we first do a GC run and then
* block GC - the system can't give us more headers */
#define THAW_BLOCK_GC_SIZE 100000
/* HEADERIZER HFILE: include/parrot/pmc_freeze.h */
/*
=head2 Public Interface
=over 4
=item C<STRING* Parrot_freeze(PARROT_INTERP, PMC *pmc)>
Freeze using either method.
=cut
*/
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
STRING*
Parrot_freeze(PARROT_INTERP, ARGIN(PMC *pmc))
{
ASSERT_ARGS(Parrot_freeze)
PMC * const image = Parrot_pmc_new(interp, enum_class_ImageIO);
VTABLE_set_pmc(interp, image, pmc);
return VTABLE_get_string(interp, image);
}
/*
=item C<UINTVAL Parrot_freeze_size(PARROT_INTERP, PMC *pmc)>
Get the size of an image to be frozen without allocating a large buffer.
Used in C<Packfile_Constant_pack_size>.
=cut
*/
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
UINTVAL
Parrot_freeze_size(PARROT_INTERP, ARGIN(PMC *pmc))
{
ASSERT_ARGS(Parrot_freeze_size)
UINTVAL int_result;
PMC *pmc_result;
PMC * const visitor = Parrot_pmc_new(interp, enum_class_ImageIOSize);
VTABLE_set_pmc(interp, visitor, pmc);
pmc_result = VTABLE_get_pmc(interp, visitor);
int_result = VTABLE_get_integer(interp, pmc_result);
return int_result;
}
/*
=item C<PMC* Parrot_thaw(PARROT_INTERP, STRING *image)>
Thaws a PMC. Called from the C<thaw> opcode.
For now it seems cheaper to use a list for remembering contained
aggregates. We could of course decide dynamically, which strategy to
use, e.g.: given a big image, the first thawed item is a small
aggregate. This implies, it probably contains (or some big strings) more
nested containers, for which another approach could be a win.
=cut
*/
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
PMC*
Parrot_thaw(PARROT_INTERP, ARGIN(STRING *image))
{
ASSERT_ARGS(Parrot_thaw)
PMC * const info = Parrot_pmc_new(interp, enum_class_ImageIO);
int gc_block = 0;
PMC *result;
/*
* if we are thawing a lot of PMCs, it's cheaper to do
* a GC run first and then block GC - the limit should be
* chosen so that no more then one GC run would be triggered
*
* XXX
*
* md5_3.pir shows a segfault during thawing the config hash
* info->thaw_ptr becomes invalid - seems that the hash got
* collected under us.
*/
if (1 || (Parrot_str_byte_length(interp, image) > THAW_BLOCK_GC_SIZE)) {
Parrot_block_GC_mark(interp);
Parrot_block_GC_sweep(interp);
gc_block = 1;
}
VTABLE_set_string_native(interp, info, image);
result = VTABLE_get_pmc(interp, info);
if (gc_block) {
Parrot_unblock_GC_mark(interp);
Parrot_unblock_GC_sweep(interp);
}
return result;
}
/*
=item C<PMC* Parrot_thaw_constants(PARROT_INTERP, STRING *image)>
Thaws constants, used by PackFile for unpacking PMC constants.
This is a lie. It does nothing different from Parrot_thaw at the moment.
=cut
*/
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
PMC*
Parrot_thaw_constants(PARROT_INTERP, ARGIN(STRING *image))
{
ASSERT_ARGS(Parrot_thaw_constants)
return Parrot_thaw(interp, image);
}
/*
=item C<PMC* Parrot_clone(PARROT_INTERP, PMC *pmc)>
There are for sure shortcuts to clone faster, e.g. always thaw the image
immediately or use a special callback. For now we just thaw a frozen PMC.
=cut
*/
PARROT_EXPORT
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
PMC*
Parrot_clone(PARROT_INTERP, ARGIN(PMC *pmc))
{
ASSERT_ARGS(Parrot_clone)
return VTABLE_clone(interp, pmc);
}
/*
=item C<void Parrot_visit_loop_visit(PARROT_INTERP, PMC *info)>
Iterate a visitor PMC visiting each encountered target PMC.
=cut
*/
void
Parrot_visit_loop_visit(PARROT_INTERP, ARGIN(PMC *info))
{
ASSERT_ARGS(Parrot_visit_loop_visit)
INTVAL i;
PMC * const todo = VTABLE_get_iter(interp, info);
/* can't cache upper limit, visit may append items */
for (i = 0; i < VTABLE_elements(interp, todo); i++) {
PMC * const current = VTABLE_get_pmc_keyed_int(interp, todo, i);
if (!current)
Parrot_ex_throw_from_c_args(interp, NULL, 1,
"NULL current PMC in visit_loop_todo_list");
PARROT_ASSERT(current->vtable);
VTABLE_visit(interp, current, info);
VISIT_PMC(interp, info, PMC_metadata(current));
}
}
/*
=item C<void Parrot_visit_loop_thawfinish(PARROT_INTERP, PMC *info)>
Iterate a visitor PMC thawfinishing each encountered target PMC.
=cut
*/
void
Parrot_visit_loop_thawfinish(PARROT_INTERP, ARGIN(PMC *info))
{
ASSERT_ARGS(Parrot_visit_loop_thawfinish)
/* call thawfinish for each processed PMC */
/*
* Thaw in reverse order. We have to fully thaw younger PMCs
* before use them in older.
*
* XXX There are no younger or older pmcs in a directed graph
* that allows cycles. Any code that requires a specific
* order here is likely broken.
*/
PMC * const todo = VTABLE_get_iter(interp, info);
const INTVAL n = VTABLE_elements(interp, todo);
int i;
for (i = n-1; i >= 0; --i) {
PMC *current = VTABLE_get_pmc_keyed_int(interp, todo, i);
if (!PMC_IS_NULL(current))
VTABLE_thawfinish(interp, current, info);
}
}
/*
=back
=head1 TODO
The seen-hash version for freezing might go away sometime.
=head1 SEE ALSO
Lot of discussion on p6i and F<docs/dev/pmc_freeze.pod>.
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4:
*/