1 /* hv.c 2 * 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others 5 * 6 * You may distribute under the terms of either the GNU General Public 7 * License or the Artistic License, as specified in the README file. 8 * 9 */ 10 11 /* 12 * I sit beside the fire and think 13 * of all that I have seen. 14 * --Bilbo 15 * 16 * [p.278 of _The Lord of the Rings_, II/iii: "The Ring Goes South"] 17 */ 18 19 /* 20 =head1 Hash Manipulation Functions 21 22 A HV structure represents a Perl hash. It consists mainly of an array 23 of pointers, each of which points to a linked list of HE structures. The 24 array is indexed by the hash function of the key, so each linked list 25 represents all the hash entries with the same hash value. Each HE contains 26 a pointer to the actual value, plus a pointer to a HEK structure which 27 holds the key and hash value. 28 29 =cut 30 31 */ 32 33 #include "EXTERN.h" 34 #define PERL_IN_HV_C 35 #define PERL_HASH_INTERNAL_ACCESS 36 #include "perl.h" 37 38 #define DO_HSPLIT(xhv) ((xhv)->xhv_keys > (xhv)->xhv_max) /* HvTOTALKEYS(hv) > HvMAX(hv) */ 39 40 static const char S_strtab_error[] 41 = "Cannot modify shared string table in hv_%s"; 42 43 #ifdef PURIFY 44 45 #define new_HE() (HE*)safemalloc(sizeof(HE)) 46 #define del_HE(p) safefree((char*)p) 47 48 #else 49 50 STATIC HE* 51 S_new_he(pTHX) 52 { 53 dVAR; 54 HE* he; 55 void ** const root = &PL_body_roots[HE_SVSLOT]; 56 57 if (!*root) 58 Perl_more_bodies(aTHX_ HE_SVSLOT, sizeof(HE), PERL_ARENA_SIZE); 59 he = (HE*) *root; 60 assert(he); 61 *root = HeNEXT(he); 62 return he; 63 } 64 65 #define new_HE() new_he() 66 #define del_HE(p) \ 67 STMT_START { \ 68 HeNEXT(p) = (HE*)(PL_body_roots[HE_SVSLOT]); \ 69 PL_body_roots[HE_SVSLOT] = p; \ 70 } STMT_END 71 72 73 74 #endif 75 76 STATIC HEK * 77 S_save_hek_flags(const char *str, I32 len, U32 hash, int flags) 78 { 79 const int flags_masked = flags & HVhek_MASK; 80 char *k; 81 HEK *hek; 82 83 PERL_ARGS_ASSERT_SAVE_HEK_FLAGS; 84 85 Newx(k, HEK_BASESIZE + len + 2, char); 86 hek = (HEK*)k; 87 Copy(str, HEK_KEY(hek), len, char); 88 HEK_KEY(hek)[len] = 0; 89 HEK_LEN(hek) = len; 90 HEK_HASH(hek) = hash; 91 HEK_FLAGS(hek) = (unsigned char)flags_masked | HVhek_UNSHARED; 92 93 if (flags & HVhek_FREEKEY) 94 Safefree(str); 95 return hek; 96 } 97 98 /* free the pool of temporary HE/HEK pairs returned by hv_fetch_ent 99 * for tied hashes */ 100 101 void 102 Perl_free_tied_hv_pool(pTHX) 103 { 104 dVAR; 105 HE *he = PL_hv_fetch_ent_mh; 106 while (he) { 107 HE * const ohe = he; 108 Safefree(HeKEY_hek(he)); 109 he = HeNEXT(he); 110 del_HE(ohe); 111 } 112 PL_hv_fetch_ent_mh = NULL; 113 } 114 115 #if defined(USE_ITHREADS) 116 HEK * 117 Perl_hek_dup(pTHX_ HEK *source, CLONE_PARAMS* param) 118 { 119 HEK *shared; 120 121 PERL_ARGS_ASSERT_HEK_DUP; 122 PERL_UNUSED_ARG(param); 123 124 if (!source) 125 return NULL; 126 127 shared = (HEK*)ptr_table_fetch(PL_ptr_table, source); 128 if (shared) { 129 /* We already shared this hash key. */ 130 (void)share_hek_hek(shared); 131 } 132 else { 133 shared 134 = share_hek_flags(HEK_KEY(source), HEK_LEN(source), 135 HEK_HASH(source), HEK_FLAGS(source)); 136 ptr_table_store(PL_ptr_table, source, shared); 137 } 138 return shared; 139 } 140 141 HE * 142 Perl_he_dup(pTHX_ const HE *e, bool shared, CLONE_PARAMS* param) 143 { 144 HE *ret; 145 146 PERL_ARGS_ASSERT_HE_DUP; 147 148 if (!e) 149 return NULL; 150 /* look for it in the table first */ 151 ret = (HE*)ptr_table_fetch(PL_ptr_table, e); 152 if (ret) 153 return ret; 154 155 /* create anew and remember what it is */ 156 ret = new_HE(); 157 ptr_table_store(PL_ptr_table, e, ret); 158 159 HeNEXT(ret) = he_dup(HeNEXT(e),shared, param); 160 if (HeKLEN(e) == HEf_SVKEY) { 161 char *k; 162 Newx(k, HEK_BASESIZE + sizeof(const SV *), char); 163 HeKEY_hek(ret) = (HEK*)k; 164 HeKEY_sv(ret) = sv_dup_inc(HeKEY_sv(e), param); 165 } 166 else if (shared) { 167 /* This is hek_dup inlined, which seems to be important for speed 168 reasons. */ 169 HEK * const source = HeKEY_hek(e); 170 HEK *shared = (HEK*)ptr_table_fetch(PL_ptr_table, source); 171 172 if (shared) { 173 /* We already shared this hash key. */ 174 (void)share_hek_hek(shared); 175 } 176 else { 177 shared 178 = share_hek_flags(HEK_KEY(source), HEK_LEN(source), 179 HEK_HASH(source), HEK_FLAGS(source)); 180 ptr_table_store(PL_ptr_table, source, shared); 181 } 182 HeKEY_hek(ret) = shared; 183 } 184 else 185 HeKEY_hek(ret) = save_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e), 186 HeKFLAGS(e)); 187 HeVAL(ret) = sv_dup_inc(HeVAL(e), param); 188 return ret; 189 } 190 #endif /* USE_ITHREADS */ 191 192 static void 193 S_hv_notallowed(pTHX_ int flags, const char *key, I32 klen, 194 const char *msg) 195 { 196 SV * const sv = sv_newmortal(); 197 198 PERL_ARGS_ASSERT_HV_NOTALLOWED; 199 200 if (!(flags & HVhek_FREEKEY)) { 201 sv_setpvn(sv, key, klen); 202 } 203 else { 204 /* Need to free saved eventually assign to mortal SV */ 205 /* XXX is this line an error ???: SV *sv = sv_newmortal(); */ 206 sv_usepvn(sv, (char *) key, klen); 207 } 208 if (flags & HVhek_UTF8) { 209 SvUTF8_on(sv); 210 } 211 Perl_croak(aTHX_ msg, SVfARG(sv)); 212 } 213 214 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot 215 * contains an SV* */ 216 217 /* 218 =for apidoc hv_store 219 220 Stores an SV in a hash. The hash key is specified as C<key> and the 221 absolute value of C<klen> is the length of the key. If C<klen> is 222 negative the key is assumed to be in UTF-8-encoded Unicode. The 223 C<hash> parameter is the precomputed hash value; if it is zero then 224 Perl will compute it. 225 226 The return value will be 227 NULL if the operation failed or if the value did not need to be actually 228 stored within the hash (as in the case of tied hashes). Otherwise it can 229 be dereferenced to get the original C<SV*>. Note that the caller is 230 responsible for suitably incrementing the reference count of C<val> before 231 the call, and decrementing it if the function returned NULL. Effectively 232 a successful hv_store takes ownership of one reference to C<val>. This is 233 usually what you want; a newly created SV has a reference count of one, so 234 if all your code does is create SVs then store them in a hash, hv_store 235 will own the only reference to the new SV, and your code doesn't need to do 236 anything further to tidy up. hv_store is not implemented as a call to 237 hv_store_ent, and does not create a temporary SV for the key, so if your 238 key data is not already in SV form then use hv_store in preference to 239 hv_store_ent. 240 241 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more 242 information on how to use this function on tied hashes. 243 244 =for apidoc hv_store_ent 245 246 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash> 247 parameter is the precomputed hash value; if it is zero then Perl will 248 compute it. The return value is the new hash entry so created. It will be 249 NULL if the operation failed or if the value did not need to be actually 250 stored within the hash (as in the case of tied hashes). Otherwise the 251 contents of the return value can be accessed using the C<He?> macros 252 described here. Note that the caller is responsible for suitably 253 incrementing the reference count of C<val> before the call, and 254 decrementing it if the function returned NULL. Effectively a successful 255 hv_store_ent takes ownership of one reference to C<val>. This is 256 usually what you want; a newly created SV has a reference count of one, so 257 if all your code does is create SVs then store them in a hash, hv_store 258 will own the only reference to the new SV, and your code doesn't need to do 259 anything further to tidy up. Note that hv_store_ent only reads the C<key>; 260 unlike C<val> it does not take ownership of it, so maintaining the correct 261 reference count on C<key> is entirely the caller's responsibility. hv_store 262 is not implemented as a call to hv_store_ent, and does not create a temporary 263 SV for the key, so if your key data is not already in SV form then use 264 hv_store in preference to hv_store_ent. 265 266 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more 267 information on how to use this function on tied hashes. 268 269 =for apidoc hv_exists 270 271 Returns a boolean indicating whether the specified hash key exists. The 272 absolute value of C<klen> is the length of the key. If C<klen> is 273 negative the key is assumed to be in UTF-8-encoded Unicode. 274 275 =for apidoc hv_fetch 276 277 Returns the SV which corresponds to the specified key in the hash. 278 The absolute value of C<klen> is the length of the key. If C<klen> is 279 negative the key is assumed to be in UTF-8-encoded Unicode. If 280 C<lval> is set then the fetch will be part of a store. This means that if 281 there is no value in the hash associated with the given key, then one is 282 created and a pointer to it is returned. The C<SV*> it points to can be 283 assigned to. But always check that the 284 return value is non-null before dereferencing it to an C<SV*>. 285 286 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more 287 information on how to use this function on tied hashes. 288 289 =for apidoc hv_exists_ent 290 291 Returns a boolean indicating whether 292 the specified hash key exists. C<hash> 293 can be a valid precomputed hash value, or 0 to ask for it to be 294 computed. 295 296 =cut 297 */ 298 299 /* returns an HE * structure with the all fields set */ 300 /* note that hent_val will be a mortal sv for MAGICAL hashes */ 301 /* 302 =for apidoc hv_fetch_ent 303 304 Returns the hash entry which corresponds to the specified key in the hash. 305 C<hash> must be a valid precomputed hash number for the given C<key>, or 0 306 if you want the function to compute it. IF C<lval> is set then the fetch 307 will be part of a store. Make sure the return value is non-null before 308 accessing it. The return value when C<hv> is a tied hash is a pointer to a 309 static location, so be sure to make a copy of the structure if you need to 310 store it somewhere. 311 312 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more 313 information on how to use this function on tied hashes. 314 315 =cut 316 */ 317 318 /* Common code for hv_delete()/hv_exists()/hv_fetch()/hv_store() */ 319 void * 320 Perl_hv_common_key_len(pTHX_ HV *hv, const char *key, I32 klen_i32, 321 const int action, SV *val, const U32 hash) 322 { 323 STRLEN klen; 324 int flags; 325 326 PERL_ARGS_ASSERT_HV_COMMON_KEY_LEN; 327 328 if (klen_i32 < 0) { 329 klen = -klen_i32; 330 flags = HVhek_UTF8; 331 } else { 332 klen = klen_i32; 333 flags = 0; 334 } 335 return hv_common(hv, NULL, key, klen, flags, action, val, hash); 336 } 337 338 void * 339 Perl_hv_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen, 340 int flags, int action, SV *val, U32 hash) 341 { 342 dVAR; 343 XPVHV* xhv; 344 HE *entry; 345 HE **oentry; 346 SV *sv; 347 bool is_utf8; 348 int masked_flags; 349 const int return_svp = action & HV_FETCH_JUST_SV; 350 351 if (!hv) 352 return NULL; 353 if (SvTYPE(hv) == (svtype)SVTYPEMASK) 354 return NULL; 355 356 assert(SvTYPE(hv) == SVt_PVHV); 357 358 if (SvSMAGICAL(hv) && SvGMAGICAL(hv) && !(action & HV_DISABLE_UVAR_XKEY)) { 359 MAGIC* mg; 360 if ((mg = mg_find((const SV *)hv, PERL_MAGIC_uvar))) { 361 struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr; 362 if (uf->uf_set == NULL) { 363 SV* obj = mg->mg_obj; 364 365 if (!keysv) { 366 keysv = newSVpvn_flags(key, klen, SVs_TEMP | 367 ((flags & HVhek_UTF8) 368 ? SVf_UTF8 : 0)); 369 } 370 371 mg->mg_obj = keysv; /* pass key */ 372 uf->uf_index = action; /* pass action */ 373 magic_getuvar(MUTABLE_SV(hv), mg); 374 keysv = mg->mg_obj; /* may have changed */ 375 mg->mg_obj = obj; 376 377 /* If the key may have changed, then we need to invalidate 378 any passed-in computed hash value. */ 379 hash = 0; 380 } 381 } 382 } 383 if (keysv) { 384 if (flags & HVhek_FREEKEY) 385 Safefree(key); 386 key = SvPV_const(keysv, klen); 387 is_utf8 = (SvUTF8(keysv) != 0); 388 if (SvIsCOW_shared_hash(keysv)) { 389 flags = HVhek_KEYCANONICAL | (is_utf8 ? HVhek_UTF8 : 0); 390 } else { 391 flags = is_utf8 ? HVhek_UTF8 : 0; 392 } 393 } else { 394 is_utf8 = ((flags & HVhek_UTF8) ? TRUE : FALSE); 395 } 396 397 if (action & HV_DELETE) { 398 return (void *) hv_delete_common(hv, keysv, key, klen, 399 flags, action, hash); 400 } 401 402 xhv = (XPVHV*)SvANY(hv); 403 if (SvMAGICAL(hv)) { 404 if (SvRMAGICAL(hv) && !(action & (HV_FETCH_ISSTORE|HV_FETCH_ISEXISTS))) { 405 if (mg_find((const SV *)hv, PERL_MAGIC_tied) 406 || SvGMAGICAL((const SV *)hv)) 407 { 408 /* FIXME should be able to skimp on the HE/HEK here when 409 HV_FETCH_JUST_SV is true. */ 410 if (!keysv) { 411 keysv = newSVpvn_utf8(key, klen, is_utf8); 412 } else { 413 keysv = newSVsv(keysv); 414 } 415 sv = sv_newmortal(); 416 mg_copy(MUTABLE_SV(hv), sv, (char *)keysv, HEf_SVKEY); 417 418 /* grab a fake HE/HEK pair from the pool or make a new one */ 419 entry = PL_hv_fetch_ent_mh; 420 if (entry) 421 PL_hv_fetch_ent_mh = HeNEXT(entry); 422 else { 423 char *k; 424 entry = new_HE(); 425 Newx(k, HEK_BASESIZE + sizeof(const SV *), char); 426 HeKEY_hek(entry) = (HEK*)k; 427 } 428 HeNEXT(entry) = NULL; 429 HeSVKEY_set(entry, keysv); 430 HeVAL(entry) = sv; 431 sv_upgrade(sv, SVt_PVLV); 432 LvTYPE(sv) = 'T'; 433 /* so we can free entry when freeing sv */ 434 LvTARG(sv) = MUTABLE_SV(entry); 435 436 /* XXX remove at some point? */ 437 if (flags & HVhek_FREEKEY) 438 Safefree(key); 439 440 if (return_svp) { 441 return entry ? (void *) &HeVAL(entry) : NULL; 442 } 443 return (void *) entry; 444 } 445 #ifdef ENV_IS_CASELESS 446 else if (mg_find((const SV *)hv, PERL_MAGIC_env)) { 447 U32 i; 448 for (i = 0; i < klen; ++i) 449 if (isLOWER(key[i])) { 450 /* Would be nice if we had a routine to do the 451 copy and upercase in a single pass through. */ 452 const char * const nkey = strupr(savepvn(key,klen)); 453 /* Note that this fetch is for nkey (the uppercased 454 key) whereas the store is for key (the original) */ 455 void *result = hv_common(hv, NULL, nkey, klen, 456 HVhek_FREEKEY, /* free nkey */ 457 0 /* non-LVAL fetch */ 458 | HV_DISABLE_UVAR_XKEY 459 | return_svp, 460 NULL /* no value */, 461 0 /* compute hash */); 462 if (!result && (action & HV_FETCH_LVALUE)) { 463 /* This call will free key if necessary. 464 Do it this way to encourage compiler to tail 465 call optimise. */ 466 result = hv_common(hv, keysv, key, klen, flags, 467 HV_FETCH_ISSTORE 468 | HV_DISABLE_UVAR_XKEY 469 | return_svp, 470 newSV(0), hash); 471 } else { 472 if (flags & HVhek_FREEKEY) 473 Safefree(key); 474 } 475 return result; 476 } 477 } 478 #endif 479 } /* ISFETCH */ 480 else if (SvRMAGICAL(hv) && (action & HV_FETCH_ISEXISTS)) { 481 if (mg_find((const SV *)hv, PERL_MAGIC_tied) 482 || SvGMAGICAL((const SV *)hv)) { 483 /* I don't understand why hv_exists_ent has svret and sv, 484 whereas hv_exists only had one. */ 485 SV * const svret = sv_newmortal(); 486 sv = sv_newmortal(); 487 488 if (keysv || is_utf8) { 489 if (!keysv) { 490 keysv = newSVpvn_utf8(key, klen, TRUE); 491 } else { 492 keysv = newSVsv(keysv); 493 } 494 mg_copy(MUTABLE_SV(hv), sv, (char *)sv_2mortal(keysv), HEf_SVKEY); 495 } else { 496 mg_copy(MUTABLE_SV(hv), sv, key, klen); 497 } 498 if (flags & HVhek_FREEKEY) 499 Safefree(key); 500 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem)); 501 /* This cast somewhat evil, but I'm merely using NULL/ 502 not NULL to return the boolean exists. 503 And I know hv is not NULL. */ 504 return SvTRUE(svret) ? (void *)hv : NULL; 505 } 506 #ifdef ENV_IS_CASELESS 507 else if (mg_find((const SV *)hv, PERL_MAGIC_env)) { 508 /* XXX This code isn't UTF8 clean. */ 509 char * const keysave = (char * const)key; 510 /* Will need to free this, so set FREEKEY flag. */ 511 key = savepvn(key,klen); 512 key = (const char*)strupr((char*)key); 513 is_utf8 = FALSE; 514 hash = 0; 515 keysv = 0; 516 517 if (flags & HVhek_FREEKEY) { 518 Safefree(keysave); 519 } 520 flags |= HVhek_FREEKEY; 521 } 522 #endif 523 } /* ISEXISTS */ 524 else if (action & HV_FETCH_ISSTORE) { 525 bool needs_copy; 526 bool needs_store; 527 hv_magic_check (hv, &needs_copy, &needs_store); 528 if (needs_copy) { 529 const bool save_taint = TAINT_get; 530 if (keysv || is_utf8) { 531 if (!keysv) { 532 keysv = newSVpvn_utf8(key, klen, TRUE); 533 } 534 if (TAINTING_get) 535 TAINT_set(SvTAINTED(keysv)); 536 keysv = sv_2mortal(newSVsv(keysv)); 537 mg_copy(MUTABLE_SV(hv), val, (char*)keysv, HEf_SVKEY); 538 } else { 539 mg_copy(MUTABLE_SV(hv), val, key, klen); 540 } 541 542 TAINT_IF(save_taint); 543 #ifdef NO_TAINT_SUPPORT 544 PERL_UNUSED_VAR(save_taint); 545 #endif 546 if (!needs_store) { 547 if (flags & HVhek_FREEKEY) 548 Safefree(key); 549 return NULL; 550 } 551 #ifdef ENV_IS_CASELESS 552 else if (mg_find((const SV *)hv, PERL_MAGIC_env)) { 553 /* XXX This code isn't UTF8 clean. */ 554 const char *keysave = key; 555 /* Will need to free this, so set FREEKEY flag. */ 556 key = savepvn(key,klen); 557 key = (const char*)strupr((char*)key); 558 is_utf8 = FALSE; 559 hash = 0; 560 keysv = 0; 561 562 if (flags & HVhek_FREEKEY) { 563 Safefree(keysave); 564 } 565 flags |= HVhek_FREEKEY; 566 } 567 #endif 568 } 569 } /* ISSTORE */ 570 } /* SvMAGICAL */ 571 572 if (!HvARRAY(hv)) { 573 if ((action & (HV_FETCH_LVALUE | HV_FETCH_ISSTORE)) 574 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */ 575 || (SvRMAGICAL((const SV *)hv) 576 && mg_find((const SV *)hv, PERL_MAGIC_env)) 577 #endif 578 ) { 579 char *array; 580 Newxz(array, 581 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */), 582 char); 583 HvARRAY(hv) = (HE**)array; 584 } 585 #ifdef DYNAMIC_ENV_FETCH 586 else if (action & HV_FETCH_ISEXISTS) { 587 /* for an %ENV exists, if we do an insert it's by a recursive 588 store call, so avoid creating HvARRAY(hv) right now. */ 589 } 590 #endif 591 else { 592 /* XXX remove at some point? */ 593 if (flags & HVhek_FREEKEY) 594 Safefree(key); 595 596 return NULL; 597 } 598 } 599 600 if (is_utf8 && !(flags & HVhek_KEYCANONICAL)) { 601 char * const keysave = (char *)key; 602 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8); 603 if (is_utf8) 604 flags |= HVhek_UTF8; 605 else 606 flags &= ~HVhek_UTF8; 607 if (key != keysave) { 608 if (flags & HVhek_FREEKEY) 609 Safefree(keysave); 610 flags |= HVhek_WASUTF8 | HVhek_FREEKEY; 611 /* If the caller calculated a hash, it was on the sequence of 612 octets that are the UTF-8 form. We've now changed the sequence 613 of octets stored to that of the equivalent byte representation, 614 so the hash we need is different. */ 615 hash = 0; 616 } 617 } 618 619 if (!hash) { 620 if (keysv && (SvIsCOW_shared_hash(keysv))) 621 hash = SvSHARED_HASH(keysv); 622 else 623 PERL_HASH(hash, key, klen); 624 } 625 626 masked_flags = (flags & HVhek_MASK); 627 628 #ifdef DYNAMIC_ENV_FETCH 629 if (!HvARRAY(hv)) entry = NULL; 630 else 631 #endif 632 { 633 entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; 634 } 635 for (; entry; entry = HeNEXT(entry)) { 636 if (HeHASH(entry) != hash) /* strings can't be equal */ 637 continue; 638 if (HeKLEN(entry) != (I32)klen) 639 continue; 640 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */ 641 continue; 642 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8) 643 continue; 644 645 if (action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE)) { 646 if (HeKFLAGS(entry) != masked_flags) { 647 /* We match if HVhek_UTF8 bit in our flags and hash key's 648 match. But if entry was set previously with HVhek_WASUTF8 649 and key now doesn't (or vice versa) then we should change 650 the key's flag, as this is assignment. */ 651 if (HvSHAREKEYS(hv)) { 652 /* Need to swap the key we have for a key with the flags we 653 need. As keys are shared we can't just write to the 654 flag, so we share the new one, unshare the old one. */ 655 HEK * const new_hek = share_hek_flags(key, klen, hash, 656 masked_flags); 657 unshare_hek (HeKEY_hek(entry)); 658 HeKEY_hek(entry) = new_hek; 659 } 660 else if (hv == PL_strtab) { 661 /* PL_strtab is usually the only hash without HvSHAREKEYS, 662 so putting this test here is cheap */ 663 if (flags & HVhek_FREEKEY) 664 Safefree(key); 665 Perl_croak(aTHX_ S_strtab_error, 666 action & HV_FETCH_LVALUE ? "fetch" : "store"); 667 } 668 else 669 HeKFLAGS(entry) = masked_flags; 670 if (masked_flags & HVhek_ENABLEHVKFLAGS) 671 HvHASKFLAGS_on(hv); 672 } 673 if (HeVAL(entry) == &PL_sv_placeholder) { 674 /* yes, can store into placeholder slot */ 675 if (action & HV_FETCH_LVALUE) { 676 if (SvMAGICAL(hv)) { 677 /* This preserves behaviour with the old hv_fetch 678 implementation which at this point would bail out 679 with a break; (at "if we find a placeholder, we 680 pretend we haven't found anything") 681 682 That break mean that if a placeholder were found, it 683 caused a call into hv_store, which in turn would 684 check magic, and if there is no magic end up pretty 685 much back at this point (in hv_store's code). */ 686 break; 687 } 688 /* LVAL fetch which actually needs a store. */ 689 val = newSV(0); 690 HvPLACEHOLDERS(hv)--; 691 } else { 692 /* store */ 693 if (val != &PL_sv_placeholder) 694 HvPLACEHOLDERS(hv)--; 695 } 696 HeVAL(entry) = val; 697 } else if (action & HV_FETCH_ISSTORE) { 698 SvREFCNT_dec(HeVAL(entry)); 699 HeVAL(entry) = val; 700 } 701 } else if (HeVAL(entry) == &PL_sv_placeholder) { 702 /* if we find a placeholder, we pretend we haven't found 703 anything */ 704 break; 705 } 706 if (flags & HVhek_FREEKEY) 707 Safefree(key); 708 if (return_svp) { 709 return entry ? (void *) &HeVAL(entry) : NULL; 710 } 711 return entry; 712 } 713 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */ 714 if (!(action & HV_FETCH_ISSTORE) 715 && SvRMAGICAL((const SV *)hv) 716 && mg_find((const SV *)hv, PERL_MAGIC_env)) { 717 unsigned long len; 718 const char * const env = PerlEnv_ENVgetenv_len(key,&len); 719 if (env) { 720 sv = newSVpvn(env,len); 721 SvTAINTED_on(sv); 722 return hv_common(hv, keysv, key, klen, flags, 723 HV_FETCH_ISSTORE|HV_DISABLE_UVAR_XKEY|return_svp, 724 sv, hash); 725 } 726 } 727 #endif 728 729 if (!entry && SvREADONLY(hv) && !(action & HV_FETCH_ISEXISTS)) { 730 hv_notallowed(flags, key, klen, 731 "Attempt to access disallowed key '%"SVf"' in" 732 " a restricted hash"); 733 } 734 if (!(action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE))) { 735 /* Not doing some form of store, so return failure. */ 736 if (flags & HVhek_FREEKEY) 737 Safefree(key); 738 return NULL; 739 } 740 if (action & HV_FETCH_LVALUE) { 741 val = action & HV_FETCH_EMPTY_HE ? NULL : newSV(0); 742 if (SvMAGICAL(hv)) { 743 /* At this point the old hv_fetch code would call to hv_store, 744 which in turn might do some tied magic. So we need to make that 745 magic check happen. */ 746 /* gonna assign to this, so it better be there */ 747 /* If a fetch-as-store fails on the fetch, then the action is to 748 recurse once into "hv_store". If we didn't do this, then that 749 recursive call would call the key conversion routine again. 750 However, as we replace the original key with the converted 751 key, this would result in a double conversion, which would show 752 up as a bug if the conversion routine is not idempotent. */ 753 return hv_common(hv, keysv, key, klen, flags, 754 HV_FETCH_ISSTORE|HV_DISABLE_UVAR_XKEY|return_svp, 755 val, hash); 756 /* XXX Surely that could leak if the fetch-was-store fails? 757 Just like the hv_fetch. */ 758 } 759 } 760 761 /* Welcome to hv_store... */ 762 763 if (!HvARRAY(hv)) { 764 /* Not sure if we can get here. I think the only case of oentry being 765 NULL is for %ENV with dynamic env fetch. But that should disappear 766 with magic in the previous code. */ 767 char *array; 768 Newxz(array, 769 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */), 770 char); 771 HvARRAY(hv) = (HE**)array; 772 } 773 774 oentry = &(HvARRAY(hv))[hash & (I32) xhv->xhv_max]; 775 776 entry = new_HE(); 777 /* share_hek_flags will do the free for us. This might be considered 778 bad API design. */ 779 if (HvSHAREKEYS(hv)) 780 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags); 781 else if (hv == PL_strtab) { 782 /* PL_strtab is usually the only hash without HvSHAREKEYS, so putting 783 this test here is cheap */ 784 if (flags & HVhek_FREEKEY) 785 Safefree(key); 786 Perl_croak(aTHX_ S_strtab_error, 787 action & HV_FETCH_LVALUE ? "fetch" : "store"); 788 } 789 else /* gotta do the real thing */ 790 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags); 791 HeVAL(entry) = val; 792 793 #ifdef PERL_HASH_RANDOMIZE_KEYS 794 /* This logic semi-randomizes the insert order in a bucket. 795 * Either we insert into the top, or the slot below the top, 796 * making it harder to see if there is a collision. We also 797 * reset the iterator randomizer if there is one. 798 */ 799 if ( *oentry && PL_HASH_RAND_BITS_ENABLED) { 800 PL_hash_rand_bits++; 801 PL_hash_rand_bits= ROTL_UV(PL_hash_rand_bits,1); 802 if ( PL_hash_rand_bits & 1 ) { 803 HeNEXT(entry) = HeNEXT(*oentry); 804 HeNEXT(*oentry) = entry; 805 } else { 806 HeNEXT(entry) = *oentry; 807 *oentry = entry; 808 } 809 } else 810 #endif 811 { 812 HeNEXT(entry) = *oentry; 813 *oentry = entry; 814 } 815 #ifdef PERL_HASH_RANDOMIZE_KEYS 816 if (SvOOK(hv)) { 817 /* Currently this makes various tests warn in annoying ways. 818 * So Silenced for now. - Yves | bogus end of comment =>* / 819 if (HvAUX(hv)->xhv_riter != -1) { 820 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), 821 "[TESTING] Inserting into a hash during each() traversal results in undefined behavior" 822 pTHX__FORMAT 823 pTHX__VALUE); 824 } 825 */ 826 if (PL_HASH_RAND_BITS_ENABLED) { 827 if (PL_HASH_RAND_BITS_ENABLED == 1) 828 PL_hash_rand_bits += (PTRV)entry + 1; /* we don't bother to use ptr_hash here */ 829 PL_hash_rand_bits= ROTL_UV(PL_hash_rand_bits,1); 830 } 831 HvAUX(hv)->xhv_rand= (U32)PL_hash_rand_bits; 832 } 833 #endif 834 835 if (val == &PL_sv_placeholder) 836 HvPLACEHOLDERS(hv)++; 837 if (masked_flags & HVhek_ENABLEHVKFLAGS) 838 HvHASKFLAGS_on(hv); 839 840 xhv->xhv_keys++; /* HvTOTALKEYS(hv)++ */ 841 if ( DO_HSPLIT(xhv) ) { 842 const STRLEN oldsize = xhv->xhv_max + 1; 843 const U32 items = (U32)HvPLACEHOLDERS_get(hv); 844 845 if (items /* hash has placeholders */ 846 && !SvREADONLY(hv) /* but is not a restricted hash */) { 847 /* If this hash previously was a "restricted hash" and had 848 placeholders, but the "restricted" flag has been turned off, 849 then the placeholders no longer serve any useful purpose. 850 However, they have the downsides of taking up RAM, and adding 851 extra steps when finding used values. It's safe to clear them 852 at this point, even though Storable rebuilds restricted hashes by 853 putting in all the placeholders (first) before turning on the 854 readonly flag, because Storable always pre-splits the hash. 855 If we're lucky, then we may clear sufficient placeholders to 856 avoid needing to split the hash at all. */ 857 clear_placeholders(hv, items); 858 if (DO_HSPLIT(xhv)) 859 hsplit(hv, oldsize, oldsize * 2); 860 } else 861 hsplit(hv, oldsize, oldsize * 2); 862 } 863 864 if (return_svp) { 865 return entry ? (void *) &HeVAL(entry) : NULL; 866 } 867 return (void *) entry; 868 } 869 870 STATIC void 871 S_hv_magic_check(HV *hv, bool *needs_copy, bool *needs_store) 872 { 873 const MAGIC *mg = SvMAGIC(hv); 874 875 PERL_ARGS_ASSERT_HV_MAGIC_CHECK; 876 877 *needs_copy = FALSE; 878 *needs_store = TRUE; 879 while (mg) { 880 if (isUPPER(mg->mg_type)) { 881 *needs_copy = TRUE; 882 if (mg->mg_type == PERL_MAGIC_tied) { 883 *needs_store = FALSE; 884 return; /* We've set all there is to set. */ 885 } 886 } 887 mg = mg->mg_moremagic; 888 } 889 } 890 891 /* 892 =for apidoc hv_scalar 893 894 Evaluates the hash in scalar context and returns the result. Handles magic when the hash is tied. 895 896 =cut 897 */ 898 899 SV * 900 Perl_hv_scalar(pTHX_ HV *hv) 901 { 902 SV *sv; 903 904 PERL_ARGS_ASSERT_HV_SCALAR; 905 906 if (SvRMAGICAL(hv)) { 907 MAGIC * const mg = mg_find((const SV *)hv, PERL_MAGIC_tied); 908 if (mg) 909 return magic_scalarpack(hv, mg); 910 } 911 912 sv = sv_newmortal(); 913 if (HvTOTALKEYS((const HV *)hv)) 914 Perl_sv_setpvf(aTHX_ sv, "%ld/%ld", 915 (long)HvFILL(hv), (long)HvMAX(hv) + 1); 916 else 917 sv_setiv(sv, 0); 918 919 return sv; 920 } 921 922 /* 923 =for apidoc hv_delete 924 925 Deletes a key/value pair in the hash. The value's SV is removed from 926 the hash, made mortal, and returned to the caller. The absolute 927 value of C<klen> is the length of the key. If C<klen> is negative the 928 key is assumed to be in UTF-8-encoded Unicode. The C<flags> value 929 will normally be zero; if set to G_DISCARD then NULL will be returned. 930 NULL will also be returned if the key is not found. 931 932 =for apidoc hv_delete_ent 933 934 Deletes a key/value pair in the hash. The value SV is removed from the hash, 935 made mortal, and returned to the caller. The C<flags> value will normally be 936 zero; if set to G_DISCARD then NULL will be returned. NULL will also be 937 returned if the key is not found. C<hash> can be a valid precomputed hash 938 value, or 0 to ask for it to be computed. 939 940 =cut 941 */ 942 943 STATIC SV * 944 S_hv_delete_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen, 945 int k_flags, I32 d_flags, U32 hash) 946 { 947 dVAR; 948 XPVHV* xhv; 949 HE *entry; 950 HE **oentry; 951 bool is_utf8 = (k_flags & HVhek_UTF8) ? TRUE : FALSE; 952 int masked_flags; 953 954 if (SvRMAGICAL(hv)) { 955 bool needs_copy; 956 bool needs_store; 957 hv_magic_check (hv, &needs_copy, &needs_store); 958 959 if (needs_copy) { 960 SV *sv; 961 entry = (HE *) hv_common(hv, keysv, key, klen, 962 k_flags & ~HVhek_FREEKEY, 963 HV_FETCH_LVALUE|HV_DISABLE_UVAR_XKEY, 964 NULL, hash); 965 sv = entry ? HeVAL(entry) : NULL; 966 if (sv) { 967 if (SvMAGICAL(sv)) { 968 mg_clear(sv); 969 } 970 if (!needs_store) { 971 if (mg_find(sv, PERL_MAGIC_tiedelem)) { 972 /* No longer an element */ 973 sv_unmagic(sv, PERL_MAGIC_tiedelem); 974 return sv; 975 } 976 return NULL; /* element cannot be deleted */ 977 } 978 #ifdef ENV_IS_CASELESS 979 else if (mg_find((const SV *)hv, PERL_MAGIC_env)) { 980 /* XXX This code isn't UTF8 clean. */ 981 keysv = newSVpvn_flags(key, klen, SVs_TEMP); 982 if (k_flags & HVhek_FREEKEY) { 983 Safefree(key); 984 } 985 key = strupr(SvPVX(keysv)); 986 is_utf8 = 0; 987 k_flags = 0; 988 hash = 0; 989 } 990 #endif 991 } 992 } 993 } 994 xhv = (XPVHV*)SvANY(hv); 995 if (!HvARRAY(hv)) 996 return NULL; 997 998 if (is_utf8 && !(k_flags & HVhek_KEYCANONICAL)) { 999 const char * const keysave = key; 1000 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8); 1001 1002 if (is_utf8) 1003 k_flags |= HVhek_UTF8; 1004 else 1005 k_flags &= ~HVhek_UTF8; 1006 if (key != keysave) { 1007 if (k_flags & HVhek_FREEKEY) { 1008 /* This shouldn't happen if our caller does what we expect, 1009 but strictly the API allows it. */ 1010 Safefree(keysave); 1011 } 1012 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY; 1013 } 1014 HvHASKFLAGS_on(MUTABLE_SV(hv)); 1015 } 1016 1017 if (!hash) { 1018 if (keysv && (SvIsCOW_shared_hash(keysv))) 1019 hash = SvSHARED_HASH(keysv); 1020 else 1021 PERL_HASH(hash, key, klen); 1022 } 1023 1024 masked_flags = (k_flags & HVhek_MASK); 1025 1026 oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; 1027 entry = *oentry; 1028 for (; entry; oentry = &HeNEXT(entry), entry = *oentry) { 1029 SV *sv; 1030 U8 mro_changes = 0; /* 1 = isa; 2 = package moved */ 1031 GV *gv = NULL; 1032 HV *stash = NULL; 1033 1034 if (HeHASH(entry) != hash) /* strings can't be equal */ 1035 continue; 1036 if (HeKLEN(entry) != (I32)klen) 1037 continue; 1038 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */ 1039 continue; 1040 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8) 1041 continue; 1042 1043 if (hv == PL_strtab) { 1044 if (k_flags & HVhek_FREEKEY) 1045 Safefree(key); 1046 Perl_croak(aTHX_ S_strtab_error, "delete"); 1047 } 1048 1049 /* if placeholder is here, it's already been deleted.... */ 1050 if (HeVAL(entry) == &PL_sv_placeholder) { 1051 if (k_flags & HVhek_FREEKEY) 1052 Safefree(key); 1053 return NULL; 1054 } 1055 if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry)) 1056 && !SvIsCOW(HeVAL(entry))) { 1057 hv_notallowed(k_flags, key, klen, 1058 "Attempt to delete readonly key '%"SVf"' from" 1059 " a restricted hash"); 1060 } 1061 if (k_flags & HVhek_FREEKEY) 1062 Safefree(key); 1063 1064 /* If this is a stash and the key ends with ::, then someone is 1065 * deleting a package. 1066 */ 1067 if (HeVAL(entry) && HvENAME_get(hv)) { 1068 gv = (GV *)HeVAL(entry); 1069 if (keysv) key = SvPV(keysv, klen); 1070 if (( 1071 (klen > 1 && key[klen-2] == ':' && key[klen-1] == ':') 1072 || 1073 (klen == 1 && key[0] == ':') 1074 ) 1075 && (klen != 6 || hv!=PL_defstash || memNE(key,"main::",6)) 1076 && SvTYPE(gv) == SVt_PVGV && (stash = GvHV((GV *)gv)) 1077 && HvENAME_get(stash)) { 1078 /* A previous version of this code checked that the 1079 * GV was still in the symbol table by fetching the 1080 * GV with its name. That is not necessary (and 1081 * sometimes incorrect), as HvENAME cannot be set 1082 * on hv if it is not in the symtab. */ 1083 mro_changes = 2; 1084 /* Hang on to it for a bit. */ 1085 SvREFCNT_inc_simple_void_NN( 1086 sv_2mortal((SV *)gv) 1087 ); 1088 } 1089 else if (klen == 3 && strnEQ(key, "ISA", 3)) 1090 mro_changes = 1; 1091 } 1092 1093 sv = d_flags & G_DISCARD ? HeVAL(entry) : sv_2mortal(HeVAL(entry)); 1094 HeVAL(entry) = &PL_sv_placeholder; 1095 if (sv) { 1096 /* deletion of method from stash */ 1097 if (isGV(sv) && isGV_with_GP(sv) && GvCVu(sv) 1098 && HvENAME_get(hv)) 1099 mro_method_changed_in(hv); 1100 } 1101 1102 /* 1103 * If a restricted hash, rather than really deleting the entry, put 1104 * a placeholder there. This marks the key as being "approved", so 1105 * we can still access via not-really-existing key without raising 1106 * an error. 1107 */ 1108 if (SvREADONLY(hv)) 1109 /* We'll be saving this slot, so the number of allocated keys 1110 * doesn't go down, but the number placeholders goes up */ 1111 HvPLACEHOLDERS(hv)++; 1112 else { 1113 *oentry = HeNEXT(entry); 1114 if (SvOOK(hv) && entry == HvAUX(hv)->xhv_eiter /* HvEITER(hv) */) 1115 HvLAZYDEL_on(hv); 1116 else { 1117 if (SvOOK(hv) && HvLAZYDEL(hv) && 1118 entry == HeNEXT(HvAUX(hv)->xhv_eiter)) 1119 HeNEXT(HvAUX(hv)->xhv_eiter) = HeNEXT(entry); 1120 hv_free_ent(hv, entry); 1121 } 1122 xhv->xhv_keys--; /* HvTOTALKEYS(hv)-- */ 1123 if (xhv->xhv_keys == 0) 1124 HvHASKFLAGS_off(hv); 1125 } 1126 1127 if (d_flags & G_DISCARD) { 1128 SvREFCNT_dec(sv); 1129 sv = NULL; 1130 } 1131 1132 if (mro_changes == 1) mro_isa_changed_in(hv); 1133 else if (mro_changes == 2) 1134 mro_package_moved(NULL, stash, gv, 1); 1135 1136 return sv; 1137 } 1138 if (SvREADONLY(hv)) { 1139 hv_notallowed(k_flags, key, klen, 1140 "Attempt to delete disallowed key '%"SVf"' from" 1141 " a restricted hash"); 1142 } 1143 1144 if (k_flags & HVhek_FREEKEY) 1145 Safefree(key); 1146 return NULL; 1147 } 1148 1149 STATIC void 1150 S_hsplit(pTHX_ HV *hv, STRLEN const oldsize, STRLEN newsize) 1151 { 1152 dVAR; 1153 STRLEN i = 0; 1154 char *a = (char*) HvARRAY(hv); 1155 HE **aep; 1156 1157 PERL_ARGS_ASSERT_HSPLIT; 1158 1159 /*PerlIO_printf(PerlIO_stderr(), "hsplit called for %p which had %d\n", 1160 (void*)hv, (int) oldsize);*/ 1161 1162 PL_nomemok = TRUE; 1163 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize) 1164 + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char); 1165 if (!a) { 1166 PL_nomemok = FALSE; 1167 return; 1168 } 1169 #ifdef PERL_HASH_RANDOMIZE_KEYS 1170 /* the idea of this is that we create a "random" value by hashing the address of 1171 * the array, we then use the low bit to decide if we insert at the top, or insert 1172 * second from top. After each such insert we rotate the hashed value. So we can 1173 * use the same hashed value over and over, and in normal build environments use 1174 * very few ops to do so. ROTL32() should produce a single machine operation. */ 1175 if (PL_HASH_RAND_BITS_ENABLED) { 1176 if (PL_HASH_RAND_BITS_ENABLED == 1) 1177 PL_hash_rand_bits += ptr_hash((PTRV)a); 1178 PL_hash_rand_bits = ROTL_UV(PL_hash_rand_bits,1); 1179 } 1180 #endif 1181 1182 if (SvOOK(hv)) { 1183 struct xpvhv_aux *const dest 1184 = (struct xpvhv_aux*) &a[newsize * sizeof(HE*)]; 1185 Move(&a[oldsize * sizeof(HE*)], dest, 1, struct xpvhv_aux); 1186 /* we reset the iterator's xhv_rand as well, so they get a totally new ordering */ 1187 #ifdef PERL_HASH_RANDOMIZE_KEYS 1188 dest->xhv_rand = (U32)PL_hash_rand_bits; 1189 #endif 1190 } 1191 1192 PL_nomemok = FALSE; 1193 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/ 1194 HvMAX(hv) = --newsize; 1195 HvARRAY(hv) = (HE**) a; 1196 1197 if (!HvTOTALKEYS(hv)) /* skip rest if no entries */ 1198 return; 1199 1200 aep = (HE**)a; 1201 do { 1202 HE **oentry = aep + i; 1203 HE *entry = aep[i]; 1204 1205 if (!entry) /* non-existent */ 1206 continue; 1207 do { 1208 U32 j = (HeHASH(entry) & newsize); 1209 if (j != (U32)i) { 1210 *oentry = HeNEXT(entry); 1211 #ifdef PERL_HASH_RANDOMIZE_KEYS 1212 /* if the target cell is empty or PL_HASH_RAND_BITS_ENABLED is false 1213 * insert to top, otherwise rotate the bucket rand 1 bit, 1214 * and use the new low bit to decide if we insert at top, 1215 * or next from top. IOW, we only rotate on a collision.*/ 1216 if (aep[j] && PL_HASH_RAND_BITS_ENABLED) { 1217 PL_hash_rand_bits+= ROTL_UV(HeHASH(entry), 17); 1218 PL_hash_rand_bits= ROTL_UV(PL_hash_rand_bits,1); 1219 if (PL_hash_rand_bits & 1) { 1220 HeNEXT(entry)= HeNEXT(aep[j]); 1221 HeNEXT(aep[j])= entry; 1222 } else { 1223 /* Note, this is structured in such a way as the optimizer 1224 * should eliminate the duplicated code here and below without 1225 * us needing to explicitly use a goto. */ 1226 HeNEXT(entry) = aep[j]; 1227 aep[j] = entry; 1228 } 1229 } else 1230 #endif 1231 { 1232 /* see comment above about duplicated code */ 1233 HeNEXT(entry) = aep[j]; 1234 aep[j] = entry; 1235 } 1236 } 1237 else { 1238 oentry = &HeNEXT(entry); 1239 } 1240 entry = *oentry; 1241 } while (entry); 1242 } while (i++ < oldsize); 1243 } 1244 1245 void 1246 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax) 1247 { 1248 dVAR; 1249 XPVHV* xhv = (XPVHV*)SvANY(hv); 1250 const I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */ 1251 I32 newsize; 1252 char *a; 1253 1254 PERL_ARGS_ASSERT_HV_KSPLIT; 1255 1256 newsize = (I32) newmax; /* possible truncation here */ 1257 if (newsize != newmax || newmax <= oldsize) 1258 return; 1259 while ((newsize & (1 + ~newsize)) != newsize) { 1260 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */ 1261 } 1262 if (newsize < newmax) 1263 newsize *= 2; 1264 if (newsize < newmax) 1265 return; /* overflow detection */ 1266 1267 a = (char *) HvARRAY(hv); 1268 if (a) { 1269 hsplit(hv, oldsize, newsize); 1270 } else { 1271 Newxz(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char); 1272 xhv->xhv_max = --newsize; 1273 HvARRAY(hv) = (HE **) a; 1274 } 1275 } 1276 1277 /* IMO this should also handle cases where hv_max is smaller than hv_keys 1278 * as tied hashes could play silly buggers and mess us around. We will 1279 * do the right thing during hv_store() afterwards, but still - Yves */ 1280 #define HV_SET_MAX_ADJUSTED_FOR_KEYS(hv,hv_max,hv_keys) STMT_START {\ 1281 /* Can we use fewer buckets? (hv_max is always 2^n-1) */ \ 1282 if (hv_max < PERL_HASH_DEFAULT_HvMAX) { \ 1283 hv_max = PERL_HASH_DEFAULT_HvMAX; \ 1284 } else { \ 1285 while (hv_max > PERL_HASH_DEFAULT_HvMAX && hv_max + 1 >= hv_keys * 2) \ 1286 hv_max = hv_max / 2; \ 1287 } \ 1288 HvMAX(hv) = hv_max; \ 1289 } STMT_END 1290 1291 1292 HV * 1293 Perl_newHVhv(pTHX_ HV *ohv) 1294 { 1295 dVAR; 1296 HV * const hv = newHV(); 1297 STRLEN hv_max; 1298 1299 if (!ohv || (!HvTOTALKEYS(ohv) && !SvMAGICAL((const SV *)ohv))) 1300 return hv; 1301 hv_max = HvMAX(ohv); 1302 1303 if (!SvMAGICAL((const SV *)ohv)) { 1304 /* It's an ordinary hash, so copy it fast. AMS 20010804 */ 1305 STRLEN i; 1306 const bool shared = !!HvSHAREKEYS(ohv); 1307 HE **ents, ** const oents = (HE **)HvARRAY(ohv); 1308 char *a; 1309 Newx(a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char); 1310 ents = (HE**)a; 1311 1312 /* In each bucket... */ 1313 for (i = 0; i <= hv_max; i++) { 1314 HE *prev = NULL; 1315 HE *oent = oents[i]; 1316 1317 if (!oent) { 1318 ents[i] = NULL; 1319 continue; 1320 } 1321 1322 /* Copy the linked list of entries. */ 1323 for (; oent; oent = HeNEXT(oent)) { 1324 const U32 hash = HeHASH(oent); 1325 const char * const key = HeKEY(oent); 1326 const STRLEN len = HeKLEN(oent); 1327 const int flags = HeKFLAGS(oent); 1328 HE * const ent = new_HE(); 1329 SV *const val = HeVAL(oent); 1330 1331 HeVAL(ent) = SvIMMORTAL(val) ? val : newSVsv(val); 1332 HeKEY_hek(ent) 1333 = shared ? share_hek_flags(key, len, hash, flags) 1334 : save_hek_flags(key, len, hash, flags); 1335 if (prev) 1336 HeNEXT(prev) = ent; 1337 else 1338 ents[i] = ent; 1339 prev = ent; 1340 HeNEXT(ent) = NULL; 1341 } 1342 } 1343 1344 HvMAX(hv) = hv_max; 1345 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv); 1346 HvARRAY(hv) = ents; 1347 } /* not magical */ 1348 else { 1349 /* Iterate over ohv, copying keys and values one at a time. */ 1350 HE *entry; 1351 const I32 riter = HvRITER_get(ohv); 1352 HE * const eiter = HvEITER_get(ohv); 1353 STRLEN hv_keys = HvTOTALKEYS(ohv); 1354 1355 HV_SET_MAX_ADJUSTED_FOR_KEYS(hv,hv_max,hv_keys); 1356 1357 hv_iterinit(ohv); 1358 while ((entry = hv_iternext_flags(ohv, 0))) { 1359 SV *val = hv_iterval(ohv,entry); 1360 SV * const keysv = HeSVKEY(entry); 1361 val = SvIMMORTAL(val) ? val : newSVsv(val); 1362 if (keysv) 1363 (void)hv_store_ent(hv, keysv, val, 0); 1364 else 1365 (void)hv_store_flags(hv, HeKEY(entry), HeKLEN(entry), val, 1366 HeHASH(entry), HeKFLAGS(entry)); 1367 } 1368 HvRITER_set(ohv, riter); 1369 HvEITER_set(ohv, eiter); 1370 } 1371 1372 return hv; 1373 } 1374 1375 /* 1376 =for apidoc Am|HV *|hv_copy_hints_hv|HV *ohv 1377 1378 A specialised version of L</newHVhv> for copying C<%^H>. I<ohv> must be 1379 a pointer to a hash (which may have C<%^H> magic, but should be generally 1380 non-magical), or C<NULL> (interpreted as an empty hash). The content 1381 of I<ohv> is copied to a new hash, which has the C<%^H>-specific magic 1382 added to it. A pointer to the new hash is returned. 1383 1384 =cut 1385 */ 1386 1387 HV * 1388 Perl_hv_copy_hints_hv(pTHX_ HV *const ohv) 1389 { 1390 HV * const hv = newHV(); 1391 1392 if (ohv) { 1393 STRLEN hv_max = HvMAX(ohv); 1394 STRLEN hv_keys = HvTOTALKEYS(ohv); 1395 HE *entry; 1396 const I32 riter = HvRITER_get(ohv); 1397 HE * const eiter = HvEITER_get(ohv); 1398 1399 ENTER; 1400 SAVEFREESV(hv); 1401 1402 HV_SET_MAX_ADJUSTED_FOR_KEYS(hv,hv_max,hv_keys); 1403 1404 hv_iterinit(ohv); 1405 while ((entry = hv_iternext_flags(ohv, 0))) { 1406 SV *const sv = newSVsv(hv_iterval(ohv,entry)); 1407 SV *heksv = HeSVKEY(entry); 1408 if (!heksv && sv) heksv = newSVhek(HeKEY_hek(entry)); 1409 if (sv) sv_magic(sv, NULL, PERL_MAGIC_hintselem, 1410 (char *)heksv, HEf_SVKEY); 1411 if (heksv == HeSVKEY(entry)) 1412 (void)hv_store_ent(hv, heksv, sv, 0); 1413 else { 1414 (void)hv_common(hv, heksv, HeKEY(entry), HeKLEN(entry), 1415 HeKFLAGS(entry), HV_FETCH_ISSTORE|HV_FETCH_JUST_SV, sv, HeHASH(entry)); 1416 SvREFCNT_dec_NN(heksv); 1417 } 1418 } 1419 HvRITER_set(ohv, riter); 1420 HvEITER_set(ohv, eiter); 1421 1422 SvREFCNT_inc_simple_void_NN(hv); 1423 LEAVE; 1424 } 1425 hv_magic(hv, NULL, PERL_MAGIC_hints); 1426 return hv; 1427 } 1428 #undef HV_SET_MAX_ADJUSTED_FOR_KEYS 1429 1430 /* like hv_free_ent, but returns the SV rather than freeing it */ 1431 STATIC SV* 1432 S_hv_free_ent_ret(pTHX_ HV *hv, HE *entry) 1433 { 1434 dVAR; 1435 SV *val; 1436 1437 PERL_ARGS_ASSERT_HV_FREE_ENT_RET; 1438 1439 val = HeVAL(entry); 1440 if (HeKLEN(entry) == HEf_SVKEY) { 1441 SvREFCNT_dec(HeKEY_sv(entry)); 1442 Safefree(HeKEY_hek(entry)); 1443 } 1444 else if (HvSHAREKEYS(hv)) 1445 unshare_hek(HeKEY_hek(entry)); 1446 else 1447 Safefree(HeKEY_hek(entry)); 1448 del_HE(entry); 1449 return val; 1450 } 1451 1452 1453 void 1454 Perl_hv_free_ent(pTHX_ HV *hv, HE *entry) 1455 { 1456 dVAR; 1457 SV *val; 1458 1459 PERL_ARGS_ASSERT_HV_FREE_ENT; 1460 1461 if (!entry) 1462 return; 1463 val = hv_free_ent_ret(hv, entry); 1464 SvREFCNT_dec(val); 1465 } 1466 1467 1468 void 1469 Perl_hv_delayfree_ent(pTHX_ HV *hv, HE *entry) 1470 { 1471 dVAR; 1472 1473 PERL_ARGS_ASSERT_HV_DELAYFREE_ENT; 1474 1475 if (!entry) 1476 return; 1477 /* SvREFCNT_inc to counter the SvREFCNT_dec in hv_free_ent */ 1478 sv_2mortal(SvREFCNT_inc(HeVAL(entry))); /* free between statements */ 1479 if (HeKLEN(entry) == HEf_SVKEY) { 1480 sv_2mortal(SvREFCNT_inc(HeKEY_sv(entry))); 1481 } 1482 hv_free_ent(hv, entry); 1483 } 1484 1485 /* 1486 =for apidoc hv_clear 1487 1488 Frees the all the elements of a hash, leaving it empty. 1489 The XS equivalent of C<%hash = ()>. See also L</hv_undef>. 1490 1491 If any destructors are triggered as a result, the hv itself may 1492 be freed. 1493 1494 =cut 1495 */ 1496 1497 void 1498 Perl_hv_clear(pTHX_ HV *hv) 1499 { 1500 dVAR; 1501 XPVHV* xhv; 1502 if (!hv) 1503 return; 1504 1505 DEBUG_A(Perl_hv_assert(aTHX_ hv)); 1506 1507 xhv = (XPVHV*)SvANY(hv); 1508 1509 ENTER; 1510 SAVEFREESV(SvREFCNT_inc_simple_NN(hv)); 1511 if (SvREADONLY(hv) && HvARRAY(hv) != NULL) { 1512 /* restricted hash: convert all keys to placeholders */ 1513 STRLEN i; 1514 for (i = 0; i <= xhv->xhv_max; i++) { 1515 HE *entry = (HvARRAY(hv))[i]; 1516 for (; entry; entry = HeNEXT(entry)) { 1517 /* not already placeholder */ 1518 if (HeVAL(entry) != &PL_sv_placeholder) { 1519 if (HeVAL(entry)) { 1520 if (SvREADONLY(HeVAL(entry)) && !SvIsCOW(HeVAL(entry))) { 1521 SV* const keysv = hv_iterkeysv(entry); 1522 Perl_croak_nocontext( 1523 "Attempt to delete readonly key '%"SVf"' from a restricted hash", 1524 (void*)keysv); 1525 } 1526 SvREFCNT_dec_NN(HeVAL(entry)); 1527 } 1528 HeVAL(entry) = &PL_sv_placeholder; 1529 HvPLACEHOLDERS(hv)++; 1530 } 1531 } 1532 } 1533 } 1534 else { 1535 hfreeentries(hv); 1536 HvPLACEHOLDERS_set(hv, 0); 1537 1538 if (SvRMAGICAL(hv)) 1539 mg_clear(MUTABLE_SV(hv)); 1540 1541 HvHASKFLAGS_off(hv); 1542 } 1543 if (SvOOK(hv)) { 1544 if(HvENAME_get(hv)) 1545 mro_isa_changed_in(hv); 1546 HvEITER_set(hv, NULL); 1547 } 1548 LEAVE; 1549 } 1550 1551 /* 1552 =for apidoc hv_clear_placeholders 1553 1554 Clears any placeholders from a hash. If a restricted hash has any of its keys 1555 marked as readonly and the key is subsequently deleted, the key is not actually 1556 deleted but is marked by assigning it a value of &PL_sv_placeholder. This tags 1557 it so it will be ignored by future operations such as iterating over the hash, 1558 but will still allow the hash to have a value reassigned to the key at some 1559 future point. This function clears any such placeholder keys from the hash. 1560 See Hash::Util::lock_keys() for an example of its use. 1561 1562 =cut 1563 */ 1564 1565 void 1566 Perl_hv_clear_placeholders(pTHX_ HV *hv) 1567 { 1568 dVAR; 1569 const U32 items = (U32)HvPLACEHOLDERS_get(hv); 1570 1571 PERL_ARGS_ASSERT_HV_CLEAR_PLACEHOLDERS; 1572 1573 if (items) 1574 clear_placeholders(hv, items); 1575 } 1576 1577 static void 1578 S_clear_placeholders(pTHX_ HV *hv, U32 items) 1579 { 1580 dVAR; 1581 I32 i; 1582 1583 PERL_ARGS_ASSERT_CLEAR_PLACEHOLDERS; 1584 1585 if (items == 0) 1586 return; 1587 1588 i = HvMAX(hv); 1589 do { 1590 /* Loop down the linked list heads */ 1591 HE **oentry = &(HvARRAY(hv))[i]; 1592 HE *entry; 1593 1594 while ((entry = *oentry)) { 1595 if (HeVAL(entry) == &PL_sv_placeholder) { 1596 *oentry = HeNEXT(entry); 1597 if (entry == HvEITER_get(hv)) 1598 HvLAZYDEL_on(hv); 1599 else { 1600 if (SvOOK(hv) && HvLAZYDEL(hv) && 1601 entry == HeNEXT(HvAUX(hv)->xhv_eiter)) 1602 HeNEXT(HvAUX(hv)->xhv_eiter) = HeNEXT(entry); 1603 hv_free_ent(hv, entry); 1604 } 1605 1606 if (--items == 0) { 1607 /* Finished. */ 1608 HvTOTALKEYS(hv) -= (IV)HvPLACEHOLDERS_get(hv); 1609 if (HvUSEDKEYS(hv) == 0) 1610 HvHASKFLAGS_off(hv); 1611 HvPLACEHOLDERS_set(hv, 0); 1612 return; 1613 } 1614 } else { 1615 oentry = &HeNEXT(entry); 1616 } 1617 } 1618 } while (--i >= 0); 1619 /* You can't get here, hence assertion should always fail. */ 1620 assert (items == 0); 1621 assert (0); 1622 } 1623 1624 STATIC void 1625 S_hfreeentries(pTHX_ HV *hv) 1626 { 1627 STRLEN index = 0; 1628 XPVHV * const xhv = (XPVHV*)SvANY(hv); 1629 SV *sv; 1630 1631 PERL_ARGS_ASSERT_HFREEENTRIES; 1632 1633 while ((sv = Perl_hfree_next_entry(aTHX_ hv, &index))||xhv->xhv_keys) { 1634 SvREFCNT_dec(sv); 1635 } 1636 } 1637 1638 1639 /* hfree_next_entry() 1640 * For use only by S_hfreeentries() and sv_clear(). 1641 * Delete the next available HE from hv and return the associated SV. 1642 * Returns null on empty hash. Nevertheless null is not a reliable 1643 * indicator that the hash is empty, as the deleted entry may have a 1644 * null value. 1645 * indexp is a pointer to the current index into HvARRAY. The index should 1646 * initially be set to 0. hfree_next_entry() may update it. */ 1647 1648 SV* 1649 Perl_hfree_next_entry(pTHX_ HV *hv, STRLEN *indexp) 1650 { 1651 struct xpvhv_aux *iter; 1652 HE *entry; 1653 HE ** array; 1654 #ifdef DEBUGGING 1655 STRLEN orig_index = *indexp; 1656 #endif 1657 1658 PERL_ARGS_ASSERT_HFREE_NEXT_ENTRY; 1659 1660 if (SvOOK(hv) && ((iter = HvAUX(hv))) 1661 && ((entry = iter->xhv_eiter)) ) 1662 { 1663 /* the iterator may get resurrected after each 1664 * destructor call, so check each time */ 1665 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */ 1666 HvLAZYDEL_off(hv); 1667 hv_free_ent(hv, entry); 1668 /* warning: at this point HvARRAY may have been 1669 * re-allocated, HvMAX changed etc */ 1670 } 1671 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */ 1672 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */ 1673 #ifdef PERL_HASH_RANDOMIZE_KEYS 1674 iter->xhv_last_rand = iter->xhv_rand; 1675 #endif 1676 } 1677 1678 if (!((XPVHV*)SvANY(hv))->xhv_keys) 1679 return NULL; 1680 1681 array = HvARRAY(hv); 1682 assert(array); 1683 while ( ! ((entry = array[*indexp])) ) { 1684 if ((*indexp)++ >= HvMAX(hv)) 1685 *indexp = 0; 1686 assert(*indexp != orig_index); 1687 } 1688 array[*indexp] = HeNEXT(entry); 1689 ((XPVHV*) SvANY(hv))->xhv_keys--; 1690 1691 if ( PL_phase != PERL_PHASE_DESTRUCT && HvENAME(hv) 1692 && HeVAL(entry) && isGV(HeVAL(entry)) 1693 && GvHV(HeVAL(entry)) && HvENAME(GvHV(HeVAL(entry))) 1694 ) { 1695 STRLEN klen; 1696 const char * const key = HePV(entry,klen); 1697 if ((klen > 1 && key[klen-1]==':' && key[klen-2]==':') 1698 || (klen == 1 && key[0] == ':')) { 1699 mro_package_moved( 1700 NULL, GvHV(HeVAL(entry)), 1701 (GV *)HeVAL(entry), 0 1702 ); 1703 } 1704 } 1705 return hv_free_ent_ret(hv, entry); 1706 } 1707 1708 1709 /* 1710 =for apidoc hv_undef 1711 1712 Undefines the hash. The XS equivalent of C<undef(%hash)>. 1713 1714 As well as freeing all the elements of the hash (like hv_clear()), this 1715 also frees any auxiliary data and storage associated with the hash. 1716 1717 If any destructors are triggered as a result, the hv itself may 1718 be freed. 1719 1720 See also L</hv_clear>. 1721 1722 =cut 1723 */ 1724 1725 void 1726 Perl_hv_undef_flags(pTHX_ HV *hv, U32 flags) 1727 { 1728 dVAR; 1729 XPVHV* xhv; 1730 const char *name; 1731 bool save; 1732 1733 if (!hv) 1734 return; 1735 DEBUG_A(Perl_hv_assert(aTHX_ hv)); 1736 xhv = (XPVHV*)SvANY(hv); 1737 save = !!SvREFCNT(hv); 1738 1739 /* The name must be deleted before the call to hfreeeeentries so that 1740 CVs are anonymised properly. But the effective name must be pre- 1741 served until after that call (and only deleted afterwards if the 1742 call originated from sv_clear). For stashes with one name that is 1743 both the canonical name and the effective name, hv_name_set has to 1744 allocate an array for storing the effective name. We can skip that 1745 during global destruction, as it does not matter where the CVs point 1746 if they will be freed anyway. */ 1747 /* note that the code following prior to hfreeentries is duplicated 1748 * in sv_clear(), and changes here should be done there too */ 1749 if (PL_phase != PERL_PHASE_DESTRUCT && (name = HvNAME(hv))) { 1750 if (PL_stashcache) { 1751 DEBUG_o(Perl_deb(aTHX_ "hv_undef_flags clearing PL_stashcache for '%" 1752 HEKf"'\n", HvNAME_HEK(hv))); 1753 (void)hv_delete(PL_stashcache, name, 1754 HEK_UTF8(HvNAME_HEK(hv)) ? -HvNAMELEN_get(hv) : HvNAMELEN_get(hv), 1755 G_DISCARD 1756 ); 1757 } 1758 hv_name_set(hv, NULL, 0, 0); 1759 } 1760 if (save) { 1761 ENTER; 1762 SAVEFREESV(SvREFCNT_inc_simple_NN(hv)); 1763 } 1764 hfreeentries(hv); 1765 if (SvOOK(hv)) { 1766 struct xpvhv_aux * const aux = HvAUX(hv); 1767 struct mro_meta *meta; 1768 1769 if ((name = HvENAME_get(hv))) { 1770 if (PL_phase != PERL_PHASE_DESTRUCT) 1771 mro_isa_changed_in(hv); 1772 if (PL_stashcache) { 1773 DEBUG_o(Perl_deb(aTHX_ "hv_undef_flags clearing PL_stashcache for effective name '%" 1774 HEKf"'\n", HvENAME_HEK(hv))); 1775 (void)hv_delete( 1776 PL_stashcache, name, 1777 HEK_UTF8(HvENAME_HEK(hv)) ? -HvENAMELEN_get(hv) : HvENAMELEN_get(hv), 1778 G_DISCARD 1779 ); 1780 } 1781 } 1782 1783 /* If this call originated from sv_clear, then we must check for 1784 * effective names that need freeing, as well as the usual name. */ 1785 name = HvNAME(hv); 1786 if (flags & HV_NAME_SETALL ? !!aux->xhv_name_u.xhvnameu_name : !!name) { 1787 if (name && PL_stashcache) { 1788 DEBUG_o(Perl_deb(aTHX_ "hv_undef_flags clearing PL_stashcache for name '%" 1789 HEKf"'\n", HvNAME_HEK(hv))); 1790 (void)hv_delete(PL_stashcache, name, (HEK_UTF8(HvNAME_HEK(hv)) ? -HvNAMELEN_get(hv) : HvNAMELEN_get(hv)), G_DISCARD); 1791 } 1792 hv_name_set(hv, NULL, 0, flags); 1793 } 1794 if((meta = aux->xhv_mro_meta)) { 1795 if (meta->mro_linear_all) { 1796 SvREFCNT_dec_NN(meta->mro_linear_all); 1797 /* mro_linear_current is just acting as a shortcut pointer, 1798 hence the else. */ 1799 } 1800 else 1801 /* Only the current MRO is stored, so this owns the data. 1802 */ 1803 SvREFCNT_dec(meta->mro_linear_current); 1804 SvREFCNT_dec(meta->mro_nextmethod); 1805 SvREFCNT_dec(meta->isa); 1806 Safefree(meta); 1807 aux->xhv_mro_meta = NULL; 1808 } 1809 SvREFCNT_dec(aux->xhv_super); 1810 if (!aux->xhv_name_u.xhvnameu_name && ! aux->xhv_backreferences) 1811 SvFLAGS(hv) &= ~SVf_OOK; 1812 } 1813 if (!SvOOK(hv)) { 1814 Safefree(HvARRAY(hv)); 1815 xhv->xhv_max = PERL_HASH_DEFAULT_HvMAX; /* HvMAX(hv) = 7 (it's a normal hash) */ 1816 HvARRAY(hv) = 0; 1817 } 1818 /* if we're freeing the HV, the SvMAGIC field has been reused for 1819 * other purposes, and so there can't be any placeholder magic */ 1820 if (SvREFCNT(hv)) 1821 HvPLACEHOLDERS_set(hv, 0); 1822 1823 if (SvRMAGICAL(hv)) 1824 mg_clear(MUTABLE_SV(hv)); 1825 if (save) LEAVE; 1826 } 1827 1828 /* 1829 =for apidoc hv_fill 1830 1831 Returns the number of hash buckets that happen to be in use. This function is 1832 wrapped by the macro C<HvFILL>. 1833 1834 Previously this value was stored in the HV structure, rather than being 1835 calculated on demand. 1836 1837 =cut 1838 */ 1839 1840 STRLEN 1841 Perl_hv_fill(pTHX_ HV const *const hv) 1842 { 1843 STRLEN count = 0; 1844 HE **ents = HvARRAY(hv); 1845 1846 PERL_ARGS_ASSERT_HV_FILL; 1847 1848 if (ents) { 1849 HE *const *const last = ents + HvMAX(hv); 1850 count = last + 1 - ents; 1851 1852 do { 1853 if (!*ents) 1854 --count; 1855 } while (++ents <= last); 1856 } 1857 return count; 1858 } 1859 1860 /* hash a pointer to a U32 - Used in the hash traversal randomization 1861 * and bucket order randomization code 1862 * 1863 * this code was derived from Sereal, which was derived from autobox. 1864 */ 1865 1866 PERL_STATIC_INLINE U32 S_ptr_hash(PTRV u) { 1867 #if PTRSIZE == 8 1868 /* 1869 * This is one of Thomas Wang's hash functions for 64-bit integers from: 1870 * http://www.concentric.net/~Ttwang/tech/inthash.htm 1871 */ 1872 u = (~u) + (u << 18); 1873 u = u ^ (u >> 31); 1874 u = u * 21; 1875 u = u ^ (u >> 11); 1876 u = u + (u << 6); 1877 u = u ^ (u >> 22); 1878 #else 1879 /* 1880 * This is one of Bob Jenkins' hash functions for 32-bit integers 1881 * from: http://burtleburtle.net/bob/hash/integer.html 1882 */ 1883 u = (u + 0x7ed55d16) + (u << 12); 1884 u = (u ^ 0xc761c23c) ^ (u >> 19); 1885 u = (u + 0x165667b1) + (u << 5); 1886 u = (u + 0xd3a2646c) ^ (u << 9); 1887 u = (u + 0xfd7046c5) + (u << 3); 1888 u = (u ^ 0xb55a4f09) ^ (u >> 16); 1889 #endif 1890 return (U32)u; 1891 } 1892 1893 1894 static struct xpvhv_aux* 1895 S_hv_auxinit(pTHX_ HV *hv) { 1896 struct xpvhv_aux *iter; 1897 char *array; 1898 1899 PERL_ARGS_ASSERT_HV_AUXINIT; 1900 1901 if (!SvOOK(hv)) { 1902 if (!HvARRAY(hv)) { 1903 Newxz(array, PERL_HV_ARRAY_ALLOC_BYTES(HvMAX(hv) + 1) 1904 + sizeof(struct xpvhv_aux), char); 1905 } else { 1906 array = (char *) HvARRAY(hv); 1907 Renew(array, PERL_HV_ARRAY_ALLOC_BYTES(HvMAX(hv) + 1) 1908 + sizeof(struct xpvhv_aux), char); 1909 } 1910 HvARRAY(hv) = (HE**)array; 1911 SvOOK_on(hv); 1912 iter = HvAUX(hv); 1913 #ifdef PERL_HASH_RANDOMIZE_KEYS 1914 if (PL_HASH_RAND_BITS_ENABLED) { 1915 /* mix in some new state to PL_hash_rand_bits to "randomize" the traversal order*/ 1916 if (PL_HASH_RAND_BITS_ENABLED == 1) 1917 PL_hash_rand_bits += ptr_hash((PTRV)array); 1918 PL_hash_rand_bits = ROTL_UV(PL_hash_rand_bits,1); 1919 } 1920 iter->xhv_rand = (U32)PL_hash_rand_bits; 1921 #endif 1922 } else { 1923 iter = HvAUX(hv); 1924 } 1925 1926 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */ 1927 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */ 1928 #ifdef PERL_HASH_RANDOMIZE_KEYS 1929 iter->xhv_last_rand = iter->xhv_rand; 1930 #endif 1931 iter->xhv_name_u.xhvnameu_name = 0; 1932 iter->xhv_name_count = 0; 1933 iter->xhv_backreferences = 0; 1934 iter->xhv_mro_meta = NULL; 1935 iter->xhv_super = NULL; 1936 return iter; 1937 } 1938 1939 /* 1940 =for apidoc hv_iterinit 1941 1942 Prepares a starting point to traverse a hash table. Returns the number of 1943 keys in the hash (i.e. the same as C<HvUSEDKEYS(hv)>). The return value is 1944 currently only meaningful for hashes without tie magic. 1945 1946 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of 1947 hash buckets that happen to be in use. If you still need that esoteric 1948 value, you can get it through the macro C<HvFILL(hv)>. 1949 1950 1951 =cut 1952 */ 1953 1954 I32 1955 Perl_hv_iterinit(pTHX_ HV *hv) 1956 { 1957 PERL_ARGS_ASSERT_HV_ITERINIT; 1958 1959 /* FIXME: Are we not NULL, or do we croak? Place bets now! */ 1960 1961 if (!hv) 1962 Perl_croak(aTHX_ "Bad hash"); 1963 1964 if (SvOOK(hv)) { 1965 struct xpvhv_aux * const iter = HvAUX(hv); 1966 HE * const entry = iter->xhv_eiter; /* HvEITER(hv) */ 1967 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */ 1968 HvLAZYDEL_off(hv); 1969 hv_free_ent(hv, entry); 1970 } 1971 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */ 1972 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */ 1973 #ifdef PERL_HASH_RANDOMIZE_KEYS 1974 iter->xhv_last_rand = iter->xhv_rand; 1975 #endif 1976 } else { 1977 hv_auxinit(hv); 1978 } 1979 1980 /* used to be xhv->xhv_fill before 5.004_65 */ 1981 return HvTOTALKEYS(hv); 1982 } 1983 1984 I32 * 1985 Perl_hv_riter_p(pTHX_ HV *hv) { 1986 struct xpvhv_aux *iter; 1987 1988 PERL_ARGS_ASSERT_HV_RITER_P; 1989 1990 if (!hv) 1991 Perl_croak(aTHX_ "Bad hash"); 1992 1993 iter = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv); 1994 return &(iter->xhv_riter); 1995 } 1996 1997 HE ** 1998 Perl_hv_eiter_p(pTHX_ HV *hv) { 1999 struct xpvhv_aux *iter; 2000 2001 PERL_ARGS_ASSERT_HV_EITER_P; 2002 2003 if (!hv) 2004 Perl_croak(aTHX_ "Bad hash"); 2005 2006 iter = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv); 2007 return &(iter->xhv_eiter); 2008 } 2009 2010 void 2011 Perl_hv_riter_set(pTHX_ HV *hv, I32 riter) { 2012 struct xpvhv_aux *iter; 2013 2014 PERL_ARGS_ASSERT_HV_RITER_SET; 2015 2016 if (!hv) 2017 Perl_croak(aTHX_ "Bad hash"); 2018 2019 if (SvOOK(hv)) { 2020 iter = HvAUX(hv); 2021 } else { 2022 if (riter == -1) 2023 return; 2024 2025 iter = hv_auxinit(hv); 2026 } 2027 iter->xhv_riter = riter; 2028 } 2029 2030 void 2031 Perl_hv_rand_set(pTHX_ HV *hv, U32 new_xhv_rand) { 2032 struct xpvhv_aux *iter; 2033 2034 PERL_ARGS_ASSERT_HV_RAND_SET; 2035 2036 #ifdef PERL_HASH_RANDOMIZE_KEYS 2037 if (!hv) 2038 Perl_croak(aTHX_ "Bad hash"); 2039 2040 if (SvOOK(hv)) { 2041 iter = HvAUX(hv); 2042 } else { 2043 iter = hv_auxinit(hv); 2044 } 2045 iter->xhv_rand = new_xhv_rand; 2046 #else 2047 Perl_croak(aTHX_ "This Perl has not been built with support for randomized hash key traversal but something called Perl_hv_rand_set()."); 2048 #endif 2049 } 2050 2051 void 2052 Perl_hv_eiter_set(pTHX_ HV *hv, HE *eiter) { 2053 struct xpvhv_aux *iter; 2054 2055 PERL_ARGS_ASSERT_HV_EITER_SET; 2056 2057 if (!hv) 2058 Perl_croak(aTHX_ "Bad hash"); 2059 2060 if (SvOOK(hv)) { 2061 iter = HvAUX(hv); 2062 } else { 2063 /* 0 is the default so don't go malloc()ing a new structure just to 2064 hold 0. */ 2065 if (!eiter) 2066 return; 2067 2068 iter = hv_auxinit(hv); 2069 } 2070 iter->xhv_eiter = eiter; 2071 } 2072 2073 void 2074 Perl_hv_name_set(pTHX_ HV *hv, const char *name, U32 len, U32 flags) 2075 { 2076 dVAR; 2077 struct xpvhv_aux *iter; 2078 U32 hash; 2079 HEK **spot; 2080 2081 PERL_ARGS_ASSERT_HV_NAME_SET; 2082 2083 if (len > I32_MAX) 2084 Perl_croak(aTHX_ "panic: hv name too long (%"UVuf")", (UV) len); 2085 2086 if (SvOOK(hv)) { 2087 iter = HvAUX(hv); 2088 if (iter->xhv_name_u.xhvnameu_name) { 2089 if(iter->xhv_name_count) { 2090 if(flags & HV_NAME_SETALL) { 2091 HEK ** const name = HvAUX(hv)->xhv_name_u.xhvnameu_names; 2092 HEK **hekp = name + ( 2093 iter->xhv_name_count < 0 2094 ? -iter->xhv_name_count 2095 : iter->xhv_name_count 2096 ); 2097 while(hekp-- > name+1) 2098 unshare_hek_or_pvn(*hekp, 0, 0, 0); 2099 /* The first elem may be null. */ 2100 if(*name) unshare_hek_or_pvn(*name, 0, 0, 0); 2101 Safefree(name); 2102 spot = &iter->xhv_name_u.xhvnameu_name; 2103 iter->xhv_name_count = 0; 2104 } 2105 else { 2106 if(iter->xhv_name_count > 0) { 2107 /* shift some things over */ 2108 Renew( 2109 iter->xhv_name_u.xhvnameu_names, iter->xhv_name_count + 1, HEK * 2110 ); 2111 spot = iter->xhv_name_u.xhvnameu_names; 2112 spot[iter->xhv_name_count] = spot[1]; 2113 spot[1] = spot[0]; 2114 iter->xhv_name_count = -(iter->xhv_name_count + 1); 2115 } 2116 else if(*(spot = iter->xhv_name_u.xhvnameu_names)) { 2117 unshare_hek_or_pvn(*spot, 0, 0, 0); 2118 } 2119 } 2120 } 2121 else if (flags & HV_NAME_SETALL) { 2122 unshare_hek_or_pvn(iter->xhv_name_u.xhvnameu_name, 0, 0, 0); 2123 spot = &iter->xhv_name_u.xhvnameu_name; 2124 } 2125 else { 2126 HEK * const existing_name = iter->xhv_name_u.xhvnameu_name; 2127 Newx(iter->xhv_name_u.xhvnameu_names, 2, HEK *); 2128 iter->xhv_name_count = -2; 2129 spot = iter->xhv_name_u.xhvnameu_names; 2130 spot[1] = existing_name; 2131 } 2132 } 2133 else { spot = &iter->xhv_name_u.xhvnameu_name; iter->xhv_name_count = 0; } 2134 } else { 2135 if (name == 0) 2136 return; 2137 2138 iter = hv_auxinit(hv); 2139 spot = &iter->xhv_name_u.xhvnameu_name; 2140 } 2141 PERL_HASH(hash, name, len); 2142 *spot = name ? share_hek(name, flags & SVf_UTF8 ? -(I32)len : (I32)len, hash) : NULL; 2143 } 2144 2145 /* 2146 This is basically sv_eq_flags() in sv.c, but we avoid the magic 2147 and bytes checking. 2148 */ 2149 2150 STATIC I32 2151 hek_eq_pvn_flags(pTHX_ const HEK *hek, const char* pv, const I32 pvlen, const U32 flags) { 2152 if ( (HEK_UTF8(hek) ? 1 : 0) != (flags & SVf_UTF8 ? 1 : 0) ) { 2153 if (flags & SVf_UTF8) 2154 return (bytes_cmp_utf8( 2155 (const U8*)HEK_KEY(hek), HEK_LEN(hek), 2156 (const U8*)pv, pvlen) == 0); 2157 else 2158 return (bytes_cmp_utf8( 2159 (const U8*)pv, pvlen, 2160 (const U8*)HEK_KEY(hek), HEK_LEN(hek)) == 0); 2161 } 2162 else 2163 return HEK_LEN(hek) == pvlen && ((HEK_KEY(hek) == pv) 2164 || memEQ(HEK_KEY(hek), pv, pvlen)); 2165 } 2166 2167 /* 2168 =for apidoc hv_ename_add 2169 2170 Adds a name to a stash's internal list of effective names. See 2171 C<hv_ename_delete>. 2172 2173 This is called when a stash is assigned to a new location in the symbol 2174 table. 2175 2176 =cut 2177 */ 2178 2179 void 2180 Perl_hv_ename_add(pTHX_ HV *hv, const char *name, U32 len, U32 flags) 2181 { 2182 dVAR; 2183 struct xpvhv_aux *aux = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv); 2184 U32 hash; 2185 2186 PERL_ARGS_ASSERT_HV_ENAME_ADD; 2187 2188 if (len > I32_MAX) 2189 Perl_croak(aTHX_ "panic: hv name too long (%"UVuf")", (UV) len); 2190 2191 PERL_HASH(hash, name, len); 2192 2193 if (aux->xhv_name_count) { 2194 HEK ** const xhv_name = aux->xhv_name_u.xhvnameu_names; 2195 I32 count = aux->xhv_name_count; 2196 HEK **hekp = xhv_name + (count < 0 ? -count : count); 2197 while (hekp-- > xhv_name) 2198 if ( 2199 (HEK_UTF8(*hekp) || (flags & SVf_UTF8)) 2200 ? hek_eq_pvn_flags(aTHX_ *hekp, name, (I32)len, flags) 2201 : (HEK_LEN(*hekp) == (I32)len && memEQ(HEK_KEY(*hekp), name, len)) 2202 ) { 2203 if (hekp == xhv_name && count < 0) 2204 aux->xhv_name_count = -count; 2205 return; 2206 } 2207 if (count < 0) aux->xhv_name_count--, count = -count; 2208 else aux->xhv_name_count++; 2209 Renew(aux->xhv_name_u.xhvnameu_names, count + 1, HEK *); 2210 (aux->xhv_name_u.xhvnameu_names)[count] = share_hek(name, (flags & SVf_UTF8 ? -(I32)len : (I32)len), hash); 2211 } 2212 else { 2213 HEK *existing_name = aux->xhv_name_u.xhvnameu_name; 2214 if ( 2215 existing_name && ( 2216 (HEK_UTF8(existing_name) || (flags & SVf_UTF8)) 2217 ? hek_eq_pvn_flags(aTHX_ existing_name, name, (I32)len, flags) 2218 : (HEK_LEN(existing_name) == (I32)len && memEQ(HEK_KEY(existing_name), name, len)) 2219 ) 2220 ) return; 2221 Newx(aux->xhv_name_u.xhvnameu_names, 2, HEK *); 2222 aux->xhv_name_count = existing_name ? 2 : -2; 2223 *aux->xhv_name_u.xhvnameu_names = existing_name; 2224 (aux->xhv_name_u.xhvnameu_names)[1] = share_hek(name, (flags & SVf_UTF8 ? -(I32)len : (I32)len), hash); 2225 } 2226 } 2227 2228 /* 2229 =for apidoc hv_ename_delete 2230 2231 Removes a name from a stash's internal list of effective names. If this is 2232 the name returned by C<HvENAME>, then another name in the list will take 2233 its place (C<HvENAME> will use it). 2234 2235 This is called when a stash is deleted from the symbol table. 2236 2237 =cut 2238 */ 2239 2240 void 2241 Perl_hv_ename_delete(pTHX_ HV *hv, const char *name, U32 len, U32 flags) 2242 { 2243 dVAR; 2244 struct xpvhv_aux *aux; 2245 2246 PERL_ARGS_ASSERT_HV_ENAME_DELETE; 2247 2248 if (len > I32_MAX) 2249 Perl_croak(aTHX_ "panic: hv name too long (%"UVuf")", (UV) len); 2250 2251 if (!SvOOK(hv)) return; 2252 2253 aux = HvAUX(hv); 2254 if (!aux->xhv_name_u.xhvnameu_name) return; 2255 2256 if (aux->xhv_name_count) { 2257 HEK ** const namep = aux->xhv_name_u.xhvnameu_names; 2258 I32 const count = aux->xhv_name_count; 2259 HEK **victim = namep + (count < 0 ? -count : count); 2260 while (victim-- > namep + 1) 2261 if ( 2262 (HEK_UTF8(*victim) || (flags & SVf_UTF8)) 2263 ? hek_eq_pvn_flags(aTHX_ *victim, name, (I32)len, flags) 2264 : (HEK_LEN(*victim) == (I32)len && memEQ(HEK_KEY(*victim), name, len)) 2265 ) { 2266 unshare_hek_or_pvn(*victim, 0, 0, 0); 2267 if (count < 0) ++aux->xhv_name_count; 2268 else --aux->xhv_name_count; 2269 if ( 2270 (aux->xhv_name_count == 1 || aux->xhv_name_count == -1) 2271 && !*namep 2272 ) { /* if there are none left */ 2273 Safefree(namep); 2274 aux->xhv_name_u.xhvnameu_names = NULL; 2275 aux->xhv_name_count = 0; 2276 } 2277 else { 2278 /* Move the last one back to fill the empty slot. It 2279 does not matter what order they are in. */ 2280 *victim = *(namep + (count < 0 ? -count : count) - 1); 2281 } 2282 return; 2283 } 2284 if ( 2285 count > 0 && (HEK_UTF8(*namep) || (flags & SVf_UTF8)) 2286 ? hek_eq_pvn_flags(aTHX_ *namep, name, (I32)len, flags) 2287 : (HEK_LEN(*namep) == (I32)len && memEQ(HEK_KEY(*namep), name, len)) 2288 ) { 2289 aux->xhv_name_count = -count; 2290 } 2291 } 2292 else if( 2293 (HEK_UTF8(aux->xhv_name_u.xhvnameu_name) || (flags & SVf_UTF8)) 2294 ? hek_eq_pvn_flags(aTHX_ aux->xhv_name_u.xhvnameu_name, name, (I32)len, flags) 2295 : (HEK_LEN(aux->xhv_name_u.xhvnameu_name) == (I32)len && 2296 memEQ(HEK_KEY(aux->xhv_name_u.xhvnameu_name), name, len)) 2297 ) { 2298 HEK * const namehek = aux->xhv_name_u.xhvnameu_name; 2299 Newx(aux->xhv_name_u.xhvnameu_names, 1, HEK *); 2300 *aux->xhv_name_u.xhvnameu_names = namehek; 2301 aux->xhv_name_count = -1; 2302 } 2303 } 2304 2305 AV ** 2306 Perl_hv_backreferences_p(pTHX_ HV *hv) { 2307 struct xpvhv_aux * const iter = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv); 2308 2309 PERL_ARGS_ASSERT_HV_BACKREFERENCES_P; 2310 PERL_UNUSED_CONTEXT; 2311 2312 return &(iter->xhv_backreferences); 2313 } 2314 2315 void 2316 Perl_hv_kill_backrefs(pTHX_ HV *hv) { 2317 AV *av; 2318 2319 PERL_ARGS_ASSERT_HV_KILL_BACKREFS; 2320 2321 if (!SvOOK(hv)) 2322 return; 2323 2324 av = HvAUX(hv)->xhv_backreferences; 2325 2326 if (av) { 2327 HvAUX(hv)->xhv_backreferences = 0; 2328 Perl_sv_kill_backrefs(aTHX_ MUTABLE_SV(hv), av); 2329 if (SvTYPE(av) == SVt_PVAV) 2330 SvREFCNT_dec_NN(av); 2331 } 2332 } 2333 2334 /* 2335 hv_iternext is implemented as a macro in hv.h 2336 2337 =for apidoc hv_iternext 2338 2339 Returns entries from a hash iterator. See C<hv_iterinit>. 2340 2341 You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the 2342 iterator currently points to, without losing your place or invalidating your 2343 iterator. Note that in this case the current entry is deleted from the hash 2344 with your iterator holding the last reference to it. Your iterator is flagged 2345 to free the entry on the next call to C<hv_iternext>, so you must not discard 2346 your iterator immediately else the entry will leak - call C<hv_iternext> to 2347 trigger the resource deallocation. 2348 2349 =for apidoc hv_iternext_flags 2350 2351 Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>. 2352 The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is 2353 set the placeholders keys (for restricted hashes) will be returned in addition 2354 to normal keys. By default placeholders are automatically skipped over. 2355 Currently a placeholder is implemented with a value that is 2356 C<&PL_sv_placeholder>. Note that the implementation of placeholders and 2357 restricted hashes may change, and the implementation currently is 2358 insufficiently abstracted for any change to be tidy. 2359 2360 =cut 2361 */ 2362 2363 HE * 2364 Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags) 2365 { 2366 dVAR; 2367 XPVHV* xhv; 2368 HE *entry; 2369 HE *oldentry; 2370 MAGIC* mg; 2371 struct xpvhv_aux *iter; 2372 2373 PERL_ARGS_ASSERT_HV_ITERNEXT_FLAGS; 2374 2375 if (!hv) 2376 Perl_croak(aTHX_ "Bad hash"); 2377 2378 xhv = (XPVHV*)SvANY(hv); 2379 2380 if (!SvOOK(hv)) { 2381 /* Too many things (well, pp_each at least) merrily assume that you can 2382 call hv_iternext without calling hv_iterinit, so we'll have to deal 2383 with it. */ 2384 hv_iterinit(hv); 2385 } 2386 iter = HvAUX(hv); 2387 2388 oldentry = entry = iter->xhv_eiter; /* HvEITER(hv) */ 2389 if (SvMAGICAL(hv) && SvRMAGICAL(hv)) { 2390 if ( ( mg = mg_find((const SV *)hv, PERL_MAGIC_tied) ) ) { 2391 SV * const key = sv_newmortal(); 2392 if (entry) { 2393 sv_setsv(key, HeSVKEY_force(entry)); 2394 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */ 2395 HeSVKEY_set(entry, NULL); 2396 } 2397 else { 2398 char *k; 2399 HEK *hek; 2400 2401 /* one HE per MAGICAL hash */ 2402 iter->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */ 2403 HvLAZYDEL_on(hv); /* make sure entry gets freed */ 2404 Zero(entry, 1, HE); 2405 Newxz(k, HEK_BASESIZE + sizeof(const SV *), char); 2406 hek = (HEK*)k; 2407 HeKEY_hek(entry) = hek; 2408 HeKLEN(entry) = HEf_SVKEY; 2409 } 2410 magic_nextpack(MUTABLE_SV(hv),mg,key); 2411 if (SvOK(key)) { 2412 /* force key to stay around until next time */ 2413 HeSVKEY_set(entry, SvREFCNT_inc_simple_NN(key)); 2414 return entry; /* beware, hent_val is not set */ 2415 } 2416 SvREFCNT_dec(HeVAL(entry)); 2417 Safefree(HeKEY_hek(entry)); 2418 del_HE(entry); 2419 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */ 2420 HvLAZYDEL_off(hv); 2421 return NULL; 2422 } 2423 } 2424 #if defined(DYNAMIC_ENV_FETCH) && !defined(__riscos__) /* set up %ENV for iteration */ 2425 if (!entry && SvRMAGICAL((const SV *)hv) 2426 && mg_find((const SV *)hv, PERL_MAGIC_env)) { 2427 prime_env_iter(); 2428 #ifdef VMS 2429 /* The prime_env_iter() on VMS just loaded up new hash values 2430 * so the iteration count needs to be reset back to the beginning 2431 */ 2432 hv_iterinit(hv); 2433 iter = HvAUX(hv); 2434 oldentry = entry = iter->xhv_eiter; /* HvEITER(hv) */ 2435 #endif 2436 } 2437 #endif 2438 2439 /* hv_iterinit now ensures this. */ 2440 assert (HvARRAY(hv)); 2441 2442 /* At start of hash, entry is NULL. */ 2443 if (entry) 2444 { 2445 entry = HeNEXT(entry); 2446 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) { 2447 /* 2448 * Skip past any placeholders -- don't want to include them in 2449 * any iteration. 2450 */ 2451 while (entry && HeVAL(entry) == &PL_sv_placeholder) { 2452 entry = HeNEXT(entry); 2453 } 2454 } 2455 } 2456 2457 #ifdef PERL_HASH_RANDOMIZE_KEYS 2458 if (iter->xhv_last_rand != iter->xhv_rand) { 2459 if (iter->xhv_riter != -1) { 2460 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), 2461 "Use of each() on hash after insertion without resetting hash iterator results in undefined behavior" 2462 pTHX__FORMAT 2463 pTHX__VALUE); 2464 } 2465 iter->xhv_last_rand = iter->xhv_rand; 2466 } 2467 #endif 2468 2469 /* Skip the entire loop if the hash is empty. */ 2470 if ((flags & HV_ITERNEXT_WANTPLACEHOLDERS) 2471 ? HvTOTALKEYS(hv) : HvUSEDKEYS(hv)) { 2472 while (!entry) { 2473 /* OK. Come to the end of the current list. Grab the next one. */ 2474 2475 iter->xhv_riter++; /* HvRITER(hv)++ */ 2476 if (iter->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) { 2477 /* There is no next one. End of the hash. */ 2478 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */ 2479 #ifdef PERL_HASH_RANDOMIZE_KEYS 2480 iter->xhv_last_rand = iter->xhv_rand; /* reset xhv_last_rand so we can detect inserts during traversal */ 2481 #endif 2482 break; 2483 } 2484 entry = (HvARRAY(hv))[ PERL_HASH_ITER_BUCKET(iter) & xhv->xhv_max ]; 2485 2486 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) { 2487 /* If we have an entry, but it's a placeholder, don't count it. 2488 Try the next. */ 2489 while (entry && HeVAL(entry) == &PL_sv_placeholder) 2490 entry = HeNEXT(entry); 2491 } 2492 /* Will loop again if this linked list starts NULL 2493 (for HV_ITERNEXT_WANTPLACEHOLDERS) 2494 or if we run through it and find only placeholders. */ 2495 } 2496 } 2497 else { 2498 iter->xhv_riter = -1; 2499 #ifdef PERL_HASH_RANDOMIZE_KEYS 2500 iter->xhv_last_rand = iter->xhv_rand; 2501 #endif 2502 } 2503 2504 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */ 2505 HvLAZYDEL_off(hv); 2506 hv_free_ent(hv, oldentry); 2507 } 2508 2509 iter->xhv_eiter = entry; /* HvEITER(hv) = entry */ 2510 return entry; 2511 } 2512 2513 /* 2514 =for apidoc hv_iterkey 2515 2516 Returns the key from the current position of the hash iterator. See 2517 C<hv_iterinit>. 2518 2519 =cut 2520 */ 2521 2522 char * 2523 Perl_hv_iterkey(pTHX_ HE *entry, I32 *retlen) 2524 { 2525 PERL_ARGS_ASSERT_HV_ITERKEY; 2526 2527 if (HeKLEN(entry) == HEf_SVKEY) { 2528 STRLEN len; 2529 char * const p = SvPV(HeKEY_sv(entry), len); 2530 *retlen = len; 2531 return p; 2532 } 2533 else { 2534 *retlen = HeKLEN(entry); 2535 return HeKEY(entry); 2536 } 2537 } 2538 2539 /* unlike hv_iterval(), this always returns a mortal copy of the key */ 2540 /* 2541 =for apidoc hv_iterkeysv 2542 2543 Returns the key as an C<SV*> from the current position of the hash 2544 iterator. The return value will always be a mortal copy of the key. Also 2545 see C<hv_iterinit>. 2546 2547 =cut 2548 */ 2549 2550 SV * 2551 Perl_hv_iterkeysv(pTHX_ HE *entry) 2552 { 2553 PERL_ARGS_ASSERT_HV_ITERKEYSV; 2554 2555 return sv_2mortal(newSVhek(HeKEY_hek(entry))); 2556 } 2557 2558 /* 2559 =for apidoc hv_iterval 2560 2561 Returns the value from the current position of the hash iterator. See 2562 C<hv_iterkey>. 2563 2564 =cut 2565 */ 2566 2567 SV * 2568 Perl_hv_iterval(pTHX_ HV *hv, HE *entry) 2569 { 2570 PERL_ARGS_ASSERT_HV_ITERVAL; 2571 2572 if (SvRMAGICAL(hv)) { 2573 if (mg_find((const SV *)hv, PERL_MAGIC_tied)) { 2574 SV* const sv = sv_newmortal(); 2575 if (HeKLEN(entry) == HEf_SVKEY) 2576 mg_copy(MUTABLE_SV(hv), sv, (char*)HeKEY_sv(entry), HEf_SVKEY); 2577 else 2578 mg_copy(MUTABLE_SV(hv), sv, HeKEY(entry), HeKLEN(entry)); 2579 return sv; 2580 } 2581 } 2582 return HeVAL(entry); 2583 } 2584 2585 /* 2586 =for apidoc hv_iternextsv 2587 2588 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one 2589 operation. 2590 2591 =cut 2592 */ 2593 2594 SV * 2595 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen) 2596 { 2597 HE * const he = hv_iternext_flags(hv, 0); 2598 2599 PERL_ARGS_ASSERT_HV_ITERNEXTSV; 2600 2601 if (!he) 2602 return NULL; 2603 *key = hv_iterkey(he, retlen); 2604 return hv_iterval(hv, he); 2605 } 2606 2607 /* 2608 2609 Now a macro in hv.h 2610 2611 =for apidoc hv_magic 2612 2613 Adds magic to a hash. See C<sv_magic>. 2614 2615 =cut 2616 */ 2617 2618 /* possibly free a shared string if no one has access to it 2619 * len and hash must both be valid for str. 2620 */ 2621 void 2622 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash) 2623 { 2624 unshare_hek_or_pvn (NULL, str, len, hash); 2625 } 2626 2627 2628 void 2629 Perl_unshare_hek(pTHX_ HEK *hek) 2630 { 2631 assert(hek); 2632 unshare_hek_or_pvn(hek, NULL, 0, 0); 2633 } 2634 2635 /* possibly free a shared string if no one has access to it 2636 hek if non-NULL takes priority over the other 3, else str, len and hash 2637 are used. If so, len and hash must both be valid for str. 2638 */ 2639 STATIC void 2640 S_unshare_hek_or_pvn(pTHX_ const HEK *hek, const char *str, I32 len, U32 hash) 2641 { 2642 dVAR; 2643 XPVHV* xhv; 2644 HE *entry; 2645 HE **oentry; 2646 bool is_utf8 = FALSE; 2647 int k_flags = 0; 2648 const char * const save = str; 2649 struct shared_he *he = NULL; 2650 2651 if (hek) { 2652 /* Find the shared he which is just before us in memory. */ 2653 he = (struct shared_he *)(((char *)hek) 2654 - STRUCT_OFFSET(struct shared_he, 2655 shared_he_hek)); 2656 2657 /* Assert that the caller passed us a genuine (or at least consistent) 2658 shared hek */ 2659 assert (he->shared_he_he.hent_hek == hek); 2660 2661 if (he->shared_he_he.he_valu.hent_refcount - 1) { 2662 --he->shared_he_he.he_valu.hent_refcount; 2663 return; 2664 } 2665 2666 hash = HEK_HASH(hek); 2667 } else if (len < 0) { 2668 STRLEN tmplen = -len; 2669 is_utf8 = TRUE; 2670 /* See the note in hv_fetch(). --jhi */ 2671 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8); 2672 len = tmplen; 2673 if (is_utf8) 2674 k_flags = HVhek_UTF8; 2675 if (str != save) 2676 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY; 2677 } 2678 2679 /* what follows was the moral equivalent of: 2680 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) { 2681 if (--*Svp == NULL) 2682 hv_delete(PL_strtab, str, len, G_DISCARD, hash); 2683 } */ 2684 xhv = (XPVHV*)SvANY(PL_strtab); 2685 /* assert(xhv_array != 0) */ 2686 oentry = &(HvARRAY(PL_strtab))[hash & (I32) HvMAX(PL_strtab)]; 2687 if (he) { 2688 const HE *const he_he = &(he->shared_he_he); 2689 for (entry = *oentry; entry; oentry = &HeNEXT(entry), entry = *oentry) { 2690 if (entry == he_he) 2691 break; 2692 } 2693 } else { 2694 const int flags_masked = k_flags & HVhek_MASK; 2695 for (entry = *oentry; entry; oentry = &HeNEXT(entry), entry = *oentry) { 2696 if (HeHASH(entry) != hash) /* strings can't be equal */ 2697 continue; 2698 if (HeKLEN(entry) != len) 2699 continue; 2700 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */ 2701 continue; 2702 if (HeKFLAGS(entry) != flags_masked) 2703 continue; 2704 break; 2705 } 2706 } 2707 2708 if (entry) { 2709 if (--entry->he_valu.hent_refcount == 0) { 2710 *oentry = HeNEXT(entry); 2711 Safefree(entry); 2712 xhv->xhv_keys--; /* HvTOTALKEYS(hv)-- */ 2713 } 2714 } 2715 2716 if (!entry) 2717 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), 2718 "Attempt to free nonexistent shared string '%s'%s" 2719 pTHX__FORMAT, 2720 hek ? HEK_KEY(hek) : str, 2721 ((k_flags & HVhek_UTF8) ? " (utf8)" : "") pTHX__VALUE); 2722 if (k_flags & HVhek_FREEKEY) 2723 Safefree(str); 2724 } 2725 2726 /* get a (constant) string ptr from the global string table 2727 * string will get added if it is not already there. 2728 * len and hash must both be valid for str. 2729 */ 2730 HEK * 2731 Perl_share_hek(pTHX_ const char *str, I32 len, U32 hash) 2732 { 2733 bool is_utf8 = FALSE; 2734 int flags = 0; 2735 const char * const save = str; 2736 2737 PERL_ARGS_ASSERT_SHARE_HEK; 2738 2739 if (len < 0) { 2740 STRLEN tmplen = -len; 2741 is_utf8 = TRUE; 2742 /* See the note in hv_fetch(). --jhi */ 2743 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8); 2744 len = tmplen; 2745 /* If we were able to downgrade here, then than means that we were passed 2746 in a key which only had chars 0-255, but was utf8 encoded. */ 2747 if (is_utf8) 2748 flags = HVhek_UTF8; 2749 /* If we found we were able to downgrade the string to bytes, then 2750 we should flag that it needs upgrading on keys or each. Also flag 2751 that we need share_hek_flags to free the string. */ 2752 if (str != save) { 2753 dVAR; 2754 PERL_HASH(hash, str, len); 2755 flags |= HVhek_WASUTF8 | HVhek_FREEKEY; 2756 } 2757 } 2758 2759 return share_hek_flags (str, len, hash, flags); 2760 } 2761 2762 STATIC HEK * 2763 S_share_hek_flags(pTHX_ const char *str, I32 len, U32 hash, int flags) 2764 { 2765 dVAR; 2766 HE *entry; 2767 const int flags_masked = flags & HVhek_MASK; 2768 const U32 hindex = hash & (I32) HvMAX(PL_strtab); 2769 XPVHV * const xhv = (XPVHV*)SvANY(PL_strtab); 2770 2771 PERL_ARGS_ASSERT_SHARE_HEK_FLAGS; 2772 2773 /* what follows is the moral equivalent of: 2774 2775 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE))) 2776 hv_store(PL_strtab, str, len, NULL, hash); 2777 2778 Can't rehash the shared string table, so not sure if it's worth 2779 counting the number of entries in the linked list 2780 */ 2781 2782 /* assert(xhv_array != 0) */ 2783 entry = (HvARRAY(PL_strtab))[hindex]; 2784 for (;entry; entry = HeNEXT(entry)) { 2785 if (HeHASH(entry) != hash) /* strings can't be equal */ 2786 continue; 2787 if (HeKLEN(entry) != len) 2788 continue; 2789 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */ 2790 continue; 2791 if (HeKFLAGS(entry) != flags_masked) 2792 continue; 2793 break; 2794 } 2795 2796 if (!entry) { 2797 /* What used to be head of the list. 2798 If this is NULL, then we're the first entry for this slot, which 2799 means we need to increate fill. */ 2800 struct shared_he *new_entry; 2801 HEK *hek; 2802 char *k; 2803 HE **const head = &HvARRAY(PL_strtab)[hindex]; 2804 HE *const next = *head; 2805 2806 /* We don't actually store a HE from the arena and a regular HEK. 2807 Instead we allocate one chunk of memory big enough for both, 2808 and put the HEK straight after the HE. This way we can find the 2809 HE directly from the HEK. 2810 */ 2811 2812 Newx(k, STRUCT_OFFSET(struct shared_he, 2813 shared_he_hek.hek_key[0]) + len + 2, char); 2814 new_entry = (struct shared_he *)k; 2815 entry = &(new_entry->shared_he_he); 2816 hek = &(new_entry->shared_he_hek); 2817 2818 Copy(str, HEK_KEY(hek), len, char); 2819 HEK_KEY(hek)[len] = 0; 2820 HEK_LEN(hek) = len; 2821 HEK_HASH(hek) = hash; 2822 HEK_FLAGS(hek) = (unsigned char)flags_masked; 2823 2824 /* Still "point" to the HEK, so that other code need not know what 2825 we're up to. */ 2826 HeKEY_hek(entry) = hek; 2827 entry->he_valu.hent_refcount = 0; 2828 HeNEXT(entry) = next; 2829 *head = entry; 2830 2831 xhv->xhv_keys++; /* HvTOTALKEYS(hv)++ */ 2832 if (!next) { /* initial entry? */ 2833 } else if ( DO_HSPLIT(xhv) ) { 2834 const STRLEN oldsize = xhv->xhv_max + 1; 2835 hsplit(PL_strtab, oldsize, oldsize * 2); 2836 } 2837 } 2838 2839 ++entry->he_valu.hent_refcount; 2840 2841 if (flags & HVhek_FREEKEY) 2842 Safefree(str); 2843 2844 return HeKEY_hek(entry); 2845 } 2846 2847 I32 * 2848 Perl_hv_placeholders_p(pTHX_ HV *hv) 2849 { 2850 dVAR; 2851 MAGIC *mg = mg_find((const SV *)hv, PERL_MAGIC_rhash); 2852 2853 PERL_ARGS_ASSERT_HV_PLACEHOLDERS_P; 2854 2855 if (!mg) { 2856 mg = sv_magicext(MUTABLE_SV(hv), 0, PERL_MAGIC_rhash, 0, 0, 0); 2857 2858 if (!mg) { 2859 Perl_die(aTHX_ "panic: hv_placeholders_p"); 2860 } 2861 } 2862 return &(mg->mg_len); 2863 } 2864 2865 2866 I32 2867 Perl_hv_placeholders_get(pTHX_ const HV *hv) 2868 { 2869 dVAR; 2870 MAGIC * const mg = mg_find((const SV *)hv, PERL_MAGIC_rhash); 2871 2872 PERL_ARGS_ASSERT_HV_PLACEHOLDERS_GET; 2873 2874 return mg ? mg->mg_len : 0; 2875 } 2876 2877 void 2878 Perl_hv_placeholders_set(pTHX_ HV *hv, I32 ph) 2879 { 2880 dVAR; 2881 MAGIC * const mg = mg_find((const SV *)hv, PERL_MAGIC_rhash); 2882 2883 PERL_ARGS_ASSERT_HV_PLACEHOLDERS_SET; 2884 2885 if (mg) { 2886 mg->mg_len = ph; 2887 } else if (ph) { 2888 if (!sv_magicext(MUTABLE_SV(hv), 0, PERL_MAGIC_rhash, 0, 0, ph)) 2889 Perl_die(aTHX_ "panic: hv_placeholders_set"); 2890 } 2891 /* else we don't need to add magic to record 0 placeholders. */ 2892 } 2893 2894 STATIC SV * 2895 S_refcounted_he_value(pTHX_ const struct refcounted_he *he) 2896 { 2897 dVAR; 2898 SV *value; 2899 2900 PERL_ARGS_ASSERT_REFCOUNTED_HE_VALUE; 2901 2902 switch(he->refcounted_he_data[0] & HVrhek_typemask) { 2903 case HVrhek_undef: 2904 value = newSV(0); 2905 break; 2906 case HVrhek_delete: 2907 value = &PL_sv_placeholder; 2908 break; 2909 case HVrhek_IV: 2910 value = newSViv(he->refcounted_he_val.refcounted_he_u_iv); 2911 break; 2912 case HVrhek_UV: 2913 value = newSVuv(he->refcounted_he_val.refcounted_he_u_uv); 2914 break; 2915 case HVrhek_PV: 2916 case HVrhek_PV_UTF8: 2917 /* Create a string SV that directly points to the bytes in our 2918 structure. */ 2919 value = newSV_type(SVt_PV); 2920 SvPV_set(value, (char *) he->refcounted_he_data + 1); 2921 SvCUR_set(value, he->refcounted_he_val.refcounted_he_u_len); 2922 /* This stops anything trying to free it */ 2923 SvLEN_set(value, 0); 2924 SvPOK_on(value); 2925 SvREADONLY_on(value); 2926 if ((he->refcounted_he_data[0] & HVrhek_typemask) == HVrhek_PV_UTF8) 2927 SvUTF8_on(value); 2928 break; 2929 default: 2930 Perl_croak(aTHX_ "panic: refcounted_he_value bad flags %"UVxf, 2931 (UV)he->refcounted_he_data[0]); 2932 } 2933 return value; 2934 } 2935 2936 /* 2937 =for apidoc m|HV *|refcounted_he_chain_2hv|const struct refcounted_he *c|U32 flags 2938 2939 Generates and returns a C<HV *> representing the content of a 2940 C<refcounted_he> chain. 2941 I<flags> is currently unused and must be zero. 2942 2943 =cut 2944 */ 2945 HV * 2946 Perl_refcounted_he_chain_2hv(pTHX_ const struct refcounted_he *chain, U32 flags) 2947 { 2948 dVAR; 2949 HV *hv; 2950 U32 placeholders, max; 2951 2952 if (flags) 2953 Perl_croak(aTHX_ "panic: refcounted_he_chain_2hv bad flags %"UVxf, 2954 (UV)flags); 2955 2956 /* We could chase the chain once to get an idea of the number of keys, 2957 and call ksplit. But for now we'll make a potentially inefficient 2958 hash with only 8 entries in its array. */ 2959 hv = newHV(); 2960 max = HvMAX(hv); 2961 if (!HvARRAY(hv)) { 2962 char *array; 2963 Newxz(array, PERL_HV_ARRAY_ALLOC_BYTES(max + 1), char); 2964 HvARRAY(hv) = (HE**)array; 2965 } 2966 2967 placeholders = 0; 2968 while (chain) { 2969 #ifdef USE_ITHREADS 2970 U32 hash = chain->refcounted_he_hash; 2971 #else 2972 U32 hash = HEK_HASH(chain->refcounted_he_hek); 2973 #endif 2974 HE **oentry = &((HvARRAY(hv))[hash & max]); 2975 HE *entry = *oentry; 2976 SV *value; 2977 2978 for (; entry; entry = HeNEXT(entry)) { 2979 if (HeHASH(entry) == hash) { 2980 /* We might have a duplicate key here. If so, entry is older 2981 than the key we've already put in the hash, so if they are 2982 the same, skip adding entry. */ 2983 #ifdef USE_ITHREADS 2984 const STRLEN klen = HeKLEN(entry); 2985 const char *const key = HeKEY(entry); 2986 if (klen == chain->refcounted_he_keylen 2987 && (!!HeKUTF8(entry) 2988 == !!(chain->refcounted_he_data[0] & HVhek_UTF8)) 2989 && memEQ(key, REF_HE_KEY(chain), klen)) 2990 goto next_please; 2991 #else 2992 if (HeKEY_hek(entry) == chain->refcounted_he_hek) 2993 goto next_please; 2994 if (HeKLEN(entry) == HEK_LEN(chain->refcounted_he_hek) 2995 && HeKUTF8(entry) == HEK_UTF8(chain->refcounted_he_hek) 2996 && memEQ(HeKEY(entry), HEK_KEY(chain->refcounted_he_hek), 2997 HeKLEN(entry))) 2998 goto next_please; 2999 #endif 3000 } 3001 } 3002 assert (!entry); 3003 entry = new_HE(); 3004 3005 #ifdef USE_ITHREADS 3006 HeKEY_hek(entry) 3007 = share_hek_flags(REF_HE_KEY(chain), 3008 chain->refcounted_he_keylen, 3009 chain->refcounted_he_hash, 3010 (chain->refcounted_he_data[0] 3011 & (HVhek_UTF8|HVhek_WASUTF8))); 3012 #else 3013 HeKEY_hek(entry) = share_hek_hek(chain->refcounted_he_hek); 3014 #endif 3015 value = refcounted_he_value(chain); 3016 if (value == &PL_sv_placeholder) 3017 placeholders++; 3018 HeVAL(entry) = value; 3019 3020 /* Link it into the chain. */ 3021 HeNEXT(entry) = *oentry; 3022 *oentry = entry; 3023 3024 HvTOTALKEYS(hv)++; 3025 3026 next_please: 3027 chain = chain->refcounted_he_next; 3028 } 3029 3030 if (placeholders) { 3031 clear_placeholders(hv, placeholders); 3032 HvTOTALKEYS(hv) -= placeholders; 3033 } 3034 3035 /* We could check in the loop to see if we encounter any keys with key 3036 flags, but it's probably not worth it, as this per-hash flag is only 3037 really meant as an optimisation for things like Storable. */ 3038 HvHASKFLAGS_on(hv); 3039 DEBUG_A(Perl_hv_assert(aTHX_ hv)); 3040 3041 return hv; 3042 } 3043 3044 /* 3045 =for apidoc m|SV *|refcounted_he_fetch_pvn|const struct refcounted_he *chain|const char *keypv|STRLEN keylen|U32 hash|U32 flags 3046 3047 Search along a C<refcounted_he> chain for an entry with the key specified 3048 by I<keypv> and I<keylen>. If I<flags> has the C<REFCOUNTED_HE_KEY_UTF8> 3049 bit set, the key octets are interpreted as UTF-8, otherwise they 3050 are interpreted as Latin-1. I<hash> is a precomputed hash of the key 3051 string, or zero if it has not been precomputed. Returns a mortal scalar 3052 representing the value associated with the key, or C<&PL_sv_placeholder> 3053 if there is no value associated with the key. 3054 3055 =cut 3056 */ 3057 3058 SV * 3059 Perl_refcounted_he_fetch_pvn(pTHX_ const struct refcounted_he *chain, 3060 const char *keypv, STRLEN keylen, U32 hash, U32 flags) 3061 { 3062 dVAR; 3063 U8 utf8_flag; 3064 PERL_ARGS_ASSERT_REFCOUNTED_HE_FETCH_PVN; 3065 3066 if (flags & ~(REFCOUNTED_HE_KEY_UTF8|REFCOUNTED_HE_EXISTS)) 3067 Perl_croak(aTHX_ "panic: refcounted_he_fetch_pvn bad flags %"UVxf, 3068 (UV)flags); 3069 if (!chain) 3070 return &PL_sv_placeholder; 3071 if (flags & REFCOUNTED_HE_KEY_UTF8) { 3072 /* For searching purposes, canonicalise to Latin-1 where possible. */ 3073 const char *keyend = keypv + keylen, *p; 3074 STRLEN nonascii_count = 0; 3075 for (p = keypv; p != keyend; p++) { 3076 U8 c = (U8)*p; 3077 if (c & 0x80) { 3078 if (!((c & 0xfe) == 0xc2 && ++p != keyend && 3079 (((U8)*p) & 0xc0) == 0x80)) 3080 goto canonicalised_key; 3081 nonascii_count++; 3082 } 3083 } 3084 if (nonascii_count) { 3085 char *q; 3086 const char *p = keypv, *keyend = keypv + keylen; 3087 keylen -= nonascii_count; 3088 Newx(q, keylen, char); 3089 SAVEFREEPV(q); 3090 keypv = q; 3091 for (; p != keyend; p++, q++) { 3092 U8 c = (U8)*p; 3093 *q = (char) 3094 ((c & 0x80) ? ((c & 0x03) << 6) | (((U8)*++p) & 0x3f) : c); 3095 } 3096 } 3097 flags &= ~REFCOUNTED_HE_KEY_UTF8; 3098 canonicalised_key: ; 3099 } 3100 utf8_flag = (flags & REFCOUNTED_HE_KEY_UTF8) ? HVhek_UTF8 : 0; 3101 if (!hash) 3102 PERL_HASH(hash, keypv, keylen); 3103 3104 for (; chain; chain = chain->refcounted_he_next) { 3105 if ( 3106 #ifdef USE_ITHREADS 3107 hash == chain->refcounted_he_hash && 3108 keylen == chain->refcounted_he_keylen && 3109 memEQ(REF_HE_KEY(chain), keypv, keylen) && 3110 utf8_flag == (chain->refcounted_he_data[0] & HVhek_UTF8) 3111 #else 3112 hash == HEK_HASH(chain->refcounted_he_hek) && 3113 keylen == (STRLEN)HEK_LEN(chain->refcounted_he_hek) && 3114 memEQ(HEK_KEY(chain->refcounted_he_hek), keypv, keylen) && 3115 utf8_flag == (HEK_FLAGS(chain->refcounted_he_hek) & HVhek_UTF8) 3116 #endif 3117 ) { 3118 if (flags & REFCOUNTED_HE_EXISTS) 3119 return (chain->refcounted_he_data[0] & HVrhek_typemask) 3120 == HVrhek_delete 3121 ? NULL : &PL_sv_yes; 3122 return sv_2mortal(refcounted_he_value(chain)); 3123 } 3124 } 3125 return flags & REFCOUNTED_HE_EXISTS ? NULL : &PL_sv_placeholder; 3126 } 3127 3128 /* 3129 =for apidoc m|SV *|refcounted_he_fetch_pv|const struct refcounted_he *chain|const char *key|U32 hash|U32 flags 3130 3131 Like L</refcounted_he_fetch_pvn>, but takes a nul-terminated string 3132 instead of a string/length pair. 3133 3134 =cut 3135 */ 3136 3137 SV * 3138 Perl_refcounted_he_fetch_pv(pTHX_ const struct refcounted_he *chain, 3139 const char *key, U32 hash, U32 flags) 3140 { 3141 PERL_ARGS_ASSERT_REFCOUNTED_HE_FETCH_PV; 3142 return refcounted_he_fetch_pvn(chain, key, strlen(key), hash, flags); 3143 } 3144 3145 /* 3146 =for apidoc m|SV *|refcounted_he_fetch_sv|const struct refcounted_he *chain|SV *key|U32 hash|U32 flags 3147 3148 Like L</refcounted_he_fetch_pvn>, but takes a Perl scalar instead of a 3149 string/length pair. 3150 3151 =cut 3152 */ 3153 3154 SV * 3155 Perl_refcounted_he_fetch_sv(pTHX_ const struct refcounted_he *chain, 3156 SV *key, U32 hash, U32 flags) 3157 { 3158 const char *keypv; 3159 STRLEN keylen; 3160 PERL_ARGS_ASSERT_REFCOUNTED_HE_FETCH_SV; 3161 if (flags & REFCOUNTED_HE_KEY_UTF8) 3162 Perl_croak(aTHX_ "panic: refcounted_he_fetch_sv bad flags %"UVxf, 3163 (UV)flags); 3164 keypv = SvPV_const(key, keylen); 3165 if (SvUTF8(key)) 3166 flags |= REFCOUNTED_HE_KEY_UTF8; 3167 if (!hash && SvIsCOW_shared_hash(key)) 3168 hash = SvSHARED_HASH(key); 3169 return refcounted_he_fetch_pvn(chain, keypv, keylen, hash, flags); 3170 } 3171 3172 /* 3173 =for apidoc m|struct refcounted_he *|refcounted_he_new_pvn|struct refcounted_he *parent|const char *keypv|STRLEN keylen|U32 hash|SV *value|U32 flags 3174 3175 Creates a new C<refcounted_he>. This consists of a single key/value 3176 pair and a reference to an existing C<refcounted_he> chain (which may 3177 be empty), and thus forms a longer chain. When using the longer chain, 3178 the new key/value pair takes precedence over any entry for the same key 3179 further along the chain. 3180 3181 The new key is specified by I<keypv> and I<keylen>. If I<flags> has 3182 the C<REFCOUNTED_HE_KEY_UTF8> bit set, the key octets are interpreted 3183 as UTF-8, otherwise they are interpreted as Latin-1. I<hash> is 3184 a precomputed hash of the key string, or zero if it has not been 3185 precomputed. 3186 3187 I<value> is the scalar value to store for this key. I<value> is copied 3188 by this function, which thus does not take ownership of any reference 3189 to it, and later changes to the scalar will not be reflected in the 3190 value visible in the C<refcounted_he>. Complex types of scalar will not 3191 be stored with referential integrity, but will be coerced to strings. 3192 I<value> may be either null or C<&PL_sv_placeholder> to indicate that no 3193 value is to be associated with the key; this, as with any non-null value, 3194 takes precedence over the existence of a value for the key further along 3195 the chain. 3196 3197 I<parent> points to the rest of the C<refcounted_he> chain to be 3198 attached to the new C<refcounted_he>. This function takes ownership 3199 of one reference to I<parent>, and returns one reference to the new 3200 C<refcounted_he>. 3201 3202 =cut 3203 */ 3204 3205 struct refcounted_he * 3206 Perl_refcounted_he_new_pvn(pTHX_ struct refcounted_he *parent, 3207 const char *keypv, STRLEN keylen, U32 hash, SV *value, U32 flags) 3208 { 3209 dVAR; 3210 STRLEN value_len = 0; 3211 const char *value_p = NULL; 3212 bool is_pv; 3213 char value_type; 3214 char hekflags; 3215 STRLEN key_offset = 1; 3216 struct refcounted_he *he; 3217 PERL_ARGS_ASSERT_REFCOUNTED_HE_NEW_PVN; 3218 3219 if (!value || value == &PL_sv_placeholder) { 3220 value_type = HVrhek_delete; 3221 } else if (SvPOK(value)) { 3222 value_type = HVrhek_PV; 3223 } else if (SvIOK(value)) { 3224 value_type = SvUOK((const SV *)value) ? HVrhek_UV : HVrhek_IV; 3225 } else if (!SvOK(value)) { 3226 value_type = HVrhek_undef; 3227 } else { 3228 value_type = HVrhek_PV; 3229 } 3230 is_pv = value_type == HVrhek_PV; 3231 if (is_pv) { 3232 /* Do it this way so that the SvUTF8() test is after the SvPV, in case 3233 the value is overloaded, and doesn't yet have the UTF-8flag set. */ 3234 value_p = SvPV_const(value, value_len); 3235 if (SvUTF8(value)) 3236 value_type = HVrhek_PV_UTF8; 3237 key_offset = value_len + 2; 3238 } 3239 hekflags = value_type; 3240 3241 if (flags & REFCOUNTED_HE_KEY_UTF8) { 3242 /* Canonicalise to Latin-1 where possible. */ 3243 const char *keyend = keypv + keylen, *p; 3244 STRLEN nonascii_count = 0; 3245 for (p = keypv; p != keyend; p++) { 3246 U8 c = (U8)*p; 3247 if (c & 0x80) { 3248 if (!((c & 0xfe) == 0xc2 && ++p != keyend && 3249 (((U8)*p) & 0xc0) == 0x80)) 3250 goto canonicalised_key; 3251 nonascii_count++; 3252 } 3253 } 3254 if (nonascii_count) { 3255 char *q; 3256 const char *p = keypv, *keyend = keypv + keylen; 3257 keylen -= nonascii_count; 3258 Newx(q, keylen, char); 3259 SAVEFREEPV(q); 3260 keypv = q; 3261 for (; p != keyend; p++, q++) { 3262 U8 c = (U8)*p; 3263 *q = (char) 3264 ((c & 0x80) ? ((c & 0x03) << 6) | (((U8)*++p) & 0x3f) : c); 3265 } 3266 } 3267 flags &= ~REFCOUNTED_HE_KEY_UTF8; 3268 canonicalised_key: ; 3269 } 3270 if (flags & REFCOUNTED_HE_KEY_UTF8) 3271 hekflags |= HVhek_UTF8; 3272 if (!hash) 3273 PERL_HASH(hash, keypv, keylen); 3274 3275 #ifdef USE_ITHREADS 3276 he = (struct refcounted_he*) 3277 PerlMemShared_malloc(sizeof(struct refcounted_he) - 1 3278 + keylen 3279 + key_offset); 3280 #else 3281 he = (struct refcounted_he*) 3282 PerlMemShared_malloc(sizeof(struct refcounted_he) - 1 3283 + key_offset); 3284 #endif 3285 3286 he->refcounted_he_next = parent; 3287 3288 if (is_pv) { 3289 Copy(value_p, he->refcounted_he_data + 1, value_len + 1, char); 3290 he->refcounted_he_val.refcounted_he_u_len = value_len; 3291 } else if (value_type == HVrhek_IV) { 3292 he->refcounted_he_val.refcounted_he_u_iv = SvIVX(value); 3293 } else if (value_type == HVrhek_UV) { 3294 he->refcounted_he_val.refcounted_he_u_uv = SvUVX(value); 3295 } 3296 3297 #ifdef USE_ITHREADS 3298 he->refcounted_he_hash = hash; 3299 he->refcounted_he_keylen = keylen; 3300 Copy(keypv, he->refcounted_he_data + key_offset, keylen, char); 3301 #else 3302 he->refcounted_he_hek = share_hek_flags(keypv, keylen, hash, hekflags); 3303 #endif 3304 3305 he->refcounted_he_data[0] = hekflags; 3306 he->refcounted_he_refcnt = 1; 3307 3308 return he; 3309 } 3310 3311 /* 3312 =for apidoc m|struct refcounted_he *|refcounted_he_new_pv|struct refcounted_he *parent|const char *key|U32 hash|SV *value|U32 flags 3313 3314 Like L</refcounted_he_new_pvn>, but takes a nul-terminated string instead 3315 of a string/length pair. 3316 3317 =cut 3318 */ 3319 3320 struct refcounted_he * 3321 Perl_refcounted_he_new_pv(pTHX_ struct refcounted_he *parent, 3322 const char *key, U32 hash, SV *value, U32 flags) 3323 { 3324 PERL_ARGS_ASSERT_REFCOUNTED_HE_NEW_PV; 3325 return refcounted_he_new_pvn(parent, key, strlen(key), hash, value, flags); 3326 } 3327 3328 /* 3329 =for apidoc m|struct refcounted_he *|refcounted_he_new_sv|struct refcounted_he *parent|SV *key|U32 hash|SV *value|U32 flags 3330 3331 Like L</refcounted_he_new_pvn>, but takes a Perl scalar instead of a 3332 string/length pair. 3333 3334 =cut 3335 */ 3336 3337 struct refcounted_he * 3338 Perl_refcounted_he_new_sv(pTHX_ struct refcounted_he *parent, 3339 SV *key, U32 hash, SV *value, U32 flags) 3340 { 3341 const char *keypv; 3342 STRLEN keylen; 3343 PERL_ARGS_ASSERT_REFCOUNTED_HE_NEW_SV; 3344 if (flags & REFCOUNTED_HE_KEY_UTF8) 3345 Perl_croak(aTHX_ "panic: refcounted_he_new_sv bad flags %"UVxf, 3346 (UV)flags); 3347 keypv = SvPV_const(key, keylen); 3348 if (SvUTF8(key)) 3349 flags |= REFCOUNTED_HE_KEY_UTF8; 3350 if (!hash && SvIsCOW_shared_hash(key)) 3351 hash = SvSHARED_HASH(key); 3352 return refcounted_he_new_pvn(parent, keypv, keylen, hash, value, flags); 3353 } 3354 3355 /* 3356 =for apidoc m|void|refcounted_he_free|struct refcounted_he *he 3357 3358 Decrements the reference count of a C<refcounted_he> by one. If the 3359 reference count reaches zero the structure's memory is freed, which 3360 (recursively) causes a reduction of its parent C<refcounted_he>'s 3361 reference count. It is safe to pass a null pointer to this function: 3362 no action occurs in this case. 3363 3364 =cut 3365 */ 3366 3367 void 3368 Perl_refcounted_he_free(pTHX_ struct refcounted_he *he) { 3369 dVAR; 3370 PERL_UNUSED_CONTEXT; 3371 3372 while (he) { 3373 struct refcounted_he *copy; 3374 U32 new_count; 3375 3376 HINTS_REFCNT_LOCK; 3377 new_count = --he->refcounted_he_refcnt; 3378 HINTS_REFCNT_UNLOCK; 3379 3380 if (new_count) { 3381 return; 3382 } 3383 3384 #ifndef USE_ITHREADS 3385 unshare_hek_or_pvn (he->refcounted_he_hek, 0, 0, 0); 3386 #endif 3387 copy = he; 3388 he = he->refcounted_he_next; 3389 PerlMemShared_free(copy); 3390 } 3391 } 3392 3393 /* 3394 =for apidoc m|struct refcounted_he *|refcounted_he_inc|struct refcounted_he *he 3395 3396 Increment the reference count of a C<refcounted_he>. The pointer to the 3397 C<refcounted_he> is also returned. It is safe to pass a null pointer 3398 to this function: no action occurs and a null pointer is returned. 3399 3400 =cut 3401 */ 3402 3403 struct refcounted_he * 3404 Perl_refcounted_he_inc(pTHX_ struct refcounted_he *he) 3405 { 3406 dVAR; 3407 if (he) { 3408 HINTS_REFCNT_LOCK; 3409 he->refcounted_he_refcnt++; 3410 HINTS_REFCNT_UNLOCK; 3411 } 3412 return he; 3413 } 3414 3415 /* 3416 =for apidoc cop_fetch_label 3417 3418 Returns the label attached to a cop. 3419 The flags pointer may be set to C<SVf_UTF8> or 0. 3420 3421 =cut 3422 */ 3423 3424 /* pp_entereval is aware that labels are stored with a key ':' at the top of 3425 the linked list. */ 3426 const char * 3427 Perl_cop_fetch_label(pTHX_ COP *const cop, STRLEN *len, U32 *flags) { 3428 struct refcounted_he *const chain = cop->cop_hints_hash; 3429 3430 PERL_ARGS_ASSERT_COP_FETCH_LABEL; 3431 3432 if (!chain) 3433 return NULL; 3434 #ifdef USE_ITHREADS 3435 if (chain->refcounted_he_keylen != 1) 3436 return NULL; 3437 if (*REF_HE_KEY(chain) != ':') 3438 return NULL; 3439 #else 3440 if ((STRLEN)HEK_LEN(chain->refcounted_he_hek) != 1) 3441 return NULL; 3442 if (*HEK_KEY(chain->refcounted_he_hek) != ':') 3443 return NULL; 3444 #endif 3445 /* Stop anyone trying to really mess us up by adding their own value for 3446 ':' into %^H */ 3447 if ((chain->refcounted_he_data[0] & HVrhek_typemask) != HVrhek_PV 3448 && (chain->refcounted_he_data[0] & HVrhek_typemask) != HVrhek_PV_UTF8) 3449 return NULL; 3450 3451 if (len) 3452 *len = chain->refcounted_he_val.refcounted_he_u_len; 3453 if (flags) { 3454 *flags = ((chain->refcounted_he_data[0] & HVrhek_typemask) 3455 == HVrhek_PV_UTF8) ? SVf_UTF8 : 0; 3456 } 3457 return chain->refcounted_he_data + 1; 3458 } 3459 3460 /* 3461 =for apidoc cop_store_label 3462 3463 Save a label into a C<cop_hints_hash>. You need to set flags to C<SVf_UTF8> 3464 for a utf-8 label. 3465 3466 =cut 3467 */ 3468 3469 void 3470 Perl_cop_store_label(pTHX_ COP *const cop, const char *label, STRLEN len, 3471 U32 flags) 3472 { 3473 SV *labelsv; 3474 PERL_ARGS_ASSERT_COP_STORE_LABEL; 3475 3476 if (flags & ~(SVf_UTF8)) 3477 Perl_croak(aTHX_ "panic: cop_store_label illegal flag bits 0x%" UVxf, 3478 (UV)flags); 3479 labelsv = newSVpvn_flags(label, len, SVs_TEMP); 3480 if (flags & SVf_UTF8) 3481 SvUTF8_on(labelsv); 3482 cop->cop_hints_hash 3483 = refcounted_he_new_pvs(cop->cop_hints_hash, ":", labelsv, 0); 3484 } 3485 3486 /* 3487 =for apidoc hv_assert 3488 3489 Check that a hash is in an internally consistent state. 3490 3491 =cut 3492 */ 3493 3494 #ifdef DEBUGGING 3495 3496 void 3497 Perl_hv_assert(pTHX_ HV *hv) 3498 { 3499 dVAR; 3500 HE* entry; 3501 int withflags = 0; 3502 int placeholders = 0; 3503 int real = 0; 3504 int bad = 0; 3505 const I32 riter = HvRITER_get(hv); 3506 HE *eiter = HvEITER_get(hv); 3507 3508 PERL_ARGS_ASSERT_HV_ASSERT; 3509 3510 (void)hv_iterinit(hv); 3511 3512 while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))) { 3513 /* sanity check the values */ 3514 if (HeVAL(entry) == &PL_sv_placeholder) 3515 placeholders++; 3516 else 3517 real++; 3518 /* sanity check the keys */ 3519 if (HeSVKEY(entry)) { 3520 NOOP; /* Don't know what to check on SV keys. */ 3521 } else if (HeKUTF8(entry)) { 3522 withflags++; 3523 if (HeKWASUTF8(entry)) { 3524 PerlIO_printf(Perl_debug_log, 3525 "hash key has both WASUTF8 and UTF8: '%.*s'\n", 3526 (int) HeKLEN(entry), HeKEY(entry)); 3527 bad = 1; 3528 } 3529 } else if (HeKWASUTF8(entry)) 3530 withflags++; 3531 } 3532 if (!SvTIED_mg((const SV *)hv, PERL_MAGIC_tied)) { 3533 static const char bad_count[] = "Count %d %s(s), but hash reports %d\n"; 3534 const int nhashkeys = HvUSEDKEYS(hv); 3535 const int nhashplaceholders = HvPLACEHOLDERS_get(hv); 3536 3537 if (nhashkeys != real) { 3538 PerlIO_printf(Perl_debug_log, bad_count, real, "keys", nhashkeys ); 3539 bad = 1; 3540 } 3541 if (nhashplaceholders != placeholders) { 3542 PerlIO_printf(Perl_debug_log, bad_count, placeholders, "placeholder", nhashplaceholders ); 3543 bad = 1; 3544 } 3545 } 3546 if (withflags && ! HvHASKFLAGS(hv)) { 3547 PerlIO_printf(Perl_debug_log, 3548 "Hash has HASKFLAGS off but I count %d key(s) with flags\n", 3549 withflags); 3550 bad = 1; 3551 } 3552 if (bad) { 3553 sv_dump(MUTABLE_SV(hv)); 3554 } 3555 HvRITER_set(hv, riter); /* Restore hash iterator state */ 3556 HvEITER_set(hv, eiter); 3557 } 3558 3559 #endif 3560 3561 /* 3562 * Local variables: 3563 * c-indentation-style: bsd 3564 * c-basic-offset: 4 3565 * indent-tabs-mode: nil 3566 * End: 3567 * 3568 * ex: set ts=8 sts=4 sw=4 et: 3569 */ 3570