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