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