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