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