| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| #include "ppport.h" |
| 6 |
|
| #include "phash.h" |
| 7 |
|
| #include "chd.h" |
| 8 |
|
| |
| 9 |
|
| #define BEXTRACT(sv) \ |
| 10 |
|
| if (!sv_isobject(sv) || !sv_derived_from(sv, "Data::PerfectHash::Shared::Builder")) \ |
| 11 |
|
| croak("Expected a Data::PerfectHash::Shared::Builder"); \ |
| 12 |
|
| PhBuilder *b = INT2PTR(PhBuilder*, SvIV(SvRV(sv))); \ |
| 13 |
|
| if (!b) croak("Builder used after build/destroy"); \ |
| 14 |
|
| if (b->type_tag != PH_BUILDER_TAG) croak("not a Data::PerfectHash::Shared::Builder handle"); \ |
| 15 |
|
| PhBuilder *b0 = b; PERL_UNUSED_VAR(b0); \ |
| 16 |
|
| sv_2mortal(SvREFCNT_inc(SvRV(sv))) |
| 17 |
|
| #define BREEXTRACT(sv) \ |
| 18 |
|
| if (!SvROK(sv)) croak("Builder replaced during the call"); \ |
| 19 |
|
| b = INT2PTR(PhBuilder*, SvIV(SvRV(sv))); \ |
| 20 |
|
| if (b != b0) croak("Builder replaced or destroyed during the call") |
| 21 |
|
| |
| 22 |
|
| #define SEXTRACT(sv) \ |
| 23 |
|
| if (!sv_isobject(sv) || !sv_derived_from(sv, "Data::PerfectHash::Shared")) \ |
| 24 |
|
| croak("Expected a Data::PerfectHash::Shared object"); \ |
| 25 |
|
| PhSet *s = INT2PTR(PhSet*, SvIV(SvRV(sv))); \ |
| 26 |
|
| if (!s) croak("Set used after destroy"); \ |
| 27 |
|
| if (s->type_tag != PH_SET_TAG) croak("not a Data::PerfectHash::Shared handle"); \ |
| 28 |
|
| PhSet *s0 = s; PERL_UNUSED_VAR(s0); \ |
| 29 |
|
| sv_2mortal(SvREFCNT_inc(SvRV(sv))) |
| 30 |
|
| #define SREEXTRACT(sv) \ |
| 31 |
|
| if (!SvROK(sv)) croak("Set replaced during the call"); \ |
| 32 |
|
| s = INT2PTR(PhSet*, SvIV(SvRV(sv))); \ |
| 33 |
|
| if (s != s0) croak("Set replaced or destroyed during the call") |
| 34 |
|
| |
| 35 |
100 |
| static SV *ph_mkobj(pTHX_ void *p, const char *cls) { |
| 36 |
100 |
| SV *rv = newRV_noinc(newSViv(PTR2IV(p))); |
| 37 |
100 |
| return sv_bless(rv, gv_stashpv(cls, GV_ADD)); |
| 38 |
|
| } |
| 39 |
|
| |
| 40 |
|
| /* Save-stack destructor for a transient builder (build_int/build_str): runs on |
| 41 |
|
| * BOTH normal scope exit AND croak-unwind, so a croak mid-collect (wide-char |
| 42 |
|
| * SvPVbyte, dying tied element, OOM) can't longjmp past the free and leak the builder. */ |
| 43 |
25 |
| static void ph_builder_free_x(pTHX_ void *p) { ph_builder_free((PhBuilder*)p); } |
| 44 |
|
| |
| 45 |
166 |
| static const char *ph_path(pTHX_ SV *sv, int copy_path, uint8_t *utf8) { |
| 46 |
|
| STRLEN len; |
| 47 |
|
| const char *p; |
| 48 |
166 |
▶ | if (utf8 && SvROK(sv)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (utf8 && SvROK(sv)) { |
95 |
71 |
166 |
| if (utf8 && SvROK(sv)) { |
2 |
93 |
95 |
|
| 49 |
2 |
| sv = sv_2mortal(newSVsv(sv)); |
| 50 |
2 |
| p = SvPV_force(sv, len); |
| 51 |
164 |
| } else p = SvPV(sv, len); |
| 52 |
166 |
▶ | if (memchr(p, '\0', len)) croak("path contains NUL"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (memchr(p, '\0', len)) croak("path contains NUL"); |
4 |
162 |
166 |
|
| 53 |
162 |
▶ | if (utf8) *utf8 = SvUTF8(sv) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (utf8) *utf8 = SvUTF8(sv) ? 1 : 0; |
94 |
68 |
162 |
|
| 54 |
162 |
▶ | if (!copy_path) return p; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!copy_path) return p; |
94 |
68 |
162 |
|
| 55 |
68 |
| char *copy = savepvn(p, len); |
| 56 |
68 |
| SAVEFREEPV(copy); |
| 57 |
68 |
| return copy; |
| 58 |
|
| } |
| 59 |
|
| |
| 60 |
|
| MODULE = Data::PerfectHash::Shared PACKAGE = Data::PerfectHash::Shared |
| 61 |
|
| PROTOTYPES: DISABLE |
| 62 |
|
| |
| 63 |
|
| SV * |
| 64 |
|
| new_builder(class, ...) |
| 65 |
|
| const char *class |
| 66 |
|
| PREINIT: |
| 67 |
24 |
| uint32_t type = PH_TYPE_INT; |
| 68 |
|
| CODE: |
| 69 |
24 |
▶ | if ((items - 1) % 2) croak("new_builder: odd number of options"); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((items - 1) % 2) croak("new_builder: odd number of options"); |
1 |
23 |
24 |
|
| 70 |
42 |
▶ | for (int i = 1; i + 1 < items; i += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 1; i + 1 < items; i += 2) { |
20 |
22 |
42 |
|
| 71 |
20 |
| const char *key = SvPV_nolen(ST(i)); |
| 72 |
20 |
▶ | if (strEQ(key, "type")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(key, "type")) { |
19 |
1 |
20 |
|
| 73 |
19 |
| const char *t = SvPV_nolen(ST(i+1)); |
| 74 |
28 |
▶ | type = strEQ(t,"str") ? PH_TYPE_STR : strEQ(t,"int") ? PH_TYPE_INT |
Branch: 1 branch
| Branch | True | False | Total |
| type = strEQ(t,"str") ? PH_TYPE_STR : strEQ(t,"int") ? PH_TYPE_INT |
9 |
10 |
19 |
|
| 75 |
9 |
▶ | : (croak("type must be 'int' or 'str'"), 0); |
Branch: 1 branch
| Branch | True | False | Total |
| : (croak("type must be 'int' or 'str'"), 0); |
0 |
9 |
9 |
|
| 76 |
1 |
| } else croak("unknown option '%s'", key); |
| 77 |
|
| } |
| 78 |
22 |
| RETVAL = ph_mkobj(aTHX_ ph_builder_new(type), "Data::PerfectHash::Shared::Builder"); |
| 79 |
|
| OUTPUT: |
| 80 |
|
| RETVAL |
| 81 |
|
| |
| 82 |
|
| MODULE = Data::PerfectHash::Shared PACKAGE = Data::PerfectHash::Shared::Builder |
| 83 |
|
| |
| 84 |
|
| void |
| 85 |
|
| add(self, key) |
| 86 |
|
| SV *self |
| 87 |
|
| SV *key |
| 88 |
|
| PREINIT: |
| 89 |
2177 |
▶ | BEXTRACT(self); |
Branch: 4 branches
| Branch | True | False | Total |
| BEXTRACT(self); |
2177 |
0 |
2177 |
| BEXTRACT(self); |
0 |
2177 |
2177 |
| BEXTRACT(self); |
0 |
2177 |
2177 |
| BEXTRACT(self); |
0 |
2177 |
2177 |
|
| 90 |
|
| CODE: |
| 91 |
2177 |
▶ | if (b->key_type == PH_TYPE_INT) { IV k = SvIV(key); BREEXTRACT(self); ph_builder_add_int(b, (int64_t)k); } |
Branch: 3 branches
| Branch | True | False | Total |
| if (b->key_type == PH_TYPE_INT) { IV k = SvIV(key); BREEXTRACT(self); ph_builder_add_int(b, (int64_t)k); } |
1088 |
1089 |
2177 |
| if (b->key_type == PH_TYPE_INT) { IV k = SvIV(key); BREEXTRACT(self); ph_builder_add_int(b, (int64_t)k); } |
0 |
1088 |
1088 |
| if (b->key_type == PH_TYPE_INT) { IV k = SvIV(key); BREEXTRACT(self); ph_builder_add_int(b, (int64_t)k); } |
0 |
1088 |
1088 |
|
| 92 |
1089 |
▶ | else { STRLEN len; const char *p = SvPVbyte(key, len); BREEXTRACT(self); ph_builder_add_str(b, p, len); } |
Branch: 2 branches
| Branch | True | False | Total |
| else { STRLEN len; const char *p = SvPVbyte(key, len); BREEXTRACT(self); ph_builder_add_str(b, p, len); } |
0 |
1089 |
1089 |
| else { STRLEN len; const char *p = SvPVbyte(key, len); BREEXTRACT(self); ph_builder_add_str(b, p, len); } |
0 |
1089 |
1089 |
|
| 93 |
|
| |
| 94 |
|
| void |
| 95 |
|
| add_many(self, aref) |
| 96 |
|
| SV *self |
| 97 |
|
| SV *aref |
| 98 |
|
| PREINIT: |
| 99 |
18 |
▶ | BEXTRACT(self); |
Branch: 4 branches
| Branch | True | False | Total |
| BEXTRACT(self); |
18 |
0 |
18 |
| BEXTRACT(self); |
0 |
18 |
18 |
| BEXTRACT(self); |
0 |
18 |
18 |
| BEXTRACT(self); |
0 |
18 |
18 |
|
| 100 |
|
| AV *av; |
| 101 |
|
| SSize_t i, n; |
| 102 |
|
| CODE: |
| 103 |
18 |
▶ | SvGETMAGIC(aref); |
Branch: 2 branches
| Branch | True | False | Total |
| SvGETMAGIC(aref); |
0 |
18 |
18 |
| SvGETMAGIC(aref); |
0 |
0 |
0 |
|
| 104 |
18 |
▶ | if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV) croak("add_many: expected an arrayref"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV) croak("add_many: expected an arrayref"); |
18 |
0 |
18 |
| if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV) croak("add_many: expected an arrayref"); |
0 |
18 |
18 |
|
| 105 |
18 |
| av = (AV *)SvRV(aref); |
| 106 |
18 |
| sv_2mortal(SvREFCNT_inc((SV *)av)); /* pin: element magic cannot free it mid-loop */ |
| 107 |
18 |
| n = av_len(av) + 1; |
| 108 |
18 |
▶ | BREEXTRACT(self); |
Branch: 2 branches
| Branch | True | False | Total |
| BREEXTRACT(self); |
0 |
18 |
18 |
| BREEXTRACT(self); |
0 |
18 |
18 |
|
| 109 |
2234 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
2216 |
18 |
2234 |
|
| 110 |
2216 |
▶ | SV **e = av_fetch(av, i, 0); if (!e || !*e) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| SV **e = av_fetch(av, i, 0); if (!e || !*e) continue; |
2216 |
0 |
2216 |
| SV **e = av_fetch(av, i, 0); if (!e || !*e) continue; |
0 |
2216 |
2216 |
|
| 111 |
2216 |
▶ | if (b->key_type == PH_TYPE_INT) { IV k = SvIV(*e); BREEXTRACT(self); ph_builder_add_int(b, (int64_t)k); } |
Branch: 3 branches
| Branch | True | False | Total |
| if (b->key_type == PH_TYPE_INT) { IV k = SvIV(*e); BREEXTRACT(self); ph_builder_add_int(b, (int64_t)k); } |
1090 |
1126 |
2216 |
| if (b->key_type == PH_TYPE_INT) { IV k = SvIV(*e); BREEXTRACT(self); ph_builder_add_int(b, (int64_t)k); } |
0 |
1090 |
1090 |
| if (b->key_type == PH_TYPE_INT) { IV k = SvIV(*e); BREEXTRACT(self); ph_builder_add_int(b, (int64_t)k); } |
0 |
1090 |
1090 |
|
| 112 |
2252 |
▶ | else { STRLEN len; const char *p = SvPVbyte(*e, len); BREEXTRACT(self); ph_builder_add_str(b, p, len); } |
Branch: 2 branches
| Branch | True | False | Total |
| else { STRLEN len; const char *p = SvPVbyte(*e, len); BREEXTRACT(self); ph_builder_add_str(b, p, len); } |
0 |
1126 |
1126 |
| else { STRLEN len; const char *p = SvPVbyte(*e, len); BREEXTRACT(self); ph_builder_add_str(b, p, len); } |
0 |
1126 |
1126 |
|
| 113 |
|
| } |
| 114 |
|
| |
| 115 |
|
| UV |
| 116 |
|
| count(self) |
| 117 |
|
| SV *self |
| 118 |
|
| PREINIT: |
| 119 |
51 |
▶ | BEXTRACT(self); |
Branch: 4 branches
| Branch | True | False | Total |
| BEXTRACT(self); |
51 |
0 |
51 |
| BEXTRACT(self); |
0 |
51 |
51 |
| BEXTRACT(self); |
0 |
51 |
51 |
| BEXTRACT(self); |
0 |
51 |
51 |
|
| 120 |
|
| CODE: |
| 121 |
51 |
| RETVAL = (UV)ph_builder_count(b); |
| 122 |
|
| OUTPUT: |
| 123 |
|
| RETVAL |
| 124 |
|
| |
| 125 |
|
| void |
| 126 |
|
| DESTROY(self) |
| 127 |
|
| SV *self |
| 128 |
|
| CODE: |
| 129 |
|
| /* Cross-bless dispatch below is only safe once we know the SV really is |
| 130 |
|
| * one of OUR two handles: a foreign blessed ref carries an unrelated IV |
| 131 |
|
| * that INT2PTR turns into a wild pointer, and reading type_tag segfaults. |
| 132 |
|
| * Accept either class -- that is exactly the case the dispatch exists for. */ |
| 133 |
22 |
▶ | if (!sv_isobject(self) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_isobject(self) |
22 |
0 |
22 |
|
| 134 |
22 |
▶ | || (!sv_derived_from(self, "Data::PerfectHash::Shared") |
Branch: 1 branch
| Branch | True | False | Total |
| || (!sv_derived_from(self, "Data::PerfectHash::Shared") |
22 |
0 |
22 |
|
| 135 |
22 |
▶ | && !sv_derived_from(self, "Data::PerfectHash::Shared::Builder"))) return; |
Branch: 1 branch
| Branch | True | False | Total |
| && !sv_derived_from(self, "Data::PerfectHash::Shared::Builder"))) return; |
0 |
22 |
22 |
|
| 136 |
22 |
| { PhBuilder *b = INT2PTR(PhBuilder*, SvIV(SvRV(self))); |
| 137 |
|
| /* A reblessed handle points at a real allocation of the OTHER struct. |
| 138 |
|
| * type_tag is the first member of both, so read it first and dispatch to |
| 139 |
|
| * the CORRECT free -- never reinterpret (crash) or skip it (leak). |
| 140 |
|
| * A 0 tag means already-destroyed: no-op. */ |
| 141 |
22 |
▶ | if (b) { uint32_t tag = b->type_tag; sv_setiv(SvRV(self), 0); |
Branch: 1 branch
| Branch | True | False | Total |
| if (b) { uint32_t tag = b->type_tag; sv_setiv(SvRV(self), 0); |
22 |
0 |
22 |
|
| 142 |
22 |
▶ | if (tag == PH_BUILDER_TAG) ph_builder_free(b); |
Branch: 1 branch
| Branch | True | False | Total |
| if (tag == PH_BUILDER_TAG) ph_builder_free(b); |
22 |
0 |
22 |
|
| 143 |
0 |
▶ | else if (tag == PH_SET_TAG) ph_close((PhSet*)b); } } |
Branch: 1 branch
| Branch | True | False | Total |
| else if (tag == PH_SET_TAG) ph_close((PhSet*)b); } } |
0 |
0 |
0 |
|
| 144 |
|
| |
| 145 |
|
| void |
| 146 |
|
| build(self, path, ...) |
| 147 |
|
| SV *self |
| 148 |
|
| SV *path |
| 149 |
|
| PREINIT: |
| 150 |
44 |
▶ | BEXTRACT(self); |
Branch: 4 branches
| Branch | True | False | Total |
| BEXTRACT(self); |
44 |
0 |
44 |
| BEXTRACT(self); |
0 |
44 |
44 |
| BEXTRACT(self); |
0 |
44 |
44 |
| BEXTRACT(self); |
0 |
44 |
44 |
|
| 151 |
44 |
| mode_t mode = 0600; char err[256]; |
| 152 |
|
| CODE: |
| 153 |
44 |
| const char *psafe = ph_path(aTHX_ path, 1, NULL); |
| 154 |
43 |
▶ | if ((items - 2) % 2) croak("build: odd number of options"); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((items - 2) % 2) croak("build: odd number of options"); |
1 |
42 |
43 |
|
| 155 |
44 |
▶ | for (int i=2;i+1<items;i+=2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i=2;i+1<items;i+=2) { |
3 |
41 |
44 |
|
| 156 |
3 |
| const char *key = SvPV_nolen(ST(i)); |
| 157 |
3 |
▶ | if (strEQ(key,"mode")) mode=(mode_t)SvUV(ST(i+1)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(key,"mode")) mode=(mode_t)SvUV(ST(i+1)); |
2 |
1 |
3 |
|
| 158 |
1 |
| else croak("unknown option '%s'", key); |
| 159 |
|
| } |
| 160 |
|
| /* SvPV_nolen/SvUV above can run arbitrary Perl (tie/overload magic) that |
| 161 |
|
| * destroys or replaces self -- re-check before touching b, like add/add_many do. */ |
| 162 |
41 |
▶ | BREEXTRACT(self); |
Branch: 2 branches
| Branch | True | False | Total |
| BREEXTRACT(self); |
0 |
41 |
41 |
| BREEXTRACT(self); |
0 |
41 |
41 |
|
| 163 |
41 |
▶ | if (ph_build(b, psafe, mode, err, sizeof err) != 0) croak("Data::PerfectHash::Shared->build: %s", err); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ph_build(b, psafe, mode, err, sizeof err) != 0) croak("Data::PerfectHash::Shared->build: %s", err); |
0 |
41 |
41 |
|
| 164 |
|
| |
| 165 |
|
| MODULE = Data::PerfectHash::Shared PACKAGE = Data::PerfectHash::Shared |
| 166 |
|
| |
| 167 |
|
| SV * |
| 168 |
|
| build_int(class, path, ids) |
| 169 |
|
| const char *class |
| 170 |
|
| SV *path |
| 171 |
|
| SV *ids |
| 172 |
|
| PREINIT: |
| 173 |
|
| char err[256]; |
| 174 |
|
| CODE: |
| 175 |
17 |
| const char *psafe = ph_path(aTHX_ path, 1, NULL); |
| 176 |
16 |
▶ | SvGETMAGIC(ids); |
Branch: 2 branches
| Branch | True | False | Total |
| SvGETMAGIC(ids); |
1 |
15 |
16 |
| SvGETMAGIC(ids); |
0 |
1 |
1 |
|
| 177 |
16 |
▶ | if (!SvROK(ids)||SvTYPE(SvRV(ids))!=SVt_PVAV) croak("build_int: expected an arrayref"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(ids)||SvTYPE(SvRV(ids))!=SVt_PVAV) croak("build_int: expected an arrayref"); |
16 |
0 |
16 |
| if (!SvROK(ids)||SvTYPE(SvRV(ids))!=SVt_PVAV) croak("build_int: expected an arrayref"); |
0 |
16 |
16 |
|
| 178 |
16 |
| { AV *av=(AV*)SvRV(ids); sv_2mortal(SvREFCNT_inc((SV*)av)); /* pin: element magic cannot free it mid-loop */ |
| 179 |
16 |
| PhBuilder *b=ph_builder_new(PH_TYPE_INT); |
| 180 |
16 |
| SAVEDESTRUCTOR_X(ph_builder_free_x, b); /* F7: free on normal return AND croak-unwind (no explicit free below) */ |
| 181 |
6494 |
▶ | SSize_t n=av_len(av)+1; for(SSize_t i=0;i<n;i++){SV**e=av_fetch(av,i,0); if(e&&*e) ph_builder_add_int(b,(int64_t)SvIV(*e));} |
Branch: 3 branches
| Branch | True | False | Total |
| SSize_t n=av_len(av)+1; for(SSize_t i=0;i<n;i++){SV**e=av_fetch(av,i,0); if(e&&*e) ph_builder_add_int(b,(int64_t)SvIV(*e));} |
6478 |
0 |
6478 |
| SSize_t n=av_len(av)+1; for(SSize_t i=0;i<n;i++){SV**e=av_fetch(av,i,0); if(e&&*e) ph_builder_add_int(b,(int64_t)SvIV(*e));} |
6478 |
0 |
6478 |
| SSize_t n=av_len(av)+1; for(SSize_t i=0;i<n;i++){SV**e=av_fetch(av,i,0); if(e&&*e) ph_builder_add_int(b,(int64_t)SvIV(*e));} |
6478 |
16 |
6494 |
|
| 182 |
16 |
| int rc=ph_build(b,psafe,0600,err,sizeof err); |
| 183 |
16 |
▶ | if(rc) croak("build_int: %s",err); } |
Branch: 1 branch
| Branch | True | False | Total |
| if(rc) croak("build_int: %s",err); } |
0 |
16 |
16 |
|
| 184 |
16 |
| RETVAL = &PL_sv_yes; |
| 185 |
|
| OUTPUT: |
| 186 |
|
| RETVAL |
| 187 |
|
| |
| 188 |
|
| SV * |
| 189 |
|
| build_str(class, path, strs) |
| 190 |
|
| const char *class |
| 191 |
|
| SV *path |
| 192 |
|
| SV *strs |
| 193 |
|
| PREINIT: |
| 194 |
|
| char err[256]; |
| 195 |
|
| CODE: |
| 196 |
10 |
| const char *psafe = ph_path(aTHX_ path, 1, NULL); |
| 197 |
9 |
▶ | SvGETMAGIC(strs); |
Branch: 2 branches
| Branch | True | False | Total |
| SvGETMAGIC(strs); |
1 |
8 |
9 |
| SvGETMAGIC(strs); |
0 |
1 |
1 |
|
| 198 |
9 |
▶ | if (!SvROK(strs)||SvTYPE(SvRV(strs))!=SVt_PVAV) croak("build_str: expected an arrayref"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(strs)||SvTYPE(SvRV(strs))!=SVt_PVAV) croak("build_str: expected an arrayref"); |
9 |
0 |
9 |
| if (!SvROK(strs)||SvTYPE(SvRV(strs))!=SVt_PVAV) croak("build_str: expected an arrayref"); |
0 |
9 |
9 |
|
| 199 |
9 |
| { AV *av=(AV*)SvRV(strs); sv_2mortal(SvREFCNT_inc((SV*)av)); /* pin: element magic cannot free it mid-loop */ |
| 200 |
9 |
| PhBuilder *b=ph_builder_new(PH_TYPE_STR); |
| 201 |
9 |
| SAVEDESTRUCTOR_X(ph_builder_free_x, b); /* F7: free on normal return AND croak-unwind (no explicit free below) */ |
| 202 |
4045 |
▶ | SSize_t n=av_len(av)+1; for(SSize_t i=0;i<n;i++){SV**e=av_fetch(av,i,0); if(e&&*e){STRLEN l;const char*q=SvPVbyte(*e,l);ph_builder_add_str(b,q,l);}} |
Branch: 3 branches
| Branch | True | False | Total |
| SSize_t n=av_len(av)+1; for(SSize_t i=0;i<n;i++){SV**e=av_fetch(av,i,0); if(e&&*e){STRLEN l;const char*q=SvPVbyte(*e,l);ph_builder_add_str(b,q,l);}} |
2019 |
0 |
2019 |
| SSize_t n=av_len(av)+1; for(SSize_t i=0;i<n;i++){SV**e=av_fetch(av,i,0); if(e&&*e){STRLEN l;const char*q=SvPVbyte(*e,l);ph_builder_add_str(b,q,l);}} |
2019 |
0 |
2019 |
| SSize_t n=av_len(av)+1; for(SSize_t i=0;i<n;i++){SV**e=av_fetch(av,i,0); if(e&&*e){STRLEN l;const char*q=SvPVbyte(*e,l);ph_builder_add_str(b,q,l);}} |
2019 |
8 |
2027 |
|
| 203 |
8 |
| int rc=ph_build(b,psafe,0600,err,sizeof err); |
| 204 |
8 |
▶ | if(rc) croak("build_str: %s",err); } |
Branch: 1 branch
| Branch | True | False | Total |
| if(rc) croak("build_str: %s",err); } |
0 |
8 |
8 |
|
| 205 |
8 |
| RETVAL = &PL_sv_yes; |
| 206 |
|
| OUTPUT: |
| 207 |
|
| RETVAL |
| 208 |
|
| |
| 209 |
|
| SV * |
| 210 |
|
| load(class, path) |
| 211 |
|
| const char *class |
| 212 |
|
| SV *path |
| 213 |
|
| PREINIT: |
| 214 |
|
| char err[256]; PhSet *s; uint8_t utf8; |
| 215 |
|
| CODE: |
| 216 |
95 |
| const char *psafe = ph_path(aTHX_ path, 0, &utf8); |
| 217 |
94 |
| s = ph_open(psafe, err, sizeof err); |
| 218 |
94 |
▶ | if (!s) croak("Data::PerfectHash::Shared->load(%s): %s", psafe, err); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) croak("Data::PerfectHash::Shared->load(%s): %s", psafe, err); |
16 |
78 |
94 |
|
| 219 |
78 |
| s->path_utf8 = utf8; |
| 220 |
78 |
| RETVAL = ph_mkobj(aTHX_ s, "Data::PerfectHash::Shared"); |
| 221 |
|
| OUTPUT: |
| 222 |
|
| RETVAL |
| 223 |
|
| |
| 224 |
|
| int |
| 225 |
|
| has(self, key) |
| 226 |
|
| SV *self |
| 227 |
|
| SV *key |
| 228 |
|
| PREINIT: |
| 229 |
53304 |
▶ | SEXTRACT(self); |
Branch: 4 branches
| Branch | True | False | Total |
| SEXTRACT(self); |
53304 |
0 |
53304 |
| SEXTRACT(self); |
0 |
53304 |
53304 |
| SEXTRACT(self); |
0 |
53304 |
53304 |
| SEXTRACT(self); |
0 |
53304 |
53304 |
|
| 230 |
|
| CODE: |
| 231 |
53304 |
▶ | if (s->hdr->key_type == PH_TYPE_INT) { IV k=SvIV(key); SREEXTRACT(self); RETVAL = ph_has_int(s,(int64_t)k); } |
Branch: 3 branches
| Branch | True | False | Total |
| if (s->hdr->key_type == PH_TYPE_INT) { IV k=SvIV(key); SREEXTRACT(self); RETVAL = ph_has_int(s,(int64_t)k); } |
46378 |
6926 |
53304 |
| if (s->hdr->key_type == PH_TYPE_INT) { IV k=SvIV(key); SREEXTRACT(self); RETVAL = ph_has_int(s,(int64_t)k); } |
0 |
46378 |
46378 |
| if (s->hdr->key_type == PH_TYPE_INT) { IV k=SvIV(key); SREEXTRACT(self); RETVAL = ph_has_int(s,(int64_t)k); } |
0 |
46378 |
46378 |
|
| 232 |
6926 |
▶ | else { STRLEN len; const char *p=SvPVbyte(key,len); SREEXTRACT(self); RETVAL = ph_has_str(s,p,len); } |
Branch: 2 branches
| Branch | True | False | Total |
| else { STRLEN len; const char *p=SvPVbyte(key,len); SREEXTRACT(self); RETVAL = ph_has_str(s,p,len); } |
0 |
6926 |
6926 |
| else { STRLEN len; const char *p=SvPVbyte(key,len); SREEXTRACT(self); RETVAL = ph_has_str(s,p,len); } |
0 |
6926 |
6926 |
|
| 233 |
|
| OUTPUT: |
| 234 |
|
| RETVAL |
| 235 |
|
| |
| 236 |
|
| const char * |
| 237 |
|
| type(self) |
| 238 |
|
| SV *self |
| 239 |
|
| PREINIT: |
| 240 |
3 |
▶ | SEXTRACT(self); |
Branch: 4 branches
| Branch | True | False | Total |
| SEXTRACT(self); |
3 |
0 |
3 |
| SEXTRACT(self); |
0 |
3 |
3 |
| SEXTRACT(self); |
0 |
3 |
3 |
| SEXTRACT(self); |
0 |
3 |
3 |
|
| 241 |
|
| CODE: |
| 242 |
3 |
▶ | RETVAL = s->hdr->key_type==PH_TYPE_INT ? "int" : "str"; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = s->hdr->key_type==PH_TYPE_INT ? "int" : "str"; |
1 |
2 |
3 |
|
| 243 |
|
| OUTPUT: |
| 244 |
|
| RETVAL |
| 245 |
|
| |
| 246 |
|
| UV |
| 247 |
|
| count(self) |
| 248 |
|
| SV *self |
| 249 |
|
| PREINIT: |
| 250 |
59 |
▶ | SEXTRACT(self); |
Branch: 4 branches
| Branch | True | False | Total |
| SEXTRACT(self); |
59 |
0 |
59 |
| SEXTRACT(self); |
0 |
59 |
59 |
| SEXTRACT(self); |
0 |
59 |
59 |
| SEXTRACT(self); |
0 |
59 |
59 |
|
| 251 |
|
| CODE: |
| 252 |
59 |
▶ | RETVAL = (UV)s->hdr->n; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (UV)s->hdr->n; |
50 |
9 |
59 |
|
| 253 |
|
| OUTPUT: |
| 254 |
|
| RETVAL |
| 255 |
|
| |
| 256 |
|
| void |
| 257 |
|
| each_key(self, cb) |
| 258 |
|
| SV *self |
| 259 |
|
| SV *cb |
| 260 |
|
| PREINIT: |
| 261 |
54 |
▶ | SEXTRACT(self); |
Branch: 4 branches
| Branch | True | False | Total |
| SEXTRACT(self); |
54 |
0 |
54 |
| SEXTRACT(self); |
0 |
54 |
54 |
| SEXTRACT(self); |
0 |
54 |
54 |
| SEXTRACT(self); |
0 |
54 |
54 |
|
| 262 |
|
| CODE: |
| 263 |
54 |
▶ | if (!SvROK(cb)||SvTYPE(SvRV(cb))!=SVt_PVCV) croak("each_key: expected a coderef"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(cb)||SvTYPE(SvRV(cb))!=SVt_PVCV) croak("each_key: expected a coderef"); |
54 |
0 |
54 |
| if (!SvROK(cb)||SvTYPE(SvRV(cb))!=SVt_PVCV) croak("each_key: expected a coderef"); |
0 |
54 |
54 |
|
| 264 |
11347 |
▶ | for (uint64_t i=0;i<s->hdr->n;i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (uint64_t i=0;i<s->hdr->n;i++) { |
11293 |
54 |
11347 |
|
| 265 |
11293 |
| const PhStrSlot *ss = NULL; |
| 266 |
11293 |
▶ | if (s->hdr->key_type==PH_TYPE_STR) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->hdr->key_type==PH_TYPE_STR) { |
5929 |
5364 |
11293 |
|
| 267 |
5929 |
| ss = &((const PhStrSlot*)s->slots)[i]; |
| 268 |
|
| /* slot (off,len) is untrusted, same as ph_has_str's lookup path -- |
| 269 |
|
| * bounds-check it and skip a corrupt slot instead of reading past the arena. */ |
| 270 |
5929 |
▶ | if (!ph_region_ok(ss->off, ss->len, s->hdr->arena_len)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ph_region_ok(ss->off, ss->len, s->hdr->arena_len)) continue; |
0 |
5929 |
5929 |
|
| 271 |
|
| } |
| 272 |
11293 |
▶ | { dSP; ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| { dSP; ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
11293 |
11293 |
|
| 273 |
11293 |
▶ | if (s->hdr->key_type==PH_TYPE_INT) XPUSHs(sv_2mortal(newSViv(((const int64_t*)s->slots)[i]))); |
Branch: 2 branches
| Branch | True | False | Total |
| if (s->hdr->key_type==PH_TYPE_INT) XPUSHs(sv_2mortal(newSViv(((const int64_t*)s->slots)[i]))); |
5364 |
5929 |
11293 |
| if (s->hdr->key_type==PH_TYPE_INT) XPUSHs(sv_2mortal(newSViv(((const int64_t*)s->slots)[i]))); |
0 |
5364 |
5364 |
|
| 274 |
5929 |
▶ | else XPUSHs(sv_2mortal(newSVpvn((const char*)s->arena+ss->off, ss->len))); |
Branch: 1 branch
| Branch | True | False | Total |
| else XPUSHs(sv_2mortal(newSVpvn((const char*)s->arena+ss->off, ss->len))); |
0 |
5929 |
5929 |
|
| 275 |
11293 |
▶ | PUTBACK; call_sv(cb, G_VOID|G_DISCARD); FREETMPS; LEAVE; } |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; call_sv(cb, G_VOID|G_DISCARD); FREETMPS; LEAVE; } |
11293 |
0 |
11293 |
|
| 276 |
11293 |
▶ | SREEXTRACT(self); /* the callback ran arbitrary Perl */ |
Branch: 2 branches
| Branch | True | False | Total |
| SREEXTRACT(self); /* the callback ran arbitrary Perl */ |
0 |
11293 |
11293 |
| SREEXTRACT(self); /* the callback ran arbitrary Perl */ |
0 |
11293 |
11293 |
|
| 277 |
|
| } |
| 278 |
|
| |
| 279 |
|
| SV * |
| 280 |
|
| path(self) |
| 281 |
|
| SV *self |
| 282 |
|
| PREINIT: |
| 283 |
10 |
▶ | SEXTRACT(self); |
Branch: 4 branches
| Branch | True | False | Total |
| SEXTRACT(self); |
10 |
0 |
10 |
| SEXTRACT(self); |
0 |
10 |
10 |
| SEXTRACT(self); |
0 |
10 |
10 |
| SEXTRACT(self); |
0 |
10 |
10 |
|
| 284 |
|
| CODE: |
| 285 |
10 |
| RETVAL = newSVpv(s->path, 0); |
| 286 |
10 |
▶ | if (s->path_utf8) SvUTF8_on(RETVAL); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->path_utf8) SvUTF8_on(RETVAL); |
4 |
6 |
10 |
|
| 287 |
|
| OUTPUT: |
| 288 |
|
| RETVAL |
| 289 |
|
| |
| 290 |
|
| void |
| 291 |
|
| unlink(self) |
| 292 |
|
| SV *self |
| 293 |
|
| PREINIT: |
| 294 |
5 |
▶ | SEXTRACT(self); |
Branch: 4 branches
| Branch | True | False | Total |
| SEXTRACT(self); |
5 |
0 |
5 |
| SEXTRACT(self); |
0 |
5 |
5 |
| SEXTRACT(self); |
0 |
5 |
5 |
| SEXTRACT(self); |
0 |
5 |
5 |
|
| 295 |
|
| CODE: |
| 296 |
5 |
▶ | if (!s->path) croak("unlink: no path"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s->path) croak("unlink: no path"); |
0 |
5 |
5 |
|
| 297 |
5 |
▶ | if (unlink(s->path) != 0 && errno != ENOENT) croak("unlink(%s): %s", s->path, strerror(errno)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (unlink(s->path) != 0 && errno != ENOENT) croak("unlink(%s): %s", s->path, strerror(errno)); |
0 |
5 |
5 |
| if (unlink(s->path) != 0 && errno != ENOENT) croak("unlink(%s): %s", s->path, strerror(errno)); |
0 |
0 |
0 |
|
| 298 |
|
| |
| 299 |
|
| void |
| 300 |
|
| DESTROY(self) |
| 301 |
|
| SV *self |
| 302 |
|
| CODE: |
| 303 |
|
| /* Reject foreign objects before the cross-bless dispatch: see the matching |
| 304 |
|
| * comment on Builder's DESTROY. */ |
| 305 |
78 |
▶ | if (!sv_isobject(self) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_isobject(self) |
78 |
0 |
78 |
|
| 306 |
78 |
▶ | || (!sv_derived_from(self, "Data::PerfectHash::Shared") |
Branch: 1 branch
| Branch | True | False | Total |
| || (!sv_derived_from(self, "Data::PerfectHash::Shared") |
0 |
78 |
78 |
|
| 307 |
0 |
▶ | && !sv_derived_from(self, "Data::PerfectHash::Shared::Builder"))) return; |
Branch: 1 branch
| Branch | True | False | Total |
| && !sv_derived_from(self, "Data::PerfectHash::Shared::Builder"))) return; |
0 |
0 |
0 |
|
| 308 |
78 |
| { PhSet *s = INT2PTR(PhSet*, SvIV(SvRV(self))); |
| 309 |
|
| /* Cross-bless safety: see the matching comment on Builder's DESTROY. */ |
| 310 |
78 |
▶ | if (s) { uint32_t tag = s->type_tag; sv_setiv(SvRV(self),0); |
Branch: 1 branch
| Branch | True | False | Total |
| if (s) { uint32_t tag = s->type_tag; sv_setiv(SvRV(self),0); |
78 |
0 |
78 |
|
| 311 |
78 |
▶ | if (tag == PH_SET_TAG) ph_close(s); |
Branch: 1 branch
| Branch | True | False | Total |
| if (tag == PH_SET_TAG) ph_close(s); |
78 |
0 |
78 |
|
| 312 |
0 |
▶ | else if (tag == PH_BUILDER_TAG) ph_builder_free((PhBuilder*)s); } } |
Branch: 1 branch
| Branch | True | False | Total |
| else if (tag == PH_BUILDER_TAG) ph_builder_free((PhBuilder*)s); } } |
0 |
0 |
0 |
|