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) {
29 items = call_pv(HUF_OB_REG, G_SCALAR|G_NOARGS);
32 if (items == 1 && TOPs && SvROK(TOPs) && SvTYPE(SvRV(TOPs)) == SVt_PVHV)
33 ob_reg = (HV*)SvRV(POPs);
39 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);
196 if (keysv && SvROK(keysv)) {
197 SV* ob_id = HUF_obj_id(keysv);
198 mg->mg_obj = ob_id; /* key replacement */
199 if (HUF_WOULD_CREATE_KEY(action)) {
200 SV* trigger = HUF_get_trigger(keysv, ob_id);
201 HUF_mark_field(trigger, field);
205 Perl_die(aTHX_ "Rogue call of 'HUF_watch_key'");
210 /* see if something is a field hash */
211 int HUF_get_status(HV* hash) {
213 if (hash && (SvTYPE(hash) == SVt_PVHV)) {
216 ans = (mg = mg_find((SV*)hash, PERL_MAGIC_uvar)) &&
217 (uf = (struct ufuncs *)mg->mg_ptr) &&
218 (uf->uf_val == &HUF_watch_key) &&
219 (uf->uf_set == NULL);
224 /* Thread support. These routines are called by CLONE (and nothing else) */
226 /* Fix entries for one object in all field hashes */
227 void HUF_fix_trigger(SV* trigger, SV* new_id) {
228 AV* cont = HUF_get_trigger_content(trigger);
229 HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
230 HV* new_tab = newHV();
232 SV* old_id = *av_fetch(cont, 0, 0);
233 hv_iterinit(field_tab);
234 while (ent = hv_iternext(field_tab)) {
235 SV* field_ref = HeVAL(ent);
236 SV* field_id = HUF_field_id(field_ref);
237 HV* field = (HV*)SvRV(field_ref);
239 /* recreate field tab entry */
240 hv_store_ent(new_tab, field_id, SvREFCNT_inc(field_ref), 0);
241 /* recreate field entry, if any */
242 if (val = hv_delete_ent(field, old_id, 0, 0))
243 hv_store_ent(field, new_id, SvREFCNT_inc(val), 0);
245 /* update the trigger */
246 av_store(cont, 0, SvREFCNT_inc(new_id));
247 av_store(cont, 1, (SV*)new_tab);
250 /* Go over object registry and fix all objects. Also fix the object
253 void HUF_fix_objects(void) {
257 AV* oblist = (AV*)sv_2mortal((SV*)newAV());
258 hv_iterinit(MY_CXT.ob_reg);
259 while(ent = hv_iternext(MY_CXT.ob_reg))
260 av_push(oblist, SvREFCNT_inc(hv_iterkeysv(ent)));
261 len = av_len(oblist);
262 for (i = 0; i <= len; ++i) {
263 SV* old_id = *av_fetch(oblist, i, 0);
264 SV* trigger = hv_delete_ent(MY_CXT.ob_reg, old_id, 0, 0);
265 SV* new_id = HUF_obj_id(trigger);
266 HUF_fix_trigger(trigger, new_id);
267 hv_store_ent(MY_CXT.ob_reg, new_id, SvREFCNT_inc(trigger), 0);
271 /* test support (not needed for functionality) */
274 I32 HUF_inc_var(pTHX_ IV index, SV* which) {
275 sv_setiv(counter, 1 + SvIV(counter));
279 MODULE = Hash::Util::FieldHash PACKAGE = Hash::Util::FieldHash
283 HUF_global(HUF_INIT); /* create variables */
287 _fieldhash(SV* href, int mode)
293 href && SvROK(href) &&
294 (field = (HV*)SvRV(href)) &&
295 SvTYPE(field) == SVt_PVHV
304 RETVAL = HUF_get_status(field);
310 CLONE(char* classname)
312 if (0 == strcmp(classname, "Hash::Util::FieldHash")) {
313 HUF_global(HUF_CLONE);
318 _active_fields(SV* obj)
321 SV* ob_id = HUF_obj_id(obj);
322 SV* trigger = HUF_ask_trigger(ob_id);
324 AV* cont = HUF_get_trigger_content(trigger);
325 HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
327 hv_iterinit(field_tab);
328 while (ent = hv_iternext(field_tab)) {
329 HV* field = (HV*)SvRV(HeVAL(ent));
330 if (hv_exists_ent(field, ob_id, 0))
331 XPUSHs(sv_2mortal(newRV_inc((SV*)field)));
337 _test_uvar_get(SV* svref, SV* countref)
339 if (SvROK(svref) && SvROK(countref)) {
340 counter = SvRV(countref);
341 sv_setiv(counter, 0);
352 _test_uvar_set(SV* svref, SV* countref)
354 if (SvROK(svref) && SvROK(countref)) {
355 counter = SvRV(countref);
356 sv_setiv(counter, 0);
357 counter = SvRV(countref);
368 _test_uvar_same(SV* svref, SV* countref)
370 if (SvROK(svref) && SvROK(countref)) {
371 counter = SvRV(countref);
372 sv_setiv(counter, 0);