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