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