hr_pl.c

filter: Statementstmt 64.9% 87 / 134 Branchbran 45.7% 64 / 140 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 55.1% 151 / 274 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Store.xs ↑ Summary hr_implattr.c »
1 #include <string.h>
2 #include <stdint.h>
3 #include "hreg.h"
4  
5 HR_INLINE MAGIC* get_our_magic(SV* objref, int create);
6 HR_INLINE void free_our_magic(SV* objref);
7  
8 static int hr_freehook(pTHX_ SV* target, MAGIC *mg);
9 static int hr_duphook(pTHX_ MAGIC *mg, CLONE_PARAMS *param);
10  
11 static MGVTBL vtbl = {
12 .svt_dup = &hr_duphook,
13 .svt_free = &hr_freehook
14 };
15  
16 #define OURMAGIC_infree(mg) (mg)->mg_private
17  
18  
19 static int
20 335 hr_freehook(pTHX_ SV* object, MAGIC *mg)
21 {
22 335 ▶ if(PL_dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if(PL_dirty) { 0 335 335
23 0 ▶ HR_DEBUG("Not triggering during global destruction");
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Not triggering during global destruction"); 0 0 0
HR_DEBUG("Not triggering during global destruction"); 0 0 0
24 0 return;
25 }
26 335 ▶ HR_DEBUG("FREEHOOK: mg=%p, obj=%p", mg, object);
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("FREEHOOK: mg=%p, obj=%p", mg, object); 335 0 335
HR_DEBUG("FREEHOOK: mg=%p, obj=%p", mg, object); 0 335 335
27 335 ▶ HR_DEBUG("Object refcount: %d", SvREFCNT(object));
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Object refcount: %d", SvREFCNT(object)); 335 0 335
HR_DEBUG("Object refcount: %d", SvREFCNT(object)); 0 335 335
28 335 OURMAGIC_infree(mg) = 1;
29
30 #if (PERL_VERSION < 10) || (PERL_VERSION == 10 && PERL_SUBVERSION < 1)
31 #warning "Nasty SvMAGIC_set hack"
32 SvMAGIC_set(object, mg);
33 #endif
34 335 HR_trigger_and_free_actions(_mg_action_list(mg), object);
35 }
36  
37 /*This is called for new threads, we initialize a new HR_Action list,
38 because old lists would presumably reference variables of the parent thread*/
39 HR_INLINE int
40 0 hr_duphook(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
41 {
42 0 ▶ HR_DEBUG("Initializing new empty action list");
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Initializing new empty action list"); 0 0 0
HR_DEBUG("Initializing new empty action list"); 0 0 0
43 0 Newxz_Action(mg->mg_ptr);
44 0 }
45  
46 HR_INLINE MAGIC*
47 1123 get_our_magic(SV* objref, int create)
48 {
49 MAGIC *mg;
50 HR_Action *action_list;
51 SV *target;
52
53 1123 ▶ if(!SvROK(objref)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!SvROK(objref)) { 0 1123 1123
54 0 die("Value=%p must be a reference type", objref);
55 }
56
57 1123 target = SvRV(objref);
58
59 1123 objref = NULL; /*Don't use this anymore*/
60
61 1123 ▶ if(SvTYPE(target) < SVt_PVMG) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvTYPE(target) < SVt_PVMG) { 15 1108 1123
62 15 ▶ HR_DEBUG("Object=%p is not yet magical!", target);
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Object=%p is not yet magical!", target); 15 0 15
HR_DEBUG("Object=%p is not yet magical!", target); 0 15 15
63 15 ▶ if(create) {
Branch: 1 branch
BranchTrueFalseTotal
if(create) { 15 0 15
64 15 goto GT_NEW_MAGIC;
65 } else {
66 0 ▶ HR_DEBUG("No magic found, but creation not requested");
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("No magic found, but creation not requested"); 0 0 0
HR_DEBUG("No magic found, but creation not requested"); 0 0 0
67 0 return NULL;
68 }
69 }
70
71 1108 ▶ HR_DEBUG("Will try to locate existing magic");
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Will try to locate existing magic"); 1108 0 1108
HR_DEBUG("Will try to locate existing magic"); 0 1108 1108
72 1108 mg = mg_find(target, PERL_MAGIC_ext);
73 1108 ▶ if(mg) {
Branch: 1 branch
BranchTrueFalseTotal
if(mg) { 632 476 1108
74 632 ▶ HR_DEBUG("Found initial mg=%p", mg);
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Found initial mg=%p", mg); 632 0 632
HR_DEBUG("Found initial mg=%p", mg); 0 632 632
75 } else {
76 476 ▶ HR_DEBUG("Can't find existing magic!");
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Can't find existing magic!"); 476 0 476
HR_DEBUG("Can't find existing magic!"); 0 476 476
77 }
78 1108 ▶ for(; mg; mg = mg->mg_moremagic) {
Branch: 1 branch
BranchTrueFalseTotal
for(; mg; mg = mg->mg_moremagic) { 632 476 1108
79
80 632 ▶ HR_DEBUG("Checking mg=%p", mg);
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Checking mg=%p", mg); 632 0 632
HR_DEBUG("Checking mg=%p", mg); 0 632 632
81 632 ▶ if(mg->mg_virtual == &vtbl) {
Branch: 1 branch
BranchTrueFalseTotal
if(mg->mg_virtual == &vtbl) { 632 0 632
82 632 return mg;
83 }
84 }
85
86 476 ▶ if(!create) {
Branch: 1 branch
BranchTrueFalseTotal
if(!create) { 1 475 476
87 1 return NULL;
88 }
89
90 475 GT_NEW_MAGIC:
91 490 ▶ HR_DEBUG("Creating new magic for %p", target);
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Creating new magic for %p", target); 490 0 490
HR_DEBUG("Creating new magic for %p", target); 0 490 490
92 490 Newxz_Action(action_list);
93 490 mg = sv_magicext(target, target, PERL_MAGIC_ext, &vtbl,
94 (const char*)action_list, 0);
95
96 490 mg->mg_flags |= MGf_DUP;
97
98 490 OURMAGIC_infree(mg) = 0;
99
100 490 ▶ if(!mg) {
Branch: 1 branch
BranchTrueFalseTotal
if(!mg) { 0 490 490
101 0 die("Couldn't create magic!");
102 } else {
103 490 ▶ HR_DEBUG("Created mg=%p, alist=%p", mg, action_list);
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Created mg=%p, alist=%p", mg, action_list); 490 0 490
HR_DEBUG("Created mg=%p, alist=%p", mg, action_list); 0 490 490
104 }
105 490 return mg;
106 }
107  
108 HR_INLINE void
109 155 free_our_magic(SV* target)
110 {
111 155 MAGIC *mg_last = mg_find(target, PERL_MAGIC_ext);
112 155 MAGIC *mg_cur = mg_last;
113 HR_Action *action;
114
115 155 ▶ for(;mg_cur; mg_last = mg_cur, mg_cur = mg_cur->mg_moremagic
Branch: 1 branch
BranchTrueFalseTotal
for(;mg_cur; mg_last = mg_cur, mg_cur = mg_cur->mg_moremagic 155 0 155
116 ) {
117 155 ▶ if(mg_cur->mg_virtual == &vtbl) {
Branch: 1 branch
BranchTrueFalseTotal
if(mg_cur->mg_virtual == &vtbl) { 155 0 155
118 155 break;
119 }
120 }
121
122 155 ▶ if(!mg_cur) {
Branch: 1 branch
BranchTrueFalseTotal
if(!mg_cur) { 0 155 155
123 0 return;
124 }
125
126 155 action = _mg_action_list(mg_cur);
127 155 ▶ if(action) {
Branch: 1 branch
BranchTrueFalseTotal
if(action) { 155 0 155
128 155 ▶ HR_DEBUG("Found action=%p", action);
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Found action=%p", action); 155 0 155
HR_DEBUG("Found action=%p", action); 0 155 155
129 155 ▶ while((action = HR_free_action(action)));
Branch: 1 branch
BranchTrueFalseTotal
while((action = HR_free_action(action))); 155 0 155
130 }
131
132 /*Check if this is the last magic on the variable*/
133 155 GT_FREE_MAGIC:
134 155 mg_cur->mg_virtual = NULL;
135 155 ▶ if(mg_cur == mg_last) {
Branch: 1 branch
BranchTrueFalseTotal
if(mg_cur == mg_last) { 155 0 155
136 /*First magic entry*/
137 155 ▶ HR_DEBUG("Calling sv_unmagic(%p)", mg_cur->mg_obj);
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Calling sv_unmagic(%p)", mg_cur->mg_obj); 155 0 155
HR_DEBUG("Calling sv_unmagic(%p)", mg_cur->mg_obj); 0 155 155
138 155 sv_unmagic(mg_cur->mg_obj, PERL_MAGIC_ext);
139 155 ▶ HR_DEBUG("Done!");
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Done!"); 155 0 155
HR_DEBUG("Done!"); 0 155 155
140 } else {
141 0 mg_last->mg_moremagic = mg_cur->mg_moremagic;
142 0 ▶ HR_DEBUG("About to Safefree(mg_cur=%p)", mg_cur);
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("About to Safefree(mg_cur=%p)", mg_cur); 0 0 0
HR_DEBUG("About to Safefree(mg_cur=%p)", mg_cur); 0 0 0
143 0 ▶ HR_DEBUG("Free=%p", mg_cur);
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Free=%p", mg_cur); 0 0 0
HR_DEBUG("Free=%p", mg_cur); 0 0 0
144 0 Safefree(mg_cur);
145 }
146 }
147  
148 HREG_API_INTERNAL void
149 918 HR_add_actions_real(SV* objref, HR_Action *actions)
150 {
151 918 ▶ HR_DEBUG("Have objref=%p, action_list=%p", objref, actions);
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Have objref=%p, action_list=%p", objref, actions); 916 2 918
HR_DEBUG("Have objref=%p, action_list=%p", objref, actions); 0 918 918
152 918 MAGIC *mg = get_our_magic(objref, 1);
153
154 918 ▶ if(!actions) {
Branch: 1 branch
BranchTrueFalseTotal
if(!actions) { 0 918 918
155 0 die("Must have at least one action!");
156 }
157
158 1960 ▶ while(actions->ktype) {
Branch: 1 branch
BranchTrueFalseTotal
while(actions->ktype) { 1042 918 1960
159 1042 ▶ if(!actions->hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if(!actions->hashref) { 0 1042 1042
160 0 die("Must have hashref!");
161 }
162 1042 HR_add_action(_mg_action_list(mg), actions, 1);
163 1042 actions++;
164 }
165 918 }
166  
167 void
168 0 HR_PL_add_actions(SV *objref, char *blob) {
169 0 HR_add_actions_real(objref, (HR_Action*)blob);
170 0 }
171  
172  
173 HR_INLINE void
174 205 pl_del_action_common(SV *objref, SV *hashref,
175 void *key, HR_KeyType_t ktype)
176 {
177 205 MAGIC *mg = get_our_magic(objref, 0);
178 int dv; /*Deletion status*/
179 205 ▶ HR_DEBUG("DELETE: O=%p, SV=%p", objref, hashref);
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("DELETE: O=%p, SV=%p", objref, hashref); 205 0 205
HR_DEBUG("DELETE: O=%p, SV=%p", objref, hashref); 0 205 205
180 205 ▶ if(!mg) {
Branch: 1 branch
BranchTrueFalseTotal
if(!mg) { 1 204 205
181 1 return;
182 }
183
184 204 ▶ if(OURMAGIC_infree(mg)) {
Branch: 1 branch
BranchTrueFalseTotal
if(OURMAGIC_infree(mg)) { 12 192 204
185 12 while(HR_nullify_action(
186 24 ▶ _mg_action_list(mg), hashref, key, ktype) == HR_ACTION_DELETED);
Branch: 1 branch
BranchTrueFalseTotal
_mg_action_list(mg), hashref, key, ktype) == HR_ACTION_DELETED); 12 12 24
187 /*no body*/
188 12 return;
189 }
190
191 192 dv = HR_ACTION_NOT_FOUND;
192 192 while( (dv = HR_del_action(
193 229 ▶ _mg_action_list(mg), hashref, key, ktype)) == HR_ACTION_DELETED );
Branch: 1 branch
BranchTrueFalseTotal
_mg_action_list(mg), hashref, key, ktype)) == HR_ACTION_DELETED ); 37 192 229
194 /*no body*/
195 192 ▶ HR_DEBUG("Delete done");
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Delete done"); 192 0 192
HR_DEBUG("Delete done"); 0 192 192
196
197 192 ▶ if(dv == HR_ACTION_EMPTY) {
Branch: 1 branch
BranchTrueFalseTotal
if(dv == HR_ACTION_EMPTY) { 155 37 192
198 155 free_our_magic(SvRV(objref));
199 }
200 }
201  
202 #define gen_del_fn(suffix, argtype, ktype) \
203 void HR_PL_del_action_ ## suffix(SV *obj, SV *ctr, argtype arg) { \
204 pl_del_action_common(obj, ctr, (void*)arg, ktype); \
205 }
206  
207 28 gen_del_fn(ptr, UV, HR_KEY_TYPE_PTR);
208 0 gen_del_fn(str, char*, HR_KEY_TYPE_STR);
209 0 gen_del_fn(sv, SV*, HR_KEY_STYPE_PTR_RV);
210  
211 #undef gen_del_fn
212  
213 123 void HR_XS_del_action_ext(
214 SV *object, void *container, void *arg, HR_KeyType_t ktype)
215 {
216 123 pl_del_action_common(object, container, arg, ktype);
217 123 }
218  
219  
220 54 void HR_PL_del_action_container(SV *object, SV *hashref)
221 {
222 54 pl_del_action_common(object, hashref, NULL, HR_KEY_TYPE_NULL);
223 54 }
224  
225 void
226 0 HR_PL_add_action_str(SV *objref, SV *hashref, char *str)
227 {
228 int action_type;
229
230 0 int reftype = SvTYPE(SvRV(hashref));
231 0 int keytype = HR_KEY_TYPE_STR;
232 0 char *real_key = str;
233
234 0 ▶ if(reftype == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if(reftype == SVt_PVAV) { 0 0 0
235 0 action_type = HR_ACTION_TYPE_DEL_AV;
236 0 keytype = HR_KEY_TYPE_PTR;
237 0 ▶ HR_DEBUG("Found Array (idx=%s)", str);
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Found Array (idx=%s)", str); 0 0 0
HR_DEBUG("Found Array (idx=%s)", str); 0 0 0
238 0 sscanf(str, "%d", &real_key);
239 0 ▶ HR_DEBUG("Extracted key=%d", real_key);
Branch: 2 branches
BranchTrueFalseTotal
HR_DEBUG("Extracted key=%d", real_key); 0 0 0
HR_DEBUG("Extracted key=%d", real_key); 0 0 0
240 0 ▶ } else if(reftype == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(reftype == SVt_PVHV) { 0 0 0
241 0 action_type = HR_ACTION_TYPE_DEL_HV;
242 } else {
243 0 die("Unknown type %d for target", reftype);
244 }
245 0 HR_Action actions[] = {
246 {
247 .key = real_key,
248 .hashref = hashref,
249 .ktype = keytype,
250 .atype = action_type
251 },
252 HR_ACTION_LIST_TERMINATOR
253 };
254 0 HR_add_actions_real(objref, actions);
255 0 }
256  
257 void
258 247 HR_PL_add_action_ptr(SV* objref, SV *hashref)
259 {
260 247 HR_Action actions[] = {
261 247 HR_DREF_FLDS_ptr_from_hv(SvRV(objref), hashref),
262 HR_ACTION_LIST_TERMINATOR
263 };
264 247 HR_add_actions_real(objref, actions);
265 247 }
266  
267 0 void HR_PL_add_action_ext(
268 SV *objref,
269 UV key,
270 unsigned int atype,
271 unsigned int ktype,
272 SV *hashref,
273 unsigned int flags
274 )
275 {
276
277 0 flags |= HR_FLAG_HASHREF_RV;
278 /*Turn off flags which make no sense coming from perl*/
279 0 flags &= ( ~(HR_FLAG_STR_NO_ALLOC|HR_FLAG_SV_REFCNT_DEC) );
280
281 0 ▶ if(ktype == HR_KEY_TYPE_STR) {
Branch: 1 branch
BranchTrueFalseTotal
if(ktype == HR_KEY_TYPE_STR) { 0 0 0
282 0 key = (UV)SvPV_nolen((SV*)key);
283 }
284
285 0 HR_Action actions[] = {
286 {
287 0 .key = (char*)key,
288 .atype = atype,
289 .ktype = ktype,
290 .hashref = hashref,
291 .flags = flags
292 },
293 HR_ACTION_LIST_TERMINATOR
294 };
295 0 HR_add_actions_real(objref, actions);
296 0 }
« Store.xs ↑ Summary hr_implattr.c »