This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
As PERL_HV_ARRAY_ALLOC_BYTES is bytes, not items, the type should be
[perl5.git] / hv.h
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 */
12 typedef struct he HE;
13 typedef struct hek HEK;
14
15 /* entry in hash value chain */
16 struct 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 */
23 struct 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
33 /* Subject to change.
34    Don't access this directly.
35 */
36 struct xpvhv_aux {
37     HEK         *xhv_name;      /* name, if a symbol table */
38     HE          *xhv_eiter;     /* current entry of iterator */
39     I32         xhv_riter;      /* current root of iterator */
40 };
41
42 #define HV_AUX_SIZE STRUCT_OFFSET(struct xpvhv_aux, xhv_array)
43
44 /* hash structure: */
45 /* This structure must match the beginning of struct xpvmg in sv.h. */
46 struct xpvhv {
47     NV          xnv_nv;         /* numeric value, if any */
48     STRLEN      xhv_fill;       /* how full xhv_array currently is */
49     STRLEN      xhv_max;        /* subscript of last element of xhv_array */
50     union {
51         IV      xivu_iv;        /* integer value or pv offset */
52         UV      xivu_uv;
53         void *  xivu_p1;
54     }           xiv_u;
55     MAGIC*      xmg_magic;      /* magic for scalar array */
56     HV*         xmg_stash;      /* class package */
57 };
58
59 #define xhv_keys xiv_u.xivu_iv
60
61 #if 0
62 typedef struct xpvhv xpvhv_allocated;
63 #else
64 typedef struct {
65     STRLEN      xhv_fill;       /* how full xhv_array currently is */
66     STRLEN      xhv_max;        /* subscript of last element of xhv_array */
67     union {
68         IV      xivu_iv;        /* integer value or pv offset */
69         UV      xivu_uv;
70         void *  xivu_p1;
71     }           xiv_u;
72     MAGIC*      xmg_magic;      /* magic for scalar array */
73     HV*         xmg_stash;      /* class package */
74 } xpvhv_allocated;
75 #endif
76
77 /* hash a key */
78 /* FYI: This is the "One-at-a-Time" algorithm by Bob Jenkins
79  * from requirements by Colin Plumb.
80  * (http://burtleburtle.net/bob/hash/doobs.html) */
81 /* The use of a temporary pointer and the casting games
82  * is needed to serve the dual purposes of
83  * (a) the hashed data being interpreted as "unsigned char" (new since 5.8,
84  *     a "char" can be either signed or signed, depending on the compiler)
85  * (b) catering for old code that uses a "char"
86  *
87  * The "hash seed" feature was added in Perl 5.8.1 to perturb the results
88  * to avoid "algorithmic complexity attacks".
89  *
90  * If USE_HASH_SEED is defined, hash randomisation is done by default
91  * If USE_HASH_SEED_EXPLICIT is defined, hash randomisation is done
92  * only if the environment variable PERL_HASH_SEED is set.
93  * For maximal control, one can define PERL_HASH_SEED.
94  * (see also perl.c:perl_parse()).
95  */
96 #ifndef PERL_HASH_SEED
97 #   if defined(USE_HASH_SEED) || defined(USE_HASH_SEED_EXPLICIT)
98 #       define PERL_HASH_SEED   PL_hash_seed
99 #   else
100 #       define PERL_HASH_SEED   0
101 #   endif
102 #endif
103 #define PERL_HASH(hash,str,len) \
104      STMT_START { \
105         register const char *s_PeRlHaSh_tmp = str; \
106         register const unsigned char *s_PeRlHaSh = (const unsigned char *)s_PeRlHaSh_tmp; \
107         register I32 i_PeRlHaSh = len; \
108         register U32 hash_PeRlHaSh = PERL_HASH_SEED; \
109         while (i_PeRlHaSh--) { \
110             hash_PeRlHaSh += *s_PeRlHaSh++; \
111             hash_PeRlHaSh += (hash_PeRlHaSh << 10); \
112             hash_PeRlHaSh ^= (hash_PeRlHaSh >> 6); \
113         } \
114         hash_PeRlHaSh += (hash_PeRlHaSh << 3); \
115         hash_PeRlHaSh ^= (hash_PeRlHaSh >> 11); \
116         (hash) = (hash_PeRlHaSh + (hash_PeRlHaSh << 15)); \
117     } STMT_END
118
119 /* Only hv.c and mod_perl should be doing this.  */
120 #ifdef PERL_HASH_INTERNAL_ACCESS
121 #define PERL_HASH_INTERNAL(hash,str,len) \
122      STMT_START { \
123         register const char *s_PeRlHaSh_tmp = str; \
124         register const unsigned char *s_PeRlHaSh = (const unsigned char *)s_PeRlHaSh_tmp; \
125         register I32 i_PeRlHaSh = len; \
126         register U32 hash_PeRlHaSh = PL_rehash_seed; \
127         while (i_PeRlHaSh--) { \
128             hash_PeRlHaSh += *s_PeRlHaSh++; \
129             hash_PeRlHaSh += (hash_PeRlHaSh << 10); \
130             hash_PeRlHaSh ^= (hash_PeRlHaSh >> 6); \
131         } \
132         hash_PeRlHaSh += (hash_PeRlHaSh << 3); \
133         hash_PeRlHaSh ^= (hash_PeRlHaSh >> 11); \
134         (hash) = (hash_PeRlHaSh + (hash_PeRlHaSh << 15)); \
135     } STMT_END
136 #endif
137
138 /*
139 =head1 Hash Manipulation Functions
140
141 =for apidoc AmU||HEf_SVKEY
142 This flag, used in the length slot of hash entries and magic structures,
143 specifies the structure contains an C<SV*> pointer where a C<char*> pointer
144 is to be expected. (For information only--not to be used).
145
146 =head1 Handy Values
147
148 =for apidoc AmU||Nullhv
149 Null HV pointer.
150
151 =head1 Hash Manipulation Functions
152
153 =for apidoc Am|char*|HvNAME|HV* stash
154 Returns the package name of a stash.  See C<SvSTASH>, C<CvSTASH>.
155
156 =for apidoc Am|void*|HeKEY|HE* he
157 Returns the actual pointer stored in the key slot of the hash entry. The
158 pointer may be either C<char*> or C<SV*>, depending on the value of
159 C<HeKLEN()>.  Can be assigned to.  The C<HePV()> or C<HeSVKEY()> macros are
160 usually preferable for finding the value of a key.
161
162 =for apidoc Am|STRLEN|HeKLEN|HE* he
163 If this is negative, and amounts to C<HEf_SVKEY>, it indicates the entry
164 holds an C<SV*> key.  Otherwise, holds the actual length of the key.  Can
165 be assigned to. The C<HePV()> macro is usually preferable for finding key
166 lengths.
167
168 =for apidoc Am|SV*|HeVAL|HE* he
169 Returns the value slot (type C<SV*>) stored in the hash entry.
170
171 =for apidoc Am|U32|HeHASH|HE* he
172 Returns the computed hash stored in the hash entry.
173
174 =for apidoc Am|char*|HePV|HE* he|STRLEN len
175 Returns the key slot of the hash entry as a C<char*> value, doing any
176 necessary dereferencing of possibly C<SV*> keys.  The length of the string
177 is placed in C<len> (this is a macro, so do I<not> use C<&len>).  If you do
178 not care about what the length of the key is, you may use the global
179 variable C<PL_na>, though this is rather less efficient than using a local
180 variable.  Remember though, that hash keys in perl are free to contain
181 embedded nulls, so using C<strlen()> or similar is not a good way to find
182 the length of hash keys. This is very similar to the C<SvPV()> macro
183 described elsewhere in this document.
184
185 =for apidoc Am|SV*|HeSVKEY|HE* he
186 Returns the key as an C<SV*>, or C<Nullsv> if the hash entry does not
187 contain an C<SV*> key.
188
189 =for apidoc Am|SV*|HeSVKEY_force|HE* he
190 Returns the key as an C<SV*>.  Will create and return a temporary mortal
191 C<SV*> if the hash entry contains only a C<char*> key.
192
193 =for apidoc Am|SV*|HeSVKEY_set|HE* he|SV* sv
194 Sets the key to a given C<SV*>, taking care to set the appropriate flags to
195 indicate the presence of an C<SV*> key, and returns the same
196 C<SV*>.
197
198 =cut
199 */
200
201 /* these hash entry flags ride on hent_klen (for use only in magic/tied HVs) */
202 #define HEf_SVKEY       -2      /* hent_key is an SV* */
203
204
205 #define Nullhv Null(HV*)
206 #define HvARRAY(hv)     (*(HE***)&((hv)->sv_u.svu_array))
207 #define HvFILL(hv)      ((XPVHV*)  SvANY(hv))->xhv_fill
208 #define HvMAX(hv)       ((XPVHV*)  SvANY(hv))->xhv_max
209 /* This quite intentionally does no flag checking first. That's your
210    responsibility.  */
211 #define HvAUX(hv)       ((struct xpvhv_aux*)&(HvARRAY(hv)[HvMAX(hv)+1]))
212 #define HvRITER(hv)     (*Perl_hv_riter_p(aTHX_ (HV*)(hv)))
213 #define HvEITER(hv)     (*Perl_hv_eiter_p(aTHX_ (HV*)(hv)))
214 #define HvRITER_set(hv,r)       Perl_hv_riter_set(aTHX_ (HV*)(hv), r)
215 #define HvEITER_set(hv,e)       Perl_hv_eiter_set(aTHX_ (HV*)(hv), e)
216 #define HvRITER_get(hv) (SvOOK(hv) ? HvAUX(hv)->xhv_riter : -1)
217 #define HvEITER_get(hv) (SvOOK(hv) ? HvAUX(hv)->xhv_eiter : 0)
218 #define HvNAME(hv)      HvNAME_get(hv)
219 /* FIXME - all of these should use a UTF8 aware API, which should also involve
220    getting the length. */
221 /* This macro may go away without notice.  */
222 #define HvNAME_HEK(hv) (SvOOK(hv) ? HvAUX(hv)->xhv_name : 0)
223 #define HvNAME_get(hv)  ((SvOOK(hv) && (HvAUX(hv)->xhv_name)) \
224                          ? HEK_KEY(HvAUX(hv)->xhv_name) : 0)
225 #define HvNAMELEN_get(hv)       ((SvOOK(hv) && (HvAUX(hv)->xhv_name)) \
226                                  ? HEK_LEN(HvAUX(hv)->xhv_name) : 0)
227
228 /* the number of keys (including any placeholers) */
229 #define XHvTOTALKEYS(xhv)       ((xhv)->xhv_keys)
230
231 /*
232  * HvKEYS gets the number of keys that actually exist(), and is provided
233  * for backwards compatibility with old XS code. The core uses HvUSEDKEYS
234  * (keys, excluding placeholdes) and HvTOTALKEYS (including placeholders)
235  */
236 #define HvKEYS(hv)              HvUSEDKEYS(hv)
237 #define HvUSEDKEYS(hv)          (HvTOTALKEYS(hv) - HvPLACEHOLDERS_get(hv))
238 #define HvTOTALKEYS(hv)         XHvTOTALKEYS((XPVHV*)  SvANY(hv))
239 #define HvPLACEHOLDERS(hv)      (*Perl_hv_placeholders_p(aTHX_ (HV*)hv))
240 #define HvPLACEHOLDERS_get(hv)  (SvMAGIC(hv) ? Perl_hv_placeholders_get(aTHX_ (HV*)hv) : 0)
241 #define HvPLACEHOLDERS_set(hv,p)        Perl_hv_placeholders_set(aTHX_ (HV*)hv, p)
242
243 #define HvSHAREKEYS(hv)         (SvFLAGS(hv) & SVphv_SHAREKEYS)
244 #define HvSHAREKEYS_on(hv)      (SvFLAGS(hv) |= SVphv_SHAREKEYS)
245 #define HvSHAREKEYS_off(hv)     (SvFLAGS(hv) &= ~SVphv_SHAREKEYS)
246
247 /* This is an optimisation flag. It won't be set if all hash keys have a 0
248  * flag. Currently the only flags relate to utf8.
249  * Hence it won't be set if all keys are 8 bit only. It will be set if any key
250  * is utf8 (including 8 bit keys that were entered as utf8, and need upgrading
251  * when retrieved during iteration. It may still be set when there are no longer
252  * any utf8 keys.
253  * See HVhek_ENABLEHVKFLAGS for the trigger.
254  */
255 #define HvHASKFLAGS(hv)         (SvFLAGS(hv) & SVphv_HASKFLAGS)
256 #define HvHASKFLAGS_on(hv)      (SvFLAGS(hv) |= SVphv_HASKFLAGS)
257 #define HvHASKFLAGS_off(hv)     (SvFLAGS(hv) &= ~SVphv_HASKFLAGS)
258
259 #define HvLAZYDEL(hv)           (SvFLAGS(hv) & SVphv_LAZYDEL)
260 #define HvLAZYDEL_on(hv)        (SvFLAGS(hv) |= SVphv_LAZYDEL)
261 #define HvLAZYDEL_off(hv)       (SvFLAGS(hv) &= ~SVphv_LAZYDEL)
262
263 #define HvREHASH(hv)            (SvFLAGS(hv) & SVphv_REHASH)
264 #define HvREHASH_on(hv)         (SvFLAGS(hv) |= SVphv_REHASH)
265 #define HvREHASH_off(hv)        (SvFLAGS(hv) &= ~SVphv_REHASH)
266
267 /* Maybe amagical: */
268 /* #define HV_AMAGICmb(hv)      (SvFLAGS(hv) & (SVpgv_badAM | SVpgv_AM)) */
269
270 #define HV_AMAGIC(hv)        (SvFLAGS(hv) &   SVpgv_AM)
271 #define HV_AMAGIC_on(hv)     (SvFLAGS(hv) |=  SVpgv_AM)
272 #define HV_AMAGIC_off(hv)    (SvFLAGS(hv) &= ~SVpgv_AM)
273
274 /*
275 #define HV_AMAGICbad(hv)     (SvFLAGS(hv) & SVpgv_badAM)
276 #define HV_badAMAGIC_on(hv)  (SvFLAGS(hv) |= SVpgv_badAM)
277 #define HV_badAMAGIC_off(hv) (SvFLAGS(hv) &= ~SVpgv_badAM)
278 */
279
280 #define Nullhe Null(HE*)
281 #define HeNEXT(he)              (he)->hent_next
282 #define HeKEY_hek(he)           (he)->hent_hek
283 #define HeKEY(he)               HEK_KEY(HeKEY_hek(he))
284 #define HeKEY_sv(he)            (*(SV**)HeKEY(he))
285 #define HeKLEN(he)              HEK_LEN(HeKEY_hek(he))
286 #define HeKUTF8(he)  HEK_UTF8(HeKEY_hek(he))
287 #define HeKWASUTF8(he)  HEK_WASUTF8(HeKEY_hek(he))
288 #define HeKREHASH(he)  HEK_REHASH(HeKEY_hek(he))
289 #define HeKLEN_UTF8(he)  (HeKUTF8(he) ? -HeKLEN(he) : HeKLEN(he))
290 #define HeKFLAGS(he)  HEK_FLAGS(HeKEY_hek(he))
291 #define HeVAL(he)               (he)->hent_val
292 #define HeHASH(he)              HEK_HASH(HeKEY_hek(he))
293 #define HePV(he,lp)             ((HeKLEN(he) == HEf_SVKEY) ?            \
294                                  SvPV(HeKEY_sv(he),lp) :                \
295                                  (((lp = HeKLEN(he)) >= 0) ?            \
296                                   HeKEY(he) : Nullch))
297
298 #define HeSVKEY(he)             ((HeKEY(he) &&                          \
299                                   HeKLEN(he) == HEf_SVKEY) ?            \
300                                  HeKEY_sv(he) : Nullsv)
301
302 #define HeSVKEY_force(he)       (HeKEY(he) ?                            \
303                                  ((HeKLEN(he) == HEf_SVKEY) ?           \
304                                   HeKEY_sv(he) :                        \
305                                   sv_2mortal(newSVpvn(HeKEY(he),        \
306                                                      HeKLEN(he)))) :    \
307                                  &PL_sv_undef)
308 #define HeSVKEY_set(he,sv)      ((HeKLEN(he) = HEf_SVKEY), (HeKEY_sv(he) = sv))
309
310 #define Nullhek Null(HEK*)
311 #define HEK_BASESIZE            STRUCT_OFFSET(HEK, hek_key[0])
312 #define HEK_HASH(hek)           (hek)->hek_hash
313 #define HEK_LEN(hek)            (hek)->hek_len
314 #define HEK_KEY(hek)            (hek)->hek_key
315 #define HEK_FLAGS(hek)  (*((unsigned char *)(HEK_KEY(hek))+HEK_LEN(hek)+1))
316
317 #define HVhek_UTF8      0x01 /* Key is utf8 encoded. */
318 #define HVhek_WASUTF8   0x02 /* Key is bytes here, but was supplied as utf8. */
319 #define HVhek_REHASH    0x04 /* This key is in an hv using a custom HASH . */
320 #define HVhek_FREEKEY   0x100 /* Internal flag to say key is malloc()ed.  */
321 #define HVhek_PLACEHOLD 0x200 /* Internal flag to create placeholder.
322                                * (may change, but Storable is a core module) */
323 #define HVhek_MASK      0xFF
324
325 /* Which flags enable HvHASKFLAGS? Somewhat a hack on a hack, as
326    HVhek_REHASH is only needed because the rehash flag has to be duplicated
327    into all keys as hv_iternext has no access to the hash flags. At this
328    point Storable's tests get upset, because sometimes hashes are "keyed"
329    and sometimes not, depending on the order of data insertion, and whether
330    it triggered rehashing. So currently HVhek_REHAS is exempt.
331 */
332    
333 #define HVhek_ENABLEHVKFLAGS    (HVhek_MASK - HVhek_REHASH)
334
335 #define HEK_UTF8(hek)           (HEK_FLAGS(hek) & HVhek_UTF8)
336 #define HEK_UTF8_on(hek)        (HEK_FLAGS(hek) |= HVhek_UTF8)
337 #define HEK_UTF8_off(hek)       (HEK_FLAGS(hek) &= ~HVhek_UTF8)
338 #define HEK_WASUTF8(hek)        (HEK_FLAGS(hek) & HVhek_WASUTF8)
339 #define HEK_WASUTF8_on(hek)     (HEK_FLAGS(hek) |= HVhek_WASUTF8)
340 #define HEK_WASUTF8_off(hek)    (HEK_FLAGS(hek) &= ~HVhek_WASUTF8)
341 #define HEK_REHASH(hek)         (HEK_FLAGS(hek) & HVhek_REHASH)
342 #define HEK_REHASH_on(hek)      (HEK_FLAGS(hek) |= HVhek_REHASH)
343
344 /* calculate HV array allocation */
345 #ifndef PERL_USE_LARGE_HV_ALLOC
346 /* Default to allocating the correct size - default to assuming that malloc()
347    is not broken and is efficient at allocating blocks sized at powers-of-two.
348 */   
349 #  define PERL_HV_ARRAY_ALLOC_BYTES(size) ((size) * sizeof(HE*))
350 #else
351 #  define MALLOC_OVERHEAD 16
352 #  define PERL_HV_ARRAY_ALLOC_BYTES(size) \
353                         (((size) < 64)                                  \
354                          ? (size) * sizeof(HE*)                         \
355                          : (size) * sizeof(HE*) * 2 - MALLOC_OVERHEAD)
356 #endif
357
358 /* Flags for hv_iternext_flags.  */
359 #define HV_ITERNEXT_WANTPLACEHOLDERS    0x01    /* Don't skip placeholders.  */
360
361 /* available as a function in hv.c */
362 #define Perl_sharepvn(sv, len, hash) HEK_KEY(share_hek(sv, len, hash))
363 #define sharepvn(sv, len, hash)      Perl_sharepvn(sv, len, hash)
364
365 /*
366  * Local variables:
367  * c-indentation-style: bsd
368  * c-basic-offset: 4
369  * indent-tabs-mode: t
370  * End:
371  *
372  * ex: set ts=8 sts=4 sw=4 noet:
373  */