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