(Retracted by #7978, too shaky yet.)
[p5sagit/p5-mst-13.2.git] / hv.c
CommitLineData
a0d0e21e 1/* hv.c
79072805 2 *
3818b22b 3 * Copyright (c) 1991-2000, 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
14#include "EXTERN.h"
864dbfa3 15#define PERL_IN_HV_C
79072805 16#include "perl.h"
17
1c846c1f 18
76e3520e 19STATIC HE*
cea2e8a9 20S_new_he(pTHX)
4633a7c4 21{
22 HE* he;
333f433b 23 LOCK_SV_MUTEX;
24 if (!PL_he_root)
25 more_he();
26 he = PL_he_root;
27 PL_he_root = HeNEXT(he);
28 UNLOCK_SV_MUTEX;
29 return he;
4633a7c4 30}
31
76e3520e 32STATIC void
cea2e8a9 33S_del_he(pTHX_ HE *p)
4633a7c4 34{
333f433b 35 LOCK_SV_MUTEX;
3280af22 36 HeNEXT(p) = (HE*)PL_he_root;
37 PL_he_root = p;
333f433b 38 UNLOCK_SV_MUTEX;
4633a7c4 39}
40
333f433b 41STATIC void
cea2e8a9 42S_more_he(pTHX)
4633a7c4 43{
44 register HE* he;
45 register HE* heend;
612f20c3 46 XPV *ptr;
47 New(54, ptr, 1008/sizeof(XPV), XPV);
48 ptr->xpv_pv = (char*)PL_he_arenaroot;
49 PL_he_arenaroot = ptr;
50
51 he = (HE*)ptr;
4633a7c4 52 heend = &he[1008 / sizeof(HE) - 1];
612f20c3 53 PL_he_root = ++he;
4633a7c4 54 while (he < heend) {
fde52b5c 55 HeNEXT(he) = (HE*)(he + 1);
4633a7c4 56 he++;
57 }
fde52b5c 58 HeNEXT(he) = 0;
4633a7c4 59}
60
d33b2eba 61#ifdef PURIFY
62
63#define new_HE() (HE*)safemalloc(sizeof(HE))
64#define del_HE(p) safefree((char*)p)
65
66#else
67
68#define new_HE() new_he()
69#define del_HE(p) del_he(p)
70
71#endif
72
76e3520e 73STATIC HEK *
cea2e8a9 74S_save_hek(pTHX_ const char *str, I32 len, U32 hash)
bbce6d69 75{
76 char *k;
77 register HEK *hek;
1c846c1f 78
ff68c719 79 New(54, k, HEK_BASESIZE + len + 1, char);
bbce6d69 80 hek = (HEK*)k;
ff68c719 81 Copy(str, HEK_KEY(hek), len, char);
82 *(HEK_KEY(hek) + len) = '\0';
83 HEK_LEN(hek) = len;
84 HEK_HASH(hek) = hash;
bbce6d69 85 return hek;
86}
87
88void
864dbfa3 89Perl_unshare_hek(pTHX_ HEK *hek)
bbce6d69 90{
ff68c719 91 unsharepvn(HEK_KEY(hek),HEK_LEN(hek),HEK_HASH(hek));
bbce6d69 92}
93
d18c6117 94#if defined(USE_ITHREADS)
95HE *
96Perl_he_dup(pTHX_ HE *e, bool shared)
97{
98 HE *ret;
99
100 if (!e)
101 return Nullhe;
7766f137 102 /* look for it in the table first */
103 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
104 if (ret)
105 return ret;
106
107 /* create anew and remember what it is */
d33b2eba 108 ret = new_HE();
7766f137 109 ptr_table_store(PL_ptr_table, e, ret);
110
111 HeNEXT(ret) = he_dup(HeNEXT(e),shared);
d18c6117 112 if (HeKLEN(e) == HEf_SVKEY)
113 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e)));
114 else if (shared)
115 HeKEY_hek(ret) = share_hek(HeKEY(e), HeKLEN(e), HeHASH(e));
116 else
117 HeKEY_hek(ret) = save_hek(HeKEY(e), HeKLEN(e), HeHASH(e));
118 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e)));
119 return ret;
120}
121#endif /* USE_ITHREADS */
122
fde52b5c 123/* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
124 * contains an SV* */
125
954c1994 126/*
127=for apidoc hv_fetch
128
129Returns the SV which corresponds to the specified key in the hash. The
130C<klen> is the length of the key. If C<lval> is set then the fetch will be
131part of a store. Check that the return value is non-null before
1c846c1f 132dereferencing it to a C<SV*>.
954c1994 133
96f1132b 134See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
954c1994 135information on how to use this function on tied hashes.
136
137=cut
138*/
139
79072805 140SV**
864dbfa3 141Perl_hv_fetch(pTHX_ HV *hv, const char *key, U32 klen, I32 lval)
79072805 142{
143 register XPVHV* xhv;
fde52b5c 144 register U32 hash;
79072805 145 register HE *entry;
79072805 146 SV *sv;
79072805 147
148 if (!hv)
149 return 0;
463ee0b2 150
8990e307 151 if (SvRMAGICAL(hv)) {
463ee0b2 152 if (mg_find((SV*)hv,'P')) {
11343788 153 dTHR;
8990e307 154 sv = sv_newmortal();
463ee0b2 155 mg_copy((SV*)hv, sv, key, klen);
3280af22 156 PL_hv_fetch_sv = sv;
157 return &PL_hv_fetch_sv;
463ee0b2 158 }
902173a3 159#ifdef ENV_IS_CASELESS
160 else if (mg_find((SV*)hv,'E')) {
e7152ba2 161 U32 i;
162 for (i = 0; i < klen; ++i)
163 if (isLOWER(key[i])) {
79cb57f6 164 char *nkey = strupr(SvPVX(sv_2mortal(newSVpvn(key,klen))));
e7152ba2 165 SV **ret = hv_fetch(hv, nkey, klen, 0);
166 if (!ret && lval)
167 ret = hv_store(hv, key, klen, NEWSV(61,0), 0);
168 return ret;
169 }
902173a3 170 }
171#endif
463ee0b2 172 }
173
79072805 174 xhv = (XPVHV*)SvANY(hv);
175 if (!xhv->xhv_array) {
1c846c1f 176 if (lval
a0d0e21e 177#ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
178 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
179#endif
180 )
d18c6117 181 Newz(503, xhv->xhv_array,
182 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
79072805 183 else
184 return 0;
185 }
186
fde52b5c 187 PERL_HASH(hash, key, klen);
79072805 188
a0d0e21e 189 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
fde52b5c 190 for (; entry; entry = HeNEXT(entry)) {
191 if (HeHASH(entry) != hash) /* strings can't be equal */
79072805 192 continue;
fde52b5c 193 if (HeKLEN(entry) != klen)
79072805 194 continue;
1c846c1f 195 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
79072805 196 continue;
fde52b5c 197 return &HeVAL(entry);
79072805 198 }
a0d0e21e 199#ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
200 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
a6c40364 201 unsigned long len;
202 char *env = PerlEnv_ENVgetenv_len(key,&len);
203 if (env) {
204 sv = newSVpvn(env,len);
205 SvTAINTED_on(sv);
206 return hv_store(hv,key,klen,sv,hash);
207 }
a0d0e21e 208 }
209#endif
79072805 210 if (lval) { /* gonna assign to this, so it better be there */
211 sv = NEWSV(61,0);
e7152ba2 212 return hv_store(hv,key,klen,sv,hash);
79072805 213 }
214 return 0;
215}
216
fde52b5c 217/* returns a HE * structure with the all fields set */
218/* note that hent_val will be a mortal sv for MAGICAL hashes */
954c1994 219/*
220=for apidoc hv_fetch_ent
221
222Returns the hash entry which corresponds to the specified key in the hash.
223C<hash> must be a valid precomputed hash number for the given C<key>, or 0
224if you want the function to compute it. IF C<lval> is set then the fetch
225will be part of a store. Make sure the return value is non-null before
226accessing it. The return value when C<tb> is a tied hash is a pointer to a
227static location, so be sure to make a copy of the structure if you need to
1c846c1f 228store it somewhere.
954c1994 229
96f1132b 230See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
954c1994 231information on how to use this function on tied hashes.
232
233=cut
234*/
235
fde52b5c 236HE *
864dbfa3 237Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
fde52b5c 238{
239 register XPVHV* xhv;
240 register char *key;
241 STRLEN klen;
242 register HE *entry;
243 SV *sv;
244
245 if (!hv)
246 return 0;
247
3c1071f2 248 if (SvUTF8((SV*)hv) && !SvUTF8(keysv))
249 sv_utf8_upgrade(keysv);
250
902173a3 251 if (SvRMAGICAL(hv)) {
252 if (mg_find((SV*)hv,'P')) {
6ff68fdd 253 dTHR;
902173a3 254 sv = sv_newmortal();
255 keysv = sv_2mortal(newSVsv(keysv));
256 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
3280af22 257 if (!HeKEY_hek(&PL_hv_fetch_ent_mh)) {
902173a3 258 char *k;
259 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
3280af22 260 HeKEY_hek(&PL_hv_fetch_ent_mh) = (HEK*)k;
902173a3 261 }
3280af22 262 HeSVKEY_set(&PL_hv_fetch_ent_mh, keysv);
263 HeVAL(&PL_hv_fetch_ent_mh) = sv;
264 return &PL_hv_fetch_ent_mh;
1cf368ac 265 }
902173a3 266#ifdef ENV_IS_CASELESS
267 else if (mg_find((SV*)hv,'E')) {
e7152ba2 268 U32 i;
902173a3 269 key = SvPV(keysv, klen);
e7152ba2 270 for (i = 0; i < klen; ++i)
271 if (isLOWER(key[i])) {
79cb57f6 272 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
e7152ba2 273 (void)strupr(SvPVX(nkeysv));
274 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
275 if (!entry && lval)
276 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
277 return entry;
278 }
902173a3 279 }
280#endif
fde52b5c 281 }
282
effa1e2d 283 xhv = (XPVHV*)SvANY(hv);
fde52b5c 284 if (!xhv->xhv_array) {
1c846c1f 285 if (lval
fde52b5c 286#ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
287 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
288#endif
289 )
d18c6117 290 Newz(503, xhv->xhv_array,
291 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
fde52b5c 292 else
293 return 0;
294 }
295
effa1e2d 296 key = SvPV(keysv, klen);
1c846c1f 297
effa1e2d 298 if (!hash)
299 PERL_HASH(hash, key, klen);
300
fde52b5c 301 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
302 for (; entry; entry = HeNEXT(entry)) {
303 if (HeHASH(entry) != hash) /* strings can't be equal */
304 continue;
305 if (HeKLEN(entry) != klen)
306 continue;
1c846c1f 307 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
fde52b5c 308 continue;
309 return entry;
310 }
311#ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
312 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
a6c40364 313 unsigned long len;
314 char *env = PerlEnv_ENVgetenv_len(key,&len);
315 if (env) {
316 sv = newSVpvn(env,len);
317 SvTAINTED_on(sv);
318 return hv_store_ent(hv,keysv,sv,hash);
319 }
fde52b5c 320 }
321#endif
322 if (lval) { /* gonna assign to this, so it better be there */
323 sv = NEWSV(61,0);
e7152ba2 324 return hv_store_ent(hv,keysv,sv,hash);
fde52b5c 325 }
326 return 0;
327}
328
864dbfa3 329STATIC void
cea2e8a9 330S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
d0066dc7 331{
332 MAGIC *mg = SvMAGIC(hv);
333 *needs_copy = FALSE;
334 *needs_store = TRUE;
335 while (mg) {
336 if (isUPPER(mg->mg_type)) {
337 *needs_copy = TRUE;
338 switch (mg->mg_type) {
339 case 'P':
d0066dc7 340 case 'S':
341 *needs_store = FALSE;
d0066dc7 342 }
343 }
344 mg = mg->mg_moremagic;
345 }
346}
347
954c1994 348/*
349=for apidoc hv_store
350
351Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
352the length of the key. The C<hash> parameter is the precomputed hash
353value; if it is zero then Perl will compute it. The return value will be
354NULL if the operation failed or if the value did not need to be actually
355stored within the hash (as in the case of tied hashes). Otherwise it can
356be dereferenced to get the original C<SV*>. Note that the caller is
357responsible for suitably incrementing the reference count of C<val> before
1c846c1f 358the call, and decrementing it if the function returned NULL.
954c1994 359
96f1132b 360See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
954c1994 361information on how to use this function on tied hashes.
362
363=cut
364*/
365
79072805 366SV**
864dbfa3 367Perl_hv_store(pTHX_ HV *hv, const char *key, U32 klen, SV *val, register U32 hash)
79072805 368{
369 register XPVHV* xhv;
79072805 370 register I32 i;
371 register HE *entry;
372 register HE **oentry;
79072805 373
374 if (!hv)
375 return 0;
376
377 xhv = (XPVHV*)SvANY(hv);
463ee0b2 378 if (SvMAGICAL(hv)) {
d0066dc7 379 bool needs_copy;
380 bool needs_store;
381 hv_magic_check (hv, &needs_copy, &needs_store);
382 if (needs_copy) {
383 mg_copy((SV*)hv, val, key, klen);
384 if (!xhv->xhv_array && !needs_store)
385 return 0;
902173a3 386#ifdef ENV_IS_CASELESS
387 else if (mg_find((SV*)hv,'E')) {
79cb57f6 388 SV *sv = sv_2mortal(newSVpvn(key,klen));
902173a3 389 key = strupr(SvPVX(sv));
390 hash = 0;
391 }
392#endif
d0066dc7 393 }
463ee0b2 394 }
fde52b5c 395 if (!hash)
396 PERL_HASH(hash, key, klen);
397
398 if (!xhv->xhv_array)
d18c6117 399 Newz(505, xhv->xhv_array,
400 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
fde52b5c 401
402 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
403 i = 1;
404
405 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
406 if (HeHASH(entry) != hash) /* strings can't be equal */
407 continue;
408 if (HeKLEN(entry) != klen)
409 continue;
1c846c1f 410 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
fde52b5c 411 continue;
412 SvREFCNT_dec(HeVAL(entry));
413 HeVAL(entry) = val;
414 return &HeVAL(entry);
415 }
416
d33b2eba 417 entry = new_HE();
fde52b5c 418 if (HvSHAREKEYS(hv))
ff68c719 419 HeKEY_hek(entry) = share_hek(key, klen, hash);
fde52b5c 420 else /* gotta do the real thing */
ff68c719 421 HeKEY_hek(entry) = save_hek(key, klen, hash);
fde52b5c 422 HeVAL(entry) = val;
fde52b5c 423 HeNEXT(entry) = *oentry;
424 *oentry = entry;
425
426 xhv->xhv_keys++;
427 if (i) { /* initial entry? */
428 ++xhv->xhv_fill;
429 if (xhv->xhv_keys > xhv->xhv_max)
430 hsplit(hv);
79072805 431 }
432
fde52b5c 433 return &HeVAL(entry);
434}
435
954c1994 436/*
437=for apidoc hv_store_ent
438
439Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
440parameter is the precomputed hash value; if it is zero then Perl will
441compute it. The return value is the new hash entry so created. It will be
442NULL if the operation failed or if the value did not need to be actually
443stored within the hash (as in the case of tied hashes). Otherwise the
444contents of the return value can be accessed using the C<He???> macros
445described here. Note that the caller is responsible for suitably
446incrementing the reference count of C<val> before the call, and
1c846c1f 447decrementing 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
fde52b5c 455HE *
864dbfa3 456Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, register U32 hash)
fde52b5c 457{
458 register XPVHV* xhv;
459 register char *key;
460 STRLEN klen;
461 register I32 i;
462 register HE *entry;
463 register HE **oentry;
464
465 if (!hv)
466 return 0;
467
468 xhv = (XPVHV*)SvANY(hv);
3c1071f2 469
470 if (SvUTF8((SV*)hv) && !SvUTF8(keysv))
471 sv_utf8_upgrade(keysv);
472 else if (SvUTF8(keysv) && !SvUTF8((SV*)hv)) { /* Upgrade hash */
473 SvUTF8_on((SV*)hv);
474 /* XXX Need to save iterator to prevent weird things during "each" */
475 (void)hv_iterinit(hv);
476 while (entry = hv_iternext(hv)) {
477 if (HeKLEN(entry) != HEf_SVKEY) /* Upgrade to SV key */
478 HeSVKEY_set(entry, newSVpvn(HeKEY(entry), HeKLEN(entry)));
479 sv_utf8_upgrade(HeKEY_sv(entry));
480 }
481 }
482
fde52b5c 483 if (SvMAGICAL(hv)) {
aeea060c 484 dTHR;
d0066dc7 485 bool needs_copy;
486 bool needs_store;
487 hv_magic_check (hv, &needs_copy, &needs_store);
488 if (needs_copy) {
3280af22 489 bool save_taint = PL_tainted;
490 if (PL_tainting)
491 PL_tainted = SvTAINTED(keysv);
d0066dc7 492 keysv = sv_2mortal(newSVsv(keysv));
493 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
494 TAINT_IF(save_taint);
495 if (!xhv->xhv_array && !needs_store)
496 return Nullhe;
902173a3 497#ifdef ENV_IS_CASELESS
498 else if (mg_find((SV*)hv,'E')) {
499 key = SvPV(keysv, klen);
79cb57f6 500 keysv = sv_2mortal(newSVpvn(key,klen));
902173a3 501 (void)strupr(SvPVX(keysv));
502 hash = 0;
503 }
504#endif
505 }
fde52b5c 506 }
507
508 key = SvPV(keysv, klen);
902173a3 509
fde52b5c 510 if (!hash)
511 PERL_HASH(hash, key, klen);
512
79072805 513 if (!xhv->xhv_array)
d18c6117 514 Newz(505, xhv->xhv_array,
515 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
79072805 516
a0d0e21e 517 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
79072805 518 i = 1;
519
fde52b5c 520 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
521 if (HeHASH(entry) != hash) /* strings can't be equal */
79072805 522 continue;
fde52b5c 523 if (HeKLEN(entry) != klen)
79072805 524 continue;
1c846c1f 525 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
79072805 526 continue;
fde52b5c 527 SvREFCNT_dec(HeVAL(entry));
528 HeVAL(entry) = val;
529 return entry;
79072805 530 }
79072805 531
d33b2eba 532 entry = new_HE();
fde52b5c 533 if (HvSHAREKEYS(hv))
ff68c719 534 HeKEY_hek(entry) = share_hek(key, klen, hash);
fde52b5c 535 else /* gotta do the real thing */
ff68c719 536 HeKEY_hek(entry) = save_hek(key, klen, hash);
fde52b5c 537 HeVAL(entry) = val;
fde52b5c 538 HeNEXT(entry) = *oentry;
79072805 539 *oentry = entry;
540
463ee0b2 541 xhv->xhv_keys++;
79072805 542 if (i) { /* initial entry? */
463ee0b2 543 ++xhv->xhv_fill;
544 if (xhv->xhv_keys > xhv->xhv_max)
79072805 545 hsplit(hv);
546 }
79072805 547
fde52b5c 548 return entry;
79072805 549}
550
954c1994 551/*
552=for apidoc hv_delete
553
554Deletes a key/value pair in the hash. The value SV is removed from the
1c846c1f 555hash and returned to the caller. The C<klen> is the length of the key.
954c1994 556The C<flags> value will normally be zero; if set to G_DISCARD then NULL
557will be returned.
558
559=cut
560*/
561
79072805 562SV *
864dbfa3 563Perl_hv_delete(pTHX_ HV *hv, const char *key, U32 klen, I32 flags)
79072805 564{
565 register XPVHV* xhv;
79072805 566 register I32 i;
fde52b5c 567 register U32 hash;
79072805 568 register HE *entry;
569 register HE **oentry;
67a38de0 570 SV **svp;
79072805 571 SV *sv;
79072805 572
573 if (!hv)
574 return Nullsv;
8990e307 575 if (SvRMAGICAL(hv)) {
0a0bb7c7 576 bool needs_copy;
577 bool needs_store;
578 hv_magic_check (hv, &needs_copy, &needs_store);
579
67a38de0 580 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
581 sv = *svp;
0a0bb7c7 582 mg_clear(sv);
583 if (!needs_store) {
584 if (mg_find(sv, 'p')) {
585 sv_unmagic(sv, 'p'); /* No longer an element */
586 return sv;
587 }
588 return Nullsv; /* element cannot be deleted */
589 }
902173a3 590#ifdef ENV_IS_CASELESS
2fd1c6b8 591 else if (mg_find((SV*)hv,'E')) {
79cb57f6 592 sv = sv_2mortal(newSVpvn(key,klen));
2fd1c6b8 593 key = strupr(SvPVX(sv));
594 }
902173a3 595#endif
2fd1c6b8 596 }
463ee0b2 597 }
79072805 598 xhv = (XPVHV*)SvANY(hv);
599 if (!xhv->xhv_array)
600 return Nullsv;
fde52b5c 601
602 PERL_HASH(hash, key, klen);
79072805 603
a0d0e21e 604 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
79072805 605 entry = *oentry;
606 i = 1;
fde52b5c 607 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
608 if (HeHASH(entry) != hash) /* strings can't be equal */
79072805 609 continue;
fde52b5c 610 if (HeKLEN(entry) != klen)
79072805 611 continue;
1c846c1f 612 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
79072805 613 continue;
fde52b5c 614 *oentry = HeNEXT(entry);
79072805 615 if (i && !*oentry)
616 xhv->xhv_fill--;
748a9306 617 if (flags & G_DISCARD)
618 sv = Nullsv;
94f7643d 619 else {
79d01fbf 620 sv = sv_2mortal(HeVAL(entry));
94f7643d 621 HeVAL(entry) = &PL_sv_undef;
622 }
a0d0e21e 623 if (entry == xhv->xhv_eiter)
72940dca 624 HvLAZYDEL_on(hv);
a0d0e21e 625 else
68dc0745 626 hv_free_ent(hv, entry);
fde52b5c 627 --xhv->xhv_keys;
628 return sv;
629 }
630 return Nullsv;
631}
632
954c1994 633/*
634=for apidoc hv_delete_ent
635
636Deletes a key/value pair in the hash. The value SV is removed from the
637hash and returned to the caller. The C<flags> value will normally be zero;
638if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
639precomputed hash value, or 0 to ask for it to be computed.
640
641=cut
642*/
643
fde52b5c 644SV *
864dbfa3 645Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
fde52b5c 646{
647 register XPVHV* xhv;
648 register I32 i;
649 register char *key;
650 STRLEN klen;
651 register HE *entry;
652 register HE **oentry;
653 SV *sv;
1c846c1f 654
fde52b5c 655 if (!hv)
656 return Nullsv;
657 if (SvRMAGICAL(hv)) {
0a0bb7c7 658 bool needs_copy;
659 bool needs_store;
660 hv_magic_check (hv, &needs_copy, &needs_store);
661
67a38de0 662 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
0a0bb7c7 663 sv = HeVAL(entry);
664 mg_clear(sv);
665 if (!needs_store) {
666 if (mg_find(sv, 'p')) {
667 sv_unmagic(sv, 'p'); /* No longer an element */
668 return sv;
669 }
670 return Nullsv; /* element cannot be deleted */
671 }
902173a3 672#ifdef ENV_IS_CASELESS
2fd1c6b8 673 else if (mg_find((SV*)hv,'E')) {
674 key = SvPV(keysv, klen);
79cb57f6 675 keysv = sv_2mortal(newSVpvn(key,klen));
2fd1c6b8 676 (void)strupr(SvPVX(keysv));
1c846c1f 677 hash = 0;
2fd1c6b8 678 }
902173a3 679#endif
2fd1c6b8 680 }
fde52b5c 681 }
682 xhv = (XPVHV*)SvANY(hv);
683 if (!xhv->xhv_array)
684 return Nullsv;
685
686 key = SvPV(keysv, klen);
1c846c1f 687
fde52b5c 688 if (!hash)
689 PERL_HASH(hash, key, klen);
690
691 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
692 entry = *oentry;
693 i = 1;
694 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
695 if (HeHASH(entry) != hash) /* strings can't be equal */
696 continue;
697 if (HeKLEN(entry) != klen)
698 continue;
1c846c1f 699 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
fde52b5c 700 continue;
701 *oentry = HeNEXT(entry);
702 if (i && !*oentry)
703 xhv->xhv_fill--;
704 if (flags & G_DISCARD)
705 sv = Nullsv;
94f7643d 706 else {
79d01fbf 707 sv = sv_2mortal(HeVAL(entry));
94f7643d 708 HeVAL(entry) = &PL_sv_undef;
709 }
fde52b5c 710 if (entry == xhv->xhv_eiter)
72940dca 711 HvLAZYDEL_on(hv);
fde52b5c 712 else
68dc0745 713 hv_free_ent(hv, entry);
463ee0b2 714 --xhv->xhv_keys;
79072805 715 return sv;
716 }
79072805 717 return Nullsv;
79072805 718}
719
954c1994 720/*
721=for apidoc hv_exists
722
723Returns a boolean indicating whether the specified hash key exists. The
724C<klen> is the length of the key.
725
726=cut
727*/
728
a0d0e21e 729bool
864dbfa3 730Perl_hv_exists(pTHX_ HV *hv, const char *key, U32 klen)
a0d0e21e 731{
732 register XPVHV* xhv;
fde52b5c 733 register U32 hash;
a0d0e21e 734 register HE *entry;
735 SV *sv;
736
737 if (!hv)
738 return 0;
739
740 if (SvRMAGICAL(hv)) {
741 if (mg_find((SV*)hv,'P')) {
11343788 742 dTHR;
a0d0e21e 743 sv = sv_newmortal();
1c846c1f 744 mg_copy((SV*)hv, sv, key, klen);
a0d0e21e 745 magic_existspack(sv, mg_find(sv, 'p'));
746 return SvTRUE(sv);
747 }
902173a3 748#ifdef ENV_IS_CASELESS
749 else if (mg_find((SV*)hv,'E')) {
79cb57f6 750 sv = sv_2mortal(newSVpvn(key,klen));
902173a3 751 key = strupr(SvPVX(sv));
752 }
753#endif
a0d0e21e 754 }
755
756 xhv = (XPVHV*)SvANY(hv);
f675dbe5 757#ifndef DYNAMIC_ENV_FETCH
a0d0e21e 758 if (!xhv->xhv_array)
1c846c1f 759 return 0;
f675dbe5 760#endif
a0d0e21e 761
fde52b5c 762 PERL_HASH(hash, key, klen);
a0d0e21e 763
f675dbe5 764#ifdef DYNAMIC_ENV_FETCH
765 if (!xhv->xhv_array) entry = Null(HE*);
766 else
767#endif
a0d0e21e 768 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
fde52b5c 769 for (; entry; entry = HeNEXT(entry)) {
770 if (HeHASH(entry) != hash) /* strings can't be equal */
a0d0e21e 771 continue;
fde52b5c 772 if (HeKLEN(entry) != klen)
a0d0e21e 773 continue;
1c846c1f 774 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
fde52b5c 775 continue;
776 return TRUE;
777 }
f675dbe5 778#ifdef DYNAMIC_ENV_FETCH /* is it out there? */
a6c40364 779 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME)) {
780 unsigned long len;
781 char *env = PerlEnv_ENVgetenv_len(key,&len);
782 if (env) {
783 sv = newSVpvn(env,len);
784 SvTAINTED_on(sv);
785 (void)hv_store(hv,key,klen,sv,hash);
786 return TRUE;
787 }
f675dbe5 788 }
789#endif
fde52b5c 790 return FALSE;
791}
792
793
954c1994 794/*
795=for apidoc hv_exists_ent
796
797Returns a boolean indicating whether the specified hash key exists. C<hash>
798can be a valid precomputed hash value, or 0 to ask for it to be
799computed.
800
801=cut
802*/
803
fde52b5c 804bool
864dbfa3 805Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
fde52b5c 806{
807 register XPVHV* xhv;
808 register char *key;
809 STRLEN klen;
810 register HE *entry;
811 SV *sv;
812
813 if (!hv)
814 return 0;
815
816 if (SvRMAGICAL(hv)) {
817 if (mg_find((SV*)hv,'P')) {
e858de61 818 dTHR; /* just for SvTRUE */
fde52b5c 819 sv = sv_newmortal();
effa1e2d 820 keysv = sv_2mortal(newSVsv(keysv));
1c846c1f 821 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
fde52b5c 822 magic_existspack(sv, mg_find(sv, 'p'));
823 return SvTRUE(sv);
824 }
902173a3 825#ifdef ENV_IS_CASELESS
826 else if (mg_find((SV*)hv,'E')) {
827 key = SvPV(keysv, klen);
79cb57f6 828 keysv = sv_2mortal(newSVpvn(key,klen));
902173a3 829 (void)strupr(SvPVX(keysv));
1c846c1f 830 hash = 0;
902173a3 831 }
832#endif
fde52b5c 833 }
834
835 xhv = (XPVHV*)SvANY(hv);
f675dbe5 836#ifndef DYNAMIC_ENV_FETCH
fde52b5c 837 if (!xhv->xhv_array)
1c846c1f 838 return 0;
f675dbe5 839#endif
fde52b5c 840
841 key = SvPV(keysv, klen);
842 if (!hash)
843 PERL_HASH(hash, key, klen);
844
f675dbe5 845#ifdef DYNAMIC_ENV_FETCH
846 if (!xhv->xhv_array) entry = Null(HE*);
847 else
848#endif
fde52b5c 849 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
850 for (; entry; entry = HeNEXT(entry)) {
851 if (HeHASH(entry) != hash) /* strings can't be equal */
852 continue;
853 if (HeKLEN(entry) != klen)
854 continue;
1c846c1f 855 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
a0d0e21e 856 continue;
857 return TRUE;
858 }
f675dbe5 859#ifdef DYNAMIC_ENV_FETCH /* is it out there? */
a6c40364 860 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME)) {
861 unsigned long len;
862 char *env = PerlEnv_ENVgetenv_len(key,&len);
863 if (env) {
864 sv = newSVpvn(env,len);
865 SvTAINTED_on(sv);
866 (void)hv_store_ent(hv,keysv,sv,hash);
867 return TRUE;
868 }
f675dbe5 869 }
870#endif
a0d0e21e 871 return FALSE;
872}
873
76e3520e 874STATIC void
cea2e8a9 875S_hsplit(pTHX_ HV *hv)
79072805 876{
877 register XPVHV* xhv = (XPVHV*)SvANY(hv);
a0d0e21e 878 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
79072805 879 register I32 newsize = oldsize * 2;
880 register I32 i;
72311751 881 register char *a = xhv->xhv_array;
882 register HE **aep;
883 register HE **bep;
79072805 884 register HE *entry;
885 register HE **oentry;
886
3280af22 887 PL_nomemok = TRUE;
8d6dde3e 888#if defined(STRANGE_MALLOC) || defined(MYMALLOC)
d18c6117 889 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
422a93e5 890 if (!a) {
4a33f861 891 PL_nomemok = FALSE;
422a93e5 892 return;
893 }
4633a7c4 894#else
4633a7c4 895#define MALLOC_OVERHEAD 16
d18c6117 896 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
422a93e5 897 if (!a) {
3280af22 898 PL_nomemok = FALSE;
422a93e5 899 return;
900 }
72311751 901 Copy(xhv->xhv_array, a, oldsize * sizeof(HE*), char);
fba3b22e 902 if (oldsize >= 64) {
d18c6117 903 offer_nice_chunk(xhv->xhv_array, PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
4633a7c4 904 }
905 else
906 Safefree(xhv->xhv_array);
907#endif
908
3280af22 909 PL_nomemok = FALSE;
72311751 910 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
79072805 911 xhv->xhv_max = --newsize;
72311751 912 xhv->xhv_array = a;
913 aep = (HE**)a;
79072805 914
72311751 915 for (i=0; i<oldsize; i++,aep++) {
916 if (!*aep) /* non-existent */
79072805 917 continue;
72311751 918 bep = aep+oldsize;
919 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
fde52b5c 920 if ((HeHASH(entry) & newsize) != i) {
921 *oentry = HeNEXT(entry);
72311751 922 HeNEXT(entry) = *bep;
923 if (!*bep)
79072805 924 xhv->xhv_fill++;
72311751 925 *bep = entry;
79072805 926 continue;
927 }
928 else
fde52b5c 929 oentry = &HeNEXT(entry);
79072805 930 }
72311751 931 if (!*aep) /* everything moved */
79072805 932 xhv->xhv_fill--;
933 }
934}
935
72940dca 936void
864dbfa3 937Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
72940dca 938{
939 register XPVHV* xhv = (XPVHV*)SvANY(hv);
940 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
941 register I32 newsize;
942 register I32 i;
943 register I32 j;
72311751 944 register char *a;
945 register HE **aep;
72940dca 946 register HE *entry;
947 register HE **oentry;
948
949 newsize = (I32) newmax; /* possible truncation here */
950 if (newsize != newmax || newmax <= oldsize)
951 return;
952 while ((newsize & (1 + ~newsize)) != newsize) {
953 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
954 }
955 if (newsize < newmax)
956 newsize *= 2;
957 if (newsize < newmax)
958 return; /* overflow detection */
959
72311751 960 a = xhv->xhv_array;
72940dca 961 if (a) {
3280af22 962 PL_nomemok = TRUE;
8d6dde3e 963#if defined(STRANGE_MALLOC) || defined(MYMALLOC)
d18c6117 964 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
422a93e5 965 if (!a) {
4a33f861 966 PL_nomemok = FALSE;
422a93e5 967 return;
968 }
72940dca 969#else
d18c6117 970 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
422a93e5 971 if (!a) {
3280af22 972 PL_nomemok = FALSE;
422a93e5 973 return;
974 }
72311751 975 Copy(xhv->xhv_array, a, oldsize * sizeof(HE*), char);
fba3b22e 976 if (oldsize >= 64) {
d18c6117 977 offer_nice_chunk(xhv->xhv_array, PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
72940dca 978 }
979 else
980 Safefree(xhv->xhv_array);
981#endif
3280af22 982 PL_nomemok = FALSE;
72311751 983 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
72940dca 984 }
985 else {
d18c6117 986 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
72940dca 987 }
988 xhv->xhv_max = --newsize;
72311751 989 xhv->xhv_array = a;
72940dca 990 if (!xhv->xhv_fill) /* skip rest if no entries */
991 return;
992
72311751 993 aep = (HE**)a;
994 for (i=0; i<oldsize; i++,aep++) {
995 if (!*aep) /* non-existent */
72940dca 996 continue;
72311751 997 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
72940dca 998 if ((j = (HeHASH(entry) & newsize)) != i) {
999 j -= i;
1000 *oentry = HeNEXT(entry);
72311751 1001 if (!(HeNEXT(entry) = aep[j]))
72940dca 1002 xhv->xhv_fill++;
72311751 1003 aep[j] = entry;
72940dca 1004 continue;
1005 }
1006 else
1007 oentry = &HeNEXT(entry);
1008 }
72311751 1009 if (!*aep) /* everything moved */
72940dca 1010 xhv->xhv_fill--;
1011 }
1012}
1013
954c1994 1014/*
1015=for apidoc newHV
1016
1017Creates a new HV. The reference count is set to 1.
1018
1019=cut
1020*/
1021
79072805 1022HV *
864dbfa3 1023Perl_newHV(pTHX)
79072805 1024{
1025 register HV *hv;
1026 register XPVHV* xhv;
1027
a0d0e21e 1028 hv = (HV*)NEWSV(502,0);
1029 sv_upgrade((SV *)hv, SVt_PVHV);
79072805 1030 xhv = (XPVHV*)SvANY(hv);
1031 SvPOK_off(hv);
1032 SvNOK_off(hv);
1c846c1f 1033#ifndef NODEFAULT_SHAREKEYS
fde52b5c 1034 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1c846c1f 1035#endif
463ee0b2 1036 xhv->xhv_max = 7; /* start with 8 buckets */
79072805 1037 xhv->xhv_fill = 0;
1038 xhv->xhv_pmroot = 0;
79072805 1039 (void)hv_iterinit(hv); /* so each() will start off right */
1040 return hv;
1041}
1042
b3ac6de7 1043HV *
864dbfa3 1044Perl_newHVhv(pTHX_ HV *ohv)
b3ac6de7 1045{
1046 register HV *hv;
b3ac6de7 1047 STRLEN hv_max = ohv ? HvMAX(ohv) : 0;
1048 STRLEN hv_fill = ohv ? HvFILL(ohv) : 0;
1049
1050 hv = newHV();
1051 while (hv_max && hv_max + 1 >= hv_fill * 2)
1052 hv_max = hv_max / 2; /* Is always 2^n-1 */
4a76a316 1053 HvMAX(hv) = hv_max;
b3ac6de7 1054 if (!hv_fill)
1055 return hv;
1056
1057#if 0
33c27489 1058 if (! SvTIED_mg((SV*)ohv, 'P')) {
b3ac6de7 1059 /* Quick way ???*/
1c846c1f 1060 }
1061 else
b3ac6de7 1062#endif
1063 {
1064 HE *entry;
1065 I32 hv_riter = HvRITER(ohv); /* current root of iterator */
1066 HE *hv_eiter = HvEITER(ohv); /* current entry of iterator */
1067
1068 /* Slow way */
4a76a316 1069 hv_iterinit(ohv);
155aba94 1070 while ((entry = hv_iternext(ohv))) {
1c846c1f 1071 hv_store(hv, HeKEY(entry), HeKLEN(entry),
b3ac6de7 1072 SvREFCNT_inc(HeVAL(entry)), HeHASH(entry));
1073 }
1074 HvRITER(ohv) = hv_riter;
1075 HvEITER(ohv) = hv_eiter;
1076 }
1c846c1f 1077
b3ac6de7 1078 return hv;
1079}
1080
79072805 1081void
864dbfa3 1082Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
79072805 1083{
16bdeea2 1084 SV *val;
1085
68dc0745 1086 if (!entry)
79072805 1087 return;
16bdeea2 1088 val = HeVAL(entry);
257c9e5b 1089 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
3280af22 1090 PL_sub_generation++; /* may be deletion of method from stash */
16bdeea2 1091 SvREFCNT_dec(val);
68dc0745 1092 if (HeKLEN(entry) == HEf_SVKEY) {
1093 SvREFCNT_dec(HeKEY_sv(entry));
1094 Safefree(HeKEY_hek(entry));
44a8e56a 1095 }
1096 else if (HvSHAREKEYS(hv))
68dc0745 1097 unshare_hek(HeKEY_hek(entry));
fde52b5c 1098 else
68dc0745 1099 Safefree(HeKEY_hek(entry));
d33b2eba 1100 del_HE(entry);
79072805 1101}
1102
1103void
864dbfa3 1104Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
79072805 1105{
68dc0745 1106 if (!entry)
79072805 1107 return;
68dc0745 1108 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
3280af22 1109 PL_sub_generation++; /* may be deletion of method from stash */
68dc0745 1110 sv_2mortal(HeVAL(entry)); /* free between statements */
1111 if (HeKLEN(entry) == HEf_SVKEY) {
1112 sv_2mortal(HeKEY_sv(entry));
1113 Safefree(HeKEY_hek(entry));
44a8e56a 1114 }
1115 else if (HvSHAREKEYS(hv))
68dc0745 1116 unshare_hek(HeKEY_hek(entry));
fde52b5c 1117 else
68dc0745 1118 Safefree(HeKEY_hek(entry));
d33b2eba 1119 del_HE(entry);
79072805 1120}
1121
954c1994 1122/*
1123=for apidoc hv_clear
1124
1125Clears a hash, making it empty.
1126
1127=cut
1128*/
1129
79072805 1130void
864dbfa3 1131Perl_hv_clear(pTHX_ HV *hv)
79072805 1132{
1133 register XPVHV* xhv;
1134 if (!hv)
1135 return;
1136 xhv = (XPVHV*)SvANY(hv);
463ee0b2 1137 hfreeentries(hv);
79072805 1138 xhv->xhv_fill = 0;
a0d0e21e 1139 xhv->xhv_keys = 0;
79072805 1140 if (xhv->xhv_array)
463ee0b2 1141 (void)memzero(xhv->xhv_array, (xhv->xhv_max + 1) * sizeof(HE*));
a0d0e21e 1142
1143 if (SvRMAGICAL(hv))
1c846c1f 1144 mg_clear((SV*)hv);
79072805 1145}
1146
76e3520e 1147STATIC void
cea2e8a9 1148S_hfreeentries(pTHX_ HV *hv)
79072805 1149{
a0d0e21e 1150 register HE **array;
68dc0745 1151 register HE *entry;
1152 register HE *oentry = Null(HE*);
a0d0e21e 1153 I32 riter;
1154 I32 max;
79072805 1155
1156 if (!hv)
1157 return;
a0d0e21e 1158 if (!HvARRAY(hv))
79072805 1159 return;
a0d0e21e 1160
1161 riter = 0;
1162 max = HvMAX(hv);
1163 array = HvARRAY(hv);
68dc0745 1164 entry = array[0];
a0d0e21e 1165 for (;;) {
68dc0745 1166 if (entry) {
1167 oentry = entry;
1168 entry = HeNEXT(entry);
1169 hv_free_ent(hv, oentry);
a0d0e21e 1170 }
68dc0745 1171 if (!entry) {
a0d0e21e 1172 if (++riter > max)
1173 break;
68dc0745 1174 entry = array[riter];
1c846c1f 1175 }
79072805 1176 }
a0d0e21e 1177 (void)hv_iterinit(hv);
79072805 1178}
1179
954c1994 1180/*
1181=for apidoc hv_undef
1182
1183Undefines the hash.
1184
1185=cut
1186*/
1187
79072805 1188void
864dbfa3 1189Perl_hv_undef(pTHX_ HV *hv)
79072805 1190{
1191 register XPVHV* xhv;
1192 if (!hv)
1193 return;
1194 xhv = (XPVHV*)SvANY(hv);
463ee0b2 1195 hfreeentries(hv);
79072805 1196 Safefree(xhv->xhv_array);
85e6fe83 1197 if (HvNAME(hv)) {
1198 Safefree(HvNAME(hv));
1199 HvNAME(hv) = 0;
1200 }
79072805 1201 xhv->xhv_array = 0;
aa689395 1202 xhv->xhv_max = 7; /* it's a normal hash */
79072805 1203 xhv->xhv_fill = 0;
a0d0e21e 1204 xhv->xhv_keys = 0;
1205
1206 if (SvRMAGICAL(hv))
1c846c1f 1207 mg_clear((SV*)hv);
79072805 1208}
1209
954c1994 1210/*
1211=for apidoc hv_iterinit
1212
1213Prepares a starting point to traverse a hash table. Returns the number of
1214keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1c846c1f 1215currently only meaningful for hashes without tie magic.
954c1994 1216
1217NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1218hash buckets that happen to be in use. If you still need that esoteric
1219value, you can get it through the macro C<HvFILL(tb)>.
1220
1221=cut
1222*/
1223
79072805 1224I32
864dbfa3 1225Perl_hv_iterinit(pTHX_ HV *hv)
79072805 1226{
aa689395 1227 register XPVHV* xhv;
1228 HE *entry;
1229
1230 if (!hv)
cea2e8a9 1231 Perl_croak(aTHX_ "Bad hash");
aa689395 1232 xhv = (XPVHV*)SvANY(hv);
1233 entry = xhv->xhv_eiter;
72940dca 1234 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1235 HvLAZYDEL_off(hv);
68dc0745 1236 hv_free_ent(hv, entry);
72940dca 1237 }
79072805 1238 xhv->xhv_riter = -1;
1239 xhv->xhv_eiter = Null(HE*);
c6601927 1240 return xhv->xhv_keys; /* used to be xhv->xhv_fill before 5.004_65 */
79072805 1241}
1242
954c1994 1243/*
1244=for apidoc hv_iternext
1245
1246Returns entries from a hash iterator. See C<hv_iterinit>.
1247
1248=cut
1249*/
1250
79072805 1251HE *
864dbfa3 1252Perl_hv_iternext(pTHX_ HV *hv)
79072805 1253{
1254 register XPVHV* xhv;
1255 register HE *entry;
a0d0e21e 1256 HE *oldentry;
463ee0b2 1257 MAGIC* mg;
79072805 1258
1259 if (!hv)
cea2e8a9 1260 Perl_croak(aTHX_ "Bad hash");
79072805 1261 xhv = (XPVHV*)SvANY(hv);
a0d0e21e 1262 oldentry = entry = xhv->xhv_eiter;
463ee0b2 1263
155aba94 1264 if ((mg = SvTIED_mg((SV*)hv, 'P'))) {
8990e307 1265 SV *key = sv_newmortal();
cd1469e6 1266 if (entry) {
fde52b5c 1267 sv_setsv(key, HeSVKEY_force(entry));
cd1469e6 1268 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1269 }
a0d0e21e 1270 else {
ff68c719 1271 char *k;
bbce6d69 1272 HEK *hek;
ff68c719 1273
d33b2eba 1274 xhv->xhv_eiter = entry = new_HE(); /* one HE per MAGICAL hash */
4633a7c4 1275 Zero(entry, 1, HE);
ff68c719 1276 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1277 hek = (HEK*)k;
1278 HeKEY_hek(entry) = hek;
fde52b5c 1279 HeKLEN(entry) = HEf_SVKEY;
a0d0e21e 1280 }
1281 magic_nextpack((SV*) hv,mg,key);
463ee0b2 1282 if (SvOK(key)) {
cd1469e6 1283 /* force key to stay around until next time */
bbce6d69 1284 HeSVKEY_set(entry, SvREFCNT_inc(key));
1285 return entry; /* beware, hent_val is not set */
463ee0b2 1286 }
fde52b5c 1287 if (HeVAL(entry))
1288 SvREFCNT_dec(HeVAL(entry));
ff68c719 1289 Safefree(HeKEY_hek(entry));
d33b2eba 1290 del_HE(entry);
463ee0b2 1291 xhv->xhv_eiter = Null(HE*);
1292 return Null(HE*);
79072805 1293 }
f675dbe5 1294#ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1295 if (!entry && HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME))
1296 prime_env_iter();
1297#endif
463ee0b2 1298
79072805 1299 if (!xhv->xhv_array)
d18c6117 1300 Newz(506, xhv->xhv_array,
1301 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
fde52b5c 1302 if (entry)
1303 entry = HeNEXT(entry);
1304 while (!entry) {
1305 ++xhv->xhv_riter;
1306 if (xhv->xhv_riter > xhv->xhv_max) {
1307 xhv->xhv_riter = -1;
1308 break;
79072805 1309 }
fde52b5c 1310 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1311 }
79072805 1312
72940dca 1313 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1314 HvLAZYDEL_off(hv);
68dc0745 1315 hv_free_ent(hv, oldentry);
72940dca 1316 }
a0d0e21e 1317
79072805 1318 xhv->xhv_eiter = entry;
1319 return entry;
1320}
1321
954c1994 1322/*
1323=for apidoc hv_iterkey
1324
1325Returns the key from the current position of the hash iterator. See
1326C<hv_iterinit>.
1327
1328=cut
1329*/
1330
79072805 1331char *
864dbfa3 1332Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
79072805 1333{
fde52b5c 1334 if (HeKLEN(entry) == HEf_SVKEY) {
fb73857a 1335 STRLEN len;
1336 char *p = SvPV(HeKEY_sv(entry), len);
1337 *retlen = len;
1338 return p;
fde52b5c 1339 }
1340 else {
1341 *retlen = HeKLEN(entry);
1342 return HeKEY(entry);
1343 }
1344}
1345
1346/* unlike hv_iterval(), this always returns a mortal copy of the key */
954c1994 1347/*
1348=for apidoc hv_iterkeysv
1349
1350Returns the key as an C<SV*> from the current position of the hash
1351iterator. The return value will always be a mortal copy of the key. Also
1352see C<hv_iterinit>.
1353
1354=cut
1355*/
1356
fde52b5c 1357SV *
864dbfa3 1358Perl_hv_iterkeysv(pTHX_ register HE *entry)
fde52b5c 1359{
1360 if (HeKLEN(entry) == HEf_SVKEY)
bbce6d69 1361 return sv_mortalcopy(HeKEY_sv(entry));
1c846c1f 1362 else {
1363 return sv_2mortal(newSVpvn_share((HeKLEN(entry) ? HeKEY(entry) : ""),
1364 HeKLEN(entry), HeHASH(entry)));
1365 }
79072805 1366}
1367
954c1994 1368/*
1369=for apidoc hv_iterval
1370
1371Returns the value from the current position of the hash iterator. See
1372C<hv_iterkey>.
1373
1374=cut
1375*/
1376
79072805 1377SV *
864dbfa3 1378Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
79072805 1379{
8990e307 1380 if (SvRMAGICAL(hv)) {
463ee0b2 1381 if (mg_find((SV*)hv,'P')) {
8990e307 1382 SV* sv = sv_newmortal();
bbce6d69 1383 if (HeKLEN(entry) == HEf_SVKEY)
1384 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1385 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
463ee0b2 1386 return sv;
1387 }
79072805 1388 }
fde52b5c 1389 return HeVAL(entry);
79072805 1390}
1391
954c1994 1392/*
1393=for apidoc hv_iternextsv
1394
1395Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1396operation.
1397
1398=cut
1399*/
1400
a0d0e21e 1401SV *
864dbfa3 1402Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
a0d0e21e 1403{
1404 HE *he;
1405 if ( (he = hv_iternext(hv)) == NULL)
1406 return NULL;
1407 *key = hv_iterkey(he, retlen);
1408 return hv_iterval(hv, he);
1409}
1410
954c1994 1411/*
1412=for apidoc hv_magic
1413
1414Adds magic to a hash. See C<sv_magic>.
1415
1416=cut
1417*/
1418
79072805 1419void
864dbfa3 1420Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
79072805 1421{
a0d0e21e 1422 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
79072805 1423}
fde52b5c 1424
bbce6d69 1425char*
864dbfa3 1426Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
bbce6d69 1427{
ff68c719 1428 return HEK_KEY(share_hek(sv, len, hash));
bbce6d69 1429}
1430
1431/* possibly free a shared string if no one has access to it
fde52b5c 1432 * len and hash must both be valid for str.
1433 */
bbce6d69 1434void
864dbfa3 1435Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
fde52b5c 1436{
1437 register XPVHV* xhv;
1438 register HE *entry;
1439 register HE **oentry;
1440 register I32 i = 1;
1441 I32 found = 0;
1c846c1f 1442
fde52b5c 1443 /* what follows is the moral equivalent of:
6b88bc9c 1444 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
bbce6d69 1445 if (--*Svp == Nullsv)
6b88bc9c 1446 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
bbce6d69 1447 } */
3280af22 1448 xhv = (XPVHV*)SvANY(PL_strtab);
fde52b5c 1449 /* assert(xhv_array != 0) */
5f08fbcd 1450 LOCK_STRTAB_MUTEX;
fde52b5c 1451 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
bbce6d69 1452 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
fde52b5c 1453 if (HeHASH(entry) != hash) /* strings can't be equal */
1454 continue;
1455 if (HeKLEN(entry) != len)
1456 continue;
1c846c1f 1457 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
fde52b5c 1458 continue;
1459 found = 1;
bbce6d69 1460 if (--HeVAL(entry) == Nullsv) {
1461 *oentry = HeNEXT(entry);
1462 if (i && !*oentry)
1463 xhv->xhv_fill--;
ff68c719 1464 Safefree(HeKEY_hek(entry));
d33b2eba 1465 del_HE(entry);
bbce6d69 1466 --xhv->xhv_keys;
fde52b5c 1467 }
bbce6d69 1468 break;
fde52b5c 1469 }
333f433b 1470 UNLOCK_STRTAB_MUTEX;
1c846c1f 1471
0453d815 1472 {
1473 dTHR;
1474 if (!found && ckWARN_d(WARN_INTERNAL))
1c846c1f 1475 Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free non-existent shared string '%s'",str);
0453d815 1476 }
fde52b5c 1477}
1478
bbce6d69 1479/* get a (constant) string ptr from the global string table
1480 * string will get added if it is not already there.
fde52b5c 1481 * len and hash must both be valid for str.
1482 */
bbce6d69 1483HEK *
864dbfa3 1484Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
fde52b5c 1485{
1486 register XPVHV* xhv;
1487 register HE *entry;
1488 register HE **oentry;
1489 register I32 i = 1;
1490 I32 found = 0;
bbce6d69 1491
fde52b5c 1492 /* what follows is the moral equivalent of:
1c846c1f 1493
6b88bc9c 1494 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
1495 hv_store(PL_strtab, str, len, Nullsv, hash);
bbce6d69 1496 */
3280af22 1497 xhv = (XPVHV*)SvANY(PL_strtab);
fde52b5c 1498 /* assert(xhv_array != 0) */
5f08fbcd 1499 LOCK_STRTAB_MUTEX;
fde52b5c 1500 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
bbce6d69 1501 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
fde52b5c 1502 if (HeHASH(entry) != hash) /* strings can't be equal */
1503 continue;
1504 if (HeKLEN(entry) != len)
1505 continue;
1c846c1f 1506 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
fde52b5c 1507 continue;
1508 found = 1;
fde52b5c 1509 break;
1510 }
bbce6d69 1511 if (!found) {
d33b2eba 1512 entry = new_HE();
ff68c719 1513 HeKEY_hek(entry) = save_hek(str, len, hash);
bbce6d69 1514 HeVAL(entry) = Nullsv;
1515 HeNEXT(entry) = *oentry;
1516 *oentry = entry;
1517 xhv->xhv_keys++;
1518 if (i) { /* initial entry? */
1519 ++xhv->xhv_fill;
1520 if (xhv->xhv_keys > xhv->xhv_max)
3280af22 1521 hsplit(PL_strtab);
bbce6d69 1522 }
1523 }
1524
1525 ++HeVAL(entry); /* use value slot as REFCNT */
5f08fbcd 1526 UNLOCK_STRTAB_MUTEX;
ff68c719 1527 return HeKEY_hek(entry);
fde52b5c 1528}
1529
bbce6d69 1530
61c8b479 1531