include/hamt/hamt_ops.h

filter: Statementstmt 100% 103 / 103 Branchbran 86.3% 53 / 66 (4 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 94.6% 156 / 169 (4 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/hamt/hamt_xs.h ↑ Summary xs/set.xs »
1 #ifndef HAMT_OPS_H
2 #define HAMT_OPS_H
3  
4 /* The function forms of the reads: hamt_map_get($m, $k) and its kin. Each is
5 * an ordinary XSUB, and where perl has call checkers (5.14 on) a call to one
6 * that is resolved at compile time is rewritten into a custom op, so the read
7 * costs no sub call. Both routes run the bodies below. */
8  
9 #ifndef PERL_VERSION_GE
10 # define PERL_VERSION_GE(r,v,s) \
11 (PERL_REVISION > (r) || (PERL_REVISION == (r) && \
12 (PERL_VERSION > (v) || (PERL_VERSION == (v) && PERL_SUBVERSION >= (s)))))
13 #endif
14  
15 #if PERL_VERSION_GE(5,14,0)
16 # define HAMT_HAS_OPS 1
17 #else
18 # define HAMT_HAS_OPS 0
19 #endif
20  
21 /* Each function lives in, and is exported by, the class it reads. */
22 static const char *const hamt_fn_names[] = {
23 "hamt_map_get", "hamt_map_exists", "hamt_map_count",
24 "hamt_set_has", "hamt_set_count",
25 "hamt_vector_get", "hamt_vector_count"
26 };
27 #define HAMT_FN_COUNT 7
28  
29 typedef struct { const char *pkg; int from, to; } hamt_fn_group;
30 static const hamt_fn_group hamt_fn_groups[3] = {
31 { "HAMT::Map", 0, 3 },
32 { "HAMT::Set", 3, 5 },
33 { "HAMT::Vector", 5, 7 }
34 };
35 #define HAMT_FN_MAP 0
36 #define HAMT_FN_SET 1
37 #define HAMT_FN_VECTOR 2
38  
39 239 static const char *hamt_fn_pkg(int i) {
40 239 ▶ return hamt_fn_groups[i < 3 ? 0 : i < 5 ? 1 : 2].pkg;
Branch: 2 branches
BranchTrueFalseTotal
return hamt_fn_groups[i < 3 ? 0 : i < 5 ? 1 : 2].pkg; 136 103 239
return hamt_fn_groups[i < 3 ? 0 : i < 5 ? 1 : 2].pkg; 67 69 136
41 }
42  
43 /* The stored scalar itself, mortal, or a fresh undef: as the methods. */
44 37 static SV *hamt_fn_map_get(pTHX_ SV *self, SV *key) {
45 37 hamt_map *m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "hamt_map_get", "map");
46 hamt_key k;
47 hamt_entry *e;
48 32 hamt_key_from_sv(aTHX_ key, &k);
49 31 e = hamt_map_find(m->root, k.p, (size_t)k.len, k.utf8, k.hash);
50 28 ▶ return (e && e->val) ? sv_2mortal(SvREFCNT_inc_simple_NN((SV *)e->val))
Branch: 1 branch
BranchTrueFalseTotal
return (e && e->val) ? sv_2mortal(SvREFCNT_inc_simple_NN((SV *)e->val)) 28 0 28
51 59 ▶ : sv_newmortal();
Branch: 1 branch
BranchTrueFalseTotal
: sv_newmortal(); 28 3 31
52 }
53  
54 7 static SV *hamt_fn_map_exists(pTHX_ SV *self, SV *key) {
55 7 hamt_map *m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "hamt_map_exists", "map");
56 hamt_key k;
57 6 hamt_key_from_sv(aTHX_ key, &k);
58 6 ▶ return boolSV(hamt_map_find(m->root, k.p, (size_t)k.len, k.utf8, k.hash) != NULL);
Branch: 1 branch
BranchTrueFalseTotal
return boolSV(hamt_map_find(m->root, k.p, (size_t)k.len, k.utf8, k.hash) != NULL); 5 1 6
59 }
60  
61 8 static SV *hamt_fn_set_has(pTHX_ SV *self, SV *key) {
62 8 hamt_map *m = hamt_handle(aTHX_ self, &hamt_set_vtbl, "hamt_set_has", "set");
63 hamt_key k;
64 7 hamt_key_from_sv(aTHX_ key, &k);
65 7 ▶ return boolSV(hamt_map_find(m->root, k.p, (size_t)k.len, k.utf8, k.hash) != NULL);
Branch: 1 branch
BranchTrueFalseTotal
return boolSV(hamt_map_find(m->root, k.p, (size_t)k.len, k.utf8, k.hash) != NULL); 6 1 7
66 }
67  
68 18 static SV *hamt_fn_vector_get(pTHX_ SV *self, SV *index) {
69 18 hamt_vec *v = hamt_vec_handle(aTHX_ self, "hamt_vector_get");
70 size_t i;
71 17 return hamt_xs_index(aTHX_ index, v->count, &i)
72 15 ? sv_2mortal(SvREFCNT_inc_simple_NN((SV *)hamt_vec_get(v, i)))
73 32 ▶ : sv_newmortal();
Branch: 1 branch
BranchTrueFalseTotal
: sv_newmortal(); 15 2 17
74 }
75  
76 14 static UV hamt_fn_map_count(pTHX_ SV *self) {
77 14 return (UV)hamt_handle(aTHX_ self, &hamt_map_vtbl, "hamt_map_count", "map")->count;
78 }
79  
80 6 static UV hamt_fn_set_count(pTHX_ SV *self) {
81 6 return (UV)hamt_handle(aTHX_ self, &hamt_set_vtbl, "hamt_set_count", "set")->count;
82 }
83  
84 7 static UV hamt_fn_vector_count(pTHX_ SV *self) {
85 7 return (UV)hamt_vec_handle(aTHX_ self, "hamt_vector_count")->count;
86 }
87  
88 /* Into the caller's package, as a glob assignment of a code reference, which
89 * is what marks the name imported. */
90 15 static void hamt_fn_export(pTHX_ HV *stash, int i) {
91 15 const char *name = hamt_fn_names[i];
92 15 CV *cv = get_cv(form("%s::%s", hamt_fn_pkg(i), name), 0);
93 SV **svp;
94 /*
95 # uncoverable branch true
96 # uncoverable condition left count:1 */
97 15 ▶ if (!cv) croak("HAMT: %s is not built", name);
Branch: 1 branch
BranchTrueFalseTotal
if (!cv) croak("HAMT: %s is not built", name); 0 15 15
98 15 svp = hv_fetch(stash, name, (I32)strlen(name), 1);
99 /*
100 # uncoverable branch true
101 # uncoverable condition left count:1 */
102 15 ▶ if (!svp) croak("HAMT: cannot export %s", name);
Branch: 1 branch
BranchTrueFalseTotal
if (!svp) croak("HAMT: cannot export %s", name); 0 15 15
103 15 ▶ if (SvTYPE(*svp) != SVt_PVGV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(*svp) != SVt_PVGV) 13 2 15
104 13 gv_init((GV *)*svp, stash, name, (I32)strlen(name), GV_ADDMULTI);
105 15 sv_setsv(*svp, sv_2mortal(newRV_inc((SV *)cv)));
106 15 }
107  
108 /* The names asked of one class, or ":all" for every one it has. */
109 58 static void hamt_fn_import(pTHX_ int group, SV **want, I32 n) {
110 58 const hamt_fn_group *g = &hamt_fn_groups[group];
111 58 HV *stash = CopSTASH(PL_curcop);
112 I32 i;
113 int j;
114  
115 66 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 12 54 66
116 12 const char *w = SvPV_nolen(want[i]);
117 12 ▶ if (strEQ(w, ":all")) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(w, ":all")) { 5 7 12
118 17 ▶ for (j = g->from; j < g->to; j++) hamt_fn_export(aTHX_ stash, j);
Branch: 1 branch
BranchTrueFalseTotal
for (j = g->from; j < g->to; j++) hamt_fn_export(aTHX_ stash, j); 12 5 17
119 5 continue;
120 }
121 18 ▶ for (j = g->from; j < g->to; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = g->from; j < g->to; j++) 14 4 18
122 14 ▶ if (strEQ(w, hamt_fn_names[j])) break;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(w, hamt_fn_names[j])) break; 3 11 14
123 7 ▶ if (j == g->to) croak("%s does not export \"%s\"", g->pkg, w);
Branch: 1 branch
BranchTrueFalseTotal
if (j == g->to) croak("%s does not export \"%s\"", g->pkg, w); 4 3 7
124 3 hamt_fn_export(aTHX_ stash, j);
125 }
126 54 }
127  
128 #if HAMT_HAS_OPS
129  
130 #ifndef OpHAS_SIBLING
131 # define OpHAS_SIBLING(o) ((o)->op_sibling != NULL)
132 #endif
133 #ifndef OpSIBLING
134 # define OpSIBLING(o) ((o)->op_sibling)
135 #endif
136 #ifndef OpMORESIB_set
137 # define OpMORESIB_set(o, sib) ((o)->op_sibling = (sib))
138 #endif
139 #ifndef OpLASTSIB_set
140 # define OpLASTSIB_set(o, parent) ((o)->op_sibling = NULL)
141 #endif
142  
143 /* The key may be an object that stringifies by running Perl. perl runs
144 * overloading and tie methods on a stack of their own, so this one does not
145 * move; the pointer is put back and read again all the same, which costs
146 * two moves and does not depend on that staying true. */
147 #define HAMT_PP_KEYED(pp, fn) \
148 static OP *pp(pTHX) { \
149 dSP; \
150 SV *key = POPs; \
151 SV *self = TOPs; \
152 SV *r; \
153 PUTBACK; \
154 r = fn(aTHX_ self, key); \
155 SPAGAIN; \
156 SETs(r); \
157 RETURN; \
158 }
159  
160 /* A count is written into a scalar the op owns where perl lets an extension
161 * have one, and into a temporary where it does not. pad_alloc is not
162 * exported by 5.14; 5.20 is the oldest perl this has been built on with it. */
163 #if PERL_VERSION_GE(5,20,0)
164 # define HAMT_OP_TARG(o) ((o)->op_targ = pad_alloc(OP_CUSTOM, SVs_PADTMP))
165 # define HAMT_PP_COUNT(pp, fn) \
166 static OP *pp(pTHX) { \
167 dSP; \
168 dTARGET; \
169 SV *self = TOPs; \
170 sv_setuv(TARG, fn(aTHX_ self)); \
171 SvSETMAGIC(TARG); \
172 SETs(TARG); \
173 RETURN; \
174 }
175 #else
176 # define HAMT_OP_TARG(o) ((void)0)
177 # define HAMT_PP_COUNT(pp, fn) \
178 static OP *pp(pTHX) { \
179 dSP; \
180 SV *self = TOPs; \
181 SETs(sv_2mortal(newSVuv(fn(aTHX_ self)))); \
182 RETURN; \
183 }
184 #endif
185  
186 31 HAMT_PP_KEYED(hamt_pp_map_get, hamt_fn_map_get)
187 4 HAMT_PP_KEYED(hamt_pp_map_exists, hamt_fn_map_exists)
188 5 HAMT_PP_KEYED(hamt_pp_set_has, hamt_fn_set_has)
189 15 HAMT_PP_KEYED(hamt_pp_vector_get, hamt_fn_vector_get)
190 12 ▶HAMT_PP_COUNT(hamt_pp_map_count, hamt_fn_map_count)
Branch: 1 branch
BranchTrueFalseTotal
HAMT_PP_COUNT(hamt_pp_map_count, hamt_fn_map_count) 0 10 10
191 4 ▶HAMT_PP_COUNT(hamt_pp_set_count, hamt_fn_set_count)
Branch: 1 branch
BranchTrueFalseTotal
HAMT_PP_COUNT(hamt_pp_set_count, hamt_fn_set_count) 0 3 3
192 5 ▶HAMT_PP_COUNT(hamt_pp_vector_count, hamt_fn_vector_count)
Branch: 1 branch
BranchTrueFalseTotal
HAMT_PP_COUNT(hamt_pp_vector_count, hamt_fn_vector_count) 0 4 4
193  
194 /* The prototype is applied first, so the arguments are counted and in scalar
195 * context whatever they are. Then they are lifted out of the call and made
196 * the children of the op. Anything not shaped as expected is left a call. */
197 83 static OP *hamt_ck_rewrite(pTHX_ OP *o, GV *namegv, SV *ckobj,
198 Perl_ppaddr_t pp, int nargs) {
199 83 OP *pushop, *a, *b = NULL, *cvop, *newop;
200  
201 83 o = ck_entersub_args_proto(o, namegv, ckobj);
202 83 pushop = cUNOPx(o)->op_first;
203 /*
204 # uncoverable branch false class:ignore_covered_err
205 # uncoverable condition right count:1 */
206 83 ▶ if (!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first;
Branch: 1 branch
BranchTrueFalseTotal
if (!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first; 83 0 83
207 83 ▶ a = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
a = OpSIBLING(pushop); 83 0 83
208 83 ▶ if (!a || !OpHAS_SIBLING(a)) return o; /* # uncoverable condition left count:1 */
Branch: 2 branches
BranchTrueFalseTotal
if (!a || !OpHAS_SIBLING(a)) return o; /* # uncoverable condition left count:1 */ 83 0 83
if (!a || !OpHAS_SIBLING(a)) return o; /* # uncoverable condition left count:1 */ 1 82 83
209 82 ▶ if (nargs == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (nargs == 2) { 59 23 82
210 59 ▶ b = OpSIBLING(a);
Branch: 1 branch
BranchTrueFalseTotal
b = OpSIBLING(a); 59 0 59
211 59 ▶ if (!OpHAS_SIBLING(b)) return o;
Branch: 1 branch
BranchTrueFalseTotal
if (!OpHAS_SIBLING(b)) return o; 1 58 59
212 58 ▶ cvop = OpSIBLING(b);
Branch: 1 branch
BranchTrueFalseTotal
cvop = OpSIBLING(b); 58 0 58
213 }
214 else {
215 23 ▶ cvop = OpSIBLING(a);
Branch: 1 branch
BranchTrueFalseTotal
cvop = OpSIBLING(a); 23 0 23
216 }
217 81 ▶ if (OpHAS_SIBLING(cvop)) return o;
Branch: 1 branch
BranchTrueFalseTotal
if (OpHAS_SIBLING(cvop)) return o; 2 79 81
218  
219 79 OpMORESIB_set(pushop, cvop);
220 79 ▶ if (b) OpLASTSIB_set(b, NULL);
Branch: 1 branch
BranchTrueFalseTotal
if (b) OpLASTSIB_set(b, NULL); 57 22 79
221 79 OpLASTSIB_set(a, NULL);
222 79 ▶ newop = b ? newBINOP(OP_NULL, 0, a, b) : newUNOP(OP_NULL, 0, a);
Branch: 1 branch
BranchTrueFalseTotal
newop = b ? newBINOP(OP_NULL, 0, a, b) : newUNOP(OP_NULL, 0, a); 57 22 79
223 79 newop->op_type = OP_CUSTOM;
224 79 newop->op_ppaddr = pp;
225 79 ▶ if (!b) HAMT_OP_TARG(newop);
Branch: 1 branch
BranchTrueFalseTotal
if (!b) HAMT_OP_TARG(newop); 22 57 79
226 79 op_free(o);
227 79 return newop;
228 }
229  
230 #define HAMT_CK(ck, pp, nargs) \
231 static OP *ck(pTHX_ OP *o, GV *namegv, SV *ckobj) { \
232 return hamt_ck_rewrite(aTHX_ o, namegv, ckobj, pp, nargs); \
233 }
234  
235 34 HAMT_CK(hamt_ck_map_get, hamt_pp_map_get, 2)
236 5 HAMT_CK(hamt_ck_map_exists, hamt_pp_map_exists, 2)
237 6 HAMT_CK(hamt_ck_set_has, hamt_pp_set_has, 2)
238 14 HAMT_CK(hamt_ck_vector_get, hamt_pp_vector_get, 2)
239 13 HAMT_CK(hamt_ck_map_count, hamt_pp_map_count, 1)
240 5 HAMT_CK(hamt_ck_set_count, hamt_pp_set_count, 1)
241 6 HAMT_CK(hamt_ck_vector_count, hamt_pp_vector_count, 1)
242  
243 static XOP hamt_xop[HAMT_FN_COUNT];
244  
245 224 static void hamt_ops_one(pTHX_ int i, Perl_ppaddr_t pp, Perl_call_checker ck, U32 cls) {
246 224 CV *cv = get_cv(form("%s::%s", hamt_fn_pkg(i), hamt_fn_names[i]), 0);
247 224 XopENTRY_set(&hamt_xop[i], xop_name, hamt_fn_names[i]);
248 224 XopENTRY_set(&hamt_xop[i], xop_desc, hamt_fn_names[i]);
249 224 XopENTRY_set(&hamt_xop[i], xop_class, cls);
250 224 Perl_custom_op_register(aTHX_ pp, &hamt_xop[i]);
251 /*
252 # uncoverable branch false
253 # uncoverable condition right count:1 */
254 224 ▶ if (cv) cv_set_call_checker(cv, ck, (SV *)cv);
Branch: 1 branch
BranchTrueFalseTotal
if (cv) cv_set_call_checker(cv, ck, (SV *)cv); 224 0 224
255 224 }
256  
257 #endif
258  
259 /* Once, after the XSUBs exist. */
260 32 static void hamt_ops_boot(pTHX) {
261 #if HAMT_HAS_OPS
262 32 hamt_ops_one(aTHX_ 0, hamt_pp_map_get, hamt_ck_map_get, OA_BINOP);
263 32 hamt_ops_one(aTHX_ 1, hamt_pp_map_exists, hamt_ck_map_exists, OA_BINOP);
264 32 hamt_ops_one(aTHX_ 2, hamt_pp_map_count, hamt_ck_map_count, OA_UNOP);
265 32 hamt_ops_one(aTHX_ 3, hamt_pp_set_has, hamt_ck_set_has, OA_BINOP);
266 32 hamt_ops_one(aTHX_ 4, hamt_pp_set_count, hamt_ck_set_count, OA_UNOP);
267 32 hamt_ops_one(aTHX_ 5, hamt_pp_vector_get, hamt_ck_vector_get, OA_BINOP);
268 32 hamt_ops_one(aTHX_ 6, hamt_pp_vector_count, hamt_ck_vector_count, OA_UNOP);
269 #else
270 PERL_UNUSED_CONTEXT;
271 #endif
272 32 }
273  
274 #endif
« include/hamt/hamt_xs.h ↑ Summary xs/set.xs »