This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
IO::getline(): use CALLRUNOPS
[perl5.git] / sv.h
CommitLineData
a0d0e21e 1/* sv.h
79072805 2 *
1129b882 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
83706693 4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by Larry Wall and others
79072805
LW
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
79072805
LW
9 */
10
a0d0e21e
LW
11#ifdef sv_flags
12#undef sv_flags /* Convex has this in <signal.h> for sigvec() */
13#endif
14
954c1994 15/*
3f620621 16=for apidoc_section $SV_flags
ccfc67b7 17
e17573c0 18=for apidoc Ay||svtype
75af9d73 19An enum of flags for Perl types. These are found in the file F<sv.h>
954c1994
GS
20in the C<svtype> enum. Test these flags with the C<SvTYPE> macro.
21
a5e62da0
FC
22The types are:
23
24 SVt_NULL
a5e62da0
FC
25 SVt_IV
26 SVt_NV
27 SVt_RV
28 SVt_PV
29 SVt_PVIV
30 SVt_PVNV
31 SVt_PVMG
e94d9b54 32 SVt_INVLIST
a5e62da0
FC
33 SVt_REGEXP
34 SVt_PVGV
35 SVt_PVLV
36 SVt_PVAV
37 SVt_PVHV
38 SVt_PVCV
39 SVt_PVFM
40 SVt_PVIO
24c33697 41 SVt_PVOBJ
a5e62da0
FC
42
43These are most easily explained from the bottom up.
44
24c33697 45C<SVt_PVOBJ> is for object instances of the new `use feature 'class'` kind.
796b6530
KW
46C<SVt_PVIO> is for I/O objects, C<SVt_PVFM> for formats, C<SVt_PVCV> for
47subroutines, C<SVt_PVHV> for hashes and C<SVt_PVAV> for arrays.
a5e62da0
FC
48
49All the others are scalar types, that is, things that can be bound to a
50C<$> variable. For these, the internal types are mostly orthogonal to
51types in the Perl language.
52
53Hence, checking C<< SvTYPE(sv) < SVt_PVAV >> is the best way to see whether
54something is a scalar.
55
796b6530
KW
56C<SVt_PVGV> represents a typeglob. If C<!SvFAKE(sv)>, then it is a real,
57incoercible typeglob. If C<SvFAKE(sv)>, then it is a scalar to which a
a5e62da0 58typeglob has been assigned. Assigning to it again will stop it from being
796b6530 59a typeglob. C<SVt_PVLV> represents a scalar that delegates to another scalar
a5e62da0
FC
60behind the scenes. It is used, e.g., for the return value of C<substr> and
61for tied hash and array elements. It can hold any scalar value, including
796b6530
KW
62a typeglob. C<SVt_REGEXP> is for regular
63expressions. C<SVt_INVLIST> is for Perl
e94d9b54 64core internal use only.
a5e62da0 65
796b6530 66C<SVt_PVMG> represents a "normal" scalar (not a typeglob, regular expression,
a5e62da0
FC
67or delegate). Since most scalars do not need all the internal fields of a
68PVMG, we save memory by allocating smaller structs when possible. All the
796b6530
KW
69other types are just simpler forms of C<SVt_PVMG>, with fewer internal fields.
70C<SVt_NULL> can only hold undef. C<SVt_IV> can hold undef, an integer, or a
71reference. (C<SVt_RV> is an alias for C<SVt_IV>, which exists for backward
1863a6c8
RL
72compatibility.) C<SVt_NV> can hold undef or a double. (In builds that support
73headless NVs, these could also hold a reference via a suitable offset, in the
74same way that SVt_IV does, but this is not currently supported and seems to
75be a rare use case.) C<SVt_PV> can hold C<undef>, a string, or a reference.
2ba5f4df
RL
76C<SVt_PVIV> is a superset of C<SVt_PV> and C<SVt_IV>. C<SVt_PVNV> is a
77superset of C<SVt_PV> and C<SVt_NV>. C<SVt_PVMG> can hold anything C<SVt_PVNV>
78can hold, but it may also be blessed or magical.
a5e62da0 79
78342678 80=for apidoc AmnU||SVt_NULL
a5e62da0 81Type flag for scalars. See L</svtype>.
954c1994 82
78342678 83=for apidoc AmnU||SVt_IV
a5e62da0 84Type flag for scalars. See L</svtype>.
954c1994 85
78342678 86=for apidoc AmnU||SVt_NV
a5e62da0
FC
87Type flag for scalars. See L</svtype>.
88
78342678 89=for apidoc AmnU||SVt_PV
a5e62da0
FC
90Type flag for scalars. See L</svtype>.
91
78342678 92=for apidoc AmnU||SVt_PVIV
a5e62da0
FC
93Type flag for scalars. See L</svtype>.
94
78342678 95=for apidoc AmnU||SVt_PVNV
a5e62da0 96Type flag for scalars. See L</svtype>.
954c1994 97
78342678 98=for apidoc AmnU||SVt_PVMG
a5e62da0
FC
99Type flag for scalars. See L</svtype>.
100
f85ae787
KW
101=for apidoc CmnU||SVt_INVLIST
102Type flag for scalars. See L<perlapi/svtype>.
e94d9b54 103
78342678 104=for apidoc AmnU||SVt_REGEXP
a5e62da0
FC
105Type flag for regular expressions. See L</svtype>.
106
78342678 107=for apidoc AmnU||SVt_PVGV
a5e62da0
FC
108Type flag for typeglobs. See L</svtype>.
109
78342678 110=for apidoc AmnU||SVt_PVLV
a5e62da0 111Type flag for scalars. See L</svtype>.
954c1994 112
78342678 113=for apidoc AmnU||SVt_PVAV
a5e62da0 114Type flag for arrays. See L</svtype>.
954c1994 115
78342678 116=for apidoc AmnU||SVt_PVHV
a5e62da0 117Type flag for hashes. See L</svtype>.
954c1994 118
78342678 119=for apidoc AmnU||SVt_PVCV
a5e62da0
FC
120Type flag for subroutines. See L</svtype>.
121
78342678 122=for apidoc AmnU||SVt_PVFM
a5e62da0
FC
123Type flag for formats. See L</svtype>.
124
78342678 125=for apidoc AmnU||SVt_PVIO
a5e62da0 126Type flag for I/O objects. See L</svtype>.
954c1994 127
24c33697
PE
128=for apidoc AmnUx||SVt_PVOBJ
129Type flag for object instances. See L</svtype>.
130
954c1994 131=cut
1078c6a2
KW
132
133 These are ordered so that the simpler types have a lower value; SvUPGRADE
134 doesn't allow you to upgrade from a higher numbered type to a lower numbered
135 one; also there is code that assumes that anything that has as a PV component
136 has a type numbered >= SVt_PV.
954c1994
GS
137*/
138
1078c6a2 139
79072805 140typedef enum {
1f4fbd3b
MS
141 SVt_NULL, /* 0 */
142 /* BIND was here, before INVLIST replaced it. */
143 SVt_IV, /* 1 */
144 SVt_NV, /* 2 */
145 /* RV was here, before it was merged with IV. */
146 SVt_PV, /* 3 */
147 SVt_INVLIST, /* 4, implemented as a PV */
148 SVt_PVIV, /* 5 */
149 SVt_PVNV, /* 6 */
150 SVt_PVMG, /* 7 */
151 SVt_REGEXP, /* 8 */
152 /* PVBM was here, before BIND replaced it. */
153 SVt_PVGV, /* 9 */
154 SVt_PVLV, /* 10 */
155 SVt_PVAV, /* 11 */
156 SVt_PVHV, /* 12 */
157 SVt_PVCV, /* 13 */
158 SVt_PVFM, /* 14 */
159 SVt_PVIO, /* 15 */
24c33697
PE
160 SVt_PVOBJ, /* 16 */
161 /* 17-31: Unused, though one should be reserved for a
90ba01ad
KW
162 * freed sv, if the other 3 bits below the flags ones
163 * get allocated */
1f4fbd3b 164 SVt_LAST /* keep last in enum. used to size arrays */
79072805
LW
165} svtype;
166
f1fb8741 167/* *** any alterations to the SV types above need to be reflected in
e94d9b54
KW
168 * SVt_MASK and the various PL_valid_types_* tables. As of this writing those
169 * tables are in perl.h. There are also two affected names tables in dump.c,
e22533f8 170 * one in B.xs, and 'bodies_by_type[]' in sv_inline.h.
f40abc99 171 *
90ba01ad
KW
172 * The bits that match 0xe0 are CURRENTLY UNUSED
173 * The bits above that are for flags, like SVf_IOK */
f1fb8741 174
90ba01ad 175#define SVt_MASK 0x1f /* smallest bitmask that covers all types */
f1fb8741 176
cecf5685 177#ifndef PERL_CORE
a5c7cb08 178/* Fast Boyer Moore tables are now stored in magic attached to PVMGs */
cecf5685 179# define SVt_PVBM SVt_PVMG
4df7f6af
NC
180/* Anything wanting to create a reference from clean should ensure that it has
181 a scalar of type SVt_IV now: */
182# define SVt_RV SVt_IV
cecf5685
NC
183#endif
184
caf0b9e5 185/* The array of arena roots for SV bodies is indexed by SvTYPE. SVt_NULL doesn't
94ee6ed7
NC
186 * use a body, so that arena root is re-used for HEs. SVt_IV also doesn't, so
187 * that arena root is used for HVs with struct xpvhv_aux. */
75acd14e 188
6a93a7e5 189#if defined(PERL_IN_HV_C) || defined(PERL_IN_XS_APITEST)
caf0b9e5 190# define HE_ARENA_ROOT_IX SVt_NULL
6a93a7e5 191#endif
94ee6ed7
NC
192#if defined(PERL_IN_HV_C) || defined(PERL_IN_SV_C)
193# define HVAUX_ARENA_ROOT_IX SVt_IV
194#endif
ff121dc6
DD
195#ifdef PERL_IN_SV_C
196# define SVt_FIRST SVt_NULL /* the type of SV that new_SV() in sv.c returns */
197#endif
43e6e717 198
232d1c15
NC
199#define PERL_ARENA_ROOTS_SIZE (SVt_LAST)
200
43e6e717
NC
201/* typedefs to eliminate some typing */
202typedef struct he HE;
203typedef struct hek HEK;
204
79072805
LW
205/* Using C's structural equivalence to help emulate C++ inheritance here... */
206
30153bd2
JC
207/* start with 2 sv-head building blocks */
208#define _SV_HEAD(ptrtype) \
209 ptrtype sv_any; /* pointer to body */ \
210 U32 sv_refcnt; /* how many references to us */ \
211 U32 sv_flags /* what we are */
212
5b306eef
DD
213#if NVSIZE <= IVSIZE
214# define _NV_BODYLESS_UNION NV svu_nv;
215#else
216# define _NV_BODYLESS_UNION
217#endif
218
30153bd2
JC
219#define _SV_HEAD_UNION \
220 union { \
1f4fbd3b
MS
221 char* svu_pv; /* pointer to malloced string */ \
222 IV svu_iv; \
223 UV svu_uv; \
224 _NV_BODYLESS_UNION \
225 SV* svu_rv; /* pointer to another SV */ \
226 SV** svu_array; \
227 HE** svu_hash; \
228 GP* svu_gp; \
229 PerlIO *svu_fp; \
bb3b7d7b
DD
230 } sv_u \
231 _SV_HEAD_DEBUG
30153bd2 232
bb3b7d7b
DD
233#ifdef DEBUG_LEAKING_SCALARS
234#define _SV_HEAD_DEBUG ;\
235 PERL_BITFIELD32 sv_debug_optype:9; /* the type of OP that allocated us */ \
236 PERL_BITFIELD32 sv_debug_inpad:1; /* was allocated in a pad for an OP */ \
237 PERL_BITFIELD32 sv_debug_line:16; /* the line where we were allocated */ \
238 UV sv_debug_serial; /* serial number of sv allocation */ \
239 char * sv_debug_file; /* the file where we were allocated */ \
240 SV * sv_debug_parent /* what we were cloned from (ithreads)*/
241#else
242#define _SV_HEAD_DEBUG
243#endif
30153bd2 244
5e045b90 245struct STRUCT_SV { /* struct sv { */
30153bd2
JC
246 _SV_HEAD(void*);
247 _SV_HEAD_UNION;
79072805
LW
248};
249
250struct gv {
30153bd2
JC
251 _SV_HEAD(XPVGV*); /* pointer to xpvgv body */
252 _SV_HEAD_UNION;
79072805
LW
253};
254
255struct cv {
30153bd2
JC
256 _SV_HEAD(XPVCV*); /* pointer to xpvcv body */
257 _SV_HEAD_UNION;
79072805
LW
258};
259
260struct av {
0f7b1ae0 261 _SV_HEAD(XPVAV*); /* pointer to xpvav body */
30153bd2 262 _SV_HEAD_UNION;
79072805
LW
263};
264
265struct hv {
30153bd2
JC
266 _SV_HEAD(XPVHV*); /* pointer to xpvhv body */
267 _SV_HEAD_UNION;
8990e307
LW
268};
269
270struct io {
30153bd2
JC
271 _SV_HEAD(XPVIO*); /* pointer to xpvio body */
272 _SV_HEAD_UNION;
79072805
LW
273};
274
d2f13c59
NC
275struct p5rx {
276 _SV_HEAD(struct regexp*); /* pointer to regexp body */
277 _SV_HEAD_UNION;
278};
279
47e6c6d9
N
280struct invlist {
281 _SV_HEAD(XINVLIST*); /* pointer to xpvinvlist body */
282 _SV_HEAD_UNION;
283};
284
24c33697
PE
285struct object {
286 _SV_HEAD(XPVOBJ*); /* pointer to xobject body */
287 _SV_HEAD_UNION;
288};
289
30153bd2 290#undef _SV_HEAD
0f7b1ae0 291#undef _SV_HEAD_UNION /* ensure no pollution */
30153bd2 292
954c1994 293/*
3f620621 294=for apidoc_section $SV
ccfc67b7 295
954c1994 296=for apidoc Am|U32|SvREFCNT|SV* sv
cb198164
YO
297Returns the value of the object's reference count. Exposed
298to perl code via Internals::SvREFCNT().
954c1994 299
1607e393
KW
300=for apidoc SvREFCNT_inc
301=for apidoc_item SvREFCNT_inc_NN
302=for apidoc_item |SV* |SvREFCNT_inc_simple|SV* sv
303=for apidoc_item |SV* |SvREFCNT_inc_simple_NN|SV* sv
52f0fcf7
KW
304=for apidoc_item |void|SvREFCNT_inc_simple_void|SV* sv
305=for apidoc_item |void|SvREFCNT_inc_simple_void_NN|SV* sv
1607e393
KW
306=for apidoc_item SvREFCNT_inc_void
307=for apidoc_item |void|SvREFCNT_inc_void_NN|SV* sv
954c1994 308
52f0fcf7
KW
309These all increment the reference count of the given SV.
310The ones without C<void> in their names return the SV.
b37c2d43 311
52f0fcf7
KW
312C<SvREFCNT_inc> is the base operation; the rest are optimizations if various
313input constraints are known to be true; hence, all can be replaced with
314C<SvREFCNT_inc>.
315
316C<SvREFCNT_inc_NN> can only be used if you know C<sv> is not C<NULL>. Since we
317don't have to check the NULLness, it's faster and smaller.
b37c2d43 318
52f0fcf7 319C<SvREFCNT_inc_void> can only be used if you don't need the
b37c2d43
AL
320return value. The macro doesn't need to return a meaningful value.
321
52f0fcf7
KW
322C<SvREFCNT_inc_void_NN> can only be used if you both don't need the return
323value, and you know that C<sv> is not C<NULL>. The macro doesn't need to
324return a meaningful value, or check for NULLness, so it's smaller and faster.
b37c2d43 325
52f0fcf7 326C<SvREFCNT_inc_simple> can only be used with expressions without side
4ea561bc 327effects. Since we don't have to store a temporary value, it's faster.
b37c2d43 328
52f0fcf7
KW
329C<SvREFCNT_inc_simple_NN> can only be used with expressions without side
330effects and you know C<sv> is not C<NULL>. Since we don't have to store a
331temporary value, nor check for NULLness, it's faster and smaller.
b37c2d43 332
52f0fcf7
KW
333C<SvREFCNT_inc_simple_void> can only be used with expressions without side
334effects and you don't need the return value.
b37c2d43 335
52f0fcf7
KW
336C<SvREFCNT_inc_simple_void_NN> can only be used with expressions without side
337effects, you don't need the return value, and you know C<sv> is not C<NULL>.
b37c2d43 338
6dd040ff
YO
339=for apidoc SvREFCNT_dec
340=for apidoc_item SvREFCNT_dec_set_NULL
341=for apidoc_item SvREFCNT_dec_ret_NULL
b29c03ae 342=for apidoc_item SvREFCNT_dec_NN
954c1994 343
b29c03ae
KW
344These decrement the reference count of the given SV.
345
346C<SvREFCNT_dec_NN> may only be used when C<sv> is known to not be C<NULL>.
4a9a56a7 347
6dd040ff
YO
348The function C<SvREFCNT_dec_ret_NULL()> is identical to the
349C<SvREFCNT_dec()> except it returns a NULL C<SV *>. It is used by
350C<SvREFCNT_dec_set_NULL()> which is a macro which will, when passed a
351non-NULL argument, decrement the reference count of its argument and
352then set it to NULL. You can replace code of the following form:
353
354 if (sv) {
355 SvREFCNT_dec_NN(sv);
356 sv = NULL;
357 }
358
359with
360
361 SvREFCNT_dec_set_NULL(sv);
362
954c1994 363=for apidoc Am|svtype|SvTYPE|SV* sv
fbe13c60 364Returns the type of the SV. See C<L</svtype>>.
954c1994
GS
365
366=for apidoc Am|void|SvUPGRADE|SV* sv|svtype type
367Used to upgrade an SV to a more complex form. Uses C<sv_upgrade> to
fbe13c60 368perform the upgrade if necessary. See C<L</svtype>>.
954c1994
GS
369
370=cut
371*/
372
463ee0b2 373#define SvANY(sv) (sv)->sv_any
79072805 374#define SvFLAGS(sv) (sv)->sv_flags
aeea060c 375#define SvREFCNT(sv) (sv)->sv_refcnt
a863c7d1 376
c9182d9c 377#define SvREFCNT_inc(sv) Perl_SvREFCNT_inc(MUTABLE_SV(sv))
27669aa4 378#define SvREFCNT_inc_simple(sv) SvREFCNT_inc(sv)
c9182d9c
KW
379#define SvREFCNT_inc_NN(sv) Perl_SvREFCNT_inc_NN(MUTABLE_SV(sv))
380#define SvREFCNT_inc_void(sv) Perl_SvREFCNT_inc_void(MUTABLE_SV(sv))
8990e307 381
b37c2d43 382/* These guys don't need the curly blocks */
fa50d782
KW
383#define SvREFCNT_inc_simple_void(sv) \
384 STMT_START { \
385 SV * sv_ = MUTABLE_SV(sv); \
386 if (sv_) \
387 SvREFCNT(sv_)++; \
388 } STMT_END
389
b1bc3f34
NC
390#define SvREFCNT_inc_simple_NN(sv) (++(SvREFCNT(sv)),MUTABLE_SV(sv))
391#define SvREFCNT_inc_void_NN(sv) (void)(++SvREFCNT(MUTABLE_SV(sv)))
392#define SvREFCNT_inc_simple_void_NN(sv) (void)(++SvREFCNT(MUTABLE_SV(sv)))
b37c2d43 393
c9182d9c 394#define SvREFCNT_dec(sv) Perl_SvREFCNT_dec(aTHX_ MUTABLE_SV(sv))
6dd040ff
YO
395#define SvREFCNT_dec_set_NULL(sv) \
396 STMT_START { \
397 sv = Perl_SvREFCNT_dec_ret_NULL(aTHX_ MUTABLE_SV(sv)); \
398 } STMT_END
c9182d9c 399#define SvREFCNT_dec_NN(sv) Perl_SvREFCNT_dec_NN(aTHX_ MUTABLE_SV(sv))
a863c7d1 400
8990e307 401#define SVTYPEMASK 0xff
1fef4616 402#define SvTYPE(sv) ((svtype)((sv)->sv_flags & SVTYPEMASK))
79072805 403
0565a181
NC
404/* Sadly there are some parts of the core that have pointers to already-freed
405 SV heads, and rely on being able to tell that they are now free. So mark
406 them all by using a consistent macro. */
7667e7f1 407#define SvIS_FREED(sv) UNLIKELY(((sv)->sv_flags == SVTYPEMASK))
0565a181 408
24e08842
DM
409/* this is defined in this peculiar way to avoid compiler warnings.
410 * See the <20121213131428.GD1842@iabyn.com> thread in p5p */
463ea229 411#define SvUPGRADE(sv, mt) \
24e08842 412 ((void)(SvTYPE(sv) >= (mt) || (sv_upgrade(sv, mt),1)))
79072805 413
3f7ef1d4
NC
414#define SVf_IOK 0x00000100 /* has valid public integer value */
415#define SVf_NOK 0x00000200 /* has valid public numeric value */
416#define SVf_POK 0x00000400 /* has valid public pointer value */
417#define SVf_ROK 0x00000800 /* has a valid reference pointer */
418
419#define SVp_IOK 0x00001000 /* has valid non-public integer value */
420#define SVp_NOK 0x00002000 /* has valid non-public numeric value */
421#define SVp_POK 0x00004000 /* has valid non-public pointer value */
9a70c74b 422#define SVp_SCREAM 0x00008000 /* currently unused on plain scalars */
2e5b91de
NC
423#define SVphv_CLONEABLE SVp_SCREAM /* PVHV (stashes) clone its objects */
424#define SVpgv_GP SVp_SCREAM /* GV has a valid GP */
1ccdb730 425#define SVprv_PCS_IMPORTED SVp_SCREAM /* RV is a proxy for a constant
1f4fbd3b
MS
426 subroutine in another package. Set the
427 GvIMPORTED_CV_on() if it needs to be
428 expanded to a real GV */
cc25b9e2
DM
429
430/* SVf_PROTECT is what SVf_READONLY should have been: i.e. modifying
431 * this SV is completely illegal. However, SVf_READONLY (via
432 * Internals::SvREADONLY()) has come to be seen as a flag that can be
433 * temporarily set and unset by the user to indicate e.g. whether a hash
434 * is "locked". Now, Hash::Util et al only set SVf_READONLY, while core
435 * sets both (SVf_READONLY|SVf_PROTECT) to indicate both to core and user
436 * code that this SV should not be messed with.
437 */
fd01b4b7 438#define SVf_PROTECT 0x00010000 /* very read-only */
c0683843 439#define SVs_PADTMP 0x00020000 /* in use as tmp */
c0683843 440#define SVs_PADSTALE 0x00040000 /* lexical has gone out of scope;
1f4fbd3b 441 only used when !PADTMP */
2c39754a 442#define SVs_TEMP 0x00080000 /* mortal (implies string is stealable) */
3f7ef1d4
NC
443#define SVs_OBJECT 0x00100000 /* is "blessed" */
444#define SVs_GMG 0x00200000 /* has magical get method */
445#define SVs_SMG 0x00400000 /* has magical set method */
446#define SVs_RMG 0x00800000 /* has random magical methods */
447
fea3622c 448#define SVf_FAKE 0x01000000 /* 0: glob is just a copy
1f4fbd3b
MS
449 1: SV head arena wasn't malloc()ed
450 2: For PVCV, whether CvUNIQUE(cv)
451 refers to an eval or once only
452 [CvEVAL(cv), CvSPECIAL(cv)]
0f94cb1f 453 3: HV: informally reserved by DAPM
df6b4bd5
DM
454 for vtables
455 4: Together with other flags (or
456 lack thereof) indicates a regex,
457 including PVLV-as-regex. See
458 isREGEXP().
459 */
922562d1
PE
460#define SVf_OOK 0x02000000 /* has valid offset value */
461#define SVphv_HasAUX SVf_OOK /* PVHV has an additional hv_aux struct */
3f7ef1d4 462#define SVf_BREAK 0x04000000 /* refcnt is artificially low - used by
1f4fbd3b
MS
463 SVs in final arena cleanup.
464 Set in S_regtry on PL_reg_curpm, so that
465 perl_destruct will skip it.
a5f48505
DM
466 Used for mark and sweep by OP_AASSIGN
467 */
3f7ef1d4 468#define SVf_READONLY 0x08000000 /* may not be modified */
8990e307 469
a0d0e21e 470
8990e307 471
5bc28da9 472
a623f893 473#define SVf_THINKFIRST (SVf_READONLY|SVf_PROTECT|SVf_ROK|SVf_FAKE \
1f4fbd3b 474 |SVs_RMG|SVf_IsCOW)
5bc28da9 475
a0d0e21e 476#define SVf_OK (SVf_IOK|SVf_NOK|SVf_POK|SVf_ROK| \
1f4fbd3b 477 SVp_IOK|SVp_NOK|SVp_POK|SVpgv_GP)
a0d0e21e 478
7a77f1af 479#define PRIVSHIFT 4 /* (SVp_?OK >> PRIVSHIFT) == SVf_?OK */
8990e307 480
a3815e44 481/* SVf_AMAGIC means that the stash *may* have overload methods. It's
d3d326f2
DM
482 * set each time a function is compiled into a stash, and is reset by the
483 * overload code when called for the first time and finds that there are
484 * no overload methods. Note that this used to be set on the object; but
485 * is now only set on stashes.
486 */
cd028baa 487#define SVf_AMAGIC 0x10000000 /* has magical overloaded methods */
45eaf8af 488#define SVf_IsCOW 0x10000000 /* copy on write (shared hash key if
1f4fbd3b 489 SvLEN == 0) */
70bc21b7 490
44b2d6d6
FC
491/* Ensure this value does not clash with the GV_ADD* flags in gv.h, or the
492 CV_CKPROTO_* flags in op.c, or the padadd_* flags in pad.h: */
cd028baa 493#define SVf_UTF8 0x20000000 /* SvPV is UTF-8 encoded
1f4fbd3b
MS
494 This is also set on RVs whose overloaded
495 stringification is UTF-8. This might
496 only happen as a side effect of SvPV() */
70bc21b7
DM
497/* PVHV */
498#define SVphv_SHAREKEYS 0x20000000 /* PVHV keys live on shared string table */
8990e307 499
2e9f07a2
NC
500/* PVAV could probably use 0x2000000 without conflict. I assume that PVFM can
501 be UTF-8 encoded, and PVCVs could well have UTF-8 prototypes. PVIOs haven't
502 been restructured, so sometimes get used as string buffers. */
503
70bc21b7
DM
504
505/* Some private flags. */
506
e1dcbbca 507
914bb574
PE
508/* scalar SVs with SVp_POK */
509#define SVppv_STATIC 0x40000000 /* PV is pointer to static const; must be set with SVf_IsCOW */
26a32e18 510/* PVAV */
3f7ef1d4 511#define SVpav_REAL 0x40000000 /* free old entries */
26a32e18 512/* PVHV */
3f7ef1d4 513#define SVphv_LAZYDEL 0x40000000 /* entry in xhv_eiter must be deleted */
25da4f38 514
26a32e18 515/* IV, PVIV, PVNV, PVMG, PVGV and (I assume) PVLV */
3f7ef1d4 516#define SVf_IVisUV 0x80000000 /* use XPVUV instead of XPVIV */
26a32e18 517/* PVAV */
3f7ef1d4 518#define SVpav_REIFY 0x80000000 /* can become real */
26a32e18 519/* PVHV */
3f7ef1d4 520#define SVphv_HASKFLAGS 0x80000000 /* keys have flag byte after hash */
26a32e18 521/* RV upwards. However, SVf_ROK and SVp_IOK are exclusive */
3f7ef1d4 522#define SVprv_WEAKREF 0x80000000 /* Weak reference */
d3ce79bd 523/* pad name vars only */
810b8aa5 524
6e128786
NC
525#define _XPV_HEAD \
526 HV* xmg_stash; /* class package */ \
527 union _xmgu xmg_u; \
8de47657 528 STRLEN xpv_cur; /* length of svu_pv as a C string */ \
8d919b0a 529 union { \
1f4fbd3b 530 STRLEN xpvlenu_len; /* allocated size */ \
df6b4bd5 531 struct regexp* xpvlenu_rx; /* regex when SV body is XPVLV */ \
1f4fbd3b 532 } xpv_len_u
8d919b0a
FC
533
534#define xpv_len xpv_len_u.xpvlenu_len
bb02a38f 535
20f4945e
MHM
536union _xnvu {
537 NV xnv_nv; /* numeric value, if any */
538 HV * xgv_stash;
9420b268 539 line_t xnv_lines; /* used internally by S_scan_subst() */
b4204fb6 540 bool xnv_bm_tail; /* an SvVALID (BM) SV has an implicit "\n" */
20f4945e
MHM
541};
542
543union _xivu {
544 IV xivu_iv; /* integer value */
20f4945e 545 UV xivu_uv;
20f4945e 546 HEK * xivu_namehek; /* xpvlv, xpvgv: GvNAME */
6432a58a 547 bool xivu_eval_seen; /* used internally by S_scan_subst() */
b4204fb6 548
20f4945e
MHM
549};
550
551union _xmgu {
552 MAGIC* xmg_magic; /* linked list of magicalness */
5bec93be 553 STRLEN xmg_hash_index; /* used while freeing hash entries */
9b7476d7 554};
11ca45c0 555
79072805 556struct xpv {
20f4945e 557 _XPV_HEAD;
79072805
LW
558};
559
560struct xpviv {
20f4945e
MHM
561 _XPV_HEAD;
562 union _xivu xiv_u;
79072805
LW
563};
564
311a25d9
NC
565#define xiv_iv xiv_u.xivu_iv
566
ff68c719 567struct xpvuv {
20f4945e
MHM
568 _XPV_HEAD;
569 union _xivu xuv_u;
ff68c719 570};
571
20f4945e 572#define xuv_uv xuv_u.xivu_uv
311a25d9 573
79072805 574struct xpvnv {
20f4945e
MHM
575 _XPV_HEAD;
576 union _xivu xiv_u;
6e128786 577 union _xnvu xnv_u;
79072805
LW
578};
579
3cd3be43 580/* This structure must match the beginning of struct xpvhv in hv.h. */
79072805 581struct xpvmg {
20f4945e 582 _XPV_HEAD;
6e128786
NC
583 union _xivu xiv_u;
584 union _xnvu xnv_u;
79072805
LW
585};
586
587struct xpvlv {
20f4945e 588 _XPV_HEAD;
6e128786
NC
589 union _xivu xiv_u;
590 union _xnvu xnv_u;
bbfdc870 591 union {
1f4fbd3b
MS
592 STRLEN xlvu_targoff;
593 SSize_t xlvu_stargoff;
bbfdc870 594 } xlv_targoff_u;
79072805
LW
595 STRLEN xlv_targlen;
596 SV* xlv_targ;
dd28f7bb 597 char xlv_type; /* k=keys .=pos x=substr v=vec /=join/re
1f4fbd3b 598 * y=alem/helem/iter t=tie T=tied HE */
1b92e694
DM
599 char xlv_flags; /* 1 = negative offset 2 = negative len
600 4 = out of range (vec) */
79072805
LW
601};
602
bbfdc870
FC
603#define xlv_targoff xlv_targoff_u.xlvu_targoff
604
d361b004
KW
605struct xpvinvlist {
606 _XPV_HEAD;
da62e549
KW
607 IV prev_index; /* caches result of previous invlist_search() */
608 STRLEN iterator; /* Stores where we are in iterating */
609 bool is_offset; /* The data structure for all inversion lists
610 begins with an element for code point U+0000.
611 If this bool is set, the actual list contains
612 that 0; otherwise, the list actually begins
613 with the following element. Thus to invert
614 the list, merely toggle this flag */
d361b004
KW
615};
616
a5c7cb08
DM
617/* This structure works in 2 ways - regular scalar, or GV with GP */
618
79072805 619struct xpvgv {
20f4945e 620 _XPV_HEAD;
6e128786
NC
621 union _xivu xiv_u;
622 union _xnvu xnv_u;
79072805
LW
623};
624
51c78f1b 625typedef U32 cv_flags_t;
77a005ab 626
20f4945e
MHM
627#define _XPVCV_COMMON \
628 HV * xcv_stash; \
629 union { \
1f4fbd3b
MS
630 OP * xcv_start; \
631 ANY xcv_xsubany; \
20f4945e
MHM
632 } xcv_start_u; \
633 union { \
1f4fbd3b
MS
634 OP * xcv_root; \
635 void (*xcv_xsub) (pTHX_ CV*); \
20f4945e 636 } xcv_root_u; \
b290562e 637 union { \
1f4fbd3b
MS
638 GV * xcv_gv; \
639 HEK * xcv_hek; \
b290562e 640 } xcv_gv_u; \
20f4945e 641 char * xcv_file; \
eacbb379 642 union { \
1f4fbd3b
MS
643 PADLIST * xcv_padlist; \
644 void * xcv_hscxt; \
eacbb379 645 } xcv_padlist_u; \
20f4945e
MHM
646 CV * xcv_outside; \
647 U32 xcv_outside_seq; /* the COP sequence (at the point of our \
1f4fbd3b
MS
648 * compilation) in the lexically enclosing \
649 * sub */ \
8de47657
FC
650 cv_flags_t xcv_flags; \
651 I32 xcv_depth /* >= 2 indicates recursive call */
8990e307 652
0d856a3a
FC
653/* This structure must match XPVCV in cv.h */
654
20f4945e
MHM
655struct xpvfm {
656 _XPV_HEAD;
20f4945e 657 _XPVCV_COMMON;
79072805
LW
658};
659
167f2c4d 660
8990e307 661struct xpvio {
20f4945e 662 _XPV_HEAD;
6e128786 663 union _xivu xiv_u;
6f7e8353
NC
664 /* ifp and ofp are normally the same, but sockets need separate streams */
665 PerlIO * xio_ofp;
c951e2ad
NC
666 /* Cray addresses everything by word boundaries (64 bits) and
667 * code and data pointers cannot be mixed (which is exactly what
668 * Perl_filter_add() tries to do with the dirp), hence the
669 * following union trick (as suggested by Gurusamy Sarathy).
670 * For further information see Geir Johansen's problem report
4fe48737 671 * titled [ID 20000612.002 (#3366)] Perl problem on Cray system
c951e2ad
NC
672 * The any pointer (known as IoANY()) will also be a good place
673 * to hang any IO disciplines to.
674 */
675 union {
1f4fbd3b
MS
676 DIR * xiou_dirp; /* for opendir, readdir, etc */
677 void * xiou_any; /* for alignment */
c951e2ad
NC
678 } xio_dirpu;
679 /* IV xio_lines is now in IVX $. */
680 IV xio_page; /* $% */
681 IV xio_page_len; /* $= */
682 IV xio_lines_left; /* $- */
683 char * xio_top_name; /* $^ */
684 GV * xio_top_gv; /* $^ */
685 char * xio_fmt_name; /* $~ */
686 GV * xio_fmt_gv; /* $~ */
687 char * xio_bottom_name;/* $^B */
688 GV * xio_bottom_gv; /* $^B */
689 char xio_type;
690 U8 xio_flags;
8990e307 691};
167f2c4d 692
4755096e
GS
693#define xio_dirp xio_dirpu.xiou_dirp
694#define xio_any xio_dirpu.xiou_any
8990e307 695
e0c19803
GS
696#define IOf_ARGV 1 /* this fp iterates over ARGV */
697#define IOf_START 2 /* check for null ARGV and substitute '-' */
698#define IOf_FLUSH 4 /* this fp wants a flush after write op */
699#define IOf_DIDTOP 8 /* just did top of form */
700#define IOf_UNTAINT 16 /* consider this fp (and its data) "safe" */
701#define IOf_NOLINE 32 /* slurped a pseudo-line from empty file */
5bb89d25 702#define IOf_FAKE_DIRP 64 /* xio_dirp is fake (source filters kludge)
1f4fbd3b 703 Also, when this is set, SvPVX() is valid */
8990e307 704
24c33697
PE
705struct xobject {
706 HV* xmg_stash;
707 union _xmgu xmg_u;
708 SSize_t xobject_maxfield;
709 SSize_t xobject_iter_sv_at; /* this is only used by Perl_sv_clear() */
710 SV** xobject_fields;
711};
712
713#define ObjectMAXFIELD(inst) ((XPVOBJ *)SvANY(inst))->xobject_maxfield
714#define ObjectITERSVAT(inst) ((XPVOBJ *)SvANY(inst))->xobject_iter_sv_at
715#define ObjectFIELDS(inst) ((XPVOBJ *)SvANY(inst))->xobject_fields
716
ed6116ce
LW
717/* The following macros define implementation-independent predicates on SVs. */
718
954c1994 719/*
fbf9f983
RGS
720=for apidoc Am|U32|SvNIOK|SV* sv
721Returns a U32 value indicating whether the SV contains a number, integer or
954c1994
GS
722double.
723
fbf9f983
RGS
724=for apidoc Am|U32|SvNIOKp|SV* sv
725Returns a U32 value indicating whether the SV contains a number, integer or
fe749c9a 726double. Checks the B<private> setting. Use C<SvNIOK> instead.
954c1994
GS
727
728=for apidoc Am|void|SvNIOK_off|SV* sv
729Unsets the NV/IV status of an SV.
730
fbf9f983 731=for apidoc Am|U32|SvOK|SV* sv
72d33970 732Returns a U32 value indicating whether the value is defined. This is
c0b6140e 733only meaningful for scalars.
954c1994 734
fbf9f983
RGS
735=for apidoc Am|U32|SvIOKp|SV* sv
736Returns a U32 value indicating whether the SV contains an integer. Checks
fe749c9a 737the B<private> setting. Use C<SvIOK> instead.
954c1994 738
fbf9f983
RGS
739=for apidoc Am|U32|SvNOKp|SV* sv
740Returns a U32 value indicating whether the SV contains a double. Checks the
fe749c9a 741B<private> setting. Use C<SvNOK> instead.
954c1994 742
fbf9f983
RGS
743=for apidoc Am|U32|SvPOKp|SV* sv
744Returns a U32 value indicating whether the SV contains a character string.
fe749c9a 745Checks the B<private> setting. Use C<SvPOK> instead.
954c1994 746
fbf9f983
RGS
747=for apidoc Am|U32|SvIOK|SV* sv
748Returns a U32 value indicating whether the SV contains an integer.
954c1994
GS
749
750=for apidoc Am|void|SvIOK_on|SV* sv
751Tells an SV that it is an integer.
752
753=for apidoc Am|void|SvIOK_off|SV* sv
754Unsets the IV status of an SV.
755
756=for apidoc Am|void|SvIOK_only|SV* sv
796b6530 757Tells an SV that it is an integer and disables all other C<OK> bits.
954c1994 758
e331fc52 759=for apidoc Am|void|SvIOK_only_UV|SV* sv
796b6530 760Tells an SV that it is an unsigned integer and disables all other C<OK> bits.
e331fc52 761
12fa07df 762=for apidoc Am|bool|SvIOK_UV|SV* sv
a6ceea06
KW
763Returns a boolean indicating whether the SV contains an integer that must be
764interpreted as unsigned. A non-negative integer whose value is within the
a3815e44 765range of both an IV and a UV may be flagged as either C<SvUOK> or C<SvIOK>.
e331fc52 766
fbf9f983 767=for apidoc Am|bool|SvUOK|SV* sv
a6ceea06
KW
768Returns a boolean indicating whether the SV contains an integer that must be
769interpreted as unsigned. A non-negative integer whose value is within the
a3815e44 770range of both an IV and a UV may be flagged as either C<SvUOK> or C<SvIOK>.
28e5dec8 771
12fa07df 772=for apidoc Am|bool|SvIOK_notUV|SV* sv
d1be9408 773Returns a boolean indicating whether the SV contains a signed integer.
e331fc52 774
fbf9f983
RGS
775=for apidoc Am|U32|SvNOK|SV* sv
776Returns a U32 value indicating whether the SV contains a double.
954c1994
GS
777
778=for apidoc Am|void|SvNOK_on|SV* sv
779Tells an SV that it is a double.
780
781=for apidoc Am|void|SvNOK_off|SV* sv
782Unsets the NV status of an SV.
783
784=for apidoc Am|void|SvNOK_only|SV* sv
785Tells an SV that it is a double and disables all other OK bits.
786
fbf9f983
RGS
787=for apidoc Am|U32|SvPOK|SV* sv
788Returns a U32 value indicating whether the SV contains a character
954c1994
GS
789string.
790
791=for apidoc Am|void|SvPOK_on|SV* sv
792Tells an SV that it is a string.
793
794=for apidoc Am|void|SvPOK_off|SV* sv
795Unsets the PV status of an SV.
796
797=for apidoc Am|void|SvPOK_only|SV* sv
796b6530 798Tells an SV that it is a string and disables all other C<OK> bits.
1e54db1a 799Will also turn off the UTF-8 status.
954c1994 800
fff0089c
YO
801=for apidoc Am|U32|SvBoolFlagsOK|SV* sv
802Returns a bool indicating whether the SV has the right flags set such
803that it is safe to call C<BOOL_INTERNALS_sv_isbool()> or
804C<BOOL_INTERNALS_sv_isbool_true()> or
805C<BOOL_INTERNALS_sv_isbool_false()>. Currently equivalent to
806C<SvIandPOK(sv)> or C<SvIOK(sv) && SvPOK(sv)>. Serialization may want to
807unroll this check. If so you are strongly recommended to add code like
808C<assert(SvBoolFlagsOK(sv));> B<before> calling using any of the
809BOOL_INTERNALS macros.
810
811=for apidoc Am|U32|SvIandPOK|SV* sv
812Returns a bool indicating whether the SV is both C<SvPOK()> and
813C<SvIOK()> at the same time. Equivalent to C<SvIOK(sv) && SvPOK(sv)> but
814more efficient.
815
816=for apidoc Am|void|SvIandPOK_on|SV* sv
817Tells an SV that is a string and a number in one operation. Equivalent
818to C<SvIOK_on(sv); SvPOK_on(sv);> but more efficient.
819
820=for apidoc Am|void|SvIandPOK_off|SV* sv
821Unsets the PV and IV status of an SV in one operation. Equivalent to
822C<SvIOK_off(sv); SvPK_off(v);> but more efficient.
823
824=for apidoc Am|bool|BOOL_INTERNALS_sv_isbool|SV* sv
825Checks if a C<SvBoolFlagsOK()> sv is a bool. B<Note> that it is the
826caller's responsibility to ensure that the sv is C<SvBoolFlagsOK()> before
827calling this. This is only useful in specialized logic like
828serialization code where performance is critical and the flags have
829already been checked to be correct. Almost always you should be using
830C<sv_isbool(sv)> instead.
831
832=for apidoc Am|bool|BOOL_INTERNALS_sv_isbool_true|SV* sv
833Checks if a C<SvBoolFlagsOK()> sv is a true bool. B<Note> that it is
834the caller's responsibility to ensure that the sv is C<SvBoolFlagsOK()>
835before calling this. This is only useful in specialized logic like
836serialization code where performance is critical and the flags have
837already been checked to be correct. This is B<NOT> what you should use
838to check if an SV is "true", for that you should be using
839C<SvTRUE(sv)> instead.
840
841=for apidoc Am|bool|BOOL_INTERNALS_sv_isbool_false|SV* sv
842Checks if a C<SvBoolFlagsOK()> sv is a false bool. B<Note> that it is
843the caller's responsibility to ensure that the sv is C<SvBoolFlagsOK()>
844before calling this. This is only useful in specialized logic like
845serialization code where performance is critical and the flags have
846already been checked to be correct. This is B<NOT> what you should use
847to check if an SV is "false", for that you should be using
848C<!SvTRUE(sv)> instead.
849
b0f01acb
JP
850=for apidoc Am|bool|SvVOK|SV* sv
851Returns a boolean indicating whether the SV contains a v-string.
852
fbf9f983 853=for apidoc Am|U32|SvOOK|SV* sv
69240efd
NC
854Returns a U32 indicating whether the pointer to the string buffer is offset.
855This hack is used internally to speed up removal of characters from the
de343f44 856beginning of a C<L</SvPV>>. When C<SvOOK> is true, then the start of the
796b6530
KW
857allocated string buffer is actually C<SvOOK_offset()> bytes before C<SvPVX>.
858This offset used to be stored in C<SvIVX>, but is now stored within the spare
69240efd 859part of the buffer.
954c1994 860
fbf9f983 861=for apidoc Am|U32|SvROK|SV* sv
954c1994
GS
862Tests if the SV is an RV.
863
864=for apidoc Am|void|SvROK_on|SV* sv
865Tells an SV that it is an RV.
866
867=for apidoc Am|void|SvROK_off|SV* sv
868Unsets the RV status of an SV.
869
870=for apidoc Am|SV*|SvRV|SV* sv
871Dereferences an RV to return the SV.
872
873=for apidoc Am|IV|SvIVX|SV* sv
645c22ef 874Returns the raw value in the SV's IV slot, without checks or conversions.
796b6530 875Only use when you are sure C<SvIOK> is true. See also C<L</SvIV>>.
954c1994
GS
876
877=for apidoc Am|UV|SvUVX|SV* sv
645c22ef 878Returns the raw value in the SV's UV slot, without checks or conversions.
796b6530 879Only use when you are sure C<SvIOK> is true. See also C<L</SvUV>>.
954c1994 880
b7822a6d
KW
881=for apidoc AmD|UV|SvUVXx|SV* sv
882This is an unnecessary synonym for L</SvUVX>
883
954c1994 884=for apidoc Am|NV|SvNVX|SV* sv
645c22ef 885Returns the raw value in the SV's NV slot, without checks or conversions.
796b6530 886Only use when you are sure C<SvNOK> is true. See also C<L</SvNV>>.
954c1994 887
1607e393 888=for apidoc Am |char* |SvPVX|SV* sv
c2b527b3 889=for apidoc_item |const char*|SvPVX_const|SV* sv
1607e393
KW
890=for apidoc_item |char* |SvPVX_mutable|SV* sv
891=for apidoc_item |char* |SvPVXx|SV* sv
c2b527b3
KW
892
893These return a pointer to the physical string in the SV. The SV must contain a
894string. Prior to 5.9.3 it is not safe to execute these unless the SV's
796b6530 895type >= C<SVt_PV>.
954c1994 896
c2b527b3 897These are also used to store the name of an autoloaded subroutine in an XS
5b36e945 898AUTOLOAD routine. See L<perlguts/Autoloading with XSUBs>.
89fc9e2a 899
c2b527b3
KW
900C<SvPVXx> is identical to C<SvPVX>.
901
902C<SvPVX_mutable> is merely a synonym for C<SvPVX>, but its name emphasizes that
903the string is modifiable by the caller.
904
905C<SvPVX_const> differs in that the return value has been cast so that the
906compiler will complain if you were to try to modify the contents of the string,
907(unless you cast away const yourself).
908
954c1994 909=for apidoc Am|STRLEN|SvCUR|SV* sv
3c3f883d
FG
910Returns the length, in bytes, of the PV inside the SV.
911Note that this may not match Perl's C<length>; for that, use
912C<sv_len_utf8(sv)>. See C<L</SvLEN>> also.
954c1994
GS
913
914=for apidoc Am|STRLEN|SvLEN|SV* sv
659239a4 915Returns the size of the string buffer in the SV, not including any part
fbe13c60 916attributable to C<SvOOK>. See C<L</SvCUR>>.
954c1994
GS
917
918=for apidoc Am|char*|SvEND|SV* sv
f990ea3e
FC
919Returns a pointer to the spot just after the last character in
920the string which is in the SV, where there is usually a trailing
417b32ce 921C<NUL> character (even though Perl scalars do not strictly require it).
796b6530 922See C<L</SvCUR>>. Access the character as C<*(SvEND(sv))>.
954c1994 923
f990ea3e
FC
924Warning: If C<SvCUR> is equal to C<SvLEN>, then C<SvEND> points to
925unallocated memory.
926
954c1994
GS
927=for apidoc Am|HV*|SvSTASH|SV* sv
928Returns the stash of the SV.
929
672994ce 930=for apidoc Am|void|SvIV_set|SV* sv|IV val
20799e15
SP
931Set the value of the IV pointer in sv to val. It is possible to perform
932the same function of this macro with an lvalue assignment to C<SvIVX>.
1f4fbd3b 933With future Perls, however, it will be more efficient to use
20799e15 934C<SvIV_set> instead of the lvalue assignment to C<SvIVX>.
672994ce
SP
935
936=for apidoc Am|void|SvNV_set|SV* sv|NV val
796b6530 937Set the value of the NV pointer in C<sv> to val. See C<L</SvIV_set>>.
672994ce
SP
938
939=for apidoc Am|void|SvPV_set|SV* sv|char* val
f2979eac
DD
940This is probably not what you want to use, you probably wanted
941L</sv_usepvn_flags> or L</sv_setpvn> or L</sv_setpvs>.
235fc044 942
f2979eac 943Set the value of the PV pointer in C<sv> to the Perl allocated
fbe13c60 944C<NUL>-terminated string C<val>. See also C<L</SvIV_set>>.
f2979eac
DD
945
946Remember to free the previous PV buffer. There are many things to check.
235fc044
FC
947Beware that the existing pointer may be involved in copy-on-write or other
948mischief, so do C<SvOOK_off(sv)> and use C<sv_force_normal> or
796b6530 949C<SvPV_force> (or check the C<SvIsCOW> flag) first to make sure this
d54acd4f
KW
950modification is safe. Then finally, if it is not a COW, call
951C<L</SvPV_free>> to free the previous PV buffer.
672994ce
SP
952
953=for apidoc Am|void|SvUV_set|SV* sv|UV val
796b6530 954Set the value of the UV pointer in C<sv> to val. See C<L</SvIV_set>>.
672994ce
SP
955
956=for apidoc Am|void|SvRV_set|SV* sv|SV* val
796b6530 957Set the value of the RV pointer in C<sv> to val. See C<L</SvIV_set>>.
672994ce
SP
958
959=for apidoc Am|void|SvMAGIC_set|SV* sv|MAGIC* val
796b6530 960Set the value of the MAGIC pointer in C<sv> to val. See C<L</SvIV_set>>.
672994ce 961
b6f4de24 962=for apidoc Am|void|SvSTASH_set|SV* sv|HV* val
796b6530 963Set the value of the STASH pointer in C<sv> to val. See C<L</SvIV_set>>.
672994ce 964
954c1994 965=for apidoc Am|void|SvCUR_set|SV* sv|STRLEN len
3c3f883d
FG
966Sets the current length, in bytes, of the C string which is in the SV.
967See C<L</SvCUR>> and C<SvIV_set>>.
672994ce
SP
968
969=for apidoc Am|void|SvLEN_set|SV* sv|STRLEN len
0ff57aee 970Set the size of the string buffer for the SV. See C<L</SvLEN>>.
954c1994
GS
971
972=cut
973*/
974
79072805 975#define SvNIOK(sv) (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
748a9306 976#define SvNIOKp(sv) (SvFLAGS(sv) & (SVp_IOK|SVp_NOK))
a0d0e21e 977#define SvNIOK_off(sv) (SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK| \
1f4fbd3b 978 SVp_IOK|SVp_NOK|SVf_IVisUV))
79072805 979
c7724415
FC
980#define assert_not_ROK(sv) assert_(!SvROK(sv) || !SvRV(sv))
981#define assert_not_glob(sv) assert_(!isGV_with_GP(sv))
906ed6d6 982
df6b4bd5 983#define SvOK(sv) (SvFLAGS(sv) & SVf_OK)
f7877b28 984#define SvOK_off(sv) (assert_not_ROK(sv) assert_not_glob(sv) \
1f4fbd3b
MS
985 SvFLAGS(sv) &= ~(SVf_OK| \
986 SVf_IVisUV|SVf_UTF8), \
987 SvOOK_off(sv))
54866b45 988#define SvOK_off_exc_UV(sv) (assert_not_ROK(sv) \
1f4fbd3b
MS
989 SvFLAGS(sv) &= ~(SVf_OK| \
990 SVf_UTF8), \
991 SvOOK_off(sv))
79072805 992
8990e307
LW
993#define SvOKp(sv) (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
994#define SvIOKp(sv) (SvFLAGS(sv) & SVp_IOK)
b326ddff 995#define SvIOKp_on(sv) (assert_not_glob(sv) \
1f4fbd3b 996 SvFLAGS(sv) |= SVp_IOK)
8990e307 997#define SvNOKp(sv) (SvFLAGS(sv) & SVp_NOK)
f7877b28 998#define SvNOKp_on(sv) (assert_not_glob(sv) SvFLAGS(sv) |= SVp_NOK)
8990e307 999#define SvPOKp(sv) (SvFLAGS(sv) & SVp_POK)
f7877b28 1000#define SvPOKp_on(sv) (assert_not_ROK(sv) assert_not_glob(sv) \
1f4fbd3b 1001 SvFLAGS(sv) |= SVp_POK)
463ee0b2 1002
79072805 1003#define SvIOK(sv) (SvFLAGS(sv) & SVf_IOK)
b326ddff 1004#define SvIOK_on(sv) (assert_not_glob(sv) \
1f4fbd3b 1005 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
25da4f38 1006#define SvIOK_off(sv) (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV))
0c34ef67 1007#define SvIOK_only(sv) (SvOK_off(sv), \
1f4fbd3b 1008 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
f7877b28 1009#define SvIOK_only_UV(sv) (assert_not_glob(sv) SvOK_off_exc_UV(sv), \
1f4fbd3b 1010 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
70401c6b 1011
25da4f38 1012#define SvIOK_UV(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV)) \
1f4fbd3b 1013 == (SVf_IOK|SVf_IVisUV))
28e5dec8 1014#define SvUOK(sv) SvIOK_UV(sv)
25da4f38 1015#define SvIOK_notUV(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV)) \
1f4fbd3b 1016 == SVf_IOK)
25da4f38 1017
fff0089c
YO
1018#define SvIandPOK(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_POK)) == (SVf_IOK|SVf_POK))
1019#define SvIandPOK_on(sv) (assert_not_glob(sv) \
1020 (SvFLAGS(sv) |= (SVf_IOK|SVp_IOK|SVf_POK|SVp_POK)))
1021#define SvIandPOK_off(sv) (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV|SVf_POK|SVp_POK))
1022
1023#define SvBoolFlagsOK(sv) SvIandPOK(sv)
1024
1025#define BOOL_INTERNALS_sv_isbool(sv) (SvIsCOW_static(sv) && \
1026 (SvPVX_const(sv) == PL_Yes || SvPVX_const(sv) == PL_No))
1027#define BOOL_INTERNALS_sv_isbool_true(sv) (SvIsCOW_static(sv) && \
1028 (SvPVX_const(sv) == PL_Yes))
1029#define BOOL_INTERNALS_sv_isbool_false(sv) (SvIsCOW_static(sv) && \
1030 (SvPVX_const(sv) == PL_No))
1031
25da4f38
IZ
1032#define SvIsUV(sv) (SvFLAGS(sv) & SVf_IVisUV)
1033#define SvIsUV_on(sv) (SvFLAGS(sv) |= SVf_IVisUV)
1034#define SvIsUV_off(sv) (SvFLAGS(sv) &= ~SVf_IVisUV)
79072805
LW
1035
1036#define SvNOK(sv) (SvFLAGS(sv) & SVf_NOK)
f7877b28 1037#define SvNOK_on(sv) (assert_not_glob(sv) \
1f4fbd3b 1038 SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
8990e307 1039#define SvNOK_off(sv) (SvFLAGS(sv) &= ~(SVf_NOK|SVp_NOK))
0c34ef67 1040#define SvNOK_only(sv) (SvOK_off(sv), \
1f4fbd3b 1041 SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
79072805 1042
914184e1 1043/*
fbf9f983 1044=for apidoc Am|U32|SvUTF8|SV* sv
3c813ed0
KW
1045Returns a U32 value indicating the UTF-8 status of an SV. If things are set-up
1046properly, this indicates whether or not the SV contains UTF-8 encoded data.
de343f44 1047You should use this I<after> a call to C<L</SvPV>> or one of its variants, in
fd142383 1048case any call to string overloading updates the internal flag.
914184e1 1049
119bc988
KW
1050If you want to take into account the L<bytes> pragma, use C<L</DO_UTF8>>
1051instead.
1052
914184e1 1053=for apidoc Am|void|SvUTF8_on|SV *sv
1e54db1a 1054Turn on the UTF-8 status of an SV (the data is not changed, just the flag).
d5ce4a7c 1055Do not use frivolously.
914184e1
JH
1056
1057=for apidoc Am|void|SvUTF8_off|SV *sv
3c813ed0
KW
1058Unsets the UTF-8 status of an SV (the data is not changed, just the flag).
1059Do not use frivolously.
914184e1
JH
1060
1061=for apidoc Am|void|SvPOK_only_UTF8|SV* sv
796b6530 1062Tells an SV that it is a string and disables all other C<OK> bits,
1e54db1a 1063and leaves the UTF-8 status as it was.
f1a1024e 1064
914184e1
JH
1065=cut
1066 */
1067
7a5fd60d
NC
1068/* Ensure the return value of this macro does not clash with the GV_ADD* flags
1069in gv.h: */
a7cb1f99
GS
1070#define SvUTF8(sv) (SvFLAGS(sv) & SVf_UTF8)
1071#define SvUTF8_on(sv) (SvFLAGS(sv) |= (SVf_UTF8))
1072#define SvUTF8_off(sv) (SvFLAGS(sv) &= ~(SVf_UTF8))
5bc28da9 1073
79072805 1074#define SvPOK(sv) (SvFLAGS(sv) & SVf_POK)
f7877b28 1075#define SvPOK_on(sv) (assert_not_ROK(sv) assert_not_glob(sv) \
1f4fbd3b 1076 SvFLAGS(sv) |= (SVf_POK|SVp_POK))
8990e307 1077#define SvPOK_off(sv) (SvFLAGS(sv) &= ~(SVf_POK|SVp_POK))
f7877b28 1078#define SvPOK_only(sv) (assert_not_ROK(sv) assert_not_glob(sv) \
1f4fbd3b
MS
1079 SvFLAGS(sv) &= ~(SVf_OK| \
1080 SVf_IVisUV|SVf_UTF8), \
1081 SvFLAGS(sv) |= (SVf_POK|SVp_POK))
f7877b28 1082#define SvPOK_only_UTF8(sv) (assert_not_ROK(sv) assert_not_glob(sv) \
1f4fbd3b
MS
1083 SvFLAGS(sv) &= ~(SVf_OK| \
1084 SVf_IVisUV), \
1085 SvFLAGS(sv) |= (SVf_POK|SVp_POK))
79072805 1086
4f2da183 1087#define SvVOK(sv) (SvMAGICAL(sv) \
1f4fbd3b 1088 && mg_find(sv,PERL_MAGIC_vstring))
c4d5dfab
KW
1089/*
1090=for apidoc Am|MAGIC*|SvVSTRING_mg|SV * sv
1091
1092Returns the vstring magic, or NULL if none
1093
1094=cut
1095*/
b0a11fe1 1096#define SvVSTRING_mg(sv) (SvMAGICAL(sv) \
1f4fbd3b 1097 ? mg_find(sv,PERL_MAGIC_vstring) : NULL)
b0a11fe1 1098
79072805 1099#define SvOOK(sv) (SvFLAGS(sv) & SVf_OOK)
a4f658f7 1100#define SvOOK_on(sv) (SvFLAGS(sv) |= SVf_OOK)
13cc62f5
KW
1101
1102
1103/*
1104=for apidoc Am|void|SvOOK_off|SV * sv
1105
1106Remove any string offset.
1107
1108=cut
1109*/
1110
fa7a1e49 1111#define SvOOK_off(sv) ((void)(SvOOK(sv) && (sv_backoff(sv),0)))
79072805 1112
a0d0e21e
LW
1113#define SvFAKE(sv) (SvFLAGS(sv) & SVf_FAKE)
1114#define SvFAKE_on(sv) (SvFLAGS(sv) |= SVf_FAKE)
1115#define SvFAKE_off(sv) (SvFLAGS(sv) &= ~SVf_FAKE)
1116
ed6116ce 1117#define SvROK(sv) (SvFLAGS(sv) & SVf_ROK)
a0d0e21e 1118#define SvROK_on(sv) (SvFLAGS(sv) |= SVf_ROK)
dd2eae66 1119#define SvROK_off(sv) (SvFLAGS(sv) &= ~(SVf_ROK))
79072805 1120
8990e307
LW
1121#define SvMAGICAL(sv) (SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG))
1122#define SvMAGICAL_on(sv) (SvFLAGS(sv) |= (SVs_GMG|SVs_SMG|SVs_RMG))
1123#define SvMAGICAL_off(sv) (SvFLAGS(sv) &= ~(SVs_GMG|SVs_SMG|SVs_RMG))
79072805 1124
8990e307
LW
1125#define SvGMAGICAL(sv) (SvFLAGS(sv) & SVs_GMG)
1126#define SvGMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_GMG)
1127#define SvGMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_GMG)
ed6116ce 1128
8990e307
LW
1129#define SvSMAGICAL(sv) (SvFLAGS(sv) & SVs_SMG)
1130#define SvSMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_SMG)
1131#define SvSMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_SMG)
ed6116ce 1132
8990e307
LW
1133#define SvRMAGICAL(sv) (SvFLAGS(sv) & SVs_RMG)
1134#define SvRMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_RMG)
1135#define SvRMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_RMG)
ed6116ce 1136
acd60c2b
KW
1137/*
1138=for apidoc Am|bool|SvAMAGIC|SV * sv
1139
1140Returns a boolean as to whether C<sv> has overloading (active magic) enabled or
1141not.
1142
1143=cut
1144*/
1145
a1cd65be 1146#define SvAMAGIC(sv) (SvROK(sv) && SvOBJECT(SvRV(sv)) && \
1f4fbd3b 1147 HvAMAGIC(SvSTASH(SvRV(sv))))
a0d0e21e 1148
0adc25b0
FC
1149/* To be used on the stashes themselves: */
1150#define HvAMAGIC(hv) (SvFLAGS(hv) & SVf_AMAGIC)
1151#define HvAMAGIC_on(hv) (SvFLAGS(hv) |= SVf_AMAGIC)
1152#define HvAMAGIC_off(hv) (SvFLAGS(hv) &=~ SVf_AMAGIC)
1153
4bac9ae4 1154
1078c6a2 1155/* "nog" means "doesn't have get magic" */
4bac9ae4
CS
1156#define SvPOK_nog(sv) ((SvFLAGS(sv) & (SVf_POK|SVs_GMG)) == SVf_POK)
1157#define SvIOK_nog(sv) ((SvFLAGS(sv) & (SVf_IOK|SVs_GMG)) == SVf_IOK)
1158#define SvUOK_nog(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV|SVs_GMG)) == (SVf_IOK|SVf_IVisUV))
1159#define SvNOK_nog(sv) ((SvFLAGS(sv) & (SVf_NOK|SVs_GMG)) == SVf_NOK)
1160#define SvNIOK_nog(sv) (SvNIOK(sv) && !(SvFLAGS(sv) & SVs_GMG))
1161
1162#define SvPOK_nogthink(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
1163#define SvIOK_nogthink(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_THINKFIRST|SVs_GMG)) == SVf_IOK)
1164#define SvUOK_nogthink(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV|SVf_THINKFIRST|SVs_GMG)) == (SVf_IOK|SVf_IVisUV))
1165#define SvNOK_nogthink(sv) ((SvFLAGS(sv) & (SVf_NOK|SVf_THINKFIRST|SVs_GMG)) == SVf_NOK)
1166#define SvNIOK_nogthink(sv) (SvNIOK(sv) && !(SvFLAGS(sv) & (SVf_THINKFIRST|SVs_GMG)))
1167
1168#define SvPOK_utf8_nog(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVs_GMG)) == (SVf_POK|SVf_UTF8))
1169#define SvPOK_utf8_nogthink(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST|SVs_GMG)) == (SVf_POK|SVf_UTF8))
1170
1171#define SvPOK_byte_nog(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVs_GMG)) == SVf_POK)
1172#define SvPOK_byte_nogthink(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
1173
8bb025ae
CS
1174#define SvPOK_pure_nogthink(sv) \
1175 ((SvFLAGS(sv) & (SVf_POK|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
1176#define SvPOK_utf8_pure_nogthink(sv) \
1177 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == (SVf_POK|SVf_UTF8))
1178#define SvPOK_byte_pure_nogthink(sv) \
1179 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
1180
d34786ba 1181/*
62a72503 1182=for apidoc Am|bool|SvIsBOOL|SV* sv
1d0d673f
PE
1183
1184Returns true if the SV is one of the special boolean constants (PL_sv_yes or
1185PL_sv_no), or is a regular SV whose last assignment stored a copy of one.
1186
1187=cut
1188*/
1189
1190#define SvIsBOOL(sv) Perl_sv_isbool(aTHX_ sv)
1191
1192/*
458b44e7 1193=for apidoc Am|U32|SvGAMAGIC|SV* sv
d34786ba 1194
8554e7a0
FC
1195Returns true if the SV has get magic or
1196overloading. If either is true then
d34786ba 1197the scalar is active data, and has the potential to return a new value every
8554e7a0
FC
1198time it is accessed. Hence you must be careful to
1199only read it once per user logical operation and work
1200with that returned value. If neither is true then
d34786ba
NC
1201the scalar's value cannot change unless written to.
1202
1203=cut
1204*/
1205
dd2eae66 1206#define SvGAMAGIC(sv) (SvGMAGICAL(sv) || SvAMAGIC(sv))
1426bbf4 1207
9299023c 1208#define Gv_AMG(stash) \
1f4fbd3b
MS
1209 (HvNAME(stash) && Gv_AMupdate(stash,FALSE) \
1210 ? 1 \
1211 : (HvAMAGIC_off(stash), 0))
a0d0e21e 1212
810b8aa5 1213#define SvWEAKREF(sv) ((SvFLAGS(sv) & (SVf_ROK|SVprv_WEAKREF)) \
1f4fbd3b 1214 == (SVf_ROK|SVprv_WEAKREF))
810b8aa5
GS
1215#define SvWEAKREF_on(sv) (SvFLAGS(sv) |= (SVf_ROK|SVprv_WEAKREF))
1216#define SvWEAKREF_off(sv) (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_WEAKREF))
1217
1ccdb730 1218#define SvPCS_IMPORTED(sv) ((SvFLAGS(sv) & (SVf_ROK|SVprv_PCS_IMPORTED)) \
1f4fbd3b 1219 == (SVf_ROK|SVprv_PCS_IMPORTED))
1ccdb730
NC
1220#define SvPCS_IMPORTED_on(sv) (SvFLAGS(sv) |= (SVf_ROK|SVprv_PCS_IMPORTED))
1221#define SvPCS_IMPORTED_off(sv) (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_PCS_IMPORTED))
1222
a11eaecb
FC
1223/*
1224=for apidoc m|U32|SvTHINKFIRST|SV *sv
1225
796b6530
KW
1226A quick flag check to see whether an C<sv> should be passed to C<sv_force_normal>
1227to be "downgraded" before C<SvIVX> or C<SvPVX> can be modified directly.
a11eaecb 1228
796b6530
KW
1229For example, if your scalar is a reference and you want to modify the C<SvIVX>
1230slot, you can't just do C<SvROK_off>, as that will leak the referent.
a11eaecb
FC
1231
1232This is used internally by various sv-modifying functions, such as
ef058b33 1233C<sv_setsv>, C<sv_setiv> and C<sv_pvn_force>.
a11eaecb
FC
1234
1235One case that this does not handle is a gv without SvFAKE set. After
1236
1237 if (SvTHINKFIRST(gv)) sv_force_normal(gv);
1238
1239it will still be a gv.
1240
796b6530
KW
1241C<SvTHINKFIRST> sometimes produces false positives. In those cases
1242C<sv_force_normal> does nothing.
a11eaecb
FC
1243
1244=cut
1245*/
1246
a0d0e21e 1247#define SvTHINKFIRST(sv) (SvFLAGS(sv) & SVf_THINKFIRST)
ed6116ce 1248
c0683843 1249#define SVs_PADMY 0
3c1759e4 1250#define SvPADMY(sv) (!(SvFLAGS(sv) & SVs_PADTMP))
145bf8ee
FC
1251#ifndef PERL_CORE
1252# define SvPADMY_on(sv) SvPADTMP_off(sv)
1253#endif
d9d18af6 1254
c0683843
FC
1255#define SvPADTMP(sv) (SvFLAGS(sv) & (SVs_PADTMP))
1256#define SvPADSTALE(sv) (SvFLAGS(sv) & (SVs_PADSTALE))
62bb6514 1257
145bf8ee
FC
1258#define SvPADTMP_on(sv) (SvFLAGS(sv) |= SVs_PADTMP)
1259#define SvPADTMP_off(sv) (SvFLAGS(sv) &= ~SVs_PADTMP)
c9182d9c
KW
1260#define SvPADSTALE_on(sv) Perl_SvPADSTALE_on(MUTABLE_SV(sv))
1261#define SvPADSTALE_off(sv) Perl_SvPADSTALE_off(MUTABLE_SV(sv))
ed6116ce 1262
8990e307
LW
1263#define SvTEMP(sv) (SvFLAGS(sv) & SVs_TEMP)
1264#define SvTEMP_on(sv) (SvFLAGS(sv) |= SVs_TEMP)
1265#define SvTEMP_off(sv) (SvFLAGS(sv) &= ~SVs_TEMP)
79072805 1266
8990e307
LW
1267#define SvOBJECT(sv) (SvFLAGS(sv) & SVs_OBJECT)
1268#define SvOBJECT_on(sv) (SvFLAGS(sv) |= SVs_OBJECT)
1269#define SvOBJECT_off(sv) (SvFLAGS(sv) &= ~SVs_OBJECT)
79072805 1270
cb198164
YO
1271/*
1272=for apidoc Am|U32|SvREADONLY|SV* sv
1273Returns true if the argument is readonly, otherwise returns false.
a3815e44 1274Exposed to perl code via Internals::SvREADONLY().
cb198164
YO
1275
1276=for apidoc Am|U32|SvREADONLY_on|SV* sv
1277Mark an object as readonly. Exactly what this means depends on the object
1278type. Exposed to perl code via Internals::SvREADONLY().
1279
1280=for apidoc Am|U32|SvREADONLY_off|SV* sv
1281Mark an object as not-readonly. Exactly what this mean depends on the
1282object type. Exposed to perl code via Internals::SvREADONLY().
1283
1284=cut
1285*/
1286
a623f893
FC
1287#define SvREADONLY(sv) (SvFLAGS(sv) & (SVf_READONLY|SVf_PROTECT))
1288#ifdef PERL_CORE
1289# define SvREADONLY_on(sv) (SvFLAGS(sv) |= (SVf_READONLY|SVf_PROTECT))
1290# define SvREADONLY_off(sv) (SvFLAGS(sv) &=~(SVf_READONLY|SVf_PROTECT))
1291#else
1292# define SvREADONLY_on(sv) (SvFLAGS(sv) |= SVf_READONLY)
1293# define SvREADONLY_off(sv) (SvFLAGS(sv) &= ~SVf_READONLY)
1294#endif
79072805 1295
180488f8 1296#define SvSCREAM(sv) ((SvFLAGS(sv) & (SVp_SCREAM|SVp_POK)) == (SVp_SCREAM|SVp_POK))
8990e307
LW
1297#define SvSCREAM_on(sv) (SvFLAGS(sv) |= SVp_SCREAM)
1298#define SvSCREAM_off(sv) (SvFLAGS(sv) &= ~SVp_SCREAM)
79072805 1299
74e0ddf7
NC
1300#ifndef PERL_CORE
1301# define SvCOMPILED(sv) 0
1302# define SvCOMPILED_on(sv)
1303# define SvCOMPILED_off(sv)
1304#endif
79072805 1305
85ffbbb0 1306
041c1a23 1307#if defined (DEBUGGING) && defined(PERL_USE_GCC_BRACE_GROUPS)
e9726144 1308# define SvTAIL(sv) ({ const SV *const _svtail = (const SV *)(sv); \
1f4fbd3b
MS
1309 assert(SvTYPE(_svtail) != SVt_PVAV); \
1310 assert(SvTYPE(_svtail) != SVt_PVHV); \
1311 assert(!(SvFLAGS(_svtail) & (SVf_NOK|SVp_NOK))); \
1312 assert(SvVALID(_svtail)); \
b4204fb6 1313 ((XPVNV*)SvANY(_svtail))->xnv_u.xnv_bm_tail; \
1f4fbd3b 1314 })
85efc3a5 1315#else
b4204fb6 1316# define SvTAIL(_svtail) (((XPVNV*)SvANY(_svtail))->xnv_u.xnv_bm_tail)
85efc3a5 1317#endif
8990e307 1318
a5c7cb08
DM
1319/* Does the SV have a Boyer-Moore table attached as magic?
1320 * 'VALID' is a poor name, but is kept for historical reasons. */
1321#define SvVALID(_svvalid) ( \
4e8879f3
DM
1322 SvPOKp(_svvalid) \
1323 && SvSMAGICAL(_svvalid) \
a5c7cb08
DM
1324 && SvMAGIC(_svvalid) \
1325 && (SvMAGIC(_svvalid)->mg_type == PERL_MAGIC_bm \
1326 || mg_find(_svvalid, PERL_MAGIC_bm)) \
1327 )
1328
ed6116ce
LW
1329#define SvRVx(sv) SvRV(sv)
1330
f599b64b 1331#ifdef PERL_DEBUG_COW
8ed30cc1
DD
1332/* Need -0.0 for SvNVX to preserve IEEE FP "negative zero" because
1333 +0.0 + -0.0 => +0.0 but -0.0 + -0.0 => -0.0 */
771ba71a
NC
1334# define SvIVX(sv) (0 + ((XPVIV*) SvANY(sv))->xiv_iv)
1335# define SvUVX(sv) (0 + ((XPVUV*) SvANY(sv))->xuv_uv)
58430790 1336# define SvNVX(sv) (-0.0 + ((XPVNV*) SvANY(sv))->xnv_u.xnv_nv)
ac09da3b 1337# define SvRV(sv) (0 + (sv)->sv_u.svu_rv)
f19a12a3 1338# define SvRV_const(sv) (0 + (sv)->sv_u.svu_rv)
c0e1089a 1339/* Don't test the core XS code yet. */
771ba71a 1340# if defined (PERL_CORE) && PERL_DEBUG_COW > 1
c7724415 1341# define SvPVX(sv) (0 + (assert_(!SvREADONLY(sv)) (sv)->sv_u.svu_pv))
771ba71a
NC
1342# else
1343# define SvPVX(sv) SvPVX_mutable(sv)
1344# endif
771ba71a
NC
1345# define SvCUR(sv) (0 + ((XPV*) SvANY(sv))->xpv_cur)
1346# define SvLEN(sv) (0 + ((XPV*) SvANY(sv))->xpv_len)
1347# define SvEND(sv) ((sv)->sv_u.svu_pv + ((XPV*)SvANY(sv))->xpv_cur)
1348
c7724415
FC
1349# define SvMAGIC(sv) (0 + *(assert_(SvTYPE(sv) >= SVt_PVMG) &((XPVMG*) SvANY(sv))->xmg_u.xmg_magic))
1350# define SvSTASH(sv) (0 + *(assert_(SvTYPE(sv) >= SVt_PVMG) &((XPVMG*) SvANY(sv))->xmg_stash))
f3476b0f 1351#else /* Below is not PERL_DEBUG_COW */
2324bdb9
DIM
1352# ifdef PERL_CORE
1353# define SvLEN(sv) (0 + ((XPV*) SvANY(sv))->xpv_len)
1354# else
771ba71a 1355# define SvLEN(sv) ((XPV*) SvANY(sv))->xpv_len
2324bdb9 1356# endif
771ba71a
NC
1357# define SvEND(sv) ((sv)->sv_u.svu_pv + ((XPV*)SvANY(sv))->xpv_cur)
1358
041c1a23 1359# if defined (DEBUGGING) && defined(PERL_USE_GCC_BRACE_GROUPS)
05ef5f8f 1360/* These get expanded inside other macros that already use a variable _sv */
f7877b28 1361# define SvPVX(sv) \
1f4fbd3b
MS
1362 (*({ SV *const _svpvx = MUTABLE_SV(sv); \
1363 assert(PL_valid_types_PVX[SvTYPE(_svpvx) & SVt_MASK]); \
1364 assert(!isGV_with_GP(_svpvx)); \
1365 assert(!(SvTYPE(_svpvx) == SVt_PVIO \
1366 && !(IoFLAGS(_svpvx) & IOf_FAKE_DIRP))); \
1367 &((_svpvx)->sv_u.svu_pv); \
1368 }))
2324bdb9
DIM
1369# ifdef PERL_CORE
1370# define SvCUR(sv) \
1f4fbd3b
MS
1371 ({ const SV *const _svcur = (const SV *)(sv); \
1372 assert(PL_valid_types_PVX[SvTYPE(_svcur) & SVt_MASK]); \
1373 assert(!isGV_with_GP(_svcur)); \
1374 assert(!(SvTYPE(_svcur) == SVt_PVIO \
1375 && !(IoFLAGS(_svcur) & IOf_FAKE_DIRP))); \
1376 (((XPV*) MUTABLE_PTR(SvANY(_svcur)))->xpv_cur); \
1377 })
2324bdb9 1378# else
08002bbf 1379# define SvCUR(sv) \
1f4fbd3b
MS
1380 (*({ const SV *const _svcur = (const SV *)(sv); \
1381 assert(PL_valid_types_PVX[SvTYPE(_svcur) & SVt_MASK]); \
1382 assert(!isGV_with_GP(_svcur)); \
1383 assert(!(SvTYPE(_svcur) == SVt_PVIO \
1384 && !(IoFLAGS(_svcur) & IOf_FAKE_DIRP))); \
1385 &(((XPV*) MUTABLE_PTR(SvANY(_svcur)))->xpv_cur); \
1386 }))
2324bdb9 1387# endif
05ef5f8f 1388# define SvIVX(sv) \
1f4fbd3b
MS
1389 (*({ const SV *const _svivx = (const SV *)(sv); \
1390 assert(PL_valid_types_IVX[SvTYPE(_svivx) & SVt_MASK]); \
1391 assert(!isGV_with_GP(_svivx)); \
1392 &(((XPVIV*) MUTABLE_PTR(SvANY(_svivx)))->xiv_iv); \
1393 }))
05ef5f8f 1394# define SvUVX(sv) \
1f4fbd3b
MS
1395 (*({ const SV *const _svuvx = (const SV *)(sv); \
1396 assert(PL_valid_types_IVX[SvTYPE(_svuvx) & SVt_MASK]); \
1397 assert(!isGV_with_GP(_svuvx)); \
1398 &(((XPVUV*) MUTABLE_PTR(SvANY(_svuvx)))->xuv_uv); \
1399 }))
05ef5f8f 1400# define SvNVX(sv) \
1f4fbd3b
MS
1401 (*({ const SV *const _svnvx = (const SV *)(sv); \
1402 assert(PL_valid_types_NVX[SvTYPE(_svnvx) & SVt_MASK]); \
1403 assert(!isGV_with_GP(_svnvx)); \
1404 &(((XPVNV*) MUTABLE_PTR(SvANY(_svnvx)))->xnv_u.xnv_nv); \
1405 }))
ac09da3b 1406# define SvRV(sv) \
1f4fbd3b
MS
1407 (*({ SV *const _svrv = MUTABLE_SV(sv); \
1408 assert(PL_valid_types_RV[SvTYPE(_svrv) & SVt_MASK]); \
1409 assert(!isGV_with_GP(_svrv)); \
1410 assert(!(SvTYPE(_svrv) == SVt_PVIO \
1411 && !(IoFLAGS(_svrv) & IOf_FAKE_DIRP))); \
1412 &((_svrv)->sv_u.svu_rv); \
1413 }))
f19a12a3 1414# define SvRV_const(sv) \
1f4fbd3b
MS
1415 ({ const SV *const _svrv = (const SV *)(sv); \
1416 assert(PL_valid_types_RV[SvTYPE(_svrv) & SVt_MASK]); \
1417 assert(!isGV_with_GP(_svrv)); \
1418 assert(!(SvTYPE(_svrv) == SVt_PVIO \
1419 && !(IoFLAGS(_svrv) & IOf_FAKE_DIRP))); \
1420 (_svrv)->sv_u.svu_rv; \
1421 })
05ef5f8f 1422# define SvMAGIC(sv) \
1f4fbd3b
MS
1423 (*({ const SV *const _svmagic = (const SV *)(sv); \
1424 assert(SvTYPE(_svmagic) >= SVt_PVMG); \
1425 &(((XPVMG*) MUTABLE_PTR(SvANY(_svmagic)))->xmg_u.xmg_magic); \
1426 }))
05ef5f8f 1427# define SvSTASH(sv) \
1f4fbd3b
MS
1428 (*({ const SV *const _svstash = (const SV *)(sv); \
1429 assert(SvTYPE(_svstash) >= SVt_PVMG); \
1430 &(((XPVMG*) MUTABLE_PTR(SvANY(_svstash)))->xmg_stash); \
1431 }))
f3476b0f 1432# else /* Below is not DEBUGGING or can't use brace groups */
08002bbf
NC
1433# define SvPVX(sv) ((sv)->sv_u.svu_pv)
1434# define SvCUR(sv) ((XPV*) SvANY(sv))->xpv_cur
05ef5f8f
NC
1435# define SvIVX(sv) ((XPVIV*) SvANY(sv))->xiv_iv
1436# define SvUVX(sv) ((XPVUV*) SvANY(sv))->xuv_uv
58430790 1437# define SvNVX(sv) ((XPVNV*) SvANY(sv))->xnv_u.xnv_nv
ac09da3b 1438# define SvRV(sv) ((sv)->sv_u.svu_rv)
f19a12a3 1439# define SvRV_const(sv) (0 + (sv)->sv_u.svu_rv)
e736a858 1440# define SvMAGIC(sv) ((XPVMG*) SvANY(sv))->xmg_u.xmg_magic
771ba71a 1441# define SvSTASH(sv) ((XPVMG*) SvANY(sv))->xmg_stash
03687789 1442# endif
62703e72
NC
1443#endif
1444
06c0cc96 1445#ifndef PERL_POISON
bb496845 1446/* Given that these two are new, there can't be any existing code using them
3968dd31 1447 * as LVALUEs, so prevent that from happening */
a8b89476
TC
1448# define SvPVX_mutable(sv) ((char *)((sv)->sv_u.svu_pv))
1449# define SvPVX_const(sv) ((const char*)((sv)->sv_u.svu_pv))
06c0cc96
NC
1450#else
1451/* Except for the poison code, which uses & to scribble over the pointer after
1452 free() is called. */
1453# define SvPVX_mutable(sv) ((sv)->sv_u.svu_pv)
1454# define SvPVX_const(sv) ((const char*)((sv)->sv_u.svu_pv))
1455#endif
bb496845 1456
62703e72
NC
1457#define SvIVXx(sv) SvIVX(sv)
1458#define SvUVXx(sv) SvUVX(sv)
1459#define SvNVXx(sv) SvNVX(sv)
1460#define SvPVXx(sv) SvPVX(sv)
1461#define SvLENx(sv) SvLEN(sv)
1462#define SvENDx(sv) ((PL_Sv = (sv)), SvEND(PL_Sv))
1463
1464
28e5dec8
JH
1465/* Ask a scalar nicely to try to become an IV, if possible.
1466 Not guaranteed to stay returning void */
1467/* Macro won't actually call sv_2iv if already IOK */
1468#define SvIV_please(sv) \
51c4bdf3
KW
1469 STMT_START { \
1470 SV * sv_ = MUTABLE_SV(sv); \
1471 if (!SvIOKp(sv_) && (SvFLAGS(sv_) & (SVf_NOK|SVf_POK))) \
1472 (void) SvIV(sv_); \
1473 } STMT_END
6f1401dc 1474#define SvIV_please_nomg(sv) \
1f4fbd3b
MS
1475 (!(SvFLAGS(sv) & (SVf_IOK|SVp_IOK)) && (SvFLAGS(sv) & (SVf_NOK|SVf_POK)) \
1476 ? (sv_2iv_flags(sv, 0), SvIOK(sv)) \
1477 : SvIOK(sv))
8c981be1 1478
79072805 1479#define SvIV_set(sv, val) \
1f4fbd3b 1480 STMT_START { \
8c981be1
KW
1481 SV * sv_ = MUTABLE_SV(sv); \
1482 assert(PL_valid_types_IV_set[SvTYPE(sv_) & SVt_MASK]); \
1483 assert(!isGV_with_GP(sv_)); \
1484 (((XPVIV*) SvANY(sv_))->xiv_iv = (val)); \
1485 } STMT_END
1486
79072805 1487#define SvNV_set(sv, val) \
1f4fbd3b 1488 STMT_START { \
8c981be1
KW
1489 SV * sv_ = MUTABLE_SV(sv); \
1490 assert(PL_valid_types_NV_set[SvTYPE(sv_) & SVt_MASK]); \
1491 assert(!isGV_with_GP(sv_)); \
1492 (((XPVNV*)SvANY(sv_))->xnv_u.xnv_nv = (val)); \
1493 } STMT_END
1494
79072805 1495#define SvPV_set(sv, val) \
1f4fbd3b 1496 STMT_START { \
8c981be1
KW
1497 SV * sv_ = MUTABLE_SV(sv); \
1498 assert(PL_valid_types_PVX[SvTYPE(sv_) & SVt_MASK]); \
1499 assert(!isGV_with_GP(sv_)); \
1500 assert(!(SvTYPE(sv_) == SVt_PVIO \
1501 && !(IoFLAGS(sv_) & IOf_FAKE_DIRP))); \
1502 ((sv_)->sv_u.svu_pv = (val)); \
1503 } STMT_END
1504
607fa7f2 1505#define SvUV_set(sv, val) \
1f4fbd3b 1506 STMT_START { \
8c981be1
KW
1507 SV * sv_ = MUTABLE_SV(sv); \
1508 assert(PL_valid_types_IV_set[SvTYPE(sv_) & SVt_MASK]); \
1509 assert(!isGV_with_GP(sv_)); \
1510 (((XPVUV*)SvANY(sv_))->xuv_uv = (val)); \
1511 } STMT_END
1512
b162af07 1513#define SvRV_set(sv, val) \
f1fb8741 1514 STMT_START { \
8c981be1
KW
1515 SV * sv_ = MUTABLE_SV(sv); \
1516 assert(PL_valid_types_RV[SvTYPE(sv_) & SVt_MASK]); \
1517 assert(!isGV_with_GP(sv_)); \
1518 assert(!(SvTYPE(sv_) == SVt_PVIO \
1519 && !(IoFLAGS(sv_) & IOf_FAKE_DIRP))); \
1520 ((sv_)->sv_u.svu_rv = (val)); \
1521 } STMT_END
b162af07
SP
1522#define SvMAGIC_set(sv, val) \
1523 STMT_START { assert(SvTYPE(sv) >= SVt_PVMG); \
e736a858 1524 (((XPVMG*)SvANY(sv))->xmg_u.xmg_magic = (val)); } STMT_END
b162af07
SP
1525#define SvSTASH_set(sv, val) \
1526 STMT_START { assert(SvTYPE(sv) >= SVt_PVMG); \
1527 (((XPVMG*) SvANY(sv))->xmg_stash = (val)); } STMT_END
79072805 1528#define SvCUR_set(sv, val) \
1f4fbd3b
MS
1529 STMT_START { \
1530 assert(PL_valid_types_PVX[SvTYPE(sv) & SVt_MASK]); \
1531 assert(!isGV_with_GP(sv)); \
1532 assert(!(SvTYPE(sv) == SVt_PVIO \
1533 && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1534 (((XPV*) SvANY(sv))->xpv_cur = (val)); } STMT_END
79072805 1535#define SvLEN_set(sv, val) \
1f4fbd3b
MS
1536 STMT_START { \
1537 assert(PL_valid_types_PVX[SvTYPE(sv) & SVt_MASK]); \
1538 assert(!isGV_with_GP(sv)); \
1539 assert(!(SvTYPE(sv) == SVt_PVIO \
1540 && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1541 (((XPV*) SvANY(sv))->xpv_len = (val)); } STMT_END
7ae8ee9e 1542#define SvEND_set(sv, val) \
1f4fbd3b
MS
1543 STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
1544 SvCUR_set(sv, (val) - SvPVX(sv)); } STMT_END
79072805 1545
cd188aff
KW
1546/*
1547=for apidoc Am|void|SvPV_renew|SV* sv|STRLEN len
1548Low level micro optimization of C<L</SvGROW>>. It is generally better to use
1549C<SvGROW> instead. This is because C<SvPV_renew> ignores potential issues that
0c6362ad 1550C<SvGROW> handles. C<sv> needs to have a real C<PV> that is unencumbered by
cbcd1db3
KW
1551things like COW. Using C<SV_CHECK_THINKFIRST> or
1552C<SV_CHECK_THINKFIRST_COW_DROP> before calling this should clean it up, but
cd188aff
KW
1553why not just use C<SvGROW> if you're not sure about the provenance?
1554
1555=cut
1556*/
b7e9a5c2 1557#define SvPV_renew(sv,n) \
1f4fbd3b
MS
1558 STMT_START { SvLEN_set(sv, n); \
1559 SvPV_set((sv), (MEM_WRAP_CHECK_(n,char) \
1560 (char*)saferealloc((Malloc_t)SvPVX(sv), \
1561 (MEM_SIZE)((n))))); \
1562 } STMT_END
e76506c9
KW
1563/*
1564=for apidoc Am|void|SvPV_shrink_to_cur|SV* sv
1565
1566Trim any trailing unused memory in the PV of C<sv>, which needs to have a real
0c6362ad 1567C<PV> that is unencumbered by things like COW. Think first before using this
e76506c9
KW
1568functionality. Is the space saving really worth giving up COW? Will the
1569needed size of C<sv> stay the same?
1570
af46018b
KW
1571If the answers are both yes, then use L</C<SV_CHECK_THINKFIRST>> or
1572L</C<SV_CHECK_THINKFIRST_COW_DROP>> before calling this.
e76506c9
KW
1573
1574=cut
1575*/
1da4ca5f
NC
1576
1577#define SvPV_shrink_to_cur(sv) STMT_START { \
1f4fbd3b
MS
1578 const STRLEN _lEnGtH = SvCUR(sv) + 1; \
1579 SvPV_renew(sv, _lEnGtH); \
1580 } STMT_END
b7e9a5c2 1581
d54acd4f
KW
1582/*
1583=for apidoc Am|void|SvPV_free|SV * sv
1584
1585Frees the PV buffer in C<sv>, leaving things in a precarious state, so should
1586only be used as part of a larger operation
1587
1588=cut
1589*/
771ba71a
NC
1590#define SvPV_free(sv) \
1591 STMT_START { \
1f4fbd3b
MS
1592 assert(SvTYPE(sv) >= SVt_PV); \
1593 if (SvLEN(sv)) { \
1594 assert(!SvROK(sv)); \
1595 if(UNLIKELY(SvOOK(sv))) { \
1596 STRLEN zok; \
1597 SvOOK_offset(sv, zok); \
1598 SvPV_set(sv, SvPVX_mutable(sv) - zok); \
1599 SvFLAGS(sv) &= ~SVf_OOK; \
1600 } \
1601 Safefree(SvPVX(sv)); \
1602 } \
1603 } STMT_END
8bd4d4c5 1604
43230e26
NC
1605#ifdef PERL_CORE
1606/* Code that crops up in three places to take a scalar and ready it to hold
1607 a reference */
1608# define prepare_SV_for_RV(sv) \
1609 STMT_START { \
1f4fbd3b
MS
1610 if (SvTYPE(sv) < SVt_PV && SvTYPE(sv) != SVt_IV) \
1611 sv_upgrade(sv, SVt_IV); \
1612 else if (SvTYPE(sv) >= SVt_PV) { \
1613 SvPV_free(sv); \
1614 SvLEN_set(sv, 0); \
43230e26 1615 SvCUR_set(sv, 0); \
1f4fbd3b
MS
1616 } \
1617 } STMT_END
43230e26 1618#endif
373b357f 1619
cffe132d
NC
1620#ifndef PERL_CORE
1621# define BmFLAGS(sv) (SvTAIL(sv) ? FBMcf_TAIL : 0)
1622#endif
6976cee3 1623
041c1a23 1624#if defined (DEBUGGING) && defined(PERL_USE_GCC_BRACE_GROUPS)
60c8298a 1625# define BmUSEFUL(sv) \
1f4fbd3b
MS
1626 (*({ SV *const _bmuseful = MUTABLE_SV(sv); \
1627 assert(SvTYPE(_bmuseful) >= SVt_PVIV); \
1628 assert(SvVALID(_bmuseful)); \
1629 assert(!SvIOK(_bmuseful)); \
1630 &(((XPVIV*) SvANY(_bmuseful))->xiv_u.xivu_iv); \
1631 }))
60c8298a 1632#else
50c7be83 1633# define BmUSEFUL(sv) ((XPVIV*) SvANY(sv))->xiv_u.xivu_iv
b4f6c04b 1634
60c8298a 1635#endif
79072805 1636
8922e438
FC
1637#ifndef PERL_CORE
1638# define BmRARE(sv) 0
1639# define BmPREVIOUS(sv) 0
1640#endif
1641
f2da823f 1642#define FmLINES(sv) ((XPVIV*) SvANY(sv))->xiv_iv
79072805
LW
1643
1644#define LvTYPE(sv) ((XPVLV*) SvANY(sv))->xlv_type
1645#define LvTARG(sv) ((XPVLV*) SvANY(sv))->xlv_targ
1646#define LvTARGOFF(sv) ((XPVLV*) SvANY(sv))->xlv_targoff
bbfdc870 1647#define LvSTARGOFF(sv) ((XPVLV*) SvANY(sv))->xlv_targoff_u.xlvu_stargoff
79072805 1648#define LvTARGLEN(sv) ((XPVLV*) SvANY(sv))->xlv_targlen
83f78d1a 1649#define LvFLAGS(sv) ((XPVLV*) SvANY(sv))->xlv_flags
79072805 1650
b063b0a8
DIM
1651#define LVf_NEG_OFF 0x1
1652#define LVf_NEG_LEN 0x2
1653#define LVf_OUT_OF_RANGE 0x4
1654
6f7e8353 1655#define IoIFP(sv) (sv)->sv_u.svu_fp
8990e307
LW
1656#define IoOFP(sv) ((XPVIO*) SvANY(sv))->xio_ofp
1657#define IoDIRP(sv) ((XPVIO*) SvANY(sv))->xio_dirp
4755096e 1658#define IoANY(sv) ((XPVIO*) SvANY(sv))->xio_any
c490b21c 1659#define IoLINES(sv) ((XPVIO*) SvANY(sv))->xiv_u.xivu_iv
8990e307
LW
1660#define IoPAGE(sv) ((XPVIO*) SvANY(sv))->xio_page
1661#define IoPAGE_LEN(sv) ((XPVIO*) SvANY(sv))->xio_page_len
1662#define IoLINES_LEFT(sv)((XPVIO*) SvANY(sv))->xio_lines_left
1663#define IoTOP_NAME(sv) ((XPVIO*) SvANY(sv))->xio_top_name
1664#define IoTOP_GV(sv) ((XPVIO*) SvANY(sv))->xio_top_gv
1665#define IoFMT_NAME(sv) ((XPVIO*) SvANY(sv))->xio_fmt_name
1666#define IoFMT_GV(sv) ((XPVIO*) SvANY(sv))->xio_fmt_gv
1667#define IoBOTTOM_NAME(sv)((XPVIO*) SvANY(sv))->xio_bottom_name
1668#define IoBOTTOM_GV(sv) ((XPVIO*) SvANY(sv))->xio_bottom_gv
8990e307
LW
1669#define IoTYPE(sv) ((XPVIO*) SvANY(sv))->xio_type
1670#define IoFLAGS(sv) ((XPVIO*) SvANY(sv))->xio_flags
1671
50952442 1672/* IoTYPE(sv) is a single character telling the type of I/O connection. */
3b6c1aba
JH
1673#define IoTYPE_RDONLY '<'
1674#define IoTYPE_WRONLY '>'
1675#define IoTYPE_RDWR '+'
1676#define IoTYPE_APPEND 'a'
1677#define IoTYPE_PIPE '|'
1678#define IoTYPE_STD '-' /* stdin or stdout */
1679#define IoTYPE_SOCKET 's'
1680#define IoTYPE_CLOSED ' '
1681#define IoTYPE_IMPLICIT 'I' /* stdin or stdout or stderr */
1682#define IoTYPE_NUMERIC '#' /* fdopen */
03fcf2fc
GS
1683
1684/*
d8e799d8 1685=for apidoc_section $tainting
954c1994 1686=for apidoc Am|bool|SvTAINTED|SV* sv
8554e7a0 1687Checks to see if an SV is tainted. Returns TRUE if it is, FALSE if
954c1994
GS
1688not.
1689
1690=for apidoc Am|void|SvTAINTED_on|SV* sv
c55831ac 1691Marks an SV as tainted if tainting is enabled.
954c1994
GS
1692
1693=for apidoc Am|void|SvTAINTED_off|SV* sv
8554e7a0
FC
1694Untaints an SV. Be I<very> careful with this routine, as it short-circuits
1695some of Perl's fundamental security features. XS module authors should not
954c1994 1696use this function unless they fully understand all the implications of
72d33970 1697unconditionally untainting the value. Untainting should be done in the
954c1994
GS
1698standard perl fashion, via a carefully crafted regexp, rather than directly
1699untainting variables.
1700
1701=for apidoc Am|void|SvTAINT|SV* sv
c98cdb04
FC
1702Taints an SV if tainting is enabled, and if some input to the current
1703expression is tainted--usually a variable, but possibly also implicit
1704inputs such as locale settings. C<SvTAINT> propagates that taintedness to
1705the outputs of an expression in a pessimistic fashion; i.e., without paying
1706attention to precisely which outputs are influenced by which inputs.
954c1994
GS
1707
1708=cut
1709*/
1710
a0714e2c 1711#define sv_taint(sv) sv_magic((sv), NULL, PERL_MAGIC_taint, NULL, 0)
aae9cea0 1712
dc6d7f5c 1713#ifdef NO_TAINT_SUPPORT
284167a5
S
1714# define SvTAINTED(sv) 0
1715#else
1716# define SvTAINTED(sv) (SvMAGICAL(sv) && sv_tainted(sv))
1717#endif
5d9574c1
DM
1718#define SvTAINTED_on(sv) STMT_START{ if(UNLIKELY(TAINTING_get)){sv_taint(sv);} }STMT_END
1719#define SvTAINTED_off(sv) STMT_START{ if(UNLIKELY(TAINTING_get)){sv_untaint(sv);} }STMT_END
bbce6d69 1720
c6ee37c5
MB
1721#define SvTAINT(sv) \
1722 STMT_START { \
d48c660d
DM
1723 assert(TAINTING_get || !TAINT_get); \
1724 if (UNLIKELY(TAINT_get)) \
1725 SvTAINTED_on(sv); \
c6ee37c5 1726 } STMT_END
79072805 1727
954c1994 1728/*
d8e799d8 1729=for apidoc_section $SV
1607e393
KW
1730=for apidoc Am|char*|SvPV_force |SV* sv|STRLEN len
1731=for apidoc_item ||SvPV_force_flags |SV * sv|STRLEN len|U32 flags
1732=for apidoc_item ||SvPV_force_flags_mutable|SV * sv|STRLEN len|U32 flags
1733=for apidoc_item ||SvPV_force_flags_nolen |SV * sv |U32 flags
1734=for apidoc_item ||SvPV_force_mutable |SV * sv|STRLEN len
1735=for apidoc_item ||SvPV_force_nolen |SV* sv
1736=for apidoc_item ||SvPV_force_nomg |SV* sv|STRLEN len
1737=for apidoc_item ||SvPV_force_nomg_nolen |SV * sv
1738=for apidoc_item ||SvPVbyte_force |SV * sv|STRLEN len
1739=for apidoc_item ||SvPVbytex_force |SV * sv|STRLEN len
1740=for apidoc_item ||SvPVutf8_force |SV * sv|STRLEN len
1741=for apidoc_item ||SvPVutf8x_force |SV * sv|STRLEN len
1742=for apidoc_item ||SvPVx_force |SV* sv|STRLEN len
26d1d7c7 1743
e97ba882
KW
1744These are like C<L</SvPV>>, returning the string in the SV, but will force the
1745SV into containing a string (C<L</SvPOK>>), and only a string
1746(C<L</SvPOK_only>>), by hook or by crook. You need to use one of these
1747C<force> routines if you are going to update the C<L</SvPVX>> directly.
954c1994 1748
fd142383 1749Note that coercing an arbitrary scalar into a plain PV will potentially
72d33970 1750strip useful data from it. For example if the SV was C<SvROK>, then the
fd142383
DM
1751referent will have its reference count decremented, and the SV itself may
1752be converted to an C<SvPOK> scalar with a string buffer containing a value
1753such as C<"ARRAY(0x1234)">.
1754
26d1d7c7
KW
1755The differences between the forms are:
1756
1757The forms with C<flags> in their names allow you to use the C<flags> parameter
1758to specify to perform 'get' magic (by setting the C<SV_GMAGIC> flag) or to skip
1759'get' magic (by clearing it). The other forms do perform 'get' magic, except
1760for the ones with C<nomg> in their names, which skip 'get' magic.
1761
e97ba882
KW
1762The forms that take a C<len> parameter will set that variable to the byte
1763length of the resultant string (these are macros, so don't use C<&len>).
1764
1765The forms with C<nolen> in their names indicate they don't have a C<len>
1766parameter. They should be used only when it is known that the PV is a C
1767string, terminated by a NUL byte, and without intermediate NUL characters; or
1768when you don't care about its length.
26d1d7c7
KW
1769
1770The forms with C<mutable> in their names are effectively the same as those without,
1771but the name emphasizes that the string is modifiable by the caller, which it is
1772in all the forms.
1773
1774C<SvPVutf8_force> is like C<SvPV_force>, but converts C<sv> to UTF-8 first if
1775not already UTF-8.
1776
1777C<SvPVutf8x_force> is like C<SvPVutf8_force>, but guarantees to evaluate C<sv>
1778only once; use the more efficient C<SvPVutf8_force> otherwise.
1779
1780C<SvPVbyte_force> is like C<SvPV_force>, but converts C<sv> to byte
1781representation first if currently encoded as UTF-8. If the SV cannot be
1782downgraded from UTF-8, this croaks.
1783
1784C<SvPVbytex_force> is like C<SvPVbyte_force>, but guarantees to evaluate C<sv>
1785only once; use the more efficient C<SvPVbyte_force> otherwise.
645c22ef 1786
1607e393
KW
1787=for apidoc Am | char*|SvPV |SV* sv|STRLEN len
1788=for apidoc_item |const char*|SvPV_const |SV* sv|STRLEN len
1789=for apidoc_item | char*|SvPV_flags |SV* sv|STRLEN len|U32 flags
1790=for apidoc_item |const char*|SvPV_flags_const |SV* sv|STRLEN len|U32 flags
1791=for apidoc_item | char*|SvPV_flags_mutable |SV* sv|STRLEN len|U32 flags
1792=for apidoc_item | char*|SvPV_mutable |SV* sv|STRLEN len
1793=for apidoc_item | char*|SvPV_nolen |SV* sv
1794=for apidoc_item |const char*|SvPV_nolen_const |SV* sv
1795=for apidoc_item | char*|SvPV_nomg |SV* sv|STRLEN len
1796=for apidoc_item |const char*|SvPV_nomg_const |SV* sv|STRLEN len
1e0eb5c5 1797=for apidoc_item |const char*|SvPV_nomg_const_nolen|SV* sv
1607e393
KW
1798=for apidoc_item | char*|SvPV_nomg_nolen |SV* sv
1799=for apidoc_item | char*|SvPVbyte |SV* sv|STRLEN len
1800=for apidoc_item | char*|SvPVbyte_nolen |SV* sv
1801=for apidoc_item | char*|SvPVbyte_nomg |SV* sv|STRLEN len
1802=for apidoc_item | char*|SvPVbyte_or_null |SV* sv|STRLEN len
1803=for apidoc_item | char*|SvPVbyte_or_null_nomg|SV* sv|STRLEN len
1804=for apidoc_item | char*|SvPVbytex |SV* sv|STRLEN len
1805=for apidoc_item | char*|SvPVbytex_nolen |SV* sv
1806=for apidoc_item | char*|SvPVutf8 |SV* sv|STRLEN len
1807=for apidoc_item | char*|SvPVutf8_nolen |SV* sv
1808=for apidoc_item | char*|SvPVutf8_nomg |SV* sv|STRLEN len
1809=for apidoc_item | char*|SvPVutf8_or_null |SV* sv|STRLEN len
1810=for apidoc_item | char*|SvPVutf8_or_null_nomg|SV* sv|STRLEN len
1811=for apidoc_item | char*|SvPVutf8x |SV* sv|STRLEN len
1812=for apidoc_item | char*|SvPVx |SV* sv|STRLEN len
1813=for apidoc_item |const char*|SvPVx_const |SV* sv|STRLEN len
1814=for apidoc_item | char*|SvPVx_nolen |SV* sv
1815=for apidoc_item |const char*|SvPVx_nolen_const |SV* sv
1e0eb5c5 1816
1ef9039b 1817These each return a pointer to the string in C<sv>, or a stringified form of
1e0eb5c5
KW
1818C<sv> if it does not contain a string. The SV may cache the stringified
1819version becoming C<SvPOK>.
1820
1821This is a very basic and common operation, so there are lots of slightly
1822different versions of it.
1823
1824Note that there is no guarantee that the return value of C<SvPV(sv)>, for
1825example, is equal to C<SvPVX(sv)>, or that C<SvPVX(sv)> contains valid data, or
1826that successive calls to C<SvPV(sv)> (or another of these forms) will return
1827the same pointer value each time. This is due to the way that things like
1828overloading and Copy-On-Write are handled. In these cases, the return value
1829may point to a temporary buffer or similar. If you absolutely need the
1830C<SvPVX> field to be valid (for example, if you intend to write to it), then
1831see C<L</SvPV_force>>.
1832
1833The differences between the forms are:
1834
3c3f883d
FG
1835The forms with neither C<byte> nor C<utf8> in their names (e.g., C<SvPV> or
1836C<SvPV_nolen>) can expose the SV's internal string buffer. If
1837that buffer consists entirely of bytes 0-255 and includes any bytes above
1838127, then you B<MUST> consult C<SvUTF8> to determine the actual code points
1839the string is meant to contain. Generally speaking, it is probably safer to
1840prefer C<SvPVbyte>, C<SvPVutf8>, and the like. See
1841L<perlguts/How do I pass a Perl string to a C library?> for more details.
1842
1e0eb5c5
KW
1843The forms with C<flags> in their names allow you to use the C<flags> parameter
1844to specify to process 'get' magic (by setting the C<SV_GMAGIC> flag) or to skip
1845'get' magic (by clearing it). The other forms process 'get' magic, except for
1846the ones with C<nomg> in their names, which skip 'get' magic.
1847
1848The forms that take a C<len> parameter will set that variable to the byte
1849length of the resultant string (these are macros, so don't use C<&len>).
1850
1851The forms with C<nolen> in their names indicate they don't have a C<len>
1852parameter. They should be used only when it is known that the PV is a C
1853string, terminated by a NUL byte, and without intermediate NUL characters; or
1854when you don't care about its length.
1855
1856The forms with C<const> in their names return S<C<const char *>> so that the
1857compiler will hopefully complain if you were to try to modify the contents of
1858the string (unless you cast away const yourself).
1859
1860The other forms return a mutable pointer so that the string is modifiable by
1861the caller; this is emphasized for the ones with C<mutable> in their names.
1862
1ef9039b
KW
1863As of 5.38, all forms are guaranteed to evaluate C<sv> exactly once. For
1864earlier Perls, use a form whose name ends with C<x> for single evaluation.
1e0eb5c5
KW
1865
1866C<SvPVutf8> is like C<SvPV>, but converts C<sv> to UTF-8 first if not already
0c6362ad 1867UTF-8. Similarly, the other forms with C<utf8> in their names correspond to
1e0eb5c5
KW
1868their respective forms without.
1869
1870C<SvPVutf8_or_null> and C<SvPVutf8_or_null_nomg> don't have corresponding
1871non-C<utf8> forms. Instead they are like C<SvPVutf8_nomg>, but when C<sv> is
1872undef, they return C<NULL>.
1873
1874C<SvPVbyte> is like C<SvPV>, but converts C<sv> to byte representation first if
1875currently encoded as UTF-8. If C<sv> cannot be downgraded from UTF-8, it
0c6362ad 1876croaks. Similarly, the other forms with C<byte> in their names correspond to
1e0eb5c5
KW
1877their respective forms without.
1878
1879C<SvPVbyte_or_null> doesn't have a corresponding non-C<byte> form. Instead it
1880is like C<SvPVbyte>, but when C<sv> is undef, it returns C<NULL>.
71eb6d8c 1881
1607e393 1882=for apidoc SvTRUE
4eff5eb8 1883=for apidoc_item SvTRUE_NN
1607e393 1884=for apidoc_item SvTRUE_nomg
4eff5eb8 1885=for apidoc_item SvTRUE_nomg_NN
1607e393 1886=for apidoc_item SvTRUEx
06c841cf 1887
4eff5eb8
KW
1888These return a boolean indicating whether Perl would evaluate the SV as true or
1889false. See C<L</SvOK>> for a defined/undefined test.
3127113c 1890
4eff5eb8
KW
1891As of Perl 5.32, all are guaranteed to evaluate C<sv> only once. Prior to that
1892release, only C<SvTRUEx> guaranteed single evaluation; now C<SvTRUEx> is
1893identical to C<SvTRUE>.
954c1994 1894
4eff5eb8
KW
1895C<SvTRUE_nomg> and C<TRUE_nomg_NN> do not perform 'get' magic; the others do
1896unless the scalar is already C<SvPOK>, C<SvIOK>, or C<SvNOK> (the public, not
1897the private flags).
1898
1899C<SvTRUE_NN> is like C<L</SvTRUE>>, but C<sv> is assumed to be
1900non-null (NN). If there is a possibility that it is NULL, use plain
1901C<SvTRUE>.
1902
1903C<SvTRUE_nomg_NN> is like C<L</SvTRUE_nomg>>, but C<sv> is assumed to be
1904non-null (NN). If there is a possibility that it is NULL, use plain
1905C<SvTRUE_nomg>.
1906
df8dcb28
DD
1907=for apidoc Am|U32|SvIsCOW|SV* sv
1908Returns a U32 value indicating whether the SV is Copy-On-Write (either shared
19dbb8f1 1909hash key scalars, or full Copy On Write scalars if 5.9.0 is configured for
8554e7a0 1910COW).
19dbb8f1
NC
1911
1912=for apidoc Am|bool|SvIsCOW_shared_hash|SV* sv
1913Returns a boolean indicating whether the SV is Copy-On-Write shared hash key
1914scalar.
645c22ef 1915
954c1994
GS
1916=cut
1917*/
1918
1ef9039b
KW
1919/* To pass the action to the functions called by the following macros */
1920typedef enum {
1921 SvPVutf8_type_,
1922 SvPVbyte_type_,
1923 SvPVnormal_type_,
1924 SvPVforce_type_,
1925 SvPVutf8_pure_type_,
1926 SvPVbyte_pure_type_
1927} PL_SvPVtype;
1928
1929START_EXTERN_C
1930
1931/* When this code was written, embed.fnc could not handle function pointer
1932 * parameters; perhaps it still can't */
1933#ifndef PERL_NO_INLINE_FUNCTIONS
1934PERL_STATIC_INLINE char*
1935Perl_SvPV_helper(pTHX_ SV *const sv, STRLEN *const lp, const U32 flags, const PL_SvPVtype type, char * (*non_trivial)(pTHX_ SV *, STRLEN * const, const U32), const bool or_null, const U32 return_flags);
1936#endif
1937
1938END_EXTERN_C
8d6d96c1 1939
cff76435
NC
1940/* This test is "is there a cached PV that we can use directly?"
1941 * We can if
1942 * a) SVf_POK is true and there's definitely no get magic on the scalar
1943 * b) SVp_POK is true, there's no get magic, and we know that the cached PV
1944 * came from an IV conversion.
1945 * For the latter case, we don't set SVf_POK so that we can distinguish whether
1946 * the value originated as a string or as an integer, before we cached the
1947 * second representation. */
1948#define SvPOK_or_cached_IV(sv) \
1949 (((SvFLAGS(sv) & (SVf_POK|SVs_GMG)) == SVf_POK) || ((SvFLAGS(sv) & (SVf_IOK|SVp_POK|SVs_GMG)) == (SVf_IOK|SVp_POK)))
1950
1ef9039b
KW
1951#define SvPV_flags(sv, len, flags) \
1952 Perl_SvPV_helper(aTHX_ sv, &len, flags, SvPVnormal_type_, \
1953 Perl_sv_2pv_flags, FALSE, 0)
1954#define SvPV_flags_const(sv, len, flags) \
1955 ((const char*) Perl_SvPV_helper(aTHX_ sv, &len, flags, SvPVnormal_type_, \
1956 Perl_sv_2pv_flags, FALSE, \
1957 SV_CONST_RETURN))
1958#define SvPV_flags_const_nolen(sv, flags) \
1959 ((const char*) Perl_SvPV_helper(aTHX_ sv, NULL, flags, SvPVnormal_type_, \
1960 Perl_sv_2pv_flags, FALSE, \
1961 SV_CONST_RETURN))
1962#define SvPV_flags_mutable(sv, len, flags) \
1963 Perl_SvPV_helper(aTHX_ sv, &len, flags, SvPVnormal_type_, \
1964 Perl_sv_2pv_flags, FALSE, SV_MUTABLE_RETURN)
1965
1966#define SvPV_nolen(sv) \
1967 Perl_SvPV_helper(aTHX_ sv, NULL, SV_GMAGIC, SvPVnormal_type_, \
1968 Perl_sv_2pv_flags, FALSE, 0)
1969
1970#define SvPV_nolen_const(sv) SvPV_flags_const_nolen(sv, SV_GMAGIC)
1971
1972#define SvPV(sv, len) SvPV_flags(sv, len, SV_GMAGIC)
1973#define SvPV_const(sv, len) SvPV_flags_const(sv, len, SV_GMAGIC)
1974#define SvPV_mutable(sv, len) SvPV_flags_mutable(sv, len, SV_GMAGIC)
1975
1976#define SvPV_nomg_nolen(sv) \
1977 Perl_SvPV_helper(aTHX_ sv, NULL, 0, SvPVnormal_type_,Perl_sv_2pv_flags, \
1978 FALSE, 0)
1979#define SvPV_nomg(sv, len) SvPV_flags(sv, len, 0)
1980#define SvPV_nomg_const(sv, len) SvPV_flags_const(sv, len, 0)
1981#define SvPV_nomg_const_nolen(sv) SvPV_flags_const_nolen(sv, 0)
1982
1983#define SvPV_force_flags(sv, len, flags) \
1984 Perl_SvPV_helper(aTHX_ sv, &len, flags, SvPVforce_type_, \
1985 Perl_sv_pvn_force_flags, FALSE, 0)
1986#define SvPV_force_flags_nolen(sv, flags) \
1987 Perl_SvPV_helper(aTHX_ sv, NULL, flags, SvPVforce_type_, \
1988 Perl_sv_pvn_force_flags, FALSE, 0)
1989#define SvPV_force_flags_mutable(sv, len, flags) \
1990 Perl_SvPV_helper(aTHX_ sv, &len, flags, SvPVforce_type_, \
1991 Perl_sv_pvn_force_flags, FALSE, SV_MUTABLE_RETURN)
1992
1993#define SvPV_force(sv, len) SvPV_force_flags(sv, len, SV_GMAGIC)
1994#define SvPV_force_nolen(sv) SvPV_force_flags_nolen(sv, SV_GMAGIC)
3629b4ec 1995#define SvPV_force_mutable(sv, len) SvPV_force_flags_mutable(sv, len, SV_GMAGIC)
baca2b92 1996
0334097d 1997/* "_nomg" in these defines means no mg_get() */
1ef9039b
KW
1998#define SvPV_force_nomg(sv, len) SvPV_force_flags(sv, len, 0)
1999#define SvPV_force_nomg_nolen(sv) SvPV_force_flags_nolen(sv, 0)
2000
2001#define SvPVutf8(sv, len) \
2002 Perl_SvPV_helper(aTHX_ sv, &len, SV_GMAGIC, SvPVutf8_type_, \
2003 Perl_sv_2pvutf8_flags, FALSE, 0)
2004#define SvPVutf8_nomg(sv, len) \
7430084e 2005 Perl_SvPV_helper(aTHX_ sv, &len, 0, SvPVutf8_type_, \
1ef9039b
KW
2006 Perl_sv_2pvutf8_flags, FALSE, 0)
2007#define SvPVutf8_nolen(sv) \
2008 Perl_SvPV_helper(aTHX_ sv, NULL, SV_GMAGIC, SvPVutf8_type_, \
2009 Perl_sv_2pvutf8_flags, FALSE, 0)
2010#define SvPVutf8_or_null(sv, len) \
2011 Perl_SvPV_helper(aTHX_ sv, &len, SV_GMAGIC, SvPVutf8_type_, \
2012 Perl_sv_2pvutf8_flags, TRUE, 0)
2013#define SvPVutf8_or_null_nomg(sv, len) \
2014 Perl_SvPV_helper(aTHX_ sv, &len, 0, SvPVutf8_type_, \
2015 Perl_sv_2pvutf8_flags, TRUE, 0)
2016
2017#define SvPVbyte(sv, len) \
2018 Perl_SvPV_helper(aTHX_ sv, &len, SV_GMAGIC, SvPVbyte_type_, \
2019 Perl_sv_2pvbyte_flags, FALSE, 0)
2020#define SvPVbyte_nomg(sv, len) \
2021 Perl_SvPV_helper(aTHX_ sv, &len, 0, SvPVbyte_type_, \
2022 Perl_sv_2pvbyte_flags, FALSE, 0)
2023#define SvPVbyte_nolen(sv) \
2024 Perl_SvPV_helper(aTHX_ sv, NULL, SV_GMAGIC, SvPVbyte_type_, \
2025 Perl_sv_2pvbyte_flags, FALSE, 0)
2026#define SvPVbyte_or_null(sv, len) \
2027 Perl_SvPV_helper(aTHX_ sv, &len, SV_GMAGIC, SvPVbyte_type_, \
2028 Perl_sv_2pvbyte_flags, TRUE, 0)
2029#define SvPVbyte_or_null_nomg(sv, len) \
2030 Perl_SvPV_helper(aTHX_ sv, &len, 0, SvPVbyte_type_, \
2031 Perl_sv_2pvbyte_flags, TRUE, 0)
2032
2033#define SvPVutf8_force(sv, len) \
2034 Perl_SvPV_helper(aTHX_ sv, &len, 0, SvPVutf8_pure_type_, \
2035 Perl_sv_pvutf8n_force_wrapper, FALSE, 0)
2036
2037#define SvPVbyte_force(sv, len) \
2038 Perl_SvPV_helper(aTHX_ sv, &len, 0, SvPVbyte_pure_type_, \
2039 Perl_sv_pvbyten_force_wrapper, FALSE, 0)
2040
2041/* define FOOx(): Before FOO(x) was inlined, these were idempotent versions of
2042 * FOO(). */
baca2b92 2043
3629b4ec
KW
2044#define SvPVx_force(sv, len) sv_pvn_force(sv, &len)
2045#define SvPVutf8x_force(sv, len) sv_pvutf8n_force(sv, &len)
2046#define SvPVbytex_force(sv, len) sv_pvbyten_force(sv, &len)
baca2b92 2047
7358a4d1 2048#define SvTRUEx(sv) SvTRUE(sv)
4eff5eb8
KW
2049#define SvTRUEx_nomg(sv) SvTRUE_nomg(sv)
2050#define SvTRUE_nomg_NN(sv) SvTRUE_common(sv, TRUE)
4bac9ae4 2051
dae8ab81
KW
2052# define SvIVx(sv) SvIV(sv)
2053# define SvUVx(sv) SvUV(sv)
2054# define SvNVx(sv) SvNV(sv)
2055
041c1a23 2056#if defined(PERL_USE_GCC_BRACE_GROUPS)
baca2b92 2057
3629b4ec
KW
2058# define SvPVx(sv, len) ({SV *_sv = (sv); SvPV(_sv, len); })
2059# define SvPVx_const(sv, len) ({SV *_sv = (sv); SvPV_const(_sv, len); })
002e4c74 2060# define SvPVx_nolen(sv) ({SV *_sv = (sv); SvPV_nolen(_sv); })
9ce348e8 2061# define SvPVx_nolen_const(sv) ({SV *_sv = (sv); SvPV_nolen_const(_sv); })
3629b4ec
KW
2062# define SvPVutf8x(sv, len) ({SV *_sv = (sv); SvPVutf8(_sv, len); })
2063# define SvPVbytex(sv, len) ({SV *_sv = (sv); SvPVbyte(_sv, len); })
002e4c74 2064# define SvPVbytex_nolen(sv) ({SV *_sv = (sv); SvPVbyte_nolen(_sv); })
baca2b92 2065
a80f87c4 2066#else /* __GNUC__ */
baca2b92 2067
a80f87c4
GS
2068/* These inlined macros use globals, which will require a thread
2069 * declaration in user code, so we avoid them under threads */
2070
3629b4ec
KW
2071# define SvPVx(sv, len) ((PL_Sv = (sv)), SvPV(PL_Sv, len))
2072# define SvPVx_const(sv, len) ((PL_Sv = (sv)), SvPV_const(PL_Sv, len))
002e4c74 2073# define SvPVx_nolen(sv) ((PL_Sv = (sv)), SvPV_nolen(PL_Sv))
5f1478c3 2074# define SvPVx_nolen_const(sv) ((PL_Sv = (sv)), SvPV_nolen_const(PL_Sv))
3629b4ec
KW
2075# define SvPVutf8x(sv, len) ((PL_Sv = (sv)), SvPVutf8(PL_Sv, len))
2076# define SvPVbytex(sv, len) ((PL_Sv = (sv)), SvPVbyte(PL_Sv, len))
002e4c74 2077# define SvPVbytex_nolen(sv) ((PL_Sv = (sv)), SvPVbyte_nolen(PL_Sv))
baca2b92
DM
2078#endif /* __GNU__ */
2079
914bb574
PE
2080#define SvIsCOW(sv) (SvFLAGS(sv) & SVf_IsCOW)
2081#define SvIsCOW_on(sv) (SvFLAGS(sv) |= SVf_IsCOW)
2082#define SvIsCOW_off(sv) (SvFLAGS(sv) &= ~(SVf_IsCOW|SVppv_STATIC))
2083#define SvIsCOW_shared_hash(sv) ((SvFLAGS(sv) & (SVf_IsCOW|SVppv_STATIC)) == (SVf_IsCOW) && SvLEN(sv) == 0)
2084#define SvIsCOW_static(sv) ((SvFLAGS(sv) & (SVf_IsCOW|SVppv_STATIC)) == (SVf_IsCOW|SVppv_STATIC))
baca2b92 2085
bdd68bc3 2086#define SvSHARED_HEK_FROM_PV(pvx) \
1f4fbd3b 2087 ((struct hek*)(pvx - STRUCT_OFFSET(struct hek, hek_key)))
fb857162
KW
2088/*
2089=for apidoc Am|struct hek*|SvSHARED_HASH|SV * sv
2090Returns the hash for C<sv> created by C<L</newSVpvn_share>>.
2091
2092=cut
2093*/
0a356b31 2094#define SvSHARED_HASH(sv) (0 + SvSHARED_HEK_FROM_PV(SvPVX_const(sv))->hek_hash)
c158a4fd 2095
baca2b92 2096/* flag values for sv_*_flags functions */
5dfbfbd5 2097#define SV_UTF8_NO_ENCODING 0 /* No longer used */
49e7c422
KW
2098
2099/*
2100=for apidoc AmnhD||SV_UTF8_NO_ENCODING
2101
2102=cut
2103*/
2104
bd90fa41
PE
2105/* Flags used as `U32 flags` arguments to various functions */
2106#define SV_IMMEDIATE_UNREF (1 << 0) /* 0x0001 - 1 */
2107#define SV_GMAGIC (1 << 1) /* 0x0002 - 2 */
2108#define SV_COW_DROP_PV (1 << 2) /* 0x0004 - 4 */
351ceb16 2109/* SV_NOT_USED (1 << 3) 0x0008 - 8 */
bd90fa41
PE
2110#define SV_NOSTEAL (1 << 4) /* 0x0010 - 16 */
2111#define SV_CONST_RETURN (1 << 5) /* 0x0020 - 32 */
2112#define SV_MUTABLE_RETURN (1 << 6) /* 0x0040 - 64 */
2113#define SV_SMAGIC (1 << 7) /* 0x0080 - 128 */
2114#define SV_HAS_TRAILING_NUL (1 << 8) /* 0x0100 - 256 */
2115#define SV_COW_SHARED_HASH_KEYS (1 << 9) /* 0x0200 - 512 */
cb23d5b1 2116/* This one is only enabled for PERL_OLD_COPY_ON_WRITE */
b7adcee4
FC
2117/* XXX This flag actually enabled for any COW. But it appears not to do
2118 anything. Can we just remove it? Or will it serve some future
2119 purpose. */
bd90fa41 2120#define SV_COW_OTHER_PVS (1 << 10) /* 0x0400 - 1024 */
9f621bb0 2121/* Make sv_2pv_flags return NULL if something is undefined. */
bd90fa41 2122#define SV_UNDEF_RETURNS_NULL (1 << 11) /* 0x0800 - 2048 */
b3ab6785
KW
2123/* Tell sv_utf8_upgrade() to not check to see if an upgrade is really needed.
2124 * This is used when the caller has already determined it is, and avoids
2125 * redundant work */
bd90fa41 2126#define SV_FORCE_UTF8_UPGRADE (1 << 12) /* 0x1000 - 4096 */
aee036bb
DM
2127/* if (after resolving magic etc), the SV is found to be overloaded,
2128 * don't call the overload magic, just return as-is */
bd90fa41
PE
2129#define SV_SKIP_OVERLOAD (1 << 13) /* 0x2000 - 8192 */
2130#define SV_CATBYTES (1 << 14) /* 0x4000 - 16384 */
2131#define SV_CATUTF8 (1 << 15) /* 0x8000 - 32768 */
34482cd6
NC
2132
2133/* The core is safe for this COW optimisation. XS code on CPAN may not be.
2134 So only default to doing the COW setup if we're in the core.
2135 */
2136#ifdef PERL_CORE
2137# ifndef SV_DO_COW_SVSETSV
cb23d5b1 2138# define SV_DO_COW_SVSETSV SV_COW_SHARED_HASH_KEYS|SV_COW_OTHER_PVS
34482cd6
NC
2139# endif
2140#endif
2141
2142#ifndef SV_DO_COW_SVSETSV
2143# define SV_DO_COW_SVSETSV 0
2144#endif
2145
765f542d 2146
5abc721d
NC
2147#define sv_unref(sv) sv_unref_flags(sv, 0)
2148#define sv_force_normal(sv) sv_force_normal_flags(sv, 0)
47518d95
NC
2149#define sv_usepvn(sv, p, l) sv_usepvn_flags(sv, p, l, 0)
2150#define sv_usepvn_mg(sv, p, l) sv_usepvn_flags(sv, p, l, SV_SMAGIC)
174c73e3 2151
2bf695be
KW
2152/*
2153=for apidoc Am|void|SV_CHECK_THINKFIRST_COW_DROP|SV * sv
2154
2155Call this when you are about to replace the PV value in C<sv>, which is
2156potentially copy-on-write. It stops any sharing with other SVs, so that no
2157Copy on Write (COW) actually happens. This COW would be useless, as it would
2158immediately get changed to something else. This function also removes any
2159other encumbrances that would be problematic when changing C<sv>.
2160
2161=cut
2162*/
765f542d
NC
2163
2164#define SV_CHECK_THINKFIRST_COW_DROP(sv) if (SvTHINKFIRST(sv)) \
1f4fbd3b 2165 sv_force_normal_flags(sv, SV_COW_DROP_PV)
46187eeb 2166
93c10d60 2167#ifdef PERL_COPY_ON_WRITE
db2c6cb3 2168# define SvCANCOW(sv) \
1f4fbd3b
MS
2169 (SvIsCOW(sv) \
2170 ? SvLEN(sv) ? CowREFCNT(sv) != SV_COW_REFCNT_MAX : 1 \
2171 : (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS \
2172 && SvCUR(sv)+1 < SvLEN(sv))
db2c6cb3
FC
2173 /* Note: To allow 256 COW "copies", a refcnt of 0 means 1. */
2174# define CowREFCNT(sv) (*(U8 *)(SvPVX(sv)+SvLEN(sv)-1))
1381ccb1 2175# define SV_COW_REFCNT_MAX nBIT_UMAX(sizeof(U8) * CHARBITS)
f7a8268c 2176# define CAN_COW_MASK (SVf_POK|SVf_ROK|SVp_POK|SVf_FAKE| \
1f4fbd3b 2177 SVf_OOK|SVf_BREAK|SVf_READONLY|SVf_PROTECT)
b7adcee4 2178#endif
ed252734 2179
ed252734
NC
2180#define CAN_COW_FLAGS (SVp_POK|SVf_POK)
2181
2bf695be
KW
2182/*
2183=for apidoc Am|void|SV_CHECK_THINKFIRST|SV * sv
2184
2185Remove any encumbrances from C<sv>, that need to be taken care of before it
2186is modifiable. For example if it is Copy on Write (COW), now is the time to
2187make that copy.
2188
2189If you know that you are about to change the PV value of C<sv>, instead use
2190L</C<SV_CHECK_THINKFIRST_COW_DROP>> to avoid the write that would be
2191immediately written again.
2192
2193=cut
2194*/
765f542d 2195#define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) \
1f4fbd3b 2196 sv_force_normal_flags(sv, 0)
765f542d
NC
2197
2198
baca2b92
DM
2199/* all these 'functions' are now just macros */
2200
2201#define sv_pv(sv) SvPV_nolen(sv)
2202#define sv_pvutf8(sv) SvPVutf8_nolen(sv)
2203#define sv_pvbyte(sv) SvPVbyte_nolen(sv)
2204
2205#define sv_pvn_force_nomg(sv, lp) sv_pvn_force_flags(sv, lp, 0)
b3ab6785 2206#define sv_utf8_upgrade_flags(sv, flags) sv_utf8_upgrade_flags_grow(sv, flags, 0)
baca2b92 2207#define sv_utf8_upgrade_nomg(sv) sv_utf8_upgrade_flags(sv, 0)
423ce623
P
2208#define sv_utf8_downgrade(sv, fail_ok) sv_utf8_downgrade_flags(sv, fail_ok, SV_GMAGIC)
2209#define sv_utf8_downgrade_nomg(sv, fail_ok) sv_utf8_downgrade_flags(sv, fail_ok, 0)
baca2b92 2210#define sv_catpvn_nomg(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, 0)
9dcc53ea 2211#define sv_catpv_nomg(dsv, sstr) sv_catpv_flags(dsv, sstr, 0)
34482cd6 2212#define sv_setsv(dsv, ssv) \
1f4fbd3b 2213 sv_setsv_flags(dsv, ssv, SV_GMAGIC|SV_DO_COW_SVSETSV)
34482cd6 2214#define sv_setsv_nomg(dsv, ssv) sv_setsv_flags(dsv, ssv, SV_DO_COW_SVSETSV)
baca2b92
DM
2215#define sv_catsv(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC)
2216#define sv_catsv_nomg(dsv, ssv) sv_catsv_flags(dsv, ssv, 0)
b347df82 2217#define sv_catsv_mg(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC|SV_SMAGIC)
baca2b92 2218#define sv_catpvn(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC)
37ee558d 2219#define sv_catpvn_mg(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC|SV_SMAGIC);
4bac9ae4
CS
2220#define sv_copypv(dsv, ssv) sv_copypv_flags(dsv, ssv, SV_GMAGIC)
2221#define sv_copypv_nomg(dsv, ssv) sv_copypv_flags(dsv, ssv, 0)
baca2b92 2222#define sv_2pv(sv, lp) sv_2pv_flags(sv, lp, SV_GMAGIC)
cb2f1b7b 2223#define sv_2pv_nolen(sv) sv_2pv(sv, 0)
757fc329 2224#define sv_2pvbyte(sv, lp) sv_2pvbyte_flags(sv, lp, SV_GMAGIC)
cb2f1b7b 2225#define sv_2pvbyte_nolen(sv) sv_2pvbyte(sv, 0)
757fc329 2226#define sv_2pvutf8(sv, lp) sv_2pvutf8_flags(sv, lp, SV_GMAGIC)
cb2f1b7b 2227#define sv_2pvutf8_nolen(sv) sv_2pvutf8(sv, 0)
baca2b92
DM
2228#define sv_2pv_nomg(sv, lp) sv_2pv_flags(sv, lp, 0)
2229#define sv_pvn_force(sv, lp) sv_pvn_force_flags(sv, lp, SV_GMAGIC)
2230#define sv_utf8_upgrade(sv) sv_utf8_upgrade_flags(sv, SV_GMAGIC)
891f9566
YST
2231#define sv_2iv(sv) sv_2iv_flags(sv, SV_GMAGIC)
2232#define sv_2uv(sv) sv_2uv_flags(sv, SV_GMAGIC)
39d5de13 2233#define sv_2nv(sv) sv_2nv_flags(sv, SV_GMAGIC)
078504b2 2234#define sv_eq(sv1, sv2) sv_eq_flags(sv1, sv2, SV_GMAGIC)
e8c9c9d4
NC
2235#define sv_cmp(sv1, sv2) sv_cmp_flags(sv1, sv2, SV_GMAGIC)
2236#define sv_cmp_locale(sv1, sv2) sv_cmp_locale_flags(sv1, sv2, SV_GMAGIC)
1dd43bce 2237#define sv_numeq(sv1, sv2) sv_numeq_flags(sv1, sv2, SV_GMAGIC)
e269455b 2238#define sv_streq(sv1, sv2) sv_streq_flags(sv1, sv2, SV_GMAGIC)
e17dadf3 2239#define sv_collxfrm(sv, nxp) sv_collxfrm_flags(sv, nxp, SV_GMAGIC)
06c841cf 2240#define sv_2bool(sv) sv_2bool_flags(sv, SV_GMAGIC)
4bac9ae4 2241#define sv_2bool_nomg(sv) sv_2bool_flags(sv, 0)
84335ee9 2242#define sv_insert(bigstr, offset, len, little, littlelen) \
1f4fbd3b
MS
2243 Perl_sv_insert_flags(aTHX_ (bigstr),(offset), (len), (little), \
2244 (littlelen), SV_GMAGIC)
108cb980 2245#define sv_mortalcopy(sv) \
1f4fbd3b 2246 Perl_sv_mortalcopy_flags(aTHX_ sv, SV_GMAGIC|SV_DO_COW_SVSETSV)
f34d8cdd 2247#define sv_cathek(sv,hek) \
1f4fbd3b
MS
2248 STMT_START { \
2249 HEK * const bmxk = hek; \
2250 sv_catpvn_flags(sv, HEK_KEY(bmxk), HEK_LEN(bmxk), \
2251 HEK_UTF8(bmxk) ? SV_CATUTF8 : SV_CATBYTES); \
2252 } STMT_END
baca2b92 2253
db79b45b 2254/* Should be named SvCatPVN_utf8_upgrade? */
4bac9ae4 2255#define sv_catpvn_nomg_utf8_upgrade(dsv, sstr, slen, nsv) \
1f4fbd3b
MS
2256 STMT_START { \
2257 if (!(nsv)) \
2258 nsv = newSVpvn_flags(sstr, slen, SVs_TEMP); \
2259 else \
2260 sv_setpvn(nsv, sstr, slen); \
2261 SvUTF8_off(nsv); \
2262 sv_utf8_upgrade(nsv); \
2263 sv_catsv_nomg(dsv, nsv); \
2264 } STMT_END
bcef955a
KW
2265#define sv_catpvn_nomg_maybeutf8(dsv, sstr, len, is_utf8) \
2266 sv_catpvn_flags(dsv, sstr, len, (is_utf8)?SV_CATUTF8:SV_CATBYTES)
79072805 2267
25fdce4a 2268#if defined(PERL_CORE) || defined(PERL_EXT)
aec43834
FC
2269# define sv_or_pv_len_utf8(sv, pv, bytelen) \
2270 (SvGAMAGIC(sv) \
1f4fbd3b
MS
2271 ? utf8_length((U8 *)(pv), (U8 *)(pv)+(bytelen)) \
2272 : sv_len_utf8(sv))
aec43834
FC
2273#endif
2274
954c1994 2275/*
ac950c89
KW
2276=for apidoc newRV
2277=for apidoc_item ||newRV_inc|
954c1994 2278
ac950c89
KW
2279These are identical. They create an RV wrapper for an SV. The reference count
2280for the original SV is incremented.
954c1994
GS
2281
2282=cut
2283*/
2284
5f05dabc 2285#define newRV_inc(sv) newRV(sv)
5f05dabc 2286
796b6530 2287/* the following macros update any magic values this C<sv> is associated with */
79072805 2288
954c1994 2289/*
574e093e 2290=for apidoc_section $SV
ccfc67b7 2291
954c1994 2292=for apidoc Am|void|SvSETMAGIC|SV* sv
8890fded 2293Invokes C<L</mg_set>> on an SV if it has 'set' magic. This is necessary
f7e029ab
FC
2294after modifying a scalar, in case it is a magical variable like C<$|>
2295or a tied variable (it calls C<STORE>). This macro evaluates its
954c1994
GS
2296argument more than once.
2297
1607e393
KW
2298=for apidoc Am|void|SvSetMagicSV|SV* dsv|SV* ssv
2299=for apidoc_item SvSetMagicSV_nosteal
2300=for apidoc_item SvSetSV
2301=for apidoc_item SvSetSV_nosteal
574e093e
KW
2302
2303if C<dsv> is the same as C<ssv>, these do nothing. Otherwise they all call
2304some form of C<L</sv_setsv>>. They may evaluate their arguments more than
2305once.
954c1994 2306
574e093e 2307The only differences are:
954c1994 2308
574e093e
KW
2309C<SvSetMagicSV> and C<SvSetMagicSV_nosteal> perform any required 'set' magic
2310afterwards on the destination SV; C<SvSetSV> and C<SvSetSV_nosteal> do not.
645c22ef 2311
574e093e
KW
2312C<SvSetSV_nosteal> C<SvSetMagicSV_nosteal> call a non-destructive version of
2313C<sv_setsv>.
645c22ef 2314
68795e93 2315=for apidoc Am|void|SvSHARE|SV* sv
796b6530 2316Arranges for C<sv> to be shared between threads if a suitable module
68795e93
NIS
2317has been loaded.
2318
2319=for apidoc Am|void|SvLOCK|SV* sv
796b6530 2320Arranges for a mutual exclusion lock to be obtained on C<sv> if a suitable module
68795e93
NIS
2321has been loaded.
2322
2323=for apidoc Am|void|SvUNLOCK|SV* sv
796b6530 2324Releases a mutual exclusion lock on C<sv> if a suitable module
68795e93
NIS
2325has been loaded.
2326
3f620621 2327=for apidoc_section $SV
ccfc67b7 2328
679ac26e 2329=for apidoc Am|char *|SvGROW|SV* sv|STRLEN len
954c1994
GS
2330Expands the character buffer in the SV so that it has room for the
2331indicated number of bytes (remember to reserve space for an extra trailing
6602b933 2332C<NUL> character). Calls C<sv_grow> to perform the expansion if necessary.
72d33970 2333Returns a pointer to the character
796b6530 2334buffer. SV must be of type >= C<SVt_PV>. One
4b1e7f06 2335alternative is to call C<sv_grow> if you are not sure of the type of SV.
954c1994 2336
d8732c60
KW
2337You might mistakenly think that C<len> is the number of bytes to add to the
2338existing size, but instead it is the total size C<sv> should be.
2339
45661033
YO
2340=for apidoc Am|char *|SvPVCLEAR|SV* sv
2341Ensures that sv is a SVt_PV and that its SvCUR is 0, and that it is
2342properly null terminated. Equivalent to sv_setpvs(""), but more efficient.
2343
90dc1489
RL
2344=for apidoc Am|char *|SvPVCLEAR_FRESH|SV* sv
2345
2346Like SvPVCLEAR, but optimized for newly-minted SVt_PV/PVIV/PVNV/PVMG
2347that already have a PV buffer allocated, but no SvTHINKFIRST.
2348
954c1994
GS
2349=cut
2350*/
2351
45661033 2352#define SvPVCLEAR(sv) sv_setpv_bufsize(sv,0,0)
b6198bcc 2353#define SvPVCLEAR_FRESH(sv) sv_setpv_freshbuf(sv)
16c91539
BM
2354#define SvSHARE(sv) PL_sharehook(aTHX_ sv)
2355#define SvLOCK(sv) PL_lockhook(aTHX_ sv)
2356#define SvUNLOCK(sv) PL_unlockhook(aTHX_ sv)
2357#define SvDESTROYABLE(sv) PL_destroyhook(aTHX_ sv)
68795e93 2358
5d9574c1 2359#define SvSETMAGIC(x) STMT_START { if (UNLIKELY(SvSMAGICAL(x))) mg_set(x); } STMT_END
79072805 2360
54310121 2361#define SvSetSV_and(dst,src,finally) \
1f4fbd3b 2362 STMT_START { \
c1bee391
KW
2363 SV * src_ = src; \
2364 SV * dst_ = dst; \
2365 if (LIKELY((dst_) != (src_))) { \
2366 sv_setsv(dst_, src_); \
1f4fbd3b
MS
2367 finally; \
2368 } \
2369 } STMT_END
c1bee391 2370
54310121 2371#define SvSetSV_nosteal_and(dst,src,finally) \
1f4fbd3b 2372 STMT_START { \
c1bee391
KW
2373 SV * src_ = src; \
2374 SV * dst_ = dst; \
2375 if (LIKELY((dst_) != (src_))) { \
2376 sv_setsv_flags(dst_, src_, \
2377 SV_GMAGIC \
2378 | SV_NOSTEAL \
2379 | SV_DO_COW_SVSETSV); \
1f4fbd3b
MS
2380 finally; \
2381 } \
2382 } STMT_END
79072805 2383
54310121 2384#define SvSetSV(dst,src) \
1f4fbd3b 2385 SvSetSV_and(dst,src,/*nothing*/;)
54310121 2386#define SvSetSV_nosteal(dst,src) \
1f4fbd3b 2387 SvSetSV_nosteal_and(dst,src,/*nothing*/;)
54310121 2388
2389#define SvSetMagicSV(dst,src) \
1f4fbd3b 2390 SvSetSV_and(dst,src,SvSETMAGIC(dst))
54310121 2391#define SvSetMagicSV_nosteal(dst,src) \
1f4fbd3b 2392 SvSetSV_nosteal_and(dst,src,SvSETMAGIC(dst))
54310121 2393
db79b45b 2394
1045810a 2395#if !defined(SKIP_DEBUGGING)
79072805 2396#define SvPEEK(sv) sv_peek(sv)
3967c732
JD
2397#else
2398#define SvPEEK(sv) ""
2399#endif
79072805 2400
7c123f9d
DM
2401/* Is this a per-interpreter immortal SV (rather than global)?
2402 * These should either occupy adjacent entries in the interpreter struct
2403 * (MULTIPLICITY) or adjacent elements of PL_sv_immortals[] otherwise.
2404 * The unsigned (Size_t) cast avoids the need for a second < 0 condition.
2405 */
2406#define SvIMMORTAL_INTERP(sv) ((Size_t)((sv) - &PL_sv_yes) < 4)
2407
2408/* Does this immortal have a true value? Currently only PL_sv_yes does. */
2409#define SvIMMORTAL_TRUE(sv) ((sv) == &PL_sv_yes)
2410
2411/* the SvREADONLY() test is to quickly reject most SVs */
2412#define SvIMMORTAL(sv) \
2413 ( SvREADONLY(sv) \
2414 && (SvIMMORTAL_INTERP(sv) || (sv) == &PL_sv_placeholder))
36477c24 2415
fe54beba
DM
2416#ifdef DEBUGGING
2417 /* exercise the immortal resurrection code in sv_free2() */
efc99d51
DM
2418# ifdef PERL_RC_STACK
2419 /* When the stack is ref-counted, the code tends to take a lot of
2420 * short cuts with immortals, such as skipping the bump of the ref
2421 * count of PL_sv_undef when pushing it on the stack. Exercise that
2422 * this doesn't cause problems, especially on code which
2423 * special-cases RC==1 etc.
2424 */
2425# define SvREFCNT_IMMORTAL 10
2426# else
2427# define SvREFCNT_IMMORTAL 1000
2428# endif
fe54beba
DM
2429#else
2430# define SvREFCNT_IMMORTAL ((~(U32)0)/2)
2431#endif
2432
567f3b56
TC
2433/*
2434=for apidoc Am|SV *|boolSV|bool b
2435
2436Returns a true SV if C<b> is a true value, or a false SV if C<b> is 0.
2437
fbe13c60 2438See also C<L</PL_sv_yes>> and C<L</PL_sv_no>>.
567f3b56
TC
2439
2440=cut
2441*/
2442
3280af22 2443#define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
54310121 2444
1d0d673f
PE
2445/*
2446=for apidoc Am|void|sv_setbool|SV *sv|bool b
2447=for apidoc_item |void|sv_setbool_mg|SV *sv|bool b
2448
2449These set an SV to a true or false boolean value, upgrading first if necessary.
2450
2451They differ only in that C<sv_setbool_mg> handles 'set' magic; C<sv_setbool>
2452does not.
2453
2454=cut
2455*/
2456
2457#define sv_setbool(sv, b) sv_setsv(sv, boolSV(b))
2458#define sv_setbool_mg(sv, b) sv_setsv_mg(sv, boolSV(b))
2459
79072805 2460#define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
f7877b28
NC
2461/* If I give every macro argument a different name, then there won't be bugs
2462 where nested macros get confused. Been there, done that. */
1d31276d
KW
2463/*
2464=for apidoc Am|bool|isGV_with_GP|SV * sv
2465Returns a boolean as to whether or not C<sv> is a GV with a pointer to a GP
2466(glob pointer).
2467
2468=cut
2469*/
f7877b28 2470#define isGV_with_GP(pwadak) \
1f4fbd3b
MS
2471 (((SvFLAGS(pwadak) & (SVp_POK|SVpgv_GP)) == SVpgv_GP) \
2472 && (SvTYPE(pwadak) == SVt_PVGV || SvTYPE(pwadak) == SVt_PVLV))
41bd242f
KW
2473
2474#define isGV_with_GP_on(sv) \
2475 STMT_START { \
2476 SV * sv_ = MUTABLE_SV(sv); \
2477 assert (SvTYPE(sv_) == SVt_PVGV || SvTYPE(sv_) == SVt_PVLV); \
2478 assert (!SvPOKp(sv_)); \
2479 assert (!SvIOKp(sv_)); \
2480 (SvFLAGS(sv_) |= SVpgv_GP); \
2e5b91de 2481 } STMT_END
41bd242f
KW
2482
2483#define isGV_with_GP_off(sv) \
2484 STMT_START { \
2485 SV * sv_ = MUTABLE_SV(sv); \
2486 assert (SvTYPE(sv_) == SVt_PVGV || SvTYPE(sv_) == SVt_PVLV); \
2487 assert (!SvPOKp(sv_)); \
2488 assert (!SvIOKp(sv_)); \
2489 (SvFLAGS(sv_) &= ~SVpgv_GP); \
2e5b91de 2490 } STMT_END
41bd242f 2491
f5b4df4d
FC
2492#ifdef PERL_CORE
2493# define isGV_or_RVCV(kadawp) \
2494 (isGV(kadawp) || (SvROK(kadawp) && SvTYPE(SvRV(kadawp)) == SVt_PVCV))
2495#endif
8d919b0a
FC
2496#define isREGEXP(sv) \
2497 (SvTYPE(sv) == SVt_REGEXP \
df6b4bd5 2498 || (SvFLAGS(sv) & (SVTYPEMASK|SVpgv_GP|SVf_FAKE)) \
1f4fbd3b 2499 == (SVt_PVLV|SVf_FAKE))
2e5b91de 2500
79072805 2501
bba82a09 2502#ifdef PERL_ANY_COW
db2c6cb3 2503# define SvGROW(sv,len) \
1f4fbd3b 2504 (SvIsCOW(sv) || SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
db2c6cb3 2505#else
6bbd724f 2506# define SvGROW(sv,len) (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
db2c6cb3 2507#endif
6bbd724f
DM
2508#define SvGROW_mutable(sv,len) \
2509 (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX_mutable(sv))
933fea7f 2510#define Sv_Grow sv_grow
3d35f11b 2511
a0739874
DM
2512#define CLONEf_COPY_STACKS 1
2513#define CLONEf_KEEP_PTR_TABLE 2
c43294b8 2514#define CLONEf_CLONE_HOST 4
0405e91e 2515#define CLONEf_JOIN_IN 8
a0739874 2516
8cf8f3d1 2517struct clone_params {
d2d73c3e
AB
2518 AV* stashes;
2519 UV flags;
59b40662 2520 PerlInterpreter *proto_perl;
1db366cc 2521 PerlInterpreter *new_perl;
d08d57ef 2522 AV *unreferenced;
8cf8f3d1 2523};
102d3b8a 2524
238f2c13
P
2525/* SV_NOSTEAL prevents TEMP buffers being, well, stolen, and saves games
2526 with SvTEMP_off and SvTEMP_on round a call to sv_setsv. */
2527#define newSVsv(sv) newSVsv_flags((sv), SV_GMAGIC|SV_NOSTEAL)
2528#define newSVsv_nomg(sv) newSVsv_flags((sv), SV_NOSTEAL)
2529
102d3b8a 2530/*
d106cb46 2531=for apidoc Am|SV*|newSVpvn_utf8|const char* s|STRLEN len|U32 utf8
740cce10 2532
30a15352 2533Creates a new SV and copies a string (which may contain C<NUL> (C<\0>)
796b6530 2534characters) into it. If C<utf8> is true, calls
740cce10
NC
2535C<SvUTF8_on> on the new SV. Implemented as a wrapper around C<newSVpvn_flags>.
2536
2537=cut
2538*/
2539
2540#define newSVpvn_utf8(s, len, u) newSVpvn_flags((s), (len), (u) ? SVf_UTF8 : 0)
2541
2542/*
35e035cc
FC
2543=for apidoc Amx|SV*|newSVpadname|PADNAME *pn
2544
0f94cb1f 2545Creates a new SV containing the pad name.
35e035cc
FC
2546
2547=cut
2548*/
2549
0f94cb1f 2550#define newSVpadname(pn) newSVpvn_utf8(PadnamePV(pn), PadnameLEN(pn), TRUE)
35e035cc
FC
2551
2552/*
d106cb46 2553=for apidoc Am|void|SvOOK_offset|SV*sv|STRLEN len
69240efd 2554
796b6530 2555Reads into C<len> the offset from C<SvPVX> back to the true start of the
69240efd 2556allocated buffer, which will be non-zero if C<sv_chop> has been used to
8554e7a0 2557efficiently remove characters from start of the buffer. Implemented as a
2d7f6611
KW
2558macro, which takes the address of C<len>, which must be of type C<STRLEN>.
2559Evaluates C<sv> more than once. Sets C<len> to 0 if C<SvOOK(sv)> is false.
69240efd
NC
2560
2561=cut
2562*/
2563
2564#ifdef DEBUGGING
2565/* Does the bot know something I don't?
256610:28 <@Nicholas> metabatman
256710:28 <+meta> Nicholas: crash
2568*/
2569# define SvOOK_offset(sv, offset) STMT_START { \
1f4fbd3b
MS
2570 STATIC_ASSERT_STMT(sizeof(offset) == sizeof(STRLEN)); \
2571 if (SvOOK(sv)) { \
2572 const U8 *_crash = (U8*)SvPVX_const(sv); \
2573 (offset) = *--_crash; \
2574 if (!(offset)) { \
2575 _crash -= sizeof(STRLEN); \
2576 Copy(_crash, (U8 *)&(offset), sizeof(STRLEN), U8); \
2577 } \
2578 { \
2579 /* Validate the preceding buffer's sentinels to \
2580 verify that no-one is using it. */ \
2581 const U8 *const _bonk = (U8*)SvPVX_const(sv) - (offset);\
2582 while (_crash > _bonk) { \
2583 --_crash; \
2584 assert (*_crash == (U8)PTR2UV(_crash)); \
2585 } \
2586 } \
2587 } else { \
2588 (offset) = 0; \
2589 } \
69240efd
NC
2590 } STMT_END
2591#else
2592 /* This is the same code, but avoids using any temporary variables: */
2593# define SvOOK_offset(sv, offset) STMT_START { \
1f4fbd3b
MS
2594 STATIC_ASSERT_STMT(sizeof(offset) == sizeof(STRLEN)); \
2595 if (SvOOK(sv)) { \
2596 (offset) = ((U8*)SvPVX_const(sv))[-1]; \
2597 if (!(offset)) { \
2598 Copy(SvPVX_const(sv) - 1 - sizeof(STRLEN), \
2599 (U8*)&(offset), sizeof(STRLEN), U8); \
2600 } \
2601 } else { \
2602 (offset) = 0; \
2603 } \
69240efd
NC
2604 } STMT_END
2605#endif
85dca89a 2606
9287e2bf 2607/*
0c8e4929 2608=for apidoc_section $io
9287e2bf
KW
2609=for apidoc newIO
2610
2611Create a new IO, setting the reference count to 1.
2612
2613=cut
2614*/
85dca89a
NC
2615#define newIO() MUTABLE_IO(newSV_type(SVt_PVIO))
2616
d5722fbc
KW
2617#if defined(PERL_CORE) || defined(PERL_EXT)
2618
2619# define SV_CONST(name) \
1f4fbd3b
MS
2620 PL_sv_consts[SV_CONST_##name] \
2621 ? PL_sv_consts[SV_CONST_##name] \
2622 : (PL_sv_consts[SV_CONST_##name] = newSVpv_share(#name, 0))
a38ab475 2623
d5722fbc
KW
2624# define SV_CONST_TIESCALAR 0
2625# define SV_CONST_TIEARRAY 1
2626# define SV_CONST_TIEHASH 2
2627# define SV_CONST_TIEHANDLE 3
2628
2629# define SV_CONST_FETCH 4
2630# define SV_CONST_FETCHSIZE 5
2631# define SV_CONST_STORE 6
2632# define SV_CONST_STORESIZE 7
2633# define SV_CONST_EXISTS 8
2634
2635# define SV_CONST_PUSH 9
2636# define SV_CONST_POP 10
2637# define SV_CONST_SHIFT 11
2638# define SV_CONST_UNSHIFT 12
2639# define SV_CONST_SPLICE 13
2640# define SV_CONST_EXTEND 14
2641
2642# define SV_CONST_FIRSTKEY 15
2643# define SV_CONST_NEXTKEY 16
2644# define SV_CONST_SCALAR 17
2645
2646# define SV_CONST_OPEN 18
2647# define SV_CONST_WRITE 19
2648# define SV_CONST_PRINT 20
2649# define SV_CONST_PRINTF 21
2650# define SV_CONST_READ 22
2651# define SV_CONST_READLINE 23
2652# define SV_CONST_GETC 24
2653# define SV_CONST_SEEK 25
2654# define SV_CONST_TELL 26
2655# define SV_CONST_EOF 27
2656# define SV_CONST_BINMODE 28
2657# define SV_CONST_FILENO 29
2658# define SV_CONST_CLOSE 30
2659
2660# define SV_CONST_DELETE 31
2661# define SV_CONST_CLEAR 32
2662# define SV_CONST_UNTIE 33
2663# define SV_CONST_DESTROY 34
2664#endif
a38ab475
RZ
2665
2666#define SV_CONSTS_COUNT 35
2667
69240efd 2668/*
82fce3d0
DM
2669 * Bodyless IVs and NVs!
2670 *
2671 * Since 5.9.2, we can avoid allocating a body for SVt_IV-type SVs.
2672 * Since the larger IV-holding variants of SVs store their integer
2673 * values in their respective bodies, the family of SvIV() accessor
2674 * macros would naively have to branch on the SV type to find the
2675 * integer value either in the HEAD or BODY. In order to avoid this
2676 * expensive branch, a clever soul has deployed a great hack:
2677 * We set up the SvANY pointer such that instead of pointing to a
2678 * real body, it points into the memory before the location of the
2679 * head. We compute this pointer such that the location of
2680 * the integer member of the hypothetical body struct happens to
2681 * be the same as the location of the integer member of the bodyless
2682 * SV head. This now means that the SvIV() family of accessors can
2683 * always read from the (hypothetical or real) body via SvANY.
2684 *
2685 * Since the 5.21 dev series, we employ the same trick for NVs
2686 * if the architecture can support it (NVSIZE <= IVSIZE).
2687 */
2688
2689/* The following two macros compute the necessary offsets for the above
2690 * trick and store them in SvANY for SvIV() (and friends) to use. */
2691
82fce3d0 2692# define SET_SVANY_FOR_BODYLESS_IV(sv) \
fecef291
KW
2693 STMT_START { \
2694 SV * sv_ = MUTABLE_SV(sv); \
2695 SvANY(sv_) = (XPVIV*)((char*)&(sv_->sv_u.svu_iv) \
2696 - STRUCT_OFFSET(XPVIV, xiv_iv)); \
2697 } STMT_END
82fce3d0
DM
2698
2699# define SET_SVANY_FOR_BODYLESS_NV(sv) \
fecef291
KW
2700 STMT_START { \
2701 SV * sv_ = MUTABLE_SV(sv); \
2702 SvANY(sv_) = (XPVNV*)((char*)&(sv_->sv_u.svu_nv) \
2703 - STRUCT_OFFSET(XPVNV, xnv_u.xnv_nv)); \
2704 } STMT_END
82fce3d0 2705
45826d9c
PE
2706#if defined(PERL_CORE) && defined(USE_ITHREADS)
2707/* Certain cases in Perl_ss_dup have been merged, by relying on the fact
2708 that currently av_dup, gv_dup and hv_dup are the same as sv_dup.
2709 If this changes, please unmerge ss_dup.
2710 Likewise, sv_dup_inc_multiple() relies on this fact. */
2711# define sv_dup_inc_NN(s,t) SvREFCNT_inc_NN(sv_dup_inc(s,t))
2712# define av_dup(s,t) MUTABLE_AV(sv_dup((const SV *)s,t))
2713# define av_dup_inc(s,t) MUTABLE_AV(sv_dup_inc((const SV *)s,t))
2714# define hv_dup(s,t) MUTABLE_HV(sv_dup((const SV *)s,t))
2715# define hv_dup_inc(s,t) MUTABLE_HV(sv_dup_inc((const SV *)s,t))
2716# define cv_dup(s,t) MUTABLE_CV(sv_dup((const SV *)s,t))
2717# define cv_dup_inc(s,t) MUTABLE_CV(sv_dup_inc((const SV *)s,t))
2718# define io_dup(s,t) MUTABLE_IO(sv_dup((const SV *)s,t))
2719# define io_dup_inc(s,t) MUTABLE_IO(sv_dup_inc((const SV *)s,t))
2720# define gv_dup(s,t) MUTABLE_GV(sv_dup((const SV *)s,t))
2721# define gv_dup_inc(s,t) MUTABLE_GV(sv_dup_inc((const SV *)s,t))
2722#endif
2723
82fce3d0 2724/*
14d04a33 2725 * ex: set ts=8 sts=4 sw=4 et:
102d3b8a 2726 */