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