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