EndOfScope/PP/HintHash.pm untested

filter: Statementstmt 0.0% 0 / 31 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 63 CC 4 SCAR 21.5
CC
4
Cov
0%
on_scope_end
20.0
B::Hooks::EndOfScope::PP::_SG_STACK::DESTROY
2.0
B::Hooks::EndOfScope::PP::HintHash::__GraveyardTransport::DESTROY
2.0
CRAP 8.5
1 # Implementation of a pure-perl on_scope_end for perls < 5.10
2 # (relies on lack of compile/runtime duality of %^H before 5.10
3 # which makes guard object operation possible)
4  
5 package # hide from the pauses
6 B::Hooks::EndOfScope::PP::HintHash;
7  
8 use strict;
9 use warnings;
10  
11 our $VERSION = '0.28';
12  
13 use Scalar::Util ();
14 use constant _NEEDS_MEMORY_CORRUPTION_FIXUP => (
15 "$]" >= 5.008
16 and
17 "$]" < 5.008004
18 ) ? 1 : 0;
19  
20  
21 use constant _PERL_VERSION => "$]";
22  
23 # This is the original implementation, which sadly is broken
24 # on perl 5.10+ within string evals
25 sub on_scope_end (&) {
26  
27 # the scope-implicit %^H localization is a 5.8+ feature
28 $^H |= 0x020000
29 if _PERL_VERSION >= 5.008;
30  
31 # the explicit localization of %^H works on anything < 5.10
32 # but we use it only on 5.6 where fiddling $^H has no effect
33 local %^H = %^H
34 if _PERL_VERSION < 5.008;
35  
36 # Workaround for memory corruption during implicit $^H-induced
37 # localization of %^H on 5.8.0~5.8.3, see extended comment below
38 bless \%^H, 'B::Hooks::EndOfScope::PP::HintHash::__GraveyardTransport' if (
39 _NEEDS_MEMORY_CORRUPTION_FIXUP
40 and
41 ref \%^H eq 'HASH' # only bless if it is a "pure hash" to start with
42 );
43  
44 # localised %^H behaves funny on 5.8 - a
45 # 'local %^H;'
46 # is in effect the same as
47 # 'local %^H = %^H;'
48 # therefore make sure we use different keys so that things do not
49 # fire too early due to hashkey overwrite
50 push @{
51 $^H{sprintf '__B_H_EOS__guardstack_0X%x', Scalar::Util::refaddr(\%^H) }
52 ||= bless ([], 'B::Hooks::EndOfScope::PP::_SG_STACK')
53 }, $_[0];
54 }
55  
56 sub B::Hooks::EndOfScope::PP::_SG_STACK::DESTROY {
57 B::Hooks::EndOfScope::PP::__invoke_callback($_) for @{$_[0]};
58 }
59  
60 # This scope implements a clunky yet effective workaround for a core perl bug
61 # https://rt.perl.org/Public/Bug/Display.html?id=27040#txn-82797
62 #
63 # While we can not prevent the hinthash being marked for destruction twice,
64 # we *can* intercept the first DESTROY pass, and squirrel away the entire
65 # structure, until a time it can (hopefully) no longer do any visible harm
66 #
67 # There still *will* be corruption by the time we get to free it for real,
68 # since we can not prevent Perl's erroneous SAVEFREESV mark. What we hope is
69 # that by then the corruption will no longer matter
70 #
71 # Yes, this code does leak by design. Yes it is better than the alternative.
72 {
73 my @Hint_Hash_Graveyard;
74  
75 # "Leak" this entire structure: ensures it and its contents will not be
76 # garbage collected until the very very very end
77 push @Hint_Hash_Graveyard, \@Hint_Hash_Graveyard
78 if _NEEDS_MEMORY_CORRUPTION_FIXUP;
79  
80 sub B::Hooks::EndOfScope::PP::HintHash::__GraveyardTransport::DESTROY {
81  
82 # Resurrect the hinthash being destroyed, persist it into the graveyard
83 push @Hint_Hash_Graveyard, $_[0];
84  
85 # ensure we won't try to re-resurrect during GlobalDestroy
86 bless $_[0], 'B::Hooks::EndOfScope::PP::HintHash::__DeactivateGraveyardTransport';
87  
88 # Perform explicit free of elements (if any) triggering all callbacks
89 # This is what would have happened without this code being active
90 %{$_[0]} = ();
91 }
92 }
93  
94 1;