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