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