This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate:
[perl5.git] / hv.h
CommitLineData
a0d0e21e 1/* hv.h
79072805 2 *
e6906430 3 * Copyright (C) 1991, 1992, 1993, 1996, 1997, 1998, 1999,
31ab2e0d 4 * 2000, 2001, 2002, 2005, 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 {
b4b2dbdc
JC
17 /* Keep hent_next first in this structure, because sv_free_arenas take
18 advantage of this to share code between the he arenas and the SV
19 body arenas */
5cbe4eec
MLF
20 HE *hent_next; /* next entry in chain */
21 HEK *hent_hek; /* hash key */
22 SV *hent_val; /* scalar value that was hashed */
bbce6d69 23};
24
5cbe4eec 25/* hash key -- defined separately for use as shared pointer */
ff68c719 26struct hek {
5cbe4eec
MLF
27 U32 hek_hash; /* hash of key */
28 I32 hek_len; /* length of hash key */
29 char hek_key[1]; /* variable-length hash key */
e05949c7 30 /* the hash-key is \0-terminated */
cd458e05
JH
31 /* after the \0 there is a byte for flags, such as whether the key
32 is UTF-8 */
79072805
LW
33};
34
5cbe4eec 35/* hash structure: */
6ee623d5 36/* This structure must match the beginning of struct xpvmg in sv.h. */
79072805 37struct xpvhv {
463ee0b2
LW
38 char * xhv_array; /* pointer to malloced string */
39 STRLEN xhv_fill; /* how full xhv_array currently is */
40 STRLEN xhv_max; /* subscript of last element of xhv_array */
6ee623d5 41 IV xhv_keys; /* how many elements in the array */
65202027 42 NV xnv_nv; /* numeric value, if any */
8aacddc1 43#define xhv_placeholders xnv_nv
79072805
LW
44 MAGIC* xmg_magic; /* magic for scalar array */
45 HV* xmg_stash; /* class package */
46
79072805
LW
47 I32 xhv_riter; /* current root of iterator */
48 HE *xhv_eiter; /* current entry of iterator */
49 PMOP *xhv_pmroot; /* list of pm's for this package */
50 char *xhv_name; /* name, if a symbol table */
79072805
LW
51};
52
6513d6ca
NC
53typedef struct xpvhv xpvhv_allocated;
54
5cbe4eec 55/* hash a key */
5afd6d42
JH
56/* FYI: This is the "One-at-a-Time" algorithm by Bob Jenkins
57 * from requirements by Colin Plumb.
58 * (http://burtleburtle.net/bob/hash/doobs.html) */
59/* The use of a temporary pointer and the casting games
60 * is needed to serve the dual purposes of
61 * (a) the hashed data being interpreted as "unsigned char" (new since 5.8,
a7f5a39f 62 * a "char" can be either signed or unsigned, depending on the compiler)
5afd6d42 63 * (b) catering for old code that uses a "char"
4f83e563 64 *
c71f9582
PN
65 * The "hash seed" feature was added in Perl 5.8.1 to perturb the results
66 * to avoid "algorithmic complexity attacks".
4f83e563
JH
67 *
68 * If USE_HASH_SEED is defined, hash randomisation is done by default
69 * If USE_HASH_SEED_EXPLICIT is defined, hash randomisation is done
70 * only if the environment variable PERL_HASH_SEED is set.
71 * For maximal control, one can define PERL_HASH_SEED.
8298e346 72 * (see also perl.c:perl_parse()).
5afd6d42 73 */
4f83e563
JH
74#ifndef PERL_HASH_SEED
75# if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT)
76# define PERL_HASH_SEED PL_hash_seed
77# else
78# define PERL_HASH_SEED 0
79# endif
c71f9582 80#endif
bf6bd887 81#define PERL_HASH(hash,str,len) \
82 STMT_START { \
8c89da26 83 register const char * const s_PeRlHaSh_tmp = str; \
5afd6d42 84 register const unsigned char *s_PeRlHaSh = (const unsigned char *)s_PeRlHaSh_tmp; \
bf6bd887 85 register I32 i_PeRlHaSh = len; \
c71f9582 86 register U32 hash_PeRlHaSh = PERL_HASH_SEED; \
a6fe520e
MLF
87 while (i_PeRlHaSh--) { \
88 hash_PeRlHaSh += *s_PeRlHaSh++; \
89 hash_PeRlHaSh += (hash_PeRlHaSh << 10); \
90 hash_PeRlHaSh ^= (hash_PeRlHaSh >> 6); \
91 } \
92 hash_PeRlHaSh += (hash_PeRlHaSh << 3); \
93 hash_PeRlHaSh ^= (hash_PeRlHaSh >> 11); \
4fee5c71 94 (hash) = (hash_PeRlHaSh + (hash_PeRlHaSh << 15)); \
bf6bd887 95 } STMT_END
96
7f047bfa
NC
97/* Only hv.c and mod_perl should be doing this. */
98#ifdef PERL_HASH_INTERNAL_ACCESS
ff38041c
NC
99#define PERL_HASH_INTERNAL(hash,str,len) \
100 STMT_START { \
8c89da26 101 register const char * const s_PeRlHaSh_tmp = str; \
ff38041c
NC
102 register const unsigned char *s_PeRlHaSh = (const unsigned char *)s_PeRlHaSh_tmp; \
103 register I32 i_PeRlHaSh = len; \
7f047bfa 104 register U32 hash_PeRlHaSh = PL_rehash_seed; \
ff38041c
NC
105 while (i_PeRlHaSh--) { \
106 hash_PeRlHaSh += *s_PeRlHaSh++; \
107 hash_PeRlHaSh += (hash_PeRlHaSh << 10); \
108 hash_PeRlHaSh ^= (hash_PeRlHaSh >> 6); \
109 } \
110 hash_PeRlHaSh += (hash_PeRlHaSh << 3); \
111 hash_PeRlHaSh ^= (hash_PeRlHaSh >> 11); \
112 (hash) = (hash_PeRlHaSh + (hash_PeRlHaSh << 15)); \
113 } STMT_END
114#endif
115
954c1994 116/*
ccfc67b7
JH
117=head1 Hash Manipulation Functions
118
954c1994
GS
119=for apidoc AmU||HEf_SVKEY
120This flag, used in the length slot of hash entries and magic structures,
d1be9408 121specifies the structure contains an C<SV*> pointer where a C<char*> pointer
954c1994
GS
122is to be expected. (For information only--not to be used).
123
ccfc67b7
JH
124=head1 Handy Values
125
954c1994
GS
126=for apidoc AmU||Nullhv
127Null HV pointer.
128
ccfc67b7
JH
129=head1 Hash Manipulation Functions
130
954c1994 131=for apidoc Am|char*|HvNAME|HV* stash
7dbaea58
NC
132Returns the package name of a stash, or NULL if C<stash> isn't a stash.
133See C<SvSTASH>, C<CvSTASH>.
954c1994
GS
134
135=for apidoc Am|void*|HeKEY|HE* he
136Returns the actual pointer stored in the key slot of the hash entry. The
137pointer may be either C<char*> or C<SV*>, depending on the value of
138C<HeKLEN()>. Can be assigned to. The C<HePV()> or C<HeSVKEY()> macros are
139usually preferable for finding the value of a key.
140
141=for apidoc Am|STRLEN|HeKLEN|HE* he
142If this is negative, and amounts to C<HEf_SVKEY>, it indicates the entry
143holds an C<SV*> key. Otherwise, holds the actual length of the key. Can
144be assigned to. The C<HePV()> macro is usually preferable for finding key
145lengths.
146
147=for apidoc Am|SV*|HeVAL|HE* he
148Returns the value slot (type C<SV*>) stored in the hash entry.
149
150=for apidoc Am|U32|HeHASH|HE* he
151Returns the computed hash stored in the hash entry.
152
153=for apidoc Am|char*|HePV|HE* he|STRLEN len
154Returns the key slot of the hash entry as a C<char*> value, doing any
155necessary dereferencing of possibly C<SV*> keys. The length of the string
156is placed in C<len> (this is a macro, so do I<not> use C<&len>). If you do
157not care about what the length of the key is, you may use the global
158variable C<PL_na>, though this is rather less efficient than using a local
159variable. Remember though, that hash keys in perl are free to contain
160embedded nulls, so using C<strlen()> or similar is not a good way to find
161the length of hash keys. This is very similar to the C<SvPV()> macro
162described elsewhere in this document.
163
164=for apidoc Am|SV*|HeSVKEY|HE* he
0e2d6244 165Returns the key as an C<SV*>, or C<NULL> if the hash entry does not
954c1994
GS
166contain an C<SV*> key.
167
168=for apidoc Am|SV*|HeSVKEY_force|HE* he
169Returns the key as an C<SV*>. Will create and return a temporary mortal
170C<SV*> if the hash entry contains only a C<char*> key.
171
172=for apidoc Am|SV*|HeSVKEY_set|HE* he|SV* sv
173Sets the key to a given C<SV*>, taking care to set the appropriate flags to
174indicate the presence of an C<SV*> key, and returns the same
175C<SV*>.
176
177=cut
178*/
bf6bd887 179
bf5b86ae 180/* these hash entry flags ride on hent_klen (for use only in magic/tied HVs) */
d1be9408 181#define HEf_SVKEY -2 /* hent_key is an SV* */
bf6bd887 182
183
79072805 184#define Nullhv Null(HV*)
4efcf9a2 185#define HvARRAY(hv) (*(HE***)&((XPVHV*) SvANY(hv))->xhv_array)
79072805 186#define HvFILL(hv) ((XPVHV*) SvANY(hv))->xhv_fill
463ee0b2 187#define HvMAX(hv) ((XPVHV*) SvANY(hv))->xhv_max
79072805 188#define HvRITER(hv) ((XPVHV*) SvANY(hv))->xhv_riter
c3f2d5da
NC
189#define HvRITER_get(hv) (0 + ((XPVHV*) SvANY(hv))->xhv_riter)
190#define HvRITER_set(hv,r) (HvRITER(hv) = (r))
79072805 191#define HvEITER(hv) ((XPVHV*) SvANY(hv))->xhv_eiter
c3f2d5da
NC
192#define HvEITER_get(hv) (0 + ((XPVHV*) SvANY(hv))->xhv_eiter)
193#define HvEITER_set(hv,e) (HvEITER(hv) = (e))
79072805
LW
194#define HvPMROOT(hv) ((XPVHV*) SvANY(hv))->xhv_pmroot
195#define HvNAME(hv) ((XPVHV*) SvANY(hv))->xhv_name
26ab6a78
NC
196/* FIXME - all of these should use a UTF8 aware API, which should also involve
197 getting the length. */
c3f2d5da 198#define HvNAME_get(hv) (0 + ((XPVHV*) SvANY(hv))->xhv_name)
26ab6a78
NC
199#define hv_name_set(hv,name,length,flags) \
200 (HvNAME((hv)) = (name) ? savepvn(name, length) : 0)
a0d0e21e 201
8aacddc1
NIS
202/* the number of keys (including any placeholers) */
203#define XHvTOTALKEYS(xhv) ((xhv)->xhv_keys)
204
205/* The number of placeholders in the enumerated-keys hash */
205c8ad3 206#define XHvPLACEHOLDERS(xhv) ((xhv)->xhv_placeholders)
8aacddc1 207
dd2710a1
VK
208/* the number of keys that exist() (i.e. excluding placeholders) */
209#define XHvUSEDKEYS(xhv) (XHvTOTALKEYS(xhv) - (IV)XHvPLACEHOLDERS(xhv))
8aacddc1
NIS
210
211/*
212 * HvKEYS gets the number of keys that actually exist(), and is provided
213 * for backwards compatibility with old XS code. The core uses HvUSEDKEYS
214 * (keys, excluding placeholdes) and HvTOTALKEYS (including placeholders)
215 */
216#define HvKEYS(hv) XHvUSEDKEYS((XPVHV*) SvANY(hv))
217#define HvUSEDKEYS(hv) XHvUSEDKEYS((XPVHV*) SvANY(hv))
218#define HvTOTALKEYS(hv) XHvTOTALKEYS((XPVHV*) SvANY(hv))
c3f2d5da
NC
219#define HvPLACEHOLDERS(hv) (XHvPLACEHOLDERS((XPVHV*) SvANY(hv)))
220#define HvPLACEHOLDERS_get(hv) (0 + XHvPLACEHOLDERS((XPVHV*) SvANY(hv)))
221#define HvPLACEHOLDERS_set(hv, p) \
222 (XHvPLACEHOLDERS((XPVHV*) SvANY(hv)) = (p))
8aacddc1 223
bf6bd887 224#define HvSHAREKEYS(hv) (SvFLAGS(hv) & SVphv_SHAREKEYS)
225#define HvSHAREKEYS_on(hv) (SvFLAGS(hv) |= SVphv_SHAREKEYS)
226#define HvSHAREKEYS_off(hv) (SvFLAGS(hv) &= ~SVphv_SHAREKEYS)
227
19692e8d
NC
228/* This is an optimisation flag. It won't be set if all hash keys have a 0
229 * flag. Currently the only flags relate to utf8.
230 * Hence it won't be set if all keys are 8 bit only. It will be set if any key
231 * is utf8 (including 8 bit keys that were entered as utf8, and need upgrading
232 * when retrieved during iteration. It may still be set when there are no longer
233 * any utf8 keys.
9c87fafe 234 * See HVhek_ENABLEHVKFLAGS for the trigger.
19692e8d
NC
235 */
236#define HvHASKFLAGS(hv) (SvFLAGS(hv) & SVphv_HASKFLAGS)
237#define HvHASKFLAGS_on(hv) (SvFLAGS(hv) |= SVphv_HASKFLAGS)
238#define HvHASKFLAGS_off(hv) (SvFLAGS(hv) &= ~SVphv_HASKFLAGS)
574c8022 239
bf5b86ae
GS
240#define HvLAZYDEL(hv) (SvFLAGS(hv) & SVphv_LAZYDEL)
241#define HvLAZYDEL_on(hv) (SvFLAGS(hv) |= SVphv_LAZYDEL)
242#define HvLAZYDEL_off(hv) (SvFLAGS(hv) &= ~SVphv_LAZYDEL)
243
ff38041c
NC
244#define HvREHASH(hv) (SvFLAGS(hv) & SVphv_REHASH)
245#define HvREHASH_on(hv) (SvFLAGS(hv) |= SVphv_REHASH)
246#define HvREHASH_off(hv) (SvFLAGS(hv) &= ~SVphv_REHASH)
247
bf6bd887 248#define Nullhe Null(HE*)
249#define HeNEXT(he) (he)->hent_next
ff68c719 250#define HeKEY_hek(he) (he)->hent_hek
251#define HeKEY(he) HEK_KEY(HeKEY_hek(he))
bbce6d69 252#define HeKEY_sv(he) (*(SV**)HeKEY(he))
ff68c719 253#define HeKLEN(he) HEK_LEN(HeKEY_hek(he))
da58a35d 254#define HeKUTF8(he) HEK_UTF8(HeKEY_hek(he))
19692e8d 255#define HeKWASUTF8(he) HEK_WASUTF8(HeKEY_hek(he))
ff38041c 256#define HeKREHASH(he) HEK_REHASH(HeKEY_hek(he))
da58a35d 257#define HeKLEN_UTF8(he) (HeKUTF8(he) ? -HeKLEN(he) : HeKLEN(he))
19692e8d 258#define HeKFLAGS(he) HEK_FLAGS(HeKEY_hek(he))
bf6bd887 259#define HeVAL(he) (he)->hent_val
ff68c719 260#define HeHASH(he) HEK_HASH(HeKEY_hek(he))
1e422769 261#define HePV(he,lp) ((HeKLEN(he) == HEf_SVKEY) ? \
262 SvPV(HeKEY_sv(he),lp) : \
263 (((lp = HeKLEN(he)) >= 0) ? \
0e2d6244 264 HeKEY(he) : NULL))
1e422769 265
bbce6d69 266#define HeSVKEY(he) ((HeKEY(he) && \
267 HeKLEN(he) == HEf_SVKEY) ? \
0e2d6244 268 HeKEY_sv(he) : NULL)
bbce6d69 269
270#define HeSVKEY_force(he) (HeKEY(he) ? \
271 ((HeKLEN(he) == HEf_SVKEY) ? \
272 HeKEY_sv(he) : \
79cb57f6 273 sv_2mortal(newSVpvn(HeKEY(he), \
bbce6d69 274 HeKLEN(he)))) : \
3280af22 275 &PL_sv_undef)
1e422769 276#define HeSVKEY_set(he,sv) ((HeKLEN(he) = HEf_SVKEY), (HeKEY_sv(he) = sv))
bbce6d69 277
ff68c719 278#define Nullhek Null(HEK*)
71be2cbc 279#define HEK_BASESIZE STRUCT_OFFSET(HEK, hek_key[0])
ff68c719 280#define HEK_HASH(hek) (hek)->hek_hash
281#define HEK_LEN(hek) (hek)->hek_len
282#define HEK_KEY(hek) (hek)->hek_key
19692e8d
NC
283#define HEK_FLAGS(hek) (*((unsigned char *)(HEK_KEY(hek))+HEK_LEN(hek)+1))
284
285#define HVhek_UTF8 0x01 /* Key is utf8 encoded. */
286#define HVhek_WASUTF8 0x02 /* Key is bytes here, but was supplied as utf8. */
ff38041c 287#define HVhek_REHASH 0x04 /* This key is in an hv using a custom HASH . */
2213ec13 288#define HVhek_UNSHARED 0x08 /* This key isn't a shared hash key. */
19692e8d 289#define HVhek_FREEKEY 0x100 /* Internal flag to say key is malloc()ed. */
e16e2ff8
NC
290#define HVhek_PLACEHOLD 0x200 /* Internal flag to create placeholder.
291 * (may change, but Storable is a core module) */
19692e8d
NC
292#define HVhek_MASK 0xFF
293
9c87fafe
NC
294/* Which flags enable HvHASKFLAGS? Somewhat a hack on a hack, as
295 HVhek_REHASH is only needed because the rehash flag has to be duplicated
296 into all keys as hv_iternext has no access to the hash flags. At this
297 point Storable's tests get upset, because sometimes hashes are "keyed"
298 and sometimes not, depending on the order of data insertion, and whether
2213ec13
NC
299 it triggered rehashing. So currently HVhek_REHASH is exempt.
300 Similarly UNSHARED
9c87fafe
NC
301*/
302
2213ec13 303#define HVhek_ENABLEHVKFLAGS (HVhek_MASK & ~(HVhek_REHASH|HVhek_UNSHARED))
9c87fafe 304
19692e8d
NC
305#define HEK_UTF8(hek) (HEK_FLAGS(hek) & HVhek_UTF8)
306#define HEK_UTF8_on(hek) (HEK_FLAGS(hek) |= HVhek_UTF8)
307#define HEK_UTF8_off(hek) (HEK_FLAGS(hek) &= ~HVhek_UTF8)
308#define HEK_WASUTF8(hek) (HEK_FLAGS(hek) & HVhek_WASUTF8)
309#define HEK_WASUTF8_on(hek) (HEK_FLAGS(hek) |= HVhek_WASUTF8)
310#define HEK_WASUTF8_off(hek) (HEK_FLAGS(hek) &= ~HVhek_WASUTF8)
ff38041c
NC
311#define HEK_REHASH(hek) (HEK_FLAGS(hek) & HVhek_REHASH)
312#define HEK_REHASH_on(hek) (HEK_FLAGS(hek) |= HVhek_REHASH)
d18c6117 313
5cbe4eec 314/* calculate HV array allocation */
69ddb3b9
NC
315#ifndef PERL_USE_LARGE_HV_ALLOC
316/* Default to allocating the correct size - default to assuming that malloc()
317 is not broken and is efficient at allocating blocks sized at powers-of-two.
318*/
d18c6117
GS
319# define PERL_HV_ARRAY_ALLOC_BYTES(size) ((size) * sizeof(HE*))
320#else
321# define MALLOC_OVERHEAD 16
322# define PERL_HV_ARRAY_ALLOC_BYTES(size) \
323 (((size) < 64) \
324 ? (size) * sizeof(HE*) \
325 : (size) * sizeof(HE*) * 2 - MALLOC_OVERHEAD)
326#endif
37d85e3a 327
e16e2ff8
NC
328/* Flags for hv_iternext_flags. */
329#define HV_ITERNEXT_WANTPLACEHOLDERS 0x01 /* Don't skip placeholders. */
330
5082993f 331#define hv_iternext(hv) hv_iternext_flags(hv, 0)
0e2d6244 332#define hv_magic(hv, gv, how) sv_magic((SV*)(hv), (SV*)(gv), how, NULL, 0)
5082993f 333
37d85e3a
JH
334/* available as a function in hv.c */
335#define Perl_sharepvn(sv, len, hash) HEK_KEY(share_hek(sv, len, hash))
336#define sharepvn(sv, len, hash) Perl_sharepvn(sv, len, hash)
26ab6a78
NC
337
338/*
339 * Local variables:
340 * c-indentation-style: bsd
341 * c-basic-offset: 4
342 * indent-tabs-mode: t
343 * End:
344 *
345 * ex: set ts=8 sts=4 sw=4 noet:
346 */