line |
stmt |
bran |
cond |
sub |
time |
code |
1
|
|
|
|
|
|
#define PERL_NO_GET_CONTEXT |
2
|
|
|
|
|
|
|
3
|
|
|
|
|
|
#include "EXTERN.h" |
4
|
|
|
|
|
|
#include "perl.h" |
5
|
|
|
|
|
|
#include "XSUB.h" |
6
|
|
|
|
|
|
|
7
|
|
|
|
|
|
/* support for Hash::Util::FieldHash, prefix HUF_ */ |
8
|
|
|
|
|
|
|
9
|
|
|
|
|
|
/* A Perl sub that returns a hashref to the object registry */ |
10
|
|
|
|
|
|
#define HUF_OB_REG "Hash::Util::FieldHash::_ob_reg" |
11
|
|
|
|
|
|
/* Identifier for PERL_MAGIC_ext magic */ |
12
|
|
|
|
|
|
#define HUF_IDCACHE 0x4944 |
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
|
|
|
|
|
|
/* Inquire the object registry (a lexical hash) from perl */ |
22
|
|
|
|
|
|
HV * |
23
|
14
|
|
|
|
|
HUF_get_ob_reg(pTHX) { |
24
|
14
|
|
|
|
|
dSP; |
25
|
|
|
|
|
|
HV* ob_reg = NULL; |
26
|
|
|
|
|
|
I32 items; |
27
|
14
|
|
|
|
|
ENTER; |
28
|
14
|
|
|
|
|
SAVETMPS; |
29
|
|
|
|
|
|
|
30
|
14
|
|
|
|
|
PUSHMARK(SP); |
31
|
14
|
|
|
|
|
items = call_pv(HUF_OB_REG, G_SCALAR|G_NOARGS); |
32
|
14
|
|
|
|
|
SPAGAIN; |
33
|
|
|
|
|
|
|
34
|
14
|
|
|
|
|
if (items == 1 && TOPs && SvROK(TOPs) && SvTYPE(SvRV(TOPs)) == SVt_PVHV) |
35
|
14
|
|
|
|
|
ob_reg = (HV*)SvRV(POPs); |
36
|
14
|
|
|
|
|
PUTBACK; |
37
|
14
|
|
|
|
|
FREETMPS; |
38
|
14
|
|
|
|
|
LEAVE; |
39
|
|
|
|
|
|
|
40
|
14
|
|
|
|
|
if (!ob_reg) |
41
|
0
|
|
|
|
|
Perl_die(aTHX_ "Can't get object registry hash"); |
42
|
14
|
|
|
|
|
return ob_reg; |
43
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
45
|
|
|
|
|
|
/* Deal with global context */ |
46
|
|
|
|
|
|
#define HUF_INIT 1 |
47
|
|
|
|
|
|
#define HUF_CLONE 0 |
48
|
|
|
|
|
|
#define HUF_RESET -1 |
49
|
|
|
|
|
|
|
50
|
|
|
|
|
|
void |
51
|
14
|
|
|
|
|
HUF_global(pTHX_ I32 how) { |
52
|
14
|
|
|
|
|
if (how == HUF_INIT) { |
53
|
|
|
|
|
|
MY_CXT_INIT; |
54
|
14
|
|
|
|
|
MY_CXT.ob_reg = HUF_get_ob_reg(aTHX); |
55
|
0
|
|
|
|
|
} else if (how == HUF_CLONE) { |
56
|
|
|
|
|
|
MY_CXT_CLONE; |
57
|
0
|
|
|
|
|
MY_CXT.ob_reg = HUF_get_ob_reg(aTHX); |
58
|
0
|
|
|
|
|
} else if (how == HUF_RESET) { |
59
|
|
|
|
|
|
dMY_CXT; |
60
|
0
|
|
|
|
|
MY_CXT.ob_reg = HUF_get_ob_reg(aTHX); |
61
|
|
|
|
|
|
} |
62
|
14
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
64
|
|
|
|
|
|
/* Object id */ |
65
|
|
|
|
|
|
|
66
|
|
|
|
|
|
/* definition of id transformation */ |
67
|
|
|
|
|
|
#define HUF_OBJ_ID(x) newSVuv(PTR2UV(x)) |
68
|
|
|
|
|
|
|
69
|
|
|
|
|
|
SV * |
70
|
40134
|
|
|
|
|
HUF_obj_id(pTHX_ SV *obj) { |
71
|
40134
|
|
|
|
|
SV *item = SvRV(obj); |
72
|
|
|
|
|
|
MAGIC *mg; |
73
|
|
|
|
|
|
SV *id; |
74
|
|
|
|
|
|
|
75
|
|
|
|
|
|
/* Get cached object ID, if it exists */ |
76
|
40134
|
|
|
|
|
if (SvTYPE(item) >= SVt_PVMG) { |
77
|
40150
|
|
|
|
|
for ( mg = SvMAGIC(item); mg; mg = mg->mg_moremagic ) { |
78
|
68
|
|
|
|
|
if ((mg->mg_type == PERL_MAGIC_ext) && |
79
|
|
|
|
|
|
(mg->mg_private == HUF_IDCACHE) |
80
|
|
|
|
|
|
) { |
81
|
40
|
|
|
|
|
return mg->mg_obj; |
82
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
86
|
|
|
|
|
|
/* Create an object ID, cache it */ |
87
|
40094
|
|
|
|
|
id = HUF_OBJ_ID(item); |
88
|
40094
|
|
|
|
|
mg = sv_magicext(item, id, PERL_MAGIC_ext, NULL, NULL, 0); |
89
|
40094
|
|
|
|
|
mg->mg_private = HUF_IDCACHE; |
90
|
|
|
|
|
|
SvREFCNT_dec(id); /* refcnt++ in sv_magicext() */ |
91
|
|
|
|
|
|
|
92
|
|
|
|
|
|
/* Return the object ID */ |
93
|
|
|
|
|
|
return id; |
94
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
96
|
|
|
|
|
|
/* set up uvar magic for any sv */ |
97
|
|
|
|
|
|
void |
98
|
40172
|
|
|
|
|
HUF_add_uvar_magic( |
99
|
|
|
|
|
|
pTHX_ |
100
|
|
|
|
|
|
SV* sv, /* the sv to enchant, visible to get/set */ |
101
|
|
|
|
|
|
I32(* val)(pTHX_ IV, SV*), /* "get" function */ |
102
|
|
|
|
|
|
I32(* set)(pTHX_ IV, SV*), /* "set" function */ |
103
|
|
|
|
|
|
I32 index, /* get/set will see this */ |
104
|
|
|
|
|
|
SV* thing /* any associated info */ |
105
|
|
|
|
|
|
) { |
106
|
|
|
|
|
|
struct ufuncs uf; |
107
|
40172
|
|
|
|
|
uf.uf_val = val; |
108
|
40172
|
|
|
|
|
uf.uf_set = set; |
109
|
40172
|
|
|
|
|
uf.uf_index = index; |
110
|
40172
|
|
|
|
|
sv_magic(sv, thing, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf)); |
111
|
40172
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
113
|
|
|
|
|
|
/* Fetch the data container of a trigger */ |
114
|
|
|
|
|
|
AV * |
115
|
80222
|
|
|
|
|
HUF_get_trigger_content(pTHX_ SV *trigger) { |
116
|
|
|
|
|
|
MAGIC* mg; |
117
|
80222
|
|
|
|
|
if (trigger && (mg = mg_find(trigger, PERL_MAGIC_uvar))) |
118
|
80222
|
|
|
|
|
return (AV*)mg->mg_obj; |
119
|
|
|
|
|
|
return NULL; |
120
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
122
|
|
|
|
|
|
/* Delete an object from all field hashes it may occur in. Also delete |
123
|
|
|
|
|
|
* the object's entry from the object registry. This function goes in |
124
|
|
|
|
|
|
* the uf_set field of the uvar magic of a trigger. |
125
|
|
|
|
|
|
*/ |
126
|
40090
|
|
|
|
|
I32 HUF_destroy_obj(pTHX_ IV index, SV *trigger) { |
127
|
|
|
|
|
|
PERL_UNUSED_ARG(index); |
128
|
|
|
|
|
|
/* Do nothing if the weakref wasn't undef'd. Also don't bother |
129
|
|
|
|
|
|
* during global destruction. (MY_CXT.ob_reg is sometimes funny there) */ |
130
|
40090
|
|
|
|
|
if (!SvROK(trigger) && (!PL_in_clean_all)) { |
131
|
|
|
|
|
|
dMY_CXT; |
132
|
40090
|
|
|
|
|
AV* cont = HUF_get_trigger_content(aTHX_ trigger); |
133
|
40090
|
|
|
|
|
SV* ob_id = *av_fetch(cont, 0, 0); |
134
|
40090
|
|
|
|
|
HV* field_tab = (HV*) *av_fetch(cont, 1, 0); |
135
|
|
|
|
|
|
HE* ent; |
136
|
40090
|
|
|
|
|
hv_iterinit(field_tab); |
137
|
120298
|
|
|
|
|
while ((ent = hv_iternext(field_tab))) { |
138
|
40118
|
|
|
|
|
SV* field_ref = HeVAL(ent); |
139
|
40118
|
|
|
|
|
SV* field = SvRV(field_ref); |
140
|
40118
|
|
|
|
|
(void) hv_delete_ent((HV*)field, ob_id, 0, 0); |
141
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
/* make it safe in case we must run in global clenaup, after all */ |
143
|
40090
|
|
|
|
|
if (PL_in_clean_all) |
144
|
0
|
|
|
|
|
HUF_global(aTHX_ HUF_RESET); /* shoudn't be needed */ |
145
|
40090
|
|
|
|
|
(void) hv_delete_ent(MY_CXT.ob_reg, ob_id, 0, 0); |
146
|
|
|
|
|
|
} |
147
|
40090
|
|
|
|
|
return 0; |
148
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
150
|
|
|
|
|
|
/* Create a trigger for an object. The trigger is a magical SV |
151
|
|
|
|
|
|
* that holds a weak ref to the object. The magic fires when the object |
152
|
|
|
|
|
|
* expires and takes care of garbage collection in registred hashes. |
153
|
|
|
|
|
|
* For that purpose, the magic structure holds the original id of |
154
|
|
|
|
|
|
* the object, and a list (a hash, really) of hashes from which the |
155
|
|
|
|
|
|
* object may * have to be deleted. The trigger is stored in the |
156
|
|
|
|
|
|
* object registry and is also deleted when the object expires. |
157
|
|
|
|
|
|
*/ |
158
|
|
|
|
|
|
SV * |
159
|
40092
|
|
|
|
|
HUF_new_trigger(pTHX_ SV *obj, SV *ob_id) { |
160
|
|
|
|
|
|
dMY_CXT; |
161
|
40092
|
|
|
|
|
SV* trigger = sv_rvweaken(newRV_inc(SvRV(obj))); |
162
|
40092
|
|
|
|
|
AV* cont = newAV(); |
163
|
40092
|
|
|
|
|
sv_2mortal((SV*)cont); |
164
|
40092
|
|
|
|
|
av_store(cont, 0, SvREFCNT_inc(ob_id)); |
165
|
40092
|
|
|
|
|
av_store(cont, 1, (SV*)newHV()); |
166
|
40092
|
|
|
|
|
HUF_add_uvar_magic(aTHX_ trigger, NULL, &HUF_destroy_obj, 0, (SV*)cont); |
167
|
40092
|
|
|
|
|
(void) hv_store_ent(MY_CXT.ob_reg, ob_id, trigger, 0); |
168
|
40092
|
|
|
|
|
return trigger; |
169
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
171
|
|
|
|
|
|
/* retrieve a trigger for obj if one exists, return NULL otherwise */ |
172
|
|
|
|
|
|
SV * |
173
|
40230
|
|
|
|
|
HUF_ask_trigger(pTHX_ SV *ob_id) { |
174
|
|
|
|
|
|
dMY_CXT; |
175
|
|
|
|
|
|
HE* ent; |
176
|
40230
|
|
|
|
|
if ((ent = hv_fetch_ent(MY_CXT.ob_reg, ob_id, 0, 0))) |
177
|
44
|
|
|
|
|
return HeVAL(ent); |
178
|
|
|
|
|
|
return NULL; |
179
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
181
|
|
|
|
|
|
/* get the trigger for an object, creating it if necessary */ |
182
|
|
|
|
|
|
SV * |
183
|
0
|
|
|
|
|
HUF_get_trigger0(pTHX_ SV *obj, SV *ob_id) { |
184
|
|
|
|
|
|
SV* trigger; |
185
|
0
|
|
|
|
|
if (!(trigger = HUF_ask_trigger(aTHX_ ob_id))) |
186
|
0
|
|
|
|
|
trigger = HUF_new_trigger(aTHX_ obj, ob_id); |
187
|
0
|
|
|
|
|
return trigger; |
188
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
190
|
|
|
|
|
|
SV * |
191
|
40110
|
|
|
|
|
HUF_get_trigger(pTHX_ SV *obj, SV *ob_id) { |
192
|
40110
|
|
|
|
|
SV* trigger = HUF_ask_trigger(aTHX_ ob_id); |
193
|
40110
|
|
|
|
|
if (!trigger) |
194
|
40092
|
|
|
|
|
trigger = HUF_new_trigger(aTHX_ obj, ob_id); |
195
|
40110
|
|
|
|
|
return( trigger); |
196
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
198
|
|
|
|
|
|
/* mark an object (trigger) as having been used with a field |
199
|
|
|
|
|
|
(a clenup-liability) |
200
|
|
|
|
|
|
*/ |
201
|
|
|
|
|
|
void |
202
|
40132
|
|
|
|
|
HUF_mark_field(pTHX_ SV *trigger, SV *field) { |
203
|
40132
|
|
|
|
|
AV* cont = HUF_get_trigger_content(aTHX_ trigger); |
204
|
40132
|
|
|
|
|
HV* field_tab = (HV*) *av_fetch(cont, 1, 0); |
205
|
40132
|
|
|
|
|
SV* field_ref = newRV_inc(field); |
206
|
40132
|
|
|
|
|
UV field_addr = PTR2UV(field); |
207
|
40132
|
|
|
|
|
(void) hv_store(field_tab, (char *)&field_addr, sizeof(field_addr), field_ref, 0); |
208
|
40132
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
210
|
|
|
|
|
|
/* Determine, from the value of action, whether this call may create a new |
211
|
|
|
|
|
|
* hash key */ |
212
|
|
|
|
|
|
#define HUF_WOULD_CREATE_KEY(x) ((x) != HV_DELETE && ((x) & (HV_FETCH_ISSTORE | HV_FETCH_LVALUE))) |
213
|
|
|
|
|
|
|
214
|
|
|
|
|
|
/* The key exchange functions. They communicate with S_hv_magic_uvar_xkey |
215
|
|
|
|
|
|
* in hv.c */ |
216
|
80662
|
|
|
|
|
I32 HUF_watch_key_safe(pTHX_ IV action, SV* field) { |
217
|
80662
|
|
|
|
|
MAGIC* mg = mg_find(field, PERL_MAGIC_uvar); |
218
|
|
|
|
|
|
SV* keysv; |
219
|
80662
|
|
|
|
|
if (mg && (keysv = mg->mg_obj)) { |
220
|
80662
|
|
|
|
|
if (SvROK(keysv)) { /* ref key */ |
221
|
40118
|
|
|
|
|
SV* ob_id = HUF_obj_id(aTHX_ keysv); |
222
|
40118
|
|
|
|
|
mg->mg_obj = ob_id; /* key replacement */ |
223
|
40118
|
|
|
|
|
if (HUF_WOULD_CREATE_KEY(action)) { |
224
|
40106
|
|
|
|
|
SV* trigger = HUF_get_trigger(aTHX_ keysv, ob_id); |
225
|
40106
|
|
|
|
|
HUF_mark_field(aTHX_ trigger, field); |
226
|
|
|
|
|
|
} |
227
|
40544
|
|
|
|
|
} else if (HUF_WOULD_CREATE_KEY(action)) { /* string key */ |
228
|
|
|
|
|
|
/* registered as object id? */ |
229
|
|
|
|
|
|
SV* trigger; |
230
|
116
|
|
|
|
|
if (( trigger = HUF_ask_trigger(aTHX_ keysv))) |
231
|
24
|
|
|
|
|
HUF_mark_field(aTHX_ trigger, field); |
232
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
} else { |
234
|
0
|
|
|
|
|
Perl_die(aTHX_ "Rogue call of 'HUF_watch_key_safe'"); |
235
|
|
|
|
|
|
} |
236
|
80662
|
|
|
|
|
return 0; |
237
|
|
|
|
|
|
} |
238
|
|
|
|
|
|
|
239
|
4
|
|
|
|
|
I32 HUF_watch_key_id(pTHX_ IV action, SV* field) { |
240
|
4
|
|
|
|
|
MAGIC* mg = mg_find(field, PERL_MAGIC_uvar); |
241
|
|
|
|
|
|
SV* keysv; |
242
|
|
|
|
|
|
PERL_UNUSED_ARG(action); |
243
|
4
|
|
|
|
|
if (mg && (keysv = mg->mg_obj)) { |
244
|
4
|
|
|
|
|
if (SvROK(keysv)) /* ref key */ |
245
|
4
|
|
|
|
|
mg->mg_obj = HUF_obj_id(aTHX_ keysv); /* key replacement */ |
246
|
|
|
|
|
|
} else { |
247
|
0
|
|
|
|
|
Perl_die(aTHX_ "Rogue call of 'HUF_watch_key_id'"); |
248
|
|
|
|
|
|
} |
249
|
4
|
|
|
|
|
return 0; |
250
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
252
|
72
|
|
|
|
|
int HUF_func_2mode( I32(* val)(pTHX_ IV, SV*)) { |
253
|
|
|
|
|
|
int ans = 0; |
254
|
72
|
|
|
|
|
if (val == &HUF_watch_key_id) |
255
|
|
|
|
|
|
ans = 1; |
256
|
72
|
|
|
|
|
if (val == &HUF_watch_key_safe) |
257
|
|
|
|
|
|
ans = 2; |
258
|
72
|
|
|
|
|
return(ans); |
259
|
|
|
|
|
|
} |
260
|
|
|
|
|
|
|
261
|
72
|
|
|
|
|
I32(* HUF_mode_2func( int mode))(pTHX_ IV, SV*) { |
262
|
|
|
|
|
|
I32(* ans)(pTHX_ IV, SV*) = NULL; |
263
|
72
|
|
|
|
|
switch (mode) { |
264
|
|
|
|
|
|
case 1: |
265
|
|
|
|
|
|
ans = &HUF_watch_key_id; |
266
|
2
|
|
|
|
|
break; |
267
|
|
|
|
|
|
case 2: |
268
|
|
|
|
|
|
ans = &HUF_watch_key_safe; |
269
|
70
|
|
|
|
|
break; |
270
|
|
|
|
|
|
} |
271
|
72
|
|
|
|
|
return(ans); |
272
|
|
|
|
|
|
} |
273
|
|
|
|
|
|
|
274
|
|
|
|
|
|
/* see if something is a field hash */ |
275
|
|
|
|
|
|
int |
276
|
72
|
|
|
|
|
HUF_get_status(pTHX_ HV *hash) { |
277
|
|
|
|
|
|
int ans = 0; |
278
|
72
|
|
|
|
|
if (hash && (SvTYPE(hash) == SVt_PVHV)) { |
279
|
|
|
|
|
|
MAGIC* mg; |
280
|
|
|
|
|
|
struct ufuncs* uf; |
281
|
144
|
|
|
|
|
if ((mg = mg_find((SV*)hash, PERL_MAGIC_uvar)) && |
282
|
144
|
|
|
|
|
(uf = (struct ufuncs *)mg->mg_ptr) && |
283
|
72
|
|
|
|
|
(uf->uf_set == NULL) |
284
|
|
|
|
|
|
) { |
285
|
72
|
|
|
|
|
ans = HUF_func_2mode(uf->uf_val); |
286
|
|
|
|
|
|
} |
287
|
|
|
|
|
|
} |
288
|
72
|
|
|
|
|
return ans; |
289
|
|
|
|
|
|
} |
290
|
|
|
|
|
|
|
291
|
|
|
|
|
|
/* Thread support. These routines are called by CLONE (and nothing else) */ |
292
|
|
|
|
|
|
|
293
|
|
|
|
|
|
/* Fix entries for one object in all field hashes */ |
294
|
|
|
|
|
|
void |
295
|
0
|
|
|
|
|
HUF_fix_trigger(pTHX_ SV *trigger, SV *new_id) { |
296
|
0
|
|
|
|
|
AV* cont = HUF_get_trigger_content(aTHX_ trigger); |
297
|
0
|
|
|
|
|
HV* field_tab = (HV*) *av_fetch(cont, 1, 0); |
298
|
0
|
|
|
|
|
HV* new_tab = newHV(); |
299
|
|
|
|
|
|
HE* ent; |
300
|
0
|
|
|
|
|
SV* old_id = *av_fetch(cont, 0, 0); |
301
|
0
|
|
|
|
|
hv_iterinit(field_tab); |
302
|
0
|
|
|
|
|
while ((ent = hv_iternext(field_tab))) { |
303
|
0
|
|
|
|
|
SV* field_ref = HeVAL(ent); |
304
|
0
|
|
|
|
|
HV* field = (HV*)SvRV(field_ref); |
305
|
0
|
|
|
|
|
UV field_addr = PTR2UV(field); |
306
|
|
|
|
|
|
SV* val; |
307
|
|
|
|
|
|
/* recreate field tab entry */ |
308
|
0
|
|
|
|
|
(void) hv_store(new_tab, (char *)&field_addr, sizeof(field_addr), SvREFCNT_inc(field_ref), 0); |
309
|
|
|
|
|
|
/* recreate field entry, if any */ |
310
|
0
|
|
|
|
|
if ((val = hv_delete_ent(field, old_id, 0, 0))) |
311
|
0
|
|
|
|
|
(void) hv_store_ent(field, new_id, SvREFCNT_inc(val), 0); |
312
|
|
|
|
|
|
} |
313
|
|
|
|
|
|
/* update the trigger */ |
314
|
0
|
|
|
|
|
av_store(cont, 0, SvREFCNT_inc(new_id)); |
315
|
0
|
|
|
|
|
av_store(cont, 1, (SV*)new_tab); |
316
|
0
|
|
|
|
|
} |
317
|
|
|
|
|
|
|
318
|
|
|
|
|
|
/* Go over object registry and fix all objects. Also fix the object |
319
|
|
|
|
|
|
* registry. |
320
|
|
|
|
|
|
*/ |
321
|
|
|
|
|
|
void |
322
|
0
|
|
|
|
|
HUF_fix_objects(pTHX) { |
323
|
|
|
|
|
|
dMY_CXT; |
324
|
|
|
|
|
|
I32 i, len; |
325
|
|
|
|
|
|
HE* ent; |
326
|
0
|
|
|
|
|
AV* oblist = (AV*)sv_2mortal((SV*)newAV()); |
327
|
0
|
|
|
|
|
hv_iterinit(MY_CXT.ob_reg); |
328
|
0
|
|
|
|
|
while((ent = hv_iternext(MY_CXT.ob_reg))) |
329
|
0
|
|
|
|
|
av_push(oblist, SvREFCNT_inc(hv_iterkeysv(ent))); |
330
|
0
|
|
|
|
|
len = av_len(oblist); |
331
|
0
|
|
|
|
|
for (i = 0; i <= len; ++i) { |
332
|
0
|
|
|
|
|
SV* old_id = *av_fetch(oblist, i, 0); |
333
|
0
|
|
|
|
|
SV* trigger = hv_delete_ent(MY_CXT.ob_reg, old_id, 0, 0); |
334
|
0
|
|
|
|
|
SV* obj = SvRV(trigger); |
335
|
|
|
|
|
|
MAGIC *mg; |
336
|
|
|
|
|
|
|
337
|
0
|
|
|
|
|
SV* new_id = HUF_OBJ_ID(obj); |
338
|
|
|
|
|
|
|
339
|
|
|
|
|
|
/* Replace cached object ID with this new one */ |
340
|
0
|
|
|
|
|
for (mg = SvMAGIC(obj); mg; mg = mg->mg_moremagic) { |
341
|
0
|
|
|
|
|
if ((mg->mg_type == PERL_MAGIC_ext) && |
342
|
|
|
|
|
|
(mg->mg_private == HUF_IDCACHE) |
343
|
|
|
|
|
|
) { |
344
|
0
|
|
|
|
|
mg->mg_obj = new_id; |
345
|
|
|
|
|
|
} |
346
|
|
|
|
|
|
} |
347
|
|
|
|
|
|
|
348
|
0
|
|
|
|
|
HUF_fix_trigger(aTHX_ trigger, new_id); |
349
|
0
|
|
|
|
|
(void) hv_store_ent(MY_CXT.ob_reg, new_id, SvREFCNT_inc(trigger), 0); |
350
|
|
|
|
|
|
} |
351
|
0
|
|
|
|
|
} |
352
|
|
|
|
|
|
|
353
|
|
|
|
|
|
/* test support (not needed for functionality) */ |
354
|
|
|
|
|
|
|
355
|
|
|
|
|
|
static SV* counter; |
356
|
28
|
|
|
|
|
I32 HUF_inc_var(pTHX_ IV index, SV* which) { |
357
|
|
|
|
|
|
PERL_UNUSED_ARG(index); |
358
|
|
|
|
|
|
PERL_UNUSED_ARG(which); |
359
|
28
|
|
|
|
|
sv_setiv(counter, 1 + SvIV(counter)); |
360
|
28
|
|
|
|
|
return 0; |
361
|
|
|
|
|
|
} |
362
|
|
|
|
|
|
|
363
|
|
|
|
|
|
MODULE = Hash::Util::FieldHash PACKAGE = Hash::Util::FieldHash |
364
|
|
|
|
|
|
|
365
|
|
|
|
|
|
BOOT: |
366
|
|
|
|
|
|
{ |
367
|
14
|
|
|
|
|
HUF_global(aTHX_ HUF_INIT); /* create variables */ |
368
|
|
|
|
|
|
} |
369
|
|
|
|
|
|
|
370
|
|
|
|
|
|
int |
371
|
|
|
|
|
|
_fieldhash(SV* href, int mode) |
372
|
|
|
|
|
|
PROTOTYPE: $$ |
373
|
|
|
|
|
|
CODE: |
374
|
|
|
|
|
|
HV* field; |
375
|
|
|
|
|
|
RETVAL = 0; |
376
|
122
|
|
|
|
|
if (mode && |
377
|
152
|
|
|
|
|
href && SvROK(href) && |
378
|
152
|
|
|
|
|
(field = (HV*)SvRV(href)) && |
379
|
76
|
|
|
|
|
SvTYPE(field) == SVt_PVHV |
380
|
|
|
|
|
|
) { |
381
|
|
|
|
|
|
|
382
|
72
|
|
|
|
|
HUF_add_uvar_magic( |
383
|
|
|
|
|
|
aTHX_ |
384
|
|
|
|
|
|
SvRV(href), |
385
|
|
|
|
|
|
HUF_mode_2func(mode), |
386
|
|
|
|
|
|
NULL, |
387
|
|
|
|
|
|
0, |
388
|
|
|
|
|
|
NULL |
389
|
|
|
|
|
|
); |
390
|
72
|
|
|
|
|
RETVAL = HUF_get_status(aTHX_ field); |
391
|
|
|
|
|
|
} |
392
|
|
|
|
|
|
OUTPUT: |
393
|
|
|
|
|
|
RETVAL |
394
|
|
|
|
|
|
|
395
|
|
|
|
|
|
void |
396
|
|
|
|
|
|
id(SV* ref) |
397
|
|
|
|
|
|
PROTOTYPE: $ |
398
|
|
|
|
|
|
PPCODE: |
399
|
12
|
|
|
|
|
if (SvROK(ref)) { |
400
|
8
|
|
|
|
|
XPUSHs(HUF_obj_id(aTHX_ ref)); |
401
|
|
|
|
|
|
} else { |
402
|
4
|
|
|
|
|
XPUSHs(ref); |
403
|
|
|
|
|
|
} |
404
|
|
|
|
|
|
|
405
|
|
|
|
|
|
SV* |
406
|
|
|
|
|
|
id_2obj(SV* id) |
407
|
|
|
|
|
|
PROTOTYPE: $ |
408
|
|
|
|
|
|
CODE: |
409
|
4
|
|
|
|
|
SV* obj = HUF_ask_trigger(aTHX_ id); |
410
|
4
|
|
|
|
|
if (obj) { |
411
|
2
|
|
|
|
|
RETVAL = newRV_inc(SvRV(obj)); |
412
|
|
|
|
|
|
} else { |
413
|
|
|
|
|
|
RETVAL = &PL_sv_undef; |
414
|
|
|
|
|
|
} |
415
|
|
|
|
|
|
OUTPUT: |
416
|
|
|
|
|
|
RETVAL |
417
|
|
|
|
|
|
|
418
|
|
|
|
|
|
SV* |
419
|
|
|
|
|
|
register(SV* obj, ...) |
420
|
|
|
|
|
|
PROTOTYPE: $@ |
421
|
|
|
|
|
|
CODE: |
422
|
|
|
|
|
|
SV* trigger; |
423
|
|
|
|
|
|
int i; |
424
|
|
|
|
|
|
RETVAL = NULL; |
425
|
6
|
|
|
|
|
if (!SvROK(obj)) { |
426
|
2
|
|
|
|
|
Perl_die(aTHX_ "Attempt to register a non-ref"); |
427
|
|
|
|
|
|
} else { |
428
|
4
|
|
|
|
|
RETVAL = newRV_inc(SvRV(obj)); |
429
|
|
|
|
|
|
} |
430
|
4
|
|
|
|
|
trigger = HUF_get_trigger(aTHX_ obj, HUF_obj_id(aTHX_ obj)); |
431
|
6
|
|
|
|
|
for (i = 1; i < items; ++ i) { |
432
|
2
|
|
|
|
|
SV* field_ref = POPs; |
433
|
2
|
|
|
|
|
if (SvROK(field_ref) && (SvTYPE(SvRV(field_ref)) == SVt_PVHV)) { |
434
|
2
|
|
|
|
|
HUF_mark_field(aTHX_ trigger, SvRV(field_ref)); |
435
|
|
|
|
|
|
} |
436
|
|
|
|
|
|
} |
437
|
|
|
|
|
|
OUTPUT: |
438
|
|
|
|
|
|
RETVAL |
439
|
|
|
|
|
|
|
440
|
|
|
|
|
|
void |
441
|
|
|
|
|
|
CLONE(char* classname) |
442
|
|
|
|
|
|
CODE: |
443
|
0
|
|
|
|
|
if (0 == strcmp(classname, "Hash::Util::FieldHash")) { |
444
|
0
|
|
|
|
|
HUF_global(aTHX_ HUF_CLONE); |
445
|
0
|
|
|
|
|
HUF_fix_objects(aTHX); |
446
|
|
|
|
|
|
} |
447
|
|
|
|
|
|
|
448
|
|
|
|
|
|
void |
449
|
|
|
|
|
|
_active_fields(SV* obj) |
450
|
|
|
|
|
|
PPCODE: |
451
|
0
|
|
|
|
|
if (SvROK(obj)) { |
452
|
0
|
|
|
|
|
SV* ob_id = HUF_obj_id(aTHX_ obj); |
453
|
0
|
|
|
|
|
SV* trigger = HUF_ask_trigger(aTHX_ ob_id); |
454
|
0
|
|
|
|
|
if (trigger) { |
455
|
0
|
|
|
|
|
AV* cont = HUF_get_trigger_content(aTHX_ trigger); |
456
|
0
|
|
|
|
|
HV* field_tab = (HV*) *av_fetch(cont, 1, 0); |
457
|
|
|
|
|
|
HE* ent; |
458
|
0
|
|
|
|
|
hv_iterinit(field_tab); |
459
|
0
|
|
|
|
|
while ((ent = hv_iternext(field_tab))) { |
460
|
0
|
|
|
|
|
HV* field = (HV*)SvRV(HeVAL(ent)); |
461
|
0
|
|
|
|
|
if (hv_exists_ent(field, ob_id, 0)) |
462
|
0
|
|
|
|
|
XPUSHs(sv_2mortal(newRV_inc((SV*)field))); |
463
|
|
|
|
|
|
} |
464
|
|
|
|
|
|
} |
465
|
|
|
|
|
|
} |
466
|
|
|
|
|
|
|
467
|
|
|
|
|
|
void |
468
|
|
|
|
|
|
_test_uvar_get(SV* svref, SV* countref) |
469
|
|
|
|
|
|
ALIAS: |
470
|
|
|
|
|
|
_test_uvar_get = 1 |
471
|
|
|
|
|
|
_test_uvar_set = 2 |
472
|
|
|
|
|
|
_test_uvar_same = 3 |
473
|
|
|
|
|
|
CODE: |
474
|
8
|
|
|
|
|
if (SvROK(svref) && SvROK(countref)) { |
475
|
8
|
|
|
|
|
counter = SvRV(countref); |
476
|
8
|
|
|
|
|
sv_setiv(counter, 0); |
477
|
24
|
|
|
|
|
HUF_add_uvar_magic( |
478
|
|
|
|
|
|
aTHX_ |
479
|
|
|
|
|
|
SvRV(svref), |
480
|
8
|
|
|
|
|
ix & 1 ? &HUF_inc_var : 0, |
481
|
8
|
|
|
|
|
ix & 2 ? &HUF_inc_var : 0, |
482
|
|
|
|
|
|
0, |
483
|
|
|
|
|
|
SvRV(countref) |
484
|
|
|
|
|
|
); |
485
|
|
|
|
|
|
} |