1 #define PERL_NO_GET_CONTEXT
7 /* support for Hash::Util::FieldHash, prefix HUF_ */
9 /* A Perl sub that returns a hashref to the object registry */
10 #define HUF_OB_REG "Hash::Util::FieldHash::_ob_reg"
11 /* Identifier for PERL_MAGIC_ext magic */
12 #define HUF_IDCACHE 0x4944
14 /* For global cache of object registry */
15 #define MY_CXT_KEY "Hash::Util::FieldHash::_guts" XS_VERSION
17 HV* ob_reg; /* Cache object registry */
21 /* Inquire the object registry (a lexical hash) from perl */
23 HUF_get_ob_reg(pTHX) {
31 items = call_pv(HUF_OB_REG, G_SCALAR|G_NOARGS);
34 if (items == 1 && TOPs && SvROK(TOPs) && SvTYPE(SvRV(TOPs)) == SVt_PVHV)
35 ob_reg = (HV*)SvRV(POPs);
41 Perl_die(aTHX_ "Can't get object registry hash");
45 /* Deal with global context */
51 HUF_global(pTHX_ I32 how) {
52 if (how == HUF_INIT) {
54 MY_CXT.ob_reg = HUF_get_ob_reg(aTHX);
55 } else if (how == HUF_CLONE) {
57 MY_CXT.ob_reg = HUF_get_ob_reg(aTHX);
58 } else if (how == HUF_RESET) {
60 MY_CXT.ob_reg = HUF_get_ob_reg(aTHX);
66 /* definition of id transformation */
67 #define HUF_OBJ_ID(x) newSVuv(PTR2UV(x))
70 HUF_obj_id(pTHX_ SV *obj) {
75 /* Get cached object ID, if it exists */
76 if (SvTYPE(item) >= SVt_PVMG) {
77 for ( mg = SvMAGIC(item); mg; mg = mg->mg_moremagic ) {
78 if ((mg->mg_type == PERL_MAGIC_ext) &&
79 (mg->mg_private == HUF_IDCACHE)
86 /* Create an object ID, cache it */
87 id = HUF_OBJ_ID(item);
88 mg = sv_magicext(item, id, PERL_MAGIC_ext, NULL, NULL, 0);
89 mg->mg_private = HUF_IDCACHE;
90 SvREFCNT_dec(id); /* refcnt++ in sv_magicext() */
92 /* Return the object ID */
96 /* set up uvar magic for any sv */
100 SV* sv, /* the sv to enchant, visible to get/set */
101 I32(* val)(pTHX_ IV, SV*), /* "get" function */
102 I32(* set)(pTHX_ IV, SV*), /* "set" function */
103 I32 index, /* get/set will see this */
104 SV* thing /* any associated info */
110 sv_magic(sv, thing, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
113 /* Fetch the data container of a trigger */
115 HUF_get_trigger_content(pTHX_ SV *trigger) {
117 if (trigger && (mg = mg_find(trigger, PERL_MAGIC_uvar)))
118 return (AV*)mg->mg_obj;
122 /* Delete an object from all field hashes it may occur in. Also delete
123 * the object's entry from the object registry. This function goes in
124 * the uf_set field of the uvar magic of a trigger.
126 static I32 HUF_destroy_obj(pTHX_ IV index, SV *trigger) {
127 PERL_UNUSED_ARG(index);
128 /* Do nothing if the weakref wasn't undef'd. Also don't bother
129 * during global destruction. (MY_CXT.ob_reg is sometimes funny there) */
130 if (!SvROK(trigger) && (!PL_in_clean_all)) {
132 AV* cont = HUF_get_trigger_content(aTHX_ trigger);
133 SV* ob_id = *av_fetch(cont, 0, 0);
134 HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
136 hv_iterinit(field_tab);
137 while ((ent = hv_iternext(field_tab))) {
138 SV* field_ref = HeVAL(ent);
139 SV* field = SvRV(field_ref);
140 (void) hv_delete_ent((HV*)field, ob_id, 0, 0);
142 /* make it safe in case we must run in global clenaup, after all */
144 HUF_global(aTHX_ HUF_RESET); /* shoudn't be needed */
145 (void) hv_delete_ent(MY_CXT.ob_reg, ob_id, 0, 0);
150 /* Create a trigger for an object. The trigger is a magical SV
151 * that holds a weak ref to the object. The magic fires when the object
152 * expires and takes care of garbage collection in registred hashes.
153 * For that purpose, the magic structure holds the original id of
154 * the object, and a list (a hash, really) of hashes from which the
155 * object may * have to be deleted. The trigger is stored in the
156 * object registry and is also deleted when the object expires.
159 HUF_new_trigger(pTHX_ SV *obj, SV *ob_id) {
161 SV* trigger = sv_rvweaken(newRV_inc(SvRV(obj)));
163 sv_2mortal((SV*)cont);
164 av_store(cont, 0, SvREFCNT_inc(ob_id));
165 av_store(cont, 1, (SV*)newHV());
166 HUF_add_uvar_magic(aTHX_ trigger, NULL, &HUF_destroy_obj, 0, (SV*)cont);
167 (void) hv_store_ent(MY_CXT.ob_reg, ob_id, trigger, 0);
171 /* retrieve a trigger for obj if one exists, return NULL otherwise */
173 HUF_ask_trigger(pTHX_ SV *ob_id) {
176 if ((ent = hv_fetch_ent(MY_CXT.ob_reg, ob_id, 0, 0)))
181 /* get the trigger for an object, creating it if necessary */
183 HUF_get_trigger0(pTHX_ SV *obj, SV *ob_id) {
185 if (!(trigger = HUF_ask_trigger(aTHX_ ob_id)))
186 trigger = HUF_new_trigger(aTHX_ obj, ob_id);
191 HUF_get_trigger(pTHX_ SV *obj, SV *ob_id) {
192 SV* trigger = HUF_ask_trigger(aTHX_ ob_id);
194 trigger = HUF_new_trigger(aTHX_ obj, ob_id);
198 /* mark an object (trigger) as having been used with a field
202 HUF_mark_field(pTHX_ SV *trigger, SV *field) {
203 AV* cont = HUF_get_trigger_content(aTHX_ trigger);
204 HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
205 SV* field_ref = newRV_inc(field);
206 UV field_addr = PTR2UV(field);
207 (void) hv_store(field_tab, (char *)&field_addr, sizeof(field_addr), field_ref, 0);
210 /* Determine, from the value of action, whether this call may create a new
212 #define HUF_WOULD_CREATE_KEY(x) ((x) != HV_DELETE && ((x) & (HV_FETCH_ISSTORE | HV_FETCH_LVALUE)))
214 /* The key exchange functions. They communicate with S_hv_magic_uvar_xkey
216 static I32 HUF_watch_key_safe(pTHX_ IV action, SV* field) {
217 MAGIC* mg = mg_find(field, PERL_MAGIC_uvar);
219 if (mg && (keysv = mg->mg_obj)) {
220 if (SvROK(keysv)) { /* ref key */
221 SV* ob_id = HUF_obj_id(aTHX_ keysv);
222 mg->mg_obj = ob_id; /* key replacement */
223 if (HUF_WOULD_CREATE_KEY(action)) {
224 SV* trigger = HUF_get_trigger(aTHX_ keysv, ob_id);
225 HUF_mark_field(aTHX_ trigger, field);
227 } else if (HUF_WOULD_CREATE_KEY(action)) { /* string key */
228 /* registered as object id? */
230 if (( trigger = HUF_ask_trigger(aTHX_ keysv)))
231 HUF_mark_field(aTHX_ trigger, field);
234 Perl_die(aTHX_ "Rogue call of 'HUF_watch_key_safe'");
239 static I32 HUF_watch_key_id(pTHX_ IV action, SV* field) {
240 MAGIC* mg = mg_find(field, PERL_MAGIC_uvar);
242 PERL_UNUSED_ARG(action);
243 if (mg && (keysv = mg->mg_obj)) {
244 if (SvROK(keysv)) /* ref key */
245 mg->mg_obj = HUF_obj_id(aTHX_ keysv); /* key replacement */
247 Perl_die(aTHX_ "Rogue call of 'HUF_watch_key_id'");
252 static int HUF_func_2mode( I32(* val)(pTHX_ IV, SV*)) {
254 if (val == &HUF_watch_key_id)
256 if (val == &HUF_watch_key_safe)
261 static I32(* HUF_mode_2func( int mode))(pTHX_ IV, SV*) {
262 I32(* ans)(pTHX_ IV, SV*) = NULL;
265 ans = &HUF_watch_key_id;
268 ans = &HUF_watch_key_safe;
274 /* see if something is a field hash */
276 HUF_get_status(pTHX_ HV *hash) {
278 if (hash && (SvTYPE(hash) == SVt_PVHV)) {
281 if ((mg = mg_find((SV*)hash, PERL_MAGIC_uvar)) &&
282 (uf = (struct ufuncs *)mg->mg_ptr) &&
285 ans = HUF_func_2mode(uf->uf_val);
291 /* Thread support. These routines are called by CLONE (and nothing else) */
293 /* Fix entries for one object in all field hashes */
295 HUF_fix_trigger(pTHX_ SV *trigger, SV *new_id) {
296 AV* cont = HUF_get_trigger_content(aTHX_ trigger);
297 HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
298 HV* new_tab = newHV();
300 SV* old_id = *av_fetch(cont, 0, 0);
301 hv_iterinit(field_tab);
302 while ((ent = hv_iternext(field_tab))) {
303 SV* field_ref = HeVAL(ent);
304 HV* field = (HV*)SvRV(field_ref);
305 UV field_addr = PTR2UV(field);
307 /* recreate field tab entry */
308 (void) hv_store(new_tab, (char *)&field_addr, sizeof(field_addr), SvREFCNT_inc(field_ref), 0);
309 /* recreate field entry, if any */
310 if ((val = hv_delete_ent(field, old_id, 0, 0)))
311 (void) hv_store_ent(field, new_id, SvREFCNT_inc(val), 0);
313 /* update the trigger */
314 av_store(cont, 0, SvREFCNT_inc(new_id));
315 av_store(cont, 1, (SV*)new_tab);
318 /* Go over object registry and fix all objects. Also fix the object
322 HUF_fix_objects(pTHX) {
326 AV* oblist = (AV*)sv_2mortal((SV*)newAV());
327 hv_iterinit(MY_CXT.ob_reg);
328 while((ent = hv_iternext(MY_CXT.ob_reg)))
329 av_push(oblist, SvREFCNT_inc(hv_iterkeysv(ent)));
330 len = av_tindex(oblist);
331 for (i = 0; i <= len; ++i) {
332 SV* old_id = *av_fetch(oblist, i, 0);
333 SV* trigger = hv_delete_ent(MY_CXT.ob_reg, old_id, 0, 0);
334 SV* obj = SvRV(trigger);
337 SV* new_id = HUF_OBJ_ID(obj);
339 /* Replace cached object ID with this new one */
340 for (mg = SvMAGIC(obj); mg; mg = mg->mg_moremagic) {
341 if ((mg->mg_type == PERL_MAGIC_ext) &&
342 (mg->mg_private == HUF_IDCACHE)
348 HUF_fix_trigger(aTHX_ trigger, new_id);
349 (void) hv_store_ent(MY_CXT.ob_reg, new_id, SvREFCNT_inc(trigger), 0);
353 /* test support (not needed for functionality) */
356 I32 HUF_inc_var(pTHX_ IV index, SV* which) {
357 PERL_UNUSED_ARG(index);
358 PERL_UNUSED_ARG(which);
359 sv_setiv(counter, 1 + SvIV(counter));
363 MODULE = Hash::Util::FieldHash PACKAGE = Hash::Util::FieldHash
367 HUF_global(aTHX_ HUF_INIT); /* create variables */
371 _fieldhash(SV* href, int mode)
377 href && SvROK(href) &&
378 (field = (HV*)SvRV(href)) &&
379 SvTYPE(field) == SVt_PVHV
385 HUF_mode_2func(mode),
390 RETVAL = HUF_get_status(aTHX_ field);
400 XPUSHs(HUF_obj_id(aTHX_ ref));
409 SV* obj = HUF_ask_trigger(aTHX_ id);
411 RETVAL = newRV_inc(SvRV(obj));
413 RETVAL = &PL_sv_undef;
419 register(SV* obj, ...)
426 Perl_die(aTHX_ "Attempt to register a non-ref");
428 RETVAL = newRV_inc(SvRV(obj));
430 trigger = HUF_get_trigger(aTHX_ obj, HUF_obj_id(aTHX_ obj));
431 for (i = 1; i < items; ++ i) {
432 SV* field_ref = POPs;
433 if (SvROK(field_ref) && (SvTYPE(SvRV(field_ref)) == SVt_PVHV)) {
434 HUF_mark_field(aTHX_ trigger, SvRV(field_ref));
441 CLONE(char* classname)
443 if (0 == strcmp(classname, "Hash::Util::FieldHash")) {
444 HUF_global(aTHX_ HUF_CLONE);
445 HUF_fix_objects(aTHX);
449 _active_fields(SV* obj)
452 SV* ob_id = HUF_obj_id(aTHX_ obj);
453 SV* trigger = HUF_ask_trigger(aTHX_ ob_id);
455 AV* cont = HUF_get_trigger_content(aTHX_ trigger);
456 HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
458 hv_iterinit(field_tab);
459 while ((ent = hv_iternext(field_tab))) {
460 HV* field = (HV*)SvRV(HeVAL(ent));
461 if (hv_exists_ent(field, ob_id, 0))
462 XPUSHs(sv_2mortal(newRV_inc((SV*)field)));
468 _test_uvar_get(SV* svref, SV* countref)
474 if (SvROK(svref) && SvROK(countref)) {
475 counter = SvRV(countref);
476 sv_setiv(counter, 0);
480 ix & 1 ? &HUF_inc_var : 0,
481 ix & 2 ? &HUF_inc_var : 0,