This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #119351] update the test note for the changed B::CV::GV test
[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
c70927a6 17PERL_STATIC_INLINE SSize_t
be3a7a5d
KW
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
25fdce4a
FC
65/* ------------------------------- mg.h ------------------------------- */
66
67#if defined(PERL_CORE) || defined(PERL_EXT)
68/* assumes get-magic and stringification have already occurred */
69PERL_STATIC_INLINE STRLEN
70S_MgBYTEPOS(pTHX_ MAGIC *mg, SV *sv, const char *s, STRLEN len)
71{
72 assert(mg->mg_type == PERL_MAGIC_regex_global);
73 assert(mg->mg_len != -1);
74 if (mg->mg_flags & MGf_BYTES || !DO_UTF8(sv))
75 return (STRLEN)mg->mg_len;
76 else {
77 const STRLEN pos = (STRLEN)mg->mg_len;
78 /* Without this check, we may read past the end of the buffer: */
79 if (pos > sv_or_pv_len_utf8(sv, s, len)) return len+1;
80 return sv_or_pv_pos_u2b(sv, s, pos, NULL);
81 }
82}
83#endif
84
8d919b0a
FC
85/* ----------------------------- regexp.h ----------------------------- */
86
87PERL_STATIC_INLINE struct regexp *
88S_ReANY(const REGEXP * const re)
89{
90 assert(isREGEXP(re));
91 return re->sv_u.svu_rx;
92}
93
27669aa4
FC
94/* ------------------------------- sv.h ------------------------------- */
95
96PERL_STATIC_INLINE SV *
97S_SvREFCNT_inc(SV *sv)
98{
2439e033 99 if (LIKELY(sv != NULL))
27669aa4
FC
100 SvREFCNT(sv)++;
101 return sv;
102}
103PERL_STATIC_INLINE SV *
104S_SvREFCNT_inc_NN(SV *sv)
105{
106 SvREFCNT(sv)++;
107 return sv;
108}
109PERL_STATIC_INLINE void
110S_SvREFCNT_inc_void(SV *sv)
111{
2439e033 112 if (LIKELY(sv != NULL))
27669aa4
FC
113 SvREFCNT(sv)++;
114}
75e16a44
FC
115PERL_STATIC_INLINE void
116S_SvREFCNT_dec(pTHX_ SV *sv)
117{
2439e033 118 if (LIKELY(sv != NULL)) {
75a9bf96 119 U32 rc = SvREFCNT(sv);
79e2a32a 120 if (LIKELY(rc > 1))
75a9bf96
DM
121 SvREFCNT(sv) = rc - 1;
122 else
123 Perl_sv_free2(aTHX_ sv, rc);
75e16a44
FC
124 }
125}
541377b1
FC
126
127PERL_STATIC_INLINE void
4a9a56a7
DM
128S_SvREFCNT_dec_NN(pTHX_ SV *sv)
129{
130 U32 rc = SvREFCNT(sv);
79e2a32a 131 if (LIKELY(rc > 1))
4a9a56a7
DM
132 SvREFCNT(sv) = rc - 1;
133 else
134 Perl_sv_free2(aTHX_ sv, rc);
135}
136
137PERL_STATIC_INLINE void
541377b1
FC
138SvAMAGIC_on(SV *sv)
139{
140 assert(SvROK(sv));
141 if (SvOBJECT(SvRV(sv))) HvAMAGIC_on(SvSTASH(SvRV(sv)));
142}
143PERL_STATIC_INLINE void
144SvAMAGIC_off(SV *sv)
145{
146 if (SvROK(sv) && SvOBJECT(SvRV(sv)))
147 HvAMAGIC_off(SvSTASH(SvRV(sv)));
148}
149
150PERL_STATIC_INLINE U32
151S_SvPADTMP_on(SV *sv)
152{
153 assert(!(SvFLAGS(sv) & SVs_PADMY));
154 return SvFLAGS(sv) |= SVs_PADTMP;
155}
156PERL_STATIC_INLINE U32
157S_SvPADTMP_off(SV *sv)
158{
159 assert(!(SvFLAGS(sv) & SVs_PADMY));
160 return SvFLAGS(sv) &= ~SVs_PADTMP;
161}
162PERL_STATIC_INLINE U32
163S_SvPADSTALE_on(SV *sv)
164{
165 assert(SvFLAGS(sv) & SVs_PADMY);
166 return SvFLAGS(sv) |= SVs_PADSTALE;
167}
168PERL_STATIC_INLINE U32
169S_SvPADSTALE_off(SV *sv)
170{
171 assert(SvFLAGS(sv) & SVs_PADMY);
172 return SvFLAGS(sv) &= ~SVs_PADSTALE;
173}
25fdce4a 174#if defined(PERL_CORE) || defined (PERL_EXT)
4ddea69a 175PERL_STATIC_INLINE STRLEN
6964422a 176S_sv_or_pv_pos_u2b(pTHX_ SV *sv, const char *pv, STRLEN pos, STRLEN *lenp)
4ddea69a 177{
25fdce4a 178 PERL_ARGS_ASSERT_SV_OR_PV_POS_U2B;
4ddea69a
FC
179 if (SvGAMAGIC(sv)) {
180 U8 *hopped = utf8_hop((U8 *)pv, pos);
181 if (lenp) *lenp = (STRLEN)(utf8_hop(hopped, *lenp) - hopped);
182 return (STRLEN)(hopped - (U8 *)pv);
183 }
184 return sv_pos_u2b_flags(sv,pos,lenp,SV_CONST_RETURN);
185}
186#endif
f019c49e 187
d1decf2b
TC
188/* ------------------------------- handy.h ------------------------------- */
189
190/* saves machine code for a common noreturn idiom typically used in Newx*() */
191#ifdef __clang__
192#pragma clang diagnostic push
193#pragma clang diagnostic ignored "-Wunused-function"
194#endif
195static void
196S_croak_memory_wrap(void)
197{
198 Perl_croak_nocontext("%s",PL_memory_wrap);
199}
200#ifdef __clang__
201#pragma clang diagnostic pop
202#endif
203
a8a2ceaa
KW
204/* ------------------------------- utf8.h ------------------------------- */
205
206/* These exist only to replace the macros they formerly were so that their use
207 * can be deprecated */
208
209PERL_STATIC_INLINE bool
210S_isIDFIRST_lazy(pTHX_ const char* p)
211{
212 PERL_ARGS_ASSERT_ISIDFIRST_LAZY;
213
214 return isIDFIRST_lazy_if(p,1);
215}
216
217PERL_STATIC_INLINE bool
218S_isALNUM_lazy(pTHX_ const char* p)
219{
220 PERL_ARGS_ASSERT_ISALNUM_LAZY;
221
222 return isALNUM_lazy_if(p,1);
223}