src/sorting.c

filter: Statementstmt 78.7% 26 / 33 Branchbran 42.8% 12 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 62.3% 38 / 61 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« XS.xs ↑ Summary src/Array.c »
1 typedef struct {
2 #ifdef USE_ITHREADS
3 PerlInterpreter *my_perl;
4 #endif
5 CV *callback; /* can be NULL */
6 SV *err;
7 } sort_ctx_t;
8  
9 static int
10 18 shvxs_sort_cmp(const void *a, const void *b, void *ctx)
11 {
12 18 sort_ctx_t *c = (sort_ctx_t *)ctx;
13 #ifdef USE_ITHREADS
14 dTHXa(c->my_perl);
15 #endif
16  
17 18 ▶ if (c->err) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (c->err) return 0; 0 18 18
18  
19 18 SV *sv_a = *(SV * const *)a;
20 18 SV *sv_b = *(SV * const *)b;
21  
22 18 ▶ if (!c->callback) {
Branch: 1 branch
BranchTrueFalseTotal
if (!c->callback) { 5 13 18
23 /* default string comparison */
24 5 return sv_cmp(sv_a, sv_b);
25 }
26  
27 13 int result = 0;
28
29 13 dSP;
30 13 ENTER;
31 13 SAVETMPS;
32  
33 13 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 13 13
34 13 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 13 13
35 13 ▶ XPUSHs(sv_a);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_a); 0 13 13
36 13 ▶ XPUSHs(sv_b);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_b); 0 13 13
37 13 PUTBACK;
38  
39 13 I32 count = call_sv((SV *)c->callback, G_SCALAR | G_EVAL);
40 13 SPAGAIN;
41  
42 13 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 13 0 13
if (SvTRUE(ERRSV)) { 0 13 13
43 0 ▶ c->err = newSVsv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
c->err = newSVsv(ERRSV); 0 0 0
44 0 ▶ sv_setsv(ERRSV, &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
sv_setsv(ERRSV, &PL_sv_undef); 0 0 0
45 0 PUTBACK;
46 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
47 0 LEAVE;
48 0 return 0;
49 }
50  
51 13 ▶ SV *ret = (count > 0) ? POPs : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *ret = (count > 0) ? POPs : &PL_sv_undef; 13 0 13
52 13 ▶ if (!SvOK(ret)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(ret)) { 0 13 13
53 0 result = 0;
54 }
55 else {
56 13 result = (int)SvIV(ret);
57 }
58  
59 13 PUTBACK;
60 13 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 13 0 13
61 13 LEAVE;
62  
63 13 return result;
64 }
65  
66 #if defined(_WIN32)
67  
68 static int __cdecl
69 shvxs_sort_cmp_win32(void *ctx, const void *a, const void *b) {
70 sort_ctx_t *c = (sort_ctx_t *)ctx;
71 return shvxs_sort_cmp(a, b, ctx);
72 }
73  
74 #define SHVXS_QSORT(base, n, size, ctx) qsort_s((base), (n), (size), shvxs_sort_cmp_win32, (ctx))
75  
76 #elif defined(__APPLE__) || defined(__FreeBSD__)
77  
78 static int
79 shvxs_sort_cmp_bsd(void *ctx, const void *a, const void *b) {
80 sort_ctx_t *c = (sort_ctx_t *)ctx;
81 return shvxs_sort_cmp(a, b, ctx);
82 }
83  
84 #define SHVXS_QSORT(base, n, size, ctx) qsort_r((base), (n), (size), (ctx), shvxs_sort_cmp_bsd)
85  
86 #else
87  
88 #define SHVXS_QSORT(base, n, size, ctx) qsort_r((base), (n), (size), shvxs_sort_cmp, (ctx))
89  
90 #endif
« XS.xs ↑ Summary src/Array.c »