forked from Perl/perl5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhv.c
3890 lines (3377 loc) · 111 KB
/
hv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* hv.c
*
* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
* 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
/*
* I sit beside the fire and think
* of all that I have seen.
* --Bilbo
*
* [p.278 of _The Lord of the Rings_, II/iii: "The Ring Goes South"]
*/
/*
=head1 HV Handling
A HV structure represents a Perl hash. It consists mainly of an array
of pointers, each of which points to a linked list of HE structures. The
array is indexed by the hash function of the key, so each linked list
represents all the hash entries with the same hash value. Each HE contains
a pointer to the actual value, plus a pointer to a HEK structure which
holds the key and hash value.
=cut
*/
#include "EXTERN.h"
#define PERL_IN_HV_C
#define PERL_HASH_INTERNAL_ACCESS
#include "perl.h"
/* we split when we collide and we have a load factor over 0.667.
* NOTE if you change this formula so we split earlier than previously
* you MUST change the logic in hv_ksplit()
*/
#define DO_HSPLIT(xhv) ( ((xhv)->xhv_keys + ((xhv)->xhv_keys >> 1)) > (xhv)->xhv_max )
static const char S_strtab_error[]
= "Cannot modify shared string table in hv_%s";
#ifdef PURIFY
#define new_HE() (HE*)safemalloc(sizeof(HE))
#define del_HE(p) safefree((char*)p)
#else
STATIC HE*
S_new_he(pTHX)
{
HE* he;
void ** const root = &PL_body_roots[HE_SVSLOT];
if (!*root)
Perl_more_bodies(aTHX_ HE_SVSLOT, sizeof(HE), PERL_ARENA_SIZE);
he = (HE*) *root;
assert(he);
*root = HeNEXT(he);
return he;
}
#define new_HE() new_he()
#define del_HE(p) \
STMT_START { \
HeNEXT(p) = (HE*)(PL_body_roots[HE_SVSLOT]); \
PL_body_roots[HE_SVSLOT] = p; \
} STMT_END
#endif
STATIC HEK *
S_save_hek_flags(const char *str, I32 len, U32 hash, int flags)
{
const int flags_masked = flags & HVhek_MASK;
char *k;
HEK *hek;
PERL_ARGS_ASSERT_SAVE_HEK_FLAGS;
Newx(k, HEK_BASESIZE + len + 2, char);
hek = (HEK*)k;
Copy(str, HEK_KEY(hek), len, char);
HEK_KEY(hek)[len] = 0;
HEK_LEN(hek) = len;
HEK_HASH(hek) = hash;
HEK_FLAGS(hek) = (unsigned char)flags_masked | HVhek_UNSHARED;
if (flags & HVhek_FREEKEY)
Safefree(str);
return hek;
}
/* free the pool of temporary HE/HEK pairs returned by hv_fetch_ent
* for tied hashes */
void
Perl_free_tied_hv_pool(pTHX)
{
HE *he = PL_hv_fetch_ent_mh;
while (he) {
HE * const ohe = he;
Safefree(HeKEY_hek(he));
he = HeNEXT(he);
del_HE(ohe);
}
PL_hv_fetch_ent_mh = NULL;
}
#if defined(USE_ITHREADS)
HEK *
Perl_hek_dup(pTHX_ HEK *source, CLONE_PARAMS* param)
{
HEK *shared;
PERL_ARGS_ASSERT_HEK_DUP;
PERL_UNUSED_ARG(param);
if (!source)
return NULL;
shared = (HEK*)ptr_table_fetch(PL_ptr_table, source);
if (shared) {
/* We already shared this hash key. */
(void)share_hek_hek(shared);
}
else {
shared
= share_hek_flags(HEK_KEY(source), HEK_LEN(source),
HEK_HASH(source), HEK_FLAGS(source));
ptr_table_store(PL_ptr_table, source, shared);
}
return shared;
}
HE *
Perl_he_dup(pTHX_ const HE *e, bool shared, CLONE_PARAMS* param)
{
HE *ret;
PERL_ARGS_ASSERT_HE_DUP;
if (!e)
return NULL;
/* look for it in the table first */
ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
if (ret)
return ret;
/* create anew and remember what it is */
ret = new_HE();
ptr_table_store(PL_ptr_table, e, ret);
HeNEXT(ret) = he_dup(HeNEXT(e),shared, param);
if (HeKLEN(e) == HEf_SVKEY) {
char *k;
Newx(k, HEK_BASESIZE + sizeof(const SV *), char);
HeKEY_hek(ret) = (HEK*)k;
HeKEY_sv(ret) = sv_dup_inc(HeKEY_sv(e), param);
}
else if (shared) {
/* This is hek_dup inlined, which seems to be important for speed
reasons. */
HEK * const source = HeKEY_hek(e);
HEK *shared = (HEK*)ptr_table_fetch(PL_ptr_table, source);
if (shared) {
/* We already shared this hash key. */
(void)share_hek_hek(shared);
}
else {
shared
= share_hek_flags(HEK_KEY(source), HEK_LEN(source),
HEK_HASH(source), HEK_FLAGS(source));
ptr_table_store(PL_ptr_table, source, shared);
}
HeKEY_hek(ret) = shared;
}
else
HeKEY_hek(ret) = save_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
HeKFLAGS(e));
HeVAL(ret) = sv_dup_inc(HeVAL(e), param);
return ret;
}
#endif /* USE_ITHREADS */
static void
S_hv_notallowed(pTHX_ int flags, const char *key, I32 klen,
const char *msg)
{
SV * const sv = sv_newmortal();
PERL_ARGS_ASSERT_HV_NOTALLOWED;
if (!(flags & HVhek_FREEKEY)) {
sv_setpvn(sv, key, klen);
}
else {
/* Need to free saved eventually assign to mortal SV */
/* XXX is this line an error ???: SV *sv = sv_newmortal(); */
sv_usepvn(sv, (char *) key, klen);
}
if (flags & HVhek_UTF8) {
SvUTF8_on(sv);
}
Perl_croak(aTHX_ msg, SVfARG(sv));
}
/* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
* contains an SV* */
/*
=for apidoc hv_store
Stores an SV in a hash. The hash key is specified as C<key> and the
absolute value of C<klen> is the length of the key. If C<klen> is
negative the key is assumed to be in UTF-8-encoded Unicode. The
C<hash> parameter is the precomputed hash value; if it is zero then
Perl will compute it.
The return value will be
C<NULL> if the operation failed or if the value did not need to be actually
stored within the hash (as in the case of tied hashes). Otherwise it can
be dereferenced to get the original C<SV*>. Note that the caller is
responsible for suitably incrementing the reference count of C<val> before
the call, and decrementing it if the function returned C<NULL>. Effectively
a successful C<hv_store> takes ownership of one reference to C<val>. This is
usually what you want; a newly created SV has a reference count of one, so
if all your code does is create SVs then store them in a hash, C<hv_store>
will own the only reference to the new SV, and your code doesn't need to do
anything further to tidy up. C<hv_store> is not implemented as a call to
C<hv_store_ent>, and does not create a temporary SV for the key, so if your
key data is not already in SV form then use C<hv_store> in preference to
C<hv_store_ent>.
See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
information on how to use this function on tied hashes.
=for apidoc hv_store_ent
Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
parameter is the precomputed hash value; if it is zero then Perl will
compute it. The return value is the new hash entry so created. It will be
C<NULL> if the operation failed or if the value did not need to be actually
stored within the hash (as in the case of tied hashes). Otherwise the
contents of the return value can be accessed using the C<He?> macros
described here. Note that the caller is responsible for suitably
incrementing the reference count of C<val> before the call, and
decrementing it if the function returned NULL. Effectively a successful
C<hv_store_ent> takes ownership of one reference to C<val>. This is
usually what you want; a newly created SV has a reference count of one, so
if all your code does is create SVs then store them in a hash, C<hv_store>
will own the only reference to the new SV, and your code doesn't need to do
anything further to tidy up. Note that C<hv_store_ent> only reads the C<key>;
unlike C<val> it does not take ownership of it, so maintaining the correct
reference count on C<key> is entirely the caller's responsibility. The reason
it does not take ownership, is that C<key> is not used after this function
returns, and so can be freed immediately. C<hv_store>
is not implemented as a call to C<hv_store_ent>, and does not create a temporary
SV for the key, so if your key data is not already in SV form then use
C<hv_store> in preference to C<hv_store_ent>.
See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
information on how to use this function on tied hashes.
=for apidoc hv_exists
Returns a boolean indicating whether the specified hash key exists. The
absolute value of C<klen> is the length of the key. If C<klen> is
negative the key is assumed to be in UTF-8-encoded Unicode.
=for apidoc hv_fetch
Returns the SV which corresponds to the specified key in the hash.
The absolute value of C<klen> is the length of the key. If C<klen> is
negative the key is assumed to be in UTF-8-encoded Unicode. If
C<lval> is set then the fetch will be part of a store. This means that if
there is no value in the hash associated with the given key, then one is
created and a pointer to it is returned. The C<SV*> it points to can be
assigned to. But always check that the
return value is non-null before dereferencing it to an C<SV*>.
See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
information on how to use this function on tied hashes.
=for apidoc hv_exists_ent
Returns a boolean indicating whether
the specified hash key exists. C<hash>
can be a valid precomputed hash value, or 0 to ask for it to be
computed.
=cut
*/
/* returns an HE * structure with the all fields set */
/* note that hent_val will be a mortal sv for MAGICAL hashes */
/*
=for apidoc hv_fetch_ent
Returns the hash entry which corresponds to the specified key in the hash.
C<hash> must be a valid precomputed hash number for the given C<key>, or 0
if you want the function to compute it. IF C<lval> is set then the fetch
will be part of a store. Make sure the return value is non-null before
accessing it. The return value when C<hv> is a tied hash is a pointer to a
static location, so be sure to make a copy of the structure if you need to
store it somewhere.
See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
information on how to use this function on tied hashes.
=cut
*/
/* Common code for hv_delete()/hv_exists()/hv_fetch()/hv_store() */
void *
Perl_hv_common_key_len(pTHX_ HV *hv, const char *key, I32 klen_i32,
const int action, SV *val, const U32 hash)
{
STRLEN klen;
int flags;
PERL_ARGS_ASSERT_HV_COMMON_KEY_LEN;
if (klen_i32 < 0) {
klen = -klen_i32;
flags = HVhek_UTF8;
} else {
klen = klen_i32;
flags = 0;
}
return hv_common(hv, NULL, key, klen, flags, action, val, hash);
}
void *
Perl_hv_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
int flags, int action, SV *val, U32 hash)
{
XPVHV* xhv;
HE *entry;
HE **oentry;
SV *sv;
bool is_utf8;
bool in_collision;
int masked_flags;
const int return_svp = action & HV_FETCH_JUST_SV;
HEK *keysv_hek = NULL;
if (!hv)
return NULL;
if (SvTYPE(hv) == (svtype)SVTYPEMASK)
return NULL;
assert(SvTYPE(hv) == SVt_PVHV);
if (SvSMAGICAL(hv) && SvGMAGICAL(hv) && !(action & HV_DISABLE_UVAR_XKEY)) {
MAGIC* mg;
if ((mg = mg_find((const SV *)hv, PERL_MAGIC_uvar))) {
struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
if (uf->uf_set == NULL) {
SV* obj = mg->mg_obj;
if (!keysv) {
keysv = newSVpvn_flags(key, klen, SVs_TEMP |
((flags & HVhek_UTF8)
? SVf_UTF8 : 0));
}
mg->mg_obj = keysv; /* pass key */
uf->uf_index = action; /* pass action */
magic_getuvar(MUTABLE_SV(hv), mg);
keysv = mg->mg_obj; /* may have changed */
mg->mg_obj = obj;
/* If the key may have changed, then we need to invalidate
any passed-in computed hash value. */
hash = 0;
}
}
}
if (keysv) {
if (flags & HVhek_FREEKEY)
Safefree(key);
key = SvPV_const(keysv, klen);
is_utf8 = (SvUTF8(keysv) != 0);
if (SvIsCOW_shared_hash(keysv)) {
flags = HVhek_KEYCANONICAL | (is_utf8 ? HVhek_UTF8 : 0);
} else {
flags = 0;
}
} else {
is_utf8 = cBOOL(flags & HVhek_UTF8);
}
if (action & HV_DELETE) {
return (void *) hv_delete_common(hv, keysv, key, klen,
flags | (is_utf8 ? HVhek_UTF8 : 0),
action, hash);
}
xhv = (XPVHV*)SvANY(hv);
if (SvMAGICAL(hv)) {
if (SvRMAGICAL(hv) && !(action & (HV_FETCH_ISSTORE|HV_FETCH_ISEXISTS))) {
if (mg_find((const SV *)hv, PERL_MAGIC_tied)
|| SvGMAGICAL((const SV *)hv))
{
/* FIXME should be able to skimp on the HE/HEK here when
HV_FETCH_JUST_SV is true. */
if (!keysv) {
keysv = newSVpvn_utf8(key, klen, is_utf8);
} else {
keysv = newSVsv(keysv);
}
sv = sv_newmortal();
mg_copy(MUTABLE_SV(hv), sv, (char *)keysv, HEf_SVKEY);
/* grab a fake HE/HEK pair from the pool or make a new one */
entry = PL_hv_fetch_ent_mh;
if (entry)
PL_hv_fetch_ent_mh = HeNEXT(entry);
else {
char *k;
entry = new_HE();
Newx(k, HEK_BASESIZE + sizeof(const SV *), char);
HeKEY_hek(entry) = (HEK*)k;
}
HeNEXT(entry) = NULL;
HeSVKEY_set(entry, keysv);
HeVAL(entry) = sv;
sv_upgrade(sv, SVt_PVLV);
LvTYPE(sv) = 'T';
/* so we can free entry when freeing sv */
LvTARG(sv) = MUTABLE_SV(entry);
/* XXX remove at some point? */
if (flags & HVhek_FREEKEY)
Safefree(key);
if (return_svp) {
return entry ? (void *) &HeVAL(entry) : NULL;
}
return (void *) entry;
}
#ifdef ENV_IS_CASELESS
else if (mg_find((const SV *)hv, PERL_MAGIC_env)) {
U32 i;
for (i = 0; i < klen; ++i)
if (isLOWER(key[i])) {
/* Would be nice if we had a routine to do the
copy and upercase in a single pass through. */
const char * const nkey = strupr(savepvn(key,klen));
/* Note that this fetch is for nkey (the uppercased
key) whereas the store is for key (the original) */
void *result = hv_common(hv, NULL, nkey, klen,
HVhek_FREEKEY, /* free nkey */
0 /* non-LVAL fetch */
| HV_DISABLE_UVAR_XKEY
| return_svp,
NULL /* no value */,
0 /* compute hash */);
if (!result && (action & HV_FETCH_LVALUE)) {
/* This call will free key if necessary.
Do it this way to encourage compiler to tail
call optimise. */
result = hv_common(hv, keysv, key, klen, flags,
HV_FETCH_ISSTORE
| HV_DISABLE_UVAR_XKEY
| return_svp,
newSV(0), hash);
} else {
if (flags & HVhek_FREEKEY)
Safefree(key);
}
return result;
}
}
#endif
} /* ISFETCH */
else if (SvRMAGICAL(hv) && (action & HV_FETCH_ISEXISTS)) {
if (mg_find((const SV *)hv, PERL_MAGIC_tied)
|| SvGMAGICAL((const SV *)hv)) {
/* I don't understand why hv_exists_ent has svret and sv,
whereas hv_exists only had one. */
SV * const svret = sv_newmortal();
sv = sv_newmortal();
if (keysv || is_utf8) {
if (!keysv) {
keysv = newSVpvn_utf8(key, klen, TRUE);
} else {
keysv = newSVsv(keysv);
}
mg_copy(MUTABLE_SV(hv), sv, (char *)sv_2mortal(keysv), HEf_SVKEY);
} else {
mg_copy(MUTABLE_SV(hv), sv, key, klen);
}
if (flags & HVhek_FREEKEY)
Safefree(key);
{
MAGIC * const mg = mg_find(sv, PERL_MAGIC_tiedelem);
if (mg)
magic_existspack(svret, mg);
}
/* This cast somewhat evil, but I'm merely using NULL/
not NULL to return the boolean exists.
And I know hv is not NULL. */
return SvTRUE_NN(svret) ? (void *)hv : NULL;
}
#ifdef ENV_IS_CASELESS
else if (mg_find((const SV *)hv, PERL_MAGIC_env)) {
/* XXX This code isn't UTF8 clean. */
char * const keysave = (char * const)key;
/* Will need to free this, so set FREEKEY flag. */
key = savepvn(key,klen);
key = (const char*)strupr((char*)key);
is_utf8 = FALSE;
hash = 0;
keysv = 0;
if (flags & HVhek_FREEKEY) {
Safefree(keysave);
}
flags |= HVhek_FREEKEY;
}
#endif
} /* ISEXISTS */
else if (action & HV_FETCH_ISSTORE) {
bool needs_copy;
bool needs_store;
hv_magic_check (hv, &needs_copy, &needs_store);
if (needs_copy) {
const bool save_taint = TAINT_get;
if (keysv || is_utf8) {
if (!keysv) {
keysv = newSVpvn_utf8(key, klen, TRUE);
}
if (TAINTING_get)
TAINT_set(SvTAINTED(keysv));
keysv = sv_2mortal(newSVsv(keysv));
mg_copy(MUTABLE_SV(hv), val, (char*)keysv, HEf_SVKEY);
} else {
mg_copy(MUTABLE_SV(hv), val, key, klen);
}
TAINT_IF(save_taint);
#ifdef NO_TAINT_SUPPORT
PERL_UNUSED_VAR(save_taint);
#endif
if (!needs_store) {
if (flags & HVhek_FREEKEY)
Safefree(key);
return NULL;
}
#ifdef ENV_IS_CASELESS
else if (mg_find((const SV *)hv, PERL_MAGIC_env)) {
/* XXX This code isn't UTF8 clean. */
const char *keysave = key;
/* Will need to free this, so set FREEKEY flag. */
key = savepvn(key,klen);
key = (const char*)strupr((char*)key);
is_utf8 = FALSE;
hash = 0;
keysv = 0;
if (flags & HVhek_FREEKEY) {
Safefree(keysave);
}
flags |= HVhek_FREEKEY;
}
#endif
}
} /* ISSTORE */
} /* SvMAGICAL */
if (!HvARRAY(hv)) {
if ((action & (HV_FETCH_LVALUE | HV_FETCH_ISSTORE))
#ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
|| (SvRMAGICAL((const SV *)hv)
&& mg_find((const SV *)hv, PERL_MAGIC_env))
#endif
) {
char *array;
Newxz(array,
PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
char);
HvARRAY(hv) = (HE**)array;
}
#ifdef DYNAMIC_ENV_FETCH
else if (action & HV_FETCH_ISEXISTS) {
/* for an %ENV exists, if we do an insert it's by a recursive
store call, so avoid creating HvARRAY(hv) right now. */
}
#endif
else {
/* XXX remove at some point? */
if (flags & HVhek_FREEKEY)
Safefree(key);
return NULL;
}
}
if (is_utf8 && !(flags & HVhek_KEYCANONICAL)) {
char * const keysave = (char *)key;
key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
if (is_utf8)
flags |= HVhek_UTF8;
else
flags &= ~HVhek_UTF8;
if (key != keysave) {
if (flags & HVhek_FREEKEY)
Safefree(keysave);
flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
/* If the caller calculated a hash, it was on the sequence of
octets that are the UTF-8 form. We've now changed the sequence
of octets stored to that of the equivalent byte representation,
so the hash we need is different. */
hash = 0;
}
}
if (keysv && (SvIsCOW_shared_hash(keysv))) {
if (HvSHAREKEYS(hv))
keysv_hek = SvSHARED_HEK_FROM_PV(SvPVX_const(keysv));
hash = SvSHARED_HASH(keysv);
}
else if (!hash)
PERL_HASH(hash, key, klen);
masked_flags = (flags & HVhek_MASK);
#ifdef DYNAMIC_ENV_FETCH
if (!HvARRAY(hv)) entry = NULL;
else
#endif
{
entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)];
}
if (!entry)
goto not_found;
if (keysv_hek) {
/* keysv is actually a HEK in disguise, so we can match just by
* comparing the HEK pointers in the HE chain. There is a slight
* caveat: on something like "\x80", which has both plain and utf8
* representations, perl's hashes do encoding-insensitive lookups,
* but preserve the encoding of the stored key. Thus a particular
* key could map to two different HEKs in PL_strtab. We only
* conclude 'not found' if all the flags are the same; otherwise
* we fall back to a full search (this should only happen in rare
* cases).
*/
int keysv_flags = HEK_FLAGS(keysv_hek);
HE *orig_entry = entry;
for (; entry; entry = HeNEXT(entry)) {
HEK *hek = HeKEY_hek(entry);
if (hek == keysv_hek)
goto found;
if (HEK_FLAGS(hek) != keysv_flags)
break; /* need to do full match */
}
if (!entry)
goto not_found;
/* failed on shortcut - do full search loop */
entry = orig_entry;
}
for (; entry; entry = HeNEXT(entry)) {
if (HeHASH(entry) != hash) /* strings can't be equal */
continue;
if (HeKLEN(entry) != (I32)klen)
continue;
if (memNE(HeKEY(entry),key,klen)) /* is this it? */
continue;
if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
continue;
found:
if (action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE)) {
if (HeKFLAGS(entry) != masked_flags) {
/* We match if HVhek_UTF8 bit in our flags and hash key's
match. But if entry was set previously with HVhek_WASUTF8
and key now doesn't (or vice versa) then we should change
the key's flag, as this is assignment. */
if (HvSHAREKEYS(hv)) {
/* Need to swap the key we have for a key with the flags we
need. As keys are shared we can't just write to the
flag, so we share the new one, unshare the old one. */
HEK * const new_hek = share_hek_flags(key, klen, hash,
masked_flags);
unshare_hek (HeKEY_hek(entry));
HeKEY_hek(entry) = new_hek;
}
else if (hv == PL_strtab) {
/* PL_strtab is usually the only hash without HvSHAREKEYS,
so putting this test here is cheap */
if (flags & HVhek_FREEKEY)
Safefree(key);
Perl_croak(aTHX_ S_strtab_error,
action & HV_FETCH_LVALUE ? "fetch" : "store");
}
else
HeKFLAGS(entry) = masked_flags;
if (masked_flags & HVhek_ENABLEHVKFLAGS)
HvHASKFLAGS_on(hv);
}
if (HeVAL(entry) == &PL_sv_placeholder) {
/* yes, can store into placeholder slot */
if (action & HV_FETCH_LVALUE) {
if (SvMAGICAL(hv)) {
/* This preserves behaviour with the old hv_fetch
implementation which at this point would bail out
with a break; (at "if we find a placeholder, we
pretend we haven't found anything")
That break mean that if a placeholder were found, it
caused a call into hv_store, which in turn would
check magic, and if there is no magic end up pretty
much back at this point (in hv_store's code). */
break;
}
/* LVAL fetch which actually needs a store. */
val = newSV(0);
HvPLACEHOLDERS(hv)--;
} else {
/* store */
if (val != &PL_sv_placeholder)
HvPLACEHOLDERS(hv)--;
}
HeVAL(entry) = val;
} else if (action & HV_FETCH_ISSTORE) {
SvREFCNT_dec(HeVAL(entry));
HeVAL(entry) = val;
}
} else if (HeVAL(entry) == &PL_sv_placeholder) {
/* if we find a placeholder, we pretend we haven't found
anything */
break;
}
if (flags & HVhek_FREEKEY)
Safefree(key);
if (return_svp) {
return (void *) &HeVAL(entry);
}
return entry;
}
not_found:
#ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
if (!(action & HV_FETCH_ISSTORE)
&& SvRMAGICAL((const SV *)hv)
&& mg_find((const SV *)hv, PERL_MAGIC_env)) {
unsigned long len;
const char * const env = PerlEnv_ENVgetenv_len(key,&len);
if (env) {
sv = newSVpvn(env,len);
SvTAINTED_on(sv);
return hv_common(hv, keysv, key, klen, flags,
HV_FETCH_ISSTORE|HV_DISABLE_UVAR_XKEY|return_svp,
sv, hash);
}
}
#endif
if (!entry && SvREADONLY(hv) && !(action & HV_FETCH_ISEXISTS)) {
hv_notallowed(flags, key, klen,
"Attempt to access disallowed key '%" SVf "' in"
" a restricted hash");
}
if (!(action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE))) {
/* Not doing some form of store, so return failure. */
if (flags & HVhek_FREEKEY)
Safefree(key);
return NULL;
}
if (action & HV_FETCH_LVALUE) {
val = action & HV_FETCH_EMPTY_HE ? NULL : newSV(0);
if (SvMAGICAL(hv)) {
/* At this point the old hv_fetch code would call to hv_store,
which in turn might do some tied magic. So we need to make that
magic check happen. */
/* gonna assign to this, so it better be there */
/* If a fetch-as-store fails on the fetch, then the action is to
recurse once into "hv_store". If we didn't do this, then that
recursive call would call the key conversion routine again.
However, as we replace the original key with the converted
key, this would result in a double conversion, which would show
up as a bug if the conversion routine is not idempotent.
Hence the use of HV_DISABLE_UVAR_XKEY. */
return hv_common(hv, keysv, key, klen, flags,
HV_FETCH_ISSTORE|HV_DISABLE_UVAR_XKEY|return_svp,
val, hash);
/* XXX Surely that could leak if the fetch-was-store fails?
Just like the hv_fetch. */
}
}
/* Welcome to hv_store... */
if (!HvARRAY(hv)) {
/* Not sure if we can get here. I think the only case of oentry being
NULL is for %ENV with dynamic env fetch. But that should disappear
with magic in the previous code. */
char *array;
Newxz(array,
PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
char);
HvARRAY(hv) = (HE**)array;
}
oentry = &(HvARRAY(hv))[hash & (I32) xhv->xhv_max];
entry = new_HE();
/* share_hek_flags will do the free for us. This might be considered
bad API design. */
if (HvSHAREKEYS(hv))
HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
else if (hv == PL_strtab) {
/* PL_strtab is usually the only hash without HvSHAREKEYS, so putting
this test here is cheap */
if (flags & HVhek_FREEKEY)
Safefree(key);
Perl_croak(aTHX_ S_strtab_error,
action & HV_FETCH_LVALUE ? "fetch" : "store");
}
else /* gotta do the real thing */
HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
HeVAL(entry) = val;
#ifdef PERL_HASH_RANDOMIZE_KEYS
/* This logic semi-randomizes the insert order in a bucket.
* Either we insert into the top, or the slot below the top,
* making it harder to see if there is a collision. We also
* reset the iterator randomizer if there is one.
*/
in_collision = *oentry != NULL;
if ( *oentry && PL_HASH_RAND_BITS_ENABLED) {
PL_hash_rand_bits++;
PL_hash_rand_bits= ROTL_UV(PL_hash_rand_bits,1);
if ( PL_hash_rand_bits & 1 ) {
HeNEXT(entry) = HeNEXT(*oentry);
HeNEXT(*oentry) = entry;
} else {
HeNEXT(entry) = *oentry;
*oentry = entry;
}
} else
#endif
{
HeNEXT(entry) = *oentry;
*oentry = entry;
}
#ifdef PERL_HASH_RANDOMIZE_KEYS
if (SvOOK(hv)) {
/* Currently this makes various tests warn in annoying ways.
* So Silenced for now. - Yves | bogus end of comment =>* /
if (HvAUX(hv)->xhv_riter != -1) {
Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
"[TESTING] Inserting into a hash during each() traversal results in undefined behavior"
pTHX__FORMAT
pTHX__VALUE);
}
*/
if (PL_HASH_RAND_BITS_ENABLED) {
if (PL_HASH_RAND_BITS_ENABLED == 1)
PL_hash_rand_bits += (PTRV)entry + 1; /* we don't bother to use ptr_hash here */
PL_hash_rand_bits= ROTL_UV(PL_hash_rand_bits,1);
}
HvAUX(hv)->xhv_rand= (U32)PL_hash_rand_bits;
}
#endif
if (val == &PL_sv_placeholder)
HvPLACEHOLDERS(hv)++;
if (masked_flags & HVhek_ENABLEHVKFLAGS)
HvHASKFLAGS_on(hv);
xhv->xhv_keys++; /* HvTOTALKEYS(hv)++ */
if ( in_collision && DO_HSPLIT(xhv) ) {
const STRLEN oldsize = xhv->xhv_max + 1;
const U32 items = (U32)HvPLACEHOLDERS_get(hv);
if (items /* hash has placeholders */
&& !SvREADONLY(hv) /* but is not a restricted hash */) {
/* If this hash previously was a "restricted hash" and had
placeholders, but the "restricted" flag has been turned off,
then the placeholders no longer serve any useful purpose.
However, they have the downsides of taking up RAM, and adding
extra steps when finding used values. It's safe to clear them
at this point, even though Storable rebuilds restricted hashes by
putting in all the placeholders (first) before turning on the
readonly flag, because Storable always pre-splits the hash.
If we're lucky, then we may clear sufficient placeholders to
avoid needing to split the hash at all. */
clear_placeholders(hv, items);
if (DO_HSPLIT(xhv))
hsplit(hv, oldsize, oldsize * 2);
} else
hsplit(hv, oldsize, oldsize * 2);
}
if (return_svp) {
return entry ? (void *) &HeVAL(entry) : NULL;
}
return (void *) entry;
}
STATIC void
S_hv_magic_check(HV *hv, bool *needs_copy, bool *needs_store)
{
const MAGIC *mg = SvMAGIC(hv);
PERL_ARGS_ASSERT_HV_MAGIC_CHECK;
*needs_copy = FALSE;
*needs_store = TRUE;
while (mg) {
if (isUPPER(mg->mg_type)) {
*needs_copy = TRUE;
if (mg->mg_type == PERL_MAGIC_tied) {
*needs_store = FALSE;
return; /* We've set all there is to set. */
}
}
mg = mg->mg_moremagic;
}
}
/*
=for apidoc hv_scalar
Evaluates the hash in scalar context and returns the result.
When the hash is tied dispatches through to the SCALAR method,
otherwise returns a mortal SV containing the number of keys
in the hash.
Note, prior to 5.25 this function returned what is now
returned by the hv_bucket_ratio() function.
=cut
*/
SV *
Perl_hv_scalar(pTHX_ HV *hv)
{
SV *sv;
PERL_ARGS_ASSERT_HV_SCALAR;
if (SvRMAGICAL(hv)) {
MAGIC * const mg = mg_find((const SV *)hv, PERL_MAGIC_tied);
if (mg)
return magic_scalarpack(hv, mg);
}
sv = sv_newmortal();
sv_setuv(sv, HvUSEDKEYS(hv));
return sv;
}
/*
hv_pushkv(): push all the keys and/or values of a hash onto the stack.
The rough Perl equivalents:
() = %hash;
() = keys %hash;
() = values %hash;
Resets the hash's iterator.
flags : 1 = push keys
2 = push values
1|2 = push keys and values
XXX use symbolic flag constants at some point?
I might unroll the non-tied hv_iternext() in here at some point - DAPM
*/
void
Perl_hv_pushkv(pTHX_ HV *hv, U32 flags)
{
HE *entry;
bool tied = SvRMAGICAL(hv) && (mg_find(MUTABLE_SV(hv), PERL_MAGIC_tied)
#ifdef DYNAMIC_ENV_FETCH /* might not know number of keys yet */
|| mg_find(MUTABLE_SV(hv), PERL_MAGIC_env)
#endif
);
dSP;
PERL_ARGS_ASSERT_HV_PUSHKV;
assert(flags); /* must be pushing at least one of keys and values */