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