hrpriv.h

filter: Statementstmt 93.1% 68 / 73 Branchbran 60.3% 35 / 58 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.6% 103 / 131 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« hr_hrimpl.c ↑ Summary blib/lib/Ref/Store/XS/cfunc.pm »
1 #ifndef HRPRIV_H_
2 #define HRPRIV_H_
3  
4 #include "hreg.h"
5 #include "hrdefs.h"
6  
7 #include <stdarg.h>
8  
9 #define REF2HASH(ref) ((HV*)(SvRV(ref)))
10 #define REF2ARRAY(ref) (AV*)(SvRV(ref))
11  
12 #define RV_Newtmp(vname, referrent) \
13 vname = newRV_noinc((referrent));
14  
15 #define RV_Freetmp(rv); \
16 SvRV_set(rv, NULL); \
17 SvROK_off(rv); \
18 SvREFCNT_dec(rv);
19  
20 #define stash_from_cache_nocheck(darry, stashtype) \
21 ((HV*)SvRV((*(av_fetch(REF2ARRAY(darry), stashtype, 0)))))
22  
23  
24 typedef char* HR_BlessParams[2];
25 #define blessparam_init(bparam) bparam[0] = NULL
26 #define blessparam_setstash(bparam, stash) bparam[1] = stash
27 #define blessparam2chrp(bparam) (char*)(bparam)
28  
29  
30 HR_INLINE HV*
31 stash_from_cache_nocheck_S(SV *aref, int stashtype)
32 {
33 HV *ret;
34 SV **elem;
35
36 elem = av_fetch(REF2ARRAY(aref), stashtype, 0);
37 if(!elem) {
38 die("Couldn't find stash!");
39 }
40 ret = (HV*)SvRV(*elem);
41 return ret;
42 }
43  
44 enum {
45 VHASH_NO_CREATE = 0,
46 VHASH_NO_DREF = 1,
47 VHASH_INIT_FULL = 2,
48 };
49  
50 typedef char* HSpec[2];
51  
52  
53 enum {
54 STORE_OPT_STRONG_KEY = 1 << 0,
55 STORE_OPT_STRONG_VALUE = 1 << 1,
56 STORE_OPT_O_CREAT = 1 << 2
57 };
58 #define STORE_OPT_STRONG_ATTR (1 << 0)
59  
60 /*This macro will convert string hash options into bitflags for the
61 various store functions
62 */
63  
64 #define _chkopt(option_id, iter, optvar) \
65 if(strcmp(HR_STROPT_ ## option_id, SvPV_nolen(ST(iter))) == 0 \
66 && SvTRUE(ST(iter+1))) { \
67 optvar |= STORE_OPT_ ## option_id; \
68 HR_DEBUG("Found option %s", HR_STROPT_ ## option_id); \
69 continue; \
70 }
71  
72 extern HSpec HR_LookupKeys[];
73  
74 #define FAKE_REFCOUNT (1 << 16)
75 HR_INLINE U32
76 70 refcnt_ka_begin(SV *sv)
77 {
78 70 U32 ret = SvREFCNT(sv);
79 70 SvREFCNT(sv) = FAKE_REFCOUNT;
80 70 return ret;
81 }
82  
83 HR_INLINE void
84 70 refcnt_ka_end(SV *sv, U32 old_refcount)
85 {
86 70 I32 effective_refcount = old_refcount + (SvREFCNT(sv) - FAKE_REFCOUNT);
87 70 ▶ if(effective_refcount <= 0 && old_refcount > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if(effective_refcount <= 0 && old_refcount > 0) { 27 43 70
if(effective_refcount <= 0 && old_refcount > 0) { 1 26 27
88 1 SvREFCNT(sv) = 1;
89 1 SvREFCNT_dec(sv);
90 } else {
91 69 SvREFCNT(sv) = effective_refcount;
92 69 ▶ if(effective_refcount != SvREFCNT(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(effective_refcount != SvREFCNT(sv)) { 0 69 69
93 0 die("Detected negative refcount!");
94 }
95 }
96 70 }
97  
98  
99 #define HR_PREFIX_DELIM "#"
100  
101 #define LOOKUP_FIELDS_COMMON \
102 unsigned char prefix_len : 4;
103 #define HR_PREFIX_LEN_MAX 16
104  
105 #ifndef HR_TABLE_ARRAY
106 typedef HV* HR_Table_t;
107 #else
108 typedef AV* HR_Table_t;
109 #endif
110  
111 #define REF2TABLE(tbl) \
112 ((HR_Table_t)SvRV(tbl))
113  
114 HR_INLINE void
115 957 get_hashes(HR_Table_t table, ...)
116 {
117 va_list ap;
118 957 va_start(ap, table);
119 SV **result;
120
121 1905 while(1) {
122 2862 int ltype = (int)va_arg(ap, int);
123 2862 ▶ if(!ltype) {
Branch: 1 branch
BranchTrueFalseTotal
if(!ltype) { 957 1905 2862
124 957 break;
125 }
126 1905 SV **hashptr = (SV**)va_arg(ap, SV**);
127 #ifdef HR_TABLE_ARRAY
128 1905 result = av_fetch(table, ltype, 0);
129 #else
130 HSpec *kspec = (HSpec*)HR_LookupKeys[ltype-1];
131 char *hkey = (*kspec)[0];
132 int klen = (int)((*kspec)[1]);
133 result = hv_fetch(table, hkey, klen, 0);
134 #endif
135 1905 ▶ if(!result) {
Branch: 1 branch
BranchTrueFalseTotal
if(!result) { 0 1905 1905
136 0 *hashptr = NULL;
137 } else {
138 1905 *hashptr = *result;
139 }
140 }
141 957 va_end(ap);
142 957 }
143  
144 #define new_hashval_ref(vsv, referrent) \
145 SvUPGRADE(vsv, SVt_RV); \
146 SvRV_set(vsv, referrent); \
147 SvROK_on(vsv);
148  
149 HR_INLINE SV*
150 473 get_vhash_from_rlookup(SV *rlookup, SV *vaddr, int create)
151 {
152 473 ▶ HE* h_ent = hv_fetch_ent(REF2HASH(rlookup), vaddr, create, 0);
Branch: 1 branch
BranchTrueFalseTotal
HE* h_ent = hv_fetch_ent(REF2HASH(rlookup), vaddr, create, 0); 440 33 473
153 SV *href;
154 473 ▶ if(h_ent && (href = HeVAL(h_ent)) && SvROK(href)) {
Branch: 3 branches
BranchTrueFalseTotal
if(h_ent && (href = HeVAL(h_ent)) && SvROK(href)) { 461 12 473
if(h_ent && (href = HeVAL(h_ent)) && SvROK(href)) { 461 0 461
if(h_ent && (href = HeVAL(h_ent)) && SvROK(href)) { 428 33 461
155 428 return href;
156 }
157 45 ▶ if(!create) {
Branch: 1 branch
BranchTrueFalseTotal
if(!create) { 12 33 45
158 12 return NULL;
159 }
160
161 /*Create*/
162 33 HV *referrent = newHV();
163 33 ▶ new_hashval_ref(href, (SV*)referrent);
Branch: 1 branch
BranchTrueFalseTotal
new_hashval_ref(href, (SV*)referrent); 33 0 33
164
165 33 ▶ if(create == VHASH_INIT_FULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(create == VHASH_INIT_FULL) { 33 0 33
166 33 ▶ HR_DEBUG("Adding DREF for HV=%p", SvRV(rlookup));
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Adding DREF for HV=%p", SvRV(rlookup)); 33 0 33
HR_DEBUG("Adding DREF for HV=%p", SvRV(rlookup)); 0 33 33
167 33 SV *vref = NULL;
168 33 RV_Newtmp(vref, ((SV*)(SvUV(vaddr))) );
169 33 HR_Action rlookup_delete[] = {
170 33 HR_DREF_FLDS_ptr_from_hv(SvRV(vref), rlookup ),
171 HR_ACTION_LIST_TERMINATOR
172 };
173 33 HR_add_actions_real(vref, rlookup_delete);
174 33 RV_Freetmp(vref);
175 }
176
177 33 return href;
178 }
179  
180  
181 /*This will insert a key object into the value's vhash.
182 It will create the vhash if it does not exist
183 It will use the existing rlookup, if found, or fetch it from the
184 table using get_hashes().
185 */
186  
187 HR_INLINE int
188 440 insert_into_vhash(
189 SV *vref,
190 SV *lobj,
191 char *kstring,
192 HR_Table_t table,
193 SV *rlookup
194 )
195 {
196 SV **stored;
197 SV *vhash;
198 440 SV *vaddr = newSVuv((UV)SvRV(vref));
199 int created;
200
201 440 ▶ if(!rlookup) {
Branch: 1 branch
BranchTrueFalseTotal
if(!rlookup) { 440 0 440
202 440 get_hashes(table, HR_HKEY_LOOKUP_REVERSE, &rlookup,
203 HR_HKEY_LOOKUP_NULL);
204 }
205 440 vhash = get_vhash_from_rlookup(rlookup, vaddr, VHASH_INIT_FULL);
206
207 440 stored = hv_fetch(REF2HASH(vhash), kstring, strlen(kstring), 1);
208 440 ▶ if(!SvROK(*stored)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!SvROK(*stored)) { 240 200 440
209 240 created = 1;
210 240 ▶ HR_DEBUG("Creating new reverse entry..");
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Creating new reverse entry.."); 240 0 240
HR_DEBUG("Creating new reverse entry.."); 0 240 240
211 240 ▶ new_hashval_ref(*stored, SvRV(lobj));
Branch: 1 branch
BranchTrueFalseTotal
new_hashval_ref(*stored, SvRV(lobj)); 240 0 240
212 240 SvREFCNT_inc(SvRV(lobj));
213 } else {
214 200 created = 0;
215 }
216
217 440 SvREFCNT_dec(vaddr);
218 440 return created;
219 }
220  
221 HR_INLINE HV*
222 20 stash_from_pkgparam(char *arg)
223 {
224 20 ▶ if(! (*arg) ) {
Branch: 1 branch
BranchTrueFalseTotal
if(! (*arg) ) { 20 0 20
225 20 ▶ HR_DEBUG("Special stash stored!");
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Special stash stored!"); 20 0 20
HR_DEBUG("Special stash stored!"); 0 20 20
226 20 return (HV*)(((char**)arg)[1]);
227 } else {
228 0 ▶ HR_DEBUG("Just a normal string :(");
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Just a normal string :("); 0 0 0
HR_DEBUG("Just a normal string :("); 0 0 0
229 0 return gv_stashpv(arg, 0);
230 }
231 }
232  
233 HR_INLINE SV*
234 20 mk_blessed_blob(char *pkg, int size)
235 {
236 20 ▶ HR_DEBUG("New blob requested with size=%d", size);
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("New blob requested with size=%d", size); 20 0 20
HR_DEBUG("New blob requested with size=%d", size); 0 20 20
237 20 HV *stash = stash_from_pkgparam(pkg);
238 20 SV *referrant = newSV(size);
239 20 ▶ HR_DEBUG("Allocated block=%p", SvPVX(referrant));
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Allocated block=%p", SvPVX(referrant)); 20 0 20
HR_DEBUG("Allocated block=%p", SvPVX(referrant)); 0 20 20
240 20 ▶ if(!stash) {
Branch: 1 branch
BranchTrueFalseTotal
if(!stash) { 0 20 20
241 0 die("Can't get stash!");
242 SvREFCNT_dec(referrant);
243 return NULL;
244 }
245 20 SV *self = newRV_noinc(referrant);
246 20 sv_bless(self, stash);
247 20 return self;
248 }
249  
250  
251 #endif /* HRPRIV_H_ */
« hr_hrimpl.c ↑ Summary blib/lib/Ref/Store/XS/cfunc.pm »