This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
for pod/perlfaq2.pod against latest snapshot for Alpaca
[perl5.git] / hv.h
CommitLineData
a0d0e21e 1/* hv.h
79072805 2 *
4bb101f2
JH
3 * Copyright (C) 1991, 1992, 1993, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 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 *
79072805
LW
9 */
10
5cbe4eec 11/* typedefs to eliminate some typing */
79072805 12typedef struct he HE;
ff68c719 13typedef struct hek HEK;
79072805 14
5cbe4eec 15/* entry in hash value chain */
79072805 16struct he {
5cbe4eec
MLF
17 HE *hent_next; /* next entry in chain */
18 HEK *hent_hek; /* hash key */
19 SV *hent_val; /* scalar value that was hashed */
bbce6d69 20};
21
5cbe4eec 22/* hash key -- defined separately for use as shared pointer */
ff68c719 23struct hek {
5cbe4eec
MLF
24 U32 hek_hash; /* hash of key */
25 I32 hek_len; /* length of hash key */
26 char hek_key[1]; /* variable-length hash key */
e05949c7 27 /* the hash-key is \0-terminated */
ddac11c4 28 /* after the \0 there is a byte for flags, such as whether the key is
19692e8d 29 UTF8 */
79072805
LW
30};
31
5cbe4eec 32/* hash structure: */
6ee623d5 33/* This structure must match the beginning of struct xpvmg in sv.h. */
79072805 34struct xpvhv {
463ee0b2
LW
35 char * xhv_array; /* pointer to malloced string */
36 STRLEN xhv_fill; /* how full xhv_array currently is */
37 STRLEN xhv_max; /* subscript of last element of xhv_array */
6ee623d5 38 IV xhv_keys; /* how many elements in the array */
65202027 39 NV xnv_nv; /* numeric value, if any */
8aacddc1 40#define xhv_placeholders xnv_nv
79072805
LW
41 MAGIC* xmg_magic; /* magic for scalar array */
42 HV* xmg_stash; /* class package */
43
79072805
LW
44 I32 xhv_riter; /* current root of iterator */
45 HE *xhv_eiter; /* current entry of iterator */
46 PMOP *xhv_pmroot; /* list of pm's for this package */
47 char *xhv_name; /* name, if a symbol table */
79072805
LW
48};
49
5cbe4eec 50/* hash a key */
5afd6d42
JH
51/* FYI: This is the "One-at-a-Time" algorithm by Bob Jenkins
52 * from requirements by Colin Plumb.
53 * (http://burtleburtle.net/bob/hash/doobs.html) */
54/* The use of a temporary pointer and the casting games
55 * is needed to serve the dual purposes of
56 * (a) the hashed data being interpreted as "unsigned char" (new since 5.8,
57 * a "char" can be either signed or signed, depending on the compiler)
58 * (b) catering for old code that uses a "char"
504f80c1
JH
59 * The "hash seed" feature was added in Perl 5.8.1 to perturb the results
60 * to avoid "algorithmic complexity attacks".
5afd6d42 61 */
504f80c1
JH
62#if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT)
63# define PERL_HASH_SEED PL_hash_seed
64#else
65# define PERL_HASH_SEED 0
66#endif
bf6bd887 67#define PERL_HASH(hash,str,len) \
68 STMT_START { \
5afd6d42
JH
69 register const char *s_PeRlHaSh_tmp = str; \
70 register const unsigned char *s_PeRlHaSh = (const unsigned char *)s_PeRlHaSh_tmp; \
bf6bd887 71 register I32 i_PeRlHaSh = len; \
504f80c1 72 register U32 hash_PeRlHaSh = PERL_HASH_SEED; \
a6fe520e
MLF
73 while (i_PeRlHaSh--) { \
74 hash_PeRlHaSh += *s_PeRlHaSh++; \
75 hash_PeRlHaSh += (hash_PeRlHaSh << 10); \
76 hash_PeRlHaSh ^= (hash_PeRlHaSh >> 6); \
77 } \
78 hash_PeRlHaSh += (hash_PeRlHaSh << 3); \
79 hash_PeRlHaSh ^= (hash_PeRlHaSh >> 11); \
4fee5c71 80 (hash) = (hash_PeRlHaSh + (hash_PeRlHaSh << 15)); \
bf6bd887 81 } STMT_END
82
954c1994 83/*
ccfc67b7
JH
84=head1 Hash Manipulation Functions
85
954c1994
GS
86=for apidoc AmU||HEf_SVKEY
87This flag, used in the length slot of hash entries and magic structures,
d1be9408 88specifies the structure contains an C<SV*> pointer where a C<char*> pointer
954c1994
GS
89is to be expected. (For information only--not to be used).
90
ccfc67b7
JH
91=head1 Handy Values
92
954c1994
GS
93=for apidoc AmU||Nullhv
94Null HV pointer.
95
ccfc67b7
JH
96=head1 Hash Manipulation Functions
97
954c1994
GS
98=for apidoc Am|char*|HvNAME|HV* stash
99Returns the package name of a stash. See C<SvSTASH>, C<CvSTASH>.
100
101=for apidoc Am|void*|HeKEY|HE* he
102Returns the actual pointer stored in the key slot of the hash entry. The
103pointer may be either C<char*> or C<SV*>, depending on the value of
104C<HeKLEN()>. Can be assigned to. The C<HePV()> or C<HeSVKEY()> macros are
105usually preferable for finding the value of a key.
106
107=for apidoc Am|STRLEN|HeKLEN|HE* he
108If this is negative, and amounts to C<HEf_SVKEY>, it indicates the entry
109holds an C<SV*> key. Otherwise, holds the actual length of the key. Can
110be assigned to. The C<HePV()> macro is usually preferable for finding key
111lengths.
112
113=for apidoc Am|SV*|HeVAL|HE* he
114Returns the value slot (type C<SV*>) stored in the hash entry.
115
116=for apidoc Am|U32|HeHASH|HE* he
117Returns the computed hash stored in the hash entry.
118
119=for apidoc Am|char*|HePV|HE* he|STRLEN len
120Returns the key slot of the hash entry as a C<char*> value, doing any
121necessary dereferencing of possibly C<SV*> keys. The length of the string
122is placed in C<len> (this is a macro, so do I<not> use C<&len>). If you do
123not care about what the length of the key is, you may use the global
124variable C<PL_na>, though this is rather less efficient than using a local
125variable. Remember though, that hash keys in perl are free to contain
126embedded nulls, so using C<strlen()> or similar is not a good way to find
127the length of hash keys. This is very similar to the C<SvPV()> macro
128described elsewhere in this document.
129
130=for apidoc Am|SV*|HeSVKEY|HE* he
131Returns the key as an C<SV*>, or C<Nullsv> if the hash entry does not
132contain an C<SV*> key.
133
134=for apidoc Am|SV*|HeSVKEY_force|HE* he
135Returns the key as an C<SV*>. Will create and return a temporary mortal
136C<SV*> if the hash entry contains only a C<char*> key.
137
138=for apidoc Am|SV*|HeSVKEY_set|HE* he|SV* sv
139Sets the key to a given C<SV*>, taking care to set the appropriate flags to
140indicate the presence of an C<SV*> key, and returns the same
141C<SV*>.
142
143=cut
144*/
bf6bd887 145
bf5b86ae 146/* these hash entry flags ride on hent_klen (for use only in magic/tied HVs) */
d1be9408 147#define HEf_SVKEY -2 /* hent_key is an SV* */
bf6bd887 148
149
79072805 150#define Nullhv Null(HV*)
4efcf9a2 151#define HvARRAY(hv) (*(HE***)&((XPVHV*) SvANY(hv))->xhv_array)
79072805 152#define HvFILL(hv) ((XPVHV*) SvANY(hv))->xhv_fill
463ee0b2 153#define HvMAX(hv) ((XPVHV*) SvANY(hv))->xhv_max
79072805
LW
154#define HvRITER(hv) ((XPVHV*) SvANY(hv))->xhv_riter
155#define HvEITER(hv) ((XPVHV*) SvANY(hv))->xhv_eiter
156#define HvPMROOT(hv) ((XPVHV*) SvANY(hv))->xhv_pmroot
157#define HvNAME(hv) ((XPVHV*) SvANY(hv))->xhv_name
a0d0e21e 158
8aacddc1
NIS
159/* the number of keys (including any placeholers) */
160#define XHvTOTALKEYS(xhv) ((xhv)->xhv_keys)
161
162/* The number of placeholders in the enumerated-keys hash */
205c8ad3 163#define XHvPLACEHOLDERS(xhv) ((xhv)->xhv_placeholders)
8aacddc1 164
dd2710a1
VK
165/* the number of keys that exist() (i.e. excluding placeholders) */
166#define XHvUSEDKEYS(xhv) (XHvTOTALKEYS(xhv) - (IV)XHvPLACEHOLDERS(xhv))
8aacddc1
NIS
167
168/*
169 * HvKEYS gets the number of keys that actually exist(), and is provided
170 * for backwards compatibility with old XS code. The core uses HvUSEDKEYS
171 * (keys, excluding placeholdes) and HvTOTALKEYS (including placeholders)
172 */
173#define HvKEYS(hv) XHvUSEDKEYS((XPVHV*) SvANY(hv))
174#define HvUSEDKEYS(hv) XHvUSEDKEYS((XPVHV*) SvANY(hv))
175#define HvTOTALKEYS(hv) XHvTOTALKEYS((XPVHV*) SvANY(hv))
176#define HvPLACEHOLDERS(hv) XHvPLACEHOLDERS((XPVHV*) SvANY(hv))
177
bf6bd887 178#define HvSHAREKEYS(hv) (SvFLAGS(hv) & SVphv_SHAREKEYS)
179#define HvSHAREKEYS_on(hv) (SvFLAGS(hv) |= SVphv_SHAREKEYS)
180#define HvSHAREKEYS_off(hv) (SvFLAGS(hv) &= ~SVphv_SHAREKEYS)
181
19692e8d
NC
182/* This is an optimisation flag. It won't be set if all hash keys have a 0
183 * flag. Currently the only flags relate to utf8.
184 * Hence it won't be set if all keys are 8 bit only. It will be set if any key
185 * is utf8 (including 8 bit keys that were entered as utf8, and need upgrading
186 * when retrieved during iteration. It may still be set when there are no longer
187 * any utf8 keys.
188 */
189#define HvHASKFLAGS(hv) (SvFLAGS(hv) & SVphv_HASKFLAGS)
190#define HvHASKFLAGS_on(hv) (SvFLAGS(hv) |= SVphv_HASKFLAGS)
191#define HvHASKFLAGS_off(hv) (SvFLAGS(hv) &= ~SVphv_HASKFLAGS)
574c8022 192
bf5b86ae
GS
193#define HvLAZYDEL(hv) (SvFLAGS(hv) & SVphv_LAZYDEL)
194#define HvLAZYDEL_on(hv) (SvFLAGS(hv) |= SVphv_LAZYDEL)
195#define HvLAZYDEL_off(hv) (SvFLAGS(hv) &= ~SVphv_LAZYDEL)
196
a0d0e21e
LW
197/* Maybe amagical: */
198/* #define HV_AMAGICmb(hv) (SvFLAGS(hv) & (SVpgv_badAM | SVpgv_AM)) */
199
200#define HV_AMAGIC(hv) (SvFLAGS(hv) & SVpgv_AM)
201#define HV_AMAGIC_on(hv) (SvFLAGS(hv) |= SVpgv_AM)
202#define HV_AMAGIC_off(hv) (SvFLAGS(hv) &= ~SVpgv_AM)
203
204/*
205#define HV_AMAGICbad(hv) (SvFLAGS(hv) & SVpgv_badAM)
206#define HV_badAMAGIC_on(hv) (SvFLAGS(hv) |= SVpgv_badAM)
207#define HV_badAMAGIC_off(hv) (SvFLAGS(hv) &= ~SVpgv_badAM)
208*/
bf6bd887 209
210#define Nullhe Null(HE*)
211#define HeNEXT(he) (he)->hent_next
ff68c719 212#define HeKEY_hek(he) (he)->hent_hek
213#define HeKEY(he) HEK_KEY(HeKEY_hek(he))
bbce6d69 214#define HeKEY_sv(he) (*(SV**)HeKEY(he))
ff68c719 215#define HeKLEN(he) HEK_LEN(HeKEY_hek(he))
da58a35d 216#define HeKUTF8(he) HEK_UTF8(HeKEY_hek(he))
19692e8d 217#define HeKWASUTF8(he) HEK_WASUTF8(HeKEY_hek(he))
da58a35d 218#define HeKLEN_UTF8(he) (HeKUTF8(he) ? -HeKLEN(he) : HeKLEN(he))
19692e8d 219#define HeKFLAGS(he) HEK_FLAGS(HeKEY_hek(he))
bf6bd887 220#define HeVAL(he) (he)->hent_val
ff68c719 221#define HeHASH(he) HEK_HASH(HeKEY_hek(he))
1e422769 222#define HePV(he,lp) ((HeKLEN(he) == HEf_SVKEY) ? \
223 SvPV(HeKEY_sv(he),lp) : \
224 (((lp = HeKLEN(he)) >= 0) ? \
225 HeKEY(he) : Nullch))
226
bbce6d69 227#define HeSVKEY(he) ((HeKEY(he) && \
228 HeKLEN(he) == HEf_SVKEY) ? \
229 HeKEY_sv(he) : Nullsv)
230
231#define HeSVKEY_force(he) (HeKEY(he) ? \
232 ((HeKLEN(he) == HEf_SVKEY) ? \
233 HeKEY_sv(he) : \
79cb57f6 234 sv_2mortal(newSVpvn(HeKEY(he), \
bbce6d69 235 HeKLEN(he)))) : \
3280af22 236 &PL_sv_undef)
1e422769 237#define HeSVKEY_set(he,sv) ((HeKLEN(he) = HEf_SVKEY), (HeKEY_sv(he) = sv))
bbce6d69 238
ff68c719 239#define Nullhek Null(HEK*)
71be2cbc 240#define HEK_BASESIZE STRUCT_OFFSET(HEK, hek_key[0])
ff68c719 241#define HEK_HASH(hek) (hek)->hek_hash
242#define HEK_LEN(hek) (hek)->hek_len
243#define HEK_KEY(hek) (hek)->hek_key
19692e8d
NC
244#define HEK_FLAGS(hek) (*((unsigned char *)(HEK_KEY(hek))+HEK_LEN(hek)+1))
245
246#define HVhek_UTF8 0x01 /* Key is utf8 encoded. */
247#define HVhek_WASUTF8 0x02 /* Key is bytes here, but was supplied as utf8. */
248#define HVhek_FREEKEY 0x100 /* Internal flag to say key is malloc()ed. */
e16e2ff8
NC
249#define HVhek_PLACEHOLD 0x200 /* Internal flag to create placeholder.
250 * (may change, but Storable is a core module) */
19692e8d
NC
251#define HVhek_MASK 0xFF
252
253#define HEK_UTF8(hek) (HEK_FLAGS(hek) & HVhek_UTF8)
254#define HEK_UTF8_on(hek) (HEK_FLAGS(hek) |= HVhek_UTF8)
255#define HEK_UTF8_off(hek) (HEK_FLAGS(hek) &= ~HVhek_UTF8)
256#define HEK_WASUTF8(hek) (HEK_FLAGS(hek) & HVhek_WASUTF8)
257#define HEK_WASUTF8_on(hek) (HEK_FLAGS(hek) |= HVhek_WASUTF8)
258#define HEK_WASUTF8_off(hek) (HEK_FLAGS(hek) &= ~HVhek_WASUTF8)
d18c6117 259
5cbe4eec 260/* calculate HV array allocation */
d18c6117
GS
261#if defined(STRANGE_MALLOC) || defined(MYMALLOC)
262# define PERL_HV_ARRAY_ALLOC_BYTES(size) ((size) * sizeof(HE*))
263#else
264# define MALLOC_OVERHEAD 16
265# define PERL_HV_ARRAY_ALLOC_BYTES(size) \
266 (((size) < 64) \
267 ? (size) * sizeof(HE*) \
268 : (size) * sizeof(HE*) * 2 - MALLOC_OVERHEAD)
269#endif
37d85e3a 270
e16e2ff8
NC
271/* Flags for hv_iternext_flags. */
272#define HV_ITERNEXT_WANTPLACEHOLDERS 0x01 /* Don't skip placeholders. */
273
37d85e3a
JH
274/* available as a function in hv.c */
275#define Perl_sharepvn(sv, len, hash) HEK_KEY(share_hek(sv, len, hash))
276#define sharepvn(sv, len, hash) Perl_sharepvn(sv, len, hash)