source: trunk/essentials/dev-lang/perl/hv.h@ 3187

Last change on this file since 3187 was 3181, checked in by bird, 19 years ago

perl 5.8.8

File size: 13.3 KB
Line 
1/* hv.h
2 *
3 * Copyright (C) 1991, 1992, 1993, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2005, by Larry Wall and others
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 *
9 */
10
11/* typedefs to eliminate some typing */
12typedef struct he HE;
13typedef struct hek HEK;
14
15/* entry in hash value chain */
16struct he {
17 HE *hent_next; /* next entry in chain */
18 HEK *hent_hek; /* hash key */
19 SV *hent_val; /* scalar value that was hashed */
20};
21
22/* hash key -- defined separately for use as shared pointer */
23struct hek {
24 U32 hek_hash; /* hash of key */
25 I32 hek_len; /* length of hash key */
26 char hek_key[1]; /* variable-length hash key */
27 /* the hash-key is \0-terminated */
28 /* after the \0 there is a byte for flags, such as whether the key
29 is UTF-8 */
30};
31
32/* hash structure: */
33/* This structure must match the beginning of struct xpvmg in sv.h. */
34struct xpvhv {
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 */
38 IV xhv_keys; /* how many elements in the array */
39 NV xnv_nv; /* numeric value, if any */
40#define xhv_placeholders xnv_nv
41 MAGIC* xmg_magic; /* magic for scalar array */
42 HV* xmg_stash; /* class package */
43
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 */
48};
49
50/* hash a key */
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 unsigned, depending on the compiler)
58 * (b) catering for old code that uses a "char"
59 *
60 * The "hash seed" feature was added in Perl 5.8.1 to perturb the results
61 * to avoid "algorithmic complexity attacks".
62 *
63 * If USE_HASH_SEED is defined, hash randomisation is done by default
64 * If USE_HASH_SEED_EXPLICIT is defined, hash randomisation is done
65 * only if the environment variable PERL_HASH_SEED is set.
66 * For maximal control, one can define PERL_HASH_SEED.
67 * (see also perl.c:perl_parse()).
68 */
69#ifndef PERL_HASH_SEED
70# if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT)
71# define PERL_HASH_SEED PL_hash_seed
72# else
73# define PERL_HASH_SEED 0
74# endif
75#endif
76#define PERL_HASH(hash,str,len) \
77 STMT_START { \
78 register const char *s_PeRlHaSh_tmp = str; \
79 register const unsigned char *s_PeRlHaSh = (const unsigned char *)s_PeRlHaSh_tmp; \
80 register I32 i_PeRlHaSh = len; \
81 register U32 hash_PeRlHaSh = PERL_HASH_SEED; \
82 while (i_PeRlHaSh--) { \
83 hash_PeRlHaSh += *s_PeRlHaSh++; \
84 hash_PeRlHaSh += (hash_PeRlHaSh << 10); \
85 hash_PeRlHaSh ^= (hash_PeRlHaSh >> 6); \
86 } \
87 hash_PeRlHaSh += (hash_PeRlHaSh << 3); \
88 hash_PeRlHaSh ^= (hash_PeRlHaSh >> 11); \
89 (hash) = (hash_PeRlHaSh + (hash_PeRlHaSh << 15)); \
90 } STMT_END
91
92/* Only hv.c and mod_perl should be doing this. */
93#ifdef PERL_HASH_INTERNAL_ACCESS
94#define PERL_HASH_INTERNAL(hash,str,len) \
95 STMT_START { \
96 register const char *s_PeRlHaSh_tmp = str; \
97 register const unsigned char *s_PeRlHaSh = (const unsigned char *)s_PeRlHaSh_tmp; \
98 register I32 i_PeRlHaSh = len; \
99 register U32 hash_PeRlHaSh = PL_rehash_seed; \
100 while (i_PeRlHaSh--) { \
101 hash_PeRlHaSh += *s_PeRlHaSh++; \
102 hash_PeRlHaSh += (hash_PeRlHaSh << 10); \
103 hash_PeRlHaSh ^= (hash_PeRlHaSh >> 6); \
104 } \
105 hash_PeRlHaSh += (hash_PeRlHaSh << 3); \
106 hash_PeRlHaSh ^= (hash_PeRlHaSh >> 11); \
107 (hash) = (hash_PeRlHaSh + (hash_PeRlHaSh << 15)); \
108 } STMT_END
109#endif
110
111/*
112=head1 Hash Manipulation Functions
113
114=for apidoc AmU||HEf_SVKEY
115This flag, used in the length slot of hash entries and magic structures,
116specifies the structure contains an C<SV*> pointer where a C<char*> pointer
117is to be expected. (For information only--not to be used).
118
119=head1 Handy Values
120
121=for apidoc AmU||Nullhv
122Null HV pointer.
123
124=head1 Hash Manipulation Functions
125
126=for apidoc Am|char*|HvNAME|HV* stash
127Returns the package name of a stash, or NULL if C<stash> isn't a stash.
128See C<SvSTASH>, C<CvSTASH>.
129
130=for apidoc Am|void*|HeKEY|HE* he
131Returns the actual pointer stored in the key slot of the hash entry. The
132pointer may be either C<char*> or C<SV*>, depending on the value of
133C<HeKLEN()>. Can be assigned to. The C<HePV()> or C<HeSVKEY()> macros are
134usually preferable for finding the value of a key.
135
136=for apidoc Am|STRLEN|HeKLEN|HE* he
137If this is negative, and amounts to C<HEf_SVKEY>, it indicates the entry
138holds an C<SV*> key. Otherwise, holds the actual length of the key. Can
139be assigned to. The C<HePV()> macro is usually preferable for finding key
140lengths.
141
142=for apidoc Am|SV*|HeVAL|HE* he
143Returns the value slot (type C<SV*>) stored in the hash entry.
144
145=for apidoc Am|U32|HeHASH|HE* he
146Returns the computed hash stored in the hash entry.
147
148=for apidoc Am|char*|HePV|HE* he|STRLEN len
149Returns the key slot of the hash entry as a C<char*> value, doing any
150necessary dereferencing of possibly C<SV*> keys. The length of the string
151is placed in C<len> (this is a macro, so do I<not> use C<&len>). If you do
152not care about what the length of the key is, you may use the global
153variable C<PL_na>, though this is rather less efficient than using a local
154variable. Remember though, that hash keys in perl are free to contain
155embedded nulls, so using C<strlen()> or similar is not a good way to find
156the length of hash keys. This is very similar to the C<SvPV()> macro
157described elsewhere in this document.
158
159=for apidoc Am|SV*|HeSVKEY|HE* he
160Returns the key as an C<SV*>, or C<Nullsv> if the hash entry does not
161contain an C<SV*> key.
162
163=for apidoc Am|SV*|HeSVKEY_force|HE* he
164Returns the key as an C<SV*>. Will create and return a temporary mortal
165C<SV*> if the hash entry contains only a C<char*> key.
166
167=for apidoc Am|SV*|HeSVKEY_set|HE* he|SV* sv
168Sets the key to a given C<SV*>, taking care to set the appropriate flags to
169indicate the presence of an C<SV*> key, and returns the same
170C<SV*>.
171
172=cut
173*/
174
175/* these hash entry flags ride on hent_klen (for use only in magic/tied HVs) */
176#define HEf_SVKEY -2 /* hent_key is an SV* */
177
178
179#define Nullhv Null(HV*)
180#define HvARRAY(hv) (*(HE***)&((XPVHV*) SvANY(hv))->xhv_array)
181#define HvFILL(hv) ((XPVHV*) SvANY(hv))->xhv_fill
182#define HvMAX(hv) ((XPVHV*) SvANY(hv))->xhv_max
183#define HvRITER(hv) ((XPVHV*) SvANY(hv))->xhv_riter
184#define HvRITER_get(hv) (0 + ((XPVHV*) SvANY(hv))->xhv_riter)
185#define HvRITER_set(hv,r) (HvRITER(hv) = (r))
186#define HvEITER(hv) ((XPVHV*) SvANY(hv))->xhv_eiter
187#define HvEITER_get(hv) (0 + ((XPVHV*) SvANY(hv))->xhv_eiter)
188#define HvEITER_set(hv,e) (HvEITER(hv) = (e))
189#define HvPMROOT(hv) ((XPVHV*) SvANY(hv))->xhv_pmroot
190#define HvNAME(hv) ((XPVHV*) SvANY(hv))->xhv_name
191/* FIXME - all of these should use a UTF8 aware API, which should also involve
192 getting the length. */
193#define HvNAME_get(hv) (0 + ((XPVHV*) SvANY(hv))->xhv_name)
194#define hv_name_set(hv,name,length,flags) \
195 (HvNAME((hv)) = (name) ? savepvn(name, length) : 0)
196
197/* the number of keys (including any placeholers) */
198#define XHvTOTALKEYS(xhv) ((xhv)->xhv_keys)
199
200/* The number of placeholders in the enumerated-keys hash */
201#define XHvPLACEHOLDERS(xhv) ((xhv)->xhv_placeholders)
202
203/* the number of keys that exist() (i.e. excluding placeholders) */
204#define XHvUSEDKEYS(xhv) (XHvTOTALKEYS(xhv) - (IV)XHvPLACEHOLDERS(xhv))
205
206/*
207 * HvKEYS gets the number of keys that actually exist(), and is provided
208 * for backwards compatibility with old XS code. The core uses HvUSEDKEYS
209 * (keys, excluding placeholdes) and HvTOTALKEYS (including placeholders)
210 */
211#define HvKEYS(hv) XHvUSEDKEYS((XPVHV*) SvANY(hv))
212#define HvUSEDKEYS(hv) XHvUSEDKEYS((XPVHV*) SvANY(hv))
213#define HvTOTALKEYS(hv) XHvTOTALKEYS((XPVHV*) SvANY(hv))
214#define HvPLACEHOLDERS(hv) (XHvPLACEHOLDERS((XPVHV*) SvANY(hv)))
215#define HvPLACEHOLDERS_get(hv) (0 + XHvPLACEHOLDERS((XPVHV*) SvANY(hv)))
216#define HvPLACEHOLDERS_set(hv, p) \
217 (XHvPLACEHOLDERS((XPVHV*) SvANY(hv)) = (p))
218
219#define HvSHAREKEYS(hv) (SvFLAGS(hv) & SVphv_SHAREKEYS)