Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* hv.c |
79072805 | 2 | * |
4bb101f2 | 3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
7272f7c1 | 4 | * 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 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 | /* | |
12 | * "I sit beside the fire and think of all that I have seen." --Bilbo | |
79072805 LW |
13 | */ |
14 | ||
d5afce77 RB |
15 | /* |
16 | =head1 Hash Manipulation Functions | |
166f8a29 DM |
17 | |
18 | A HV structure represents a Perl hash. It consists mainly of an array | |
19 | of pointers, each of which points to a linked list of HE structures. The | |
20 | array is indexed by the hash function of the key, so each linked list | |
21 | represents all the hash entries with the same hash value. Each HE contains | |
22 | a pointer to the actual value, plus a pointer to a HEK structure which | |
23 | holds the key and hash value. | |
24 | ||
25 | =cut | |
26 | ||
d5afce77 RB |
27 | */ |
28 | ||
79072805 | 29 | #include "EXTERN.h" |
864dbfa3 | 30 | #define PERL_IN_HV_C |
3d78eb94 | 31 | #define PERL_HASH_INTERNAL_ACCESS |
79072805 LW |
32 | #include "perl.h" |
33 | ||
d8012aaf | 34 | #define HV_MAX_LENGTH_BEFORE_SPLIT 14 |
fdcd69b6 | 35 | |
d75ce684 | 36 | static const char S_strtab_error[] |
5d2b1485 NC |
37 | = "Cannot modify shared string table in hv_%s"; |
38 | ||
cac9b346 NC |
39 | STATIC void |
40 | S_more_he(pTHX) | |
41 | { | |
97aff369 | 42 | dVAR; |
1e05feb3 AL |
43 | HE* he = (HE*) Perl_get_arena(aTHX_ PERL_ARENA_SIZE, HE_SVSLOT); |
44 | HE * const heend = &he[PERL_ARENA_SIZE / sizeof(HE) - 1]; | |
cac9b346 | 45 | |
d2a0f284 | 46 | PL_body_roots[HE_SVSLOT] = he; |
cac9b346 NC |
47 | while (he < heend) { |
48 | HeNEXT(he) = (HE*)(he + 1); | |
49 | he++; | |
50 | } | |
51 | HeNEXT(he) = 0; | |
52 | } | |
53 | ||
c941fb51 NC |
54 | #ifdef PURIFY |
55 | ||
56 | #define new_HE() (HE*)safemalloc(sizeof(HE)) | |
57 | #define del_HE(p) safefree((char*)p) | |
58 | ||
59 | #else | |
60 | ||
76e3520e | 61 | STATIC HE* |
cea2e8a9 | 62 | S_new_he(pTHX) |
4633a7c4 | 63 | { |
97aff369 | 64 | dVAR; |
4633a7c4 | 65 | HE* he; |
0bd48802 | 66 | void ** const root = &PL_body_roots[HE_SVSLOT]; |
6a93a7e5 | 67 | |
6a93a7e5 | 68 | if (!*root) |
cac9b346 | 69 | S_more_he(aTHX); |
10edeb5d | 70 | he = (HE*) *root; |
ce3e5c45 | 71 | assert(he); |
6a93a7e5 | 72 | *root = HeNEXT(he); |
333f433b | 73 | return he; |
4633a7c4 LW |
74 | } |
75 | ||
c941fb51 NC |
76 | #define new_HE() new_he() |
77 | #define del_HE(p) \ | |
78 | STMT_START { \ | |
6a93a7e5 NC |
79 | HeNEXT(p) = (HE*)(PL_body_roots[HE_SVSLOT]); \ |
80 | PL_body_roots[HE_SVSLOT] = p; \ | |
c941fb51 | 81 | } STMT_END |
d33b2eba | 82 | |
d33b2eba | 83 | |
d33b2eba GS |
84 | |
85 | #endif | |
86 | ||
76e3520e | 87 | STATIC HEK * |
5f66b61c | 88 | S_save_hek_flags(const char *str, I32 len, U32 hash, int flags) |
bbce6d69 | 89 | { |
35a4481c | 90 | const int flags_masked = flags & HVhek_MASK; |
bbce6d69 | 91 | char *k; |
92 | register HEK *hek; | |
1c846c1f | 93 | |
a02a5408 | 94 | Newx(k, HEK_BASESIZE + len + 2, char); |
bbce6d69 | 95 | hek = (HEK*)k; |
ff68c719 | 96 | Copy(str, HEK_KEY(hek), len, char); |
e05949c7 | 97 | HEK_KEY(hek)[len] = 0; |
ff68c719 | 98 | HEK_LEN(hek) = len; |
99 | HEK_HASH(hek) = hash; | |
45e34800 | 100 | HEK_FLAGS(hek) = (unsigned char)flags_masked | HVhek_UNSHARED; |
dcf933a4 NC |
101 | |
102 | if (flags & HVhek_FREEKEY) | |
103 | Safefree(str); | |
bbce6d69 | 104 | return hek; |
105 | } | |
106 | ||
4a31713e | 107 | /* free the pool of temporary HE/HEK pairs returned by hv_fetch_ent |
dd28f7bb DM |
108 | * for tied hashes */ |
109 | ||
110 | void | |
111 | Perl_free_tied_hv_pool(pTHX) | |
112 | { | |
97aff369 | 113 | dVAR; |
dd28f7bb DM |
114 | HE *he = PL_hv_fetch_ent_mh; |
115 | while (he) { | |
9d4ba2ae | 116 | HE * const ohe = he; |
dd28f7bb | 117 | Safefree(HeKEY_hek(he)); |
dd28f7bb DM |
118 | he = HeNEXT(he); |
119 | del_HE(ohe); | |
120 | } | |
4608196e | 121 | PL_hv_fetch_ent_mh = NULL; |
dd28f7bb DM |
122 | } |
123 | ||
d18c6117 | 124 | #if defined(USE_ITHREADS) |
0bff533c NC |
125 | HEK * |
126 | Perl_hek_dup(pTHX_ HEK *source, CLONE_PARAMS* param) | |
127 | { | |
658b4a4a | 128 | HEK *shared = (HEK*)ptr_table_fetch(PL_ptr_table, source); |
9d4ba2ae AL |
129 | |
130 | PERL_UNUSED_ARG(param); | |
0bff533c NC |
131 | |
132 | if (shared) { | |
133 | /* We already shared this hash key. */ | |
454f1e26 | 134 | (void)share_hek_hek(shared); |
0bff533c NC |
135 | } |
136 | else { | |
658b4a4a | 137 | shared |
6e838c70 NC |
138 | = share_hek_flags(HEK_KEY(source), HEK_LEN(source), |
139 | HEK_HASH(source), HEK_FLAGS(source)); | |
658b4a4a | 140 | ptr_table_store(PL_ptr_table, source, shared); |
0bff533c | 141 | } |
658b4a4a | 142 | return shared; |
0bff533c NC |
143 | } |
144 | ||
d18c6117 | 145 | HE * |
5c4138a0 | 146 | Perl_he_dup(pTHX_ const HE *e, bool shared, CLONE_PARAMS* param) |
d18c6117 GS |
147 | { |
148 | HE *ret; | |
149 | ||
150 | if (!e) | |
4608196e | 151 | return NULL; |
7766f137 GS |
152 | /* look for it in the table first */ |
153 | ret = (HE*)ptr_table_fetch(PL_ptr_table, e); | |
154 | if (ret) | |
155 | return ret; | |
156 | ||
157 | /* create anew and remember what it is */ | |
d33b2eba | 158 | ret = new_HE(); |
7766f137 GS |
159 | ptr_table_store(PL_ptr_table, e, ret); |
160 | ||
d2d73c3e | 161 | HeNEXT(ret) = he_dup(HeNEXT(e),shared, param); |
dd28f7bb DM |
162 | if (HeKLEN(e) == HEf_SVKEY) { |
163 | char *k; | |
a02a5408 | 164 | Newx(k, HEK_BASESIZE + sizeof(SV*), char); |
dd28f7bb | 165 | HeKEY_hek(ret) = (HEK*)k; |
d2d73c3e | 166 | HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e), param)); |
dd28f7bb | 167 | } |
c21d1a0f | 168 | else if (shared) { |
0bff533c NC |
169 | /* This is hek_dup inlined, which seems to be important for speed |
170 | reasons. */ | |
1b6737cc | 171 | HEK * const source = HeKEY_hek(e); |
658b4a4a | 172 | HEK *shared = (HEK*)ptr_table_fetch(PL_ptr_table, source); |
c21d1a0f NC |
173 | |
174 | if (shared) { | |
175 | /* We already shared this hash key. */ | |
454f1e26 | 176 | (void)share_hek_hek(shared); |
c21d1a0f NC |
177 | } |
178 | else { | |
658b4a4a | 179 | shared |
6e838c70 NC |
180 | = share_hek_flags(HEK_KEY(source), HEK_LEN(source), |
181 | HEK_HASH(source), HEK_FLAGS(source)); | |
658b4a4a | 182 | ptr_table_store(PL_ptr_table, source, shared); |
c21d1a0f | 183 | } |
658b4a4a | 184 | HeKEY_hek(ret) = shared; |
c21d1a0f | 185 | } |
d18c6117 | 186 | else |
19692e8d NC |
187 | HeKEY_hek(ret) = save_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e), |
188 | HeKFLAGS(e)); | |
d2d73c3e | 189 | HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e), param)); |
d18c6117 GS |
190 | return ret; |
191 | } | |
192 | #endif /* USE_ITHREADS */ | |
193 | ||
1b1f1335 | 194 | static void |
2393f1b9 JH |
195 | S_hv_notallowed(pTHX_ int flags, const char *key, I32 klen, |
196 | const char *msg) | |
1b1f1335 | 197 | { |
1b6737cc | 198 | SV * const sv = sv_newmortal(); |
19692e8d | 199 | if (!(flags & HVhek_FREEKEY)) { |
1b1f1335 NIS |
200 | sv_setpvn(sv, key, klen); |
201 | } | |
202 | else { | |
203 | /* Need to free saved eventually assign to mortal SV */ | |
34c3c4e3 | 204 | /* XXX is this line an error ???: SV *sv = sv_newmortal(); */ |
1b1f1335 NIS |
205 | sv_usepvn(sv, (char *) key, klen); |
206 | } | |
19692e8d | 207 | if (flags & HVhek_UTF8) { |
1b1f1335 NIS |
208 | SvUTF8_on(sv); |
209 | } | |
be2597df | 210 | Perl_croak(aTHX_ msg, SVfARG(sv)); |
1b1f1335 NIS |
211 | } |
212 | ||
fde52b5c | 213 | /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot |
214 | * contains an SV* */ | |
215 | ||
34a6f7b4 NC |
216 | /* |
217 | =for apidoc hv_store | |
218 | ||
219 | Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is | |
220 | the length of the key. The C<hash> parameter is the precomputed hash | |
221 | value; if it is zero then Perl will compute it. The return value will be | |
222 | NULL if the operation failed or if the value did not need to be actually | |
223 | stored within the hash (as in the case of tied hashes). Otherwise it can | |
224 | be dereferenced to get the original C<SV*>. Note that the caller is | |
225 | responsible for suitably incrementing the reference count of C<val> before | |
226 | the call, and decrementing it if the function returned NULL. Effectively | |
227 | a successful hv_store takes ownership of one reference to C<val>. This is | |
228 | usually what you want; a newly created SV has a reference count of one, so | |
229 | if all your code does is create SVs then store them in a hash, hv_store | |
230 | will own the only reference to the new SV, and your code doesn't need to do | |
231 | anything further to tidy up. hv_store is not implemented as a call to | |
232 | hv_store_ent, and does not create a temporary SV for the key, so if your | |
233 | key data is not already in SV form then use hv_store in preference to | |
234 | hv_store_ent. | |
235 | ||
236 | See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more | |
237 | information on how to use this function on tied hashes. | |
238 | ||
239 | =cut | |
240 | */ | |
241 | ||
242 | SV** | |
243 | Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen_i32, SV *val, U32 hash) | |
244 | { | |
245 | HE *hek; | |
246 | STRLEN klen; | |
247 | int flags; | |
248 | ||
249 | if (klen_i32 < 0) { | |
250 | klen = -klen_i32; | |
251 | flags = HVhek_UTF8; | |
252 | } else { | |
253 | klen = klen_i32; | |
254 | flags = 0; | |
255 | } | |
256 | hek = hv_fetch_common (hv, NULL, key, klen, flags, | |
52d01cc2 | 257 | (HV_FETCH_ISSTORE|HV_FETCH_JUST_SV), val, hash); |
34a6f7b4 NC |
258 | return hek ? &HeVAL(hek) : NULL; |
259 | } | |
260 | ||
fabdb6c0 | 261 | /* XXX This looks like an ideal candidate to inline */ |
34a6f7b4 NC |
262 | SV** |
263 | Perl_hv_store_flags(pTHX_ HV *hv, const char *key, I32 klen, SV *val, | |
264 | register U32 hash, int flags) | |
265 | { | |
9d4ba2ae | 266 | HE * const hek = hv_fetch_common (hv, NULL, key, klen, flags, |
34a6f7b4 NC |
267 | (HV_FETCH_ISSTORE|HV_FETCH_JUST_SV), val, hash); |
268 | return hek ? &HeVAL(hek) : NULL; | |
269 | } | |
270 | ||
271 | /* | |
272 | =for apidoc hv_store_ent | |
273 | ||
274 | Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash> | |
275 | parameter is the precomputed hash value; if it is zero then Perl will | |
276 | compute it. The return value is the new hash entry so created. It will be | |
277 | NULL if the operation failed or if the value did not need to be actually | |
278 | stored within the hash (as in the case of tied hashes). Otherwise the | |
279 | contents of the return value can be accessed using the C<He?> macros | |
280 | described here. Note that the caller is responsible for suitably | |
281 | incrementing the reference count of C<val> before the call, and | |
282 | decrementing it if the function returned NULL. Effectively a successful | |
283 | hv_store_ent takes ownership of one reference to C<val>. This is | |
284 | usually what you want; a newly created SV has a reference count of one, so | |
285 | if all your code does is create SVs then store them in a hash, hv_store | |
286 | will own the only reference to the new SV, and your code doesn't need to do | |
287 | anything further to tidy up. Note that hv_store_ent only reads the C<key>; | |
288 | unlike C<val> it does not take ownership of it, so maintaining the correct | |
289 | reference count on C<key> is entirely the caller's responsibility. hv_store | |
290 | is not implemented as a call to hv_store_ent, and does not create a temporary | |
291 | SV for the key, so if your key data is not already in SV form then use | |
292 | hv_store in preference to hv_store_ent. | |
293 | ||
294 | See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more | |
295 | information on how to use this function on tied hashes. | |
296 | ||
297 | =cut | |
298 | */ | |
299 | ||
fabdb6c0 | 300 | /* XXX This looks like an ideal candidate to inline */ |
34a6f7b4 NC |
301 | HE * |
302 | Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, U32 hash) | |
303 | { | |
304 | return hv_fetch_common(hv, keysv, NULL, 0, 0, HV_FETCH_ISSTORE, val, hash); | |
305 | } | |
306 | ||
307 | /* | |
308 | =for apidoc hv_exists | |
309 | ||
310 | Returns a boolean indicating whether the specified hash key exists. The | |
311 | C<klen> is the length of the key. | |
312 | ||
313 | =cut | |
314 | */ | |
315 | ||
316 | bool | |
317 | Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen_i32) | |
318 | { | |
319 | STRLEN klen; | |
320 | int flags; | |
321 | ||
322 | if (klen_i32 < 0) { | |
323 | klen = -klen_i32; | |
324 | flags = HVhek_UTF8; | |
325 | } else { | |
326 | klen = klen_i32; | |
327 | flags = 0; | |
328 | } | |
329 | return hv_fetch_common(hv, NULL, key, klen, flags, HV_FETCH_ISEXISTS, 0, 0) | |
330 | ? TRUE : FALSE; | |
331 | } | |
332 | ||
954c1994 GS |
333 | /* |
334 | =for apidoc hv_fetch | |
335 | ||
336 | Returns the SV which corresponds to the specified key in the hash. The | |
337 | C<klen> is the length of the key. If C<lval> is set then the fetch will be | |
338 | part of a store. Check that the return value is non-null before | |
d1be9408 | 339 | dereferencing it to an C<SV*>. |
954c1994 | 340 | |
96f1132b | 341 | See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more |
954c1994 GS |
342 | information on how to use this function on tied hashes. |
343 | ||
344 | =cut | |
345 | */ | |
346 | ||
79072805 | 347 | SV** |
c1fe5510 | 348 | Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 lval) |
79072805 | 349 | { |
c1fe5510 NC |
350 | HE *hek; |
351 | STRLEN klen; | |
352 | int flags; | |
353 | ||
354 | if (klen_i32 < 0) { | |
355 | klen = -klen_i32; | |
356 | flags = HVhek_UTF8; | |
357 | } else { | |
358 | klen = klen_i32; | |
359 | flags = 0; | |
360 | } | |
361 | hek = hv_fetch_common (hv, NULL, key, klen, flags, | |
c445ea15 | 362 | lval ? (HV_FETCH_JUST_SV | HV_FETCH_LVALUE) : HV_FETCH_JUST_SV, |
a0714e2c | 363 | NULL, 0); |
113738bb | 364 | return hek ? &HeVAL(hek) : NULL; |
79072805 LW |
365 | } |
366 | ||
34a6f7b4 NC |
367 | /* |
368 | =for apidoc hv_exists_ent | |
369 | ||
370 | Returns a boolean indicating whether the specified hash key exists. C<hash> | |
371 | can be a valid precomputed hash value, or 0 to ask for it to be | |
372 | computed. | |
373 | ||
374 | =cut | |
375 | */ | |
376 | ||
fabdb6c0 | 377 | /* XXX This looks like an ideal candidate to inline */ |
34a6f7b4 NC |
378 | bool |
379 | Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash) | |
380 | { | |
381 | return hv_fetch_common(hv, keysv, NULL, 0, 0, HV_FETCH_ISEXISTS, 0, hash) | |
382 | ? TRUE : FALSE; | |
383 | } | |
384 | ||
d1be9408 | 385 | /* returns an HE * structure with the all fields set */ |
fde52b5c | 386 | /* note that hent_val will be a mortal sv for MAGICAL hashes */ |
954c1994 GS |
387 | /* |
388 | =for apidoc hv_fetch_ent | |
389 | ||
390 | Returns the hash entry which corresponds to the specified key in the hash. | |
391 | C<hash> must be a valid precomputed hash number for the given C<key>, or 0 | |
392 | if you want the function to compute it. IF C<lval> is set then the fetch | |
393 | will be part of a store. Make sure the return value is non-null before | |
394 | accessing it. The return value when C<tb> is a tied hash is a pointer to a | |
395 | static location, so be sure to make a copy of the structure if you need to | |
1c846c1f | 396 | store it somewhere. |
954c1994 | 397 | |
96f1132b | 398 | See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more |
954c1994 GS |
399 | information on how to use this function on tied hashes. |
400 | ||
401 | =cut | |
402 | */ | |
403 | ||
fde52b5c | 404 | HE * |
864dbfa3 | 405 | Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash) |
fde52b5c | 406 | { |
7f66fda2 | 407 | return hv_fetch_common(hv, keysv, NULL, 0, 0, |
a0714e2c | 408 | (lval ? HV_FETCH_LVALUE : 0), NULL, hash); |
113738bb NC |
409 | } |
410 | ||
8f8d40ab | 411 | STATIC HE * |
c1fe5510 | 412 | S_hv_fetch_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen, |
b2c64049 | 413 | int flags, int action, SV *val, register U32 hash) |
113738bb | 414 | { |
27da23d5 | 415 | dVAR; |
b2c64049 | 416 | XPVHV* xhv; |
b2c64049 NC |
417 | HE *entry; |
418 | HE **oentry; | |
fde52b5c | 419 | SV *sv; |
da58a35d | 420 | bool is_utf8; |
113738bb | 421 | int masked_flags; |
fde52b5c | 422 | |
423 | if (!hv) | |
a4fc7abc | 424 | return NULL; |
fde52b5c | 425 | |
113738bb | 426 | if (keysv) { |
1e73acc8 AS |
427 | if (SvSMAGICAL(hv) && SvGMAGICAL(hv)) |
428 | keysv = hv_magic_uvar_xkey(hv, keysv, action); | |
e593d2fe AE |
429 | if (flags & HVhek_FREEKEY) |
430 | Safefree(key); | |
5c144d81 | 431 | key = SvPV_const(keysv, klen); |
c1fe5510 | 432 | flags = 0; |
113738bb NC |
433 | is_utf8 = (SvUTF8(keysv) != 0); |
434 | } else { | |
c1fe5510 | 435 | is_utf8 = ((flags & HVhek_UTF8) ? TRUE : FALSE); |
113738bb | 436 | } |
113738bb | 437 | |
b2c64049 | 438 | xhv = (XPVHV*)SvANY(hv); |
7f66fda2 | 439 | if (SvMAGICAL(hv)) { |
6136c704 | 440 | if (SvRMAGICAL(hv) && !(action & (HV_FETCH_ISSTORE|HV_FETCH_ISEXISTS))) { |
44a2ac75 | 441 | if ( mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) |
e62cc96a | 442 | { |
7f66fda2 NC |
443 | /* XXX should be able to skimp on the HE/HEK here when |
444 | HV_FETCH_JUST_SV is true. */ | |
7f66fda2 NC |
445 | if (!keysv) { |
446 | keysv = newSVpvn(key, klen); | |
447 | if (is_utf8) { | |
448 | SvUTF8_on(keysv); | |
449 | } | |
450 | } else { | |
451 | keysv = newSVsv(keysv); | |
113738bb | 452 | } |
44a2ac75 YO |
453 | sv = sv_newmortal(); |
454 | mg_copy((SV*)hv, sv, (char *)keysv, HEf_SVKEY); | |
7f66fda2 NC |
455 | |
456 | /* grab a fake HE/HEK pair from the pool or make a new one */ | |
457 | entry = PL_hv_fetch_ent_mh; | |
458 | if (entry) | |
459 | PL_hv_fetch_ent_mh = HeNEXT(entry); | |
460 | else { | |
461 | char *k; | |
462 | entry = new_HE(); | |
a02a5408 | 463 | Newx(k, HEK_BASESIZE + sizeof(SV*), char); |
7f66fda2 NC |
464 | HeKEY_hek(entry) = (HEK*)k; |
465 | } | |
4608196e | 466 | HeNEXT(entry) = NULL; |
7f66fda2 NC |
467 | HeSVKEY_set(entry, keysv); |
468 | HeVAL(entry) = sv; | |
469 | sv_upgrade(sv, SVt_PVLV); | |
470 | LvTYPE(sv) = 'T'; | |
471 | /* so we can free entry when freeing sv */ | |
472 | LvTARG(sv) = (SV*)entry; | |
473 | ||
474 | /* XXX remove at some point? */ | |
475 | if (flags & HVhek_FREEKEY) | |
476 | Safefree(key); | |
477 | ||
478 | return entry; | |
113738bb | 479 | } |
7f66fda2 NC |
480 | #ifdef ENV_IS_CASELESS |
481 | else if (mg_find((SV*)hv, PERL_MAGIC_env)) { | |
482 | U32 i; | |
483 | for (i = 0; i < klen; ++i) | |
484 | if (isLOWER(key[i])) { | |
086cb327 NC |
485 | /* Would be nice if we had a routine to do the |
486 | copy and upercase in a single pass through. */ | |
0bd48802 | 487 | const char * const nkey = strupr(savepvn(key,klen)); |
086cb327 NC |
488 | /* Note that this fetch is for nkey (the uppercased |
489 | key) whereas the store is for key (the original) */ | |
a0714e2c | 490 | entry = hv_fetch_common(hv, NULL, nkey, klen, |
086cb327 NC |
491 | HVhek_FREEKEY, /* free nkey */ |
492 | 0 /* non-LVAL fetch */, | |
a0714e2c | 493 | NULL /* no value */, |
086cb327 NC |
494 | 0 /* compute hash */); |
495 | if (!entry && (action & HV_FETCH_LVALUE)) { | |
496 | /* This call will free key if necessary. | |
497 | Do it this way to encourage compiler to tail | |
498 | call optimise. */ | |
499 | entry = hv_fetch_common(hv, keysv, key, klen, | |
500 | flags, HV_FETCH_ISSTORE, | |
561b68a9 | 501 | newSV(0), hash); |
086cb327 NC |
502 | } else { |
503 | if (flags & HVhek_FREEKEY) | |
504 | Safefree(key); | |
505 | } | |
506 | return entry; | |
7f66fda2 | 507 | } |
902173a3 | 508 | } |
7f66fda2 NC |
509 | #endif |
510 | } /* ISFETCH */ | |
511 | else if (SvRMAGICAL(hv) && (action & HV_FETCH_ISEXISTS)) { | |
512 | if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) { | |
b2c64049 NC |
513 | /* I don't understand why hv_exists_ent has svret and sv, |
514 | whereas hv_exists only had one. */ | |
9d4ba2ae | 515 | SV * const svret = sv_newmortal(); |
b2c64049 | 516 | sv = sv_newmortal(); |
7f66fda2 NC |
517 | |
518 | if (keysv || is_utf8) { | |
519 | if (!keysv) { | |
520 | keysv = newSVpvn(key, klen); | |
521 | SvUTF8_on(keysv); | |
522 | } else { | |
523 | keysv = newSVsv(keysv); | |
524 | } | |
b2c64049 NC |
525 | mg_copy((SV*)hv, sv, (char *)sv_2mortal(keysv), HEf_SVKEY); |
526 | } else { | |
527 | mg_copy((SV*)hv, sv, key, klen); | |
7f66fda2 | 528 | } |
b2c64049 NC |
529 | if (flags & HVhek_FREEKEY) |
530 | Safefree(key); | |
7f66fda2 NC |
531 | magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem)); |
532 | /* This cast somewhat evil, but I'm merely using NULL/ | |
533 | not NULL to return the boolean exists. | |
534 | And I know hv is not NULL. */ | |
535 | return SvTRUE(svret) ? (HE *)hv : NULL; | |
e7152ba2 | 536 | } |
7f66fda2 NC |
537 | #ifdef ENV_IS_CASELESS |
538 | else if (mg_find((SV*)hv, PERL_MAGIC_env)) { | |
539 | /* XXX This code isn't UTF8 clean. */ | |
a15d23f8 | 540 | char * const keysave = (char * const)key; |
b2c64049 NC |
541 | /* Will need to free this, so set FREEKEY flag. */ |
542 | key = savepvn(key,klen); | |
543 | key = (const char*)strupr((char*)key); | |
6136c704 | 544 | is_utf8 = FALSE; |
7f66fda2 | 545 | hash = 0; |
8b4f7dd5 | 546 | keysv = 0; |
b2c64049 NC |
547 | |
548 | if (flags & HVhek_FREEKEY) { | |
549 | Safefree(keysave); | |
550 | } | |
551 | flags |= HVhek_FREEKEY; | |
7f66fda2 | 552 | } |
902173a3 | 553 | #endif |
7f66fda2 | 554 | } /* ISEXISTS */ |
b2c64049 NC |
555 | else if (action & HV_FETCH_ISSTORE) { |
556 | bool needs_copy; | |
557 | bool needs_store; | |
558 | hv_magic_check (hv, &needs_copy, &needs_store); | |
559 | if (needs_copy) { | |
a3b680e6 | 560 | const bool save_taint = PL_tainted; |
b2c64049 NC |
561 | if (keysv || is_utf8) { |
562 | if (!keysv) { | |
563 | keysv = newSVpvn(key, klen); | |
564 | SvUTF8_on(keysv); | |
565 | } | |
566 | if (PL_tainting) | |
567 | PL_tainted = SvTAINTED(keysv); | |
568 | keysv = sv_2mortal(newSVsv(keysv)); | |
569 | mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY); | |
570 | } else { | |
571 | mg_copy((SV*)hv, val, key, klen); | |
572 | } | |
573 | ||
574 | TAINT_IF(save_taint); | |
1baaf5d7 | 575 | if (!needs_store) { |
b2c64049 NC |
576 | if (flags & HVhek_FREEKEY) |
577 | Safefree(key); | |
4608196e | 578 | return NULL; |
b2c64049 NC |
579 | } |
580 | #ifdef ENV_IS_CASELESS | |
581 | else if (mg_find((SV*)hv, PERL_MAGIC_env)) { | |
582 | /* XXX This code isn't UTF8 clean. */ | |
583 | const char *keysave = key; | |
584 | /* Will need to free this, so set FREEKEY flag. */ | |
585 | key = savepvn(key,klen); | |
586 | key = (const char*)strupr((char*)key); | |
6136c704 | 587 | is_utf8 = FALSE; |
b2c64049 | 588 | hash = 0; |
8b4f7dd5 | 589 | keysv = 0; |
b2c64049 NC |
590 | |
591 | if (flags & HVhek_FREEKEY) { | |
592 | Safefree(keysave); | |
593 | } | |
594 | flags |= HVhek_FREEKEY; | |
595 | } | |
596 | #endif | |
597 | } | |
598 | } /* ISSTORE */ | |
7f66fda2 | 599 | } /* SvMAGICAL */ |
fde52b5c | 600 | |
7b2c381c | 601 | if (!HvARRAY(hv)) { |
b2c64049 | 602 | if ((action & (HV_FETCH_LVALUE | HV_FETCH_ISSTORE)) |
fde52b5c | 603 | #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */ |
8aacddc1 | 604 | || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) |
fde52b5c | 605 | #endif |
d58e6666 NC |
606 | ) { |
607 | char *array; | |
a02a5408 | 608 | Newxz(array, |
cbec9347 | 609 | PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */), |
d58e6666 NC |
610 | char); |
611 | HvARRAY(hv) = (HE**)array; | |
612 | } | |
7f66fda2 NC |
613 | #ifdef DYNAMIC_ENV_FETCH |
614 | else if (action & HV_FETCH_ISEXISTS) { | |
615 | /* for an %ENV exists, if we do an insert it's by a recursive | |
616 | store call, so avoid creating HvARRAY(hv) right now. */ | |
617 | } | |
618 | #endif | |
113738bb NC |
619 | else { |
620 | /* XXX remove at some point? */ | |
621 | if (flags & HVhek_FREEKEY) | |
622 | Safefree(key); | |
623 | ||
fde52b5c | 624 | return 0; |
113738bb | 625 | } |
fde52b5c | 626 | } |
627 | ||
19692e8d | 628 | if (is_utf8) { |
41d88b63 | 629 | char * const keysave = (char *)key; |
f9a63242 | 630 | key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8); |
19692e8d | 631 | if (is_utf8) |
c1fe5510 NC |
632 | flags |= HVhek_UTF8; |
633 | else | |
634 | flags &= ~HVhek_UTF8; | |
7f66fda2 NC |
635 | if (key != keysave) { |
636 | if (flags & HVhek_FREEKEY) | |
637 | Safefree(keysave); | |
19692e8d | 638 | flags |= HVhek_WASUTF8 | HVhek_FREEKEY; |
7f66fda2 | 639 | } |
19692e8d | 640 | } |
f9a63242 | 641 | |
4b5190b5 NC |
642 | if (HvREHASH(hv)) { |
643 | PERL_HASH_INTERNAL(hash, key, klen); | |
b2c64049 NC |
644 | /* We don't have a pointer to the hv, so we have to replicate the |
645 | flag into every HEK, so that hv_iterkeysv can see it. */ | |
646 | /* And yes, you do need this even though you are not "storing" because | |
fdcd69b6 NC |
647 | you can flip the flags below if doing an lval lookup. (And that |
648 | was put in to give the semantics Andreas was expecting.) */ | |
649 | flags |= HVhek_REHASH; | |
4b5190b5 | 650 | } else if (!hash) { |
113738bb | 651 | if (keysv && (SvIsCOW_shared_hash(keysv))) { |
c158a4fd | 652 | hash = SvSHARED_HASH(keysv); |
46187eeb NC |
653 | } else { |
654 | PERL_HASH(hash, key, klen); | |
655 | } | |
656 | } | |
effa1e2d | 657 | |
113738bb NC |
658 | masked_flags = (flags & HVhek_MASK); |
659 | ||
7f66fda2 | 660 | #ifdef DYNAMIC_ENV_FETCH |
4608196e | 661 | if (!HvARRAY(hv)) entry = NULL; |
7f66fda2 NC |
662 | else |
663 | #endif | |
b2c64049 | 664 | { |
7b2c381c | 665 | entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; |
b2c64049 | 666 | } |
0298d7b9 | 667 | for (; entry; entry = HeNEXT(entry)) { |
fde52b5c | 668 | if (HeHASH(entry) != hash) /* strings can't be equal */ |
669 | continue; | |
eb160463 | 670 | if (HeKLEN(entry) != (I32)klen) |
fde52b5c | 671 | continue; |
1c846c1f | 672 | if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */ |
fde52b5c | 673 | continue; |
113738bb | 674 | if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8) |
c3654f1a | 675 | continue; |
b2c64049 NC |
676 | |
677 | if (action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE)) { | |
678 | if (HeKFLAGS(entry) != masked_flags) { | |
679 | /* We match if HVhek_UTF8 bit in our flags and hash key's | |
680 | match. But if entry was set previously with HVhek_WASUTF8 | |
681 | and key now doesn't (or vice versa) then we should change | |
682 | the key's flag, as this is assignment. */ | |
683 | if (HvSHAREKEYS(hv)) { | |
684 | /* Need to swap the key we have for a key with the flags we | |
685 | need. As keys are shared we can't just write to the | |
686 | flag, so we share the new one, unshare the old one. */ | |
6136c704 | 687 | HEK * const new_hek = share_hek_flags(key, klen, hash, |
6e838c70 | 688 | masked_flags); |
b2c64049 NC |
689 | unshare_hek (HeKEY_hek(entry)); |
690 | HeKEY_hek(entry) = new_hek; | |
691 | } | |
5d2b1485 NC |
692 | else if (hv == PL_strtab) { |
693 | /* PL_strtab is usually the only hash without HvSHAREKEYS, | |
694 | so putting this test here is cheap */ | |
695 | if (flags & HVhek_FREEKEY) | |
696 | Safefree(key); | |
697 | Perl_croak(aTHX_ S_strtab_error, | |
698 | action & HV_FETCH_LVALUE ? "fetch" : "store"); | |
699 | } | |
b2c64049 NC |
700 | else |
701 | HeKFLAGS(entry) = masked_flags; | |
702 | if (masked_flags & HVhek_ENABLEHVKFLAGS) | |
703 | HvHASKFLAGS_on(hv); | |
704 | } | |
705 | if (HeVAL(entry) == &PL_sv_placeholder) { | |
706 | /* yes, can store into placeholder slot */ | |
707 | if (action & HV_FETCH_LVALUE) { | |
708 | if (SvMAGICAL(hv)) { | |
709 | /* This preserves behaviour with the old hv_fetch | |
710 | implementation which at this point would bail out | |
711 | with a break; (at "if we find a placeholder, we | |
712 | pretend we haven't found anything") | |
713 | ||
714 | That break mean that if a placeholder were found, it | |
715 | caused a call into hv_store, which in turn would | |
716 | check magic, and if there is no magic end up pretty | |
717 | much back at this point (in hv_store's code). */ | |
718 | break; | |
719 | } | |
720 | /* LVAL fetch which actaully needs a store. */ | |
561b68a9 | 721 | val = newSV(0); |
ca732855 | 722 | HvPLACEHOLDERS(hv)--; |
b2c64049 NC |
723 | } else { |
724 | /* store */ | |
725 | if (val != &PL_sv_placeholder) | |
ca732855 | 726 | HvPLACEHOLDERS(hv)--; |
b2c64049 NC |
727 | } |
728 | HeVAL(entry) = val; | |
729 | } else if (action & HV_FETCH_ISSTORE) { | |
730 | SvREFCNT_dec(HeVAL(entry)); | |
731 | HeVAL(entry) = val; | |
732 | } | |
27bcc0a7 | 733 | } else if (HeVAL(entry) == &PL_sv_placeholder) { |
b2c64049 NC |
734 | /* if we find a placeholder, we pretend we haven't found |
735 | anything */ | |
8aacddc1 | 736 | break; |
b2c64049 | 737 | } |
113738bb NC |
738 | if (flags & HVhek_FREEKEY) |
739 | Safefree(key); | |
fde52b5c | 740 | return entry; |
741 | } | |
742 | #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */ | |
0ed29950 NC |
743 | if (!(action & HV_FETCH_ISSTORE) |
744 | && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) { | |
a6c40364 | 745 | unsigned long len; |
9d4ba2ae | 746 | const char * const env = PerlEnv_ENVgetenv_len(key,&len); |
a6c40364 GS |
747 | if (env) { |
748 | sv = newSVpvn(env,len); | |
749 | SvTAINTED_on(sv); | |
7fd3d16e | 750 | return hv_fetch_common(hv,keysv,key,klen,flags,HV_FETCH_ISSTORE,sv, |
b2c64049 | 751 | hash); |
a6c40364 | 752 | } |
fde52b5c | 753 | } |
754 | #endif | |
7f66fda2 NC |
755 | |
756 | if (!entry && SvREADONLY(hv) && !(action & HV_FETCH_ISEXISTS)) { | |
c445ea15 | 757 | hv_notallowed(flags, key, klen, |
c8cd6465 NC |
758 | "Attempt to access disallowed key '%"SVf"' in" |
759 | " a restricted hash"); | |
1b1f1335 | 760 | } |
b2c64049 NC |
761 | if (!(action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE))) { |
762 | /* Not doing some form of store, so return failure. */ | |
763 | if (flags & HVhek_FREEKEY) | |
764 | Safefree(key); | |
765 | return 0; | |
766 | } | |
113738bb | 767 | if (action & HV_FETCH_LVALUE) { |
561b68a9 | 768 | val = newSV(0); |
b2c64049 NC |
769 | if (SvMAGICAL(hv)) { |
770 | /* At this point the old hv_fetch code would call to hv_store, | |
771 | which in turn might do some tied magic. So we need to make that | |
772 | magic check happen. */ | |
773 | /* gonna assign to this, so it better be there */ | |
774 | return hv_fetch_common(hv, keysv, key, klen, flags, | |
775 | HV_FETCH_ISSTORE, val, hash); | |
776 | /* XXX Surely that could leak if the fetch-was-store fails? | |
777 | Just like the hv_fetch. */ | |
113738bb NC |
778 | } |
779 | } | |
780 | ||
b2c64049 NC |
781 | /* Welcome to hv_store... */ |
782 | ||
7b2c381c | 783 | if (!HvARRAY(hv)) { |
b2c64049 NC |
784 | /* Not sure if we can get here. I think the only case of oentry being |
785 | NULL is for %ENV with dynamic env fetch. But that should disappear | |
786 | with magic in the previous code. */ | |
d58e6666 | 787 | char *array; |
a02a5408 | 788 | Newxz(array, |
b2c64049 | 789 | PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */), |
d58e6666 NC |
790 | char); |
791 | HvARRAY(hv) = (HE**)array; | |
b2c64049 NC |
792 | } |
793 | ||
7b2c381c | 794 | oentry = &(HvARRAY(hv))[hash & (I32) xhv->xhv_max]; |
ab4af705 | 795 | |
b2c64049 NC |
796 | entry = new_HE(); |
797 | /* share_hek_flags will do the free for us. This might be considered | |
798 | bad API design. */ | |
799 | if (HvSHAREKEYS(hv)) | |
6e838c70 | 800 | HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags); |
5d2b1485 NC |
801 | else if (hv == PL_strtab) { |
802 | /* PL_strtab is usually the only hash without HvSHAREKEYS, so putting | |
803 | this test here is cheap */ | |
804 | if (flags & HVhek_FREEKEY) | |
805 | Safefree(key); | |
806 | Perl_croak(aTHX_ S_strtab_error, | |
807 | action & HV_FETCH_LVALUE ? "fetch" : "store"); | |
808 | } | |
b2c64049 NC |
809 | else /* gotta do the real thing */ |
810 | HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags); | |
811 | HeVAL(entry) = val; | |
812 | HeNEXT(entry) = *oentry; | |
813 | *oentry = entry; | |
814 | ||
815 | if (val == &PL_sv_placeholder) | |
ca732855 | 816 | HvPLACEHOLDERS(hv)++; |
b2c64049 NC |
817 | if (masked_flags & HVhek_ENABLEHVKFLAGS) |
818 | HvHASKFLAGS_on(hv); | |
819 | ||
0298d7b9 NC |
820 | { |
821 | const HE *counter = HeNEXT(entry); | |
822 | ||
4c7185a0 | 823 | xhv->xhv_keys++; /* HvTOTALKEYS(hv)++ */ |
0298d7b9 NC |
824 | if (!counter) { /* initial entry? */ |
825 | xhv->xhv_fill++; /* HvFILL(hv)++ */ | |
826 | } else if (xhv->xhv_keys > (IV)xhv->xhv_max) { | |
827 | hsplit(hv); | |
828 | } else if(!HvREHASH(hv)) { | |
829 | U32 n_links = 1; | |
830 | ||
831 | while ((counter = HeNEXT(counter))) | |
832 | n_links++; | |
833 | ||
834 | if (n_links > HV_MAX_LENGTH_BEFORE_SPLIT) { | |
835 | /* Use only the old HvKEYS(hv) > HvMAX(hv) condition to limit | |
836 | bucket splits on a rehashed hash, as we're not going to | |
837 | split it again, and if someone is lucky (evil) enough to | |
838 | get all the keys in one list they could exhaust our memory | |
839 | as we repeatedly double the number of buckets on every | |
840 | entry. Linear search feels a less worse thing to do. */ | |
841 | hsplit(hv); | |
842 | } | |
843 | } | |
fde52b5c | 844 | } |
b2c64049 NC |
845 | |
846 | return entry; | |
fde52b5c | 847 | } |
848 | ||
864dbfa3 | 849 | STATIC void |
b0e6ae5b | 850 | S_hv_magic_check(HV *hv, bool *needs_copy, bool *needs_store) |
d0066dc7 | 851 | { |
a3b680e6 | 852 | const MAGIC *mg = SvMAGIC(hv); |
d0066dc7 OT |
853 | *needs_copy = FALSE; |
854 | *needs_store = TRUE; | |
855 | while (mg) { | |
856 | if (isUPPER(mg->mg_type)) { | |
857 | *needs_copy = TRUE; | |
d60c5a05 | 858 | if (mg->mg_type == PERL_MAGIC_tied) { |
d0066dc7 | 859 | *needs_store = FALSE; |
4ab2a30b | 860 | return; /* We've set all there is to set. */ |
d0066dc7 OT |
861 | } |
862 | } | |
863 | mg = mg->mg_moremagic; | |
864 | } | |
865 | } | |
866 | ||
954c1994 | 867 | /* |
a3bcc51e TP |
868 | =for apidoc hv_scalar |
869 | ||
870 | Evaluates the hash in scalar context and returns the result. Handles magic when the hash is tied. | |
871 | ||
872 | =cut | |
873 | */ | |
874 | ||
875 | SV * | |
876 | Perl_hv_scalar(pTHX_ HV *hv) | |
877 | { | |
a3bcc51e | 878 | SV *sv; |
823a54a3 AL |
879 | |
880 | if (SvRMAGICAL(hv)) { | |
881 | MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_tied); | |
882 | if (mg) | |
883 | return magic_scalarpack(hv, mg); | |
884 | } | |
a3bcc51e TP |
885 | |
886 | sv = sv_newmortal(); | |
887 | if (HvFILL((HV*)hv)) | |
888 | Perl_sv_setpvf(aTHX_ sv, "%ld/%ld", | |
889 | (long)HvFILL(hv), (long)HvMAX(hv) + 1); | |
890 | else | |
891 | sv_setiv(sv, 0); | |
892 | ||
893 | return sv; | |
894 | } | |
895 | ||
896 | /* | |
954c1994 GS |
897 | =for apidoc hv_delete |
898 | ||
899 | Deletes a key/value pair in the hash. The value SV is removed from the | |
1c846c1f | 900 | hash and returned to the caller. The C<klen> is the length of the key. |
954c1994 GS |
901 | The C<flags> value will normally be zero; if set to G_DISCARD then NULL |
902 | will be returned. | |
903 | ||
904 | =cut | |
905 | */ | |
906 | ||
79072805 | 907 | SV * |
cd6d36ac | 908 | Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 flags) |
79072805 | 909 | { |
cd6d36ac | 910 | STRLEN klen; |
6136c704 | 911 | int k_flags; |
cd6d36ac NC |
912 | |
913 | if (klen_i32 < 0) { | |
914 | klen = -klen_i32; | |
6136c704 | 915 | k_flags = HVhek_UTF8; |
cd6d36ac NC |
916 | } else { |
917 | klen = klen_i32; | |
6136c704 | 918 | k_flags = 0; |
cd6d36ac NC |
919 | } |
920 | return hv_delete_common(hv, NULL, key, klen, k_flags, flags, 0); | |
fde52b5c | 921 | } |
922 | ||
954c1994 GS |
923 | /* |
924 | =for apidoc hv_delete_ent | |
925 | ||
926 | Deletes a key/value pair in the hash. The value SV is removed from the | |
927 | hash and returned to the caller. The C<flags> value will normally be zero; | |
928 | if set to G_DISCARD then NULL will be returned. C<hash> can be a valid | |
929 | precomputed hash value, or 0 to ask for it to be computed. | |
930 | ||
931 | =cut | |
932 | */ | |
933 | ||
fabdb6c0 | 934 | /* XXX This looks like an ideal candidate to inline */ |
fde52b5c | 935 | SV * |
864dbfa3 | 936 | Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash) |
fde52b5c | 937 | { |
cd6d36ac | 938 | return hv_delete_common(hv, keysv, NULL, 0, 0, flags, hash); |
f1317c8d NC |
939 | } |
940 | ||
8f8d40ab | 941 | STATIC SV * |
cd6d36ac NC |
942 | S_hv_delete_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen, |
943 | int k_flags, I32 d_flags, U32 hash) | |
f1317c8d | 944 | { |
27da23d5 | 945 | dVAR; |
cbec9347 | 946 | register XPVHV* xhv; |
fde52b5c | 947 | register HE *entry; |
948 | register HE **oentry; | |
9e720f71 | 949 | HE *const *first_entry; |
da58a35d | 950 | bool is_utf8; |
7a9669ca | 951 | int masked_flags; |
1c846c1f | 952 | |
fde52b5c | 953 | if (!hv) |
a0714e2c | 954 | return NULL; |
f1317c8d NC |
955 | |
956 | if (keysv) { | |
1e73acc8 | 957 | if (SvSMAGICAL(hv) && SvGMAGICAL(hv)) |
324a0d18 | 958 | keysv = hv_magic_uvar_xkey(hv, keysv, HV_DELETE); |
e593d2fe AE |
959 | if (k_flags & HVhek_FREEKEY) |
960 | Safefree(key); | |
5c144d81 | 961 | key = SvPV_const(keysv, klen); |
cd6d36ac | 962 | k_flags = 0; |
f1317c8d NC |
963 | is_utf8 = (SvUTF8(keysv) != 0); |
964 | } else { | |
cd6d36ac | 965 | is_utf8 = ((k_flags & HVhek_UTF8) ? TRUE : FALSE); |
f1317c8d | 966 | } |
f1317c8d | 967 | |
fde52b5c | 968 | if (SvRMAGICAL(hv)) { |
0a0bb7c7 OT |
969 | bool needs_copy; |
970 | bool needs_store; | |
971 | hv_magic_check (hv, &needs_copy, &needs_store); | |
972 | ||
f1317c8d | 973 | if (needs_copy) { |
6136c704 | 974 | SV *sv; |
7a9669ca NC |
975 | entry = hv_fetch_common(hv, keysv, key, klen, |
976 | k_flags & ~HVhek_FREEKEY, HV_FETCH_LVALUE, | |
a0714e2c | 977 | NULL, hash); |
7a9669ca | 978 | sv = entry ? HeVAL(entry) : NULL; |
f1317c8d NC |
979 | if (sv) { |
980 | if (SvMAGICAL(sv)) { | |
981 | mg_clear(sv); | |
982 | } | |
983 | if (!needs_store) { | |
984 | if (mg_find(sv, PERL_MAGIC_tiedelem)) { | |
985 | /* No longer an element */ | |
986 | sv_unmagic(sv, PERL_MAGIC_tiedelem); | |
987 | return sv; | |
988 | } | |
a0714e2c | 989 | return NULL; /* element cannot be deleted */ |
f1317c8d | 990 | } |
902173a3 | 991 | #ifdef ENV_IS_CASELESS |
8167a60a NC |
992 | else if (mg_find((SV*)hv, PERL_MAGIC_env)) { |
993 | /* XXX This code isn't UTF8 clean. */ | |
994 | keysv = sv_2mortal(newSVpvn(key,klen)); | |
995 | if (k_flags & HVhek_FREEKEY) { | |
996 | Safefree(key); | |
997 | } | |
998 | key = strupr(SvPVX(keysv)); | |
999 | is_utf8 = 0; | |
1000 | k_flags = 0; | |
1001 | hash = 0; | |
7f66fda2 | 1002 | } |
510ac311 | 1003 | #endif |
2fd1c6b8 | 1004 | } |
2fd1c6b8 | 1005 | } |
fde52b5c | 1006 | } |
cbec9347 | 1007 | xhv = (XPVHV*)SvANY(hv); |
7b2c381c | 1008 | if (!HvARRAY(hv)) |
a0714e2c | 1009 | return NULL; |
fde52b5c | 1010 | |
19692e8d | 1011 | if (is_utf8) { |
c445ea15 | 1012 | const char * const keysave = key; |
b464bac0 | 1013 | key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8); |
cd6d36ac | 1014 | |
19692e8d | 1015 | if (is_utf8) |
cd6d36ac NC |
1016 | k_flags |= HVhek_UTF8; |
1017 | else | |
1018 | k_flags &= ~HVhek_UTF8; | |
7f66fda2 NC |
1019 | if (key != keysave) { |
1020 | if (k_flags & HVhek_FREEKEY) { | |
1021 | /* This shouldn't happen if our caller does what we expect, | |
1022 | but strictly the API allows it. */ | |
1023 | Safefree(keysave); | |
1024 | } | |
1025 | k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY; | |
1026 | } | |
cd6d36ac | 1027 | HvHASKFLAGS_on((SV*)hv); |
19692e8d | 1028 | } |
f9a63242 | 1029 | |
4b5190b5 NC |
1030 | if (HvREHASH(hv)) { |
1031 | PERL_HASH_INTERNAL(hash, key, klen); | |
1032 | } else if (!hash) { | |
7a9669ca | 1033 | if (keysv && (SvIsCOW_shared_hash(keysv))) { |
c158a4fd | 1034 | hash = SvSHARED_HASH(keysv); |
7a9669ca NC |
1035 | } else { |
1036 | PERL_HASH(hash, key, klen); | |
1037 | } | |
4b5190b5 | 1038 | } |
fde52b5c | 1039 | |
7a9669ca NC |
1040 | masked_flags = (k_flags & HVhek_MASK); |
1041 | ||
9e720f71 | 1042 | first_entry = oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; |
fde52b5c | 1043 | entry = *oentry; |
9e720f71 | 1044 | for (; entry; oentry = &HeNEXT(entry), entry = *oentry) { |
6136c704 | 1045 | SV *sv; |
fde52b5c | 1046 | if (HeHASH(entry) != hash) /* strings can't be equal */ |
1047 | continue; | |
eb160463 | 1048 | if (HeKLEN(entry) != (I32)klen) |
fde52b5c | 1049 | continue; |
1c846c1f | 1050 | if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */ |
fde52b5c | 1051 | continue; |
7a9669ca | 1052 | if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8) |
c3654f1a | 1053 | continue; |
8aacddc1 | 1054 | |
5d2b1485 NC |
1055 | if (hv == PL_strtab) { |
1056 | if (k_flags & HVhek_FREEKEY) | |
1057 | Safefree(key); | |
1058 | Perl_croak(aTHX_ S_strtab_error, "delete"); | |
1059 | } | |
1060 | ||
8aacddc1 | 1061 | /* if placeholder is here, it's already been deleted.... */ |
6136c704 AL |
1062 | if (HeVAL(entry) == &PL_sv_placeholder) { |
1063 | if (k_flags & HVhek_FREEKEY) | |
1064 | Safefree(key); | |
1065 | return NULL; | |
8aacddc1 | 1066 | } |
6136c704 | 1067 | if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) { |
d4c19fe8 | 1068 | hv_notallowed(k_flags, key, klen, |
c8cd6465 NC |
1069 | "Attempt to delete readonly key '%"SVf"' from" |
1070 | " a restricted hash"); | |
8aacddc1 | 1071 | } |
b84d0860 NC |
1072 | if (k_flags & HVhek_FREEKEY) |
1073 | Safefree(key); | |
8aacddc1 | 1074 | |
cd6d36ac | 1075 | if (d_flags & G_DISCARD) |
a0714e2c | 1076 | sv = NULL; |
94f7643d | 1077 | else { |
79d01fbf | 1078 | sv = sv_2mortal(HeVAL(entry)); |
7996736c | 1079 | HeVAL(entry) = &PL_sv_placeholder; |
94f7643d | 1080 | } |
8aacddc1 NIS |
1081 | |
1082 | /* | |
1083 | * If a restricted hash, rather than really deleting the entry, put | |
1084 | * a placeholder there. This marks the key as being "approved", so | |
1085 | * we can still access via not-really-existing key without raising | |
1086 | * an error. | |
1087 | */ | |
1088 | if (SvREADONLY(hv)) { | |
754604c4 | 1089 | SvREFCNT_dec(HeVAL(entry)); |
7996736c | 1090 | HeVAL(entry) = &PL_sv_placeholder; |
8aacddc1 NIS |
1091 | /* We'll be saving this slot, so the number of allocated keys |
1092 | * doesn't go down, but the number placeholders goes up */ | |
ca732855 | 1093 | HvPLACEHOLDERS(hv)++; |
8aacddc1 | 1094 | } else { |
a26e96df | 1095 | *oentry = HeNEXT(entry); |
9e720f71 | 1096 | if(!*first_entry) { |
a26e96df | 1097 | xhv->xhv_fill--; /* HvFILL(hv)-- */ |
9e720f71 | 1098 | } |
b79f7545 | 1099 | if (SvOOK(hv) && entry == HvAUX(hv)->xhv_eiter /* HvEITER(hv) */) |
8aacddc1 NIS |
1100 | HvLAZYDEL_on(hv); |
1101 | else | |
1102 | hv_free_ent(hv, entry); | |
4c7185a0 | 1103 | xhv->xhv_keys--; /* HvTOTALKEYS(hv)-- */ |
574c8022 | 1104 | if (xhv->xhv_keys == 0) |
19692e8d | 1105 | HvHASKFLAGS_off(hv); |
8aacddc1 | 1106 | } |
79072805 LW |
1107 | return sv; |
1108 | } | |
8aacddc1 | 1109 | if (SvREADONLY(hv)) { |
d4c19fe8 | 1110 | hv_notallowed(k_flags, key, klen, |
c8cd6465 NC |
1111 | "Attempt to delete disallowed key '%"SVf"' from" |
1112 | " a restricted hash"); | |
8aacddc1 NIS |
1113 | } |
1114 | ||
19692e8d | 1115 | if (k_flags & HVhek_FREEKEY) |
f9a63242 | 1116 | Safefree(key); |
a0714e2c | 1117 | return NULL; |
79072805 LW |
1118 | } |
1119 | ||
76e3520e | 1120 | STATIC void |
cea2e8a9 | 1121 | S_hsplit(pTHX_ HV *hv) |
79072805 | 1122 | { |
97aff369 | 1123 | dVAR; |
1e05feb3 | 1124 | register XPVHV* const xhv = (XPVHV*)SvANY(hv); |
a3b680e6 | 1125 | const I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */ |
79072805 LW |
1126 | register I32 newsize = oldsize * 2; |
1127 | register I32 i; | |
7b2c381c | 1128 | char *a = (char*) HvARRAY(hv); |
72311751 | 1129 | register HE **aep; |
79072805 | 1130 | register HE **oentry; |
4b5190b5 NC |
1131 | int longest_chain = 0; |
1132 | int was_shared; | |
79072805 | 1133 | |
18026298 | 1134 | /*PerlIO_printf(PerlIO_stderr(), "hsplit called for %p which had %d\n", |
6c9570dc | 1135 | (void*)hv, (int) oldsize);*/ |
18026298 | 1136 | |
5d88ecd7 | 1137 | if (HvPLACEHOLDERS_get(hv) && !SvREADONLY(hv)) { |
18026298 NC |
1138 | /* Can make this clear any placeholders first for non-restricted hashes, |
1139 | even though Storable rebuilds restricted hashes by putting in all the | |
1140 | placeholders (first) before turning on the readonly flag, because | |
1141 | Storable always pre-splits the hash. */ | |
1142 | hv_clear_placeholders(hv); | |
1143 | } | |
1144 | ||
3280af22 | 1145 | PL_nomemok = TRUE; |
8d6dde3e | 1146 | #if defined(STRANGE_MALLOC) || defined(MYMALLOC) |
b79f7545 NC |
1147 | Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize) |
1148 | + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char); | |
422a93e5 | 1149 | if (!a) { |
4a33f861 | 1150 | PL_nomemok = FALSE; |
422a93e5 GA |
1151 | return; |
1152 | } | |
b79f7545 | 1153 | if (SvOOK(hv)) { |
7a9b70e9 | 1154 | Copy(&a[oldsize * sizeof(HE*)], &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux); |
b79f7545 | 1155 | } |
4633a7c4 | 1156 | #else |
a02a5408 | 1157 | Newx(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize) |
b79f7545 | 1158 | + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char); |
422a93e5 | 1159 | if (!a) { |
3280af22 | 1160 | PL_nomemok = FALSE; |
422a93e5 GA |
1161 | return; |
1162 | } | |
7b2c381c | 1163 | Copy(HvARRAY(hv), a, oldsize * sizeof(HE*), char); |
b79f7545 NC |
1164 | if (SvOOK(hv)) { |
1165 | Copy(HvAUX(hv), &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux); | |
1166 | } | |
fba3b22e | 1167 | if (oldsize >= 64) { |
7b2c381c | 1168 | offer_nice_chunk(HvARRAY(hv), |
b79f7545 NC |
1169 | PERL_HV_ARRAY_ALLOC_BYTES(oldsize) |
1170 | + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0)); | |
4633a7c4 LW |
1171 | } |
1172 | else | |
7b2c381c | 1173 | Safefree(HvARRAY(hv)); |
4633a7c4 LW |
1174 | #endif |
1175 | ||
3280af22 | 1176 | PL_nomemok = FALSE; |
72311751 | 1177 | Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/ |
cbec9347 | 1178 | xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */ |
7b2c381c | 1179 | HvARRAY(hv) = (HE**) a; |
72311751 | 1180 | aep = (HE**)a; |
79072805 | 1181 | |
72311751 | 1182 | for (i=0; i<oldsize; i++,aep++) { |
4b5190b5 NC |
1183 | int left_length = 0; |
1184 | int right_length = 0; | |
a3b680e6 AL |
1185 | register HE *entry; |
1186 | register HE **bep; | |
4b5190b5 | 1187 | |
72311751 | 1188 | if (!*aep) /* non-existent */ |
79072805 | 1189 | continue; |
72311751 GS |
1190 | bep = aep+oldsize; |
1191 | for (oentry = aep, entry = *aep; entry; entry = *oentry) { | |
eb160463 | 1192 | if ((HeHASH(entry) & newsize) != (U32)i) { |
fde52b5c | 1193 | *oentry = HeNEXT(entry); |
72311751 GS |
1194 | HeNEXT(entry) = *bep; |
1195 | if (!*bep) | |
cbec9347 | 1196 | xhv->xhv_fill++; /* HvFILL(hv)++ */ |
72311751 | 1197 | *bep = entry; |
4b5190b5 | 1198 | right_length++; |
79072805 LW |
1199 | continue; |
1200 | } | |
4b5190b5 | 1201 | else { |
fde52b5c | 1202 | oentry = &HeNEXT(entry); |
4b5190b5 NC |
1203 | left_length++; |
1204 | } | |
79072805 | 1205 | } |
72311751 | 1206 | if (!*aep) /* everything moved */ |
cbec9347 | 1207 | xhv->xhv_fill--; /* HvFILL(hv)-- */ |
4b5190b5 NC |
1208 | /* I think we don't actually need to keep track of the longest length, |
1209 | merely flag if anything is too long. But for the moment while | |
1210 | developing this code I'll track it. */ | |
1211 | if (left_length > longest_chain) | |
1212 | longest_chain = left_length; | |
1213 | if (right_length > longest_chain) | |
1214 | longest_chain = right_length; | |
1215 | } | |
1216 | ||
1217 | ||
1218 | /* Pick your policy for "hashing isn't working" here: */ | |
fdcd69b6 | 1219 | if (longest_chain <= HV_MAX_LENGTH_BEFORE_SPLIT /* split worked? */ |
4b5190b5 NC |
1220 | || HvREHASH(hv)) { |
1221 | return; | |
79072805 | 1222 | } |
4b5190b5 NC |
1223 | |
1224 | if (hv == PL_strtab) { | |
1225 | /* Urg. Someone is doing something nasty to the string table. | |
1226 | Can't win. */ | |
1227 | return; | |
1228 | } | |
1229 | ||
1230 | /* Awooga. Awooga. Pathological data. */ | |
6c9570dc | 1231 | /*PerlIO_printf(PerlIO_stderr(), "%p %d of %d with %d/%d buckets\n", (void*)hv, |
4b5190b5 NC |
1232 | longest_chain, HvTOTALKEYS(hv), HvFILL(hv), 1+HvMAX(hv));*/ |
1233 | ||
1234 | ++newsize; | |
a02a5408 | 1235 | Newxz(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize) |
b79f7545 NC |
1236 | + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char); |
1237 | if (SvOOK(hv)) { | |
1238 | Copy(HvAUX(hv), &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux); | |
1239 | } | |
1240 | ||
4b5190b5 NC |
1241 | was_shared = HvSHAREKEYS(hv); |
1242 | ||
1243 | xhv->xhv_fill = 0; | |
1244 | HvSHAREKEYS_off(hv); | |
1245 | HvREHASH_on(hv); | |
1246 | ||
7b2c381c | 1247 | aep = HvARRAY(hv); |
4b5190b5 NC |
1248 | |
1249 | for (i=0; i<newsize; i++,aep++) { | |
a3b680e6 | 1250 | register HE *entry = *aep; |
4b5190b5 NC |
1251 | while (entry) { |
1252 | /* We're going to trash this HE's next pointer when we chain it | |
1253 | into the new hash below, so store where we go next. */ | |
9d4ba2ae | 1254 | HE * const next = HeNEXT(entry); |
4b5190b5 | 1255 | UV hash; |
a3b680e6 | 1256 | HE **bep; |
4b5190b5 NC |
1257 | |
1258 | /* Rehash it */ | |
1259 | PERL_HASH_INTERNAL(hash, HeKEY(entry), HeKLEN(entry)); | |
1260 | ||
1261 | if (was_shared) { | |
1262 | /* Unshare it. */ | |
aec46f14 | 1263 | HEK * const new_hek |
4b5190b5 NC |
1264 | = save_hek_flags(HeKEY(entry), HeKLEN(entry), |
1265 | hash, HeKFLAGS(entry)); | |
1266 | unshare_hek (HeKEY_hek(entry)); | |
1267 | HeKEY_hek(entry) = new_hek; | |
1268 | } else { | |
1269 | /* Not shared, so simply write the new hash in. */ | |
1270 | HeHASH(entry) = hash; | |
1271 | } | |
1272 | /*PerlIO_printf(PerlIO_stderr(), "%d ", HeKFLAGS(entry));*/ | |
1273 | HEK_REHASH_on(HeKEY_hek(entry)); | |
1274 | /*PerlIO_printf(PerlIO_stderr(), "%d\n", HeKFLAGS(entry));*/ | |
1275 | ||
1276 | /* Copy oentry to the correct new chain. */ | |
1277 | bep = ((HE**)a) + (hash & (I32) xhv->xhv_max); | |
1278 | if (!*bep) | |
1279 | xhv->xhv_fill++; /* HvFILL(hv)++ */ | |
1280 | HeNEXT(entry) = *bep; | |
1281 | *bep = entry; | |
1282 | ||
1283 | entry = next; | |
1284 | } | |
1285 | } | |
7b2c381c NC |
1286 | Safefree (HvARRAY(hv)); |
1287 | HvARRAY(hv) = (HE **)a; | |
79072805 LW |
1288 | } |
1289 | ||
72940dca | 1290 | void |
864dbfa3 | 1291 | Perl_hv_ksplit(pTHX_ HV *hv, IV newmax) |
72940dca | 1292 | { |
97aff369 | 1293 | dVAR; |
cbec9347 | 1294 | register XPVHV* xhv = (XPVHV*)SvANY(hv); |
a3b680e6 | 1295 | const I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */ |
72940dca | 1296 | register I32 newsize; |
1297 | register I32 i; | |
72311751 GS |
1298 | register char *a; |
1299 | register HE **aep; | |
72940dca | 1300 | register HE *entry; |
1301 | register HE **oentry; | |
1302 | ||
1303 | newsize = (I32) newmax; /* possible truncation here */ | |
1304 | if (newsize != newmax || newmax <= oldsize) | |
1305 | return; | |
1306 | while ((newsize & (1 + ~newsize)) != newsize) { | |
1307 | newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */ | |
1308 | } | |
1309 | if (newsize < newmax) | |
1310 | newsize *= 2; | |
1311 | if (newsize < newmax) | |
1312 | return; /* overflow detection */ | |
1313 | ||
7b2c381c | 1314 | a = (char *) HvARRAY(hv); |
72940dca | 1315 | if (a) { |
3280af22 | 1316 | PL_nomemok = TRUE; |
8d6dde3e | 1317 | #if defined(STRANGE_MALLOC) || defined(MYMALLOC) |
b79f7545 NC |
1318 | Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize) |
1319 | + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char); | |
8aacddc1 | 1320 | if (!a) { |
4a33f861 | 1321 | PL_nomemok = FALSE; |
422a93e5 GA |
1322 | return; |
1323 | } | |
b79f7545 | 1324 | if (SvOOK(hv)) { |
7a9b70e9 | 1325 | Copy(&a[oldsize * sizeof(HE*)], &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux); |
b79f7545 | 1326 | } |
72940dca | 1327 | #else |
a02a5408 | 1328 | Newx(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize) |
b79f7545 | 1329 | + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char); |
8aacddc1 | 1330 | if (!a) { |
3280af22 | 1331 | PL_nomemok = FALSE; |
422a93e5 GA |
1332 | return; |
1333 | } | |
7b2c381c | 1334 | Copy(HvARRAY(hv), a, oldsize * sizeof(HE*), char); |
b79f7545 NC |
1335 | if (SvOOK(hv)) { |
1336 | Copy(HvAUX(hv), &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux); | |
1337 | } | |
fba3b22e | 1338 | if (oldsize >= 64) { |
7b2c381c | 1339 | offer_nice_chunk(HvARRAY(hv), |
b79f7545 NC |
1340 | PERL_HV_ARRAY_ALLOC_BYTES(oldsize) |
1341 | + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0)); | |
72940dca | 1342 | } |
1343 | else | |
7b2c381c | 1344 | Safefree(HvARRAY(hv)); |
72940dca | 1345 | #endif |
3280af22 | 1346 | PL_nomemok = FALSE; |
72311751 | 1347 | Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/ |
72940dca | 1348 | } |
1349 | else { | |
a02a5408 | 1350 | Newxz(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char); |
72940dca | 1351 | } |
cbec9347 | 1352 | xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */ |
7b2c381c | 1353 | HvARRAY(hv) = (HE **) a; |
cbec9347 | 1354 | if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */ |
72940dca | 1355 | return; |
1356 | ||
72311751 GS |
1357 | aep = (HE**)a; |
1358 | for (i=0; i<oldsize; i++,aep++) { | |
1359 | if (!*aep) /* non-existent */ | |
72940dca | 1360 | continue; |
72311751 | 1361 | for (oentry = aep, entry = *aep; entry; entry = *oentry) { |
6136c704 AL |
1362 | register I32 j = (HeHASH(entry) & newsize); |
1363 | ||
1364 | if (j != i) { | |
72940dca | 1365 | j -= i; |
1366 | *oentry = HeNEXT(entry); | |
72311751 | 1367 | if (!(HeNEXT(entry) = aep[j])) |
cbec9347 | 1368 | xhv->xhv_fill++; /* HvFILL(hv)++ */ |
72311751 | 1369 | aep[j] = entry; |
72940dca | 1370 | continue; |
1371 | } | |
1372 | else | |
1373 | oentry = &HeNEXT(entry); | |
1374 | } | |
72311751 | 1375 | if (!*aep) /* everything moved */ |
cbec9347 | 1376 | xhv->xhv_fill--; /* HvFILL(hv)-- */ |
72940dca | 1377 | } |
1378 | } | |
1379 | ||
954c1994 GS |
1380 | /* |
1381 | =for apidoc newHV | |
1382 | ||
1383 | Creates a new HV. The reference count is set to 1. | |
1384 | ||
1385 | =cut | |
1386 | */ | |
1387 | ||
79072805 | 1388 | HV * |
864dbfa3 | 1389 | Perl_newHV(pTHX) |
79072805 | 1390 | { |
cbec9347 | 1391 | register XPVHV* xhv; |
b9f83d2f | 1392 | HV * const hv = (HV*)newSV_type(SVt_PVHV); |
cbec9347 | 1393 | xhv = (XPVHV*)SvANY(hv); |
ce5d0612 | 1394 | assert(!SvOK(hv)); |
1c846c1f | 1395 | #ifndef NODEFAULT_SHAREKEYS |
fde52b5c | 1396 | HvSHAREKEYS_on(hv); /* key-sharing on by default */ |
1c846c1f | 1397 | #endif |
4b5190b5 | 1398 | |
cbec9347 JH |
1399 | xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */ |
1400 | xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */ | |
79072805 LW |
1401 | return hv; |
1402 | } | |
1403 | ||
b3ac6de7 | 1404 | HV * |
864dbfa3 | 1405 | Perl_newHVhv(pTHX_ HV *ohv) |
b3ac6de7 | 1406 | { |
9d4ba2ae | 1407 | HV * const hv = newHV(); |
4beac62f | 1408 | STRLEN hv_max, hv_fill; |
4beac62f AMS |
1409 | |
1410 | if (!ohv || (hv_fill = HvFILL(ohv)) == 0) | |
1411 | return hv; | |
4beac62f | 1412 | hv_max = HvMAX(ohv); |
b3ac6de7 | 1413 | |
b56ba0bf AMS |
1414 | if (!SvMAGICAL((SV *)ohv)) { |
1415 | /* It's an ordinary hash, so copy it fast. AMS 20010804 */ | |
eb160463 | 1416 | STRLEN i; |
a3b680e6 | 1417 | const bool shared = !!HvSHAREKEYS(ohv); |
aec46f14 | 1418 | HE **ents, ** const oents = (HE **)HvARRAY(ohv); |
ff875642 | 1419 | char *a; |
a02a5408 | 1420 | Newx(a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char); |
ff875642 | 1421 | ents = (HE**)a; |
b56ba0bf AMS |
1422 | |
1423 | /* In each bucket... */ | |
1424 | for (i = 0; i <= hv_max; i++) { | |
6136c704 | 1425 | HE *prev = NULL; |
aec46f14 | 1426 | HE *oent = oents[i]; |
b56ba0bf AMS |
1427 | |
1428 | if (!oent) { | |
1429 | ents[i] = NULL; | |
1430 | continue; | |
1431 | } | |
1432 | ||
1433 | /* Copy the linked list of entries. */ | |
aec46f14 | 1434 | for (; oent; oent = HeNEXT(oent)) { |
a3b680e6 AL |
1435 | const U32 hash = HeHASH(oent); |
1436 | const char * const key = HeKEY(oent); | |
1437 | const STRLEN len = HeKLEN(oent); | |
1438 | const int flags = HeKFLAGS(oent); | |
6136c704 | 1439 | HE * const ent = new_HE(); |
b56ba0bf | 1440 | |
45dea987 | 1441 | HeVAL(ent) = newSVsv(HeVAL(oent)); |
19692e8d | 1442 | HeKEY_hek(ent) |
6e838c70 | 1443 | = shared ? share_hek_flags(key, len, hash, flags) |
19692e8d | 1444 | : save_hek_flags(key, len, hash, flags); |
b56ba0bf AMS |
1445 | if (prev) |
1446 | HeNEXT(prev) = ent; | |
1447 | else | |
1448 | ents[i] = ent; | |
1449 | prev = ent; | |
1450 | HeNEXT(ent) = NULL; | |
1451 | } | |
1452 | } | |
1453 | ||
1454 | HvMAX(hv) = hv_max; | |
1455 | HvFILL(hv) = hv_fill; | |
8aacddc1 | 1456 | HvTOTALKEYS(hv) = HvTOTALKEYS(ohv); |
b56ba0bf | 1457 | HvARRAY(hv) = ents; |
aec46f14 | 1458 | } /* not magical */ |
b56ba0bf AMS |
1459 | else { |
1460 | /* Iterate over ohv, copying keys and values one at a time. */ | |
b3ac6de7 | 1461 | HE *entry; |
bfcb3514 NC |
1462 | const I32 riter = HvRITER_get(ohv); |
1463 | HE * const eiter = HvEITER_get(ohv); | |
b56ba0bf AMS |
1464 | |
1465 | /* Can we use fewer buckets? (hv_max is always 2^n-1) */ | |
1466 | while (hv_max && hv_max + 1 >= hv_fill * 2) | |
1467 | hv_max = hv_max / 2; | |
1468 | HvMAX(hv) = hv_max; | |
1469 | ||
4a76a316 | 1470 | hv_iterinit(ohv); |
e16e2ff8 | 1471 | while ((entry = hv_iternext_flags(ohv, 0))) { |
19692e8d NC |
1472 | hv_store_flags(hv, HeKEY(entry), HeKLEN(entry), |
1473 | newSVsv(HeVAL(entry)), HeHASH(entry), | |
1474 | HeKFLAGS(entry)); | |
b3ac6de7 | 1475 | } |
bfcb3514 NC |
1476 | HvRITER_set(ohv, riter); |
1477 | HvEITER_set(ohv, eiter); | |
b3ac6de7 | 1478 | } |
1c846c1f | 1479 | |
b3ac6de7 IZ |
1480 | return hv; |
1481 | } | |
1482 | ||
5b9c0671 NC |
1483 | /* A rather specialised version of newHVhv for copying %^H, ensuring all the |
1484 | magic stays on it. */ | |
1485 | HV * | |
1486 | Perl_hv_copy_hints_hv(pTHX_ HV *const ohv) | |
1487 | { | |
1488 | HV * const hv = newHV(); | |
1489 | STRLEN hv_fill; | |
1490 | ||
1491 | if (ohv && (hv_fill = HvFILL(ohv))) { | |
1492 | STRLEN hv_max = HvMAX(ohv); | |
1493 | HE *entry; | |
1494 | const I32 riter = HvRITER_get(ohv); | |
1495 | HE * const eiter = HvEITER_get(ohv); | |
1496 | ||
1497 | while (hv_max && hv_max + 1 >= hv_fill * 2) | |
1498 | hv_max = hv_max / 2; | |
1499 | HvMAX(hv) = hv_max; | |
1500 | ||
1501 | hv_iterinit(ohv); | |
1502 | while ((entry = hv_iternext_flags(ohv, 0))) { | |
1503 | SV *const sv = newSVsv(HeVAL(entry)); | |
1504 | sv_magic(sv, NULL, PERL_MAGIC_hintselem, | |
1505 | (char *)newSVhek (HeKEY_hek(entry)), HEf_SVKEY); | |
1506 | hv_store_flags(hv, HeKEY(entry), HeKLEN(entry), | |
1507 | sv, HeHASH(entry), HeKFLAGS(entry)); | |
1508 | } | |
1509 | HvRITER_set(ohv, riter); | |
1510 | HvEITER_set(ohv, eiter); | |
1511 | } | |
1512 | hv_magic(hv, NULL, PERL_MAGIC_hints); | |
1513 | return hv; | |
1514 | } | |
1515 | ||
79072805 | 1516 | void |
864dbfa3 | 1517 | Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry) |
79072805 | 1518 | { |
97aff369 | 1519 | dVAR; |
16bdeea2 GS |
1520 | SV *val; |
1521 | ||
68dc0745 | 1522 | if (!entry) |
79072805 | 1523 | return; |
16bdeea2 | 1524 | val = HeVAL(entry); |
0fa56319 RGS |
1525 | if (val && isGV(val) && GvCVu(val) && HvNAME_get(hv)) |
1526 | mro_method_changed_in(hv); /* deletion of method from stash */ | |
16bdeea2 | 1527 | SvREFCNT_dec(val); |
68dc0745 | 1528 | if (HeKLEN(entry) == HEf_SVKEY) { |
1529 | SvREFCNT_dec(HeKEY_sv(entry)); | |
8aacddc1 | 1530 | Safefree(HeKEY_hek(entry)); |
44a8e56a | 1531 | } |
1532 | else if (HvSHAREKEYS(hv)) | |
68dc0745 | 1533 | unshare_hek(HeKEY_hek(entry)); |
fde52b5c | 1534 | else |
68dc0745 | 1535 | Safefree(HeKEY_hek(entry)); |
d33b2eba | 1536 | del_HE(entry); |
79072805 LW |
1537 | } |
1538 | ||
1539 | void | |
864dbfa3 | 1540 | Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry) |
79072805 | 1541 | { |
97aff369 | 1542 | dVAR; |
68dc0745 | 1543 | if (!entry) |
79072805 | 1544 | return; |
bc4947fc NC |
1545 | /* SvREFCNT_inc to counter the SvREFCNT_dec in hv_free_ent */ |
1546 | sv_2mortal(SvREFCNT_inc(HeVAL(entry))); /* free between statements */ | |
68dc0745 | 1547 | if (HeKLEN(entry) == HEf_SVKEY) { |
bc4947fc | 1548 | sv_2mortal(SvREFCNT_inc(HeKEY_sv(entry))); |
44a8e56a | 1549 | } |
bc4947fc | 1550 | hv_free_ent(hv, entry); |
79072805 LW |
1551 | } |
1552 | ||
954c1994 GS |
1553 | /* |
1554 | =for apidoc hv_clear | |
1555 | ||
1556 | Clears a hash, making it empty. | |
1557 | ||
1558 | =cut | |
1559 | */ | |
1560 | ||
79072805 | 1561 | void |
864dbfa3 | 1562 | Perl_hv_clear(pTHX_ HV *hv) |
79072805 | 1563 | { |
27da23d5 | 1564 | dVAR; |
cbec9347 | 1565 | register XPVHV* xhv; |
79072805 LW |
1566 | if (!hv) |
1567 | return; | |
49293501 | 1568 | |
ecae49c0 NC |
1569 | DEBUG_A(Perl_hv_assert(aTHX_ hv)); |
1570 | ||
34c3c4e3 DM |
1571 | xhv = (XPVHV*)SvANY(hv); |
1572 | ||
7b2c381c | 1573 | if (SvREADONLY(hv) && HvARRAY(hv) != NULL) { |
34c3c4e3 | 1574 | /* restricted hash: convert all keys to placeholders */ |
b464bac0 AL |
1575 | STRLEN i; |
1576 | for (i = 0; i <= xhv->xhv_max; i++) { | |
7b2c381c | 1577 | HE *entry = (HvARRAY(hv))[i]; |
3a676441 JH |
1578 | for (; entry; entry = HeNEXT(entry)) { |
1579 | /* not already placeholder */ | |
7996736c | 1580 | if (HeVAL(entry) != &PL_sv_placeholder) { |
3a676441 | 1581 | if (HeVAL(entry) && SvREADONLY(HeVAL(entry))) { |
6136c704 | 1582 | SV* const keysv = hv_iterkeysv(entry); |
3a676441 | 1583 | Perl_croak(aTHX_ |
95b63a38 JH |
1584 | "Attempt to delete readonly key '%"SVf"' from a restricted hash", |
1585 | (void*)keysv); | |
3a676441 JH |
1586 | } |
1587 | SvREFCNT_dec(HeVAL(entry)); | |
7996736c | 1588 | HeVAL(entry) = &PL_sv_placeholder; |
ca732855 | 1589 | HvPLACEHOLDERS(hv)++; |
3a676441 | 1590 | } |
34c3c4e3 DM |
1591 | } |
1592 | } | |
df8c6964 | 1593 | goto reset; |
49293501 MS |
1594 | } |
1595 | ||
463ee0b2 | 1596 | hfreeentries(hv); |
ca732855 | 1597 | HvPLACEHOLDERS_set(hv, 0); |
7b2c381c | 1598 | if (HvARRAY(hv)) |
41f62432 | 1599 | Zero(HvARRAY(hv), xhv->xhv_max+1 /* HvMAX(hv)+1 */, HE*); |
a0d0e21e LW |
1600 | |
1601 | if (SvRMAGICAL(hv)) | |
1c846c1f | 1602 | mg_clear((SV*)hv); |
574c8022 | 1603 | |
19692e8d | 1604 | HvHASKFLAGS_off(hv); |
bb443f97 | 1605 | HvREHASH_off(hv); |
df8c6964 | 1606 | reset: |
b79f7545 | 1607 | if (SvOOK(hv)) { |
dd69841b BB |
1608 | if(HvNAME_get(hv)) |
1609 | mro_isa_changed_in(hv); | |
bfcb3514 NC |
1610 | HvEITER_set(hv, NULL); |
1611 | } | |
79072805 LW |
1612 | } |
1613 | ||
3540d4ce AB |
1614 | /* |
1615 | =for apidoc hv_clear_placeholders | |
1616 | ||
1617 | Clears any placeholders from a hash. If a restricted hash has any of its keys | |
1618 | marked as readonly and the key is subsequently deleted, the key is not actually | |
1619 | deleted but is marked by assigning it a value of &PL_sv_placeholder. This tags | |
1620 | it so it will be ignored by future operations such as iterating over the hash, | |
4cdaeff7 | 1621 | but will still allow the hash to have a value reassigned to the key at some |
3540d4ce AB |
1622 | future point. This function clears any such placeholder keys from the hash. |
1623 | See Hash::Util::lock_keys() for an example of its use. | |
1624 | ||
1625 | =cut | |
1626 | */ | |
1627 | ||
1628 | void | |
1629 | Perl_hv_clear_placeholders(pTHX_ HV *hv) | |
1630 | { | |
27da23d5 | 1631 | dVAR; |
b3ca2e83 NC |
1632 | const U32 items = (U32)HvPLACEHOLDERS_get(hv); |
1633 | ||
1634 | if (items) | |
1635 | clear_placeholders(hv, items); | |
1636 | } | |
1637 | ||
1638 | static void | |
1639 | S_clear_placeholders(pTHX_ HV *hv, U32 items) | |
1640 | { | |
1641 | dVAR; | |
b464bac0 | 1642 | I32 i; |
d3677389 NC |
1643 | |
1644 | if (items == 0) | |
1645 | return; | |
1646 | ||
b464bac0 | 1647 | i = HvMAX(hv); |
d3677389 NC |
1648 | do { |
1649 | /* Loop down the linked list heads */ | |
6136c704 | 1650 | bool first = TRUE; |
d3677389 | 1651 | HE **oentry = &(HvARRAY(hv))[i]; |
cf6db12b | 1652 | HE *entry; |
d3677389 | 1653 | |
cf6db12b | 1654 | while ((entry = *oentry)) { |
d3677389 NC |
1655 | if (HeVAL(entry) == &PL_sv_placeholder) { |
1656 | *oentry = HeNEXT(entry); | |
1657 | if (first && !*oentry) | |
1658 | HvFILL(hv)--; /* This linked list is now empty. */ | |
2e58978b | 1659 | if (entry == HvEITER_get(hv)) |
d3677389 NC |
1660 | HvLAZYDEL_on(hv); |
1661 | else | |
1662 | hv_free_ent(hv, entry); | |
1663 | ||
1664 | if (--items == 0) { | |
1665 | /* Finished. */ | |
5d88ecd7 | 1666 | HvTOTALKEYS(hv) -= (IV)HvPLACEHOLDERS_get(hv); |
d3677389 NC |
1667 | if (HvKEYS(hv) == 0) |
1668 | HvHASKFLAGS_off(hv); | |
5d88ecd7 | 1669 | HvPLACEHOLDERS_set(hv, 0); |
d3677389 NC |
1670 | return; |
1671 | } | |
213ce8b3 NC |
1672 | } else { |
1673 | oentry = &HeNEXT(entry); | |
6136c704 | 1674 | first = FALSE; |
d3677389 NC |
1675 | } |
1676 | } | |
1677 | } while (--i >= 0); | |
1678 | /* You can't get here, hence assertion should always fail. */ | |
1679 | assert (items == 0); | |
1680 | assert (0); | |
3540d4ce AB |
1681 | } |
1682 | ||
76e3520e | 1683 | STATIC void |
cea2e8a9 | 1684 | S_hfreeentries(pTHX_ HV *hv) |
79072805 | 1685 | { |
23976bdd | 1686 | /* This is the array that we're going to restore */ |
fd7de8a8 | 1687 | HE **const orig_array = HvARRAY(hv); |
23976bdd NC |
1688 | HEK *name; |
1689 | int attempts = 100; | |
3abe233e | 1690 | |
fd7de8a8 | 1691 | if (!orig_array) |
79072805 | 1692 | return; |
a0d0e21e | 1693 | |
23976bdd NC |
1694 | if (SvOOK(hv)) { |
1695 | /* If the hash is actually a symbol table with a name, look after the | |
1696 | name. */ | |
1697 | struct xpvhv_aux *iter = HvAUX(hv); | |
1698 | ||
1699 | name = iter->xhv_name; | |
1700 | iter->xhv_name = NULL; | |
1701 | } else { | |
1702 | name = NULL; | |
1703 | } | |
1704 | ||
23976bdd NC |
1705 | /* orig_array remains unchanged throughout the loop. If after freeing all |
1706 | the entries it turns out that one of the little blighters has triggered | |
1707 | an action that has caused HvARRAY to be re-allocated, then we set | |
1708 | array to the new HvARRAY, and try again. */ | |
1709 | ||
1710 | while (1) { | |
1711 | /* This is the one we're going to try to empty. First time round | |
1712 | it's the original array. (Hopefully there will only be 1 time | |
1713 | round) */ | |
6136c704 | 1714 | HE ** const array = HvARRAY(hv); |
7440661e | 1715 | I32 i = HvMAX(hv); |
23976bdd NC |
1716 | |
1717 | /* Because we have taken xhv_name out, the only allocated pointer | |
1718 | in the aux structure that might exist is the backreference array. | |
1719 | */ | |
1720 | ||
1721 | if (SvOOK(hv)) { | |
7440661e | 1722 | HE *entry; |
e1a479c5 | 1723 | struct mro_meta *meta; |
23976bdd NC |
1724 | struct xpvhv_aux *iter = HvAUX(hv); |
1725 | /* If there are weak references to this HV, we need to avoid | |
1726 | freeing them up here. In particular we need to keep the AV | |
1727 | visible as what we're deleting might well have weak references | |
1728 | back to this HV, so the for loop below may well trigger | |
1729 | the removal of backreferences from this array. */ | |
1730 | ||
1731 | if (iter->xhv_backreferences) { | |
1732 | /* So donate them to regular backref magic to keep them safe. | |
1733 | The sv_magic will increase the reference count of the AV, | |
1734 | so we need to drop it first. */ | |
5b285ea4 | 1735 | SvREFCNT_dec(iter->xhv_backreferences); |
23976bdd NC |
1736 | if (AvFILLp(iter->xhv_backreferences) == -1) { |
1737 | /* Turns out that the array is empty. Just free it. */ | |
1738 | SvREFCNT_dec(iter->xhv_backreferences); | |
1b8791d1 | 1739 | |
23976bdd NC |
1740 | } else { |
1741 | sv_magic((SV*)hv, (SV*)iter->xhv_backreferences, | |
1742 | PERL_MAGIC_backref, NULL, 0); | |
1743 | } | |
1744 | iter->xhv_backreferences = NULL; | |
5b285ea4 | 1745 | } |
86f55936 | 1746 | |
23976bdd NC |
1747 | entry = iter->xhv_eiter; /* HvEITER(hv) */ |
1748 | if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */ | |
1749 | HvLAZYDEL_off(hv); | |
1750 | hv_free_ent(hv, entry); | |
1751 | } | |
1752 | iter->xhv_riter = -1; /* HvRITER(hv) = -1 */ | |
4608196e | 1753 | iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */ |
b79f7545 | 1754 | |
e1a479c5 BB |
1755 | if((meta = iter->xhv_mro_meta)) { |
1756 | if(meta->mro_linear_dfs) SvREFCNT_dec(meta->mro_linear_dfs); | |
1757 | if(meta->mro_linear_c3) SvREFCNT_dec(meta->mro_linear_c3); | |
e1a479c5 BB |
1758 | if(meta->mro_nextmethod) SvREFCNT_dec(meta->mro_nextmethod); |
1759 | Safefree(meta); | |
1760 | iter->xhv_mro_meta = NULL; | |
1761 | } | |
1762 | ||
23976bdd | 1763 | /* There are now no allocated pointers in the aux structure. */ |
2f86008e | 1764 | |
23976bdd NC |
1765 | SvFLAGS(hv) &= ~SVf_OOK; /* Goodbye, aux structure. */ |
1766 | /* What aux structure? */ | |
a0d0e21e | 1767 | } |
bfcb3514 | 1768 | |
23976bdd NC |
1769 | /* make everyone else think the array is empty, so that the destructors |
1770 | * called for freed entries can't recusively mess with us */ | |
1771 | HvARRAY(hv) = NULL; | |
1772 | HvFILL(hv) = 0; | |
1773 | ((XPVHV*) SvANY(hv))->xhv_keys = 0; | |
1774 | ||
7440661e NC |
1775 | |
1776 | do { | |
1777 | /* Loop down the linked list heads */ | |
1778 | HE *entry = array[i]; | |
1779 | ||
1780 | while (entry) { | |
23976bdd NC |
1781 | register HE * const oentry = entry; |
1782 | entry = HeNEXT(entry); | |
1783 | hv_free_ent(hv, oentry); | |
1784 | } | |
7440661e | 1785 | } while (--i >= 0); |
b79f7545 | 1786 | |
23976bdd NC |
1787 | /* As there are no allocated pointers in the aux structure, it's now |
1788 | safe to free the array we just cleaned up, if it's not the one we're | |
1789 | going to put back. */ | |
1790 | if (array != orig_array) { | |
1791 | Safefree(array); | |
1792 | } | |
b79f7545 | 1793 | |
23976bdd NC |
1794 | if (!HvARRAY(hv)) { |
1795 | /* Good. No-one added anything this time round. */ | |
1796 | break; | |
bfcb3514 | 1797 | } |
b79f7545 | 1798 | |
23976bdd NC |
1799 | if (SvOOK(hv)) { |
1800 | /* Someone attempted to iterate or set the hash name while we had | |
1801 | the array set to 0. We'll catch backferences on the next time | |
1802 | round the while loop. */ | |
1803 | assert(HvARRAY(hv)); | |
1b8791d1 | 1804 | |
23976bdd NC |
1805 | if (HvAUX(hv)->xhv_name) { |
1806 | unshare_hek_or_pvn(HvAUX(hv)->xhv_name, 0, 0, 0); | |
1807 | } | |
1808 | } | |
1809 | ||
1810 | if (--attempts == 0) { | |
1811 | Perl_die(aTHX_ "panic: hfreeentries failed to free hash - something is repeatedly re-creating entries"); | |
1812 | } | |
6136c704 | 1813 | } |
23976bdd NC |
1814 | |
1815 | HvARRAY(hv) = orig_array; | |
1816 | ||
1817 | /* If the hash was actually a symbol table, put the name back. */ | |
1818 | if (name) { | |
1819 | /* We have restored the original array. If name is non-NULL, then | |
1820 | the original array had an aux structure at the end. So this is | |
1821 | valid: */ | |
1822 | SvFLAGS(hv) |= SVf_OOK; | |
1823 | HvAUX(hv)->xhv_name = name; | |
1b8791d1 | 1824 | } |
79072805 LW |
1825 | } |
1826 | ||
954c1994 GS |
1827 | /* |
1828 | =for apidoc hv_undef | |
1829 | ||
1830 | Undefines the hash. | |
1831 | ||
1832 | =cut | |
1833 | */ | |
1834 | ||
79072805 | 1835 | void |
864dbfa3 | 1836 | Perl_hv_undef(pTHX_ HV *hv) |
79072805 | 1837 | { |
97aff369 | 1838 | dVAR; |
cbec9347 | 1839 | register XPVHV* xhv; |
bfcb3514 | 1840 | const char *name; |
86f55936 | 1841 | |
79072805 LW |
1842 | if (!hv) |
1843 | return; | |
ecae49c0 | 1844 | DEBUG_A(Perl_hv_assert(aTHX_ hv)); |
cbec9347 | 1845 | xhv = (XPVHV*)SvANY(hv); |
dd69841b | 1846 | |
0fa56319 | 1847 | if ((name = HvNAME_get(hv)) && !PL_dirty) |
dd69841b BB |
1848 | mro_isa_changed_in(hv); |
1849 | ||
463ee0b2 | 1850 | hfreeentries(hv); |
dd69841b | 1851 | if (name) { |
7e8961ec | 1852 | if(PL_stashcache) |
7423f6db | 1853 | hv_delete(PL_stashcache, name, HvNAMELEN_get(hv), G_DISCARD); |
bd61b366 | 1854 | hv_name_set(hv, NULL, 0, 0); |
85e6fe83 | 1855 | } |
b79f7545 NC |
1856 | SvFLAGS(hv) &= ~SVf_OOK; |
1857 | Safefree(HvARRAY(hv)); | |
cbec9347 | 1858 | xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */ |
7b2c381c | 1859 | HvARRAY(hv) = 0; |
ca732855 | 1860 | HvPLACEHOLDERS_set(hv, 0); |
a0d0e21e LW |
1861 | |
1862 | if (SvRMAGICAL(hv)) | |
1c846c1f | 1863 | mg_clear((SV*)hv); |
79072805 LW |
1864 | } |
1865 | ||
b464bac0 | 1866 | static struct xpvhv_aux* |
5f66b61c | 1867 | S_hv_auxinit(HV *hv) { |
bfcb3514 | 1868 | struct xpvhv_aux *iter; |
b79f7545 | 1869 | char *array; |
bfcb3514 | 1870 | |
b79f7545 | 1871 | if (!HvARRAY(hv)) { |
a02a5408 | 1872 | Newxz(array, PERL_HV_ARRAY_ALLOC_BYTES(HvMAX(hv) + 1) |
b79f7545 NC |
1873 | + sizeof(struct xpvhv_aux), char); |
1874 | } else { | |
1875 | array = (char *) HvARRAY(hv); | |
1876 | Renew(array, PERL_HV_ARRAY_ALLOC_BYTES(HvMAX(hv) + 1) | |
1877 | + sizeof(struct xpvhv_aux), char); | |
1878 | } | |
1879 | HvARRAY(hv) = (HE**) array; | |
1880 | /* SvOOK_on(hv) attacks the IV flags. */ | |
1881 | SvFLAGS(hv) |= SVf_OOK; | |
1882 | iter = HvAUX(hv); | |
bfcb3514 NC |
1883 | |
1884 | iter->xhv_riter = -1; /* HvRITER(hv) = -1 */ | |
4608196e | 1885 | iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */ |
bfcb3514 | 1886 | iter->xhv_name = 0; |
86f55936 | 1887 | iter->xhv_backreferences = 0; |
e1a479c5 | 1888 | iter->xhv_mro_meta = NULL; |
bfcb3514 NC |
1889 | return iter; |
1890 | } | |
1891 | ||
954c1994 GS |
1892 | /* |
1893 | =for apidoc hv_iterinit | |
1894 | ||
1895 | Prepares a starting point to traverse a hash table. Returns the number of | |
1896 | keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is | |
1c846c1f | 1897 | currently only meaningful for hashes without tie magic. |
954c1994 GS |
1898 | |
1899 | NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of | |
1900 | hash buckets that happen to be in use. If you still need that esoteric | |
1901 | value, you can get it through the macro C<HvFILL(tb)>. | |
1902 | ||
e16e2ff8 | 1903 | |
954c1994 GS |
1904 | =cut |
1905 | */ | |
1906 | ||
79072805 | 1907 | I32 |
864dbfa3 | 1908 | Perl_hv_iterinit(pTHX_ HV *hv) |
79072805 | 1909 | { |
aa689395 | 1910 | if (!hv) |
cea2e8a9 | 1911 | Perl_croak(aTHX_ "Bad hash"); |
bfcb3514 | 1912 | |
b79f7545 | 1913 | if (SvOOK(hv)) { |
6136c704 | 1914 | struct xpvhv_aux * const iter = HvAUX(hv); |
0bd48802 | 1915 | HE * const entry = iter->xhv_eiter; /* HvEITER(hv) */ |
bfcb3514 NC |
1916 | if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */ |
1917 | HvLAZYDEL_off(hv); | |
1918 | hv_free_ent(hv, entry); | |
1919 | } | |
1920 | iter->xhv_riter = -1; /* HvRITER(hv) = -1 */ | |
4608196e | 1921 | iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */ |
bfcb3514 | 1922 | } else { |
6136c704 | 1923 | hv_auxinit(hv); |
72940dca | 1924 | } |
44a2ac75 | 1925 | |
cbec9347 | 1926 | /* used to be xhv->xhv_fill before 5.004_65 */ |
5d88ecd7 | 1927 | return HvTOTALKEYS(hv); |
79072805 | 1928 | } |
bfcb3514 NC |
1929 | |
1930 | I32 * | |
1931 | Perl_hv_riter_p(pTHX_ HV *hv) { | |
1932 | struct xpvhv_aux *iter; | |
1933 | ||
1934 | if (!hv) | |
1935 | Perl_croak(aTHX_ "Bad hash"); | |
1936 | ||
6136c704 | 1937 | iter = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv); |
bfcb3514 NC |
1938 | return &(iter->xhv_riter); |
1939 | } | |
1940 | ||
1941 | HE ** | |
1942 | Perl_hv_eiter_p(pTHX_ HV *hv) { | |
1943 | struct xpvhv_aux *iter; | |
1944 | ||
1945 | if (!hv) | |
1946 | Perl_croak(aTHX_ "Bad hash"); | |
1947 | ||
6136c704 | 1948 | iter = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv); |
bfcb3514 NC |
1949 | return &(iter->xhv_eiter); |
1950 | } | |
1951 | ||
1952 | void | |
1953 | Perl_hv_riter_set(pTHX_ HV *hv, I32 riter) { | |
1954 | struct xpvhv_aux *iter; | |
1955 | ||
1956 | if (!hv) | |
1957 | Perl_croak(aTHX_ "Bad hash"); | |
1958 | ||
b79f7545 NC |
1959 | if (SvOOK(hv)) { |
1960 | iter = HvAUX(hv); | |
1961 | } else { | |
bfcb3514 NC |
1962 | if (riter == -1) |
1963 | return; | |
1964 | ||
6136c704 | 1965 | iter = hv_auxinit(hv); |
bfcb3514 NC |
1966 | } |
1967 | iter->xhv_riter = riter; | |
1968 | } | |
1969 | ||
1970 | void | |
1971 | Perl_hv_eiter_set(pTHX_ HV *hv, HE *eiter) { | |
1972 | struct xpvhv_aux *iter; | |
1973 | ||
1974 | if (!hv) | |
1975 | Perl_croak(aTHX_ "Bad hash"); | |
1976 | ||
b79f7545 NC |
1977 | if (SvOOK(hv)) { |
1978 | iter = HvAUX(hv); | |
1979 | } else { | |
bfcb3514 NC |
1980 | /* 0 is the default so don't go malloc()ing a new structure just to |
1981 | hold 0. */ | |
1982 | if (!eiter) | |
1983 | return; | |
1984 | ||
6136c704 | 1985 | iter = hv_auxinit(hv); |
bfcb3514 NC |
1986 | } |
1987 | iter->xhv_eiter = eiter; | |
1988 | } | |
1989 | ||
bfcb3514 | 1990 | void |
4164be69 | 1991 | Perl_hv_name_set(pTHX_ HV *hv, const char *name, U32 len, U32 flags) |
bfcb3514 | 1992 | { |
97aff369 | 1993 | dVAR; |
b79f7545 | 1994 | struct xpvhv_aux *iter; |
7423f6db | 1995 | U32 hash; |
46c461b5 AL |
1996 | |
1997 | PERL_UNUSED_ARG(flags); | |
bfcb3514 | 1998 | |
4164be69 NC |
1999 | if (len > I32_MAX) |
2000 | Perl_croak(aTHX_ "panic: hv name too long (%"UVuf")", (UV) len); | |
2001 | ||
b79f7545 NC |
2002 | if (SvOOK(hv)) { |
2003 | iter = HvAUX(hv); | |
7423f6db NC |
2004 | if (iter->xhv_name) { |
2005 | unshare_hek_or_pvn(iter->xhv_name, 0, 0, 0); | |
2006 | } | |
16580ff5 | 2007 | } else { |
bfcb3514 NC |
2008 | if (name == 0) |
2009 | return; | |
2010 | ||
6136c704 | 2011 | iter = hv_auxinit(hv); |
bfcb3514 | 2012 | } |
7423f6db | 2013 | PERL_HASH(hash, name, len); |
adf4e37a | 2014 | iter->xhv_name = name ? share_hek(name, len, hash) : NULL; |
bfcb3514 NC |
2015 | } |
2016 | ||
86f55936 NC |
2017 | AV ** |
2018 | Perl_hv_backreferences_p(pTHX_ HV *hv) { | |
6136c704 | 2019 | struct xpvhv_aux * const iter = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv); |
96a5add6 | 2020 | PERL_UNUSED_CONTEXT; |
86f55936 NC |
2021 | return &(iter->xhv_backreferences); |
2022 | } | |
2023 | ||
2024 | void | |
2025 | Perl_hv_kill_backrefs(pTHX_ HV *hv) { | |
2026 | AV *av; | |
2027 | ||
2028 | if (!SvOOK(hv)) | |
2029 | return; | |
2030 | ||
2031 | av = HvAUX(hv)->xhv_backreferences; | |
2032 | ||
2033 | if (av) { | |
2034 | HvAUX(hv)->xhv_backreferences = 0; | |
2035 | Perl_sv_kill_backrefs(aTHX_ (SV*) hv, av); | |
2036 | } | |
2037 | } | |
2038 | ||
954c1994 | 2039 | /* |
7a7b9979 NC |
2040 | hv_iternext is implemented as a macro in hv.h |
2041 | ||
954c1994 GS |
2042 | =for apidoc hv_iternext |
2043 | ||
2044 | Returns entries from a hash iterator. See C<hv_iterinit>. | |
2045 | ||
fe7bca90 NC |
2046 | You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the |
2047 | iterator currently points to, without losing your place or invalidating your | |
2048 | iterator. Note that in this case the current entry is deleted from the hash | |
2049 | with your iterator holding the last reference to it. Your iterator is flagged | |
2050 | to free the entry on the next call to C<hv_iternext>, so you must not discard | |
2051 | your iterator immediately else the entry will leak - call C<hv_iternext> to | |
2052 | trigger the resource deallocation. | |
2053 | ||
fe7bca90 NC |
2054 | =for apidoc hv_iternext_flags |
2055 | ||
2056 | Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>. | |
2057 | The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is | |
2058 | set the placeholders keys (for restricted hashes) will be returned in addition | |
2059 | to normal keys. By default placeholders are automatically skipped over. | |
7996736c MHM |
2060 | Currently a placeholder is implemented with a value that is |
2061 | C<&Perl_sv_placeholder>. Note that the implementation of placeholders and | |
fe7bca90 NC |
2062 | restricted hashes may change, and the implementation currently is |
2063 | insufficiently abstracted for any change to be tidy. | |
e16e2ff8 | 2064 | |
fe7bca90 | 2065 | =cut |
e16e2ff8 NC |
2066 | */ |
2067 | ||
2068 | HE * | |
2069 | Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags) | |
2070 | { | |
27da23d5 | 2071 | dVAR; |
cbec9347 | 2072 | register XPVHV* xhv; |
79072805 | 2073 | register HE *entry; |
a0d0e21e | 2074 | HE *oldentry; |
463ee0b2 | 2075 | MAGIC* mg; |
bfcb3514 | 2076 | struct xpvhv_aux *iter; |
79072805 LW |
2077 | |
2078 | if (!hv) | |
cea2e8a9 | 2079 | Perl_croak(aTHX_ "Bad hash"); |
81714fb9 | 2080 | |
cbec9347 | 2081 | xhv = (XPVHV*)SvANY(hv); |
bfcb3514 | 2082 | |
b79f7545 | 2083 | if (!SvOOK(hv)) { |
bfcb3514 NC |
2084 | /* Too many things (well, pp_each at least) merrily assume that you can |
2085 | call iv_iternext without calling hv_iterinit, so we'll have to deal | |
2086 | with it. */ | |
2087 | hv_iterinit(hv); | |
bfcb3514 | 2088 | } |
b79f7545 | 2089 | iter = HvAUX(hv); |
bfcb3514 NC |
2090 | |
2091 | oldentry = entry = iter->xhv_eiter; /* HvEITER(hv) */ | |
e62cc96a | 2092 | if (SvMAGICAL(hv) && SvRMAGICAL(hv)) { |
44a2ac75 | 2093 | if ( ( mg = mg_find((SV*)hv, PERL_MAGIC_tied) ) ) { |
e62cc96a YO |
2094 | SV * const key = sv_newmortal(); |
2095 | if (entry) { | |
2096 | sv_setsv(key, HeSVKEY_force(entry)); | |
2097 | SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */ | |
2098 | } | |
2099 | else { | |
2100 | char *k; | |
2101 | HEK *hek; | |
2102 | ||
2103 | /* one HE per MAGICAL hash */ | |
2104 | iter->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */ | |
2105 | Zero(entry, 1, HE); | |
2106 | Newxz(k, HEK_BASESIZE + sizeof(SV*), char); | |
2107 | hek = (HEK*)k; | |
2108 | HeKEY_hek(entry) = hek; | |
2109 | HeKLEN(entry) = HEf_SVKEY; | |
2110 | } | |
2111 | magic_nextpack((SV*) hv,mg,key); | |
2112 | if (SvOK(key)) { | |
2113 | /* force key to stay around until next time */ | |
2114 | HeSVKEY_set(entry, SvREFCNT_inc_simple_NN(key)); | |
2115 | return entry; /* beware, hent_val is not set */ | |
2116 | } | |
2117 | if (HeVAL(entry)) | |
2118 | SvREFCNT_dec(HeVAL(entry)); | |
2119 | Safefree(HeKEY_hek(entry)); | |
2120 | del_HE(entry); | |
2121 | iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */ | |
2122 | return NULL; | |
81714fb9 | 2123 | } |
79072805 | 2124 | } |
7ee146b1 | 2125 | #if defined(DYNAMIC_ENV_FETCH) && !defined(__riscos__) /* set up %ENV for iteration */ |
03026e68 | 2126 | if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) { |
f675dbe5 | 2127 | prime_env_iter(); |
03026e68 JM |
2128 | #ifdef VMS |
2129 | /* The prime_env_iter() on VMS just loaded up new hash values | |
2130 | * so the iteration count needs to be reset back to the beginning | |
2131 | */ | |
2132 | hv_iterinit(hv); | |
2133 | iter = HvAUX(hv); | |
2134 | oldentry = entry = iter->xhv_eiter; /* HvEITER(hv) */ | |
2135 | #endif | |
2136 | } | |
f675dbe5 | 2137 | #endif |
463ee0b2 | 2138 | |
b79f7545 NC |
2139 | /* hv_iterint now ensures this. */ |
2140 | assert (HvARRAY(hv)); | |
2141 | ||
015a5f36 | 2142 | /* At start of hash, entry is NULL. */ |
fde52b5c | 2143 | if (entry) |
8aacddc1 | 2144 | { |
fde52b5c | 2145 | entry = HeNEXT(entry); |
e16e2ff8 NC |
2146 | if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) { |
2147 | /* | |
2148 | * Skip past any placeholders -- don't want to include them in | |
2149 | * any iteration. | |
2150 | */ | |
7996736c | 2151 | while (entry && HeVAL(entry) == &PL_sv_placeholder) { |
e16e2ff8 NC |
2152 | entry = HeNEXT(entry); |
2153 | } | |
8aacddc1 NIS |
2154 | } |
2155 | } | |
fde52b5c | 2156 | while (!entry) { |
015a5f36 NC |
2157 | /* OK. Come to the end of the current list. Grab the next one. */ |
2158 | ||
bfcb3514 NC |
2159 | iter->xhv_riter++; /* HvRITER(hv)++ */ |
2160 | if (iter->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) { | |
015a5f36 | 2161 | /* There is no next one. End of the hash. */ |
bfcb3514 | 2162 | iter->xhv_riter = -1; /* HvRITER(hv) = -1 */ |
fde52b5c | 2163 | break; |
79072805 | 2164 | } |
7b2c381c | 2165 | entry = (HvARRAY(hv))[iter->xhv_riter]; |
8aacddc1 | 2166 | |
e16e2ff8 | 2167 | if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) { |
015a5f36 NC |
2168 | /* If we have an entry, but it's a placeholder, don't count it. |
2169 | Try the next. */ | |
7996736c | 2170 | while (entry && HeVAL(entry) == &PL_sv_placeholder) |
015a5f36 NC |
2171 | entry = HeNEXT(entry); |
2172 | } | |
2173 | /* Will loop again if this linked list starts NULL | |
2174 | (for HV_ITERNEXT_WANTPLACEHOLDERS) | |
2175 | or if we run through it and find only placeholders. */ | |
fde52b5c | 2176 | } |
79072805 | 2177 | |
72940dca | 2178 | if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */ |
2179 | HvLAZYDEL_off(hv); | |
68dc0745 | 2180 | hv_free_ent(hv, oldentry); |
72940dca | 2181 | } |
a0d0e21e | 2182 | |
fdcd69b6 | 2183 | /*if (HvREHASH(hv) && entry && !HeKREHASH(entry)) |
6c9570dc | 2184 | PerlIO_printf(PerlIO_stderr(), "Awooga %p %p\n", (void*)hv, (void*)entry);*/ |
fdcd69b6 | 2185 | |
bfcb3514 | 2186 | iter->xhv_eiter = entry; /* HvEITER(hv) = entry */ |
79072805 LW |
2187 | return entry; |
2188 | } | |
2189 | ||
954c1994 GS |
2190 | /* |
2191 | =for apidoc hv_iterkey | |
2192 | ||
2193 | Returns the key from the current position of the hash iterator. See | |
2194 | C<hv_iterinit>. | |
2195 | ||
2196 | =cut | |
2197 | */ | |
2198 | ||
79072805 | 2199 | char * |
864dbfa3 | 2200 | Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen) |
79072805 | 2201 | { |
fde52b5c | 2202 | if (HeKLEN(entry) == HEf_SVKEY) { |
fb73857a | 2203 | STRLEN len; |
0bd48802 | 2204 | char * const p = SvPV(HeKEY_sv(entry), len); |
fb73857a | 2205 | *retlen = len; |
2206 | return p; | |
fde52b5c | 2207 | } |
2208 | else { | |
2209 | *retlen = HeKLEN(entry); | |
2210 | return HeKEY(entry); | |
2211 | } | |
2212 | } | |
2213 | ||
2214 | /* unlike hv_iterval(), this always returns a mortal copy of the key */ | |
954c1994 GS |
2215 | /* |
2216 | =for apidoc hv_iterkeysv | |
2217 | ||
2218 | Returns the key as an C<SV*> from the current position of the hash | |
2219 | iterator. The return value will always be a mortal copy of the key. Also | |
2220 | see C<hv_iterinit>. | |
2221 | ||
2222 | =cut | |
2223 | */ | |
2224 | ||
fde52b5c | 2225 | SV * |
864dbfa3 | 2226 | Perl_hv_iterkeysv(pTHX_ register HE *entry) |
fde52b5c | 2227 | { |
c1b02ed8 | 2228 | return sv_2mortal(newSVhek(HeKEY_hek(entry))); |
79072805 LW |
2229 | } |
2230 | ||
954c1994 GS |
2231 | /* |
2232 | =for apidoc hv_iterval | |
2233 | ||
2234 | Returns the value from the current position of the hash iterator. See | |
2235 | C<hv_iterkey>. | |
2236 | ||
2237 | =cut | |
2238 | */ | |
2239 | ||
79072805 | 2240 | SV * |
864dbfa3 | 2241 | Perl_hv_iterval(pTHX_ HV *hv, register HE *entry) |
79072805 | 2242 | { |
8990e307 | 2243 | if (SvRMAGICAL(hv)) { |
14befaf4 | 2244 | if (mg_find((SV*)hv, PERL_MAGIC_tied)) { |
c4420975 | 2245 | SV* const sv = sv_newmortal(); |
bbce6d69 | 2246 | if (HeKLEN(entry) == HEf_SVKEY) |
2247 | mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY); | |
a3b680e6 AL |
2248 | else |
2249 | mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry)); | |
463ee0b2 LW |
2250 | return sv; |
2251 | } | |
79072805 | 2252 | } |
fde52b5c | 2253 | return HeVAL(entry); |
79072805 LW |
2254 | } |
2255 | ||
954c1994 GS |
2256 | /* |
2257 | =for apidoc hv_iternextsv | |
2258 | ||
2259 | Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one | |
2260 | operation. | |
2261 | ||
2262 | =cut | |
2263 | */ | |
2264 | ||
a0d0e21e | 2265 | SV * |
864dbfa3 | 2266 | Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen) |
a0d0e21e | 2267 | { |
0bd48802 AL |
2268 | HE * const he = hv_iternext_flags(hv, 0); |
2269 | ||
2270 | if (!he) | |
a0d0e21e LW |
2271 | return NULL; |
2272 | *key = hv_iterkey(he, retlen); | |
2273 | return hv_iterval(hv, he); | |
2274 | } | |
2275 | ||
954c1994 | 2276 | /* |
bc5cdc23 NC |
2277 | |
2278 | Now a macro in hv.h | |
2279 | ||
954c1994 GS |
2280 | =for apidoc hv_magic |
2281 | ||
2282 | Adds magic to a hash. See C<sv_magic>. | |
2283 | ||
2284 | =cut | |
2285 | */ | |
2286 | ||
bbce6d69 | 2287 | /* possibly free a shared string if no one has access to it |
fde52b5c | 2288 | * len and hash must both be valid for str. |
2289 | */ | |
bbce6d69 | 2290 | void |
864dbfa3 | 2291 | Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash) |
fde52b5c | 2292 | { |
19692e8d NC |
2293 | unshare_hek_or_pvn (NULL, str, len, hash); |
2294 | } | |
2295 | ||
2296 | ||
2297 | void | |
2298 | Perl_unshare_hek(pTHX_ HEK *hek) | |
2299 | { | |
bf11fd37 | 2300 | assert(hek); |
19692e8d NC |
2301 | unshare_hek_or_pvn(hek, NULL, 0, 0); |
2302 | } | |
2303 | ||
2304 | /* possibly free a shared string if no one has access to it | |
2305 | hek if non-NULL takes priority over the other 3, else str, len and hash | |
2306 | are used. If so, len and hash must both be valid for str. | |
2307 | */ | |
df132699 | 2308 | STATIC void |
97ddebaf | 2309 | S_unshare_hek_or_pvn(pTHX_ const HEK *hek, const char *str, I32 len, U32 hash) |
19692e8d | 2310 | { |
97aff369 | 2311 | dVAR; |
cbec9347 | 2312 | register XPVHV* xhv; |
20454177 | 2313 | HE *entry; |
fde52b5c | 2314 | register HE **oentry; |
45d1cc86 | 2315 | HE **first; |
c3654f1a | 2316 | bool is_utf8 = FALSE; |
19692e8d | 2317 | int k_flags = 0; |
aec46f14 | 2318 | const char * const save = str; |
cbbf8932 | 2319 | struct shared_he *he = NULL; |
c3654f1a | 2320 | |
19692e8d | 2321 | if (hek) { |
cbae3960 NC |
2322 | /* Find the shared he which is just before us in memory. */ |
2323 | he = (struct shared_he *)(((char *)hek) | |
2324 | - STRUCT_OFFSET(struct shared_he, | |
2325 | shared_he_hek)); | |
2326 | ||
2327 | /* Assert that the caller passed us a genuine (or at least consistent) | |
2328 | shared hek */ | |
2329 | assert (he->shared_he_he.hent_hek == hek); | |
29404ae0 NC |
2330 | |
2331 | LOCK_STRTAB_MUTEX; | |
de616631 NC |
2332 | if (he->shared_he_he.he_valu.hent_refcount - 1) { |
2333 | --he->shared_he_he.he_valu.hent_refcount; | |
29404ae0 NC |
2334 | UNLOCK_STRTAB_MUTEX; |
2335 | return; | |
2336 | } | |
2337 | UNLOCK_STRTAB_MUTEX; | |
2338 | ||
19692e8d NC |
2339 | hash = HEK_HASH(hek); |
2340 | } else if (len < 0) { | |
2341 | STRLEN tmplen = -len; | |
2342 | is_utf8 = TRUE; | |
2343 | /* See the note in hv_fetch(). --jhi */ | |
2344 | str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8); | |
2345 | len = tmplen; | |
2346 | if (is_utf8) | |
2347 | k_flags = HVhek_UTF8; | |
2348 | if (str != save) | |
2349 | k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY; | |
c3654f1a | 2350 | } |
1c846c1f | 2351 | |
de616631 | 2352 | /* what follows was the moral equivalent of: |
6b88bc9c | 2353 | if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) { |
a0714e2c | 2354 | if (--*Svp == NULL) |
6b88bc9c | 2355 | hv_delete(PL_strtab, str, len, G_DISCARD, hash); |
bbce6d69 | 2356 | } */ |
cbec9347 | 2357 | xhv = (XPVHV*)SvANY(PL_strtab); |
fde52b5c | 2358 | /* assert(xhv_array != 0) */ |
5f08fbcd | 2359 | LOCK_STRTAB_MUTEX; |
45d1cc86 | 2360 | first = oentry = &(HvARRAY(PL_strtab))[hash & (I32) HvMAX(PL_strtab)]; |
6c1b96a1 NC |
2361 | if (he) { |
2362 | const HE *const he_he = &(he->shared_he_he); | |
45d1cc86 | 2363 | for (entry = *oentry; entry; oentry = &HeNEXT(entry), entry = *oentry) { |
35ab5632 NC |
2364 | if (entry == he_he) |
2365 | break; | |
19692e8d NC |
2366 | } |
2367 | } else { | |
35a4481c | 2368 | const int flags_masked = k_flags & HVhek_MASK; |
45d1cc86 | 2369 | for (entry = *oentry; entry; oentry = &HeNEXT(entry), entry = *oentry) { |
19692e8d NC |
2370 | if (HeHASH(entry) != hash) /* strings can't be equal */ |
2371 | continue; | |
2372 | if (HeKLEN(entry) != len) | |
2373 | continue; | |
2374 | if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */ | |
2375 | continue; | |
2376 | if (HeKFLAGS(entry) != flags_masked) | |
2377 | continue; | |
19692e8d NC |
2378 | break; |
2379 | } | |
2380 | } | |
2381 | ||
35ab5632 NC |
2382 | if (entry) { |
2383 | if (--entry->he_valu.hent_refcount == 0) { | |
19692e8d | 2384 | *oentry = HeNEXT(entry); |
45d1cc86 NC |
2385 | if (!*first) { |
2386 | /* There are now no entries in our slot. */ | |
19692e8d | 2387 | xhv->xhv_fill--; /* HvFILL(hv)-- */ |
45d1cc86 | 2388 | } |
cbae3960 | 2389 | Safefree(entry); |
4c7185a0 | 2390 | xhv->xhv_keys--; /* HvTOTALKEYS(hv)-- */ |
19692e8d | 2391 | } |
fde52b5c | 2392 | } |
19692e8d | 2393 | |
333f433b | 2394 | UNLOCK_STRTAB_MUTEX; |
35ab5632 | 2395 | if (!entry && ckWARN_d(WARN_INTERNAL)) |
19692e8d | 2396 | Perl_warner(aTHX_ packWARN(WARN_INTERNAL), |
472d47bc SB |
2397 | "Attempt to free non-existent shared string '%s'%s" |
2398 | pTHX__FORMAT, | |
19692e8d | 2399 | hek ? HEK_KEY(hek) : str, |
472d47bc | 2400 | ((k_flags & HVhek_UTF8) ? " (utf8)" : "") pTHX__VALUE); |
19692e8d NC |
2401 | if (k_flags & HVhek_FREEKEY) |
2402 | Safefree(str); | |
fde52b5c | 2403 | } |
2404 | ||
bbce6d69 | 2405 | /* get a (constant) string ptr from the global string table |
2406 | * string will get added if it is not already there. | |
fde52b5c | 2407 | * len and hash must both be valid for str. |
2408 | */ | |
bbce6d69 | 2409 | HEK * |
864dbfa3 | 2410 | Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash) |
fde52b5c | 2411 | { |
da58a35d | 2412 | bool is_utf8 = FALSE; |
19692e8d | 2413 | int flags = 0; |
aec46f14 | 2414 | const char * const save = str; |
da58a35d JH |
2415 | |
2416 | if (len < 0) { | |
77caf834 | 2417 | STRLEN tmplen = -len; |
da58a35d | 2418 | is_utf8 = TRUE; |
77caf834 JH |
2419 | /* See the note in hv_fetch(). --jhi */ |
2420 | str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8); | |
2421 | len = tmplen; | |
19692e8d NC |
2422 | /* If we were able to downgrade here, then than means that we were passed |
2423 | in a key which only had chars 0-255, but was utf8 encoded. */ | |
2424 | if (is_utf8) | |
2425 | flags = HVhek_UTF8; | |
2426 | /* If we found we were able to downgrade the string to bytes, then | |
2427 | we should flag that it needs upgrading on keys or each. Also flag | |
2428 | that we need share_hek_flags to free the string. */ | |
2429 | if (str != save) | |
2430 | flags |= HVhek_WASUTF8 | HVhek_FREEKEY; | |
2431 | } | |
2432 | ||
6e838c70 | 2433 | return share_hek_flags (str, len, hash, flags); |
19692e8d NC |
2434 | } |
2435 | ||
6e838c70 | 2436 | STATIC HEK * |
19692e8d NC |
2437 | S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags) |
2438 | { | |
97aff369 | 2439 | dVAR; |
19692e8d | 2440 | register HE *entry; |
35a4481c | 2441 | const int flags_masked = flags & HVhek_MASK; |
263cb4a6 | 2442 | const U32 hindex = hash & (I32) HvMAX(PL_strtab); |
bbce6d69 | 2443 | |
fde52b5c | 2444 | /* what follows is the moral equivalent of: |
1c846c1f | 2445 | |
6b88bc9c | 2446 | if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE))) |
a0714e2c | 2447 | hv_store(PL_strtab, str, len, NULL, hash); |
fdcd69b6 NC |
2448 | |
2449 | Can't rehash the shared string table, so not sure if it's worth | |
2450 | counting the number of entries in the linked list | |
bbce6d69 | 2451 | */ |
1b6737cc | 2452 | register XPVHV * const xhv = (XPVHV*)SvANY(PL_strtab); |
fde52b5c | 2453 | /* assert(xhv_array != 0) */ |
5f08fbcd | 2454 | LOCK_STRTAB_MUTEX; |
263cb4a6 NC |
2455 | entry = (HvARRAY(PL_strtab))[hindex]; |
2456 | for (;entry; entry = HeNEXT(entry)) { | |
fde52b5c | 2457 | if (HeHASH(entry) != hash) /* strings can't be equal */ |
2458 | continue; | |
2459 | if (HeKLEN(entry) != len) | |
2460 | continue; | |
1c846c1f | 2461 | if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */ |
fde52b5c | 2462 | continue; |
19692e8d | 2463 | if (HeKFLAGS(entry) != flags_masked) |
c3654f1a | 2464 | continue; |
fde52b5c | 2465 | break; |
2466 | } | |
263cb4a6 NC |
2467 | |
2468 | if (!entry) { | |
45d1cc86 NC |
2469 | /* What used to be head of the list. |
2470 | If this is NULL, then we're the first entry for this slot, which | |
2471 | means we need to increate fill. */ | |
cbae3960 NC |
2472 | struct shared_he *new_entry; |
2473 | HEK *hek; | |
2474 | char *k; | |
263cb4a6 NC |
2475 | HE **const head = &HvARRAY(PL_strtab)[hindex]; |
2476 | HE *const next = *head; | |
cbae3960 NC |
2477 | |
2478 | /* We don't actually store a HE from the arena and a regular HEK. | |
2479 | Instead we allocate one chunk of memory big enough for both, | |
2480 | and put the HEK straight after the HE. This way we can find the | |
2481 | HEK directly from the HE. | |
2482 | */ | |
2483 | ||
a02a5408 | 2484 | Newx(k, STRUCT_OFFSET(struct shared_he, |
cbae3960 NC |
2485 | shared_he_hek.hek_key[0]) + len + 2, char); |
2486 | new_entry = (struct shared_he *)k; | |
2487 | entry = &(new_entry->shared_he_he); | |
2488 | hek = &(new_entry->shared_he_hek); | |
2489 | ||
2490 | Copy(str, HEK_KEY(hek), len, char); | |
2491 | HEK_KEY(hek)[len] = 0; | |
2492 | HEK_LEN(hek) = len; | |
2493 | HEK_HASH(hek) = hash; | |
2494 | HEK_FLAGS(hek) = (unsigned char)flags_masked; | |
2495 | ||
2496 | /* Still "point" to the HEK, so that other code need not know what | |
2497 | we're up to. */ | |
2498 | HeKEY_hek(entry) = hek; | |
de616631 | 2499 | entry->he_valu.hent_refcount = 0; |
263cb4a6 NC |
2500 | HeNEXT(entry) = next; |
2501 | *head = entry; | |
cbae3960 | 2502 | |
4c7185a0 | 2503 | xhv->xhv_keys++; /* HvTOTALKEYS(hv)++ */ |
263cb4a6 | 2504 | if (!next) { /* initial entry? */ |
cbec9347 | 2505 | xhv->xhv_fill++; /* HvFILL(hv)++ */ |
4c9cc595 | 2506 | } else if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) { |
cbec9347 | 2507 | hsplit(PL_strtab); |
bbce6d69 | 2508 | } |
2509 | } | |
2510 | ||
de616631 | 2511 | ++entry->he_valu.hent_refcount; |
5f08fbcd | 2512 | UNLOCK_STRTAB_MUTEX; |
19692e8d NC |
2513 | |
2514 | if (flags & HVhek_FREEKEY) | |
f9a63242 | 2515 | Safefree(str); |
19692e8d | 2516 | |
6e838c70 | 2517 | return HeKEY_hek(entry); |
fde52b5c | 2518 | } |
ecae49c0 | 2519 | |
1e73acc8 AS |
2520 | STATIC SV * |
2521 | S_hv_magic_uvar_xkey(pTHX_ HV* hv, SV* keysv, int action) | |
2522 | { | |
2523 | MAGIC* mg; | |
2524 | if ((mg = mg_find((SV*)hv, PERL_MAGIC_uvar))) { | |
2525 | struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr; | |
2526 | if (uf->uf_set == NULL) { | |
2527 | SV* obj = mg->mg_obj; | |
2528 | mg->mg_obj = keysv; /* pass key */ | |
2529 | uf->uf_index = action; /* pass action */ | |
2530 | magic_getuvar((SV*)hv, mg); | |
2531 | keysv = mg->mg_obj; /* may have changed */ | |
2532 | mg->mg_obj = obj; | |
2533 | } | |
2534 | } | |
2535 | return keysv; | |
2536 | } | |
2537 | ||
ca732855 NC |
2538 | I32 * |
2539 | Perl_hv_placeholders_p(pTHX_ HV *hv) | |
2540 | { | |
2541 | dVAR; | |
2542 | MAGIC *mg = mg_find((SV*)hv, PERL_MAGIC_rhash); | |
2543 | ||
2544 | if (!mg) { | |
2545 | mg = sv_magicext((SV*)hv, 0, PERL_MAGIC_rhash, 0, 0, 0); | |
2546 | ||
2547 | if (!mg) { | |
2548 | Perl_die(aTHX_ "panic: hv_placeholders_p"); | |
2549 | } | |
2550 | } | |
2551 | return &(mg->mg_len); | |
2552 | } | |
2553 | ||
2554 | ||
2555 | I32 | |
2556 | Perl_hv_placeholders_get(pTHX_ HV *hv) | |
2557 | { | |
2558 | dVAR; | |
b464bac0 | 2559 | MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_rhash); |
ca732855 NC |
2560 | |
2561 | return mg ? mg->mg_len : 0; | |
2562 | } | |
2563 | ||
2564 | void | |
ac1e784a | 2565 | Perl_hv_placeholders_set(pTHX_ HV *hv, I32 ph) |
ca732855 NC |
2566 | { |
2567 | dVAR; | |
b464bac0 | 2568 | MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_rhash); |
ca732855 NC |
2569 | |
2570 | if (mg) { | |
2571 | mg->mg_len = ph; | |
2572 | } else if (ph) { | |
2573 | if (!sv_magicext((SV*)hv, 0, PERL_MAGIC_rhash, 0, 0, ph)) | |
2574 | Perl_die(aTHX_ "panic: hv_placeholders_set"); | |
2575 | } | |
2576 | /* else we don't need to add magic to record 0 placeholders. */ | |
2577 | } | |
ecae49c0 | 2578 | |
2a49f0f5 | 2579 | STATIC SV * |
7b0bddfa NC |
2580 | S_refcounted_he_value(pTHX_ const struct refcounted_he *he) |
2581 | { | |
0b2d3faa | 2582 | dVAR; |
7b0bddfa NC |
2583 | SV *value; |
2584 | switch(he->refcounted_he_data[0] & HVrhek_typemask) { | |
2585 | case HVrhek_undef: | |
2586 | value = newSV(0); | |
2587 | break; | |
2588 | case HVrhek_delete: | |
2589 | value = &PL_sv_placeholder; | |
2590 | break; | |
2591 | case HVrhek_IV: | |
44ebaf21 NC |
2592 | value = newSViv(he->refcounted_he_val.refcounted_he_u_iv); |
2593 | break; | |
2594 | case HVrhek_UV: | |
2595 | value = newSVuv(he->refcounted_he_val.refcounted_he_u_uv); | |
7b0bddfa NC |
2596 | break; |
2597 | case HVrhek_PV: | |
44ebaf21 | 2598 | case HVrhek_PV_UTF8: |
7b0bddfa NC |
2599 | /* Create a string SV that directly points to the bytes in our |
2600 | structure. */ | |
b9f83d2f | 2601 | value = newSV_type(SVt_PV); |
7b0bddfa NC |
2602 | SvPV_set(value, (char *) he->refcounted_he_data + 1); |
2603 | SvCUR_set(value, he->refcounted_he_val.refcounted_he_u_len); | |
2604 | /* This stops anything trying to free it */ | |
2605 | SvLEN_set(value, 0); | |
2606 | SvPOK_on(value); | |
2607 | SvREADONLY_on(value); | |
44ebaf21 | 2608 | if ((he->refcounted_he_data[0] & HVrhek_typemask) == HVrhek_PV_UTF8) |
7b0bddfa NC |
2609 | SvUTF8_on(value); |
2610 | break; | |
2611 | default: | |
2612 | Perl_croak(aTHX_ "panic: refcounted_he_value bad flags %x", | |
2613 | he->refcounted_he_data[0]); | |
2614 | } | |
2615 | return value; | |
2616 | } | |
2617 | ||
ecae49c0 | 2618 | /* |
b3ca2e83 NC |
2619 | =for apidoc refcounted_he_chain_2hv |
2620 | ||
abc25d8c | 2621 | Generates and returns a C<HV *> by walking up the tree starting at the passed |
b3ca2e83 NC |
2622 | in C<struct refcounted_he *>. |
2623 | ||
2624 | =cut | |
2625 | */ | |
2626 | HV * | |
2627 | Perl_refcounted_he_chain_2hv(pTHX_ const struct refcounted_he *chain) | |
2628 | { | |
7a89be66 | 2629 | dVAR; |
b3ca2e83 NC |
2630 | HV *hv = newHV(); |
2631 | U32 placeholders = 0; | |
2632 | /* We could chase the chain once to get an idea of the number of keys, | |
2633 | and call ksplit. But for now we'll make a potentially inefficient | |
2634 | hash with only 8 entries in its array. */ | |
2635 | const U32 max = HvMAX(hv); | |
2636 | ||
2637 | if (!HvARRAY(hv)) { | |
2638 | char *array; | |
2639 | Newxz(array, PERL_HV_ARRAY_ALLOC_BYTES(max + 1), char); | |
2640 | HvARRAY(hv) = (HE**)array; | |
2641 | } | |
2642 | ||
2643 | while (chain) { | |
cbb1fbea | 2644 | #ifdef USE_ITHREADS |
b6bbf3fa | 2645 | U32 hash = chain->refcounted_he_hash; |
cbb1fbea NC |
2646 | #else |
2647 | U32 hash = HEK_HASH(chain->refcounted_he_hek); | |
2648 | #endif | |
b3ca2e83 NC |
2649 | HE **oentry = &((HvARRAY(hv))[hash & max]); |
2650 | HE *entry = *oentry; | |
b6bbf3fa | 2651 | SV *value; |
cbb1fbea | 2652 | |
b3ca2e83 NC |
2653 | for (; entry; entry = HeNEXT(entry)) { |
2654 | if (HeHASH(entry) == hash) { | |
9f769845 NC |
2655 | /* We might have a duplicate key here. If so, entry is older |
2656 | than the key we've already put in the hash, so if they are | |
2657 | the same, skip adding entry. */ | |
2658 | #ifdef USE_ITHREADS | |
2659 | const STRLEN klen = HeKLEN(entry); | |
2660 | const char *const key = HeKEY(entry); | |
2661 | if (klen == chain->refcounted_he_keylen | |
2662 | && (!!HeKUTF8(entry) | |
2663 | == !!(chain->refcounted_he_data[0] & HVhek_UTF8)) | |
2664 | && memEQ(key, REF_HE_KEY(chain), klen)) | |
2665 | goto next_please; | |
2666 | #else | |
2667 | if (HeKEY_hek(entry) == chain->refcounted_he_hek) | |
2668 | goto next_please; | |
2669 | if (HeKLEN(entry) == HEK_LEN(chain->refcounted_he_hek) | |
2670 | && HeKUTF8(entry) == HEK_UTF8(chain->refcounted_he_hek) | |
2671 | && memEQ(HeKEY(entry), HEK_KEY(chain->refcounted_he_hek), | |
2672 | HeKLEN(entry))) | |
2673 | goto next_please; | |
2674 | #endif | |
b3ca2e83 NC |
2675 | } |
2676 | } | |
2677 | assert (!entry); | |
2678 | entry = new_HE(); | |
2679 | ||
cbb1fbea NC |
2680 | #ifdef USE_ITHREADS |
2681 | HeKEY_hek(entry) | |
7b0bddfa | 2682 | = share_hek_flags(REF_HE_KEY(chain), |
b6bbf3fa NC |
2683 | chain->refcounted_he_keylen, |
2684 | chain->refcounted_he_hash, | |
2685 | (chain->refcounted_he_data[0] | |
2686 | & (HVhek_UTF8|HVhek_WASUTF8))); | |
cbb1fbea | 2687 | #else |
71ad1b0c | 2688 | HeKEY_hek(entry) = share_hek_hek(chain->refcounted_he_hek); |
cbb1fbea | 2689 | #endif |
7b0bddfa NC |
2690 | value = refcounted_he_value(chain); |
2691 | if (value == &PL_sv_placeholder) | |
b3ca2e83 | 2692 | placeholders++; |
b6bbf3fa | 2693 | HeVAL(entry) = value; |
b3ca2e83 NC |
2694 | |
2695 | /* Link it into the chain. */ | |
2696 | HeNEXT(entry) = *oentry; | |
2697 | if (!HeNEXT(entry)) { | |
2698 | /* initial entry. */ | |
2699 | HvFILL(hv)++; | |
2700 | } | |
2701 | *oentry = entry; | |
2702 | ||
2703 | HvTOTALKEYS(hv)++; | |
2704 | ||
2705 | next_please: | |
71ad1b0c | 2706 | chain = chain->refcounted_he_next; |
b3ca2e83 NC |
2707 | } |
2708 | ||
2709 | if (placeholders) { | |
2710 | clear_placeholders(hv, placeholders); | |
2711 | HvTOTALKEYS(hv) -= placeholders; | |
2712 | } | |
2713 | ||
2714 | /* We could check in the loop to see if we encounter any keys with key | |
2715 | flags, but it's probably not worth it, as this per-hash flag is only | |
2716 | really meant as an optimisation for things like Storable. */ | |
2717 | HvHASKFLAGS_on(hv); | |
def9038f | 2718 | DEBUG_A(Perl_hv_assert(aTHX_ hv)); |
b3ca2e83 NC |
2719 | |
2720 | return hv; | |
2721 | } | |
2722 | ||
7b0bddfa NC |
2723 | SV * |
2724 | Perl_refcounted_he_fetch(pTHX_ const struct refcounted_he *chain, SV *keysv, | |
2725 | const char *key, STRLEN klen, int flags, U32 hash) | |
2726 | { | |
0b2d3faa | 2727 | dVAR; |
7b0bddfa NC |
2728 | /* Just to be awkward, if you're using this interface the UTF-8-or-not-ness |
2729 | of your key has to exactly match that which is stored. */ | |
2730 | SV *value = &PL_sv_placeholder; | |
d8c5b3c5 | 2731 | bool is_utf8; |
7b0bddfa NC |
2732 | |
2733 | if (keysv) { | |
2734 | if (flags & HVhek_FREEKEY) | |
2735 | Safefree(key); | |
2736 | key = SvPV_const(keysv, klen); | |
2737 | flags = 0; | |
d8c5b3c5 NC |
2738 | is_utf8 = (SvUTF8(keysv) != 0); |
2739 | } else { | |
2740 | is_utf8 = ((flags & HVhek_UTF8) ? TRUE : FALSE); | |
7b0bddfa NC |
2741 | } |
2742 | ||
2743 | if (!hash) { | |
2744 | if (keysv && (SvIsCOW_shared_hash(keysv))) { | |
2745 | hash = SvSHARED_HASH(keysv); | |
2746 | } else { | |
2747 | PERL_HASH(hash, key, klen); | |
2748 | } | |
2749 | } | |
2750 | ||
2751 | for (; chain; chain = chain->refcounted_he_next) { | |
2752 | #ifdef USE_ITHREADS | |
2753 | if (hash != chain->refcounted_he_hash) | |
2754 | continue; | |
2755 | if (klen != chain->refcounted_he_keylen) | |
2756 | continue; | |
2757 | if (memNE(REF_HE_KEY(chain),key,klen)) | |
2758 | continue; | |
d8c5b3c5 NC |
2759 | if (!!is_utf8 != !!(chain->refcounted_he_data[0] & HVhek_UTF8)) |
2760 | continue; | |
7b0bddfa NC |
2761 | #else |
2762 | if (hash != HEK_HASH(chain->refcounted_he_hek)) | |
2763 | continue; | |
670f1322 | 2764 | if (klen != (STRLEN)HEK_LEN(chain->refcounted_he_hek)) |
7b0bddfa NC |
2765 | continue; |
2766 | if (memNE(HEK_KEY(chain->refcounted_he_hek),key,klen)) | |
2767 | continue; | |
d8c5b3c5 NC |
2768 | if (!!is_utf8 != !!HEK_UTF8(chain->refcounted_he_hek)) |
2769 | continue; | |
7b0bddfa NC |
2770 | #endif |
2771 | ||
2772 | value = sv_2mortal(refcounted_he_value(chain)); | |
2773 | break; | |
2774 | } | |
2775 | ||
2776 | if (flags & HVhek_FREEKEY) | |
2777 | Safefree(key); | |
2778 | ||
2779 | return value; | |
2780 | } | |
2781 | ||
b3ca2e83 NC |
2782 | /* |
2783 | =for apidoc refcounted_he_new | |
2784 | ||
ec2a1de7 NC |
2785 | Creates a new C<struct refcounted_he>. As S<key> is copied, and value is |
2786 | stored in a compact form, all references remain the property of the caller. | |
2787 | The C<struct refcounted_he> is returned with a reference count of 1. | |
b3ca2e83 NC |
2788 | |
2789 | =cut | |
2790 | */ | |
2791 | ||
2792 | struct refcounted_he * | |
2793 | Perl_refcounted_he_new(pTHX_ struct refcounted_he *const parent, | |
2794 | SV *const key, SV *const value) { | |
7a89be66 | 2795 | dVAR; |
b3ca2e83 | 2796 | struct refcounted_he *he; |
b6bbf3fa NC |
2797 | STRLEN key_len; |
2798 | const char *key_p = SvPV_const(key, key_len); | |
2799 | STRLEN value_len = 0; | |
95b63a38 | 2800 | const char *value_p = NULL; |
b6bbf3fa NC |
2801 | char value_type; |
2802 | char flags; | |
2803 | STRLEN key_offset; | |
b3ca2e83 | 2804 | U32 hash; |
d8c5b3c5 | 2805 | bool is_utf8 = SvUTF8(key) ? TRUE : FALSE; |
b6bbf3fa NC |
2806 | |
2807 | if (SvPOK(value)) { | |
2808 | value_type = HVrhek_PV; | |
2809 | } else if (SvIOK(value)) { | |
2810 | value_type = HVrhek_IV; | |
2811 | } else if (value == &PL_sv_placeholder) { | |
2812 | value_type = HVrhek_delete; | |
2813 | } else if (!SvOK(value)) { | |
2814 | value_type = HVrhek_undef; | |
2815 | } else { | |
2816 | value_type = HVrhek_PV; | |
2817 | } | |
b3ca2e83 | 2818 | |
b6bbf3fa NC |
2819 | if (value_type == HVrhek_PV) { |
2820 | value_p = SvPV_const(value, value_len); | |
2821 | key_offset = value_len + 2; | |
2822 | } else { | |
2823 | value_len = 0; | |
2824 | key_offset = 1; | |
2825 | } | |
b6bbf3fa | 2826 | |
b6bbf3fa | 2827 | #ifdef USE_ITHREADS |
10edeb5d JH |
2828 | he = (struct refcounted_he*) |
2829 | PerlMemShared_malloc(sizeof(struct refcounted_he) - 1 | |
2830 | + key_len | |
2831 | + key_offset); | |
6cef672b | 2832 | #else |
10edeb5d JH |
2833 | he = (struct refcounted_he*) |
2834 | PerlMemShared_malloc(sizeof(struct refcounted_he) - 1 | |
2835 | + key_offset); | |
6cef672b | 2836 | #endif |
b3ca2e83 | 2837 | |
b3ca2e83 | 2838 | |
71ad1b0c | 2839 | he->refcounted_he_next = parent; |
b6bbf3fa NC |
2840 | |
2841 | if (value_type == HVrhek_PV) { | |
2842 | Copy(value_p, he->refcounted_he_data + 1, value_len + 1, char); | |
2843 | he->refcounted_he_val.refcounted_he_u_len = value_len; | |
44ebaf21 NC |
2844 | /* Do it this way so that the SvUTF8() test is after the SvPV, in case |
2845 | the value is overloaded, and doesn't yet have the UTF-8flag set. */ | |
2846 | if (SvUTF8(value)) | |
2847 | value_type = HVrhek_PV_UTF8; | |
b6bbf3fa NC |
2848 | } else if (value_type == HVrhek_IV) { |
2849 | if (SvUOK(value)) { | |
2850 | he->refcounted_he_val.refcounted_he_u_uv = SvUVX(value); | |
44ebaf21 | 2851 | value_type = HVrhek_UV; |
b6bbf3fa NC |
2852 | } else { |
2853 | he->refcounted_he_val.refcounted_he_u_iv = SvIVX(value); | |
2854 | } | |
2855 | } | |
44ebaf21 | 2856 | flags = value_type; |
b6bbf3fa NC |
2857 | |
2858 | if (is_utf8) { | |
2859 | /* Hash keys are always stored normalised to (yes) ISO-8859-1. | |
2860 | As we're going to be building hash keys from this value in future, | |
2861 | normalise it now. */ | |
2862 | key_p = (char*)bytes_from_utf8((const U8*)key_p, &key_len, &is_utf8); | |
2863 | flags |= is_utf8 ? HVhek_UTF8 : HVhek_WASUTF8; | |
2864 | } | |
2865 | PERL_HASH(hash, key_p, key_len); | |
2866 | ||
cbb1fbea | 2867 | #ifdef USE_ITHREADS |
b6bbf3fa NC |
2868 | he->refcounted_he_hash = hash; |
2869 | he->refcounted_he_keylen = key_len; | |
2870 | Copy(key_p, he->refcounted_he_data + key_offset, key_len, char); | |
cbb1fbea | 2871 | #else |
b6bbf3fa | 2872 | he->refcounted_he_hek = share_hek_flags(key_p, key_len, hash, flags); |
cbb1fbea | 2873 | #endif |
b6bbf3fa NC |
2874 | |
2875 | if (flags & HVhek_WASUTF8) { | |
2876 | /* If it was downgraded from UTF-8, then the pointer returned from | |
2877 | bytes_from_utf8 is an allocated pointer that we must free. */ | |
2878 | Safefree(key_p); | |
2879 | } | |
2880 | ||
2881 | he->refcounted_he_data[0] = flags; | |
b3ca2e83 NC |
2882 | he->refcounted_he_refcnt = 1; |
2883 | ||
2884 | return he; | |
2885 | } | |
2886 | ||
2887 | /* | |
2888 | =for apidoc refcounted_he_free | |
2889 | ||
2890 | Decrements the reference count of the passed in C<struct refcounted_he *> | |
2891 | by one. If the reference count reaches zero the structure's memory is freed, | |
2892 | and C<refcounted_he_free> iterates onto the parent node. | |
2893 | ||
2894 | =cut | |
2895 | */ | |
2896 | ||
2897 | void | |
2898 | Perl_refcounted_he_free(pTHX_ struct refcounted_he *he) { | |
53d44271 | 2899 | dVAR; |
57ca3b03 AL |
2900 | PERL_UNUSED_CONTEXT; |
2901 | ||
b3ca2e83 NC |
2902 | while (he) { |
2903 | struct refcounted_he *copy; | |
cbb1fbea | 2904 | U32 new_count; |
b3ca2e83 | 2905 | |
cbb1fbea NC |
2906 | HINTS_REFCNT_LOCK; |
2907 | new_count = --he->refcounted_he_refcnt; | |
2908 | HINTS_REFCNT_UNLOCK; | |
2909 | ||
2910 | if (new_count) { | |
b3ca2e83 | 2911 | return; |
cbb1fbea | 2912 | } |
b3ca2e83 | 2913 | |
b6bbf3fa | 2914 | #ifndef USE_ITHREADS |
71ad1b0c | 2915 | unshare_hek_or_pvn (he->refcounted_he_hek, 0, 0, 0); |
cbb1fbea | 2916 | #endif |
b3ca2e83 | 2917 | copy = he; |
71ad1b0c | 2918 | he = he->refcounted_he_next; |
b6bbf3fa | 2919 | PerlMemShared_free(copy); |
b3ca2e83 NC |
2920 | } |
2921 | } | |
2922 | ||
b3ca2e83 | 2923 | /* |
ecae49c0 NC |
2924 | =for apidoc hv_assert |
2925 | ||
2926 | Check that a hash is in an internally consistent state. | |
2927 | ||
2928 | =cut | |
2929 | */ | |
2930 | ||
943795c2 NC |
2931 | #ifdef DEBUGGING |
2932 | ||
ecae49c0 NC |
2933 | void |
2934 | Perl_hv_assert(pTHX_ HV *hv) | |
2935 | { | |
57ca3b03 AL |
2936 | dVAR; |
2937 | HE* entry; | |
2938 | int withflags = 0; | |
2939 | int placeholders = 0; | |
2940 | int real = 0; | |
2941 | int bad = 0; | |
2942 | const I32 riter = HvRITER_get(hv); | |
2943 | HE *eiter = HvEITER_get(hv); | |
2944 | ||
2945 | (void)hv_iterinit(hv); | |
2946 | ||
2947 | while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))) { | |
2948 | /* sanity check the values */ | |
2949 | if (HeVAL(entry) == &PL_sv_placeholder) | |
2950 | placeholders++; | |
2951 | else | |
2952 | real++; | |
2953 | /* sanity check the keys */ | |
2954 | if (HeSVKEY(entry)) { | |
6f207bd3 | 2955 | NOOP; /* Don't know what to check on SV keys. */ |
57ca3b03 AL |
2956 | } else if (HeKUTF8(entry)) { |
2957 | withflags++; | |
2958 | if (HeKWASUTF8(entry)) { | |
2959 | PerlIO_printf(Perl_debug_log, | |
d2a455e7 | 2960 | "hash key has both WASUTF8 and UTF8: '%.*s'\n", |
57ca3b03 AL |
2961 | (int) HeKLEN(entry), HeKEY(entry)); |
2962 | bad = 1; | |
2963 | } | |
2964 | } else if (HeKWASUTF8(entry)) | |
2965 | withflags++; | |
2966 | } | |
2967 | if (!SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) { | |
2968 | static const char bad_count[] = "Count %d %s(s), but hash reports %d\n"; | |
2969 | const int nhashkeys = HvUSEDKEYS(hv); | |
2970 | const int nhashplaceholders = HvPLACEHOLDERS_get(hv); | |
2971 | ||
2972 | if (nhashkeys != real) { | |
2973 | PerlIO_printf(Perl_debug_log, bad_count, real, "keys", nhashkeys ); | |
2974 | bad = 1; | |
2975 | } | |
2976 | if (nhashplaceholders != placeholders) { | |
2977 | PerlIO_printf(Perl_debug_log, bad_count, placeholders, "placeholder", nhashplaceholders ); | |
2978 | bad = 1; | |
2979 | } | |
2980 | } | |
2981 | if (withflags && ! HvHASKFLAGS(hv)) { | |
2982 | PerlIO_printf(Perl_debug_log, | |
2983 | "Hash has HASKFLAGS off but I count %d key(s) with flags\n", | |
2984 | withflags); | |
2985 | bad = 1; | |
2986 | } | |
2987 | if (bad) { | |
2988 | sv_dump((SV *)hv); | |
2989 | } | |
2990 | HvRITER_set(hv, riter); /* Restore hash iterator state */ | |
2991 | HvEITER_set(hv, eiter); | |
ecae49c0 | 2992 | } |
af3babe4 | 2993 | |
943795c2 NC |
2994 | #endif |
2995 | ||
af3babe4 NC |
2996 | /* |
2997 | * Local variables: | |
2998 | * c-indentation-style: bsd | |
2999 | * c-basic-offset: 4 | |
3000 | * indent-tabs-mode: t | |
3001 | * End: | |
3002 | * | |
37442d52 RGS |
3003 | * ex: set ts=8 sts=4 sw=4 noet: |
3004 | */ |