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