Re: [PATCH lib/Cwd.pm] fixing proto mismatch warning
[p5sagit/p5-mst-13.2.git] / hv.c
CommitLineData
a0d0e21e 1/* hv.c
79072805 2 *
be3c0a43 3 * Copyright (c) 1991-2002, 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 *
cea2e8a9 77S_save_hek(pTHX_ const char *str, I32 len, U32 hash)
bbce6d69 78{
79 char *k;
80 register HEK *hek;
da58a35d 81 bool is_utf8 = FALSE;
82
83 if (len < 0) {
84 len = -len;
85 is_utf8 = TRUE;
86 }
1c846c1f 87
e05949c7 88 New(54, k, HEK_BASESIZE + len + 2, char);
bbce6d69 89 hek = (HEK*)k;
ff68c719 90 Copy(str, HEK_KEY(hek), len, char);
e05949c7 91 HEK_KEY(hek)[len] = 0;
ff68c719 92 HEK_LEN(hek) = len;
93 HEK_HASH(hek) = hash;
da58a35d 94 HEK_UTF8(hek) = (char)is_utf8;
bbce6d69 95 return hek;
96}
97
98void
864dbfa3 99Perl_unshare_hek(pTHX_ HEK *hek)
bbce6d69 100{
c3654f1a 101 unsharepvn(HEK_KEY(hek),HEK_UTF8(hek)?-HEK_LEN(hek):HEK_LEN(hek),
102 HEK_HASH(hek));
bbce6d69 103}
104
d18c6117 105#if defined(USE_ITHREADS)
106HE *
a8fc9800 107Perl_he_dup(pTHX_ HE *e, bool shared, CLONE_PARAMS* param)
d18c6117 108{
109 HE *ret;
110
111 if (!e)
112 return Nullhe;
7766f137 113 /* look for it in the table first */
114 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
115 if (ret)
116 return ret;
117
118 /* create anew and remember what it is */
d33b2eba 119 ret = new_HE();
7766f137 120 ptr_table_store(PL_ptr_table, e, ret);
121
d2d73c3e 122 HeNEXT(ret) = he_dup(HeNEXT(e),shared, param);
d18c6117 123 if (HeKLEN(e) == HEf_SVKEY)
d2d73c3e 124 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e), param));
d18c6117 125 else if (shared)
c3654f1a 126 HeKEY_hek(ret) = share_hek(HeKEY(e), HeKLEN_UTF8(e), HeHASH(e));
d18c6117 127 else
c3654f1a 128 HeKEY_hek(ret) = save_hek(HeKEY(e), HeKLEN_UTF8(e), HeHASH(e));
d2d73c3e 129 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e), param));
d18c6117 130 return ret;
131}
132#endif /* USE_ITHREADS */
133
1b1f1335 134static void
135Perl_hv_notallowed(pTHX_ bool is_utf8, const char *key, I32 klen,
49293501 136 const char *keysave, const char *msg)
1b1f1335 137{
138 SV *sv = sv_newmortal();
139 if (key == keysave) {
140 sv_setpvn(sv, key, klen);
141 }
142 else {
143 /* Need to free saved eventually assign to mortal SV */
144 SV *sv = sv_newmortal();
145 sv_usepvn(sv, (char *) key, klen);
146 }
147 if (is_utf8) {
148 SvUTF8_on(sv);
149 }
49293501 150 Perl_croak(aTHX_ msg, sv);
1b1f1335 151}
152
fde52b5c 153/* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
154 * contains an SV* */
155
954c1994 156/*
157=for apidoc hv_fetch
158
159Returns the SV which corresponds to the specified key in the hash. The
160C<klen> is the length of the key. If C<lval> is set then the fetch will be
161part of a store. Check that the return value is non-null before
d1be9408 162dereferencing it to an C<SV*>.
954c1994 163
96f1132b 164See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
954c1994 165information on how to use this function on tied hashes.
166
167=cut
168*/
169
79072805 170SV**
da58a35d 171Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen, I32 lval)
79072805 172{
cbec9347 173 register XPVHV* xhv;
fde52b5c 174 register U32 hash;
79072805 175 register HE *entry;
79072805 176 SV *sv;
da58a35d 177 bool is_utf8 = FALSE;
f9a63242 178 const char *keysave = key;
79072805 179
180 if (!hv)
181 return 0;
463ee0b2 182
da58a35d 183 if (klen < 0) {
184 klen = -klen;
185 is_utf8 = TRUE;
186 }
187
8990e307 188 if (SvRMAGICAL(hv)) {
14befaf4 189 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
8990e307 190 sv = sv_newmortal();
463ee0b2 191 mg_copy((SV*)hv, sv, key, klen);
3280af22 192 PL_hv_fetch_sv = sv;
193 return &PL_hv_fetch_sv;
463ee0b2 194 }
902173a3 195#ifdef ENV_IS_CASELESS
14befaf4 196 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
e7152ba2 197 U32 i;
198 for (i = 0; i < klen; ++i)
199 if (isLOWER(key[i])) {
79cb57f6 200 char *nkey = strupr(SvPVX(sv_2mortal(newSVpvn(key,klen))));
e7152ba2 201 SV **ret = hv_fetch(hv, nkey, klen, 0);
202 if (!ret && lval)
203 ret = hv_store(hv, key, klen, NEWSV(61,0), 0);
204 return ret;
205 }
902173a3 206 }
207#endif
463ee0b2 208 }
209
cbec9347 210 /* We use xhv->xhv_foo fields directly instead of HvFOO(hv) to
211 avoid unnecessary pointer dereferencing. */
212 xhv = (XPVHV*)SvANY(hv);
213 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
1c846c1f 214 if (lval
a0d0e21e 215#ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
8aacddc1 216 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
a0d0e21e 217#endif
8aacddc1 218 )
cbec9347 219 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
220 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
221 char);
79072805 222 else
223 return 0;
224 }
225
77caf834 226 if (is_utf8) {
75a54232 227 STRLEN tmplen = klen;
228 /* Just casting the &klen to (STRLEN) won't work well
229 * if STRLEN and I32 are of different widths. --jhi */
230 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
231 klen = tmplen;
232 }
f9a63242 233
fde52b5c 234 PERL_HASH(hash, key, klen);
79072805 235
cbec9347 236 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
237 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
fde52b5c 238 for (; entry; entry = HeNEXT(entry)) {
239 if (HeHASH(entry) != hash) /* strings can't be equal */
79072805 240 continue;
fde52b5c 241 if (HeKLEN(entry) != klen)
79072805 242 continue;
1c846c1f 243 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
79072805 244 continue;
c3654f1a 245 if (HeKUTF8(entry) != (char)is_utf8)
246 continue;
f9a63242 247 if (key != keysave)
248 Safefree(key);
8aacddc1 249 /* if we find a placeholder, we pretend we haven't found anything */
250 if (HeVAL(entry) == &PL_sv_undef)
251 break;
fde52b5c 252 return &HeVAL(entry);
8aacddc1 253
79072805 254 }
a0d0e21e 255#ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
cbec9347 256 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
a6c40364 257 unsigned long len;
258 char *env = PerlEnv_ENVgetenv_len(key,&len);
259 if (env) {
260 sv = newSVpvn(env,len);
261 SvTAINTED_on(sv);
f9a63242 262 if (key != keysave)
263 Safefree(key);
a6c40364 264 return hv_store(hv,key,klen,sv,hash);
265 }
a0d0e21e 266 }
267#endif
8aacddc1 268 if (!entry && SvREADONLY(hv)) {
49293501 269 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave,
270 "Attempt to access disallowed key '%"SVf"' in a fixed hash"
271 );
1b1f1335 272 }
79072805 273 if (lval) { /* gonna assign to this, so it better be there */
274 sv = NEWSV(61,0);
f9a63242 275 if (key != keysave) { /* must be is_utf8 == 0 */
276 SV **ret = hv_store(hv,key,klen,sv,hash);
277 Safefree(key);
278 return ret;
279 }
280 else
281 return hv_store(hv,key,is_utf8?-klen:klen,sv,hash);
79072805 282 }
f9a63242 283 if (key != keysave)
284 Safefree(key);
79072805 285 return 0;
286}
287
d1be9408 288/* returns an HE * structure with the all fields set */
fde52b5c 289/* note that hent_val will be a mortal sv for MAGICAL hashes */
954c1994 290/*
291=for apidoc hv_fetch_ent
292
293Returns the hash entry which corresponds to the specified key in the hash.
294C<hash> must be a valid precomputed hash number for the given C<key>, or 0
295if you want the function to compute it. IF C<lval> is set then the fetch
296will be part of a store. Make sure the return value is non-null before
297accessing it. The return value when C<tb> is a tied hash is a pointer to a
298static location, so be sure to make a copy of the structure if you need to
1c846c1f 299store it somewhere.
954c1994 300
96f1132b 301See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
954c1994 302information on how to use this function on tied hashes.
303
304=cut
305*/
306
fde52b5c 307HE *
864dbfa3 308Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
fde52b5c 309{
cbec9347 310 register XPVHV* xhv;
fde52b5c 311 register char *key;
312 STRLEN klen;
313 register HE *entry;
314 SV *sv;
da58a35d 315 bool is_utf8;
f9a63242 316 char *keysave;
fde52b5c 317
318 if (!hv)
319 return 0;
320
902173a3 321 if (SvRMAGICAL(hv)) {
14befaf4 322 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
902173a3 323 sv = sv_newmortal();
324 keysv = sv_2mortal(newSVsv(keysv));
325 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
3280af22 326 if (!HeKEY_hek(&PL_hv_fetch_ent_mh)) {
902173a3 327 char *k;
328 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
3280af22 329 HeKEY_hek(&PL_hv_fetch_ent_mh) = (HEK*)k;
902173a3 330 }
3280af22 331 HeSVKEY_set(&PL_hv_fetch_ent_mh, keysv);
332 HeVAL(&PL_hv_fetch_ent_mh) = sv;
333 return &PL_hv_fetch_ent_mh;
1cf368ac 334 }
902173a3 335#ifdef ENV_IS_CASELESS
14befaf4 336 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
e7152ba2 337 U32 i;
902173a3 338 key = SvPV(keysv, klen);
e7152ba2 339 for (i = 0; i < klen; ++i)
340 if (isLOWER(key[i])) {
79cb57f6 341 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
e7152ba2 342 (void)strupr(SvPVX(nkeysv));
343 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
344 if (!entry && lval)
345 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
346 return entry;
347 }
902173a3 348 }
349#endif
fde52b5c 350 }
351
cbec9347 352 xhv = (XPVHV*)SvANY(hv);
353 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
1c846c1f 354 if (lval
fde52b5c 355#ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
8aacddc1 356 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
fde52b5c 357#endif
8aacddc1 358 )
cbec9347 359 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
360 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
361 char);
fde52b5c 362 else
363 return 0;
364 }
365
f9a63242 366 keysave = key = SvPV(keysv, klen);
da58a35d 367 is_utf8 = (SvUTF8(keysv)!=0);
1c846c1f 368
77caf834 369 if (is_utf8)
f9a63242 370 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
371
effa1e2d 372 if (!hash)
373 PERL_HASH(hash, key, klen);
374
cbec9347 375 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
376 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
fde52b5c 377 for (; entry; entry = HeNEXT(entry)) {
378 if (HeHASH(entry) != hash) /* strings can't be equal */
379 continue;
380 if (HeKLEN(entry) != klen)
381 continue;
1c846c1f 382 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
fde52b5c 383 continue;
c3654f1a 384 if (HeKUTF8(entry) != (char)is_utf8)
385 continue;
f9a63242 386 if (key != keysave)
387 Safefree(key);
8aacddc1 388 /* if we find a placeholder, we pretend we haven't found anything */
389 if (HeVAL(entry) == &PL_sv_undef)
390 break;
fde52b5c 391 return entry;
392 }
393#ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
cbec9347 394 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
a6c40364 395 unsigned long len;
396 char *env = PerlEnv_ENVgetenv_len(key,&len);
397 if (env) {
398 sv = newSVpvn(env,len);
399 SvTAINTED_on(sv);
400 return hv_store_ent(hv,keysv,sv,hash);
401 }
fde52b5c 402 }
403#endif
8aacddc1 404 if (!entry && SvREADONLY(hv)) {
49293501 405 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave,
406 "Attempt to access disallowed key '%"SVf"' in a fixed hash"
407 );
1b1f1335 408 }
f9a63242 409 if (key != keysave)
410 Safefree(key);
fde52b5c 411 if (lval) { /* gonna assign to this, so it better be there */
412 sv = NEWSV(61,0);
e7152ba2 413 return hv_store_ent(hv,keysv,sv,hash);
fde52b5c 414 }
415 return 0;
416}
417
864dbfa3 418STATIC void
cea2e8a9 419S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
d0066dc7 420{
421 MAGIC *mg = SvMAGIC(hv);
422 *needs_copy = FALSE;
423 *needs_store = TRUE;
424 while (mg) {
425 if (isUPPER(mg->mg_type)) {
426 *needs_copy = TRUE;
427 switch (mg->mg_type) {
14befaf4 428 case PERL_MAGIC_tied:
429 case PERL_MAGIC_sig:
d0066dc7 430 *needs_store = FALSE;
d0066dc7 431 }
432 }
433 mg = mg->mg_moremagic;
434 }
435}
436
954c1994 437/*
438=for apidoc hv_store
439
440Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
441the length of the key. The C<hash> parameter is the precomputed hash
442value; if it is zero then Perl will compute it. The return value will be
443NULL if the operation failed or if the value did not need to be actually
444stored within the hash (as in the case of tied hashes). Otherwise it can
445be dereferenced to get the original C<SV*>. Note that the caller is
446responsible for suitably incrementing the reference count of C<val> before
1c846c1f 447the call, and decrementing it if the function returned NULL.
954c1994 448
96f1132b 449See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
954c1994 450information on how to use this function on tied hashes.
451
452=cut
453*/
454
79072805 455SV**
da58a35d 456Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen, SV *val, register U32 hash)
79072805 457{
cbec9347 458 register XPVHV* xhv;
79072805 459 register I32 i;
460 register HE *entry;
461 register HE **oentry;
da58a35d 462 bool is_utf8 = FALSE;
f9a63242 463 const char *keysave = key;
79072805 464
465 if (!hv)
466 return 0;
467
da58a35d 468 if (klen < 0) {
469 klen = -klen;
470 is_utf8 = TRUE;
471 }
472
cbec9347 473 xhv = (XPVHV*)SvANY(hv);
463ee0b2 474 if (SvMAGICAL(hv)) {
d0066dc7 475 bool needs_copy;
476 bool needs_store;
477 hv_magic_check (hv, &needs_copy, &needs_store);
478 if (needs_copy) {
479 mg_copy((SV*)hv, val, key, klen);
cbec9347 480 if (!xhv->xhv_array /* !HvARRAY */ && !needs_store)
d0066dc7 481 return 0;
902173a3 482#ifdef ENV_IS_CASELESS
14befaf4 483 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
8aacddc1 484 key = savepvn(key,klen);
25716404 485 key = (const char*)strupr((char*)key);
902173a3 486 hash = 0;
487 }
488#endif
d0066dc7 489 }
463ee0b2 490 }
77caf834 491 if (is_utf8) {
75a54232 492 STRLEN tmplen = klen;
493 /* See the note in hv_fetch(). --jhi */
494 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
495 klen = tmplen;
496 }
f9a63242 497
fde52b5c 498 if (!hash)
499 PERL_HASH(hash, key, klen);
500
cbec9347 501 if (!xhv->xhv_array /* !HvARRAY(hv) */)
502 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
503 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
504 char);
fde52b5c 505
cbec9347 506 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
507 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
fde52b5c 508 i = 1;
509
510 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
511 if (HeHASH(entry) != hash) /* strings can't be equal */
512 continue;
513 if (HeKLEN(entry) != klen)
514 continue;
1c846c1f 515 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
fde52b5c 516 continue;
c3654f1a 517 if (HeKUTF8(entry) != (char)is_utf8)
518 continue;
8aacddc1 519 if (HeVAL(entry) == &PL_sv_undef)
520 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
521 else
522 SvREFCNT_dec(HeVAL(entry));
fde52b5c 523 HeVAL(entry) = val;
f9a63242 524 if (key != keysave)
525 Safefree(key);
fde52b5c 526 return &HeVAL(entry);
527 }
528
1b1f1335 529 if (SvREADONLY(hv)) {
49293501 530 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave,
531 "Attempt to access disallowed key '%"SVf"' to a fixed hash"
532 );
1b1f1335 533 }
534
d33b2eba 535 entry = new_HE();
fde52b5c 536 if (HvSHAREKEYS(hv))
c3654f1a 537 HeKEY_hek(entry) = share_hek(key, is_utf8?-klen:klen, hash);
fde52b5c 538 else /* gotta do the real thing */
c3654f1a 539 HeKEY_hek(entry) = save_hek(key, is_utf8?-klen:klen, hash);
f9a63242 540 if (key != keysave)
541 Safefree(key);
fde52b5c 542 HeVAL(entry) = val;
fde52b5c 543 HeNEXT(entry) = *oentry;
544 *oentry = entry;
545
cbec9347 546 xhv->xhv_keys++; /* HvKEYS(hv)++ */
fde52b5c 547 if (i) { /* initial entry? */
cbec9347 548 xhv->xhv_fill++; /* HvFILL(hv)++ */
549 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
fde52b5c 550 hsplit(hv);
79072805 551 }
552
fde52b5c 553 return &HeVAL(entry);
554}
555
954c1994 556/*
557=for apidoc hv_store_ent
558
559Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
560parameter is the precomputed hash value; if it is zero then Perl will
561compute it. The return value is the new hash entry so created. It will be
562NULL if the operation failed or if the value did not need to be actually
563stored within the hash (as in the case of tied hashes). Otherwise the
87324b0f 564contents of the return value can be accessed using the C<He?> macros
954c1994 565described here. Note that the caller is responsible for suitably
566incrementing the reference count of C<val> before the call, and
1c846c1f 567decrementing it if the function returned NULL.
954c1994 568
96f1132b 569See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
954c1994 570information on how to use this function on tied hashes.
571
572=cut
573*/
574
fde52b5c 575HE *
864dbfa3 576Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, register U32 hash)
fde52b5c 577{
cbec9347 578 register XPVHV* xhv;
fde52b5c 579 register char *key;
580 STRLEN klen;
581 register I32 i;
582 register HE *entry;
583 register HE **oentry;
da58a35d 584 bool is_utf8;
f9a63242 585 char *keysave;
fde52b5c 586
587 if (!hv)
588 return 0;
589
cbec9347 590 xhv = (XPVHV*)SvANY(hv);
fde52b5c 591 if (SvMAGICAL(hv)) {
8aacddc1 592 bool needs_copy;
593 bool needs_store;
594 hv_magic_check (hv, &needs_copy, &needs_store);
595 if (needs_copy) {
596 bool save_taint = PL_tainted;
597 if (PL_tainting)
598 PL_tainted = SvTAINTED(keysv);
599 keysv = sv_2mortal(newSVsv(keysv));
600 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
601 TAINT_IF(save_taint);
602 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store)
603 return Nullhe;
902173a3 604#ifdef ENV_IS_CASELESS
14befaf4 605 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
902173a3 606 key = SvPV(keysv, klen);
79cb57f6 607 keysv = sv_2mortal(newSVpvn(key,klen));
902173a3 608 (void)strupr(SvPVX(keysv));
609 hash = 0;
610 }
611#endif
612 }
fde52b5c 613 }
614
f9a63242 615 keysave = key = SvPV(keysv, klen);
da58a35d 616 is_utf8 = (SvUTF8(keysv) != 0);
902173a3 617
77caf834 618 if (is_utf8)
f9a63242 619 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
620
fde52b5c 621 if (!hash)
622 PERL_HASH(hash, key, klen);
623
cbec9347 624 if (!xhv->xhv_array /* !HvARRAY(hv) */)
625 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
626 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
627 char);
79072805 628
cbec9347 629 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
630 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
79072805 631 i = 1;
632
fde52b5c 633 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
634 if (HeHASH(entry) != hash) /* strings can't be equal */
79072805 635 continue;
fde52b5c 636 if (HeKLEN(entry) != klen)
79072805 637 continue;
1c846c1f 638 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
79072805 639 continue;
c3654f1a 640 if (HeKUTF8(entry) != (char)is_utf8)
641 continue;
8aacddc1 642 if (HeVAL(entry) == &PL_sv_undef)
643 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
644 else
645 SvREFCNT_dec(HeVAL(entry));
fde52b5c 646 HeVAL(entry) = val;
f9a63242 647 if (key != keysave)
648 Safefree(key);
fde52b5c 649 return entry;
79072805 650 }
79072805 651
1b1f1335 652 if (SvREADONLY(hv)) {
49293501 653 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave,
654 "Attempt to access disallowed key '%"SVf"' to a fixed hash"
655 );
1b1f1335 656 }
657
d33b2eba 658 entry = new_HE();
fde52b5c 659 if (HvSHAREKEYS(hv))
25716404 660 HeKEY_hek(entry) = share_hek(key, is_utf8?-(I32)klen:klen, hash);
fde52b5c 661 else /* gotta do the real thing */
25716404 662 HeKEY_hek(entry) = save_hek(key, is_utf8?-(I32)klen:klen, hash);
f9a63242 663 if (key != keysave)
664 Safefree(key);
fde52b5c 665 HeVAL(entry) = val;
fde52b5c 666 HeNEXT(entry) = *oentry;
79072805 667 *oentry = entry;
668
cbec9347 669 xhv->xhv_keys++; /* HvKEYS(hv)++ */
79072805 670 if (i) { /* initial entry? */
cbec9347 671 xhv->xhv_fill++; /* HvFILL(hv)++ */
672 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
79072805 673 hsplit(hv);
674 }
79072805 675
fde52b5c 676 return entry;
79072805 677}
678
954c1994 679/*
680=for apidoc hv_delete
681
682Deletes a key/value pair in the hash. The value SV is removed from the
1c846c1f 683hash and returned to the caller. The C<klen> is the length of the key.
954c1994 684The C<flags> value will normally be zero; if set to G_DISCARD then NULL
685will be returned.
686
687=cut
688*/
689
79072805 690SV *
da58a35d 691Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen, I32 flags)
79072805 692{
cbec9347 693 register XPVHV* xhv;
79072805 694 register I32 i;
fde52b5c 695 register U32 hash;
79072805 696 register HE *entry;
697 register HE **oentry;
67a38de0 698 SV **svp;
79072805 699 SV *sv;
da58a35d 700 bool is_utf8 = FALSE;
f9a63242 701 const char *keysave = key;
79072805 702
703 if (!hv)
704 return Nullsv;
da58a35d 705 if (klen < 0) {
706 klen = -klen;
707 is_utf8 = TRUE;
708 }
8990e307 709 if (SvRMAGICAL(hv)) {
0a0bb7c7 710 bool needs_copy;
711 bool needs_store;
712 hv_magic_check (hv, &needs_copy, &needs_store);
713
67a38de0 714 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
715 sv = *svp;
0a0bb7c7 716 mg_clear(sv);
717 if (!needs_store) {
14befaf4 718 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
719 /* No longer an element */
720 sv_unmagic(sv, PERL_MAGIC_tiedelem);
0a0bb7c7 721 return sv;
722 }
723 return Nullsv; /* element cannot be deleted */
724 }
902173a3 725#ifdef ENV_IS_CASELESS
14befaf4 726 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
79cb57f6 727 sv = sv_2mortal(newSVpvn(key,klen));
2fd1c6b8 728 key = strupr(SvPVX(sv));
729 }
902173a3 730#endif
8aacddc1 731 }
463ee0b2 732 }
cbec9347 733 xhv = (XPVHV*)SvANY(hv);
734 if (!xhv->xhv_array /* !HvARRAY(hv) */)
79072805 735 return Nullsv;
fde52b5c 736
77caf834 737 if (is_utf8) {
75a54232 738 STRLEN tmplen = klen;
739 /* See the note in hv_fetch(). --jhi */
740 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
741 klen = tmplen;
742 }
f9a63242 743
fde52b5c 744 PERL_HASH(hash, key, klen);
79072805 745
cbec9347 746 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
747 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
79072805 748 entry = *oentry;
749 i = 1;
fde52b5c 750 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
751 if (HeHASH(entry) != hash) /* strings can't be equal */
79072805 752 continue;
fde52b5c 753 if (HeKLEN(entry) != klen)
79072805 754 continue;
1c846c1f 755 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
79072805 756 continue;
c3654f1a 757 if (HeKUTF8(entry) != (char)is_utf8)
758 continue;
f9a63242 759 if (key != keysave)
760 Safefree(key);
8aacddc1 761 /* if placeholder is here, it's already been deleted.... */
762 if (HeVAL(entry) == &PL_sv_undef)
763 {
764 if (SvREADONLY(hv))
765 return Nullsv; /* if still SvREADONLY, leave it deleted. */
766 else {
767 /* okay, really delete the placeholder... */
768 *oentry = HeNEXT(entry);
769 if (i && !*oentry)
770 xhv->xhv_fill--; /* HvFILL(hv)-- */
771 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
772 HvLAZYDEL_on(hv);
773 else
774 hv_free_ent(hv, entry);
775 xhv->xhv_keys--; /* HvKEYS(hv)-- */
776 xhv->xhv_placeholders--;
777 return Nullsv;
778 }
779 }
780 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
49293501 781 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave,
782 "Attempt to delete readonly key '%"SVf"' from a fixed hash"
783 );
8aacddc1 784 }
785
748a9306 786 if (flags & G_DISCARD)
787 sv = Nullsv;
94f7643d 788 else {
79d01fbf 789 sv = sv_2mortal(HeVAL(entry));
94f7643d 790 HeVAL(entry) = &PL_sv_undef;
791 }
8aacddc1 792
793 /*
794 * If a restricted hash, rather than really deleting the entry, put
795 * a placeholder there. This marks the key as being "approved", so
796 * we can still access via not-really-existing key without raising
797 * an error.
798 */
799 if (SvREADONLY(hv)) {
800 HeVAL(entry) = &PL_sv_undef;
801 /* We'll be saving this slot, so the number of allocated keys
802 * doesn't go down, but the number placeholders goes up */
803 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
804 } else {
a26e96df 805 *oentry = HeNEXT(entry);
806 if (i && !*oentry)
807 xhv->xhv_fill--; /* HvFILL(hv)-- */
8aacddc1 808 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
809 HvLAZYDEL_on(hv);
810 else
811 hv_free_ent(hv, entry);
812 xhv->xhv_keys--; /* HvKEYS(hv)-- */
813 }
fde52b5c 814 return sv;
815 }
8aacddc1 816 if (SvREADONLY(hv)) {
49293501 817 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave,
818 "Attempt to access disallowed key '%"SVf"' from a fixed hash"
819 );
8aacddc1 820 }
821
f9a63242 822 if (key != keysave)
823 Safefree(key);
fde52b5c 824 return Nullsv;
825}
826
954c1994 827/*
828=for apidoc hv_delete_ent
829
830Deletes a key/value pair in the hash. The value SV is removed from the
831hash and returned to the caller. The C<flags> value will normally be zero;
832if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
833precomputed hash value, or 0 to ask for it to be computed.
834
835=cut
836*/
837
fde52b5c 838SV *
864dbfa3 839Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
fde52b5c 840{
cbec9347 841 register XPVHV* xhv;
fde52b5c 842 register I32 i;
843 register char *key;
844 STRLEN klen;
845 register HE *entry;
846 register HE **oentry;
847 SV *sv;
da58a35d 848 bool is_utf8;
f9a63242 849 char *keysave;
1c846c1f 850
fde52b5c 851 if (!hv)
852 return Nullsv;
853 if (SvRMAGICAL(hv)) {
0a0bb7c7 854 bool needs_copy;
855 bool needs_store;
856 hv_magic_check (hv, &needs_copy, &needs_store);
857
67a38de0 858 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
0a0bb7c7 859 sv = HeVAL(entry);
860 mg_clear(sv);
861 if (!needs_store) {
14befaf4 862 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
863 /* No longer an element */
864 sv_unmagic(sv, PERL_MAGIC_tiedelem);
0a0bb7c7 865 return sv;
866 }
867 return Nullsv; /* element cannot be deleted */
868 }
902173a3 869#ifdef ENV_IS_CASELESS
14befaf4 870 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
2fd1c6b8 871 key = SvPV(keysv, klen);
79cb57f6 872 keysv = sv_2mortal(newSVpvn(key,klen));
2fd1c6b8 873 (void)strupr(SvPVX(keysv));
1c846c1f 874 hash = 0;
2fd1c6b8 875 }
902173a3 876#endif
2fd1c6b8 877 }
fde52b5c 878 }
cbec9347 879 xhv = (XPVHV*)SvANY(hv);
880 if (!xhv->xhv_array /* !HvARRAY(hv) */)
fde52b5c 881 return Nullsv;
882
f9a63242 883 keysave = key = SvPV(keysv, klen);
da58a35d 884 is_utf8 = (SvUTF8(keysv) != 0);
1c846c1f 885
77caf834 886 if (is_utf8)
f9a63242 887 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
888
fde52b5c 889 if (!hash)
890 PERL_HASH(hash, key, klen);
891
cbec9347 892 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
893 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
fde52b5c 894 entry = *oentry;
895 i = 1;
896 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
897 if (HeHASH(entry) != hash) /* strings can't be equal */
898 continue;
899 if (HeKLEN(entry) != klen)
900 continue;
1c846c1f 901 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
fde52b5c 902 continue;
c3654f1a 903 if (HeKUTF8(entry) != (char)is_utf8)
904 continue;
f9a63242 905 if (key != keysave)
906 Safefree(key);
8aacddc1 907
908 /* if placeholder is here, it's already been deleted.... */
909 if (HeVAL(entry) == &PL_sv_undef)
910 {
911 if (SvREADONLY(hv))
912 return Nullsv; /* if still SvREADONLY, leave it deleted. */
03fed38d 913
914 /* okay, really delete the placeholder. */
915 *oentry = HeNEXT(entry);
916 if (i && !*oentry)
917 xhv->xhv_fill--; /* HvFILL(hv)-- */
918 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
919 HvLAZYDEL_on(hv);
920 else
921 hv_free_ent(hv, entry);
922 xhv->xhv_keys--; /* HvKEYS(hv)-- */
923 xhv->xhv_placeholders--;
924 return Nullsv;
8aacddc1 925 }
926 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
49293501 927 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave,
928 "Attempt to delete readonly key '%"SVf"' from a fixed hash"
929 );
8aacddc1 930 }
931
fde52b5c 932 if (flags & G_DISCARD)
933 sv = Nullsv;
94f7643d 934 else {
79d01fbf 935 sv = sv_2mortal(HeVAL(entry));
94f7643d 936 HeVAL(entry) = &PL_sv_undef;
937 }
8aacddc1 938
939 /*
940 * If a restricted hash, rather than really deleting the entry, put
941 * a placeholder there. This marks the key as being "approved", so
942 * we can still access via not-really-existing key without raising
943 * an error.
944 */
945 if (SvREADONLY(hv)) {
946 HeVAL(entry) = &PL_sv_undef;
947 /* We'll be saving this slot, so the number of allocated keys
948 * doesn't go down, but the number placeholders goes up */
949 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
950 } else {
a26e96df 951 *oentry = HeNEXT(entry);
952 if (i && !*oentry)
953 xhv->xhv_fill--; /* HvFILL(hv)-- */
8aacddc1 954 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
955 HvLAZYDEL_on(hv);
956 else
957 hv_free_ent(hv, entry);
958 xhv->xhv_keys--; /* HvKEYS(hv)-- */
959 }
79072805 960 return sv;
961 }
8aacddc1 962 if (SvREADONLY(hv)) {
49293501 963 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave,
964 "Attempt to delete disallowed key '%"SVf"' from a fixed hash"
965 );
8aacddc1 966 }
967
f9a63242 968 if (key != keysave)
969 Safefree(key);
79072805 970 return Nullsv;
79072805 971}
972
954c1994 973/*
974=for apidoc hv_exists
975
976Returns a boolean indicating whether the specified hash key exists. The
977C<klen> is the length of the key.
978
979=cut
980*/
981
a0d0e21e 982bool
da58a35d 983Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen)
a0d0e21e 984{
cbec9347 985 register XPVHV* xhv;
fde52b5c 986 register U32 hash;
a0d0e21e 987 register HE *entry;
988 SV *sv;
da58a35d 989 bool is_utf8 = FALSE;
f9a63242 990 const char *keysave = key;
a0d0e21e 991
992 if (!hv)
993 return 0;
994
da58a35d 995 if (klen < 0) {
996 klen = -klen;
997 is_utf8 = TRUE;
998 }
999
a0d0e21e 1000 if (SvRMAGICAL(hv)) {
14befaf4 1001 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
a0d0e21e 1002 sv = sv_newmortal();
1c846c1f 1003 mg_copy((SV*)hv, sv, key, klen);
14befaf4 1004 magic_existspack(sv, mg_find(sv, PERL_MAGIC_tiedelem));
a0d0e21e 1005 return SvTRUE(sv);
1006 }
902173a3 1007#ifdef ENV_IS_CASELESS
14befaf4 1008 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
79cb57f6 1009 sv = sv_2mortal(newSVpvn(key,klen));
902173a3 1010 key = strupr(SvPVX(sv));
1011 }
1012#endif
a0d0e21e 1013 }
1014
cbec9347 1015 xhv = (XPVHV*)SvANY(hv);
f675dbe5 1016#ifndef DYNAMIC_ENV_FETCH
cbec9347 1017 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1c846c1f 1018 return 0;
f675dbe5 1019#endif
a0d0e21e 1020
77caf834 1021 if (is_utf8) {
75a54232 1022 STRLEN tmplen = klen;
1023 /* See the note in hv_fetch(). --jhi */
1024 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
1025 klen = tmplen;
1026 }
f9a63242 1027
fde52b5c 1028 PERL_HASH(hash, key, klen);
a0d0e21e 1029
f675dbe5 1030#ifdef DYNAMIC_ENV_FETCH
cbec9347 1031 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
f675dbe5 1032 else
1033#endif
cbec9347 1034 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1035 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
fde52b5c 1036 for (; entry; entry = HeNEXT(entry)) {
1037 if (HeHASH(entry) != hash) /* strings can't be equal */
a0d0e21e 1038 continue;
fde52b5c 1039 if (HeKLEN(entry) != klen)
a0d0e21e 1040 continue;
1c846c1f 1041 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
fde52b5c 1042 continue;
c3654f1a 1043 if (HeKUTF8(entry) != (char)is_utf8)
1044 continue;
f9a63242 1045 if (key != keysave)
1046 Safefree(key);
8aacddc1 1047 /* If we find the key, but the value is a placeholder, return false. */
1048 if (HeVAL(entry) == &PL_sv_undef)
1049 return FALSE;
1050
fde52b5c 1051 return TRUE;
1052 }
f675dbe5 1053#ifdef DYNAMIC_ENV_FETCH /* is it out there? */
cbec9347 1054 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
a6c40364 1055 unsigned long len;
1056 char *env = PerlEnv_ENVgetenv_len(key,&len);
1057 if (env) {
1058 sv = newSVpvn(env,len);
1059 SvTAINTED_on(sv);
1060 (void)hv_store(hv,key,klen,sv,hash);
1061 return TRUE;
1062 }
f675dbe5 1063 }
1064#endif
f9a63242 1065 if (key != keysave)
1066 Safefree(key);
fde52b5c 1067 return FALSE;
1068}
1069
1070
954c1994 1071/*
1072=for apidoc hv_exists_ent
1073
1074Returns a boolean indicating whether the specified hash key exists. C<hash>
1075can be a valid precomputed hash value, or 0 to ask for it to be
1076computed.
1077
1078=cut
1079*/
1080
fde52b5c 1081bool
864dbfa3 1082Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
fde52b5c 1083{
cbec9347 1084 register XPVHV* xhv;
fde52b5c 1085 register char *key;
1086 STRLEN klen;
1087 register HE *entry;
1088 SV *sv;
c3654f1a 1089 bool is_utf8;
f9a63242 1090 char *keysave;
fde52b5c 1091
1092 if (!hv)
1093 return 0;
1094
1095 if (SvRMAGICAL(hv)) {
14befaf4 1096 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
8aacddc1 1097 SV* svret = sv_newmortal();
fde52b5c 1098 sv = sv_newmortal();
effa1e2d 1099 keysv = sv_2mortal(newSVsv(keysv));
1c846c1f 1100 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
8aacddc1 1101 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
1102 return SvTRUE(svret);
fde52b5c 1103 }
902173a3 1104#ifdef ENV_IS_CASELESS
14befaf4 1105 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
902173a3 1106 key = SvPV(keysv, klen);
79cb57f6 1107 keysv = sv_2mortal(newSVpvn(key,klen));
902173a3 1108 (void)strupr(SvPVX(keysv));
1c846c1f 1109 hash = 0;
902173a3 1110 }
1111#endif
fde52b5c 1112 }
1113
cbec9347 1114 xhv = (XPVHV*)SvANY(hv);
f675dbe5 1115#ifndef DYNAMIC_ENV_FETCH
cbec9347 1116 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1c846c1f 1117 return 0;
f675dbe5 1118#endif
fde52b5c 1119
f9a63242 1120 keysave = key = SvPV(keysv, klen);
c3654f1a 1121 is_utf8 = (SvUTF8(keysv) != 0);
77caf834 1122 if (is_utf8)
f9a63242 1123 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
fde52b5c 1124 if (!hash)
1125 PERL_HASH(hash, key, klen);
1126
f675dbe5 1127#ifdef DYNAMIC_ENV_FETCH
cbec9347 1128 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
f675dbe5 1129 else
1130#endif
cbec9347 1131 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1132 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
fde52b5c 1133 for (; entry; entry = HeNEXT(entry)) {
1134 if (HeHASH(entry) != hash) /* strings can't be equal */
1135 continue;
1136 if (HeKLEN(entry) != klen)
1137 continue;
1c846c1f 1138 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
a0d0e21e 1139 continue;
c3654f1a 1140 if (HeKUTF8(entry) != (char)is_utf8)
1141 continue;
f9a63242 1142 if (key != keysave)
1143 Safefree(key);
8aacddc1 1144 /* If we find the key, but the value is a placeholder, return false. */
1145 if (HeVAL(entry) == &PL_sv_undef)
1146 return FALSE;
a0d0e21e 1147 return TRUE;
1148 }
f675dbe5 1149#ifdef DYNAMIC_ENV_FETCH /* is it out there? */
cbec9347 1150 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
a6c40364 1151 unsigned long len;
1152 char *env = PerlEnv_ENVgetenv_len(key,&len);
1153 if (env) {
1154 sv = newSVpvn(env,len);
1155 SvTAINTED_on(sv);
1156 (void)hv_store_ent(hv,keysv,sv,hash);
1157 return TRUE;
1158 }
f675dbe5 1159 }
1160#endif
f9a63242 1161 if (key != keysave)
1162 Safefree(key);
a0d0e21e 1163 return FALSE;
1164}
1165
76e3520e 1166STATIC void
cea2e8a9 1167S_hsplit(pTHX_ HV *hv)
79072805 1168{
cbec9347 1169 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1170 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
79072805 1171 register I32 newsize = oldsize * 2;
1172 register I32 i;
cbec9347 1173 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
72311751 1174 register HE **aep;
1175 register HE **bep;
79072805 1176 register HE *entry;
1177 register HE **oentry;
1178
3280af22 1179 PL_nomemok = TRUE;
8d6dde3e 1180#if defined(STRANGE_MALLOC) || defined(MYMALLOC)
d18c6117 1181 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
422a93e5 1182 if (!a) {
4a33f861 1183 PL_nomemok = FALSE;
422a93e5 1184 return;
1185 }
4633a7c4 1186#else
d18c6117 1187 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
422a93e5 1188 if (!a) {
3280af22 1189 PL_nomemok = FALSE;
422a93e5 1190 return;
1191 }
cbec9347 1192 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
fba3b22e 1193 if (oldsize >= 64) {
cbec9347 1194 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1195 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
4633a7c4 1196 }
1197 else
cbec9347 1198 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
4633a7c4 1199#endif
1200
3280af22 1201 PL_nomemok = FALSE;
72311751 1202 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
cbec9347 1203 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1204 xhv->xhv_array = a; /* HvARRAY(hv) = a */
72311751 1205 aep = (HE**)a;
79072805 1206
72311751 1207 for (i=0; i<oldsize; i++,aep++) {
1208 if (!*aep) /* non-existent */
79072805 1209 continue;
72311751 1210 bep = aep+oldsize;
1211 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
fde52b5c 1212 if ((HeHASH(entry) & newsize) != i) {
1213 *oentry = HeNEXT(entry);
72311751 1214 HeNEXT(entry) = *bep;
1215 if (!*bep)
cbec9347 1216 xhv->xhv_fill++; /* HvFILL(hv)++ */
72311751 1217 *bep = entry;
79072805 1218 continue;
1219 }
1220 else
fde52b5c 1221 oentry = &HeNEXT(entry);
79072805 1222 }
72311751 1223 if (!*aep) /* everything moved */
cbec9347 1224 xhv->xhv_fill--; /* HvFILL(hv)-- */
79072805 1225 }
1226}
1227
72940dca 1228void
864dbfa3 1229Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
72940dca 1230{
cbec9347 1231 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1232 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
72940dca 1233 register I32 newsize;
1234 register I32 i;
1235 register I32 j;
72311751 1236 register char *a;
1237 register HE **aep;
72940dca 1238 register HE *entry;
1239 register HE **oentry;
1240
1241 newsize = (I32) newmax; /* possible truncation here */
1242 if (newsize != newmax || newmax <= oldsize)
1243 return;
1244 while ((newsize & (1 + ~newsize)) != newsize) {
1245 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1246 }
1247 if (newsize < newmax)
1248 newsize *= 2;
1249 if (newsize < newmax)
1250 return; /* overflow detection */
1251
cbec9347 1252 a = xhv->xhv_array; /* HvARRAY(hv) */
72940dca 1253 if (a) {
3280af22 1254 PL_nomemok = TRUE;
8d6dde3e 1255#if defined(STRANGE_MALLOC) || defined(MYMALLOC)
d18c6117 1256 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
8aacddc1 1257 if (!a) {
4a33f861 1258 PL_nomemok = FALSE;
422a93e5 1259 return;
1260 }
72940dca 1261#else
d18c6117 1262 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
8aacddc1 1263 if (!a) {
3280af22 1264 PL_nomemok = FALSE;
422a93e5 1265 return;
1266 }
cbec9347 1267 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
fba3b22e 1268 if (oldsize >= 64) {
cbec9347 1269 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1270 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
72940dca 1271 }
1272 else
cbec9347 1273 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
72940dca 1274#endif
3280af22 1275 PL_nomemok = FALSE;
72311751 1276 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
72940dca 1277 }
1278 else {
d18c6117 1279 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
72940dca 1280 }
cbec9347 1281 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1282 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1283 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
72940dca 1284 return;
1285
72311751 1286 aep = (HE**)a;
1287 for (i=0; i<oldsize; i++,aep++) {
1288 if (!*aep) /* non-existent */
72940dca 1289 continue;
72311751 1290 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
72940dca 1291 if ((j = (HeHASH(entry) & newsize)) != i) {
1292 j -= i;
1293 *oentry = HeNEXT(entry);
72311751 1294 if (!(HeNEXT(entry) = aep[j]))
cbec9347 1295 xhv->xhv_fill++; /* HvFILL(hv)++ */
72311751 1296 aep[j] = entry;
72940dca 1297 continue;
1298 }
1299 else
1300 oentry = &HeNEXT(entry);
1301 }
72311751 1302 if (!*aep) /* everything moved */
cbec9347 1303 xhv->xhv_fill--; /* HvFILL(hv)-- */
72940dca 1304 }
1305}
1306
954c1994 1307/*
1308=for apidoc newHV
1309
1310Creates a new HV. The reference count is set to 1.
1311
1312=cut
1313*/
1314
79072805 1315HV *
864dbfa3 1316Perl_newHV(pTHX)
79072805 1317{
1318 register HV *hv;
cbec9347 1319 register XPVHV* xhv;
79072805 1320
a0d0e21e 1321 hv = (HV*)NEWSV(502,0);
1322 sv_upgrade((SV *)hv, SVt_PVHV);
cbec9347 1323 xhv = (XPVHV*)SvANY(hv);
79072805 1324 SvPOK_off(hv);
1325 SvNOK_off(hv);
1c846c1f 1326#ifndef NODEFAULT_SHAREKEYS
fde52b5c 1327 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1c846c1f 1328#endif
cbec9347 1329 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1330 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1331 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
79072805 1332 (void)hv_iterinit(hv); /* so each() will start off right */
1333 return hv;
1334}
1335
b3ac6de7 1336HV *
864dbfa3 1337Perl_newHVhv(pTHX_ HV *ohv)
b3ac6de7 1338{
b56ba0bf 1339 HV *hv = newHV();
4beac62f 1340 STRLEN hv_max, hv_fill;
4beac62f 1341
1342 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1343 return hv;
4beac62f 1344 hv_max = HvMAX(ohv);
b3ac6de7 1345
b56ba0bf 1346 if (!SvMAGICAL((SV *)ohv)) {
1347 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1348 int i, shared = !!HvSHAREKEYS(ohv);
1349 HE **ents, **oents = (HE **)HvARRAY(ohv);
ff875642 1350 char *a;
1351 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1352 ents = (HE**)a;
b56ba0bf 1353
1354 /* In each bucket... */
1355 for (i = 0; i <= hv_max; i++) {
1356 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1357
1358 if (!oent) {
1359 ents[i] = NULL;
1360 continue;
1361 }
1362
1363 /* Copy the linked list of entries. */
1364 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1365 U32 hash = HeHASH(oent);
1366 char *key = HeKEY(oent);
1367 STRLEN len = HeKLEN_UTF8(oent);
1368
1369 ent = new_HE();
45dea987 1370 HeVAL(ent) = newSVsv(HeVAL(oent));
b56ba0bf 1371 HeKEY_hek(ent) = shared ? share_hek(key, len, hash)
1372 : save_hek(key, len, hash);
1373 if (prev)
1374 HeNEXT(prev) = ent;
1375 else
1376 ents[i] = ent;
1377 prev = ent;
1378 HeNEXT(ent) = NULL;
1379 }
1380 }
1381
1382 HvMAX(hv) = hv_max;
1383 HvFILL(hv) = hv_fill;
8aacddc1 1384 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
b56ba0bf 1385 HvARRAY(hv) = ents;
1c846c1f 1386 }
b56ba0bf 1387 else {
1388 /* Iterate over ohv, copying keys and values one at a time. */
b3ac6de7 1389 HE *entry;
b56ba0bf 1390 I32 riter = HvRITER(ohv);
1391 HE *eiter = HvEITER(ohv);
1392
1393 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1394 while (hv_max && hv_max + 1 >= hv_fill * 2)
1395 hv_max = hv_max / 2;
1396 HvMAX(hv) = hv_max;
1397
4a76a316 1398 hv_iterinit(ohv);
155aba94 1399 while ((entry = hv_iternext(ohv))) {
c3654f1a 1400 hv_store(hv, HeKEY(entry), HeKLEN_UTF8(entry),
00122d59 1401 newSVsv(HeVAL(entry)), HeHASH(entry));
b3ac6de7 1402 }
b56ba0bf 1403 HvRITER(ohv) = riter;
1404 HvEITER(ohv) = eiter;
b3ac6de7 1405 }
1c846c1f 1406
b3ac6de7 1407 return hv;
1408}
1409
79072805 1410void
864dbfa3 1411Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
79072805 1412{
16bdeea2 1413 SV *val;
1414
68dc0745 1415 if (!entry)
79072805 1416 return;
16bdeea2 1417 val = HeVAL(entry);
257c9e5b 1418 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
3280af22 1419 PL_sub_generation++; /* may be deletion of method from stash */
16bdeea2 1420 SvREFCNT_dec(val);
68dc0745 1421 if (HeKLEN(entry) == HEf_SVKEY) {
1422 SvREFCNT_dec(HeKEY_sv(entry));
8aacddc1 1423 Safefree(HeKEY_hek(entry));
44a8e56a 1424 }
1425 else if (HvSHAREKEYS(hv))
68dc0745 1426 unshare_hek(HeKEY_hek(entry));
fde52b5c 1427 else
68dc0745 1428 Safefree(HeKEY_hek(entry));
d33b2eba 1429 del_HE(entry);
79072805 1430}
1431
1432void
864dbfa3 1433Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
79072805 1434{
68dc0745 1435 if (!entry)
79072805 1436 return;
68dc0745 1437 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
3280af22 1438 PL_sub_generation++; /* may be deletion of method from stash */
68dc0745 1439 sv_2mortal(HeVAL(entry)); /* free between statements */
1440 if (HeKLEN(entry) == HEf_SVKEY) {
1441 sv_2mortal(HeKEY_sv(entry));
1442 Safefree(HeKEY_hek(entry));
44a8e56a 1443 }
1444 else if (HvSHAREKEYS(hv))
68dc0745 1445 unshare_hek(HeKEY_hek(entry));
fde52b5c 1446 else
68dc0745 1447 Safefree(HeKEY_hek(entry));
d33b2eba 1448 del_HE(entry);
79072805 1449}
1450
954c1994 1451/*
1452=for apidoc hv_clear
1453
1454Clears a hash, making it empty.
1455
1456=cut
1457*/
1458
79072805 1459void
864dbfa3 1460Perl_hv_clear(pTHX_ HV *hv)
79072805 1461{
cbec9347 1462 register XPVHV* xhv;
79072805 1463 if (!hv)
1464 return;
49293501 1465
1466 if(SvREADONLY(hv)) {
1467 Perl_croak(aTHX_ "Attempt to clear a fixed hash");
1468 }
1469
cbec9347 1470 xhv = (XPVHV*)SvANY(hv);
463ee0b2 1471 hfreeentries(hv);
cbec9347 1472 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1473 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
8aacddc1 1474 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
cbec9347 1475 if (xhv->xhv_array /* HvARRAY(hv) */)
1476 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1477 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
a0d0e21e 1478
1479 if (SvRMAGICAL(hv))
1c846c1f 1480 mg_clear((SV*)hv);
79072805 1481}
1482
76e3520e 1483STATIC void
cea2e8a9 1484S_hfreeentries(pTHX_ HV *hv)
79072805 1485{
a0d0e21e 1486 register HE **array;
68dc0745 1487 register HE *entry;
1488 register HE *oentry = Null(HE*);
a0d0e21e 1489 I32 riter;
1490 I32 max;
79072805 1491
1492 if (!hv)
1493 return;
a0d0e21e 1494 if (!HvARRAY(hv))
79072805 1495 return;
a0d0e21e 1496
1497 riter = 0;
1498 max = HvMAX(hv);
1499 array = HvARRAY(hv);
68dc0745 1500 entry = array[0];
a0d0e21e 1501 for (;;) {
68dc0745 1502 if (entry) {
1503 oentry = entry;
1504 entry = HeNEXT(entry);
1505 hv_free_ent(hv, oentry);
a0d0e21e 1506 }
68dc0745 1507 if (!entry) {
a0d0e21e 1508 if (++riter > max)
1509 break;
68dc0745 1510 entry = array[riter];
1c846c1f 1511 }
79072805 1512 }
a0d0e21e 1513 (void)hv_iterinit(hv);
79072805 1514}
1515
954c1994 1516/*
1517=for apidoc hv_undef
1518
1519Undefines the hash.
1520
1521=cut
1522*/
1523
79072805 1524void
864dbfa3 1525Perl_hv_undef(pTHX_ HV *hv)
79072805 1526{
cbec9347 1527 register XPVHV* xhv;
79072805 1528 if (!hv)
1529 return;
cbec9347 1530 xhv = (XPVHV*)SvANY(hv);
463ee0b2 1531 hfreeentries(hv);
cbec9347 1532 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
85e6fe83 1533 if (HvNAME(hv)) {
1534 Safefree(HvNAME(hv));
1535 HvNAME(hv) = 0;
1536 }
cbec9347 1537 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1538 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1539 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1540 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
8aacddc1 1541 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
a0d0e21e 1542
1543 if (SvRMAGICAL(hv))
1c846c1f 1544 mg_clear((SV*)hv);
79072805 1545}
1546
954c1994 1547/*
1548=for apidoc hv_iterinit
1549
1550Prepares a starting point to traverse a hash table. Returns the number of
1551keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1c846c1f 1552currently only meaningful for hashes without tie magic.
954c1994 1553
1554NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1555hash buckets that happen to be in use. If you still need that esoteric
1556value, you can get it through the macro C<HvFILL(tb)>.
1557
1558=cut
1559*/
1560
79072805 1561I32
864dbfa3 1562Perl_hv_iterinit(pTHX_ HV *hv)
79072805 1563{
cbec9347 1564 register XPVHV* xhv;
aa689395 1565 HE *entry;
1566
1567 if (!hv)
cea2e8a9 1568 Perl_croak(aTHX_ "Bad hash");
cbec9347 1569 xhv = (XPVHV*)SvANY(hv);
1570 entry = xhv->xhv_eiter; /* HvEITER(hv) */
72940dca 1571 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1572 HvLAZYDEL_off(hv);
68dc0745 1573 hv_free_ent(hv, entry);
72940dca 1574 }
cbec9347 1575 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1576 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1577 /* used to be xhv->xhv_fill before 5.004_65 */
8aacddc1 1578 return XHvTOTALKEYS(xhv);
79072805 1579}
1580
954c1994 1581/*
1582=for apidoc hv_iternext
1583
1584Returns entries from a hash iterator. See C<hv_iterinit>.
1585
1586=cut
1587*/
1588
79072805 1589HE *
864dbfa3 1590Perl_hv_iternext(pTHX_ HV *hv)
79072805 1591{
cbec9347 1592 register XPVHV* xhv;
79072805 1593 register HE *entry;
a0d0e21e 1594 HE *oldentry;
463ee0b2 1595 MAGIC* mg;
79072805 1596
1597 if (!hv)
cea2e8a9 1598 Perl_croak(aTHX_ "Bad hash");
cbec9347 1599 xhv = (XPVHV*)SvANY(hv);
1600 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
463ee0b2 1601
14befaf4 1602 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
8990e307 1603 SV *key = sv_newmortal();
cd1469e6 1604 if (entry) {
fde52b5c 1605 sv_setsv(key, HeSVKEY_force(entry));
cd1469e6 1606 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1607 }
a0d0e21e 1608 else {
ff68c719 1609 char *k;
bbce6d69 1610 HEK *hek;
ff68c719 1611
cbec9347 1612 /* one HE per MAGICAL hash */
1613 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
4633a7c4 1614 Zero(entry, 1, HE);
ff68c719 1615 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1616 hek = (HEK*)k;
1617 HeKEY_hek(entry) = hek;
fde52b5c 1618 HeKLEN(entry) = HEf_SVKEY;
a0d0e21e 1619 }
1620 magic_nextpack((SV*) hv,mg,key);
8aacddc1 1621 if (SvOK(key)) {
cd1469e6 1622 /* force key to stay around until next time */
bbce6d69 1623 HeSVKEY_set(entry, SvREFCNT_inc(key));
1624 return entry; /* beware, hent_val is not set */
8aacddc1 1625 }
fde52b5c 1626 if (HeVAL(entry))
1627 SvREFCNT_dec(HeVAL(entry));
ff68c719 1628 Safefree(HeKEY_hek(entry));
d33b2eba 1629 del_HE(entry);
cbec9347 1630 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
463ee0b2 1631 return Null(HE*);
79072805 1632 }
f675dbe5 1633#ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
cbec9347 1634 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
f675dbe5 1635 prime_env_iter();
1636#endif
463ee0b2 1637
cbec9347 1638 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1639 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1640 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1641 char);
fde52b5c 1642 if (entry)
8aacddc1 1643 {
fde52b5c 1644 entry = HeNEXT(entry);
8aacddc1 1645 /*
1646 * Skip past any placeholders -- don't want to include them in
1647 * any iteration.
1648 */
1649 while (entry && HeVAL(entry) == &PL_sv_undef) {
1650 entry = HeNEXT(entry);
1651 }
1652 }
fde52b5c 1653 while (!entry) {
cbec9347 1654 xhv->xhv_riter++; /* HvRITER(hv)++ */
1655 if (xhv->xhv_riter > xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1656 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
fde52b5c 1657 break;
79072805 1658 }
cbec9347 1659 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1660 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
8aacddc1 1661
1662 /* if we have an entry, but it's a placeholder, don't count it */
1663 if (entry && HeVAL(entry) == &PL_sv_undef)
1664 entry = 0;
1665
fde52b5c 1666 }
79072805 1667
72940dca 1668 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1669 HvLAZYDEL_off(hv);
68dc0745 1670 hv_free_ent(hv, oldentry);
72940dca 1671 }
a0d0e21e 1672
cbec9347 1673 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
79072805 1674 return entry;
1675}
1676
954c1994 1677/*
1678=for apidoc hv_iterkey
1679
1680Returns the key from the current position of the hash iterator. See
1681C<hv_iterinit>.
1682
1683=cut
1684*/
1685
79072805 1686char *
864dbfa3 1687Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
79072805 1688{
fde52b5c 1689 if (HeKLEN(entry) == HEf_SVKEY) {
fb73857a 1690 STRLEN len;
1691 char *p = SvPV(HeKEY_sv(entry), len);
1692 *retlen = len;
1693 return p;
fde52b5c 1694 }
1695 else {
1696 *retlen = HeKLEN(entry);
1697 return HeKEY(entry);
1698 }
1699}
1700
1701/* unlike hv_iterval(), this always returns a mortal copy of the key */
954c1994 1702/*
1703=for apidoc hv_iterkeysv
1704
1705Returns the key as an C<SV*> from the current position of the hash
1706iterator. The return value will always be a mortal copy of the key. Also
1707see C<hv_iterinit>.
1708
1709=cut
1710*/
1711
fde52b5c 1712SV *
864dbfa3 1713Perl_hv_iterkeysv(pTHX_ register HE *entry)
fde52b5c 1714{
1715 if (HeKLEN(entry) == HEf_SVKEY)
bbce6d69 1716 return sv_mortalcopy(HeKEY_sv(entry));
c3654f1a 1717 else
1718 return sv_2mortal(newSVpvn_share((HeKLEN(entry) ? HeKEY(entry) : ""),
1719 HeKLEN_UTF8(entry), HeHASH(entry)));
79072805 1720}
1721
954c1994 1722/*
1723=for apidoc hv_iterval
1724
1725Returns the value from the current position of the hash iterator. See
1726C<hv_iterkey>.
1727
1728=cut
1729*/
1730
79072805 1731SV *
864dbfa3 1732Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
79072805 1733{
8990e307 1734 if (SvRMAGICAL(hv)) {
14befaf4 1735 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
8990e307 1736 SV* sv = sv_newmortal();
bbce6d69 1737 if (HeKLEN(entry) == HEf_SVKEY)
1738 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1739 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
463ee0b2 1740 return sv;
1741 }
79072805 1742 }
fde52b5c 1743 return HeVAL(entry);
79072805 1744}
1745
954c1994 1746/*
1747=for apidoc hv_iternextsv
1748
1749Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1750operation.
1751
1752=cut
1753*/
1754
a0d0e21e 1755SV *
864dbfa3 1756Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
a0d0e21e 1757{
1758 HE *he;
1759 if ( (he = hv_iternext(hv)) == NULL)
1760 return NULL;
1761 *key = hv_iterkey(he, retlen);
1762 return hv_iterval(hv, he);
1763}
1764
954c1994 1765/*
1766=for apidoc hv_magic
1767
1768Adds magic to a hash. See C<sv_magic>.
1769
1770=cut
1771*/
1772
79072805 1773void
864dbfa3 1774Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
79072805 1775{
a0d0e21e 1776 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
79072805 1777}
fde52b5c 1778
37d85e3a 1779#if 0 /* use the macro from hv.h instead */
1780
bbce6d69 1781char*
864dbfa3 1782Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
bbce6d69 1783{
ff68c719 1784 return HEK_KEY(share_hek(sv, len, hash));
bbce6d69 1785}
1786
37d85e3a 1787#endif
1788
bbce6d69 1789/* possibly free a shared string if no one has access to it
fde52b5c 1790 * len and hash must both be valid for str.
1791 */
bbce6d69 1792void
864dbfa3 1793Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
fde52b5c 1794{
cbec9347 1795 register XPVHV* xhv;
fde52b5c 1796 register HE *entry;
1797 register HE **oentry;
1798 register I32 i = 1;
1799 I32 found = 0;
c3654f1a 1800 bool is_utf8 = FALSE;
f9a63242 1801 const char *save = str;
c3654f1a 1802
1803 if (len < 0) {
77caf834 1804 STRLEN tmplen = -len;
c3654f1a 1805 is_utf8 = TRUE;
77caf834 1806 /* See the note in hv_fetch(). --jhi */
1807 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
1808 len = tmplen;
c3654f1a 1809 }
1c846c1f 1810
fde52b5c 1811 /* what follows is the moral equivalent of:
6b88bc9c 1812 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
bbce6d69 1813 if (--*Svp == Nullsv)
6b88bc9c 1814 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
bbce6d69 1815 } */
cbec9347 1816 xhv = (XPVHV*)SvANY(PL_strtab);
fde52b5c 1817 /* assert(xhv_array != 0) */
5f08fbcd 1818 LOCK_STRTAB_MUTEX;
cbec9347 1819 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1820 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
bbce6d69 1821 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
fde52b5c 1822 if (HeHASH(entry) != hash) /* strings can't be equal */
1823 continue;
1824 if (HeKLEN(entry) != len)
1825 continue;
1c846c1f 1826 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
fde52b5c 1827 continue;
c3654f1a 1828 if (HeKUTF8(entry) != (char)is_utf8)
1829 continue;
fde52b5c 1830 found = 1;
bbce6d69 1831 if (--HeVAL(entry) == Nullsv) {
1832 *oentry = HeNEXT(entry);
1833 if (i && !*oentry)
cbec9347 1834 xhv->xhv_fill--; /* HvFILL(hv)-- */
ff68c719 1835 Safefree(HeKEY_hek(entry));
d33b2eba 1836 del_HE(entry);
cbec9347 1837 xhv->xhv_keys--; /* HvKEYS(hv)-- */
fde52b5c 1838 }
bbce6d69 1839 break;
fde52b5c 1840 }
333f433b 1841 UNLOCK_STRTAB_MUTEX;
f9a63242 1842 if (str != save)
1843 Safefree(str);
411caa50 1844 if (!found && ckWARN_d(WARN_INTERNAL))
9014280d 1845 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "Attempt to free non-existent shared string '%s'",str);
fde52b5c 1846}
1847
bbce6d69 1848/* get a (constant) string ptr from the global string table
1849 * string will get added if it is not already there.
fde52b5c 1850 * len and hash must both be valid for str.
1851 */
bbce6d69 1852HEK *
864dbfa3 1853Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
fde52b5c 1854{
cbec9347 1855 register XPVHV* xhv;
fde52b5c 1856 register HE *entry;
1857 register HE **oentry;
1858 register I32 i = 1;
1859 I32 found = 0;
da58a35d 1860 bool is_utf8 = FALSE;
f9a63242 1861 const char *save = str;
da58a35d 1862
1863 if (len < 0) {
77caf834 1864 STRLEN tmplen = -len;
da58a35d 1865 is_utf8 = TRUE;
77caf834 1866 /* See the note in hv_fetch(). --jhi */
1867 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
1868 len = tmplen;
da58a35d 1869 }
bbce6d69 1870
fde52b5c 1871 /* what follows is the moral equivalent of:
1c846c1f 1872
6b88bc9c 1873 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
8aacddc1 1874 hv_store(PL_strtab, str, len, Nullsv, hash);
bbce6d69 1875 */
cbec9347 1876 xhv = (XPVHV*)SvANY(PL_strtab);
fde52b5c 1877 /* assert(xhv_array != 0) */
5f08fbcd 1878 LOCK_STRTAB_MUTEX;
cbec9347 1879 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1880 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
bbce6d69 1881 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
fde52b5c 1882 if (HeHASH(entry) != hash) /* strings can't be equal */
1883 continue;
1884 if (HeKLEN(entry) != len)
1885 continue;
1c846c1f 1886 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
fde52b5c 1887 continue;
c3654f1a 1888 if (HeKUTF8(entry) != (char)is_utf8)
1889 continue;
fde52b5c 1890 found = 1;
fde52b5c 1891 break;
1892 }
bbce6d69 1893 if (!found) {
d33b2eba 1894 entry = new_HE();
c3654f1a 1895 HeKEY_hek(entry) = save_hek(str, is_utf8?-len:len, hash);
bbce6d69 1896 HeVAL(entry) = Nullsv;
1897 HeNEXT(entry) = *oentry;
1898 *oentry = entry;
cbec9347 1899 xhv->xhv_keys++; /* HvKEYS(hv)++ */
bbce6d69 1900 if (i) { /* initial entry? */
cbec9347 1901 xhv->xhv_fill++; /* HvFILL(hv)++ */
1902 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
1903 hsplit(PL_strtab);
bbce6d69 1904 }
1905 }
1906
1907 ++HeVAL(entry); /* use value slot as REFCNT */
5f08fbcd 1908 UNLOCK_STRTAB_MUTEX;
f9a63242 1909 if (str != save)
1910 Safefree(str);
ff68c719 1911 return HeKEY_hek(entry);
fde52b5c 1912}