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