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 132959 SV *dds_hget(pTHX_ HV *hv, const char *key)
10 {
11 132959 ▶ 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; 132959 0 132959
12  
13 132959 ▶ return svp ? *svp : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
return svp ? *svp : &PL_sv_undef; 126432 6527 132959
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 29598 SV *dds_hget_sv(pTHX_ HV *hv, SV *key)
20 {
21 29598 ▶ 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; 29598 0 29598
22  
23 29598 ▶ return entry ? HeVAL(entry) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
return entry ? HeVAL(entry) : &PL_sv_undef; 1289 28309 29598
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 56984 HV *dds_hash(pTHX_ HV *hv, const char *key, bool create)
30 {
31 56984 SV *sv = dds_hget(aTHX_ hv, key);
32 HV *result;
33  
34 56984 ▶ if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV) 55045 1939 56984
if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV) 55045 0 55045
35 55045 return (HV *)SvRV(sv);
36 1939 ▶ if (!create)
Branch: 1 branch
BranchTrueFalseTotal
if (!create) 0 1939 1939
37 0 return NULL;
38  
39 1939 result = newHV();
40 1939 hv_store(hv, key, (I32)strlen(key), newRV_noinc((SV *)result), 0);
41 1939 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 1977 AV *dds_array(pTHX_ HV *hv, const char *key, bool create)
48 {
49 1977 SV *sv = dds_hget(aTHX_ hv, key);
50 AV *result;
51  
52 1977 ▶ if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV) 1130 847 1977
if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV) 1130 0 1130
53 1130 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 31444 static UV dds_pointer_hash(UV address)
73 {
74 31444 UV x = address;
75  
76 31444 x ^= x >> 4;
77 31444 x *= (UV)2654435761UL;
78 31444 x ^= x >> 16;
79  
80 31444 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 31185 dds_node *dds_find(pTHX_ dds_ctx *ctx, SV *sv, bool create)
87 {
88 31185 dds_table *table = &ctx->table;
89 UV bucket;
90 31185 UV address = PTR2UV(sv);
91 dds_node *node;
92  
93 31185 ctx->lookups++;
94 31185 ▶ if (!table->buckets) {
Branch: 1 branch
BranchTrueFalseTotal
if (!table->buckets) { 1191 29994 31185
95 1191 ▶ if (!create)
Branch: 1 branch
BranchTrueFalseTotal
if (!create) 0 1191 1191
96 0 return NULL;
97 1191 table->mask = 31;
98 1191 ▶ Newxz(table->buckets, table->mask + 1, dds_node *);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(table->buckets, table->mask + 1, dds_node *); 0 1191 1191
99 }
100  
101 31185 bucket = dds_pointer_hash(address) & table->mask;
102 41094 ▶ for (node = table->buckets[bucket]; node; node = node->next)
Branch: 1 branch
BranchTrueFalseTotal
for (node = table->buckets[bucket]; node; node = node->next) 31971 9123 41094
103 31971 ▶ if (node->address == address)
Branch: 1 branch
BranchTrueFalseTotal
if (node->address == address) 22062 9909 31971
104 22062 return node;
105 9123 ▶ if (!create)
Branch: 1 branch
BranchTrueFalseTotal
if (!create) 3853 5270 9123
106 3853 return NULL;
107  
108 5270 Newxz(node, 1, dds_node);
109 5270 node->address = address;
110 5270 node->next = table->buckets[bucket];
111 5270 table->buckets[bucket] = node;
112 5270 table->count++;
113 5270 ctx->candidates++;
114  
115 /* Relink nodes during growth so borrowed catalogue pointers stay valid. */
116 5270 ▶ if (table->count > (table->mask + 1) * 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (table->count > (table->mask + 1) * 2) { 3 5267 5270
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 5270 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 103233 static SV *dds_backrefs(pTHX_ SV *sv)
146 {
147 #if PERL_VERSION >= 22
148 103233 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 14533 UV dds_weak_count(pTHX_ SV *sv)
164 {
165 14533 SV *refs = dds_backrefs(aTHX_ sv);
166 14533 UV count = 0;
167  
168 14533 ▶ if (!refs)
Branch: 1 branch
BranchTrueFalseTotal
if (!refs) 13958 575 14533
169 13958 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 116994 bool dds_candidate(pTHX_ SV *sv)
190 {
191 116994 ▶ 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) 116994 0 116994
if (!sv || sv == &PL_sv_undef || sv == &PL_sv_yes || sv == &PL_sv_no) 116958 36 116994
if (!sv || sv == &PL_sv_undef || sv == &PL_sv_yes || sv == &PL_sv_no) 116958 0 116958
if (!sv || sv == &PL_sv_undef || sv == &PL_sv_yes || sv == &PL_sv_no) 0 116958 116958
192 36 return FALSE;
193  
194 /* A count of one rules out strong sharing, but not weak references. */
195 116958 ▶ return SvREFCNT(sv) > 1 || dds_backrefs(aTHX_ sv) != NULL;
Branch: 2 branches
BranchTrueFalseTotal
return SvREFCNT(sv) > 1 || dds_backrefs(aTHX_ sv) != NULL; 88700 28258 116958
return SvREFCNT(sv) > 1 || dds_backrefs(aTHX_ sv) != NULL; 165 88535 88700
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 34807 SV *dds_class(pTHX_ SV *sv)
202 {
203 SV *name;
204  
205 34807 ▶ if (!SvOBJECT(sv) || !SvSTASH(sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOBJECT(sv) || !SvSTASH(sv)) 1719 33088 34807
if (!SvOBJECT(sv) || !SvSTASH(sv)) 0 1719 1719
206 33088 return &PL_sv_undef;
207  
208 1719 ▶ name = DDS_TEMP(newSVpv(HvNAME_get(SvSTASH(sv)), 0));
Branch: 6 branches
BranchTrueFalseTotal
name = DDS_TEMP(newSVpv(HvNAME_get(SvSTASH(sv)), 0)); 1719 0 1719
name = DDS_TEMP(newSVpv(HvNAME_get(SvSTASH(sv)), 0)); 1719 0 1719
name = DDS_TEMP(newSVpv(HvNAME_get(SvSTASH(sv)), 0)); 0 1719 1719
name = DDS_TEMP(newSVpv(HvNAME_get(SvSTASH(sv)), 0)); 0 0 0
name = DDS_TEMP(newSVpv(HvNAME_get(SvSTASH(sv)), 0)); 1719 0 1719
name = DDS_TEMP(newSVpv(HvNAME_get(SvSTASH(sv)), 0)); 0 1719 1719
209 1719 ▶ if (HvNAMEUTF8(SvSTASH(sv)))
Branch: 7 branches
BranchTrueFalseTotal
if (HvNAMEUTF8(SvSTASH(sv))) 1719 0 1719
if (HvNAMEUTF8(SvSTASH(sv))) 1719 0 1719
if (HvNAMEUTF8(SvSTASH(sv))) 0 1719 1719
if (HvNAMEUTF8(SvSTASH(sv))) 1719 0 1719
if (HvNAMEUTF8(SvSTASH(sv))) 0 1719 1719
if (HvNAMEUTF8(SvSTASH(sv))) 0 1719 1719
if (HvNAMEUTF8(SvSTASH(sv))) 32 1687 1719
210 32 SvUTF8_on(name);
211  
212 1719 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 5766 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 5766 dds_clear_frames(aTHX_ ctx);
234 5766 ▶ while ((postop = ctx->postop_head)) {
Branch: 1 branch
BranchTrueFalseTotal
while ((postop = ctx->postop_head)) { 0 5766 5766
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 17298 ▶ for (i = 0; i < 2; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 2; i++) { 11532 5766 17298
245 11532 ▶ dds_fix **head = i ? &ctx->glob_head : &ctx->fix_head;
Branch: 1 branch
BranchTrueFalseTotal
dds_fix **head = i ? &ctx->glob_head : &ctx->fix_head; 5766 5766 11532
246  
247 11533 ▶ while ((fix = *head)) {
Branch: 1 branch
BranchTrueFalseTotal
while ((fix = *head)) { 1 11532 11533
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 5766 ctx->fix_tail = NULL;
260 5766 ctx->pending_aliases = 0;
261 7911 ▶ while ((mark = ctx->marks)) {
Branch: 1 branch
BranchTrueFalseTotal
while ((mark = ctx->marks)) { 2145 5766 7911
262 2145 ctx->marks = mark->next;
263 2145 Safefree(mark);
264 }
265  
266 /* The catalogue survives repeated Out calls; emission state does not. */
267 5766 ▶ if (ctx->table.buckets)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->table.buckets) 2290 3476 5766
268 75698 ▶ for (i = 0; i <= ctx->table.mask; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= ctx->table.mask; i++) { 73408 2290 75698
269 dds_node *node;
270  
271 80373 ▶ for (node = ctx->table.buckets[i]; node; node = node->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (node = ctx->table.buckets[i]; node; node = node->next) { 6965 73408 80373
272 6965 node->slot.done = node->ref.done = NULL;
273 6965 node->glob_dumped = FALSE;
274 6965 node->postop_queued = FALSE;
275 6965 node->class_restored = FALSE;
276 }
277 }
278  
279 5766 SvREFCNT_dec(ctx->out);
280 5766 SvREFCNT_dec(ctx->chunks);
281 5766 SvREFCNT_dec(ctx->printer);
282  
283 5766 ctx->out = ctx->printer = NULL;
284 5766 ctx->chunks = NULL;
285 5766 ctx->column = 0;
286 5766 ctx->symbol_required = FALSE;
287 5766 ctx->final_weakening = FALSE;
288 5766 }
289  
290 /* Release emission state and catalogue-owned values, empty analysis queues and
291 * caches, and reset traversal statistics.
292 */
293 4253 void dds_clear_catalogue(pTHX_ dds_ctx *ctx)
294 {
295 UV i;
296  
297 4253 dds_clear_emission(aTHX_ ctx);
298 4253 ▶ if (ctx->table.buckets)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->table.buckets) 1857 2396 4253
299 61409 ▶ for (i = 0; i <= ctx->table.mask; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= ctx->table.mask; i++) { 59552 1857 61409
300 59552 dds_node *node = ctx->table.buckets[i];
301  
302 64822 ▶ while (node) {
Branch: 1 branch
BranchTrueFalseTotal
while (node) { 5270 59552 64822
303 5270 dds_node *next = node->next;
304  
305 5270 SvREFCNT_dec(node->slot.name);
306 5270 SvREFCNT_dec(node->slot.original);
307 5270 SvREFCNT_dec(node->ref.name);
308 5270 SvREFCNT_dec(node->ref.original);
309 5270 SvREFCNT_dec(node->proxy);
310 5270 SvREFCNT_dec(node->thaw);
311 5270 SvREFCNT_dec(node->postop);
312 5270 SvREFCNT_dec(node->class_fields);
313 5270 SvREFCNT_dec(node->class_fields_name);
314 5270 SvREFCNT_dec(node->class_package);
315 5270 Safefree(node);
316 5270 node = next;
317 }
318 59552 ctx->table.buckets[i] = NULL;
319 }
320  
321 4253 ctx->table.count = 0;
322 4253 ctx->class_head = ctx->class_tail = NULL;
323 4253 ctx->class_serial = 0;
324  
325 15059 ▶ for (i = 0; i < ctx->queue_count; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ctx->queue_count; i++) 10806 4253 15059
326 10806 SvREFCNT_dec(ctx->queue[i].name);
327 4253 ctx->queue_head = ctx->queue_count = 0;
328 4253 ▶ if (ctx->queue_levels)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->queue_levels) 1304 2949 4253
329 1304 ▶ 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 1304 1304
330 4253 ctx->queue_depth = 0;
331  
332 4253 hv_clear(ctx->types);
333 4253 hv_clear(ctx->abbreviations);
334 4253 hv_clear(ctx->lexical_names);
335 4253 hv_clear(ctx->lexical_added);
336 4253 av_clear(ctx->keepalive);
337  
338 4253 ctx->callbacks = ctx->cataloged = FALSE;
339 4253 ctx->active = NULL;
340 4253 ctx->visits = ctx->lookups = ctx->candidates = ctx->repeats = 0;
341 4253 ctx->queue_peak = ctx->fixes = 0;
342 4253 }
343  
344 /* Destroy the native context when its facade hash is freed, releasing retained
345 * roots, work buffers and owned Perl containers.
346 */
347 1199 static int dds_magic_free(pTHX_ SV *sv, MAGIC *mg)
348 {
349 1199 dds_ctx *ctx = (dds_ctx *)mg->mg_ptr;
350 UV i;
351  
352 PERL_UNUSED_ARG(sv);
353 1199 ▶ if (!ctx)
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx) 0 1199 1199
354 0 return 0;
355  
356 1199 mg->mg_ptr = NULL;
357 1199 dds_clear_catalogue(aTHX_ ctx);
358 2446 ▶ for (i = 0; i < ctx->root_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ctx->root_count; i++) { 1247 1199 2446
359 1247 SvREFCNT_dec(ctx->roots[i].sv);
360 1247 SvREFCNT_dec(ctx->roots[i].name);
361 }
362 1199 Safefree(ctx->roots);
363 1199 Safefree(ctx->queue);
364 1199 Safefree(ctx->queue_levels);
365 1199 Safefree(ctx->table.buckets);
366 1199 SvREFCNT_dec(ctx->types);
367 1199 SvREFCNT_dec(ctx->abbreviations);
368 1199 SvREFCNT_dec(ctx->lexical_names);
369 1199 SvREFCNT_dec(ctx->lexical_added);
370 1199 SvREFCNT_dec(ctx->keepalive);
371 1199 SvREFCNT_dec(ctx->style_values);
372 1199 Safefree(ctx);
373 1199 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 6830 dds_ctx *dds_context(pTHX_ SV *self)
382 {
383 HV *hv;
384 MAGIC *mg;
385 dds_ctx *ctx;
386  
387 6830 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 6830 0 6830
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 0 6830 6830
388 0 croak("Data::Dump::Streamer requires an object");
389  
390 6830 hv = (HV *)SvRV(self);
391 6830 mg = mg_findext((SV *)hv, PERL_MAGIC_ext, &dds_magic_vtbl);
392 6830 ▶ if (mg)
Branch: 1 branch
BranchTrueFalseTotal
if (mg) 5631 1199 6830
393 5631 return (dds_ctx *)mg->mg_ptr;
394  
395 1199 Newxz(ctx, 1, dds_ctx);
396 1199 ctx->object = hv;
397 1199 ctx->types = newHV();
398 1199 ctx->abbreviations = newHV();
399 1199 ctx->lexical_names = newHV();
400 1199 ctx->lexical_added = newHV();
401 1199 ctx->keepalive = newAV();
402 1199 ctx->style_values = newAV();
403 1199 sv_magicext((SV *)hv, NULL, PERL_MAGIC_ext, &dds_magic_vtbl, (const char *)ctx, 0);
404  
405 1199 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 1518 void dds_capture(pTHX_ dds_ctx *ctx, I32 count, SV **args)
412 {
413 I32 i;
414 1518 dds_root *roots, *old_roots = ctx->roots;
415 1518 UV old_count = ctx->root_count;
416  
417 1518 ▶ Newxz(roots, count ? count : 1, dds_root);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(roots, count ? count : 1, dds_root); 1518 0 1518
418 3436 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 1918 1518 3436
419 1918 roots[i].sv = SvREFCNT_inc(args[i]);
420 1918 roots[i].count = SvREFCNT(args[i]) + dds_weak_count(aTHX_ args[i]);
421 1918 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 1518 ctx->roots = roots;
427 1518 ctx->root_count = ctx->root_capacity = count;
428 1518 dds_clear_catalogue(aTHX_ ctx);
429  
430 2233 ▶ for (i = 0; i < (I32)old_count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < (I32)old_count; i++) { 715 1518 2233
431 715 SvREFCNT_dec(old_roots[i].sv);
432 715 SvREFCNT_dec(old_roots[i].name);
433 }
434 1518 Safefree(old_roots);
435 1518 }
436  
437 /* Snapshot facade formatting options into native fields and own copies of
438 * string values for the active operation.
439 */
440 3031 void dds_load_style(pTHX_ dds_ctx *ctx)
441 {
442 3031 HV *style = dds_hash(aTHX_ ctx->object, "style", FALSE);
443 3031 dds_style *s = &ctx->style;
444  
445 /* Snapshot strings so callbacks cannot invalidate the active style. */
446 3031 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 3031 STR(space, "optspace");
458 3031 STR(hashsep, "hashsep");
459 3031 STR(arysep, "arysep");
460 3031 STR(pairsep, "pairsep");
461 3031 STR(bless, "bless");
462 3031 STR(compressor, "compressor");
463 3031 STR(codestub, "codestub");
464 3031 STR(formatstub, "formatstub");
465  
466 3031 NUM(indent, "indent");
467 3031 NUM(indentcols, "indentcols");
468 3031 NUM(compress, "compress");
469  
470 3031 BOOL(indentkeys, "indentkeys");
471 3031 BOOL(declare, "declare");
472 3031 BOOL(verbose, "verbose");
473 3031 BOOL(dumpglob, "dumpglob");
474 3031 BOOL(deparse, "deparse");
475 3031 BOOL(deparseglob, "deparseglob");
476 3031 BOOL(rle, "rle");
477 3031 BOOL(readonly, "ro");
478 3031 BOOL(dualvars, "dualvars");
479 3031 BOOL(purity, "purity");
480 3031 BOOL(terse, "terse");
481 3031 BOOL(special, "special");
482 #undef STR
483 #undef NUM
484 #undef BOOL
485 3031 }
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 1071 void dds_callback_guard(pTHX_ dds_ctx *ctx)
497 {
498 UV i;
499 dds_active *active;
500  
501 1071 ▶ if (!ctx->callbacks) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ctx->callbacks) { 270 801 1071
502 270 ctx->callbacks = TRUE;
503 749 ▶ for (i = 0; i < ctx->root_count; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ctx->root_count; i++) 479 270 749
504 479 av_push(ctx->keepalive, SvREFCNT_inc(ctx->roots[i].sv));
505 270 ▶ if (ctx->queue_head < ctx->queue_count)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->queue_head < ctx->queue_count) 79 191 270
506 490 ▶ for (i = 0; i < ctx->queue_count; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ctx->queue_count; i++) 411 79 490
507 411 ▶ if (ctx->queue[i].name)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->queue[i].name) 150 261 411
508 150 av_push(ctx->keepalive, SvREFCNT_inc(ctx->queue[i].sv));
509 }
510  
511 152211 ▶ for (active = ctx->active; active; active = active->parent) {
Branch: 1 branch
BranchTrueFalseTotal
for (active = ctx->active; active; active = active->parent) { 151140 1071 152211
512 151140 ▶ SV *referent = SvROK(active->sv) ? SvRV(active->sv) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *referent = SvROK(active->sv) ? SvRV(active->sv) : NULL; 150756 384 151140
513  
514 /* Ancestor frames recur at every deeper callback. Keep their
515 * unchanged SVs once, but still retain replacements after mutation. */
516 151140 ▶ if (active->retained_sv != active->sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (active->retained_sv != active->sv) { 3140 148000 151140
517 3140 av_push(ctx->keepalive, SvREFCNT_inc(active->sv));
518 3140 active->retained_sv = active->sv;
519 }
520 151140 ▶ if (referent && active->retained_ref != referent) {
Branch: 2 branches
BranchTrueFalseTotal
if (referent && active->retained_ref != referent) { 150756 384 151140
if (referent && active->retained_ref != referent) { 2763 147993 150756
521 2763 av_push(ctx->keepalive, SvREFCNT_inc(referent));
522 2763 active->retained_ref = referent;
523 }
524 }
525 1071 }
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 3023 static void dds_operation_cleanup(pTHX_ void *ptr)
536 {
537 3023 dds_operation *operation = (dds_operation *)ptr;
538 3023 dds_ctx *ctx = operation->ctx;
539  
540 3023 ctx->active = NULL;
541 3023 ctx->owner = NULL;
542 3023 ctx->busy = FALSE;
543 3023 ▶ if (!operation->committed) {
Branch: 1 branch
BranchTrueFalseTotal
if (!operation->committed) { 6 3017 3023
544 6 dds_clear_catalogue(aTHX_ ctx);
545 6 hv_stores(ctx->object, "cataloged", newSViv(0));
546 }
547  
548 3023 Safefree(operation);
549 3023 }
550  
551 /* Protect the dumper for an operation, reject concurrent use of its context
552 * and install exception-safe scope cleanup.
553 */
554 3023 void *dds_operation_begin(pTHX_ dds_ctx *ctx, SV *self)
555 {
556 dds_operation *operation;
557  
558 3023 ▶ if (ctx->busy)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->busy) 0 3023 3023
559 0 croak("DDS operation already active");
560  
561 3023 SAVEFREESV(SvREFCNT_inc(self));
562 3023 Newxz(operation, 1, dds_operation);
563 3023 operation->ctx = ctx;
564 3023 ctx->busy = TRUE;
565 3023 ctx->owner = self;
566 3023 SAVEDESTRUCTOR_X(dds_operation_cleanup, operation);
567  
568 3023 return operation;
569 }
570  
571 /* Mark an operation successful so scope cleanup preserves its completed
572 * catalogue.
573 */
574 3017 void dds_operation_commit(void *operation)
575 {
576 3017 ((dds_operation *)operation)->committed = TRUE;
577 3017 }
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 1050 SV *dds_invoke(pTHX_ dds_ctx *ctx, SV *callable, bool method, I32 flags, I32 argc, SV **argv)
583 {
584 1050 dSP;
585 I32 i, count;
586 1050 SV *result = DDS_TEMP(newSV(0));
587 1050 AV *values = NULL;
588 1050 AV *arguments = (AV *)DDS_TEMP((SV *)newAV());
589 1050 ▶ SV *error = (flags & G_EVAL) ? DDS_TEMP(newSV(0)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *error = (flags & G_EVAL) ? DDS_TEMP(newSV(0)) : NULL; 68 982 1050
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 4217 ▶ for (i = 0; i < argc; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < argc; i++) 3167 1050 4217
594 3167 av_push(arguments, SvREFCNT_inc(argv[i]));
595 1050 callable = DDS_TEMP(newSVsv(callable));
596 1050 ▶ if (ctx)
Branch: 1 branch
BranchTrueFalseTotal
if (ctx) 1048 2 1050
597 1048 dds_callback_guard(aTHX_ ctx);
598  
599 1050 ENTER;
600 1050 SAVETMPS;
601 1050 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1050 1050
602 1050 ▶ EXTEND(SP, argc);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, argc); 1050 0 1050
EXTEND(SP, argc); 0 1050 1050
603 4217 ▶ for (i = 0; i < argc; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < argc; i++) 3167 1050 4217
604 3167 PUSHs(AvARRAY(arguments)[i]);
605 1050 PUTBACK;
606 1171 ▶ 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 929 1050
607 1044 SPAGAIN;
608  
609 /* Copy results out before leaving the callback's temporary scope. */
610 1044 ▶ if (error)
Branch: 1 branch
BranchTrueFalseTotal
if (error) 68 976 1044
611 68 ▶ sv_setsv(error, ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(error, ERRSV); 68 0 68
612 1044 ▶ if ((flags & G_WANT) == G_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if ((flags & G_WANT) == G_ARRAY) { 839 205 1044
613 839 values = newAV();
614 839 sv_setsv(result, DDS_TEMP(newRV_noinc((SV *)values)));
615 21868 ▶ for (i = count; i > 0; i--)
Branch: 1 branch
BranchTrueFalseTotal
for (i = count; i > 0; i--) 21029 839 21868
616 21029 av_push(values, newSVsv(SP[1 - i]));
617 839 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 1044 PUTBACK;
627 1044 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 1009 35 1044
628 1044 LEAVE;
629  
630 /* G_EVAL's exception value must survive temporary cleanup as well. */
631 1044 ▶ if (error)
Branch: 1 branch
BranchTrueFalseTotal
if (error) 68 976 1044
632 68 ▶ sv_setsv(ERRSV, error);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, error); 68 0 68
633  
634 1044 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 347 SV *dds_freeze(pTHX_ dds_ctx *ctx, SV *rv)
661 {
662 347 SV *target = SvRV(rv), *class_name = dds_class(aTHX_ target);
663 347 HV *style = dds_hash(aTHX_ ctx->object, "style", FALSE);
664 347 HV *classes = dds_hash(aTHX_ style, "freeze_class", FALSE);
665 347 HV *skip = dds_hash(aTHX_ ctx->object, "cache_skip_freeze", TRUE);
666 347 HV *cache = dds_hash(aTHX_ ctx->object, "cache_freeze_class", TRUE);
667 347 SV *cached = dds_hget_sv(aTHX_ cache, class_name);
668 SV *choices[3];
669 UV i;
670  
671 347 ▶ if (SvTRUE(dds_hget_sv(aTHX_ skip, class_name)))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(dds_hget_sv(aTHX_ skip, class_name))) 25 322 347
672 25 return &PL_sv_undef;
673  
674 322 ▶ if (SvOK(cached)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(cached)) { 23 299 322
675 23 SV *values = dds_invoke(aTHX_ ctx, cached, !SvROK(cached), G_ARRAY, 1, &rv);
676 22 AV *list = (AV *)SvRV(values);
677 22 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 22 ▶ if (!proxy || !SvOK(*proxy))
Branch: 2 branches
BranchTrueFalseTotal
if (!proxy || !SvOK(*proxy)) 21 1 22
if (!proxy || !SvOK(*proxy)) 1 20 21
682 2 av_store(list, 0, newSViv(0));
683 22 return values;
684 }
685  
686 /* Try the class policy, configured hook, then the conventional method. */
687 299 choices[0] = DDS_TEMP(newSVsv(dds_hget_sv(aTHX_ classes, class_name)));
688 299 choices[1] = DDS_TEMP(newSVsv(dds_hget(aTHX_ style, "freezer")));
689 299 choices[2] = DDS_TEMP(newSVpvs("DDS_freeze"));
690  
691 1024 ▶ for (i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) { 793 231 1024
692 793 SV *action = choices[i], *values;
693 SV **proxy;
694  
695 793 ▶ if (!SvTRUE(action)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvTRUE(action)) { 263 530 793
696 263 ▶ if (SvOK(action))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(action)) 1 262 263
697 1 break;
698 else
699 262 continue;
700 }
701 1023 ▶ 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)) 493 37 530
if (!SvROK(action) && !gv_fetchmethod_autoload(SvSTASH(target), DDS_PV(action), 0)) 462 31 493
702 462 continue;
703  
704 68 values = dds_invoke(aTHX_ ctx, action, !SvROK(action), G_ARRAY | G_EVAL, 1, &rv);
705 68 ▶ if (SvROK(ERRSV) || SvTRUE(ERRSV)) {
Branch: 4 branches
BranchTrueFalseTotal
if (SvROK(ERRSV) || SvTRUE(ERRSV)) { 68 0 68
if (SvROK(ERRSV) || SvTRUE(ERRSV)) { 67 1 68
if (SvROK(ERRSV) || SvTRUE(ERRSV)) { 67 0 67
if (SvROK(ERRSV) || SvTRUE(ERRSV)) { 0 67 67
706 1 ▶ sv_setpvs(ERRSV, "");
Branch: 1 branch
BranchTrueFalseTotal
sv_setpvs(ERRSV, ""); 1 0 1
707 1 continue;
708 }
709  
710 67 proxy = av_fetch((AV *)SvRV(values), 0, 0);
711 67 ▶ if (proxy && SvOK(*proxy))
Branch: 2 branches
BranchTrueFalseTotal
if (proxy && SvOK(*proxy)) 64 3 67
if (proxy && SvOK(*proxy)) 60 4 64
712 60 hv_store_ent(cache, class_name, newSVsv(action), 0);
713 else
714 7 hv_store_ent(skip, class_name, newSViv(1), 0);
715 67 return values;
716 }
717  
718 232 hv_store_ent(skip, class_name, newSViv(1), 0);
719 232 return &PL_sv_undef;
720 }
« dds_format.c ↑ Summary dds_analyze.c »