Commit | Line | Data |
a0d0e21e |
1 | /* hv.c |
79072805 |
2 | * |
9607fc9c |
3 | * Copyright (c) 1991-1997, Larry Wall |
79072805 |
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 |
8 | */ |
9 | |
10 | /* |
11 | * "I sit beside the fire and think of all that I have seen." --Bilbo |
79072805 |
12 | */ |
13 | |
14 | #include "EXTERN.h" |
15 | #include "perl.h" |
16 | |
76e3520e |
17 | static void hv_magic_check _((HV *hv, bool *needs_copy, bool *needs_store)); |
18 | #ifndef PERL_OBJECT |
a0d0e21e |
19 | static void hsplit _((HV *hv)); |
20 | static void hfreeentries _((HV *hv)); |
c31fac66 |
21 | static HE* more_he _((void)); |
76e3520e |
22 | #endif |
4633a7c4 |
23 | |
76e3520e |
24 | STATIC HE* |
8ac85365 |
25 | new_he(void) |
4633a7c4 |
26 | { |
27 | HE* he; |
28 | if (he_root) { |
29 | he = he_root; |
fde52b5c |
30 | he_root = HeNEXT(he); |
4633a7c4 |
31 | return he; |
32 | } |
33 | return more_he(); |
34 | } |
35 | |
76e3520e |
36 | STATIC void |
8ac85365 |
37 | del_he(HE *p) |
4633a7c4 |
38 | { |
fde52b5c |
39 | HeNEXT(p) = (HE*)he_root; |
4633a7c4 |
40 | he_root = p; |
41 | } |
42 | |
76e3520e |
43 | STATIC HE* |
8ac85365 |
44 | more_he(void) |
4633a7c4 |
45 | { |
46 | register HE* he; |
47 | register HE* heend; |
8c52afec |
48 | New(54, he_root, 1008/sizeof(HE), HE); |
4633a7c4 |
49 | he = he_root; |
50 | heend = &he[1008 / sizeof(HE) - 1]; |
51 | while (he < heend) { |
fde52b5c |
52 | HeNEXT(he) = (HE*)(he + 1); |
4633a7c4 |
53 | he++; |
54 | } |
fde52b5c |
55 | HeNEXT(he) = 0; |
4633a7c4 |
56 | return new_he(); |
57 | } |
58 | |
76e3520e |
59 | STATIC HEK * |
8ac85365 |
60 | save_hek(char *str, I32 len, U32 hash) |
bbce6d69 |
61 | { |
62 | char *k; |
63 | register HEK *hek; |
64 | |
ff68c719 |
65 | New(54, k, HEK_BASESIZE + len + 1, char); |
bbce6d69 |
66 | hek = (HEK*)k; |
ff68c719 |
67 | Copy(str, HEK_KEY(hek), len, char); |
68 | *(HEK_KEY(hek) + len) = '\0'; |
69 | HEK_LEN(hek) = len; |
70 | HEK_HASH(hek) = hash; |
bbce6d69 |
71 | return hek; |
72 | } |
73 | |
74 | void |
8ac85365 |
75 | unshare_hek(HEK *hek) |
bbce6d69 |
76 | { |
ff68c719 |
77 | unsharepvn(HEK_KEY(hek),HEK_LEN(hek),HEK_HASH(hek)); |
bbce6d69 |
78 | } |
79 | |
fde52b5c |
80 | /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot |
81 | * contains an SV* */ |
82 | |
79072805 |
83 | SV** |
8ac85365 |
84 | hv_fetch(HV *hv, char *key, U32 klen, I32 lval) |
79072805 |
85 | { |
86 | register XPVHV* xhv; |
fde52b5c |
87 | register U32 hash; |
79072805 |
88 | register HE *entry; |
79072805 |
89 | SV *sv; |
79072805 |
90 | |
91 | if (!hv) |
92 | return 0; |
463ee0b2 |
93 | |
8990e307 |
94 | if (SvRMAGICAL(hv)) { |
463ee0b2 |
95 | if (mg_find((SV*)hv,'P')) { |
11343788 |
96 | dTHR; |
8990e307 |
97 | sv = sv_newmortal(); |
463ee0b2 |
98 | mg_copy((SV*)hv, sv, key, klen); |
4e4c362e |
99 | hv_fetch_sv = sv; |
100 | return &hv_fetch_sv; |
463ee0b2 |
101 | } |
902173a3 |
102 | #ifdef ENV_IS_CASELESS |
103 | else if (mg_find((SV*)hv,'E')) { |
e7152ba2 |
104 | U32 i; |
105 | for (i = 0; i < klen; ++i) |
106 | if (isLOWER(key[i])) { |
107 | char *nkey = strupr(SvPVX(sv_2mortal(newSVpv(key,klen)))); |
108 | SV **ret = hv_fetch(hv, nkey, klen, 0); |
109 | if (!ret && lval) |
110 | ret = hv_store(hv, key, klen, NEWSV(61,0), 0); |
111 | return ret; |
112 | } |
902173a3 |
113 | } |
114 | #endif |
463ee0b2 |
115 | } |
116 | |
79072805 |
117 | xhv = (XPVHV*)SvANY(hv); |
118 | if (!xhv->xhv_array) { |
a0d0e21e |
119 | if (lval |
120 | #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */ |
121 | || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) |
122 | #endif |
123 | ) |
463ee0b2 |
124 | Newz(503,xhv->xhv_array, sizeof(HE*) * (xhv->xhv_max + 1), char); |
79072805 |
125 | else |
126 | return 0; |
127 | } |
128 | |
fde52b5c |
129 | PERL_HASH(hash, key, klen); |
79072805 |
130 | |
a0d0e21e |
131 | entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max]; |
fde52b5c |
132 | for (; entry; entry = HeNEXT(entry)) { |
133 | if (HeHASH(entry) != hash) /* strings can't be equal */ |
79072805 |
134 | continue; |
fde52b5c |
135 | if (HeKLEN(entry) != klen) |
79072805 |
136 | continue; |
36477c24 |
137 | if (memNE(HeKEY(entry),key,klen)) /* is this it? */ |
79072805 |
138 | continue; |
fde52b5c |
139 | return &HeVAL(entry); |
79072805 |
140 | } |
a0d0e21e |
141 | #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */ |
142 | if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) { |
143 | char *gotenv; |
144 | |
7fae4e64 |
145 | if ((gotenv = PerlEnv_getenv(key)) != Nullch) { |
a0d0e21e |
146 | sv = newSVpv(gotenv,strlen(gotenv)); |
1e422769 |
147 | SvTAINTED_on(sv); |
e7152ba2 |
148 | return hv_store(hv,key,klen,sv,hash); |
a0d0e21e |
149 | } |
150 | } |
151 | #endif |
79072805 |
152 | if (lval) { /* gonna assign to this, so it better be there */ |
153 | sv = NEWSV(61,0); |
e7152ba2 |
154 | return hv_store(hv,key,klen,sv,hash); |
79072805 |
155 | } |
156 | return 0; |
157 | } |
158 | |
fde52b5c |
159 | /* returns a HE * structure with the all fields set */ |
160 | /* note that hent_val will be a mortal sv for MAGICAL hashes */ |
161 | HE * |
8ac85365 |
162 | hv_fetch_ent(HV *hv, SV *keysv, I32 lval, register U32 hash) |
fde52b5c |
163 | { |
164 | register XPVHV* xhv; |
165 | register char *key; |
166 | STRLEN klen; |
167 | register HE *entry; |
168 | SV *sv; |
169 | |
170 | if (!hv) |
171 | return 0; |
172 | |
902173a3 |
173 | if (SvRMAGICAL(hv)) { |
174 | if (mg_find((SV*)hv,'P')) { |
6ff68fdd |
175 | dTHR; |
902173a3 |
176 | sv = sv_newmortal(); |
177 | keysv = sv_2mortal(newSVsv(keysv)); |
178 | mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY); |
4e4c362e |
179 | if (!HeKEY_hek(&hv_fetch_ent_mh)) { |
902173a3 |
180 | char *k; |
181 | New(54, k, HEK_BASESIZE + sizeof(SV*), char); |
4e4c362e |
182 | HeKEY_hek(&hv_fetch_ent_mh) = (HEK*)k; |
902173a3 |
183 | } |
4e4c362e |
184 | HeSVKEY_set(&hv_fetch_ent_mh, keysv); |
185 | HeVAL(&hv_fetch_ent_mh) = sv; |
186 | return &hv_fetch_ent_mh; |
1cf368ac |
187 | } |
902173a3 |
188 | #ifdef ENV_IS_CASELESS |
189 | else if (mg_find((SV*)hv,'E')) { |
e7152ba2 |
190 | U32 i; |
902173a3 |
191 | key = SvPV(keysv, klen); |
e7152ba2 |
192 | for (i = 0; i < klen; ++i) |
193 | if (isLOWER(key[i])) { |
194 | SV *nkeysv = sv_2mortal(newSVpv(key,klen)); |
195 | (void)strupr(SvPVX(nkeysv)); |
196 | entry = hv_fetch_ent(hv, nkeysv, 0, 0); |
197 | if (!entry && lval) |
198 | entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash); |
199 | return entry; |
200 | } |
902173a3 |
201 | } |
202 | #endif |
fde52b5c |
203 | } |
204 | |
effa1e2d |
205 | xhv = (XPVHV*)SvANY(hv); |
fde52b5c |
206 | if (!xhv->xhv_array) { |
207 | if (lval |
208 | #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */ |
209 | || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) |
210 | #endif |
211 | ) |
212 | Newz(503,xhv->xhv_array, sizeof(HE*) * (xhv->xhv_max + 1), char); |
213 | else |
214 | return 0; |
215 | } |
216 | |
effa1e2d |
217 | key = SvPV(keysv, klen); |
218 | |
219 | if (!hash) |
220 | PERL_HASH(hash, key, klen); |
221 | |
fde52b5c |
222 | entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max]; |
223 | for (; entry; entry = HeNEXT(entry)) { |
224 | if (HeHASH(entry) != hash) /* strings can't be equal */ |
225 | continue; |
226 | if (HeKLEN(entry) != klen) |
227 | continue; |
36477c24 |
228 | if (memNE(HeKEY(entry),key,klen)) /* is this it? */ |
fde52b5c |
229 | continue; |
230 | return entry; |
231 | } |
232 | #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */ |
233 | if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) { |
234 | char *gotenv; |
235 | |
7fae4e64 |
236 | if ((gotenv = PerlEnv_getenv(key)) != Nullch) { |
fde52b5c |
237 | sv = newSVpv(gotenv,strlen(gotenv)); |
1e422769 |
238 | SvTAINTED_on(sv); |
e7152ba2 |
239 | return hv_store_ent(hv,keysv,sv,hash); |
fde52b5c |
240 | } |
241 | } |
242 | #endif |
243 | if (lval) { /* gonna assign to this, so it better be there */ |
244 | sv = NEWSV(61,0); |
e7152ba2 |
245 | return hv_store_ent(hv,keysv,sv,hash); |
fde52b5c |
246 | } |
247 | return 0; |
248 | } |
249 | |
d0066dc7 |
250 | static void |
61c8b479 |
251 | hv_magic_check (HV *hv, bool *needs_copy, bool *needs_store) |
d0066dc7 |
252 | { |
253 | MAGIC *mg = SvMAGIC(hv); |
254 | *needs_copy = FALSE; |
255 | *needs_store = TRUE; |
256 | while (mg) { |
257 | if (isUPPER(mg->mg_type)) { |
258 | *needs_copy = TRUE; |
259 | switch (mg->mg_type) { |
260 | case 'P': |
d0066dc7 |
261 | case 'S': |
262 | *needs_store = FALSE; |
d0066dc7 |
263 | } |
264 | } |
265 | mg = mg->mg_moremagic; |
266 | } |
267 | } |
268 | |
79072805 |
269 | SV** |
8ac85365 |
270 | hv_store(HV *hv, char *key, U32 klen, SV *val, register U32 hash) |
79072805 |
271 | { |
272 | register XPVHV* xhv; |
79072805 |
273 | register I32 i; |
274 | register HE *entry; |
275 | register HE **oentry; |
79072805 |
276 | |
277 | if (!hv) |
278 | return 0; |
279 | |
280 | xhv = (XPVHV*)SvANY(hv); |
463ee0b2 |
281 | if (SvMAGICAL(hv)) { |
d0066dc7 |
282 | bool needs_copy; |
283 | bool needs_store; |
284 | hv_magic_check (hv, &needs_copy, &needs_store); |
285 | if (needs_copy) { |
286 | mg_copy((SV*)hv, val, key, klen); |
287 | if (!xhv->xhv_array && !needs_store) |
288 | return 0; |
902173a3 |
289 | #ifdef ENV_IS_CASELESS |
290 | else if (mg_find((SV*)hv,'E')) { |
291 | SV *sv = sv_2mortal(newSVpv(key,klen)); |
292 | key = strupr(SvPVX(sv)); |
293 | hash = 0; |
294 | } |
295 | #endif |
d0066dc7 |
296 | } |
463ee0b2 |
297 | } |
fde52b5c |
298 | if (!hash) |
299 | PERL_HASH(hash, key, klen); |
300 | |
301 | if (!xhv->xhv_array) |
302 | Newz(505, xhv->xhv_array, sizeof(HE**) * (xhv->xhv_max + 1), char); |
303 | |
304 | oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max]; |
305 | i = 1; |
306 | |
307 | for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) { |
308 | if (HeHASH(entry) != hash) /* strings can't be equal */ |
309 | continue; |
310 | if (HeKLEN(entry) != klen) |
311 | continue; |
36477c24 |
312 | if (memNE(HeKEY(entry),key,klen)) /* is this it? */ |
fde52b5c |
313 | continue; |
314 | SvREFCNT_dec(HeVAL(entry)); |
315 | HeVAL(entry) = val; |
316 | return &HeVAL(entry); |
317 | } |
318 | |
319 | entry = new_he(); |
fde52b5c |
320 | if (HvSHAREKEYS(hv)) |
ff68c719 |
321 | HeKEY_hek(entry) = share_hek(key, klen, hash); |
fde52b5c |
322 | else /* gotta do the real thing */ |
ff68c719 |
323 | HeKEY_hek(entry) = save_hek(key, klen, hash); |
fde52b5c |
324 | HeVAL(entry) = val; |
fde52b5c |
325 | HeNEXT(entry) = *oentry; |
326 | *oentry = entry; |
327 | |
328 | xhv->xhv_keys++; |
329 | if (i) { /* initial entry? */ |
330 | ++xhv->xhv_fill; |
331 | if (xhv->xhv_keys > xhv->xhv_max) |
332 | hsplit(hv); |
79072805 |
333 | } |
334 | |
fde52b5c |
335 | return &HeVAL(entry); |
336 | } |
337 | |
338 | HE * |
8ac85365 |
339 | hv_store_ent(HV *hv, SV *keysv, SV *val, register U32 hash) |
fde52b5c |
340 | { |
341 | register XPVHV* xhv; |
342 | register char *key; |
343 | STRLEN klen; |
344 | register I32 i; |
345 | register HE *entry; |
346 | register HE **oentry; |
347 | |
348 | if (!hv) |
349 | return 0; |
350 | |
351 | xhv = (XPVHV*)SvANY(hv); |
352 | if (SvMAGICAL(hv)) { |
aeea060c |
353 | dTHR; |
d0066dc7 |
354 | bool needs_copy; |
355 | bool needs_store; |
356 | hv_magic_check (hv, &needs_copy, &needs_store); |
357 | if (needs_copy) { |
358 | bool save_taint = tainted; |
359 | if (tainting) |
360 | tainted = SvTAINTED(keysv); |
361 | keysv = sv_2mortal(newSVsv(keysv)); |
362 | mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY); |
363 | TAINT_IF(save_taint); |
364 | if (!xhv->xhv_array && !needs_store) |
365 | return Nullhe; |
902173a3 |
366 | #ifdef ENV_IS_CASELESS |
367 | else if (mg_find((SV*)hv,'E')) { |
368 | key = SvPV(keysv, klen); |
369 | keysv = sv_2mortal(newSVpv(key,klen)); |
370 | (void)strupr(SvPVX(keysv)); |
371 | hash = 0; |
372 | } |
373 | #endif |
374 | } |
fde52b5c |
375 | } |
376 | |
377 | key = SvPV(keysv, klen); |
902173a3 |
378 | |
fde52b5c |
379 | if (!hash) |
380 | PERL_HASH(hash, key, klen); |
381 | |
79072805 |
382 | if (!xhv->xhv_array) |
463ee0b2 |
383 | Newz(505, xhv->xhv_array, sizeof(HE**) * (xhv->xhv_max + 1), char); |
79072805 |
384 | |
a0d0e21e |
385 | oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max]; |
79072805 |
386 | i = 1; |
387 | |
fde52b5c |
388 | for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) { |
389 | if (HeHASH(entry) != hash) /* strings can't be equal */ |
79072805 |
390 | continue; |
fde52b5c |
391 | if (HeKLEN(entry) != klen) |
79072805 |
392 | continue; |
36477c24 |
393 | if (memNE(HeKEY(entry),key,klen)) /* is this it? */ |
79072805 |
394 | continue; |
fde52b5c |
395 | SvREFCNT_dec(HeVAL(entry)); |
396 | HeVAL(entry) = val; |
397 | return entry; |
79072805 |
398 | } |
79072805 |
399 | |
4633a7c4 |
400 | entry = new_he(); |
fde52b5c |
401 | if (HvSHAREKEYS(hv)) |
ff68c719 |
402 | HeKEY_hek(entry) = share_hek(key, klen, hash); |
fde52b5c |
403 | else /* gotta do the real thing */ |
ff68c719 |
404 | HeKEY_hek(entry) = save_hek(key, klen, hash); |
fde52b5c |
405 | HeVAL(entry) = val; |
fde52b5c |
406 | HeNEXT(entry) = *oentry; |
79072805 |
407 | *oentry = entry; |
408 | |
463ee0b2 |
409 | xhv->xhv_keys++; |
79072805 |
410 | if (i) { /* initial entry? */ |
463ee0b2 |
411 | ++xhv->xhv_fill; |
412 | if (xhv->xhv_keys > xhv->xhv_max) |
79072805 |
413 | hsplit(hv); |
414 | } |
79072805 |
415 | |
fde52b5c |
416 | return entry; |
79072805 |
417 | } |
418 | |
419 | SV * |
8ac85365 |
420 | hv_delete(HV *hv, char *key, U32 klen, I32 flags) |
79072805 |
421 | { |
422 | register XPVHV* xhv; |
79072805 |
423 | register I32 i; |
fde52b5c |
424 | register U32 hash; |
79072805 |
425 | register HE *entry; |
426 | register HE **oentry; |
67a38de0 |
427 | SV **svp; |
79072805 |
428 | SV *sv; |
79072805 |
429 | |
430 | if (!hv) |
431 | return Nullsv; |
8990e307 |
432 | if (SvRMAGICAL(hv)) { |
0a0bb7c7 |
433 | bool needs_copy; |
434 | bool needs_store; |
435 | hv_magic_check (hv, &needs_copy, &needs_store); |
436 | |
67a38de0 |
437 | if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) { |
438 | sv = *svp; |
0a0bb7c7 |
439 | mg_clear(sv); |
440 | if (!needs_store) { |
441 | if (mg_find(sv, 'p')) { |
442 | sv_unmagic(sv, 'p'); /* No longer an element */ |
443 | return sv; |
444 | } |
445 | return Nullsv; /* element cannot be deleted */ |
446 | } |
902173a3 |
447 | #ifdef ENV_IS_CASELESS |
2fd1c6b8 |
448 | else if (mg_find((SV*)hv,'E')) { |
449 | sv = sv_2mortal(newSVpv(key,klen)); |
450 | key = strupr(SvPVX(sv)); |
451 | } |
902173a3 |
452 | #endif |
2fd1c6b8 |
453 | } |
463ee0b2 |
454 | } |
79072805 |
455 | xhv = (XPVHV*)SvANY(hv); |
456 | if (!xhv->xhv_array) |
457 | return Nullsv; |
fde52b5c |
458 | |
459 | PERL_HASH(hash, key, klen); |
79072805 |
460 | |
a0d0e21e |
461 | oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max]; |
79072805 |
462 | entry = *oentry; |
463 | i = 1; |
fde52b5c |
464 | for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) { |
465 | if (HeHASH(entry) != hash) /* strings can't be equal */ |
79072805 |
466 | continue; |
fde52b5c |
467 | if (HeKLEN(entry) != klen) |
79072805 |
468 | continue; |
36477c24 |
469 | if (memNE(HeKEY(entry),key,klen)) /* is this it? */ |
79072805 |
470 | continue; |
fde52b5c |
471 | *oentry = HeNEXT(entry); |
79072805 |
472 | if (i && !*oentry) |
473 | xhv->xhv_fill--; |
748a9306 |
474 | if (flags & G_DISCARD) |
475 | sv = Nullsv; |
476 | else |
fde52b5c |
477 | sv = sv_mortalcopy(HeVAL(entry)); |
a0d0e21e |
478 | if (entry == xhv->xhv_eiter) |
72940dca |
479 | HvLAZYDEL_on(hv); |
a0d0e21e |
480 | else |
68dc0745 |
481 | hv_free_ent(hv, entry); |
fde52b5c |
482 | --xhv->xhv_keys; |
483 | return sv; |
484 | } |
485 | return Nullsv; |
486 | } |
487 | |
488 | SV * |
8ac85365 |
489 | hv_delete_ent(HV *hv, SV *keysv, I32 flags, U32 hash) |
fde52b5c |
490 | { |
491 | register XPVHV* xhv; |
492 | register I32 i; |
493 | register char *key; |
494 | STRLEN klen; |
495 | register HE *entry; |
496 | register HE **oentry; |
497 | SV *sv; |
498 | |
499 | if (!hv) |
500 | return Nullsv; |
501 | if (SvRMAGICAL(hv)) { |
0a0bb7c7 |
502 | bool needs_copy; |
503 | bool needs_store; |
504 | hv_magic_check (hv, &needs_copy, &needs_store); |
505 | |
67a38de0 |
506 | if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) { |
0a0bb7c7 |
507 | sv = HeVAL(entry); |
508 | mg_clear(sv); |
509 | if (!needs_store) { |
510 | if (mg_find(sv, 'p')) { |
511 | sv_unmagic(sv, 'p'); /* No longer an element */ |
512 | return sv; |
513 | } |
514 | return Nullsv; /* element cannot be deleted */ |
515 | } |
902173a3 |
516 | #ifdef ENV_IS_CASELESS |
2fd1c6b8 |
517 | else if (mg_find((SV*)hv,'E')) { |
518 | key = SvPV(keysv, klen); |
519 | keysv = sv_2mortal(newSVpv(key,klen)); |
520 | (void)strupr(SvPVX(keysv)); |
521 | hash = 0; |
522 | } |
902173a3 |
523 | #endif |
2fd1c6b8 |
524 | } |
fde52b5c |
525 | } |
526 | xhv = (XPVHV*)SvANY(hv); |
527 | if (!xhv->xhv_array) |
528 | return Nullsv; |
529 | |
530 | key = SvPV(keysv, klen); |
531 | |
532 | if (!hash) |
533 | PERL_HASH(hash, key, klen); |
534 | |
535 | oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max]; |
536 | entry = *oentry; |
537 | i = 1; |
538 | for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) { |
539 | if (HeHASH(entry) != hash) /* strings can't be equal */ |
540 | continue; |
541 | if (HeKLEN(entry) != klen) |
542 | continue; |
36477c24 |
543 | if (memNE(HeKEY(entry),key,klen)) /* is this it? */ |
fde52b5c |
544 | continue; |
545 | *oentry = HeNEXT(entry); |
546 | if (i && !*oentry) |
547 | xhv->xhv_fill--; |
548 | if (flags & G_DISCARD) |
549 | sv = Nullsv; |
550 | else |
551 | sv = sv_mortalcopy(HeVAL(entry)); |
552 | if (entry == xhv->xhv_eiter) |
72940dca |
553 | HvLAZYDEL_on(hv); |
fde52b5c |
554 | else |
68dc0745 |
555 | hv_free_ent(hv, entry); |
463ee0b2 |
556 | --xhv->xhv_keys; |
79072805 |
557 | return sv; |
558 | } |
79072805 |
559 | return Nullsv; |
79072805 |
560 | } |
561 | |
a0d0e21e |
562 | bool |
8ac85365 |
563 | hv_exists(HV *hv, char *key, U32 klen) |
a0d0e21e |
564 | { |
565 | register XPVHV* xhv; |
fde52b5c |
566 | register U32 hash; |
a0d0e21e |
567 | register HE *entry; |
568 | SV *sv; |
569 | |
570 | if (!hv) |
571 | return 0; |
572 | |
573 | if (SvRMAGICAL(hv)) { |
574 | if (mg_find((SV*)hv,'P')) { |
11343788 |
575 | dTHR; |
a0d0e21e |
576 | sv = sv_newmortal(); |
577 | mg_copy((SV*)hv, sv, key, klen); |
578 | magic_existspack(sv, mg_find(sv, 'p')); |
579 | return SvTRUE(sv); |
580 | } |
902173a3 |
581 | #ifdef ENV_IS_CASELESS |
582 | else if (mg_find((SV*)hv,'E')) { |
583 | sv = sv_2mortal(newSVpv(key,klen)); |
584 | key = strupr(SvPVX(sv)); |
585 | } |
586 | #endif |
a0d0e21e |
587 | } |
588 | |
589 | xhv = (XPVHV*)SvANY(hv); |
590 | if (!xhv->xhv_array) |
591 | return 0; |
592 | |
fde52b5c |
593 | PERL_HASH(hash, key, klen); |
a0d0e21e |
594 | |
595 | entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max]; |
fde52b5c |
596 | for (; entry; entry = HeNEXT(entry)) { |
597 | if (HeHASH(entry) != hash) /* strings can't be equal */ |
a0d0e21e |
598 | continue; |
fde52b5c |
599 | if (HeKLEN(entry) != klen) |
a0d0e21e |
600 | continue; |
36477c24 |
601 | if (memNE(HeKEY(entry),key,klen)) /* is this it? */ |
fde52b5c |
602 | continue; |
603 | return TRUE; |
604 | } |
605 | return FALSE; |
606 | } |
607 | |
608 | |
609 | bool |
8ac85365 |
610 | hv_exists_ent(HV *hv, SV *keysv, U32 hash) |
fde52b5c |
611 | { |
612 | register XPVHV* xhv; |
613 | register char *key; |
614 | STRLEN klen; |
615 | register HE *entry; |
616 | SV *sv; |
617 | |
618 | if (!hv) |
619 | return 0; |
620 | |
621 | if (SvRMAGICAL(hv)) { |
622 | if (mg_find((SV*)hv,'P')) { |
e858de61 |
623 | dTHR; /* just for SvTRUE */ |
fde52b5c |
624 | sv = sv_newmortal(); |
effa1e2d |
625 | keysv = sv_2mortal(newSVsv(keysv)); |
fde52b5c |
626 | mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY); |
627 | magic_existspack(sv, mg_find(sv, 'p')); |
628 | return SvTRUE(sv); |
629 | } |
902173a3 |
630 | #ifdef ENV_IS_CASELESS |
631 | else if (mg_find((SV*)hv,'E')) { |
632 | key = SvPV(keysv, klen); |
633 | keysv = sv_2mortal(newSVpv(key,klen)); |
634 | (void)strupr(SvPVX(keysv)); |
635 | hash = 0; |
636 | } |
637 | #endif |
fde52b5c |
638 | } |
639 | |
640 | xhv = (XPVHV*)SvANY(hv); |
641 | if (!xhv->xhv_array) |
642 | return 0; |
643 | |
644 | key = SvPV(keysv, klen); |
645 | if (!hash) |
646 | PERL_HASH(hash, key, klen); |
647 | |
648 | entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max]; |
649 | for (; entry; entry = HeNEXT(entry)) { |
650 | if (HeHASH(entry) != hash) /* strings can't be equal */ |
651 | continue; |
652 | if (HeKLEN(entry) != klen) |
653 | continue; |
36477c24 |
654 | if (memNE(HeKEY(entry),key,klen)) /* is this it? */ |
a0d0e21e |
655 | continue; |
656 | return TRUE; |
657 | } |
658 | return FALSE; |
659 | } |
660 | |
76e3520e |
661 | STATIC void |
8ac85365 |
662 | hsplit(HV *hv) |
79072805 |
663 | { |
664 | register XPVHV* xhv = (XPVHV*)SvANY(hv); |
a0d0e21e |
665 | I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */ |
79072805 |
666 | register I32 newsize = oldsize * 2; |
667 | register I32 i; |
668 | register HE **a; |
669 | register HE **b; |
670 | register HE *entry; |
671 | register HE **oentry; |
c07a80fd |
672 | #ifndef STRANGE_MALLOC |
4633a7c4 |
673 | I32 tmp; |
c07a80fd |
674 | #endif |
79072805 |
675 | |
463ee0b2 |
676 | a = (HE**)xhv->xhv_array; |
79072805 |
677 | nomemok = TRUE; |
4633a7c4 |
678 | #ifdef STRANGE_MALLOC |
79072805 |
679 | Renew(a, newsize, HE*); |
422a93e5 |
680 | if (!a) { |
681 | nomemok = FALSE; |
682 | return; |
683 | } |
4633a7c4 |
684 | #else |
685 | i = newsize * sizeof(HE*); |
686 | #define MALLOC_OVERHEAD 16 |
687 | tmp = MALLOC_OVERHEAD; |
688 | while (tmp - MALLOC_OVERHEAD < i) |
689 | tmp += tmp; |
690 | tmp -= MALLOC_OVERHEAD; |
691 | tmp /= sizeof(HE*); |
692 | assert(tmp >= newsize); |
693 | New(2,a, tmp, HE*); |
422a93e5 |
694 | if (!a) { |
695 | nomemok = FALSE; |
696 | return; |
697 | } |
4633a7c4 |
698 | Copy(xhv->xhv_array, a, oldsize, HE*); |
fba3b22e |
699 | if (oldsize >= 64) { |
700 | offer_nice_chunk(xhv->xhv_array, |
701 | oldsize * sizeof(HE*) * 2 - MALLOC_OVERHEAD); |
4633a7c4 |
702 | } |
703 | else |
704 | Safefree(xhv->xhv_array); |
705 | #endif |
706 | |
79072805 |
707 | nomemok = FALSE; |
79072805 |
708 | Zero(&a[oldsize], oldsize, HE*); /* zero 2nd half*/ |
709 | xhv->xhv_max = --newsize; |
463ee0b2 |
710 | xhv->xhv_array = (char*)a; |
79072805 |
711 | |
712 | for (i=0; i<oldsize; i++,a++) { |
713 | if (!*a) /* non-existent */ |
714 | continue; |
715 | b = a+oldsize; |
716 | for (oentry = a, entry = *a; entry; entry = *oentry) { |
fde52b5c |
717 | if ((HeHASH(entry) & newsize) != i) { |
718 | *oentry = HeNEXT(entry); |
719 | HeNEXT(entry) = *b; |
79072805 |
720 | if (!*b) |
721 | xhv->xhv_fill++; |
722 | *b = entry; |
723 | continue; |
724 | } |
725 | else |
fde52b5c |
726 | oentry = &HeNEXT(entry); |
79072805 |
727 | } |
728 | if (!*a) /* everything moved */ |
729 | xhv->xhv_fill--; |
730 | } |
731 | } |
732 | |
72940dca |
733 | void |
8ac85365 |
734 | hv_ksplit(HV *hv, IV newmax) |
72940dca |
735 | { |
736 | register XPVHV* xhv = (XPVHV*)SvANY(hv); |
737 | I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */ |
738 | register I32 newsize; |
739 | register I32 i; |
740 | register I32 j; |
741 | register HE **a; |
742 | register HE *entry; |
743 | register HE **oentry; |
744 | |
745 | newsize = (I32) newmax; /* possible truncation here */ |
746 | if (newsize != newmax || newmax <= oldsize) |
747 | return; |
748 | while ((newsize & (1 + ~newsize)) != newsize) { |
749 | newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */ |
750 | } |
751 | if (newsize < newmax) |
752 | newsize *= 2; |
753 | if (newsize < newmax) |
754 | return; /* overflow detection */ |
755 | |
756 | a = (HE**)xhv->xhv_array; |
757 | if (a) { |
758 | nomemok = TRUE; |
759 | #ifdef STRANGE_MALLOC |
760 | Renew(a, newsize, HE*); |
422a93e5 |
761 | if (!a) { |
762 | nomemok = FALSE; |
763 | return; |
764 | } |
72940dca |
765 | #else |
766 | i = newsize * sizeof(HE*); |
767 | j = MALLOC_OVERHEAD; |
768 | while (j - MALLOC_OVERHEAD < i) |
769 | j += j; |
770 | j -= MALLOC_OVERHEAD; |
771 | j /= sizeof(HE*); |
772 | assert(j >= newsize); |
773 | New(2, a, j, HE*); |
422a93e5 |
774 | if (!a) { |
775 | nomemok = FALSE; |
776 | return; |
777 | } |
72940dca |
778 | Copy(xhv->xhv_array, a, oldsize, HE*); |
fba3b22e |
779 | if (oldsize >= 64) { |
780 | offer_nice_chunk(xhv->xhv_array, |
781 | oldsize * sizeof(HE*) * 2 - MALLOC_OVERHEAD); |
72940dca |
782 | } |
783 | else |
784 | Safefree(xhv->xhv_array); |
785 | #endif |
786 | nomemok = FALSE; |
787 | Zero(&a[oldsize], newsize-oldsize, HE*); /* zero 2nd half*/ |
788 | } |
789 | else { |
790 | Newz(0, a, newsize, HE*); |
791 | } |
792 | xhv->xhv_max = --newsize; |
793 | xhv->xhv_array = (char*)a; |
794 | if (!xhv->xhv_fill) /* skip rest if no entries */ |
795 | return; |
796 | |
797 | for (i=0; i<oldsize; i++,a++) { |
798 | if (!*a) /* non-existent */ |
799 | continue; |
800 | for (oentry = a, entry = *a; entry; entry = *oentry) { |
801 | if ((j = (HeHASH(entry) & newsize)) != i) { |
802 | j -= i; |
803 | *oentry = HeNEXT(entry); |
804 | if (!(HeNEXT(entry) = a[j])) |
805 | xhv->xhv_fill++; |
806 | a[j] = entry; |
807 | continue; |
808 | } |
809 | else |
810 | oentry = &HeNEXT(entry); |
811 | } |
812 | if (!*a) /* everything moved */ |
813 | xhv->xhv_fill--; |
814 | } |
815 | } |
816 | |
79072805 |
817 | HV * |
8ac85365 |
818 | newHV(void) |
79072805 |
819 | { |
820 | register HV *hv; |
821 | register XPVHV* xhv; |
822 | |
a0d0e21e |
823 | hv = (HV*)NEWSV(502,0); |
824 | sv_upgrade((SV *)hv, SVt_PVHV); |
79072805 |
825 | xhv = (XPVHV*)SvANY(hv); |
826 | SvPOK_off(hv); |
827 | SvNOK_off(hv); |
fde52b5c |
828 | #ifndef NODEFAULT_SHAREKEYS |
829 | HvSHAREKEYS_on(hv); /* key-sharing on by default */ |
830 | #endif |
463ee0b2 |
831 | xhv->xhv_max = 7; /* start with 8 buckets */ |
79072805 |
832 | xhv->xhv_fill = 0; |
833 | xhv->xhv_pmroot = 0; |
79072805 |
834 | (void)hv_iterinit(hv); /* so each() will start off right */ |
835 | return hv; |
836 | } |
837 | |
838 | void |
8ac85365 |
839 | hv_free_ent(HV *hv, register HE *entry) |
79072805 |
840 | { |
16bdeea2 |
841 | SV *val; |
842 | |
68dc0745 |
843 | if (!entry) |
79072805 |
844 | return; |
16bdeea2 |
845 | val = HeVAL(entry); |
257c9e5b |
846 | if (val && isGV(val) && GvCVu(val) && HvNAME(hv)) |
44a8e56a |
847 | sub_generation++; /* may be deletion of method from stash */ |
16bdeea2 |
848 | SvREFCNT_dec(val); |
68dc0745 |
849 | if (HeKLEN(entry) == HEf_SVKEY) { |
850 | SvREFCNT_dec(HeKEY_sv(entry)); |
851 | Safefree(HeKEY_hek(entry)); |
44a8e56a |
852 | } |
853 | else if (HvSHAREKEYS(hv)) |
68dc0745 |
854 | unshare_hek(HeKEY_hek(entry)); |
fde52b5c |
855 | else |
68dc0745 |
856 | Safefree(HeKEY_hek(entry)); |
857 | del_he(entry); |
79072805 |
858 | } |
859 | |
860 | void |
8ac85365 |
861 | hv_delayfree_ent(HV *hv, register HE *entry) |
79072805 |
862 | { |
68dc0745 |
863 | if (!entry) |
79072805 |
864 | return; |
68dc0745 |
865 | if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv)) |
44a8e56a |
866 | sub_generation++; /* may be deletion of method from stash */ |
68dc0745 |
867 | sv_2mortal(HeVAL(entry)); /* free between statements */ |
868 | if (HeKLEN(entry) == HEf_SVKEY) { |
869 | sv_2mortal(HeKEY_sv(entry)); |
870 | Safefree(HeKEY_hek(entry)); |
44a8e56a |
871 | } |
872 | else if (HvSHAREKEYS(hv)) |
68dc0745 |
873 | unshare_hek(HeKEY_hek(entry)); |
fde52b5c |
874 | else |
68dc0745 |
875 | Safefree(HeKEY_hek(entry)); |
876 | del_he(entry); |
79072805 |
877 | } |
878 | |
879 | void |
8ac85365 |
880 | hv_clear(HV *hv) |
79072805 |
881 | { |
882 | register XPVHV* xhv; |
883 | if (!hv) |
884 | return; |
885 | xhv = (XPVHV*)SvANY(hv); |
463ee0b2 |
886 | hfreeentries(hv); |
79072805 |
887 | xhv->xhv_fill = 0; |
a0d0e21e |
888 | xhv->xhv_keys = 0; |
79072805 |
889 | if (xhv->xhv_array) |
463ee0b2 |
890 | (void)memzero(xhv->xhv_array, (xhv->xhv_max + 1) * sizeof(HE*)); |
a0d0e21e |
891 | |
892 | if (SvRMAGICAL(hv)) |
893 | mg_clear((SV*)hv); |
79072805 |
894 | } |
895 | |
76e3520e |
896 | STATIC void |
8ac85365 |
897 | hfreeentries(HV *hv) |
79072805 |
898 | { |
a0d0e21e |
899 | register HE **array; |
68dc0745 |
900 | register HE *entry; |
901 | register HE *oentry = Null(HE*); |
a0d0e21e |
902 | I32 riter; |
903 | I32 max; |
79072805 |
904 | |
905 | if (!hv) |
906 | return; |
a0d0e21e |
907 | if (!HvARRAY(hv)) |
79072805 |
908 | return; |
a0d0e21e |
909 | |
910 | riter = 0; |
911 | max = HvMAX(hv); |
912 | array = HvARRAY(hv); |
68dc0745 |
913 | entry = array[0]; |
a0d0e21e |
914 | for (;;) { |
68dc0745 |
915 | if (entry) { |
916 | oentry = entry; |
917 | entry = HeNEXT(entry); |
918 | hv_free_ent(hv, oentry); |
a0d0e21e |
919 | } |
68dc0745 |
920 | if (!entry) { |
a0d0e21e |
921 | if (++riter > max) |
922 | break; |
68dc0745 |
923 | entry = array[riter]; |
a0d0e21e |
924 | } |
79072805 |
925 | } |
a0d0e21e |
926 | (void)hv_iterinit(hv); |
79072805 |
927 | } |
928 | |
929 | void |
8ac85365 |
930 | hv_undef(HV *hv) |
79072805 |
931 | { |
932 | register XPVHV* xhv; |
933 | if (!hv) |
934 | return; |
935 | xhv = (XPVHV*)SvANY(hv); |
463ee0b2 |
936 | hfreeentries(hv); |
79072805 |
937 | Safefree(xhv->xhv_array); |
85e6fe83 |
938 | if (HvNAME(hv)) { |
939 | Safefree(HvNAME(hv)); |
940 | HvNAME(hv) = 0; |
941 | } |
79072805 |
942 | xhv->xhv_array = 0; |
aa689395 |
943 | xhv->xhv_max = 7; /* it's a normal hash */ |
79072805 |
944 | xhv->xhv_fill = 0; |
a0d0e21e |
945 | xhv->xhv_keys = 0; |
946 | |
947 | if (SvRMAGICAL(hv)) |
948 | mg_clear((SV*)hv); |
79072805 |
949 | } |
950 | |
79072805 |
951 | I32 |
8ac85365 |
952 | hv_iterinit(HV *hv) |
79072805 |
953 | { |
aa689395 |
954 | register XPVHV* xhv; |
955 | HE *entry; |
956 | |
957 | if (!hv) |
958 | croak("Bad hash"); |
959 | xhv = (XPVHV*)SvANY(hv); |
960 | entry = xhv->xhv_eiter; |
effa1e2d |
961 | #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */ |
aa689395 |
962 | if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME)) |
963 | prime_env_iter(); |
effa1e2d |
964 | #endif |
72940dca |
965 | if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */ |
966 | HvLAZYDEL_off(hv); |
68dc0745 |
967 | hv_free_ent(hv, entry); |
72940dca |
968 | } |
79072805 |
969 | xhv->xhv_riter = -1; |
970 | xhv->xhv_eiter = Null(HE*); |
c6601927 |
971 | return xhv->xhv_keys; /* used to be xhv->xhv_fill before 5.004_65 */ |
79072805 |
972 | } |
973 | |
974 | HE * |
8ac85365 |
975 | hv_iternext(HV *hv) |
79072805 |
976 | { |
977 | register XPVHV* xhv; |
978 | register HE *entry; |
a0d0e21e |
979 | HE *oldentry; |
463ee0b2 |
980 | MAGIC* mg; |
79072805 |
981 | |
982 | if (!hv) |
aa689395 |
983 | croak("Bad hash"); |
79072805 |
984 | xhv = (XPVHV*)SvANY(hv); |
a0d0e21e |
985 | oldentry = entry = xhv->xhv_eiter; |
463ee0b2 |
986 | |
8990e307 |
987 | if (SvRMAGICAL(hv) && (mg = mg_find((SV*)hv,'P'))) { |
988 | SV *key = sv_newmortal(); |
cd1469e6 |
989 | if (entry) { |
fde52b5c |
990 | sv_setsv(key, HeSVKEY_force(entry)); |
cd1469e6 |
991 | SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */ |
992 | } |
a0d0e21e |
993 | else { |
ff68c719 |
994 | char *k; |
bbce6d69 |
995 | HEK *hek; |
ff68c719 |
996 | |
997 | xhv->xhv_eiter = entry = new_he(); /* one HE per MAGICAL hash */ |
4633a7c4 |
998 | Zero(entry, 1, HE); |
ff68c719 |
999 | Newz(54, k, HEK_BASESIZE + sizeof(SV*), char); |
1000 | hek = (HEK*)k; |
1001 | HeKEY_hek(entry) = hek; |
fde52b5c |
1002 | HeKLEN(entry) = HEf_SVKEY; |
a0d0e21e |
1003 | } |
1004 | magic_nextpack((SV*) hv,mg,key); |
463ee0b2 |
1005 | if (SvOK(key)) { |
cd1469e6 |
1006 | /* force key to stay around until next time */ |
bbce6d69 |
1007 | HeSVKEY_set(entry, SvREFCNT_inc(key)); |
1008 | return entry; /* beware, hent_val is not set */ |
463ee0b2 |
1009 | } |
fde52b5c |
1010 | if (HeVAL(entry)) |
1011 | SvREFCNT_dec(HeVAL(entry)); |
ff68c719 |
1012 | Safefree(HeKEY_hek(entry)); |
4633a7c4 |
1013 | del_he(entry); |
463ee0b2 |
1014 | xhv->xhv_eiter = Null(HE*); |
1015 | return Null(HE*); |
79072805 |
1016 | } |
463ee0b2 |
1017 | |
79072805 |
1018 | if (!xhv->xhv_array) |
4633a7c4 |
1019 | Newz(506,xhv->xhv_array, sizeof(HE*) * (xhv->xhv_max + 1), char); |
fde52b5c |
1020 | if (entry) |
1021 | entry = HeNEXT(entry); |
1022 | while (!entry) { |
1023 | ++xhv->xhv_riter; |
1024 | if (xhv->xhv_riter > xhv->xhv_max) { |
1025 | xhv->xhv_riter = -1; |
1026 | break; |
79072805 |
1027 | } |
fde52b5c |
1028 | entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter]; |
1029 | } |
79072805 |
1030 | |
72940dca |
1031 | if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */ |
1032 | HvLAZYDEL_off(hv); |
68dc0745 |
1033 | hv_free_ent(hv, oldentry); |
72940dca |
1034 | } |
a0d0e21e |
1035 | |
79072805 |
1036 | xhv->xhv_eiter = entry; |
1037 | return entry; |
1038 | } |
1039 | |
1040 | char * |
8ac85365 |
1041 | hv_iterkey(register HE *entry, I32 *retlen) |
79072805 |
1042 | { |
fde52b5c |
1043 | if (HeKLEN(entry) == HEf_SVKEY) { |
fb73857a |
1044 | STRLEN len; |
1045 | char *p = SvPV(HeKEY_sv(entry), len); |
1046 | *retlen = len; |
1047 | return p; |
fde52b5c |
1048 | } |
1049 | else { |
1050 | *retlen = HeKLEN(entry); |
1051 | return HeKEY(entry); |
1052 | } |
1053 | } |
1054 | |
1055 | /* unlike hv_iterval(), this always returns a mortal copy of the key */ |
1056 | SV * |
8ac85365 |
1057 | hv_iterkeysv(register HE *entry) |
fde52b5c |
1058 | { |
1059 | if (HeKLEN(entry) == HEf_SVKEY) |
bbce6d69 |
1060 | return sv_mortalcopy(HeKEY_sv(entry)); |
fde52b5c |
1061 | else |
1062 | return sv_2mortal(newSVpv((HeKLEN(entry) ? HeKEY(entry) : ""), |
1063 | HeKLEN(entry))); |
79072805 |
1064 | } |
1065 | |
1066 | SV * |
8ac85365 |
1067 | hv_iterval(HV *hv, register HE *entry) |
79072805 |
1068 | { |
8990e307 |
1069 | if (SvRMAGICAL(hv)) { |
463ee0b2 |
1070 | if (mg_find((SV*)hv,'P')) { |
8990e307 |
1071 | SV* sv = sv_newmortal(); |
bbce6d69 |
1072 | if (HeKLEN(entry) == HEf_SVKEY) |
1073 | mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY); |
1074 | else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry)); |
463ee0b2 |
1075 | return sv; |
1076 | } |
79072805 |
1077 | } |
fde52b5c |
1078 | return HeVAL(entry); |
79072805 |
1079 | } |
1080 | |
a0d0e21e |
1081 | SV * |
8ac85365 |
1082 | hv_iternextsv(HV *hv, char **key, I32 *retlen) |
a0d0e21e |
1083 | { |
1084 | HE *he; |
1085 | if ( (he = hv_iternext(hv)) == NULL) |
1086 | return NULL; |
1087 | *key = hv_iterkey(he, retlen); |
1088 | return hv_iterval(hv, he); |
1089 | } |
1090 | |
79072805 |
1091 | void |
8ac85365 |
1092 | hv_magic(HV *hv, GV *gv, int how) |
79072805 |
1093 | { |
a0d0e21e |
1094 | sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0); |
79072805 |
1095 | } |
fde52b5c |
1096 | |
bbce6d69 |
1097 | char* |
8ac85365 |
1098 | sharepvn(char *sv, I32 len, U32 hash) |
bbce6d69 |
1099 | { |
ff68c719 |
1100 | return HEK_KEY(share_hek(sv, len, hash)); |
bbce6d69 |
1101 | } |
1102 | |
1103 | /* possibly free a shared string if no one has access to it |
fde52b5c |
1104 | * len and hash must both be valid for str. |
1105 | */ |
bbce6d69 |
1106 | void |
8ac85365 |
1107 | unsharepvn(char *str, I32 len, U32 hash) |
fde52b5c |
1108 | { |
1109 | register XPVHV* xhv; |
1110 | register HE *entry; |
1111 | register HE **oentry; |
1112 | register I32 i = 1; |
1113 | I32 found = 0; |
bbce6d69 |
1114 | |
fde52b5c |
1115 | /* what follows is the moral equivalent of: |
bbce6d69 |
1116 | if ((Svp = hv_fetch(strtab, tmpsv, FALSE, hash))) { |
1117 | if (--*Svp == Nullsv) |
1118 | hv_delete(strtab, str, len, G_DISCARD, hash); |
1119 | } */ |
fde52b5c |
1120 | xhv = (XPVHV*)SvANY(strtab); |
1121 | /* assert(xhv_array != 0) */ |
1122 | oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max]; |
bbce6d69 |
1123 | for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) { |
fde52b5c |
1124 | if (HeHASH(entry) != hash) /* strings can't be equal */ |
1125 | continue; |
1126 | if (HeKLEN(entry) != len) |
1127 | continue; |
36477c24 |
1128 | if (memNE(HeKEY(entry),str,len)) /* is this it? */ |
fde52b5c |
1129 | continue; |
1130 | found = 1; |
bbce6d69 |
1131 | if (--HeVAL(entry) == Nullsv) { |
1132 | *oentry = HeNEXT(entry); |
1133 | if (i && !*oentry) |
1134 | xhv->xhv_fill--; |
ff68c719 |
1135 | Safefree(HeKEY_hek(entry)); |
bbce6d69 |
1136 | del_he(entry); |
1137 | --xhv->xhv_keys; |
fde52b5c |
1138 | } |
bbce6d69 |
1139 | break; |
fde52b5c |
1140 | } |
bbce6d69 |
1141 | |
1142 | if (!found) |
1143 | warn("Attempt to free non-existent shared string"); |
fde52b5c |
1144 | } |
1145 | |
bbce6d69 |
1146 | /* get a (constant) string ptr from the global string table |
1147 | * string will get added if it is not already there. |
fde52b5c |
1148 | * len and hash must both be valid for str. |
1149 | */ |
bbce6d69 |
1150 | HEK * |
8ac85365 |
1151 | share_hek(char *str, I32 len, register U32 hash) |
fde52b5c |
1152 | { |
1153 | register XPVHV* xhv; |
1154 | register HE *entry; |
1155 | register HE **oentry; |
1156 | register I32 i = 1; |
1157 | I32 found = 0; |
bbce6d69 |
1158 | |
fde52b5c |
1159 | /* what follows is the moral equivalent of: |
bbce6d69 |
1160 | |
1161 | if (!(Svp = hv_fetch(strtab, str, len, FALSE))) |
1162 | hv_store(strtab, str, len, Nullsv, hash); |
1163 | */ |
fde52b5c |
1164 | xhv = (XPVHV*)SvANY(strtab); |
1165 | /* assert(xhv_array != 0) */ |
1166 | oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max]; |
bbce6d69 |
1167 | for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) { |
fde52b5c |
1168 | if (HeHASH(entry) != hash) /* strings can't be equal */ |
1169 | continue; |
1170 | if (HeKLEN(entry) != len) |
1171 | continue; |
36477c24 |
1172 | if (memNE(HeKEY(entry),str,len)) /* is this it? */ |
fde52b5c |
1173 | continue; |
1174 | found = 1; |
fde52b5c |
1175 | break; |
1176 | } |
bbce6d69 |
1177 | if (!found) { |
1178 | entry = new_he(); |
ff68c719 |
1179 | HeKEY_hek(entry) = save_hek(str, len, hash); |
bbce6d69 |
1180 | HeVAL(entry) = Nullsv; |
1181 | HeNEXT(entry) = *oentry; |
1182 | *oentry = entry; |
1183 | xhv->xhv_keys++; |
1184 | if (i) { /* initial entry? */ |
1185 | ++xhv->xhv_fill; |
1186 | if (xhv->xhv_keys > xhv->xhv_max) |
1187 | hsplit(strtab); |
1188 | } |
1189 | } |
1190 | |
1191 | ++HeVAL(entry); /* use value slot as REFCNT */ |
ff68c719 |
1192 | return HeKEY_hek(entry); |
fde52b5c |
1193 | } |
1194 | |
bbce6d69 |
1195 | |
61c8b479 |
1196 | |