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