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