Re: [PATCH] Hash::Util::FieldHash
[p5sagit/p5-mst-13.2.git] / ext / Hash / Util / FieldHash / FieldHash.xs
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 /* support for Hash::Util::FieldHash, prefix HUF_ */
6
7 /* The object registry, a package variable */
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
12
13
14 /* For global cache of object registry */
15 #define MY_CXT_KEY "Hash::Util::FieldHash::_guts" XS_VERSION
16 typedef struct {
17     HV* ob_reg; /* Cache object registry */
18 } my_cxt_t;
19 START_MY_CXT
20
21 /* Deal with global context */
22 #define HUF_INIT 1
23 #define HUF_CLONE 0
24 #define HUF_RESET -1
25
26 void HUF_global(I32 how) {
27     if (how == HUF_INIT) {
28         MY_CXT_INIT;
29         MY_CXT.ob_reg = get_hv(HUF_OB_REG, 1);
30     } else if (how == HUF_CLONE) {
31         MY_CXT_CLONE;
32         MY_CXT.ob_reg = get_hv(HUF_OB_REG, 0);
33     } else if (how == HUF_RESET) {
34         dMY_CXT;
35         MY_CXT.ob_reg = get_hv(HUF_OB_REG, 0);
36     }
37 }
38
39 /* the id as an SV, optionally marked in the nv (unused feature) */
40 SV* HUF_id(SV* ref, NV cookie) {
41     SV* id = sv_newmortal();
42     if (cookie == 0 ) {
43         SvUPGRADE(id, SVt_PVIV);
44     } else {
45         SvUPGRADE(id, SVt_PVNV);
46         SvNV_set(id, cookie);
47         SvNOK_on(id);
48     }
49     SvIV_set(id, (IV)SvRV(ref));
50     SvIOK_on(id);
51     return id;
52 }
53
54 /* plain id, only used for field hash entries in field lists */
55 SV* HUF_field_id(SV* obj) {
56     return HUF_id(obj, 0.0);
57 }
58
59 /* object id (may be different in future) */
60 SV* HUF_obj_id(SV* obj) {
61     return HUF_id(obj, 0.0);
62 }
63
64 /* set up uvar magic for any sv */
65 void HUF_add_uvar_magic(
66     SV* sv,                    /* the sv to enchant, visible to * get/set */
67     I32(* val)(pTHX_ IV, SV*), /* "get" function */
68     I32(* set)(pTHX_ IV, SV*), /* "set" function */
69     I32 index,                 /* get/set will see this */
70     SV* thing                  /* any associated info */
71 ) {
72     MAGIC* mg;
73     struct ufuncs uf;
74         uf.uf_val = val;
75         uf.uf_set = set;
76         uf.uf_index = index;
77     sv_magic(sv, thing, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
78 }
79
80 /* Fetch the data container of a trigger */
81 AV* HUF_get_trigger_content(SV* trigger) {
82     MAGIC* mg;
83     if (trigger && (mg = mg_find(trigger, PERL_MAGIC_uvar)))
84         return (AV*)mg->mg_obj;
85     return NULL;
86 }
87
88 /* Delete an object from all field hashes it may occur in.  Also delete
89  * the object's entry from the object registry.
90  */
91 I32 HUF_destroy_obj(pTHX_ IV index, SV* trigger) {
92     /* Do nothing if the weakref wasn't undef'd.  Also don't bother
93      * during global destruction.  (MY_CXT.ob_reg is sometimes funny there) */
94     if (!SvROK(trigger) && (!PL_in_clean_all)) {
95         dMY_CXT;
96         AV* cont = HUF_get_trigger_content(trigger);
97         SV* ob_id = *av_fetch(cont, 0, 0);
98         HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
99         HE* ent;
100         hv_iterinit(field_tab);
101         while (ent = hv_iternext(field_tab)) {
102             SV* field_ref = HeVAL(ent);
103             SV* field = SvRV(field_ref);
104             hv_delete_ent((HV*)field, ob_id, G_DISCARD, 0);
105         }
106         /* make it safe in case we must run in global clenaup, after all */
107         if (PL_in_clean_all)
108             HUF_global(HUF_RESET);
109         hv_delete_ent(MY_CXT.ob_reg, ob_id, G_DISCARD, 0);
110     }
111     return 0;
112 }
113
114 /* Create a trigger for an object.  The trigger is a magical weak ref
115  * that fires when the weak ref expires.  it holds the original id of
116  * the object, and a list of field hashes from which the object may
117  * have to be deleted.  The trigger is stored in the object registry
118  * and also deleted when the object expires.
119  */
120 SV* HUF_new_trigger(SV* obj, SV* ob_id) {
121     dMY_CXT;
122     SV* trigger = sv_rvweaken(newRV_inc(SvRV(obj)));
123     AV* cont = newAV();
124     sv_2mortal((SV*)cont);
125     av_store(cont, 0, SvREFCNT_inc(ob_id));
126     av_store(cont, 1, (SV*)newHV());
127     HUF_add_uvar_magic(trigger, NULL, &HUF_destroy_obj, 0, (SV*)cont);
128     hv_store_ent(MY_CXT.ob_reg, ob_id, trigger, 0);
129     return trigger;
130 }
131
132 /* retrieve a trigger for obj if one exists, return NULL otherwise */
133 SV* HUF_ask_trigger(SV* ob_id) {
134     dMY_CXT;
135     HE* ent;
136     if (ent = hv_fetch_ent(MY_CXT.ob_reg, ob_id, 0, 0))
137         return HeVAL(ent);
138     return NULL;
139 }
140
141 /* get the trigger for an object, creating it if necessary */
142 SV* HUF_get_trigger(SV* obj, SV* ob_id) {
143     SV* trigger;
144     if (!(trigger = HUF_ask_trigger(ob_id)))
145         trigger = HUF_new_trigger(obj, ob_id);
146     return trigger;
147 }
148
149 /* mark an object (trigger) as having been used with a field */
150 void HUF_mark_field(SV* trigger, SV* field) {
151     AV* cont = HUF_get_trigger_content(trigger);
152     HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
153     SV* field_ref = newRV_inc(field);
154     SV* field_id = HUF_field_id(field_ref);
155     hv_store_ent(field_tab, field_id, field_ref, 0);
156 }
157
158 /* The key exchange function.  It communicates with S_hv_magic_uvar_xkey
159  * in hv.c */
160 IV HUF_watch_key(pTHX_ IV action, SV* field) {
161     MAGIC* mg = mg_find(field, PERL_MAGIC_uvar);
162     SV* keysv = mg->mg_obj;
163     if (keysv && SvROK(keysv)) {
164         SV* ob_id = HUF_obj_id(keysv);
165         SV* trigger = HUF_get_trigger(keysv, ob_id);
166         HUF_mark_field(trigger, field);
167         mg->mg_obj = ob_id; /* key replacement */
168     }
169     return 0;
170 }
171
172 /* see if something is a field hash */
173 int HUF_get_status(HV* hash) {
174     int ans = 0;
175     if (hash && (SvTYPE(hash) == SVt_PVHV)) {
176         dMY_CXT;
177         MAGIC* mg;
178         struct ufuncs* uf;
179         ans = (mg = mg_find((SV*)hash, PERL_MAGIC_uvar)) &&
180             (uf = (struct ufuncs *)mg->mg_ptr) &&
181             (uf->uf_val == &HUF_watch_key) &&
182             (uf->uf_set == NULL);
183     }
184     return ans;
185 }
186
187 /* Thread support.  These routines are called by CLONE (and nothing else) */
188
189 /* Fix entries for one object in all field hashes */
190 void HUF_fix_trigger(SV* trigger, SV* new_id) {
191     AV* cont = HUF_get_trigger_content(trigger);
192     HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
193     HV* new_tab = newHV();
194     HE* ent;
195     SV* old_id = *av_fetch(cont, 0, 0);
196     hv_iterinit(field_tab);
197     while (ent = hv_iternext(field_tab)) {
198         SV* field_ref = HeVAL(ent);
199         SV* field_id = HUF_field_id(field_ref);
200         HV* field = (HV*)SvRV(field_ref);
201         SV* val;
202         /* recreate field tab entry */
203         hv_store_ent(new_tab, field_id, SvREFCNT_inc(field_ref), 0);
204         /* recreate field entry, if any */
205         if (val = hv_delete_ent(field, old_id, 0, 0))
206             hv_store_ent(field, new_id, SvREFCNT_inc(val), 0);
207     }
208     /* update the trigger */
209     av_store(cont, 0, SvREFCNT_inc(new_id));
210     av_store(cont, 1, (SV*)new_tab);
211 }
212
213 /* Go over object registry and fix all objects.  Also fix the object
214  * registry.
215  */
216 void HUF_fix_objects() {
217     dMY_CXT;
218     I32 i, len;
219     HE* ent;
220     AV* oblist = (AV*)sv_2mortal((SV*)newAV());
221     hv_iterinit(MY_CXT.ob_reg);
222     while(ent = hv_iternext(MY_CXT.ob_reg))
223         av_push(oblist, SvREFCNT_inc(hv_iterkeysv(ent)));
224     len = av_len(oblist);
225     for (i = 0; i <= len; ++i) {
226         SV* old_id = *av_fetch(oblist, i, 0);
227         SV* trigger = hv_delete_ent(MY_CXT.ob_reg, old_id, 0, 0);
228         SV* new_id = HUF_obj_id(trigger);
229         HUF_fix_trigger(trigger, new_id);
230         hv_store_ent(MY_CXT.ob_reg, new_id, SvREFCNT_inc(trigger), 0);
231     }
232 }
233
234 /* test support (not needed for functionality) */
235
236 static SV* counter;
237 IV HUF_inc_var(pTHX_ IV index, SV* which) {
238     sv_setiv(counter, 1 + SvIV(counter));
239     return 0;
240 }
241
242 MODULE = Hash::Util::FieldHash          PACKAGE = Hash::Util::FieldHash
243
244 BOOT:
245 {
246     HUF_global(HUF_INIT); /* create variables */
247 }
248
249 int
250 _fieldhash(SV* href, int mode)
251 PROTOTYPE: $$
252 CODE:
253     HV* field;
254     RETVAL = 0;
255     if (mode &&
256         href && SvROK(href) &&
257         (field = (HV*)SvRV(href)) &&
258         SvTYPE(field) == SVt_PVHV
259     ) {
260         HUF_add_uvar_magic(
261             SvRV(href),
262             &HUF_watch_key,
263             NULL,
264             0,
265             NULL
266         );
267         RETVAL = HUF_get_status(field);
268     }
269 OUTPUT:
270     RETVAL
271
272 void
273 CLONE(char* class)
274 CODE:
275     if (0 == strcmp(class, "Hash::Util::FieldHash")) {
276         HUF_global(HUF_CLONE);
277         HUF_fix_objects();
278     }
279
280 SV*
281 _get_obj_id(SV* obj)
282 CODE:
283     RETVAL = NULL;
284     if (SvROK(obj))
285         RETVAL = HUF_obj_id(obj);
286 OUTPUT:
287     RETVAL
288
289 SV*
290 _active_fields(SV* obj)
291 PPCODE:
292     if (SvROK(obj)) {
293         SV* ob_id = HUF_obj_id(obj);
294         SV* trigger = HUF_ask_trigger(ob_id);
295         if (trigger) {
296             AV* cont = HUF_get_trigger_content(trigger);
297             HV* field_tab = (HV*) *av_fetch(cont, 1, 0);
298             HE* ent;
299             hv_iterinit(field_tab);
300             while (ent = hv_iternext(field_tab)) {
301                 HV* field = (HV*)SvRV(HeVAL(ent));
302                 if (hv_exists_ent(field, ob_id, 0))
303                     XPUSHs(sv_2mortal(newRV_inc((SV*)field)));
304             }
305         }
306     }
307
308 void
309 _test_uvar_get(SV* svref, SV* countref)
310 CODE:
311     if (SvROK(svref) && SvROK(countref)) {
312         counter = SvRV(countref);
313         sv_setiv(counter, 0);
314         HUF_add_uvar_magic(
315             SvRV(svref),
316             &HUF_inc_var,
317             NULL,
318             0,
319             SvRV(countref)
320         );
321     }
322
323 void
324 _test_uvar_set(SV* svref, SV* countref)
325 CODE:
326     if (SvROK(svref) && SvROK(countref)) {
327         counter = SvRV(countref);
328         sv_setiv(counter, 0);
329         counter = SvRV(countref);
330         HUF_add_uvar_magic(
331             SvRV(svref),
332             NULL,
333             &HUF_inc_var,
334             0,
335             SvRV(countref)
336         );
337     }
338
339 void
340 _test_uvar_same(SV* svref, SV* countref)
341 CODE:
342     if (SvROK(svref) && SvROK(countref)) {
343         counter = SvRV(countref);
344         sv_setiv(counter, 0);
345         HUF_add_uvar_magic(
346             SvRV(svref),
347             &HUF_inc_var,
348             &HUF_inc_var,
349             0,
350             NULL
351         );
352     }
353