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