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