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