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