3 * Copyright (c) 1991-2001, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
10 /* typedefs to eliminate some typing */
12 typedef struct hek HEK;
14 /* entry in hash value chain */
16 HE *hent_next; /* next entry in chain */
17 HEK *hent_hek; /* hash key */
18 SV *hent_val; /* scalar value that was hashed */
21 /* hash key -- defined separately for use as shared pointer */
23 U32 hek_hash; /* hash of key */
24 I32 hek_len; /* length of hash key */
25 char hek_key[1]; /* variable-length hash key */
29 /* This structure must match the beginning of struct xpvmg in sv.h. */
31 char * xhv_array; /* pointer to malloced string */
32 STRLEN xhv_fill; /* how full xhv_array currently is */
33 STRLEN xhv_max; /* subscript of last element of xhv_array */
34 IV xhv_keys; /* how many elements in the array */
35 NV xnv_nv; /* numeric value, if any */
36 #define xhv_placeholders xnv_nv
37 MAGIC* xmg_magic; /* magic for scalar array */
38 HV* xmg_stash; /* class package */
40 I32 xhv_riter; /* current root of iterator */
41 HE *xhv_eiter; /* current entry of iterator */
42 PMOP *xhv_pmroot; /* list of pm's for this package */
43 char *xhv_name; /* name, if a symbol table */
47 /* FYI: This is the "One-at-a-Time" algorithm by Bob Jenkins */
48 /* from requirements by Colin Plumb. */
49 /* (http://burtleburtle.net/bob/hash/doobs.html) */
50 #define PERL_HASH(hash,str,len) \
52 register const char *s_PeRlHaSh = str; \
53 register I32 i_PeRlHaSh = len; \
54 register U32 hash_PeRlHaSh = 0; \
55 while (i_PeRlHaSh--) { \
56 hash_PeRlHaSh += *s_PeRlHaSh++; \
57 hash_PeRlHaSh += (hash_PeRlHaSh << 10); \
58 hash_PeRlHaSh ^= (hash_PeRlHaSh >> 6); \
60 hash_PeRlHaSh += (hash_PeRlHaSh << 3); \
61 hash_PeRlHaSh ^= (hash_PeRlHaSh >> 11); \
62 (hash) = (hash_PeRlHaSh + (hash_PeRlHaSh << 15)); \
66 =for apidoc AmU||HEf_SVKEY
67 This flag, used in the length slot of hash entries and magic structures,
68 specifies the structure contains an C<SV*> pointer where a C<char*> pointer
69 is to be expected. (For information only--not to be used).
71 =for apidoc AmU||Nullhv
74 =for apidoc Am|char*|HvNAME|HV* stash
75 Returns the package name of a stash. See C<SvSTASH>, C<CvSTASH>.
77 =for apidoc Am|void*|HeKEY|HE* he
78 Returns the actual pointer stored in the key slot of the hash entry. The
79 pointer may be either C<char*> or C<SV*>, depending on the value of
80 C<HeKLEN()>. Can be assigned to. The C<HePV()> or C<HeSVKEY()> macros are
81 usually preferable for finding the value of a key.
83 =for apidoc Am|STRLEN|HeKLEN|HE* he
84 If this is negative, and amounts to C<HEf_SVKEY>, it indicates the entry
85 holds an C<SV*> key. Otherwise, holds the actual length of the key. Can
86 be assigned to. The C<HePV()> macro is usually preferable for finding key
89 =for apidoc Am|SV*|HeVAL|HE* he
90 Returns the value slot (type C<SV*>) stored in the hash entry.
92 =for apidoc Am|U32|HeHASH|HE* he
93 Returns the computed hash stored in the hash entry.
95 =for apidoc Am|char*|HePV|HE* he|STRLEN len
96 Returns the key slot of the hash entry as a C<char*> value, doing any
97 necessary dereferencing of possibly C<SV*> keys. The length of the string
98 is placed in C<len> (this is a macro, so do I<not> use C<&len>). If you do
99 not care about what the length of the key is, you may use the global
100 variable C<PL_na>, though this is rather less efficient than using a local
101 variable. Remember though, that hash keys in perl are free to contain
102 embedded nulls, so using C<strlen()> or similar is not a good way to find
103 the length of hash keys. This is very similar to the C<SvPV()> macro
104 described elsewhere in this document.
106 =for apidoc Am|SV*|HeSVKEY|HE* he
107 Returns the key as an C<SV*>, or C<Nullsv> if the hash entry does not
108 contain an C<SV*> key.
110 =for apidoc Am|SV*|HeSVKEY_force|HE* he
111 Returns the key as an C<SV*>. Will create and return a temporary mortal
112 C<SV*> if the hash entry contains only a C<char*> key.
114 =for apidoc Am|SV*|HeSVKEY_set|HE* he|SV* sv
115 Sets the key to a given C<SV*>, taking care to set the appropriate flags to
116 indicate the presence of an C<SV*> key, and returns the same
122 /* these hash entry flags ride on hent_klen (for use only in magic/tied HVs) */
123 #define HEf_SVKEY -2 /* hent_key is an SV* */
126 #define Nullhv Null(HV*)
127 #define HvARRAY(hv) (*(HE***)&((XPVHV*) SvANY(hv))->xhv_array)
128 #define HvFILL(hv) ((XPVHV*) SvANY(hv))->xhv_fill
129 #define HvMAX(hv) ((XPVHV*) SvANY(hv))->xhv_max
130 #define HvRITER(hv) ((XPVHV*) SvANY(hv))->xhv_riter
131 #define HvEITER(hv) ((XPVHV*) SvANY(hv))->xhv_eiter
132 #define HvPMROOT(hv) ((XPVHV*) SvANY(hv))->xhv_pmroot
133 #define HvNAME(hv) ((XPVHV*) SvANY(hv))->xhv_name
135 /* the number of keys (including any placeholers) */
136 #define XHvTOTALKEYS(xhv) ((xhv)->xhv_keys)
138 /* The number of placeholders in the enumerated-keys hash */
139 #define XHvPLACEHOLDERS(xhv) ((IV)((xhv)->xhv_placeholders))
141 /* the number of keys that exist() (i.e. excluding placeholers) */
142 #define XHvUSEDKEYS(xhv) (XHvTOTALKEYS(xhv) - XHvPLACEHOLDERS(xhv))
145 * HvKEYS gets the number of keys that actually exist(), and is provided
146 * for backwards compatibility with old XS code. The core uses HvUSEDKEYS
147 * (keys, excluding placeholdes) and HvTOTALKEYS (including placeholders)
149 #define HvKEYS(hv) XHvUSEDKEYS((XPVHV*) SvANY(hv))
150 #define HvUSEDKEYS(hv) XHvUSEDKEYS((XPVHV*) SvANY(hv))
151 #define HvTOTALKEYS(hv) XHvTOTALKEYS((XPVHV*) SvANY(hv))
152 #define HvPLACEHOLDERS(hv) XHvPLACEHOLDERS((XPVHV*) SvANY(hv))
155 #define HvSHAREKEYS(hv) (SvFLAGS(hv) & SVphv_SHAREKEYS)
156 #define HvSHAREKEYS_on(hv) (SvFLAGS(hv) |= SVphv_SHAREKEYS)
157 #define HvSHAREKEYS_off(hv) (SvFLAGS(hv) &= ~SVphv_SHAREKEYS)
159 #define HvLAZYDEL(hv) (SvFLAGS(hv) & SVphv_LAZYDEL)
160 #define HvLAZYDEL_on(hv) (SvFLAGS(hv) |= SVphv_LAZYDEL)
161 #define HvLAZYDEL_off(hv) (SvFLAGS(hv) &= ~SVphv_LAZYDEL)
163 /* Maybe amagical: */
164 /* #define HV_AMAGICmb(hv) (SvFLAGS(hv) & (SVpgv_badAM | SVpgv_AM)) */
166 #define HV_AMAGIC(hv) (SvFLAGS(hv) & SVpgv_AM)
167 #define HV_AMAGIC_on(hv) (SvFLAGS(hv) |= SVpgv_AM)
168 #define HV_AMAGIC_off(hv) (SvFLAGS(hv) &= ~SVpgv_AM)
171 #define HV_AMAGICbad(hv) (SvFLAGS(hv) & SVpgv_badAM)
172 #define HV_badAMAGIC_on(hv) (SvFLAGS(hv) |= SVpgv_badAM)
173 #define HV_badAMAGIC_off(hv) (SvFLAGS(hv) &= ~SVpgv_badAM)
176 #define Nullhe Null(HE*)
177 #define HeNEXT(he) (he)->hent_next
178 #define HeKEY_hek(he) (he)->hent_hek
179 #define HeKEY(he) HEK_KEY(HeKEY_hek(he))
180 #define HeKEY_sv(he) (*(SV**)HeKEY(he))
181 #define HeKLEN(he) HEK_LEN(HeKEY_hek(he))
182 #define HeKUTF8(he) HEK_UTF8(HeKEY_hek(he))
183 #define HeKLEN_UTF8(he) (HeKUTF8(he) ? -HeKLEN(he) : HeKLEN(he))
184 #define HeVAL(he) (he)->hent_val
185 #define HeHASH(he) HEK_HASH(HeKEY_hek(he))
186 #define HePV(he,lp) ((HeKLEN(he) == HEf_SVKEY) ? \
187 SvPV(HeKEY_sv(he),lp) : \
188 (((lp = HeKLEN(he)) >= 0) ? \
191 #define HeSVKEY(he) ((HeKEY(he) && \
192 HeKLEN(he) == HEf_SVKEY) ? \
193 HeKEY_sv(he) : Nullsv)
195 #define HeSVKEY_force(he) (HeKEY(he) ? \
196 ((HeKLEN(he) == HEf_SVKEY) ? \
198 sv_2mortal(newSVpvn(HeKEY(he), \
201 #define HeSVKEY_set(he,sv) ((HeKLEN(he) = HEf_SVKEY), (HeKEY_sv(he) = sv))
203 #define Nullhek Null(HEK*)
204 #define HEK_BASESIZE STRUCT_OFFSET(HEK, hek_key[0])
205 #define HEK_HASH(hek) (hek)->hek_hash
206 #define HEK_LEN(hek) (hek)->hek_len
207 #define HEK_KEY(hek) (hek)->hek_key
208 #define HEK_UTF8(hek) (*(HEK_KEY(hek)+HEK_LEN(hek)))
210 /* calculate HV array allocation */
211 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
212 # define PERL_HV_ARRAY_ALLOC_BYTES(size) ((size) * sizeof(HE*))
214 # define MALLOC_OVERHEAD 16
215 # define PERL_HV_ARRAY_ALLOC_BYTES(size) \
217 ? (size) * sizeof(HE*) \
218 : (size) * sizeof(HE*) * 2 - MALLOC_OVERHEAD)
221 /* available as a function in hv.c */
222 #define Perl_sharepvn(sv, len, hash) HEK_KEY(share_hek(sv, len, hash))
223 #define sharepvn(sv, len, hash) Perl_sharepvn(sv, len, hash)