This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
s.
[perl5.git] / hv.h
1 /*    hv.h
2  *
3  *    Copyright (c) 1991-2002, Larry Wall
4  *
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.
7  *
8  */
9
10 /* typedefs to eliminate some typing */
11 typedef struct he HE;
12 typedef struct hek HEK;
13
14 /* entry in hash value chain */
15 struct he {
16     HE          *hent_next;     /* next entry in chain */
17     HEK         *hent_hek;      /* hash key */
18     SV          *hent_val;      /* scalar value that was hashed */
19 };
20
21 /* hash key -- defined separately for use as shared pointer */
22 struct hek {
23     U32         hek_hash;       /* hash of key */
24     I32         hek_len;        /* length of hash key */
25     char        hek_key[1];     /* variable-length hash key */
26     /* the hash-key is \0-terminated */
27     /* after the \0 there is a byte for flags, such as whether the key is
28        UTF8 */
29 };
30
31 /* hash structure: */
32 /* This structure must match the beginning of struct xpvmg in sv.h. */
33 struct xpvhv {
34     char *      xhv_array;      /* pointer to malloced string */
35     STRLEN      xhv_fill;       /* how full xhv_array currently is */
36     STRLEN      xhv_max;        /* subscript of last element of xhv_array */
37     IV          xhv_keys;       /* how many elements in the array */
38     NV          xnv_nv;         /* numeric value, if any */
39 #define xhv_placeholders xnv_nv
40     MAGIC*      xmg_magic;      /* magic for scalar array */
41     HV*         xmg_stash;      /* class package */
42
43     I32         xhv_riter;      /* current root of iterator */
44     HE          *xhv_eiter;     /* current entry of iterator */
45     PMOP        *xhv_pmroot;    /* list of pm's for this package */
46     char        *xhv_name;      /* name, if a symbol table */
47 };
48
49 /* hash a key */
50 /* FYI: This is the "One-at-a-Time" algorithm by Bob Jenkins */
51 /* from requirements by Colin Plumb. */
52 /* (http://burtleburtle.net/bob/hash/doobs.html) */
53 #define PERL_HASH(hash,str,len) \
54      STMT_START { \
55         register const char *s_PeRlHaSh = str; \
56         register I32 i_PeRlHaSh = len; \
57         register U32 hash_PeRlHaSh = 0; \
58         while (i_PeRlHaSh--) { \
59             hash_PeRlHaSh += *s_PeRlHaSh++; \
60             hash_PeRlHaSh += (hash_PeRlHaSh << 10); \
61             hash_PeRlHaSh ^= (hash_PeRlHaSh >> 6); \
62         } \
63         hash_PeRlHaSh += (hash_PeRlHaSh << 3); \
64         hash_PeRlHaSh ^= (hash_PeRlHaSh >> 11); \
65         (hash) = (hash_PeRlHaSh + (hash_PeRlHaSh << 15)); \
66     } STMT_END
67
68 /*
69 =head1 Hash Manipulation Functions
70
71 =for apidoc AmU||HEf_SVKEY
72 This flag, used in the length slot of hash entries and magic structures,
73 specifies the structure contains an C<SV*> pointer where a C<char*> pointer
74 is to be expected. (For information only--not to be used).
75
76 =head1 Handy Values
77
78 =for apidoc AmU||Nullhv
79 Null HV pointer.
80
81 =head1 Hash Manipulation Functions
82
83 =for apidoc Am|char*|HvNAME|HV* stash
84 Returns the package name of a stash.  See C<SvSTASH>, C<CvSTASH>.
85
86 =for apidoc Am|void*|HeKEY|HE* he
87 Returns the actual pointer stored in the key slot of the hash entry. The
88 pointer may be either C<char*> or C<SV*>, depending on the value of
89 C<HeKLEN()>.  Can be assigned to.  The C<HePV()> or C<HeSVKEY()> macros are
90 usually preferable for finding the value of a key.
91
92 =for apidoc Am|STRLEN|HeKLEN|HE* he
93 If this is negative, and amounts to C<HEf_SVKEY>, it indicates the entry
94 holds an C<SV*> key.  Otherwise, holds the actual length of the key.  Can
95 be assigned to. The C<HePV()> macro is usually preferable for finding key
96 lengths.
97
98 =for apidoc Am|SV*|HeVAL|HE* he
99 Returns the value slot (type C<SV*>) stored in the hash entry.
100
101 =for apidoc Am|U32|HeHASH|HE* he
102 Returns the computed hash stored in the hash entry.
103
104 =for apidoc Am|char*|HePV|HE* he|STRLEN len
105 Returns the key slot of the hash entry as a C<char*> value, doing any
106 necessary dereferencing of possibly C<SV*> keys.  The length of the string
107 is placed in C<len> (this is a macro, so do I<not> use C<&len>).  If you do
108 not care about what the length of the key is, you may use the global
109 variable C<PL_na>, though this is rather less efficient than using a local
110 variable.  Remember though, that hash keys in perl are free to contain
111 embedded nulls, so using C<strlen()> or similar is not a good way to find
112 the length of hash keys. This is very similar to the C<SvPV()> macro
113 described elsewhere in this document.
114
115 =for apidoc Am|SV*|HeSVKEY|HE* he
116 Returns the key as an C<SV*>, or C<Nullsv> if the hash entry does not
117 contain an C<SV*> key.
118
119 =for apidoc Am|SV*|HeSVKEY_force|HE* he
120 Returns the key as an C<SV*>.  Will create and return a temporary mortal
121 C<SV*> if the hash entry contains only a C<char*> key.
122
123 =for apidoc Am|SV*|HeSVKEY_set|HE* he|SV* sv
124 Sets the key to a given C<SV*>, taking care to set the appropriate flags to
125 indicate the presence of an C<SV*> key, and returns the same
126 C<SV*>.
127
128 =cut
129 */
130
131 /* these hash entry flags ride on hent_klen (for use only in magic/tied HVs) */
132 #define HEf_SVKEY       -2      /* hent_key is an SV* */
133
134
135 #define Nullhv Null(HV*)
136 #define HvARRAY(hv)     (*(HE***)&((XPVHV*)  SvANY(hv))->xhv_array)
137 #define HvFILL(hv)      ((XPVHV*)  SvANY(hv))->xhv_fill
138 #define HvMAX(hv)       ((XPVHV*)  SvANY(hv))->xhv_max
139 #define HvRITER(hv)     ((XPVHV*)  SvANY(hv))->xhv_riter
140 #define HvEITER(hv)     ((XPVHV*)  SvANY(hv))->xhv_eiter
141 #define HvPMROOT(hv)    ((XPVHV*)  SvANY(hv))->xhv_pmroot
142 #define HvNAME(hv)      ((XPVHV*)  SvANY(hv))->xhv_name
143
144 /* the number of keys (including any placeholers) */
145 #define XHvTOTALKEYS(xhv)       ((xhv)->xhv_keys)
146
147 /* The number of placeholders in the enumerated-keys hash */
148 #define XHvPLACEHOLDERS(xhv)    ((xhv)->xhv_placeholders)
149
150 /* the number of keys that exist() (i.e. excluding placeholders) */
151 #define XHvUSEDKEYS(xhv)      (XHvTOTALKEYS(xhv) - (IV)XHvPLACEHOLDERS(xhv))
152
153 /*
154  * HvKEYS gets the number of keys that actually exist(), and is provided
155  * for backwards compatibility with old XS code. The core uses HvUSEDKEYS
156  * (keys, excluding placeholdes) and HvTOTALKEYS (including placeholders)
157  */
158 #define HvKEYS(hv)              XHvUSEDKEYS((XPVHV*)  SvANY(hv))
159 #define HvUSEDKEYS(hv)          XHvUSEDKEYS((XPVHV*)  SvANY(hv))
160 #define HvTOTALKEYS(hv)         XHvTOTALKEYS((XPVHV*)  SvANY(hv))
161 #define HvPLACEHOLDERS(hv)      XHvPLACEHOLDERS((XPVHV*)  SvANY(hv))
162
163 #define HvSHAREKEYS(hv)         (SvFLAGS(hv) & SVphv_SHAREKEYS)
164 #define HvSHAREKEYS_on(hv)      (SvFLAGS(hv) |= SVphv_SHAREKEYS)
165 #define HvSHAREKEYS_off(hv)     (SvFLAGS(hv) &= ~SVphv_SHAREKEYS)
166
167 /* This is an optimisation flag. It won't be set if all hash keys have a 0
168  * flag. Currently the only flags relate to utf8.
169  * Hence it won't be set if all keys are 8 bit only. It will be set if any key
170  * is utf8 (including 8 bit keys that were entered as utf8, and need upgrading
171  * when retrieved during iteration. It may still be set when there are no longer
172  * any utf8 keys.
173  */
174 #define HvHASKFLAGS(hv)         (SvFLAGS(hv) & SVphv_HASKFLAGS)
175 #define HvHASKFLAGS_on(hv)      (SvFLAGS(hv) |= SVphv_HASKFLAGS)
176 #define HvHASKFLAGS_off(hv)     (SvFLAGS(hv) &= ~SVphv_HASKFLAGS)
177
178 #define HvLAZYDEL(hv)           (SvFLAGS(hv) & SVphv_LAZYDEL)
179 #define HvLAZYDEL_on(hv)        (SvFLAGS(hv) |= SVphv_LAZYDEL)
180 #define HvLAZYDEL_off(hv)       (SvFLAGS(hv) &= ~SVphv_LAZYDEL)
181
182 /* Maybe amagical: */
183 /* #define HV_AMAGICmb(hv)      (SvFLAGS(hv) & (SVpgv_badAM | SVpgv_AM)) */
184
185 #define HV_AMAGIC(hv)        (SvFLAGS(hv) &   SVpgv_AM)
186 #define HV_AMAGIC_on(hv)     (SvFLAGS(hv) |=  SVpgv_AM)
187 #define HV_AMAGIC_off(hv)    (SvFLAGS(hv) &= ~SVpgv_AM)
188
189 /*
190 #define HV_AMAGICbad(hv)     (SvFLAGS(hv) & SVpgv_badAM)
191 #define HV_badAMAGIC_on(hv)  (SvFLAGS(hv) |= SVpgv_badAM)
192 #define HV_badAMAGIC_off(hv) (SvFLAGS(hv) &= ~SVpgv_badAM)
193 */
194
195 #define Nullhe Null(HE*)
196 #define HeNEXT(he)              (he)->hent_next
197 #define HeKEY_hek(he)           (he)->hent_hek
198 #define HeKEY(he)               HEK_KEY(HeKEY_hek(he))
199 #define HeKEY_sv(he)            (*(SV**)HeKEY(he))
200 #define HeKLEN(he)              HEK_LEN(HeKEY_hek(he))
201 #define HeKUTF8(he)  HEK_UTF8(HeKEY_hek(he))
202 #define HeKWASUTF8(he)  HEK_WASUTF8(HeKEY_hek(he))
203 #define HeKLEN_UTF8(he)  (HeKUTF8(he) ? -HeKLEN(he) : HeKLEN(he))
204 #define HeKFLAGS(he)  HEK_FLAGS(HeKEY_hek(he))
205 #define HeVAL(he)               (he)->hent_val
206 #define HeHASH(he)              HEK_HASH(HeKEY_hek(he))
207 #define HePV(he,lp)             ((HeKLEN(he) == HEf_SVKEY) ?            \
208                                  SvPV(HeKEY_sv(he),lp) :                \
209                                  (((lp = HeKLEN(he)) >= 0) ?            \
210                                   HeKEY(he) : Nullch))
211
212 #define HeSVKEY(he)             ((HeKEY(he) &&                          \
213                                   HeKLEN(he) == HEf_SVKEY) ?            \
214                                  HeKEY_sv(he) : Nullsv)
215
216 #define HeSVKEY_force(he)       (HeKEY(he) ?                            \
217                                  ((HeKLEN(he) == HEf_SVKEY) ?           \
218                                   HeKEY_sv(he) :                        \
219                                   sv_2mortal(newSVpvn(HeKEY(he),        \
220                                                      HeKLEN(he)))) :    \
221                                  &PL_sv_undef)
222 #define HeSVKEY_set(he,sv)      ((HeKLEN(he) = HEf_SVKEY), (HeKEY_sv(he) = sv))
223
224 #define Nullhek Null(HEK*)
225 #define HEK_BASESIZE            STRUCT_OFFSET(HEK, hek_key[0])
226 #define HEK_HASH(hek)           (hek)->hek_hash
227 #define HEK_LEN(hek)            (hek)->hek_len
228 #define HEK_KEY(hek)            (hek)->hek_key
229 #define HEK_FLAGS(hek)  (*((unsigned char *)(HEK_KEY(hek))+HEK_LEN(hek)+1))
230
231 #define HVhek_UTF8      0x01 /* Key is utf8 encoded. */
232 #define HVhek_WASUTF8   0x02 /* Key is bytes here, but was supplied as utf8. */
233 #define HVhek_FREEKEY   0x100 /* Internal flag to say key is malloc()ed.  */
234 #define HVhek_MASK      0xFF
235
236 #define HEK_UTF8(hek)           (HEK_FLAGS(hek) & HVhek_UTF8)
237 #define HEK_UTF8_on(hek)        (HEK_FLAGS(hek) |= HVhek_UTF8)
238 #define HEK_UTF8_off(hek)       (HEK_FLAGS(hek) &= ~HVhek_UTF8)
239 #define HEK_WASUTF8(hek)        (HEK_FLAGS(hek) & HVhek_WASUTF8)
240 #define HEK_WASUTF8_on(hek)     (HEK_FLAGS(hek) |= HVhek_WASUTF8)
241 #define HEK_WASUTF8_off(hek)    (HEK_FLAGS(hek) &= ~HVhek_WASUTF8)
242
243 /* calculate HV array allocation */
244 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
245 #  define PERL_HV_ARRAY_ALLOC_BYTES(size) ((size) * sizeof(HE*))
246 #else
247 #  define MALLOC_OVERHEAD 16
248 #  define PERL_HV_ARRAY_ALLOC_BYTES(size) \
249                         (((size) < 64)                                  \
250                          ? (size) * sizeof(HE*)                         \
251                          : (size) * sizeof(HE*) * 2 - MALLOC_OVERHEAD)
252 #endif
253
254 /* available as a function in hv.c */
255 #define Perl_sharepvn(sv, len, hash) HEK_KEY(share_hek(sv, len, hash))
256 #define sharepvn(sv, len, hash)      Perl_sharepvn(sv, len, hash)