Commit | Line | Data |
1e73acc8 |
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 | ) { |
1e73acc8 |
72 | struct ufuncs uf; |
73 | uf.uf_val = val; |
74 | uf.uf_set = set; |
75 | uf.uf_index = index; |
76 | sv_magic(sv, thing, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf)); |
77 | } |
78 | |
79 | /* Fetch the data container of a trigger */ |
80 | AV* HUF_get_trigger_content(SV* trigger) { |
81 | MAGIC* mg; |
82 | if (trigger && (mg = mg_find(trigger, PERL_MAGIC_uvar))) |
83 | return (AV*)mg->mg_obj; |
84 | return NULL; |
85 | } |
86 | |
87 | /* Delete an object from all field hashes it may occur in. Also delete |
c418a2d4 |
88 | * the object's entry from the object registry. This function goes in |
89 | * the uf_set field of the uvar magic of a trigger. |
1e73acc8 |
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 | |
a607227a |
158 | #define HV_FETCH_ISSTORE 0x01 |
159 | #define HV_FETCH_ISEXISTS 0x02 |
160 | #define HV_FETCH_LVALUE 0x04 |
161 | #define HV_FETCH_JUST_SV 0x08 |
162 | |
163 | #define HUF_WOULD_CREATE_KEY(x) ((x) != -1 && ((x) & (HV_FETCH_ISSTORE | HV_FETCH_LVALUE))) |
164 | |
1e73acc8 |
165 | /* The key exchange function. It communicates with S_hv_magic_uvar_xkey |
166 | * in hv.c */ |
1748121c |
167 | I32 HUF_watch_key(pTHX_ IV action, SV* field) { |
1e73acc8 |
168 | MAGIC* mg = mg_find(field, PERL_MAGIC_uvar); |
169 | SV* keysv = mg->mg_obj; |
170 | if (keysv && SvROK(keysv)) { |
171 | SV* ob_id = HUF_obj_id(keysv); |
1e73acc8 |
172 | mg->mg_obj = ob_id; /* key replacement */ |
a607227a |
173 | if (HUF_WOULD_CREATE_KEY(action)) { |
174 | SV* trigger = HUF_get_trigger(keysv, ob_id); |
175 | HUF_mark_field(trigger, field); |
176 | } |
1e73acc8 |
177 | } |
178 | return 0; |
179 | } |
180 | |
181 | /* see if something is a field hash */ |
182 | int HUF_get_status(HV* hash) { |
183 | int ans = 0; |
184 | if (hash && (SvTYPE(hash) == SVt_PVHV)) { |
1e73acc8 |
185 | MAGIC* mg; |
186 | struct ufuncs* uf; |
187 | ans = (mg = mg_find((SV*)hash, PERL_MAGIC_uvar)) && |
188 | (uf = (struct ufuncs *)mg->mg_ptr) && |
189 | (uf->uf_val == &HUF_watch_key) && |
190 | (uf->uf_set == NULL); |
191 | } |
192 | return ans; |
193 | } |
194 | |
195 | /* Thread support. These routines are called by CLONE (and nothing else) */ |
196 | |
197 | /* Fix entries for one object in all field hashes */ |
198 | void HUF_fix_trigger(SV* trigger, SV* new_id) { |
199 | AV* cont = HUF_get_trigger_content(trigger); |
200 | HV* field_tab = (HV*) *av_fetch(cont, 1, 0); |
201 | HV* new_tab = newHV(); |
202 | HE* ent; |
203 | SV* old_id = *av_fetch(cont, 0, 0); |
204 | hv_iterinit(field_tab); |
205 | while (ent = hv_iternext(field_tab)) { |
206 | SV* field_ref = HeVAL(ent); |
207 | SV* field_id = HUF_field_id(field_ref); |
208 | HV* field = (HV*)SvRV(field_ref); |
209 | SV* val; |
210 | /* recreate field tab entry */ |
211 | hv_store_ent(new_tab, field_id, SvREFCNT_inc(field_ref), 0); |
212 | /* recreate field entry, if any */ |
213 | if (val = hv_delete_ent(field, old_id, 0, 0)) |
214 | hv_store_ent(field, new_id, SvREFCNT_inc(val), 0); |
215 | } |
216 | /* update the trigger */ |
217 | av_store(cont, 0, SvREFCNT_inc(new_id)); |
218 | av_store(cont, 1, (SV*)new_tab); |
219 | } |
220 | |
221 | /* Go over object registry and fix all objects. Also fix the object |
222 | * registry. |
223 | */ |
224 | void HUF_fix_objects() { |
225 | dMY_CXT; |
226 | I32 i, len; |
227 | HE* ent; |
228 | AV* oblist = (AV*)sv_2mortal((SV*)newAV()); |
229 | hv_iterinit(MY_CXT.ob_reg); |
230 | while(ent = hv_iternext(MY_CXT.ob_reg)) |
231 | av_push(oblist, SvREFCNT_inc(hv_iterkeysv(ent))); |
232 | len = av_len(oblist); |
233 | for (i = 0; i <= len; ++i) { |
234 | SV* old_id = *av_fetch(oblist, i, 0); |
235 | SV* trigger = hv_delete_ent(MY_CXT.ob_reg, old_id, 0, 0); |
236 | SV* new_id = HUF_obj_id(trigger); |
237 | HUF_fix_trigger(trigger, new_id); |
238 | hv_store_ent(MY_CXT.ob_reg, new_id, SvREFCNT_inc(trigger), 0); |
239 | } |
240 | } |
241 | |
242 | /* test support (not needed for functionality) */ |
243 | |
244 | static SV* counter; |
1748121c |
245 | I32 HUF_inc_var(pTHX_ IV index, SV* which) { |
1e73acc8 |
246 | sv_setiv(counter, 1 + SvIV(counter)); |
247 | return 0; |
248 | } |
249 | |
250 | MODULE = Hash::Util::FieldHash PACKAGE = Hash::Util::FieldHash |
251 | |
252 | BOOT: |
253 | { |
254 | HUF_global(HUF_INIT); /* create variables */ |
255 | } |
256 | |
257 | int |
258 | _fieldhash(SV* href, int mode) |
259 | PROTOTYPE: $$ |
260 | CODE: |
261 | HV* field; |
262 | RETVAL = 0; |
263 | if (mode && |
264 | href && SvROK(href) && |
265 | (field = (HV*)SvRV(href)) && |
266 | SvTYPE(field) == SVt_PVHV |
267 | ) { |
268 | HUF_add_uvar_magic( |
269 | SvRV(href), |
270 | &HUF_watch_key, |
271 | NULL, |
272 | 0, |
273 | NULL |
274 | ); |
275 | RETVAL = HUF_get_status(field); |
276 | } |
277 | OUTPUT: |
278 | RETVAL |
279 | |
280 | void |
281 | CLONE(char* class) |
282 | CODE: |
283 | if (0 == strcmp(class, "Hash::Util::FieldHash")) { |
284 | HUF_global(HUF_CLONE); |
285 | HUF_fix_objects(); |
286 | } |
287 | |
288 | SV* |
289 | _get_obj_id(SV* obj) |
290 | CODE: |
291 | RETVAL = NULL; |
292 | if (SvROK(obj)) |
293 | RETVAL = HUF_obj_id(obj); |
294 | OUTPUT: |
295 | RETVAL |
296 | |
c418a2d4 |
297 | void |
1e73acc8 |
298 | _active_fields(SV* obj) |
299 | PPCODE: |
300 | if (SvROK(obj)) { |
301 | SV* ob_id = HUF_obj_id(obj); |
302 | SV* trigger = HUF_ask_trigger(ob_id); |
303 | if (trigger) { |
304 | AV* cont = HUF_get_trigger_content(trigger); |
305 | HV* field_tab = (HV*) *av_fetch(cont, 1, 0); |
306 | HE* ent; |
307 | hv_iterinit(field_tab); |
308 | while (ent = hv_iternext(field_tab)) { |
309 | HV* field = (HV*)SvRV(HeVAL(ent)); |
310 | if (hv_exists_ent(field, ob_id, 0)) |
311 | XPUSHs(sv_2mortal(newRV_inc((SV*)field))); |
312 | } |
313 | } |
314 | } |
315 | |
316 | void |
317 | _test_uvar_get(SV* svref, SV* countref) |
318 | CODE: |
319 | if (SvROK(svref) && SvROK(countref)) { |
320 | counter = SvRV(countref); |
321 | sv_setiv(counter, 0); |
322 | HUF_add_uvar_magic( |
323 | SvRV(svref), |
324 | &HUF_inc_var, |
325 | NULL, |
326 | 0, |
327 | SvRV(countref) |
328 | ); |
329 | } |
330 | |
331 | void |
332 | _test_uvar_set(SV* svref, SV* countref) |
333 | CODE: |
334 | if (SvROK(svref) && SvROK(countref)) { |
335 | counter = SvRV(countref); |
336 | sv_setiv(counter, 0); |
337 | counter = SvRV(countref); |
338 | HUF_add_uvar_magic( |
339 | SvRV(svref), |
340 | NULL, |
341 | &HUF_inc_var, |
342 | 0, |
343 | SvRV(countref) |
344 | ); |
345 | } |
346 | |
347 | void |
348 | _test_uvar_same(SV* svref, SV* countref) |
349 | CODE: |
350 | if (SvROK(svref) && SvROK(countref)) { |
351 | counter = SvRV(countref); |
352 | sv_setiv(counter, 0); |
353 | HUF_add_uvar_magic( |
354 | SvRV(svref), |
355 | &HUF_inc_var, |
356 | &HUF_inc_var, |
357 | 0, |
358 | NULL |
359 | ); |
360 | } |
361 | |