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