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