xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/hv.h (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*    hv.h
2*0Sstevel@tonic-gate  *
3*0Sstevel@tonic-gate  *    Copyright (C) 1991, 1992, 1993, 1996, 1997, 1998, 1999,
4*0Sstevel@tonic-gate  *    2000, 2001, 2002, by Larry Wall and others
5*0Sstevel@tonic-gate  *
6*0Sstevel@tonic-gate  *    You may distribute under the terms of either the GNU General Public
7*0Sstevel@tonic-gate  *    License or the Artistic License, as specified in the README file.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  */
10*0Sstevel@tonic-gate 
11*0Sstevel@tonic-gate /* typedefs to eliminate some typing */
12*0Sstevel@tonic-gate typedef struct he HE;
13*0Sstevel@tonic-gate typedef struct hek HEK;
14*0Sstevel@tonic-gate 
15*0Sstevel@tonic-gate /* entry in hash value chain */
16*0Sstevel@tonic-gate struct he {
17*0Sstevel@tonic-gate     HE		*hent_next;	/* next entry in chain */
18*0Sstevel@tonic-gate     HEK		*hent_hek;	/* hash key */
19*0Sstevel@tonic-gate     SV		*hent_val;	/* scalar value that was hashed */
20*0Sstevel@tonic-gate };
21*0Sstevel@tonic-gate 
22*0Sstevel@tonic-gate /* hash key -- defined separately for use as shared pointer */
23*0Sstevel@tonic-gate struct hek {
24*0Sstevel@tonic-gate     U32		hek_hash;	/* hash of key */
25*0Sstevel@tonic-gate     I32		hek_len;	/* length of hash key */
26*0Sstevel@tonic-gate     char	hek_key[1];	/* variable-length hash key */
27*0Sstevel@tonic-gate     /* the hash-key is \0-terminated */
28*0Sstevel@tonic-gate     /* after the \0 there is a byte for flags, such as whether the key
29*0Sstevel@tonic-gate        is UTF-8 */
30*0Sstevel@tonic-gate };
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate /* hash structure: */
33*0Sstevel@tonic-gate /* This structure must match the beginning of struct xpvmg in sv.h. */
34*0Sstevel@tonic-gate struct xpvhv {
35*0Sstevel@tonic-gate     char *	xhv_array;	/* pointer to malloced string */
36*0Sstevel@tonic-gate     STRLEN	xhv_fill;	/* how full xhv_array currently is */
37*0Sstevel@tonic-gate     STRLEN	xhv_max;	/* subscript of last element of xhv_array */
38*0Sstevel@tonic-gate     IV		xhv_keys;	/* how many elements in the array */
39*0Sstevel@tonic-gate     NV		xnv_nv;		/* numeric value, if any */
40*0Sstevel@tonic-gate #define xhv_placeholders xnv_nv
41*0Sstevel@tonic-gate     MAGIC*	xmg_magic;	/* magic for scalar array */
42*0Sstevel@tonic-gate     HV*		xmg_stash;	/* class package */
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate     I32		xhv_riter;	/* current root of iterator */
45*0Sstevel@tonic-gate     HE		*xhv_eiter;	/* current entry of iterator */
46*0Sstevel@tonic-gate     PMOP	*xhv_pmroot;	/* list of pm's for this package */
47*0Sstevel@tonic-gate     char	*xhv_name;	/* name, if a symbol table */
48*0Sstevel@tonic-gate };
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate /* hash a key */
51*0Sstevel@tonic-gate /* FYI: This is the "One-at-a-Time" algorithm by Bob Jenkins
52*0Sstevel@tonic-gate  * from requirements by Colin Plumb.
53*0Sstevel@tonic-gate  * (http://burtleburtle.net/bob/hash/doobs.html) */
54*0Sstevel@tonic-gate /* The use of a temporary pointer and the casting games
55*0Sstevel@tonic-gate  * is needed to serve the dual purposes of
56*0Sstevel@tonic-gate  * (a) the hashed data being interpreted as "unsigned char" (new since 5.8,
57*0Sstevel@tonic-gate  *     a "char" can be either signed or signed, depending on the compiler)
58*0Sstevel@tonic-gate  * (b) catering for old code that uses a "char"
59*0Sstevel@tonic-gate  *
60*0Sstevel@tonic-gate  * The "hash seed" feature was added in Perl 5.8.1 to perturb the results
61*0Sstevel@tonic-gate  * to avoid "algorithmic complexity attacks".
62*0Sstevel@tonic-gate  *
63*0Sstevel@tonic-gate  * If USE_HASH_SEED is defined, hash randomisation is done by default
64*0Sstevel@tonic-gate  * If USE_HASH_SEED_EXPLICIT is defined, hash randomisation is done
65*0Sstevel@tonic-gate  * only if the environment variable PERL_HASH_SEED is set.
66*0Sstevel@tonic-gate  * For maximal control, one can define PERL_HASH_SEED.
67*0Sstevel@tonic-gate  * (see also erl.c:perl_parse()).
68*0Sstevel@tonic-gate  */
69*0Sstevel@tonic-gate #ifndef PERL_HASH_SEED
70*0Sstevel@tonic-gate #   if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT)
71*0Sstevel@tonic-gate #       define PERL_HASH_SEED	PL_hash_seed
72*0Sstevel@tonic-gate #   else
73*0Sstevel@tonic-gate #       define PERL_HASH_SEED	0
74*0Sstevel@tonic-gate #   endif
75*0Sstevel@tonic-gate #endif
76*0Sstevel@tonic-gate #define PERL_HASH(hash,str,len) \
77*0Sstevel@tonic-gate      STMT_START	{ \
78*0Sstevel@tonic-gate 	register const char *s_PeRlHaSh_tmp = str; \
79*0Sstevel@tonic-gate 	register const unsigned char *s_PeRlHaSh = (const unsigned char *)s_PeRlHaSh_tmp; \
80*0Sstevel@tonic-gate 	register I32 i_PeRlHaSh = len; \
81*0Sstevel@tonic-gate 	register U32 hash_PeRlHaSh = PERL_HASH_SEED; \
82*0Sstevel@tonic-gate 	while (i_PeRlHaSh--) { \
83*0Sstevel@tonic-gate 	    hash_PeRlHaSh += *s_PeRlHaSh++; \
84*0Sstevel@tonic-gate 	    hash_PeRlHaSh += (hash_PeRlHaSh << 10); \
85*0Sstevel@tonic-gate 	    hash_PeRlHaSh ^= (hash_PeRlHaSh >> 6); \
86*0Sstevel@tonic-gate 	} \
87*0Sstevel@tonic-gate 	hash_PeRlHaSh += (hash_PeRlHaSh << 3); \
88*0Sstevel@tonic-gate 	hash_PeRlHaSh ^= (hash_PeRlHaSh >> 11); \
89*0Sstevel@tonic-gate 	(hash) = (hash_PeRlHaSh + (hash_PeRlHaSh << 15)); \
90*0Sstevel@tonic-gate     } STMT_END
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate /* Only hv.c and mod_perl should be doing this.  */
93*0Sstevel@tonic-gate #ifdef PERL_HASH_INTERNAL_ACCESS
94*0Sstevel@tonic-gate #define PERL_HASH_INTERNAL(hash,str,len) \
95*0Sstevel@tonic-gate      STMT_START	{ \
96*0Sstevel@tonic-gate 	register const char *s_PeRlHaSh_tmp = str; \
97*0Sstevel@tonic-gate 	register const unsigned char *s_PeRlHaSh = (const unsigned char *)s_PeRlHaSh_tmp; \
98*0Sstevel@tonic-gate 	register I32 i_PeRlHaSh = len; \
99*0Sstevel@tonic-gate 	register U32 hash_PeRlHaSh = PL_rehash_seed; \
100*0Sstevel@tonic-gate 	while (i_PeRlHaSh--) { \
101*0Sstevel@tonic-gate 	    hash_PeRlHaSh += *s_PeRlHaSh++; \
102*0Sstevel@tonic-gate 	    hash_PeRlHaSh += (hash_PeRlHaSh << 10); \
103*0Sstevel@tonic-gate 	    hash_PeRlHaSh ^= (hash_PeRlHaSh >> 6); \
104*0Sstevel@tonic-gate 	} \
105*0Sstevel@tonic-gate 	hash_PeRlHaSh += (hash_PeRlHaSh << 3); \
106*0Sstevel@tonic-gate 	hash_PeRlHaSh ^= (hash_PeRlHaSh >> 11); \
107*0Sstevel@tonic-gate 	(hash) = (hash_PeRlHaSh + (hash_PeRlHaSh << 15)); \
108*0Sstevel@tonic-gate     } STMT_END
109*0Sstevel@tonic-gate #endif
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate /*
112*0Sstevel@tonic-gate =head1 Hash Manipulation Functions
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate =for apidoc AmU||HEf_SVKEY
115*0Sstevel@tonic-gate This flag, used in the length slot of hash entries and magic structures,
116*0Sstevel@tonic-gate specifies the structure contains an C<SV*> pointer where a C<char*> pointer
117*0Sstevel@tonic-gate is to be expected. (For information only--not to be used).
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate =head1 Handy Values
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate =for apidoc AmU||Nullhv
122*0Sstevel@tonic-gate Null HV pointer.
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate =head1 Hash Manipulation Functions
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate =for apidoc Am|char*|HvNAME|HV* stash
127*0Sstevel@tonic-gate Returns the package name of a stash.  See C<SvSTASH>, C<CvSTASH>.
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate =for apidoc Am|void*|HeKEY|HE* he
130*0Sstevel@tonic-gate Returns the actual pointer stored in the key slot of the hash entry. The
131*0Sstevel@tonic-gate pointer may be either C<char*> or C<SV*>, depending on the value of
132*0Sstevel@tonic-gate C<HeKLEN()>.  Can be assigned to.  The C<HePV()> or C<HeSVKEY()> macros are
133*0Sstevel@tonic-gate usually preferable for finding the value of a key.
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate =for apidoc Am|STRLEN|HeKLEN|HE* he
136*0Sstevel@tonic-gate If this is negative, and amounts to C<HEf_SVKEY>, it indicates the entry
137*0Sstevel@tonic-gate holds an C<SV*> key.  Otherwise, holds the actual length of the key.  Can
138*0Sstevel@tonic-gate be assigned to. The C<HePV()> macro is usually preferable for finding key
139*0Sstevel@tonic-gate lengths.
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate =for apidoc Am|SV*|HeVAL|HE* he
142*0Sstevel@tonic-gate Returns the value slot (type C<SV*>) stored in the hash entry.
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate =for apidoc Am|U32|HeHASH|HE* he
145*0Sstevel@tonic-gate Returns the computed hash stored in the hash entry.
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate =for apidoc Am|char*|HePV|HE* he|STRLEN len
148*0Sstevel@tonic-gate Returns the key slot of the hash entry as a C<char*> value, doing any
149*0Sstevel@tonic-gate necessary dereferencing of possibly C<SV*> keys.  The length of the string
150*0Sstevel@tonic-gate is placed in C<len> (this is a macro, so do I<not> use C<&len>).  If you do
151*0Sstevel@tonic-gate not care about what the length of the key is, you may use the global
152*0Sstevel@tonic-gate variable C<PL_na>, though this is rather less efficient than using a local
153*0Sstevel@tonic-gate variable.  Remember though, that hash keys in perl are free to contain
154*0Sstevel@tonic-gate embedded nulls, so using C<strlen()> or similar is not a good way to find
155*0Sstevel@tonic-gate the length of hash keys. This is very similar to the C<SvPV()> macro
156*0Sstevel@tonic-gate described elsewhere in this document.
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate =for apidoc Am|SV*|HeSVKEY|HE* he
159*0Sstevel@tonic-gate Returns the key as an C<SV*>, or C<Nullsv> if the hash entry does not
160*0Sstevel@tonic-gate contain an C<SV*> key.
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate =for apidoc Am|SV*|HeSVKEY_force|HE* he
163*0Sstevel@tonic-gate Returns the key as an C<SV*>.  Will create and return a temporary mortal
164*0Sstevel@tonic-gate C<SV*> if the hash entry contains only a C<char*> key.
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate =for apidoc Am|SV*|HeSVKEY_set|HE* he|SV* sv
167*0Sstevel@tonic-gate Sets the key to a given C<SV*>, taking care to set the appropriate flags to
168*0Sstevel@tonic-gate indicate the presence of an C<SV*> key, and returns the same
169*0Sstevel@tonic-gate C<SV*>.
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate =cut
172*0Sstevel@tonic-gate */
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate /* these hash entry flags ride on hent_klen (for use only in magic/tied HVs) */
175*0Sstevel@tonic-gate #define HEf_SVKEY	-2	/* hent_key is an SV* */
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate #define Nullhv Null(HV*)
179*0Sstevel@tonic-gate #define HvARRAY(hv)	(*(HE***)&((XPVHV*)  SvANY(hv))->xhv_array)
180*0Sstevel@tonic-gate #define HvFILL(hv)	((XPVHV*)  SvANY(hv))->xhv_fill
181*0Sstevel@tonic-gate #define HvMAX(hv)	((XPVHV*)  SvANY(hv))->xhv_max
182*0Sstevel@tonic-gate #define HvRITER(hv)	((XPVHV*)  SvANY(hv))->xhv_riter
183*0Sstevel@tonic-gate #define HvEITER(hv)	((XPVHV*)  SvANY(hv))->xhv_eiter
184*0Sstevel@tonic-gate #define HvPMROOT(hv)	((XPVHV*)  SvANY(hv))->xhv_pmroot
185*0Sstevel@tonic-gate #define HvNAME(hv)	((XPVHV*)  SvANY(hv))->xhv_name
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate /* the number of keys (including any placeholers) */
188*0Sstevel@tonic-gate #define XHvTOTALKEYS(xhv)	((xhv)->xhv_keys)
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate /* The number of placeholders in the enumerated-keys hash */
191*0Sstevel@tonic-gate #define XHvPLACEHOLDERS(xhv)	((xhv)->xhv_placeholders)
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate /* the number of keys that exist() (i.e. excluding placeholders) */
194*0Sstevel@tonic-gate #define XHvUSEDKEYS(xhv)      (XHvTOTALKEYS(xhv) - (IV)XHvPLACEHOLDERS(xhv))
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate /*
197*0Sstevel@tonic-gate  * HvKEYS gets the number of keys that actually exist(), and is provided
198*0Sstevel@tonic-gate  * for backwards compatibility with old XS code. The core uses HvUSEDKEYS
199*0Sstevel@tonic-gate  * (keys, excluding placeholdes) and HvTOTALKEYS (including placeholders)
200*0Sstevel@tonic-gate  */
201*0Sstevel@tonic-gate #define HvKEYS(hv)		XHvUSEDKEYS((XPVHV*)  SvANY(hv))
202*0Sstevel@tonic-gate #define HvUSEDKEYS(hv)		XHvUSEDKEYS((XPVHV*)  SvANY(hv))
203*0Sstevel@tonic-gate #define HvTOTALKEYS(hv)		XHvTOTALKEYS((XPVHV*)  SvANY(hv))
204*0Sstevel@tonic-gate #define HvPLACEHOLDERS(hv)	XHvPLACEHOLDERS((XPVHV*)  SvANY(hv))
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate #define HvSHAREKEYS(hv)		(SvFLAGS(hv) & SVphv_SHAREKEYS)
207*0Sstevel@tonic-gate #define HvSHAREKEYS_on(hv)	(SvFLAGS(hv) |= SVphv_SHAREKEYS)
208*0Sstevel@tonic-gate #define HvSHAREKEYS_off(hv)	(SvFLAGS(hv) &= ~SVphv_SHAREKEYS)
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate /* This is an optimisation flag. It won't be set if all hash keys have a 0
211*0Sstevel@tonic-gate  * flag. Currently the only flags relate to utf8.
212*0Sstevel@tonic-gate  * Hence it won't be set if all keys are 8 bit only. It will be set if any key
213*0Sstevel@tonic-gate  * is utf8 (including 8 bit keys that were entered as utf8, and need upgrading
214*0Sstevel@tonic-gate  * when retrieved during iteration. It may still be set when there are no longer
215*0Sstevel@tonic-gate  * any utf8 keys.
216*0Sstevel@tonic-gate  * See HVhek_ENABLEHVKFLAGS for the trigger.
217*0Sstevel@tonic-gate  */
218*0Sstevel@tonic-gate #define HvHASKFLAGS(hv)		(SvFLAGS(hv) & SVphv_HASKFLAGS)
219*0Sstevel@tonic-gate #define HvHASKFLAGS_on(hv)	(SvFLAGS(hv) |= SVphv_HASKFLAGS)
220*0Sstevel@tonic-gate #define HvHASKFLAGS_off(hv)	(SvFLAGS(hv) &= ~SVphv_HASKFLAGS)
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate #define HvLAZYDEL(hv)		(SvFLAGS(hv) & SVphv_LAZYDEL)
223*0Sstevel@tonic-gate #define HvLAZYDEL_on(hv)	(SvFLAGS(hv) |= SVphv_LAZYDEL)
224*0Sstevel@tonic-gate #define HvLAZYDEL_off(hv)	(SvFLAGS(hv) &= ~SVphv_LAZYDEL)
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate #define HvREHASH(hv)		(SvFLAGS(hv) & SVphv_REHASH)
227*0Sstevel@tonic-gate #define HvREHASH_on(hv)		(SvFLAGS(hv) |= SVphv_REHASH)
228*0Sstevel@tonic-gate #define HvREHASH_off(hv)	(SvFLAGS(hv) &= ~SVphv_REHASH)
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate /* Maybe amagical: */
231*0Sstevel@tonic-gate /* #define HV_AMAGICmb(hv)      (SvFLAGS(hv) & (SVpgv_badAM | SVpgv_AM)) */
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate #define HV_AMAGIC(hv)        (SvFLAGS(hv) &   SVpgv_AM)
234*0Sstevel@tonic-gate #define HV_AMAGIC_on(hv)     (SvFLAGS(hv) |=  SVpgv_AM)
235*0Sstevel@tonic-gate #define HV_AMAGIC_off(hv)    (SvFLAGS(hv) &= ~SVpgv_AM)
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate /*
238*0Sstevel@tonic-gate #define HV_AMAGICbad(hv)     (SvFLAGS(hv) & SVpgv_badAM)
239*0Sstevel@tonic-gate #define HV_badAMAGIC_on(hv)  (SvFLAGS(hv) |= SVpgv_badAM)
240*0Sstevel@tonic-gate #define HV_badAMAGIC_off(hv) (SvFLAGS(hv) &= ~SVpgv_badAM)
241*0Sstevel@tonic-gate */
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate #define Nullhe Null(HE*)
244*0Sstevel@tonic-gate #define HeNEXT(he)		(he)->hent_next
245*0Sstevel@tonic-gate #define HeKEY_hek(he)		(he)->hent_hek
246*0Sstevel@tonic-gate #define HeKEY(he)		HEK_KEY(HeKEY_hek(he))
247*0Sstevel@tonic-gate #define HeKEY_sv(he)		(*(SV**)HeKEY(he))
248*0Sstevel@tonic-gate #define HeKLEN(he)		HEK_LEN(HeKEY_hek(he))
249*0Sstevel@tonic-gate #define HeKUTF8(he)  HEK_UTF8(HeKEY_hek(he))
250*0Sstevel@tonic-gate #define HeKWASUTF8(he)  HEK_WASUTF8(HeKEY_hek(he))
251*0Sstevel@tonic-gate #define HeKREHASH(he)  HEK_REHASH(HeKEY_hek(he))
252*0Sstevel@tonic-gate #define HeKLEN_UTF8(he)  (HeKUTF8(he) ? -HeKLEN(he) : HeKLEN(he))
253*0Sstevel@tonic-gate #define HeKFLAGS(he)  HEK_FLAGS(HeKEY_hek(he))
254*0Sstevel@tonic-gate #define HeVAL(he)		(he)->hent_val
255*0Sstevel@tonic-gate #define HeHASH(he)		HEK_HASH(HeKEY_hek(he))
256*0Sstevel@tonic-gate #define HePV(he,lp)		((HeKLEN(he) == HEf_SVKEY) ?		\
257*0Sstevel@tonic-gate 				 SvPV(HeKEY_sv(he),lp) :		\
258*0Sstevel@tonic-gate 				 (((lp = HeKLEN(he)) >= 0) ?		\
259*0Sstevel@tonic-gate 				  HeKEY(he) : Nullch))
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate #define HeSVKEY(he)		((HeKEY(he) && 				\
262*0Sstevel@tonic-gate 				  HeKLEN(he) == HEf_SVKEY) ?		\
263*0Sstevel@tonic-gate 				 HeKEY_sv(he) : Nullsv)
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate #define HeSVKEY_force(he)	(HeKEY(he) ?				\
266*0Sstevel@tonic-gate 				 ((HeKLEN(he) == HEf_SVKEY) ?		\
267*0Sstevel@tonic-gate 				  HeKEY_sv(he) :			\
268*0Sstevel@tonic-gate 				  sv_2mortal(newSVpvn(HeKEY(he),	\
269*0Sstevel@tonic-gate 						     HeKLEN(he)))) :	\
270*0Sstevel@tonic-gate 				 &PL_sv_undef)
271*0Sstevel@tonic-gate #define HeSVKEY_set(he,sv)	((HeKLEN(he) = HEf_SVKEY), (HeKEY_sv(he) = sv))
272*0Sstevel@tonic-gate 
273*0Sstevel@tonic-gate #define Nullhek Null(HEK*)
274*0Sstevel@tonic-gate #define HEK_BASESIZE		STRUCT_OFFSET(HEK, hek_key[0])
275*0Sstevel@tonic-gate #define HEK_HASH(hek)		(hek)->hek_hash
276*0Sstevel@tonic-gate #define HEK_LEN(hek)		(hek)->hek_len
277*0Sstevel@tonic-gate #define HEK_KEY(hek)		(hek)->hek_key
278*0Sstevel@tonic-gate #define HEK_FLAGS(hek)	(*((unsigned char *)(HEK_KEY(hek))+HEK_LEN(hek)+1))
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate #define HVhek_UTF8	0x01 /* Key is utf8 encoded. */
281*0Sstevel@tonic-gate #define HVhek_WASUTF8	0x02 /* Key is bytes here, but was supplied as utf8. */
282*0Sstevel@tonic-gate #define HVhek_REHASH	0x04 /* This key is in an hv using a custom HASH . */
283*0Sstevel@tonic-gate #define HVhek_FREEKEY	0x100 /* Internal flag to say key is malloc()ed.  */
284*0Sstevel@tonic-gate #define HVhek_PLACEHOLD	0x200 /* Internal flag to create placeholder.
285*0Sstevel@tonic-gate                                * (may change, but Storable is a core module) */
286*0Sstevel@tonic-gate #define HVhek_MASK	0xFF
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate /* Which flags enable HvHASKFLAGS? Somewhat a hack on a hack, as
289*0Sstevel@tonic-gate    HVhek_REHASH is only needed because the rehash flag has to be duplicated
290*0Sstevel@tonic-gate    into all keys as hv_iternext has no access to the hash flags. At this
291*0Sstevel@tonic-gate    point Storable's tests get upset, because sometimes hashes are "keyed"
292*0Sstevel@tonic-gate    and sometimes not, depending on the order of data insertion, and whether
293*0Sstevel@tonic-gate    it triggered rehashing. So currently HVhek_REHAS is exempt.
294*0Sstevel@tonic-gate */
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate #define HVhek_ENABLEHVKFLAGS	(HVhek_MASK - HVhek_REHASH)
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate #define HEK_UTF8(hek)		(HEK_FLAGS(hek) & HVhek_UTF8)
299*0Sstevel@tonic-gate #define HEK_UTF8_on(hek)	(HEK_FLAGS(hek) |= HVhek_UTF8)
300*0Sstevel@tonic-gate #define HEK_UTF8_off(hek)	(HEK_FLAGS(hek) &= ~HVhek_UTF8)
301*0Sstevel@tonic-gate #define HEK_WASUTF8(hek)	(HEK_FLAGS(hek) & HVhek_WASUTF8)
302*0Sstevel@tonic-gate #define HEK_WASUTF8_on(hek)	(HEK_FLAGS(hek) |= HVhek_WASUTF8)
303*0Sstevel@tonic-gate #define HEK_WASUTF8_off(hek)	(HEK_FLAGS(hek) &= ~HVhek_WASUTF8)
304*0Sstevel@tonic-gate #define HEK_REHASH(hek)		(HEK_FLAGS(hek) & HVhek_REHASH)
305*0Sstevel@tonic-gate #define HEK_REHASH_on(hek)	(HEK_FLAGS(hek) |= HVhek_REHASH)
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate /* calculate HV array allocation */
308*0Sstevel@tonic-gate #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
309*0Sstevel@tonic-gate #  define PERL_HV_ARRAY_ALLOC_BYTES(size) ((size) * sizeof(HE*))
310*0Sstevel@tonic-gate #else
311*0Sstevel@tonic-gate #  define MALLOC_OVERHEAD 16
312*0Sstevel@tonic-gate #  define PERL_HV_ARRAY_ALLOC_BYTES(size) \
313*0Sstevel@tonic-gate 			(((size) < 64)					\
314*0Sstevel@tonic-gate 			 ? (size) * sizeof(HE*)				\
315*0Sstevel@tonic-gate 			 : (size) * sizeof(HE*) * 2 - MALLOC_OVERHEAD)
316*0Sstevel@tonic-gate #endif
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate /* Flags for hv_iternext_flags.  */
319*0Sstevel@tonic-gate #define HV_ITERNEXT_WANTPLACEHOLDERS	0x01	/* Don't skip placeholders.  */
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate /* available as a function in hv.c */
322*0Sstevel@tonic-gate #define Perl_sharepvn(sv, len, hash) HEK_KEY(share_hek(sv, len, hash))
323*0Sstevel@tonic-gate #define sharepvn(sv, len, hash)	     Perl_sharepvn(sv, len, hash)
324