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