5 /* support for Hash::Util::FieldHash, prefix HUF_ */
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
13 /* For global cache of object registry */
14 #define MY_CXT_KEY "Hash::Util::FieldHash::_guts" XS_VERSION
16 HV* ob_reg; /* Cache object registry */
20 /* Inquire the object registry (a lexical hash) from perl */
21 HV* HUF_get_ob_reg(void) {
28 I32 items = call_pv(HUF_OB_REG, G_SCALAR|G_NOARGS);
31 if (items == 1 && TOPs && SvROK(TOPs) && SvTYPE(SvRV(TOPs)) == SVt_PVHV) {
32 ob_reg = (HV*)SvRV(POPs);
40 Perl_die(aTHX_ "Can't get object registry hash");
43 /* Deal with global context */
48 void HUF_global(I32 how) {
49 if (how == HUF_INIT) {
51 MY_CXT.ob_reg = HUF_get_ob_reg();
52 } else if (how == HUF_CLONE) {
54 MY_CXT.ob_reg = HUF_get_ob_reg();
55 } else if (how == HUF_RESET) {
57 MY_CXT.ob_reg = HUF_get_ob_reg();
61 /* the id as an SV, optionally marked in the nv (unused feature) */
62 SV* HUF_id(SV* ref, NV cookie) {
63 SV* id = sv_newmortal();
65 SvUPGRADE(id, SVt_PVIV);
67 SvUPGRADE(id, SVt_PVNV);
71 SvIV_set(id, PTR2UV(SvRV(ref)));
76 /* plain id, only used for field hash entries in field lists */
77 SV* HUF_field_id(SV* obj) {
78 return HUF_id(obj, 0.0);
81 /* object id (same as plain, may be different in future) */
82 SV* HUF_obj_id(SV* obj) {
83 return HUF_id(obj, 0.0);
86 /* set up uvar magic for any sv */
87 void HUF_add_uvar_magic(
88 SV* sv, /* the sv to enchant, visible to get/set */
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 */
98 sv_magic(sv, thing, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
101 /* Fetch the data container of a trigger */
102 AV* HUF_get_trigger_content(SV* trigger) {
104 if (trigger && (mg = mg_find(trigger, PERL_MAGIC_uvar)))
105 return (AV*)mg->mg_obj;
109 /* Delete an object from all field hashes it may occur in. Also delete
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.
113 I32 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)) {
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);
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);
128 /* make it safe in case we must run in global clenaup, after all */
130 HUF_global(HUF_RESET);
131 hv_delete_ent(MY_CXT.ob_reg, ob_id, G_DISCARD, 0);
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.
142 SV* HUF_new_trigger(SV* obj, SV* ob_id) {
144 SV* trigger = sv_rvweaken(newRV_inc(SvRV(obj)));
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);
154 /* retrieve a trigger for obj if one exists, return NULL otherwise */
155 SV* HUF_ask_trigger(SV* ob_id) {
158 if (ent = hv_fetch_ent(MY_CXT.ob_reg, ob_id, 0, 0))
163 /* get the trigger for an object, creating it if necessary */
164 SV* HUF_get_trigger(SV* obj, SV* ob_id) {
166 if (!(trigger = HUF_ask_trigger(ob_id)))
167 trigger = HUF_new_trigger(obj, ob_id);
171 /* mark an object (trigger) as having been used with a field */
172 void 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);
180 /* These constants are not in the API. If they ever change in hv.c this code
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
187 #define HUF_WOULD_CREATE_KEY(x) ((x) != -1 && ((x) & (HV_FETCH_ISSTORE | HV_FETCH_LVALUE)))
189 /* The key exchange function. It communicates with S_hv_magic_uvar_xkey
191 I32 HUF_watch_key(pTHX_ IV action, SV* field) {
192 MAGIC* mg = mg_find(field, PERL_MAGIC_uvar);
195 Perl_die(aTHX_ "Rogue call of 'HUF_watch_key'");
197 if (keysv && SvROK(keysv)) {
198 SV* ob_id = HUF_obj_id(keysv);
199 mg->mg_obj = ob_id; /* key replacement */
200 if (HUF_WOULD_CREATE_KEY(action)) {
201 SV* trigger = HUF_get_trigger(keysv, ob_id);
202 HUF_mark_field(trigger, field);
208 /* see if something is a field hash */
209 int HUF_get_status(HV* hash) {
211 if (hash && (SvTYPE(hash) == SVt_PVHV)) {
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);
222 /* Thread support. These routines are called by CLONE (and nothing else) */
224 /* Fix entries for one object in all field hashes */
225 void 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();
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);
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);
243 /* update the trigger */
244 av_store(cont, 0, SvREFCNT_inc(new_id));
245 av_store(cont, 1, (SV*)new_tab);
248 /* Go over object registry and fix all objects. Also fix the object
251 void HUF_fix_objects(void) {
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);
269 /* test support (not needed for functionality) */
272 I32 HUF_inc_var(pTHX_ IV index, SV* which) {
273 sv_setiv(counter, 1 + SvIV(counter));
277 MODULE = Hash::Util::FieldHash PACKAGE = Hash::Util::FieldHash
281 HUF_global(HUF_INIT); /* create variables */
285 _fieldhash(SV* href, int mode)
291 href && SvROK(href) &&
292 (field = (HV*)SvRV(href)) &&
293 SvTYPE(field) == SVt_PVHV
302 RETVAL = HUF_get_status(field);
310 if (0 == strcmp(class, "Hash::Util::FieldHash")) {
311 HUF_global(HUF_CLONE);
316 _active_fields(SV* obj)
319 SV* ob_id = HUF_obj_id(obj);
320 SV* trigger = HUF_ask_trigger(ob_id);
322 AV* cont = HUF_get_trigger_content(trigger);
323 HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
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)));
335 _test_uvar_get(SV* svref, SV* countref)
337 if (SvROK(svref) && SvROK(countref)) {
338 counter = SvRV(countref);
339 sv_setiv(counter, 0);
350 _test_uvar_set(SV* svref, SV* countref)
352 if (SvROK(svref) && SvROK(countref)) {
353 counter = SvRV(countref);
354 sv_setiv(counter, 0);
355 counter = SvRV(countref);
366 _test_uvar_same(SV* svref, SV* countref)
368 if (SvROK(svref) && SvROK(countref)) {
369 counter = SvRV(countref);
370 sv_setiv(counter, 0);