lib/Scalar/Footnote/Functions.xs

filter: Statementstmt 93.6% 44 / 47 Branchbran 71.8% 23 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.8% 67 / 79 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Scalar/Footnote.pm ↑ Summary blib/lib/Scalar/Footnote/Functions.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 /* this number was reserved with p5p by Piers Cawley */
6 #define SCALAR_MAGIC_footnote (char)0x9b
7  
8 MAGIC*
9 62 _find_magic_footnote(SV* sv) {
10 62 ▶ if (SvMAGICAL( sv )) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvMAGICAL( sv )) { 36 26 62
11 36 return mg_find( sv, SCALAR_MAGIC_footnote );
12 } else {
13 26 return NULL;
14 }
15 }
16  
17 int
18 29 _remove_magic_footnote(MAGIC* mg) {
19 29 ▶ if (mg->mg_flags & MGf_REFCOUNTED) {
Branch: 1 branch
BranchTrueFalseTotal
if (mg->mg_flags & MGf_REFCOUNTED) { 26 3 29
20 26 SV* footnote = mg->mg_obj;
21 26 ▶ if (footnote && footnote != Nullsv) {
Branch: 2 branches
BranchTrueFalseTotal
if (footnote && footnote != Nullsv) { 26 0 26
if (footnote && footnote != Nullsv) { 26 0 26
22 26 ▶ if (SvTYPE( footnote ) == SVTYPEMASK) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE( footnote ) == SVTYPEMASK) { 0 26 26
23 0 warn( "Warning: looks like footnote at 0x%x has already been freed", footnote );
24 }
25 26 ▶ if (SvREFCNT( footnote ) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvREFCNT( footnote ) > 0) { 26 0 26
26 26 SvREFCNT_dec( footnote );
27 26 mg->mg_flags &= ~MGf_REFCOUNTED;
28 }
29 }
30 26 return 0;
31 }
32 3 return 1;
33 }
34  
35 int
36 22 _magic_freefootnote(pTHX_ SV* sv, MAGIC* mg) {
37 22 _remove_magic_footnote( mg );
38 22 return 0;
39 }
40  
41 MGVTBL vtbl_footnote = {0,0,0,0,MEMBER_TO_FPTR(_magic_freefootnote)};
42  
43 MODULE = Scalar::Footnote::Functions PACKAGE = Scalar::Footnote::Functions
44  
45 SV*
46 set_footnote(ref, footnote)
47 SV* ref
48 SV* footnote
49 PREINIT:
50 SV* sv;
51 SV* footnote_cp;
52 MAGIC* mg;
53 29 MGVTBL *vtable = &vtbl_footnote;
54 CODE:
55 29 ▶ if (!SvROK(ref)) { croak("set_footnote() needs a reference!"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(ref)) { croak("set_footnote() needs a reference!"); } 2 27 29
56  
57 27 sv = (SV*) SvRV(ref);
58 27 mg = _find_magic_footnote(sv);
59  
60 27 footnote_cp = newSVsv( footnote );
61 27 ▶ if (mg) {
Branch: 1 branch
BranchTrueFalseTotal
if (mg) { 4 23 27
62 4 _remove_magic_footnote( mg );
63 4 mg->mg_obj = footnote_cp;
64 4 mg->mg_flags |= MGf_REFCOUNTED; // turned off by remove
65 } else {
66 // this incs refcnt and sets MGf_REFCOUNTED
67 23 sv_magicext(sv, footnote_cp, SCALAR_MAGIC_footnote, vtable, NULL, 0);
68 23 ▶ if (SvREFCNT( footnote_cp ) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvREFCNT( footnote_cp ) > 1) { 23 0 23
69 23 SvREFCNT_dec( footnote_cp ); // we do our own refcounting
70 }
71 23 SvRMAGICAL_on(sv);
72 }
73 OUTPUT:
74 ref
75  
76 SV*
77 get_footnote(ref)
78 SV* ref
79 PREINIT:
80 MAGIC* mg;
81 SV* sv;
82 CODE:
83 31 ▶ if (!SvROK(ref)) { croak("get_footnote() needs a reference!"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(ref)) { croak("get_footnote() needs a reference!"); } 0 31 31
84  
85 31 sv = (SV*) SvRV(ref);
86 31 mg = _find_magic_footnote(sv);
87  
88 31 ▶ if (mg && (mg->mg_flags & MGf_REFCOUNTED)) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg && (mg->mg_flags & MGf_REFCOUNTED)) { 28 3 31
if (mg && (mg->mg_flags & MGf_REFCOUNTED)) { 27 1 28
89 27 RETVAL = newSVsv( mg->mg_obj );
90 27 ▶ if (RETVAL == Nullsv) {
Branch: 1 branch
BranchTrueFalseTotal
if (RETVAL == Nullsv) { 0 27 27
91 0 warn( "Warning: got 'Nullsv' for get_footnote(sv=0x%x)", sv );
92 0 RETVAL = &PL_sv_undef;
93 }
94 } else {
95 4 RETVAL = &PL_sv_undef;
96 }
97 OUTPUT:
98 RETVAL
99  
100 SV*
101 remove_footnote(ref)
102 SV* ref
103 PREINIT:
104 MAGIC* mg;
105 SV* sv;
106 CODE:
107 4 ▶ if (!SvROK(ref)) { croak("remove_footnote() needs a reference!"); }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(ref)) { croak("remove_footnote() needs a reference!"); } 0 4 4
108  
109 4 sv = (SV*) SvRV(ref);
110 4 mg = _find_magic_footnote(sv);
111  
112 4 ▶ if (mg && (mg->mg_flags & MGf_REFCOUNTED)) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg && (mg->mg_flags & MGf_REFCOUNTED)) { 4 0 4
if (mg && (mg->mg_flags & MGf_REFCOUNTED)) { 3 1 4
113 3 RETVAL = newSVsv( mg->mg_obj );
114 3 _remove_magic_footnote( mg );
115 } else {
116 1 RETVAL = &PL_sv_undef;
117 }
118 OUTPUT:
119 RETVAL
120  
« blib/lib/Scalar/Footnote.pm ↑ Summary blib/lib/Scalar/Footnote/Functions.pm »