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