FieldHash coverity-compliant
[p5sagit/p5-mst-13.2.git] / ext / Hash / Util / FieldHash / FieldHash.xs
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 /* support for Hash::Util::FieldHash, prefix HUF_ */
6
7 /* A Perl sub that returns a hashref to the object registry */
8 #define HUF_OB_REG "Hash::Util::FieldHash::_ob_reg"
9 /* Magic cookies to recognize object id's.  Hi, Eva, David */
10 #define HUF_COOKIE 2805.1980
11 #define HUF_REFADDR_COOKIE 1811.1976
12
13 /* For global cache of object registry */
14 #define MY_CXT_KEY "Hash::Util::FieldHash::_guts" XS_VERSION
15 typedef struct {
16     HV* ob_reg; /* Cache object registry */
17 } my_cxt_t;
18 START_MY_CXT
19
20 /* Inquire the object registry (a lexical hash) from perl */
21 HV* HUF_get_ob_reg(void) {
22     dSP;
23     I32 items = call_pv(HUF_OB_REG, G_SCALAR|G_NOARGS);
24     SPAGAIN;
25     if (items == 1) {
26         SV* ref = POPs;
27         PUTBACK;
28         if (ref && SvROK(ref) && SvTYPE(SvRV(ref)) == SVt_PVHV)
29             return (HV*)SvRV(ref);
30     }
31     Perl_die(aTHX_ "Can't get object registry hash");
32 }
33
34 /* Deal with global context */
35 #define HUF_INIT 1
36 #define HUF_CLONE 0
37 #define HUF_RESET -1
38
39 void HUF_global(I32 how) {
40     if (how == HUF_INIT) {
41         MY_CXT_INIT;
42         MY_CXT.ob_reg = HUF_get_ob_reg();
43     } else if (how == HUF_CLONE) {
44         MY_CXT_CLONE;
45         MY_CXT.ob_reg = HUF_get_ob_reg();
46     } else if (how == HUF_RESET) {
47         dMY_CXT;
48         MY_CXT.ob_reg = HUF_get_ob_reg();
49     }
50 }
51
52 /* the id as an SV, optionally marked in the nv (unused feature) */
53 SV* HUF_id(SV* ref, NV cookie) {
54     SV* id = sv_newmortal();
55     if (cookie == 0 ) {
56         SvUPGRADE(id, SVt_PVIV);
57     } else {
58         SvUPGRADE(id, SVt_PVNV);
59         SvNV_set(id, cookie);
60         SvNOK_on(id);
61     }
62     SvIV_set(id, PTR2UV(SvRV(ref)));
63     SvIOK_on(id);
64     return id;
65 }
66
67 /* plain id, only used for field hash entries in field lists */
68 SV* HUF_field_id(SV* obj) {
69     return HUF_id(obj, 0.0);
70 }
71
72 /* object id (same as plain, may be different in future) */
73 SV* HUF_obj_id(SV* obj) {
74     return HUF_id(obj, 0.0);
75 }
76
77 /* set up uvar magic for any sv */
78 void HUF_add_uvar_magic(
79     SV* sv,                    /* the sv to enchant, visible to get/set */
80     I32(* val)(pTHX_ IV, SV*), /* "get" function */
81     I32(* set)(pTHX_ IV, SV*), /* "set" function */
82     I32 index,                 /* get/set will see this */
83     SV* thing                  /* any associated info */
84 ) {
85     struct ufuncs uf;
86         uf.uf_val = val;
87         uf.uf_set = set;
88         uf.uf_index = index;
89     sv_magic(sv, thing, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
90 }
91
92 /* Fetch the data container of a trigger */
93 AV* HUF_get_trigger_content(SV* trigger) {
94     MAGIC* mg;
95     if (trigger && (mg = mg_find(trigger, PERL_MAGIC_uvar)))
96         return (AV*)mg->mg_obj;
97     return NULL;
98 }
99
100 /* Delete an object from all field hashes it may occur in.  Also delete
101  * the object's entry from the object registry.  This function goes in
102  * the uf_set field of the uvar magic of a trigger.
103  */
104 I32 HUF_destroy_obj(pTHX_ IV index, SV* trigger) {
105     /* Do nothing if the weakref wasn't undef'd.  Also don't bother
106      * during global destruction.  (MY_CXT.ob_reg is sometimes funny there) */
107     if (!SvROK(trigger) && (!PL_in_clean_all)) {
108         dMY_CXT;
109         AV* cont = HUF_get_trigger_content(trigger);
110         SV* ob_id = *av_fetch(cont, 0, 0);
111         HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
112         HE* ent;
113         hv_iterinit(field_tab);
114         while (ent = hv_iternext(field_tab)) {
115             SV* field_ref = HeVAL(ent);
116             SV* field = SvRV(field_ref);
117             hv_delete_ent((HV*)field, ob_id, G_DISCARD, 0);
118         }
119         /* make it safe in case we must run in global clenaup, after all */
120         if (PL_in_clean_all)
121             HUF_global(HUF_RESET);
122         hv_delete_ent(MY_CXT.ob_reg, ob_id, G_DISCARD, 0);
123     }
124     return 0;
125 }
126
127 /* Create a trigger for an object.  The trigger is a magical weak ref
128  * that fires when the weak ref expires.  it holds the original id of
129  * the object, and a list of field hashes from which the object may
130  * have to be deleted.  The trigger is stored in the object registry
131  * and also deleted when the object expires.
132  */
133 SV* HUF_new_trigger(SV* obj, SV* ob_id) {
134     dMY_CXT;
135     SV* trigger = sv_rvweaken(newRV_inc(SvRV(obj)));
136     AV* cont = newAV();
137     sv_2mortal((SV*)cont);
138     av_store(cont, 0, SvREFCNT_inc(ob_id));
139     av_store(cont, 1, (SV*)newHV());
140     HUF_add_uvar_magic(trigger, NULL, &HUF_destroy_obj, 0, (SV*)cont);
141     hv_store_ent(MY_CXT.ob_reg, ob_id, trigger, 0);
142     return trigger;
143 }
144
145 /* retrieve a trigger for obj if one exists, return NULL otherwise */
146 SV* HUF_ask_trigger(SV* ob_id) {
147     dMY_CXT;
148     HE* ent;
149     if (ent = hv_fetch_ent(MY_CXT.ob_reg, ob_id, 0, 0))
150         return HeVAL(ent);
151     return NULL;
152 }
153
154 /* get the trigger for an object, creating it if necessary */
155 SV* HUF_get_trigger(SV* obj, SV* ob_id) {
156     SV* trigger;
157     if (!(trigger = HUF_ask_trigger(ob_id)))
158         trigger = HUF_new_trigger(obj, ob_id);
159     return trigger;
160 }
161
162 /* mark an object (trigger) as having been used with a field */
163 void HUF_mark_field(SV* trigger, SV* field) {
164     AV* cont = HUF_get_trigger_content(trigger);
165     HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
166     SV* field_ref = newRV_inc(field);
167     SV* field_id = HUF_field_id(field_ref);
168     hv_store_ent(field_tab, field_id, field_ref, 0);
169 }
170
171 /* These constants are not in the API.  If they ever change in hv.c this code
172  * must be updated */
173 #define HV_FETCH_ISSTORE   0x01
174 #define HV_FETCH_ISEXISTS  0x02
175 #define HV_FETCH_LVALUE    0x04
176 #define HV_FETCH_JUST_SV   0x08
177
178 #define HUF_WOULD_CREATE_KEY(x) ((x) != -1 && ((x) & (HV_FETCH_ISSTORE | HV_FETCH_LVALUE)))
179
180 /* The key exchange function.  It communicates with S_hv_magic_uvar_xkey
181  * in hv.c */
182 I32 HUF_watch_key(pTHX_ IV action, SV* field) {
183     MAGIC* mg = mg_find(field, PERL_MAGIC_uvar);
184     SV* keysv;
185     if (!mg)
186         Perl_die(aTHX_ "Rogue call of 'HUF_watch_key'");
187     keysv = mg->mg_obj;
188     if (keysv && SvROK(keysv)) {
189         SV* ob_id = HUF_obj_id(keysv);
190         mg->mg_obj = ob_id; /* key replacement */
191         if (HUF_WOULD_CREATE_KEY(action)) {
192             SV* trigger = HUF_get_trigger(keysv, ob_id);
193             HUF_mark_field(trigger, field);
194         }
195     }
196     return 0;
197 }
198
199 /* see if something is a field hash */
200 int HUF_get_status(HV* hash) {
201     int ans = 0;
202     if (hash && (SvTYPE(hash) == SVt_PVHV)) {
203         MAGIC* mg;
204         struct ufuncs* uf;
205         ans = (mg = mg_find((SV*)hash, PERL_MAGIC_uvar)) &&
206             (uf = (struct ufuncs *)mg->mg_ptr) &&
207             (uf->uf_val == &HUF_watch_key) &&
208             (uf->uf_set == NULL);
209     }
210     return ans;
211 }
212
213 /* Thread support.  These routines are called by CLONE (and nothing else) */
214
215 /* Fix entries for one object in all field hashes */
216 void HUF_fix_trigger(SV* trigger, SV* new_id) {
217     AV* cont = HUF_get_trigger_content(trigger);
218     HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
219     HV* new_tab = newHV();
220     HE* ent;
221     SV* old_id = *av_fetch(cont, 0, 0);
222     hv_iterinit(field_tab);
223     while (ent = hv_iternext(field_tab)) {
224         SV* field_ref = HeVAL(ent);
225         SV* field_id = HUF_field_id(field_ref);
226         HV* field = (HV*)SvRV(field_ref);
227         SV* val;
228         /* recreate field tab entry */
229         hv_store_ent(new_tab, field_id, SvREFCNT_inc(field_ref), 0);
230         /* recreate field entry, if any */
231         if (val = hv_delete_ent(field, old_id, 0, 0))
232             hv_store_ent(field, new_id, SvREFCNT_inc(val), 0);
233     }
234     /* update the trigger */
235     av_store(cont, 0, SvREFCNT_inc(new_id));
236     av_store(cont, 1, (SV*)new_tab);
237 }
238
239 /* Go over object registry and fix all objects.  Also fix the object
240  * registry.
241  */
242 void HUF_fix_objects(void) {
243     dMY_CXT;
244     I32 i, len;
245     HE* ent;
246     AV* oblist = (AV*)sv_2mortal((SV*)newAV());
247     hv_iterinit(MY_CXT.ob_reg);
248     while(ent = hv_iternext(MY_CXT.ob_reg))
249         av_push(oblist, SvREFCNT_inc(hv_iterkeysv(ent)));
250     len = av_len(oblist);
251     for (i = 0; i <= len; ++i) {
252         SV* old_id = *av_fetch(oblist, i, 0);
253         SV* trigger = hv_delete_ent(MY_CXT.ob_reg, old_id, 0, 0);
254         SV* new_id = HUF_obj_id(trigger);
255         HUF_fix_trigger(trigger, new_id);
256         hv_store_ent(MY_CXT.ob_reg, new_id, SvREFCNT_inc(trigger), 0);
257     }
258 }
259
260 /* test support (not needed for functionality) */
261
262 static SV* counter;
263 I32 HUF_inc_var(pTHX_ IV index, SV* which) {
264     sv_setiv(counter, 1 + SvIV(counter));
265     return 0;
266 }
267
268 MODULE = Hash::Util::FieldHash          PACKAGE = Hash::Util::FieldHash
269
270 BOOT:
271 {
272     HUF_global(HUF_INIT); /* create variables */
273 }
274
275 int
276 _fieldhash(SV* href, int mode)
277 PROTOTYPE: $$
278 CODE:
279     HV* field;
280     RETVAL = 0;
281     if (mode &&
282         href && SvROK(href) &&
283         (field = (HV*)SvRV(href)) &&
284         SvTYPE(field) == SVt_PVHV
285     ) {
286         HUF_add_uvar_magic(
287             SvRV(href),
288             &HUF_watch_key,
289             NULL,
290             0,
291             NULL
292         );
293         RETVAL = HUF_get_status(field);
294     }
295 OUTPUT:
296     RETVAL
297
298 void
299 CLONE(char* class)
300 CODE:
301     if (0 == strcmp(class, "Hash::Util::FieldHash")) {
302         HUF_global(HUF_CLONE);
303         HUF_fix_objects();
304     }
305
306 void
307 _active_fields(SV* obj)
308 PPCODE:
309     if (SvROK(obj)) {
310         SV* ob_id = HUF_obj_id(obj);
311         SV* trigger = HUF_ask_trigger(ob_id);
312         if (trigger) {
313             AV* cont = HUF_get_trigger_content(trigger);
314             HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
315             HE* ent;
316             hv_iterinit(field_tab);
317             while (ent = hv_iternext(field_tab)) {
318                 HV* field = (HV*)SvRV(HeVAL(ent));
319                 if (hv_exists_ent(field, ob_id, 0))
320                     XPUSHs(sv_2mortal(newRV_inc((SV*)field)));
321             }
322         }
323     }
324
325 void
326 _test_uvar_get(SV* svref, SV* countref)
327 CODE:
328     if (SvROK(svref) && SvROK(countref)) {
329         counter = SvRV(countref);
330         sv_setiv(counter, 0);
331         HUF_add_uvar_magic(
332             SvRV(svref),
333             &HUF_inc_var,
334             NULL,
335             0,
336             SvRV(countref)
337         );
338     }
339
340 void
341 _test_uvar_set(SV* svref, SV* countref)
342 CODE:
343     if (SvROK(svref) && SvROK(countref)) {
344         counter = SvRV(countref);
345         sv_setiv(counter, 0);
346         counter = SvRV(countref);
347         HUF_add_uvar_magic(
348             SvRV(svref),
349             NULL,
350             &HUF_inc_var,
351             0,
352             SvRV(countref)
353         );
354     }
355
356 void
357 _test_uvar_same(SV* svref, SV* countref)
358 CODE:
359     if (SvROK(svref) && SvROK(countref)) {
360         counter = SvRV(countref);
361         sv_setiv(counter, 0);
362         HUF_add_uvar_magic(
363             SvRV(svref),
364             &HUF_inc_var,
365             &HUF_inc_var,
366             0,
367             NULL
368         );
369     }
370