Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* hv.c |
79072805 | 2 | * |
4bb101f2 | 3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
af3babe4 | 4 | * 2000, 2001, 2002, 2003, 2004, 2005, 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 | { | |
42 | HE* he; | |
43 | HE* heend; | |
a02a5408 | 44 | Newx(he, PERL_ARENA_SIZE/sizeof(HE), HE); |
6a93a7e5 NC |
45 | HeNEXT(he) = (HE*) PL_body_arenaroots[HE_SVSLOT]; |
46 | PL_body_arenaroots[HE_SVSLOT] = he; | |
cac9b346 NC |
47 | |
48 | heend = &he[PERL_ARENA_SIZE / sizeof(HE) - 1]; | |
6a93a7e5 | 49 | PL_body_roots[HE_SVSLOT] = ++he; |
cac9b346 NC |
50 | while (he < heend) { |
51 | HeNEXT(he) = (HE*)(he + 1); | |
52 | he++; | |
53 | } | |
54 | HeNEXT(he) = 0; | |
55 | } | |
56 | ||
c941fb51 NC |
57 | #ifdef PURIFY |
58 | ||
59 | #define new_HE() (HE*)safemalloc(sizeof(HE)) | |
60 | #define del_HE(p) safefree((char*)p) | |
61 | ||
62 | #else | |
63 | ||
76e3520e | 64 | STATIC HE* |
cea2e8a9 | 65 | S_new_he(pTHX) |
4633a7c4 LW |
66 | { |
67 | HE* he; | |
6a93a7e5 NC |
68 | void **root = &PL_body_roots[HE_SVSLOT]; |
69 | ||
333f433b | 70 | LOCK_SV_MUTEX; |
6a93a7e5 | 71 | if (!*root) |
cac9b346 | 72 | S_more_he(aTHX); |
6a93a7e5 NC |
73 | he = *root; |
74 | *root = HeNEXT(he); | |
333f433b DG |
75 | UNLOCK_SV_MUTEX; |
76 | return he; | |
4633a7c4 LW |
77 | } |
78 | ||
c941fb51 NC |
79 | #define new_HE() new_he() |
80 | #define del_HE(p) \ | |
81 | STMT_START { \ | |
82 | LOCK_SV_MUTEX; \ | |
6a93a7e5 NC |
83 | HeNEXT(p) = (HE*)(PL_body_roots[HE_SVSLOT]); \ |
84 | PL_body_roots[HE_SVSLOT] = p; \ | |
c941fb51 NC |
85 | UNLOCK_SV_MUTEX; \ |
86 | } STMT_END | |
d33b2eba | 87 | |
d33b2eba | 88 | |
d33b2eba GS |
89 | |
90 | #endif | |
91 | ||
76e3520e | 92 | STATIC HEK * |
19692e8d | 93 | S_save_hek_flags(pTHX_ const char *str, I32 len, U32 hash, int flags) |
bbce6d69 | 94 | { |
35a4481c | 95 | const int flags_masked = flags & HVhek_MASK; |
bbce6d69 | 96 | char *k; |
97 | register HEK *hek; | |
1c846c1f | 98 | |
a02a5408 | 99 | Newx(k, HEK_BASESIZE + len + 2, char); |
bbce6d69 | 100 | hek = (HEK*)k; |
ff68c719 | 101 | Copy(str, HEK_KEY(hek), len, char); |
e05949c7 | 102 | HEK_KEY(hek)[len] = 0; |
ff68c719 | 103 | HEK_LEN(hek) = len; |
104 | HEK_HASH(hek) = hash; | |
dcf933a4 NC |
105 | HEK_FLAGS(hek) = (unsigned char)flags_masked; |
106 | ||
107 | if (flags & HVhek_FREEKEY) | |
108 | Safefree(str); | |
bbce6d69 | 109 | return hek; |
110 | } | |
111 | ||
4a31713e | 112 | /* free the pool of temporary HE/HEK pairs returned by hv_fetch_ent |
dd28f7bb DM |
113 | * for tied hashes */ |
114 | ||
115 | void | |
116 | Perl_free_tied_hv_pool(pTHX) | |
117 | { | |
dd28f7bb DM |
118 | HE *he = PL_hv_fetch_ent_mh; |
119 | while (he) { | |
9d4ba2ae | 120 | HE * const ohe = he; |
dd28f7bb | 121 | Safefree(HeKEY_hek(he)); |
dd28f7bb DM |
122 | he = HeNEXT(he); |
123 | del_HE(ohe); | |
124 | } | |
bf9cdc68 | 125 | PL_hv_fetch_ent_mh = Nullhe; |
dd28f7bb DM |
126 | } |
127 | ||
d18c6117 | 128 | #if defined(USE_ITHREADS) |
0bff533c NC |
129 | HEK * |
130 | Perl_hek_dup(pTHX_ HEK *source, CLONE_PARAMS* param) | |
131 | { | |
658b4a4a | 132 | HEK *shared = (HEK*)ptr_table_fetch(PL_ptr_table, source); |
9d4ba2ae AL |
133 | |
134 | PERL_UNUSED_ARG(param); | |
0bff533c NC |
135 | |
136 | if (shared) { | |
137 | /* We already shared this hash key. */ | |
454f1e26 | 138 | (void)share_hek_hek(shared); |
0bff533c NC |
139 | } |
140 | else { | |
658b4a4a | 141 | shared |
6e838c70 NC |
142 | = share_hek_flags(HEK_KEY(source), HEK_LEN(source), |
143 | HEK_HASH(source), HEK_FLAGS(source)); | |
658b4a4a | 144 | ptr_table_store(PL_ptr_table, source, shared); |
0bff533c | 145 | } |
658b4a4a | 146 | return shared; |
0bff533c NC |
147 | } |
148 | ||
d18c6117 | 149 | HE * |
5c4138a0 | 150 | Perl_he_dup(pTHX_ const HE *e, bool shared, CLONE_PARAMS* param) |
d18c6117 GS |
151 | { |
152 | HE *ret; | |
153 | ||
154 | if (!e) | |
155 | return Nullhe; | |
7766f137 GS |
156 | /* look for it in the table first */ |
157 | ret = (HE*)ptr_table_fetch(PL_ptr_table, e); | |
158 | if (ret) | |
159 | return ret; | |
160 | ||
161 | /* create anew and remember what it is */ | |
d33b2eba | 162 | ret = new_HE(); |
7766f137 GS |
163 | ptr_table_store(PL_ptr_table, e, ret); |
164 | ||
d2d73c3e | 165 | HeNEXT(ret) = he_dup(HeNEXT(e),shared, param); |
dd28f7bb DM |
166 | if (HeKLEN(e) == HEf_SVKEY) { |
167 | char *k; | |
a02a5408 | 168 | Newx(k, HEK_BASESIZE + sizeof(SV*), char); |
dd28f7bb | 169 | HeKEY_hek(ret) = (HEK*)k; |
d2d73c3e | 170 | HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e), param)); |
dd28f7bb | 171 | } |
c21d1a0f | 172 | else if (shared) { |
0bff533c NC |
173 | /* This is hek_dup inlined, which seems to be important for speed |
174 | reasons. */ | |
1b6737cc | 175 | HEK * const source = HeKEY_hek(e); |
658b4a4a | 176 | HEK *shared = (HEK*)ptr_table_fetch(PL_ptr_table, source); |
c21d1a0f NC |
177 | |
178 | if (shared) { | |
179 | /* We already shared this hash key. */ | |
454f1e26 | 180 | (void)share_hek_hek(shared); |
c21d1a0f NC |
181 | } |
182 | else { | |
658b4a4a | 183 | shared |
6e838c70 NC |
184 | = share_hek_flags(HEK_KEY(source), HEK_LEN(source), |
185 | HEK_HASH(source), HEK_FLAGS(source)); | |
658b4a4a | 186 | ptr_table_store(PL_ptr_table, source, shared); |
c21d1a0f | 187 | } |
658b4a4a | 188 | HeKEY_hek(ret) = shared; |
c21d1a0f | 189 | } |
d18c6117 | 190 | else |
19692e8d NC |
191 | HeKEY_hek(ret) = save_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e), |
192 | HeKFLAGS(e)); | |
d2d73c3e | 193 | HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e), param)); |
d18c6117 GS |
194 | return ret; |
195 | } | |
196 | #endif /* USE_ITHREADS */ | |
197 | ||
1b1f1335 | 198 | static void |
2393f1b9 JH |
199 | S_hv_notallowed(pTHX_ int flags, const char *key, I32 klen, |
200 | const char *msg) | |
1b1f1335 | 201 | { |
1b6737cc | 202 | SV * const sv = sv_newmortal(); |
19692e8d | 203 | if (!(flags & HVhek_FREEKEY)) { |
1b1f1335 NIS |
204 | sv_setpvn(sv, key, klen); |
205 | } | |
206 | else { | |
207 | /* Need to free saved eventually assign to mortal SV */ | |
34c3c4e3 | 208 | /* XXX is this line an error ???: SV *sv = sv_newmortal(); */ |
1b1f1335 NIS |
209 | sv_usepvn(sv, (char *) key, klen); |
210 | } | |
19692e8d | 211 | if (flags & HVhek_UTF8) { |
1b1f1335 NIS |
212 | SvUTF8_on(sv); |
213 | } | |
c8cd6465 | 214 | Perl_croak(aTHX_ msg, sv); |
1b1f1335 NIS |
215 | } |
216 | ||
fde52b5c | 217 | /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot |
218 | * contains an SV* */ | |
219 | ||
34a6f7b4 NC |
220 | #define HV_FETCH_ISSTORE 0x01 |
221 | #define HV_FETCH_ISEXISTS 0x02 | |
222 | #define HV_FETCH_LVALUE 0x04 | |
223 | #define HV_FETCH_JUST_SV 0x08 | |
224 | ||
225 | /* | |
226 | =for apidoc hv_store | |
227 | ||
228 | Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is | |
229 | the length of the key. The C<hash> parameter is the precomputed hash | |
230 | value; if it is zero then Perl will compute it. The return value will be | |
231 | NULL if the operation failed or if the value did not need to be actually | |
232 | stored within the hash (as in the case of tied hashes). Otherwise it can | |
233 | be dereferenced to get the original C<SV*>. Note that the caller is | |
234 | responsible for suitably incrementing the reference count of C<val> before | |
235 | the call, and decrementing it if the function returned NULL. Effectively | |
236 | a successful hv_store takes ownership of one reference to C<val>. This is | |
237 | usually what you want; a newly created SV has a reference count of one, so | |
238 | if all your code does is create SVs then store them in a hash, hv_store | |
239 | will own the only reference to the new SV, and your code doesn't need to do | |
240 | anything further to tidy up. hv_store is not implemented as a call to | |
241 | hv_store_ent, and does not create a temporary SV for the key, so if your | |
242 | key data is not already in SV form then use hv_store in preference to | |
243 | hv_store_ent. | |
244 | ||
245 | See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more | |
246 | information on how to use this function on tied hashes. | |
247 | ||
248 | =cut | |
249 | */ | |
250 | ||
251 | SV** | |
252 | Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen_i32, SV *val, U32 hash) | |
253 | { | |
254 | HE *hek; | |
255 | STRLEN klen; | |
256 | int flags; | |
257 | ||
258 | if (klen_i32 < 0) { | |
259 | klen = -klen_i32; | |
260 | flags = HVhek_UTF8; | |
261 | } else { | |
262 | klen = klen_i32; | |
263 | flags = 0; | |
264 | } | |
265 | hek = hv_fetch_common (hv, NULL, key, klen, flags, | |
52d01cc2 | 266 | (HV_FETCH_ISSTORE|HV_FETCH_JUST_SV), val, hash); |
34a6f7b4 NC |
267 | return hek ? &HeVAL(hek) : NULL; |
268 | } | |
269 | ||
270 | SV** | |
271 | Perl_hv_store_flags(pTHX_ HV *hv, const char *key, I32 klen, SV *val, | |
272 | register U32 hash, int flags) | |
273 | { | |
9d4ba2ae | 274 | HE * const hek = hv_fetch_common (hv, NULL, key, klen, flags, |
34a6f7b4 NC |
275 | (HV_FETCH_ISSTORE|HV_FETCH_JUST_SV), val, hash); |
276 | return hek ? &HeVAL(hek) : NULL; | |
277 | } | |
278 | ||
279 | /* | |
280 | =for apidoc hv_store_ent | |
281 | ||
282 | Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash> | |
283 | parameter is the precomputed hash value; if it is zero then Perl will | |
284 | compute it. The return value is the new hash entry so created. It will be | |
285 | NULL if the operation failed or if the value did not need to be actually | |
286 | stored within the hash (as in the case of tied hashes). Otherwise the | |
287 | contents of the return value can be accessed using the C<He?> macros | |
288 | described here. Note that the caller is responsible for suitably | |
289 | incrementing the reference count of C<val> before the call, and | |
290 | decrementing it if the function returned NULL. Effectively a successful | |
291 | hv_store_ent takes ownership of one reference to C<val>. This is | |
292 | usually what you want; a newly created SV has a reference count of one, so | |
293 | if all your code does is create SVs then store them in a hash, hv_store | |
294 | will own the only reference to the new SV, and your code doesn't need to do | |
295 | anything further to tidy up. Note that hv_store_ent only reads the C<key>; | |
296 | unlike C<val> it does not take ownership of it, so maintaining the correct | |
297 | reference count on C<key> is entirely the caller's responsibility. hv_store | |
298 | is not implemented as a call to hv_store_ent, and does not create a temporary | |
299 | SV for the key, so if your key data is not already in SV form then use | |
300 | hv_store in preference to hv_store_ent. | |
301 | ||
302 | See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more | |
303 | information on how to use this function on tied hashes. | |
304 | ||
305 | =cut | |
306 | */ | |
307 | ||
308 | HE * | |
309 | Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, U32 hash) | |
310 | { | |
311 | return hv_fetch_common(hv, keysv, NULL, 0, 0, HV_FETCH_ISSTORE, val, hash); | |
312 | } | |
313 | ||
314 | /* | |
315 | =for apidoc hv_exists | |
316 | ||
317 | Returns a boolean indicating whether the specified hash key exists. The | |
318 | C<klen> is the length of the key. | |
319 | ||
320 | =cut | |
321 | */ | |
322 | ||
323 | bool | |
324 | Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen_i32) | |
325 | { | |
326 | STRLEN klen; | |
327 | int flags; | |
328 | ||
329 | if (klen_i32 < 0) { | |
330 | klen = -klen_i32; | |
331 | flags = HVhek_UTF8; | |
332 | } else { | |
333 | klen = klen_i32; | |
334 | flags = 0; | |
335 | } | |
336 | return hv_fetch_common(hv, NULL, key, klen, flags, HV_FETCH_ISEXISTS, 0, 0) | |
337 | ? TRUE : FALSE; | |
338 | } | |
339 | ||
954c1994 GS |
340 | /* |
341 | =for apidoc hv_fetch | |
342 | ||
343 | Returns the SV which corresponds to the specified key in the hash. The | |
344 | C<klen> is the length of the key. If C<lval> is set then the fetch will be | |
345 | part of a store. Check that the return value is non-null before | |
d1be9408 | 346 | dereferencing it to an C<SV*>. |
954c1994 | 347 | |
96f1132b | 348 | See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more |
954c1994 GS |
349 | information on how to use this function on tied hashes. |
350 | ||
351 | =cut | |
352 | */ | |
353 | ||
79072805 | 354 | SV** |
c1fe5510 | 355 | Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 lval) |
79072805 | 356 | { |
c1fe5510 NC |
357 | HE *hek; |
358 | STRLEN klen; | |
359 | int flags; | |
360 | ||
361 | if (klen_i32 < 0) { | |
362 | klen = -klen_i32; | |
363 | flags = HVhek_UTF8; | |
364 | } else { | |
365 | klen = klen_i32; | |
366 | flags = 0; | |
367 | } | |
368 | hek = hv_fetch_common (hv, NULL, key, klen, flags, | |
b2c64049 NC |
369 | HV_FETCH_JUST_SV | (lval ? HV_FETCH_LVALUE : 0), |
370 | Nullsv, 0); | |
113738bb | 371 | return hek ? &HeVAL(hek) : NULL; |
79072805 LW |
372 | } |
373 | ||
34a6f7b4 NC |
374 | /* |
375 | =for apidoc hv_exists_ent | |
376 | ||
377 | Returns a boolean indicating whether the specified hash key exists. C<hash> | |
378 | can be a valid precomputed hash value, or 0 to ask for it to be | |
379 | computed. | |
380 | ||
381 | =cut | |
382 | */ | |
383 | ||
384 | bool | |
385 | Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash) | |
386 | { | |
387 | return hv_fetch_common(hv, keysv, NULL, 0, 0, HV_FETCH_ISEXISTS, 0, hash) | |
388 | ? TRUE : FALSE; | |
389 | } | |
390 | ||
d1be9408 | 391 | /* returns an HE * structure with the all fields set */ |
fde52b5c | 392 | /* note that hent_val will be a mortal sv for MAGICAL hashes */ |
954c1994 GS |
393 | /* |
394 | =for apidoc hv_fetch_ent | |
395 | ||
396 | Returns the hash entry which corresponds to the specified key in the hash. | |
397 | C<hash> must be a valid precomputed hash number for the given C<key>, or 0 | |
398 | if you want the function to compute it. IF C<lval> is set then the fetch | |
399 | will be part of a store. Make sure the return value is non-null before | |
400 | accessing it. The return value when C<tb> is a tied hash is a pointer to a | |
401 | static location, so be sure to make a copy of the structure if you need to | |
1c846c1f | 402 | store it somewhere. |
954c1994 | 403 | |
96f1132b | 404 | See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more |
954c1994 GS |
405 | information on how to use this function on tied hashes. |
406 | ||
407 | =cut | |
408 | */ | |
409 | ||
fde52b5c | 410 | HE * |
864dbfa3 | 411 | Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash) |
fde52b5c | 412 | { |
7f66fda2 | 413 | return hv_fetch_common(hv, keysv, NULL, 0, 0, |
b2c64049 | 414 | (lval ? HV_FETCH_LVALUE : 0), Nullsv, hash); |
113738bb NC |
415 | } |
416 | ||
8f8d40ab | 417 | STATIC HE * |
c1fe5510 | 418 | S_hv_fetch_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen, |
b2c64049 | 419 | int flags, int action, SV *val, register U32 hash) |
113738bb | 420 | { |
27da23d5 | 421 | dVAR; |
b2c64049 | 422 | XPVHV* xhv; |
b2c64049 NC |
423 | HE *entry; |
424 | HE **oentry; | |
fde52b5c | 425 | SV *sv; |
da58a35d | 426 | bool is_utf8; |
113738bb | 427 | int masked_flags; |
fde52b5c | 428 | |
429 | if (!hv) | |
430 | return 0; | |
431 | ||
113738bb | 432 | if (keysv) { |
e593d2fe AE |
433 | if (flags & HVhek_FREEKEY) |
434 | Safefree(key); | |
5c144d81 | 435 | key = SvPV_const(keysv, klen); |
c1fe5510 | 436 | flags = 0; |
113738bb NC |
437 | is_utf8 = (SvUTF8(keysv) != 0); |
438 | } else { | |
c1fe5510 | 439 | is_utf8 = ((flags & HVhek_UTF8) ? TRUE : FALSE); |
113738bb | 440 | } |
113738bb | 441 | |
b2c64049 | 442 | xhv = (XPVHV*)SvANY(hv); |
7f66fda2 NC |
443 | if (SvMAGICAL(hv)) { |
444 | if (SvRMAGICAL(hv) && !(action & (HV_FETCH_ISSTORE|HV_FETCH_ISEXISTS))) | |
445 | { | |
446 | if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) { | |
447 | sv = sv_newmortal(); | |
113738bb | 448 | |
7f66fda2 NC |
449 | /* XXX should be able to skimp on the HE/HEK here when |
450 | HV_FETCH_JUST_SV is true. */ | |
113738bb | 451 | |
7f66fda2 NC |
452 | if (!keysv) { |
453 | keysv = newSVpvn(key, klen); | |
454 | if (is_utf8) { | |
455 | SvUTF8_on(keysv); | |
456 | } | |
457 | } else { | |
458 | keysv = newSVsv(keysv); | |
113738bb | 459 | } |
7f66fda2 NC |
460 | mg_copy((SV*)hv, sv, (char *)keysv, HEf_SVKEY); |
461 | ||
462 | /* grab a fake HE/HEK pair from the pool or make a new one */ | |
463 | entry = PL_hv_fetch_ent_mh; | |
464 | if (entry) | |
465 | PL_hv_fetch_ent_mh = HeNEXT(entry); | |
466 | else { | |
467 | char *k; | |
468 | entry = new_HE(); | |
a02a5408 | 469 | Newx(k, HEK_BASESIZE + sizeof(SV*), char); |
7f66fda2 NC |
470 | HeKEY_hek(entry) = (HEK*)k; |
471 | } | |
472 | HeNEXT(entry) = Nullhe; | |
473 | HeSVKEY_set(entry, keysv); | |
474 | HeVAL(entry) = sv; | |
475 | sv_upgrade(sv, SVt_PVLV); | |
476 | LvTYPE(sv) = 'T'; | |
477 | /* so we can free entry when freeing sv */ | |
478 | LvTARG(sv) = (SV*)entry; | |
479 | ||
480 | /* XXX remove at some point? */ | |
481 | if (flags & HVhek_FREEKEY) | |
482 | Safefree(key); | |
483 | ||
484 | return entry; | |
113738bb | 485 | } |
7f66fda2 NC |
486 | #ifdef ENV_IS_CASELESS |
487 | else if (mg_find((SV*)hv, PERL_MAGIC_env)) { | |
488 | U32 i; | |
489 | for (i = 0; i < klen; ++i) | |
490 | if (isLOWER(key[i])) { | |
086cb327 NC |
491 | /* Would be nice if we had a routine to do the |
492 | copy and upercase in a single pass through. */ | |
e1ec3a88 | 493 | const char *nkey = strupr(savepvn(key,klen)); |
086cb327 NC |
494 | /* Note that this fetch is for nkey (the uppercased |
495 | key) whereas the store is for key (the original) */ | |
496 | entry = hv_fetch_common(hv, Nullsv, nkey, klen, | |
497 | HVhek_FREEKEY, /* free nkey */ | |
498 | 0 /* non-LVAL fetch */, | |
499 | Nullsv /* no value */, | |
500 | 0 /* compute hash */); | |
501 | if (!entry && (action & HV_FETCH_LVALUE)) { | |
502 | /* This call will free key if necessary. | |
503 | Do it this way to encourage compiler to tail | |
504 | call optimise. */ | |
505 | entry = hv_fetch_common(hv, keysv, key, klen, | |
506 | flags, HV_FETCH_ISSTORE, | |
507 | NEWSV(61,0), hash); | |
508 | } else { | |
509 | if (flags & HVhek_FREEKEY) | |
510 | Safefree(key); | |
511 | } | |
512 | return entry; | |
7f66fda2 | 513 | } |
902173a3 | 514 | } |
7f66fda2 NC |
515 | #endif |
516 | } /* ISFETCH */ | |
517 | else if (SvRMAGICAL(hv) && (action & HV_FETCH_ISEXISTS)) { | |
518 | if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) { | |
b2c64049 NC |
519 | /* I don't understand why hv_exists_ent has svret and sv, |
520 | whereas hv_exists only had one. */ | |
9d4ba2ae | 521 | SV * const svret = sv_newmortal(); |
b2c64049 | 522 | sv = sv_newmortal(); |
7f66fda2 NC |
523 | |
524 | if (keysv || is_utf8) { | |
525 | if (!keysv) { | |
526 | keysv = newSVpvn(key, klen); | |
527 | SvUTF8_on(keysv); | |
528 | } else { | |
529 | keysv = newSVsv(keysv); | |
530 | } | |
b2c64049 NC |
531 | mg_copy((SV*)hv, sv, (char *)sv_2mortal(keysv), HEf_SVKEY); |
532 | } else { | |
533 | mg_copy((SV*)hv, sv, key, klen); | |
7f66fda2 | 534 | } |
b2c64049 NC |
535 | if (flags & HVhek_FREEKEY) |
536 | Safefree(key); | |
7f66fda2 NC |
537 | magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem)); |
538 | /* This cast somewhat evil, but I'm merely using NULL/ | |
539 | not NULL to return the boolean exists. | |
540 | And I know hv is not NULL. */ | |
541 | return SvTRUE(svret) ? (HE *)hv : NULL; | |
e7152ba2 | 542 | } |
7f66fda2 NC |
543 | #ifdef ENV_IS_CASELESS |
544 | else if (mg_find((SV*)hv, PERL_MAGIC_env)) { | |
545 | /* XXX This code isn't UTF8 clean. */ | |
a15d23f8 | 546 | char * const keysave = (char * const)key; |
b2c64049 NC |
547 | /* Will need to free this, so set FREEKEY flag. */ |
548 | key = savepvn(key,klen); | |
549 | key = (const char*)strupr((char*)key); | |
7f66fda2 NC |
550 | is_utf8 = 0; |
551 | hash = 0; | |
8b4f7dd5 | 552 | keysv = 0; |
b2c64049 NC |
553 | |
554 | if (flags & HVhek_FREEKEY) { | |
555 | Safefree(keysave); | |
556 | } | |
557 | flags |= HVhek_FREEKEY; | |
7f66fda2 | 558 | } |
902173a3 | 559 | #endif |
7f66fda2 | 560 | } /* ISEXISTS */ |
b2c64049 NC |
561 | else if (action & HV_FETCH_ISSTORE) { |
562 | bool needs_copy; | |
563 | bool needs_store; | |
564 | hv_magic_check (hv, &needs_copy, &needs_store); | |
565 | if (needs_copy) { | |
a3b680e6 | 566 | const bool save_taint = PL_tainted; |
b2c64049 NC |
567 | if (keysv || is_utf8) { |
568 | if (!keysv) { | |
569 | keysv = newSVpvn(key, klen); | |
570 | SvUTF8_on(keysv); | |
571 | } | |
572 | if (PL_tainting) | |
573 | PL_tainted = SvTAINTED(keysv); | |
574 | keysv = sv_2mortal(newSVsv(keysv)); | |
575 | mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY); | |
576 | } else { | |
577 | mg_copy((SV*)hv, val, key, klen); | |
578 | } | |
579 | ||
580 | TAINT_IF(save_taint); | |
7b2c381c | 581 | if (!HvARRAY(hv) && !needs_store) { |
b2c64049 NC |
582 | if (flags & HVhek_FREEKEY) |
583 | Safefree(key); | |
584 | return Nullhe; | |
585 | } | |
586 | #ifdef ENV_IS_CASELESS | |
587 | else if (mg_find((SV*)hv, PERL_MAGIC_env)) { | |
588 | /* XXX This code isn't UTF8 clean. */ | |
589 | const char *keysave = key; | |
590 | /* Will need to free this, so set FREEKEY flag. */ | |
591 | key = savepvn(key,klen); | |
592 | key = (const char*)strupr((char*)key); | |
593 | is_utf8 = 0; | |
594 | hash = 0; | |
8b4f7dd5 | 595 | keysv = 0; |
b2c64049 NC |
596 | |
597 | if (flags & HVhek_FREEKEY) { | |
598 | Safefree(keysave); | |
599 | } | |
600 | flags |= HVhek_FREEKEY; | |
601 | } | |
602 | #endif | |
603 | } | |
604 | } /* ISSTORE */ | |
7f66fda2 | 605 | } /* SvMAGICAL */ |
fde52b5c | 606 | |
7b2c381c | 607 | if (!HvARRAY(hv)) { |
b2c64049 | 608 | if ((action & (HV_FETCH_LVALUE | HV_FETCH_ISSTORE)) |
fde52b5c | 609 | #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */ |
8aacddc1 | 610 | || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) |
fde52b5c | 611 | #endif |
d58e6666 NC |
612 | ) { |
613 | char *array; | |
a02a5408 | 614 | Newxz(array, |
cbec9347 | 615 | PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */), |
d58e6666 NC |
616 | char); |
617 | HvARRAY(hv) = (HE**)array; | |
618 | } | |
7f66fda2 NC |
619 | #ifdef DYNAMIC_ENV_FETCH |
620 | else if (action & HV_FETCH_ISEXISTS) { | |
621 | /* for an %ENV exists, if we do an insert it's by a recursive | |
622 | store call, so avoid creating HvARRAY(hv) right now. */ | |
623 | } | |
624 | #endif | |
113738bb NC |
625 | else { |
626 | /* XXX remove at some point? */ | |
627 | if (flags & HVhek_FREEKEY) | |
628 | Safefree(key); | |
629 | ||
fde52b5c | 630 | return 0; |
113738bb | 631 | } |
fde52b5c | 632 | } |
633 | ||
19692e8d | 634 | if (is_utf8) { |
a15d23f8 | 635 | char * const keysave = (char * const)key; |
f9a63242 | 636 | key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8); |
19692e8d | 637 | if (is_utf8) |
c1fe5510 NC |
638 | flags |= HVhek_UTF8; |
639 | else | |
640 | flags &= ~HVhek_UTF8; | |
7f66fda2 NC |
641 | if (key != keysave) { |
642 | if (flags & HVhek_FREEKEY) | |
643 | Safefree(keysave); | |
19692e8d | 644 | flags |= HVhek_WASUTF8 | HVhek_FREEKEY; |
7f66fda2 | 645 | } |
19692e8d | 646 | } |
f9a63242 | 647 | |
4b5190b5 NC |
648 | if (HvREHASH(hv)) { |
649 | PERL_HASH_INTERNAL(hash, key, klen); | |
b2c64049 NC |
650 | /* We don't have a pointer to the hv, so we have to replicate the |
651 | flag into every HEK, so that hv_iterkeysv can see it. */ | |
652 | /* And yes, you do need this even though you are not "storing" because | |
fdcd69b6 NC |
653 | you can flip the flags below if doing an lval lookup. (And that |
654 | was put in to give the semantics Andreas was expecting.) */ | |
655 | flags |= HVhek_REHASH; | |
4b5190b5 | 656 | } else if (!hash) { |
113738bb | 657 | if (keysv && (SvIsCOW_shared_hash(keysv))) { |
c158a4fd | 658 | hash = SvSHARED_HASH(keysv); |
46187eeb NC |
659 | } else { |
660 | PERL_HASH(hash, key, klen); | |
661 | } | |
662 | } | |
effa1e2d | 663 | |
113738bb NC |
664 | masked_flags = (flags & HVhek_MASK); |
665 | ||
7f66fda2 | 666 | #ifdef DYNAMIC_ENV_FETCH |
7b2c381c | 667 | if (!HvARRAY(hv)) entry = Null(HE*); |
7f66fda2 NC |
668 | else |
669 | #endif | |
b2c64049 | 670 | { |
7b2c381c | 671 | entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; |
b2c64049 | 672 | } |
0298d7b9 | 673 | for (; entry; entry = HeNEXT(entry)) { |
fde52b5c | 674 | if (HeHASH(entry) != hash) /* strings can't be equal */ |
675 | continue; | |
eb160463 | 676 | if (HeKLEN(entry) != (I32)klen) |
fde52b5c | 677 | continue; |
1c846c1f | 678 | if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */ |
fde52b5c | 679 | continue; |
113738bb | 680 | if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8) |
c3654f1a | 681 | continue; |
b2c64049 NC |
682 | |
683 | if (action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE)) { | |
684 | if (HeKFLAGS(entry) != masked_flags) { | |
685 | /* We match if HVhek_UTF8 bit in our flags and hash key's | |
686 | match. But if entry was set previously with HVhek_WASUTF8 | |
687 | and key now doesn't (or vice versa) then we should change | |
688 | the key's flag, as this is assignment. */ | |
689 | if (HvSHAREKEYS(hv)) { | |
690 | /* Need to swap the key we have for a key with the flags we | |
691 | need. As keys are shared we can't just write to the | |
692 | flag, so we share the new one, unshare the old one. */ | |
6e838c70 NC |
693 | HEK *new_hek = share_hek_flags(key, klen, hash, |
694 | masked_flags); | |
b2c64049 NC |
695 | unshare_hek (HeKEY_hek(entry)); |
696 | HeKEY_hek(entry) = new_hek; | |
697 | } | |
5d2b1485 NC |
698 | else if (hv == PL_strtab) { |
699 | /* PL_strtab is usually the only hash without HvSHAREKEYS, | |
700 | so putting this test here is cheap */ | |
701 | if (flags & HVhek_FREEKEY) | |
702 | Safefree(key); | |
703 | Perl_croak(aTHX_ S_strtab_error, | |
704 | action & HV_FETCH_LVALUE ? "fetch" : "store"); | |
705 | } | |
b2c64049 NC |
706 | else |
707 | HeKFLAGS(entry) = masked_flags; | |
708 | if (masked_flags & HVhek_ENABLEHVKFLAGS) | |
709 | HvHASKFLAGS_on(hv); | |
710 | } | |
711 | if (HeVAL(entry) == &PL_sv_placeholder) { | |
712 | /* yes, can store into placeholder slot */ | |
713 | if (action & HV_FETCH_LVALUE) { | |
714 | if (SvMAGICAL(hv)) { | |
715 | /* This preserves behaviour with the old hv_fetch | |
716 | implementation which at this point would bail out | |
717 | with a break; (at "if we find a placeholder, we | |
718 | pretend we haven't found anything") | |
719 | ||
720 | That break mean that if a placeholder were found, it | |
721 | caused a call into hv_store, which in turn would | |
722 | check magic, and if there is no magic end up pretty | |
723 | much back at this point (in hv_store's code). */ | |
724 | break; | |
725 | } | |
726 | /* LVAL fetch which actaully needs a store. */ | |
727 | val = NEWSV(61,0); | |
ca732855 | 728 | HvPLACEHOLDERS(hv)--; |
b2c64049 NC |
729 | } else { |
730 | /* store */ | |
731 | if (val != &PL_sv_placeholder) | |
ca732855 | 732 | HvPLACEHOLDERS(hv)--; |
b2c64049 NC |
733 | } |
734 | HeVAL(entry) = val; | |
735 | } else if (action & HV_FETCH_ISSTORE) { | |
736 | SvREFCNT_dec(HeVAL(entry)); | |
737 | HeVAL(entry) = val; | |
738 | } | |
27bcc0a7 | 739 | } else if (HeVAL(entry) == &PL_sv_placeholder) { |
b2c64049 NC |
740 | /* if we find a placeholder, we pretend we haven't found |
741 | anything */ | |
8aacddc1 | 742 | break; |
b2c64049 | 743 | } |
113738bb NC |
744 | if (flags & HVhek_FREEKEY) |
745 | Safefree(key); | |
fde52b5c | 746 | return entry; |
747 | } | |
748 | #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */ | |
0ed29950 NC |
749 | if (!(action & HV_FETCH_ISSTORE) |
750 | && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) { | |
a6c40364 | 751 | unsigned long len; |
9d4ba2ae | 752 | const char * const env = PerlEnv_ENVgetenv_len(key,&len); |
a6c40364 GS |
753 | if (env) { |
754 | sv = newSVpvn(env,len); | |
755 | SvTAINTED_on(sv); | |
7fd3d16e | 756 | return hv_fetch_common(hv,keysv,key,klen,flags,HV_FETCH_ISSTORE,sv, |
b2c64049 | 757 | hash); |
a6c40364 | 758 | } |
fde52b5c | 759 | } |
760 | #endif | |
7f66fda2 NC |
761 | |
762 | if (!entry && SvREADONLY(hv) && !(action & HV_FETCH_ISEXISTS)) { | |
2393f1b9 | 763 | S_hv_notallowed(aTHX_ flags, key, klen, |
c8cd6465 NC |
764 | "Attempt to access disallowed key '%"SVf"' in" |
765 | " a restricted hash"); | |
1b1f1335 | 766 | } |
b2c64049 NC |
767 | if (!(action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE))) { |
768 | /* Not doing some form of store, so return failure. */ | |
769 | if (flags & HVhek_FREEKEY) | |
770 | Safefree(key); | |
771 | return 0; | |
772 | } | |
113738bb | 773 | if (action & HV_FETCH_LVALUE) { |
b2c64049 NC |
774 | val = NEWSV(61,0); |
775 | if (SvMAGICAL(hv)) { | |
776 | /* At this point the old hv_fetch code would call to hv_store, | |
777 | which in turn might do some tied magic. So we need to make that | |
778 | magic check happen. */ | |
779 | /* gonna assign to this, so it better be there */ | |
780 | return hv_fetch_common(hv, keysv, key, klen, flags, | |
781 | HV_FETCH_ISSTORE, val, hash); | |
782 | /* XXX Surely that could leak if the fetch-was-store fails? | |
783 | Just like the hv_fetch. */ | |
113738bb NC |
784 | } |
785 | } | |
786 | ||
b2c64049 NC |
787 | /* Welcome to hv_store... */ |
788 | ||
7b2c381c | 789 | if (!HvARRAY(hv)) { |
b2c64049 NC |
790 | /* Not sure if we can get here. I think the only case of oentry being |
791 | NULL is for %ENV with dynamic env fetch. But that should disappear | |
792 | with magic in the previous code. */ | |
d58e6666 | 793 | char *array; |
a02a5408 | 794 | Newxz(array, |
b2c64049 | 795 | PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */), |
d58e6666 NC |
796 | char); |
797 | HvARRAY(hv) = (HE**)array; | |
b2c64049 NC |
798 | } |
799 | ||
7b2c381c | 800 | oentry = &(HvARRAY(hv))[hash & (I32) xhv->xhv_max]; |
ab4af705 | 801 | |
b2c64049 NC |
802 | entry = new_HE(); |
803 | /* share_hek_flags will do the free for us. This might be considered | |
804 | bad API design. */ | |
805 | if (HvSHAREKEYS(hv)) | |
6e838c70 | 806 | HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags); |
5d2b1485 NC |
807 | else if (hv == PL_strtab) { |
808 | /* PL_strtab is usually the only hash without HvSHAREKEYS, so putting | |
809 | this test here is cheap */ | |
810 | if (flags & HVhek_FREEKEY) | |
811 | Safefree(key); | |
812 | Perl_croak(aTHX_ S_strtab_error, | |
813 | action & HV_FETCH_LVALUE ? "fetch" : "store"); | |
814 | } | |
b2c64049 NC |
815 | else /* gotta do the real thing */ |
816 | HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags); | |
817 | HeVAL(entry) = val; | |
818 | HeNEXT(entry) = *oentry; | |
819 | *oentry = entry; | |
820 | ||
821 | if (val == &PL_sv_placeholder) | |
ca732855 | 822 | HvPLACEHOLDERS(hv)++; |
b2c64049 NC |
823 | if (masked_flags & HVhek_ENABLEHVKFLAGS) |
824 | HvHASKFLAGS_on(hv); | |
825 | ||
0298d7b9 NC |
826 | { |
827 | const HE *counter = HeNEXT(entry); | |
828 | ||
829 | xhv->xhv_keys++; /* HvKEYS(hv)++ */ | |
830 | if (!counter) { /* initial entry? */ | |
831 | xhv->xhv_fill++; /* HvFILL(hv)++ */ | |
832 | } else if (xhv->xhv_keys > (IV)xhv->xhv_max) { | |
833 | hsplit(hv); | |
834 | } else if(!HvREHASH(hv)) { | |
835 | U32 n_links = 1; | |
836 | ||
837 | while ((counter = HeNEXT(counter))) | |
838 | n_links++; | |
839 | ||
840 | if (n_links > HV_MAX_LENGTH_BEFORE_SPLIT) { | |
841 | /* Use only the old HvKEYS(hv) > HvMAX(hv) condition to limit | |
842 | bucket splits on a rehashed hash, as we're not going to | |
843 | split it again, and if someone is lucky (evil) enough to | |
844 | get all the keys in one list they could exhaust our memory | |
845 | as we repeatedly double the number of buckets on every | |
846 | entry. Linear search feels a less worse thing to do. */ | |
847 | hsplit(hv); | |
848 | } | |
849 | } | |
fde52b5c | 850 | } |
b2c64049 NC |
851 | |
852 | return entry; | |
fde52b5c | 853 | } |
854 | ||
864dbfa3 | 855 | STATIC void |
cea2e8a9 | 856 | S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store) |
d0066dc7 | 857 | { |
a3b680e6 | 858 | const MAGIC *mg = SvMAGIC(hv); |
d0066dc7 OT |
859 | *needs_copy = FALSE; |
860 | *needs_store = TRUE; | |
861 | while (mg) { | |
862 | if (isUPPER(mg->mg_type)) { | |
863 | *needs_copy = TRUE; | |
d60c5a05 | 864 | if (mg->mg_type == PERL_MAGIC_tied) { |
d0066dc7 | 865 | *needs_store = FALSE; |
4ab2a30b | 866 | return; /* We've set all there is to set. */ |
d0066dc7 OT |
867 | } |
868 | } | |
869 | mg = mg->mg_moremagic; | |
870 | } | |
871 | } | |
872 | ||
954c1994 | 873 | /* |
a3bcc51e TP |
874 | =for apidoc hv_scalar |
875 | ||
876 | Evaluates the hash in scalar context and returns the result. Handles magic when the hash is tied. | |
877 | ||
878 | =cut | |
879 | */ | |
880 | ||
881 | SV * | |
882 | Perl_hv_scalar(pTHX_ HV *hv) | |
883 | { | |
a3bcc51e | 884 | SV *sv; |
823a54a3 AL |
885 | |
886 | if (SvRMAGICAL(hv)) { | |
887 | MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_tied); | |
888 | if (mg) | |
889 | return magic_scalarpack(hv, mg); | |
890 | } | |
a3bcc51e TP |
891 | |
892 | sv = sv_newmortal(); | |
893 | if (HvFILL((HV*)hv)) | |
894 | Perl_sv_setpvf(aTHX_ sv, "%ld/%ld", | |
895 | (long)HvFILL(hv), (long)HvMAX(hv) + 1); | |
896 | else | |
897 | sv_setiv(sv, 0); | |
898 | ||
899 | return sv; | |
900 | } | |
901 | ||
902 | /* | |
954c1994 GS |
903 | =for apidoc hv_delete |
904 | ||
905 | Deletes a key/value pair in the hash. The value SV is removed from the | |
1c846c1f | 906 | hash and returned to the caller. The C<klen> is the length of the key. |
954c1994 GS |
907 | The C<flags> value will normally be zero; if set to G_DISCARD then NULL |
908 | will be returned. | |
909 | ||
910 | =cut | |
911 | */ | |
912 | ||
79072805 | 913 | SV * |
cd6d36ac | 914 | Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 flags) |
79072805 | 915 | { |
cd6d36ac NC |
916 | STRLEN klen; |
917 | int k_flags = 0; | |
918 | ||
919 | if (klen_i32 < 0) { | |
920 | klen = -klen_i32; | |
921 | k_flags |= HVhek_UTF8; | |
922 | } else { | |
923 | klen = klen_i32; | |
924 | } | |
925 | return hv_delete_common(hv, NULL, key, klen, k_flags, flags, 0); | |
fde52b5c | 926 | } |
927 | ||
954c1994 GS |
928 | /* |
929 | =for apidoc hv_delete_ent | |
930 | ||
931 | Deletes a key/value pair in the hash. The value SV is removed from the | |
932 | hash and returned to the caller. The C<flags> value will normally be zero; | |
933 | if set to G_DISCARD then NULL will be returned. C<hash> can be a valid | |
934 | precomputed hash value, or 0 to ask for it to be computed. | |
935 | ||
936 | =cut | |
937 | */ | |
938 | ||
fde52b5c | 939 | SV * |
864dbfa3 | 940 | Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash) |
fde52b5c | 941 | { |
cd6d36ac | 942 | return hv_delete_common(hv, keysv, NULL, 0, 0, flags, hash); |
f1317c8d NC |
943 | } |
944 | ||
8f8d40ab | 945 | STATIC SV * |
cd6d36ac NC |
946 | S_hv_delete_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen, |
947 | int k_flags, I32 d_flags, U32 hash) | |
f1317c8d | 948 | { |
27da23d5 | 949 | dVAR; |
cbec9347 | 950 | register XPVHV* xhv; |
fde52b5c | 951 | register HE *entry; |
952 | register HE **oentry; | |
9e720f71 | 953 | HE *const *first_entry; |
fde52b5c | 954 | SV *sv; |
da58a35d | 955 | bool is_utf8; |
7a9669ca | 956 | int masked_flags; |
1c846c1f | 957 | |
fde52b5c | 958 | if (!hv) |
959 | return Nullsv; | |
f1317c8d NC |
960 | |
961 | if (keysv) { | |
e593d2fe AE |
962 | if (k_flags & HVhek_FREEKEY) |
963 | Safefree(key); | |
5c144d81 | 964 | key = SvPV_const(keysv, klen); |
cd6d36ac | 965 | k_flags = 0; |
f1317c8d NC |
966 | is_utf8 = (SvUTF8(keysv) != 0); |
967 | } else { | |
cd6d36ac | 968 | is_utf8 = ((k_flags & HVhek_UTF8) ? TRUE : FALSE); |
f1317c8d | 969 | } |
f1317c8d | 970 | |
fde52b5c | 971 | if (SvRMAGICAL(hv)) { |
0a0bb7c7 OT |
972 | bool needs_copy; |
973 | bool needs_store; | |
974 | hv_magic_check (hv, &needs_copy, &needs_store); | |
975 | ||
f1317c8d | 976 | if (needs_copy) { |
7a9669ca NC |
977 | entry = hv_fetch_common(hv, keysv, key, klen, |
978 | k_flags & ~HVhek_FREEKEY, HV_FETCH_LVALUE, | |
b2c64049 | 979 | Nullsv, hash); |
7a9669ca | 980 | sv = entry ? HeVAL(entry) : NULL; |
f1317c8d NC |
981 | if (sv) { |
982 | if (SvMAGICAL(sv)) { | |
983 | mg_clear(sv); | |
984 | } | |
985 | if (!needs_store) { | |
986 | if (mg_find(sv, PERL_MAGIC_tiedelem)) { | |
987 | /* No longer an element */ | |
988 | sv_unmagic(sv, PERL_MAGIC_tiedelem); | |
989 | return sv; | |
990 | } | |
991 | return Nullsv; /* element cannot be deleted */ | |
992 | } | |
902173a3 | 993 | #ifdef ENV_IS_CASELESS |
8167a60a NC |
994 | else if (mg_find((SV*)hv, PERL_MAGIC_env)) { |
995 | /* XXX This code isn't UTF8 clean. */ | |
996 | keysv = sv_2mortal(newSVpvn(key,klen)); | |
997 | if (k_flags & HVhek_FREEKEY) { | |
998 | Safefree(key); | |
999 | } | |
1000 | key = strupr(SvPVX(keysv)); | |
1001 | is_utf8 = 0; | |
1002 | k_flags = 0; | |
1003 | hash = 0; | |
7f66fda2 | 1004 | } |
510ac311 | 1005 | #endif |
2fd1c6b8 | 1006 | } |
2fd1c6b8 | 1007 | } |
fde52b5c | 1008 | } |
cbec9347 | 1009 | xhv = (XPVHV*)SvANY(hv); |
7b2c381c | 1010 | if (!HvARRAY(hv)) |
fde52b5c | 1011 | return Nullsv; |
1012 | ||
19692e8d | 1013 | if (is_utf8) { |
b464bac0 AL |
1014 | const char *keysave = key; |
1015 | key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8); | |
cd6d36ac | 1016 | |
19692e8d | 1017 | if (is_utf8) |
cd6d36ac NC |
1018 | k_flags |= HVhek_UTF8; |
1019 | else | |
1020 | k_flags &= ~HVhek_UTF8; | |
7f66fda2 NC |
1021 | if (key != keysave) { |
1022 | if (k_flags & HVhek_FREEKEY) { | |
1023 | /* This shouldn't happen if our caller does what we expect, | |
1024 | but strictly the API allows it. */ | |
1025 | Safefree(keysave); | |
1026 | } | |
1027 | k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY; | |
1028 | } | |
cd6d36ac | 1029 | HvHASKFLAGS_on((SV*)hv); |
19692e8d | 1030 | } |
f9a63242 | 1031 | |
4b5190b5 NC |
1032 | if (HvREHASH(hv)) { |
1033 | PERL_HASH_INTERNAL(hash, key, klen); | |
1034 | } else if (!hash) { | |
7a9669ca | 1035 | if (keysv && (SvIsCOW_shared_hash(keysv))) { |
c158a4fd | 1036 | hash = SvSHARED_HASH(keysv); |
7a9669ca NC |
1037 | } else { |
1038 | PERL_HASH(hash, key, klen); | |
1039 | } | |
4b5190b5 | 1040 | } |
fde52b5c | 1041 | |
7a9669ca NC |
1042 | masked_flags = (k_flags & HVhek_MASK); |
1043 | ||
9e720f71 | 1044 | first_entry = oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; |
fde52b5c | 1045 | entry = *oentry; |
9e720f71 | 1046 | for (; entry; oentry = &HeNEXT(entry), entry = *oentry) { |
fde52b5c | 1047 | if (HeHASH(entry) != hash) /* strings can't be equal */ |
1048 | continue; | |
eb160463 | 1049 | if (HeKLEN(entry) != (I32)klen) |
fde52b5c | 1050 | continue; |
1c846c1f | 1051 | if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */ |
fde52b5c | 1052 | continue; |
7a9669ca | 1053 | if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8) |
c3654f1a | 1054 | continue; |
8aacddc1 | 1055 | |
5d2b1485 NC |
1056 | if (hv == PL_strtab) { |
1057 | if (k_flags & HVhek_FREEKEY) | |
1058 | Safefree(key); | |
1059 | Perl_croak(aTHX_ S_strtab_error, "delete"); | |
1060 | } | |
1061 | ||
8aacddc1 | 1062 | /* if placeholder is here, it's already been deleted.... */ |
7996736c | 1063 | if (HeVAL(entry) == &PL_sv_placeholder) |
8aacddc1 | 1064 | { |
b84d0860 NC |
1065 | if (k_flags & HVhek_FREEKEY) |
1066 | Safefree(key); | |
1067 | return Nullsv; | |
8aacddc1 NIS |
1068 | } |
1069 | else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) { | |
2393f1b9 | 1070 | S_hv_notallowed(aTHX_ k_flags, key, klen, |
c8cd6465 NC |
1071 | "Attempt to delete readonly key '%"SVf"' from" |
1072 | " a restricted hash"); | |
8aacddc1 | 1073 | } |
b84d0860 NC |
1074 | if (k_flags & HVhek_FREEKEY) |
1075 | Safefree(key); | |
8aacddc1 | 1076 | |
cd6d36ac | 1077 | if (d_flags & G_DISCARD) |
fde52b5c | 1078 | sv = Nullsv; |
94f7643d | 1079 | else { |
79d01fbf | 1080 | sv = sv_2mortal(HeVAL(entry)); |
7996736c | 1081 | HeVAL(entry) = &PL_sv_placeholder; |
94f7643d | 1082 | } |
8aacddc1 NIS |
1083 | |
1084 | /* | |
1085 | * If a restricted hash, rather than really deleting the entry, put | |
1086 | * a placeholder there. This marks the key as being "approved", so | |
1087 | * we can still access via not-really-existing key without raising | |
1088 | * an error. | |
1089 | */ | |
1090 | if (SvREADONLY(hv)) { | |
754604c4 | 1091 | SvREFCNT_dec(HeVAL(entry)); |
7996736c | 1092 | HeVAL(entry) = &PL_sv_placeholder; |
8aacddc1 NIS |
1093 | /* We'll be saving this slot, so the number of allocated keys |
1094 | * doesn't go down, but the number placeholders goes up */ | |
ca732855 | 1095 | HvPLACEHOLDERS(hv)++; |
8aacddc1 | 1096 | } else { |
a26e96df | 1097 | *oentry = HeNEXT(entry); |
9e720f71 | 1098 | if(!*first_entry) { |
a26e96df | 1099 | xhv->xhv_fill--; /* HvFILL(hv)-- */ |
9e720f71 | 1100 | } |
b79f7545 | 1101 | if (SvOOK(hv) && entry == HvAUX(hv)->xhv_eiter /* HvEITER(hv) */) |
8aacddc1 NIS |
1102 | HvLAZYDEL_on(hv); |
1103 | else | |
1104 | hv_free_ent(hv, entry); | |
1105 | xhv->xhv_keys--; /* HvKEYS(hv)-- */ | |
574c8022 | 1106 | if (xhv->xhv_keys == 0) |
19692e8d | 1107 | HvHASKFLAGS_off(hv); |
8aacddc1 | 1108 | } |
79072805 LW |
1109 | return sv; |
1110 | } | |
8aacddc1 | 1111 | if (SvREADONLY(hv)) { |
2393f1b9 | 1112 | S_hv_notallowed(aTHX_ k_flags, key, klen, |
c8cd6465 NC |
1113 | "Attempt to delete disallowed key '%"SVf"' from" |
1114 | " a restricted hash"); | |
8aacddc1 NIS |
1115 | } |
1116 | ||
19692e8d | 1117 | if (k_flags & HVhek_FREEKEY) |
f9a63242 | 1118 | Safefree(key); |
79072805 | 1119 | return Nullsv; |
79072805 LW |
1120 | } |
1121 | ||
76e3520e | 1122 | STATIC void |
cea2e8a9 | 1123 | S_hsplit(pTHX_ HV *hv) |
79072805 | 1124 | { |
cbec9347 | 1125 | register XPVHV* xhv = (XPVHV*)SvANY(hv); |
a3b680e6 | 1126 | const I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */ |
79072805 LW |
1127 | register I32 newsize = oldsize * 2; |
1128 | register I32 i; | |
7b2c381c | 1129 | char *a = (char*) HvARRAY(hv); |
72311751 | 1130 | register HE **aep; |
79072805 | 1131 | register HE **oentry; |
4b5190b5 NC |
1132 | int longest_chain = 0; |
1133 | int was_shared; | |
79072805 | 1134 | |
18026298 NC |
1135 | /*PerlIO_printf(PerlIO_stderr(), "hsplit called for %p which had %d\n", |
1136 | hv, (int) oldsize);*/ | |
1137 | ||
5d88ecd7 | 1138 | if (HvPLACEHOLDERS_get(hv) && !SvREADONLY(hv)) { |
18026298 NC |
1139 | /* Can make this clear any placeholders first for non-restricted hashes, |
1140 | even though Storable rebuilds restricted hashes by putting in all the | |
1141 | placeholders (first) before turning on the readonly flag, because | |
1142 | Storable always pre-splits the hash. */ | |
1143 | hv_clear_placeholders(hv); | |
1144 | } | |
1145 | ||
3280af22 | 1146 | PL_nomemok = TRUE; |
8d6dde3e | 1147 | #if defined(STRANGE_MALLOC) || defined(MYMALLOC) |
b79f7545 NC |
1148 | Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize) |
1149 | + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char); | |
422a93e5 | 1150 | if (!a) { |
4a33f861 | 1151 | PL_nomemok = FALSE; |
422a93e5 GA |
1152 | return; |
1153 | } | |
b79f7545 | 1154 | if (SvOOK(hv)) { |
7a9b70e9 | 1155 | Copy(&a[oldsize * sizeof(HE*)], &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux); |
b79f7545 | 1156 | } |
4633a7c4 | 1157 | #else |
a02a5408 | 1158 | Newx(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize) |
b79f7545 | 1159 | + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char); |
422a93e5 | 1160 | if (!a) { |
3280af22 | 1161 | PL_nomemok = FALSE; |
422a93e5 GA |
1162 | return; |
1163 | } | |
7b2c381c | 1164 | Copy(HvARRAY(hv), a, oldsize * sizeof(HE*), char); |
b79f7545 NC |
1165 | if (SvOOK(hv)) { |
1166 | Copy(HvAUX(hv), &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux); | |
1167 | } | |
fba3b22e | 1168 | if (oldsize >= 64) { |
7b2c381c | 1169 | offer_nice_chunk(HvARRAY(hv), |
b79f7545 NC |
1170 | PERL_HV_ARRAY_ALLOC_BYTES(oldsize) |
1171 | + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0)); | |
4633a7c4 LW |
1172 | } |
1173 | else | |
7b2c381c | 1174 | Safefree(HvARRAY(hv)); |
4633a7c4 LW |
1175 | #endif |
1176 | ||
3280af22 | 1177 | PL_nomemok = FALSE; |
72311751 | 1178 | Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/ |
cbec9347 | 1179 | xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */ |
7b2c381c | 1180 | HvARRAY(hv) = (HE**) a; |
72311751 | 1181 | aep = (HE**)a; |
79072805 | 1182 | |
72311751 | 1183 | for (i=0; i<oldsize; i++,aep++) { |
4b5190b5 NC |
1184 | int left_length = 0; |
1185 | int right_length = 0; | |
a3b680e6 AL |
1186 | register HE *entry; |
1187 | register HE **bep; | |
4b5190b5 | 1188 | |
72311751 | 1189 | if (!*aep) /* non-existent */ |
79072805 | 1190 | continue; |
72311751 GS |
1191 | bep = aep+oldsize; |
1192 | for (oentry = aep, entry = *aep; entry; entry = *oentry) { | |
eb160463 | 1193 | if ((HeHASH(entry) & newsize) != (U32)i) { |
fde52b5c | 1194 | *oentry = HeNEXT(entry); |
72311751 GS |
1195 | HeNEXT(entry) = *bep; |
1196 | if (!*bep) | |
cbec9347 | 1197 | xhv->xhv_fill++; /* HvFILL(hv)++ */ |
72311751 | 1198 | *bep = entry; |
4b5190b5 | 1199 | right_length++; |
79072805 LW |
1200 | continue; |
1201 | } | |
4b5190b5 | 1202 | else { |
fde52b5c | 1203 | oentry = &HeNEXT(entry); |
4b5190b5 NC |
1204 | left_length++; |
1205 | } | |
79072805 | 1206 | } |
72311751 | 1207 | if (!*aep) /* everything moved */ |
cbec9347 | 1208 | xhv->xhv_fill--; /* HvFILL(hv)-- */ |
4b5190b5 NC |
1209 | /* I think we don't actually need to keep track of the longest length, |
1210 | merely flag if anything is too long. But for the moment while | |
1211 | developing this code I'll track it. */ | |
1212 | if (left_length > longest_chain) | |
1213 | longest_chain = left_length; | |
1214 | if (right_length > longest_chain) | |
1215 | longest_chain = right_length; | |
1216 | } | |
1217 | ||
1218 | ||
1219 | /* Pick your policy for "hashing isn't working" here: */ | |
fdcd69b6 | 1220 | if (longest_chain <= HV_MAX_LENGTH_BEFORE_SPLIT /* split worked? */ |
4b5190b5 NC |
1221 | || HvREHASH(hv)) { |
1222 | return; | |
79072805 | 1223 | } |
4b5190b5 NC |
1224 | |
1225 | if (hv == PL_strtab) { | |
1226 | /* Urg. Someone is doing something nasty to the string table. | |
1227 | Can't win. */ | |
1228 | return; | |
1229 | } | |
1230 | ||
1231 | /* Awooga. Awooga. Pathological data. */ | |
fdcd69b6 | 1232 | /*PerlIO_printf(PerlIO_stderr(), "%p %d of %d with %d/%d buckets\n", hv, |
4b5190b5 NC |
1233 | longest_chain, HvTOTALKEYS(hv), HvFILL(hv), 1+HvMAX(hv));*/ |
1234 | ||
1235 | ++newsize; | |
a02a5408 | 1236 | Newxz(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize) |
b79f7545 NC |
1237 | + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char); |
1238 | if (SvOOK(hv)) { | |
1239 | Copy(HvAUX(hv), &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux); | |
1240 | } | |
1241 | ||
4b5190b5 NC |
1242 | was_shared = HvSHAREKEYS(hv); |
1243 | ||
1244 | xhv->xhv_fill = 0; | |
1245 | HvSHAREKEYS_off(hv); | |
1246 | HvREHASH_on(hv); | |
1247 | ||
7b2c381c | 1248 | aep = HvARRAY(hv); |
4b5190b5 NC |
1249 | |
1250 | for (i=0; i<newsize; i++,aep++) { | |
a3b680e6 | 1251 | register HE *entry = *aep; |
4b5190b5 NC |
1252 | while (entry) { |
1253 | /* We're going to trash this HE's next pointer when we chain it | |
1254 | into the new hash below, so store where we go next. */ | |
9d4ba2ae | 1255 | HE * const next = HeNEXT(entry); |
4b5190b5 | 1256 | UV hash; |
a3b680e6 | 1257 | HE **bep; |
4b5190b5 NC |
1258 | |
1259 | /* Rehash it */ | |
1260 | PERL_HASH_INTERNAL(hash, HeKEY(entry), HeKLEN(entry)); | |
1261 | ||
1262 | if (was_shared) { | |
1263 | /* Unshare it. */ | |
aec46f14 | 1264 | HEK * const new_hek |
4b5190b5 NC |
1265 | = save_hek_flags(HeKEY(entry), HeKLEN(entry), |
1266 | hash, HeKFLAGS(entry)); | |
1267 | unshare_hek (HeKEY_hek(entry)); | |
1268 | HeKEY_hek(entry) = new_hek; | |
1269 | } else { | |
1270 | /* Not shared, so simply write the new hash in. */ | |
1271 | HeHASH(entry) = hash; | |
1272 | } | |
1273 | /*PerlIO_printf(PerlIO_stderr(), "%d ", HeKFLAGS(entry));*/ | |
1274 | HEK_REHASH_on(HeKEY_hek(entry)); | |
1275 | /*PerlIO_printf(PerlIO_stderr(), "%d\n", HeKFLAGS(entry));*/ | |
1276 | ||
1277 | /* Copy oentry to the correct new chain. */ | |
1278 | bep = ((HE**)a) + (hash & (I32) xhv->xhv_max); | |
1279 | if (!*bep) | |
1280 | xhv->xhv_fill++; /* HvFILL(hv)++ */ | |
1281 | HeNEXT(entry) = *bep; | |
1282 | *bep = entry; | |
1283 | ||
1284 | entry = next; | |
1285 | } | |
1286 | } | |
7b2c381c NC |
1287 | Safefree (HvARRAY(hv)); |
1288 | HvARRAY(hv) = (HE **)a; | |
79072805 LW |
1289 | } |
1290 | ||
72940dca | 1291 | void |
864dbfa3 | 1292 | Perl_hv_ksplit(pTHX_ HV *hv, IV newmax) |
72940dca | 1293 | { |
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) { |
a3b680e6 | 1362 | register I32 j; |
72940dca | 1363 | if ((j = (HeHASH(entry) & newsize)) != i) { |
1364 | j -= i; | |
1365 | *oentry = HeNEXT(entry); | |
72311751 | 1366 | if (!(HeNEXT(entry) = aep[j])) |
cbec9347 | 1367 | xhv->xhv_fill++; /* HvFILL(hv)++ */ |
72311751 | 1368 | aep[j] = entry; |
72940dca | 1369 | continue; |
1370 | } | |
1371 | else | |
1372 | oentry = &HeNEXT(entry); | |
1373 | } | |
72311751 | 1374 | if (!*aep) /* everything moved */ |
cbec9347 | 1375 | xhv->xhv_fill--; /* HvFILL(hv)-- */ |
72940dca | 1376 | } |
1377 | } | |
1378 | ||
954c1994 GS |
1379 | /* |
1380 | =for apidoc newHV | |
1381 | ||
1382 | Creates a new HV. The reference count is set to 1. | |
1383 | ||
1384 | =cut | |
1385 | */ | |
1386 | ||
79072805 | 1387 | HV * |
864dbfa3 | 1388 | Perl_newHV(pTHX) |
79072805 | 1389 | { |
cbec9347 | 1390 | register XPVHV* xhv; |
9d4ba2ae | 1391 | HV * const hv = (HV*)NEWSV(502,0); |
79072805 | 1392 | |
a0d0e21e | 1393 | sv_upgrade((SV *)hv, SVt_PVHV); |
cbec9347 | 1394 | xhv = (XPVHV*)SvANY(hv); |
79072805 LW |
1395 | SvPOK_off(hv); |
1396 | SvNOK_off(hv); | |
1c846c1f | 1397 | #ifndef NODEFAULT_SHAREKEYS |
fde52b5c | 1398 | HvSHAREKEYS_on(hv); /* key-sharing on by default */ |
1c846c1f | 1399 | #endif |
4b5190b5 | 1400 | |
cbec9347 JH |
1401 | xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */ |
1402 | xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */ | |
79072805 LW |
1403 | return hv; |
1404 | } | |
1405 | ||
b3ac6de7 | 1406 | HV * |
864dbfa3 | 1407 | Perl_newHVhv(pTHX_ HV *ohv) |
b3ac6de7 | 1408 | { |
9d4ba2ae | 1409 | HV * const hv = newHV(); |
4beac62f | 1410 | STRLEN hv_max, hv_fill; |
4beac62f AMS |
1411 | |
1412 | if (!ohv || (hv_fill = HvFILL(ohv)) == 0) | |
1413 | return hv; | |
4beac62f | 1414 | hv_max = HvMAX(ohv); |
b3ac6de7 | 1415 | |
b56ba0bf AMS |
1416 | if (!SvMAGICAL((SV *)ohv)) { |
1417 | /* It's an ordinary hash, so copy it fast. AMS 20010804 */ | |
eb160463 | 1418 | STRLEN i; |
a3b680e6 | 1419 | const bool shared = !!HvSHAREKEYS(ohv); |
aec46f14 | 1420 | HE **ents, ** const oents = (HE **)HvARRAY(ohv); |
ff875642 | 1421 | char *a; |
a02a5408 | 1422 | Newx(a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char); |
ff875642 | 1423 | ents = (HE**)a; |
b56ba0bf AMS |
1424 | |
1425 | /* In each bucket... */ | |
1426 | for (i = 0; i <= hv_max; i++) { | |
aec46f14 AL |
1427 | HE *prev = NULL, *ent = NULL; |
1428 | HE *oent = oents[i]; | |
b56ba0bf AMS |
1429 | |
1430 | if (!oent) { | |
1431 | ents[i] = NULL; | |
1432 | continue; | |
1433 | } | |
1434 | ||
1435 | /* Copy the linked list of entries. */ | |
aec46f14 | 1436 | for (; oent; oent = HeNEXT(oent)) { |
a3b680e6 AL |
1437 | const U32 hash = HeHASH(oent); |
1438 | const char * const key = HeKEY(oent); | |
1439 | const STRLEN len = HeKLEN(oent); | |
1440 | const int flags = HeKFLAGS(oent); | |
b56ba0bf AMS |
1441 | |
1442 | ent = new_HE(); | |
45dea987 | 1443 | HeVAL(ent) = newSVsv(HeVAL(oent)); |
19692e8d | 1444 | HeKEY_hek(ent) |
6e838c70 | 1445 | = shared ? share_hek_flags(key, len, hash, flags) |
19692e8d | 1446 | : save_hek_flags(key, len, hash, flags); |
b56ba0bf AMS |
1447 | if (prev) |
1448 | HeNEXT(prev) = ent; | |
1449 | else | |
1450 | ents[i] = ent; | |
1451 | prev = ent; | |
1452 | HeNEXT(ent) = NULL; | |
1453 | } | |
1454 | } | |
1455 | ||
1456 | HvMAX(hv) = hv_max; | |
1457 | HvFILL(hv) = hv_fill; | |
8aacddc1 | 1458 | HvTOTALKEYS(hv) = HvTOTALKEYS(ohv); |
b56ba0bf | 1459 | HvARRAY(hv) = ents; |
aec46f14 | 1460 | } /* not magical */ |
b56ba0bf AMS |
1461 | else { |
1462 | /* Iterate over ohv, copying keys and values one at a time. */ | |
b3ac6de7 | 1463 | HE *entry; |
bfcb3514 NC |
1464 | const I32 riter = HvRITER_get(ohv); |
1465 | HE * const eiter = HvEITER_get(ohv); | |
b56ba0bf AMS |
1466 | |
1467 | /* Can we use fewer buckets? (hv_max is always 2^n-1) */ | |
1468 | while (hv_max && hv_max + 1 >= hv_fill * 2) | |
1469 | hv_max = hv_max / 2; | |
1470 | HvMAX(hv) = hv_max; | |
1471 | ||
4a76a316 | 1472 | hv_iterinit(ohv); |
e16e2ff8 | 1473 | while ((entry = hv_iternext_flags(ohv, 0))) { |
19692e8d NC |
1474 | hv_store_flags(hv, HeKEY(entry), HeKLEN(entry), |
1475 | newSVsv(HeVAL(entry)), HeHASH(entry), | |
1476 | HeKFLAGS(entry)); | |
b3ac6de7 | 1477 | } |
bfcb3514 NC |
1478 | HvRITER_set(ohv, riter); |
1479 | HvEITER_set(ohv, eiter); | |
b3ac6de7 | 1480 | } |
1c846c1f | 1481 | |
b3ac6de7 IZ |
1482 | return hv; |
1483 | } | |
1484 | ||
79072805 | 1485 | void |
864dbfa3 | 1486 | Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry) |
79072805 | 1487 | { |
16bdeea2 GS |
1488 | SV *val; |
1489 | ||
68dc0745 | 1490 | if (!entry) |
79072805 | 1491 | return; |
16bdeea2 | 1492 | val = HeVAL(entry); |
bfcb3514 | 1493 | if (val && isGV(val) && GvCVu(val) && HvNAME_get(hv)) |
3280af22 | 1494 | PL_sub_generation++; /* may be deletion of method from stash */ |
16bdeea2 | 1495 | SvREFCNT_dec(val); |
68dc0745 | 1496 | if (HeKLEN(entry) == HEf_SVKEY) { |
1497 | SvREFCNT_dec(HeKEY_sv(entry)); | |
8aacddc1 | 1498 | Safefree(HeKEY_hek(entry)); |
44a8e56a | 1499 | } |
1500 | else if (HvSHAREKEYS(hv)) | |
68dc0745 | 1501 | unshare_hek(HeKEY_hek(entry)); |
fde52b5c | 1502 | else |
68dc0745 | 1503 | Safefree(HeKEY_hek(entry)); |
d33b2eba | 1504 | del_HE(entry); |
79072805 LW |
1505 | } |
1506 | ||
1507 | void | |
864dbfa3 | 1508 | Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry) |
79072805 | 1509 | { |
68dc0745 | 1510 | if (!entry) |
79072805 | 1511 | return; |
bc4947fc NC |
1512 | /* SvREFCNT_inc to counter the SvREFCNT_dec in hv_free_ent */ |
1513 | sv_2mortal(SvREFCNT_inc(HeVAL(entry))); /* free between statements */ | |
68dc0745 | 1514 | if (HeKLEN(entry) == HEf_SVKEY) { |
bc4947fc | 1515 | sv_2mortal(SvREFCNT_inc(HeKEY_sv(entry))); |
44a8e56a | 1516 | } |
bc4947fc | 1517 | hv_free_ent(hv, entry); |
79072805 LW |
1518 | } |
1519 | ||
954c1994 GS |
1520 | /* |
1521 | =for apidoc hv_clear | |
1522 | ||
1523 | Clears a hash, making it empty. | |
1524 | ||
1525 | =cut | |
1526 | */ | |
1527 | ||
79072805 | 1528 | void |
864dbfa3 | 1529 | Perl_hv_clear(pTHX_ HV *hv) |
79072805 | 1530 | { |
27da23d5 | 1531 | dVAR; |
cbec9347 | 1532 | register XPVHV* xhv; |
79072805 LW |
1533 | if (!hv) |
1534 | return; | |
49293501 | 1535 | |
ecae49c0 NC |
1536 | DEBUG_A(Perl_hv_assert(aTHX_ hv)); |
1537 | ||
34c3c4e3 DM |
1538 | xhv = (XPVHV*)SvANY(hv); |
1539 | ||
7b2c381c | 1540 | if (SvREADONLY(hv) && HvARRAY(hv) != NULL) { |
34c3c4e3 | 1541 | /* restricted hash: convert all keys to placeholders */ |
b464bac0 AL |
1542 | STRLEN i; |
1543 | for (i = 0; i <= xhv->xhv_max; i++) { | |
7b2c381c | 1544 | HE *entry = (HvARRAY(hv))[i]; |
3a676441 JH |
1545 | for (; entry; entry = HeNEXT(entry)) { |
1546 | /* not already placeholder */ | |
7996736c | 1547 | if (HeVAL(entry) != &PL_sv_placeholder) { |
3a676441 JH |
1548 | if (HeVAL(entry) && SvREADONLY(HeVAL(entry))) { |
1549 | SV* keysv = hv_iterkeysv(entry); | |
1550 | Perl_croak(aTHX_ | |
1551 | "Attempt to delete readonly key '%"SVf"' from a restricted hash", | |
1552 | keysv); | |
1553 | } | |
1554 | SvREFCNT_dec(HeVAL(entry)); | |
7996736c | 1555 | HeVAL(entry) = &PL_sv_placeholder; |
ca732855 | 1556 | HvPLACEHOLDERS(hv)++; |
3a676441 | 1557 | } |
34c3c4e3 DM |
1558 | } |
1559 | } | |
df8c6964 | 1560 | goto reset; |
49293501 MS |
1561 | } |
1562 | ||
463ee0b2 | 1563 | hfreeentries(hv); |
ca732855 | 1564 | HvPLACEHOLDERS_set(hv, 0); |
7b2c381c NC |
1565 | if (HvARRAY(hv)) |
1566 | (void)memzero(HvARRAY(hv), | |
cbec9347 | 1567 | (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*)); |
a0d0e21e LW |
1568 | |
1569 | if (SvRMAGICAL(hv)) | |
1c846c1f | 1570 | mg_clear((SV*)hv); |
574c8022 | 1571 | |
19692e8d | 1572 | HvHASKFLAGS_off(hv); |
bb443f97 | 1573 | HvREHASH_off(hv); |
df8c6964 | 1574 | reset: |
b79f7545 | 1575 | if (SvOOK(hv)) { |
bfcb3514 NC |
1576 | HvEITER_set(hv, NULL); |
1577 | } | |
79072805 LW |
1578 | } |
1579 | ||
3540d4ce AB |
1580 | /* |
1581 | =for apidoc hv_clear_placeholders | |
1582 | ||
1583 | Clears any placeholders from a hash. If a restricted hash has any of its keys | |
1584 | marked as readonly and the key is subsequently deleted, the key is not actually | |
1585 | deleted but is marked by assigning it a value of &PL_sv_placeholder. This tags | |
1586 | it so it will be ignored by future operations such as iterating over the hash, | |
4cdaeff7 | 1587 | but will still allow the hash to have a value reassigned to the key at some |
3540d4ce AB |
1588 | future point. This function clears any such placeholder keys from the hash. |
1589 | See Hash::Util::lock_keys() for an example of its use. | |
1590 | ||
1591 | =cut | |
1592 | */ | |
1593 | ||
1594 | void | |
1595 | Perl_hv_clear_placeholders(pTHX_ HV *hv) | |
1596 | { | |
27da23d5 | 1597 | dVAR; |
5d88ecd7 | 1598 | I32 items = (I32)HvPLACEHOLDERS_get(hv); |
b464bac0 | 1599 | I32 i; |
d3677389 NC |
1600 | |
1601 | if (items == 0) | |
1602 | return; | |
1603 | ||
b464bac0 | 1604 | i = HvMAX(hv); |
d3677389 NC |
1605 | do { |
1606 | /* Loop down the linked list heads */ | |
a3b680e6 | 1607 | bool first = 1; |
d3677389 NC |
1608 | HE **oentry = &(HvARRAY(hv))[i]; |
1609 | HE *entry = *oentry; | |
1610 | ||
1611 | if (!entry) | |
1612 | continue; | |
1613 | ||
213ce8b3 | 1614 | for (; entry; entry = *oentry) { |
d3677389 NC |
1615 | if (HeVAL(entry) == &PL_sv_placeholder) { |
1616 | *oentry = HeNEXT(entry); | |
1617 | if (first && !*oentry) | |
1618 | HvFILL(hv)--; /* This linked list is now empty. */ | |
bfcb3514 | 1619 | if (HvEITER_get(hv)) |
d3677389 NC |
1620 | HvLAZYDEL_on(hv); |
1621 | else | |
1622 | hv_free_ent(hv, entry); | |
1623 | ||
1624 | if (--items == 0) { | |
1625 | /* Finished. */ | |
5d88ecd7 | 1626 | HvTOTALKEYS(hv) -= (IV)HvPLACEHOLDERS_get(hv); |
d3677389 NC |
1627 | if (HvKEYS(hv) == 0) |
1628 | HvHASKFLAGS_off(hv); | |
5d88ecd7 | 1629 | HvPLACEHOLDERS_set(hv, 0); |
d3677389 NC |
1630 | return; |
1631 | } | |
213ce8b3 NC |
1632 | } else { |
1633 | oentry = &HeNEXT(entry); | |
1634 | first = 0; | |
d3677389 NC |
1635 | } |
1636 | } | |
1637 | } while (--i >= 0); | |
1638 | /* You can't get here, hence assertion should always fail. */ | |
1639 | assert (items == 0); | |
1640 | assert (0); | |
3540d4ce AB |
1641 | } |
1642 | ||
76e3520e | 1643 | STATIC void |
cea2e8a9 | 1644 | S_hfreeentries(pTHX_ HV *hv) |
79072805 | 1645 | { |
a0d0e21e | 1646 | register HE **array; |
68dc0745 | 1647 | register HE *entry; |
a0d0e21e LW |
1648 | I32 riter; |
1649 | I32 max; | |
bfcb3514 | 1650 | struct xpvhv_aux *iter; |
3abe233e | 1651 | |
a0d0e21e | 1652 | if (!HvARRAY(hv)) |
79072805 | 1653 | return; |
a0d0e21e | 1654 | |
b79f7545 NC |
1655 | iter = SvOOK(hv) ? HvAUX(hv) : 0; |
1656 | ||
a0d0e21e LW |
1657 | riter = 0; |
1658 | max = HvMAX(hv); | |
1659 | array = HvARRAY(hv); | |
2f86008e DM |
1660 | /* make everyone else think the array is empty, so that the destructors |
1661 | * called for freed entries can't recusively mess with us */ | |
1662 | HvARRAY(hv) = Null(HE**); | |
b79f7545 NC |
1663 | SvFLAGS(hv) &= ~SVf_OOK; |
1664 | ||
2f86008e DM |
1665 | HvFILL(hv) = 0; |
1666 | ((XPVHV*) SvANY(hv))->xhv_keys = 0; | |
1667 | ||
68dc0745 | 1668 | entry = array[0]; |
a0d0e21e | 1669 | for (;;) { |
68dc0745 | 1670 | if (entry) { |
890ce7af | 1671 | register HE * const oentry = entry; |
68dc0745 | 1672 | entry = HeNEXT(entry); |
1673 | hv_free_ent(hv, oentry); | |
a0d0e21e | 1674 | } |
68dc0745 | 1675 | if (!entry) { |
a0d0e21e LW |
1676 | if (++riter > max) |
1677 | break; | |
68dc0745 | 1678 | entry = array[riter]; |
1c846c1f | 1679 | } |
79072805 | 1680 | } |
bfcb3514 | 1681 | |
b79f7545 NC |
1682 | if (SvOOK(hv)) { |
1683 | /* Someone attempted to iterate or set the hash name while we had | |
1684 | the array set to 0. */ | |
1685 | assert(HvARRAY(hv)); | |
1686 | ||
1687 | if (HvAUX(hv)->xhv_name) | |
1688 | unshare_hek_or_pvn(HvAUX(hv)->xhv_name, 0, 0, 0); | |
1689 | /* SvOOK_off calls sv_backoff, which isn't correct. */ | |
1690 | ||
1691 | Safefree(HvARRAY(hv)); | |
1692 | HvARRAY(hv) = 0; | |
1693 | SvFLAGS(hv) &= ~SVf_OOK; | |
1694 | } | |
1695 | ||
1696 | /* FIXME - things will still go horribly wrong (or at least leak) if | |
1697 | people attempt to add elements to the hash while we're undef()ing it */ | |
bfcb3514 NC |
1698 | if (iter) { |
1699 | entry = iter->xhv_eiter; /* HvEITER(hv) */ | |
1700 | if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */ | |
1701 | HvLAZYDEL_off(hv); | |
1702 | hv_free_ent(hv, entry); | |
1703 | } | |
b79f7545 NC |
1704 | iter->xhv_riter = -1; /* HvRITER(hv) = -1 */ |
1705 | iter->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */ | |
1706 | SvFLAGS(hv) |= SVf_OOK; | |
bfcb3514 | 1707 | } |
b79f7545 NC |
1708 | |
1709 | HvARRAY(hv) = array; | |
79072805 LW |
1710 | } |
1711 | ||
954c1994 GS |
1712 | /* |
1713 | =for apidoc hv_undef | |
1714 | ||
1715 | Undefines the hash. | |
1716 | ||
1717 | =cut | |
1718 | */ | |
1719 | ||
79072805 | 1720 | void |
864dbfa3 | 1721 | Perl_hv_undef(pTHX_ HV *hv) |
79072805 | 1722 | { |
cbec9347 | 1723 | register XPVHV* xhv; |
bfcb3514 | 1724 | const char *name; |
79072805 LW |
1725 | if (!hv) |
1726 | return; | |
ecae49c0 | 1727 | DEBUG_A(Perl_hv_assert(aTHX_ hv)); |
cbec9347 | 1728 | xhv = (XPVHV*)SvANY(hv); |
463ee0b2 | 1729 | hfreeentries(hv); |
bfcb3514 | 1730 | if ((name = HvNAME_get(hv))) { |
7e8961ec | 1731 | if(PL_stashcache) |
7423f6db | 1732 | hv_delete(PL_stashcache, name, HvNAMELEN_get(hv), G_DISCARD); |
51a37f80 | 1733 | hv_name_set(hv, Nullch, 0, 0); |
85e6fe83 | 1734 | } |
b79f7545 NC |
1735 | SvFLAGS(hv) &= ~SVf_OOK; |
1736 | Safefree(HvARRAY(hv)); | |
cbec9347 | 1737 | xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */ |
7b2c381c | 1738 | HvARRAY(hv) = 0; |
ca732855 | 1739 | HvPLACEHOLDERS_set(hv, 0); |
a0d0e21e LW |
1740 | |
1741 | if (SvRMAGICAL(hv)) | |
1c846c1f | 1742 | mg_clear((SV*)hv); |
79072805 LW |
1743 | } |
1744 | ||
b464bac0 | 1745 | static struct xpvhv_aux* |
b79f7545 | 1746 | S_hv_auxinit(pTHX_ HV *hv) { |
bfcb3514 | 1747 | struct xpvhv_aux *iter; |
b79f7545 | 1748 | char *array; |
bfcb3514 | 1749 | |
b79f7545 | 1750 | if (!HvARRAY(hv)) { |
a02a5408 | 1751 | Newxz(array, PERL_HV_ARRAY_ALLOC_BYTES(HvMAX(hv) + 1) |
b79f7545 NC |
1752 | + sizeof(struct xpvhv_aux), char); |
1753 | } else { | |
1754 | array = (char *) HvARRAY(hv); | |
1755 | Renew(array, PERL_HV_ARRAY_ALLOC_BYTES(HvMAX(hv) + 1) | |
1756 | + sizeof(struct xpvhv_aux), char); | |
1757 | } | |
1758 | HvARRAY(hv) = (HE**) array; | |
1759 | /* SvOOK_on(hv) attacks the IV flags. */ | |
1760 | SvFLAGS(hv) |= SVf_OOK; | |
1761 | iter = HvAUX(hv); | |
bfcb3514 NC |
1762 | |
1763 | iter->xhv_riter = -1; /* HvRITER(hv) = -1 */ | |
1764 | iter->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */ | |
1765 | iter->xhv_name = 0; | |
1766 | ||
1767 | return iter; | |
1768 | } | |
1769 | ||
954c1994 GS |
1770 | /* |
1771 | =for apidoc hv_iterinit | |
1772 | ||
1773 | Prepares a starting point to traverse a hash table. Returns the number of | |
1774 | keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is | |
1c846c1f | 1775 | currently only meaningful for hashes without tie magic. |
954c1994 GS |
1776 | |
1777 | NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of | |
1778 | hash buckets that happen to be in use. If you still need that esoteric | |
1779 | value, you can get it through the macro C<HvFILL(tb)>. | |
1780 | ||
e16e2ff8 | 1781 | |
954c1994 GS |
1782 | =cut |
1783 | */ | |
1784 | ||
79072805 | 1785 | I32 |
864dbfa3 | 1786 | Perl_hv_iterinit(pTHX_ HV *hv) |
79072805 | 1787 | { |
aa689395 | 1788 | HE *entry; |
1789 | ||
1790 | if (!hv) | |
cea2e8a9 | 1791 | Perl_croak(aTHX_ "Bad hash"); |
bfcb3514 | 1792 | |
b79f7545 NC |
1793 | if (SvOOK(hv)) { |
1794 | struct xpvhv_aux *iter = HvAUX(hv); | |
bfcb3514 NC |
1795 | entry = iter->xhv_eiter; /* HvEITER(hv) */ |
1796 | if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */ | |
1797 | HvLAZYDEL_off(hv); | |
1798 | hv_free_ent(hv, entry); | |
1799 | } | |
1800 | iter->xhv_riter = -1; /* HvRITER(hv) = -1 */ | |
1801 | iter->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */ | |
1802 | } else { | |
b79f7545 | 1803 | S_hv_auxinit(aTHX_ hv); |
72940dca | 1804 | } |
bfcb3514 | 1805 | |
cbec9347 | 1806 | /* used to be xhv->xhv_fill before 5.004_65 */ |
5d88ecd7 | 1807 | return HvTOTALKEYS(hv); |
79072805 | 1808 | } |
bfcb3514 NC |
1809 | |
1810 | I32 * | |
1811 | Perl_hv_riter_p(pTHX_ HV *hv) { | |
1812 | struct xpvhv_aux *iter; | |
1813 | ||
1814 | if (!hv) | |
1815 | Perl_croak(aTHX_ "Bad hash"); | |
1816 | ||
b79f7545 | 1817 | iter = SvOOK(hv) ? HvAUX(hv) : S_hv_auxinit(aTHX_ hv); |
bfcb3514 NC |
1818 | return &(iter->xhv_riter); |
1819 | } | |
1820 | ||
1821 | HE ** | |
1822 | Perl_hv_eiter_p(pTHX_ HV *hv) { | |
1823 | struct xpvhv_aux *iter; | |
1824 | ||
1825 | if (!hv) | |
1826 | Perl_croak(aTHX_ "Bad hash"); | |
1827 | ||
b79f7545 | 1828 | iter = SvOOK(hv) ? HvAUX(hv) : S_hv_auxinit(aTHX_ hv); |
bfcb3514 NC |
1829 | return &(iter->xhv_eiter); |
1830 | } | |
1831 | ||
1832 | void | |
1833 | Perl_hv_riter_set(pTHX_ HV *hv, I32 riter) { | |
1834 | struct xpvhv_aux *iter; | |
1835 | ||
1836 | if (!hv) | |
1837 | Perl_croak(aTHX_ "Bad hash"); | |
1838 | ||
b79f7545 NC |
1839 | if (SvOOK(hv)) { |
1840 | iter = HvAUX(hv); | |
1841 | } else { | |
bfcb3514 NC |
1842 | if (riter == -1) |
1843 | return; | |
1844 | ||
b79f7545 | 1845 | iter = S_hv_auxinit(aTHX_ hv); |
bfcb3514 NC |
1846 | } |
1847 | iter->xhv_riter = riter; | |
1848 | } | |
1849 | ||
1850 | void | |
1851 | Perl_hv_eiter_set(pTHX_ HV *hv, HE *eiter) { | |
1852 | struct xpvhv_aux *iter; | |
1853 | ||
1854 | if (!hv) | |
1855 | Perl_croak(aTHX_ "Bad hash"); | |
1856 | ||
b79f7545 NC |
1857 | if (SvOOK(hv)) { |
1858 | iter = HvAUX(hv); | |
1859 | } else { | |
bfcb3514 NC |
1860 | /* 0 is the default so don't go malloc()ing a new structure just to |
1861 | hold 0. */ | |
1862 | if (!eiter) | |
1863 | return; | |
1864 | ||
b79f7545 | 1865 | iter = S_hv_auxinit(aTHX_ hv); |
bfcb3514 NC |
1866 | } |
1867 | iter->xhv_eiter = eiter; | |
1868 | } | |
1869 | ||
bfcb3514 | 1870 | void |
7423f6db | 1871 | Perl_hv_name_set(pTHX_ HV *hv, const char *name, I32 len, int flags) |
bfcb3514 | 1872 | { |
b79f7545 | 1873 | struct xpvhv_aux *iter; |
7423f6db | 1874 | U32 hash; |
46c461b5 AL |
1875 | |
1876 | PERL_UNUSED_ARG(flags); | |
bfcb3514 | 1877 | |
b79f7545 NC |
1878 | if (SvOOK(hv)) { |
1879 | iter = HvAUX(hv); | |
7423f6db NC |
1880 | if (iter->xhv_name) { |
1881 | unshare_hek_or_pvn(iter->xhv_name, 0, 0, 0); | |
1882 | } | |
16580ff5 | 1883 | } else { |
bfcb3514 NC |
1884 | if (name == 0) |
1885 | return; | |
1886 | ||
b79f7545 | 1887 | iter = S_hv_auxinit(aTHX_ hv); |
bfcb3514 | 1888 | } |
7423f6db NC |
1889 | PERL_HASH(hash, name, len); |
1890 | iter->xhv_name = name ? share_hek(name, len, hash) : 0; | |
bfcb3514 NC |
1891 | } |
1892 | ||
954c1994 | 1893 | /* |
7a7b9979 NC |
1894 | hv_iternext is implemented as a macro in hv.h |
1895 | ||
954c1994 GS |
1896 | =for apidoc hv_iternext |
1897 | ||
1898 | Returns entries from a hash iterator. See C<hv_iterinit>. | |
1899 | ||
fe7bca90 NC |
1900 | You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the |
1901 | iterator currently points to, without losing your place or invalidating your | |
1902 | iterator. Note that in this case the current entry is deleted from the hash | |
1903 | with your iterator holding the last reference to it. Your iterator is flagged | |
1904 | to free the entry on the next call to C<hv_iternext>, so you must not discard | |
1905 | your iterator immediately else the entry will leak - call C<hv_iternext> to | |
1906 | trigger the resource deallocation. | |
1907 | ||
fe7bca90 NC |
1908 | =for apidoc hv_iternext_flags |
1909 | ||
1910 | Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>. | |
1911 | The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is | |
1912 | set the placeholders keys (for restricted hashes) will be returned in addition | |
1913 | to normal keys. By default placeholders are automatically skipped over. | |
7996736c MHM |
1914 | Currently a placeholder is implemented with a value that is |
1915 | C<&Perl_sv_placeholder>. Note that the implementation of placeholders and | |
fe7bca90 NC |
1916 | restricted hashes may change, and the implementation currently is |
1917 | insufficiently abstracted for any change to be tidy. | |
e16e2ff8 | 1918 | |
fe7bca90 | 1919 | =cut |
e16e2ff8 NC |
1920 | */ |
1921 | ||
1922 | HE * | |
1923 | Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags) | |
1924 | { | |
27da23d5 | 1925 | dVAR; |
cbec9347 | 1926 | register XPVHV* xhv; |
79072805 | 1927 | register HE *entry; |
a0d0e21e | 1928 | HE *oldentry; |
463ee0b2 | 1929 | MAGIC* mg; |
bfcb3514 | 1930 | struct xpvhv_aux *iter; |
79072805 LW |
1931 | |
1932 | if (!hv) | |
cea2e8a9 | 1933 | Perl_croak(aTHX_ "Bad hash"); |
cbec9347 | 1934 | xhv = (XPVHV*)SvANY(hv); |
bfcb3514 | 1935 | |
b79f7545 | 1936 | if (!SvOOK(hv)) { |
bfcb3514 NC |
1937 | /* Too many things (well, pp_each at least) merrily assume that you can |
1938 | call iv_iternext without calling hv_iterinit, so we'll have to deal | |
1939 | with it. */ | |
1940 | hv_iterinit(hv); | |
bfcb3514 | 1941 | } |
b79f7545 | 1942 | iter = HvAUX(hv); |
bfcb3514 NC |
1943 | |
1944 | oldentry = entry = iter->xhv_eiter; /* HvEITER(hv) */ | |
463ee0b2 | 1945 | |
14befaf4 | 1946 | if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) { |
c4420975 | 1947 | SV * const key = sv_newmortal(); |
cd1469e6 | 1948 | if (entry) { |
fde52b5c | 1949 | sv_setsv(key, HeSVKEY_force(entry)); |
cd1469e6 | 1950 | SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */ |
1951 | } | |
a0d0e21e | 1952 | else { |
ff68c719 | 1953 | char *k; |
bbce6d69 | 1954 | HEK *hek; |
ff68c719 | 1955 | |
cbec9347 | 1956 | /* one HE per MAGICAL hash */ |
bfcb3514 | 1957 | iter->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */ |
4633a7c4 | 1958 | Zero(entry, 1, HE); |
a02a5408 | 1959 | Newxz(k, HEK_BASESIZE + sizeof(SV*), char); |
ff68c719 | 1960 | hek = (HEK*)k; |
1961 | HeKEY_hek(entry) = hek; | |
fde52b5c | 1962 | HeKLEN(entry) = HEf_SVKEY; |
a0d0e21e LW |
1963 | } |
1964 | magic_nextpack((SV*) hv,mg,key); | |
8aacddc1 | 1965 | if (SvOK(key)) { |
cd1469e6 | 1966 | /* force key to stay around until next time */ |
bbce6d69 | 1967 | HeSVKEY_set(entry, SvREFCNT_inc(key)); |
1968 | return entry; /* beware, hent_val is not set */ | |
8aacddc1 | 1969 | } |
fde52b5c | 1970 | if (HeVAL(entry)) |
1971 | SvREFCNT_dec(HeVAL(entry)); | |
ff68c719 | 1972 | Safefree(HeKEY_hek(entry)); |
d33b2eba | 1973 | del_HE(entry); |
bfcb3514 | 1974 | iter->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */ |
463ee0b2 | 1975 | return Null(HE*); |
79072805 | 1976 | } |
f675dbe5 | 1977 | #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */ |
03026e68 | 1978 | if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) { |
f675dbe5 | 1979 | prime_env_iter(); |
03026e68 JM |
1980 | #ifdef VMS |
1981 | /* The prime_env_iter() on VMS just loaded up new hash values | |
1982 | * so the iteration count needs to be reset back to the beginning | |
1983 | */ | |
1984 | hv_iterinit(hv); | |
1985 | iter = HvAUX(hv); | |
1986 | oldentry = entry = iter->xhv_eiter; /* HvEITER(hv) */ | |
1987 | #endif | |
1988 | } | |
f675dbe5 | 1989 | #endif |
463ee0b2 | 1990 | |
b79f7545 NC |
1991 | /* hv_iterint now ensures this. */ |
1992 | assert (HvARRAY(hv)); | |
1993 | ||
015a5f36 | 1994 | /* At start of hash, entry is NULL. */ |
fde52b5c | 1995 | if (entry) |
8aacddc1 | 1996 | { |
fde52b5c | 1997 | entry = HeNEXT(entry); |
e16e2ff8 NC |
1998 | if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) { |
1999 | /* | |
2000 | * Skip past any placeholders -- don't want to include them in | |
2001 | * any iteration. | |
2002 | */ | |
7996736c | 2003 | while (entry && HeVAL(entry) == &PL_sv_placeholder) { |
e16e2ff8 NC |
2004 | entry = HeNEXT(entry); |
2005 | } | |
8aacddc1 NIS |
2006 | } |
2007 | } | |
fde52b5c | 2008 | while (!entry) { |
015a5f36 NC |
2009 | /* OK. Come to the end of the current list. Grab the next one. */ |
2010 | ||
bfcb3514 NC |
2011 | iter->xhv_riter++; /* HvRITER(hv)++ */ |
2012 | if (iter->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) { | |
015a5f36 | 2013 | /* There is no next one. End of the hash. */ |
bfcb3514 | 2014 | iter->xhv_riter = -1; /* HvRITER(hv) = -1 */ |
fde52b5c | 2015 | break; |
79072805 | 2016 | } |
7b2c381c | 2017 | entry = (HvARRAY(hv))[iter->xhv_riter]; |
8aacddc1 | 2018 | |
e16e2ff8 | 2019 | if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) { |
015a5f36 NC |
2020 | /* If we have an entry, but it's a placeholder, don't count it. |
2021 | Try the next. */ | |
7996736c | 2022 | while (entry && HeVAL(entry) == &PL_sv_placeholder) |
015a5f36 NC |
2023 | entry = HeNEXT(entry); |
2024 | } | |
2025 | /* Will loop again if this linked list starts NULL | |
2026 | (for HV_ITERNEXT_WANTPLACEHOLDERS) | |
2027 | or if we run through it and find only placeholders. */ | |
fde52b5c | 2028 | } |
79072805 | 2029 | |
72940dca | 2030 | if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */ |
2031 | HvLAZYDEL_off(hv); | |
68dc0745 | 2032 | hv_free_ent(hv, oldentry); |
72940dca | 2033 | } |
a0d0e21e | 2034 | |
fdcd69b6 NC |
2035 | /*if (HvREHASH(hv) && entry && !HeKREHASH(entry)) |
2036 | PerlIO_printf(PerlIO_stderr(), "Awooga %p %p\n", hv, entry);*/ | |
2037 | ||
bfcb3514 | 2038 | iter->xhv_eiter = entry; /* HvEITER(hv) = entry */ |
79072805 LW |
2039 | return entry; |
2040 | } | |
2041 | ||
954c1994 GS |
2042 | /* |
2043 | =for apidoc hv_iterkey | |
2044 | ||
2045 | Returns the key from the current position of the hash iterator. See | |
2046 | C<hv_iterinit>. | |
2047 | ||
2048 | =cut | |
2049 | */ | |
2050 | ||
79072805 | 2051 | char * |
864dbfa3 | 2052 | Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen) |
79072805 | 2053 | { |
fde52b5c | 2054 | if (HeKLEN(entry) == HEf_SVKEY) { |
fb73857a | 2055 | STRLEN len; |
2056 | char *p = SvPV(HeKEY_sv(entry), len); | |
2057 | *retlen = len; | |
2058 | return p; | |
fde52b5c | 2059 | } |
2060 | else { | |
2061 | *retlen = HeKLEN(entry); | |
2062 | return HeKEY(entry); | |
2063 | } | |
2064 | } | |
2065 | ||
2066 | /* unlike hv_iterval(), this always returns a mortal copy of the key */ | |
954c1994 GS |
2067 | /* |
2068 | =for apidoc hv_iterkeysv | |
2069 | ||
2070 | Returns the key as an C<SV*> from the current position of the hash | |
2071 | iterator. The return value will always be a mortal copy of the key. Also | |
2072 | see C<hv_iterinit>. | |
2073 | ||
2074 | =cut | |
2075 | */ | |
2076 | ||
fde52b5c | 2077 | SV * |
864dbfa3 | 2078 | Perl_hv_iterkeysv(pTHX_ register HE *entry) |
fde52b5c | 2079 | { |
c1b02ed8 | 2080 | return sv_2mortal(newSVhek(HeKEY_hek(entry))); |
79072805 LW |
2081 | } |
2082 | ||
954c1994 GS |
2083 | /* |
2084 | =for apidoc hv_iterval | |
2085 | ||
2086 | Returns the value from the current position of the hash iterator. See | |
2087 | C<hv_iterkey>. | |
2088 | ||
2089 | =cut | |
2090 | */ | |
2091 | ||
79072805 | 2092 | SV * |
864dbfa3 | 2093 | Perl_hv_iterval(pTHX_ HV *hv, register HE *entry) |
79072805 | 2094 | { |
8990e307 | 2095 | if (SvRMAGICAL(hv)) { |
14befaf4 | 2096 | if (mg_find((SV*)hv, PERL_MAGIC_tied)) { |
c4420975 | 2097 | SV* const sv = sv_newmortal(); |
bbce6d69 | 2098 | if (HeKLEN(entry) == HEf_SVKEY) |
2099 | mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY); | |
a3b680e6 AL |
2100 | else |
2101 | mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry)); | |
463ee0b2 LW |
2102 | return sv; |
2103 | } | |
79072805 | 2104 | } |
fde52b5c | 2105 | return HeVAL(entry); |
79072805 LW |
2106 | } |
2107 | ||
954c1994 GS |
2108 | /* |
2109 | =for apidoc hv_iternextsv | |
2110 | ||
2111 | Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one | |
2112 | operation. | |
2113 | ||
2114 | =cut | |
2115 | */ | |
2116 | ||
a0d0e21e | 2117 | SV * |
864dbfa3 | 2118 | Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen) |
a0d0e21e LW |
2119 | { |
2120 | HE *he; | |
e16e2ff8 | 2121 | if ( (he = hv_iternext_flags(hv, 0)) == NULL) |
a0d0e21e LW |
2122 | return NULL; |
2123 | *key = hv_iterkey(he, retlen); | |
2124 | return hv_iterval(hv, he); | |
2125 | } | |
2126 | ||
954c1994 | 2127 | /* |
bc5cdc23 NC |
2128 | |
2129 | Now a macro in hv.h | |
2130 | ||
954c1994 GS |
2131 | =for apidoc hv_magic |
2132 | ||
2133 | Adds magic to a hash. See C<sv_magic>. | |
2134 | ||
2135 | =cut | |
2136 | */ | |
2137 | ||
bbce6d69 | 2138 | /* possibly free a shared string if no one has access to it |
fde52b5c | 2139 | * len and hash must both be valid for str. |
2140 | */ | |
bbce6d69 | 2141 | void |
864dbfa3 | 2142 | Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash) |
fde52b5c | 2143 | { |
19692e8d NC |
2144 | unshare_hek_or_pvn (NULL, str, len, hash); |
2145 | } | |
2146 | ||
2147 | ||
2148 | void | |
2149 | Perl_unshare_hek(pTHX_ HEK *hek) | |
2150 | { | |
2151 | unshare_hek_or_pvn(hek, NULL, 0, 0); | |
2152 | } | |
2153 | ||
2154 | /* possibly free a shared string if no one has access to it | |
2155 | hek if non-NULL takes priority over the other 3, else str, len and hash | |
2156 | are used. If so, len and hash must both be valid for str. | |
2157 | */ | |
df132699 | 2158 | STATIC void |
97ddebaf | 2159 | S_unshare_hek_or_pvn(pTHX_ const HEK *hek, const char *str, I32 len, U32 hash) |
19692e8d | 2160 | { |
cbec9347 | 2161 | register XPVHV* xhv; |
20454177 | 2162 | HE *entry; |
fde52b5c | 2163 | register HE **oentry; |
45d1cc86 | 2164 | HE **first; |
a3b680e6 | 2165 | bool found = 0; |
c3654f1a | 2166 | bool is_utf8 = FALSE; |
19692e8d | 2167 | int k_flags = 0; |
aec46f14 | 2168 | const char * const save = str; |
cbae3960 | 2169 | struct shared_he *he = 0; |
c3654f1a | 2170 | |
19692e8d | 2171 | if (hek) { |
cbae3960 NC |
2172 | /* Find the shared he which is just before us in memory. */ |
2173 | he = (struct shared_he *)(((char *)hek) | |
2174 | - STRUCT_OFFSET(struct shared_he, | |
2175 | shared_he_hek)); | |
2176 | ||
2177 | /* Assert that the caller passed us a genuine (or at least consistent) | |
2178 | shared hek */ | |
2179 | assert (he->shared_he_he.hent_hek == hek); | |
29404ae0 NC |
2180 | |
2181 | LOCK_STRTAB_MUTEX; | |
2182 | if (he->shared_he_he.hent_val - 1) { | |
2183 | --he->shared_he_he.hent_val; | |
2184 | UNLOCK_STRTAB_MUTEX; | |
2185 | return; | |
2186 | } | |
2187 | UNLOCK_STRTAB_MUTEX; | |
2188 | ||
19692e8d NC |
2189 | hash = HEK_HASH(hek); |
2190 | } else if (len < 0) { | |
2191 | STRLEN tmplen = -len; | |
2192 | is_utf8 = TRUE; | |
2193 | /* See the note in hv_fetch(). --jhi */ | |
2194 | str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8); | |
2195 | len = tmplen; | |
2196 | if (is_utf8) | |
2197 | k_flags = HVhek_UTF8; | |
2198 | if (str != save) | |
2199 | k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY; | |
c3654f1a | 2200 | } |
1c846c1f | 2201 | |
fde52b5c | 2202 | /* what follows is the moral equivalent of: |
6b88bc9c | 2203 | if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) { |
bbce6d69 | 2204 | if (--*Svp == Nullsv) |
6b88bc9c | 2205 | hv_delete(PL_strtab, str, len, G_DISCARD, hash); |
bbce6d69 | 2206 | } */ |
cbec9347 | 2207 | xhv = (XPVHV*)SvANY(PL_strtab); |
fde52b5c | 2208 | /* assert(xhv_array != 0) */ |
5f08fbcd | 2209 | LOCK_STRTAB_MUTEX; |
45d1cc86 | 2210 | first = oentry = &(HvARRAY(PL_strtab))[hash & (I32) HvMAX(PL_strtab)]; |
6c1b96a1 NC |
2211 | if (he) { |
2212 | const HE *const he_he = &(he->shared_he_he); | |
45d1cc86 | 2213 | for (entry = *oentry; entry; oentry = &HeNEXT(entry), entry = *oentry) { |
6c1b96a1 | 2214 | if (entry != he_he) |
19692e8d NC |
2215 | continue; |
2216 | found = 1; | |
2217 | break; | |
2218 | } | |
2219 | } else { | |
35a4481c | 2220 | const int flags_masked = k_flags & HVhek_MASK; |
45d1cc86 | 2221 | for (entry = *oentry; entry; oentry = &HeNEXT(entry), entry = *oentry) { |
19692e8d NC |
2222 | if (HeHASH(entry) != hash) /* strings can't be equal */ |
2223 | continue; | |
2224 | if (HeKLEN(entry) != len) | |
2225 | continue; | |
2226 | if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */ | |
2227 | continue; | |
2228 | if (HeKFLAGS(entry) != flags_masked) | |
2229 | continue; | |
2230 | found = 1; | |
2231 | break; | |
2232 | } | |
2233 | } | |
2234 | ||
2235 | if (found) { | |
2236 | if (--HeVAL(entry) == Nullsv) { | |
2237 | *oentry = HeNEXT(entry); | |
45d1cc86 NC |
2238 | if (!*first) { |
2239 | /* There are now no entries in our slot. */ | |
19692e8d | 2240 | xhv->xhv_fill--; /* HvFILL(hv)-- */ |
45d1cc86 | 2241 | } |
cbae3960 | 2242 | Safefree(entry); |
19692e8d NC |
2243 | xhv->xhv_keys--; /* HvKEYS(hv)-- */ |
2244 | } | |
fde52b5c | 2245 | } |
19692e8d | 2246 | |
333f433b | 2247 | UNLOCK_STRTAB_MUTEX; |
411caa50 | 2248 | if (!found && ckWARN_d(WARN_INTERNAL)) |
19692e8d | 2249 | Perl_warner(aTHX_ packWARN(WARN_INTERNAL), |
472d47bc SB |
2250 | "Attempt to free non-existent shared string '%s'%s" |
2251 | pTHX__FORMAT, | |
19692e8d | 2252 | hek ? HEK_KEY(hek) : str, |
472d47bc | 2253 | ((k_flags & HVhek_UTF8) ? " (utf8)" : "") pTHX__VALUE); |
19692e8d NC |
2254 | if (k_flags & HVhek_FREEKEY) |
2255 | Safefree(str); | |
fde52b5c | 2256 | } |
2257 | ||
bbce6d69 | 2258 | /* get a (constant) string ptr from the global string table |
2259 | * string will get added if it is not already there. | |
fde52b5c | 2260 | * len and hash must both be valid for str. |
2261 | */ | |
bbce6d69 | 2262 | HEK * |
864dbfa3 | 2263 | Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash) |
fde52b5c | 2264 | { |
da58a35d | 2265 | bool is_utf8 = FALSE; |
19692e8d | 2266 | int flags = 0; |
aec46f14 | 2267 | const char * const save = str; |
da58a35d JH |
2268 | |
2269 | if (len < 0) { | |
77caf834 | 2270 | STRLEN tmplen = -len; |
da58a35d | 2271 | is_utf8 = TRUE; |
77caf834 JH |
2272 | /* See the note in hv_fetch(). --jhi */ |
2273 | str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8); | |
2274 | len = tmplen; | |
19692e8d NC |
2275 | /* If we were able to downgrade here, then than means that we were passed |
2276 | in a key which only had chars 0-255, but was utf8 encoded. */ | |
2277 | if (is_utf8) | |
2278 | flags = HVhek_UTF8; | |
2279 | /* If we found we were able to downgrade the string to bytes, then | |
2280 | we should flag that it needs upgrading on keys or each. Also flag | |
2281 | that we need share_hek_flags to free the string. */ | |
2282 | if (str != save) | |
2283 | flags |= HVhek_WASUTF8 | HVhek_FREEKEY; | |
2284 | } | |
2285 | ||
6e838c70 | 2286 | return share_hek_flags (str, len, hash, flags); |
19692e8d NC |
2287 | } |
2288 | ||
6e838c70 | 2289 | STATIC HEK * |
19692e8d NC |
2290 | S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags) |
2291 | { | |
19692e8d NC |
2292 | register HE *entry; |
2293 | register HE **oentry; | |
19692e8d | 2294 | I32 found = 0; |
35a4481c | 2295 | const int flags_masked = flags & HVhek_MASK; |
bbce6d69 | 2296 | |
fde52b5c | 2297 | /* what follows is the moral equivalent of: |
1c846c1f | 2298 | |
6b88bc9c | 2299 | if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE))) |
8aacddc1 | 2300 | hv_store(PL_strtab, str, len, Nullsv, hash); |
fdcd69b6 NC |
2301 | |
2302 | Can't rehash the shared string table, so not sure if it's worth | |
2303 | counting the number of entries in the linked list | |
bbce6d69 | 2304 | */ |
1b6737cc | 2305 | register XPVHV * const xhv = (XPVHV*)SvANY(PL_strtab); |
fde52b5c | 2306 | /* assert(xhv_array != 0) */ |
5f08fbcd | 2307 | LOCK_STRTAB_MUTEX; |
7b2c381c | 2308 | oentry = &(HvARRAY(PL_strtab))[hash & (I32) HvMAX(PL_strtab)]; |
45d1cc86 | 2309 | for (entry = *oentry; entry; entry = HeNEXT(entry)) { |
fde52b5c | 2310 | if (HeHASH(entry) != hash) /* strings can't be equal */ |
2311 | continue; | |
2312 | if (HeKLEN(entry) != len) | |
2313 | continue; | |
1c846c1f | 2314 | if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */ |
fde52b5c | 2315 | continue; |
19692e8d | 2316 | if (HeKFLAGS(entry) != flags_masked) |
c3654f1a | 2317 | continue; |
fde52b5c | 2318 | found = 1; |
fde52b5c | 2319 | break; |
2320 | } | |
bbce6d69 | 2321 | if (!found) { |
45d1cc86 NC |
2322 | /* What used to be head of the list. |
2323 | If this is NULL, then we're the first entry for this slot, which | |
2324 | means we need to increate fill. */ | |
2325 | const HE *old_first = *oentry; | |
cbae3960 NC |
2326 | struct shared_he *new_entry; |
2327 | HEK *hek; | |
2328 | char *k; | |
2329 | ||
2330 | /* We don't actually store a HE from the arena and a regular HEK. | |
2331 | Instead we allocate one chunk of memory big enough for both, | |
2332 | and put the HEK straight after the HE. This way we can find the | |
2333 | HEK directly from the HE. | |
2334 | */ | |
2335 | ||
a02a5408 | 2336 | Newx(k, STRUCT_OFFSET(struct shared_he, |
cbae3960 NC |
2337 | shared_he_hek.hek_key[0]) + len + 2, char); |
2338 | new_entry = (struct shared_he *)k; | |
2339 | entry = &(new_entry->shared_he_he); | |
2340 | hek = &(new_entry->shared_he_hek); | |
2341 | ||
2342 | Copy(str, HEK_KEY(hek), len, char); | |
2343 | HEK_KEY(hek)[len] = 0; | |
2344 | HEK_LEN(hek) = len; | |
2345 | HEK_HASH(hek) = hash; | |
2346 | HEK_FLAGS(hek) = (unsigned char)flags_masked; | |
2347 | ||
2348 | /* Still "point" to the HEK, so that other code need not know what | |
2349 | we're up to. */ | |
2350 | HeKEY_hek(entry) = hek; | |
bbce6d69 | 2351 | HeVAL(entry) = Nullsv; |
2352 | HeNEXT(entry) = *oentry; | |
2353 | *oentry = entry; | |
cbae3960 | 2354 | |
cbec9347 | 2355 | xhv->xhv_keys++; /* HvKEYS(hv)++ */ |
45d1cc86 | 2356 | if (!old_first) { /* initial entry? */ |
cbec9347 | 2357 | xhv->xhv_fill++; /* HvFILL(hv)++ */ |
4c9cc595 | 2358 | } else if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) { |
cbec9347 | 2359 | hsplit(PL_strtab); |
bbce6d69 | 2360 | } |
2361 | } | |
2362 | ||
2363 | ++HeVAL(entry); /* use value slot as REFCNT */ | |
5f08fbcd | 2364 | UNLOCK_STRTAB_MUTEX; |
19692e8d NC |
2365 | |
2366 | if (flags & HVhek_FREEKEY) | |
f9a63242 | 2367 | Safefree(str); |
19692e8d | 2368 | |
6e838c70 | 2369 | return HeKEY_hek(entry); |
fde52b5c | 2370 | } |
ecae49c0 | 2371 | |
ca732855 NC |
2372 | I32 * |
2373 | Perl_hv_placeholders_p(pTHX_ HV *hv) | |
2374 | { | |
2375 | dVAR; | |
2376 | MAGIC *mg = mg_find((SV*)hv, PERL_MAGIC_rhash); | |
2377 | ||
2378 | if (!mg) { | |
2379 | mg = sv_magicext((SV*)hv, 0, PERL_MAGIC_rhash, 0, 0, 0); | |
2380 | ||
2381 | if (!mg) { | |
2382 | Perl_die(aTHX_ "panic: hv_placeholders_p"); | |
2383 | } | |
2384 | } | |
2385 | return &(mg->mg_len); | |
2386 | } | |
2387 | ||
2388 | ||
2389 | I32 | |
2390 | Perl_hv_placeholders_get(pTHX_ HV *hv) | |
2391 | { | |
2392 | dVAR; | |
b464bac0 | 2393 | MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_rhash); |
ca732855 NC |
2394 | |
2395 | return mg ? mg->mg_len : 0; | |
2396 | } | |
2397 | ||
2398 | void | |
ac1e784a | 2399 | Perl_hv_placeholders_set(pTHX_ HV *hv, I32 ph) |
ca732855 NC |
2400 | { |
2401 | dVAR; | |
b464bac0 | 2402 | MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_rhash); |
ca732855 NC |
2403 | |
2404 | if (mg) { | |
2405 | mg->mg_len = ph; | |
2406 | } else if (ph) { | |
2407 | if (!sv_magicext((SV*)hv, 0, PERL_MAGIC_rhash, 0, 0, ph)) | |
2408 | Perl_die(aTHX_ "panic: hv_placeholders_set"); | |
2409 | } | |
2410 | /* else we don't need to add magic to record 0 placeholders. */ | |
2411 | } | |
ecae49c0 NC |
2412 | |
2413 | /* | |
2414 | =for apidoc hv_assert | |
2415 | ||
2416 | Check that a hash is in an internally consistent state. | |
2417 | ||
2418 | =cut | |
2419 | */ | |
2420 | ||
2421 | void | |
2422 | Perl_hv_assert(pTHX_ HV *hv) | |
2423 | { | |
27da23d5 | 2424 | dVAR; |
ecae49c0 NC |
2425 | HE* entry; |
2426 | int withflags = 0; | |
2427 | int placeholders = 0; | |
2428 | int real = 0; | |
2429 | int bad = 0; | |
bfcb3514 NC |
2430 | const I32 riter = HvRITER_get(hv); |
2431 | HE *eiter = HvEITER_get(hv); | |
ecae49c0 NC |
2432 | |
2433 | (void)hv_iterinit(hv); | |
2434 | ||
2435 | while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))) { | |
2436 | /* sanity check the values */ | |
2437 | if (HeVAL(entry) == &PL_sv_placeholder) { | |
2438 | placeholders++; | |
2439 | } else { | |
2440 | real++; | |
2441 | } | |
2442 | /* sanity check the keys */ | |
2443 | if (HeSVKEY(entry)) { | |
2444 | /* Don't know what to check on SV keys. */ | |
2445 | } else if (HeKUTF8(entry)) { | |
2446 | withflags++; | |
2447 | if (HeKWASUTF8(entry)) { | |
2448 | PerlIO_printf(Perl_debug_log, | |
2449 | "hash key has both WASUFT8 and UTF8: '%.*s'\n", | |
2450 | (int) HeKLEN(entry), HeKEY(entry)); | |
2451 | bad = 1; | |
2452 | } | |
2453 | } else if (HeKWASUTF8(entry)) { | |
2454 | withflags++; | |
2455 | } | |
2456 | } | |
2457 | if (!SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) { | |
2458 | if (HvUSEDKEYS(hv) != real) { | |
2459 | PerlIO_printf(Perl_debug_log, "Count %d key(s), but hash reports %d\n", | |
2460 | (int) real, (int) HvUSEDKEYS(hv)); | |
2461 | bad = 1; | |
2462 | } | |
5d88ecd7 | 2463 | if (HvPLACEHOLDERS_get(hv) != placeholders) { |
ecae49c0 NC |
2464 | PerlIO_printf(Perl_debug_log, |
2465 | "Count %d placeholder(s), but hash reports %d\n", | |
5d88ecd7 | 2466 | (int) placeholders, (int) HvPLACEHOLDERS_get(hv)); |
ecae49c0 NC |
2467 | bad = 1; |
2468 | } | |
2469 | } | |
2470 | if (withflags && ! HvHASKFLAGS(hv)) { | |
2471 | PerlIO_printf(Perl_debug_log, | |
2472 | "Hash has HASKFLAGS off but I count %d key(s) with flags\n", | |
2473 | withflags); | |
2474 | bad = 1; | |
2475 | } | |
2476 | if (bad) { | |
2477 | sv_dump((SV *)hv); | |
2478 | } | |
bfcb3514 NC |
2479 | HvRITER_set(hv, riter); /* Restore hash iterator state */ |
2480 | HvEITER_set(hv, eiter); | |
ecae49c0 | 2481 | } |
af3babe4 NC |
2482 | |
2483 | /* | |
2484 | * Local variables: | |
2485 | * c-indentation-style: bsd | |
2486 | * c-basic-offset: 4 | |
2487 | * indent-tabs-mode: t | |
2488 | * End: | |
2489 | * | |
37442d52 RGS |
2490 | * ex: set ts=8 sts=4 sw=4 noet: |
2491 | */ |