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