1 | /* hv.c |
---|
2 | * |
---|
3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
---|
4 | * 2000, 2001, 2002, 2003, by Larry Wall and others |
---|
5 | * |
---|
6 | * You may distribute under the terms of either the GNU General Public |
---|
7 | * License or the Artistic License, as specified in the README file. |
---|
8 | * |
---|
9 | */ |
---|
10 | |
---|
11 | /* |
---|
12 | * "I sit beside the fire and think of all that I have seen." --Bilbo |
---|
13 | */ |
---|
14 | |
---|
15 | /* |
---|
16 | =head1 Hash Manipulation Functions |
---|
17 | */ |
---|
18 | |
---|
19 | #include "EXTERN.h" |
---|
20 | #define PERL_IN_HV_C |
---|
21 | #define PERL_HASH_INTERNAL_ACCESS |
---|
22 | #include "perl.h" |
---|
23 | |
---|
24 | #define HV_MAX_LENGTH_BEFORE_SPLIT 14 |
---|
25 | |
---|
26 | STATIC HE* |
---|
27 | S_new_he(pTHX) |
---|
28 | { |
---|
29 | HE* he; |
---|
30 | LOCK_SV_MUTEX; |
---|
31 | if (!PL_he_root) |
---|
32 | more_he(); |
---|
33 | he = PL_he_root; |
---|
34 | PL_he_root = HeNEXT(he); |
---|
35 | UNLOCK_SV_MUTEX; |
---|
36 | return he; |
---|
37 | } |
---|
38 | |
---|
39 | STATIC void |
---|
40 | S_del_he(pTHX_ HE *p) |
---|
41 | { |
---|
42 | LOCK_SV_MUTEX; |
---|
43 | HeNEXT(p) = (HE*)PL_he_root; |
---|
44 | PL_he_root = p; |
---|
45 | UNLOCK_SV_MUTEX; |
---|
46 | } |
---|
47 | |
---|
48 | STATIC void |
---|
49 | S_more_he(pTHX) |
---|
50 | { |
---|
51 | register HE* he; |
---|
52 | register HE* heend; |
---|
53 | XPV *ptr; |
---|
54 | New(54, ptr, 1008/sizeof(XPV), XPV); |
---|
55 | ptr->xpv_pv = (char*)PL_he_arenaroot; |
---|
56 | PL_he_arenaroot = ptr; |
---|
57 | |
---|
58 | he = (HE*)ptr; |
---|
59 | heend = &he[1008 / sizeof(HE) - 1]; |
---|
60 | PL_he_root = ++he; |
---|
61 | while (he < heend) { |
---|
62 | HeNEXT(he) = (HE*)(he + 1); |
---|
63 | he++; |
---|
64 | } |
---|
65 | HeNEXT(he) = 0; |
---|
66 | } |
---|
67 | |
---|
68 | #ifdef PURIFY |
---|
69 | |
---|
70 | #define new_HE() (HE*)safemalloc(sizeof(HE)) |
---|
71 | #define del_HE(p) safefree((char*)p) |
---|
72 | |
---|
73 | #else |
---|
74 | |
---|
75 | #define new_HE() new_he() |
---|
76 | #define del_HE(p) del_he(p) |
---|
77 | |
---|
78 | #endif |
---|
79 | |
---|
80 | STATIC HEK * |
---|
81 | S_save_hek_flags(pTHX_ const char *str, I32 len, U32 hash, int flags) |
---|
82 | { |
---|
83 | int flags_masked = flags & HVhek_MASK; |
---|
84 | char *k; |
---|
85 | register HEK *hek; |
---|
86 | |
---|
87 | New(54, k, HEK_BASESIZE + len + 2, char); |
---|
88 | hek = (HEK*)k; |
---|
89 | Copy(str, HEK_KEY(hek), len, char); |
---|
90 | HEK_KEY(hek)[len] = 0; |
---|
91 | HEK_LEN(hek) = len; |
---|
92 | HEK_HASH(hek) = hash; |
---|
93 | HEK_FLAGS(hek) = (unsigned char)flags_masked; |
---|
94 | |
---|
95 | if (flags & HVhek_FREEKEY) |
---|
96 | Safefree(str); |
---|
97 | return hek; |
---|
98 | } |
---|
99 | |
---|
100 | /* free the pool of temporary HE/HEK pairs retunrned by hv_fetch_ent |
---|
101 | * for tied hashes */ |
---|
102 | |
---|
103 | void |
---|
104 | Perl_free_tied_hv_pool(pTHX) |
---|
105 | { |
---|
106 | HE *ohe; |
---|
107 | HE *he = PL_hv_fetch_ent_mh; |
---|
108 | while (he) { |
---|
109 | Safefree(HeKEY_hek(he)); |
---|
110 | ohe = he; |
---|
111 | he = HeNEXT(he); |
---|
112 | del_HE(ohe); |
---|
113 | } |
---|
114 | PL_hv_fetch_ent_mh = Nullhe; |
---|
115 | } |
---|
116 | |
---|
117 | #if defined(USE_ITHREADS) |
---|
118 | HE * |
---|
119 | Perl_he_dup(pTHX_ HE *e, bool shared, CLONE_PARAMS* param) |
---|
120 | { |
---|
121 | HE *ret; |
---|
122 | |
---|
123 | if (!e) |
---|
124 | return Nullhe; |
---|
125 | /* look for it in the table first */ |
---|
126 | ret = (HE*)ptr_table_fetch(PL_ptr_table, e); |
---|
127 | if (ret) |
---|
128 | return ret; |
---|
129 | |
---|
130 | /* create anew and remember what it is */ |
---|
131 | ret = new_HE(); |
---|
132 | ptr_table_store(PL_ptr_table, e, ret); |
---|
133 | |
---|
134 | HeNEXT(ret) = he_dup(HeNEXT(e),shared, param); |
---|
135 | if (HeKLEN(e) == HEf_SVKEY) { |
---|
136 | char *k; |
---|
137 | New(54, k, HEK_BASESIZE + sizeof(SV*), char); |
---|
138 | HeKEY_hek(ret) = (HEK*)k; |
---|
139 | HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e), param)); |
---|
140 | } |
---|
141 | else if (shared) |
---|
142 | HeKEY_hek(ret) = share_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e), |
---|
143 | HeKFLAGS(e)); |
---|
144 | else |
---|
145 | HeKEY_hek(ret) = save_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e), |
---|
146 | HeKFLAGS(e)); |
---|
147 | HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e), param)); |
---|
148 | return ret; |
---|
149 | } |
---|
150 | #endif /* USE_ITHREADS */ |
---|
151 | |
---|
152 | static void |
---|
153 | S_hv_notallowed(pTHX_ int flags, const char *key, I32 klen, |
---|
154 | const char *msg) |
---|
155 | { |
---|
156 | SV *sv = sv_newmortal(), *esv = sv_newmortal(); |
---|
157 | if (!(flags & HVhek_FREEKEY)) { |
---|
158 | sv_setpvn(sv, key, klen); |
---|
159 | } |
---|
160 | else { |
---|
161 | /* Need to free saved eventually assign to mortal SV */ |
---|
162 | /* XXX is this line an error ???: SV *sv = sv_newmortal(); */ |
---|
163 | sv_usepvn(sv, (char *) key, klen); |
---|
164 | } |
---|
165 | if (flags & HVhek_UTF8) { |
---|
166 | SvUTF8_on(sv); |
---|
167 | } |
---|
168 | Perl_sv_setpvf(aTHX_ esv, "Attempt to %s a restricted hash", msg); |
---|
169 | Perl_croak(aTHX_ SvPVX(esv), sv); |
---|
170 | } |
---|
171 | |
---|
172 | /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot |
---|
173 | * contains an SV* */ |
---|
174 | |
---|
175 | #define HV_FETCH_ISSTORE 0x01 |
---|
176 | #define HV_FETCH_ISEXISTS 0x02 |
---|
177 | #define HV_FETCH_LVALUE 0x04 |
---|
178 | #define HV_FETCH_JUST_SV 0x08 |
---|
179 | |
---|
180 | /* |
---|
181 | =for apidoc hv_store |
---|
182 | |
---|
183 | Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is |
---|
184 | the length of the key. The C<hash> parameter is the precomputed hash |
---|
185 | value; if it is zero then Perl will compute it. The return value will be |
---|
186 | NULL if the operation failed or if the value did not need to be actually |
---|
187 | stored within the hash (as in the case of tied hashes). Otherwise it can |
---|
188 | be dereferenced to get the original C<SV*>. Note that the caller is |
---|
189 | responsible for suitably incrementing the reference count of C<val> before |
---|
190 | the call, and decrementing it if the function returned NULL. Effectively |
---|
191 | a successful hv_store takes ownership of one reference to C<val>. This is |
---|
192 | usually what you want; a newly created SV has a reference count of one, so |
---|
193 | if all your code does is create SVs then store them in a hash, hv_store |
---|
194 | will own the only reference to the new SV, and your code doesn't need to do |
---|
195 | anything further to tidy up. hv_store is not implemented as a call to |
---|
196 | hv_store_ent, and does not create a temporary SV for the key, so if your |
---|
197 | key data is not already in SV form then use hv_store in preference to |
---|
198 | hv_store_ent. |
---|
199 | |
---|
200 | See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more |
---|
201 | information on how to use this function on tied hashes. |
---|
202 | |
---|
203 | =cut |
---|
204 | */ |
---|
205 | |
---|
206 | SV** |
---|
207 | Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen_i32, SV *val, U32 hash) |
---|
208 | { |
---|
209 | HE *hek; |
---|
210 | STRLEN klen; |
---|
211 | int flags; |
---|
212 | |
---|
213 | if (klen_i32 < 0) { |
---|
214 | klen = -klen_i32; |
---|
215 | flags = HVhek_UTF8; |
---|
216 | } else { |
---|
217 | klen = klen_i32; |
---|
218 | flags = 0; |
---|
219 | } |
---|
220 | hek = hv_fetch_common (hv, NULL, key, klen, flags, |
---|
221 | (HV_FETCH_ISSTORE|HV_FETCH_JUST_SV), val, 0); |
---|
222 | return hek ? &HeVAL(hek) : NULL; |
---|
223 | } |
---|
224 | |
---|
225 | SV** |
---|
226 | Perl_hv_store_flags(pTHX_ HV *hv, const char *key, I32 klen, SV *val, |
---|
227 | register U32 hash, int flags) |
---|
228 | { |
---|
229 | HE *hek = hv_fetch_common (hv, NULL, key, klen, flags, |
---|
230 | (HV_FETCH_ISSTORE|HV_FETCH_JUST_SV), val, hash); |
---|
231 | return hek ? &HeVAL(hek) : NULL; |
---|
232 | } |
---|
233 | |
---|
234 | /* |
---|
235 | =for apidoc hv_store_ent |
---|
236 | |
---|
237 | Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash> |
---|
238 | parameter is the precomputed hash value; if it is zero then Perl will |
---|
239 | compute it. The return value is the new hash entry so created. It will be |
---|
240 | NULL if the operation failed or if the value did not need to be actually |
---|
241 | stored within the hash (as in the case of tied hashes). Otherwise the |
---|
242 | contents of the return value can be accessed using the C<He?> macros |
---|
243 | described here. Note that the caller is responsible for suitably |
---|
244 | incrementing the reference count of C<val> before the call, and |
---|
245 | decrementing it if the function returned NULL. Effectively a successful |
---|
246 | hv_store_ent takes ownership of one reference to C<val>. This is |
---|
247 | usually what you want; a newly created SV has a reference count of one, so |
---|
248 | if all your code does is create SVs then store them in a hash, hv_store |
---|
249 | will own the only reference to the new SV, and your code doesn't need to do |
---|
250 | anything further to tidy up. Note that hv_store_ent only reads the C<key>; |
---|
251 | unlike C<val> it does not take ownership of it, so maintaining the correct |
---|
252 | reference count on C<key> is entirely the caller's responsibility. hv_store |
---|
253 | is not implemented as a call to hv_store_ent, and does not create a temporary |
---|
254 | SV for the key, so if your key data is not already in SV form then use |
---|
255 | hv_store in preference to hv_store_ent. |
---|
256 | |
---|
257 | See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more |
---|
258 | information on how to use this function on tied hashes. |
---|
259 | |
---|
260 | =cut |
---|
261 | */ |
---|
262 | |
---|
263 | HE * |
---|
264 | Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, U32 hash) |
---|
265 | { |
---|
266 | return hv_fetch_common(hv, keysv, NULL, 0, 0, HV_FETCH_ISSTORE, val, hash); |
---|
267 | } |
---|
268 | |
---|
269 | /* |
---|
270 | =for apidoc hv_exists |
---|
271 | |
---|
272 | Returns a boolean indicating whether the specified hash key exists. The |
---|
273 | C<klen> is the length of the key. |
---|
274 | |
---|
275 | =cut |
---|
276 | */ |
---|
277 | |
---|
278 | bool |
---|
279 | Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen_i32) |
---|
280 | { |
---|
281 | STRLEN klen; |
---|
282 | int flags; |
---|
283 | |
---|
284 | if (klen_i32 < 0) { |
---|
285 | klen = -klen_i32; |
---|
286 | flags = HVhek_UTF8; |
---|
287 | } else { |
---|
288 | klen = klen_i32; |
---|
289 | flags = 0; |
---|
290 | } |
---|
291 | return hv_fetch_common(hv, NULL, key, klen, flags, HV_FETCH_ISEXISTS, 0, 0) |
---|
292 | ? TRUE : FALSE; |
---|
293 | } |
---|
294 | |
---|
295 | /* |
---|
296 | =for apidoc hv_fetch |
---|
297 | |
---|
298 | Returns the SV which corresponds to the specified key in the hash. The |
---|
299 | C<klen> is the length of the key. If C<lval> is set then the fetch will be |
---|
300 | part of a store. Check that the return value is non-null before |
---|
301 | dereferencing it to an C<SV*>. |
---|
302 | |
---|
303 | See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more |
---|
304 | information on how to use this function on tied hashes. |
---|
305 | |
---|
306 | =cut |
---|
307 | */ |
---|
308 | |
---|
309 | SV** |
---|
310 | Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 lval) |
---|
311 | { |
---|
312 | HE *hek; |
---|
313 | STRLEN klen; |
---|
314 | int flags; |
---|
315 | |
---|
316 | if (klen_i32 < 0) { |
---|
317 | klen = -klen_i32; |
---|
318 | flags = HVhek_UTF8; |
---|
319 | } else { |
---|
320 | klen = klen_i32; |
---|
321 | flags = 0; |
---|
322 | } |
---|
323 | hek = hv_fetch_common (hv, NULL, key, klen, flags, |
---|
324 | HV_FETCH_JUST_SV | (lval ? HV_FETCH_LVALUE : 0), |
---|
325 | Nullsv, 0); |
---|
326 | return hek ? &HeVAL(hek) : NULL; |
---|
327 | } |
---|
328 | |
---|
329 | /* |
---|
330 | =for apidoc hv_exists_ent |
---|
331 | |
---|
332 | Returns a boolean indicating whether the specified hash key exists. C<hash> |
---|
333 | can be a valid precomputed hash value, or 0 to ask for it to be |
---|
334 | computed. |
---|
335 | |
---|
336 | =cut |
---|
337 | */ |
---|
338 | |
---|
339 | bool |
---|
340 | Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash) |
---|
341 | { |
---|
342 | return hv_fetch_common(hv, keysv, NULL, 0, 0, HV_FETCH_ISEXISTS, 0, hash) |
---|
343 | ? TRUE : FALSE; |
---|
344 | } |
---|
345 | |
---|
346 | /* returns an HE * structure with the all fields set */ |
---|
347 | /* note that hent_val will be a mortal sv for MAGICAL hashes */ |
---|
348 | /* |
---|
349 | =for apidoc hv_fetch_ent |
---|
350 | |
---|
351 | Returns the hash entry which corresponds to the specified key in the hash. |
---|
352 | C<hash> must be a valid precomputed hash number for the given C<key>, or 0 |
---|
353 | if you want the function to compute it. IF C<lval> is set then the fetch |
---|
354 | will be part of a store. Make sure the return value is non-null before |
---|
355 | accessing it. The return value when C<tb> is a tied hash is a pointer to a |
---|
356 | static location, so be sure to make a copy of the structure if you need to |
---|
357 | store it somewhere. |
---|
358 | |
---|
359 | See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more |
---|
360 | information on how to use this function on tied hashes. |
---|
361 | |
---|
362 | =cut |
---|
363 | */ |
---|
364 | |
---|
365 | HE * |
---|
366 | Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash) |
---|
367 | { |
---|
368 | return hv_fetch_common(hv, keysv, NULL, 0, 0, |
---|
369 | (lval ? HV_FETCH_LVALUE : 0), Nullsv, hash); |
---|
370 | } |
---|
371 | |
---|
372 | HE * |
---|
373 | S_hv_fetch_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen, |
---|
374 | int flags, int action, SV *val, register U32 hash) |
---|
375 | { |
---|
376 | XPVHV* xhv; |
---|
377 | U32 n_links; |
---|
378 | HE *entry; |
---|
379 | HE **oentry; |
---|
380 | SV *sv; |
---|
381 | bool is_utf8; |
---|
382 | int masked_flags; |
---|
383 | |
---|
384 | if (!hv) |
---|
385 | return 0; |
---|
386 | |
---|
387 | if (keysv) { |
---|
388 | if (flags & HVhek_FREEKEY) |
---|
389 | Safefree(key); |
---|
390 | key = SvPV(keysv, klen); |
---|
391 | flags = 0; |
---|
392 | is_utf8 = (SvUTF8(keysv) != 0); |
---|
393 | } else { |
---|
394 | is_utf8 = ((flags & HVhek_UTF8) ? TRUE : FALSE); |
---|
395 | } |
---|
396 | |
---|
397 | xhv = (XPVHV*)SvANY(hv); |
---|
398 | if (SvMAGICAL(hv)) { |
---|
399 | if (SvRMAGICAL(hv) && !(action & (HV_FETCH_ISSTORE|HV_FETCH_ISEXISTS))) |
---|
400 | { |
---|
401 | if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) { |
---|
402 | sv = sv_newmortal(); |
---|
403 | |
---|
404 | /* XXX should be able to skimp on the HE/HEK here when |
---|
405 | HV_FETCH_JUST_SV is true. */ |
---|
406 | |
---|
407 | if (!keysv) { |
---|
408 | keysv = newSVpvn(key, klen); |
---|
409 | if (is_utf8) { |
---|
410 | SvUTF8_on(keysv); |
---|
411 | } |
---|
412 | } else { |
---|
413 | keysv = newSVsv(keysv); |
---|
414 | } |
---|
415 | mg_copy((SV*)hv, sv, (char *)keysv, HEf_SVKEY); |
---|
416 | |
---|
417 | /* grab a fake HE/HEK pair from the pool or make a new one */ |
---|
418 | entry = PL_hv_fetch_ent_mh; |
---|
419 | if (entry) |
---|
420 | PL_hv_fetch_ent_mh = HeNEXT(entry); |
---|
421 | else { |
---|
422 | char *k; |
---|
423 | entry = new_HE(); |
---|
424 | New(54, k, HEK_BASESIZE + sizeof(SV*), char); |
---|
425 | HeKEY_hek(entry) = (HEK*)k; |
---|
426 | } |
---|
427 | HeNEXT(entry) = Nullhe; |
---|
428 | HeSVKEY_set(entry, keysv); |
---|
429 | HeVAL(entry) = sv; |
---|
430 | sv_upgrade(sv, SVt_PVLV); |
---|
431 | LvTYPE(sv) = 'T'; |
---|
432 | /* so we can free entry when freeing sv */ |
---|
433 | LvTARG(sv) = (SV*)entry; |
---|
434 | |
---|
435 | /* XXX remove at some point? */ |
---|
436 | if (flags & HVhek_FREEKEY) |
---|
437 | Safefree(key); |
---|
438 | |
---|
439 | return entry; |
---|
440 | } |
---|
441 | #ifdef ENV_IS_CASELESS |
---|
442 | else if (mg_find((SV*)hv, PERL_MAGIC_env)) { |
---|
443 | U32 i; |
---|
444 | for (i = 0; i < klen; ++i) |
---|
445 | if (isLOWER(key[i])) { |
---|
446 | /* Would be nice if we had a routine to do the |
---|
447 | copy and upercase in a single pass through. */ |
---|
448 | char *nkey = strupr(savepvn(key,klen)); |
---|
449 | /* Note that this fetch is for nkey (the uppercased |
---|
450 | key) whereas the store is for key (the original) */ |
---|
451 | entry = hv_fetch_common(hv, Nullsv, nkey, klen, |
---|
452 | HVhek_FREEKEY, /* free nkey */ |
---|
453 | 0 /* non-LVAL fetch */, |
---|
454 | Nullsv /* no value */, |
---|
455 | 0 /* compute hash */); |
---|
456 | if (!entry && (action & HV_FETCH_LVALUE)) { |
---|
457 | /* This call will free key if necessary. |
---|
458 | Do it this way to encourage compiler to tail |
---|
459 | call optimise. */ |
---|
460 | entry = hv_fetch_common(hv, keysv, key, klen, |
---|
461 | flags, HV_FETCH_ISSTORE, |
---|
462 | NEWSV(61,0), hash); |
---|
463 | } else { |
---|
464 | if (flags & HVhek_FREEKEY) |
---|
465 | Safefree(key); |
---|
466 | } |
---|
467 | return entry; |
---|
468 | } |
---|
469 | } |
---|
470 | #endif |
---|
471 | } /* ISFETCH */ |
---|
472 | else if (SvRMAGICAL(hv) && (action & HV_FETCH_ISEXISTS)) { |
---|
473 | if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) { |
---|
474 | SV* svret; |
---|
475 | /* I don't understand why hv_exists_ent has svret and sv, |
---|
476 | whereas hv_exists only had one. */ |
---|
477 | svret = sv_newmortal(); |
---|
478 | sv = sv_newmortal(); |
---|
479 | |
---|
480 | if (keysv || is_utf8) { |
---|
481 | if (!keysv) { |
---|
482 | keysv = newSVpvn(key, klen); |
---|
483 | SvUTF8_on(keysv); |
---|
484 | } else { |
---|
485 | keysv = newSVsv(keysv); |
---|
486 | } |
---|
487 | mg_copy((SV*)hv, sv, (char *)sv_2mortal(keysv), HEf_SVKEY); |
---|
488 | } else { |
---|
489 | mg_copy((SV*)hv, sv, key, klen); |
---|
490 | } |
---|
491 | if (flags & HVhek_FREEKEY) |
---|
492 | Safefree(key); |
---|
493 | magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem)); |
---|
494 | /* This cast somewhat evil, but I'm merely using NULL/ |
---|
495 | not NULL to return the boolean exists. |
---|
496 | And I know hv is not NULL. */ |
---|
497 | return SvTRUE(svret) ? (HE *)hv : NULL; |
---|
498 | } |
---|
499 | #ifdef ENV_IS_CASELESS |
---|
500 | else if (mg_find((SV*)hv, PERL_MAGIC_env)) { |
---|
501 | /* XXX This code isn't UTF8 clean. */ |
---|
502 | const char *keysave = key; |
---|
503 | /* Will need to free this, so set FREEKEY flag. */ |
---|
504 | key = savepvn(key,klen); |
---|
505 | key = (const char*)strupr((char*)key); |
---|
506 | is_utf8 = 0; |
---|
507 | hash = 0; |
---|
508 | |
---|
509 | if (flags & HVhek_FREEKEY) { |
---|
510 | Safefree(keysave); |
---|
511 | } |
---|
512 | flags |= HVhek_FREEKEY; |
---|
513 | } |
---|
514 | #endif |
---|
515 | } /* ISEXISTS */ |
---|
516 | else if (action & HV_FETCH_ISSTORE) { |
---|
517 | bool needs_copy; |
---|
518 | bool needs_store; |
---|
519 | hv_magic_check (hv, &needs_copy, &needs_store); |
---|
520 | if (needs_copy) { |
---|
521 | bool save_taint = PL_tainted; |
---|
522 | if (keysv || is_utf8) { |
---|
523 | if (!keysv) { |
---|
524 | keysv = newSVpvn(key, klen); |
---|
525 | SvUTF8_on(keysv); |
---|
526 | } |
---|
527 | if (PL_tainting) |
---|
528 | PL_tainted = SvTAINTED(keysv); |
---|
529 | keysv = sv_2mortal(newSVsv(keysv)); |
---|
530 | mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY); |
---|
531 | } else { |
---|
532 | mg_copy((SV*)hv, val, key, klen); |
---|
533 | } |
---|
534 | |
---|
535 | TAINT_IF(save_taint); |
---|
536 | if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store) { |
---|
537 | if (flags & HVhek_FREEKEY) |
---|
538 | Safefree(key); |
---|
539 | return Nullhe; |
---|
540 | } |
---|
541 | #ifdef ENV_IS_CASELESS |
---|
542 | else if (mg_find((SV*)hv, PERL_MAGIC_env)) { |
---|
543 | /* XXX This code isn't UTF8 clean. */ |
---|
544 | const char *keysave = key; |
---|
545 | /* Will need to free this, so set FREEKEY flag. */ |
---|
546 | key = savepvn(key,klen); |
---|
547 | key = (const char*)strupr((char*)key); |
---|
548 | is_utf8 = 0; |
---|
549 | hash = 0; |
---|
550 | |
---|
551 | if (flags & HVhek_FREEKEY) { |
---|
552 | Safefree(keysave); |
---|
553 | } |
---|
554 | flags |= HVhek_FREEKEY; |
---|
555 | } |
---|
556 | #endif |
---|
557 | } |
---|
558 | } /* ISSTORE */ |
---|
559 | } /* SvMAGICAL */ |
---|
560 | |
---|
561 | if (!xhv->xhv_array /* !HvARRAY(hv) */) { |
---|
562 | if ((action & (HV_FETCH_LVALUE | HV_FETCH_ISSTORE)) |
---|
563 | #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */ |
---|
564 | || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) |
---|
565 | #endif |
---|
566 | ) |
---|
567 | Newz(503, xhv->xhv_array /* HvARRAY(hv) */, |
---|
568 | PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */), |
---|
569 | char); |
---|
570 | #ifdef DYNAMIC_ENV_FETCH |
---|
571 | else if (action & HV_FETCH_ISEXISTS) { |
---|
572 | /* for an %ENV exists, if we do an insert it's by a recursive |
---|
573 | store call, so avoid creating HvARRAY(hv) right now. */ |
---|
574 | } |
---|
575 | #endif |
---|
576 | else { |
---|
577 | /* XXX remove at some point? */ |
---|
578 | if (flags & HVhek_FREEKEY) |
---|
579 | Safefree(key); |
---|
580 | |
---|
581 | return 0; |
---|
582 | } |
---|
583 | } |
---|
584 | |
---|
585 | if (is_utf8) { |
---|
586 | const char *keysave = key; |
---|
587 | key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8); |
---|
588 | if (is_utf8) |
---|
589 | flags |= HVhek_UTF8; |
---|
590 | else |
---|
591 | flags &= ~HVhek_UTF8; |
---|
592 | if (key != keysave) { |
---|
593 | if (flags & HVhek_FREEKEY) |
---|
594 | Safefree(keysave); |
---|
595 | flags |= HVhek_WASUTF8 | HVhek_FREEKEY; |
---|
596 | } |
---|
597 | } |
---|
598 | |
---|
599 | if (HvREHASH(hv)) { |
---|
600 | PERL_HASH_INTERNAL(hash, key, klen); |
---|
601 | /* We don't have a pointer to the hv, so we have to replicate the |
---|
602 | flag into every HEK, so that hv_iterkeysv can see it. */ |
---|
603 | /* And yes, you do need this even though you are not "storing" because |
---|
604 | you can flip the flags below if doing an lval lookup. (And that |
---|
605 | was put in to give the semantics Andreas was expecting.) */ |
---|
606 | flags |= HVhek_REHASH; |
---|
607 | } else if (!hash) { |
---|
608 | /* Not enough shared hash key scalars around to make this worthwhile |
---|
609 | (about 4% slowdown in perlbench with this in) |
---|
610 | if (keysv && (SvIsCOW_shared_hash(keysv))) { |
---|
611 | hash = SvUVX(keysv); |
---|
612 | } else |
---|
613 | */ |
---|
614 | { |
---|
615 | PERL_HASH(hash, key, klen); |
---|
616 | } |
---|
617 | } |
---|
618 | |
---|
619 | masked_flags = (flags & HVhek_MASK); |
---|
620 | n_links = 0; |
---|
621 | |
---|
622 | #ifdef DYNAMIC_ENV_FETCH |
---|
623 | if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*); |
---|
624 | else |
---|
625 | #endif |
---|
626 | { |
---|
627 | /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */ |
---|
628 | entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max]; |
---|
629 | } |
---|
630 | for (; entry; ++n_links, entry = HeNEXT(entry)) { |
---|
631 | if (HeHASH(entry) != hash) /* strings can't be equal */ |
---|
632 | continue; |
---|
633 | if (HeKLEN(entry) != (I32)klen) |
---|
634 | continue; |
---|
635 | if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */ |
---|
636 | continue; |
---|
637 | if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8) |
---|
638 | continue; |
---|
639 | |
---|
640 | if (action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE)) { |
---|
641 | if (HeKFLAGS(entry) != masked_flags) { |
---|
642 | /* We match if HVhek_UTF8 bit in our flags and hash key's |
---|
643 | match. But if entry was set previously with HVhek_WASUTF8 |
---|
644 | and key now doesn't (or vice versa) then we should change |
---|
645 | the key's flag, as this is assignment. */ |
---|
646 | if (HvSHAREKEYS(hv)) { |
---|
647 | /* Need to swap the key we have for a key with the flags we |
---|
648 | need. As keys are shared we can't just write to the |
---|
649 | flag, so we share the new one, unshare the old one. */ |
---|
650 | HEK *new_hek = share_hek_flags(key, klen, hash, |
---|
651 | masked_flags); |
---|
652 | unshare_hek (HeKEY_hek(entry)); |
---|
653 | HeKEY_hek(entry) = new_hek; |
---|
654 | } |
---|
655 | else |
---|
656 | HeKFLAGS(entry) = masked_flags; |
---|
657 | if (masked_flags & HVhek_ENABLEHVKFLAGS) |
---|
658 | HvHASKFLAGS_on(hv); |
---|
659 | } |
---|
660 | if (HeVAL(entry) == &PL_sv_placeholder) { |
---|
661 | /* yes, can store into placeholder slot */ |
---|
662 | if (action & HV_FETCH_LVALUE) { |
---|
663 | if (SvMAGICAL(hv)) { |
---|
664 | /* This preserves behaviour with the old hv_fetch |
---|
665 | implementation which at this point would bail out |
---|
666 | with a break; (at "if we find a placeholder, we |
---|
667 | pretend we haven't found anything") |
---|
668 | |
---|
669 | That break mean that if a placeholder were found, it |
---|
670 | caused a call into hv_store, which in turn would |
---|
671 | check magic, and if there is no magic end up pretty |
---|
672 | much back at this point (in hv_store's code). */ |
---|
673 | break; |
---|
674 | } |
---|
675 | /* LVAL fetch which actaully needs a store. */ |
---|
676 | val = NEWSV(61,0); |
---|
677 | xhv->xhv_placeholders--; |
---|
678 | } else { |
---|
679 | /* store */ |
---|
680 | if (val != &PL_sv_placeholder) |
---|
681 | xhv->xhv_placeholders--; |
---|
682 | } |
---|
683 | HeVAL(entry) = val; |
---|
684 | } else if (action & HV_FETCH_ISSTORE) { |
---|
685 | SvREFCNT_dec(HeVAL(entry)); |
---|
686 | HeVAL(entry) = val; |
---|
687 | } |
---|
688 | } else if (HeVAL(entry) == &PL_sv_placeholder) { |
---|
689 | /* if we find a placeholder, we pretend we haven't found |
---|
690 | anything */ |
---|
691 | break; |
---|
692 | } |
---|
693 | if (flags & HVhek_FREEKEY) |
---|
694 | Safefree(key); |
---|
695 | return entry; |
---|
696 | } |
---|
697 | #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */ |
---|
698 | if (!(action & HV_FETCH_ISSTORE) |
---|
699 | && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) { |
---|
700 | unsigned long len; |
---|
701 | char *env = PerlEnv_ENVgetenv_len(key,&len); |
---|
702 | if (env) { |
---|
703 | sv = newSVpvn(env,len); |
---|
704 | SvTAINTED_on(sv); |
---|
705 | return hv_fetch_common(hv,keysv,key,klen,flags,HV_FETCH_ISSTORE,sv, |
---|
706 | hash); |
---|
707 | } |
---|
708 | } |
---|
709 | #endif |
---|
710 | |
---|
711 | if (!entry && SvREADONLY(hv) && !(action & HV_FETCH_ISEXISTS)) { |
---|
712 | S_hv_notallowed(aTHX_ flags, key, klen, |
---|
713 | "access disallowed key '%"SVf"' in" |
---|
714 | ); |
---|
715 | } |
---|
716 | if (!(action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE))) { |
---|
717 | /* Not doing some form of store, so return failure. */ |
---|
718 | if (flags & HVhek_FREEKEY) |
---|
719 | Safefree(key); |
---|
720 | return 0; |
---|
721 | } |
---|
722 | if (action & HV_FETCH_LVALUE) { |
---|
723 | val = NEWSV(61,0); |
---|
724 | if (SvMAGICAL(hv)) { |
---|
725 | /* At this point the old hv_fetch code would call to hv_store, |
---|
726 | which in turn might do some tied magic. So we need to make that |
---|
727 | magic check happen. */ |
---|
728 | /* gonna assign to this, so it better be there */ |
---|
729 | return hv_fetch_common(hv, keysv, key, klen, flags, |
---|
730 | HV_FETCH_ISSTORE, val, hash); |
---|
731 | /* XXX Surely that could leak if the fetch-was-store fails? |
---|
732 | Just like the hv_fetch. */ |
---|
733 | } |
---|
734 | } |
---|
735 | |
---|
736 | /* Welcome to hv_store... */ |
---|
737 | |
---|
738 | if (!xhv->xhv_array) { |
---|
739 | /* Not sure if we can get here. I think the only case of oentry being |
---|
740 | NULL is for %ENV with dynamic env fetch. But that should disappear |
---|
741 | with magic in the previous code. */ |
---|
742 | Newz(503, xhv->xhv_array /* HvARRAY(hv) */, |
---|
743 | PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */), |
---|
744 | char); |
---|
745 | } |
---|
746 | |
---|
747 | oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max]; |
---|
748 | |
---|
749 | entry = new_HE(); |
---|
750 | /* share_hek_flags will do the free for us. This might be considered |
---|
751 | bad API design. */ |
---|
752 | if (HvSHAREKEYS(hv)) |
---|
753 | HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags); |
---|
754 | else /* gotta do the real thing */ |
---|
755 | HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags); |
---|
756 | HeVAL(entry) = val; |
---|
757 | HeNEXT(entry) = *oentry; |
---|
758 | *oentry = entry; |
---|
759 | |
---|
760 | if (val == &PL_sv_placeholder) |
---|
761 | xhv->xhv_placeholders++; |
---|
762 | if (masked_flags & HVhek_ENABLEHVKFLAGS) |
---|
763 | HvHASKFLAGS_on(hv); |
---|
764 | |
---|
765 | xhv->xhv_keys++; /* HvKEYS(hv)++ */ |
---|
766 | if (!n_links) { /* initial entry? */ |
---|
767 | xhv->xhv_fill++; /* HvFILL(hv)++ */ |
---|
768 | } else if ((xhv->xhv_keys > (IV)xhv->xhv_max) |
---|
769 | || ((n_links > HV_MAX_LENGTH_BEFORE_SPLIT) && !HvREHASH(hv))) { |
---|
770 | /* Use only the old HvKEYS(hv) > HvMAX(hv) condition to limit bucket |
---|
771 | splits on a rehashed hash, as we're not going to split it again, |
---|
772 | and if someone is lucky (evil) enough to get all the keys in one |
---|
773 | list they could exhaust our memory as we repeatedly double the |
---|
774 | number of buckets on every entry. Linear search feels a less worse |
---|
775 | thing to do. */ |
---|
776 | hsplit(hv); |
---|
777 | } |
---|
778 | |
---|
779 | return entry; |
---|
780 | } |
---|
781 | |
---|
782 | STATIC void |
---|
783 | S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store) |
---|
784 | { |
---|
785 | MAGIC *mg = SvMAGIC(hv); |
---|
786 | *needs_copy = FALSE; |
---|
787 | *needs_store = TRUE; |
---|
788 | while (mg) { |
---|
789 | if (isUPPER(mg->mg_type)) { |
---|
790 | *needs_copy = TRUE; |
---|
791 | switch (mg->mg_type) { |
---|
792 | case PERL_MAGIC_tied: |
---|
793 | case PERL_MAGIC_sig: |
---|
794 | *needs_store = FALSE; |
---|
795 | } |
---|
796 | } |
---|
797 | mg = mg->mg_moremagic; |
---|
798 | } |
---|
799 | } |
---|
800 | |
---|
801 | /* |
---|
802 | =for apidoc hv_scalar |
---|
803 | |
---|
804 | Evaluates the hash in scalar context and returns the result. Handles magic when the hash is tied. |
---|
805 | |
---|
806 | =cut |
---|
807 | */ |
---|
808 | |
---|
809 | SV * |
---|
810 | Perl_hv_scalar(pTHX_ HV *hv) |
---|
811 | { |
---|
812 | MAGIC *mg; |
---|
813 | SV *sv; |
---|
814 | |
---|
815 | if ((SvRMAGICAL(hv) && (mg = mg_find((SV*)hv, PERL_MAGIC_tied)))) { |
---|
816 | sv = magic_scalarpack(hv, mg); |
---|
817 | return sv; |
---|
818 | } |
---|
819 | |
---|
820 | sv = sv_newmortal(); |
---|
821 | if (HvFILL((HV*)hv)) |
---|
822 | Perl_sv_setpvf(aTHX_ sv, "%ld/%ld", |
---|
823 | (long)HvFILL(hv), (long)HvMAX(hv) + 1); |
---|
824 | else |
---|
825 | sv_setiv(sv, 0); |
---|
826 | |
---|
827 | return sv; |
---|
828 | } |
---|
829 | |
---|
830 | /* |
---|
831 | =for apidoc hv_delete |
---|
832 | |
---|
833 | Deletes a key/value pair in the hash. The value SV is removed from the |
---|
834 | hash and returned to the caller. The C<klen> is the length of the key. |
---|
835 | The C<flags> value will normally be zero; if set to G_DISCARD then NULL |
---|
836 | will be returned. |
---|
837 | |
---|
838 | =cut |
---|
839 | */ |
---|
840 | |
---|
841 | SV * |
---|
842 | Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 flags) |
---|
843 | { |
---|
844 | STRLEN klen; |
---|
845 | int k_flags = 0; |
---|
846 | |
---|
847 | if (klen_i32 < 0) { |
---|
848 | klen = -klen_i32; |
---|
849 | k_flags |= HVhek_UTF8; |
---|
850 | } else { |
---|
851 | klen = klen_i32; |
---|
852 | } |
---|
853 | return hv_delete_common(hv, NULL, key, klen, k_flags, flags, 0); |
---|
854 | } |
---|
855 | |
---|
856 | /* |
---|
857 | =for apidoc hv_delete_ent |
---|
858 | |
---|
859 | Deletes a key/value pair in the hash. The value SV is removed from the |
---|
860 | hash and returned to the caller. The C<flags> value will normally be zero; |
---|
861 | if set to G_DISCARD then NULL will be returned. C<hash> can be a valid |
---|
862 | precomputed hash value, or 0 to ask for it to be computed. |
---|
863 | |
---|
864 | =cut |
---|
865 | */ |
---|
866 | |
---|
867 | SV * |
---|
868 | Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash) |
---|
869 | { |
---|
870 | return hv_delete_common(hv, keysv, NULL, 0, 0, flags, hash); |
---|
871 | } |
---|
872 | |
---|
873 | SV * |
---|
874 | S_hv_delete_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen, |
---|
875 | int k_flags, I32 d_flags, U32 hash) |
---|
876 | { |
---|
877 | register XPVHV* xhv; |
---|
878 | register I32 i; |
---|
879 | register HE *entry; |
---|
880 | register HE **oentry; |
---|
881 | SV *sv; |
---|
882 | bool is_utf8; |
---|
883 | int masked_flags; |
---|
884 | |
---|
885 | if (!hv) |
---|
886 | return Nullsv; |
---|
887 | |
---|
888 | if (keysv) { |
---|
889 | if (k_flags & HVhek_FREEKEY) |
---|
890 | Safefree(key); |
---|
891 | key = SvPV(keysv, klen); |
---|
892 | k_flags = 0; |
---|
893 | is_utf8 = (SvUTF8(keysv) != 0); |
---|
894 | } else { |
---|
895 | is_utf8 = ((k_flags & HVhek_UTF8) ? TRUE : FALSE); |
---|
896 | } |
---|
897 | |
---|
898 | if (SvRMAGICAL(hv)) { |
---|
899 | bool needs_copy; |
---|
900 | bool needs_store; |
---|
901 | hv_magic_check (hv, &needs_copy, &needs_store); |
---|
902 | |
---|
903 | if (needs_copy) { |
---|
904 | entry = hv_fetch_common(hv, keysv, key, klen, |
---|
905 | k_flags & ~HVhek_FREEKEY, HV_FETCH_LVALUE, |
---|
906 | Nullsv, hash); |
---|
907 | sv = entry ? HeVAL(entry) : NULL; |
---|
908 | if (sv) { |
---|
909 | if (SvMAGICAL(sv)) { |
---|
910 | mg_clear(sv); |
---|
911 | } |
---|
912 | if (!needs_store) { |
---|
913 | if (mg_find(sv, PERL_MAGIC_tiedelem)) { |
---|
914 | /* No longer an element */ |
---|
915 | sv_unmagic(sv, PERL_MAGIC_tiedelem); |
---|
916 | return sv; |
---|
917 | } |
---|
918 | return Nullsv; /* element cannot be deleted */ |
---|
919 | } |
---|
920 | #ifdef ENV_IS_CASELESS |
---|
921 | else if (mg_find((SV*)hv, PERL_MAGIC_env)) { |
---|
922 | /* XXX This code isn't UTF8 clean. */ |
---|
923 | keysv = sv_2mortal(newSVpvn(key,klen)); |
---|
924 | if (k_flags & HVhek_FREEKEY) { |
---|
925 | Safefree(key); |
---|
926 | } |
---|
927 | key = strupr(SvPVX(keysv)); |
---|
928 | is_utf8 = 0; |
---|
929 | k_flags = 0; |
---|
930 | hash = 0; |
---|
931 | } |
---|
932 | #endif |
---|
933 | } |
---|
934 | } |
---|
935 | } |
---|
936 | xhv = (XPVHV*)SvANY(hv); |
---|
937 | if (!xhv->xhv_array /* !HvARRAY(hv) */) |
---|
938 | return Nullsv; |
---|
939 | |
---|
940 | if (is_utf8) { |
---|
941 | const char *keysave = key; |
---|
942 | key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8); |
---|
943 | |
---|
944 | if (is_utf8) |
---|
945 | k_flags |= HVhek_UTF8; |
---|
946 | else |
---|
947 | k_flags &= ~HVhek_UTF8; |
---|
948 | if (key != keysave) { |
---|
949 | if (k_flags & HVhek_FREEKEY) { |
---|
950 | /* This shouldn't happen if our caller does what we expect, |
---|
951 | but strictly the API allows it. */ |
---|
952 | Safefree(keysave); |
---|
953 | } |
---|
954 | k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY; |
---|
955 | } |
---|
956 | HvHASKFLAGS_on((SV*)hv); |
---|
957 | } |
---|
958 | |
---|
959 | if (HvREHASH(hv)) { |
---|
960 | PERL_HASH_INTERNAL(hash, key, klen); |
---|
961 | } else if (!hash) { |
---|
962 | /* Not enough shared hash key scalars around to make this worthwhile |
---|
963 | (about 4% slowdown in perlbench with this in) |
---|
964 | if (keysv && (SvIsCOW_shared_hash(keysv))) { |
---|
965 | hash = SvUVX(keysv); |
---|
966 | } else |
---|
967 | */ |
---|
968 | { |
---|
969 | PERL_HASH(hash, key, klen); |
---|
970 | } |
---|
971 | } |
---|
972 | |
---|
973 | masked_flags = (k_flags & HVhek_MASK); |
---|
974 | |
---|
975 | /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */ |
---|
976 | oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max]; |
---|
977 | entry = *oentry; |
---|
978 | i = 1; |
---|
979 | for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) { |
---|
980 | if (HeHASH(entry) != hash) /* strings can't be equal */ |
---|
981 | continue; |
---|
982 | if (HeKLEN(entry) != (I32)klen) |
---|
983 | continue; |
---|
984 | if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */ |
---|
985 | continue; |
---|
986 | if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8) |
---|
987 | continue; |
---|
988 | if (k_flags & HVhek_FREEKEY) |
---|
989 | Safefree(key); |
---|
990 | |
---|
991 | /* if placeholder is here, it's already been deleted.... */ |
---|
992 | if (HeVAL(entry) == &PL_sv_placeholder) |
---|
993 | { |
---|
994 | if (SvREADONLY(hv)) |
---|
995 | return Nullsv; /* if still SvREADONLY, leave it deleted. */ |
---|
996 | |
---|
997 | /* okay, really delete the placeholder. */ |
---|
998 | *oentry = HeNEXT(entry); |
---|
999 | if (i && !*oentry) |
---|
1000 | xhv->xhv_fill--; /* HvFILL(hv)-- */ |
---|
1001 | if (entry == xhv->xhv_eiter /* HvEITER(hv) */) |
---|
1002 | HvLAZYDEL_on(hv); |
---|
1003 | else |
---|
1004 | hv_free_ent(hv, entry); |
---|
1005 | xhv->xhv_keys--; /* HvKEYS(hv)-- */ |
---|
1006 | if (xhv->xhv_keys == 0) |
---|
1007 | HvHASKFLAGS_off(hv); |
---|
1008 | xhv->xhv_placeholders--; |
---|
1009 | return Nullsv; |
---|
1010 | } |
---|
1011 | else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) { |
---|
1012 | S_hv_notallowed(aTHX_ k_flags, key, klen, |
---|
1013 | "delete readonly key '%"SVf"' from" |
---|
1014 | ); |
---|
1015 | } |
---|
1016 | |
---|
1017 | if (d_flags & G_DISCARD) |
---|
1018 | sv = Nullsv; |
---|
1019 | else { |
---|
1020 | sv = sv_2mortal(HeVAL(entry)); |
---|
1021 | HeVAL(entry) = &PL_sv_placeholder; |
---|
1022 | } |
---|
1023 | |
---|
1024 | /* |
---|
1025 | * If a restricted hash, rather than really deleting the entry, put |
---|
1026 | * a placeholder there. This marks the key as being "approved", so |
---|
1027 | * we can still access via not-really-existing key without raising |
---|
1028 | * an error. |
---|
1029 | */ |
---|
1030 | if (SvREADONLY(hv)) { |
---|
1031 | HeVAL(entry) = &PL_sv_placeholder; |
---|
1032 | /* We'll be saving this slot, so the number of allocated keys |
---|
1033 | * doesn't go down, but the number placeholders goes up */ |
---|
1034 | xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */ |
---|
1035 | } else { |
---|
1036 | *oentry = HeNEXT(entry); |
---|
1037 | if (i && !*oentry) |
---|
1038 | xhv->xhv_fill--; /* HvFILL(hv)-- */ |
---|
1039 | if (entry == xhv->xhv_eiter /* HvEITER(hv) */) |
---|
1040 | HvLAZYDEL_on(hv); |
---|
1041 | else |
---|
1042 | hv_free_ent(hv, entry); |
---|
1043 | xhv->xhv_keys--; /* HvKEYS(hv)-- */ |
---|
1044 | if (xhv->xhv_keys == 0) |
---|
1045 | HvHASKFLAGS_off(hv); |
---|
1046 | } |
---|
1047 | return sv; |
---|
1048 | } |
---|
1049 | if (SvREADONLY(hv)) { |
---|
1050 | S_hv_notallowed(aTHX_ k_flags, key, klen, |
---|
1051 | "delete disallowed key '%"SVf"' from" |
---|
1052 | ); |
---|
1053 | } |
---|
1054 | |
---|
1055 | if (k_flags & HVhek_FREEKEY) |
---|
1056 | Safefree(key); |
---|
1057 | return Nullsv; |
---|
1058 | } |
---|
1059 | |
---|
1060 | STATIC void |
---|
1061 | S_hsplit(pTHX_ HV *hv) |
---|
1062 | { |
---|
1063 | register XPVHV* xhv = (XPVHV*)SvANY(hv); |
---|
1064 | I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */ |
---|
1065 | register I32 newsize = oldsize * 2; |
---|
1066 | register I32 i; |
---|
1067 | register char *a = xhv->xhv_array; /* HvARRAY(hv) */ |
---|
1068 | register HE **aep; |
---|
1069 | register HE **bep; |
---|
1070 | register HE *entry; |
---|
1071 | register HE **oentry; |
---|
1072 | int longest_chain = 0; |
---|
1073 | int was_shared; |
---|
1074 | |
---|
1075 | PL_nomemok = TRUE; |
---|
1076 | #if defined(STRANGE_MALLOC) || defined(MYMALLOC) |
---|
1077 | Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char); |
---|
1078 | if (!a) { |
---|
1079 | PL_nomemok = FALSE; |
---|
1080 | return; |
---|
1081 | } |
---|
1082 | #else |
---|
1083 | New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char); |
---|
1084 | if (!a) { |
---|
1085 | PL_nomemok = FALSE; |
---|
1086 | return; |
---|
1087 | } |
---|
1088 | Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char); |
---|
1089 | if (oldsize >= 64) { |
---|
1090 | offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */, |
---|
1091 | PERL_HV_ARRAY_ALLOC_BYTES(oldsize)); |
---|
1092 | } |
---|
1093 | else |
---|
1094 | Safefree(xhv->xhv_array /* HvARRAY(hv) */); |
---|
1095 | #endif |
---|
1096 | |
---|
1097 | PL_nomemok = FALSE; |
---|
1098 | Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/ |
---|
1099 | xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */ |
---|
1100 | xhv->xhv_array = a; /* HvARRAY(hv) = a */ |
---|
1101 | aep = (HE**)a; |
---|
1102 | |
---|
1103 | for (i=0; i<oldsize; i++,aep++) { |
---|
1104 | int left_length = 0; |
---|
1105 | int right_length = 0; |
---|
1106 | |
---|
1107 | if (!*aep) /* non-existent */ |
---|
1108 | continue; |
---|
1109 | bep = aep+oldsize; |
---|
1110 | for (oentry = aep, entry = *aep; entry; entry = *oentry) { |
---|
1111 | if ((HeHASH(entry) & newsize) != (U32)i) { |
---|
1112 | *oentry = HeNEXT(entry); |
---|
1113 | HeNEXT(entry) = *bep; |
---|
1114 | if (!*bep) |
---|
1115 | xhv->xhv_fill++; /* HvFILL(hv)++ */ |
---|
1116 | *bep = entry; |
---|
1117 | right_length++; |
---|
1118 | continue; |
---|
1119 | } |
---|
1120 | else { |
---|
1121 | oentry = &HeNEXT(entry); |
---|
1122 | left_length++; |
---|
1123 | } |
---|
1124 | } |
---|
1125 | if (!*aep) /* everything moved */ |
---|
1126 | xhv->xhv_fill--; /* HvFILL(hv)-- */ |
---|
1127 | /* I think we don't actually need to keep track of the longest length, |
---|
1128 | merely flag if anything is too long. But for the moment while |
---|
1129 | developing this code I'll track it. */ |
---|
1130 | if (left_length > longest_chain) |
---|
1131 | longest_chain = left_length; |
---|
1132 | if (right_length > longest_chain) |
---|
1133 | longest_chain = right_length; |
---|
1134 | } |
---|
1135 | |
---|
1136 | |
---|
1137 | /* Pick your policy for "hashing isn't working" here: */ |
---|
1138 | if (longest_chain <= HV_MAX_LENGTH_BEFORE_SPLIT /* split worked? */ |
---|
1139 | || HvREHASH(hv)) { |
---|
1140 | return; |
---|
1141 | } |
---|
1142 | |
---|
1143 | if (hv == PL_strtab) { |
---|
1144 | /* Urg. Someone is doing something nasty to the string table. |
---|
1145 | Can't win. */ |
---|
1146 | return; |
---|
1147 | } |
---|
1148 | |
---|
1149 | /* Awooga. Awooga. Pathological data. */ |
---|
1150 | /*PerlIO_printf(PerlIO_stderr(), "%p %d of %d with %d/%d buckets\n", hv, |
---|
1151 | longest_chain, HvTOTALKEYS(hv), HvFILL(hv), 1+HvMAX(hv));*/ |
---|
1152 | |
---|
1153 | ++newsize; |
---|
1154 | Newz(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char); |
---|
1155 | was_shared = HvSHAREKEYS(hv); |
---|
1156 | |
---|
1157 | xhv->xhv_fill = 0; |
---|
1158 | HvSHAREKEYS_off(hv); |
---|
1159 | HvREHASH_on(hv); |
---|
1160 | |
---|
1161 | aep = (HE **) xhv->xhv_array; |
---|
1162 | |
---|
1163 | for (i=0; i<newsize; i++,aep++) { |
---|
1164 | entry = *aep; |
---|
1165 | while (entry) { |
---|
1166 | /* We're going to trash this HE's next pointer when we chain it |
---|
1167 | into the new hash below, so store where we go next. */ |
---|
1168 | HE *next = HeNEXT(entry); |
---|
1169 | UV hash; |
---|
1170 | |
---|
1171 | /* Rehash it */ |
---|
1172 | PERL_HASH_INTERNAL(hash, HeKEY(entry), HeKLEN(entry)); |
---|
1173 | |
---|
1174 | if (was_shared) { |
---|
1175 | /* Unshare it. */ |
---|
1176 | HEK *new_hek |
---|
1177 | = save_hek_flags(HeKEY(entry), HeKLEN(entry), |
---|
1178 | hash, HeKFLAGS(entry)); |
---|
1179 | unshare_hek (HeKEY_hek(entry)); |
---|
1180 | HeKEY_hek(entry) = new_hek; |
---|
1181 | } else { |
---|
1182 | /* Not shared, so simply write the new hash in. */ |
---|
1183 | HeHASH(entry) = hash; |
---|
1184 | } |
---|
1185 | /*PerlIO_printf(PerlIO_stderr(), "%d ", HeKFLAGS(entry));*/ |
---|
1186 | HEK_REHASH_on(HeKEY_hek(entry)); |
---|
1187 | /*PerlIO_printf(PerlIO_stderr(), "%d\n", HeKFLAGS(entry));*/ |
---|
1188 | |
---|
1189 | /* Copy oentry to the correct new chain. */ |
---|
1190 | bep = ((HE**)a) + (hash & (I32) xhv->xhv_max); |
---|
1191 | if (!*bep) |
---|
1192 | xhv->xhv_fill++; /* HvFILL(hv)++ */ |
---|
1193 | HeNEXT(entry) = *bep; |
---|
1194 | *bep = entry; |
---|
1195 | |
---|
1196 | entry = next; |
---|
1197 | } |
---|
1198 | } |
---|
1199 | Safefree (xhv->xhv_array); |
---|
1200 | xhv->xhv_array = a; /* HvARRAY(hv) = a */ |
---|
1201 | } |
---|
1202 | |
---|
1203 | void |
---|
1204 | Perl_hv_ksplit(pTHX_ HV *hv, IV newmax) |
---|
1205 | { |
---|
1206 | register XPVHV* xhv = (XPVHV*)SvANY(hv); |
---|
1207 | I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */ |
---|
1208 | register I32 newsize; |
---|
1209 | register I32 i; |
---|
1210 | register I32 j; |
---|
1211 | register char *a; |
---|
1212 | register HE **aep; |
---|
1213 | register HE *entry; |
---|
1214 | register HE **oentry; |
---|
1215 | |
---|
1216 | newsize = (I32) newmax; /* possible truncation here */ |
---|
1217 | if (newsize != newmax || newmax <= oldsize) |
---|
1218 | return; |
---|
1219 | while ((newsize & (1 + ~newsize)) != newsize) { |
---|
1220 | newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */ |
---|
1221 | } |
---|
1222 | if (newsize < newmax) |
---|
1223 | newsize *= 2; |
---|
1224 | if (newsize < newmax) |
---|
1225 | return; /* overflow detection */ |
---|
1226 | |
---|
1227 | a = xhv->xhv_array; /* HvARRAY(hv) */ |
---|
1228 | if (a) { |
---|
1229 | PL_nomemok = TRUE; |
---|
1230 | #if defined(STRANGE_MALLOC) || defined(MYMALLOC) |
---|
1231 | Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char); |
---|
1232 | if (!a) { |
---|
1233 | PL_nomemok = FALSE; |
---|
1234 | return; |
---|
1235 | } |
---|
1236 | #else |
---|
1237 | New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char); |
---|
1238 | if (!a) { |
---|
1239 | PL_nomemok = FALSE; |
---|
1240 | return; |
---|
1241 | } |
---|
1242 | Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char); |
---|
1243 | if (oldsize >= 64) { |
---|
1244 | offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */, |
---|
1245 | PERL_HV_ARRAY_ALLOC_BYTES(oldsize)); |
---|
1246 | } |
---|
1247 | else |
---|
1248 | Safefree(xhv->xhv_array /* HvARRAY(hv) */); |
---|
1249 | #endif |
---|
1250 | PL_nomemok = FALSE; |
---|
1251 | Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/ |
---|
1252 | } |
---|
1253 | else { |
---|
1254 | Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char); |
---|
1255 | } |
---|
1256 | xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */ |
---|
1257 | xhv->xhv_array = a; /* HvARRAY(hv) = a */ |
---|
1258 | if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */ |
---|
1259 | return; |
---|
1260 | |
---|
1261 | aep = (HE**)a; |
---|
1262 | for (i=0; i<oldsize; i++,aep++) { |
---|
1263 | if (!*aep) /* non-existent */ |
---|
1264 | continue; |
---|
1265 | for (oentry = aep, entry = *aep; entry; entry = *oentry) { |
---|
1266 | if ((j = (HeHASH(entry) & newsize)) != i) { |
---|
1267 | j -= i; |
---|
1268 | *oentry = HeNEXT(entry); |
---|
1269 | if (!(HeNEXT(entry) = aep[j])) |
---|
1270 | xhv->xhv_fill++; /* HvFILL(hv)++ */ |
---|
1271 | aep[j] = entry; |
---|
1272 | continue; |
---|
1273 | } |
---|
1274 | else |
---|
1275 | oentry = &HeNEXT(entry); |
---|
1276 | } |
---|
1277 | if (!*aep) /* everything moved */ |
---|
1278 | xhv->xhv_fill--; /* HvFILL(hv)-- */ |
---|
1279 | } |
---|
1280 | } |
---|
1281 | |
---|
1282 | /* |
---|
1283 | =for apidoc newHV |
---|
1284 | |
---|
1285 | Creates a new HV. The reference count is set to 1. |
---|
1286 | |
---|
1287 | =cut |
---|
1288 | */ |
---|
1289 | |
---|
1290 | HV * |
---|
1291 | Perl_newHV(pTHX) |
---|
1292 | { |
---|
1293 | register HV *hv; |
---|
1294 | register XPVHV* xhv; |
---|
1295 | |
---|
1296 | hv = (HV*)NEWSV(502,0); |
---|
1297 | sv_upgrade((SV *)hv, SVt_PVHV); |
---|
1298 | xhv = (XPVHV*)SvANY(hv); |
---|
1299 | SvPOK_off(hv); |
---|
1300 | SvNOK_off(hv); |
---|
1301 | #ifndef NODEFAULT_SHAREKEYS |
---|
1302 | HvSHAREKEYS_on(hv); /* key-sharing on by default */ |
---|
1303 | #endif |
---|
1304 | |
---|
1305 | xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */ |
---|
1306 | xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */ |
---|
1307 | xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */ |
---|
1308 | (void)hv_iterinit(hv); /* so each() will start off right */ |
---|
1309 | return hv; |
---|
1310 | } |
---|
1311 | |
---|
1312 | HV * |
---|
1313 | Perl_newHVhv(pTHX_ HV *ohv) |
---|
1314 | { |
---|
1315 | HV *hv = newHV(); |
---|
1316 | STRLEN hv_max, hv_fill; |
---|
1317 | |
---|
1318 | if (!ohv || (hv_fill = HvFILL(ohv)) == 0) |
---|
1319 | return hv; |
---|
1320 | hv_max = HvMAX(ohv); |
---|
1321 | |
---|
1322 | if (!SvMAGICAL((SV *)ohv)) { |
---|
1323 | /* It's an ordinary hash, so copy it fast. AMS 20010804 */ |
---|
1324 | STRLEN i; |
---|
1325 | bool shared = !!HvSHAREKEYS(ohv); |
---|
1326 | HE **ents, **oents = (HE **)HvARRAY(ohv); |
---|
1327 | char *a; |
---|
1328 | New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char); |
---|
1329 | ents = (HE**)a; |
---|
1330 | |
---|
1331 | /* In each bucket... */ |
---|
1332 | for (i = 0; i <= hv_max; i++) { |
---|
1333 | HE *prev = NULL, *ent = NULL, *oent = oents[i]; |
---|
1334 | |
---|
1335 | if (!oent) { |
---|
1336 | ents[i] = NULL; |
---|
1337 | continue; |
---|
1338 | } |
---|
1339 | |
---|
1340 | /* Copy the linked list of entries. */ |
---|
1341 | for (oent = oents[i]; oent; oent = HeNEXT(oent)) { |
---|
1342 | U32 hash = HeHASH(oent); |
---|
1343 | char *key = HeKEY(oent); |
---|
1344 | STRLEN len = HeKLEN(oent); |
---|
1345 | int flags = HeKFLAGS(oent); |
---|
1346 | |
---|
1347 | ent = new_HE(); |
---|
1348 | HeVAL(ent) = newSVsv(HeVAL(oent)); |
---|
1349 | HeKEY_hek(ent) |
---|
1350 | = shared ? share_hek_flags(key, len, hash, flags) |
---|
1351 | : save_hek_flags(key, len, hash, flags); |
---|
1352 | if (prev) |
---|
1353 | HeNEXT(prev) = ent; |
---|
1354 | else |
---|
1355 | ents[i] = ent; |
---|
1356 | prev = ent; |
---|
1357 | HeNEXT(ent) = NULL; |
---|
1358 | } |
---|
1359 | } |
---|
1360 | |
---|
1361 | HvMAX(hv) = hv_max; |
---|
1362 | HvFILL(hv) = hv_fill; |
---|
1363 | HvTOTALKEYS(hv) = HvTOTALKEYS(ohv); |
---|
1364 | HvARRAY(hv) = ents; |
---|
1365 | } |
---|
1366 | else { |
---|
1367 | /* Iterate over ohv, copying keys and values one at a time. */ |
---|
1368 | HE *entry; |
---|
1369 | I32 riter = HvRITER(ohv); |
---|
1370 | HE *eiter = HvEITER(ohv); |
---|
1371 | |
---|
1372 | /* Can we use fewer buckets? (hv_max is always 2^n-1) */ |
---|
1373 | while (hv_max && hv_max + 1 >= hv_fill * 2) |
---|
1374 | hv_max = hv_max / 2; |
---|
1375 | HvMAX(hv) = hv_max; |
---|
1376 | |
---|
1377 | hv_iterinit(ohv); |
---|
1378 | while ((entry = hv_iternext_flags(ohv, 0))) { |
---|
1379 | hv_store_flags(hv, HeKEY(entry), HeKLEN(entry), |
---|
1380 | newSVsv(HeVAL(entry)), HeHASH(entry), |
---|
1381 | HeKFLAGS(entry)); |
---|
1382 | } |
---|
1383 | HvRITER(ohv) = riter; |
---|
1384 | HvEITER(ohv) = eiter; |
---|
1385 | } |
---|
1386 | |
---|
1387 | return hv; |
---|
1388 | } |
---|
1389 | |
---|
1390 | void |
---|
1391 | Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry) |
---|
1392 | { |
---|
1393 | SV *val; |
---|
1394 | |
---|
1395 | if (!entry) |
---|
1396 | return; |
---|
1397 | val = HeVAL(entry); |
---|
1398 | if (val && isGV(val) && GvCVu(val) && HvNAME(hv)) |
---|
1399 | PL_sub_generation++; /* may be deletion of method from stash */ |
---|
1400 | SvREFCNT_dec(val); |
---|
1401 | if (HeKLEN(entry) == HEf_SVKEY) { |
---|
1402 | SvREFCNT_dec(HeKEY_sv(entry)); |
---|
1403 | Safefree(HeKEY_hek(entry)); |
---|
1404 | } |
---|
1405 | else if (HvSHAREKEYS(hv)) |
---|
1406 | unshare_hek(HeKEY_hek(entry)); |
---|
1407 | else |
---|
1408 | Safefree(HeKEY_hek(entry)); |
---|
1409 | del_HE(entry); |
---|
1410 | } |
---|
1411 | |
---|
1412 | void |
---|
1413 | Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry) |
---|
1414 | { |
---|
1415 | if (!entry) |
---|
1416 | return; |
---|
1417 | if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv)) |
---|
1418 | PL_sub_generation++; /* may be deletion of method from stash */ |
---|
1419 | sv_2mortal(HeVAL(entry)); /* free between statements */ |
---|
1420 | if (HeKLEN(entry) == HEf_SVKEY) { |
---|
1421 | sv_2mortal(HeKEY_sv(entry)); |
---|
1422 | Safefree(HeKEY_hek(entry)); |
---|
1423 | } |
---|
1424 | else if (HvSHAREKEYS(hv)) |
---|
1425 | unshare_hek(HeKEY_hek(entry)); |
---|
1426 | else |
---|
1427 | Safefree(HeKEY_hek(entry)); |
---|
1428 | del_HE(entry); |
---|
1429 | } |
---|
1430 | |
---|
1431 | /* |
---|
1432 | =for apidoc hv_clear |
---|
1433 | |
---|
1434 | Clears a hash, making it empty. |
---|
1435 | |
---|
1436 | =cut |
---|
1437 | */ |
---|
1438 | |
---|
1439 | void |
---|
1440 | Perl_hv_clear(pTHX_ HV *hv) |
---|
1441 | { |
---|
1442 | register XPVHV* xhv; |
---|
1443 | if (!hv) |
---|
1444 | return; |
---|
1445 | |
---|
1446 | xhv = (XPVHV*)SvANY(hv); |
---|
1447 | |
---|
1448 | if (SvREADONLY(hv) && xhv->xhv_array != NULL) { |
---|
1449 | /* restricted hash: convert all keys to placeholders */ |
---|
1450 | I32 i; |
---|
1451 | HE* entry; |
---|
1452 | for (i = 0; i <= (I32) xhv->xhv_max; i++) { |
---|
1453 | entry = ((HE**)xhv->xhv_array)[i]; |
---|
1454 | for (; entry; entry = HeNEXT(entry)) { |
---|
1455 | /* not already placeholder */ |
---|
1456 | if (HeVAL(entry) != &PL_sv_placeholder) { |
---|
1457 | if (HeVAL(entry) && SvREADONLY(HeVAL(entry))) { |
---|
1458 | SV* keysv = hv_iterkeysv(entry); |
---|
1459 | Perl_croak(aTHX_ |
---|
1460 | "Attempt to delete readonly key '%"SVf"' from a restricted hash", |
---|
1461 | keysv); |
---|
1462 | } |
---|
1463 | SvREFCNT_dec(HeVAL(entry)); |
---|
1464 | HeVAL(entry) = &PL_sv_placeholder; |
---|
1465 | xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */ |
---|
1466 | } |
---|
1467 | } |
---|
1468 | } |
---|
1469 | goto reset; |
---|
1470 | } |
---|
1471 | |
---|
1472 | hfreeentries(hv); |
---|
1473 | xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */ |
---|
1474 | if (xhv->xhv_array /* HvARRAY(hv) */) |
---|
1475 | (void)memzero(xhv->xhv_array /* HvARRAY(hv) */, |
---|
1476 | (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*)); |
---|
1477 | |
---|
1478 | if (SvRMAGICAL(hv)) |
---|
1479 | mg_clear((SV*)hv); |
---|
1480 | |
---|
1481 | HvHASKFLAGS_off(hv); |
---|
1482 | HvREHASH_off(hv); |
---|
1483 | reset: |
---|
1484 | HvEITER(hv) = NULL; |
---|
1485 | } |
---|
1486 | |
---|
1487 | /* |
---|
1488 | =for apidoc hv_clear_placeholders |
---|
1489 | |
---|
1490 | Clears any placeholders from a hash. If a restricted hash has any of its keys |
---|
1491 | marked as readonly and the key is subsequently deleted, the key is not actually |
---|
1492 | deleted but is marked by assigning it a value of &PL_sv_placeholder. This tags |
---|
1493 | it so it will be ignored by future operations such as iterating over the hash, |
---|
1494 | but will still allow the hash to have a value reaasigned to the key at some |
---|
1495 | future point. This function clears any such placeholder keys from the hash. |
---|
1496 | See Hash::Util::lock_keys() for an example of its use. |
---|
1497 | |
---|
1498 | =cut |
---|
1499 | */ |
---|
1500 | |
---|
1501 | void |
---|
1502 | Perl_hv_clear_placeholders(pTHX_ HV *hv) |
---|
1503 | { |
---|
1504 | I32 items; |
---|
1505 | items = (I32)HvPLACEHOLDERS(hv); |
---|
1506 | if (items) { |
---|
1507 | HE *entry; |
---|
1508 | I32 riter = HvRITER(hv); |
---|
1509 | HE *eiter = HvEITER(hv); |
---|
1510 | hv_iterinit(hv); |
---|
1511 | /* This may look suboptimal with the items *after* the iternext, but |
---|
1512 | it's quite deliberate. We only get here with items==0 if we've |
---|
1513 | just deleted the last placeholder in the hash. If we've just done |
---|
1514 | that then it means that the hash is in lazy delete mode, and the |
---|
1515 | HE is now only referenced in our iterator. If we just quit the loop |
---|
1516 | and discarded our iterator then the HE leaks. So we do the && the |
---|
1517 | other way to ensure iternext is called just one more time, which |
---|
1518 | has the side effect of triggering the lazy delete. */ |
---|
1519 | while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS)) |
---|
1520 | && items) { |
---|
1521 | SV *val = hv_iterval(hv, entry); |
---|
1522 | |
---|
1523 | if (val == &PL_sv_placeholder) { |
---|
1524 | |
---|
1525 | /* It seems that I have to go back in the front of the hash |
---|
1526 | API to delete a hash, even though I have a HE structure |
---|
1527 | pointing to the very entry I want to delete, and could hold |
---|
1528 | onto the previous HE that points to it. And it's easier to |
---|
1529 | go in with SVs as I can then specify the precomputed hash, |
---|
1530 | and don't have fun and games with utf8 keys. */ |
---|
1531 | SV *key = hv_iterkeysv(entry); |
---|
1532 | |
---|
1533 | hv_delete_ent (hv, key, G_DISCARD, HeHASH(entry)); |
---|
1534 | items--; |
---|
1535 | } |
---|
1536 | } |
---|
1537 | HvRITER(hv) = riter; |
---|
1538 | HvEITER(hv) = eiter; |
---|
1539 | } |
---|
1540 | } |
---|
1541 | |
---|
1542 | STATIC void |
---|
1543 | S_hfreeentries(pTHX_ HV *hv) |
---|
1544 | { |
---|
1545 | register HE **array; |
---|
1546 | register HE *entry; |
---|
1547 | register HE *oentry = Null(HE*); |
---|
1548 | I32 riter; |
---|
1549 | I32 max; |
---|
1550 | |
---|
1551 | if (!hv) |
---|
1552 | return; |
---|
1553 | if (!HvARRAY(hv)) |
---|
1554 | return; |
---|
1555 | |
---|
1556 | riter = 0; |
---|
1557 | max = HvMAX(hv); |
---|
1558 | array = HvARRAY(hv); |
---|
1559 | /* make everyone else think the array is empty, so that the destructors |
---|
1560 | * called for freed entries can't recusively mess with us */ |
---|
1561 | HvARRAY(hv) = Null(HE**); |
---|
1562 | HvFILL(hv) = 0; |
---|
1563 | ((XPVHV*) SvANY(hv))->xhv_keys = 0; |
---|
1564 | |
---|
1565 | entry = array[0]; |
---|
1566 | for (;;) { |
---|
1567 | if (entry) { |
---|
1568 | oentry = entry; |
---|
1569 | entry = HeNEXT(entry); |
---|
1570 | hv_free_ent(hv, oentry); |
---|
1571 | } |
---|
1572 | if (!entry) { |
---|
1573 | if (++riter > max) |
---|
1574 | break; |
---|
1575 | entry = array[riter]; |
---|
1576 | } |
---|
1577 | } |
---|
1578 | HvARRAY(hv) = array; |
---|
1579 | (void)hv_iterinit(hv); |
---|
1580 | } |
---|
1581 | |
---|
1582 | /* |
---|
1583 | =for apidoc hv_undef |
---|
1584 | |
---|
1585 | Undefines the hash. |
---|
1586 | |
---|
1587 | =cut |
---|
1588 | */ |
---|
1589 | |
---|
1590 | void |
---|
1591 | Perl_hv_undef(pTHX_ HV *hv) |
---|
1592 | { |
---|
1593 | register XPVHV* xhv; |
---|
1594 | if (!hv) |
---|
1595 | return; |
---|
1596 | xhv = (XPVHV*)SvANY(hv); |
---|
1597 | hfreeentries(hv); |
---|
1598 | Safefree(xhv->xhv_array /* HvARRAY(hv) */); |
---|
1599 | if (HvNAME(hv)) { |
---|
1600 | if(PL_stashcache) |
---|
1601 | hv_delete(PL_stashcache, HvNAME(hv), strlen(HvNAME(hv)), G_DISCARD); |
---|
1602 | Safefree(HvNAME(hv)); |
---|
1603 | HvNAME(hv) = 0; |
---|
1604 | } |
---|
1605 | xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */ |
---|
1606 | xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */ |
---|
1607 | xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */ |
---|
1608 | |
---|
1609 | if (SvRMAGICAL(hv)) |
---|
1610 | mg_clear((SV*)hv); |
---|
1611 | } |
---|
1612 | |
---|
1613 | /* |
---|
1614 | =for apidoc hv_iterinit |
---|
1615 | |
---|
1616 | Prepares a starting point to traverse a hash table. Returns the number of |
---|
1617 | keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is |
---|
1618 | currently only meaningful for hashes without tie magic. |
---|
1619 | |
---|
1620 | NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of |
---|
1621 | hash buckets that happen to be in use. If you still need that esoteric |
---|
1622 | value, you can get it through the macro C<HvFILL(tb)>. |
---|
1623 | |
---|
1624 | |
---|
1625 | =cut |
---|
1626 | */ |
---|
1627 | |
---|
1628 | I32 |
---|
1629 | Perl_hv_iterinit(pTHX_ HV *hv) |
---|
1630 | { |
---|
1631 | register XPVHV* xhv; |
---|
1632 | HE *entry; |
---|
1633 | |
---|
1634 | if (!hv) |
---|
1635 | Perl_croak(aTHX_ "Bad hash"); |
---|
1636 | xhv = (XPVHV*)SvANY(hv); |
---|
1637 | entry = xhv->xhv_eiter; /* HvEITER(hv) */ |
---|
1638 | if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */ |
---|
1639 | HvLAZYDEL_off(hv); |
---|
1640 | hv_free_ent(hv, entry); |
---|
1641 | } |
---|
1642 | xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */ |
---|
1643 | xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */ |
---|
1644 | /* used to be xhv->xhv_fill before 5.004_65 */ |
---|
1645 | return XHvTOTALKEYS(xhv); |
---|
1646 | } |
---|
1647 | /* |
---|
1648 | =for apidoc hv_iternext |
---|
1649 | |
---|
1650 | Returns entries from a hash iterator. See C<hv_iterinit>. |
---|
1651 | |
---|
1652 | You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the |
---|
1653 | iterator currently points to, without losing your place or invalidating your |
---|
1654 | iterator. Note that in this case the current entry is deleted from the hash |
---|
1655 | with your iterator holding the last reference to it. Your iterator is flagged |
---|
1656 | to free the entry on the next call to C<hv_iternext>, so you must not discard |
---|
1657 | your iterator immediately else the entry will leak - call C<hv_iternext> to |
---|
1658 | trigger the resource deallocation. |
---|
1659 | |
---|
1660 | =cut |
---|
1661 | */ |
---|
1662 | |
---|
1663 | HE * |
---|
1664 | Perl_hv_iternext(pTHX_ HV *hv) |
---|
1665 | { |
---|
1666 | return hv_iternext_flags(hv, 0); |
---|
1667 | } |
---|
1668 | |
---|
1669 | /* |
---|
1670 | =for apidoc hv_iternext_flags |
---|
1671 | |
---|
1672 | Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>. |
---|
1673 | The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is |
---|
1674 | set the placeholders keys (for restricted hashes) will be returned in addition |
---|
1675 | to normal keys. By default placeholders are automatically skipped over. |
---|
1676 | Currently a placeholder is implemented with a value that is |
---|
1677 | C<&Perl_sv_placeholder>. Note that the implementation of placeholders and |
---|
1678 | restricted hashes may change, and the implementation currently is |
---|
1679 | insufficiently abstracted for any change to be tidy. |
---|
1680 | |
---|
1681 | =cut |
---|
1682 | */ |
---|
1683 | |
---|
1684 | HE * |
---|
1685 | Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags) |
---|
1686 | { |
---|
1687 | register XPVHV* xhv; |
---|
1688 | register HE *entry; |
---|
1689 | HE *oldentry; |
---|
1690 | MAGIC* mg; |
---|
1691 | |
---|
1692 | if (!hv) |
---|
1693 | Perl_croak(aTHX_ "Bad hash"); |
---|
1694 | xhv = (XPVHV*)SvANY(hv); |
---|
1695 | oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */ |
---|
1696 | |
---|
1697 | if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) { |
---|
1698 | SV *key = sv_newmortal(); |
---|
1699 | if (entry) { |
---|
1700 | sv_setsv(key, HeSVKEY_force(entry)); |
---|
1701 | SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */ |
---|
1702 | } |
---|
1703 | else { |
---|
1704 | char *k; |
---|
1705 | HEK *hek; |
---|
1706 | |
---|
1707 | /* one HE per MAGICAL hash */ |
---|
1708 | xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */ |
---|
1709 | Zero(entry, 1, HE); |
---|
1710 | Newz(54, k, HEK_BASESIZE + sizeof(SV*), char); |
---|
1711 | hek = (HEK*)k; |
---|
1712 | HeKEY_hek(entry) = hek; |
---|
1713 | HeKLEN(entry) = HEf_SVKEY; |
---|
1714 | } |
---|
1715 | magic_nextpack((SV*) hv,mg,key); |
---|
1716 | if (SvOK(key)) { |
---|
1717 | /* force key to stay around until next time */ |
---|
1718 | HeSVKEY_set(entry, SvREFCNT_inc(key)); |
---|
1719 | return entry; /* beware, hent_val is not set */ |
---|
1720 | } |
---|
1721 | if (HeVAL(entry)) |
---|
1722 | SvREFCNT_dec(HeVAL(entry)); |
---|
1723 | Safefree(HeKEY_hek(entry)); |
---|
1724 | del_HE(entry); |
---|
1725 | xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */ |
---|
1726 | return Null(HE*); |
---|
1727 | } |
---|
1728 | #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */ |
---|
1729 | if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) |
---|
1730 | prime_env_iter(); |
---|
1731 | #endif |
---|
1732 | |
---|
1733 | if (!xhv->xhv_array /* !HvARRAY(hv) */) |
---|
1734 | Newz(506, xhv->xhv_array /* HvARRAY(hv) */, |
---|
1735 | PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */), |
---|
1736 | char); |
---|
1737 | /* At start of hash, entry is NULL. */ |
---|
1738 | if (entry) |
---|
1739 | { |
---|
1740 | entry = HeNEXT(entry); |
---|
1741 | if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) { |
---|
1742 | /* |
---|
1743 | * Skip past any placeholders -- don't want to include them in |
---|
1744 | * any iteration. |
---|
1745 | */ |
---|
1746 | while (entry && HeVAL(entry) == &PL_sv_placeholder) { |
---|
1747 | entry = HeNEXT(entry); |
---|
1748 | } |
---|
1749 | } |
---|
1750 | } |
---|
1751 | while (!entry) { |
---|
1752 | /* OK. Come to the end of the current list. Grab the next one. */ |
---|
1753 | |
---|
1754 | xhv->xhv_riter++; /* HvRITER(hv)++ */ |
---|
1755 | if (xhv->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) { |
---|
1756 | /* There is no next one. End of the hash. */ |
---|
1757 | xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */ |
---|
1758 | break; |
---|
1759 | } |
---|
1760 | /* entry = (HvARRAY(hv))[HvRITER(hv)]; */ |
---|
1761 | entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter]; |
---|
1762 | |
---|
1763 | if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) { |
---|
1764 | /* If we have an entry, but it's a placeholder, don't count it. |
---|
1765 | Try the next. */ |
---|
1766 | while (entry && HeVAL(entry) == &PL_sv_placeholder) |
---|
1767 | entry = HeNEXT(entry); |
---|
1768 | } |
---|
1769 | /* Will loop again if this linked list starts NULL |
---|
1770 | (for HV_ITERNEXT_WANTPLACEHOLDERS) |
---|
1771 | or if we run through it and find only placeholders. */ |
---|
1772 | } |
---|
1773 | |
---|
1774 | if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */ |
---|
1775 | HvLAZYDEL_off(hv); |
---|
1776 | hv_free_ent(hv, oldentry); |
---|
1777 | } |
---|
1778 | |
---|
1779 | /*if (HvREHASH(hv) && entry && !HeKREHASH(entry)) |
---|
1780 | PerlIO_printf(PerlIO_stderr(), "Awooga %p %p\n", hv, entry);*/ |
---|
1781 | |
---|
1782 | xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */ |
---|
1783 | return entry; |
---|
1784 | } |
---|
1785 | |
---|
1786 | /* |
---|
1787 | =for apidoc hv_iterkey |
---|
1788 | |
---|
1789 | Returns the key from the current position of the hash iterator. See |
---|
1790 | C<hv_iterinit>. |
---|
1791 | |
---|
1792 | =cut |
---|
1793 | */ |
---|
1794 | |
---|
1795 | char * |
---|
1796 | Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen) |
---|
1797 | { |
---|
1798 | if (HeKLEN(entry) == HEf_SVKEY) { |
---|
1799 | STRLEN len; |
---|
1800 | char *p = SvPV(HeKEY_sv(entry), len); |
---|
1801 | *retlen = len; |
---|
1802 | return p; |
---|
1803 | } |
---|
1804 | else { |
---|
1805 | *retlen = HeKLEN(entry); |
---|
1806 | return HeKEY(entry); |
---|
1807 | } |
---|
1808 | } |
---|
1809 | |
---|
1810 | /* unlike hv_iterval(), this always returns a mortal copy of the key */ |
---|
1811 | /* |
---|
1812 | =for apidoc hv_iterkeysv |
---|
1813 | |
---|
1814 | Returns the key as an C<SV*> from the current position of the hash |
---|
1815 | iterator. The return value will always be a mortal copy of the key. Also |
---|
1816 | see C<hv_iterinit>. |
---|
1817 | |
---|
1818 | =cut |
---|
1819 | */ |
---|
1820 | |
---|
1821 | SV * |
---|
1822 | Perl_hv_iterkeysv(pTHX_ register HE *entry) |
---|
1823 | { |
---|
1824 | if (HeKLEN(entry) != HEf_SVKEY) { |
---|
1825 | HEK *hek = HeKEY_hek(entry); |
---|
1826 | int flags = HEK_FLAGS(hek); |
---|
1827 | SV *sv; |
---|
1828 | |
---|
1829 | if (flags & HVhek_WASUTF8) { |
---|
1830 | /* Trouble :-) |
---|
1831 | Andreas would like keys he put in as utf8 to come back as utf8 |
---|
1832 | */ |
---|
1833 | STRLEN utf8_len = HEK_LEN(hek); |
---|
1834 | U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len); |
---|
1835 | |
---|
1836 | sv = newSVpvn ((char*)as_utf8, utf8_len); |
---|
1837 | SvUTF8_on (sv); |
---|
1838 | Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */ |
---|
1839 | } else if (flags & HVhek_REHASH) { |
---|
1840 | /* We don't have a pointer to the hv, so we have to replicate the |
---|
1841 | flag into every HEK. This hv is using custom a hasing |
---|
1842 | algorithm. Hence we can't return a shared string scalar, as |
---|
1843 | that would contain the (wrong) hash value, and might get passed |
---|
1844 | into an hv routine with a regular hash */ |
---|
1845 | |
---|
1846 | sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek)); |
---|
1847 | if (HEK_UTF8(hek)) |
---|
1848 | SvUTF8_on (sv); |
---|
1849 | } else { |
---|
1850 | sv = newSVpvn_share(HEK_KEY(hek), |
---|
1851 | (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)), |
---|
1852 | HEK_HASH(hek)); |
---|
1853 | } |
---|
1854 | return sv_2mortal(sv); |
---|
1855 | } |
---|
1856 | return sv_mortalcopy(HeKEY_sv(entry)); |
---|
1857 | } |
---|
1858 | |
---|
1859 | /* |
---|
1860 | =for apidoc hv_iterval |
---|
1861 | |
---|
1862 | Returns the value from the current position of the hash iterator. See |
---|
1863 | C<hv_iterkey>. |
---|
1864 | |
---|
1865 | =cut |
---|
1866 | */ |
---|
1867 | |
---|
1868 | SV * |
---|
1869 | Perl_hv_iterval(pTHX_ HV *hv, register HE *entry) |
---|
1870 | { |
---|
1871 | if (SvRMAGICAL(hv)) { |
---|
1872 | if (mg_find((SV*)hv, PERL_MAGIC_tied)) { |
---|
1873 | SV* sv = sv_newmortal(); |
---|
1874 | if (HeKLEN(entry) == HEf_SVKEY) |
---|
1875 | mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY); |
---|
1876 | else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry)); |
---|
1877 | return sv; |
---|
1878 | } |
---|
1879 | } |
---|
1880 | return HeVAL(entry); |
---|
1881 | } |
---|
1882 | |
---|
1883 | /* |
---|
1884 | =for apidoc hv_iternextsv |
---|
1885 | |
---|
1886 | Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one |
---|
1887 | operation. |
---|
1888 | |
---|
1889 | =cut |
---|
1890 | */ |
---|
1891 | |
---|
1892 | SV * |
---|
1893 | Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen) |
---|
1894 | { |
---|
1895 | HE *he; |
---|
1896 | if ( (he = hv_iternext_flags(hv, 0)) == NULL) |
---|
1897 | return NULL; |
---|
1898 | *key = hv_iterkey(he, retlen); |
---|
1899 | return hv_iterval(hv, he); |
---|
1900 | } |
---|
1901 | |
---|
1902 | /* |
---|
1903 | =for apidoc hv_magic |
---|
1904 | |
---|
1905 | Adds magic to a hash. See C<sv_magic>. |
---|
1906 | |
---|
1907 | =cut |
---|
1908 | */ |
---|
1909 | |
---|
1910 | void |
---|
1911 | Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how) |
---|
1912 | { |
---|
1913 | sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0); |
---|
1914 | } |
---|
1915 | |
---|
1916 | #if 0 /* use the macro from hv.h instead */ |
---|
1917 | |
---|
1918 | char* |
---|
1919 | Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash) |
---|
1920 | { |
---|
1921 | return HEK_KEY(share_hek(sv, len, hash)); |
---|
1922 | } |
---|
1923 | |
---|
1924 | #endif |
---|
1925 | |
---|
1926 | /* possibly free a shared string if no one has access to it |
---|
1927 | * len and hash must both be valid for str. |
---|
1928 | */ |
---|
1929 | void |
---|
1930 | Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash) |
---|
1931 | { |
---|
1932 | unshare_hek_or_pvn (NULL, str, len, hash); |
---|
1933 | } |
---|
1934 | |
---|
1935 | |
---|
1936 | void |
---|
1937 | Perl_unshare_hek(pTHX_ HEK *hek) |
---|
1938 | { |
---|
1939 | unshare_hek_or_pvn(hek, NULL, 0, 0); |
---|
1940 | } |
---|
1941 | |
---|
1942 | /* possibly free a shared string if no one has access to it |
---|
1943 | hek if non-NULL takes priority over the other 3, else str, len and hash |
---|
1944 | are used. If so, len and hash must both be valid for str. |
---|
1945 | */ |
---|
1946 | STATIC void |
---|
1947 | S_unshare_hek_or_pvn(pTHX_ HEK *hek, const char *str, I32 len, U32 hash) |
---|
1948 | { |
---|
1949 | register XPVHV* xhv; |
---|
1950 | register HE *entry; |
---|
1951 | register HE **oentry; |
---|
1952 | register I32 i = 1; |
---|
1953 | I32 found = 0; |
---|
1954 | bool is_utf8 = FALSE; |
---|
1955 | int k_flags = 0; |
---|
1956 | const char *save = str; |
---|
1957 | |
---|
1958 | if (hek) { |
---|
1959 | hash = HEK_HASH(hek); |
---|
1960 | } else if (len < 0) { |
---|
1961 | STRLEN tmplen = -len; |
---|
1962 | is_utf8 = TRUE; |
---|
1963 | /* See the note in hv_fetch(). --jhi */ |
---|
1964 | str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8); |
---|
1965 | len = tmplen; |
---|
1966 | if (is_utf8) |
---|
1967 | k_flags = HVhek_UTF8; |
---|
1968 | if (str != save) |
---|
1969 | k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY; |
---|
1970 | } |
---|
1971 | |
---|
1972 | /* what follows is the moral equivalent of: |
---|
1973 | if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) { |
---|
1974 | if (--*Svp == Nullsv) |
---|
1975 | hv_delete(PL_strtab, str, len, G_DISCARD, hash); |
---|
1976 | } */ |
---|
1977 | xhv = (XPVHV*)SvANY(PL_strtab); |
---|
1978 | /* assert(xhv_array != 0) */ |
---|
1979 | LOCK_STRTAB_MUTEX; |
---|
1980 | /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */ |
---|
1981 | oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max]; |
---|
1982 | if (hek) { |
---|
1983 | for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) { |
---|
1984 | if (HeKEY_hek(entry) != hek) |
---|
1985 | continue; |
---|
1986 | found = 1; |
---|
1987 | break; |
---|
1988 | } |
---|
1989 | } else { |
---|
1990 | int flags_masked = k_flags & HVhek_MASK; |
---|
1991 | for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) { |
---|
1992 | if (HeHASH(entry) != hash) /* strings can't be equal */ |
---|
1993 | continue; |
---|
1994 | if (HeKLEN(entry) != len) |
---|
1995 | continue; |
---|
1996 | if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */ |
---|
1997 | continue; |
---|
1998 | if (HeKFLAGS(entry) != flags_masked) |
---|
1999 | continue; |
---|
2000 | found = 1; |
---|
2001 | break; |
---|
2002 | } |
---|
2003 | } |
---|
2004 | |
---|
2005 | if (found) { |
---|
2006 | if (--HeVAL(entry) == Nullsv) { |
---|
2007 | *oentry = HeNEXT(entry); |
---|
2008 | if (i && !*oentry) |
---|
2009 | xhv->xhv_fill--; /* HvFILL(hv)-- */ |
---|
2010 | Safefree(HeKEY_hek(entry)); |
---|
2011 | del_HE(entry); |
---|
2012 | xhv->xhv_keys--; /* HvKEYS(hv)-- */ |
---|
2013 | } |
---|
2014 | } |
---|
2015 | |
---|
2016 | UNLOCK_STRTAB_MUTEX; |
---|
2017 | if (!found && ckWARN_d(WARN_INTERNAL)) |
---|
2018 | Perl_warner(aTHX_ packWARN(WARN_INTERNAL), |
---|
2019 | "Attempt to free non-existent shared string '%s'%s", |
---|
2020 | hek ? HEK_KEY(hek) : str, |
---|
2021 | (k_flags & HVhek_UTF8) ? " (utf8)" : ""); |
---|
2022 | if (k_flags & HVhek_FREEKEY) |
---|
2023 | Safefree(str); |
---|
2024 | } |
---|
2025 | |
---|
2026 | /* get a (constant) string ptr from the global string table |
---|
2027 | * string will get added if it is not already there. |
---|
2028 | * len and hash must both be valid for str. |
---|
2029 | */ |
---|
2030 | HEK * |
---|
2031 | Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash) |
---|
2032 | { |
---|
2033 | bool is_utf8 = FALSE; |
---|
2034 | int flags = 0; |
---|
2035 | const char *save = str; |
---|
2036 | |
---|
2037 | if (len < 0) { |
---|
2038 | STRLEN tmplen = -len; |
---|
2039 | is_utf8 = TRUE; |
---|
2040 | /* See the note in hv_fetch(). --jhi */ |
---|
2041 | str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8); |
---|
2042 | len = tmplen; |
---|
2043 | /* If we were able to downgrade here, then than means that we were passed |
---|
2044 | in a key which only had chars 0-255, but was utf8 encoded. */ |
---|
2045 | if (is_utf8) |
---|
2046 | flags = HVhek_UTF8; |
---|
2047 | /* If we found we were able to downgrade the string to bytes, then |
---|
2048 | we should flag that it needs upgrading on keys or each. Also flag |
---|
2049 | that we need share_hek_flags to free the string. */ |
---|
2050 | if (str != save) |
---|
2051 | flags |= HVhek_WASUTF8 | HVhek_FREEKEY; |
---|
2052 | } |
---|
2053 | |
---|
2054 | return share_hek_flags (str, len, hash, flags); |
---|
2055 | } |
---|
2056 | |
---|
2057 | STATIC HEK * |
---|
2058 | S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags) |
---|
2059 | { |
---|
2060 | register XPVHV* xhv; |
---|
2061 | register HE *entry; |
---|
2062 | register HE **oentry; |
---|
2063 | register I32 i = 1; |
---|
2064 | I32 found = 0; |
---|
2065 | int flags_masked = flags & HVhek_MASK; |
---|
2066 | |
---|
2067 | /* what follows is the moral equivalent of: |
---|
2068 | |
---|
2069 | if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE))) |
---|
2070 | hv_store(PL_strtab, str, len, Nullsv, hash); |
---|
2071 | |
---|
2072 | Can't rehash the shared string table, so not sure if it's worth |
---|
2073 | counting the number of entries in the linked list |
---|
2074 | */ |
---|
2075 | xhv = (XPVHV*)SvANY(PL_strtab); |
---|
2076 | /* assert(xhv_array != 0) */ |
---|
2077 | LOCK_STRTAB_MUTEX; |
---|
2078 | /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */ |
---|
2079 | oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max]; |
---|
2080 | for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) { |
---|
2081 | if (HeHASH(entry) != hash) /* strings can't be equal */ |
---|
2082 | continue; |
---|
2083 | if (HeKLEN(entry) != len) |
---|
2084 | continue; |
---|
2085 | if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */ |
---|
2086 | continue; |
---|
2087 | if (HeKFLAGS(entry) != flags_masked) |
---|
2088 | continue; |
---|
2089 | found = 1; |
---|
2090 | break; |
---|
2091 | } |
---|
2092 | if (!found) { |
---|
2093 | entry = new_HE(); |
---|
2094 | HeKEY_hek(entry) = save_hek_flags(str, len, hash, flags_masked); |
---|
2095 | HeVAL(entry) = Nullsv; |
---|
2096 | HeNEXT(entry) = *oentry; |
---|
2097 | *oentry = entry; |
---|
2098 | xhv->xhv_keys++; /* HvKEYS(hv)++ */ |
---|
2099 | if (i) { /* initial entry? */ |
---|
2100 | xhv->xhv_fill++; /* HvFILL(hv)++ */ |
---|
2101 | } else if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) { |
---|
2102 | hsplit(PL_strtab); |
---|
2103 | } |
---|
2104 | } |
---|
2105 | |
---|
2106 | ++HeVAL(entry); /* use value slot as REFCNT */ |
---|
2107 | UNLOCK_STRTAB_MUTEX; |
---|
2108 | |
---|
2109 | if (flags & HVhek_FREEKEY) |
---|
2110 | Safefree(str); |
---|
2111 | |
---|
2112 | return HeKEY_hek(entry); |
---|
2113 | } |
---|