This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix change #28552: VC++ doesn't like code before declarations
[perl5.git] / ext / Hash / Util / FieldHash / FieldHash.xs
CommitLineData
1e73acc8
AS
1#include "EXTERN.h"
2#include "perl.h"
3#include "XSUB.h"
4
5/* support for Hash::Util::FieldHash, prefix HUF_ */
6
6ff38c27
AS
7/* A Perl sub that returns a hashref to the object registry */
8#define HUF_OB_REG "Hash::Util::FieldHash::_ob_reg"
1e73acc8
AS
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
1e73acc8
AS
13/* For global cache of object registry */
14#define MY_CXT_KEY "Hash::Util::FieldHash::_guts" XS_VERSION
15typedef struct {
16 HV* ob_reg; /* Cache object registry */
17} my_cxt_t;
18START_MY_CXT
19
6ff38c27
AS
20/* Inquire the object registry (a lexical hash) from perl */
21HV* HUF_get_ob_reg(void) {
22 dSP;
91dba0be 23 HV* ob_reg = NULL;
0dbc1c87 24 I32 items;
91dba0be
AS
25 ENTER;
26 SAVETMPS;
27
28 PUSHMARK(SP);
0dbc1c87 29 items = call_pv(HUF_OB_REG, G_SCALAR|G_NOARGS);
6ff38c27 30 SPAGAIN;
91dba0be
AS
31
32 if (items == 1 && TOPs && SvROK(TOPs) && SvTYPE(SvRV(TOPs)) == SVt_PVHV) {
33 ob_reg = (HV*)SvRV(POPs);
6ff38c27 34 }
91dba0be
AS
35 PUTBACK;
36 FREETMPS;
37 LEAVE;
38
39 if (ob_reg)
40 return ob_reg;
6ff38c27
AS
41 Perl_die(aTHX_ "Can't get object registry hash");
42}
43
1e73acc8
AS
44/* Deal with global context */
45#define HUF_INIT 1
46#define HUF_CLONE 0
47#define HUF_RESET -1
48
49void HUF_global(I32 how) {
50 if (how == HUF_INIT) {
51 MY_CXT_INIT;
6ff38c27 52 MY_CXT.ob_reg = HUF_get_ob_reg();
1e73acc8
AS
53 } else if (how == HUF_CLONE) {
54 MY_CXT_CLONE;
6ff38c27 55 MY_CXT.ob_reg = HUF_get_ob_reg();
1e73acc8
AS
56 } else if (how == HUF_RESET) {
57 dMY_CXT;
6ff38c27 58 MY_CXT.ob_reg = HUF_get_ob_reg();
1e73acc8
AS
59 }
60}
61
62/* the id as an SV, optionally marked in the nv (unused feature) */
63SV* 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 }
ce809d1f 72 SvIV_set(id, PTR2UV(SvRV(ref)));
1e73acc8
AS
73 SvIOK_on(id);
74 return id;
75}
76
77/* plain id, only used for field hash entries in field lists */
78SV* HUF_field_id(SV* obj) {
79 return HUF_id(obj, 0.0);
80}
81
6ff38c27 82/* object id (same as plain, may be different in future) */
1e73acc8
AS
83SV* HUF_obj_id(SV* obj) {
84 return HUF_id(obj, 0.0);
85}
86
87/* set up uvar magic for any sv */
88void HUF_add_uvar_magic(
6ff38c27 89 SV* sv, /* the sv to enchant, visible to get/set */
1e73acc8
AS
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) {
1e73acc8
AS
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 */
103AV* 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
c418a2d4
AS
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.
1e73acc8
AS
113 */
114I32 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 */
143SV* 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 */
156SV* 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 */
165SV* 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 */
173void 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
6ff38c27
AS
181/* These constants are not in the API. If they ever change in hv.c this code
182 * must be updated */
a607227a
AS
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
1e73acc8
AS
190/* The key exchange function. It communicates with S_hv_magic_uvar_xkey
191 * in hv.c */
1748121c 192I32 HUF_watch_key(pTHX_ IV action, SV* field) {
1e73acc8 193 MAGIC* mg = mg_find(field, PERL_MAGIC_uvar);
6ff38c27
AS
194 SV* keysv;
195 if (!mg)
196 Perl_die(aTHX_ "Rogue call of 'HUF_watch_key'");
197 keysv = mg->mg_obj;
1e73acc8
AS
198 if (keysv && SvROK(keysv)) {
199 SV* ob_id = HUF_obj_id(keysv);
1e73acc8 200 mg->mg_obj = ob_id; /* key replacement */
a607227a
AS
201 if (HUF_WOULD_CREATE_KEY(action)) {
202 SV* trigger = HUF_get_trigger(keysv, ob_id);
203 HUF_mark_field(trigger, field);
204 }
1e73acc8
AS
205 }
206 return 0;
207}
208
209/* see if something is a field hash */
210int HUF_get_status(HV* hash) {
211 int ans = 0;
212 if (hash && (SvTYPE(hash) == SVt_PVHV)) {
1e73acc8
AS
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 */
226void 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 */
ce809d1f 252void HUF_fix_objects(void) {
1e73acc8
AS
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
272static SV* counter;
1748121c 273I32 HUF_inc_var(pTHX_ IV index, SV* which) {
1e73acc8
AS
274 sv_setiv(counter, 1 + SvIV(counter));
275 return 0;
276}
277
278MODULE = Hash::Util::FieldHash PACKAGE = Hash::Util::FieldHash
279
280BOOT:
281{
282 HUF_global(HUF_INIT); /* create variables */
283}
284
285int
286_fieldhash(SV* href, int mode)
287PROTOTYPE: $$
288CODE:
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 }
305OUTPUT:
306 RETVAL
307
308void
309CLONE(char* class)
310CODE:
311 if (0 == strcmp(class, "Hash::Util::FieldHash")) {
312 HUF_global(HUF_CLONE);
313 HUF_fix_objects();
314 }
315
c418a2d4 316void
1e73acc8
AS
317_active_fields(SV* obj)
318PPCODE:
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
335void
336_test_uvar_get(SV* svref, SV* countref)
337CODE:
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
350void
351_test_uvar_set(SV* svref, SV* countref)
352CODE:
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
366void
367_test_uvar_same(SV* svref, SV* countref)
368CODE:
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