ScoreBoard.xs

filter: Statementstmt 80.7% 84 / 104 Branchbran 57.6% 60 / 104 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 69.2% 144 / 208 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/IPC/ScoreBoard.pm ↑ Summary
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include "ppport.h"
6  
7 # undef HAVE_ATOMICS
8  
9 # ifdef __GNUC__
10 # define GCC_V (10000*__GNUC__ + 100*__GNUC_MINOR__ + __GNUC_PATCHLEVEL__)
11  
12 # if GCC_V >= 30000
13 # define expect(expr,value) __builtin_expect ((expr),(value))
14 # define INLINE static inline
15 # endif
16  
17 # if GCC_V >= 40102
18 # pragma message "GCC Version >= 40102: using atomic builtins"
19 # define HAVE_ATOMICS 1
20 # define atomic_add(ptr, v) __sync_add_and_fetch((ptr), (v))
21 # define atomic_sub(ptr, v) __sync_sub_and_fetch((ptr), (v))
22 # endif
23 # endif
24  
25 # include "config.h"
26  
27 # ifndef expect
28 # define expect(expr,value) (expr)
29 # endif
30  
31 # ifndef INLINE
32 # define INLINE static
33 # endif
34  
35 # ifndef atomic_add
36 # define atomic_add(ptr, v) (*(ptr)+=(v))
37 # endif
38  
39 # ifndef atomic_sub
40 # define atomic_sub(ptr, v) (*(ptr)-=(v))
41 # endif
42  
43 # ifndef HAVE_ATOMICS
44 # define HAVE_ATOMICS 0
45 # endif
46  
47 #define expect_false(expr) expect ((expr) != 0, 0)
48 #define expect_true(expr) expect ((expr) != 0, 1)
49  
50 typedef volatile IV vIV;
51 typedef volatile UV vUV;
52  
53 struct hdr {
54 union {
55 vUV iv;
56 char *c;
57 } magic;
58 vUV how_many;
59 vUV slotsize;
60 vUV extra;
61 };
62  
63 204 INLINE vIV* get_slot( SV *sb, UV slot, UV *slotsize ) {
64 204 ▶ if( expect_true((sb && SvROK(sb))) ) {
Branch: 3 branches
BranchTrueFalseTotal
if( expect_true((sb && SvROK(sb))) ) { 204 0 204
if( expect_true((sb && SvROK(sb))) ) { 204 0 204
if( expect_true((sb && SvROK(sb))) ) { 204 0 204
65 204 struct hdr *hdr=(struct hdr *)SvPV_nolen(SvRV(sb));
66 204 vIV *data=(vIV*)(sizeof(*hdr)+(char*)hdr);
67 204 ▶ if( expect_true(slot<hdr->how_many) ) {
Branch: 1 branch
BranchTrueFalseTotal
if( expect_true(slot<hdr->how_many) ) { 204 0 204
68 204 ▶ if( expect_true(slotsize) ) *slotsize=hdr->slotsize;
Branch: 1 branch
BranchTrueFalseTotal
if( expect_true(slotsize) ) *slotsize=hdr->slotsize; 204 0 204
69 204 return &data[slot*hdr->slotsize];
70 } else {
71 0 croak("slot number out of range");
72 }
73 } else {
74 0 croak("invalid scoreboard parameter");
75 }
76 }
77  
78 45 INLINE IV sum( SV *sb, UV idx ) {
79 45 ▶ if( expect_true((sb && SvROK(sb))) ) {
Branch: 3 branches
BranchTrueFalseTotal
if( expect_true((sb && SvROK(sb))) ) { 45 0 45
if( expect_true((sb && SvROK(sb))) ) { 45 0 45
if( expect_true((sb && SvROK(sb))) ) { 45 0 45
80 45 struct hdr *hdr=(struct hdr *)SvPV_nolen(SvRV(sb));
81 45 vIV *data=(vIV*)(sizeof(*hdr)+(char*)hdr);
82 45 IV res=0;
83 UV i;
84 45 ▶ if( expect_true(idx<hdr->slotsize) ) {
Branch: 1 branch
BranchTrueFalseTotal
if( expect_true(idx<hdr->slotsize) ) { 45 0 45
85 180 ▶ for( i=0; i<hdr->how_many; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for( i=0; i<hdr->how_many; i++ ) { 135 45 180
86 135 res+=data[i*hdr->slotsize+idx];
87 }
88 45 return res;
89 } else {
90 0 croak("index within slot out of range");
91 }
92 } else {
93 0 croak("invalid scoreboard parameter");
94 }
95 }
96  
97 31 INLINE vIV* get_extra( SV *sb, UV *slotsize ) {
98 31 ▶ if( expect_true((sb && SvROK(sb))) ) {
Branch: 3 branches
BranchTrueFalseTotal
if( expect_true((sb && SvROK(sb))) ) { 31 0 31
if( expect_true((sb && SvROK(sb))) ) { 31 0 31
if( expect_true((sb && SvROK(sb))) ) { 31 0 31
99 31 struct hdr *hdr=(struct hdr *)SvPV_nolen(SvRV(sb));
100 31 vIV *data=(vIV*)(sizeof(*hdr)+(char*)hdr);
101 31 ▶ if( expect_true(slotsize) ) *slotsize=hdr->extra;
Branch: 1 branch
BranchTrueFalseTotal
if( expect_true(slotsize) ) *slotsize=hdr->extra; 31 0 31
102 31 return &data[hdr->how_many*hdr->slotsize];
103 } else {
104 0 croak("invalid scoreboard parameter");
105 }
106 }
107  
108 MODULE = IPC::ScoreBoard PACKAGE = IPC::ScoreBoard
109  
110 PROTOTYPES: ENABLE
111  
112 IV
113 nslots(sb)
114 SV* sb
115 PROTOTYPE: $
116 CODE:
117 14 ▶ if( expect_true((sb && SvROK(sb))) ) {
Branch: 3 branches
BranchTrueFalseTotal
if( expect_true((sb && SvROK(sb))) ) { 14 0 14
if( expect_true((sb && SvROK(sb))) ) { 14 0 14
if( expect_true((sb && SvROK(sb))) ) { 14 0 14
118 14 struct hdr *hdr=(struct hdr *)SvPV_nolen(SvRV(sb));
119 14 RETVAL=hdr->how_many;
120 } else {
121 0 croak("invalid scoreboard parameter");
122 }
123 OUTPUT:
124 RETVAL
125  
126 IV
127 slotsize(sb)
128 SV* sb
129 PROTOTYPE: $
130 CODE:
131 14 ▶ if( expect_true((sb && SvROK(sb))) ) {
Branch: 3 branches
BranchTrueFalseTotal
if( expect_true((sb && SvROK(sb))) ) { 14 0 14
if( expect_true((sb && SvROK(sb))) ) { 14 0 14
if( expect_true((sb && SvROK(sb))) ) { 14 0 14
132 14 struct hdr *hdr=(struct hdr *)SvPV_nolen(SvRV(sb));
133 14 RETVAL=hdr->slotsize;
134 } else {
135 0 croak("invalid scoreboard parameter");
136 }
137 OUTPUT:
138 RETVAL
139  
140 IV
141 nextra(sb)
142 SV* sb
143 PROTOTYPE: $
144 CODE:
145 65 ▶ if( expect_true((sb && SvROK(sb))) ) {
Branch: 3 branches
BranchTrueFalseTotal
if( expect_true((sb && SvROK(sb))) ) { 65 0 65
if( expect_true((sb && SvROK(sb))) ) { 65 0 65
if( expect_true((sb && SvROK(sb))) ) { 65 0 65
146 65 struct hdr *hdr=(struct hdr *)SvPV_nolen(SvRV(sb));
147 65 RETVAL=hdr->extra;
148 } else {
149 0 croak("invalid scoreboard parameter");
150 }
151 OUTPUT:
152 RETVAL
153  
154 IV
155 get(sb, slot, idx=0)
156 SV* sb
157 UV slot
158 UV idx
159 PROTOTYPE: $$;$
160 INIT:
161 UV sz;
162 135 vIV *slotptr=get_slot(sb, slot, &sz);
163 CODE:
164 135 ▶ if( expect_true(idx<sz) ) {
Branch: 1 branch
BranchTrueFalseTotal
if( expect_true(idx<sz) ) { 135 0 135
165 135 RETVAL=slotptr[idx];
166 } else {
167 0 croak("index within slot out of range");
168 }
169 OUTPUT:
170 RETVAL
171  
172 IV
173 set(sb, slot, idx, val)
174 SV* sb
175 UV slot
176 UV idx
177 IV val
178 PROTOTYPE: $$$$
179 INIT:
180 UV sz;
181 6 vIV *slotptr=get_slot(sb, slot, &sz);
182 CODE:
183 6 ▶ if( expect_true(idx<sz) ) {
Branch: 1 branch
BranchTrueFalseTotal
if( expect_true(idx<sz) ) { 6 0 6
184 6 RETVAL=slotptr[idx]=val;
185 } else {
186 0 croak("index within slot out of range");
187 }
188 OUTPUT:
189 RETVAL
190  
191 IV
192 incr(sb, slot, idx, amount=1)
193 SV* sb
194 UV slot
195 UV idx
196 IV amount
197 PROTOTYPE: $$$;$
198 INIT:
199 UV sz;
200 18 vIV *slotptr=get_slot(sb, slot, &sz);
201 CODE:
202 18 ▶ if( expect_true(idx<sz) ) {
Branch: 1 branch
BranchTrueFalseTotal
if( expect_true(idx<sz) ) { 18 0 18
203 18 RETVAL=atomic_add(&slotptr[idx], amount);
204 } else {
205 0 croak("index within slot out of range");
206 }
207 OUTPUT:
208 RETVAL
209  
210 IV
211 decr(sb, slot, idx, amount=1)
212 SV* sb
213 UV slot
214 UV idx
215 IV amount
216 PROTOTYPE: $$$;$
217 INIT:
218 UV sz;
219 18 vIV *slotptr=get_slot(sb, slot, &sz);
220 CODE:
221 18 ▶ if( expect_true(idx<sz) ) {
Branch: 1 branch
BranchTrueFalseTotal
if( expect_true(idx<sz) ) { 18 0 18
222 18 RETVAL=atomic_sub(&slotptr[idx], amount);
223 } else {
224 0 croak("index within slot out of range");
225 }
226 OUTPUT:
227 RETVAL
228  
229 IV
230 sum(sb, idx)
231 SV* sb
232 UV idx
233 PROTOTYPE: $$
234 CODE:
235 45 RETVAL=sum(sb, idx);
236 OUTPUT:
237 RETVAL
238  
239 void
240 get_all(sb, slot)
241 SV* sb
242 UV slot
243 PROTOTYPE: $$
244 INIT:
245 UV sz, i;
246 27 vIV *slotptr=get_slot(sb, slot, &sz);
247 PPCODE:
248 27 ▶ EXTEND(SP, sz);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, sz); 0 27 27
249 162 ▶ for( i=0; i<sz; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for( i=0; i<sz; i++ ) { 135 27 162
250 135 mPUSHi(slotptr[i]);
251 }
252  
253 void
254 sum_all(sb)
255 SV* sb
256 PROTOTYPE: $
257 PPCODE:
258 9 ▶ if( expect_true((sb && SvROK(sb))) ) {
Branch: 3 branches
BranchTrueFalseTotal
if( expect_true((sb && SvROK(sb))) ) { 9 0 9
if( expect_true((sb && SvROK(sb))) ) { 9 0 9
if( expect_true((sb && SvROK(sb))) ) { 9 0 9
259 9 struct hdr *hdr=(struct hdr *)SvPV_nolen(SvRV(sb));
260 9 vIV *data=(vIV*)(sizeof(*hdr)+(char*)hdr);
261 UV i, j;
262 9 ▶ EXTEND(SP, hdr->slotsize);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, hdr->slotsize); 0 9 9
263 54 ▶ for( j=0; j<hdr->slotsize; j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for( j=0; j<hdr->slotsize; j++ ) { 45 9 54
264 45 mPUSHi(0);
265 }
266 36 ▶ for( i=0; i<hdr->how_many; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for( i=0; i<hdr->how_many; i++ ) { 27 9 36
267 162 ▶ for( j=0; j<hdr->slotsize; j++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for( j=0; j<hdr->slotsize; j++ ) { 135 27 162
268 135 SvIV_set(ST(j), SvIVX(ST(j))+data[i*hdr->slotsize+j]);
269 }
270 }
271 } else {
272 0 croak("invalid scoreboard parameter");
273 }
274  
275 IV
276 get_extra(sb, idx=0)
277 SV* sb
278 UV idx
279 PROTOTYPE: $;$
280 INIT:
281 UV sz;
282 6 vIV *slotptr=get_extra(sb, &sz);
283 CODE:
284 6 ▶ if( expect_true(idx<sz) ) {
Branch: 1 branch
BranchTrueFalseTotal
if( expect_true(idx<sz) ) { 6 0 6
285 6 RETVAL=slotptr[idx];
286 } else {
287 0 croak("index within extra slot out of range");
288 }
289 OUTPUT:
290 RETVAL
291  
292 IV
293 set_extra(sb, idx, val)
294 SV* sb
295 UV idx
296 IV val
297 PROTOTYPE: $$$
298 INIT:
299 UV sz;
300 2 vIV *slotptr=get_extra(sb, &sz);
301 CODE:
302 2 ▶ if( expect_true(idx<sz) ) {
Branch: 1 branch
BranchTrueFalseTotal
if( expect_true(idx<sz) ) { 2 0 2
303 2 RETVAL=slotptr[idx]=val;
304 } else {
305 0 croak("index within extra slot out of range");
306 }
307 OUTPUT:
308 RETVAL
309  
310 IV
311 incr_extra(sb, idx, amount=1)
312 SV* sb
313 UV idx
314 IV amount
315 PROTOTYPE: $$;$
316 INIT:
317 UV sz;
318 15 vIV *slotptr=get_extra(sb, &sz);
319 CODE:
320 15 ▶ if( expect_true(idx<sz) ) {
Branch: 1 branch
BranchTrueFalseTotal
if( expect_true(idx<sz) ) { 15 0 15
321 15 RETVAL=atomic_add(&slotptr[idx], amount);
322 } else {
323 0 croak("index within extra slot out of range");
324 }
325 OUTPUT:
326 RETVAL
327  
328 IV
329 decr_extra(sb, idx, amount=1)
330 SV* sb
331 UV idx
332 IV amount
333 PROTOTYPE: $$;$
334 INIT:
335 UV sz;
336 6 vIV *slotptr=get_extra(sb, &sz);
337 CODE:
338 6 ▶ if( expect_true(idx<sz) ) {
Branch: 1 branch
BranchTrueFalseTotal
if( expect_true(idx<sz) ) { 6 0 6
339 6 RETVAL=atomic_sub(&slotptr[idx], amount);
340 } else {
341 0 croak("index within extra slot out of range");
342 }
343 OUTPUT:
344 RETVAL
345  
346 void
347 get_all_extra(sb)
348 SV* sb
349 PROTOTYPE: $
350 INIT:
351 UV sz, i;
352 2 vIV *slotptr=get_extra(sb, &sz);
353 PPCODE:
354 2 ▶ EXTEND(SP, sz);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, sz); 0 2 2
355 18 ▶ for( i=0; i<sz; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for( i=0; i<sz; i++ ) { 16 2 18
356 16 mPUSHi(slotptr[i]);
357 }
358  
359 IV
360 have_atomics()
361 PROTOTYPE:
362 CODE:
363 2 ▶ RETVAL=HAVE_ATOMICS;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL=HAVE_ATOMICS; 0 2 2
364 OUTPUT:
365 RETVAL
366  
367  
368 IV
369 offset_of(sb, slot, idx=0)
370 SV* sb
371 UV slot
372 UV idx
373 PROTOTYPE: $$;$
374 CODE:
375 10 RETVAL=-1;
376 10 ▶ if( expect_true((sb && SvROK(sb))) ) {
Branch: 3 branches
BranchTrueFalseTotal
if( expect_true((sb && SvROK(sb))) ) { 10 0 10
if( expect_true((sb && SvROK(sb))) ) { 10 0 10
if( expect_true((sb && SvROK(sb))) ) { 10 0 10
377 10 struct hdr *hdr=(struct hdr *)SvPV_nolen(SvRV(sb));
378 10 IV *data=(IV*)(sizeof(*hdr)+(char*)hdr);
379  
380 10 ▶ if( expect_true(items>2 && SvOK(ST(2))) ) {
Branch: 3 branches
BranchTrueFalseTotal
if( expect_true(items>2 && SvOK(ST(2))) ) { 6 4 10
if( expect_true(items>2 && SvOK(ST(2))) ) { 6 0 6
if( expect_true(items>2 && SvOK(ST(2))) ) { 6 4 10
381 /* fetch slot offset */
382 6 ▶ if( expect_true(slot<hdr->how_many) ) {
Branch: 1 branch
BranchTrueFalseTotal
if( expect_true(slot<hdr->how_many) ) { 6 0 6
383 6 ▶ if( expect_true(idx<hdr->slotsize) ) {
Branch: 1 branch
BranchTrueFalseTotal
if( expect_true(idx<hdr->slotsize) ) { 6 0 6
384 6 RETVAL=(char*)&data[slot*hdr->slotsize+idx] - (char*)hdr;
385 } else {
386 0 croak("index within extra slot out of range");
387 }
388 } else {
389 0 croak("slot number out of range");
390 }
391 } else {
392 /* fetch extra offset */
393 4 ▶ if( expect_true(slot<hdr->extra) ) {
Branch: 1 branch
BranchTrueFalseTotal
if( expect_true(slot<hdr->extra) ) { 4 0 4
394 4 RETVAL=(char*)&data[hdr->how_many*hdr->slotsize+slot] - (char*)hdr;
395 } else {
396 0 croak("index within extra slot out of range");
397 }
398 }
399 }
400 OUTPUT:
401 RETVAL
402  
403 ## Local Variables:
404 ## mode: C
405 ## End:
« blib/lib/IPC/ScoreBoard.pm ↑ Summary