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