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