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