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