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