This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
make SvREFCNT_dec() more efficient
[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
1afe1db1
FC
15/* ------------------------------- cv.h ------------------------------- */
16
17PERL_STATIC_INLINE I32 *
18S_CvDEPTHp(const CV * const sv)
19{
20 assert(SvTYPE(sv) == SVt_PVCV || SvTYPE(sv) == SVt_PVFM);
8de47657 21 return &((XPVCV*)SvANY(sv))->xcv_depth;
1afe1db1
FC
22}
23
8d919b0a
FC
24/* ----------------------------- regexp.h ----------------------------- */
25
26PERL_STATIC_INLINE struct regexp *
27S_ReANY(const REGEXP * const re)
28{
29 assert(isREGEXP(re));
30 return re->sv_u.svu_rx;
31}
32
27669aa4
FC
33/* ------------------------------- sv.h ------------------------------- */
34
35PERL_STATIC_INLINE SV *
36S_SvREFCNT_inc(SV *sv)
37{
38 if (sv)
39 SvREFCNT(sv)++;
40 return sv;
41}
42PERL_STATIC_INLINE SV *
43S_SvREFCNT_inc_NN(SV *sv)
44{
45 SvREFCNT(sv)++;
46 return sv;
47}
48PERL_STATIC_INLINE void
49S_SvREFCNT_inc_void(SV *sv)
50{
51 if (sv)
52 SvREFCNT(sv)++;
53}
75e16a44
FC
54PERL_STATIC_INLINE void
55S_SvREFCNT_dec(pTHX_ SV *sv)
56{
57 if (sv) {
75a9bf96
DM
58 U32 rc = SvREFCNT(sv);
59 if (rc > 1)
60 SvREFCNT(sv) = rc - 1;
61 else
62 Perl_sv_free2(aTHX_ sv, rc);
75e16a44
FC
63 }
64}
541377b1
FC
65
66PERL_STATIC_INLINE void
67SvAMAGIC_on(SV *sv)
68{
69 assert(SvROK(sv));
70 if (SvOBJECT(SvRV(sv))) HvAMAGIC_on(SvSTASH(SvRV(sv)));
71}
72PERL_STATIC_INLINE void
73SvAMAGIC_off(SV *sv)
74{
75 if (SvROK(sv) && SvOBJECT(SvRV(sv)))
76 HvAMAGIC_off(SvSTASH(SvRV(sv)));
77}
78
79PERL_STATIC_INLINE U32
80S_SvPADTMP_on(SV *sv)
81{
82 assert(!(SvFLAGS(sv) & SVs_PADMY));
83 return SvFLAGS(sv) |= SVs_PADTMP;
84}
85PERL_STATIC_INLINE U32
86S_SvPADTMP_off(SV *sv)
87{
88 assert(!(SvFLAGS(sv) & SVs_PADMY));
89 return SvFLAGS(sv) &= ~SVs_PADTMP;
90}
91PERL_STATIC_INLINE U32
92S_SvPADSTALE_on(SV *sv)
93{
94 assert(SvFLAGS(sv) & SVs_PADMY);
95 return SvFLAGS(sv) |= SVs_PADSTALE;
96}
97PERL_STATIC_INLINE U32
98S_SvPADSTALE_off(SV *sv)
99{
100 assert(SvFLAGS(sv) & SVs_PADMY);
101 return SvFLAGS(sv) &= ~SVs_PADSTALE;
102}
4ddea69a
FC
103#ifdef PERL_CORE
104PERL_STATIC_INLINE STRLEN
6964422a 105S_sv_or_pv_pos_u2b(pTHX_ SV *sv, const char *pv, STRLEN pos, STRLEN *lenp)
4ddea69a
FC
106{
107 if (SvGAMAGIC(sv)) {
108 U8 *hopped = utf8_hop((U8 *)pv, pos);
109 if (lenp) *lenp = (STRLEN)(utf8_hop(hopped, *lenp) - hopped);
110 return (STRLEN)(hopped - (U8 *)pv);
111 }
112 return sv_pos_u2b_flags(sv,pos,lenp,SV_CONST_RETURN);
113}
114#endif
f019c49e
DD
115
116/* ------------------------------- handy.h ------------------------------- */
117
118/* saves machine code for a common noreturn idiom typically used in Newx*() */
119static void
120S_croak_memory_wrap(void)
121{
122 Perl_croak_nocontext("%s",PL_memory_wrap);
123}