This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Supply description for only test lacking one.
[perl5.git] / hv.c
CommitLineData
a0d0e21e 1/* hv.c
79072805 2 *
1129b882
NC
3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 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/*
4ac71550
TC
12 * I sit beside the fire and think
13 * of all that I have seen.
14 * --Bilbo
15 *
16 * [p.278 of _The Lord of the Rings_, II/iii: "The Ring Goes South"]
79072805
LW
17 */
18
d5afce77
RB
19/*
20=head1 Hash Manipulation Functions
db4fbf16
FC
21A HV structure represents a Perl hash. It consists mainly of an array
22of pointers, each of which points to a linked list of HE structures. The
166f8a29 23array is indexed by the hash function of the key, so each linked list
db4fbf16 24represents all the hash entries with the same hash value. Each HE contains
166f8a29
DM
25a pointer to the actual value, plus a pointer to a HEK structure which
26holds the key and hash value.
27
28=cut
29
d5afce77
RB
30*/
31
79072805 32#include "EXTERN.h"
864dbfa3 33#define PERL_IN_HV_C
3d78eb94 34#define PERL_HASH_INTERNAL_ACCESS
79072805
LW
35#include "perl.h"
36
8e317198 37#define DO_HSPLIT(xhv) ((xhv)->xhv_keys > (xhv)->xhv_max) /* HvTOTALKEYS(hv) > HvMAX(hv) */
9faf471a 38#define HV_FILL_THRESHOLD 31
fdcd69b6 39
d75ce684 40static const char S_strtab_error[]
5d2b1485
NC
41 = "Cannot modify shared string table in hv_%s";
42
c941fb51
NC
43#ifdef PURIFY
44
45#define new_HE() (HE*)safemalloc(sizeof(HE))
46#define del_HE(p) safefree((char*)p)
47
48#else
49
76e3520e 50STATIC HE*
cea2e8a9 51S_new_he(pTHX)
4633a7c4
LW
52{
53 HE* he;
0bd48802 54 void ** const root = &PL_body_roots[HE_SVSLOT];
6a93a7e5 55
6a93a7e5 56 if (!*root)
1e30fcd5 57 Perl_more_bodies(aTHX_ HE_SVSLOT, sizeof(HE), PERL_ARENA_SIZE);
10edeb5d 58 he = (HE*) *root;
ce3e5c45 59 assert(he);
6a93a7e5 60 *root = HeNEXT(he);
333f433b 61 return he;
4633a7c4
LW
62}
63
c941fb51
NC
64#define new_HE() new_he()
65#define del_HE(p) \
66 STMT_START { \
6a93a7e5
NC
67 HeNEXT(p) = (HE*)(PL_body_roots[HE_SVSLOT]); \
68 PL_body_roots[HE_SVSLOT] = p; \
c941fb51 69 } STMT_END
d33b2eba 70
d33b2eba 71
d33b2eba
GS
72
73#endif
74
76e3520e 75STATIC HEK *
5f66b61c 76S_save_hek_flags(const char *str, I32 len, U32 hash, int flags)
bbce6d69 77{
35a4481c 78 const int flags_masked = flags & HVhek_MASK;
bbce6d69 79 char *k;
eb578fdb 80 HEK *hek;
1c846c1f 81
7918f24d
NC
82 PERL_ARGS_ASSERT_SAVE_HEK_FLAGS;
83
a02a5408 84 Newx(k, HEK_BASESIZE + len + 2, char);
bbce6d69 85 hek = (HEK*)k;
ff68c719 86 Copy(str, HEK_KEY(hek), len, char);
e05949c7 87 HEK_KEY(hek)[len] = 0;
ff68c719 88 HEK_LEN(hek) = len;
89 HEK_HASH(hek) = hash;
45e34800 90 HEK_FLAGS(hek) = (unsigned char)flags_masked | HVhek_UNSHARED;
dcf933a4
NC
91
92 if (flags & HVhek_FREEKEY)
93 Safefree(str);
bbce6d69 94 return hek;
95}
96
4a31713e 97/* free the pool of temporary HE/HEK pairs returned by hv_fetch_ent
dd28f7bb
DM
98 * for tied hashes */
99
100void
101Perl_free_tied_hv_pool(pTHX)
102{
dd28f7bb
DM
103 HE *he = PL_hv_fetch_ent_mh;
104 while (he) {
9d4ba2ae 105 HE * const ohe = he;
dd28f7bb 106 Safefree(HeKEY_hek(he));
dd28f7bb
DM
107 he = HeNEXT(he);
108 del_HE(ohe);
109 }
4608196e 110 PL_hv_fetch_ent_mh = NULL;
dd28f7bb
DM
111}
112
d18c6117 113#if defined(USE_ITHREADS)
0bff533c
NC
114HEK *
115Perl_hek_dup(pTHX_ HEK *source, CLONE_PARAMS* param)
116{
566771cc 117 HEK *shared;
9d4ba2ae 118
7918f24d 119 PERL_ARGS_ASSERT_HEK_DUP;
9d4ba2ae 120 PERL_UNUSED_ARG(param);
0bff533c 121
566771cc
NC
122 if (!source)
123 return NULL;
124
125 shared = (HEK*)ptr_table_fetch(PL_ptr_table, source);
0bff533c
NC
126 if (shared) {
127 /* We already shared this hash key. */
454f1e26 128 (void)share_hek_hek(shared);
0bff533c
NC
129 }
130 else {
658b4a4a 131 shared
6e838c70
NC
132 = share_hek_flags(HEK_KEY(source), HEK_LEN(source),
133 HEK_HASH(source), HEK_FLAGS(source));
658b4a4a 134 ptr_table_store(PL_ptr_table, source, shared);
0bff533c 135 }
658b4a4a 136 return shared;
0bff533c
NC
137}
138
d18c6117 139HE *
5c4138a0 140Perl_he_dup(pTHX_ const HE *e, bool shared, CLONE_PARAMS* param)
d18c6117
GS
141{
142 HE *ret;
143
7918f24d
NC
144 PERL_ARGS_ASSERT_HE_DUP;
145
d18c6117 146 if (!e)
4608196e 147 return NULL;
7766f137
GS
148 /* look for it in the table first */
149 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
150 if (ret)
151 return ret;
152
153 /* create anew and remember what it is */
d33b2eba 154 ret = new_HE();
7766f137
GS
155 ptr_table_store(PL_ptr_table, e, ret);
156
d2d73c3e 157 HeNEXT(ret) = he_dup(HeNEXT(e),shared, param);
dd28f7bb
DM
158 if (HeKLEN(e) == HEf_SVKEY) {
159 char *k;
ad64d0ec 160 Newx(k, HEK_BASESIZE + sizeof(const SV *), char);
dd28f7bb 161 HeKEY_hek(ret) = (HEK*)k;
a09252eb 162 HeKEY_sv(ret) = sv_dup_inc(HeKEY_sv(e), param);
dd28f7bb 163 }
c21d1a0f 164 else if (shared) {
0bff533c
NC
165 /* This is hek_dup inlined, which seems to be important for speed
166 reasons. */
1b6737cc 167 HEK * const source = HeKEY_hek(e);
658b4a4a 168 HEK *shared = (HEK*)ptr_table_fetch(PL_ptr_table, source);
c21d1a0f
NC
169
170 if (shared) {
171 /* We already shared this hash key. */
454f1e26 172 (void)share_hek_hek(shared);
c21d1a0f
NC
173 }
174 else {
658b4a4a 175 shared
6e838c70
NC
176 = share_hek_flags(HEK_KEY(source), HEK_LEN(source),
177 HEK_HASH(source), HEK_FLAGS(source));
658b4a4a 178 ptr_table_store(PL_ptr_table, source, shared);
c21d1a0f 179 }
658b4a4a 180 HeKEY_hek(ret) = shared;
c21d1a0f 181 }
d18c6117 182 else
19692e8d
NC
183 HeKEY_hek(ret) = save_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
184 HeKFLAGS(e));
a09252eb 185 HeVAL(ret) = sv_dup_inc(HeVAL(e), param);
d18c6117
GS
186 return ret;
187}
188#endif /* USE_ITHREADS */
189
1b1f1335 190static void
2393f1b9
JH
191S_hv_notallowed(pTHX_ int flags, const char *key, I32 klen,
192 const char *msg)
1b1f1335 193{
1b6737cc 194 SV * const sv = sv_newmortal();
7918f24d
NC
195
196 PERL_ARGS_ASSERT_HV_NOTALLOWED;
197
19692e8d 198 if (!(flags & HVhek_FREEKEY)) {
1b1f1335
NIS
199 sv_setpvn(sv, key, klen);
200 }
201 else {
202 /* Need to free saved eventually assign to mortal SV */
34c3c4e3 203 /* XXX is this line an error ???: SV *sv = sv_newmortal(); */
1b1f1335
NIS
204 sv_usepvn(sv, (char *) key, klen);
205 }
19692e8d 206 if (flags & HVhek_UTF8) {
1b1f1335
NIS
207 SvUTF8_on(sv);
208 }
be2597df 209 Perl_croak(aTHX_ msg, SVfARG(sv));
1b1f1335
NIS
210}
211
fde52b5c 212/* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
213 * contains an SV* */
214
34a6f7b4
NC
215/*
216=for apidoc hv_store
217
a05d6c5d
TC
218Stores an SV in a hash. The hash key is specified as C<key> and the
219absolute value of C<klen> is the length of the key. If C<klen> is
220negative the key is assumed to be in UTF-8-encoded Unicode. The
221C<hash> parameter is the precomputed hash value; if it is zero then
222Perl will compute it.
223
224The return value will be
34a6f7b4
NC
225NULL if the operation failed or if the value did not need to be actually
226stored within the hash (as in the case of tied hashes). Otherwise it can
227be dereferenced to get the original C<SV*>. Note that the caller is
228responsible for suitably incrementing the reference count of C<val> before
229the call, and decrementing it if the function returned NULL. Effectively
230a successful hv_store takes ownership of one reference to C<val>. This is
231usually what you want; a newly created SV has a reference count of one, so
232if all your code does is create SVs then store them in a hash, hv_store
233will own the only reference to the new SV, and your code doesn't need to do
234anything further to tidy up. hv_store is not implemented as a call to
235hv_store_ent, and does not create a temporary SV for the key, so if your
236key data is not already in SV form then use hv_store in preference to
237hv_store_ent.
238
239See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
240information on how to use this function on tied hashes.
241
34a6f7b4
NC
242=for apidoc hv_store_ent
243
244Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
245parameter is the precomputed hash value; if it is zero then Perl will
246compute it. The return value is the new hash entry so created. It will be
247NULL if the operation failed or if the value did not need to be actually
248stored within the hash (as in the case of tied hashes). Otherwise the
249contents of the return value can be accessed using the C<He?> macros
250described here. Note that the caller is responsible for suitably
251incrementing the reference count of C<val> before the call, and
252decrementing it if the function returned NULL. Effectively a successful
253hv_store_ent takes ownership of one reference to C<val>. This is
254usually what you want; a newly created SV has a reference count of one, so
255if all your code does is create SVs then store them in a hash, hv_store
256will own the only reference to the new SV, and your code doesn't need to do
257anything further to tidy up. Note that hv_store_ent only reads the C<key>;
258unlike C<val> it does not take ownership of it, so maintaining the correct
259reference count on C<key> is entirely the caller's responsibility. hv_store
260is not implemented as a call to hv_store_ent, and does not create a temporary
261SV for the key, so if your key data is not already in SV form then use
262hv_store in preference to hv_store_ent.
263
264See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
265information on how to use this function on tied hashes.
266
34a6f7b4
NC
267=for apidoc hv_exists
268
269Returns a boolean indicating whether the specified hash key exists. The
a05d6c5d
TC
270absolute value of C<klen> is the length of the key. If C<klen> is
271negative the key is assumed to be in UTF-8-encoded Unicode.
34a6f7b4 272
954c1994
GS
273=for apidoc hv_fetch
274
a05d6c5d
TC
275Returns the SV which corresponds to the specified key in the hash.
276The absolute value of C<klen> is the length of the key. If C<klen> is
277negative the key is assumed to be in UTF-8-encoded Unicode. If
43d3b06a
KW
278C<lval> is set then the fetch will be part of a store. This means that if
279there is no value in the hash associated with the given key, then one is
280created and a pointer to it is returned. The C<SV*> it points to can be
281assigned to. But always check that the
a05d6c5d 282return value is non-null before dereferencing it to an C<SV*>.
954c1994 283
96f1132b 284See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
954c1994
GS
285information on how to use this function on tied hashes.
286
34a6f7b4
NC
287=for apidoc hv_exists_ent
288
db4fbf16
FC
289Returns a boolean indicating whether
290the specified hash key exists. C<hash>
34a6f7b4
NC
291can be a valid precomputed hash value, or 0 to ask for it to be
292computed.
293
294=cut
295*/
296
d1be9408 297/* returns an HE * structure with the all fields set */
fde52b5c 298/* note that hent_val will be a mortal sv for MAGICAL hashes */
954c1994
GS
299/*
300=for apidoc hv_fetch_ent
301
302Returns the hash entry which corresponds to the specified key in the hash.
303C<hash> must be a valid precomputed hash number for the given C<key>, or 0
304if you want the function to compute it. IF C<lval> is set then the fetch
305will be part of a store. Make sure the return value is non-null before
b24b84ef 306accessing it. The return value when C<hv> is a tied hash is a pointer to a
954c1994 307static location, so be sure to make a copy of the structure if you need to
1c846c1f 308store it somewhere.
954c1994 309
96f1132b 310See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
954c1994
GS
311information on how to use this function on tied hashes.
312
313=cut
314*/
315
a038e571
NC
316/* Common code for hv_delete()/hv_exists()/hv_fetch()/hv_store() */
317void *
318Perl_hv_common_key_len(pTHX_ HV *hv, const char *key, I32 klen_i32,
319 const int action, SV *val, const U32 hash)
320{
321 STRLEN klen;
322 int flags;
323
7918f24d
NC
324 PERL_ARGS_ASSERT_HV_COMMON_KEY_LEN;
325
a038e571
NC
326 if (klen_i32 < 0) {
327 klen = -klen_i32;
328 flags = HVhek_UTF8;
329 } else {
330 klen = klen_i32;
331 flags = 0;
332 }
333 return hv_common(hv, NULL, key, klen, flags, action, val, hash);
334}
335
63c89345 336void *
d3ba3f5c 337Perl_hv_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
5aaab254 338 int flags, int action, SV *val, U32 hash)
113738bb 339{
27da23d5 340 dVAR;
b2c64049 341 XPVHV* xhv;
b2c64049
NC
342 HE *entry;
343 HE **oentry;
fde52b5c 344 SV *sv;
da58a35d 345 bool is_utf8;
113738bb 346 int masked_flags;
3c84c864 347 const int return_svp = action & HV_FETCH_JUST_SV;
fde52b5c 348
349 if (!hv)
a4fc7abc 350 return NULL;
e4787c0c 351 if (SvTYPE(hv) == (svtype)SVTYPEMASK)
8265e3d1
NC
352 return NULL;
353
354 assert(SvTYPE(hv) == SVt_PVHV);
fde52b5c 355
bdee33e4 356 if (SvSMAGICAL(hv) && SvGMAGICAL(hv) && !(action & HV_DISABLE_UVAR_XKEY)) {
fda2d18a 357 MAGIC* mg;
ad64d0ec 358 if ((mg = mg_find((const SV *)hv, PERL_MAGIC_uvar))) {
fda2d18a
NC
359 struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
360 if (uf->uf_set == NULL) {
361 SV* obj = mg->mg_obj;
362
363 if (!keysv) {
59cd0e26
NC
364 keysv = newSVpvn_flags(key, klen, SVs_TEMP |
365 ((flags & HVhek_UTF8)
366 ? SVf_UTF8 : 0));
fda2d18a
NC
367 }
368
369 mg->mg_obj = keysv; /* pass key */
370 uf->uf_index = action; /* pass action */
ad64d0ec 371 magic_getuvar(MUTABLE_SV(hv), mg);
fda2d18a
NC
372 keysv = mg->mg_obj; /* may have changed */
373 mg->mg_obj = obj;
374
375 /* If the key may have changed, then we need to invalidate
376 any passed-in computed hash value. */
377 hash = 0;
378 }
379 }
bdee33e4 380 }
113738bb 381 if (keysv) {
e593d2fe
AE
382 if (flags & HVhek_FREEKEY)
383 Safefree(key);
5c144d81 384 key = SvPV_const(keysv, klen);
113738bb 385 is_utf8 = (SvUTF8(keysv) != 0);
44b87b50
NC
386 if (SvIsCOW_shared_hash(keysv)) {
387 flags = HVhek_KEYCANONICAL | (is_utf8 ? HVhek_UTF8 : 0);
388 } else {
0ddecb91 389 flags = is_utf8 ? HVhek_UTF8 : 0;
44b87b50 390 }
113738bb 391 } else {
c1fe5510 392 is_utf8 = ((flags & HVhek_UTF8) ? TRUE : FALSE);
113738bb 393 }
113738bb 394
9dbc5603 395 if (action & HV_DELETE) {
3c84c864 396 return (void *) hv_delete_common(hv, keysv, key, klen,
0ddecb91 397 flags, action, hash);
9dbc5603
NC
398 }
399
b2c64049 400 xhv = (XPVHV*)SvANY(hv);
7f66fda2 401 if (SvMAGICAL(hv)) {
6136c704 402 if (SvRMAGICAL(hv) && !(action & (HV_FETCH_ISSTORE|HV_FETCH_ISEXISTS))) {
ad64d0ec
NC
403 if (mg_find((const SV *)hv, PERL_MAGIC_tied)
404 || SvGMAGICAL((const SV *)hv))
e62cc96a 405 {
3c84c864 406 /* FIXME should be able to skimp on the HE/HEK here when
7f66fda2 407 HV_FETCH_JUST_SV is true. */
7f66fda2 408 if (!keysv) {
740cce10
NC
409 keysv = newSVpvn_utf8(key, klen, is_utf8);
410 } else {
7f66fda2 411 keysv = newSVsv(keysv);
113738bb 412 }
44a2ac75 413 sv = sv_newmortal();
ad64d0ec 414 mg_copy(MUTABLE_SV(hv), sv, (char *)keysv, HEf_SVKEY);
7f66fda2
NC
415
416 /* grab a fake HE/HEK pair from the pool or make a new one */
417 entry = PL_hv_fetch_ent_mh;
418 if (entry)
419 PL_hv_fetch_ent_mh = HeNEXT(entry);
420 else {
421 char *k;
422 entry = new_HE();
ad64d0ec 423 Newx(k, HEK_BASESIZE + sizeof(const SV *), char);
7f66fda2
NC
424 HeKEY_hek(entry) = (HEK*)k;
425 }
4608196e 426 HeNEXT(entry) = NULL;
7f66fda2
NC
427 HeSVKEY_set(entry, keysv);
428 HeVAL(entry) = sv;
429 sv_upgrade(sv, SVt_PVLV);
430 LvTYPE(sv) = 'T';
431 /* so we can free entry when freeing sv */
ad64d0ec 432 LvTARG(sv) = MUTABLE_SV(entry);
7f66fda2
NC
433
434 /* XXX remove at some point? */
435 if (flags & HVhek_FREEKEY)
436 Safefree(key);
437
3c84c864
NC
438 if (return_svp) {
439 return entry ? (void *) &HeVAL(entry) : NULL;
440 }
441 return (void *) entry;
113738bb 442 }
7f66fda2 443#ifdef ENV_IS_CASELESS
ad64d0ec 444 else if (mg_find((const SV *)hv, PERL_MAGIC_env)) {
7f66fda2
NC
445 U32 i;
446 for (i = 0; i < klen; ++i)
447 if (isLOWER(key[i])) {
086cb327
NC
448 /* Would be nice if we had a routine to do the
449 copy and upercase in a single pass through. */
0bd48802 450 const char * const nkey = strupr(savepvn(key,klen));
086cb327
NC
451 /* Note that this fetch is for nkey (the uppercased
452 key) whereas the store is for key (the original) */
63c89345
NC
453 void *result = hv_common(hv, NULL, nkey, klen,
454 HVhek_FREEKEY, /* free nkey */
455 0 /* non-LVAL fetch */
3c84c864
NC
456 | HV_DISABLE_UVAR_XKEY
457 | return_svp,
63c89345
NC
458 NULL /* no value */,
459 0 /* compute hash */);
26488bcf 460 if (!result && (action & HV_FETCH_LVALUE)) {
086cb327
NC
461 /* This call will free key if necessary.
462 Do it this way to encourage compiler to tail
463 call optimise. */
63c89345
NC
464 result = hv_common(hv, keysv, key, klen, flags,
465 HV_FETCH_ISSTORE
3c84c864
NC
466 | HV_DISABLE_UVAR_XKEY
467 | return_svp,
63c89345 468 newSV(0), hash);
086cb327
NC
469 } else {
470 if (flags & HVhek_FREEKEY)
471 Safefree(key);
472 }
63c89345 473 return result;
7f66fda2 474 }
902173a3 475 }
7f66fda2
NC
476#endif
477 } /* ISFETCH */
478 else if (SvRMAGICAL(hv) && (action & HV_FETCH_ISEXISTS)) {
ad64d0ec
NC
479 if (mg_find((const SV *)hv, PERL_MAGIC_tied)
480 || SvGMAGICAL((const SV *)hv)) {
b2c64049
NC
481 /* I don't understand why hv_exists_ent has svret and sv,
482 whereas hv_exists only had one. */
9d4ba2ae 483 SV * const svret = sv_newmortal();
b2c64049 484 sv = sv_newmortal();
7f66fda2
NC
485
486 if (keysv || is_utf8) {
487 if (!keysv) {
740cce10 488 keysv = newSVpvn_utf8(key, klen, TRUE);
7f66fda2
NC
489 } else {
490 keysv = newSVsv(keysv);
491 }
ad64d0ec 492 mg_copy(MUTABLE_SV(hv), sv, (char *)sv_2mortal(keysv), HEf_SVKEY);
b2c64049 493 } else {
ad64d0ec 494 mg_copy(MUTABLE_SV(hv), sv, key, klen);
7f66fda2 495 }
b2c64049
NC
496 if (flags & HVhek_FREEKEY)
497 Safefree(key);
7f66fda2
NC
498 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
499 /* This cast somewhat evil, but I'm merely using NULL/
500 not NULL to return the boolean exists.
501 And I know hv is not NULL. */
3c84c864 502 return SvTRUE(svret) ? (void *)hv : NULL;
e7152ba2 503 }
7f66fda2 504#ifdef ENV_IS_CASELESS
ad64d0ec 505 else if (mg_find((const SV *)hv, PERL_MAGIC_env)) {
7f66fda2 506 /* XXX This code isn't UTF8 clean. */
a15d23f8 507 char * const keysave = (char * const)key;
b2c64049
NC
508 /* Will need to free this, so set FREEKEY flag. */
509 key = savepvn(key,klen);
510 key = (const char*)strupr((char*)key);
6136c704 511 is_utf8 = FALSE;
7f66fda2 512 hash = 0;
8b4f7dd5 513 keysv = 0;
b2c64049
NC
514
515 if (flags & HVhek_FREEKEY) {
516 Safefree(keysave);
517 }
518 flags |= HVhek_FREEKEY;
7f66fda2 519 }
902173a3 520#endif
7f66fda2 521 } /* ISEXISTS */
b2c64049
NC
522 else if (action & HV_FETCH_ISSTORE) {
523 bool needs_copy;
524 bool needs_store;
525 hv_magic_check (hv, &needs_copy, &needs_store);
526 if (needs_copy) {
9a9b5ec9 527 const bool save_taint = TAINT_get;
b2c64049
NC
528 if (keysv || is_utf8) {
529 if (!keysv) {
740cce10 530 keysv = newSVpvn_utf8(key, klen, TRUE);
b2c64049 531 }
284167a5
S
532 if (TAINTING_get)
533 TAINT_set(SvTAINTED(keysv));
b2c64049 534 keysv = sv_2mortal(newSVsv(keysv));
ad64d0ec 535 mg_copy(MUTABLE_SV(hv), val, (char*)keysv, HEf_SVKEY);
b2c64049 536 } else {
ad64d0ec 537 mg_copy(MUTABLE_SV(hv), val, key, klen);
b2c64049
NC
538 }
539
540 TAINT_IF(save_taint);
9a9b5ec9
DM
541#ifdef NO_TAINT_SUPPORT
542 PERL_UNUSED_VAR(save_taint);
543#endif
1baaf5d7 544 if (!needs_store) {
b2c64049
NC
545 if (flags & HVhek_FREEKEY)
546 Safefree(key);
4608196e 547 return NULL;
b2c64049
NC
548 }
549#ifdef ENV_IS_CASELESS
ad64d0ec 550 else if (mg_find((const SV *)hv, PERL_MAGIC_env)) {
b2c64049
NC
551 /* XXX This code isn't UTF8 clean. */
552 const char *keysave = key;
553 /* Will need to free this, so set FREEKEY flag. */
554 key = savepvn(key,klen);
555 key = (const char*)strupr((char*)key);
6136c704 556 is_utf8 = FALSE;
b2c64049 557 hash = 0;
8b4f7dd5 558 keysv = 0;
b2c64049
NC
559
560 if (flags & HVhek_FREEKEY) {
561 Safefree(keysave);
562 }
563 flags |= HVhek_FREEKEY;
564 }
565#endif
566 }
567 } /* ISSTORE */
7f66fda2 568 } /* SvMAGICAL */
fde52b5c 569
7b2c381c 570 if (!HvARRAY(hv)) {
b2c64049 571 if ((action & (HV_FETCH_LVALUE | HV_FETCH_ISSTORE))
fde52b5c 572#ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
ad64d0ec
NC
573 || (SvRMAGICAL((const SV *)hv)
574 && mg_find((const SV *)hv, PERL_MAGIC_env))
fde52b5c 575#endif
d58e6666
NC
576 ) {
577 char *array;
a02a5408 578 Newxz(array,
cbec9347 579 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
d58e6666
NC
580 char);
581 HvARRAY(hv) = (HE**)array;
582 }
7f66fda2
NC
583#ifdef DYNAMIC_ENV_FETCH
584 else if (action & HV_FETCH_ISEXISTS) {
585 /* for an %ENV exists, if we do an insert it's by a recursive
586 store call, so avoid creating HvARRAY(hv) right now. */
587 }
588#endif
113738bb
NC
589 else {
590 /* XXX remove at some point? */
591 if (flags & HVhek_FREEKEY)
592 Safefree(key);
593
3c84c864 594 return NULL;
113738bb 595 }
fde52b5c 596 }
597
37ae23ff 598 if (is_utf8 && !(flags & HVhek_KEYCANONICAL)) {
41d88b63 599 char * const keysave = (char *)key;
f9a63242 600 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
19692e8d 601 if (is_utf8)
c1fe5510
NC
602 flags |= HVhek_UTF8;
603 else
604 flags &= ~HVhek_UTF8;
7f66fda2
NC
605 if (key != keysave) {
606 if (flags & HVhek_FREEKEY)
607 Safefree(keysave);
19692e8d 608 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
527df579
NC
609 /* If the caller calculated a hash, it was on the sequence of
610 octets that are the UTF-8 form. We've now changed the sequence
611 of octets stored to that of the equivalent byte representation,
612 so the hash we need is different. */
613 hash = 0;
7f66fda2 614 }
19692e8d 615 }
f9a63242 616
7dc86639
YO
617 if (!hash) {
618 if (keysv && (SvIsCOW_shared_hash(keysv)))
619 hash = SvSHARED_HASH(keysv);
620 else
621 PERL_HASH(hash, key, klen);
622 }
effa1e2d 623
113738bb
NC
624 masked_flags = (flags & HVhek_MASK);
625
7f66fda2 626#ifdef DYNAMIC_ENV_FETCH
4608196e 627 if (!HvARRAY(hv)) entry = NULL;
7f66fda2
NC
628 else
629#endif
b2c64049 630 {
7b2c381c 631 entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)];
b2c64049 632 }
0298d7b9 633 for (; entry; entry = HeNEXT(entry)) {
fde52b5c 634 if (HeHASH(entry) != hash) /* strings can't be equal */
635 continue;
eb160463 636 if (HeKLEN(entry) != (I32)klen)
fde52b5c 637 continue;
1c846c1f 638 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
fde52b5c 639 continue;
113738bb 640 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
c3654f1a 641 continue;
b2c64049
NC
642
643 if (action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE)) {
644 if (HeKFLAGS(entry) != masked_flags) {
645 /* We match if HVhek_UTF8 bit in our flags and hash key's
646 match. But if entry was set previously with HVhek_WASUTF8
647 and key now doesn't (or vice versa) then we should change
648 the key's flag, as this is assignment. */
649 if (HvSHAREKEYS(hv)) {
650 /* Need to swap the key we have for a key with the flags we
651 need. As keys are shared we can't just write to the
652 flag, so we share the new one, unshare the old one. */
6136c704 653 HEK * const new_hek = share_hek_flags(key, klen, hash,
6e838c70 654 masked_flags);
b2c64049
NC
655 unshare_hek (HeKEY_hek(entry));
656 HeKEY_hek(entry) = new_hek;
657 }
5d2b1485
NC
658 else if (hv == PL_strtab) {
659 /* PL_strtab is usually the only hash without HvSHAREKEYS,
660 so putting this test here is cheap */
661 if (flags & HVhek_FREEKEY)
662 Safefree(key);
663 Perl_croak(aTHX_ S_strtab_error,
664 action & HV_FETCH_LVALUE ? "fetch" : "store");
665 }
b2c64049
NC
666 else
667 HeKFLAGS(entry) = masked_flags;
668 if (masked_flags & HVhek_ENABLEHVKFLAGS)
669 HvHASKFLAGS_on(hv);
670 }
671 if (HeVAL(entry) == &PL_sv_placeholder) {
672 /* yes, can store into placeholder slot */
673 if (action & HV_FETCH_LVALUE) {
674 if (SvMAGICAL(hv)) {
675 /* This preserves behaviour with the old hv_fetch
676 implementation which at this point would bail out
677 with a break; (at "if we find a placeholder, we
678 pretend we haven't found anything")
679
680 That break mean that if a placeholder were found, it
681 caused a call into hv_store, which in turn would
682 check magic, and if there is no magic end up pretty
683 much back at this point (in hv_store's code). */
684 break;
685 }
486ec47a 686 /* LVAL fetch which actually needs a store. */
561b68a9 687 val = newSV(0);
ca732855 688 HvPLACEHOLDERS(hv)--;
b2c64049
NC
689 } else {
690 /* store */
691 if (val != &PL_sv_placeholder)
ca732855 692 HvPLACEHOLDERS(hv)--;
b2c64049
NC
693 }
694 HeVAL(entry) = val;
695 } else if (action & HV_FETCH_ISSTORE) {
cefd5c7c 696 SvREFCNT_dec(HeVAL(entry));
b2c64049
NC
697 HeVAL(entry) = val;
698 }
27bcc0a7 699 } else if (HeVAL(entry) == &PL_sv_placeholder) {
b2c64049
NC
700 /* if we find a placeholder, we pretend we haven't found
701 anything */
8aacddc1 702 break;
b2c64049 703 }
113738bb
NC
704 if (flags & HVhek_FREEKEY)
705 Safefree(key);
3c84c864
NC
706 if (return_svp) {
707 return entry ? (void *) &HeVAL(entry) : NULL;
708 }
fde52b5c 709 return entry;
710 }
711#ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
0ed29950 712 if (!(action & HV_FETCH_ISSTORE)
ad64d0ec
NC
713 && SvRMAGICAL((const SV *)hv)
714 && mg_find((const SV *)hv, PERL_MAGIC_env)) {
a6c40364 715 unsigned long len;
9d4ba2ae 716 const char * const env = PerlEnv_ENVgetenv_len(key,&len);
a6c40364
GS
717 if (env) {
718 sv = newSVpvn(env,len);
719 SvTAINTED_on(sv);
d3ba3f5c 720 return hv_common(hv, keysv, key, klen, flags,
3c84c864
NC
721 HV_FETCH_ISSTORE|HV_DISABLE_UVAR_XKEY|return_svp,
722 sv, hash);
a6c40364 723 }
fde52b5c 724 }
725#endif
7f66fda2
NC
726
727 if (!entry && SvREADONLY(hv) && !(action & HV_FETCH_ISEXISTS)) {
c445ea15 728 hv_notallowed(flags, key, klen,
c8cd6465
NC
729 "Attempt to access disallowed key '%"SVf"' in"
730 " a restricted hash");
1b1f1335 731 }
b2c64049
NC
732 if (!(action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE))) {
733 /* Not doing some form of store, so return failure. */
734 if (flags & HVhek_FREEKEY)
735 Safefree(key);
3c84c864 736 return NULL;
b2c64049 737 }
113738bb 738 if (action & HV_FETCH_LVALUE) {
df5f182b 739 val = action & HV_FETCH_EMPTY_HE ? NULL : newSV(0);
b2c64049
NC
740 if (SvMAGICAL(hv)) {
741 /* At this point the old hv_fetch code would call to hv_store,
742 which in turn might do some tied magic. So we need to make that
743 magic check happen. */
744 /* gonna assign to this, so it better be there */
fda2d18a
NC
745 /* If a fetch-as-store fails on the fetch, then the action is to
746 recurse once into "hv_store". If we didn't do this, then that
747 recursive call would call the key conversion routine again.
748 However, as we replace the original key with the converted
749 key, this would result in a double conversion, which would show
e987ad1c
FC
750 up as a bug if the conversion routine is not idempotent.
751 Hence the use of HV_DISABLE_UVAR_XKEY. */
d3ba3f5c 752 return hv_common(hv, keysv, key, klen, flags,
3c84c864
NC
753 HV_FETCH_ISSTORE|HV_DISABLE_UVAR_XKEY|return_svp,
754 val, hash);
b2c64049
NC
755 /* XXX Surely that could leak if the fetch-was-store fails?
756 Just like the hv_fetch. */
113738bb
NC
757 }
758 }
759
b2c64049
NC
760 /* Welcome to hv_store... */
761
7b2c381c 762 if (!HvARRAY(hv)) {
b2c64049
NC
763 /* Not sure if we can get here. I think the only case of oentry being
764 NULL is for %ENV with dynamic env fetch. But that should disappear
765 with magic in the previous code. */
d58e6666 766 char *array;
a02a5408 767 Newxz(array,
b2c64049 768 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
d58e6666
NC
769 char);
770 HvARRAY(hv) = (HE**)array;
b2c64049
NC
771 }
772
7b2c381c 773 oentry = &(HvARRAY(hv))[hash & (I32) xhv->xhv_max];
ab4af705 774
b2c64049
NC
775 entry = new_HE();
776 /* share_hek_flags will do the free for us. This might be considered
777 bad API design. */
778 if (HvSHAREKEYS(hv))
6e838c70 779 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
5d2b1485
NC
780 else if (hv == PL_strtab) {
781 /* PL_strtab is usually the only hash without HvSHAREKEYS, so putting
782 this test here is cheap */
783 if (flags & HVhek_FREEKEY)
784 Safefree(key);
785 Perl_croak(aTHX_ S_strtab_error,
786 action & HV_FETCH_LVALUE ? "fetch" : "store");
787 }
b2c64049
NC
788 else /* gotta do the real thing */
789 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
790 HeVAL(entry) = val;
3078e109 791
9faf471a
NC
792 if (!*oentry && SvOOK(hv)) {
793 /* initial entry, and aux struct present. */
794 struct xpvhv_aux *const aux = HvAUX(hv);
795 if (aux->xhv_fill_lazy)
796 ++aux->xhv_fill_lazy;
797 }
798
6a5b4183 799#ifdef PERL_HASH_RANDOMIZE_KEYS
3078e109
YO
800 /* This logic semi-randomizes the insert order in a bucket.
801 * Either we insert into the top, or the slot below the top,
d5fc06cb
YO
802 * making it harder to see if there is a collision. We also
803 * reset the iterator randomizer if there is one.
3078e109 804 */
6a5b4183
YO
805 if ( *oentry && PL_HASH_RAND_BITS_ENABLED) {
806 PL_hash_rand_bits++;
807 PL_hash_rand_bits= ROTL_UV(PL_hash_rand_bits,1);
808 if ( PL_hash_rand_bits & 1 ) {
809 HeNEXT(entry) = HeNEXT(*oentry);
810 HeNEXT(*oentry) = entry;
811 } else {
812 HeNEXT(entry) = *oentry;
813 *oentry = entry;
814 }
815 } else
816#endif
817 {
3078e109
YO
818 HeNEXT(entry) = *oentry;
819 *oentry = entry;
3078e109 820 }
6a5b4183 821#ifdef PERL_HASH_RANDOMIZE_KEYS
3a714294 822 if (SvOOK(hv)) {
ff20b672
YO
823 /* Currently this makes various tests warn in annoying ways.
824 * So Silenced for now. - Yves | bogus end of comment =>* /
825 if (HvAUX(hv)->xhv_riter != -1) {
826 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
827 "[TESTING] Inserting into a hash during each() traversal results in undefined behavior"
828 pTHX__FORMAT
829 pTHX__VALUE);
830 }
831 */
6a5b4183
YO
832 if (PL_HASH_RAND_BITS_ENABLED) {
833 if (PL_HASH_RAND_BITS_ENABLED == 1)
834 PL_hash_rand_bits += (PTRV)entry + 1; /* we don't bother to use ptr_hash here */
835 PL_hash_rand_bits= ROTL_UV(PL_hash_rand_bits,1);
836 }
3a714294
YO
837 HvAUX(hv)->xhv_rand= (U32)PL_hash_rand_bits;
838 }
6a5b4183 839#endif
b2c64049
NC
840
841 if (val == &PL_sv_placeholder)
ca732855 842 HvPLACEHOLDERS(hv)++;
b2c64049
NC
843 if (masked_flags & HVhek_ENABLEHVKFLAGS)
844 HvHASKFLAGS_on(hv);
845
8e317198
YO
846 xhv->xhv_keys++; /* HvTOTALKEYS(hv)++ */
847 if ( DO_HSPLIT(xhv) ) {
adf6906b 848 const STRLEN oldsize = xhv->xhv_max + 1;
81a3ba35 849 const U32 items = (U32)HvPLACEHOLDERS_get(hv);
adf6906b 850
81a3ba35 851 if (items /* hash has placeholders */
1eaee784
NC
852 && !SvREADONLY(hv) /* but is not a restricted hash */) {
853 /* If this hash previously was a "restricted hash" and had
854 placeholders, but the "restricted" flag has been turned off,
855 then the placeholders no longer serve any useful purpose.
856 However, they have the downsides of taking up RAM, and adding
857 extra steps when finding used values. It's safe to clear them
858 at this point, even though Storable rebuilds restricted hashes by
0ca1b5c3 859 putting in all the placeholders (first) before turning on the
1eaee784
NC
860 readonly flag, because Storable always pre-splits the hash.
861 If we're lucky, then we may clear sufficient placeholders to
862 avoid needing to split the hash at all. */
81a3ba35 863 clear_placeholders(hv, items);
1eaee784
NC
864 if (DO_HSPLIT(xhv))
865 hsplit(hv, oldsize, oldsize * 2);
866 } else
867 hsplit(hv, oldsize, oldsize * 2);
fde52b5c 868 }
b2c64049 869
3c84c864
NC
870 if (return_svp) {
871 return entry ? (void *) &HeVAL(entry) : NULL;
872 }
873 return (void *) entry;
fde52b5c 874}
875
864dbfa3 876STATIC void
b0e6ae5b 877S_hv_magic_check(HV *hv, bool *needs_copy, bool *needs_store)
d0066dc7 878{
a3b680e6 879 const MAGIC *mg = SvMAGIC(hv);
7918f24d
NC
880
881 PERL_ARGS_ASSERT_HV_MAGIC_CHECK;
882
d0066dc7
OT
883 *needs_copy = FALSE;
884 *needs_store = TRUE;
885 while (mg) {
886 if (isUPPER(mg->mg_type)) {
887 *needs_copy = TRUE;
d60c5a05 888 if (mg->mg_type == PERL_MAGIC_tied) {
d0066dc7 889 *needs_store = FALSE;
4ab2a30b 890 return; /* We've set all there is to set. */
d0066dc7
OT
891 }
892 }
893 mg = mg->mg_moremagic;
894 }
895}
896
954c1994 897/*
a3bcc51e
TP
898=for apidoc hv_scalar
899
72d33970
FC
900Evaluates the hash in scalar context and returns the result. Handles magic
901when the hash is tied.
a3bcc51e
TP
902
903=cut
904*/
905
906SV *
907Perl_hv_scalar(pTHX_ HV *hv)
908{
a3bcc51e 909 SV *sv;
823a54a3 910
7918f24d
NC
911 PERL_ARGS_ASSERT_HV_SCALAR;
912
823a54a3 913 if (SvRMAGICAL(hv)) {
ad64d0ec 914 MAGIC * const mg = mg_find((const SV *)hv, PERL_MAGIC_tied);
823a54a3
AL
915 if (mg)
916 return magic_scalarpack(hv, mg);
917 }
a3bcc51e
TP
918
919 sv = sv_newmortal();
f4431c56 920 if (HvTOTALKEYS((const HV *)hv))
a3bcc51e
TP
921 Perl_sv_setpvf(aTHX_ sv, "%ld/%ld",
922 (long)HvFILL(hv), (long)HvMAX(hv) + 1);
923 else
924 sv_setiv(sv, 0);
925
926 return sv;
927}
928
929/*
954c1994
GS
930=for apidoc hv_delete
931
a05d6c5d
TC
932Deletes a key/value pair in the hash. The value's SV is removed from
933the hash, made mortal, and returned to the caller. The absolute
934value of C<klen> is the length of the key. If C<klen> is negative the
935key is assumed to be in UTF-8-encoded Unicode. The C<flags> value
936will normally be zero; if set to G_DISCARD then NULL will be returned.
937NULL will also be returned if the key is not found.
954c1994 938
954c1994
GS
939=for apidoc hv_delete_ent
940
3025a2e4
CS
941Deletes a key/value pair in the hash. The value SV is removed from the hash,
942made mortal, and returned to the caller. The C<flags> value will normally be
943zero; if set to G_DISCARD then NULL will be returned. NULL will also be
944returned if the key is not found. C<hash> can be a valid precomputed hash
945value, or 0 to ask for it to be computed.
954c1994
GS
946
947=cut
948*/
949
8f8d40ab 950STATIC SV *
cd6d36ac
NC
951S_hv_delete_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
952 int k_flags, I32 d_flags, U32 hash)
f1317c8d 953{
27da23d5 954 dVAR;
eb578fdb
KW
955 XPVHV* xhv;
956 HE *entry;
957 HE **oentry;
9faf471a 958 HE *const *first_entry;
9dbc5603 959 bool is_utf8 = (k_flags & HVhek_UTF8) ? TRUE : FALSE;
7a9669ca 960 int masked_flags;
1c846c1f 961
fde52b5c 962 if (SvRMAGICAL(hv)) {
0a0bb7c7
OT
963 bool needs_copy;
964 bool needs_store;
965 hv_magic_check (hv, &needs_copy, &needs_store);
966
f1317c8d 967 if (needs_copy) {
6136c704 968 SV *sv;
63c89345
NC
969 entry = (HE *) hv_common(hv, keysv, key, klen,
970 k_flags & ~HVhek_FREEKEY,
971 HV_FETCH_LVALUE|HV_DISABLE_UVAR_XKEY,
972 NULL, hash);
7a9669ca 973 sv = entry ? HeVAL(entry) : NULL;
f1317c8d
NC
974 if (sv) {
975 if (SvMAGICAL(sv)) {
976 mg_clear(sv);
977 }
978 if (!needs_store) {
979 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
980 /* No longer an element */
981 sv_unmagic(sv, PERL_MAGIC_tiedelem);
982 return sv;
983 }
a0714e2c 984 return NULL; /* element cannot be deleted */
f1317c8d 985 }
902173a3 986#ifdef ENV_IS_CASELESS
ad64d0ec 987 else if (mg_find((const SV *)hv, PERL_MAGIC_env)) {
8167a60a 988 /* XXX This code isn't UTF8 clean. */
59cd0e26 989 keysv = newSVpvn_flags(key, klen, SVs_TEMP);
8167a60a
NC
990 if (k_flags & HVhek_FREEKEY) {
991 Safefree(key);
992 }
993 key = strupr(SvPVX(keysv));
994 is_utf8 = 0;
995 k_flags = 0;
996 hash = 0;
7f66fda2 997 }
510ac311 998#endif
2fd1c6b8 999 }
2fd1c6b8 1000 }
fde52b5c 1001 }
cbec9347 1002 xhv = (XPVHV*)SvANY(hv);
7b2c381c 1003 if (!HvARRAY(hv))
a0714e2c 1004 return NULL;
fde52b5c 1005
6b230254 1006 if (is_utf8 && !(k_flags & HVhek_KEYCANONICAL)) {
c445ea15 1007 const char * const keysave = key;
b464bac0 1008 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
cd6d36ac 1009
19692e8d 1010 if (is_utf8)
cd6d36ac
NC
1011 k_flags |= HVhek_UTF8;
1012 else
1013 k_flags &= ~HVhek_UTF8;
7f66fda2
NC
1014 if (key != keysave) {
1015 if (k_flags & HVhek_FREEKEY) {
1016 /* This shouldn't happen if our caller does what we expect,
1017 but strictly the API allows it. */
1018 Safefree(keysave);
1019 }
1020 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
1021 }
ad64d0ec 1022 HvHASKFLAGS_on(MUTABLE_SV(hv));
19692e8d 1023 }
f9a63242 1024
7dc86639
YO
1025 if (!hash) {
1026 if (keysv && (SvIsCOW_shared_hash(keysv)))
1027 hash = SvSHARED_HASH(keysv);
1028 else
1029 PERL_HASH(hash, key, klen);
1030 }
fde52b5c 1031
7a9669ca
NC
1032 masked_flags = (k_flags & HVhek_MASK);
1033
9faf471a 1034 first_entry = oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)];
fde52b5c 1035 entry = *oentry;
9e720f71 1036 for (; entry; oentry = &HeNEXT(entry), entry = *oentry) {
6136c704 1037 SV *sv;
f3d2f32d 1038 U8 mro_changes = 0; /* 1 = isa; 2 = package moved */
0290c710 1039 GV *gv = NULL;
0c3bb3c2
FC
1040 HV *stash = NULL;
1041
fde52b5c 1042 if (HeHASH(entry) != hash) /* strings can't be equal */
1043 continue;
eb160463 1044 if (HeKLEN(entry) != (I32)klen)
fde52b5c 1045 continue;
1c846c1f 1046 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
fde52b5c 1047 continue;
7a9669ca 1048 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
c3654f1a 1049 continue;
8aacddc1 1050
5d2b1485
NC
1051 if (hv == PL_strtab) {
1052 if (k_flags & HVhek_FREEKEY)
1053 Safefree(key);
1054 Perl_croak(aTHX_ S_strtab_error, "delete");
1055 }
1056
8aacddc1 1057 /* if placeholder is here, it's already been deleted.... */
6136c704
AL
1058 if (HeVAL(entry) == &PL_sv_placeholder) {
1059 if (k_flags & HVhek_FREEKEY)
1060 Safefree(key);
1061 return NULL;
8aacddc1 1062 }
0ffdaf1a 1063 if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
d4c19fe8 1064 hv_notallowed(k_flags, key, klen,
c8cd6465
NC
1065 "Attempt to delete readonly key '%"SVf"' from"
1066 " a restricted hash");
8aacddc1 1067 }
b84d0860
NC
1068 if (k_flags & HVhek_FREEKEY)
1069 Safefree(key);
8aacddc1 1070
35759254 1071 /* If this is a stash and the key ends with ::, then someone is
0c3bb3c2 1072 * deleting a package.
0c3bb3c2 1073 */
78b79c77 1074 if (HeVAL(entry) && HvENAME_get(hv)) {
0290c710 1075 gv = (GV *)HeVAL(entry);
35759254 1076 if (keysv) key = SvPV(keysv, klen);
1f656fcf
FC
1077 if ((
1078 (klen > 1 && key[klen-2] == ':' && key[klen-1] == ':')
1079 ||
1080 (klen == 1 && key[0] == ':')
1081 )
e0a52395 1082 && (klen != 6 || hv!=PL_defstash || memNE(key,"main::",6))
0290c710 1083 && SvTYPE(gv) == SVt_PVGV && (stash = GvHV((GV *)gv))
0c3bb3c2 1084 && HvENAME_get(stash)) {
0290c710
FC
1085 /* A previous version of this code checked that the
1086 * GV was still in the symbol table by fetching the
1087 * GV with its name. That is not necessary (and
1088 * sometimes incorrect), as HvENAME cannot be set
1089 * on hv if it is not in the symtab. */
f3d2f32d 1090 mro_changes = 2;
0c3bb3c2
FC
1091 /* Hang on to it for a bit. */
1092 SvREFCNT_inc_simple_void_NN(
0290c710 1093 sv_2mortal((SV *)gv)
35759254
FC
1094 );
1095 }
f3d2f32d
FC
1096 else if (klen == 3 && strnEQ(key, "ISA", 3))
1097 mro_changes = 1;
35759254
FC
1098 }
1099
8571a3cc
FC
1100 sv = d_flags & G_DISCARD ? HeVAL(entry) : sv_2mortal(HeVAL(entry));
1101 HeVAL(entry) = &PL_sv_placeholder;
5743f2a3
FC
1102 if (sv) {
1103 /* deletion of method from stash */
1104 if (isGV(sv) && isGV_with_GP(sv) && GvCVu(sv)
1105 && HvENAME_get(hv))
1106 mro_method_changed_in(hv);
5743f2a3 1107 }
8aacddc1
NIS
1108
1109 /*
1110 * If a restricted hash, rather than really deleting the entry, put
1111 * a placeholder there. This marks the key as being "approved", so
1112 * we can still access via not-really-existing key without raising
1113 * an error.
1114 */
f50383f5 1115 if (SvREADONLY(hv))
8aacddc1
NIS
1116 /* We'll be saving this slot, so the number of allocated keys
1117 * doesn't go down, but the number placeholders goes up */
ca732855 1118 HvPLACEHOLDERS(hv)++;
f50383f5 1119 else {
a26e96df 1120 *oentry = HeNEXT(entry);
9faf471a
NC
1121 if(!*first_entry && SvOOK(hv)) {
1122 /* removed last entry, and aux struct present. */
1123 struct xpvhv_aux *const aux = HvAUX(hv);
1124 if (aux->xhv_fill_lazy)
1125 --aux->xhv_fill_lazy;
1126 }
b79f7545 1127 if (SvOOK(hv) && entry == HvAUX(hv)->xhv_eiter /* HvEITER(hv) */)
8aacddc1 1128 HvLAZYDEL_on(hv);
ae199939
TH
1129 else {
1130 if (SvOOK(hv) && HvLAZYDEL(hv) &&
1131 entry == HeNEXT(HvAUX(hv)->xhv_eiter))
1132 HeNEXT(HvAUX(hv)->xhv_eiter) = HeNEXT(entry);
8aacddc1 1133 hv_free_ent(hv, entry);
ae199939 1134 }
4c7185a0 1135 xhv->xhv_keys--; /* HvTOTALKEYS(hv)-- */
574c8022 1136 if (xhv->xhv_keys == 0)
19692e8d 1137 HvHASKFLAGS_off(hv);
8aacddc1 1138 }
0c3bb3c2 1139
3b2cd809
FC
1140 if (d_flags & G_DISCARD) {
1141 SvREFCNT_dec(sv);
1142 sv = NULL;
1143 }
1144
f3d2f32d
FC
1145 if (mro_changes == 1) mro_isa_changed_in(hv);
1146 else if (mro_changes == 2)
afdbe55d 1147 mro_package_moved(NULL, stash, gv, 1);
0c3bb3c2 1148
79072805
LW
1149 return sv;
1150 }
8aacddc1 1151 if (SvREADONLY(hv)) {
d4c19fe8 1152 hv_notallowed(k_flags, key, klen,
c8cd6465
NC
1153 "Attempt to delete disallowed key '%"SVf"' from"
1154 " a restricted hash");
8aacddc1
NIS
1155 }
1156
19692e8d 1157 if (k_flags & HVhek_FREEKEY)
f9a63242 1158 Safefree(key);
a0714e2c 1159 return NULL;
79072805
LW
1160}
1161
32dfa2a7 1162
76e3520e 1163STATIC void
adf6906b 1164S_hsplit(pTHX_ HV *hv, STRLEN const oldsize, STRLEN newsize)
79072805 1165{
7663aa67 1166 STRLEN i = 0;
7b2c381c 1167 char *a = (char*) HvARRAY(hv);
eb578fdb 1168 HE **aep;
79072805 1169
32dfa2a7
YO
1170 bool do_aux= (
1171 /* already have an HvAUX(hv) so we have to move it */
1172 SvOOK(hv) ||
1173 /* no HvAUX() but array we are going to allocate is large enough
1174 * there is no point in saving the space for the iterator, and
1175 * speeds up later traversals. */
1176 ( ( hv != PL_strtab ) && ( newsize >= PERL_HV_ALLOC_AUX_SIZE ) )
1177 );
7918f24d 1178
32dfa2a7 1179 PERL_ARGS_ASSERT_HSPLIT;
18026298 1180
3280af22 1181 PL_nomemok = TRUE;
b79f7545 1182 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize)
32dfa2a7
YO
1183 + (do_aux ? sizeof(struct xpvhv_aux) : 0), char);
1184 PL_nomemok = FALSE;
422a93e5 1185 if (!a) {
422a93e5
GA
1186 return;
1187 }
32dfa2a7 1188
6a5b4183 1189#ifdef PERL_HASH_RANDOMIZE_KEYS
3078e109
YO
1190 /* the idea of this is that we create a "random" value by hashing the address of
1191 * the array, we then use the low bit to decide if we insert at the top, or insert
1192 * second from top. After each such insert we rotate the hashed value. So we can
1193 * use the same hashed value over and over, and in normal build environments use
1194 * very few ops to do so. ROTL32() should produce a single machine operation. */
6a5b4183
YO
1195 if (PL_HASH_RAND_BITS_ENABLED) {
1196 if (PL_HASH_RAND_BITS_ENABLED == 1)
1197 PL_hash_rand_bits += ptr_hash((PTRV)a);
1198 PL_hash_rand_bits = ROTL_UV(PL_hash_rand_bits,1);
1199 }
1200#endif
32dfa2a7
YO
1201 HvARRAY(hv) = (HE**) a;
1202 HvMAX(hv) = newsize - 1;
1203 /* before we zero the newly added memory, we
1204 * need to deal with the aux struct that may be there
1205 * or have been allocated by us*/
1206 if (do_aux) {
3078e109
YO
1207 struct xpvhv_aux *const dest
1208 = (struct xpvhv_aux*) &a[newsize * sizeof(HE*)];
32dfa2a7
YO
1209 if (SvOOK(hv)) {
1210 /* alread have an aux, copy the old one in place. */
1211 Move(&a[oldsize * sizeof(HE*)], dest, 1, struct xpvhv_aux);
1212 /* we reset the iterator's xhv_rand as well, so they get a totally new ordering */
6a5b4183 1213#ifdef PERL_HASH_RANDOMIZE_KEYS
32dfa2a7 1214 dest->xhv_rand = (U32)PL_hash_rand_bits;
6a5b4183 1215#endif
32dfa2a7
YO
1216 /* For now, just reset the lazy fill counter.
1217 It would be possible to update the counter in the code below
1218 instead. */
1219 dest->xhv_fill_lazy = 0;
1220 } else {
1221 /* no existing aux structure, but we allocated space for one
1222 * so intialize it properly. This unrolls hv_auxinit() a bit,
1223 * since we have to do the realloc anyway. */
1224 /* first we set the iterator's xhv_rand so it can be copied into lastrand below */
1225#ifdef PERL_HASH_RANDOMIZE_KEYS
1226 dest->xhv_rand = (U32)PL_hash_rand_bits;
1227#endif
1228 /* this is the "non realloc" part of the hv_auxinit() */
1229 (void)hv_auxinit_internal(dest);
1230 /* Turn on the OOK flag */
1231 SvOOK_on(hv);
1232 }
b79f7545 1233 }
32dfa2a7 1234 /* now we can safely clear the second half */
72311751 1235 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
79072805 1236
68303b5c
NC
1237 if (!HvTOTALKEYS(hv)) /* skip rest if no entries */
1238 return;
1239
32dfa2a7 1240 newsize--;
68303b5c 1241 aep = (HE**)a;
7663aa67 1242 do {
c23dc12b
NC
1243 HE **oentry = aep + i;
1244 HE *entry = aep[i];
4b5190b5 1245
a50a3493 1246 if (!entry) /* non-existent */
79072805 1247 continue;
4c9d89c5 1248 do {
c23dc12b
NC
1249 U32 j = (HeHASH(entry) & newsize);
1250 if (j != (U32)i) {
fde52b5c 1251 *oentry = HeNEXT(entry);
6a5b4183
YO
1252#ifdef PERL_HASH_RANDOMIZE_KEYS
1253 /* if the target cell is empty or PL_HASH_RAND_BITS_ENABLED is false
1254 * insert to top, otherwise rotate the bucket rand 1 bit,
1255 * and use the new low bit to decide if we insert at top,
1256 * or next from top. IOW, we only rotate on a collision.*/
1257 if (aep[j] && PL_HASH_RAND_BITS_ENABLED) {
3f49e765 1258 PL_hash_rand_bits+= ROTL32(HeHASH(entry), 17);
6a5b4183
YO
1259 PL_hash_rand_bits= ROTL_UV(PL_hash_rand_bits,1);
1260 if (PL_hash_rand_bits & 1) {
1261 HeNEXT(entry)= HeNEXT(aep[j]);
1262 HeNEXT(aep[j])= entry;
1263 } else {
1264 /* Note, this is structured in such a way as the optimizer
1265 * should eliminate the duplicated code here and below without
1266 * us needing to explicitly use a goto. */
1267 HeNEXT(entry) = aep[j];
1268 aep[j] = entry;
1269 }
1270 } else
1271#endif
1272 {
1273 /* see comment above about duplicated code */
3078e109
YO
1274 HeNEXT(entry) = aep[j];
1275 aep[j] = entry;
3078e109 1276 }
79072805 1277 }
4b5190b5 1278 else {
fde52b5c 1279 oentry = &HeNEXT(entry);
4b5190b5 1280 }
4c9d89c5
NC
1281 entry = *oentry;
1282 } while (entry);
7663aa67 1283 } while (i++ < oldsize);
79072805
LW
1284}
1285
72940dca 1286void
864dbfa3 1287Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
72940dca 1288{
eb578fdb 1289 XPVHV* xhv = (XPVHV*)SvANY(hv);
a3b680e6 1290 const I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
eb578fdb 1291 I32 newsize;
eb578fdb 1292 char *a;
72940dca 1293
7918f24d
NC
1294 PERL_ARGS_ASSERT_HV_KSPLIT;
1295
72940dca 1296 newsize = (I32) newmax; /* possible truncation here */
1297 if (newsize != newmax || newmax <= oldsize)
1298 return;
1299 while ((newsize & (1 + ~newsize)) != newsize) {
1300 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1301 }
1302 if (newsize < newmax)
1303 newsize *= 2;
1304 if (newsize < newmax)
1305 return; /* overflow detection */
1306
7b2c381c 1307 a = (char *) HvARRAY(hv);
e8c10cf3
NC
1308 if (a) {
1309 hsplit(hv, oldsize, newsize);
1310 } else {
0df05616
NC
1311 Newxz(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1312 xhv->xhv_max = --newsize;
1313 HvARRAY(hv) = (HE **) a;
72940dca 1314 }
1315}
1316
f6bb1c88
YO
1317/* IMO this should also handle cases where hv_max is smaller than hv_keys
1318 * as tied hashes could play silly buggers and mess us around. We will
1319 * do the right thing during hv_store() afterwards, but still - Yves */
1320#define HV_SET_MAX_ADJUSTED_FOR_KEYS(hv,hv_max,hv_keys) STMT_START {\
1321 /* Can we use fewer buckets? (hv_max is always 2^n-1) */ \
1322 if (hv_max < PERL_HASH_DEFAULT_HvMAX) { \
1323 hv_max = PERL_HASH_DEFAULT_HvMAX; \
1324 } else { \
1325 while (hv_max > PERL_HASH_DEFAULT_HvMAX && hv_max + 1 >= hv_keys * 2) \
1326 hv_max = hv_max / 2; \
1327 } \
1328 HvMAX(hv) = hv_max; \
1329} STMT_END
1330
1331
b3ac6de7 1332HV *
864dbfa3 1333Perl_newHVhv(pTHX_ HV *ohv)
b3ac6de7 1334{
749123ff 1335 dVAR;
9d4ba2ae 1336 HV * const hv = newHV();
f4431c56 1337 STRLEN hv_max;
4beac62f 1338
3f4d1d78 1339 if (!ohv || (!HvTOTALKEYS(ohv) && !SvMAGICAL((const SV *)ohv)))
4beac62f 1340 return hv;
4beac62f 1341 hv_max = HvMAX(ohv);
b3ac6de7 1342
ad64d0ec 1343 if (!SvMAGICAL((const SV *)ohv)) {
b56ba0bf 1344 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
eb160463 1345 STRLEN i;
a3b680e6 1346 const bool shared = !!HvSHAREKEYS(ohv);
aec46f14 1347 HE **ents, ** const oents = (HE **)HvARRAY(ohv);
ff875642 1348 char *a;
a02a5408 1349 Newx(a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
ff875642 1350 ents = (HE**)a;
b56ba0bf
AMS
1351
1352 /* In each bucket... */
1353 for (i = 0; i <= hv_max; i++) {
6136c704 1354 HE *prev = NULL;
aec46f14 1355 HE *oent = oents[i];
b56ba0bf
AMS
1356
1357 if (!oent) {
1358 ents[i] = NULL;
1359 continue;
1360 }
1361
1362 /* Copy the linked list of entries. */
aec46f14 1363 for (; oent; oent = HeNEXT(oent)) {
a3b680e6
AL
1364 const U32 hash = HeHASH(oent);
1365 const char * const key = HeKEY(oent);
1366 const STRLEN len = HeKLEN(oent);
1367 const int flags = HeKFLAGS(oent);
6136c704 1368 HE * const ent = new_HE();
c3acb9e0 1369 SV *const val = HeVAL(oent);
b56ba0bf 1370
c3acb9e0 1371 HeVAL(ent) = SvIMMORTAL(val) ? val : newSVsv(val);
19692e8d 1372 HeKEY_hek(ent)
6e838c70 1373 = shared ? share_hek_flags(key, len, hash, flags)
19692e8d 1374 : save_hek_flags(key, len, hash, flags);
b56ba0bf
AMS
1375 if (prev)
1376 HeNEXT(prev) = ent;
1377 else
1378 ents[i] = ent;
1379 prev = ent;
1380 HeNEXT(ent) = NULL;
1381 }
1382 }
1383
1384 HvMAX(hv) = hv_max;
8aacddc1 1385 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
b56ba0bf 1386 HvARRAY(hv) = ents;
aec46f14 1387 } /* not magical */
b56ba0bf
AMS
1388 else {
1389 /* Iterate over ohv, copying keys and values one at a time. */
b3ac6de7 1390 HE *entry;
bfcb3514
NC
1391 const I32 riter = HvRITER_get(ohv);
1392 HE * const eiter = HvEITER_get(ohv);
f6bb1c88 1393 STRLEN hv_keys = HvTOTALKEYS(ohv);
b56ba0bf 1394
f6bb1c88 1395 HV_SET_MAX_ADJUSTED_FOR_KEYS(hv,hv_max,hv_keys);
b56ba0bf 1396
4a76a316 1397 hv_iterinit(ohv);
e16e2ff8 1398 while ((entry = hv_iternext_flags(ohv, 0))) {
3f4d1d78
FC
1399 SV *val = hv_iterval(ohv,entry);
1400 SV * const keysv = HeSVKEY(entry);
1401 val = SvIMMORTAL(val) ? val : newSVsv(val);
1402 if (keysv)
1403 (void)hv_store_ent(hv, keysv, val, 0);
1404 else
1405 (void)hv_store_flags(hv, HeKEY(entry), HeKLEN(entry), val,
c3acb9e0 1406 HeHASH(entry), HeKFLAGS(entry));
b3ac6de7 1407 }
bfcb3514
NC
1408 HvRITER_set(ohv, riter);
1409 HvEITER_set(ohv, eiter);
b3ac6de7 1410 }
1c846c1f 1411
b3ac6de7
IZ
1412 return hv;
1413}
1414
defdfed5
Z
1415/*
1416=for apidoc Am|HV *|hv_copy_hints_hv|HV *ohv
1417
1418A specialised version of L</newHVhv> for copying C<%^H>. I<ohv> must be
1419a pointer to a hash (which may have C<%^H> magic, but should be generally
1420non-magical), or C<NULL> (interpreted as an empty hash). The content
1421of I<ohv> is copied to a new hash, which has the C<%^H>-specific magic
1422added to it. A pointer to the new hash is returned.
1423
1424=cut
1425*/
1426
5b9c0671
NC
1427HV *
1428Perl_hv_copy_hints_hv(pTHX_ HV *const ohv)
1429{
1430 HV * const hv = newHV();
5b9c0671 1431
cb1f05e8 1432 if (ohv) {
5b9c0671 1433 STRLEN hv_max = HvMAX(ohv);
f6bb1c88 1434 STRLEN hv_keys = HvTOTALKEYS(ohv);
5b9c0671
NC
1435 HE *entry;
1436 const I32 riter = HvRITER_get(ohv);
1437 HE * const eiter = HvEITER_get(ohv);
1438
0db511c0
FC
1439 ENTER;
1440 SAVEFREESV(hv);
1441
f6bb1c88 1442 HV_SET_MAX_ADJUSTED_FOR_KEYS(hv,hv_max,hv_keys);
5b9c0671
NC
1443
1444 hv_iterinit(ohv);
1445 while ((entry = hv_iternext_flags(ohv, 0))) {
cb1f05e8 1446 SV *const sv = newSVsv(hv_iterval(ohv,entry));
7ef9d42c
FC
1447 SV *heksv = HeSVKEY(entry);
1448 if (!heksv && sv) heksv = newSVhek(HeKEY_hek(entry));
95cf2368 1449 if (sv) sv_magic(sv, NULL, PERL_MAGIC_hintselem,
e3b1b6b1 1450 (char *)heksv, HEf_SVKEY);
7ef9d42c
FC
1451 if (heksv == HeSVKEY(entry))
1452 (void)hv_store_ent(hv, heksv, sv, 0);
1453 else {
1454 (void)hv_common(hv, heksv, HeKEY(entry), HeKLEN(entry),
1455 HeKFLAGS(entry), HV_FETCH_ISSTORE|HV_FETCH_JUST_SV, sv, HeHASH(entry));
a03199ea 1456 SvREFCNT_dec_NN(heksv);
7ef9d42c 1457 }
5b9c0671
NC
1458 }
1459 HvRITER_set(ohv, riter);
1460 HvEITER_set(ohv, eiter);
0db511c0
FC
1461
1462 SvREFCNT_inc_simple_void_NN(hv);
1463 LEAVE;
5b9c0671
NC
1464 }
1465 hv_magic(hv, NULL, PERL_MAGIC_hints);
1466 return hv;
1467}
f6bb1c88 1468#undef HV_SET_MAX_ADJUSTED_FOR_KEYS
5b9c0671 1469
e0171a1a
DM
1470/* like hv_free_ent, but returns the SV rather than freeing it */
1471STATIC SV*
5aaab254 1472S_hv_free_ent_ret(pTHX_ HV *hv, HE *entry)
79072805 1473{
16bdeea2
GS
1474 SV *val;
1475
e0171a1a 1476 PERL_ARGS_ASSERT_HV_FREE_ENT_RET;
7918f24d 1477
16bdeea2 1478 val = HeVAL(entry);
68dc0745 1479 if (HeKLEN(entry) == HEf_SVKEY) {
1480 SvREFCNT_dec(HeKEY_sv(entry));
8aacddc1 1481 Safefree(HeKEY_hek(entry));
44a8e56a 1482 }
1483 else if (HvSHAREKEYS(hv))
68dc0745 1484 unshare_hek(HeKEY_hek(entry));
fde52b5c 1485 else
68dc0745 1486 Safefree(HeKEY_hek(entry));
d33b2eba 1487 del_HE(entry);
e0171a1a
DM
1488 return val;
1489}
1490
1491
1492void
5aaab254 1493Perl_hv_free_ent(pTHX_ HV *hv, HE *entry)
e0171a1a 1494{
e0171a1a
DM
1495 SV *val;
1496
1497 PERL_ARGS_ASSERT_HV_FREE_ENT;
1498
1499 if (!entry)
1500 return;
1501 val = hv_free_ent_ret(hv, entry);
272e8453 1502 SvREFCNT_dec(val);
79072805
LW
1503}
1504
f1c32fec 1505
79072805 1506void
5aaab254 1507Perl_hv_delayfree_ent(pTHX_ HV *hv, HE *entry)
79072805 1508{
7918f24d
NC
1509 PERL_ARGS_ASSERT_HV_DELAYFREE_ENT;
1510
68dc0745 1511 if (!entry)
79072805 1512 return;
bc4947fc
NC
1513 /* SvREFCNT_inc to counter the SvREFCNT_dec in hv_free_ent */
1514 sv_2mortal(SvREFCNT_inc(HeVAL(entry))); /* free between statements */
68dc0745 1515 if (HeKLEN(entry) == HEf_SVKEY) {
bc4947fc 1516 sv_2mortal(SvREFCNT_inc(HeKEY_sv(entry)));
44a8e56a 1517 }
bc4947fc 1518 hv_free_ent(hv, entry);
79072805
LW
1519}
1520
954c1994
GS
1521/*
1522=for apidoc hv_clear
1523
c2217cd3 1524Frees the all the elements of a hash, leaving it empty.
8b9a1153
FC
1525The XS equivalent of C<%hash = ()>. See also L</hv_undef>.
1526
1527If any destructors are triggered as a result, the hv itself may
1528be freed.
954c1994
GS
1529
1530=cut
1531*/
1532
79072805 1533void
864dbfa3 1534Perl_hv_clear(pTHX_ HV *hv)
79072805 1535{
27da23d5 1536 dVAR;
eb578fdb 1537 XPVHV* xhv;
79072805
LW
1538 if (!hv)
1539 return;
49293501 1540
ecae49c0
NC
1541 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1542
34c3c4e3
DM
1543 xhv = (XPVHV*)SvANY(hv);
1544
8505eec0
FC
1545 ENTER;
1546 SAVEFREESV(SvREFCNT_inc_simple_NN(hv));
7b2c381c 1547 if (SvREADONLY(hv) && HvARRAY(hv) != NULL) {
34c3c4e3 1548 /* restricted hash: convert all keys to placeholders */
b464bac0
AL
1549 STRLEN i;
1550 for (i = 0; i <= xhv->xhv_max; i++) {
7b2c381c 1551 HE *entry = (HvARRAY(hv))[i];
3a676441
JH
1552 for (; entry; entry = HeNEXT(entry)) {
1553 /* not already placeholder */
7996736c 1554 if (HeVAL(entry) != &PL_sv_placeholder) {
a03199ea 1555 if (HeVAL(entry)) {
0ffdaf1a 1556 if (SvREADONLY(HeVAL(entry))) {
a03199ea
DD
1557 SV* const keysv = hv_iterkeysv(entry);
1558 Perl_croak_nocontext(
1559 "Attempt to delete readonly key '%"SVf"' from a restricted hash",
1560 (void*)keysv);
1561 }
1562 SvREFCNT_dec_NN(HeVAL(entry));
3a676441 1563 }
7996736c 1564 HeVAL(entry) = &PL_sv_placeholder;
ca732855 1565 HvPLACEHOLDERS(hv)++;
3a676441 1566 }
34c3c4e3
DM
1567 }
1568 }
49293501 1569 }
afbbf215
DM
1570 else {
1571 hfreeentries(hv);
1572 HvPLACEHOLDERS_set(hv, 0);
49293501 1573
afbbf215
DM
1574 if (SvRMAGICAL(hv))
1575 mg_clear(MUTABLE_SV(hv));
574c8022 1576
afbbf215 1577 HvHASKFLAGS_off(hv);
afbbf215 1578 }
b79f7545 1579 if (SvOOK(hv)) {
00169e2c 1580 if(HvENAME_get(hv))
dd69841b 1581 mro_isa_changed_in(hv);
bfcb3514
NC
1582 HvEITER_set(hv, NULL);
1583 }
8505eec0 1584 LEAVE;
79072805
LW
1585}
1586
3540d4ce
AB
1587/*
1588=for apidoc hv_clear_placeholders
1589
1590Clears any placeholders from a hash. If a restricted hash has any of its keys
1591marked as readonly and the key is subsequently deleted, the key is not actually
1592deleted but is marked by assigning it a value of &PL_sv_placeholder. This tags
1593it so it will be ignored by future operations such as iterating over the hash,
4cdaeff7 1594but will still allow the hash to have a value reassigned to the key at some
3540d4ce
AB
1595future point. This function clears any such placeholder keys from the hash.
1596See Hash::Util::lock_keys() for an example of its use.
1597
1598=cut
1599*/
1600
1601void
1602Perl_hv_clear_placeholders(pTHX_ HV *hv)
1603{
b3ca2e83
NC
1604 const U32 items = (U32)HvPLACEHOLDERS_get(hv);
1605
7918f24d
NC
1606 PERL_ARGS_ASSERT_HV_CLEAR_PLACEHOLDERS;
1607
b3ca2e83
NC
1608 if (items)
1609 clear_placeholders(hv, items);
1610}
1611
1612static void
1613S_clear_placeholders(pTHX_ HV *hv, U32 items)
1614{
1615 dVAR;
b464bac0 1616 I32 i;
d3677389 1617
7918f24d
NC
1618 PERL_ARGS_ASSERT_CLEAR_PLACEHOLDERS;
1619
d3677389
NC
1620 if (items == 0)
1621 return;
1622
b464bac0 1623 i = HvMAX(hv);
d3677389
NC
1624 do {
1625 /* Loop down the linked list heads */
d3677389 1626 HE **oentry = &(HvARRAY(hv))[i];
cf6db12b 1627 HE *entry;
d3677389 1628
cf6db12b 1629 while ((entry = *oentry)) {
d3677389
NC
1630 if (HeVAL(entry) == &PL_sv_placeholder) {
1631 *oentry = HeNEXT(entry);
2e58978b 1632 if (entry == HvEITER_get(hv))
d3677389 1633 HvLAZYDEL_on(hv);
ae199939
TH
1634 else {
1635 if (SvOOK(hv) && HvLAZYDEL(hv) &&
1636 entry == HeNEXT(HvAUX(hv)->xhv_eiter))
1637 HeNEXT(HvAUX(hv)->xhv_eiter) = HeNEXT(entry);
d3677389 1638 hv_free_ent(hv, entry);
ae199939 1639 }
d3677389
NC
1640
1641 if (--items == 0) {
1642 /* Finished. */
5d27ee4a
DD
1643 I32 placeholders = HvPLACEHOLDERS_get(hv);
1644 HvTOTALKEYS(hv) -= (IV)placeholders;
1645 /* HvUSEDKEYS expanded */
1646 if ((HvTOTALKEYS(hv) - placeholders) == 0)
d3677389 1647 HvHASKFLAGS_off(hv);
5d88ecd7 1648 HvPLACEHOLDERS_set(hv, 0);
d3677389
NC
1649 return;
1650 }
213ce8b3
NC
1651 } else {
1652 oentry = &HeNEXT(entry);
d3677389
NC
1653 }
1654 }
1655 } while (--i >= 0);
1656 /* You can't get here, hence assertion should always fail. */
1657 assert (items == 0);
31cdb61f 1658 NOT_REACHED;
3540d4ce
AB
1659}
1660
76e3520e 1661STATIC void
cea2e8a9 1662S_hfreeentries(pTHX_ HV *hv)
79072805 1663{
e0171a1a 1664 STRLEN index = 0;
7d6175ef 1665 XPVHV * const xhv = (XPVHV*)SvANY(hv);
6d1c68e6 1666 SV *sv;
3abe233e 1667
7918f24d
NC
1668 PERL_ARGS_ASSERT_HFREEENTRIES;
1669
6d1c68e6
FC
1670 while ((sv = Perl_hfree_next_entry(aTHX_ hv, &index))||xhv->xhv_keys) {
1671 SvREFCNT_dec(sv);
e0171a1a
DM
1672 }
1673}
23976bdd 1674
b79f7545 1675
e0171a1a
DM
1676/* hfree_next_entry()
1677 * For use only by S_hfreeentries() and sv_clear().
1678 * Delete the next available HE from hv and return the associated SV.
7d6175ef
FC
1679 * Returns null on empty hash. Nevertheless null is not a reliable
1680 * indicator that the hash is empty, as the deleted entry may have a
1681 * null value.
e0171a1a
DM
1682 * indexp is a pointer to the current index into HvARRAY. The index should
1683 * initially be set to 0. hfree_next_entry() may update it. */
1684
1685SV*
1686Perl_hfree_next_entry(pTHX_ HV *hv, STRLEN *indexp)
1687{
1688 struct xpvhv_aux *iter;
1689 HE *entry;
1690 HE ** array;
1691#ifdef DEBUGGING
1692 STRLEN orig_index = *indexp;
1693#endif
1694
1695 PERL_ARGS_ASSERT_HFREE_NEXT_ENTRY;
1696
9faf471a
NC
1697 if (SvOOK(hv) && ((iter = HvAUX(hv)))) {
1698 if ((entry = iter->xhv_eiter)) {
1699 /* the iterator may get resurrected after each
1700 * destructor call, so check each time */
1701 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1702 HvLAZYDEL_off(hv);
1703 hv_free_ent(hv, entry);
1704 /* warning: at this point HvARRAY may have been
1705 * re-allocated, HvMAX changed etc */
1706 }
339441ef 1707 iter = HvAUX(hv); /* may have been realloced */
9faf471a
NC
1708 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */
1709 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */
6a5b4183 1710#ifdef PERL_HASH_RANDOMIZE_KEYS
9faf471a 1711 iter->xhv_last_rand = iter->xhv_rand;
6a5b4183 1712#endif
9faf471a
NC
1713 }
1714 /* Reset any cached HvFILL() to "unknown". It's unlikely that anyone
1715 will actually call HvFILL() on a hash under destruction, so it
1716 seems pointless attempting to track the number of keys remaining.
1717 But if they do, we want to reset it again. */
1718 if (iter->xhv_fill_lazy)
1719 iter->xhv_fill_lazy = 0;
e0171a1a
DM
1720 }
1721
00a1a643
DM
1722 if (!((XPVHV*)SvANY(hv))->xhv_keys)
1723 return NULL;
1724
e0171a1a
DM
1725 array = HvARRAY(hv);
1726 assert(array);
1727 while ( ! ((entry = array[*indexp])) ) {
1728 if ((*indexp)++ >= HvMAX(hv))
1729 *indexp = 0;
1730 assert(*indexp != orig_index);
1731 }
1732 array[*indexp] = HeNEXT(entry);
1733 ((XPVHV*) SvANY(hv))->xhv_keys--;
1734
1735 if ( PL_phase != PERL_PHASE_DESTRUCT && HvENAME(hv)
1736 && HeVAL(entry) && isGV(HeVAL(entry))
1737 && GvHV(HeVAL(entry)) && HvENAME(GvHV(HeVAL(entry)))
1738 ) {
1739 STRLEN klen;
1740 const char * const key = HePV(entry,klen);
1741 if ((klen > 1 && key[klen-1]==':' && key[klen-2]==':')
1742 || (klen == 1 && key[0] == ':')) {
1743 mro_package_moved(
1744 NULL, GvHV(HeVAL(entry)),
1745 (GV *)HeVAL(entry), 0
1746 );
1747 }
1748 }
1749 return hv_free_ent_ret(hv, entry);
79072805
LW
1750}
1751
e0171a1a 1752
954c1994
GS
1753/*
1754=for apidoc hv_undef
1755
8b9a1153 1756Undefines the hash. The XS equivalent of C<undef(%hash)>.
c2217cd3
DM
1757
1758As well as freeing all the elements of the hash (like hv_clear()), this
1759also frees any auxiliary data and storage associated with the hash.
8b9a1153
FC
1760
1761If any destructors are triggered as a result, the hv itself may
1762be freed.
1763
c2217cd3 1764See also L</hv_clear>.
954c1994
GS
1765
1766=cut
1767*/
1768
79072805 1769void
8581adba 1770Perl_hv_undef_flags(pTHX_ HV *hv, U32 flags)
79072805 1771{
eb578fdb 1772 XPVHV* xhv;
8a50cd03 1773 bool save;
86f55936 1774
79072805
LW
1775 if (!hv)
1776 return;
8a50cd03 1777 save = !!SvREFCNT(hv);
ecae49c0 1778 DEBUG_A(Perl_hv_assert(aTHX_ hv));
cbec9347 1779 xhv = (XPVHV*)SvANY(hv);
dd69841b 1780
745edda6
FC
1781 /* The name must be deleted before the call to hfreeeeentries so that
1782 CVs are anonymised properly. But the effective name must be pre-
1783 served until after that call (and only deleted afterwards if the
1784 call originated from sv_clear). For stashes with one name that is
1785 both the canonical name and the effective name, hv_name_set has to
1786 allocate an array for storing the effective name. We can skip that
1787 during global destruction, as it does not matter where the CVs point
1788 if they will be freed anyway. */
104d7b69
DM
1789 /* note that the code following prior to hfreeentries is duplicated
1790 * in sv_clear(), and changes here should be done there too */
0ca9877d 1791 if (PL_phase != PERL_PHASE_DESTRUCT && HvNAME(hv)) {
103f5a36
NC
1792 if (PL_stashcache) {
1793 DEBUG_o(Perl_deb(aTHX_ "hv_undef_flags clearing PL_stashcache for '%"
10bafe90 1794 HEKf"'\n", HEKfARG(HvNAME_HEK(hv))));
0ca9877d 1795 (void)hv_deletehek(PL_stashcache, HvNAME_HEK(hv), G_DISCARD);
103f5a36 1796 }
bd61b366 1797 hv_name_set(hv, NULL, 0, 0);
85e6fe83 1798 }
8505eec0
FC
1799 if (save) {
1800 ENTER;
1801 SAVEFREESV(SvREFCNT_inc_simple_NN(hv));
1802 }
2d0d1ecc 1803 hfreeentries(hv);
47f1cf77 1804 if (SvOOK(hv)) {
47f1cf77 1805 struct mro_meta *meta;
0ca9877d 1806 const char *name;
745edda6 1807
0ca9877d 1808 if (HvENAME_get(hv)) {
5f243b5f 1809 if (PL_phase != PERL_PHASE_DESTRUCT)
745edda6 1810 mro_isa_changed_in(hv);
103f5a36
NC
1811 if (PL_stashcache) {
1812 DEBUG_o(Perl_deb(aTHX_ "hv_undef_flags clearing PL_stashcache for effective name '%"
10bafe90 1813 HEKf"'\n", HEKfARG(HvENAME_HEK(hv))));
0ca9877d 1814 (void)hv_deletehek(PL_stashcache, HvENAME_HEK(hv), G_DISCARD);
103f5a36 1815 }
745edda6
FC
1816 }
1817
1818 /* If this call originated from sv_clear, then we must check for
1819 * effective names that need freeing, as well as the usual name. */
1820 name = HvNAME(hv);
339441ef 1821 if (flags & HV_NAME_SETALL ? !!HvAUX(hv)->xhv_name_u.xhvnameu_name : !!name) {
103f5a36
NC
1822 if (name && PL_stashcache) {
1823 DEBUG_o(Perl_deb(aTHX_ "hv_undef_flags clearing PL_stashcache for name '%"
10bafe90 1824 HEKf"'\n", HEKfARG(HvNAME_HEK(hv))));
0ca9877d 1825 (void)hv_deletehek(PL_stashcache, HvNAME_HEK(hv), G_DISCARD);
103f5a36 1826 }
745edda6 1827 hv_name_set(hv, NULL, 0, flags);
47f1cf77 1828 }
339441ef 1829 if((meta = HvAUX(hv)->xhv_mro_meta)) {
47f1cf77 1830 if (meta->mro_linear_all) {
d4f87935
FC
1831 SvREFCNT_dec_NN(meta->mro_linear_all);
1832 /* mro_linear_current is just acting as a shortcut pointer,
1833 hence the else. */
1834 }
1835 else
47f1cf77
FC
1836 /* Only the current MRO is stored, so this owns the data.
1837 */
1838 SvREFCNT_dec(meta->mro_linear_current);
9bfbb681 1839 SvREFCNT_dec(meta->mro_nextmethod);
47f1cf77 1840 SvREFCNT_dec(meta->isa);
1a33a059 1841 SvREFCNT_dec(meta->super);
47f1cf77 1842 Safefree(meta);
339441ef 1843 HvAUX(hv)->xhv_mro_meta = NULL;
47f1cf77 1844 }
339441ef 1845 if (!HvAUX(hv)->xhv_name_u.xhvnameu_name && ! HvAUX(hv)->xhv_backreferences)
745edda6 1846 SvFLAGS(hv) &= ~SVf_OOK;
745edda6
FC
1847 }
1848 if (!SvOOK(hv)) {
1849 Safefree(HvARRAY(hv));
f6bb1c88 1850 xhv->xhv_max = PERL_HASH_DEFAULT_HvMAX; /* HvMAX(hv) = 7 (it's a normal hash) */
745edda6 1851 HvARRAY(hv) = 0;
2d0d1ecc 1852 }
5bec93be
DM
1853 /* if we're freeing the HV, the SvMAGIC field has been reused for
1854 * other purposes, and so there can't be any placeholder magic */
1855 if (SvREFCNT(hv))
1856 HvPLACEHOLDERS_set(hv, 0);
a0d0e21e
LW
1857
1858 if (SvRMAGICAL(hv))
ad64d0ec 1859 mg_clear(MUTABLE_SV(hv));
8505eec0 1860 if (save) LEAVE;
79072805
LW
1861}
1862
4d0fbddd
NC
1863/*
1864=for apidoc hv_fill
1865
72d33970
FC
1866Returns the number of hash buckets that
1867happen to be in use. This function is
4d0fbddd
NC
1868wrapped by the macro C<HvFILL>.
1869
9faf471a
NC
1870Previously this value was always stored in the HV structure, which created an
1871overhead on every hash (and pretty much every object) for something that was
72d33970
FC
1872rarely used. Now we calculate it on demand the first
1873time that it is needed, and cache it if that calculation
1874is going to be costly to repeat. The cached
9faf471a
NC
1875value is updated by insertions and deletions, but (currently) discarded if
1876the hash is split.
4d0fbddd
NC
1877
1878=cut
1879*/
1880
1881STRLEN
9faf471a 1882Perl_hv_fill(pTHX_ HV *const hv)
4d0fbddd
NC
1883{
1884 STRLEN count = 0;
1885 HE **ents = HvARRAY(hv);
9faf471a 1886 struct xpvhv_aux *aux = SvOOK(hv) ? HvAUX(hv) : NULL;
4d0fbddd
NC
1887
1888 PERL_ARGS_ASSERT_HV_FILL;
1889
553215cc
NC
1890 /* No keys implies no buckets used.
1891 One key can only possibly mean one bucket used. */
1892 if (HvTOTALKEYS(hv) < 2)
1893 return HvTOTALKEYS(hv);
1894
9faf471a
NC
1895#ifndef DEBUGGING
1896 if (aux && aux->xhv_fill_lazy)
1897 return aux->xhv_fill_lazy;
1898#endif
1899
4d0fbddd 1900 if (ents) {
fcd24582
NC
1901 HE *const *const last = ents + HvMAX(hv);
1902 count = last + 1 - ents;
4d0fbddd
NC
1903
1904 do {
fcd24582
NC
1905 if (!*ents)
1906 --count;
1907 } while (++ents <= last);
4d0fbddd 1908 }
9faf471a
NC
1909 if (aux) {
1910#ifdef DEBUGGING
1911 if (aux->xhv_fill_lazy)
1912 assert(aux->xhv_fill_lazy == count);
1913#endif
1914 aux->xhv_fill_lazy = count;
1915 } else if (HvMAX(hv) >= HV_FILL_THRESHOLD) {
1916 aux = hv_auxinit(hv);
1917 aux->xhv_fill_lazy = count;
1918 }
4d0fbddd
NC
1919 return count;
1920}
1921
0e0ab621
YO
1922/* hash a pointer to a U32 - Used in the hash traversal randomization
1923 * and bucket order randomization code
1924 *
1925 * this code was derived from Sereal, which was derived from autobox.
1926 */
1927
1928PERL_STATIC_INLINE U32 S_ptr_hash(PTRV u) {
1929#if PTRSIZE == 8
1930 /*
1931 * This is one of Thomas Wang's hash functions for 64-bit integers from:
1932 * http://www.concentric.net/~Ttwang/tech/inthash.htm
1933 */
1934 u = (~u) + (u << 18);
1935 u = u ^ (u >> 31);
1936 u = u * 21;
1937 u = u ^ (u >> 11);
1938 u = u + (u << 6);
1939 u = u ^ (u >> 22);
1940#else
1941 /*
1942 * This is one of Bob Jenkins' hash functions for 32-bit integers
1943 * from: http://burtleburtle.net/bob/hash/integer.html
1944 */
1945 u = (u + 0x7ed55d16) + (u << 12);
1946 u = (u ^ 0xc761c23c) ^ (u >> 19);
1947 u = (u + 0x165667b1) + (u << 5);
1948 u = (u + 0xd3a2646c) ^ (u << 9);
1949 u = (u + 0xfd7046c5) + (u << 3);
1950 u = (u ^ 0xb55a4f09) ^ (u >> 16);
1951#endif
1952 return (U32)u;
1953}
1954
bea177f3
YO
1955static struct xpvhv_aux*
1956S_hv_auxinit_internal(struct xpvhv_aux *iter) {
1957 PERL_ARGS_ASSERT_HV_AUXINIT_INTERNAL;
1958 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */
1959 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */
1960#ifdef PERL_HASH_RANDOMIZE_KEYS
1961 iter->xhv_last_rand = iter->xhv_rand;
1962#endif
1963 iter->xhv_fill_lazy = 0;
1964 iter->xhv_name_u.xhvnameu_name = 0;
1965 iter->xhv_name_count = 0;
1966 iter->xhv_backreferences = 0;
1967 iter->xhv_mro_meta = NULL;
1968 iter->xhv_aux_flags = 0;
1969 return iter;
1970}
1971
0e0ab621 1972
b464bac0 1973static struct xpvhv_aux*
0e0ab621 1974S_hv_auxinit(pTHX_ HV *hv) {
bfcb3514 1975 struct xpvhv_aux *iter;
b79f7545 1976 char *array;
bfcb3514 1977
7918f24d
NC
1978 PERL_ARGS_ASSERT_HV_AUXINIT;
1979
0e0ab621
YO
1980 if (!SvOOK(hv)) {
1981 if (!HvARRAY(hv)) {
1982 Newxz(array, PERL_HV_ARRAY_ALLOC_BYTES(HvMAX(hv) + 1)
1983 + sizeof(struct xpvhv_aux), char);
1984 } else {
1985 array = (char *) HvARRAY(hv);
1986 Renew(array, PERL_HV_ARRAY_ALLOC_BYTES(HvMAX(hv) + 1)
1987 + sizeof(struct xpvhv_aux), char);
1988 }
1989 HvARRAY(hv) = (HE**)array;
1990 SvOOK_on(hv);
a7b39f85 1991 iter = HvAUX(hv);
6a5b4183
YO
1992#ifdef PERL_HASH_RANDOMIZE_KEYS
1993 if (PL_HASH_RAND_BITS_ENABLED) {
1994 /* mix in some new state to PL_hash_rand_bits to "randomize" the traversal order*/
1995 if (PL_HASH_RAND_BITS_ENABLED == 1)
1996 PL_hash_rand_bits += ptr_hash((PTRV)array);
1997 PL_hash_rand_bits = ROTL_UV(PL_hash_rand_bits,1);
1998 }
a7b39f85 1999 iter->xhv_rand = (U32)PL_hash_rand_bits;
6a5b4183 2000#endif
a7b39f85
YO
2001 } else {
2002 iter = HvAUX(hv);
b79f7545 2003 }
bfcb3514 2004
bea177f3 2005 return hv_auxinit_internal(iter);
bfcb3514
NC
2006}
2007
954c1994
GS
2008/*
2009=for apidoc hv_iterinit
2010
2011Prepares a starting point to traverse a hash table. Returns the number of
1b95d04f 2012keys in the hash (i.e. the same as C<HvUSEDKEYS(hv)>). The return value is
1c846c1f 2013currently only meaningful for hashes without tie magic.
954c1994
GS
2014
2015NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
2016hash buckets that happen to be in use. If you still need that esoteric
b24b84ef 2017value, you can get it through the macro C<HvFILL(hv)>.
954c1994 2018
e16e2ff8 2019
954c1994
GS
2020=cut
2021*/
2022
79072805 2023I32
864dbfa3 2024Perl_hv_iterinit(pTHX_ HV *hv)
79072805 2025{
7918f24d
NC
2026 PERL_ARGS_ASSERT_HV_ITERINIT;
2027
2028 /* FIXME: Are we not NULL, or do we croak? Place bets now! */
2029
aa689395 2030 if (!hv)
cea2e8a9 2031 Perl_croak(aTHX_ "Bad hash");
bfcb3514 2032
b79f7545 2033 if (SvOOK(hv)) {
339441ef 2034 struct xpvhv_aux * iter = HvAUX(hv);
0bd48802 2035 HE * const entry = iter->xhv_eiter; /* HvEITER(hv) */
bfcb3514
NC
2036 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
2037 HvLAZYDEL_off(hv);
2038 hv_free_ent(hv, entry);
2039 }
339441ef 2040 iter = HvAUX(hv); /* may have been reallocated */
bfcb3514 2041 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */
4608196e 2042 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */
6a5b4183 2043#ifdef PERL_HASH_RANDOMIZE_KEYS
a7b39f85 2044 iter->xhv_last_rand = iter->xhv_rand;
6a5b4183 2045#endif
bfcb3514 2046 } else {
6136c704 2047 hv_auxinit(hv);
72940dca 2048 }
44a2ac75 2049
cbec9347 2050 /* used to be xhv->xhv_fill before 5.004_65 */
5d88ecd7 2051 return HvTOTALKEYS(hv);
79072805 2052}
bfcb3514
NC
2053
2054I32 *
2055Perl_hv_riter_p(pTHX_ HV *hv) {
2056 struct xpvhv_aux *iter;
2057
7918f24d
NC
2058 PERL_ARGS_ASSERT_HV_RITER_P;
2059
bfcb3514
NC
2060 if (!hv)
2061 Perl_croak(aTHX_ "Bad hash");
2062
6136c704 2063 iter = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv);
bfcb3514
NC
2064 return &(iter->xhv_riter);
2065}
2066
2067HE **
2068Perl_hv_eiter_p(pTHX_ HV *hv) {
2069 struct xpvhv_aux *iter;
2070
7918f24d
NC
2071 PERL_ARGS_ASSERT_HV_EITER_P;
2072
bfcb3514
NC
2073 if (!hv)
2074 Perl_croak(aTHX_ "Bad hash");
2075
6136c704 2076 iter = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv);
bfcb3514
NC
2077 return &(iter->xhv_eiter);
2078}
2079
2080void
2081Perl_hv_riter_set(pTHX_ HV *hv, I32 riter) {
2082 struct xpvhv_aux *iter;
2083
7918f24d
NC
2084 PERL_ARGS_ASSERT_HV_RITER_SET;
2085
bfcb3514
NC
2086 if (!hv)
2087 Perl_croak(aTHX_ "Bad hash");
2088
b79f7545
NC
2089 if (SvOOK(hv)) {
2090 iter = HvAUX(hv);
2091 } else {
bfcb3514
NC
2092 if (riter == -1)
2093 return;
2094
6136c704 2095 iter = hv_auxinit(hv);
bfcb3514
NC
2096 }
2097 iter->xhv_riter = riter;
2098}
2099
2100void
6a5b4183
YO
2101Perl_hv_rand_set(pTHX_ HV *hv, U32 new_xhv_rand) {
2102 struct xpvhv_aux *iter;
2103
2104 PERL_ARGS_ASSERT_HV_RAND_SET;
2105
2106#ifdef PERL_HASH_RANDOMIZE_KEYS
2107 if (!hv)
2108 Perl_croak(aTHX_ "Bad hash");
2109
2110 if (SvOOK(hv)) {
2111 iter = HvAUX(hv);
2112 } else {
2113 iter = hv_auxinit(hv);
2114 }
2115 iter->xhv_rand = new_xhv_rand;
2116#else
2117 Perl_croak(aTHX_ "This Perl has not been built with support for randomized hash key traversal but something called Perl_hv_rand_set().");
2118#endif
2119}
2120
2121void
bfcb3514
NC
2122Perl_hv_eiter_set(pTHX_ HV *hv, HE *eiter) {
2123 struct xpvhv_aux *iter;
2124
7918f24d
NC
2125 PERL_ARGS_ASSERT_HV_EITER_SET;
2126
bfcb3514
NC
2127 if (!hv)
2128 Perl_croak(aTHX_ "Bad hash");
2129
b79f7545
NC
2130 if (SvOOK(hv)) {
2131 iter = HvAUX(hv);
2132 } else {
bfcb3514
NC
2133 /* 0 is the default so don't go malloc()ing a new structure just to
2134 hold 0. */
2135 if (!eiter)
2136 return;
2137
6136c704 2138 iter = hv_auxinit(hv);
bfcb3514
NC
2139 }
2140 iter->xhv_eiter = eiter;
2141}
2142
bfcb3514 2143void
4164be69 2144Perl_hv_name_set(pTHX_ HV *hv, const char *name, U32 len, U32 flags)
bfcb3514 2145{
97aff369 2146 dVAR;
b79f7545 2147 struct xpvhv_aux *iter;
7423f6db 2148 U32 hash;
78b79c77 2149 HEK **spot;
46c461b5 2150
7918f24d 2151 PERL_ARGS_ASSERT_HV_NAME_SET;
bfcb3514 2152
4164be69
NC
2153 if (len > I32_MAX)
2154 Perl_croak(aTHX_ "panic: hv name too long (%"UVuf")", (UV) len);
2155
b79f7545
NC
2156 if (SvOOK(hv)) {
2157 iter = HvAUX(hv);
15d9236d 2158 if (iter->xhv_name_u.xhvnameu_name) {
b7247a80 2159 if(iter->xhv_name_count) {
745edda6 2160 if(flags & HV_NAME_SETALL) {
15d9236d 2161 HEK ** const name = HvAUX(hv)->xhv_name_u.xhvnameu_names;
78b79c77
FC
2162 HEK **hekp = name + (
2163 iter->xhv_name_count < 0
2164 ? -iter->xhv_name_count
2165 : iter->xhv_name_count
2166 );
2167 while(hekp-- > name+1)
b7247a80 2168 unshare_hek_or_pvn(*hekp, 0, 0, 0);
78b79c77
FC
2169 /* The first elem may be null. */
2170 if(*name) unshare_hek_or_pvn(*name, 0, 0, 0);
b7247a80 2171 Safefree(name);
339441ef 2172 iter = HvAUX(hv); /* may been realloced */
15d9236d 2173 spot = &iter->xhv_name_u.xhvnameu_name;
78b79c77
FC
2174 iter->xhv_name_count = 0;
2175 }
2176 else {
78b79c77
FC
2177 if(iter->xhv_name_count > 0) {
2178 /* shift some things over */
15d9236d
NC
2179 Renew(
2180 iter->xhv_name_u.xhvnameu_names, iter->xhv_name_count + 1, HEK *
4c2bfb4f 2181 );
15d9236d 2182 spot = iter->xhv_name_u.xhvnameu_names;
4c2bfb4f 2183 spot[iter->xhv_name_count] = spot[1];
78b79c77 2184 spot[1] = spot[0];
4c2bfb4f 2185 iter->xhv_name_count = -(iter->xhv_name_count + 1);
78b79c77 2186 }
15d9236d 2187 else if(*(spot = iter->xhv_name_u.xhvnameu_names)) {
78b79c77
FC
2188 unshare_hek_or_pvn(*spot, 0, 0, 0);
2189 }
2190 }
2191 }
745edda6 2192 else if (flags & HV_NAME_SETALL) {
15d9236d 2193 unshare_hek_or_pvn(iter->xhv_name_u.xhvnameu_name, 0, 0, 0);
339441ef 2194 iter = HvAUX(hv); /* may been realloced */
15d9236d 2195 spot = &iter->xhv_name_u.xhvnameu_name;
b7247a80 2196 }
745edda6 2197 else {
15d9236d
NC
2198 HEK * const existing_name = iter->xhv_name_u.xhvnameu_name;
2199 Newx(iter->xhv_name_u.xhvnameu_names, 2, HEK *);
745edda6 2200 iter->xhv_name_count = -2;
15d9236d 2201 spot = iter->xhv_name_u.xhvnameu_names;
745edda6
FC
2202 spot[1] = existing_name;
2203 }
7423f6db 2204 }
15d9236d 2205 else { spot = &iter->xhv_name_u.xhvnameu_name; iter->xhv_name_count = 0; }
16580ff5 2206 } else {
bfcb3514
NC
2207 if (name == 0)
2208 return;
2209
6136c704 2210 iter = hv_auxinit(hv);
15d9236d 2211 spot = &iter->xhv_name_u.xhvnameu_name;
bfcb3514 2212 }
7423f6db 2213 PERL_HASH(hash, name, len);
c60dbbc3 2214 *spot = name ? share_hek(name, flags & SVf_UTF8 ? -(I32)len : (I32)len, hash) : NULL;
4643eb69
BF
2215}
2216
2217/*
2218This is basically sv_eq_flags() in sv.c, but we avoid the magic
2219and bytes checking.
2220*/
2221
2222STATIC I32
2223hek_eq_pvn_flags(pTHX_ const HEK *hek, const char* pv, const I32 pvlen, const U32 flags) {
2224 if ( (HEK_UTF8(hek) ? 1 : 0) != (flags & SVf_UTF8 ? 1 : 0) ) {
2225 if (flags & SVf_UTF8)
2226 return (bytes_cmp_utf8(
2227 (const U8*)HEK_KEY(hek), HEK_LEN(hek),
2228 (const U8*)pv, pvlen) == 0);
2229 else
2230 return (bytes_cmp_utf8(
2231 (const U8*)pv, pvlen,
2232 (const U8*)HEK_KEY(hek), HEK_LEN(hek)) == 0);
2233 }
2234 else
d35fec6c 2235 return HEK_LEN(hek) == pvlen && ((HEK_KEY(hek) == pv)
4643eb69 2236 || memEQ(HEK_KEY(hek), pv, pvlen));
bfcb3514
NC
2237}
2238
99206677
FC
2239/*
2240=for apidoc hv_ename_add
2241
db4fbf16 2242Adds a name to a stash's internal list of effective names. See
99206677
FC
2243C<hv_ename_delete>.
2244
2245This is called when a stash is assigned to a new location in the symbol
2246table.
2247
2248=cut
2249*/
2250
ee72b38d 2251void
27a1175b 2252Perl_hv_ename_add(pTHX_ HV *hv, const char *name, U32 len, U32 flags)
ee72b38d
FC
2253{
2254 dVAR;
2255 struct xpvhv_aux *aux = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv);
2256 U32 hash;
2257
78b79c77 2258 PERL_ARGS_ASSERT_HV_ENAME_ADD;
ee72b38d
FC
2259
2260 if (len > I32_MAX)
2261 Perl_croak(aTHX_ "panic: hv name too long (%"UVuf")", (UV) len);
2262
2263 PERL_HASH(hash, name, len);
2264
ee72b38d 2265 if (aux->xhv_name_count) {
15d9236d 2266 HEK ** const xhv_name = aux->xhv_name_u.xhvnameu_names;
78b79c77
FC
2267 I32 count = aux->xhv_name_count;
2268 HEK **hekp = xhv_name + (count < 0 ? -count : count);
ee72b38d
FC
2269 while (hekp-- > xhv_name)
2270 if (
4643eb69
BF
2271 (HEK_UTF8(*hekp) || (flags & SVf_UTF8))
2272 ? hek_eq_pvn_flags(aTHX_ *hekp, name, (I32)len, flags)
2273 : (HEK_LEN(*hekp) == (I32)len && memEQ(HEK_KEY(*hekp), name, len))
2274 ) {
78b79c77
FC
2275 if (hekp == xhv_name && count < 0)
2276 aux->xhv_name_count = -count;
2277 return;
2278 }
2279 if (count < 0) aux->xhv_name_count--, count = -count;
2280 else aux->xhv_name_count++;
15d9236d 2281 Renew(aux->xhv_name_u.xhvnameu_names, count + 1, HEK *);
c60dbbc3 2282 (aux->xhv_name_u.xhvnameu_names)[count] = share_hek(name, (flags & SVf_UTF8 ? -(I32)len : (I32)len), hash);
ee72b38d
FC
2283 }
2284 else {
15d9236d 2285 HEK *existing_name = aux->xhv_name_u.xhvnameu_name;
ee72b38d 2286 if (
4643eb69
BF
2287 existing_name && (
2288 (HEK_UTF8(existing_name) || (flags & SVf_UTF8))
2289 ? hek_eq_pvn_flags(aTHX_ existing_name, name, (I32)len, flags)
2290 : (HEK_LEN(existing_name) == (I32)len && memEQ(HEK_KEY(existing_name), name, len))
2291 )
ee72b38d 2292 ) return;
15d9236d 2293 Newx(aux->xhv_name_u.xhvnameu_names, 2, HEK *);
78b79c77 2294 aux->xhv_name_count = existing_name ? 2 : -2;
15d9236d 2295 *aux->xhv_name_u.xhvnameu_names = existing_name;
c60dbbc3 2296 (aux->xhv_name_u.xhvnameu_names)[1] = share_hek(name, (flags & SVf_UTF8 ? -(I32)len : (I32)len), hash);
ee72b38d
FC
2297 }
2298}
2299
99206677
FC
2300/*
2301=for apidoc hv_ename_delete
2302
db4fbf16 2303Removes a name from a stash's internal list of effective names. If this is
99206677
FC
2304the name returned by C<HvENAME>, then another name in the list will take
2305its place (C<HvENAME> will use it).
2306
2307This is called when a stash is deleted from the symbol table.
2308
2309=cut
2310*/
2311
ee72b38d 2312void
27a1175b 2313Perl_hv_ename_delete(pTHX_ HV *hv, const char *name, U32 len, U32 flags)
ee72b38d 2314{
ee72b38d
FC
2315 struct xpvhv_aux *aux;
2316
78b79c77 2317 PERL_ARGS_ASSERT_HV_ENAME_DELETE;
ee72b38d
FC
2318
2319 if (len > I32_MAX)
2320 Perl_croak(aTHX_ "panic: hv name too long (%"UVuf")", (UV) len);
2321
2322 if (!SvOOK(hv)) return;
2323
2324 aux = HvAUX(hv);
15d9236d 2325 if (!aux->xhv_name_u.xhvnameu_name) return;
ee72b38d
FC
2326
2327 if (aux->xhv_name_count) {
15d9236d 2328 HEK ** const namep = aux->xhv_name_u.xhvnameu_names;
78b79c77
FC
2329 I32 const count = aux->xhv_name_count;
2330 HEK **victim = namep + (count < 0 ? -count : count);
2331 while (victim-- > namep + 1)
ee72b38d 2332 if (
4643eb69
BF
2333 (HEK_UTF8(*victim) || (flags & SVf_UTF8))
2334 ? hek_eq_pvn_flags(aTHX_ *victim, name, (I32)len, flags)
2335 : (HEK_LEN(*victim) == (I32)len && memEQ(HEK_KEY(*victim), name, len))
ee72b38d
FC
2336 ) {
2337 unshare_hek_or_pvn(*victim, 0, 0, 0);
339441ef 2338 aux = HvAUX(hv); /* may been realloced */
78b79c77
FC
2339 if (count < 0) ++aux->xhv_name_count;
2340 else --aux->xhv_name_count;
2341 if (
2342 (aux->xhv_name_count == 1 || aux->xhv_name_count == -1)
2343 && !*namep
2344 ) { /* if there are none left */
ee72b38d 2345 Safefree(namep);
15d9236d 2346 aux->xhv_name_u.xhvnameu_names = NULL;
78b79c77 2347 aux->xhv_name_count = 0;
ee72b38d
FC
2348 }
2349 else {
2350 /* Move the last one back to fill the empty slot. It
2351 does not matter what order they are in. */
78b79c77 2352 *victim = *(namep + (count < 0 ? -count : count) - 1);
ee72b38d
FC
2353 }
2354 return;
2355 }
78b79c77 2356 if (
4643eb69
BF
2357 count > 0 && (HEK_UTF8(*namep) || (flags & SVf_UTF8))
2358 ? hek_eq_pvn_flags(aTHX_ *namep, name, (I32)len, flags)
2359 : (HEK_LEN(*namep) == (I32)len && memEQ(HEK_KEY(*namep), name, len))
78b79c77
FC
2360 ) {
2361 aux->xhv_name_count = -count;
2362 }
ee72b38d
FC
2363 }
2364 else if(
4643eb69
BF
2365 (HEK_UTF8(aux->xhv_name_u.xhvnameu_name) || (flags & SVf_UTF8))
2366 ? hek_eq_pvn_flags(aTHX_ aux->xhv_name_u.xhvnameu_name, name, (I32)len, flags)
2367 : (HEK_LEN(aux->xhv_name_u.xhvnameu_name) == (I32)len &&
2368 memEQ(HEK_KEY(aux->xhv_name_u.xhvnameu_name), name, len))
ee72b38d 2369 ) {
15d9236d
NC
2370 HEK * const namehek = aux->xhv_name_u.xhvnameu_name;
2371 Newx(aux->xhv_name_u.xhvnameu_names, 1, HEK *);
2372 *aux->xhv_name_u.xhvnameu_names = namehek;
3f783763 2373 aux->xhv_name_count = -1;
ee72b38d
FC
2374 }
2375}
2376
86f55936
NC
2377AV **
2378Perl_hv_backreferences_p(pTHX_ HV *hv) {
6136c704 2379 struct xpvhv_aux * const iter = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv);
7918f24d
NC
2380
2381 PERL_ARGS_ASSERT_HV_BACKREFERENCES_P;
7918f24d 2382
86f55936
NC
2383 return &(iter->xhv_backreferences);
2384}
2385
09aad8f0
DM
2386void
2387Perl_hv_kill_backrefs(pTHX_ HV *hv) {
2388 AV *av;
2389
2390 PERL_ARGS_ASSERT_HV_KILL_BACKREFS;
2391
2392 if (!SvOOK(hv))
2393 return;
2394
2395 av = HvAUX(hv)->xhv_backreferences;
2396
2397 if (av) {
2398 HvAUX(hv)->xhv_backreferences = 0;
2399 Perl_sv_kill_backrefs(aTHX_ MUTABLE_SV(hv), av);
5648c0ae 2400 if (SvTYPE(av) == SVt_PVAV)
0c920c9b 2401 SvREFCNT_dec_NN(av);
09aad8f0
DM
2402 }
2403}
2404
954c1994 2405/*
7a7b9979
NC
2406hv_iternext is implemented as a macro in hv.h
2407
954c1994
GS
2408=for apidoc hv_iternext
2409
2410Returns entries from a hash iterator. See C<hv_iterinit>.
2411
fe7bca90
NC
2412You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
2413iterator currently points to, without losing your place or invalidating your
2414iterator. Note that in this case the current entry is deleted from the hash
2415with your iterator holding the last reference to it. Your iterator is flagged
2416to free the entry on the next call to C<hv_iternext>, so you must not discard
2417your iterator immediately else the entry will leak - call C<hv_iternext> to
2418trigger the resource deallocation.
2419
fe7bca90
NC
2420=for apidoc hv_iternext_flags
2421
2422Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>.
2423The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
2424set the placeholders keys (for restricted hashes) will be returned in addition
72d33970 2425to normal keys. By default placeholders are automatically skipped over.
7996736c 2426Currently a placeholder is implemented with a value that is
990c89d7 2427C<&PL_sv_placeholder>. Note that the implementation of placeholders and
fe7bca90
NC
2428restricted hashes may change, and the implementation currently is
2429insufficiently abstracted for any change to be tidy.
e16e2ff8 2430
fe7bca90 2431=cut
e16e2ff8
NC
2432*/
2433
2434HE *
2435Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
2436{
27da23d5 2437 dVAR;
eb578fdb
KW
2438 XPVHV* xhv;
2439 HE *entry;
a0d0e21e 2440 HE *oldentry;
463ee0b2 2441 MAGIC* mg;
bfcb3514 2442 struct xpvhv_aux *iter;
79072805 2443
7918f24d
NC
2444 PERL_ARGS_ASSERT_HV_ITERNEXT_FLAGS;
2445
79072805 2446 if (!hv)
cea2e8a9 2447 Perl_croak(aTHX_ "Bad hash");
81714fb9 2448
cbec9347 2449 xhv = (XPVHV*)SvANY(hv);
bfcb3514 2450
b79f7545 2451 if (!SvOOK(hv)) {
bfcb3514 2452 /* Too many things (well, pp_each at least) merrily assume that you can
caee4c53 2453 call hv_iternext without calling hv_iterinit, so we'll have to deal
bfcb3514
NC
2454 with it. */
2455 hv_iterinit(hv);
bfcb3514 2456 }
b79f7545 2457 iter = HvAUX(hv);
bfcb3514
NC
2458
2459 oldentry = entry = iter->xhv_eiter; /* HvEITER(hv) */
e62cc96a 2460 if (SvMAGICAL(hv) && SvRMAGICAL(hv)) {
ad64d0ec 2461 if ( ( mg = mg_find((const SV *)hv, PERL_MAGIC_tied) ) ) {
e62cc96a
YO
2462 SV * const key = sv_newmortal();
2463 if (entry) {
2464 sv_setsv(key, HeSVKEY_force(entry));
2465 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
895cdc83 2466 HeSVKEY_set(entry, NULL);
e62cc96a
YO
2467 }
2468 else {
2469 char *k;
2470 HEK *hek;
2471
2472 /* one HE per MAGICAL hash */
2473 iter->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
895cdc83 2474 HvLAZYDEL_on(hv); /* make sure entry gets freed */
e62cc96a 2475 Zero(entry, 1, HE);
ad64d0ec 2476 Newxz(k, HEK_BASESIZE + sizeof(const SV *), char);
e62cc96a
YO
2477 hek = (HEK*)k;
2478 HeKEY_hek(entry) = hek;
2479 HeKLEN(entry) = HEf_SVKEY;
2480 }
ad64d0ec 2481 magic_nextpack(MUTABLE_SV(hv),mg,key);
e62cc96a
YO
2482 if (SvOK(key)) {
2483 /* force key to stay around until next time */
2484 HeSVKEY_set(entry, SvREFCNT_inc_simple_NN(key));
2485 return entry; /* beware, hent_val is not set */
2486 }
ef8d46e8 2487 SvREFCNT_dec(HeVAL(entry));
e62cc96a
YO
2488 Safefree(HeKEY_hek(entry));
2489 del_HE(entry);
339441ef 2490 iter = HvAUX(hv); /* may been realloced */
e62cc96a 2491 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */
895cdc83 2492 HvLAZYDEL_off(hv);
e62cc96a 2493 return NULL;
81714fb9 2494 }
79072805 2495 }
7ee146b1 2496#if defined(DYNAMIC_ENV_FETCH) && !defined(__riscos__) /* set up %ENV for iteration */
ad64d0ec
NC
2497 if (!entry && SvRMAGICAL((const SV *)hv)
2498 && mg_find((const SV *)hv, PERL_MAGIC_env)) {
f675dbe5 2499 prime_env_iter();
03026e68
JM
2500#ifdef VMS
2501 /* The prime_env_iter() on VMS just loaded up new hash values
2502 * so the iteration count needs to be reset back to the beginning
2503 */
2504 hv_iterinit(hv);
2505 iter = HvAUX(hv);
2506 oldentry = entry = iter->xhv_eiter; /* HvEITER(hv) */
2507#endif
2508 }
f675dbe5 2509#endif
463ee0b2 2510
bfaf5b52 2511 /* hv_iterinit now ensures this. */
b79f7545
NC
2512 assert (HvARRAY(hv));
2513
015a5f36 2514 /* At start of hash, entry is NULL. */
fde52b5c 2515 if (entry)
8aacddc1 2516 {
fde52b5c 2517 entry = HeNEXT(entry);
e16e2ff8
NC
2518 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
2519 /*
2520 * Skip past any placeholders -- don't want to include them in
2521 * any iteration.
2522 */
7996736c 2523 while (entry && HeVAL(entry) == &PL_sv_placeholder) {
e16e2ff8
NC
2524 entry = HeNEXT(entry);
2525 }
8aacddc1
NIS
2526 }
2527 }
6a5b4183
YO
2528
2529#ifdef PERL_HASH_RANDOMIZE_KEYS
a7b39f85
YO
2530 if (iter->xhv_last_rand != iter->xhv_rand) {
2531 if (iter->xhv_riter != -1) {
2532 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
2533 "Use of each() on hash after insertion without resetting hash iterator results in undefined behavior"
2534 pTHX__FORMAT
2535 pTHX__VALUE);
2536 }
339441ef 2537 iter = HvAUX(hv); /* may been realloced */
a7b39f85
YO
2538 iter->xhv_last_rand = iter->xhv_rand;
2539 }
6a5b4183 2540#endif
015a5f36 2541
9eb4ebd1
NC
2542 /* Skip the entire loop if the hash is empty. */
2543 if ((flags & HV_ITERNEXT_WANTPLACEHOLDERS)
2544 ? HvTOTALKEYS(hv) : HvUSEDKEYS(hv)) {
900ac051
MM
2545 while (!entry) {
2546 /* OK. Come to the end of the current list. Grab the next one. */
2547
2548 iter->xhv_riter++; /* HvRITER(hv)++ */
2549 if (iter->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
2550 /* There is no next one. End of the hash. */
2551 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */
6a5b4183
YO
2552#ifdef PERL_HASH_RANDOMIZE_KEYS
2553 iter->xhv_last_rand = iter->xhv_rand; /* reset xhv_last_rand so we can detect inserts during traversal */
2554#endif
900ac051
MM
2555 break;
2556 }
6a5b4183 2557 entry = (HvARRAY(hv))[ PERL_HASH_ITER_BUCKET(iter) & xhv->xhv_max ];
8aacddc1 2558
900ac051
MM
2559 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
2560 /* If we have an entry, but it's a placeholder, don't count it.
2561 Try the next. */
2562 while (entry && HeVAL(entry) == &PL_sv_placeholder)
2563 entry = HeNEXT(entry);
2564 }
2565 /* Will loop again if this linked list starts NULL
2566 (for HV_ITERNEXT_WANTPLACEHOLDERS)
2567 or if we run through it and find only placeholders. */
015a5f36 2568 }
fde52b5c 2569 }
a7b39f85
YO
2570 else {
2571 iter->xhv_riter = -1;
6a5b4183 2572#ifdef PERL_HASH_RANDOMIZE_KEYS
a7b39f85 2573 iter->xhv_last_rand = iter->xhv_rand;
6a5b4183 2574#endif
a7b39f85 2575 }
79072805 2576
72940dca 2577 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
2578 HvLAZYDEL_off(hv);
68dc0745 2579 hv_free_ent(hv, oldentry);
72940dca 2580 }
a0d0e21e 2581
339441ef 2582 iter = HvAUX(hv); /* may been realloced */
bfcb3514 2583 iter->xhv_eiter = entry; /* HvEITER(hv) = entry */
79072805
LW
2584 return entry;
2585}
2586
954c1994
GS
2587/*
2588=for apidoc hv_iterkey
2589
2590Returns the key from the current position of the hash iterator. See
2591C<hv_iterinit>.
2592
2593=cut
2594*/
2595
79072805 2596char *
5aaab254 2597Perl_hv_iterkey(pTHX_ HE *entry, I32 *retlen)
79072805 2598{
7918f24d
NC
2599 PERL_ARGS_ASSERT_HV_ITERKEY;
2600
fde52b5c 2601 if (HeKLEN(entry) == HEf_SVKEY) {
fb73857a 2602 STRLEN len;
0bd48802 2603 char * const p = SvPV(HeKEY_sv(entry), len);
fb73857a 2604 *retlen = len;
2605 return p;
fde52b5c 2606 }
2607 else {
2608 *retlen = HeKLEN(entry);
2609 return HeKEY(entry);
2610 }
2611}
2612
2613/* unlike hv_iterval(), this always returns a mortal copy of the key */
954c1994
GS
2614/*
2615=for apidoc hv_iterkeysv
2616
2617Returns the key as an C<SV*> from the current position of the hash
2618iterator. The return value will always be a mortal copy of the key. Also
2619see C<hv_iterinit>.
2620
2621=cut
2622*/
2623
fde52b5c 2624SV *
5aaab254 2625Perl_hv_iterkeysv(pTHX_ HE *entry)
fde52b5c 2626{
7918f24d
NC
2627 PERL_ARGS_ASSERT_HV_ITERKEYSV;
2628
c1b02ed8 2629 return sv_2mortal(newSVhek(HeKEY_hek(entry)));
79072805
LW
2630}
2631
954c1994
GS
2632/*
2633=for apidoc hv_iterval
2634
2635Returns the value from the current position of the hash iterator. See
2636C<hv_iterkey>.
2637
2638=cut
2639*/
2640
79072805 2641SV *
5aaab254 2642Perl_hv_iterval(pTHX_ HV *hv, HE *entry)
79072805 2643{
7918f24d
NC
2644 PERL_ARGS_ASSERT_HV_ITERVAL;
2645
8990e307 2646 if (SvRMAGICAL(hv)) {
ad64d0ec 2647 if (mg_find((const SV *)hv, PERL_MAGIC_tied)) {
c4420975 2648 SV* const sv = sv_newmortal();
bbce6d69 2649 if (HeKLEN(entry) == HEf_SVKEY)
ad64d0ec 2650 mg_copy(MUTABLE_SV(hv), sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
a3b680e6 2651 else
ad64d0ec 2652 mg_copy(MUTABLE_SV(hv), sv, HeKEY(entry), HeKLEN(entry));
463ee0b2
LW
2653 return sv;
2654 }
79072805 2655 }
fde52b5c 2656 return HeVAL(entry);
79072805
LW
2657}
2658
954c1994
GS
2659/*
2660=for apidoc hv_iternextsv
2661
2662Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
2663operation.
2664
2665=cut
2666*/
2667
a0d0e21e 2668SV *
864dbfa3 2669Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
a0d0e21e 2670{
0bd48802
AL
2671 HE * const he = hv_iternext_flags(hv, 0);
2672
7918f24d
NC
2673 PERL_ARGS_ASSERT_HV_ITERNEXTSV;
2674
0bd48802 2675 if (!he)
a0d0e21e
LW
2676 return NULL;
2677 *key = hv_iterkey(he, retlen);
2678 return hv_iterval(hv, he);
2679}
2680
954c1994 2681/*
bc5cdc23
NC
2682
2683Now a macro in hv.h
2684
954c1994
GS
2685=for apidoc hv_magic
2686
2687Adds magic to a hash. See C<sv_magic>.
2688
2689=cut
2690*/
2691
bbce6d69 2692/* possibly free a shared string if no one has access to it
fde52b5c 2693 * len and hash must both be valid for str.
2694 */
bbce6d69 2695void
864dbfa3 2696Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
fde52b5c 2697{
19692e8d
NC
2698 unshare_hek_or_pvn (NULL, str, len, hash);
2699}
2700
2701
2702void
2703Perl_unshare_hek(pTHX_ HEK *hek)
2704{
bf11fd37 2705 assert(hek);
19692e8d
NC
2706 unshare_hek_or_pvn(hek, NULL, 0, 0);
2707}
2708
2709/* possibly free a shared string if no one has access to it
2710 hek if non-NULL takes priority over the other 3, else str, len and hash
2711 are used. If so, len and hash must both be valid for str.
2712 */
df132699 2713STATIC void
97ddebaf 2714S_unshare_hek_or_pvn(pTHX_ const HEK *hek, const char *str, I32 len, U32 hash)
19692e8d 2715{
eb578fdb 2716 XPVHV* xhv;
20454177 2717 HE *entry;
eb578fdb 2718 HE **oentry;
c3654f1a 2719 bool is_utf8 = FALSE;
19692e8d 2720 int k_flags = 0;
aec46f14 2721 const char * const save = str;
cbbf8932 2722 struct shared_he *he = NULL;
c3654f1a 2723
19692e8d 2724 if (hek) {
cbae3960
NC
2725 /* Find the shared he which is just before us in memory. */
2726 he = (struct shared_he *)(((char *)hek)
2727 - STRUCT_OFFSET(struct shared_he,
2728 shared_he_hek));
2729
2730 /* Assert that the caller passed us a genuine (or at least consistent)
2731 shared hek */
2732 assert (he->shared_he_he.hent_hek == hek);
29404ae0 2733
de616631
NC
2734 if (he->shared_he_he.he_valu.hent_refcount - 1) {
2735 --he->shared_he_he.he_valu.hent_refcount;
29404ae0
NC
2736 return;
2737 }
29404ae0 2738
19692e8d
NC
2739 hash = HEK_HASH(hek);
2740 } else if (len < 0) {
2741 STRLEN tmplen = -len;
2742 is_utf8 = TRUE;
2743 /* See the note in hv_fetch(). --jhi */
2744 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2745 len = tmplen;
2746 if (is_utf8)
2747 k_flags = HVhek_UTF8;
2748 if (str != save)
2749 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
c3654f1a 2750 }
1c846c1f 2751
de616631 2752 /* what follows was the moral equivalent of:
6b88bc9c 2753 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
a0714e2c 2754 if (--*Svp == NULL)
6b88bc9c 2755 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
bbce6d69 2756 } */
cbec9347 2757 xhv = (XPVHV*)SvANY(PL_strtab);
fde52b5c 2758 /* assert(xhv_array != 0) */
9de10d5c 2759 oentry = &(HvARRAY(PL_strtab))[hash & (I32) HvMAX(PL_strtab)];
6c1b96a1
NC
2760 if (he) {
2761 const HE *const he_he = &(he->shared_he_he);
45d1cc86 2762 for (entry = *oentry; entry; oentry = &HeNEXT(entry), entry = *oentry) {
35ab5632
NC
2763 if (entry == he_he)
2764 break;
19692e8d
NC
2765 }
2766 } else {
35a4481c 2767 const int flags_masked = k_flags & HVhek_MASK;
45d1cc86 2768 for (entry = *oentry; entry; oentry = &HeNEXT(entry), entry = *oentry) {
19692e8d
NC
2769 if (HeHASH(entry) != hash) /* strings can't be equal */
2770 continue;
2771 if (HeKLEN(entry) != len)
2772 continue;
2773 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2774 continue;
2775 if (HeKFLAGS(entry) != flags_masked)
2776 continue;
19692e8d
NC
2777 break;
2778 }
2779 }
2780
35ab5632
NC
2781 if (entry) {
2782 if (--entry->he_valu.hent_refcount == 0) {
19692e8d 2783 *oentry = HeNEXT(entry);
cbae3960 2784 Safefree(entry);
4c7185a0 2785 xhv->xhv_keys--; /* HvTOTALKEYS(hv)-- */
19692e8d 2786 }
fde52b5c 2787 }
19692e8d 2788
9b387841
NC
2789 if (!entry)
2790 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
12578ffb 2791 "Attempt to free nonexistent shared string '%s'%s"
9b387841
NC
2792 pTHX__FORMAT,
2793 hek ? HEK_KEY(hek) : str,
2794 ((k_flags & HVhek_UTF8) ? " (utf8)" : "") pTHX__VALUE);
19692e8d
NC
2795 if (k_flags & HVhek_FREEKEY)
2796 Safefree(str);
fde52b5c 2797}
2798
bbce6d69 2799/* get a (constant) string ptr from the global string table
2800 * string will get added if it is not already there.
fde52b5c 2801 * len and hash must both be valid for str.
2802 */
bbce6d69 2803HEK *
5aaab254 2804Perl_share_hek(pTHX_ const char *str, I32 len, U32 hash)
fde52b5c 2805{
da58a35d 2806 bool is_utf8 = FALSE;
19692e8d 2807 int flags = 0;
aec46f14 2808 const char * const save = str;
da58a35d 2809
7918f24d
NC
2810 PERL_ARGS_ASSERT_SHARE_HEK;
2811
da58a35d 2812 if (len < 0) {
77caf834 2813 STRLEN tmplen = -len;
da58a35d 2814 is_utf8 = TRUE;
77caf834
JH
2815 /* See the note in hv_fetch(). --jhi */
2816 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2817 len = tmplen;
19692e8d
NC
2818 /* If we were able to downgrade here, then than means that we were passed
2819 in a key which only had chars 0-255, but was utf8 encoded. */
2820 if (is_utf8)
2821 flags = HVhek_UTF8;
2822 /* If we found we were able to downgrade the string to bytes, then
2823 we should flag that it needs upgrading on keys or each. Also flag
2824 that we need share_hek_flags to free the string. */
4643eb69 2825 if (str != save) {
c2587955 2826 dVAR;
4643eb69 2827 PERL_HASH(hash, str, len);
19692e8d 2828 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
4643eb69 2829 }
19692e8d
NC
2830 }
2831
6e838c70 2832 return share_hek_flags (str, len, hash, flags);
19692e8d
NC
2833}
2834
6e838c70 2835STATIC HEK *
5aaab254 2836S_share_hek_flags(pTHX_ const char *str, I32 len, U32 hash, int flags)
19692e8d 2837{
eb578fdb 2838 HE *entry;
35a4481c 2839 const int flags_masked = flags & HVhek_MASK;
263cb4a6 2840 const U32 hindex = hash & (I32) HvMAX(PL_strtab);
eb578fdb 2841 XPVHV * const xhv = (XPVHV*)SvANY(PL_strtab);
7918f24d
NC
2842
2843 PERL_ARGS_ASSERT_SHARE_HEK_FLAGS;
bbce6d69 2844
fde52b5c 2845 /* what follows is the moral equivalent of:
1c846c1f 2846
6b88bc9c 2847 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
a0714e2c 2848 hv_store(PL_strtab, str, len, NULL, hash);
fdcd69b6
NC
2849
2850 Can't rehash the shared string table, so not sure if it's worth
2851 counting the number of entries in the linked list
bbce6d69 2852 */
7918f24d 2853
fde52b5c 2854 /* assert(xhv_array != 0) */
263cb4a6
NC
2855 entry = (HvARRAY(PL_strtab))[hindex];
2856 for (;entry; entry = HeNEXT(entry)) {
fde52b5c 2857 if (HeHASH(entry) != hash) /* strings can't be equal */
2858 continue;
2859 if (HeKLEN(entry) != len)
2860 continue;
1c846c1f 2861 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
fde52b5c 2862 continue;
19692e8d 2863 if (HeKFLAGS(entry) != flags_masked)
c3654f1a 2864 continue;
fde52b5c 2865 break;
2866 }
263cb4a6
NC
2867
2868 if (!entry) {
45d1cc86
NC
2869 /* What used to be head of the list.
2870 If this is NULL, then we're the first entry for this slot, which
2871 means we need to increate fill. */
cbae3960
NC
2872 struct shared_he *new_entry;
2873 HEK *hek;
2874 char *k;
263cb4a6
NC
2875 HE **const head = &HvARRAY(PL_strtab)[hindex];
2876 HE *const next = *head;
cbae3960
NC
2877
2878 /* We don't actually store a HE from the arena and a regular HEK.
2879 Instead we allocate one chunk of memory big enough for both,
2880 and put the HEK straight after the HE. This way we can find the
f52337cf 2881 HE directly from the HEK.
cbae3960
NC
2882 */
2883
a02a5408 2884 Newx(k, STRUCT_OFFSET(struct shared_he,
cbae3960
NC
2885 shared_he_hek.hek_key[0]) + len + 2, char);
2886 new_entry = (struct shared_he *)k;
2887 entry = &(new_entry->shared_he_he);
2888 hek = &(new_entry->shared_he_hek);
2889
2890 Copy(str, HEK_KEY(hek), len, char);
2891 HEK_KEY(hek)[len] = 0;
2892 HEK_LEN(hek) = len;
2893 HEK_HASH(hek) = hash;
2894 HEK_FLAGS(hek) = (unsigned char)flags_masked;
2895
2896 /* Still "point" to the HEK, so that other code need not know what
2897 we're up to. */
2898 HeKEY_hek(entry) = hek;
de616631 2899 entry->he_valu.hent_refcount = 0;
263cb4a6
NC
2900 HeNEXT(entry) = next;
2901 *head = entry;
cbae3960 2902
4c7185a0 2903 xhv->xhv_keys++; /* HvTOTALKEYS(hv)++ */
263cb4a6 2904 if (!next) { /* initial entry? */
8e317198 2905 } else if ( DO_HSPLIT(xhv) ) {
adf6906b
NC
2906 const STRLEN oldsize = xhv->xhv_max + 1;
2907 hsplit(PL_strtab, oldsize, oldsize * 2);
bbce6d69 2908 }
2909 }
2910
de616631 2911 ++entry->he_valu.hent_refcount;
19692e8d
NC
2912
2913 if (flags & HVhek_FREEKEY)
f9a63242 2914 Safefree(str);
19692e8d 2915
6e838c70 2916 return HeKEY_hek(entry);
fde52b5c 2917}
ecae49c0 2918
6174b39a 2919SSize_t *
ca732855
NC
2920Perl_hv_placeholders_p(pTHX_ HV *hv)
2921{
ad64d0ec 2922 MAGIC *mg = mg_find((const SV *)hv, PERL_MAGIC_rhash);
ca732855 2923
7918f24d
NC
2924 PERL_ARGS_ASSERT_HV_PLACEHOLDERS_P;
2925
ca732855 2926 if (!mg) {
ad64d0ec 2927 mg = sv_magicext(MUTABLE_SV(hv), 0, PERL_MAGIC_rhash, 0, 0, 0);
ca732855
NC
2928
2929 if (!mg) {
2930 Perl_die(aTHX_ "panic: hv_placeholders_p");
2931 }
2932 }
2933 return &(mg->mg_len);
2934}
2935
2936
2937I32
0c289d13 2938Perl_hv_placeholders_get(pTHX_ const HV *hv)
ca732855 2939{
0c289d13 2940 MAGIC * const mg = mg_find((const SV *)hv, PERL_MAGIC_rhash);
ca732855 2941
7918f24d 2942 PERL_ARGS_ASSERT_HV_PLACEHOLDERS_GET;
23491f1d 2943 PERL_UNUSED_CONTEXT;
7918f24d 2944
ca732855
NC
2945 return mg ? mg->mg_len : 0;
2946}
2947
2948void
ac1e784a 2949Perl_hv_placeholders_set(pTHX_ HV *hv, I32 ph)
ca732855 2950{
ad64d0ec 2951 MAGIC * const mg = mg_find((const SV *)hv, PERL_MAGIC_rhash);
ca732855 2952
7918f24d
NC
2953 PERL_ARGS_ASSERT_HV_PLACEHOLDERS_SET;
2954
ca732855
NC
2955 if (mg) {
2956 mg->mg_len = ph;
2957 } else if (ph) {
ad64d0ec 2958 if (!sv_magicext(MUTABLE_SV(hv), 0, PERL_MAGIC_rhash, 0, 0, ph))
ca732855
NC
2959 Perl_die(aTHX_ "panic: hv_placeholders_set");
2960 }
2961 /* else we don't need to add magic to record 0 placeholders. */
2962}
ecae49c0 2963
2a49f0f5 2964STATIC SV *
7b0bddfa
NC
2965S_refcounted_he_value(pTHX_ const struct refcounted_he *he)
2966{
0b2d3faa 2967 dVAR;
7b0bddfa 2968 SV *value;
7918f24d
NC
2969
2970 PERL_ARGS_ASSERT_REFCOUNTED_HE_VALUE;
2971
7b0bddfa
NC
2972 switch(he->refcounted_he_data[0] & HVrhek_typemask) {
2973 case HVrhek_undef:
2974 value = newSV(0);
2975 break;
2976 case HVrhek_delete:
2977 value = &PL_sv_placeholder;
2978 break;
2979 case HVrhek_IV:
44ebaf21
NC
2980 value = newSViv(he->refcounted_he_val.refcounted_he_u_iv);
2981 break;
2982 case HVrhek_UV:
2983 value = newSVuv(he->refcounted_he_val.refcounted_he_u_uv);
7b0bddfa
NC
2984 break;
2985 case HVrhek_PV:
44ebaf21 2986 case HVrhek_PV_UTF8:
7b0bddfa
NC
2987 /* Create a string SV that directly points to the bytes in our
2988 structure. */
b9f83d2f 2989 value = newSV_type(SVt_PV);
7b0bddfa
NC
2990 SvPV_set(value, (char *) he->refcounted_he_data + 1);
2991 SvCUR_set(value, he->refcounted_he_val.refcounted_he_u_len);
2992 /* This stops anything trying to free it */
2993 SvLEN_set(value, 0);
2994 SvPOK_on(value);
2995 SvREADONLY_on(value);
44ebaf21 2996 if ((he->refcounted_he_data[0] & HVrhek_typemask) == HVrhek_PV_UTF8)
7b0bddfa
NC
2997 SvUTF8_on(value);
2998 break;
2999 default:
20439bc7
Z
3000 Perl_croak(aTHX_ "panic: refcounted_he_value bad flags %"UVxf,
3001 (UV)he->refcounted_he_data[0]);
7b0bddfa
NC
3002 }
3003 return value;
3004}
3005
ecae49c0 3006/*
20439bc7 3007=for apidoc m|HV *|refcounted_he_chain_2hv|const struct refcounted_he *c|U32 flags
8dff4fc5 3008
20439bc7
Z
3009Generates and returns a C<HV *> representing the content of a
3010C<refcounted_he> chain.
3011I<flags> is currently unused and must be zero.
8dff4fc5
BM
3012
3013=cut
3014*/
3015HV *
20439bc7 3016Perl_refcounted_he_chain_2hv(pTHX_ const struct refcounted_he *chain, U32 flags)
8dff4fc5 3017{
20439bc7
Z
3018 dVAR;
3019 HV *hv;
3020 U32 placeholders, max;
b3ca2e83 3021
20439bc7
Z
3022 if (flags)
3023 Perl_croak(aTHX_ "panic: refcounted_he_chain_2hv bad flags %"UVxf,
3024 (UV)flags);
b3ca2e83 3025
b3ca2e83
NC
3026 /* We could chase the chain once to get an idea of the number of keys,
3027 and call ksplit. But for now we'll make a potentially inefficient
3028 hash with only 8 entries in its array. */
20439bc7
Z
3029 hv = newHV();
3030 max = HvMAX(hv);
b3ca2e83
NC
3031 if (!HvARRAY(hv)) {
3032 char *array;
3033 Newxz(array, PERL_HV_ARRAY_ALLOC_BYTES(max + 1), char);
3034 HvARRAY(hv) = (HE**)array;
3035 }
3036
20439bc7 3037 placeholders = 0;
b3ca2e83 3038 while (chain) {
cbb1fbea 3039#ifdef USE_ITHREADS
b6bbf3fa 3040 U32 hash = chain->refcounted_he_hash;
cbb1fbea
NC
3041#else
3042 U32 hash = HEK_HASH(chain->refcounted_he_hek);
3043#endif
b3ca2e83
NC
3044 HE **oentry = &((HvARRAY(hv))[hash & max]);
3045 HE *entry = *oentry;
b6bbf3fa 3046 SV *value;
cbb1fbea 3047
b3ca2e83
NC
3048 for (; entry; entry = HeNEXT(entry)) {
3049 if (HeHASH(entry) == hash) {
9f769845
NC
3050 /* We might have a duplicate key here. If so, entry is older
3051 than the key we've already put in the hash, so if they are
3052 the same, skip adding entry. */
3053#ifdef USE_ITHREADS
3054 const STRLEN klen = HeKLEN(entry);
3055 const char *const key = HeKEY(entry);
3056 if (klen == chain->refcounted_he_keylen
3057 && (!!HeKUTF8(entry)
3058 == !!(chain->refcounted_he_data[0] & HVhek_UTF8))
3059 && memEQ(key, REF_HE_KEY(chain), klen))
3060 goto next_please;
3061#else
3062 if (HeKEY_hek(entry) == chain->refcounted_he_hek)
3063 goto next_please;
3064 if (HeKLEN(entry) == HEK_LEN(chain->refcounted_he_hek)
3065 && HeKUTF8(entry) == HEK_UTF8(chain->refcounted_he_hek)
3066 && memEQ(HeKEY(entry), HEK_KEY(chain->refcounted_he_hek),
3067 HeKLEN(entry)))
3068 goto next_please;
3069#endif
b3ca2e83
NC
3070 }
3071 }
3072 assert (!entry);
3073 entry = new_HE();
3074
cbb1fbea
NC
3075#ifdef USE_ITHREADS
3076 HeKEY_hek(entry)
7b0bddfa 3077 = share_hek_flags(REF_HE_KEY(chain),
b6bbf3fa
NC
3078 chain->refcounted_he_keylen,
3079 chain->refcounted_he_hash,
3080 (chain->refcounted_he_data[0]
3081 & (HVhek_UTF8|HVhek_WASUTF8)));
cbb1fbea 3082#else
71ad1b0c 3083 HeKEY_hek(entry) = share_hek_hek(chain->refcounted_he_hek);
cbb1fbea 3084#endif
7b0bddfa
NC
3085 value = refcounted_he_value(chain);
3086 if (value == &PL_sv_placeholder)
b3ca2e83 3087 placeholders++;
b6bbf3fa 3088 HeVAL(entry) = value;
b3ca2e83
NC
3089
3090 /* Link it into the chain. */
3091 HeNEXT(entry) = *oentry;
b3ca2e83
NC
3092 *oentry = entry;
3093
3094 HvTOTALKEYS(hv)++;
3095
3096 next_please:
71ad1b0c 3097 chain = chain->refcounted_he_next;
b3ca2e83
NC
3098 }
3099
3100 if (placeholders) {
3101 clear_placeholders(hv, placeholders);
3102 HvTOTALKEYS(hv) -= placeholders;
3103 }
3104
3105 /* We could check in the loop to see if we encounter any keys with key
3106 flags, but it's probably not worth it, as this per-hash flag is only
3107 really meant as an optimisation for things like Storable. */
3108 HvHASKFLAGS_on(hv);
def9038f 3109 DEBUG_A(Perl_hv_assert(aTHX_ hv));
b3ca2e83
NC
3110
3111 return hv;
3112}
3113
20439bc7
Z
3114/*
3115=for apidoc m|SV *|refcounted_he_fetch_pvn|const struct refcounted_he *chain|const char *keypv|STRLEN keylen|U32 hash|U32 flags
3116
3117Search along a C<refcounted_he> chain for an entry with the key specified
3118by I<keypv> and I<keylen>. If I<flags> has the C<REFCOUNTED_HE_KEY_UTF8>
3119bit set, the key octets are interpreted as UTF-8, otherwise they
3120are interpreted as Latin-1. I<hash> is a precomputed hash of the key
3121string, or zero if it has not been precomputed. Returns a mortal scalar
3122representing the value associated with the key, or C<&PL_sv_placeholder>
3123if there is no value associated with the key.
3124
3125=cut
3126*/
3127
7b0bddfa 3128SV *
20439bc7
Z
3129Perl_refcounted_he_fetch_pvn(pTHX_ const struct refcounted_he *chain,
3130 const char *keypv, STRLEN keylen, U32 hash, U32 flags)
7b0bddfa 3131{
0b2d3faa 3132 dVAR;
20439bc7
Z
3133 U8 utf8_flag;
3134 PERL_ARGS_ASSERT_REFCOUNTED_HE_FETCH_PVN;
7b0bddfa 3135
94250aee 3136 if (flags & ~(REFCOUNTED_HE_KEY_UTF8|REFCOUNTED_HE_EXISTS))
20439bc7
Z
3137 Perl_croak(aTHX_ "panic: refcounted_he_fetch_pvn bad flags %"UVxf,
3138 (UV)flags);
3139 if (!chain)
3140 return &PL_sv_placeholder;
3141 if (flags & REFCOUNTED_HE_KEY_UTF8) {
3142 /* For searching purposes, canonicalise to Latin-1 where possible. */
3143 const char *keyend = keypv + keylen, *p;
3144 STRLEN nonascii_count = 0;
3145 for (p = keypv; p != keyend; p++) {
e8e5e5b3
KW
3146 if (! UTF8_IS_INVARIANT(*p)) {
3147 if (! UTF8_IS_NEXT_CHAR_DOWNGRADEABLE(p, keyend)) {
20439bc7 3148 goto canonicalised_key;
e8e5e5b3 3149 }
20439bc7 3150 nonascii_count++;
e8e5e5b3 3151 p++;
20439bc7 3152 }
cd1d2f8a 3153 }
20439bc7
Z
3154 if (nonascii_count) {
3155 char *q;
3156 const char *p = keypv, *keyend = keypv + keylen;
3157 keylen -= nonascii_count;
3158 Newx(q, keylen, char);
3159 SAVEFREEPV(q);
3160 keypv = q;
3161 for (; p != keyend; p++, q++) {
3162 U8 c = (U8)*p;
e8e5e5b3
KW
3163 if (UTF8_IS_INVARIANT(c)) {
3164 *q = (char) c;
3165 }
3166 else {
3167 p++;
3168 *q = (char) TWO_BYTE_UTF8_TO_NATIVE(c, *p);
3169 }
cd1d2f8a
NC
3170 }
3171 }
20439bc7
Z
3172 flags &= ~REFCOUNTED_HE_KEY_UTF8;
3173 canonicalised_key: ;
3174 }
3175 utf8_flag = (flags & REFCOUNTED_HE_KEY_UTF8) ? HVhek_UTF8 : 0;
3176 if (!hash)
3177 PERL_HASH(hash, keypv, keylen);
7b0bddfa 3178
20439bc7
Z
3179 for (; chain; chain = chain->refcounted_he_next) {
3180 if (
7b0bddfa 3181#ifdef USE_ITHREADS
20439bc7
Z
3182 hash == chain->refcounted_he_hash &&
3183 keylen == chain->refcounted_he_keylen &&
3184 memEQ(REF_HE_KEY(chain), keypv, keylen) &&
3185 utf8_flag == (chain->refcounted_he_data[0] & HVhek_UTF8)
7b0bddfa 3186#else
20439bc7
Z
3187 hash == HEK_HASH(chain->refcounted_he_hek) &&
3188 keylen == (STRLEN)HEK_LEN(chain->refcounted_he_hek) &&
3189 memEQ(HEK_KEY(chain->refcounted_he_hek), keypv, keylen) &&
3190 utf8_flag == (HEK_FLAGS(chain->refcounted_he_hek) & HVhek_UTF8)
7b0bddfa 3191#endif
ef8156f5
NC
3192 ) {
3193 if (flags & REFCOUNTED_HE_EXISTS)
3194 return (chain->refcounted_he_data[0] & HVrhek_typemask)
3195 == HVrhek_delete
3196 ? NULL : &PL_sv_yes;
3197 return sv_2mortal(refcounted_he_value(chain));
3198 }
94250aee
FC
3199 }
3200 return flags & REFCOUNTED_HE_EXISTS ? NULL : &PL_sv_placeholder;
20439bc7 3201}
7b0bddfa 3202
20439bc7
Z
3203/*
3204=for apidoc m|SV *|refcounted_he_fetch_pv|const struct refcounted_he *chain|const char *key|U32 hash|U32 flags
7b0bddfa 3205
20439bc7
Z
3206Like L</refcounted_he_fetch_pvn>, but takes a nul-terminated string
3207instead of a string/length pair.
3208
3209=cut
3210*/
3211
3212SV *
3213Perl_refcounted_he_fetch_pv(pTHX_ const struct refcounted_he *chain,
3214 const char *key, U32 hash, U32 flags)
3215{
3216 PERL_ARGS_ASSERT_REFCOUNTED_HE_FETCH_PV;
3217 return refcounted_he_fetch_pvn(chain, key, strlen(key), hash, flags);
7b0bddfa
NC
3218}
3219
b3ca2e83 3220/*
20439bc7
Z
3221=for apidoc m|SV *|refcounted_he_fetch_sv|const struct refcounted_he *chain|SV *key|U32 hash|U32 flags
3222
3223Like L</refcounted_he_fetch_pvn>, but takes a Perl scalar instead of a
3224string/length pair.
3225
3226=cut
3227*/
b3ca2e83 3228
20439bc7
Z
3229SV *
3230Perl_refcounted_he_fetch_sv(pTHX_ const struct refcounted_he *chain,
3231 SV *key, U32 hash, U32 flags)
3232{
3233 const char *keypv;
3234 STRLEN keylen;
3235 PERL_ARGS_ASSERT_REFCOUNTED_HE_FETCH_SV;
3236 if (flags & REFCOUNTED_HE_KEY_UTF8)
3237 Perl_croak(aTHX_ "panic: refcounted_he_fetch_sv bad flags %"UVxf,
3238 (UV)flags);
3239 keypv = SvPV_const(key, keylen);
3240 if (SvUTF8(key))
3241 flags |= REFCOUNTED_HE_KEY_UTF8;
3242 if (!hash && SvIsCOW_shared_hash(key))
3243 hash = SvSHARED_HASH(key);
3244 return refcounted_he_fetch_pvn(chain, keypv, keylen, hash, flags);
3245}
3246
3247/*
3248=for apidoc m|struct refcounted_he *|refcounted_he_new_pvn|struct refcounted_he *parent|const char *keypv|STRLEN keylen|U32 hash|SV *value|U32 flags
3249
3250Creates a new C<refcounted_he>. This consists of a single key/value
3251pair and a reference to an existing C<refcounted_he> chain (which may
3252be empty), and thus forms a longer chain. When using the longer chain,
3253the new key/value pair takes precedence over any entry for the same key
3254further along the chain.
3255
3256The new key is specified by I<keypv> and I<keylen>. If I<flags> has
3257the C<REFCOUNTED_HE_KEY_UTF8> bit set, the key octets are interpreted
3258as UTF-8, otherwise they are interpreted as Latin-1. I<hash> is
3259a precomputed hash of the key string, or zero if it has not been
3260precomputed.
3261
3262I<value> is the scalar value to store for this key. I<value> is copied
3263by this function, which thus does not take ownership of any reference
3264to it, and later changes to the scalar will not be reflected in the
3265value visible in the C<refcounted_he>. Complex types of scalar will not
3266be stored with referential integrity, but will be coerced to strings.
3267I<value> may be either null or C<&PL_sv_placeholder> to indicate that no
3268value is to be associated with the key; this, as with any non-null value,
3269takes precedence over the existence of a value for the key further along
3270the chain.
3271
3272I<parent> points to the rest of the C<refcounted_he> chain to be
3273attached to the new C<refcounted_he>. This function takes ownership
3274of one reference to I<parent>, and returns one reference to the new
3275C<refcounted_he>.
b3ca2e83
NC
3276
3277=cut
3278*/
3279
3280struct refcounted_he *
20439bc7
Z
3281Perl_refcounted_he_new_pvn(pTHX_ struct refcounted_he *parent,
3282 const char *keypv, STRLEN keylen, U32 hash, SV *value, U32 flags)
3283{
7a89be66 3284 dVAR;
b6bbf3fa 3285 STRLEN value_len = 0;
95b63a38 3286 const char *value_p = NULL;
20439bc7 3287 bool is_pv;
b6bbf3fa 3288 char value_type;
20439bc7
Z
3289 char hekflags;
3290 STRLEN key_offset = 1;
3291 struct refcounted_he *he;
3292 PERL_ARGS_ASSERT_REFCOUNTED_HE_NEW_PVN;
b6bbf3fa 3293
20439bc7
Z
3294 if (!value || value == &PL_sv_placeholder) {
3295 value_type = HVrhek_delete;
3296 } else if (SvPOK(value)) {
b6bbf3fa
NC
3297 value_type = HVrhek_PV;
3298 } else if (SvIOK(value)) {
ad64d0ec 3299 value_type = SvUOK((const SV *)value) ? HVrhek_UV : HVrhek_IV;
b6bbf3fa
NC
3300 } else if (!SvOK(value)) {
3301 value_type = HVrhek_undef;
3302 } else {
3303 value_type = HVrhek_PV;
3304 }
20439bc7
Z
3305 is_pv = value_type == HVrhek_PV;
3306 if (is_pv) {
012da8e5
NC
3307 /* Do it this way so that the SvUTF8() test is after the SvPV, in case
3308 the value is overloaded, and doesn't yet have the UTF-8flag set. */
b6bbf3fa 3309 value_p = SvPV_const(value, value_len);
012da8e5
NC
3310 if (SvUTF8(value))
3311 value_type = HVrhek_PV_UTF8;
20439bc7
Z
3312 key_offset = value_len + 2;
3313 }
3314 hekflags = value_type;
3315
3316 if (flags & REFCOUNTED_HE_KEY_UTF8) {
3317 /* Canonicalise to Latin-1 where possible. */
3318 const char *keyend = keypv + keylen, *p;
3319 STRLEN nonascii_count = 0;
3320 for (p = keypv; p != keyend; p++) {
e8e5e5b3
KW
3321 if (! UTF8_IS_INVARIANT(*p)) {
3322 if (! UTF8_IS_NEXT_CHAR_DOWNGRADEABLE(p, keyend)) {
20439bc7 3323 goto canonicalised_key;
e8e5e5b3 3324 }
20439bc7 3325 nonascii_count++;
e8e5e5b3 3326 p++;
20439bc7
Z
3327 }
3328 }
3329 if (nonascii_count) {
3330 char *q;
3331 const char *p = keypv, *keyend = keypv + keylen;
3332 keylen -= nonascii_count;
3333 Newx(q, keylen, char);
3334 SAVEFREEPV(q);
3335 keypv = q;
3336 for (; p != keyend; p++, q++) {
3337 U8 c = (U8)*p;
e8e5e5b3
KW
3338 if (UTF8_IS_INVARIANT(c)) {
3339 *q = (char) c;
3340 }
3341 else {
3342 p++;
3343 *q = (char) TWO_BYTE_UTF8_TO_NATIVE(c, *p);
3344 }
20439bc7
Z
3345 }
3346 }
3347 flags &= ~REFCOUNTED_HE_KEY_UTF8;
3348 canonicalised_key: ;
b6bbf3fa 3349 }
20439bc7
Z
3350 if (flags & REFCOUNTED_HE_KEY_UTF8)
3351 hekflags |= HVhek_UTF8;
3352 if (!hash)
3353 PERL_HASH(hash, keypv, keylen);
012da8e5 3354
0de694c5 3355#ifdef USE_ITHREADS
10edeb5d
JH
3356 he = (struct refcounted_he*)
3357 PerlMemShared_malloc(sizeof(struct refcounted_he) - 1
20439bc7 3358 + keylen
20439bc7 3359 + key_offset);
0de694c5
NC
3360#else
3361 he = (struct refcounted_he*)
3362 PerlMemShared_malloc(sizeof(struct refcounted_he) - 1
3363 + key_offset);
3364#endif
b3ca2e83 3365
71ad1b0c 3366 he->refcounted_he_next = parent;
b6bbf3fa 3367
012da8e5 3368 if (is_pv) {
20439bc7 3369 Copy(value_p, he->refcounted_he_data + 1, value_len + 1, char);
b6bbf3fa 3370 he->refcounted_he_val.refcounted_he_u_len = value_len;
b6bbf3fa 3371 } else if (value_type == HVrhek_IV) {
20439bc7 3372 he->refcounted_he_val.refcounted_he_u_iv = SvIVX(value);
012da8e5 3373 } else if (value_type == HVrhek_UV) {
20439bc7 3374 he->refcounted_he_val.refcounted_he_u_uv = SvUVX(value);
b6bbf3fa
NC
3375 }
3376
cbb1fbea 3377#ifdef USE_ITHREADS
b6bbf3fa 3378 he->refcounted_he_hash = hash;
20439bc7
Z
3379 he->refcounted_he_keylen = keylen;
3380 Copy(keypv, he->refcounted_he_data + key_offset, keylen, char);
cbb1fbea 3381#else
20439bc7 3382 he->refcounted_he_hek = share_hek_flags(keypv, keylen, hash, hekflags);
cbb1fbea 3383#endif
b6bbf3fa 3384
20439bc7 3385 he->refcounted_he_data[0] = hekflags;
b3ca2e83
NC
3386 he->refcounted_he_refcnt = 1;
3387
3388 return he;
3389}
3390
3391/*
20439bc7 3392=for apidoc m|struct refcounted_he *|refcounted_he_new_pv|struct refcounted_he *parent|const char *key|U32 hash|SV *value|U32 flags
b3ca2e83 3393
20439bc7
Z
3394Like L</refcounted_he_new_pvn>, but takes a nul-terminated string instead
3395of a string/length pair.
3396
3397=cut
3398*/
3399
3400struct refcounted_he *
3401Perl_refcounted_he_new_pv(pTHX_ struct refcounted_he *parent,
3402 const char *key, U32 hash, SV *value, U32 flags)
3403{
3404 PERL_ARGS_ASSERT_REFCOUNTED_HE_NEW_PV;
3405 return refcounted_he_new_pvn(parent, key, strlen(key), hash, value, flags);
3406}
3407
3408/*
3409=for apidoc m|struct refcounted_he *|refcounted_he_new_sv|struct refcounted_he *parent|SV *key|U32 hash|SV *value|U32 flags
3410
3411Like L</refcounted_he_new_pvn>, but takes a Perl scalar instead of a
3412string/length pair.
3413
3414=cut
3415*/
3416
3417struct refcounted_he *
3418Perl_refcounted_he_new_sv(pTHX_ struct refcounted_he *parent,
3419 SV *key, U32 hash, SV *value, U32 flags)
3420{
3421 const char *keypv;
3422 STRLEN keylen;
3423 PERL_ARGS_ASSERT_REFCOUNTED_HE_NEW_SV;
3424 if (flags & REFCOUNTED_HE_KEY_UTF8)
3425 Perl_croak(aTHX_ "panic: refcounted_he_new_sv bad flags %"UVxf,
3426 (UV)flags);
3427 keypv = SvPV_const(key, keylen);
3428 if (SvUTF8(key))
3429 flags |= REFCOUNTED_HE_KEY_UTF8;
3430 if (!hash && SvIsCOW_shared_hash(key))
3431 hash = SvSHARED_HASH(key);
3432 return refcounted_he_new_pvn(parent, keypv, keylen, hash, value, flags);
3433}
3434
3435/*
3436=for apidoc m|void|refcounted_he_free|struct refcounted_he *he
3437
3438Decrements the reference count of a C<refcounted_he> by one. If the
3439reference count reaches zero the structure's memory is freed, which
3440(recursively) causes a reduction of its parent C<refcounted_he>'s
3441reference count. It is safe to pass a null pointer to this function:
3442no action occurs in this case.
b3ca2e83
NC
3443
3444=cut
3445*/
3446
3447void
3448Perl_refcounted_he_free(pTHX_ struct refcounted_he *he) {
20b7effb 3449#ifdef USE_ITHREADS
53d44271 3450 dVAR;
20b7effb 3451#endif
57ca3b03
AL
3452 PERL_UNUSED_CONTEXT;
3453
b3ca2e83
NC
3454 while (he) {
3455 struct refcounted_he *copy;
cbb1fbea 3456 U32 new_count;
b3ca2e83 3457
cbb1fbea
NC
3458 HINTS_REFCNT_LOCK;
3459 new_count = --he->refcounted_he_refcnt;
3460 HINTS_REFCNT_UNLOCK;
3461
3462 if (new_count) {
b3ca2e83 3463 return;
cbb1fbea 3464 }
b3ca2e83 3465
b6bbf3fa 3466#ifndef USE_ITHREADS
71ad1b0c 3467 unshare_hek_or_pvn (he->refcounted_he_hek, 0, 0, 0);
cbb1fbea 3468#endif
b3ca2e83 3469 copy = he;
71ad1b0c 3470 he = he->refcounted_he_next;
b6bbf3fa 3471 PerlMemShared_free(copy);
b3ca2e83
NC
3472 }
3473}
3474
20439bc7
Z
3475/*
3476=for apidoc m|struct refcounted_he *|refcounted_he_inc|struct refcounted_he *he
3477
3478Increment the reference count of a C<refcounted_he>. The pointer to the
3479C<refcounted_he> is also returned. It is safe to pass a null pointer
3480to this function: no action occurs and a null pointer is returned.
3481
3482=cut
3483*/
3484
3485struct refcounted_he *
3486Perl_refcounted_he_inc(pTHX_ struct refcounted_he *he)
3487{
20b7effb 3488#ifdef USE_ITHREADS
09ddd873 3489 dVAR;
20b7effb 3490#endif
dc3bf405 3491 PERL_UNUSED_CONTEXT;
20439bc7
Z
3492 if (he) {
3493 HINTS_REFCNT_LOCK;
3494 he->refcounted_he_refcnt++;
3495 HINTS_REFCNT_UNLOCK;
3496 }
3497 return he;
3498}
3499
8375c93e 3500/*
aebc0cbe 3501=for apidoc cop_fetch_label
8375c93e
RU
3502
3503Returns the label attached to a cop.
3504The flags pointer may be set to C<SVf_UTF8> or 0.
3505
3506=cut
3507*/
3508
47550813
NC
3509/* pp_entereval is aware that labels are stored with a key ':' at the top of
3510 the linked list. */
dca6062a 3511const char *
aebc0cbe 3512Perl_cop_fetch_label(pTHX_ COP *const cop, STRLEN *len, U32 *flags) {
d6747b7a
NC
3513 struct refcounted_he *const chain = cop->cop_hints_hash;
3514
aebc0cbe 3515 PERL_ARGS_ASSERT_COP_FETCH_LABEL;
dc3bf405 3516 PERL_UNUSED_CONTEXT;
d6747b7a 3517
dca6062a
NC
3518 if (!chain)
3519 return NULL;
3520#ifdef USE_ITHREADS
3521 if (chain->refcounted_he_keylen != 1)
3522 return NULL;
3523 if (*REF_HE_KEY(chain) != ':')
3524 return NULL;
3525#else
3526 if ((STRLEN)HEK_LEN(chain->refcounted_he_hek) != 1)
3527 return NULL;
3528 if (*HEK_KEY(chain->refcounted_he_hek) != ':')
3529 return NULL;
3530#endif
012da8e5
NC
3531 /* Stop anyone trying to really mess us up by adding their own value for
3532 ':' into %^H */
3533 if ((chain->refcounted_he_data[0] & HVrhek_typemask) != HVrhek_PV
3534 && (chain->refcounted_he_data[0] & HVrhek_typemask) != HVrhek_PV_UTF8)
3535 return NULL;
3536
dca6062a
NC
3537 if (len)
3538 *len = chain->refcounted_he_val.refcounted_he_u_len;
3539 if (flags) {
3540 *flags = ((chain->refcounted_he_data[0] & HVrhek_typemask)
3541 == HVrhek_PV_UTF8) ? SVf_UTF8 : 0;
3542 }
3543 return chain->refcounted_he_data + 1;
3544}
3545
8375c93e 3546/*
aebc0cbe 3547=for apidoc cop_store_label
8375c93e 3548
72d33970
FC
3549Save a label into a C<cop_hints_hash>.
3550You need to set flags to C<SVf_UTF8>
8375c93e
RU
3551for a utf-8 label.
3552
3553=cut
3554*/
3555
a77ac40c 3556void
aebc0cbe 3557Perl_cop_store_label(pTHX_ COP *const cop, const char *label, STRLEN len,
a77ac40c 3558 U32 flags)
012da8e5 3559{
20439bc7 3560 SV *labelsv;
aebc0cbe 3561 PERL_ARGS_ASSERT_COP_STORE_LABEL;
547bb267 3562
a77ac40c 3563 if (flags & ~(SVf_UTF8))
aebc0cbe 3564 Perl_croak(aTHX_ "panic: cop_store_label illegal flag bits 0x%" UVxf,
a77ac40c 3565 (UV)flags);
a3179684 3566 labelsv = newSVpvn_flags(label, len, SVs_TEMP);
20439bc7
Z
3567 if (flags & SVf_UTF8)
3568 SvUTF8_on(labelsv);
a77ac40c 3569 cop->cop_hints_hash
20439bc7 3570 = refcounted_he_new_pvs(cop->cop_hints_hash, ":", labelsv, 0);
012da8e5
NC
3571}
3572
b3ca2e83 3573/*
ecae49c0
NC
3574=for apidoc hv_assert
3575
3576Check that a hash is in an internally consistent state.
3577
3578=cut
3579*/
3580
943795c2
NC
3581#ifdef DEBUGGING
3582
ecae49c0
NC
3583void
3584Perl_hv_assert(pTHX_ HV *hv)
3585{
57ca3b03
AL
3586 dVAR;
3587 HE* entry;
3588 int withflags = 0;
3589 int placeholders = 0;
3590 int real = 0;
3591 int bad = 0;
3592 const I32 riter = HvRITER_get(hv);
3593 HE *eiter = HvEITER_get(hv);
3594
7918f24d
NC
3595 PERL_ARGS_ASSERT_HV_ASSERT;
3596
57ca3b03
AL
3597 (void)hv_iterinit(hv);
3598
3599 while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))) {
3600 /* sanity check the values */
3601 if (HeVAL(entry) == &PL_sv_placeholder)
3602 placeholders++;
3603 else
3604 real++;
3605 /* sanity check the keys */
3606 if (HeSVKEY(entry)) {
6f207bd3 3607 NOOP; /* Don't know what to check on SV keys. */
57ca3b03
AL
3608 } else if (HeKUTF8(entry)) {
3609 withflags++;
3610 if (HeKWASUTF8(entry)) {
3611 PerlIO_printf(Perl_debug_log,
d2a455e7 3612 "hash key has both WASUTF8 and UTF8: '%.*s'\n",
57ca3b03
AL
3613 (int) HeKLEN(entry), HeKEY(entry));
3614 bad = 1;
3615 }
3616 } else if (HeKWASUTF8(entry))
3617 withflags++;
3618 }
ad64d0ec 3619 if (!SvTIED_mg((const SV *)hv, PERL_MAGIC_tied)) {
57ca3b03
AL
3620 static const char bad_count[] = "Count %d %s(s), but hash reports %d\n";
3621 const int nhashkeys = HvUSEDKEYS(hv);
3622 const int nhashplaceholders = HvPLACEHOLDERS_get(hv);
3623
3624 if (nhashkeys != real) {
3625 PerlIO_printf(Perl_debug_log, bad_count, real, "keys", nhashkeys );
3626 bad = 1;
3627 }
3628 if (nhashplaceholders != placeholders) {
3629 PerlIO_printf(Perl_debug_log, bad_count, placeholders, "placeholder", nhashplaceholders );
3630 bad = 1;
3631 }
3632 }
3633 if (withflags && ! HvHASKFLAGS(hv)) {
3634 PerlIO_printf(Perl_debug_log,
3635 "Hash has HASKFLAGS off but I count %d key(s) with flags\n",
3636 withflags);
3637 bad = 1;
3638 }
3639 if (bad) {
ad64d0ec 3640 sv_dump(MUTABLE_SV(hv));
57ca3b03
AL
3641 }
3642 HvRITER_set(hv, riter); /* Restore hash iterator state */
3643 HvEITER_set(hv, eiter);
ecae49c0 3644}
af3babe4 3645
943795c2
NC
3646#endif
3647
af3babe4
NC
3648/*
3649 * Local variables:
3650 * c-indentation-style: bsd
3651 * c-basic-offset: 4
14d04a33 3652 * indent-tabs-mode: nil
af3babe4
NC
3653 * End:
3654 *
14d04a33 3655 * ex: set ts=8 sts=4 sw=4 et:
37442d52 3656 */