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