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