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