This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix various compiler warnings from XS code
[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"
d74d639b
AS
9/* Identifier for PERL_MAGIC_ext magic */
10#define HUF_IDCACHE 0x4944
1e73acc8 11
1e73acc8
AS
12/* For global cache of object registry */
13#define MY_CXT_KEY "Hash::Util::FieldHash::_guts" XS_VERSION
14typedef struct {
15 HV* ob_reg; /* Cache object registry */
16} my_cxt_t;
17START_MY_CXT
18
6ff38c27
AS
19/* Inquire the object registry (a lexical hash) from perl */
20HV* HUF_get_ob_reg(void) {
21 dSP;
91dba0be 22 HV* ob_reg = NULL;
0dbc1c87 23 I32 items;
91dba0be
AS
24 ENTER;
25 SAVETMPS;
26
27 PUSHMARK(SP);
0dbc1c87 28 items = call_pv(HUF_OB_REG, G_SCALAR|G_NOARGS);
6ff38c27 29 SPAGAIN;
91dba0be 30
45552c86 31 if (items == 1 && TOPs && SvROK(TOPs) && SvTYPE(SvRV(TOPs)) == SVt_PVHV)
91dba0be 32 ob_reg = (HV*)SvRV(POPs);
91dba0be
AS
33 PUTBACK;
34 FREETMPS;
35 LEAVE;
36
45552c86
AS
37 if (!ob_reg)
38 Perl_die(aTHX_ "Can't get object registry hash");
39 return ob_reg;
6ff38c27
AS
40}
41
1e73acc8
AS
42/* Deal with global context */
43#define HUF_INIT 1
44#define HUF_CLONE 0
45#define HUF_RESET -1
46
47void HUF_global(I32 how) {
48 if (how == HUF_INIT) {
49 MY_CXT_INIT;
6ff38c27 50 MY_CXT.ob_reg = HUF_get_ob_reg();
1e73acc8
AS
51 } else if (how == HUF_CLONE) {
52 MY_CXT_CLONE;
6ff38c27 53 MY_CXT.ob_reg = HUF_get_ob_reg();
1e73acc8
AS
54 } else if (how == HUF_RESET) {
55 dMY_CXT;
6ff38c27 56 MY_CXT.ob_reg = HUF_get_ob_reg();
1e73acc8
AS
57 }
58}
59
d74d639b 60/* Object id */
1e73acc8 61
d74d639b
AS
62/* definition of id transformation */
63#define HUF_OBJ_ID(x) newSVuv(PTR2UV(x))
1e73acc8 64
1e73acc8 65SV* HUF_obj_id(SV* obj) {
d74d639b
AS
66 SV *item = SvRV(obj);
67 MAGIC *mg;
68 SV *id;
69
70 /* Get cached object ID, if it exists */
71 if (SvTYPE(item) >= SVt_PVMG) {
72 for ( mg = SvMAGIC(item); mg; mg = mg->mg_moremagic ) {
73 if ((mg->mg_type == PERL_MAGIC_ext) &&
74 (mg->mg_private == HUF_IDCACHE)
75 ) {
76 return mg->mg_obj;
77 }
78 }
79 }
80
81 /* Create an object ID, cache it */
82 id = HUF_OBJ_ID(item);
83 mg = sv_magicext(item, id, PERL_MAGIC_ext, NULL, NULL, 0);
84 mg->mg_private = HUF_IDCACHE;
f72763ca 85 SvREFCNT_dec(id); /* refcnt++ in sv_magicext() */
d74d639b
AS
86
87 /* Return the object ID */
88 return id;
1e73acc8
AS
89}
90
91/* set up uvar magic for any sv */
92void HUF_add_uvar_magic(
6ff38c27 93 SV* sv, /* the sv to enchant, visible to get/set */
1e73acc8
AS
94 I32(* val)(pTHX_ IV, SV*), /* "get" function */
95 I32(* set)(pTHX_ IV, SV*), /* "set" function */
96 I32 index, /* get/set will see this */
97 SV* thing /* any associated info */
98) {
1e73acc8
AS
99 struct ufuncs uf;
100 uf.uf_val = val;
101 uf.uf_set = set;
102 uf.uf_index = index;
103 sv_magic(sv, thing, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
104}
105
106/* Fetch the data container of a trigger */
107AV* HUF_get_trigger_content(SV* trigger) {
108 MAGIC* mg;
109 if (trigger && (mg = mg_find(trigger, PERL_MAGIC_uvar)))
110 return (AV*)mg->mg_obj;
111 return NULL;
112}
113
114/* Delete an object from all field hashes it may occur in. Also delete
c418a2d4
AS
115 * the object's entry from the object registry. This function goes in
116 * the uf_set field of the uvar magic of a trigger.
1e73acc8
AS
117 */
118I32 HUF_destroy_obj(pTHX_ IV index, SV* trigger) {
c33e8be1 119 PERL_UNUSED_ARG(index);
1e73acc8
AS
120 /* Do nothing if the weakref wasn't undef'd. Also don't bother
121 * during global destruction. (MY_CXT.ob_reg is sometimes funny there) */
122 if (!SvROK(trigger) && (!PL_in_clean_all)) {
123 dMY_CXT;
124 AV* cont = HUF_get_trigger_content(trigger);
125 SV* ob_id = *av_fetch(cont, 0, 0);
126 HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
127 HE* ent;
128 hv_iterinit(field_tab);
c33e8be1 129 while ((ent = hv_iternext(field_tab))) {
1e73acc8
AS
130 SV* field_ref = HeVAL(ent);
131 SV* field = SvRV(field_ref);
c33e8be1 132 (void) hv_delete_ent((HV*)field, ob_id, 0, 0);
1e73acc8
AS
133 }
134 /* make it safe in case we must run in global clenaup, after all */
135 if (PL_in_clean_all)
d74d639b 136 HUF_global(HUF_RESET); /* shoudn't be needed */
c33e8be1 137 (void) hv_delete_ent(MY_CXT.ob_reg, ob_id, 0, 0);
1e73acc8
AS
138 }
139 return 0;
140}
141
d74d639b
AS
142/* Create a trigger for an object. The trigger is a magical SV
143 * that holds a weak ref to the object. The magic fires when the object
144 * expires and takes care of garbage collection in registred hashes.
145 * For that purpose, the magic structure holds the original id of
146 * the object, and a list (a hash, really) of hashes from which the
147 * object may * have to be deleted. The trigger is stored in the
148 * object registry and is also deleted when the object expires.
1e73acc8
AS
149 */
150SV* HUF_new_trigger(SV* obj, SV* ob_id) {
151 dMY_CXT;
152 SV* trigger = sv_rvweaken(newRV_inc(SvRV(obj)));
153 AV* cont = newAV();
154 sv_2mortal((SV*)cont);
155 av_store(cont, 0, SvREFCNT_inc(ob_id));
156 av_store(cont, 1, (SV*)newHV());
157 HUF_add_uvar_magic(trigger, NULL, &HUF_destroy_obj, 0, (SV*)cont);
c33e8be1 158 (void) hv_store_ent(MY_CXT.ob_reg, ob_id, trigger, 0);
1e73acc8
AS
159 return trigger;
160}
161
162/* retrieve a trigger for obj if one exists, return NULL otherwise */
163SV* HUF_ask_trigger(SV* ob_id) {
164 dMY_CXT;
165 HE* ent;
c33e8be1 166 if ((ent = hv_fetch_ent(MY_CXT.ob_reg, ob_id, 0, 0)))
1e73acc8
AS
167 return HeVAL(ent);
168 return NULL;
169}
170
171/* get the trigger for an object, creating it if necessary */
d74d639b 172SV* HUF_get_trigger0(SV* obj, SV* ob_id) {
1e73acc8
AS
173 SV* trigger;
174 if (!(trigger = HUF_ask_trigger(ob_id)))
175 trigger = HUF_new_trigger(obj, ob_id);
176 return trigger;
177}
178
d74d639b
AS
179SV* HUF_get_trigger(SV* obj, SV* ob_id) {
180 SV* trigger = HUF_ask_trigger(ob_id);
181 if (!trigger)
182 trigger = HUF_new_trigger(obj, ob_id);
183 return( trigger);
184}
185
186/* mark an object (trigger) as having been used with a field
187 (a clenup-liability)
188*/
1e73acc8
AS
189void HUF_mark_field(SV* trigger, SV* field) {
190 AV* cont = HUF_get_trigger_content(trigger);
191 HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
192 SV* field_ref = newRV_inc(field);
d74d639b 193 UV field_addr = PTR2UV(field);
c33e8be1 194 (void) hv_store(field_tab, (char *)&field_addr, sizeof(field_addr), field_ref, 0);
1e73acc8
AS
195}
196
d74d639b
AS
197/* Determine, from the value of action, whether this call may create a new
198 * hash key */
324a0d18 199#define HUF_WOULD_CREATE_KEY(x) ((x) != HV_DELETE && ((x) & (HV_FETCH_ISSTORE | HV_FETCH_LVALUE)))
a607227a 200
d74d639b 201/* The key exchange functions. They communicate with S_hv_magic_uvar_xkey
1e73acc8 202 * in hv.c */
d74d639b 203I32 HUF_watch_key_safe(pTHX_ IV action, SV* field) {
1e73acc8 204 MAGIC* mg = mg_find(field, PERL_MAGIC_uvar);
6ff38c27 205 SV* keysv;
d74d639b
AS
206 if (mg && (keysv = mg->mg_obj)) {
207 if (SvROK(keysv)) { /* ref key */
45552c86
AS
208 SV* ob_id = HUF_obj_id(keysv);
209 mg->mg_obj = ob_id; /* key replacement */
210 if (HUF_WOULD_CREATE_KEY(action)) {
211 SV* trigger = HUF_get_trigger(keysv, ob_id);
212 HUF_mark_field(trigger, field);
213 }
d74d639b
AS
214 } else if (HUF_WOULD_CREATE_KEY(action)) { /* string key */
215 /* registered as object id? */
216 SV* trigger;
c33e8be1 217 if (( trigger = HUF_ask_trigger(keysv)))
d74d639b 218 HUF_mark_field( trigger, field);
a607227a 219 }
45552c86 220 } else {
d74d639b
AS
221 Perl_die(aTHX_ "Rogue call of 'HUF_watch_key_safe'");
222 }
223 return 0;
224}
225
226I32 HUF_watch_key_id(pTHX_ IV action, SV* field) {
227 MAGIC* mg = mg_find(field, PERL_MAGIC_uvar);
228 SV* keysv;
c33e8be1 229 PERL_UNUSED_ARG(action);
d74d639b
AS
230 if (mg && (keysv = mg->mg_obj)) {
231 if (SvROK(keysv)) /* ref key */
232 mg->mg_obj = HUF_obj_id(keysv); /* key replacement */
233 } else {
234 Perl_die(aTHX_ "Rogue call of 'HUF_watch_key_id'");
1e73acc8
AS
235 }
236 return 0;
237}
238
d74d639b
AS
239int HUF_func_2mode( I32(* val)(pTHX_ IV, SV*)) {
240 int ans = 0;
241 if (val == &HUF_watch_key_id)
242 ans = 1;
243 if (val == &HUF_watch_key_safe)
244 ans = 2;
245 return(ans);
246}
247
248I32(* HUF_mode_2func( int mode))(pTHX_ IV, SV*) {
249 I32(* ans)(pTHX_ IV, SV*) = NULL;
250 switch (mode) {
251 case 1:
252 ans = &HUF_watch_key_id;
253 break;
254 case 2:
255 ans = &HUF_watch_key_safe;
256 break;
257 }
258 return(ans);
259}
260
51a5b41d
SH
261/* see if something is a field hash */
262int HUF_get_status(HV* hash) {
263 int ans = 0;
264 if (hash && (SvTYPE(hash) == SVt_PVHV)) {
265 MAGIC* mg;
266 struct ufuncs* uf;
267 if ((mg = mg_find((SV*)hash, PERL_MAGIC_uvar)) &&
268 (uf = (struct ufuncs *)mg->mg_ptr) &&
269 (uf->uf_set == NULL)
270 ) {
271 ans = HUF_func_2mode(uf->uf_val);
272 }
273 }
274 return ans;
275}
276
1e73acc8
AS
277/* Thread support. These routines are called by CLONE (and nothing else) */
278
279/* Fix entries for one object in all field hashes */
280void HUF_fix_trigger(SV* trigger, SV* new_id) {
281 AV* cont = HUF_get_trigger_content(trigger);
282 HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
283 HV* new_tab = newHV();
284 HE* ent;
285 SV* old_id = *av_fetch(cont, 0, 0);
286 hv_iterinit(field_tab);
c33e8be1 287 while ((ent = hv_iternext(field_tab))) {
1e73acc8 288 SV* field_ref = HeVAL(ent);
1e73acc8 289 HV* field = (HV*)SvRV(field_ref);
d74d639b 290 UV field_addr = PTR2UV(field);
1e73acc8
AS
291 SV* val;
292 /* recreate field tab entry */
c33e8be1 293 (void) hv_store(new_tab, (char *)&field_addr, sizeof(field_addr), SvREFCNT_inc(field_ref), 0);
1e73acc8 294 /* recreate field entry, if any */
c33e8be1
Z
295 if ((val = hv_delete_ent(field, old_id, 0, 0)))
296 (void) hv_store_ent(field, new_id, SvREFCNT_inc(val), 0);
1e73acc8
AS
297 }
298 /* update the trigger */
299 av_store(cont, 0, SvREFCNT_inc(new_id));
300 av_store(cont, 1, (SV*)new_tab);
301}
302
303/* Go over object registry and fix all objects. Also fix the object
304 * registry.
305 */
ce809d1f 306void HUF_fix_objects(void) {
1e73acc8
AS
307 dMY_CXT;
308 I32 i, len;
309 HE* ent;
310 AV* oblist = (AV*)sv_2mortal((SV*)newAV());
311 hv_iterinit(MY_CXT.ob_reg);
c33e8be1 312 while((ent = hv_iternext(MY_CXT.ob_reg)))
1e73acc8
AS
313 av_push(oblist, SvREFCNT_inc(hv_iterkeysv(ent)));
314 len = av_len(oblist);
315 for (i = 0; i <= len; ++i) {
316 SV* old_id = *av_fetch(oblist, i, 0);
317 SV* trigger = hv_delete_ent(MY_CXT.ob_reg, old_id, 0, 0);
d74d639b
AS
318 SV* obj = SvRV(trigger);
319 MAGIC *mg;
320
321 SV* new_id = HUF_OBJ_ID(obj);
322
323 /* Replace cached object ID with this new one */
324 for (mg = SvMAGIC(obj); mg; mg = mg->mg_moremagic) {
325 if ((mg->mg_type == PERL_MAGIC_ext) &&
326 (mg->mg_private == HUF_IDCACHE)
327 ) {
328 mg->mg_obj = new_id;
329 }
330 }
331
1e73acc8 332 HUF_fix_trigger(trigger, new_id);
c33e8be1 333 (void) hv_store_ent(MY_CXT.ob_reg, new_id, SvREFCNT_inc(trigger), 0);
1e73acc8
AS
334 }
335}
336
337/* test support (not needed for functionality) */
338
339static SV* counter;
1748121c 340I32 HUF_inc_var(pTHX_ IV index, SV* which) {
c33e8be1
Z
341 PERL_UNUSED_ARG(index);
342 PERL_UNUSED_ARG(which);
1e73acc8
AS
343 sv_setiv(counter, 1 + SvIV(counter));
344 return 0;
345}
346
347MODULE = Hash::Util::FieldHash PACKAGE = Hash::Util::FieldHash
348
349BOOT:
350{
351 HUF_global(HUF_INIT); /* create variables */
352}
353
354int
355_fieldhash(SV* href, int mode)
356PROTOTYPE: $$
357CODE:
358 HV* field;
359 RETVAL = 0;
360 if (mode &&
361 href && SvROK(href) &&
362 (field = (HV*)SvRV(href)) &&
363 SvTYPE(field) == SVt_PVHV
364 ) {
d74d639b 365
1e73acc8
AS
366 HUF_add_uvar_magic(
367 SvRV(href),
d74d639b 368 HUF_mode_2func( mode),
1e73acc8
AS
369 NULL,
370 0,
371 NULL
372 );
373 RETVAL = HUF_get_status(field);
374 }
375OUTPUT:
376 RETVAL
377
378void
d74d639b 379id(SV* ref)
3ae03d21 380PROTOTYPE: $
d74d639b
AS
381PPCODE:
382 if (SvROK(ref)) {
383 XPUSHs(HUF_obj_id(ref));
384 } else {
385 XPUSHs(ref);
386 }
387
388SV*
389id_2obj(SV* id)
390PROTOTYPE: $
391CODE:
392 SV* obj = HUF_ask_trigger(id);
393 if (obj) {
394 RETVAL = newRV_inc(SvRV(obj));
395 } else {
396 RETVAL = &PL_sv_undef;
397 }
398OUTPUT:
399 RETVAL
400
401SV*
402register(SV* obj, ...)
403PROTOTYPE: $@
404CODE:
405 SV* trigger;
406 int i;
407 RETVAL = NULL;
408 if (!SvROK(obj)) {
409 Perl_die(aTHX_ "Attempt to register a non-ref");
410 } else {
411 RETVAL = newRV_inc(SvRV(obj));
412 }
413 trigger = HUF_get_trigger(obj, HUF_obj_id(obj));
414 for (i = 1; i < items; ++ i) {
415 SV* field_ref = POPs;
416 if (SvROK(field_ref) && (SvTYPE(SvRV(field_ref)) == SVt_PVHV)) {
417 HUF_mark_field(trigger, SvRV(field_ref));
418 }
419 }
420OUTPUT:
421 RETVAL
422
423void
87d46f97 424CLONE(char* classname)
1e73acc8 425CODE:
87d46f97 426 if (0 == strcmp(classname, "Hash::Util::FieldHash")) {
1e73acc8
AS
427 HUF_global(HUF_CLONE);
428 HUF_fix_objects();
429 }
430
c418a2d4 431void
1e73acc8
AS
432_active_fields(SV* obj)
433PPCODE:
434 if (SvROK(obj)) {
435 SV* ob_id = HUF_obj_id(obj);
436 SV* trigger = HUF_ask_trigger(ob_id);
437 if (trigger) {
438 AV* cont = HUF_get_trigger_content(trigger);
439 HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
440 HE* ent;
441 hv_iterinit(field_tab);
c33e8be1 442 while ((ent = hv_iternext(field_tab))) {
1e73acc8
AS
443 HV* field = (HV*)SvRV(HeVAL(ent));
444 if (hv_exists_ent(field, ob_id, 0))
445 XPUSHs(sv_2mortal(newRV_inc((SV*)field)));
446 }
447 }
448 }
449
450void
451_test_uvar_get(SV* svref, SV* countref)
ff0c75af
NC
452ALIAS:
453_test_uvar_get = 1
454_test_uvar_set = 2
455_test_uvar_same = 3
1e73acc8
AS
456CODE:
457 if (SvROK(svref) && SvROK(countref)) {
458 counter = SvRV(countref);
459 sv_setiv(counter, 0);
460 HUF_add_uvar_magic(
461 SvRV(svref),
ff0c75af
NC
462 ix & 1 ? &HUF_inc_var : 0,
463 ix & 2 ? &HUF_inc_var : 0,
464 0,
1e73acc8
AS
465 SvRV(countref)
466 );
467 }