3 * Copyright (C) 1991, 1992, 1993, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, by Larry Wall and others
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.
11 /* typedefs to eliminate some typing */
13 typedef struct hek HEK;
15 /* entry in hash value chain */
17 HE *hent_next; /* next entry in chain */
18 HEK *hent_hek; /* hash key */
19 SV *hent_val; /* scalar value that was hashed */
22 /* hash key -- defined separately for use as shared pointer */
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 is
33 /* This structure must match the beginning of struct xpvmg in sv.h. */
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 */
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 */
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"
60 #define PERL_HASH(hash,str,len) \
62 register const char *s_PeRlHaSh_tmp = str; \
63 register const unsigned char *s_PeRlHaSh = (const unsigned char *)s_PeRlHaSh_tmp; \
64 register I32 i_PeRlHaSh = len; \
65 register U32 hash_PeRlHaSh = 0; \
66 while (i_PeRlHaSh--) { \
67 hash_PeRlHaSh += *s_PeRlHaSh++; \
68 hash_PeRlHaSh += (hash_PeRlHaSh << 10); \
69 hash_PeRlHaSh ^= (hash_PeRlHaSh >> 6); \
71 hash_PeRlHaSh += (hash_PeRlHaSh << 3); \
72 hash_PeRlHaSh ^= (hash_PeRlHaSh >> 11); \
73 (hash) = (hash_PeRlHaSh + (hash_PeRlHaSh << 15)); \
77 =head1 Hash Manipulation Functions
79 =for apidoc AmU||HEf_SVKEY
80 This flag, used in the length slot of hash entries and magic structures,
81 specifies the structure contains an C<SV*> pointer where a C<char*> pointer
82 is to be expected. (For information only--not to be used).
86 =for apidoc AmU||Nullhv
89 =head1 Hash Manipulation Functions
91 =for apidoc Am|char*|HvNAME|HV* stash
92 Returns the package name of a stash. See C<SvSTASH>, C<CvSTASH>.
94 =for apidoc Am|void*|HeKEY|HE* he
95 Returns the actual pointer stored in the key slot of the hash entry. The
96 pointer may be either C<char*> or C<SV*>, depending on the value of
97 C<HeKLEN()>. Can be assigned to. The C<HePV()> or C<HeSVKEY()> macros are
98 usually preferable for finding the value of a key.
100 =for apidoc Am|STRLEN|HeKLEN|HE* he
101 If this is negative, and amounts to C<HEf_SVKEY>, it indicates the entry
102 holds an C<SV*> key. Otherwise, holds the actual length of the key. Can
103 be assigned to. The C<HePV()> macro is usually preferable for finding key
106 =for apidoc Am|SV*|HeVAL|HE* he
107 Returns the value slot (type C<SV*>) stored in the hash entry.
109 =for apidoc Am|U32|HeHASH|HE* he
110 Returns the computed hash stored in the hash entry.
112 =for apidoc Am|char*|HePV|HE* he|STRLEN len
113 Returns the key slot of the hash entry as a C<char*> value, doing any
114 necessary dereferencing of possibly C<SV*> keys. The length of the string
115 is placed in C<len> (this is a macro, so do I<not> use C<&len>). If you do
116 not care about what the length of the key is, you may use the global
117 variable C<PL_na>, though this is rather less efficient than using a local
118 variable. Remember though, that hash keys in perl are free to contain
119 embedded nulls, so using C<strlen()> or similar is not a good way to find
120 the length of hash keys. This is very similar to the C<SvPV()> macro
121 described elsewhere in this document.
123 =for apidoc Am|SV*|HeSVKEY|HE* he
124 Returns the key as an C<SV*>, or C<Nullsv> if the hash entry does not
125 contain an C<SV*> key.
127 =for apidoc Am|SV*|HeSVKEY_force|HE* he
128 Returns the key as an C<SV*>. Will create and return a temporary mortal
129 C<SV*> if the hash entry contains only a C<char*> key.
131 =for apidoc Am|SV*|HeSVKEY_set|HE* he|SV* sv
132 Sets the key to a given C<SV*>, taking care to set the appropriate flags to
133 indicate the presence of an C<SV*> key, and returns the same
139 /* these hash entry flags ride on hent_klen (for use only in magic/tied HVs) */
140 #define HEf_SVKEY -2 /* hent_key is an SV* */
143 #define Nullhv Null(HV*)
144 #define HvARRAY(hv) (*(HE***)&((XPVHV*) SvANY(hv))->xhv_array)
145 #define HvFILL(hv) ((XPVHV*) SvANY(hv))->xhv_fill
146 #define HvMAX(hv) ((XPVHV*) SvANY(hv))->xhv_max
147 #define HvRITER(hv) ((XPVHV*) SvANY(hv))->xhv_riter
148 #define HvEITER(hv) ((XPVHV*) SvANY(hv))->xhv_eiter
149 #define HvPMROOT(hv) ((XPVHV*) SvANY(hv))->xhv_pmroot
150 #define HvNAME(hv) ((XPVHV*) SvANY(hv))->xhv_name
152 /* the number of keys (including any placeholers) */
153 #define XHvTOTALKEYS(xhv) ((xhv)->xhv_keys)
155 /* The number of placeholders in the enumerated-keys hash */
156 #define XHvPLACEHOLDERS(xhv) ((xhv)->xhv_placeholders)
158 /* the number of keys that exist() (i.e. excluding placeholders) */
159 #define XHvUSEDKEYS(xhv) (XHvTOTALKEYS(xhv) - (IV)XHvPLACEHOLDERS(xhv))
162 * HvKEYS gets the number of keys that actually exist(), and is provided
163 * for backwards compatibility with old XS code. The core uses HvUSEDKEYS
164 * (keys, excluding placeholdes) and HvTOTALKEYS (including placeholders)
166 #define HvKEYS(hv) XHvUSEDKEYS((XPVHV*) SvANY(hv))
167 #define HvUSEDKEYS(hv) XHvUSEDKEYS((XPVHV*) SvANY(hv))
168 #define HvTOTALKEYS(hv) XHvTOTALKEYS((XPVHV*) SvANY(hv))
169 #define HvPLACEHOLDERS(hv) XHvPLACEHOLDERS((XPVHV*) SvANY(hv))
171 #define HvSHAREKEYS(hv) (SvFLAGS(hv) & SVphv_SHAREKEYS)
172 #define HvSHAREKEYS_on(hv) (SvFLAGS(hv) |= SVphv_SHAREKEYS)
173 #define HvSHAREKEYS_off(hv) (SvFLAGS(hv) &= ~SVphv_SHAREKEYS)
175 /* This is an optimisation flag. It won't be set if all hash keys have a 0
176 * flag. Currently the only flags relate to utf8.
177 * Hence it won't be set if all keys are 8 bit only. It will be set if any key
178 * is utf8 (including 8 bit keys that were entered as utf8, and need upgrading
179 * when retrieved during iteration. It may still be set when there are no longer
182 #define HvHASKFLAGS(hv) (SvFLAGS(hv) & SVphv_HASKFLAGS)
183 #define HvHASKFLAGS_on(hv) (SvFLAGS(hv) |= SVphv_HASKFLAGS)
184 #define HvHASKFLAGS_off(hv) (SvFLAGS(hv) &= ~SVphv_HASKFLAGS)
186 #define HvLAZYDEL(hv) (SvFLAGS(hv) & SVphv_LAZYDEL)
187 #define HvLAZYDEL_on(hv) (SvFLAGS(hv) |= SVphv_LAZYDEL)
188 #define HvLAZYDEL_off(hv) (SvFLAGS(hv) &= ~SVphv_LAZYDEL)
190 /* Maybe amagical: */
191 /* #define HV_AMAGICmb(hv) (SvFLAGS(hv) & (SVpgv_badAM | SVpgv_AM)) */
193 #define HV_AMAGIC(hv) (SvFLAGS(hv) & SVpgv_AM)
194 #define HV_AMAGIC_on(hv) (SvFLAGS(hv) |= SVpgv_AM)
195 #define HV_AMAGIC_off(hv) (SvFLAGS(hv) &= ~SVpgv_AM)
198 #define HV_AMAGICbad(hv) (SvFLAGS(hv) & SVpgv_badAM)
199 #define HV_badAMAGIC_on(hv) (SvFLAGS(hv) |= SVpgv_badAM)
200 #define HV_badAMAGIC_off(hv) (SvFLAGS(hv) &= ~SVpgv_badAM)
203 #define Nullhe Null(HE*)
204 #define HeNEXT(he) (he)->hent_next
205 #define HeKEY_hek(he) (he)->hent_hek
206 #define HeKEY(he) HEK_KEY(HeKEY_hek(he))
207 #define HeKEY_sv(he) (*(SV**)HeKEY(he))
208 #define HeKLEN(he) HEK_LEN(HeKEY_hek(he))
209 #define HeKUTF8(he) HEK_UTF8(HeKEY_hek(he))
210 #define HeKWASUTF8(he) HEK_WASUTF8(HeKEY_hek(he))
211 #define HeKLEN_UTF8(he) (HeKUTF8(he) ? -HeKLEN(he) : HeKLEN(he))
212 #define HeKFLAGS(he) HEK_FLAGS(HeKEY_hek(he))
213 #define HeVAL(he) (he)->hent_val
214 #define HeHASH(he) HEK_HASH(HeKEY_hek(he))
215 #define HePV(he,lp) ((HeKLEN(he) == HEf_SVKEY) ? \
216 SvPV(HeKEY_sv(he),lp) : \
217 (((lp = HeKLEN(he)) >= 0) ? \
220 #define HeSVKEY(he) ((HeKEY(he) && \
221 HeKLEN(he) == HEf_SVKEY) ? \
222 HeKEY_sv(he) : Nullsv)
224 #define HeSVKEY_force(he) (HeKEY(he) ? \
225 ((HeKLEN(he) == HEf_SVKEY) ? \
227 sv_2mortal(newSVpvn(HeKEY(he), \
230 #define HeSVKEY_set(he,sv) ((HeKLEN(he) = HEf_SVKEY), (HeKEY_sv(he) = sv))
232 #define Nullhek Null(HEK*)
233 #define HEK_BASESIZE STRUCT_OFFSET(HEK, hek_key[0])
234 #define HEK_HASH(hek) (hek)->hek_hash
235 #define HEK_LEN(hek) (hek)->hek_len
236 #define HEK_KEY(hek) (hek)->hek_key
237 #define HEK_FLAGS(hek) (*((unsigned char *)(HEK_KEY(hek))+HEK_LEN(hek)+1))
239 #define HVhek_UTF8 0x01 /* Key is utf8 encoded. */
240 #define HVhek_WASUTF8 0x02 /* Key is bytes here, but was supplied as utf8. */
241 #define HVhek_FREEKEY 0x100 /* Internal flag to say key is malloc()ed. */
242 #define HVhek_PLACEHOLD 0x200 /* Internal flag to create placeholder.
243 * (may change, but Storable is a core module) */
244 #define HVhek_MASK 0xFF
246 #define HEK_UTF8(hek) (HEK_FLAGS(hek) & HVhek_UTF8)
247 #define HEK_UTF8_on(hek) (HEK_FLAGS(hek) |= HVhek_UTF8)
248 #define HEK_UTF8_off(hek) (HEK_FLAGS(hek) &= ~HVhek_UTF8)
249 #define HEK_WASUTF8(hek) (HEK_FLAGS(hek) & HVhek_WASUTF8)
250 #define HEK_WASUTF8_on(hek) (HEK_FLAGS(hek) |= HVhek_WASUTF8)
251 #define HEK_WASUTF8_off(hek) (HEK_FLAGS(hek) &= ~HVhek_WASUTF8)
253 /* calculate HV array allocation */
254 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
255 # define PERL_HV_ARRAY_ALLOC_BYTES(size) ((size) * sizeof(HE*))
257 # define MALLOC_OVERHEAD 16
258 # define PERL_HV_ARRAY_ALLOC_BYTES(size) \
260 ? (size) * sizeof(HE*) \
261 : (size) * sizeof(HE*) * 2 - MALLOC_OVERHEAD)
264 /* Flags for hv_iternext_flags. */
265 #define HV_ITERNEXT_WANTPLACEHOLDERS 0x01 /* Don't skip placeholders. */
267 /* available as a function in hv.c */
268 #define Perl_sharepvn(sv, len, hash) HEK_KEY(share_hek(sv, len, hash))
269 #define sharepvn(sv, len, hash) Perl_sharepvn(sv, len, hash)