dds_engine.c

filter: Statementstmt 96.8% 369 / 381 Branchbran 79.3% 192 / 242 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 90.0% 561 / 623 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« dds_format.c ↑ Summary dds_analyze.c »
1 #define NEED_mg_findext
2 #include "dds_engine.h"
3  
4 /* Facade accessors return borrowed values; newly created containers are
5 * owned by the facade hash that receives them. */
6 /* Fetch a borrowed facade value by a byte-string key, returning Perl's undef
7 * singleton when the hash or key is absent.
8 */
9 132630 SV *dds_hget(pTHX_ HV *hv, const char *key)
10 {
11 132630 ▶ SV **svp = hv ? hv_fetch(hv, key, (I32)strlen(key), 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **svp = hv ? hv_fetch(hv, key, (I32)strlen(key), 0) : NULL; 132630 0 132630
12  
13 132630 ▶ return svp ? *svp : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
return svp ? *svp : &PL_sv_undef; 126130 6500 132630
14 }
15  
16 /* Fetch a borrowed value using an SV key so UTF-8 key semantics are preserved;
17 * return undef when absent.
18 */
19 29520 SV *dds_hget_sv(pTHX_ HV *hv, SV *key)
20 {
21 29520 ▶ HE *entry = hv ? hv_fetch_ent(hv, key, 0, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
HE *entry = hv ? hv_fetch_ent(hv, key, 0, 0) : NULL; 29520 0 29520
22  
23 29520 ▶ return entry ? HeVAL(entry) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
return entry ? HeVAL(entry) : &PL_sv_undef; 1279 28241 29520
24 }
25  
26 /* Return a borrowed hash stored in the facade, optionally replacing a missing
27 * or unsuitable value with a new hash.
28 */
29 56862 HV *dds_hash(pTHX_ HV *hv, const char *key, bool create)
30 {
31 56862 SV *sv = dds_hget(aTHX_ hv, key);
32 HV *result;
33  
34 56862 ▶ if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV) 54934 1928 56862
if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV) 54934 0 54934
35 54934 return (HV *)SvRV(sv);
36 1928 ▶ if (!create)
Branch: 1 branch
BranchTrueFalseTotal
if (!create) 0 1928 1928
37 0 return NULL;
38  
39 1928 result = newHV();
40 1928 hv_store(hv, key, (I32)strlen(key), newRV_noinc((SV *)result), 0);
41 1928 return result;
42 }
43  
44 /* Return a borrowed array stored in the facade, optionally replacing a missing
45 * or unsuitable value with a new array.
46 */
47 1973 AV *dds_array(pTHX_ HV *hv, const char *key, bool create)
48 {
49 1973 SV *sv = dds_hget(aTHX_ hv, key);
50 AV *result;
51  
52 1973 ▶ if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV) 1126 847 1973
if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV) 1126 0 1126
53 1126 return (AV *)SvRV(sv);
54 847 ▶ if (!create)
Branch: 1 branch
BranchTrueFalseTotal
if (!create) 836 11 847
55 836 return NULL;
56  
57 11 result = newAV();
58 11 hv_store(hv, key, (I32)strlen(key), newRV_noinc((SV *)result), 0);
59 11 return result;
60 }
61  
62 /*
63 * Identity catalogue
64 *
65 * Address keys find stable metadata records. They are never converted back
66 * into graph SVs.
67 */
68  
69 /* Mix an opaque address into a table hash. The algorithm follows Sereal's
70 * ptable.h, derived from Perl's pointer table and Chocolateboy's autobox
71 * implementation, under Perl's license. */
72 31383 static UV dds_pointer_hash(UV address)
73 {
74 31383 UV x = address;
75  
76 31383 x ^= x >> 4;
77 31383 x *= (UV)2654435761UL;
78 31383 x ^= x >> 16;
79  
80 31383 return x;
81 }
82  
83 /* Look up an SV's opaque address in the identity table, optionally creating a
84 * stable catalogue node and growing the table.
85 */
86 31124 dds_node *dds_find(pTHX_ dds_ctx *ctx, SV *sv, bool create)
87 {
88 31124 dds_table *table = &ctx->table;
89 UV bucket;
90 31124 UV address = PTR2UV(sv);
91 dds_node *node;
92  
93 31124 ctx->lookups++;
94 31124 ▶ if (!table->buckets) {
Branch: 1 branch
BranchTrueFalseTotal
if (!table->buckets) { 1187 29937 31124
95 1187 ▶ if (!create)
Branch: 1 branch
BranchTrueFalseTotal
if (!create) 0 1187 1187
96 0 return NULL;
97 1187 table->mask = 31;
98 1187 ▶ Newxz(table->buckets, table->mask + 1, dds_node *);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(table->buckets, table->mask + 1, dds_node *); 0 1187 1187
99 }
100  
101 31124 bucket = dds_pointer_hash(address) & table->mask;
102 39912 ▶ for (node = table->buckets[bucket]; node; node = node->next)
Branch: 1 branch
BranchTrueFalseTotal
for (node = table->buckets[bucket]; node; node = node->next) 30824 9088 39912
103 30824 ▶ if (node->address == address)
Branch: 1 branch
BranchTrueFalseTotal
if (node->address == address) 22036 8788 30824
104 22036 return node;
105 9088 ▶ if (!create)
Branch: 1 branch
BranchTrueFalseTotal
if (!create) 3834 5254 9088
106 3834 return NULL;
107  
108 5254 Newxz(node, 1, dds_node);
109 5254 node->address = address;
110 5254 node->next = table->buckets[bucket];
111 5254 table->buckets[bucket] = node;
112 5254 table->count++;
113 5254 ctx->candidates++;
114  
115 /* Relink nodes during growth so borrowed catalogue pointers stay valid. */
116 5254 ▶ if (table->count > (table->mask + 1) * 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (table->count > (table->mask + 1) * 2) { 3 5251 5254
117 3 UV i, mask = table->mask * 2 + 1;
118 dds_node **buckets;
119  
120 3 ▶ Newxz(buckets, mask + 1, dds_node *);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(buckets, mask + 1, dds_node *); 0 3 3
121 131 ▶ for (i = 0; i <= table->mask; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= table->mask; i++) { 128 3 131
122 128 dds_node *entry = table->buckets[i];
123  
124 387 ▶ while (entry) {
Branch: 1 branch
BranchTrueFalseTotal
while (entry) { 259 128 387
125 259 dds_node *next = entry->next;
126  
127 259 bucket = dds_pointer_hash(entry->address) & mask;
128 259 entry->next = buckets[bucket];
129 259 buckets[bucket] = entry;
130 259 entry = next;
131 }
132 }
133  
134 3 Safefree(table->buckets);
135 3 table->buckets = buckets;
136 3 table->mask = mask;
137 }
138  
139 5254 return node;
140 }
141  
142 /* Return the borrowed back-reference storage for this Perl's SV layout, or
143 * NULL when none is present.
144 */
145 103185 static SV *dds_backrefs(pTHX_ SV *sv)
146 {
147 #if PERL_VERSION >= 22
148 103185 return sv_get_backrefs(sv);
149 #else
150 MAGIC *mg;
151 #if PERL_VERSION >= 10
152 if (SvTYPE(sv) == SVt_PVHV && SvOOK(sv))
153 return (SV *)HvAUX((HV *)sv)->xhv_backreferences;
154 #endif
155 mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_backref) : NULL;
156 return mg ? mg->mg_obj : NULL;
157 #endif
158 }
159  
160 /* Count weak RVs pointing at this SV, excluding other kinds of back references
161 * in the same storage.
162 */
163 14514 UV dds_weak_count(pTHX_ SV *sv)
164 {
165 14514 SV *refs = dds_backrefs(aTHX_ sv);
166 14514 UV count = 0;
167  
168 14514 ▶ if (!refs)
Branch: 1 branch
BranchTrueFalseTotal
if (!refs) 13939 575 14514
169 13939 return 0;
170  
171 575 ▶ if (SvTYPE(refs) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(refs) == SVt_PVAV) { 166 409 575
172 SSize_t i;
173  
174 524 ▶ for (i = 0; i <= av_len((AV *)refs); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= av_len((AV *)refs); i++) { 358 166 524
175 358 SV **entry = av_fetch((AV *)refs, i, 0);
176  
177 358 ▶ if (entry && SvROK(*entry) && SvWEAKREF(*entry) && SvRV(*entry) == sv)
Branch: 4 branches
BranchTrueFalseTotal
if (entry && SvROK(*entry) && SvWEAKREF(*entry) && SvRV(*entry) == sv) 358 0 358
if (entry && SvROK(*entry) && SvWEAKREF(*entry) && SvRV(*entry) == sv) 358 0 358
if (entry && SvROK(*entry) && SvWEAKREF(*entry) && SvRV(*entry) == sv) 358 0 358
if (entry && SvROK(*entry) && SvWEAKREF(*entry) && SvRV(*entry) == sv) 358 0 358
178 358 count++;
179 }
180 409 ▶ } else if (SvROK(refs) && SvWEAKREF(refs) && SvRV(refs) == sv)
Branch: 3 branches
BranchTrueFalseTotal
} else if (SvROK(refs) && SvWEAKREF(refs) && SvRV(refs) == sv) 408 1 409
} else if (SvROK(refs) && SvWEAKREF(refs) && SvRV(refs) == sv) 408 0 408
} else if (SvROK(refs) && SvWEAKREF(refs) && SvRV(refs) == sv) 408 0 408
181 408 count = 1;
182  
183 575 return count;
184 }
185  
186 /* Test whether an ordinary SV may need identity tracking because of its strong
187 * count or back references; exclude immortal constants.
188 */
189 116921 bool dds_candidate(pTHX_ SV *sv)
190 {
191 116921 ▶ if (!sv || sv == &PL_sv_undef || sv == &PL_sv_yes || sv == &PL_sv_no)
Branch: 4 branches
BranchTrueFalseTotal
if (!sv || sv == &PL_sv_undef || sv == &PL_sv_yes || sv == &PL_sv_no) 116921 0 116921
if (!sv || sv == &PL_sv_undef || sv == &PL_sv_yes || sv == &PL_sv_no) 116885 36 116921
if (!sv || sv == &PL_sv_undef || sv == &PL_sv_yes || sv == &PL_sv_no) 116885 0 116885
if (!sv || sv == &PL_sv_undef || sv == &PL_sv_yes || sv == &PL_sv_no) 0 116885 116885
192 36 return FALSE;
193  
194 /* A count of one rules out strong sharing, but not weak references. */
195 116885 ▶ return SvREFCNT(sv) > 1 || dds_backrefs(aTHX_ sv) != NULL;
Branch: 2 branches
BranchTrueFalseTotal
return SvREFCNT(sv) > 1 || dds_backrefs(aTHX_ sv) != NULL; 88671 28214 116885
return SvREFCNT(sv) > 1 || dds_backrefs(aTHX_ sv) != NULL; 165 88506 88671
196 }
197  
198 /* Return a mortal class-name string with the stash's UTF-8 flag, or undef for
199 * an unblessed value.
200 */
201 34757 SV *dds_class(pTHX_ SV *sv)
202 {
203 SV *name;
204  
205 34757 ▶ if (!SvOBJECT(sv) || !SvSTASH(sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOBJECT(sv) || !SvSTASH(sv)) 1687 33070 34757
if (!SvOBJECT(sv) || !SvSTASH(sv)) 0 1687 1687
206 33070 return &PL_sv_undef;
207  
208 1687 ▶ name = DDS_TEMP(newSVpv(HvNAME_get(SvSTASH(sv)), 0));
Branch: 6 branches
BranchTrueFalseTotal
name = DDS_TEMP(newSVpv(HvNAME_get(SvSTASH(sv)), 0)); 1687 0 1687
name = DDS_TEMP(newSVpv(HvNAME_get(SvSTASH(sv)), 0)); 1687 0 1687
name = DDS_TEMP(newSVpv(HvNAME_get(SvSTASH(sv)), 0)); 0 1687 1687
name = DDS_TEMP(newSVpv(HvNAME_get(SvSTASH(sv)), 0)); 0 0 0
name = DDS_TEMP(newSVpv(HvNAME_get(SvSTASH(sv)), 0)); 1687 0 1687
name = DDS_TEMP(newSVpv(HvNAME_get(SvSTASH(sv)), 0)); 0 1687 1687
209 1687 ▶ if (HvNAMEUTF8(SvSTASH(sv)))
Branch: 7 branches
BranchTrueFalseTotal
if (HvNAMEUTF8(SvSTASH(sv))) 1687 0 1687
if (HvNAMEUTF8(SvSTASH(sv))) 1687 0 1687
if (HvNAMEUTF8(SvSTASH(sv))) 0 1687 1687
if (HvNAMEUTF8(SvSTASH(sv))) 1687 0 1687
if (HvNAMEUTF8(SvSTASH(sv))) 0 1687 1687
if (HvNAMEUTF8(SvSTASH(sv))) 0 1687 1687
if (HvNAMEUTF8(SvSTASH(sv))) 32 1655 1687
210 32 SvUTF8_on(name);
211  
212 1687 return name;
213 }
214  
215 /*
216 * Context lifetime
217 *
218 * Emission cleanup keeps analysis reusable; catalogue cleanup releases
219 * analysis too. The facade magic destructor also releases roots and buffers.
220 */
221  
222 /* Release frames, pending output work and completion marks, then reset output
223 * state while keeping the analysis catalogue reusable.
224 */
225 5750 void dds_clear_emission(pTHX_ dds_ctx *ctx)
226 {
227 dds_mark *mark;
228 dds_fix *fix;
229 dds_postop *postop;
230 UV i;
231  
232 /* Drop outstanding work before its catalogue records and marks. */
233 5750 dds_clear_frames(aTHX_ ctx);
234 5750 ▶ while ((postop = ctx->postop_head)) {
Branch: 1 branch
BranchTrueFalseTotal
while ((postop = ctx->postop_head)) { 0 5750 5750
235 0 SV *invocant = postop->invocant;
236  
237 0 ctx->postop_head = postop->next;
238 0 ▶ if (!ctx->postop_head)
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx->postop_head) 0 0 0
239 0 ctx->postop_tail = NULL;
240 0 Safefree(postop);
241 0 SvREFCNT_dec(invocant);
242 }
243  
244 17250 ▶ for (i = 0; i < 2; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 2; i++) { 11500 5750 17250
245 11500 ▶ dds_fix **head = i ? &ctx->glob_head : &ctx->fix_head;
Branch: 1 branch
BranchTrueFalseTotal
dds_fix **head = i ? &ctx->glob_head : &ctx->fix_head; 5750 5750 11500
246  
247 11501 ▶ while ((fix = *head)) {
Branch: 1 branch
BranchTrueFalseTotal
while ((fix = *head)) { 1 11500 11501
248 1 *head = fix->next;
249 1 SvREFCNT_dec(fix->lhs);
250 1 SvREFCNT_dec(fix->text);
251 1 SvREFCNT_dec(fix->class_name);
252 1 SvREFCNT_dec(fix->args);
253 1 SvREFCNT_dec(fix->glob);
254 1 SvREFCNT_dec(fix->path);
255 1 Safefree(fix);
256 }
257 }
258  
259 5750 ctx->fix_tail = NULL;
260 5750 ctx->pending_aliases = 0;
261 7891 ▶ while ((mark = ctx->marks)) {
Branch: 1 branch
BranchTrueFalseTotal
while ((mark = ctx->marks)) { 2141 5750 7891
262 2141 ctx->marks = mark->next;
263 2141 Safefree(mark);
264 }
265  
266 /* The catalogue survives repeated Out calls; emission state does not. */
267 5750 ▶ if (ctx->table.buckets)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->table.buckets) 2282 3468 5750
268 75434 ▶ for (i = 0; i <= ctx->table.mask; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= ctx->table.mask; i++) { 73152 2282 75434
269 dds_node *node;
270  
271 80085 ▶ for (node = ctx->table.buckets[i]; node; node = node->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (node = ctx->table.buckets[i]; node; node = node->next) { 6933 73152 80085
272 6933 node->slot.done = node->ref.done = NULL;
273 6933 node->glob_dumped = FALSE;
274 6933 node->postop_queued = FALSE;
275 6933 node->class_restored = FALSE;
276 }
277 }
278  
279 5750 SvREFCNT_dec(ctx->out);
280 5750 SvREFCNT_dec(ctx->chunks);
281 5750 SvREFCNT_dec(ctx->printer);
282  
283 5750 ctx->out = ctx->printer = NULL;
284 5750 ctx->chunks = NULL;
285 5750 ctx->column = 0;
286 5750 ctx->symbol_required = FALSE;
287 5750 ctx->final_weakening = FALSE;
288 5750 }
289  
290 /* Release emission state and catalogue-owned values, empty analysis queues and
291 * caches, and reset traversal statistics.
292 */
293 4241 void dds_clear_catalogue(pTHX_ dds_ctx *ctx)
294 {
295 UV i;
296  
297 4241 dds_clear_emission(aTHX_ ctx);
298 4241 ▶ if (ctx->table.buckets)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->table.buckets) 1853 2388 4241
299 61277 ▶ for (i = 0; i <= ctx->table.mask; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= ctx->table.mask; i++) { 59424 1853 61277
300 59424 dds_node *node = ctx->table.buckets[i];
301  
302 64678 ▶ while (node) {
Branch: 1 branch
BranchTrueFalseTotal
while (node) { 5254 59424 64678
303 5254 dds_node *next = node->next;
304  
305 5254 SvREFCNT_dec(node->slot.name);
306 5254 SvREFCNT_dec(node->slot.original);
307 5254 SvREFCNT_dec(node->ref.name);
308 5254 SvREFCNT_dec(node->ref.original);
309 5254 SvREFCNT_dec(node->proxy);
310 5254 SvREFCNT_dec(node->thaw);
311 5254 SvREFCNT_dec(node->postop);
312 5254 SvREFCNT_dec(node->class_fields);
313 5254 SvREFCNT_dec(node->class_fields_name);
314 5254 SvREFCNT_dec(node->class_package);
315 5254 Safefree(node);
316 5254 node = next;
317 }
318 59424 ctx->table.buckets[i] = NULL;
319 }
320  
321 4241 ctx->table.count = 0;
322 4241 ctx->class_head = ctx->class_tail = NULL;
323 4241 ctx->class_serial = 0;
324  
325 15037 ▶ for (i = 0; i < ctx->queue_count; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ctx->queue_count; i++) 10796 4241 15037
326 10796 SvREFCNT_dec(ctx->queue[i].name);
327 4241 ctx->queue_head = ctx->queue_count = 0;
328 4241 ▶ if (ctx->queue_levels)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->queue_levels) 1300 2941 4241
329 1300 ▶ Zero(ctx->queue_levels, ctx->queue_level_capacity, dds_queue_level);
Branch: 1 branch
BranchTrueFalseTotal
Zero(ctx->queue_levels, ctx->queue_level_capacity, dds_queue_level); 0 1300 1300
330 4241 ctx->queue_depth = 0;
331  
332 4241 hv_clear(ctx->types);
333 4241 hv_clear(ctx->abbreviations);
334 4241 hv_clear(ctx->lexical_names);
335 4241 hv_clear(ctx->lexical_added);
336 4241 av_clear(ctx->keepalive);
337  
338 4241 ctx->callbacks = ctx->cataloged = FALSE;
339 4241 ctx->active = NULL;
340 4241 ctx->visits = ctx->lookups = ctx->candidates = ctx->repeats = 0;
341 4241 ctx->queue_peak = ctx->fixes = 0;
342 4241 }
343  
344 /* Destroy the native context when its facade hash is freed, releasing retained
345 * roots, work buffers and owned Perl containers.
346 */
347 1195 static int dds_magic_free(pTHX_ SV *sv, MAGIC *mg)
348 {
349 1195 dds_ctx *ctx = (dds_ctx *)mg->mg_ptr;
350 UV i;
351  
352 PERL_UNUSED_ARG(sv);
353 1195 ▶ if (!ctx)
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx) 0 1195 1195
354 0 return 0;
355  
356 1195 mg->mg_ptr = NULL;
357 1195 dds_clear_catalogue(aTHX_ ctx);
358 2438 ▶ for (i = 0; i < ctx->root_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ctx->root_count; i++) { 1243 1195 2438
359 1243 SvREFCNT_dec(ctx->roots[i].sv);
360 1243 SvREFCNT_dec(ctx->roots[i].name);
361 }
362 1195 Safefree(ctx->roots);
363 1195 Safefree(ctx->queue);
364 1195 Safefree(ctx->queue_levels);
365 1195 Safefree(ctx->table.buckets);
366 1195 SvREFCNT_dec(ctx->types);
367 1195 SvREFCNT_dec(ctx->abbreviations);
368 1195 SvREFCNT_dec(ctx->lexical_names);
369 1195 SvREFCNT_dec(ctx->lexical_added);
370 1195 SvREFCNT_dec(ctx->keepalive);
371 1195 SvREFCNT_dec(ctx->style_values);
372 1195 Safefree(ctx);
373 1195 return 0;
374 }
375  
376 static MGVTBL dds_magic_vtbl = {0, 0, 0, 0, dds_magic_free, 0, 0, 0};
377  
378 /* Validate the dumper facade and return its attached native context, creating
379 * and attaching one on first use.
380 */
381 6805 dds_ctx *dds_context(pTHX_ SV *self)
382 {
383 HV *hv;
384 MAGIC *mg;
385 dds_ctx *ctx;
386  
387 6805 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 6805 0 6805
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 0 6805 6805
388 0 croak("Data::Dump::Streamer requires an object");
389  
390 6805 hv = (HV *)SvRV(self);
391 6805 mg = mg_findext((SV *)hv, PERL_MAGIC_ext, &dds_magic_vtbl);
392 6805 ▶ if (mg)
Branch: 1 branch
BranchTrueFalseTotal
if (mg) 5610 1195 6805
393 5610 return (dds_ctx *)mg->mg_ptr;
394  
395 1195 Newxz(ctx, 1, dds_ctx);
396 1195 ctx->object = hv;
397 1195 ctx->types = newHV();
398 1195 ctx->abbreviations = newHV();
399 1195 ctx->lexical_names = newHV();
400 1195 ctx->lexical_added = newHV();
401 1195 ctx->keepalive = newAV();
402 1195 ctx->style_values = newAV();
403 1195 sv_magicext((SV *)hv, NULL, PERL_MAGIC_ext, &dds_magic_vtbl, (const char *)ctx, 0);
404  
405 1195 return ctx;
406 }
407  
408 /* Retain a new root set before publishing it and releasing old roots, so
409 * destructor reentry cannot observe dying input.
410 */
411 1514 void dds_capture(pTHX_ dds_ctx *ctx, I32 count, SV **args)
412 {
413 I32 i;
414 1514 dds_root *roots, *old_roots = ctx->roots;
415 1514 UV old_count = ctx->root_count;
416  
417 1514 ▶ Newxz(roots, count ? count : 1, dds_root);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(roots, count ? count : 1, dds_root); 1514 0 1514
418 3428 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 1914 1514 3428
419 1914 roots[i].sv = SvREFCNT_inc(args[i]);
420 1914 roots[i].count = SvREFCNT(args[i]) + dds_weak_count(aTHX_ args[i]);
421 1914 roots[i].readonly = cBOOL(SvREADONLY(args[i]));
422 }
423  
424 /* Publish retained input before releasing values whose destructors may
425 * recursively use the dumper. They must never see dying old roots. */
426 1514 ctx->roots = roots;
427 1514 ctx->root_count = ctx->root_capacity = count;
428 1514 dds_clear_catalogue(aTHX_ ctx);
429  
430 2229 ▶ for (i = 0; i < (I32)old_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < (I32)old_count; i++) { 715 1514 2229
431 715 SvREFCNT_dec(old_roots[i].sv);
432 715 SvREFCNT_dec(old_roots[i].name);
433 }
434 1514 Safefree(old_roots);
435 1514 }
436  
437 /* Snapshot facade formatting options into native fields and own copies of
438 * string values for the active operation.
439 */
440 3023 void dds_load_style(pTHX_ dds_ctx *ctx)
441 {
442 3023 HV *style = dds_hash(aTHX_ ctx->object, "style", FALSE);
443 3023 dds_style *s = &ctx->style;
444  
445 /* Snapshot strings so callbacks cannot invalidate the active style. */
446 3023 av_clear(ctx->style_values);
447  
448 #define STR(field, key) \
449 STMT_START \
450 { \
451 s->field = newSVsv(dds_hget(aTHX_ style, key)); \
452 av_push(ctx->style_values, s->field); \
453 } \
454 STMT_END
455 #define NUM(field, key) s->field = SvIV(dds_hget(aTHX_ style, key))
456 #define BOOL(field, key) s->field = cBOOL(SvTRUE(dds_hget(aTHX_ style, key)))
457 3023 STR(space, "optspace");
458 3023 STR(hashsep, "hashsep");
459 3023 STR(arysep, "arysep");
460 3023 STR(pairsep, "pairsep");
461 3023 STR(bless, "bless");
462 3023 STR(compressor, "compressor");
463 3023 STR(codestub, "codestub");
464 3023 STR(formatstub, "formatstub");
465  
466 3023 NUM(indent, "indent");
467 3023 NUM(indentcols, "indentcols");
468 3023 NUM(compress, "compress");
469  
470 3023 BOOL(indentkeys, "indentkeys");
471 3023 BOOL(declare, "declare");
472 3023 BOOL(verbose, "verbose");
473 3023 BOOL(dumpglob, "dumpglob");
474 3023 BOOL(deparse, "deparse");
475 3023 BOOL(deparseglob, "deparseglob");
476 3023 BOOL(rle, "rle");
477 3023 BOOL(readonly, "ro");
478 3023 BOOL(dualvars, "dualvars");
479 3023 BOOL(purity, "purity");
480 3023 BOOL(terse, "terse");
481 3023 BOOL(special, "special");
482 #undef STR
483 #undef NUM
484 #undef BOOL
485 3023 }
486  
487 /*
488 * Operations and Perl callbacks
489 *
490 * The ordinary graph walk borrows SVs. Entering Perl code enables keepalive
491 * protection for live traversal work and exception-safe operation cleanup.
492 */
493  
494 /* Called only when Perl code can run. Retain current work, never values
495 * recovered from catalogue addresses. Ordinary graph walks borrow SVs. */
496 1065 void dds_callback_guard(pTHX_ dds_ctx *ctx)
497 {
498 UV i;
499 dds_active *active;
500  
501 1065 ▶ if (!ctx->callbacks) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx->callbacks) { 267 798 1065
502 267 ctx->callbacks = TRUE;
503 743 ▶ for (i = 0; i < ctx->root_count; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ctx->root_count; i++) 476 267 743
504 476 av_push(ctx->keepalive, SvREFCNT_inc(ctx->roots[i].sv));
505 267 ▶ if (ctx->queue_head < ctx->queue_count)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->queue_head < ctx->queue_count) 77 190 267
506 480 ▶ for (i = 0; i < ctx->queue_count; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ctx->queue_count; i++) 403 77 480
507 403 ▶ if (ctx->queue[i].name)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->queue[i].name) 146 257 403
508 146 av_push(ctx->keepalive, SvREFCNT_inc(ctx->queue[i].sv));
509 }
510  
511 152197 ▶ for (active = ctx->active; active; active = active->parent) {
Branch: 1 branch
BranchTrueFalseTotal
for (active = ctx->active; active; active = active->parent) { 151132 1065 152197
512 151132 ▶ SV *referent = SvROK(active->sv) ? SvRV(active->sv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *referent = SvROK(active->sv) ? SvRV(active->sv) : NULL; 150749 383 151132
513  
514 /* Ancestor frames recur at every deeper callback. Keep their
515 * unchanged SVs once, but still retain replacements after mutation. */
516 151132 ▶ if (active->retained_sv != active->sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (active->retained_sv != active->sv) { 3132 148000 151132
517 3132 av_push(ctx->keepalive, SvREFCNT_inc(active->sv));
518 3132 active->retained_sv = active->sv;
519 }
520 151132 ▶ if (referent && active->retained_ref != referent) {
Branch: 2 branches
BranchTrueFalseTotal
if (referent && active->retained_ref != referent) { 150749 383 151132
if (referent && active->retained_ref != referent) { 2756 147993 150749
521 2756 av_push(ctx->keepalive, SvREFCNT_inc(referent));
522 2756 active->retained_ref = referent;
523 }
524 }
525 1065 }
526  
527 typedef struct {
528 dds_ctx *ctx;
529 bool committed;
530 } dds_operation;
531  
532 /* Clear the busy guard on scope exit and discard partial analysis when an
533 * operation unwinds without committing.
534 */
535 3015 static void dds_operation_cleanup(pTHX_ void *ptr)
536 {
537 3015 dds_operation *operation = (dds_operation *)ptr;
538 3015 dds_ctx *ctx = operation->ctx;
539  
540 3015 ctx->active = NULL;
541 3015 ctx->owner = NULL;
542 3015 ctx->busy = FALSE;
543 3015 ▶ if (!operation->committed) {
Branch: 1 branch
BranchTrueFalseTotal
if (!operation->committed) { 6 3009 3015
544 6 dds_clear_catalogue(aTHX_ ctx);
545 6 hv_stores(ctx->object, "cataloged", newSViv(0));
546 }
547  
548 3015 Safefree(operation);
549 3015 }
550  
551 /* Protect the dumper for an operation, reject concurrent use of its context
552 * and install exception-safe scope cleanup.
553 */
554 3015 void *dds_operation_begin(pTHX_ dds_ctx *ctx, SV *self)
555 {
556 dds_operation *operation;
557  
558 3015 ▶ if (ctx->busy)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->busy) 0 3015 3015
559 0 croak("DDS operation already active");
560  
561 3015 SAVEFREESV(SvREFCNT_inc(self));
562 3015 Newxz(operation, 1, dds_operation);
563 3015 operation->ctx = ctx;
564 3015 ctx->busy = TRUE;
565 3015 ctx->owner = self;
566 3015 SAVEDESTRUCTOR_X(dds_operation_cleanup, operation);
567  
568 3015 return operation;
569 }
570  
571 /* Mark an operation successful so scope cleanup preserves its completed
572 * catalogue.
573 */
574 3009 void dds_operation_commit(void *operation)
575 {
576 3009 ((dds_operation *)operation)->committed = TRUE;
577 3009 }
578  
579 /* Invoke Perl code or a method with protected arguments and active graph
580 * values; return a mortal scalar or array reference for list results.
581 */
582 1044 SV *dds_invoke(pTHX_ dds_ctx *ctx, SV *callable, bool method, I32 flags, I32 argc, SV **argv)
583 {
584 1044 dSP;
585 I32 i, count;
586 1044 SV *result = DDS_TEMP(newSV(0));
587 1044 AV *values = NULL;
588 1044 AV *arguments = (AV *)DDS_TEMP((SV *)newAV());
589 1044 ▶ SV *error = (flags & G_EVAL) ? DDS_TEMP(newSV(0)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *error = (flags & G_EVAL) ? DDS_TEMP(newSV(0)) : NULL; 66 978 1044
590  
591 /* argv may point into the Perl stack. Retain its SVs before EXTEND
592 * can relocate it; do not retain addresses of argument slots. */
593 4199 ▶ for (i = 0; i < argc; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < argc; i++) 3155 1044 4199
594 3155 av_push(arguments, SvREFCNT_inc(argv[i]));
595 1044 callable = DDS_TEMP(newSVsv(callable));
596 1044 ▶ if (ctx)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx) 1042 2 1044
597 1042 dds_callback_guard(aTHX_ ctx);
598  
599 1044 ENTER;
600 1044 SAVETMPS;
601 1044 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1044 1044
602 1044 ▶ EXTEND(SP, argc);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, argc); 1044 0 1044
EXTEND(SP, argc); 0 1044 1044
603 4199 ▶ for (i = 0; i < argc; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < argc; i++) 3155 1044 4199
604 3155 PUSHs(AvARRAY(arguments)[i]);
605 1044 PUTBACK;
606 1165 ▶ count = method ? call_method(DDS_PV(callable), flags) : call_sv(callable, flags);
Branch: 1 branch
BranchTrueFalseTotal
count = method ? call_method(DDS_PV(callable), flags) : call_sv(callable, flags); 121 923 1044
607 1038 SPAGAIN;
608  
609 /* Copy results out before leaving the callback's temporary scope. */
610 1038 ▶ if (error)
Branch: 1 branch
BranchTrueFalseTotal
if (error) 66 972 1038
611 66 ▶ sv_setsv(error, ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(error, ERRSV); 66 0 66
612 1038 ▶ if ((flags & G_WANT) == G_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & G_WANT) == G_ARRAY) { 833 205 1038
613 833 values = newAV();
614 833 sv_setsv(result, DDS_TEMP(newRV_noinc((SV *)values)));
615 21856 ▶ for (i = count; i > 0; i--)
Branch: 1 branch
BranchTrueFalseTotal
for (i = count; i > 0; i--) 21023 833 21856
616 21023 av_push(values, newSVsv(SP[1 - i]));
617 833 SP -= count;
618 } else {
619 205 ▶ SV *value = count ? POPs : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *value = count ? POPs : &PL_sv_undef; 140 65 205
620  
621 205 sv_setsv(result, value);
622 205 ▶ if (count > 1)
Branch: 1 branch
BranchTrueFalseTotal
if (count > 1) 0 205 205
623 0 SP -= count - 1;
624 }
625  
626 1038 PUTBACK;
627 1038 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 1003 35 1038
628 1038 LEAVE;
629  
630 /* G_EVAL's exception value must survive temporary cleanup as well. */
631 1038 ▶ if (error)
Branch: 1 branch
BranchTrueFalseTotal
if (error) 66 972 1038
632 66 ▶ sv_setsv(ERRSV, error);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, error); 66 0 66
633  
634 1038 return result;
635 }
636  
637 /* Invoke a named Perl function, or prepend an invocant and dispatch a named
638 * method, using the common callback guard.
639 */
640 275 SV *dds_call(pTHX_ dds_ctx *ctx, const char *sub, SV *invocant, I32 flags, I32 argc, SV **argv)
641 {
642 275 SV *callable = DDS_TEMP(newSVpv(sub, 0));
643  
644 275 ▶ if (invocant) {
Branch: 1 branch
BranchTrueFalseTotal
if (invocant) { 82 193 275
645 82 AV *args = (AV *)DDS_TEMP((SV *)newAV());
646 I32 i;
647  
648 82 av_push(args, SvREFCNT_inc(invocant));
649 327 ▶ for (i = 0; i < argc; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < argc; i++) 245 82 327
650 245 av_push(args, SvREFCNT_inc(argv[i]));
651 82 return dds_invoke(aTHX_ ctx, callable, TRUE, flags, argc + 1, AvARRAY(args));
652 }
653  
654 193 return dds_invoke(aTHX_ ctx, callable, FALSE, flags, argc, argv);
655 }
656  
657 /* Select and cache a class freezer, returning its proxy/thaw/postdump tuple as
658 * a list reference or undef when no hook applies.
659 */
660 340 SV *dds_freeze(pTHX_ dds_ctx *ctx, SV *rv)
661 {
662 340 SV *target = SvRV(rv), *class_name = dds_class(aTHX_ target);
663 340 HV *style = dds_hash(aTHX_ ctx->object, "style", FALSE);
664 340 HV *classes = dds_hash(aTHX_ style, "freeze_class", FALSE);
665 340 HV *skip = dds_hash(aTHX_ ctx->object, "cache_skip_freeze", TRUE);
666 340 HV *cache = dds_hash(aTHX_ ctx->object, "cache_freeze_class", TRUE);
667 340 SV *cached = dds_hget_sv(aTHX_ cache, class_name);
668 SV *choices[3];
669 UV i;
670  
671 340 ▶ if (SvTRUE(dds_hget_sv(aTHX_ skip, class_name)))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(dds_hget_sv(aTHX_ skip, class_name))) 23 317 340
672 23 return &PL_sv_undef;
673  
674 317 ▶ if (SvOK(cached)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(cached)) { 21 296 317
675 21 SV *values = dds_invoke(aTHX_ ctx, cached, !SvROK(cached), G_ARRAY, 1, &rv);
676 20 AV *list = (AV *)SvRV(values);
677 20 SV **proxy = av_fetch(list, 0, 0);
678  
679 /* Only the first undef disables a class hook. Once selected, an
680 * empty or undef result means ignore this object, like false. */
681 20 ▶ if (!proxy || !SvOK(*proxy))
Branch: 2 branches
BranchTrueFalseTotal
if (!proxy || !SvOK(*proxy)) 19 1 20
if (!proxy || !SvOK(*proxy)) 1 18 19
682 2 av_store(list, 0, newSViv(0));
683 20 return values;
684 }
685  
686 /* Try the class policy, configured hook, then the conventional method. */
687 296 choices[0] = DDS_TEMP(newSVsv(dds_hget_sv(aTHX_ classes, class_name)));
688 296 choices[1] = DDS_TEMP(newSVsv(dds_hget(aTHX_ style, "freezer")));
689 296 choices[2] = DDS_TEMP(newSVpvs("DDS_freeze"));
690  
691 1018 ▶ for (i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) { 788 230 1018
692 788 SV *action = choices[i], *values;
693 SV **proxy;
694  
695 788 ▶ if (!SvTRUE(action)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvTRUE(action)) { 262 526 788
696 262 ▶ if (SvOK(action))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(action)) 1 261 262
697 1 break;
698 else
699 261 continue;
700 }
701 1017 ▶ if (!SvROK(action) && !gv_fetchmethod_autoload(SvSTASH(target), DDS_PV(action), 0))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(action) && !gv_fetchmethod_autoload(SvSTASH(target), DDS_PV(action), 0)) 491 35 526
if (!SvROK(action) && !gv_fetchmethod_autoload(SvSTASH(target), DDS_PV(action), 0)) 460 31 491
702 460 continue;
703  
704 66 values = dds_invoke(aTHX_ ctx, action, !SvROK(action), G_ARRAY | G_EVAL, 1, &rv);
705 66 ▶ if (SvROK(ERRSV) || SvTRUE(ERRSV)) {
Branch: 4 branches
BranchTrueFalseTotal
if (SvROK(ERRSV) || SvTRUE(ERRSV)) { 66 0 66
if (SvROK(ERRSV) || SvTRUE(ERRSV)) { 65 1 66
if (SvROK(ERRSV) || SvTRUE(ERRSV)) { 65 0 65
if (SvROK(ERRSV) || SvTRUE(ERRSV)) { 0 65 65
706 1 ▶ sv_setpvs(ERRSV, "");
Branch: 1 branch
BranchTrueFalseTotal
sv_setpvs(ERRSV, ""); 1 0 1
707 1 continue;
708 }
709  
710 65 proxy = av_fetch((AV *)SvRV(values), 0, 0);
711 65 ▶ if (proxy && SvOK(*proxy))
Branch: 2 branches
BranchTrueFalseTotal
if (proxy && SvOK(*proxy)) 62 3 65
if (proxy && SvOK(*proxy)) 59 3 62
712 59 hv_store_ent(cache, class_name, newSVsv(action), 0);
713 else
714 6 hv_store_ent(skip, class_name, newSViv(1), 0);
715 65 return values;
716 }
717  
718 231 hv_store_ent(skip, class_name, newSViv(1), 0);
719 231 return &PL_sv_undef;
720 }
« dds_format.c ↑ Summary dds_analyze.c »