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) {
23 I32 items = call_pv(HUF_OB_REG, G_SCALAR|G_NOARGS);
28 if (ref && SvROK(ref) && SvTYPE(SvRV(ref)) == SVt_PVHV)
29 return (HV*)SvRV(ref);
31 Perl_die(aTHX_ "Can't get object registry hash");
34 /* Deal with global context */
39 void HUF_global(I32 how) {
40 if (how == HUF_INIT) {
42 MY_CXT.ob_reg = HUF_get_ob_reg();
43 } else if (how == HUF_CLONE) {
45 MY_CXT.ob_reg = HUF_get_ob_reg();
46 } else if (how == HUF_RESET) {
48 MY_CXT.ob_reg = HUF_get_ob_reg();
52 /* the id as an SV, optionally marked in the nv (unused feature) */
53 SV* HUF_id(SV* ref, NV cookie) {
54 SV* id = sv_newmortal();
56 SvUPGRADE(id, SVt_PVIV);
58 SvUPGRADE(id, SVt_PVNV);
62 SvIV_set(id, PTR2UV(SvRV(ref)));
67 /* plain id, only used for field hash entries in field lists */
68 SV* HUF_field_id(SV* obj) {
69 return HUF_id(obj, 0.0);
72 /* object id (same as plain, may be different in future) */
73 SV* HUF_obj_id(SV* obj) {
74 return HUF_id(obj, 0.0);
77 /* set up uvar magic for any sv */
78 void HUF_add_uvar_magic(
79 SV* sv, /* the sv to enchant, visible to get/set */
80 I32(* val)(pTHX_ IV, SV*), /* "get" function */
81 I32(* set)(pTHX_ IV, SV*), /* "set" function */
82 I32 index, /* get/set will see this */
83 SV* thing /* any associated info */
89 sv_magic(sv, thing, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
92 /* Fetch the data container of a trigger */
93 AV* HUF_get_trigger_content(SV* trigger) {
95 if (trigger && (mg = mg_find(trigger, PERL_MAGIC_uvar)))
96 return (AV*)mg->mg_obj;
100 /* Delete an object from all field hashes it may occur in. Also delete
101 * the object's entry from the object registry. This function goes in
102 * the uf_set field of the uvar magic of a trigger.
104 I32 HUF_destroy_obj(pTHX_ IV index, SV* trigger) {
105 /* Do nothing if the weakref wasn't undef'd. Also don't bother
106 * during global destruction. (MY_CXT.ob_reg is sometimes funny there) */
107 if (!SvROK(trigger) && (!PL_in_clean_all)) {
109 AV* cont = HUF_get_trigger_content(trigger);
110 SV* ob_id = *av_fetch(cont, 0, 0);
111 HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
113 hv_iterinit(field_tab);
114 while (ent = hv_iternext(field_tab)) {
115 SV* field_ref = HeVAL(ent);
116 SV* field = SvRV(field_ref);
117 hv_delete_ent((HV*)field, ob_id, G_DISCARD, 0);
119 /* make it safe in case we must run in global clenaup, after all */
121 HUF_global(HUF_RESET);
122 hv_delete_ent(MY_CXT.ob_reg, ob_id, G_DISCARD, 0);
127 /* Create a trigger for an object. The trigger is a magical weak ref
128 * that fires when the weak ref expires. it holds the original id of
129 * the object, and a list of field hashes from which the object may
130 * have to be deleted. The trigger is stored in the object registry
131 * and also deleted when the object expires.
133 SV* HUF_new_trigger(SV* obj, SV* ob_id) {
135 SV* trigger = sv_rvweaken(newRV_inc(SvRV(obj)));
137 sv_2mortal((SV*)cont);
138 av_store(cont, 0, SvREFCNT_inc(ob_id));
139 av_store(cont, 1, (SV*)newHV());
140 HUF_add_uvar_magic(trigger, NULL, &HUF_destroy_obj, 0, (SV*)cont);
141 hv_store_ent(MY_CXT.ob_reg, ob_id, trigger, 0);
145 /* retrieve a trigger for obj if one exists, return NULL otherwise */
146 SV* HUF_ask_trigger(SV* ob_id) {
149 if (ent = hv_fetch_ent(MY_CXT.ob_reg, ob_id, 0, 0))
154 /* get the trigger for an object, creating it if necessary */
155 SV* HUF_get_trigger(SV* obj, SV* ob_id) {
157 if (!(trigger = HUF_ask_trigger(ob_id)))
158 trigger = HUF_new_trigger(obj, ob_id);
162 /* mark an object (trigger) as having been used with a field */
163 void HUF_mark_field(SV* trigger, SV* field) {
164 AV* cont = HUF_get_trigger_content(trigger);
165 HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
166 SV* field_ref = newRV_inc(field);
167 SV* field_id = HUF_field_id(field_ref);
168 hv_store_ent(field_tab, field_id, field_ref, 0);
171 /* These constants are not in the API. If they ever change in hv.c this code
173 #define HV_FETCH_ISSTORE 0x01
174 #define HV_FETCH_ISEXISTS 0x02
175 #define HV_FETCH_LVALUE 0x04
176 #define HV_FETCH_JUST_SV 0x08
178 #define HUF_WOULD_CREATE_KEY(x) ((x) != -1 && ((x) & (HV_FETCH_ISSTORE | HV_FETCH_LVALUE)))
180 /* The key exchange function. It communicates with S_hv_magic_uvar_xkey
182 I32 HUF_watch_key(pTHX_ IV action, SV* field) {
183 MAGIC* mg = mg_find(field, PERL_MAGIC_uvar);
186 Perl_die(aTHX_ "Rogue call of 'HUF_watch_key'");
188 if (keysv && SvROK(keysv)) {
189 SV* ob_id = HUF_obj_id(keysv);
190 mg->mg_obj = ob_id; /* key replacement */
191 if (HUF_WOULD_CREATE_KEY(action)) {
192 SV* trigger = HUF_get_trigger(keysv, ob_id);
193 HUF_mark_field(trigger, field);
199 /* see if something is a field hash */
200 int HUF_get_status(HV* hash) {
202 if (hash && (SvTYPE(hash) == SVt_PVHV)) {
205 ans = (mg = mg_find((SV*)hash, PERL_MAGIC_uvar)) &&
206 (uf = (struct ufuncs *)mg->mg_ptr) &&
207 (uf->uf_val == &HUF_watch_key) &&
208 (uf->uf_set == NULL);
213 /* Thread support. These routines are called by CLONE (and nothing else) */
215 /* Fix entries for one object in all field hashes */
216 void HUF_fix_trigger(SV* trigger, SV* new_id) {
217 AV* cont = HUF_get_trigger_content(trigger);
218 HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
219 HV* new_tab = newHV();
221 SV* old_id = *av_fetch(cont, 0, 0);
222 hv_iterinit(field_tab);
223 while (ent = hv_iternext(field_tab)) {
224 SV* field_ref = HeVAL(ent);
225 SV* field_id = HUF_field_id(field_ref);
226 HV* field = (HV*)SvRV(field_ref);
228 /* recreate field tab entry */
229 hv_store_ent(new_tab, field_id, SvREFCNT_inc(field_ref), 0);
230 /* recreate field entry, if any */
231 if (val = hv_delete_ent(field, old_id, 0, 0))
232 hv_store_ent(field, new_id, SvREFCNT_inc(val), 0);
234 /* update the trigger */
235 av_store(cont, 0, SvREFCNT_inc(new_id));
236 av_store(cont, 1, (SV*)new_tab);
239 /* Go over object registry and fix all objects. Also fix the object
242 void HUF_fix_objects(void) {
246 AV* oblist = (AV*)sv_2mortal((SV*)newAV());
247 hv_iterinit(MY_CXT.ob_reg);
248 while(ent = hv_iternext(MY_CXT.ob_reg))
249 av_push(oblist, SvREFCNT_inc(hv_iterkeysv(ent)));
250 len = av_len(oblist);
251 for (i = 0; i <= len; ++i) {
252 SV* old_id = *av_fetch(oblist, i, 0);
253 SV* trigger = hv_delete_ent(MY_CXT.ob_reg, old_id, 0, 0);
254 SV* new_id = HUF_obj_id(trigger);
255 HUF_fix_trigger(trigger, new_id);
256 hv_store_ent(MY_CXT.ob_reg, new_id, SvREFCNT_inc(trigger), 0);
260 /* test support (not needed for functionality) */
263 I32 HUF_inc_var(pTHX_ IV index, SV* which) {
264 sv_setiv(counter, 1 + SvIV(counter));
268 MODULE = Hash::Util::FieldHash PACKAGE = Hash::Util::FieldHash
272 HUF_global(HUF_INIT); /* create variables */
276 _fieldhash(SV* href, int mode)
282 href && SvROK(href) &&
283 (field = (HV*)SvRV(href)) &&
284 SvTYPE(field) == SVt_PVHV
293 RETVAL = HUF_get_status(field);
301 if (0 == strcmp(class, "Hash::Util::FieldHash")) {
302 HUF_global(HUF_CLONE);
307 _active_fields(SV* obj)
310 SV* ob_id = HUF_obj_id(obj);
311 SV* trigger = HUF_ask_trigger(ob_id);
313 AV* cont = HUF_get_trigger_content(trigger);
314 HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
316 hv_iterinit(field_tab);
317 while (ent = hv_iternext(field_tab)) {
318 HV* field = (HV*)SvRV(HeVAL(ent));
319 if (hv_exists_ent(field, ob_id, 0))
320 XPUSHs(sv_2mortal(newRV_inc((SV*)field)));
326 _test_uvar_get(SV* svref, SV* countref)
328 if (SvROK(svref) && SvROK(countref)) {
329 counter = SvRV(countref);
330 sv_setiv(counter, 0);
341 _test_uvar_set(SV* svref, SV* countref)
343 if (SvROK(svref) && SvROK(countref)) {
344 counter = SvRV(countref);
345 sv_setiv(counter, 0);
346 counter = SvRV(countref);
357 _test_uvar_same(SV* svref, SV* countref)
359 if (SvROK(svref) && SvROK(countref)) {
360 counter = SvRV(countref);
361 sv_setiv(counter, 0);