This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
porting/args_assert.t: Needs to look in inline.h too
[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
4a9a56a7
DM
67S_SvREFCNT_dec_NN(pTHX_ SV *sv)
68{
69 U32 rc = SvREFCNT(sv);
70 if (rc > 1)
71 SvREFCNT(sv) = rc - 1;
72 else
73 Perl_sv_free2(aTHX_ sv, rc);
74}
75
76PERL_STATIC_INLINE void
541377b1
FC
77SvAMAGIC_on(SV *sv)
78{
79 assert(SvROK(sv));
80 if (SvOBJECT(SvRV(sv))) HvAMAGIC_on(SvSTASH(SvRV(sv)));
81}
82PERL_STATIC_INLINE void
83SvAMAGIC_off(SV *sv)
84{
85 if (SvROK(sv) && SvOBJECT(SvRV(sv)))
86 HvAMAGIC_off(SvSTASH(SvRV(sv)));
87}
88
89PERL_STATIC_INLINE U32
90S_SvPADTMP_on(SV *sv)
91{
92 assert(!(SvFLAGS(sv) & SVs_PADMY));
93 return SvFLAGS(sv) |= SVs_PADTMP;
94}
95PERL_STATIC_INLINE U32
96S_SvPADTMP_off(SV *sv)
97{
98 assert(!(SvFLAGS(sv) & SVs_PADMY));
99 return SvFLAGS(sv) &= ~SVs_PADTMP;
100}
101PERL_STATIC_INLINE U32
102S_SvPADSTALE_on(SV *sv)
103{
104 assert(SvFLAGS(sv) & SVs_PADMY);
105 return SvFLAGS(sv) |= SVs_PADSTALE;
106}
107PERL_STATIC_INLINE U32
108S_SvPADSTALE_off(SV *sv)
109{
110 assert(SvFLAGS(sv) & SVs_PADMY);
111 return SvFLAGS(sv) &= ~SVs_PADSTALE;
112}
4ddea69a
FC
113#ifdef PERL_CORE
114PERL_STATIC_INLINE STRLEN
6964422a 115S_sv_or_pv_pos_u2b(pTHX_ SV *sv, const char *pv, STRLEN pos, STRLEN *lenp)
4ddea69a
FC
116{
117 if (SvGAMAGIC(sv)) {
118 U8 *hopped = utf8_hop((U8 *)pv, pos);
119 if (lenp) *lenp = (STRLEN)(utf8_hop(hopped, *lenp) - hopped);
120 return (STRLEN)(hopped - (U8 *)pv);
121 }
122 return sv_pos_u2b_flags(sv,pos,lenp,SV_CONST_RETURN);
123}
124#endif
f019c49e
DD
125
126/* ------------------------------- handy.h ------------------------------- */
127
128/* saves machine code for a common noreturn idiom typically used in Newx*() */
b3ba608d 129#ifdef __clang__
ccde8531
DM
130#pragma clang diagnostic push
131#pragma clang diagnostic ignored "-Wunused-function"
b3ba608d 132#endif
f019c49e
DD
133static void
134S_croak_memory_wrap(void)
135{
136 Perl_croak_nocontext("%s",PL_memory_wrap);
137}
b3ba608d 138#ifdef __clang__
ccde8531 139#pragma clang diagnostic pop
b3ba608d 140#endif