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