This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate:
[perl5.git] / sv.h
1 /*    sv.h
2  *
3  *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4  *    2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others
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  *
9  */
10
11 #ifdef sv_flags
12 #undef sv_flags         /* Convex has this in <signal.h> for sigvec() */
13 #endif
14
15 /*
16 =head1 SV Flags
17
18 =for apidoc AmU||svtype
19 An enum of flags for Perl types.  These are found in the file B<sv.h>
20 in the C<svtype> enum.  Test these flags with the C<SvTYPE> macro.
21
22 =for apidoc AmU||SVt_PV
23 Pointer type flag for scalars.  See C<svtype>.
24
25 =for apidoc AmU||SVt_IV
26 Integer type flag for scalars.  See C<svtype>.
27
28 =for apidoc AmU||SVt_NV
29 Double type flag for scalars.  See C<svtype>.
30
31 =for apidoc AmU||SVt_PVMG
32 Type flag for blessed scalars.  See C<svtype>.
33
34 =for apidoc AmU||SVt_PVAV
35 Type flag for arrays.  See C<svtype>.
36
37 =for apidoc AmU||SVt_PVHV
38 Type flag for hashes.  See C<svtype>.
39
40 =for apidoc AmU||SVt_PVCV
41 Type flag for code refs.  See C<svtype>.
42
43 =cut
44 */
45
46 typedef enum {
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 */
62         SVt_PVIO,       /* 15 */
63         SVt_LAST        /* keep last in enum. used to size arrays */
64 } svtype;
65
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.  */
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
79 /* Using C's structural equivalence to help emulate C++ inheritance here... */
80
81 struct STRUCT_SV {              /* struct sv { */
82     void*       sv_any;         /* pointer to something */
83     U32         sv_refcnt;      /* how many references to us */
84     U32         sv_flags;       /* what we are */
85 };
86
87 struct gv {
88     XPVGV*      sv_any;         /* pointer to something */
89     U32         sv_refcnt;      /* how many references to us */
90     U32         sv_flags;       /* what we are */
91 };
92
93 struct cv {
94     XPVCV*      sv_any;         /* pointer to something */
95     U32         sv_refcnt;      /* how many references to us */
96     U32         sv_flags;       /* what we are */
97 };
98
99 struct av {
100     XPVAV*      sv_any;         /* pointer to something */
101     U32         sv_refcnt;      /* how many references to us */
102     U32         sv_flags;       /* what we are */
103 };
104
105 struct hv {
106     XPVHV*      sv_any;         /* pointer to something */
107     U32         sv_refcnt;      /* how many references to us */
108     U32         sv_flags;       /* what we are */
109 };
110
111 struct io {
112     XPVIO*      sv_any;         /* pointer to something */
113     U32         sv_refcnt;      /* how many references to us */
114     U32         sv_flags;       /* what we are */
115 };
116
117 /*
118 =head1 SV Manipulation Functions
119
120 =for apidoc Am|U32|SvREFCNT|SV* sv
121 Returns the value of the object's reference count.
122
123 =for apidoc Am|SV*|SvREFCNT_inc|SV* sv
124 Increments the reference count of the given SV.
125
126 All of the following SvREFCNT_inc* macros are optimized versions of
127 SvREFCNT_inc, and can be replaced with SvREFCNT_inc.
128
129 =for apidoc Am|SV*|SvREFCNT_inc_NN|SV* sv
130 Same as SvREFCNT_inc, but can only be used if you know I<sv>
131 is not NULL.  Since we don't have to check the NULLness, it's faster
132 and smaller.
133
134 =for apidoc Am|SV*|SvREFCNT_inc_void|SV* sv
135 Same as SvREFCNT_inc, but can only be used if you don't need the
136 return value.  The macro doesn't need to return a meaningful value.
137
138 =for apidoc Am|SV*|SvREFCNT_inc_void_NN|SV* sv
139 Same as SvREFCNT_inc, but can only be used if you don't need the return
140 value, and you know that I<sv> is not NULL.  The macro doesn't need
141 to return a meaningful value, or check for NULLness, so it's smaller
142 and faster.
143
144 =for apidoc Am|SV*|SvREFCNT_inc_simple|SV* sv
145 Same as SvREFCNT_inc, but can only be used with simple variables, not
146 expressions or pointer dereferences.  Since we don't have to store a
147 temporary value, it's faster.
148
149 =for apidoc Am|SV*|SvREFCNT_inc_simple_NN|SV* sv
150 Same as SvREFCNT_inc_simple, but can only be used if you know I<sv>
151 is not NULL.  Since we don't have to check the NULLness, it's faster
152 and smaller.
153
154 =for apidoc Am|SV*|SvREFCNT_inc_simple_void|SV* sv
155 Same as SvREFCNT_inc_simple, but can only be used if you don't need the
156 return value.  The macro doesn't need to return a meaningful value.
157
158 =for apidoc Am|SV*|SvREFCNT_inc|SV* sv
159 Increments the reference count of the given SV.
160
161 =for apidoc Am|void|SvREFCNT_dec|SV* sv
162 Decrements the reference count of the given SV.
163
164 =for apidoc Am|svtype|SvTYPE|SV* sv
165 Returns the type of the SV.  See C<svtype>.
166
167 =for apidoc Am|void|SvUPGRADE|SV* sv|svtype type
168 Used to upgrade an SV to a more complex form.  Uses C<sv_upgrade> to
169 perform the upgrade if necessary.  See C<svtype>.
170
171 =cut
172 */
173
174 #define SvANY(sv)       (sv)->sv_any
175 #define SvFLAGS(sv)     (sv)->sv_flags
176 #define SvREFCNT(sv)    (sv)->sv_refcnt
177
178 #ifdef USE_5005THREADS
179
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 */
200 #else
201 #  define ATOMIC_INC(count) (++count)
202 #  define ATOMIC_DEC_AND_TEST(res, count) (res = (--count == 0))
203 #endif /* USE_5005THREADS */
204
205 #if defined(__GNUC__) && !defined(__STRICT_ANSI__) && !defined(PERL_GCC_PEDANTIC)
206 #  define SvREFCNT_inc(sv)              \
207     ({                                  \
208         SV * const _sv = (SV*)(sv);     \
209         if (_sv)                        \
210              ATOMIC_INC(SvREFCNT(_sv)); \
211         _sv;                            \
212     })
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     })
231 #else
232 #  ifdef USE_5005THREADS
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
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)
242 #  else
243 #    define SvREFCNT_inc(sv)    \
244         ((PL_Sv=(SV*)(sv)) ? (ATOMIC_INC(SvREFCNT(PL_Sv)), (SV*)PL_Sv) : NULL)
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)
251 #  endif
252 #endif
253
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
260 #define SvREFCNT_dec(sv)        sv_free((SV*)(sv))
261
262 #define SVTYPEMASK      0xff
263 #define SvTYPE(sv)      ((svtype)((sv)->sv_flags & SVTYPEMASK))
264
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
270 #define SvUPGRADE(sv, mt) (SvTYPE(sv) >= mt || sv_upgrade(sv, mt))
271
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
312
313 #define SVf_THINKFIRST  (SVf_READONLY|SVf_ROK|SVf_FAKE)
314
315 #define SVf_OK          (SVf_IOK|SVf_NOK|SVf_POK|SVf_ROK| \
316                          SVp_IOK|SVp_NOK|SVp_POK)
317
318 #define PRIVSHIFT 8     /* (SVp_?OK >> PRIVSHIFT) == SVf_?OK */
319
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 */
327
328 /* Some private flags. */
329
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 */
339 #define SVpbm_TAIL      0x40000000
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 */
356
357 typedef struct {
358     IV          xiv_iv;         /* integer value or pv offset */
359 } xiv_allocated;
360
361 typedef struct {
362     NV          xnv_nv;         /* numeric value, if any */
363 } xnv_allocated;
364
365 struct xrv {
366     SV *        xrv_rv;         /* pointer to another SV */
367 };
368
369 struct xpv {
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 */
373 };
374
375 typedef struct xpv xpv_allocated;
376
377 struct xpviv {
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 */
381     IV          xiv_iv;         /* integer value or pv offset */
382 };
383
384 typedef struct xpviv xpviv_allocated;
385
386 struct 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
393 struct xpvnv {
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 */
397     IV          xiv_iv;         /* integer value or pv offset */
398     NV          xnv_nv;         /* numeric value, if any */
399 };
400
401 /* These structure must match the beginning of struct xpvhv in hv.h. */
402 struct xpvmg {
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 */
406     IV          xiv_iv;         /* integer value or pv offset */
407     NV          xnv_nv;         /* numeric value, if any */
408     MAGIC*      xmg_magic;      /* linked list of magicalness */
409     HV*         xmg_stash;      /* class package */
410 };
411
412 struct xpvlv {
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 */
416     IV          xiv_iv;         /* integer value or pv offset */
417     NV          xnv_nv;         /* numeric value, if any */
418     MAGIC*      xmg_magic;      /* linked list of magicalness */
419     HV*         xmg_stash;      /* class package */
420
421     STRLEN      xlv_targoff;
422     STRLEN      xlv_targlen;
423     SV*         xlv_targ;
424     char        xlv_type;       /* k=keys .=pos x=substr v=vec /=join/re
425                                  * y=alem/helem/iter t=tie T=tied HE */
426 };
427
428 struct xpvgv {
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 */
432     IV          xiv_iv;         /* integer value or pv offset */
433     NV          xnv_nv;         /* numeric value, if any */
434     MAGIC*      xmg_magic;      /* linked list of magicalness */
435     HV*         xmg_stash;      /* class package */
436
437     GP*         xgv_gp;
438     char*       xgv_name;
439     STRLEN      xgv_namelen;
440     HV*         xgv_stash;
441     U8          xgv_flags;
442 };
443
444 struct xpvbm {
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 */
448     IV          xiv_iv;         /* integer value or pv offset */
449     NV          xnv_nv;         /* numeric value, if any */
450     MAGIC*      xmg_magic;      /* linked list of magicalness */
451     HV*         xmg_stash;      /* class package */
452
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
458 /* This structure must match XPVCV in cv.h */
459
460 typedef U16 cv_flags_t;
461
462 struct xpvfm {
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 */
466     IV          xiv_iv;         /* integer value or pv offset */
467     NV          xnv_nv;         /* numeric value, if any */
468     MAGIC*      xmg_magic;      /* linked list of magicalness */
469     HV*         xmg_stash;      /* class package */
470
471     HV *        xcv_stash;
472     OP *        xcv_start;
473     OP *        xcv_root;
474     void      (*xcv_xsub)(pTHX_ CV*);
475     ANY         xcv_xsubany;
476     GV *        xcv_gv;
477     char *      xcv_file;
478     long        xcv_depth;      /* >= 2 indicates recursive call */
479     AV *        xcv_padlist;
480     CV *        xcv_outside;
481 #ifdef USE_5005THREADS
482     perl_mutex *xcv_mutexp;     /* protects xcv_owner */
483     struct perl_thread *xcv_owner;      /* current owner thread */
484 #endif /* USE_5005THREADS */
485     cv_flags_t  xcv_flags;
486     U32         xcv_outside_seq; /* the COP sequence (at the point of our
487                                   * compilation) in the lexically enclosing
488                                   * sub */
489     IV          xfm_lines;
490 };
491
492 struct 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 */
496     IV          xiv_iv;         /* integer value or pv offset */
497     NV          xnv_nv;         /* numeric value, if any */
498     MAGIC*      xmg_magic;      /* linked list of magicalness */
499     HV*         xmg_stash;      /* class package */
500
501     PerlIO *    xio_ifp;        /* ifp and ofp are normally the same */
502     PerlIO *    xio_ofp;        /* but sockets need separate streams */
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;
516     IV          xio_lines;      /* $. */
517     IV          xio_page;       /* $% */
518     IV          xio_page_len;   /* $= */
519     IV          xio_lines_left; /* $- */
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 };
530 #define xio_dirp        xio_dirpu.xiou_dirp
531 #define xio_any         xio_dirpu.xiou_any
532
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) */
540
541 /* The following macros define implementation-independent predicates on SVs. */
542
543 /*
544 =for apidoc Am|bool|SvNIOK|SV* sv
545 Returns a boolean indicating whether the SV contains a number, integer or
546 double.
547
548 =for apidoc Am|bool|SvNIOKp|SV* sv
549 Returns a boolean indicating whether the SV contains a number, integer or
550 double.  Checks the B<private> setting.  Use C<SvNIOK>.
551
552 =for apidoc Am|void|SvNIOK_off|SV* sv
553 Unsets the NV/IV status of an SV.
554
555 =for apidoc Am|bool|SvOK|SV* sv
556 Returns a boolean indicating whether the value is an SV. It also tells
557 whether the value is defined or not.
558
559 =for apidoc Am|bool|SvIOKp|SV* sv
560 Returns a boolean indicating whether the SV contains an integer.  Checks
561 the B<private> setting.  Use C<SvIOK>.
562
563 =for apidoc Am|bool|SvNOKp|SV* sv
564 Returns a boolean indicating whether the SV contains a double.  Checks the
565 B<private> setting.  Use C<SvNOK>.
566
567 =for apidoc Am|bool|SvPOKp|SV* sv
568 Returns a boolean indicating whether the SV contains a character string.
569 Checks the B<private> setting.  Use C<SvPOK>.
570
571 =for apidoc Am|bool|SvIOK|SV* sv
572 Returns a boolean indicating whether the SV contains an integer.
573
574 =for apidoc Am|void|SvIOK_on|SV* sv
575 Tells an SV that it is an integer.
576
577 =for apidoc Am|void|SvIOK_off|SV* sv
578 Unsets the IV status of an SV.
579
580 =for apidoc Am|void|SvIOK_only|SV* sv
581 Tells an SV that it is an integer and disables all other OK bits.
582
583 =for apidoc Am|void|SvIOK_only_UV|SV* sv
584 Tells and SV that it is an unsigned integer and disables all other OK bits.
585
586 =for apidoc Am|bool|SvIOK_UV|SV* sv
587 Returns a boolean indicating whether the SV contains an unsigned integer.
588
589 =for apidoc Am|void|SvUOK|SV* sv
590 Returns a boolean indicating whether the SV contains an unsigned integer.
591
592 =for apidoc Am|bool|SvIOK_notUV|SV* sv
593 Returns a boolean indicating whether the SV contains a signed integer.
594
595 =for apidoc Am|bool|SvNOK|SV* sv
596 Returns a boolean indicating whether the SV contains a double.
597
598 =for apidoc Am|void|SvNOK_on|SV* sv
599 Tells an SV that it is a double.
600
601 =for apidoc Am|void|SvNOK_off|SV* sv
602 Unsets the NV status of an SV.
603
604 =for apidoc Am|void|SvNOK_only|SV* sv
605 Tells an SV that it is a double and disables all other OK bits.
606
607 =for apidoc Am|bool|SvPOK|SV* sv
608 Returns a boolean indicating whether the SV contains a character
609 string.
610
611 =for apidoc Am|void|SvPOK_on|SV* sv
612 Tells an SV that it is a string.
613
614 =for apidoc Am|void|SvPOK_off|SV* sv
615 Unsets the PV status of an SV.
616
617 =for apidoc Am|void|SvPOK_only|SV* sv
618 Tells an SV that it is a string and disables all other OK bits.
619 Will also turn off the UTF-8 status.
620
621 =for apidoc Am|bool|SvOOK|SV* sv
622 Returns a boolean indicating whether the SvIVX is a valid offset value for
623 the SvPVX.  This hack is used internally to speed up removal of characters
624 from the beginning of a SvPV.  When SvOOK is true, then the start of the
625 allocated string buffer is really (SvPVX - SvIVX).
626
627 =for apidoc Am|bool|SvROK|SV* sv
628 Tests if the SV is an RV.
629
630 =for apidoc Am|void|SvROK_on|SV* sv
631 Tells an SV that it is an RV.
632
633 =for apidoc Am|void|SvROK_off|SV* sv
634 Unsets the RV status of an SV.
635
636 =for apidoc Am|SV*|SvRV|SV* sv
637 Dereferences an RV to return the SV.
638
639 =for apidoc Am|IV|SvIVX|SV* sv
640 Returns the raw value in the SV's IV slot, without checks or conversions.
641 Only use when you are sure SvIOK is true. See also C<SvIV()>.
642
643 =for apidoc Am|UV|SvUVX|SV* sv
644 Returns the raw value in the SV's UV slot, without checks or conversions.
645 Only use when you are sure SvIOK is true. See also C<SvUV()>.
646
647 =for apidoc Am|NV|SvNVX|SV* sv
648 Returns the raw value in the SV's NV slot, without checks or conversions.
649 Only use when you are sure SvNOK is true. See also C<SvNV()>.
650
651 =for apidoc Am|char*|SvPVX|SV* sv
652 Returns a pointer to the physical string in the SV.  The SV must contain a
653 string.
654
655 =for apidoc Am|STRLEN|SvCUR|SV* sv
656 Returns the length of the string which is in the SV.  See C<SvLEN>.
657
658 =for apidoc Am|STRLEN|SvLEN|SV* sv
659 Returns the size of the string buffer in the SV, not including any part
660 attributable to C<SvOOK>.  See C<SvCUR>.
661
662 =for apidoc Am|char*|SvEND|SV* sv
663 Returns a pointer to the last character in the string which is in the SV.
664 See C<SvCUR>.  Access the character as *(SvEND(sv)).
665
666 =for apidoc Am|HV*|SvSTASH|SV* sv
667 Returns the stash of the SV.
668
669 =for apidoc Am|void|SvIV_set|SV* sv|IV val
670 Set the value of the IV pointer in sv to val.  It is possible to perform
671 the same function of this macro with an lvalue assignment to C<SvIVX>.
672 With future Perls, however, it will be more efficient to use 
673 C<SvIV_set> instead of the lvalue assignment to C<SvIVX>.
674
675 =for apidoc Am|void|SvNV_set|SV* sv|NV val
676 Set the value of the NV pointer in sv to val.  See C<SvIV_set>.
677
678 =for apidoc Am|void|SvPV_set|SV* sv|char* val
679 Set the value of the PV pointer in sv to val.  See C<SvIV_set>.
680
681 =for apidoc Am|void|SvUV_set|SV* sv|UV val
682 Set the value of the UV pointer in sv to val.  See C<SvIV_set>.
683
684 =for apidoc Am|void|SvRV_set|SV* sv|SV* val
685 Set the value of the RV pointer in sv to val.  See C<SvIV_set>.
686
687 =for apidoc Am|void|SvMAGIC_set|SV* sv|MAGIC* val
688 Set the value of the MAGIC pointer in sv to val.  See C<SvIV_set>.
689
690 =for apidoc Am|void|SvSTASH_set|SV* sv|HV* val
691 Set the value of the STASH pointer in sv to val.  See C<SvIV_set>.
692
693 =for apidoc Am|void|SvCUR_set|SV* sv|STRLEN len
694 Set the current length of the string which is in the SV.  See C<SvCUR>
695 and C<SvIV_set>.
696
697 =for apidoc Am|void|SvLEN_set|SV* sv|STRLEN len
698 Set the actual length of the string which is in the SV.  See C<SvIV_set>.
699
700 =cut
701 */
702
703 #define SvNIOK(sv)              (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
704 #define SvNIOKp(sv)             (SvFLAGS(sv) & (SVp_IOK|SVp_NOK))
705 #define SvNIOK_off(sv)          (SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK| \
706                                                   SVp_IOK|SVp_NOK|SVf_IVisUV))
707
708 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
709 #define assert_not_ROK(sv)      ({assert(!SvROK(sv) || !SvRV(sv));}),
710 #else
711 #define assert_not_ROK(sv)      
712 #endif
713
714 #define SvOK(sv)                (SvFLAGS(sv) & SVf_OK)
715 #define SvOK_off(sv)            (assert_not_ROK(sv)                     \
716                                  SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|    \
717                                                   SVf_IVisUV|SVf_UTF8), \
718                                                         SvOOK_off(sv))
719 #define SvOK_off_exc_UV(sv)     (assert_not_ROK(sv)                     \
720                                  SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|    \
721                                                   SVf_UTF8),            \
722                                                         SvOOK_off(sv))
723
724 #define SvOKp(sv)               (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
725 #define SvIOKp(sv)              (SvFLAGS(sv) & SVp_IOK)
726 #define SvIOKp_on(sv)           ((void)SvOOK_off(sv), SvFLAGS(sv) |= SVp_IOK)
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)
730 #define SvPOKp_on(sv)           (assert_not_ROK(sv)                     \
731                                  SvFLAGS(sv) |= SVp_POK)
732
733 #define SvIOK(sv)               (SvFLAGS(sv) & SVf_IOK)
734 #define SvIOK_on(sv)            ((void)SvOOK_off(sv), \
735                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
736 #define SvIOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV))
737 #define SvIOK_only(sv)          (SvOK_off(sv), \
738                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
739 #define SvIOK_only_UV(sv)       (SvOK_off_exc_UV(sv), \
740                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
741
742 #define SvIOK_UV(sv)            ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))   \
743                                  == (SVf_IOK|SVf_IVisUV))
744 #define SvUOK(sv)               SvIOK_UV(sv)
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)
751
752 #define SvNOK(sv)               (SvFLAGS(sv) & SVf_NOK)
753 #define SvNOK_on(sv)            (SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
754 #define SvNOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_NOK|SVp_NOK))
755 #define SvNOK_only(sv)          (SvOK_off(sv), \
756                                     SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
757
758 /*
759 =for apidoc Am|bool|SvUTF8|SV* sv
760 Returns a boolean indicating whether the SV contains UTF-8 encoded data.
761 Call this after SvPV() in case any call to string overloading updates the
762 internal flag.
763
764 =for apidoc Am|void|SvUTF8_on|SV *sv
765 Turn on the UTF-8 status of an SV (the data is not changed, just the flag).
766 Do not use frivolously.
767
768 =for apidoc Am|void|SvUTF8_off|SV *sv
769 Unsets the UTF-8 status of an SV.
770
771 =for apidoc Am|void|SvPOK_only_UTF8|SV* sv
772 Tells an SV that it is a string and disables all other OK bits,
773 and leaves the UTF-8 status as it was.
774
775 =cut
776  */
777
778 /* Ensure the return value of this macro does not clash with the GV_ADD* flags
779 in gv.h: */
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))
783
784 #define SvPOK(sv)               (SvFLAGS(sv) & SVf_POK)
785 #define SvPOK_on(sv)            (assert_not_ROK(sv)                     \
786                                  SvFLAGS(sv) |= (SVf_POK|SVp_POK))
787 #define SvPOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_POK|SVp_POK))
788 #define SvPOK_only(sv)          (assert_not_ROK(sv)                     \
789                                  SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|    \
790                                                   SVf_IVisUV|SVf_UTF8), \
791                                     SvFLAGS(sv) |= (SVf_POK|SVp_POK))
792 #define SvPOK_only_UTF8(sv)     (assert_not_ROK(sv)                     \
793                                  SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|    \
794                                                   SVf_IVisUV),          \
795                                     SvFLAGS(sv) |= (SVf_POK|SVp_POK))
796
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
803 #define SvOOK(sv)               (SvFLAGS(sv) & SVf_OOK)
804 #define SvOOK_on(sv)            ((void)SvIOK_off(sv), SvFLAGS(sv) |= SVf_OOK)
805 #define SvOOK_off(sv)           ((void)(SvOOK(sv) && sv_backoff(sv)))
806
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
811 #define SvROK(sv)               (SvFLAGS(sv) & SVf_ROK)
812 #define SvROK_on(sv)            (SvFLAGS(sv) |= SVf_ROK)
813 #define SvROK_off(sv)           (SvFLAGS(sv) &= ~(SVf_ROK|SVf_AMAGIC))
814
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))
818
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)
822
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)
826
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)
830
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)
834
835 #define SvGAMAGIC(sv)           (SvFLAGS(sv) & (SVs_GMG|SVf_AMAGIC))
836
837 #define Gv_AMG(stash)           (PL_amagic_generation && Gv_AMupdate(stash))
838
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
844 #define SvTHINKFIRST(sv)        (SvFLAGS(sv) & SVf_THINKFIRST)
845
846 #define SvPADBUSY(sv)           (SvFLAGS(sv) & SVs_PADBUSY)
847
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)
851
852 #define SvPADMY(sv)             (SvFLAGS(sv) & SVs_PADMY)
853 #define SvPADMY_on(sv)          (SvFLAGS(sv) |= SVs_PADMY|SVs_PADBUSY)
854
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)
858
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)
862
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)
866
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)
870
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)
874
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
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
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
887 #ifdef USE_ITHREADS
888 /* The following uses the FAKE flag to show that a regex pointer is infact
889    its own offset in the regexpad for ithreads */
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
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
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
910 #define SvRV(sv) ((XRV*)  SvANY(sv))->xrv_rv
911 #define SvRVx(sv) SvRV(sv)
912
913 #define SvIVX(sv) ((XPVIV*)  SvANY(sv))->xiv_iv
914 #define SvUVX(sv) ((XPVUV*)  SvANY(sv))->xuv_uv
915 #define SvNVX(sv)  ((XPVNV*)SvANY(sv))->xnv_nv
916 #define SvPVX(sv)  ((XPV*)  SvANY(sv))->xpv_pv
917 #ifndef PERL_POISON
918 /* Given that these two are new, there can't be any existing code using them
919  *  as LVALUEs  */
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
928 #define SvCUR(sv) ((XPV*)  SvANY(sv))->xpv_cur
929 #define SvLEN(sv) ((XPV*)  SvANY(sv))->xpv_len
930 #define SvEND(sv)(((XPV*)  SvANY(sv))->xpv_pv + ((XPV*)SvANY(sv))->xpv_cur)
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)
937 #define SvENDx(sv) ((PL_Sv = (sv)), SvEND(PL_Sv))
938 #define SvMAGIC(sv)     ((XPVMG*)  SvANY(sv))->xmg_magic
939 #define SvSTASH(sv)     ((XPVMG*)  SvANY(sv))->xmg_stash
940
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
947 /* Put the asserts back at some point and figure out where they reveal bugs
948 */
949 #define SvIV_set(sv, val) \
950         STMT_START { assert(SvTYPE(sv) == SVt_IV || SvTYPE(sv) >= SVt_PVIV); \
951                 (((XPVIV*)  SvANY(sv))->xiv_iv = (val)); } STMT_END
952 #define SvNV_set(sv, val) \
953         STMT_START { assert(SvTYPE(sv) == SVt_NV || SvTYPE(sv) >= SVt_PVNV); \
954                 (((XPVNV*)SvANY(sv))->xnv_nv = (val)); } STMT_END
955 /* assert(SvTYPE(sv) >= SVt_PV); */
956 #define SvPV_set(sv, val) \
957         STMT_START { \
958                 (((XPV*)  SvANY(sv))->xpv_pv = (val)); } STMT_END
959 /* assert(SvTYPE(sv) == SVt_IV || SvTYPE(sv) >= SVt_PVIV); */
960 #define SvUV_set(sv, val) \
961         STMT_START { \
962                 (((XPVUV*)SvANY(sv))->xuv_uv = (val)); } STMT_END
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); */
976 #define SvCUR_set(sv, val) \
977         STMT_START { \
978                 (((XPV*)  SvANY(sv))->xpv_cur = (val)); } STMT_END
979 /* assert(SvTYPE(sv) >= SVt_PV); */
980 #define SvLEN_set(sv, val) \
981         STMT_START { \
982                 (((XPV*)  SvANY(sv))->xpv_len = (val)); } STMT_END
983 #define SvEND_set(sv, val) \
984         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
985                 (SvCUR(sv) = (val) - SvPVX(sv)); } STMT_END
986
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
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
1010
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
1015 #define PERL_FBM_TABLE_OFFSET 2 /* Number of bytes between EOS and table */
1016 #define PERL_FBM_FLAGS_OFFSET_FROM_TABLE -1
1017
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
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
1028 #define IoANY(sv)       ((XPVIO*)  SvANY(sv))->xio_any
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
1043 /* IoTYPE(sv) is a single character telling the type of I/O connection. */
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 */
1054
1055 /*
1056 =for apidoc Am|bool|SvTAINTED|SV* sv
1057 Checks to see if an SV is tainted. Returns TRUE if it is, FALSE if
1058 not.
1059
1060 =for apidoc Am|void|SvTAINTED_on|SV* sv
1061 Marks an SV as tainted if tainting is enabled.
1062
1063 =for apidoc Am|void|SvTAINTED_off|SV* sv
1064 Untaints an SV. Be I<very> careful with this routine, as it short-circuits
1065 some of Perl's fundamental security features. XS module authors should not
1066 use this function unless they fully understand all the implications of
1067 unconditionally untainting the value. Untainting should be done in the
1068 standard perl fashion, via a carefully crafted regexp, rather than directly
1069 untainting variables.
1070
1071 =for apidoc Am|void|SvTAINT|SV* sv
1072 Taints an SV if tainting is enabled.
1073
1074 =cut
1075 */
1076
1077 #define sv_taint(sv)      sv_magic((sv), NULL, PERL_MAGIC_taint, NULL, 0)
1078
1079 #define SvTAINTED(sv)     (SvMAGICAL(sv) && sv_tainted(sv))
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
1082
1083 #define SvTAINT(sv)                     \
1084     STMT_START {                        \
1085         if (PL_tainting) {              \
1086             if (PL_tainted)             \
1087                 SvTAINTED_on(sv);       \
1088         }                               \
1089     } STMT_END
1090
1091 /*
1092 =for apidoc Am|char*|SvPV_force|SV* sv|STRLEN len
1093 Like 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>
1095 directly.
1096
1097 =for apidoc Am|char*|SvPV_force_nomg|SV* sv|STRLEN len
1098 Like 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>
1100 directly. Doesn't process magic.
1101
1102 =for apidoc Am|char*|SvPV|SV* sv|STRLEN len
1103 Returns a pointer to the string in the SV, or a stringified form of
1104 the SV if the SV does not contain a string.  The SV may cache the
1105 stringified version becoming C<SvPOK>.  Handles 'get' magic. See also
1106 C<SvPVx> for a version which guarantees to evaluate sv only once.
1107
1108 =for apidoc Am|char*|SvPVx|SV* sv|STRLEN len
1109 A version of C<SvPV> which guarantees to evaluate sv only once.
1110
1111 =for apidoc Am|char*|SvPV_nomg|SV* sv|STRLEN len
1112 Like C<SvPV> but doesn't process magic.
1113
1114 =for apidoc Am|char*|SvPV_nolen|SV* sv
1115 Returns a pointer to the string in the SV, or a stringified form of
1116 the SV if the SV does not contain a string.  The SV may cache the
1117 stringified form becoming C<SvPOK>.  Handles 'get' magic.
1118
1119 =for apidoc Am|IV|SvIV|SV* sv
1120 Coerces the given SV to an integer and returns it. See  C<SvIVx> for a
1121 version which guarantees to evaluate sv only once.
1122
1123 =for apidoc Am|IV|SvIV_nomg|SV* sv
1124 Like C<SvIV> but doesn't process magic.
1125
1126 =for apidoc Am|IV|SvIVx|SV* sv
1127 Coerces the given SV to an integer and returns it. Guarantees to evaluate
1128 sv only once. Use the more efficient C<SvIV> otherwise.
1129
1130 =for apidoc Am|NV|SvNV|SV* sv
1131 Coerce the given SV to a double and return it. See  C<SvNVx> for a version
1132 which guarantees to evaluate sv only once.
1133
1134 =for apidoc Am|NV|SvNVx|SV* sv
1135 Coerces the given SV to a double and returns it. Guarantees to evaluate
1136 sv only once. Use the more efficient C<SvNV> otherwise.
1137
1138 =for apidoc Am|UV|SvUV|SV* sv
1139 Coerces the given SV to an unsigned integer and returns it.  See C<SvUVx>
1140 for a version which guarantees to evaluate sv only once.
1141
1142 =for apidoc Am|UV|SvUV_nomg|SV* sv
1143 Like C<SvUV> but doesn't process magic.
1144
1145 =for apidoc Am|UV|SvUVx|SV* sv
1146 Coerces the given SV to an unsigned integer and returns it. Guarantees to
1147 evaluate sv only once. Use the more efficient C<SvUV> otherwise.
1148
1149 =for apidoc Am|bool|SvTRUE|SV* sv
1150 Returns a boolean indicating whether Perl would evaluate the SV as true or
1151 false, defined or undefined.  Does not handle 'get' magic.
1152
1153 =for apidoc Am|char*|SvPVutf8_force|SV* sv|STRLEN len
1154 Like C<SvPV_force>, but converts sv to utf8 first if necessary.
1155
1156 =for apidoc Am|char*|SvPVutf8|SV* sv|STRLEN len
1157 Like C<SvPV>, but converts sv to utf8 first if necessary.
1158
1159 =for apidoc Am|char*|SvPVutf8_nolen|SV* sv
1160 Like C<SvPV_nolen>, but converts sv to utf8 first if necessary.
1161
1162 =for apidoc Am|char*|SvPVbyte_force|SV* sv|STRLEN len
1163 Like C<SvPV_force>, but converts sv to byte representation first if necessary.
1164
1165 =for apidoc Am|char*|SvPVbyte|SV* sv|STRLEN len
1166 Like C<SvPV>, but converts sv to byte representation first if necessary.
1167
1168 =for apidoc Am|char*|SvPVbyte_nolen|SV* sv
1169 Like C<SvPV_nolen>, but converts sv to byte representation first if necessary.
1170
1171 =for apidoc Am|char*|SvPVutf8x_force|SV* sv|STRLEN len
1172 Like C<SvPV_force>, but converts sv to utf8 first if necessary.
1173 Guarantees to evaluate sv only once; use the more efficient C<SvPVutf8_force>
1174 otherwise.
1175
1176 =for apidoc Am|char*|SvPVutf8x|SV* sv|STRLEN len
1177 Like C<SvPV>, but converts sv to utf8 first if necessary.
1178 Guarantees to evaluate sv only once; use the more efficient C<SvPVutf8>
1179 otherwise.
1180
1181 =for apidoc Am|char*|SvPVbytex_force|SV* sv|STRLEN len
1182 Like C<SvPV_force>, but converts sv to byte representation first if necessary.
1183 Guarantees to evaluate sv only once; use the more efficient C<SvPVbyte_force>
1184 otherwise.
1185
1186 =for apidoc Am|char*|SvPVbytex|SV* sv|STRLEN len
1187 Like C<SvPV>, but converts sv to byte representation first if necessary.
1188 Guarantees to evaluate sv only once; use the more efficient C<SvPVbyte>
1189 otherwise.
1190
1191 =for apidoc Am|bool|SvIsCOW|SV* sv
1192 Returns a boolean indicating whether the SV is Copy-On-Write. (either shared
1193 hash key scalars, or full Copy On Write scalars if 5.9.0 is configured for
1194 COW)
1195
1196 =for apidoc Am|bool|SvIsCOW_shared_hash|SV* sv
1197 Returns a boolean indicating whether the SV is Copy-On-Write shared hash key
1198 scalar.
1199
1200 =for apidoc Am|void|sv_catpvn_nomg|SV* sv|const char* ptr|STRLEN len
1201 Like C<sv_catpvn> but doesn't process magic.
1202
1203 =for apidoc Am|void|sv_setsv_nomg|SV* dsv|SV* ssv
1204 Like C<sv_setsv> but doesn't process magic.
1205
1206 =for apidoc Am|void|sv_catsv_nomg|SV* dsv|SV* ssv
1207 Like C<sv_catsv> but doesn't process magic.
1208
1209 =cut
1210 */
1211
1212 /* Let us hope that bitmaps for UV and IV are the same */
1213 #define SvIV(sv) (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv))
1214 #define SvUV(sv) (SvIOK(sv) ? SvUVX(sv) : sv_2uv(sv))
1215 #define SvNV(sv) (SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv))
1216
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
1220 /* ----*/
1221
1222 #define SvPV(sv, lp) SvPV_flags(sv, lp, SV_GMAGIC)
1223 #define SvPV_const(sv, lp) SvPV_flags_const(sv, lp, SV_GMAGIC)
1224 #define SvPV_mutable(sv, lp) SvPV_flags_mutable(sv, lp, SV_GMAGIC)
1225
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))
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))
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))
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))
1241
1242 #define SvPV_force(sv, lp) SvPV_force_flags(sv, lp, SV_GMAGIC)
1243 #define SvPV_force_nolen(sv) SvPV_force_flags_nolen(sv, SV_GMAGIC)
1244 #define SvPV_force_mutable(sv, lp) SvPV_force_flags_mutable(sv, lp, SV_GMAGIC)
1245
1246 #define SvPV_force_nomg(sv, lp) SvPV_force_flags(sv, lp, 0)
1247 #define SvPV_force_nomg_nolen(sv) SvPV_force_flags_nolen(sv, 0)
1248
1249 #define SvPV_force_flags(sv, lp, flags) \
1250     ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
1251     ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvn_force_flags(sv, &lp, flags))
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))
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))
1259
1260 #define SvPV_nolen(sv) \
1261     ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
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))
1267
1268 #define SvPV_nomg(sv, lp) SvPV_flags(sv, lp, 0)
1269 #define SvPV_nomg_const(sv, lp) SvPV_flags_const(sv, lp, 0)
1270 #define SvPV_nomg_const_nolen(sv) SvPV_flags_const_nolen(sv, 0)
1271
1272 /* ----*/
1273
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))
1277
1278 #define SvPVutf8_force(sv, lp) \
1279     ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == (SVf_POK|SVf_UTF8) \
1280      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvutf8n_force(sv, &lp))
1281
1282
1283 #define SvPVutf8_nolen(sv) \
1284     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK|SVf_UTF8)\
1285      ? SvPVX(sv) : sv_2pvutf8(sv, 0))
1286
1287 /* ----*/
1288
1289 #define SvPVbyte(sv, lp) \
1290     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK) \
1291      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvbyte(sv, &lp))
1292
1293 #define SvPVbyte_force(sv, lp) \
1294     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST)) == (SVf_POK) \
1295      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvbyten_force(sv, &lp))
1296
1297 #define SvPVbyte_nolen(sv) \
1298     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK)\
1299      ? SvPVX(sv) : sv_2pvbyte(sv, 0))
1300
1301
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
1312 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
1313
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); })
1318 #  define SvPVx_const(sv, lp) ({SV *_sv = (sv); SvPV_const(_sv, lp); })
1319 #  define SvPVx_nolen(sv) ({SV *_sv = (sv); SvPV_nolen(_sv); })
1320 #  define SvPVx_nolen_const(sv) ({SV *_sv = (sv); SvPV_nolen_const(_sv); })
1321 #  define SvPVutf8x(sv, lp) ({SV *_sv = (sv); SvPVutf8(_sv, lp); })
1322 #  define SvPVbytex(sv, lp) ({SV *_sv = (sv); SvPVbyte(_sv, lp); })
1323 #  define SvPVbytex_nolen(sv) ({SV *_sv = (sv); SvPVbyte_nolen(_sv); })
1324 #  define SvTRUE(sv) (                                          \
1325     !sv                                                         \
1326     ? 0                                                         \
1327     :    SvPOK(sv)                                              \
1328         ?   (({XPV *nxpv = (XPV*)SvANY(sv);                     \
1329              nxpv &&                                            \
1330              (nxpv->xpv_cur > 1 ||                              \
1331               (nxpv->xpv_cur && *nxpv->xpv_pv != '0')); })      \
1332              ? 1                                                \
1333              : 0)                                               \
1334         :                                                       \
1335             SvIOK(sv)                                           \
1336             ? SvIVX(sv) != 0                                    \
1337             :   SvNOK(sv)                                       \
1338                 ? SvNVX(sv) != 0.0                              \
1339                 : sv_2bool(sv) )
1340 #  define SvTRUEx(sv) ({SV *_sv = (sv); SvTRUE(_sv); })
1341
1342 #else /* __GNUC__ */
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
1356 /* These inlined macros use globals, which will require a thread
1357  * declaration in user code, so we avoid them under threads */
1358
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))
1363 #    define SvPVx_const(sv, lp) ((PL_Sv = (sv)), SvPV_const(PL_Sv, lp))
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))
1366 #    define SvPVutf8x(sv, lp) ((PL_Sv = (sv)), SvPVutf8(PL_Sv, lp))
1367 #    define SvPVbytex(sv, lp) ((PL_Sv = (sv)), SvPVbyte(PL_Sv, lp))
1368 #    define SvPVbytex_nolen(sv) ((PL_Sv = (sv)), SvPVbyte_nolen(PL_Sv))
1369 #    define SvTRUE(sv) (                                                \
1370     !sv                                                         \
1371     ? 0                                                         \
1372     :    SvPOK(sv)                                              \
1373         ?   ((PL_Xpv = (XPV*)SvANY(sv)) &&                      \
1374              (PL_Xpv->xpv_cur > 1 ||                            \
1375               (PL_Xpv->xpv_cur && *PL_Xpv->xpv_pv != '0'))      \
1376              ? 1                                                \
1377              : 0)                                               \
1378         :                                                       \
1379             SvIOK(sv)                                           \
1380             ? SvIVX(sv) != 0                                    \
1381             :   SvNOK(sv)                                       \
1382                 ? SvNVX(sv) != 0.0                              \
1383                 : sv_2bool(sv) )
1384 #    define SvTRUEx(sv) ((PL_Sv = (sv)), SvTRUE(PL_Sv))
1385 #  endif /* USE_5005THREADS */
1386 #endif /* __GNU__ */
1387
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)
1391
1392 #define SvSHARED_HASH(sv) (0 + SvUVX(sv))
1393
1394 /* flag values for sv_*_flags functions */
1395 #define SV_IMMEDIATE_UNREF      1
1396 #define SV_GMAGIC               2
1397 #define SV_COW_DROP_PV          4       /* Unused in Perl 5.8.x */
1398 #define SV_UTF8_NO_ENCODING     8
1399 #define SV_NOSTEAL              16
1400 #define SV_CONST_RETURN         32
1401 #define SV_MUTABLE_RETURN       64
1402 #define SV_SMAGIC               128
1403 #define SV_HAS_TRAILING_NUL     256
1404
1405 #define sv_unref(sv)            sv_unref_flags(sv, 0)
1406 #define sv_force_normal(sv)     sv_force_normal_flags(sv, 0)
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)
1409
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)
1423 #define sv_catsv_mg(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC|SV_SMAGIC)
1424 #define sv_catpvn(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC)
1425 #define sv_catpvn_mg(sv, sstr, slen) \
1426         sv_catpvn_flags(sv, sstr, slen, SV_GMAGIC|SV_SMAGIC);
1427 #define sv_2pv(sv, lp) sv_2pv_flags(sv, lp, SV_GMAGIC)
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)
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)
1434 #define sv_2iv(sv) sv_2iv_flags(sv, SV_GMAGIC)
1435 #define sv_2uv(sv) sv_2uv_flags(sv, SV_GMAGIC)
1436
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
1448
1449 /*
1450 =for apidoc Am|SV*|newRV_inc|SV* sv
1451
1452 Creates an RV wrapper for an SV.  The reference count for the original SV is
1453 incremented.
1454
1455 =cut
1456 */
1457
1458 #define newRV_inc(sv)   newRV(sv)
1459
1460 /* the following macros update any magic values this sv is associated with */
1461
1462 /*
1463 =head1 Magical Functions
1464
1465 =for apidoc Am|void|SvGETMAGIC|SV* sv
1466 Invokes C<mg_get> on an SV if it has 'get' magic.  This macro evaluates its
1467 argument more than once.
1468
1469 =for apidoc Am|void|SvSETMAGIC|SV* sv
1470 Invokes C<mg_set> on an SV if it has 'set' magic.  This macro evaluates its
1471 argument more than once.
1472
1473 =for apidoc Am|void|SvSetSV|SV* dsb|SV* ssv
1474 Calls C<sv_setsv> if dsv is not the same as ssv.  May evaluate arguments
1475 more than once.
1476
1477 =for apidoc Am|void|SvSetSV_nosteal|SV* dsv|SV* ssv
1478 Calls a non-destructive version of C<sv_setsv> if dsv is not the same as
1479 ssv. May evaluate arguments more than once.
1480
1481 =for apidoc Am|void|SvSetMagicSV|SV* dsb|SV* ssv
1482 Like C<SvSetSV>, but does any set magic required afterwards.
1483
1484 =for apidoc Am|void|SvSetMagicSV_nosteal|SV* dsv|SV* ssv
1485 Like C<SvSetSV_nosteal>, but does any set magic required afterwards.
1486
1487 =for apidoc Am|void|SvSHARE|SV* sv
1488 Arranges for sv to be shared between threads if a suitable module
1489 has been loaded.
1490
1491 =for apidoc Am|void|SvLOCK|SV* sv
1492 Arranges for a mutual exclusion lock to be obtained on sv if a suitable module
1493 has been loaded.
1494
1495 =for apidoc Am|void|SvUNLOCK|SV* sv
1496 Releases a mutual exclusion lock on sv if a suitable module
1497 has been loaded.
1498
1499 =head1 SV Manipulation Functions
1500
1501 =for apidoc Am|char *|SvGROW|SV* sv|STRLEN len
1502 Expands the character buffer in the SV so that it has room for the
1503 indicated number of bytes (remember to reserve space for an extra trailing
1504 NUL character).  Calls C<sv_grow> to perform the expansion if necessary.
1505 Returns a pointer to the character buffer.
1506
1507 =cut
1508 */
1509
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
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
1516
1517 #define SvSetSV_and(dst,src,finally) \
1518         STMT_START {                                    \
1519             if ((dst) != (src)) {                       \
1520                 sv_setsv(dst, src);                     \
1521                 finally;                                \
1522             }                                           \
1523         } STMT_END
1524 #define SvSetSV_nosteal_and(dst,src,finally) \
1525         STMT_START {                                    \
1526             if ((dst) != (src)) {                       \
1527                 sv_setsv_flags(dst, src, SV_GMAGIC | SV_NOSTEAL);       \
1528                 finally;                                \
1529             }                                           \
1530         } STMT_END
1531
1532 #define SvSetSV(dst,src) \
1533                 SvSetSV_and(dst,src,/*nothing*/;)
1534 #define SvSetSV_nosteal(dst,src) \
1535                 SvSetSV_nosteal_and(dst,src,/*nothing*/;)
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
1542
1543 #if !defined(SKIP_DEBUGGING)
1544 #define SvPEEK(sv) sv_peek(sv)
1545 #else
1546 #define SvPEEK(sv) ""
1547 #endif
1548
1549 #define SvIMMORTAL(sv) ((sv)==&PL_sv_undef || (sv)==&PL_sv_yes || (sv)==&PL_sv_no || (sv)==&PL_sv_placeholder)
1550
1551 #define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
1552
1553 #define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
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))
1558
1559 #define SvGROW(sv,len) (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
1560 #define SvGROW_mutable(sv,len) \
1561     (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX_mutable(sv))
1562 #define Sv_Grow sv_grow
1563
1564 #define CLONEf_COPY_STACKS 1
1565 #define CLONEf_KEEP_PTR_TABLE 2
1566 #define CLONEf_CLONE_HOST 4
1567 #define CLONEf_JOIN_IN 8
1568
1569 struct clone_params {
1570   AV* stashes;
1571   UV  flags;
1572   PerlInterpreter *proto_perl;
1573 };
1574
1575 #define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) sv_force_normal(sv)