c/typemap.c

filter: Statementstmt 79.3% 92 / 116 Branchbran 63.2% 43 / 68 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 73.3% 135 / 184 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« c/watcher.c ↑ Summary c/queue.c »
1 5044 static SV *wrap_thing(U16 mgcode, void *ptr, HV *stash, SV *temple) {
2 SV *ref;
3 MAGIC **mgp;
4 MAGIC *mg;
5  
6 assert(ptr);
7 assert(stash);
8  
9 5044 ▶ if (!temple)
Branch: 1 branch
BranchTrueFalseTotal
if (!temple) 0 5044 5044
10 0 temple = (SV*)newHV();
11 else
12 5044 SvREFCNT_inc(temple);
13 5044 ▶ if (SvOBJECT(temple))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOBJECT(temple)) 1 5043 5044
14 1 croak("Can't attach to blessed reference");
15 assert(!SvROK(temple));
16 assert(mg_find(temple, '~') == 0); /* multiplicity disallowed! */
17  
18 5043 ref = newRV_noinc(temple);
19 5043 sv_bless(ref, stash);
20  
21 5043 mgp = &SvMAGIC(temple);
22 5043 ▶ while ((mg = *mgp))
Branch: 1 branch
BranchTrueFalseTotal
while ((mg = *mgp)) 0 5043 5043
23 0 mgp = &mg->mg_moremagic;
24  
25 5043 New(0, mg, 1, MAGIC);
26 5043 Zero(mg, 1, MAGIC);
27 5043 mg->mg_type = '~';
28 5043 mg->mg_ptr = (char*) ptr; /* NOT refcnt'd */
29 5043 mg->mg_private = mgcode;
30 5043 *mgp = mg;
31  
32 5043 return ref;
33 }
34  
35 390039 static void* sv_2thing(U16 mgcode, SV *sv) {
36 MAGIC *mg;
37 390039 SV *origsv = sv;
38 390039 ▶ if (!sv || !SvROK(sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvROK(sv)) 390039 0 390039
if (!sv || !SvROK(sv)) 1 390038 390039
39 1 croak("sv_2thing: not a reference?");
40 390038 sv = SvRV(sv);
41 390038 ▶ if (SvTYPE(sv) < SVt_PVMG)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(sv) < SVt_PVMG) 3 390035 390038
42 3 croak("sv_2thing: not a thing");
43 390035 ▶ if (!SvOBJECT(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOBJECT(sv)) 0 390035 390035
44 0 croak("sv_2thing: not an object");
45 390035 mg = mg_find(sv, '~');
46 390035 ▶ if (mg) {
Branch: 1 branch
BranchTrueFalseTotal
if (mg) { 390035 0 390035
47 390035 ▶ if (mg->mg_private != mgcode) {
Branch: 1 branch
BranchTrueFalseTotal
if (mg->mg_private != mgcode) { 1 390034 390035
48 1 croak("Can't find event magic (SV=0x%x)", sv);
49 }
50 390034 return (void*) mg->mg_ptr;
51 }
52 0 croak("sv_2thing: can't decode SV=0x%x", origsv);
53 0 return 0;
54 }
55  
56 #define MG_WATCHER_CODE ((((unsigned)'e')<<8) + (unsigned)'v')
57  
58 5043 static SV *wrap_watcher(void *ptr, HV *stash, SV *temple) {
59 5043 return wrap_thing(MG_WATCHER_CODE, ptr, stash, temple);
60 }
61  
62 354805 SV *watcher_2sv(pe_watcher *wa) { /**SLOW IS OKAY**/
63 assert(!WaDESTROYED(wa));
64 354805 ▶ if (!wa->mysv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!wa->mysv) { 0 354805 354805
65 0 wa->mysv = wrap_watcher(wa, wa->vtbl->stash, 0);
66 if (WaDEBUGx(wa) >= 4) {
67 STRLEN n_a;
68 warn("Watcher=0x%x '%s' wrapped with SV=0x%x",
69 wa, SvPV(wa->desc, n_a), SvRV(wa->mysv));
70 }
71 }
72 354805 return SvREFCNT_inc(sv_2mortal(wa->mysv));
73 }
74  
75 390029 void* sv_2watcher(SV *sv) {
76 390029 return sv_2thing(MG_WATCHER_CODE, sv);
77 }
78  
79 #define MG_GENERICSRC_CODE 2422 /* randomly chosen */
80  
81 1 static SV *wrap_genericsrc(void *ptr, HV *stash, SV *temple) {
82 1 return wrap_thing(MG_GENERICSRC_CODE, ptr, stash, temple);
83 }
84  
85 static HV *pe_genericsrc_stash;
86  
87 1 static SV *genericsrc_2sv(pe_genericsrc *src) { /**SLOW IS OKAY**/
88 1 ▶ if (!src->mysv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!src->mysv) { 0 1 1
89 0 src->mysv = wrap_genericsrc(src, pe_genericsrc_stash, 0);
90 }
91 1 return SvREFCNT_inc(sv_2mortal(src->mysv));
92 }
93  
94 10 static void* sv_2genericsrc(SV *sv) {
95 10 return sv_2thing(MG_GENERICSRC_CODE, sv);
96 }
97  
98 /*
99 Events have a short lifetime. mysv is kept alive until the event
100 has been serviced. Once perl finally releases mysv then the event
101 is deallocated (or, more likely, recycled).
102 */
103  
104 345779 SV *event_2sv(pe_event *ev) { /**MAKE FAST**/
105 345779 ▶ if (!ev->mysv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ev->mysv) { 345768 11 345779
106 345768 SV *rv = newSV(0);
107 345768 SV *sv = newSVrv(rv,0);
108 345768 sv_bless(rv, ev->vtbl->stash);
109 345768 sv_setiv(sv, PTR2IV(ev));
110 345768 ev->mysv = rv;
111  
112 if (WaDEBUGx(ev->up) >= 4) {
113 STRLEN n_a;
114 warn("Event=0x%x '%s' wrapped with SV=0x%x",
115 ev, SvPV(ev->up->desc, n_a), SvRV(ev->mysv));
116 }
117 }
118 345779 return SvREFCNT_inc(sv_2mortal(ev->mysv));
119 }
120  
121 1036027 void *sv_2event(SV *sv) {
122 void *ptr;
123 assert(sv);
124 assert(SvROK(sv));
125 1036027 sv = SvRV(sv);
126 1036027 ptr = INT2PTR(void *, SvIV(sv));
127 assert(ptr);
128 1036027 return ptr;
129 }
130  
131 /***************************************************************/
132  
133 #define VERIFYINTERVAL(name, f) \
134 STMT_START { NV ign; sv_2interval(name, f, &ign); } STMT_END
135  
136 140 int sv_2interval(char *label, SV *in, NV *out) {
137 140 SV *sv = in;
138 140 ▶ if (!sv) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!sv) return 0; 0 140 140
139 140 ▶ if (SvGMAGICAL(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvGMAGICAL(sv)) 0 140 140
140 0 mg_get(sv);
141 140 ▶ if (!SvOK(sv)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) return 0; 15 125 140
142 125 ▶ if (SvROK(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) 0 125 125
143 0 sv = SvRV(sv);
144 125 ▶ if (!SvOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) { 0 125 125
145 0 warn("Event: %s interval undef", label);
146 0 *out = 0;
147 125 ▶ } else if (SvNOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvNOK(sv)) { 123 2 125
148 123 *out = SvNVX(sv);
149 2 ▶ } else if (SvIOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvIOK(sv)) { 2 0 2
150 2 *out = SvIVX(sv);
151 0 ▶ } else if (looks_like_number(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (looks_like_number(sv)) { 0 0 0
152 0 *out = SvNV(sv);
153 } else {
154 0 sv_dump(in);
155 0 croak("Event: %s interval must be a number or reference to a number",
156 label);
157 0 return 0;
158 }
159 125 ▶ if (*out < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (*out < 0) { 0 125 125
160 0 warn("Event: %s has negative timeout %.2f (clipped to zero)",
161 label, *out);
162 0 *out = 0;
163 }
164 125 return 1;
165 }
166  
167 344707 SV* events_mask_2sv(int mask) {
168 344707 SV *ret = newSV(0);
169 344707 ▶ (void)SvUPGRADE(ret, SVt_PVIV);
Branch: 1 branch
BranchTrueFalseTotal
(void)SvUPGRADE(ret, SVt_PVIV); 344707 0 344707
170 344707 sv_setpvn(ret, "", 0);
171 344707 ▶ if (mask & PE_R) sv_catpv(ret, "r");
Branch: 1 branch
BranchTrueFalseTotal
if (mask & PE_R) sv_catpv(ret, "r"); 172350 172357 344707
172 344707 ▶ if (mask & PE_W) sv_catpv(ret, "w");
Branch: 1 branch
BranchTrueFalseTotal
if (mask & PE_W) sv_catpv(ret, "w"); 172349 172358 344707
173 344707 ▶ if (mask & PE_E) sv_catpv(ret, "e");
Branch: 1 branch
BranchTrueFalseTotal
if (mask & PE_E) sv_catpv(ret, "e"); 0 344707 344707
174 344707 ▶ if (mask & PE_T) sv_catpv(ret, "t");
Branch: 1 branch
BranchTrueFalseTotal
if (mask & PE_T) sv_catpv(ret, "t"); 7 344700 344707
175 344707 SvIVX(ret) = mask;
176 344707 SvIOK_on(ret);
177 344707 return ret;
178 }
179  
180 8 int sv_2events_mask(SV *sv, int bits) {
181 8 ▶ if (SvPOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(sv)) { 6 2 8
182 6 UV got=0;
183 int xx;
184 STRLEN el;
185 6 char *ep = SvPV(sv,el);
186 12 ▶ for (xx=0; xx < el; xx++) {
Branch: 1 branch
BranchTrueFalseTotal
for (xx=0; xx < el; xx++) { 6 6 12
187 6 switch (ep[xx]) {
188 4 ▶ case 'r': if (bits & PE_R) { got |= PE_R; continue; }
Branch: 1 branch
BranchTrueFalseTotal
case 'r': if (bits & PE_R) { got |= PE_R; continue; } 4 0 4
189 2 ▶ case 'w': if (bits & PE_W) { got |= PE_W; continue; }
Branch: 1 branch
BranchTrueFalseTotal
case 'w': if (bits & PE_W) { got |= PE_W; continue; } 2 0 2
190 0 ▶ case 'e': if (bits & PE_E) { got |= PE_E; continue; }
Branch: 1 branch
BranchTrueFalseTotal
case 'e': if (bits & PE_E) { got |= PE_E; continue; } 0 0 0
191 0 ▶ case 't': if (bits & PE_T) { got |= PE_T; continue; }
Branch: 1 branch
BranchTrueFalseTotal
case 't': if (bits & PE_T) { got |= PE_T; continue; } 0 0 0
192 }
193 0 warn("Ignored '%c' in poll mask", ep[xx]);
194 }
195 6 return got;
196 }
197 2 ▶ else if (SvIOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
else if (SvIOK(sv)) { 2 0 2
198 2 UV extra = SvIVX(sv) & ~bits;
199 2 ▶ if (extra) warn("Ignored extra bits (0x%x) in poll mask", extra);
Branch: 1 branch
BranchTrueFalseTotal
if (extra) warn("Ignored extra bits (0x%x) in poll mask", extra); 0 2 2
200 2 return SvIVX(sv) & bits;
201 }
202 else {
203 0 sv_dump(sv);
204 0 croak("Must be a string /[rwet]/ or bit mask");
205 0 return 0; /* NOTREACHED */
206 }
207 }
« c/watcher.c ↑ Summary c/queue.c »