lib/Devel/Size/Report.pm

filter: Statementstmt 95.9% 238 / 248 Branchbran 88.6% 101 / 114 Conditioncond 89.3% 42 / 47 MC/DCmcdc 82.1% 23 / 28 Subroutinesub 100% 23 / 23 Podpod 100% 6 / 6 totaltotal 92.9% 433 / 466 CC 81 SCAR 28.6
CC
81
Cov
93%
_track_size
28.0
report_size
27.1
_default_options
14.1
CRAP 17.5
↑ Summary
1 package Devel::Size::Report;
2  
3 10 require 5.006;
4  
5 10 $VERSION = '0.13';
6  
7 10 ▶use Devel::Size qw(size total_size);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 10 ▶use Scalar::Util qw/reftype refaddr blessed dualvar isweak readonly isvstring/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use Time::HiRes qw/time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Array::RefElem qw/hv_store av_push/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Devel::Peek qw/SvREFCNT/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 require Exporter;
14 10 @ISA = qw/Exporter/;
15 10 @EXPORT_OK = qw/
16 report_size track_size element_type type entries_per_element track_sizes
17 hide_tracks
18  
19 S_SCALAR
20 S_HASH
21 S_ARRAY
22 S_GLOB
23 S_UNKNOWN
24 S_CODE
25 S_LVALUE
26 S_REGEXP
27 S_CYCLE
28 S_DOUBLE
29 S_VSTRING
30
31 SF_WEAK
32 SF_KEY
33 SF_REF
34 SF_WEAK
35 SF_RO
36 SF_DUAL
37 SF_MAGIC
38  
39 /;
40  
41 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 #############################################################################
44 # The following should not be global to be thread safe:
45  
46 # If somebody used hv_store, we need also to enter hash key addresses into
47 # SEEN. Default is off, because this wastes memory.
48 10 my $TRACK_DOUBLES = 0;
49  
50 # _track_size() stores it's result here:
51 10 my @sizes;
52  
53 # for cycles in memory:
54 my %SEEN;
55  
56 # count calls to track_size for statistics
57 10 my $CALLS;
58  
59 #############################################################################
60 # The overhead for one ref. Used to correct the results from Devel::Size.
61 10 my $SIZE_OF_REF;
62  
63 BEGIN
64 10 ▶ {
Subroutine BEGIN: called CC=1 SCAR=0.0
65 # disable any warnings Devel::Size might spill
66 10 $Devel::Size::warn = 0;
67  
68 # Devel::Size will dereference arguments, so it misses the size of the
69 # reference. Compute the size for \\0 and \0 and infer the overhead for
70 # one reference from that. Thanx to SADAHIRO Tomoyuki.
71  
72 10 $SIZE_OF_REF = total_size(\\0) - total_size(\0);
73 }
74  
75 # scalar that can be entered into %SEEN many times:
76 10 my $UNDEF = undef;
77 # scalar that can be entered into @sizes many times:
78 10 my $ZERO = 0;
79  
80 # the different types of elements
81 10 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
82 10 S_UNKNOWN => 0,
83 S_CYCLE => 1,
84 S_SCALAR => 2,
85 S_ARRAY => 3,
86 S_HASH => 4,
87 S_GLOB => 5,
88 S_CODE => 6,
89 S_REGEXP => 7,
90 S_LVALUE => 8,
91 S_DOUBLE => 9,
92 10 S_VSTRING => 10 };
93  
94 # some flags (to be added to the types)
95 10 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
96 10 SF_KEY => 0x0100,
97 SF_REF => 0x0200,
98 SF_BLESS => 0x0400,
99 SF_WEAK => 0x0800,
100 SF_RO => 0x1000,
101 SF_DUAL => 0x2000,
102 10 SF_MAGIC => 0x4000 };
103  
104 sub entries_per_element () { 7; }
105  
106 # default mapping for type output names (human readable)
107 10 my $TYPE = {
108 S_SCALAR() => 'Scalar',
109 S_UNKNOWN() => 'Unknown',
110 S_HASH() => 'Hash ref',
111 S_GLOB() => 'Glob',
112 S_ARRAY() => 'Array ref',
113 S_CODE() => 'Code',
114 S_REGEXP() => 'Regexp',
115 S_LVALUE() => 'Lvalue',
116 S_CYCLE() => 'Circular ref',
117 S_DOUBLE() => 'Double scalar ref',
118 S_VSTRING() => 'VString',
119  
120 SF_REF() => 'Ref',
121 SF_BLESS() => 'Blessed',
122 SF_WEAK() => 'Weak',
123 SF_RO() => 'Read-Only',
124 SF_DUAL() => 'Dual-Var',
125 SF_MAGIC() => 'Magical',
126 SF_KEY() => '',
127 };
128  
129 # default mapping for type name (internal comparisation)
130 10 my $TYPE_CLASS = {
131 S_SCALAR() => 'SCALAR',
132 S_UNKNOWN() => 'UNKNOWN',
133 S_HASH() => 'HASH',
134 S_GLOB() => 'GLOB',
135 S_ARRAY() => 'ARRAY',
136 S_CODE() => 'CODE',
137 S_REGEXP() => 'REGEXP',
138 S_LVALUE() => 'LVALUE',
139 S_CYCLE() => 'CYCLE',
140 S_DOUBLE() => 'DOUBLE',
141 S_VSTRING() => 'VSTRING',
142 };
143  
144 # map 'SCALAR' => S_SCALAR
145 10 my $NAME_MAP = {
146 SCALAR => S_SCALAR(),
147 HASH => S_HASH(),
148 GLOB => S_GLOB(),
149 ARRAY => S_ARRAY(),
150 CODE => S_CODE(),
151 REGEXP => S_REGEXP(),
152 LVALUE => S_LVALUE(),
153 CYCLE => S_CYCLE(),
154 DOUBLE => S_DOUBLE(),
155 VSTRING => S_VSTRING(),
156  
157 REF => SF_REF(),
158 KEY => SF_KEY(),
159 WEAK => SF_WEAK(),
160 DUAL => SF_DUAL(),
161 RO => SF_RO(),
162 MAGIC => SF_MAGIC(),
163 };
164  
165 sub _default_options
166 54 ▶ {
Subroutine _default_options: called CC=13 SCAR=26.5
167 # set the options to their default values
168 54 my ($options) = @_;
169  
170 54 my $o = {};
171 54 for my $k (keys %$options) { $o->{$k} = $options->{$k}; }
172
173 54 ▶ $o->{indent} = ' ' if !defined $o->{indent};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o->{"indent"} 54 0 54
174 54 ▶ $o->{names} ||= $TYPE;
Condition33%1/3
$o->{"names"} ||= $TYPE
l!l&&r!l&&!r
0540
MC/DC0%0/2
$o->{"names"} ||= $TYPE
$o->{"names"} $TYPE
Truth table33%1/3
A ||= B
A= $o->{"names"}B= $TYPE
ABresult
1X1
011
000
175  
176 54 ▶ $o->{bytes} = 'bytes' unless defined $o->{bytes};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o->{"bytes"} 53 1 54
177 54 ▶ $o->{bytes} = ' ' . $o->{bytes} if $o->{bytes} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"bytes"} ne "" 53 1 54
178  
179 54 ▶ $o->{left} = '' if !defined $o->{left};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o->{"left"} 54 0 54
180 54 ▶ $o->{inner} = ' ' if !defined $o->{inner};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o->{"inner"} 54 0 54
181
182 54 ▶ $o->{total} = 1 if !defined $o->{total};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o->{"total"} 54 0 54
183  
184 54 ▶ $o->{head} = "Size report v$Devel::Size::Report::VERSION for" if !defined $o->{head};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o->{"head"} 13 41 54
185  
186 54 ▶ $o->{overhead} = " (overhead: %i%s, %0.2f%%)" if !defined $o->{overhead};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o->{"overhead"} 54 0 54
187  
188 # binary flags
189 54 for my $k (qw/addr terse class/)
190 {
191 162 ▶ $o->{$k} ||= 0;
Condition100%2/2
$o->{$k} ||= 0
l!l
25137
MC/DC100%1/1
$o->{$k} ||= 0
$o->{$k}
Truth table100%2/2
A
A= $o->{$k}
Aresult
11
00
192 }
193  
194 54 $o;
195 }
196  
197 sub report_size
198 55 ▶ {
Subroutine report_size: called CC=27 SCAR=33.0
Pod documented
199 # walk the given reference recursively and return text describing the size
200 # of each element
201 55 my ($ref,$opt) = @_;
202
203 55 ▶ $opt = {} unless defined $opt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt 2 53 55
204 55 ▶ if (ref($opt) ne 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt ne "HASH") 1 54 55
205 {
206 1 require Carp;
207 1 Carp::confess ("report_size() needs a hash ref for options");
208 }
209
210 54 my $options = _default_options($opt);
211  
212 54 ▶ $TRACK_DOUBLES = $options->{doubles} || 0;
Condition100%2/2
$options->{'doubles'} || 0
l!l
252
MC/DC100%1/1
$options->{'doubles'} || 0
$options->{'doubles'}
Truth table100%2/2
A
A= $options->{'doubles'}
Aresult
11
00
213
214 # DONT do "track_size($ref)" because $ref is a copy of $_[0], reusing some
215 # pre-allocated slot and this can have a different total size than $_[0]!!
216  
217 # get the size for all elements so that we can generate a report on it
218 54 track_sizes($_[0],$opt);
219  
220 54 my $text = '';
221
222 54 my $indent = $options->{indent};
223 54 my $names = $options->{names};
224 54 my $bytes = $options->{bytes};
225 54 my $left = $options->{left};
226 54 my $inner = $options->{inner};
227 54 $inner .= $left;
228
229 54 my $total = $options->{total};
230 54 my $head = $options->{head};
231 54 my $terse = $options->{terse};
232 # show summary?
233 54 my $show_summary = $options->{summary};
234  
235 54 my $foverhead = $options->{overhead};
236
237 # show class?
238 54 my $class = $options->{class};
239
240 # show addr?
241 54 my $addr = $options->{addr};
242  
243 54 my $count = {}; # per class/element type counter
244 54 my $sum = {}; # per class/element memory sum
245  
246 # XXX TODO: why not HASH here?
247 54 ▶ my $r = ref($ref); $r = '' if $r =~ /^(ARRAY|SCALAR)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $r =~ /^(ARRAY|SCALAR)$/ 12 42 54
248 54 ▶ $r = " ($r)" if $r ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $r ne "" 29 25 54
249 54 ▶ $text = "$left$head '$ref'$r:\n" if $head ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $head ne "" 13 41 54
250  
251 54 my $e = entries_per_element();
252
253 54 ▶ for (my $i = 0; $i < @sizes; $i += $e)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @sizes) 204 54 258
254 {
255 # inline element_type for speed:
256 # my $type = element_type( ($sizes[$i+1] & 0xFF),$names);
257 204 ▶ my $type = $names->{ ($sizes[$i+1] & 0xFF) } || 'Unknown';
Condition50%1/2
$$names{$sizes[$i + 1] & 255} || 'Unknown'
l!l
2040
MC/DC0%0/1
$$names{$sizes[$i + 1] & 255} || 'Unknown'
$$names{$sizes[$i + 1] & 255}
Truth table50%1/2
A
A= $$names{$sizes[$i + 1] & 255}
Aresult
11
00
258  
259 204 ▶ if ($show_summary)
Branch: 1 branch
BranchTrueFalseTotal
if ($show_summary) 4 200 204
260 {
261 4 my $t = $sizes[$i+1] & 0xFF; $t = $TYPE_CLASS->{$t};
262 4 ▶ $t = $sizes[$i+6] if $sizes[$i+6];
Branch: 1 branch
BranchTrueFalseTotal
if $sizes[$i + 6] 2 2 4
263 4 ▶ print "# $t $sizes[$i+1]\n" if $t eq '_set';
Branch: 1 branch
BranchTrueFalseTotal
if $t eq "_set" 0 4 4
264 4 ▶ if ($t)
Branch: 1 branch
BranchTrueFalseTotal
if ($t) 4 0 4
265 {
266 4 $count->{$t} ++;
267 4 $sum->{$t} += $sizes[$i+2];
268 }
269 # else { should not happen }
270 }
271  
272 204 ▶ if (!$terse)
Branch: 1 branch
BranchTrueFalseTotal
unless ($terse) 200 4 204
273 {
274 # include flags
275 200 for my $flag (SF_WEAK, SF_RO, SF_DUAL)
276 {
277 600 ▶ if ( ($sizes[$i+1] & $flag) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (($sizes[$i + 1] & $flag) != 0) 8 592 600
278 {
279 8 $type = element_type($flag, $names) . ' ' . $type;
280 }
281 }
282 200 ▶ if ( ($sizes[$i+1] & SF_REF) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (($sizes[$i + 1] & 512) != 0) 14 186 200
283 {
284 14 $type .= " " . element_type(SF_REF, $names);
285 }
286  
287 # add addr of element if wanted
288 200 ▶ $type .= "(" . $sizes[$i+5] . ")" if $addr && $sizes[$i+5];
Branch: 1 branch
BranchTrueFalseTotal
if $addr and $sizes[$i + 5] 76 124 200
Condition66%2/3
$addr and $sizes[$i + 5]
!ll&&!rl&&r
124076
MC/DC50%1/2
$addr and $sizes[$i + 5]
$addr $sizes[$i + 5]
Truth table66%2/3
A and B
A= $addrB= $sizes[$i + 5]
ABresult
0X0
100
111
289  
290 # add class of element if wanted
291 200 ▶ $type .= " (" . $sizes[$i+6] . ")" if $class && $sizes[$i+6];
Branch: 1 branch
BranchTrueFalseTotal
if $class and $sizes[$i + 6] 5 195 200
Condition100%3/3
$class and $sizes[$i + 6]
!ll&&!rl&&r
19055
MC/DC100%2/2
$class and $sizes[$i + 6]
$class $sizes[$i + 6]
Truth table100%3/3
A and B
A= $classB= $sizes[$i + 6]
ABresult
0X0
100
111
292  
293 200 my $str = $type;
294 200 ▶ if ( ($sizes[$i+1] & SF_KEY) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (($sizes[$i + 1] & 256) != 0) 93 107 200
295 {
296 93 $str = "'$sizes[$i+4]' => " . $type;
297 }
298 200 $str .= " $sizes[$i+2]$bytes";
299 200 ▶ if ($sizes[$i+3] != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($sizes[$i + 3] != 0) 54 146 200
300 {
301 54 my $overhead =
302 sprintf($foverhead, $sizes[$i+3], $bytes,
303 100 * $sizes[$i+3] / $sizes[$i+2]);
304 54 ▶ $overhead = ' (overhead: unknown)' if $sizes[$i+3] < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $sizes[$i + 3] < 0 4 50 54
305 54 $str .= $overhead;
306 }
307 200 $text .= $inner . ($indent x $sizes[$i]) . "$str\n";
308 }
309 }
310  
311 54 ▶ if ($show_summary)
Branch: 1 branch
BranchTrueFalseTotal
if ($show_summary) 1 53 54
312 {
313 # default sort is by size
314 1 ▶ my $sort = sub { $sum->{$b} <=> $sum->{$a} };
Subroutine __ANON__: called CC=1 SCAR=0.0
315  
316 1 $text .= "Total memory by class (inclusive contained elements):\n";
317 1 foreach my $k (sort $sort keys %$count)
318 {
319 3 $text .= $indent . _right_align($sum->{$k},10) . " bytes in " . _right_align($count->{$k},6) . " $k\n";
320 }
321 }
322 54 my $elements = scalar @sizes / $e;
323 54 ▶ $text .= $left . "Total: $sizes[2]$bytes in $elements elements\n" if $total;
Branch: 1 branch
BranchTrueFalseTotal
if $total 54 0 54
324  
325 54 hide_tracks(); # release memory
326  
327 54 $text;
328 }
329  
330 sub hide_tracks
331 55 ▶ {
Subroutine hide_tracks: called CC=1 SCAR=0.0
Pod documented
332 55 @sizes = ();
333 }
334  
335 sub _right_align
336 6 ▶ {
Subroutine _right_align: called CC=2 SCAR=6.9
337 6 my ($txt,$len) = @_;
338  
339 6 ▶ $txt = ' ' . $txt while (length($txt) < $len);
Branch: 1 branch
BranchTrueFalseTotal
length $txt < $len and do { $txt = ' ' . $txt } 37 6 43
340 6 $txt;
341 }
342  
343 sub element_type
344 24 ▶ {
Subroutine element_type: called CC=2 SCAR=6.9
Pod documented
345 24 my ($type,$TYPE) = @_;
346 24 ▶ $TYPE->{$type} || 'Unknown';
Branch: 1 branch
BranchTrueFalseTotal
$TYPE->{$type} or "Unknown" 23 1 24
347 }
348  
349 sub type
350 27 ▶ {
Subroutine type: called CC=2 SCAR=7.6
Pod documented
351 # map a typename to a type number
352 27 ▶ $NAME_MAP->{$_[0]} || S_UNKNOWN;
Branch: 1 branch
BranchTrueFalseTotal
$$NAME_MAP{$_[0]} or 0 27 0 27
353 }
354  
355 sub track_sizes
356 90 ▶ {
Subroutine track_sizes: called CC=3 SCAR=13.2
Pod documented
357 90 my $opt = $_[1];
358  
359 90 ▶ $TRACK_DOUBLES = $opt->{doubles} || 0;
Condition100%2/2
$opt->{'doubles'} || 0
l!l
585
MC/DC100%1/1
$opt->{'doubles'} || 0
$opt->{'doubles'}
Truth table100%2/2
A
A= $opt->{'doubles'}
Aresult
11
00
360
361 90 my $time = time(); # record start time
362 90 undef %SEEN; # reset cycle memory
363 90 $CALLS = 0;
364 90 @sizes = (); # reset results array & stores result:
365 90 _track_size($_[0]); # use $_[0] directly to avoid slot-reusing
366  
367 90 ▶ if ($opt->{debug})
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"debug"}) 0 90 90
368 {
369 0 $time = time() - $time;
370 0 print STDERR "\n DEBUG: Devel::Size::Report v$Devel::Size::Report::VERSION\n";
371 0 my $size_seen = total_size(\%SEEN);
372 0 my $size_sizes = total_size(\@sizes);
373  
374 0 print STDERR " DEBUG: \%SEEN : ", _right_align($size_seen,12), " bytes, ", scalar keys %SEEN, " elements\n";
375 0 print STDERR " DEBUG: \@sizes: ", _right_align($size_sizes,12), " bytes, ", scalar @sizes, " elements\n";
376 0 print STDERR " DEBUG: Total : ", _right_align($size_sizes + $size_seen,12), " bytes, ", scalar @sizes + scalar keys %SEEN, " elements\n";
377 0 print STDERR " DEBUG: Calls to _track_size: $CALLS\n";
378 0 print STDERR " DEBUG: took ", sprintf("%0.3f",$time), " seconds to gather data for report.\n\n";
379 }
380 90 undef %SEEN; # save memory, throw away
381  
382 90 \@sizes;
383 }
384  
385 sub track_size
386 35 ▶ {
Subroutine track_size: called CC=1 SCAR=0.0
Pod documented
387 # fill the results into @sizes
388 35 track_sizes($_[0], $_[1]);
389  
390 # return a copy (backwards compatibility)
391 35 @sizes; # return results
392 }
393  
394 sub _addr
395 364 ▶ {
Subroutine _addr: called CC=3 SCAR=11.0
396 # return address of an element as string
397 364 my $adr;
398 364 ▶ if (ref($_[0]) && $_[1] ne 'REF')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and $_[1] ne 'REF') { } 122 242 364
Condition100%3/3
ref $_[0] and $_[1] ne "REF"
!ll&&!rl&&r
22022122
MC/DC100%2/2
ref $_[0] and $_[1] ne "REF"
ref $_[0] $_[1] ne "REF"
Truth table100%3/3
A and B
A= ref $_[0]B= $_[1] ne "REF"
ABresult
0X0
100
111
399 {
400 122 $adr = sprintf("0x%x", refaddr($_[0]));
401 }
402 else
403 {
404 242 $adr = sprintf("0x%x", refaddr(\($_[0])));
405 }
406  
407 364 $adr;
408 }
409  
410 sub _type
411 364 ▶ {
Subroutine _type: called CC=7 SCAR=19.5
412 # find the type of an element and return as string
413 364 ▶ my $type = uc(reftype($_[0]) || '');
Condition100%2/2
builtin::reftype($_[0]) || ''
l!l
144220
MC/DC100%1/1
builtin::reftype($_[0]) || ''
builtin::reftype($_[0])
Truth table100%2/2
A
A= builtin::reftype($_[0])
Aresult
11
00
414 364 ▶ my $class = blessed($_[0]); $class = '' unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 349 15 364
415  
416 # blessed "Regexp" and ref to scalar?
417 364 ▶ $type ='REGEXP' if $class eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "Regexp" 3 361 364
418  
419 # refs to scalars are tricky
420 364 ▶ $type ='REF'
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] and UNIVERSAL::isa($_[0], "SCALAR") and $type ne "REGEXP" 18 346 364
Condition83%5/6
ref $_[0] and UNIVERSAL::isa($_[0], "SCALAR") and $type ne "REGEXP"
!ll&&!rl&&r
346018
ref $_[0] and UNIVERSAL::isa($_[0], "SCALAR")
!ll&&!rl&&r
22012618
MC/DC66%2/3
ref $_[0] and UNIVERSAL::isa($_[0], "SCALAR") and $type ne "REGEXP"
ref $_[0] UNIVERSAL::isa($_[0], "SCALAR") $type ne "REGEXP"
Truth table75%3/4
A and B and C
A= ref $_[0]B= UNIVERSAL::isa($_[0], "SCALAR")C= $type ne "REGEXP"
ABCresult
0XX0
10X0
1100
1111
421 if ref($_[0]) && UNIVERSAL::isa($_[0],'SCALAR') && $type ne 'REGEXP';
422 364 ($type,$class);
423 }
424  
425 sub _track_size
426 149 ▶ {
Subroutine _track_size: called CC=28 SCAR=33.3
427 # Walk the given reference recursively and store the size, type etc of each
428 # element
429 149 my ($ref, $level) = @_;
430  
431 149 ▶ $level ||= 0;
Condition100%2/2
$level ||= 0
l!l
5990
MC/DC100%1/1
$level ||= 0
$level
Truth table100%2/2
A
A= $level
Aresult
11
00
432  
433 149 $CALLS++;
434
435 10 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
436  
437 # DO NOT use "total_size($ref)" because $ref is a copy of $_[0], reusing some
438 # pre-allocated slot and this can have a different total size than $_[0]!!
439 149 my $total_size = size($_[0]);
440 149 my ($type,$blessed) = _type($_[0]);
441
442 149 my $adr = _addr($_[0],$type);
443  
444 149 ▶ if (exists $SEEN{$adr})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $SEEN{$adr}) 16 133 149
445 {
446 # already seen this part of the world, so return
447 16 ▶ if (ref($ref))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref) 7 9 16
448 {
449 7 push @sizes, $level, S_CYCLE, $SIZE_OF_REF, 0, undef, $adr, $blessed;
450 7 return;
451 }
452 # a scalar seen twice
453 9 push @sizes, $level, S_DOUBLE, 0, 0;
454 9 av_push (@sizes, $UNDEF);
455 9 push @sizes, $adr;
456 9 av_push (@sizes, $UNDEF);
457 9 return;
458 }
459  
460 # put in the address of $ref in the %SEEN hash (things with a refcnt of 1
461 # cannot be part of a cycle, since only one thing is pointing at them)
462 133 ▶ hv_store (%SEEN, $adr , $UNDEF) if ref($_[0]) || SvREFCNT($_[0]) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] or &SvREFCNT(\$_[0]) > 1 112 21 133
Condition100%3/3
ref $_[0] or &SvREFCNT(\$_[0]) > 1
l!l&&r!l&&!r
1001221
MC/DC100%2/2
ref $_[0] or &SvREFCNT(\$_[0]) > 1
ref $_[0] &SvREFCNT(\$_[0]) > 1
Truth table100%3/3
A or B
A= ref $_[0]B= &SvREFCNT(\$_[0]) > 1
ABresult
1X1
011
000
463  
464 # not a reference, but a plain scalar?
465 133 ▶ if (!ref($ref))
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $ref) 33 100 133
466 {
467 33 my $type = S_SCALAR;
468 33 ▶ $type = S_VSTRING if isvstring($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if &isvstring($_[0]) 2 31 33
469  
470 33 push @sizes, $level, _flags($_[0]) + $type, $total_size;
471 33 av_push (@sizes, $ZERO);
472 33 av_push (@sizes, $UNDEF);
473 33 push @sizes, $adr, $blessed;
474 33 return;
475 }
476  
477 100 my $index = scalar @sizes + 2; # idx of "total_size" entry
478  
479 100 ▶ if ($type eq 'ARRAY')
Branch: 5 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 46 54 100
elsif ($type eq 'HASH') { } 32 22 54
elsif ($type eq 'REGEXP') { } 3 19 22
elsif ($type eq 'REF') { } 15 4 19
elsif ($type eq 'SCALAR') { } 0 4 4
480 {
481 46 push @sizes, $level, S_ARRAY, $total_size + $SIZE_OF_REF, 0, undef, $adr, $blessed;
482  
483 46 my $sum = 0;
484 46 ▶ for (my $i = 0; $i < scalar @$ref; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$ref) 96 46 142
485 {
486 96 my $adr = _addr($ref->[$i], _type($ref->[$i]));
487  
488 96 ▶ if (exists $SEEN{$adr} || ref($ref->[$i]))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $SEEN{$adr} or ref $ref->[$i]) { } 27 69 96
Condition100%3/3
exists $SEEN{$adr} or ref $ref->[$i]
l!l&&r!l&&!r
52269
MC/DC100%2/2
exists $SEEN{$adr} or ref $ref->[$i]
exists $SEEN{$adr} ref $ref->[$i]
Truth table100%3/3
A or B
A= exists $SEEN{$adr}B= ref $ref->[$i]
ABresult
1X1
011
000
489 {
490 27 my $index = scalar @sizes;
491 27 _track_size($ref->[$i], $level+1);
492 27 $sum += $sizes[$index+2];
493 }
494 else
495 {
496 # Put in the address of $ref in the %SEEN hash.
497 # If TRACK_DOUBLES is set, we also need to store scalars with
498 # REFCNT == 1 because somebody might have used hv_store() to make all
499 # keys point to the same scalar and these "shared" scalars have
500 # unfortunately a REFCNT of 1.
501 69 ▶ hv_store (%SEEN, $adr , $UNDEF) if $TRACK_DOUBLES || SvREFCNT($_[0]) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $TRACK_DOUBLES or &SvREFCNT(\$_[0]) > 1 6 63 69
Condition100%3/3
$TRACK_DOUBLES or &SvREFCNT(\$_[0]) > 1
l!l&&r!l&&!r
3363
MC/DC100%2/2
$TRACK_DOUBLES or &SvREFCNT(\$_[0]) > 1
$TRACK_DOUBLES &SvREFCNT(\$_[0]) > 1
Truth table100%3/3
A or B
A= $TRACK_DOUBLESB= &SvREFCNT(\$_[0]) > 1
ABresult
1X1
011
000
502 69 my $size = size($ref->[$i]);
503 69 push @sizes, $level+1, S_SCALAR, $size;
504 69 av_push (@sizes, $ZERO);
505 69 av_push (@sizes, $UNDEF);
506 69 push @sizes, $adr;
507 69 av_push (@sizes, $UNDEF);
508 69 $sum += $size;
509 }
510 }
511 46 $sizes[$index] += $sum;
512 46 $sizes[$index+1] = $sizes[$index] - $sum;
513 }
514 elsif ($type eq 'HASH')
515 {
516 32 push @sizes, $level, S_HASH, $total_size + $SIZE_OF_REF, 0, undef, $adr, $blessed;
517  
518 32 my $sum = 0;
519 32 foreach my $elem ( keys %$ref )
520 {
521 119 my $adr = _addr($ref->{$elem}, _type($ref->{$elem}));
522 119 ▶ if (exists $SEEN{$adr} || ref($ref->{$elem}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $SEEN{$adr} or ref $ref->{$elem}) { } 17 102 119
Condition100%3/3
exists $SEEN{$adr} or ref $ref->{$elem}
l!l&&r!l&&!r
98102
MC/DC100%2/2
exists $SEEN{$adr} or ref $ref->{$elem}
exists $SEEN{$adr} ref $ref->{$elem}
Truth table100%3/3
A or B
A= exists $SEEN{$adr}B= ref $ref->{$elem}
ABresult
1X1
011
000
523 {
524 17 my $index = scalar @sizes;
525 17 _track_size($ref->{$elem},$level+1);
526  
527 17 $sizes[$index+1] += SF_KEY;
528 17 $sizes[$index+4] = $elem;
529 17 $sum += $sizes[$index+2];
530 }
531 else
532 {
533 # Put in the address of $ref in the %SEEN hash.
534 # If TRACK_DOUBLES is set, we also need to store scalars with
535 # REFCNT == 1 because somebody might have used hv_store() to make all
536 # keys point to the same scalar and these "shared" scalars have
537 # unfortunately a REFCNT of 1.
538 102 ▶ hv_store (%SEEN, $adr , $UNDEF) if $TRACK_DOUBLES || SvREFCNT($_[0]) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $TRACK_DOUBLES or &SvREFCNT(\$_[0]) > 1 4 98 102
Condition100%3/3
$TRACK_DOUBLES or &SvREFCNT(\$_[0]) > 1
l!l&&r!l&&!r
2298
MC/DC100%2/2
$TRACK_DOUBLES or &SvREFCNT(\$_[0]) > 1
$TRACK_DOUBLES &SvREFCNT(\$_[0]) > 1
Truth table100%3/3
A or B
A= $TRACK_DOUBLESB= &SvREFCNT(\$_[0]) > 1
ABresult
1X1
011
000
539 102 my $size = size($ref->{$elem});
540 102 push @sizes, $level+1, SF_KEY + S_SCALAR, $size, 0, $elem, $adr, undef;
541 102 $sum += $size;
542 }
543 }
544 32 $sizes[$index] += $sum;
545 32 $sizes[$index+1] = $sizes[$index] - $sum;
546 }
547 elsif ($type eq 'REGEXP')
548 {
549 3 push @sizes, $level, type($type), $total_size;
550 3 av_push (@sizes, $ZERO);
551 3 av_push (@sizes, $UNDEF);
552 3 push @sizes, $adr, $blessed;
553 }
554 elsif ($type eq 'REF')
555 {
556 15 ▶ my $type = uc(reftype(${$_[0]}) || '');
Condition100%2/2
builtin::reftype(${$_[0];}) || ''
l!l
411
MC/DC100%1/1
builtin::reftype(${$_[0];}) || ''
builtin::reftype(${$_[0];})
Truth table100%2/2
A
A= builtin::reftype(${$_[0];})
Aresult
11
00
557 15 ▶ $type ='REGEXP' if $blessed eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
if $blessed eq "Regexp" 0 15 15
558 15 ▶ $type ='SCALAR' if !ref(${$_[0]});
Branch: 1 branch
BranchTrueFalseTotal
unless ref ${$_[0];} 11 4 15
559 15 my $flags = SF_REF;
560 15 ▶ $flags += SF_WEAK if isweak($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::is_weak($_[0]) 1 14 15
561  
562 15 push @sizes,
563 ($level, $flags + type($type), $total_size, 0, undef, $adr, $blessed);
564 15 _track_size($$ref,$level+1);
565 15 $sizes[$index] += $SIZE_OF_REF; # account for wrong \"" sizes
566 15 $sizes[$index+1] = $sizes[$index] - total_size($$ref);
567 }
568 # SCALAR reference must come after Regexp, because these are also SCALAR !?
569 elsif ($type eq 'SCALAR')
570 {
571 0 push @sizes, ($level, SF_REF, $total_size, 0, undef, $adr, $blessed);
572 }
573 else
574 {
575 4 my $overhead = 0;
576 4 ▶ $overhead = -1 if type($type) == S_UNKNOWN;
Branch: 1 branch
BranchTrueFalseTotal
if type($type) == 0 0 4 4
577 4 push @sizes, ($level, type($type), $total_size, $overhead, undef, $adr, $blessed);
578 }
579 }
580  
581 sub _flags
582 40 ▶ {
Subroutine _flags: called CC=3 SCAR=11.0
583 40 my $flags = 0;
584  
585 40 ▶ $flags += SF_RO if readonly($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if &readonly($_[0]) 10 30 40
586 40 ▶ $flags += SF_WEAK if isweak($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::is_weak($_[0]) 2 38 40
587  
588 # XXX TODO: how to find out if something is:
589 # an LVALUE
590 # a DUALVAR
591 # a STASH
592  
593 40 $flags;
594 }
595  
596 10 1;
597 __END__
↑ Summary