This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate:
[perl5.git] / sv.h
CommitLineData
a0d0e21e 1/* sv.h
79072805 2 *
e6906430 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
fc64cf4d 4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006 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/*
ccfc67b7
JH
16=head1 SV Flags
17
954c1994 18=for apidoc AmU||svtype
8cf8f3d1 19An enum of flags for Perl types. These are found in the file B<sv.h>
954c1994
GS
20in the C<svtype> enum. Test these flags with the C<SvTYPE> macro.
21
22=for apidoc AmU||SVt_PV
23Pointer type flag for scalars. See C<svtype>.
24
25=for apidoc AmU||SVt_IV
26Integer type flag for scalars. See C<svtype>.
27
28=for apidoc AmU||SVt_NV
29Double type flag for scalars. See C<svtype>.
30
31=for apidoc AmU||SVt_PVMG
32Type flag for blessed scalars. See C<svtype>.
33
34=for apidoc AmU||SVt_PVAV
35Type flag for arrays. See C<svtype>.
36
37=for apidoc AmU||SVt_PVHV
38Type flag for hashes. See C<svtype>.
39
40=for apidoc AmU||SVt_PVCV
41Type flag for code refs. See C<svtype>.
42
43=cut
44*/
45
79072805 46typedef enum {
a0d0e21e
LW
47 SVt_NULL, /* 0 */
48 SVt_IV, /* 1 */
49 SVt_NV, /* 2 */
50 SVt_RV, /* 3 */
51 SVt_PV, /* 4 */
52 SVt_PVIV, /* 5 */
53 SVt_PVNV, /* 6 */
54 SVt_PVMG, /* 7 */
55 SVt_PVBM, /* 8 */
56 SVt_PVLV, /* 9 */
57 SVt_PVAV, /* 10 */
58 SVt_PVHV, /* 11 */
59 SVt_PVCV, /* 12 */
60 SVt_PVGV, /* 13 */
61 SVt_PVFM, /* 14 */
47e6fc1e
JC
62 SVt_PVIO, /* 15 */
63 SVt_LAST /* keep last in enum. used to size arrays */
79072805
LW
64} svtype;
65
47e6fc1e
JC
66#ifdef PERL_IN_SV_C
67#define PTE_SVSLOT SVt_LAST
68#endif
69#if defined(PERL_IN_HV_C) || defined(PERL_IN_XS_APITEST)
70#define HE_SVSLOT (SVt_LAST + 1)
71#endif
72#define PERL_ARENA_ROOTS_SIZE (SVt_LAST + 2)
73
79072805
LW
74/* Using C's structural equivalence to help emulate C++ inheritance here... */
75
5e045b90 76struct STRUCT_SV { /* struct sv { */
463ee0b2 77 void* sv_any; /* pointer to something */
79072805 78 U32 sv_refcnt; /* how many references to us */
8990e307 79 U32 sv_flags; /* what we are */
79072805
LW
80};
81
82struct gv {
463ee0b2 83 XPVGV* sv_any; /* pointer to something */
79072805 84 U32 sv_refcnt; /* how many references to us */
8990e307 85 U32 sv_flags; /* what we are */
79072805
LW
86};
87
88struct cv {
232e078e 89 XPVCV* sv_any; /* pointer to something */
79072805 90 U32 sv_refcnt; /* how many references to us */
8990e307 91 U32 sv_flags; /* what we are */
79072805
LW
92};
93
94struct av {
463ee0b2 95 XPVAV* sv_any; /* pointer to something */
79072805 96 U32 sv_refcnt; /* how many references to us */
8990e307 97 U32 sv_flags; /* what we are */
79072805
LW
98};
99
100struct hv {
463ee0b2 101 XPVHV* sv_any; /* pointer to something */
79072805 102 U32 sv_refcnt; /* how many references to us */
8990e307
LW
103 U32 sv_flags; /* what we are */
104};
105
106struct io {
107 XPVIO* sv_any; /* pointer to something */
108 U32 sv_refcnt; /* how many references to us */
109 U32 sv_flags; /* what we are */
79072805
LW
110};
111
954c1994 112/*
ccfc67b7
JH
113=head1 SV Manipulation Functions
114
954c1994
GS
115=for apidoc Am|U32|SvREFCNT|SV* sv
116Returns the value of the object's reference count.
117
118=for apidoc Am|SV*|SvREFCNT_inc|SV* sv
119Increments the reference count of the given SV.
120
121=for apidoc Am|void|SvREFCNT_dec|SV* sv
122Decrements the reference count of the given SV.
123
124=for apidoc Am|svtype|SvTYPE|SV* sv
125Returns the type of the SV. See C<svtype>.
126
127=for apidoc Am|void|SvUPGRADE|SV* sv|svtype type
128Used to upgrade an SV to a more complex form. Uses C<sv_upgrade> to
129perform the upgrade if necessary. See C<svtype>.
130
131=cut
132*/
133
463ee0b2 134#define SvANY(sv) (sv)->sv_any
79072805 135#define SvFLAGS(sv) (sv)->sv_flags
aeea060c 136#define SvREFCNT(sv) (sv)->sv_refcnt
a863c7d1 137
4d1ff10f 138#ifdef USE_5005THREADS
dce16143 139
3d35f11b
GS
140# if defined(VMS)
141# define ATOMIC_INC(count) __ATOMIC_INCREMENT_LONG(&count)
142# define ATOMIC_DEC_AND_TEST(res,count) res=(1==__ATOMIC_DECREMENT_LONG(&count))
143 # else
144# ifdef EMULATE_ATOMIC_REFCOUNTS
145 # define ATOMIC_INC(count) STMT_START { \
146 MUTEX_LOCK(&PL_svref_mutex); \
147 ++count; \
148 MUTEX_UNLOCK(&PL_svref_mutex); \
149 } STMT_END
150# define ATOMIC_DEC_AND_TEST(res,count) STMT_START { \
151 MUTEX_LOCK(&PL_svref_mutex); \
152 res = (--count == 0); \
153 MUTEX_UNLOCK(&PL_svref_mutex); \
154 } STMT_END
155# else
156# define ATOMIC_INC(count) atomic_inc(&count)
157# define ATOMIC_DEC_AND_TEST(res,count) (res = atomic_dec_and_test(&count))
158# endif /* EMULATE_ATOMIC_REFCOUNTS */
159# endif /* VMS */
dce16143
MB
160#else
161# define ATOMIC_INC(count) (++count)
83b0a010 162# define ATOMIC_DEC_AND_TEST(res, count) (res = (--count == 0))
4d1ff10f 163#endif /* USE_5005THREADS */
dce16143 164
5b7ea690 165#if defined(__GNUC__) && !defined(__STRICT_ANSI__) && !defined(PERL_GCC_PEDANTIC)
dce16143
MB
166# define SvREFCNT_inc(sv) \
167 ({ \
8c18bf38 168 SV * const _sv = (SV*)(sv); \
f123e731
NC
169 if (_sv) \
170 ATOMIC_INC(SvREFCNT(_sv)); \
171 _sv; \
dce16143 172 })
8990e307 173#else
9ede5bc8 174# ifdef USE_5005THREADS
3d35f11b
GS
175# if defined(VMS) && defined(__ALPHA)
176# define SvREFCNT_inc(sv) \
177 (PL_Sv=(SV*)(sv), (PL_Sv && __ATOMIC_INCREMENT_LONG(&(SvREFCNT(PL_Sv)))), (SV *)PL_Sv)
178# else
179# define SvREFCNT_inc(sv) sv_newref((SV*)sv)
180# endif
a863c7d1 181# else
dce16143 182# define SvREFCNT_inc(sv) \
6b88bc9c 183 ((PL_Sv=(SV*)(sv)), (PL_Sv && ATOMIC_INC(SvREFCNT(PL_Sv))), (SV*)PL_Sv)
a863c7d1 184# endif
8990e307
LW
185#endif
186
91f3b821 187#define SvREFCNT_dec(sv) sv_free((SV*)(sv))
a863c7d1 188
8990e307
LW
189#define SVTYPEMASK 0xff
190#define SvTYPE(sv) ((sv)->sv_flags & SVTYPEMASK)
79072805
LW
191
192#define SvUPGRADE(sv, mt) (SvTYPE(sv) >= mt || sv_upgrade(sv, mt))
193
8990e307
LW
194#define SVs_PADBUSY 0x00000100 /* reserved for tmp or my already */
195#define SVs_PADTMP 0x00000200 /* in use as tmp */
196#define SVs_PADMY 0x00000400 /* in use a "my" variable */
197#define SVs_TEMP 0x00000800 /* string is stealable? */
198#define SVs_OBJECT 0x00001000 /* is "blessed" */
199#define SVs_GMG 0x00002000 /* has magical get method */
200#define SVs_SMG 0x00004000 /* has magical set method */
201#define SVs_RMG 0x00008000 /* has random magical methods */
202
203#define SVf_IOK 0x00010000 /* has valid public integer value */
204#define SVf_NOK 0x00020000 /* has valid public numeric value */
205#define SVf_POK 0x00040000 /* has valid public pointer value */
206#define SVf_ROK 0x00080000 /* has a valid reference pointer */
a0d0e21e 207
748a9306 208#define SVf_FAKE 0x00100000 /* glob or lexical is just a copy */
8990e307 209#define SVf_OOK 0x00200000 /* has valid offset value */
645c22ef
DM
210#define SVf_BREAK 0x00400000 /* refcnt is artificially low - used
211 * by SV's in final arena cleanup */
8990e307
LW
212#define SVf_READONLY 0x00800000 /* may not be modified */
213
a0d0e21e 214
8990e307
LW
215#define SVp_IOK 0x01000000 /* has valid non-public integer value */
216#define SVp_NOK 0x02000000 /* has valid non-public numeric value */
217#define SVp_POK 0x04000000 /* has valid non-public pointer value */
218#define SVp_SCREAM 0x08000000 /* has been studied? */
219
5b7ea690 220#define SVf_UTF8 0x20000000 /* SvPV is UTF-8 encoded */
5bc28da9 221
430eacda 222#define SVf_THINKFIRST (SVf_READONLY|SVf_ROK|SVf_FAKE)
5bc28da9 223
a0d0e21e
LW
224#define SVf_OK (SVf_IOK|SVf_NOK|SVf_POK|SVf_ROK| \
225 SVp_IOK|SVp_NOK|SVp_POK)
226
9e7bc3e8 227#define SVf_AMAGIC 0x10000000 /* has magical overloaded methods */
a0d0e21e 228
c240c76d 229#define PRIVSHIFT 8 /* (SVp_?OK >> PRIVSHIFT) == SVf_?OK */
8990e307
LW
230
231/* Some private flags. */
232
f472eb5c 233/* SVpad_OUR may be set on SVt_PV{NV,MG,GV} types */
032d6771 234#define SVpad_OUR 0x80000000 /* pad name is "our" instead of "my" */
524189f1 235#define SVpad_TYPED 0x40000000 /* Typed Lexical */
032d6771 236
25da4f38
IZ
237#define SVf_IVisUV 0x80000000 /* use XPVUV instead of XPVIV */
238
239#define SVpfm_COMPILED 0x80000000 /* FORMLINE is compiled */
8990e307
LW
240
241#define SVpbm_VALID 0x80000000
bbce6d69 242#define SVpbm_TAIL 0x40000000
8990e307 243
25da4f38
IZ
244#define SVrepl_EVAL 0x40000000 /* Replacement part of s///e */
245
c7df1ae8 246#define SVphv_CLONEABLE 0x08000000 /* for stashes: clone its objects */
ff38041c 247#define SVphv_REHASH 0x10000000 /* HV is recalculating hash values */
6bfc225d 248#define SVphv_SHAREKEYS 0x20000000 /* keys live on shared string table */
51594c39 249#define SVphv_LAZYDEL 0x40000000 /* entry in xhv_eiter must be deleted */
19692e8d 250#define SVphv_HASKFLAGS 0x80000000 /* keys have flag byte after hash */
bf6bd887 251
810b8aa5
GS
252#define SVprv_WEAKREF 0x80000000 /* Weak reference */
253
80e92b6f
JC
254typedef struct {
255 IV xiv_iv; /* integer value or pv offset */
256} xiv_allocated;
257
258typedef struct {
259 NV xnv_nv; /* numeric value, if any */
260} xnv_allocated;
261
ed6116ce
LW
262struct xrv {
263 SV * xrv_rv; /* pointer to another SV */
264};
265
79072805 266struct xpv {
ed6116ce
LW
267 char * xpv_pv; /* pointer to malloced string */
268 STRLEN xpv_cur; /* length of xpv_pv as a C string */
269 STRLEN xpv_len; /* allocated size */
79072805
LW
270};
271
6513d6ca
NC
272typedef struct xpv xpv_allocated;
273
79072805 274struct xpviv {
ed6116ce
LW
275 char * xpv_pv; /* pointer to malloced string */
276 STRLEN xpv_cur; /* length of xpv_pv as a C string */
277 STRLEN xpv_len; /* allocated size */
a0d0e21e 278 IV xiv_iv; /* integer value or pv offset */
79072805
LW
279};
280
80e92b6f
JC
281typedef struct xpviv xpviv_allocated;
282
ff68c719 283struct xpvuv {
284 char * xpv_pv; /* pointer to malloced string */
285 STRLEN xpv_cur; /* length of xpv_pv as a C string */
286 STRLEN xpv_len; /* allocated size */
287 UV xuv_uv; /* unsigned value or pv offset */
288};
289
79072805 290struct xpvnv {
ed6116ce
LW
291 char * xpv_pv; /* pointer to malloced string */
292 STRLEN xpv_cur; /* length of xpv_pv as a C string */
293 STRLEN xpv_len; /* allocated size */
a0d0e21e 294 IV xiv_iv; /* integer value or pv offset */
65202027 295 NV xnv_nv; /* numeric value, if any */
79072805
LW
296};
297
6ee623d5 298/* These structure must match the beginning of struct xpvhv in hv.h. */
79072805 299struct xpvmg {
ed6116ce
LW
300 char * xpv_pv; /* pointer to malloced string */
301 STRLEN xpv_cur; /* length of xpv_pv as a C string */
302 STRLEN xpv_len; /* allocated size */
a0d0e21e 303 IV xiv_iv; /* integer value or pv offset */
65202027 304 NV xnv_nv; /* numeric value, if any */
79072805
LW
305 MAGIC* xmg_magic; /* linked list of magicalness */
306 HV* xmg_stash; /* class package */
307};
308
309struct xpvlv {
ed6116ce
LW
310 char * xpv_pv; /* pointer to malloced string */
311 STRLEN xpv_cur; /* length of xpv_pv as a C string */
312 STRLEN xpv_len; /* allocated size */
a0d0e21e 313 IV xiv_iv; /* integer value or pv offset */
65202027 314 NV xnv_nv; /* numeric value, if any */
79072805
LW
315 MAGIC* xmg_magic; /* linked list of magicalness */
316 HV* xmg_stash; /* class package */
8990e307 317
79072805
LW
318 STRLEN xlv_targoff;
319 STRLEN xlv_targlen;
320 SV* xlv_targ;
73c86719
JH
321 char xlv_type; /* k=keys .=pos x=substr v=vec /=join/re
322 * y=alem/helem/iter t=tie T=tied HE */
79072805
LW
323};
324
325struct xpvgv {
ed6116ce
LW
326 char * xpv_pv; /* pointer to malloced string */
327 STRLEN xpv_cur; /* length of xpv_pv as a C string */
328 STRLEN xpv_len; /* allocated size */
a0d0e21e 329 IV xiv_iv; /* integer value or pv offset */
65202027 330 NV xnv_nv; /* numeric value, if any */
79072805
LW
331 MAGIC* xmg_magic; /* linked list of magicalness */
332 HV* xmg_stash; /* class package */
8990e307 333
79072805
LW
334 GP* xgv_gp;
335 char* xgv_name;
336 STRLEN xgv_namelen;
337 HV* xgv_stash;
a5f75d66 338 U8 xgv_flags;
79072805
LW
339};
340
341struct xpvbm {
ed6116ce
LW
342 char * xpv_pv; /* pointer to malloced string */
343 STRLEN xpv_cur; /* length of xpv_pv as a C string */
344 STRLEN xpv_len; /* allocated size */
a0d0e21e 345 IV xiv_iv; /* integer value or pv offset */
65202027 346 NV xnv_nv; /* numeric value, if any */
79072805
LW
347 MAGIC* xmg_magic; /* linked list of magicalness */
348 HV* xmg_stash; /* class package */
8990e307 349
79072805
LW
350 I32 xbm_useful; /* is this constant pattern being useful? */
351 U16 xbm_previous; /* how many characters in string before rare? */
352 U8 xbm_rare; /* rarest character in string */
353};
354
a77f7f8b 355/* This structure must match XPVCV in cv.h */
44a8e56a 356
77a005ab
MB
357typedef U16 cv_flags_t;
358
79072805 359struct xpvfm {
ed6116ce
LW
360 char * xpv_pv; /* pointer to malloced string */
361 STRLEN xpv_cur; /* length of xpv_pv as a C string */
362 STRLEN xpv_len; /* allocated size */
a0d0e21e 363 IV xiv_iv; /* integer value or pv offset */
65202027 364 NV xnv_nv; /* numeric value, if any */
79072805
LW
365 MAGIC* xmg_magic; /* linked list of magicalness */
366 HV* xmg_stash; /* class package */
8990e307 367
79072805
LW
368 HV * xcv_stash;
369 OP * xcv_start;
370 OP * xcv_root;
acfe0abc 371 void (*xcv_xsub)(pTHX_ CV*);
a0d0e21e
LW
372 ANY xcv_xsubany;
373 GV * xcv_gv;
57843af0 374 char * xcv_file;
b195d487 375 long xcv_depth; /* >= 2 indicates recursive call */
79072805 376 AV * xcv_padlist;
748a9306 377 CV * xcv_outside;
4d1ff10f 378#ifdef USE_5005THREADS
f6d98b14 379 perl_mutex *xcv_mutexp; /* protects xcv_owner */
52e1cb5e 380 struct perl_thread *xcv_owner; /* current owner thread */
4d1ff10f 381#endif /* USE_5005THREADS */
77a005ab 382 cv_flags_t xcv_flags;
d7afa7f5
JH
383 U32 xcv_outside_seq; /* the COP sequence (at the point of our
384 * compilation) in the lexically enclosing
385 * sub */
11a7ac70 386 IV xfm_lines;
79072805
LW
387};
388
8990e307
LW
389struct xpvio {
390 char * xpv_pv; /* pointer to malloced string */
391 STRLEN xpv_cur; /* length of xpv_pv as a C string */
392 STRLEN xpv_len; /* allocated size */
a0d0e21e 393 IV xiv_iv; /* integer value or pv offset */
65202027 394 NV xnv_nv; /* numeric value, if any */
8990e307
LW
395 MAGIC* xmg_magic; /* linked list of magicalness */
396 HV* xmg_stash; /* class package */
397
760ac839
LW
398 PerlIO * xio_ifp; /* ifp and ofp are normally the same */
399 PerlIO * xio_ofp; /* but sockets need separate streams */
4755096e
GS
400 /* Cray addresses everything by word boundaries (64 bits) and
401 * code and data pointers cannot be mixed (which is exactly what
402 * Perl_filter_add() tries to do with the dirp), hence the following
403 * union trick (as suggested by Gurusamy Sarathy).
404 * For further information see Geir Johansen's problem report titled
405 [ID 20000612.002] Perl problem on Cray system
406 * The any pointer (known as IoANY()) will also be a good place
407 * to hang any IO disciplines to.
408 */
409 union {
410 DIR * xiou_dirp; /* for opendir, readdir, etc */
411 void * xiou_any; /* for alignment */
412 } xio_dirpu;
11a7ac70
JH
413 IV xio_lines; /* $. */
414 IV xio_page; /* $% */
415 IV xio_page_len; /* $= */
416 IV xio_lines_left; /* $- */
8990e307
LW
417 char * xio_top_name; /* $^ */
418 GV * xio_top_gv; /* $^ */
419 char * xio_fmt_name; /* $~ */
420 GV * xio_fmt_gv; /* $~ */
421 char * xio_bottom_name;/* $^B */
422 GV * xio_bottom_gv; /* $^B */
423 short xio_subprocess; /* -| or |- */
424 char xio_type;
425 char xio_flags;
426};
4755096e
GS
427#define xio_dirp xio_dirpu.xiou_dirp
428#define xio_any xio_dirpu.xiou_any
8990e307 429
e0c19803
GS
430#define IOf_ARGV 1 /* this fp iterates over ARGV */
431#define IOf_START 2 /* check for null ARGV and substitute '-' */
432#define IOf_FLUSH 4 /* this fp wants a flush after write op */
433#define IOf_DIDTOP 8 /* just did top of form */
434#define IOf_UNTAINT 16 /* consider this fp (and its data) "safe" */
435#define IOf_NOLINE 32 /* slurped a pseudo-line from empty file */
436#define IOf_FAKE_DIRP 64 /* xio_dirp is fake (source filters kludge) */
8990e307 437
ed6116ce
LW
438/* The following macros define implementation-independent predicates on SVs. */
439
954c1994
GS
440/*
441=for apidoc Am|bool|SvNIOK|SV* sv
442Returns a boolean indicating whether the SV contains a number, integer or
443double.
444
445=for apidoc Am|bool|SvNIOKp|SV* sv
446Returns a boolean indicating whether the SV contains a number, integer or
447double. Checks the B<private> setting. Use C<SvNIOK>.
448
449=for apidoc Am|void|SvNIOK_off|SV* sv
450Unsets the NV/IV status of an SV.
451
452=for apidoc Am|bool|SvOK|SV* sv
a1e5c4b6
NC
453Returns a boolean indicating whether the value is an SV. It also tells
454whether the value is defined or not.
954c1994
GS
455
456=for apidoc Am|bool|SvIOKp|SV* sv
457Returns a boolean indicating whether the SV contains an integer. Checks
458the B<private> setting. Use C<SvIOK>.
459
460=for apidoc Am|bool|SvNOKp|SV* sv
461Returns a boolean indicating whether the SV contains a double. Checks the
462B<private> setting. Use C<SvNOK>.
463
464=for apidoc Am|bool|SvPOKp|SV* sv
465Returns a boolean indicating whether the SV contains a character string.
466Checks the B<private> setting. Use C<SvPOK>.
467
468=for apidoc Am|bool|SvIOK|SV* sv
469Returns a boolean indicating whether the SV contains an integer.
470
471=for apidoc Am|void|SvIOK_on|SV* sv
472Tells an SV that it is an integer.
473
474=for apidoc Am|void|SvIOK_off|SV* sv
475Unsets the IV status of an SV.
476
477=for apidoc Am|void|SvIOK_only|SV* sv
478Tells an SV that it is an integer and disables all other OK bits.
479
e331fc52
JH
480=for apidoc Am|void|SvIOK_only_UV|SV* sv
481Tells and SV that it is an unsigned integer and disables all other OK bits.
482
2ef4a045 483=for apidoc Am|bool|SvIOK_UV|SV* sv
e331fc52
JH
484Returns a boolean indicating whether the SV contains an unsigned integer.
485
28e5dec8
JH
486=for apidoc Am|void|SvUOK|SV* sv
487Returns a boolean indicating whether the SV contains an unsigned integer.
488
2ef4a045 489=for apidoc Am|bool|SvIOK_notUV|SV* sv
d1be9408 490Returns a boolean indicating whether the SV contains a signed integer.
e331fc52 491
954c1994
GS
492=for apidoc Am|bool|SvNOK|SV* sv
493Returns a boolean indicating whether the SV contains a double.
494
495=for apidoc Am|void|SvNOK_on|SV* sv
496Tells an SV that it is a double.
497
498=for apidoc Am|void|SvNOK_off|SV* sv
499Unsets the NV status of an SV.
500
501=for apidoc Am|void|SvNOK_only|SV* sv
502Tells an SV that it is a double and disables all other OK bits.
503
504=for apidoc Am|bool|SvPOK|SV* sv
505Returns a boolean indicating whether the SV contains a character
506string.
507
508=for apidoc Am|void|SvPOK_on|SV* sv
509Tells an SV that it is a string.
510
511=for apidoc Am|void|SvPOK_off|SV* sv
512Unsets the PV status of an SV.
513
514=for apidoc Am|void|SvPOK_only|SV* sv
515Tells an SV that it is a string and disables all other OK bits.
cd458e05 516Will also turn off the UTF-8 status.
954c1994
GS
517
518=for apidoc Am|bool|SvOOK|SV* sv
519Returns a boolean indicating whether the SvIVX is a valid offset value for
520the SvPVX. This hack is used internally to speed up removal of characters
521from the beginning of a SvPV. When SvOOK is true, then the start of the
522allocated string buffer is really (SvPVX - SvIVX).
523
524=for apidoc Am|bool|SvROK|SV* sv
525Tests if the SV is an RV.
526
527=for apidoc Am|void|SvROK_on|SV* sv
528Tells an SV that it is an RV.
529
530=for apidoc Am|void|SvROK_off|SV* sv
531Unsets the RV status of an SV.
532
533=for apidoc Am|SV*|SvRV|SV* sv
534Dereferences an RV to return the SV.
535
536=for apidoc Am|IV|SvIVX|SV* sv
645c22ef
DM
537Returns the raw value in the SV's IV slot, without checks or conversions.
538Only use when you are sure SvIOK is true. See also C<SvIV()>.
954c1994
GS
539
540=for apidoc Am|UV|SvUVX|SV* sv
645c22ef
DM
541Returns the raw value in the SV's UV slot, without checks or conversions.
542Only use when you are sure SvIOK is true. See also C<SvUV()>.
954c1994
GS
543
544=for apidoc Am|NV|SvNVX|SV* sv
645c22ef
DM
545Returns the raw value in the SV's NV slot, without checks or conversions.
546Only use when you are sure SvNOK is true. See also C<SvNV()>.
954c1994
GS
547
548=for apidoc Am|char*|SvPVX|SV* sv
645c22ef 549Returns a pointer to the physical string in the SV. The SV must contain a
954c1994
GS
550string.
551
552=for apidoc Am|STRLEN|SvCUR|SV* sv
553Returns the length of the string which is in the SV. See C<SvLEN>.
554
555=for apidoc Am|STRLEN|SvLEN|SV* sv
659239a4
MG
556Returns the size of the string buffer in the SV, not including any part
557attributable to C<SvOOK>. See C<SvCUR>.
954c1994
GS
558
559=for apidoc Am|char*|SvEND|SV* sv
560Returns a pointer to the last character in the string which is in the SV.
561See C<SvCUR>. Access the character as *(SvEND(sv)).
562
563=for apidoc Am|HV*|SvSTASH|SV* sv
564Returns the stash of the SV.
565
5e7e76a3 566=for apidoc Am|void|SvIV_set|SV* sv|IV val
0e61ce87
SP
567Set the value of the IV pointer in sv to val. It is possible to perform
568the same function of this macro with an lvalue assignment to C<SvIVX>.
569With future Perls, however, it will be more efficient to use
570C<SvIV_set> instead of the lvalue assignment to C<SvIVX>.
5e7e76a3
SP
571
572=for apidoc Am|void|SvNV_set|SV* sv|NV val
0e61ce87 573Set the value of the NV pointer in sv to val. See C<SvIV_set>.
5e7e76a3
SP
574
575=for apidoc Am|void|SvPV_set|SV* sv|char* val
0e61ce87 576Set the value of the PV pointer in sv to val. See C<SvIV_set>.
5e7e76a3
SP
577
578=for apidoc Am|void|SvUV_set|SV* sv|UV val
0e61ce87 579Set the value of the UV pointer in sv to val. See C<SvIV_set>.
5e7e76a3
SP
580
581=for apidoc Am|void|SvRV_set|SV* sv|SV* val
0e61ce87 582Set the value of the RV pointer in sv to val. See C<SvIV_set>.
5e7e76a3
SP
583
584=for apidoc Am|void|SvMAGIC_set|SV* sv|MAGIC* val
0e61ce87 585Set the value of the MAGIC pointer in sv to val. See C<SvIV_set>.
5e7e76a3
SP
586
587=for apidoc Am|void|SvSTASH_set|SV* sv|STASH* val
0e61ce87 588Set the value of the STASH pointer in sv to val. See C<SvIV_set>.
5e7e76a3 589
954c1994 590=for apidoc Am|void|SvCUR_set|SV* sv|STRLEN len
0e61ce87
SP
591Set the current length of the string which is in the SV. See C<SvCUR>
592and C<SvIV_set>.
5e7e76a3
SP
593
594=for apidoc Am|void|SvLEN_set|SV* sv|STRLEN len
0e61ce87 595Set the actual length of the string which is in the SV. See C<SvIV_set>.
954c1994
GS
596
597=cut
598*/
599
79072805 600#define SvNIOK(sv) (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
748a9306 601#define SvNIOKp(sv) (SvFLAGS(sv) & (SVp_IOK|SVp_NOK))
a0d0e21e 602#define SvNIOK_off(sv) (SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK| \
25da4f38 603 SVp_IOK|SVp_NOK|SVf_IVisUV))
79072805 604
653fb8f3 605#if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
56b65a99 606#define assert_not_ROK(sv) ({assert(!SvROK(sv) || !SvRV(sv));}),
137365c5 607#else
ecde6dd6 608#define assert_not_ROK(sv)
137365c5
JH
609#endif
610
463ee0b2 611#define SvOK(sv) (SvFLAGS(sv) & SVf_OK)
ecde6dd6 612#define SvOK_off(sv) (assert_not_ROK(sv) \
137365c5 613 SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC| \
d41ff1b8 614 SVf_IVisUV|SVf_UTF8), \
25da4f38 615 SvOOK_off(sv))
ecde6dd6 616#define SvOK_off_exc_UV(sv) (assert_not_ROK(sv) \
137365c5 617 SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC| \
d41ff1b8 618 SVf_UTF8), \
a0d0e21e 619 SvOOK_off(sv))
79072805 620
8990e307
LW
621#define SvOKp(sv) (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
622#define SvIOKp(sv) (SvFLAGS(sv) & SVp_IOK)
155aba94 623#define SvIOKp_on(sv) ((void)SvOOK_off(sv), SvFLAGS(sv) |= SVp_IOK)
8990e307
LW
624#define SvNOKp(sv) (SvFLAGS(sv) & SVp_NOK)
625#define SvNOKp_on(sv) (SvFLAGS(sv) |= SVp_NOK)
626#define SvPOKp(sv) (SvFLAGS(sv) & SVp_POK)
ecde6dd6 627#define SvPOKp_on(sv) (assert_not_ROK(sv) \
137365c5 628 SvFLAGS(sv) |= SVp_POK)
463ee0b2 629
79072805 630#define SvIOK(sv) (SvFLAGS(sv) & SVf_IOK)
155aba94 631#define SvIOK_on(sv) ((void)SvOOK_off(sv), \
a0d0e21e 632 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
25da4f38 633#define SvIOK_off(sv) (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV))
7460c263 634#define SvIOK_only(sv) (SvOK_off(sv), \
a0d0e21e 635 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
7460c263 636#define SvIOK_only_UV(sv) (SvOK_off_exc_UV(sv), \
25da4f38 637 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
70401c6b 638
25da4f38
IZ
639#define SvIOK_UV(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV)) \
640 == (SVf_IOK|SVf_IVisUV))
28e5dec8 641#define SvUOK(sv) SvIOK_UV(sv)
25da4f38
IZ
642#define SvIOK_notUV(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV)) \
643 == SVf_IOK)
644
7d7ce6cc 645#define SvVOK(sv) (SvMAGICAL(sv) && mg_find(sv,'V'))
25da4f38
IZ
646#define SvIsUV(sv) (SvFLAGS(sv) & SVf_IVisUV)
647#define SvIsUV_on(sv) (SvFLAGS(sv) |= SVf_IVisUV)
648#define SvIsUV_off(sv) (SvFLAGS(sv) &= ~SVf_IVisUV)
79072805
LW
649
650#define SvNOK(sv) (SvFLAGS(sv) & SVf_NOK)
a0d0e21e 651#define SvNOK_on(sv) (SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
8990e307 652#define SvNOK_off(sv) (SvFLAGS(sv) &= ~(SVf_NOK|SVp_NOK))
7460c263 653#define SvNOK_only(sv) (SvOK_off(sv), \
a0d0e21e 654 SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
79072805 655
914184e1 656/*
2ef4a045 657=for apidoc Am|bool|SvUTF8|SV* sv
914184e1
JH
658Returns a boolean indicating whether the SV contains UTF-8 encoded data.
659
660=for apidoc Am|void|SvUTF8_on|SV *sv
cd458e05 661Turn on the UTF-8 status of an SV (the data is not changed, just the flag).
d5ce4a7c 662Do not use frivolously.
914184e1
JH
663
664=for apidoc Am|void|SvUTF8_off|SV *sv
cd458e05 665Unsets the UTF-8 status of an SV.
914184e1
JH
666
667=for apidoc Am|void|SvPOK_only_UTF8|SV* sv
d5ce4a7c 668Tells an SV that it is a string and disables all other OK bits,
cd458e05 669and leaves the UTF-8 status as it was.
f1a1024e 670
914184e1
JH
671=cut
672 */
673
a7cb1f99
GS
674#define SvUTF8(sv) (SvFLAGS(sv) & SVf_UTF8)
675#define SvUTF8_on(sv) (SvFLAGS(sv) |= (SVf_UTF8))
676#define SvUTF8_off(sv) (SvFLAGS(sv) &= ~(SVf_UTF8))
5bc28da9 677
79072805 678#define SvPOK(sv) (SvFLAGS(sv) & SVf_POK)
ecde6dd6 679#define SvPOK_on(sv) (assert_not_ROK(sv) \
137365c5 680 SvFLAGS(sv) |= (SVf_POK|SVp_POK))
8990e307 681#define SvPOK_off(sv) (SvFLAGS(sv) &= ~(SVf_POK|SVp_POK))
ecde6dd6 682#define SvPOK_only(sv) (assert_not_ROK(sv) \
137365c5 683 SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC| \
d41ff1b8
GS
684 SVf_IVisUV|SVf_UTF8), \
685 SvFLAGS(sv) |= (SVf_POK|SVp_POK))
ecde6dd6 686#define SvPOK_only_UTF8(sv) (assert_not_ROK(sv) \
137365c5 687 SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC| \
d41ff1b8 688 SVf_IVisUV), \
a0d0e21e 689 SvFLAGS(sv) |= (SVf_POK|SVp_POK))
79072805
LW
690
691#define SvOOK(sv) (SvFLAGS(sv) & SVf_OOK)
155aba94 692#define SvOOK_on(sv) ((void)SvIOK_off(sv), SvFLAGS(sv) |= SVf_OOK)
7460c263 693#define SvOOK_off(sv) ((void)(SvOOK(sv) && sv_backoff(sv)))
79072805 694
a0d0e21e
LW
695#define SvFAKE(sv) (SvFLAGS(sv) & SVf_FAKE)
696#define SvFAKE_on(sv) (SvFLAGS(sv) |= SVf_FAKE)
697#define SvFAKE_off(sv) (SvFLAGS(sv) &= ~SVf_FAKE)
698
ed6116ce 699#define SvROK(sv) (SvFLAGS(sv) & SVf_ROK)
a0d0e21e 700#define SvROK_on(sv) (SvFLAGS(sv) |= SVf_ROK)
a0d0e21e 701#define SvROK_off(sv) (SvFLAGS(sv) &= ~(SVf_ROK|SVf_AMAGIC))
79072805 702
8990e307
LW
703#define SvMAGICAL(sv) (SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG))
704#define SvMAGICAL_on(sv) (SvFLAGS(sv) |= (SVs_GMG|SVs_SMG|SVs_RMG))
705#define SvMAGICAL_off(sv) (SvFLAGS(sv) &= ~(SVs_GMG|SVs_SMG|SVs_RMG))
79072805 706
8990e307
LW
707#define SvGMAGICAL(sv) (SvFLAGS(sv) & SVs_GMG)
708#define SvGMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_GMG)
709#define SvGMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_GMG)
ed6116ce 710
8990e307
LW
711#define SvSMAGICAL(sv) (SvFLAGS(sv) & SVs_SMG)
712#define SvSMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_SMG)
713#define SvSMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_SMG)
ed6116ce 714
8990e307
LW
715#define SvRMAGICAL(sv) (SvFLAGS(sv) & SVs_RMG)
716#define SvRMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_RMG)
717#define SvRMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_RMG)
ed6116ce 718
9e7bc3e8
JD
719#define SvAMAGIC(sv) (SvFLAGS(sv) & SVf_AMAGIC)
720#define SvAMAGIC_on(sv) (SvFLAGS(sv) |= SVf_AMAGIC)
721#define SvAMAGIC_off(sv) (SvFLAGS(sv) &= ~SVf_AMAGIC)
a0d0e21e 722
8cf8f3d1 723#define SvGAMAGIC(sv) (SvFLAGS(sv) & (SVs_GMG|SVf_AMAGIC))
1426bbf4 724
a0d0e21e
LW
725/*
726#define Gv_AMG(stash) \
727 (HV_AMAGICmb(stash) && \
728 ((!HV_AMAGICbad(stash) && HV_AMAGIC(stash)) || Gv_AMupdate(stash)))
729*/
3280af22 730#define Gv_AMG(stash) (PL_amagic_generation && Gv_AMupdate(stash))
a0d0e21e 731
810b8aa5
GS
732#define SvWEAKREF(sv) ((SvFLAGS(sv) & (SVf_ROK|SVprv_WEAKREF)) \
733 == (SVf_ROK|SVprv_WEAKREF))
734#define SvWEAKREF_on(sv) (SvFLAGS(sv) |= (SVf_ROK|SVprv_WEAKREF))
735#define SvWEAKREF_off(sv) (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_WEAKREF))
736
a0d0e21e 737#define SvTHINKFIRST(sv) (SvFLAGS(sv) & SVf_THINKFIRST)
ed6116ce 738
8990e307 739#define SvPADBUSY(sv) (SvFLAGS(sv) & SVs_PADBUSY)
ed6116ce 740
8990e307
LW
741#define SvPADTMP(sv) (SvFLAGS(sv) & SVs_PADTMP)
742#define SvPADTMP_on(sv) (SvFLAGS(sv) |= SVs_PADTMP|SVs_PADBUSY)
743#define SvPADTMP_off(sv) (SvFLAGS(sv) &= ~SVs_PADTMP)
ed6116ce 744
8990e307
LW
745#define SvPADMY(sv) (SvFLAGS(sv) & SVs_PADMY)
746#define SvPADMY_on(sv) (SvFLAGS(sv) |= SVs_PADMY|SVs_PADBUSY)
ed6116ce 747
8990e307
LW
748#define SvTEMP(sv) (SvFLAGS(sv) & SVs_TEMP)
749#define SvTEMP_on(sv) (SvFLAGS(sv) |= SVs_TEMP)
750#define SvTEMP_off(sv) (SvFLAGS(sv) &= ~SVs_TEMP)
79072805 751
8990e307
LW
752#define SvOBJECT(sv) (SvFLAGS(sv) & SVs_OBJECT)
753#define SvOBJECT_on(sv) (SvFLAGS(sv) |= SVs_OBJECT)
754#define SvOBJECT_off(sv) (SvFLAGS(sv) &= ~SVs_OBJECT)
79072805 755
8990e307
LW
756#define SvREADONLY(sv) (SvFLAGS(sv) & SVf_READONLY)
757#define SvREADONLY_on(sv) (SvFLAGS(sv) |= SVf_READONLY)
758#define SvREADONLY_off(sv) (SvFLAGS(sv) &= ~SVf_READONLY)
79072805 759
8990e307
LW
760#define SvSCREAM(sv) (SvFLAGS(sv) & SVp_SCREAM)
761#define SvSCREAM_on(sv) (SvFLAGS(sv) |= SVp_SCREAM)
762#define SvSCREAM_off(sv) (SvFLAGS(sv) &= ~SVp_SCREAM)
79072805 763
8990e307
LW
764#define SvCOMPILED(sv) (SvFLAGS(sv) & SVpfm_COMPILED)
765#define SvCOMPILED_on(sv) (SvFLAGS(sv) |= SVpfm_COMPILED)
766#define SvCOMPILED_off(sv) (SvFLAGS(sv) &= ~SVpfm_COMPILED)
79072805 767
25da4f38
IZ
768#define SvEVALED(sv) (SvFLAGS(sv) & SVrepl_EVAL)
769#define SvEVALED_on(sv) (SvFLAGS(sv) |= SVrepl_EVAL)
770#define SvEVALED_off(sv) (SvFLAGS(sv) &= ~SVrepl_EVAL)
771
8990e307
LW
772#define SvTAIL(sv) (SvFLAGS(sv) & SVpbm_TAIL)
773#define SvTAIL_on(sv) (SvFLAGS(sv) |= SVpbm_TAIL)
774#define SvTAIL_off(sv) (SvFLAGS(sv) &= ~SVpbm_TAIL)
775
8990e307
LW
776#define SvVALID(sv) (SvFLAGS(sv) & SVpbm_VALID)
777#define SvVALID_on(sv) (SvFLAGS(sv) |= SVpbm_VALID)
778#define SvVALID_off(sv) (SvFLAGS(sv) &= ~SVpbm_VALID)
779
1cc8b4c5
AB
780#ifdef USE_ITHREADS
781/* The following uses the FAKE flag to show that a regex pointer is infact
8ca6097c 782 its own offset in the regexpad for ithreads */
1cc8b4c5
AB
783#define SvREPADTMP(sv) (SvFLAGS(sv) & SVf_FAKE)
784#define SvREPADTMP_on(sv) (SvFLAGS(sv) |= SVf_FAKE)
785#define SvREPADTMP_off(sv) (SvFLAGS(sv) &= ~SVf_FAKE)
786#endif
787
ed6116ce
LW
788#define SvRV(sv) ((XRV*) SvANY(sv))->xrv_rv
789#define SvRVx(sv) SvRV(sv)
790
463ee0b2 791#define SvIVX(sv) ((XPVIV*) SvANY(sv))->xiv_iv
ff68c719 792#define SvUVX(sv) ((XPVUV*) SvANY(sv))->xuv_uv
463ee0b2 793#define SvNVX(sv) ((XPVNV*)SvANY(sv))->xnv_nv
463ee0b2 794#define SvPVX(sv) ((XPV*) SvANY(sv))->xpv_pv
fc64cf4d
NC
795/* Given that these two are new, there can't be any existing code using them
796 * as LVALUEs */
797#define SvPVX_mutable(sv) (0 + SvPVX(sv))
798#define SvPVX_const(sv) ((const char*) (0 + SvPVX(sv)))
79072805 799#define SvCUR(sv) ((XPV*) SvANY(sv))->xpv_cur
79072805 800#define SvLEN(sv) ((XPV*) SvANY(sv))->xpv_len
79072805 801#define SvEND(sv)(((XPV*) SvANY(sv))->xpv_pv + ((XPV*)SvANY(sv))->xpv_cur)
b43557d6
NC
802
803#define SvIVXx(sv) SvIVX(sv)
804#define SvUVXx(sv) SvUVX(sv)
805#define SvNVXx(sv) SvNVX(sv)
806#define SvPVXx(sv) SvPVX(sv)
807#define SvLENx(sv) SvLEN(sv)
6b88bc9c 808#define SvENDx(sv) ((PL_Sv = (sv)), SvEND(PL_Sv))
79072805
LW
809#define SvMAGIC(sv) ((XPVMG*) SvANY(sv))->xmg_magic
810#define SvSTASH(sv) ((XPVMG*) SvANY(sv))->xmg_stash
811
28e5dec8
JH
812/* Ask a scalar nicely to try to become an IV, if possible.
813 Not guaranteed to stay returning void */
814/* Macro won't actually call sv_2iv if already IOK */
815#define SvIV_please(sv) \
816 STMT_START {if (!SvIOKp(sv) && (SvNOK(sv) || SvPOK(sv))) \
817 (void) SvIV(sv); } STMT_END
a8dc4fe8
SP
818/* Put the asserts back at some point and figure out where they reveal bugs
819*/
79072805 820#define SvIV_set(sv, val) \
80b92232 821 STMT_START { assert(SvTYPE(sv) == SVt_IV || SvTYPE(sv) >= SVt_PVIV); \
0da6cfda 822 (((XPVIV*) SvANY(sv))->xiv_iv = (val)); } STMT_END
79072805 823#define SvNV_set(sv, val) \
80b92232 824 STMT_START { assert(SvTYPE(sv) == SVt_NV || SvTYPE(sv) >= SVt_PVNV); \
b43557d6 825 (((XPVNV*)SvANY(sv))->xnv_nv = (val)); } STMT_END
a8dc4fe8 826/* assert(SvTYPE(sv) >= SVt_PV); */
79072805 827#define SvPV_set(sv, val) \
a8dc4fe8 828 STMT_START { \
0da6cfda 829 (((XPV*) SvANY(sv))->xpv_pv = (val)); } STMT_END
a8dc4fe8 830/* assert(SvTYPE(sv) == SVt_IV || SvTYPE(sv) >= SVt_PVIV); */
0da6cfda 831#define SvUV_set(sv, val) \
a8dc4fe8 832 STMT_START { \
0da6cfda 833 (((XPVUV*)SvANY(sv))->xuv_uv = (val)); } STMT_END
a8dc4fe8
SP
834/* assert(SvTYPE(sv) >= SVt_RV); */
835#define SvRV_set(sv, val) \
836 STMT_START { \
837 (((XRV*)SvANY(sv))->xrv_rv = (val)); } STMT_END
838/* assert(SvTYPE(sv) >= SVt_PVMG); */
839#define SvMAGIC_set(sv, val) \
840 STMT_START { \
841 (((XPVMG*)SvANY(sv))->xmg_magic = (val)); } STMT_END
842/* assert(SvTYPE(sv) >= SVt_PVMG); */
843#define SvSTASH_set(sv, val) \
844 STMT_START { \
845 (((XPVMG*) SvANY(sv))->xmg_stash = (val)); } STMT_END
846/* assert(SvTYPE(sv) >= SVt_PV); */
79072805 847#define SvCUR_set(sv, val) \
a8dc4fe8
SP
848 STMT_START { \
849 (((XPV*) SvANY(sv))->xpv_cur = (val)); } STMT_END
850/* assert(SvTYPE(sv) >= SVt_PV); */
79072805 851#define SvLEN_set(sv, val) \
a8dc4fe8
SP
852 STMT_START { \
853 (((XPV*) SvANY(sv))->xpv_len = (val)); } STMT_END
79072805 854#define SvEND_set(sv, val) \
80b92232 855 STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
1896a75e 856 (SvCUR(sv) = (val) - SvPVX(sv)); } STMT_END
79072805 857
ea5389ca
NC
858#define SvPV_renew(sv,n) \
859 STMT_START { SvLEN_set(sv, n); \
860 SvPV_set((sv), (MEM_WRAP_CHECK_(n,char) \
861 (char*)saferealloc((Malloc_t)SvPVX(sv), \
862 (MEM_SIZE)((n))))); \
863 } STMT_END
864
865#define SvPV_shrink_to_cur(sv) STMT_START { \
866 const STRLEN _lEnGtH = SvCUR(sv) + 1; \
867 SvPV_renew(sv, _lEnGtH); \
868 } STMT_END
869
676a626c
NC
870#define SvPV_free(sv) \
871 STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
872 if (SvLEN(sv)) { \
873 if(SvOOK(sv)) { \
874 Safefree(SvPVX(sv) - SvIVX(sv)); \
875 SvFLAGS(sv) &= ~SVf_OOK; \
876 } else { \
877 Safefree(SvPVX(sv)); \
878 } \
879 } \
880 } STMT_END
881
79072805
LW
882#define BmRARE(sv) ((XPVBM*) SvANY(sv))->xbm_rare
883#define BmUSEFUL(sv) ((XPVBM*) SvANY(sv))->xbm_useful
884#define BmPREVIOUS(sv) ((XPVBM*) SvANY(sv))->xbm_previous
885
886#define FmLINES(sv) ((XPVFM*) SvANY(sv))->xfm_lines
887
888#define LvTYPE(sv) ((XPVLV*) SvANY(sv))->xlv_type
889#define LvTARG(sv) ((XPVLV*) SvANY(sv))->xlv_targ
890#define LvTARGOFF(sv) ((XPVLV*) SvANY(sv))->xlv_targoff
891#define LvTARGLEN(sv) ((XPVLV*) SvANY(sv))->xlv_targlen
892
8990e307
LW
893#define IoIFP(sv) ((XPVIO*) SvANY(sv))->xio_ifp
894#define IoOFP(sv) ((XPVIO*) SvANY(sv))->xio_ofp
895#define IoDIRP(sv) ((XPVIO*) SvANY(sv))->xio_dirp
4755096e 896#define IoANY(sv) ((XPVIO*) SvANY(sv))->xio_any
8990e307
LW
897#define IoLINES(sv) ((XPVIO*) SvANY(sv))->xio_lines
898#define IoPAGE(sv) ((XPVIO*) SvANY(sv))->xio_page
899#define IoPAGE_LEN(sv) ((XPVIO*) SvANY(sv))->xio_page_len
900#define IoLINES_LEFT(sv)((XPVIO*) SvANY(sv))->xio_lines_left
901#define IoTOP_NAME(sv) ((XPVIO*) SvANY(sv))->xio_top_name
902#define IoTOP_GV(sv) ((XPVIO*) SvANY(sv))->xio_top_gv
903#define IoFMT_NAME(sv) ((XPVIO*) SvANY(sv))->xio_fmt_name
904#define IoFMT_GV(sv) ((XPVIO*) SvANY(sv))->xio_fmt_gv
905#define IoBOTTOM_NAME(sv)((XPVIO*) SvANY(sv))->xio_bottom_name
906#define IoBOTTOM_GV(sv) ((XPVIO*) SvANY(sv))->xio_bottom_gv
907#define IoSUBPROCESS(sv)((XPVIO*) SvANY(sv))->xio_subprocess
908#define IoTYPE(sv) ((XPVIO*) SvANY(sv))->xio_type
909#define IoFLAGS(sv) ((XPVIO*) SvANY(sv))->xio_flags
910
50952442 911/* IoTYPE(sv) is a single character telling the type of I/O connection. */
f3c7dfbe
JH
912#define IoTYPE_RDONLY '<'
913#define IoTYPE_WRONLY '>'
914#define IoTYPE_RDWR '+'
915#define IoTYPE_APPEND 'a'
916#define IoTYPE_PIPE '|'
917#define IoTYPE_STD '-' /* stdin or stdout */
918#define IoTYPE_SOCKET 's'
919#define IoTYPE_CLOSED ' '
920#define IoTYPE_IMPLICIT 'I' /* stdin or stdout or stderr */
921#define IoTYPE_NUMERIC '#' /* fdopen */
03fcf2fc
GS
922
923/*
954c1994
GS
924=for apidoc Am|bool|SvTAINTED|SV* sv
925Checks to see if an SV is tainted. Returns TRUE if it is, FALSE if
926not.
927
928=for apidoc Am|void|SvTAINTED_on|SV* sv
702faa49 929Marks an SV as tainted if tainting is enabled.
954c1994
GS
930
931=for apidoc Am|void|SvTAINTED_off|SV* sv
932Untaints an SV. Be I<very> careful with this routine, as it short-circuits
933some of Perl's fundamental security features. XS module authors should not
934use this function unless they fully understand all the implications of
935unconditionally untainting the value. Untainting should be done in the
936standard perl fashion, via a carefully crafted regexp, rather than directly
937untainting variables.
938
939=for apidoc Am|void|SvTAINT|SV* sv
702faa49 940Taints an SV if tainting is enabled.
954c1994
GS
941
942=cut
943*/
944
0e2d6244 945#define sv_taint(sv) sv_magic((sv), NULL, PERL_MAGIC_taint, NULL, 0)
05b233af 946
bbce6d69 947#define SvTAINTED(sv) (SvMAGICAL(sv) && sv_tainted(sv))
3280af22
NIS
948#define SvTAINTED_on(sv) STMT_START{ if(PL_tainting){sv_taint(sv);} }STMT_END
949#define SvTAINTED_off(sv) STMT_START{ if(PL_tainting){sv_untaint(sv);} }STMT_END
bbce6d69 950
c6ee37c5
MB
951#define SvTAINT(sv) \
952 STMT_START { \
3280af22 953 if (PL_tainting) { \
3280af22 954 if (PL_tainted) \
c6ee37c5
MB
955 SvTAINTED_on(sv); \
956 } \
957 } STMT_END
79072805 958
954c1994
GS
959/*
960=for apidoc Am|char*|SvPV_force|SV* sv|STRLEN len
5f08bad4
ST
961Like C<SvPV> but will force the SV into containing just a string
962(C<SvPOK_only>). You want force if you are going to update the C<SvPVX>
963directly.
954c1994 964
645c22ef 965=for apidoc Am|char*|SvPV_force_nomg|SV* sv|STRLEN len
5f08bad4
ST
966Like C<SvPV> but will force the SV into containing just a string
967(C<SvPOK_only>). You want force if you are going to update the C<SvPVX>
968directly. Doesn't process magic.
645c22ef 969
954c1994 970=for apidoc Am|char*|SvPV|SV* sv|STRLEN len
5f08bad4
ST
971Returns a pointer to the string in the SV, or a stringified form of
972the SV if the SV does not contain a string. The SV may cache the
973stringified version becoming C<SvPOK>. Handles 'get' magic. See also
645c22ef
DM
974C<SvPVx> for a version which guarantees to evaluate sv only once.
975
976=for apidoc Am|char*|SvPVx|SV* sv|STRLEN len
977A version of C<SvPV> which guarantees to evaluate sv only once.
954c1994 978
891f9566
YST
979=for apidoc Am|char*|SvPV_nomg|SV* sv|STRLEN len
980Like C<SvPV> but doesn't process magic.
981
954c1994 982=for apidoc Am|char*|SvPV_nolen|SV* sv
5f08bad4
ST
983Returns a pointer to the string in the SV, or a stringified form of
984the SV if the SV does not contain a string. The SV may cache the
985stringified form becoming C<SvPOK>. Handles 'get' magic.
954c1994
GS
986
987=for apidoc Am|IV|SvIV|SV* sv
645c22ef
DM
988Coerces the given SV to an integer and returns it. See C<SvIVx> for a
989version which guarantees to evaluate sv only once.
990
891f9566
YST
991=for apidoc Am|IV|SvIV_nomg|SV* sv
992Like C<SvIV> but doesn't process magic.
993
645c22ef
DM
994=for apidoc Am|IV|SvIVx|SV* sv
995Coerces the given SV to an integer and returns it. Guarantees to evaluate
d1be9408 996sv only once. Use the more efficient C<SvIV> otherwise.
954c1994
GS
997
998=for apidoc Am|NV|SvNV|SV* sv
645c22ef
DM
999Coerce the given SV to a double and return it. See C<SvNVx> for a version
1000which guarantees to evaluate sv only once.
1001
1002=for apidoc Am|NV|SvNVx|SV* sv
1003Coerces the given SV to a double and returns it. Guarantees to evaluate
d1be9408 1004sv only once. Use the more efficient C<SvNV> otherwise.
954c1994
GS
1005
1006=for apidoc Am|UV|SvUV|SV* sv
645c22ef
DM
1007Coerces the given SV to an unsigned integer and returns it. See C<SvUVx>
1008for a version which guarantees to evaluate sv only once.
1009
891f9566
YST
1010=for apidoc Am|UV|SvUV_nomg|SV* sv
1011Like C<SvUV> but doesn't process magic.
1012
645c22ef
DM
1013=for apidoc Am|UV|SvUVx|SV* sv
1014Coerces the given SV to an unsigned integer and returns it. Guarantees to
d1be9408 1015evaluate sv only once. Use the more efficient C<SvUV> otherwise.
954c1994
GS
1016
1017=for apidoc Am|bool|SvTRUE|SV* sv
1018Returns a boolean indicating whether Perl would evaluate the SV as true or
1019false, defined or undefined. Does not handle 'get' magic.
1020
645c22ef 1021=for apidoc Am|char*|SvPVutf8_force|SV* sv|STRLEN len
b70b15d2 1022Like C<SvPV_force>, but converts sv to utf8 first if necessary.
645c22ef
DM
1023
1024=for apidoc Am|char*|SvPVutf8|SV* sv|STRLEN len
b70b15d2 1025Like C<SvPV>, but converts sv to utf8 first if necessary.
645c22ef 1026
b70b15d2
TJ
1027=for apidoc Am|char*|SvPVutf8_nolen|SV* sv
1028Like C<SvPV_nolen>, but converts sv to utf8 first if necessary.
645c22ef
DM
1029
1030=for apidoc Am|char*|SvPVbyte_force|SV* sv|STRLEN len
1031Like C<SvPV_force>, but converts sv to byte representation first if necessary.
1032
1033=for apidoc Am|char*|SvPVbyte|SV* sv|STRLEN len
1034Like C<SvPV>, but converts sv to byte representation first if necessary.
1035
b70b15d2 1036=for apidoc Am|char*|SvPVbyte_nolen|SV* sv
645c22ef
DM
1037Like C<SvPV_nolen>, but converts sv to byte representation first if necessary.
1038
1039=for apidoc Am|char*|SvPVutf8x_force|SV* sv|STRLEN len
b70b15d2 1040Like C<SvPV_force>, but converts sv to utf8 first if necessary.
d1be9408 1041Guarantees to evaluate sv only once; use the more efficient C<SvPVutf8_force>
645c22ef
DM
1042otherwise.
1043
1044=for apidoc Am|char*|SvPVutf8x|SV* sv|STRLEN len
b70b15d2 1045Like C<SvPV>, but converts sv to utf8 first if necessary.
d1be9408 1046Guarantees to evaluate sv only once; use the more efficient C<SvPVutf8>
645c22ef
DM
1047otherwise.
1048
1049=for apidoc Am|char*|SvPVbytex_force|SV* sv|STRLEN len
1050Like C<SvPV_force>, but converts sv to byte representation first if necessary.
d1be9408 1051Guarantees to evaluate sv only once; use the more efficient C<SvPVbyte_force>
645c22ef
DM
1052otherwise.
1053
1054=for apidoc Am|char*|SvPVbytex|SV* sv|STRLEN len
1055Like C<SvPV>, but converts sv to byte representation first if necessary.
d1be9408 1056Guarantees to evaluate sv only once; use the more efficient C<SvPVbyte>
645c22ef
DM
1057otherwise.
1058
fd4f854d
NC
1059=for apidoc Am|bool|SvIsCOW|SV* sv
1060Returns a boolean indicating whether the SV is Copy-On-Write. (either shared
1061hash key scalars, or full Copy On Write scalars if 5.9.0 is configured for
1062COW)
1063
1064=for apidoc Am|bool|SvIsCOW_shared_hash|SV* sv
1065Returns a boolean indicating whether the SV is Copy-On-Write shared hash key
1066scalar.
645c22ef 1067
40d34c0d
SB
1068=for apidoc Am|void|sv_catpvn_nomg|SV* sv|const char* ptr|STRLEN len
1069Like C<sv_catpvn> but doesn't process magic.
1070
1071=for apidoc Am|void|sv_setsv_nomg|SV* dsv|SV* ssv
1072Like C<sv_setsv> but doesn't process magic.
1073
1074=for apidoc Am|void|sv_catsv_nomg|SV* dsv|SV* ssv
1075Like C<sv_catsv> but doesn't process magic.
1076
954c1994
GS
1077=cut
1078*/
1079
25da4f38 1080/* Let us hope that bitmaps for UV and IV are the same */
463ee0b2 1081#define SvIV(sv) (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv))
ff68c719 1082#define SvUV(sv) (SvIOK(sv) ? SvUVX(sv) : sv_2uv(sv))
463ee0b2 1083#define SvNV(sv) (SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv))
79072805 1084
891f9566
YST
1085#define SvIV_nomg(sv) (SvIOK(sv) ? SvIVX(sv) : sv_2iv_flags(sv, 0))
1086#define SvUV_nomg(sv) (SvIOK(sv) ? SvUVX(sv) : sv_2uv_flags(sv, 0))
1087
baca2b92 1088/* ----*/
8d6d96c1 1089
8d6d96c1 1090#define SvPV(sv, lp) SvPV_flags(sv, lp, SV_GMAGIC)
8ec8dcb0 1091#define SvPV_const(sv, lp) SvPV_flags_const(sv, lp, SV_GMAGIC)
b92a0170 1092#define SvPV_mutable(sv, lp) SvPV_flags_mutable(sv, lp, SV_GMAGIC)
8d6d96c1 1093
8d6d96c1
HS
1094#define SvPV_flags(sv, lp, flags) \
1095 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
1096 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pv_flags(sv, &lp, flags))
8ec8dcb0
NC
1097#define SvPV_flags_const(sv, lp, flags) \
1098 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
1099 ? ((lp = SvCUR(sv)), SvPVX_const(sv)) : \
1100 (const char*) sv_2pv_flags(sv, &lp, flags|SV_CONST_RETURN))
c53afe68
NC
1101#define SvPV_flags_const_nolen(sv, flags) \
1102 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
1103 ? SvPVX_const(sv) : \
1104 (const char*) sv_2pv_flags(sv, 0, flags|SV_CONST_RETURN))
b92a0170
NC
1105#define SvPV_flags_mutable(sv, lp, flags) \
1106 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
1107 ? ((lp = SvCUR(sv)), SvPVX_mutable(sv)) : \
1108 sv_2pv_flags(sv, &lp, flags|SV_MUTABLE_RETURN))
79072805 1109
8d6d96c1 1110#define SvPV_force(sv, lp) SvPV_force_flags(sv, lp, SV_GMAGIC)
291a7e74 1111#define SvPV_force_nolen(sv) SvPV_force_flags_nolen(sv, SV_GMAGIC)
8ec8dcb0 1112#define SvPV_force_mutable(sv, lp) SvPV_force_flags_mutable(sv, lp, SV_GMAGIC)
baca2b92 1113
8d6d96c1 1114#define SvPV_force_nomg(sv, lp) SvPV_force_flags(sv, lp, 0)
c53afe68 1115#define SvPV_force_nomg_nolen(sv) SvPV_force_flags_nolen(sv, 0)
8d6d96c1 1116
8d6d96c1 1117#define SvPV_force_flags(sv, lp, flags) \
ff68c719 1118 ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
8d6d96c1 1119 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvn_force_flags(sv, &lp, flags))
291a7e74
NC
1120#define SvPV_force_flags_nolen(sv, flags) \
1121 ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
1122 ? SvPVX(sv) : sv_pvn_force_flags(sv, 0, flags))
8ec8dcb0
NC
1123#define SvPV_force_flags_mutable(sv, lp, flags) \
1124 ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
1125 ? ((lp = SvCUR(sv)), SvPVX_mutable(sv)) \
1126 : sv_pvn_force_flags(sv, &lp, flags|SV_MUTABLE_RETURN))
a0d0e21e 1127
1fa8b10d 1128#define SvPV_nolen(sv) \
5bc28da9 1129 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
b92a0170
NC
1130 ? SvPVX(sv) : sv_2pv_flags(sv, 0, SV_GMAGIC))
1131
1132#define SvPV_nolen_const(sv) \
1133 ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
1134 ? SvPVX_const(sv) : sv_2pv_flags(sv, 0, SV_GMAGIC|SV_CONST_RETURN))
70401c6b 1135
baca2b92 1136#define SvPV_nomg(sv, lp) SvPV_flags(sv, lp, 0)
b92a0170 1137#define SvPV_nomg_const(sv, lp) SvPV_flags_const(sv, lp, 0)
c53afe68 1138#define SvPV_nomg_const_nolen(sv) SvPV_flags_const_nolen(sv, 0)
5bc28da9 1139
baca2b92 1140/* ----*/
70401c6b 1141
5bc28da9
NIS
1142#define SvPVutf8(sv, lp) \
1143 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK|SVf_UTF8) \
1144 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvutf8(sv, &lp))
70401c6b 1145
5bc28da9 1146#define SvPVutf8_force(sv, lp) \
70401c6b 1147 ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == (SVf_POK|SVf_UTF8) \
5bc28da9
NIS
1148 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvutf8n_force(sv, &lp))
1149
baca2b92 1150
5bc28da9
NIS
1151#define SvPVutf8_nolen(sv) \
1152 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK|SVf_UTF8)\
b92a0170 1153 ? SvPVX(sv) : sv_2pvutf8(sv, 0))
70401c6b 1154
baca2b92
DM
1155/* ----*/
1156
5bc28da9
NIS
1157#define SvPVbyte(sv, lp) \
1158 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK) \
1159 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvbyte(sv, &lp))
70401c6b 1160
5bc28da9
NIS
1161#define SvPVbyte_force(sv, lp) \
1162 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST)) == (SVf_POK) \
2c2a9fc1 1163 ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvbyten_force(sv, &lp))
5bc28da9 1164
5bc28da9
NIS
1165#define SvPVbyte_nolen(sv) \
1166 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK)\
b92a0170 1167 ? SvPVX(sv) : sv_2pvbyte(sv, 0))
70401c6b 1168
1fa8b10d 1169
baca2b92
DM
1170
1171/* define FOOx(): idempotent versions of FOO(). If possible, use a local
1172 * var to evaluate the arg once; failing that, use a global if possible;
1173 * failing that, call a function to do the work
1174 */
1175
1176#define SvPVx_force(sv, lp) sv_pvn_force(sv, &lp)
1177#define SvPVutf8x_force(sv, lp) sv_pvutf8n_force(sv, &lp)
1178#define SvPVbytex_force(sv, lp) sv_pvbyten_force(sv, &lp)
1179
653fb8f3 1180#if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
baca2b92 1181
f123e731
NC
1182# define SvIVx(sv) ({SV *_sv = (SV*)(sv); SvIV(_sv); })
1183# define SvUVx(sv) ({SV *_sv = (SV*)(sv); SvUV(_sv); })
1184# define SvNVx(sv) ({SV *_sv = (SV*)(sv); SvNV(_sv); })
1185# define SvPVx(sv, lp) ({SV *_sv = (sv); SvPV(_sv, lp); })
8ec8dcb0 1186# define SvPVx_const(sv, lp) ({SV *_sv = (sv); SvPV_const(_sv, lp); })
b92a0170
NC
1187# define SvPVx_nolen(sv) ({SV *_sv = (sv); SvPV_nolen(_sv); })
1188# define SvPVx_nolen_const(sv) ({SV *_sv = (sv); SvPV_nolen_const(_sv); })
f123e731
NC
1189# define SvPVutf8x(sv, lp) ({SV *_sv = (sv); SvPVutf8(_sv, lp); })
1190# define SvPVbytex(sv, lp) ({SV *_sv = (sv); SvPVbyte(_sv, lp); })
b92a0170 1191# define SvPVbytex_nolen(sv) ({SV *_sv = (sv); SvPVbyte_nolen(_sv); })
a80f87c4 1192# define SvTRUE(sv) ( \
8990e307
LW
1193 !sv \
1194 ? 0 \
1195 : SvPOK(sv) \
a80f87c4
GS
1196 ? (({XPV *nxpv = (XPV*)SvANY(sv); \
1197 nxpv && \
c2f1de04 1198 (nxpv->xpv_cur > 1 || \
a80f87c4 1199 (nxpv->xpv_cur && *nxpv->xpv_pv != '0')); }) \
79072805
LW
1200 ? 1 \
1201 : 0) \
1202 : \
1203 SvIOK(sv) \
463ee0b2 1204 ? SvIVX(sv) != 0 \
79072805 1205 : SvNOK(sv) \
463ee0b2
LW
1206 ? SvNVX(sv) != 0.0 \
1207 : sv_2bool(sv) )
f123e731 1208# define SvTRUEx(sv) ({SV *_sv = (sv); SvTRUE(_sv); })
baca2b92 1209
a80f87c4 1210#else /* __GNUC__ */
baca2b92
DM
1211
1212# ifdef USE_5005THREADS
1213# define SvIVx(sv) sv_iv(sv)
1214# define SvUVx(sv) sv_uv(sv)
1215# define SvNVx(sv) sv_nv(sv)
1216# define SvPVx(sv, lp) sv_pvn(sv, &lp)
1217# define SvPVutf8x(sv, lp) sv_pvutf8n(sv, &lp)
1218# define SvPVbytex(sv, lp) sv_pvbyten(sv, &lp)
1219# define SvTRUE(sv) SvTRUEx(sv)
1220# define SvTRUEx(sv) sv_true(sv)
1221
1222# else /* USE_5005THREADS */
1223
a80f87c4
GS
1224/* These inlined macros use globals, which will require a thread
1225 * declaration in user code, so we avoid them under threads */
1226
baca2b92
DM
1227# define SvIVx(sv) ((PL_Sv = (sv)), SvIV(PL_Sv))
1228# define SvUVx(sv) ((PL_Sv = (sv)), SvUV(PL_Sv))
1229# define SvNVx(sv) ((PL_Sv = (sv)), SvNV(PL_Sv))
1230# define SvPVx(sv, lp) ((PL_Sv = (sv)), SvPV(PL_Sv, lp))
8ec8dcb0 1231# define SvPVx_const(sv, lp) ((PL_Sv = (sv)), SvPV_const(PL_Sv, lp))
b92a0170
NC
1232# define SvPVx_nolen(sv) ((PL_Sv = (sv)), SvPV_nolen(PL_Sv))
1233# define SvPVx_nolen_const(sv) ((PL_Sv = (sv)), SvPV_nolen_const(PL_Sv))
baca2b92
DM
1234# define SvPVutf8x(sv, lp) ((PL_Sv = (sv)), SvPVutf8(PL_Sv, lp))
1235# define SvPVbytex(sv, lp) ((PL_Sv = (sv)), SvPVbyte(PL_Sv, lp))
b92a0170 1236# define SvPVbytex_nolen(sv) ((PL_Sv = (sv)), SvPVbyte_nolen(PL_Sv))
baca2b92 1237# define SvTRUE(sv) ( \
a80f87c4
GS
1238 !sv \
1239 ? 0 \
1240 : SvPOK(sv) \
6b88bc9c 1241 ? ((PL_Xpv = (XPV*)SvANY(sv)) && \
c2f1de04 1242 (PL_Xpv->xpv_cur > 1 || \
6b88bc9c 1243 (PL_Xpv->xpv_cur && *PL_Xpv->xpv_pv != '0')) \
a80f87c4
GS
1244 ? 1 \
1245 : 0) \
1246 : \
1247 SvIOK(sv) \
1248 ? SvIVX(sv) != 0 \
1249 : SvNOK(sv) \
1250 ? SvNVX(sv) != 0.0 \
1251 : sv_2bool(sv) )
baca2b92
DM
1252# define SvTRUEx(sv) ((PL_Sv = (sv)), SvTRUE(PL_Sv))
1253# endif /* USE_5005THREADS */
1254#endif /* __GNU__ */
1255
71799650
NC
1256#define SvIsCOW(sv) ((SvFLAGS(sv) & (SVf_FAKE | SVf_READONLY)) == \
1257 (SVf_FAKE | SVf_READONLY))
1258#define SvIsCOW_shared_hash(sv) (SvIsCOW(sv) && SvLEN(sv) == 0)
baca2b92 1259
2a979b61
NC
1260#define SvSHARED_HASH(sv) (0 + SvUVX(sv))
1261
baca2b92
DM
1262/* flag values for sv_*_flags functions */
1263#define SV_IMMEDIATE_UNREF 1
1264#define SV_GMAGIC 2
6563884d
JH
1265#define SV_COW_DROP_PV 4 /* Unused in Perl 5.8.x */
1266#define SV_UTF8_NO_ENCODING 8
65daad90 1267#define SV_NOSTEAL 16
8ec8dcb0
NC
1268#define SV_CONST_RETURN 32
1269#define SV_MUTABLE_RETURN 64
66067514 1270#define SV_SMAGIC 128
baca2b92 1271
05b233af
NC
1272#define sv_unref(sv) sv_unref_flags(sv, 0)
1273#define sv_force_normal(sv) sv_force_normal_flags(sv, 0)
1274
baca2b92
DM
1275/* all these 'functions' are now just macros */
1276
1277#define sv_pv(sv) SvPV_nolen(sv)
1278#define sv_pvutf8(sv) SvPVutf8_nolen(sv)
1279#define sv_pvbyte(sv) SvPVbyte_nolen(sv)
1280
1281#define sv_pvn_force_nomg(sv, lp) sv_pvn_force_flags(sv, lp, 0)
1282#define sv_utf8_upgrade_nomg(sv) sv_utf8_upgrade_flags(sv, 0)
1283#define sv_catpvn_nomg(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, 0)
1284#define sv_setsv(dsv, ssv) sv_setsv_flags(dsv, ssv, SV_GMAGIC)
1285#define sv_setsv_nomg(dsv, ssv) sv_setsv_flags(dsv, ssv, 0)
1286#define sv_catsv(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC)
1287#define sv_catsv_nomg(dsv, ssv) sv_catsv_flags(dsv, ssv, 0)
66067514 1288#define sv_catsv_mg(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC|SV_SMAGIC)
baca2b92 1289#define sv_catpvn(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC)
66067514
NC
1290#define sv_catpvn_mg(sv, sstr, slen) \
1291 sv_catpvn_flags(sv, sstr, slen, SV_GMAGIC|SV_SMAGIC);
baca2b92 1292#define sv_2pv(sv, lp) sv_2pv_flags(sv, lp, SV_GMAGIC)
66067514
NC
1293#define sv_2pv_nolen(sv) sv_2pv(sv, 0)
1294#define sv_2pvbyte_nolen(sv) sv_2pvbyte(sv, 0)
1295#define sv_2pvutf8_nolen(sv) sv_2pvutf8(sv, 0)
baca2b92
DM
1296#define sv_2pv_nomg(sv, lp) sv_2pv_flags(sv, lp, 0)
1297#define sv_pvn_force(sv, lp) sv_pvn_force_flags(sv, lp, SV_GMAGIC)
1298#define sv_utf8_upgrade(sv) sv_utf8_upgrade_flags(sv, SV_GMAGIC)
891f9566
YST
1299#define sv_2iv(sv) sv_2iv_flags(sv, SV_GMAGIC)
1300#define sv_2uv(sv) sv_2uv_flags(sv, SV_GMAGIC)
baca2b92 1301
5835a535
JH
1302/* Should be named SvCatPVN_utf8_upgrade? */
1303#define sv_catpvn_utf8_upgrade(dsv, sstr, slen, nsv) \
1304 STMT_START { \
1305 if (!(nsv)) \
1306 nsv = sv_2mortal(newSVpvn(sstr, slen)); \
1307 else \
1308 sv_setpvn(nsv, sstr, slen); \
1309 SvUTF8_off(nsv); \
1310 sv_utf8_upgrade(nsv); \
1311 sv_catsv(dsv, nsv); \
1312 } STMT_END
79072805 1313
954c1994
GS
1314/*
1315=for apidoc Am|SV*|newRV_inc|SV* sv
1316
1317Creates an RV wrapper for an SV. The reference count for the original SV is
1318incremented.
1319
1320=cut
1321*/
1322
5f05dabc 1323#define newRV_inc(sv) newRV(sv)
5f05dabc 1324
ef50df4b 1325/* the following macros update any magic values this sv is associated with */
79072805 1326
954c1994 1327/*
ccfc67b7
JH
1328=head1 Magical Functions
1329
954c1994
GS
1330=for apidoc Am|void|SvGETMAGIC|SV* sv
1331Invokes C<mg_get> on an SV if it has 'get' magic. This macro evaluates its
1332argument more than once.
1333
1334=for apidoc Am|void|SvSETMAGIC|SV* sv
1335Invokes C<mg_set> on an SV if it has 'set' magic. This macro evaluates its
1336argument more than once.
1337
1338=for apidoc Am|void|SvSetSV|SV* dsb|SV* ssv
1339Calls C<sv_setsv> if dsv is not the same as ssv. May evaluate arguments
1340more than once.
1341
1342=for apidoc Am|void|SvSetSV_nosteal|SV* dsv|SV* ssv
1343Calls a non-destructive version of C<sv_setsv> if dsv is not the same as
1344ssv. May evaluate arguments more than once.
1345
645c22ef
DM
1346=for apidoc Am|void|SvSetMagicSV|SV* dsb|SV* ssv
1347Like C<SvSetSV>, but does any set magic required afterwards.
1348
1349=for apidoc Am|void|SvSetMagicSV_nosteal|SV* dsv|SV* ssv
40d34c0d 1350Like C<SvSetSV_nosteal>, but does any set magic required afterwards.
645c22ef 1351
68795e93
NIS
1352=for apidoc Am|void|SvSHARE|SV* sv
1353Arranges for sv to be shared between threads if a suitable module
1354has been loaded.
1355
1356=for apidoc Am|void|SvLOCK|SV* sv
1357Arranges for a mutual exclusion lock to be obtained on sv if a suitable module
1358has been loaded.
1359
1360=for apidoc Am|void|SvUNLOCK|SV* sv
1361Releases a mutual exclusion lock on sv if a suitable module
1362has been loaded.
1363
ccfc67b7
JH
1364=head1 SV Manipulation Functions
1365
679ac26e 1366=for apidoc Am|char *|SvGROW|SV* sv|STRLEN len
954c1994
GS
1367Expands the character buffer in the SV so that it has room for the
1368indicated number of bytes (remember to reserve space for an extra trailing
8cf8f3d1 1369NUL character). Calls C<sv_grow> to perform the expansion if necessary.
954c1994
GS
1370Returns a pointer to the character buffer.
1371
1372=cut
1373*/
1374
68795e93
NIS
1375#define SvSHARE(sv) CALL_FPTR(PL_sharehook)(aTHX_ sv)
1376#define SvLOCK(sv) CALL_FPTR(PL_lockhook)(aTHX_ sv)
1377#define SvUNLOCK(sv) CALL_FPTR(PL_unlockhook)(aTHX_ sv)
1378
189b2af5
GS
1379#define SvGETMAGIC(x) STMT_START { if (SvGMAGICAL(x)) mg_get(x); } STMT_END
1380#define SvSETMAGIC(x) STMT_START { if (SvSMAGICAL(x)) mg_set(x); } STMT_END
79072805 1381
54310121 1382#define SvSetSV_and(dst,src,finally) \
189b2af5 1383 STMT_START { \
54310121 1384 if ((dst) != (src)) { \
1385 sv_setsv(dst, src); \
1386 finally; \
189b2af5
GS
1387 } \
1388 } STMT_END
54310121 1389#define SvSetSV_nosteal_and(dst,src,finally) \
189b2af5 1390 STMT_START { \
8ebc5c01 1391 if ((dst) != (src)) { \
65daad90 1392 sv_setsv_flags(dst, src, SV_GMAGIC | SV_NOSTEAL); \
54310121 1393 finally; \
189b2af5
GS
1394 } \
1395 } STMT_END
79072805 1396
54310121 1397#define SvSetSV(dst,src) \
c9d716f7 1398 SvSetSV_and(dst,src,/*nothing*/;)
54310121 1399#define SvSetSV_nosteal(dst,src) \
c9d716f7 1400 SvSetSV_nosteal_and(dst,src,/*nothing*/;)
54310121 1401
1402#define SvSetMagicSV(dst,src) \
1403 SvSetSV_and(dst,src,SvSETMAGIC(dst))
1404#define SvSetMagicSV_nosteal(dst,src) \
1405 SvSetSV_nosteal_and(dst,src,SvSETMAGIC(dst))
1406
5835a535 1407
1045810a 1408#if !defined(SKIP_DEBUGGING)
79072805 1409#define SvPEEK(sv) sv_peek(sv)
3967c732
JD
1410#else
1411#define SvPEEK(sv) ""
1412#endif
79072805 1413
653fb8f3 1414#define SvIMMORTAL(sv) ((sv)==&PL_sv_undef || (sv)==&PL_sv_yes || (sv)==&PL_sv_no || (sv)==&PL_sv_placeholder)
36477c24 1415
3280af22 1416#define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
54310121 1417
79072805
LW
1418#define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
1419
933fea7f 1420#define SvGROW(sv,len) (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
71a0dd65
NC
1421#define SvGROW_mutable(sv,len) \
1422 (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX_mutable(sv))
933fea7f 1423#define Sv_Grow sv_grow
3d35f11b 1424
a0739874
DM
1425#define CLONEf_COPY_STACKS 1
1426#define CLONEf_KEEP_PTR_TABLE 2
c43294b8 1427#define CLONEf_CLONE_HOST 4
457d3ae8 1428#define CLONEf_JOIN_IN 8
a0739874 1429
8cf8f3d1 1430struct clone_params {
d2d73c3e
AB
1431 AV* stashes;
1432 UV flags;
59b40662 1433 PerlInterpreter *proto_perl;
8cf8f3d1 1434};
b4cc2f95
JH
1435
1436#define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) sv_force_normal(sv)