This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
For changelogs suggest name it Changes and follow CPAN::Changes::Spec
[perl5.git] / inline.h
CommitLineData
25468daa
FC
1/* inline.h
2 *
3 * Copyright (C) 2012 by Larry Wall and others
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 * This file is a home for static inline functions that cannot go in other
9 * headers files, because they depend on proto.h (included after most other
10 * headers) or struct definitions.
11 *
12 * Each section names the header file that the functions "belong" to.
13 */
27669aa4 14
be3a7a5d
KW
15/* ------------------------------- av.h ------------------------------- */
16
17PERL_STATIC_INLINE I32
18S_av_top_index(pTHX_ AV *av)
19{
20 PERL_ARGS_ASSERT_AV_TOP_INDEX;
21 assert(SvTYPE(av) == SVt_PVAV);
22
23 return AvFILL(av);
24}
25
1afe1db1
FC
26/* ------------------------------- cv.h ------------------------------- */
27
28PERL_STATIC_INLINE I32 *
29S_CvDEPTHp(const CV * const sv)
30{
31 assert(SvTYPE(sv) == SVt_PVCV || SvTYPE(sv) == SVt_PVFM);
8de47657 32 return &((XPVCV*)SvANY(sv))->xcv_depth;
1afe1db1
FC
33}
34
d16269d8
PM
35/*
36 CvPROTO returns the prototype as stored, which is not necessarily what
37 the interpreter should be using. Specifically, the interpreter assumes
38 that spaces have been stripped, which has been the case if the prototype
39 was added by toke.c, but is generally not the case if it was added elsewhere.
40 Since we can't enforce the spacelessness at assignment time, this routine
41 provides a temporary copy at parse time with spaces removed.
42 I<orig> is the start of the original buffer, I<len> is the length of the
43 prototype and will be updated when this returns.
44 */
45
5b67adb8 46#ifdef PERL_CORE
d16269d8
PM
47PERL_STATIC_INLINE char *
48S_strip_spaces(pTHX_ const char * orig, STRLEN * const len)
49{
50 SV * tmpsv;
51 char * tmps;
52 tmpsv = newSVpvn_flags(orig, *len, SVs_TEMP);
53 tmps = SvPVX(tmpsv);
54 while ((*len)--) {
55 if (!isSPACE(*orig))
56 *tmps++ = *orig;
57 orig++;
58 }
59 *tmps = '\0';
60 *len = tmps - SvPVX(tmpsv);
61 return SvPVX(tmpsv);
62}
5b67adb8 63#endif
d16269d8 64
8d919b0a
FC
65/* ----------------------------- regexp.h ----------------------------- */
66
67PERL_STATIC_INLINE struct regexp *
68S_ReANY(const REGEXP * const re)
69{
70 assert(isREGEXP(re));
71 return re->sv_u.svu_rx;
72}
73
27669aa4
FC
74/* ------------------------------- sv.h ------------------------------- */
75
76PERL_STATIC_INLINE SV *
77S_SvREFCNT_inc(SV *sv)
78{
2439e033 79 if (LIKELY(sv != NULL))
27669aa4
FC
80 SvREFCNT(sv)++;
81 return sv;
82}
83PERL_STATIC_INLINE SV *
84S_SvREFCNT_inc_NN(SV *sv)
85{
86 SvREFCNT(sv)++;
87 return sv;
88}
89PERL_STATIC_INLINE void
90S_SvREFCNT_inc_void(SV *sv)
91{
2439e033 92 if (LIKELY(sv != NULL))
27669aa4
FC
93 SvREFCNT(sv)++;
94}
75e16a44
FC
95PERL_STATIC_INLINE void
96S_SvREFCNT_dec(pTHX_ SV *sv)
97{
2439e033 98 if (LIKELY(sv != NULL)) {
75a9bf96 99 U32 rc = SvREFCNT(sv);
79e2a32a 100 if (LIKELY(rc > 1))
75a9bf96
DM
101 SvREFCNT(sv) = rc - 1;
102 else
103 Perl_sv_free2(aTHX_ sv, rc);
75e16a44
FC
104 }
105}
541377b1
FC
106
107PERL_STATIC_INLINE void
4a9a56a7
DM
108S_SvREFCNT_dec_NN(pTHX_ SV *sv)
109{
110 U32 rc = SvREFCNT(sv);
79e2a32a 111 if (LIKELY(rc > 1))
4a9a56a7
DM
112 SvREFCNT(sv) = rc - 1;
113 else
114 Perl_sv_free2(aTHX_ sv, rc);
115}
116
117PERL_STATIC_INLINE void
541377b1
FC
118SvAMAGIC_on(SV *sv)
119{
120 assert(SvROK(sv));
121 if (SvOBJECT(SvRV(sv))) HvAMAGIC_on(SvSTASH(SvRV(sv)));
122}
123PERL_STATIC_INLINE void
124SvAMAGIC_off(SV *sv)
125{
126 if (SvROK(sv) && SvOBJECT(SvRV(sv)))
127 HvAMAGIC_off(SvSTASH(SvRV(sv)));
128}
129
130PERL_STATIC_INLINE U32
131S_SvPADTMP_on(SV *sv)
132{
133 assert(!(SvFLAGS(sv) & SVs_PADMY));
134 return SvFLAGS(sv) |= SVs_PADTMP;
135}
136PERL_STATIC_INLINE U32
137S_SvPADTMP_off(SV *sv)
138{
139 assert(!(SvFLAGS(sv) & SVs_PADMY));
140 return SvFLAGS(sv) &= ~SVs_PADTMP;
141}
142PERL_STATIC_INLINE U32
143S_SvPADSTALE_on(SV *sv)
144{
145 assert(SvFLAGS(sv) & SVs_PADMY);
146 return SvFLAGS(sv) |= SVs_PADSTALE;
147}
148PERL_STATIC_INLINE U32
149S_SvPADSTALE_off(SV *sv)
150{
151 assert(SvFLAGS(sv) & SVs_PADMY);
152 return SvFLAGS(sv) &= ~SVs_PADSTALE;
153}
4ddea69a
FC
154#ifdef PERL_CORE
155PERL_STATIC_INLINE STRLEN
6964422a 156S_sv_or_pv_pos_u2b(pTHX_ SV *sv, const char *pv, STRLEN pos, STRLEN *lenp)
4ddea69a
FC
157{
158 if (SvGAMAGIC(sv)) {
159 U8 *hopped = utf8_hop((U8 *)pv, pos);
160 if (lenp) *lenp = (STRLEN)(utf8_hop(hopped, *lenp) - hopped);
161 return (STRLEN)(hopped - (U8 *)pv);
162 }
163 return sv_pos_u2b_flags(sv,pos,lenp,SV_CONST_RETURN);
164}
165#endif
f019c49e 166
a8a2ceaa
KW
167/* ------------------------------- utf8.h ------------------------------- */
168
169/* These exist only to replace the macros they formerly were so that their use
170 * can be deprecated */
171
172PERL_STATIC_INLINE bool
173S_isIDFIRST_lazy(pTHX_ const char* p)
174{
175 PERL_ARGS_ASSERT_ISIDFIRST_LAZY;
176
177 return isIDFIRST_lazy_if(p,1);
178}
179
180PERL_STATIC_INLINE bool
181S_isALNUM_lazy(pTHX_ const char* p)
182{
183 PERL_ARGS_ASSERT_ISALNUM_LAZY;
184
185 return isALNUM_lazy_if(p,1);
186}