This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perldelta for 1311cfc
[perl5.git] / sv.h
1 /*    sv.h
2  *
3  *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4  *    2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 The types are:
23
24     SVt_NULL
25     SVt_IV
26     SVt_NV
27     SVt_RV
28     SVt_PV
29     SVt_PVIV
30     SVt_PVNV
31     SVt_PVMG
32     SVt_INVLIST
33     SVt_REGEXP
34     SVt_PVGV
35     SVt_PVLV
36     SVt_PVAV
37     SVt_PVHV
38     SVt_PVCV
39     SVt_PVFM
40     SVt_PVIO
41
42 These are most easily explained from the bottom up.
43
44 SVt_PVIO is for I/O objects, SVt_PVFM for formats, SVt_PVCV for
45 subroutines, SVt_PVHV for hashes and SVt_PVAV for arrays.
46
47 All the others are scalar types, that is, things that can be bound to a
48 C<$> variable.  For these, the internal types are mostly orthogonal to
49 types in the Perl language.
50
51 Hence, checking C<< SvTYPE(sv) < SVt_PVAV >> is the best way to see whether
52 something is a scalar.
53
54 SVt_PVGV represents a typeglob.  If !SvFAKE(sv), then it is a real,
55 incoercible typeglob.  If SvFAKE(sv), then it is a scalar to which a
56 typeglob has been assigned.  Assigning to it again will stop it from being
57 a typeglob.  SVt_PVLV represents a scalar that delegates to another scalar
58 behind the scenes.  It is used, e.g., for the return value of C<substr> and
59 for tied hash and array elements.  It can hold any scalar value, including
60 a typeglob. SVt_REGEXP is for regular expressions.  SVt_INVLIST is for Perl
61 core internal use only.
62
63 SVt_PVMG represents a "normal" scalar (not a typeglob, regular expression,
64 or delegate).  Since most scalars do not need all the internal fields of a
65 PVMG, we save memory by allocating smaller structs when possible.  All the
66 other types are just simpler forms of SVt_PVMG, with fewer internal fields.
67  SVt_NULL can only hold undef.  SVt_IV can hold undef, an integer, or a
68 reference.  (SVt_RV is an alias for SVt_IV, which exists for backward
69 compatibility.)  SVt_NV can hold any of those or a double.  SVt_PV can only
70 hold undef or a string.  SVt_PVIV is a superset of SVt_PV and SVt_IV.
71 SVt_PVNV is similar.  SVt_PVMG can hold anything SVt_PVNV can hold, but it
72 can, but does not have to, be blessed or magical.
73
74 =for apidoc AmU||SVt_NULL
75 Type flag for scalars.  See L</svtype>.
76
77 =for apidoc AmU||SVt_IV
78 Type flag for scalars.  See L</svtype>.
79
80 =for apidoc AmU||SVt_NV
81 Type flag for scalars.  See L</svtype>.
82
83 =for apidoc AmU||SVt_PV
84 Type flag for scalars.  See L</svtype>.
85
86 =for apidoc AmU||SVt_PVIV
87 Type flag for scalars.  See L</svtype>.
88
89 =for apidoc AmU||SVt_PVNV
90 Type flag for scalars.  See L</svtype>.
91
92 =for apidoc AmU||SVt_PVMG
93 Type flag for scalars.  See L</svtype>.
94
95 =for apidoc AmU||SVt_INVLIST
96 Type flag for scalars.  See L</svtype>.
97
98 =for apidoc AmU||SVt_REGEXP
99 Type flag for regular expressions.  See L</svtype>.
100
101 =for apidoc AmU||SVt_PVGV
102 Type flag for typeglobs.  See L</svtype>.
103
104 =for apidoc AmU||SVt_PVLV
105 Type flag for scalars.  See L</svtype>.
106
107 =for apidoc AmU||SVt_PVAV
108 Type flag for arrays.  See L</svtype>.
109
110 =for apidoc AmU||SVt_PVHV
111 Type flag for hashes.  See L</svtype>.
112
113 =for apidoc AmU||SVt_PVCV
114 Type flag for subroutines.  See L</svtype>.
115
116 =for apidoc AmU||SVt_PVFM
117 Type flag for formats.  See L</svtype>.
118
119 =for apidoc AmU||SVt_PVIO
120 Type flag for I/O objects.  See L</svtype>.
121
122 =cut
123
124   These are ordered so that the simpler types have a lower value; SvUPGRADE
125   doesn't allow you to upgrade from a higher numbered type to a lower numbered
126   one; also there is code that assumes that anything that has as a PV component
127   has a type numbered >= SVt_PV.
128 */
129
130
131 typedef enum {
132         SVt_NULL,       /* 0 */
133         /* BIND was here, before INVLIST replaced it.  */
134         SVt_IV,         /* 1 */
135         SVt_NV,         /* 2 */
136         /* RV was here, before it was merged with IV.  */
137         SVt_PV,         /* 3 */
138         SVt_INVLIST,    /* 4, implemented as a PV */
139         SVt_PVIV,       /* 5 */
140         SVt_PVNV,       /* 6 */
141         SVt_PVMG,       /* 7 */
142         SVt_REGEXP,     /* 8 */
143         /* PVBM was here, before BIND replaced it.  */
144         SVt_PVGV,       /* 9 */
145         SVt_PVLV,       /* 10 */
146         SVt_PVAV,       /* 11 */
147         SVt_PVHV,       /* 12 */
148         SVt_PVCV,       /* 13 */
149         SVt_PVFM,       /* 14 */
150         SVt_PVIO,       /* 15 */
151         SVt_LAST        /* keep last in enum. used to size arrays */
152 } svtype;
153
154 /* *** any alterations to the SV types above need to be reflected in
155  * SVt_MASK and the various PL_valid_types_* tables.  As of this writing those
156  * tables are in perl.h.  There are also two affected names tables in dump.c,
157  * one in B.xs, and 'bodies_by_type[]' in sv.c */
158
159 #define SVt_MASK 0xf    /* smallest bitmask that covers all types */
160
161 #ifndef PERL_CORE
162 /* Although Fast Boyer Moore tables are now being stored in PVGVs, for most
163    purposes external code wanting to consider PVBM probably needs to think of
164    PVMG instead.  */
165 #  define SVt_PVBM      SVt_PVMG
166 /* Anything wanting to create a reference from clean should ensure that it has
167    a scalar of type SVt_IV now:  */
168 #  define SVt_RV        SVt_IV
169 #endif
170
171 /* There is collusion here with sv_clear - sv_clear exits early for SVt_NULL
172    so never reaches the clause at the end that uses sv_type_details->body_size
173    to determine whether to call safefree(). Hence body_size can be set
174    non-zero to record the size of HEs, without fear of bogus frees.  */
175 #if defined(PERL_IN_HV_C) || defined(PERL_IN_XS_APITEST)
176 #define HE_SVSLOT       SVt_NULL
177 #endif
178
179 #define PERL_ARENA_ROOTS_SIZE   (SVt_LAST)
180
181 /* typedefs to eliminate some typing */
182 typedef struct he HE;
183 typedef struct hek HEK;
184
185 /* Using C's structural equivalence to help emulate C++ inheritance here... */
186
187 /* start with 2 sv-head building blocks */
188 #define _SV_HEAD(ptrtype) \
189     ptrtype     sv_any;         /* pointer to body */   \
190     U32         sv_refcnt;      /* how many references to us */ \
191     U32         sv_flags        /* what we are */
192
193 #define _SV_HEAD_UNION \
194     union {                             \
195         char*   svu_pv;         /* pointer to malloced string */        \
196         IV      svu_iv;                 \
197         UV      svu_uv;                 \
198         SV*     svu_rv;         /* pointer to another SV */             \
199         struct regexp* svu_rx;          \
200         SV**    svu_array;              \
201         HE**    svu_hash;               \
202         GP*     svu_gp;                 \
203         PerlIO *svu_fp;                 \
204     }   sv_u
205
206
207 struct STRUCT_SV {              /* struct sv { */
208     _SV_HEAD(void*);
209     _SV_HEAD_UNION;
210 #ifdef DEBUG_LEAKING_SCALARS
211     PERL_BITFIELD32 sv_debug_optype:9;  /* the type of OP that allocated us */
212     PERL_BITFIELD32 sv_debug_inpad:1;   /* was allocated in a pad for an OP */
213     PERL_BITFIELD32 sv_debug_line:16;   /* the line where we were allocated */
214     UV              sv_debug_serial;    /* serial number of sv allocation   */
215     char *          sv_debug_file;      /* the file where we were allocated */
216     SV *            sv_debug_parent;    /* what we were cloned from (ithreads)*/
217 #endif
218 };
219
220 struct gv {
221     _SV_HEAD(XPVGV*);           /* pointer to xpvgv body */
222     _SV_HEAD_UNION;
223 };
224
225 struct cv {
226     _SV_HEAD(XPVCV*);           /* pointer to xpvcv body */
227     _SV_HEAD_UNION;
228 };
229
230 struct av {
231     _SV_HEAD(XPVAV*);           /* pointer to xpvav body */
232     _SV_HEAD_UNION;
233 };
234
235 struct hv {
236     _SV_HEAD(XPVHV*);           /* pointer to xpvhv body */
237     _SV_HEAD_UNION;
238 };
239
240 struct io {
241     _SV_HEAD(XPVIO*);           /* pointer to xpvio body */
242     _SV_HEAD_UNION;
243 };
244
245 struct p5rx {
246     _SV_HEAD(struct regexp*);   /* pointer to regexp body */
247     _SV_HEAD_UNION;
248 };
249
250 #undef _SV_HEAD
251 #undef _SV_HEAD_UNION           /* ensure no pollution */
252
253 /*
254 =head1 SV Manipulation Functions
255
256 =for apidoc Am|U32|SvREFCNT|SV* sv
257 Returns the value of the object's reference count.
258
259 =for apidoc Am|SV*|SvREFCNT_inc|SV* sv
260 Increments the reference count of the given SV, returning the SV.
261
262 All of the following SvREFCNT_inc* macros are optimized versions of
263 SvREFCNT_inc, and can be replaced with SvREFCNT_inc.
264
265 =for apidoc Am|SV*|SvREFCNT_inc_NN|SV* sv
266 Same as SvREFCNT_inc, but can only be used if you know I<sv>
267 is not NULL.  Since we don't have to check the NULLness, it's faster
268 and smaller.
269
270 =for apidoc Am|void|SvREFCNT_inc_void|SV* sv
271 Same as SvREFCNT_inc, but can only be used if you don't need the
272 return value.  The macro doesn't need to return a meaningful value.
273
274 =for apidoc Am|void|SvREFCNT_inc_void_NN|SV* sv
275 Same as SvREFCNT_inc, but can only be used if you don't need the return
276 value, and you know that I<sv> is not NULL.  The macro doesn't need
277 to return a meaningful value, or check for NULLness, so it's smaller
278 and faster.
279
280 =for apidoc Am|SV*|SvREFCNT_inc_simple|SV* sv
281 Same as SvREFCNT_inc, but can only be used with expressions without side
282 effects.  Since we don't have to store a temporary value, it's faster.
283
284 =for apidoc Am|SV*|SvREFCNT_inc_simple_NN|SV* sv
285 Same as SvREFCNT_inc_simple, but can only be used if you know I<sv>
286 is not NULL.  Since we don't have to check the NULLness, it's faster
287 and smaller.
288
289 =for apidoc Am|void|SvREFCNT_inc_simple_void|SV* sv
290 Same as SvREFCNT_inc_simple, but can only be used if you don't need the
291 return value.  The macro doesn't need to return a meaningful value.
292
293 =for apidoc Am|void|SvREFCNT_inc_simple_void_NN|SV* sv
294 Same as SvREFCNT_inc, but can only be used if you don't need the return
295 value, and you know that I<sv> is not NULL.  The macro doesn't need
296 to return a meaningful value, or check for NULLness, so it's smaller
297 and faster.
298
299 =for apidoc Am|void|SvREFCNT_dec|SV* sv
300 Decrements the reference count of the given SV. I<sv> may be NULL.
301
302 =for apidoc Am|void|SvREFCNT_dec_NN|SV* sv
303 Same as SvREFCNT_dec, but can only be used if you know I<sv>
304 is not NULL.  Since we don't have to check the NULLness, it's faster
305 and smaller.
306
307 =for apidoc Am|svtype|SvTYPE|SV* sv
308 Returns the type of the SV.  See C<svtype>.
309
310 =for apidoc Am|void|SvUPGRADE|SV* sv|svtype type
311 Used to upgrade an SV to a more complex form.  Uses C<sv_upgrade> to
312 perform the upgrade if necessary.  See C<svtype>.
313
314 =cut
315 */
316
317 #define SvANY(sv)       (sv)->sv_any
318 #define SvFLAGS(sv)     (sv)->sv_flags
319 #define SvREFCNT(sv)    (sv)->sv_refcnt
320
321 #define SvREFCNT_inc(sv)                S_SvREFCNT_inc(MUTABLE_SV(sv))
322 #define SvREFCNT_inc_simple(sv)         SvREFCNT_inc(sv)
323 #define SvREFCNT_inc_NN(sv)             S_SvREFCNT_inc_NN(MUTABLE_SV(sv))
324 #define SvREFCNT_inc_void(sv)           S_SvREFCNT_inc_void(MUTABLE_SV(sv))
325
326 /* These guys don't need the curly blocks */
327 #define SvREFCNT_inc_simple_void(sv)    STMT_START { if (sv) SvREFCNT(sv)++; } STMT_END
328 #define SvREFCNT_inc_simple_NN(sv)      (++(SvREFCNT(sv)),MUTABLE_SV(sv))
329 #define SvREFCNT_inc_void_NN(sv)        (void)(++SvREFCNT(MUTABLE_SV(sv)))
330 #define SvREFCNT_inc_simple_void_NN(sv) (void)(++SvREFCNT(MUTABLE_SV(sv)))
331
332 #define SvREFCNT_dec(sv)        S_SvREFCNT_dec(aTHX_ MUTABLE_SV(sv))
333 #define SvREFCNT_dec_NN(sv)     S_SvREFCNT_dec_NN(aTHX_ MUTABLE_SV(sv))
334
335 #define SVTYPEMASK      0xff
336 #define SvTYPE(sv)      ((svtype)((sv)->sv_flags & SVTYPEMASK))
337
338 /* Sadly there are some parts of the core that have pointers to already-freed
339    SV heads, and rely on being able to tell that they are now free. So mark
340    them all by using a consistent macro.  */
341 #define SvIS_FREED(sv)  ((sv)->sv_flags == SVTYPEMASK)
342
343 /* this is defined in this peculiar way to avoid compiler warnings.
344  * See the <20121213131428.GD1842@iabyn.com> thread in p5p */
345 #define SvUPGRADE(sv, mt) \
346     ((void)(SvTYPE(sv) >= (mt) || (sv_upgrade(sv, mt),1)))
347
348 #define SVf_IOK         0x00000100  /* has valid public integer value */
349 #define SVf_NOK         0x00000200  /* has valid public numeric value */
350 #define SVf_POK         0x00000400  /* has valid public pointer value */
351 #define SVf_ROK         0x00000800  /* has a valid reference pointer */
352
353 #define SVp_IOK         0x00001000  /* has valid non-public integer value */
354 #define SVp_NOK         0x00002000  /* has valid non-public numeric value */
355 #define SVp_POK         0x00004000  /* has valid non-public pointer value */
356 #define SVp_SCREAM      0x00008000  /* method name is DOES */
357 #define SVphv_CLONEABLE SVp_SCREAM  /* PVHV (stashes) clone its objects */
358 #define SVpgv_GP        SVp_SCREAM  /* GV has a valid GP */
359 #define SVprv_PCS_IMPORTED  SVp_SCREAM  /* RV is a proxy for a constant
360                                        subroutine in another package. Set the
361                                        GvIMPORTED_CV_on() if it needs to be
362                                        expanded to a real GV */
363 #define SVpad_NAMELIST  SVp_SCREAM  /* AV is a padnamelist */
364 #define SVf_IsCOW       0x00010000  /* copy on write (shared hash key if
365                                        SvLEN == 0) */
366                                     /* Also used on HVs in gv.c:gv_check */
367 #define SVs_PADTMP      0x00020000  /* in use as tmp; only if ! SVs_PADMY */
368 #define SVs_PADSTALE    0x00020000  /* lexical has gone out of scope;
369                                         only valid for SVs_PADMY */
370 #define SVpad_TYPED     0x00020000  /* pad name is a Typed Lexical */
371 #define SVs_PADMY       0x00040000  /* in use a "my" variable */
372 #define SVpad_OUR       0x00040000  /* pad name is "our" instead of "my" */
373 #define SVs_TEMP        0x00080000  /* string is stealable? */
374 #define SVs_OBJECT      0x00100000  /* is "blessed" */
375 #define SVs_GMG         0x00200000  /* has magical get method */
376 #define SVs_SMG         0x00400000  /* has magical set method */
377 #define SVs_RMG         0x00800000  /* has random magical methods */
378
379 #define SVf_FAKE        0x01000000  /* 0: glob is just a copy
380                                        1: SV head arena wasn't malloc()ed
381                                        2: For PVCV, whether CvUNIQUE(cv)
382                                           refers to an eval or once only
383                                           [CvEVAL(cv), CvSPECIAL(cv)]
384                                        3: On a pad name SV, that slot in the
385                                           frame AV is a REFCNT'ed reference
386                                           to a lexical from "outside". */
387 #define SVf_OOK         0x02000000  /* has valid offset value. For a PVHV this
388                                        means that a hv_aux struct is present
389                                        after the main array */
390 #define SVf_BREAK       0x04000000  /* refcnt is artificially low - used by
391                                        SVs in final arena cleanup.
392                                        Set in S_regtry on PL_reg_curpm, so that
393                                        perl_destruct will skip it. */
394 #define SVf_READONLY    0x08000000  /* may not be modified */
395
396
397
398
399 #define SVf_THINKFIRST  (SVf_READONLY|SVf_ROK|SVf_FAKE|SVs_RMG|SVf_IsCOW)
400
401 #define SVf_OK          (SVf_IOK|SVf_NOK|SVf_POK|SVf_ROK| \
402                          SVp_IOK|SVp_NOK|SVp_POK|SVpgv_GP)
403
404 #define PRIVSHIFT 4     /* (SVp_?OK >> PRIVSHIFT) == SVf_?OK */
405
406 #define SVf_AMAGIC      0x10000000  /* has magical overloaded methods */
407
408 /* Ensure this value does not clash with the GV_ADD* flags in gv.h: */
409 #define SVf_UTF8        0x20000000  /* SvPV is UTF-8 encoded
410                                        This is also set on RVs whose overloaded
411                                        stringification is UTF-8. This might
412                                        only happen as a side effect of SvPV() */
413                                            
414
415 /* Some private flags. */
416
417 /* PVAV could probably use 0x2000000 without conflict. I assume that PVFM can
418    be UTF-8 encoded, and PVCVs could well have UTF-8 prototypes. PVIOs haven't
419    been restructured, so sometimes get used as string buffers.  */
420
421 /* PVHV */
422 #define SVphv_SHAREKEYS 0x20000000  /* PVHV keys live on shared string table */
423
424 /* PVNV, PVMG only, and only used in pads. Should be safe to test on any scalar
425    SV, as the core is careful to avoid setting both.
426
427    SVf_POK, SVp_POK also set:
428    0x00004400   Normal
429    0x0000C400   method name for DOES (SvSCREAM)
430    0x40004400   FBM compiled (SvVALID)
431    0x4000C400   pad name.
432
433    0x00008000   GV with GP
434    0x00008800   RV with PCS imported
435 */
436 #define SVpad_NAME      (SVp_SCREAM|SVpbm_VALID)
437                                     /* This SV is a name in the PAD, so
438                                        SVpad_TYPED, SVpad_OUR and SVpad_STATE
439                                        apply */
440 /* PVAV */
441 #define SVpav_REAL      0x40000000  /* free old entries */
442 /* PVHV */
443 #define SVphv_LAZYDEL   0x40000000  /* entry in xhv_eiter must be deleted */
444 /* This is only set true on a PVGV when it's playing "PVBM", but is tested for
445    on any regular scalar (anything <= PVLV) */
446 #define SVpbm_VALID     0x40000000
447 /* Only used in toke.c on an SV stored in PL_lex_repl */
448 #define SVrepl_EVAL     0x40000000  /* Replacement part of s///e */
449
450 /* IV, PVIV, PVNV, PVMG, PVGV and (I assume) PVLV  */
451 #define SVf_IVisUV      0x80000000  /* use XPVUV instead of XPVIV */
452 /* PVAV */
453 #define SVpav_REIFY     0x80000000  /* can become real */
454 /* PVHV */
455 #define SVphv_HASKFLAGS 0x80000000  /* keys have flag byte after hash */
456 /* PVGV when SVpbm_VALID is true */
457 #define SVpbm_TAIL      0x80000000
458 /* RV upwards. However, SVf_ROK and SVp_IOK are exclusive  */
459 #define SVprv_WEAKREF   0x80000000  /* Weak reference */
460 /* pad name vars only */
461 #define SVpad_STATE     0x80000000  /* pad name is a "state" var */
462
463 #define _XPV_HEAD                                                       \
464     HV*         xmg_stash;      /* class package */                     \
465     union _xmgu xmg_u;                                                  \
466     STRLEN      xpv_cur;        /* length of svu_pv as a C string */    \
467     union {                                                             \
468         STRLEN  xpvlenu_len;    /* allocated size */                    \
469         char *  xpvlenu_pv;     /* regexp string */                     \
470     } xpv_len_u 
471
472 #define xpv_len xpv_len_u.xpvlenu_len
473
474 union _xnvu {
475     NV      xnv_nv;             /* numeric value, if any */
476     HV *    xgv_stash;
477     struct {
478         U32 xlow;
479         U32 xhigh;
480     }       xpad_cop_seq;       /* used by pad.c for cop_sequence */
481     I32     xbm_useful;
482 };
483
484 union _xivu {
485     IV      xivu_iv;            /* integer value */
486     UV      xivu_uv;
487     HEK *   xivu_namehek;       /* xpvlv, xpvgv: GvNAME */
488 };
489
490 union _xmgu {
491     MAGIC*  xmg_magic;          /* linked list of magicalness */
492     HV*     xmg_ourstash;       /* Stash for our (when SvPAD_OUR is true) */
493     STRLEN  xmg_hash_index;     /* used while freeing hash entries */
494 };                              /* also used by PadnamelistMAXNAMED */
495
496 struct xpv {
497     _XPV_HEAD;
498 };
499
500 struct xpviv {
501     _XPV_HEAD;
502     union _xivu xiv_u;
503 };
504
505 #define xiv_iv xiv_u.xivu_iv
506
507 struct xpvuv {
508     _XPV_HEAD;
509     union _xivu xuv_u;
510 };
511
512 #define xuv_uv xuv_u.xivu_uv
513
514 struct xpvnv {
515     _XPV_HEAD;
516     union _xivu xiv_u;
517     union _xnvu xnv_u;
518 };
519
520 /* This structure must match the beginning of struct xpvhv in hv.h. */
521 struct xpvmg {
522     _XPV_HEAD;
523     union _xivu xiv_u;
524     union _xnvu xnv_u;
525 };
526
527 struct xpvlv {
528     _XPV_HEAD;
529     union _xivu xiv_u;
530     union _xnvu xnv_u;
531     STRLEN      xlv_targoff;
532     STRLEN      xlv_targlen;
533     SV*         xlv_targ;
534     char        xlv_type;       /* k=keys .=pos x=substr v=vec /=join/re
535                                  * y=alem/helem/iter t=tie T=tied HE */
536     char        xlv_flags;      /* 1 = negative offset  2 = negative len */
537 };
538
539 struct xpvinvlist {
540     _XPV_HEAD;
541     IV          prev_index;
542     STRLEN      iterator;
543     bool        is_offset;      /* */
544 };
545
546 /* This structure works in 3 ways - regular scalar, GV with GP, or fast
547    Boyer-Moore.  */
548 struct xpvgv {
549     _XPV_HEAD;
550     union _xivu xiv_u;
551     union _xnvu xnv_u;
552 };
553
554 typedef U32 cv_flags_t;
555
556 #define _XPVCV_COMMON                                                           \
557     HV *        xcv_stash;                                                      \
558     union {                                                                     \
559         OP *    xcv_start;                                                      \
560         ANY     xcv_xsubany;                                                    \
561     }           xcv_start_u;                                                    \
562     union {                                                                     \
563         OP *    xcv_root;                                                       \
564         void    (*xcv_xsub) (pTHX_ CV*);                                        \
565     }           xcv_root_u;                                                     \
566     union {                                                             \
567         GV *    xcv_gv;                                                 \
568         HEK *   xcv_hek;                                                \
569     }           xcv_gv_u;                                               \
570     char *      xcv_file;                                                       \
571     PADLIST *   xcv_padlist;                                                    \
572     CV *        xcv_outside;                                                    \
573     U32         xcv_outside_seq; /* the COP sequence (at the point of our       \
574                                   * compilation) in the lexically enclosing     \
575                                   * sub */                                      \
576     cv_flags_t  xcv_flags;                                              \
577     I32 xcv_depth       /* >= 2 indicates recursive call */
578
579 /* This structure must match XPVCV in cv.h */
580
581 struct xpvfm {
582     _XPV_HEAD;
583     _XPVCV_COMMON;
584 };
585
586
587 struct xpvio {
588     _XPV_HEAD;
589     union _xivu xiv_u;
590     /* ifp and ofp are normally the same, but sockets need separate streams */
591     PerlIO *    xio_ofp;
592     /* Cray addresses everything by word boundaries (64 bits) and
593      * code and data pointers cannot be mixed (which is exactly what
594      * Perl_filter_add() tries to do with the dirp), hence the
595      *  following union trick (as suggested by Gurusamy Sarathy).
596      * For further information see Geir Johansen's problem report
597      * titled [ID 20000612.002] Perl problem on Cray system
598      * The any pointer (known as IoANY()) will also be a good place
599      * to hang any IO disciplines to.
600      */
601     union {
602         DIR *   xiou_dirp;      /* for opendir, readdir, etc */
603         void *  xiou_any;       /* for alignment */
604     } xio_dirpu;
605     /* IV xio_lines is now in IVX  $. */
606     IV          xio_page;       /* $% */
607     IV          xio_page_len;   /* $= */
608     IV          xio_lines_left; /* $- */
609     char *      xio_top_name;   /* $^ */
610     GV *        xio_top_gv;     /* $^ */
611     char *      xio_fmt_name;   /* $~ */
612     GV *        xio_fmt_gv;     /* $~ */
613     char *      xio_bottom_name;/* $^B */
614     GV *        xio_bottom_gv;  /* $^B */
615     char        xio_type;
616     U8          xio_flags;
617 };
618
619 #define xio_dirp        xio_dirpu.xiou_dirp
620 #define xio_any         xio_dirpu.xiou_any
621
622 #define IOf_ARGV        1       /* this fp iterates over ARGV */
623 #define IOf_START       2       /* check for null ARGV and substitute '-' */
624 #define IOf_FLUSH       4       /* this fp wants a flush after write op */
625 #define IOf_DIDTOP      8       /* just did top of form */
626 #define IOf_UNTAINT     16      /* consider this fp (and its data) "safe" */
627 #define IOf_NOLINE      32      /* slurped a pseudo-line from empty file */
628 #define IOf_FAKE_DIRP   64      /* xio_dirp is fake (source filters kludge)
629                                    Also, when this is set, SvPVX() is valid */
630
631 /* The following macros define implementation-independent predicates on SVs. */
632
633 /*
634 =for apidoc Am|U32|SvNIOK|SV* sv
635 Returns a U32 value indicating whether the SV contains a number, integer or
636 double.
637
638 =for apidoc Am|U32|SvNIOKp|SV* sv
639 Returns a U32 value indicating whether the SV contains a number, integer or
640 double.  Checks the B<private> setting.  Use C<SvNIOK> instead.
641
642 =for apidoc Am|void|SvNIOK_off|SV* sv
643 Unsets the NV/IV status of an SV.
644
645 =for apidoc Am|U32|SvOK|SV* sv
646 Returns a U32 value indicating whether the value is defined. This is
647 only meaningful for scalars.
648
649 =for apidoc Am|U32|SvIOKp|SV* sv
650 Returns a U32 value indicating whether the SV contains an integer.  Checks
651 the B<private> setting.  Use C<SvIOK> instead.
652
653 =for apidoc Am|U32|SvNOKp|SV* sv
654 Returns a U32 value indicating whether the SV contains a double.  Checks the
655 B<private> setting.  Use C<SvNOK> instead.
656
657 =for apidoc Am|U32|SvPOKp|SV* sv
658 Returns a U32 value indicating whether the SV contains a character string.
659 Checks the B<private> setting.  Use C<SvPOK> instead.
660
661 =for apidoc Am|U32|SvIOK|SV* sv
662 Returns a U32 value indicating whether the SV contains an integer.
663
664 =for apidoc Am|void|SvIOK_on|SV* sv
665 Tells an SV that it is an integer.
666
667 =for apidoc Am|void|SvIOK_off|SV* sv
668 Unsets the IV status of an SV.
669
670 =for apidoc Am|void|SvIOK_only|SV* sv
671 Tells an SV that it is an integer and disables all other OK bits.
672
673 =for apidoc Am|void|SvIOK_only_UV|SV* sv
674 Tells an SV that it is an unsigned integer and disables all other OK bits.
675
676 =for apidoc Am|bool|SvIOK_UV|SV* sv
677 Returns a boolean indicating whether the SV contains an integer that must be
678 interpreted as unsigned.  A non-negative integer whose value is within the
679 range of both an IV and a UV may be be flagged as either SvUOK or SVIOK.
680
681 =for apidoc Am|bool|SvUOK|SV* sv
682 Returns a boolean indicating whether the SV contains an integer that must be
683 interpreted as unsigned.  A non-negative integer whose value is within the
684 range of both an IV and a UV may be be flagged as either SvUOK or SVIOK.
685
686 =for apidoc Am|bool|SvIOK_notUV|SV* sv
687 Returns a boolean indicating whether the SV contains a signed integer.
688
689 =for apidoc Am|U32|SvNOK|SV* sv
690 Returns a U32 value indicating whether the SV contains a double.
691
692 =for apidoc Am|void|SvNOK_on|SV* sv
693 Tells an SV that it is a double.
694
695 =for apidoc Am|void|SvNOK_off|SV* sv
696 Unsets the NV status of an SV.
697
698 =for apidoc Am|void|SvNOK_only|SV* sv
699 Tells an SV that it is a double and disables all other OK bits.
700
701 =for apidoc Am|U32|SvPOK|SV* sv
702 Returns a U32 value indicating whether the SV contains a character
703 string.
704
705 =for apidoc Am|void|SvPOK_on|SV* sv
706 Tells an SV that it is a string.
707
708 =for apidoc Am|void|SvPOK_off|SV* sv
709 Unsets the PV status of an SV.
710
711 =for apidoc Am|void|SvPOK_only|SV* sv
712 Tells an SV that it is a string and disables all other OK bits.
713 Will also turn off the UTF-8 status.
714
715 =for apidoc Am|bool|SvVOK|SV* sv
716 Returns a boolean indicating whether the SV contains a v-string.
717
718 =for apidoc Am|U32|SvOOK|SV* sv
719 Returns a U32 indicating whether the pointer to the string buffer is offset.
720 This hack is used internally to speed up removal of characters from the
721 beginning of a SvPV.  When SvOOK is true, then the start of the
722 allocated string buffer is actually C<SvOOK_offset()> bytes before SvPVX.
723 This offset used to be stored in SvIVX, but is now stored within the spare
724 part of the buffer.
725
726 =for apidoc Am|U32|SvROK|SV* sv
727 Tests if the SV is an RV.
728
729 =for apidoc Am|void|SvROK_on|SV* sv
730 Tells an SV that it is an RV.
731
732 =for apidoc Am|void|SvROK_off|SV* sv
733 Unsets the RV status of an SV.
734
735 =for apidoc Am|SV*|SvRV|SV* sv
736 Dereferences an RV to return the SV.
737
738 =for apidoc Am|IV|SvIVX|SV* sv
739 Returns the raw value in the SV's IV slot, without checks or conversions.
740 Only use when you are sure SvIOK is true.  See also C<SvIV()>.
741
742 =for apidoc Am|UV|SvUVX|SV* sv
743 Returns the raw value in the SV's UV slot, without checks or conversions.
744 Only use when you are sure SvIOK is true.  See also C<SvUV()>.
745
746 =for apidoc Am|NV|SvNVX|SV* sv
747 Returns the raw value in the SV's NV slot, without checks or conversions.
748 Only use when you are sure SvNOK is true.  See also C<SvNV()>.
749
750 =for apidoc Am|char*|SvPVX|SV* sv
751 Returns a pointer to the physical string in the SV.  The SV must contain a
752 string. Prior to 5.9.3 it is not safe to execute this macro unless the SV's
753 type >= SVt_PV.
754
755 This is also used to store the name of an autoloaded subroutine in an XS
756 AUTOLOAD routine.  See L<perlguts/Autoloading with XSUBs>.
757
758 =for apidoc Am|STRLEN|SvCUR|SV* sv
759 Returns the length of the string which is in the SV.  See C<SvLEN>.
760
761 =for apidoc Am|STRLEN|SvLEN|SV* sv
762 Returns the size of the string buffer in the SV, not including any part
763 attributable to C<SvOOK>.  See C<SvCUR>.
764
765 =for apidoc Am|char*|SvEND|SV* sv
766 Returns a pointer to the spot just after the last character in
767 the string which is in the SV, where there is usually a trailing
768 null (even though Perl scalars do not strictly require it).
769 See C<SvCUR>.  Access the character as *(SvEND(sv)).
770
771 Warning: If C<SvCUR> is equal to C<SvLEN>, then C<SvEND> points to
772 unallocated memory.
773
774 =for apidoc Am|HV*|SvSTASH|SV* sv
775 Returns the stash of the SV.
776
777 =for apidoc Am|void|SvIV_set|SV* sv|IV val
778 Set the value of the IV pointer in sv to val.  It is possible to perform
779 the same function of this macro with an lvalue assignment to C<SvIVX>.
780 With future Perls, however, it will be more efficient to use 
781 C<SvIV_set> instead of the lvalue assignment to C<SvIVX>.
782
783 =for apidoc Am|void|SvNV_set|SV* sv|NV val
784 Set the value of the NV pointer in sv to val.  See C<SvIV_set>.
785
786 =for apidoc Am|void|SvPV_set|SV* sv|char* val
787 Set the value of the PV pointer in sv to val.  See also C<SvIV_set>.
788
789 Beware that the existing pointer may be involved in copy-on-write or other
790 mischief, so do C<SvOOK_off(sv)> and use C<sv_force_normal> or
791 C<SvPV_force> (or check the SvIsCOW flag) first to make sure this
792 modification is safe.
793
794 =for apidoc Am|void|SvUV_set|SV* sv|UV val
795 Set the value of the UV pointer in sv to val.  See C<SvIV_set>.
796
797 =for apidoc Am|void|SvRV_set|SV* sv|SV* val
798 Set the value of the RV pointer in sv to val.  See C<SvIV_set>.
799
800 =for apidoc Am|void|SvMAGIC_set|SV* sv|MAGIC* val
801 Set the value of the MAGIC pointer in sv to val.  See C<SvIV_set>.
802
803 =for apidoc Am|void|SvSTASH_set|SV* sv|HV* val
804 Set the value of the STASH pointer in sv to val.  See C<SvIV_set>.
805
806 =for apidoc Am|void|SvCUR_set|SV* sv|STRLEN len
807 Set the current length of the string which is in the SV.  See C<SvCUR>
808 and C<SvIV_set>.
809
810 =for apidoc Am|void|SvLEN_set|SV* sv|STRLEN len
811 Set the actual length of the string which is in the SV.  See C<SvIV_set>.
812
813 =cut
814 */
815
816 #define SvNIOK(sv)              (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
817 #define SvNIOKp(sv)             (SvFLAGS(sv) & (SVp_IOK|SVp_NOK))
818 #define SvNIOK_off(sv)          (SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK| \
819                                                   SVp_IOK|SVp_NOK|SVf_IVisUV))
820
821 #define assert_not_ROK(sv)      assert_(!SvROK(sv) || !SvRV(sv))
822 #define assert_not_glob(sv)     assert_(!isGV_with_GP(sv))
823
824 #define SvOK(sv)                (SvFLAGS(sv) & SVf_OK || isREGEXP(sv))
825 #define SvOK_off(sv)            (assert_not_ROK(sv) assert_not_glob(sv) \
826                                  SvFLAGS(sv) &= ~(SVf_OK|               \
827                                                   SVf_IVisUV|SVf_UTF8), \
828                                                         SvOOK_off(sv))
829 #define SvOK_off_exc_UV(sv)     (assert_not_ROK(sv)                     \
830                                  SvFLAGS(sv) &= ~(SVf_OK|               \
831                                                   SVf_UTF8),            \
832                                                         SvOOK_off(sv))
833
834 #define SvOKp(sv)               (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
835 #define SvIOKp(sv)              (SvFLAGS(sv) & SVp_IOK)
836 #define SvIOKp_on(sv)           (assert_not_glob(sv) SvRELEASE_IVX_(sv) \
837                                     SvFLAGS(sv) |= SVp_IOK)
838 #define SvNOKp(sv)              (SvFLAGS(sv) & SVp_NOK)
839 #define SvNOKp_on(sv)           (assert_not_glob(sv) SvFLAGS(sv) |= SVp_NOK)
840 #define SvPOKp(sv)              (SvFLAGS(sv) & SVp_POK)
841 #define SvPOKp_on(sv)           (assert_not_ROK(sv) assert_not_glob(sv) \
842                                  SvFLAGS(sv) |= SVp_POK)
843
844 #define SvIOK(sv)               (SvFLAGS(sv) & SVf_IOK)
845 #define SvIOK_on(sv)            (assert_not_glob(sv) SvRELEASE_IVX_(sv) \
846                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
847 #define SvIOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV))
848 #define SvIOK_only(sv)          (SvOK_off(sv), \
849                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
850 #define SvIOK_only_UV(sv)       (assert_not_glob(sv) SvOK_off_exc_UV(sv), \
851                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
852
853 #define SvIOK_UV(sv)            ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))   \
854                                  == (SVf_IOK|SVf_IVisUV))
855 #define SvUOK(sv)               SvIOK_UV(sv)
856 #define SvIOK_notUV(sv)         ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))   \
857                                  == SVf_IOK)
858
859 #define SvIsUV(sv)              (SvFLAGS(sv) & SVf_IVisUV)
860 #define SvIsUV_on(sv)           (SvFLAGS(sv) |= SVf_IVisUV)
861 #define SvIsUV_off(sv)          (SvFLAGS(sv) &= ~SVf_IVisUV)
862
863 #define SvNOK(sv)               (SvFLAGS(sv) & SVf_NOK)
864 #define SvNOK_on(sv)            (assert_not_glob(sv) \
865                                  SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
866 #define SvNOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_NOK|SVp_NOK))
867 #define SvNOK_only(sv)          (SvOK_off(sv), \
868                                     SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
869
870 /*
871 =for apidoc Am|U32|SvUTF8|SV* sv
872 Returns a U32 value indicating the UTF-8 status of an SV.  If things are set-up
873 properly, this indicates whether or not the SV contains UTF-8 encoded data.
874 You should use this I<after> a call to SvPV() or one of its variants, in
875 case any call to string overloading updates the internal flag.
876
877 =for apidoc Am|void|SvUTF8_on|SV *sv
878 Turn on the UTF-8 status of an SV (the data is not changed, just the flag).
879 Do not use frivolously.
880
881 =for apidoc Am|void|SvUTF8_off|SV *sv
882 Unsets the UTF-8 status of an SV (the data is not changed, just the flag).
883 Do not use frivolously.
884
885 =for apidoc Am|void|SvPOK_only_UTF8|SV* sv
886 Tells an SV that it is a string and disables all other OK bits,
887 and leaves the UTF-8 status as it was.
888
889 =cut
890  */
891
892 /* Ensure the return value of this macro does not clash with the GV_ADD* flags
893 in gv.h: */
894 #define SvUTF8(sv)              (SvFLAGS(sv) & SVf_UTF8)
895 #define SvUTF8_on(sv)           (SvFLAGS(sv) |= (SVf_UTF8))
896 #define SvUTF8_off(sv)          (SvFLAGS(sv) &= ~(SVf_UTF8))
897
898 #define SvPOK(sv)               (SvFLAGS(sv) & SVf_POK)
899 #define SvPOK_on(sv)            (assert_not_ROK(sv) assert_not_glob(sv) \
900                                  SvFLAGS(sv) |= (SVf_POK|SVp_POK))
901 #define SvPOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_POK|SVp_POK))
902 #define SvPOK_only(sv)          (assert_not_ROK(sv) assert_not_glob(sv) \
903                                  SvFLAGS(sv) &= ~(SVf_OK|               \
904                                                   SVf_IVisUV|SVf_UTF8), \
905                                     SvFLAGS(sv) |= (SVf_POK|SVp_POK))
906 #define SvPOK_only_UTF8(sv)     (assert_not_ROK(sv) assert_not_glob(sv) \
907                                  SvFLAGS(sv) &= ~(SVf_OK|               \
908                                                   SVf_IVisUV),          \
909                                     SvFLAGS(sv) |= (SVf_POK|SVp_POK))
910
911 #define SvVOK(sv)               (SvMAGICAL(sv)                          \
912                                  && mg_find(sv,PERL_MAGIC_vstring))
913 /* returns the vstring magic, if any */
914 #define SvVSTRING_mg(sv)        (SvMAGICAL(sv) \
915                                  ? mg_find(sv,PERL_MAGIC_vstring) : NULL)
916
917 #define SvOOK(sv)               (SvFLAGS(sv) & SVf_OOK)
918 #define SvOOK_on(sv)            (SvFLAGS(sv) |= SVf_OOK)
919 #define SvOOK_off(sv)           ((void)(SvOOK(sv) && sv_backoff(sv)))
920
921 #define SvFAKE(sv)              (SvFLAGS(sv) & SVf_FAKE)
922 #define SvFAKE_on(sv)           (SvFLAGS(sv) |= SVf_FAKE)
923 #define SvFAKE_off(sv)          (SvFLAGS(sv) &= ~SVf_FAKE)
924
925 #define SvROK(sv)               (SvFLAGS(sv) & SVf_ROK)
926 #define SvROK_on(sv)            (SvFLAGS(sv) |= SVf_ROK)
927 #define SvROK_off(sv)           (SvFLAGS(sv) &= ~(SVf_ROK))
928
929 #define SvMAGICAL(sv)           (SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG))
930 #define SvMAGICAL_on(sv)        (SvFLAGS(sv) |= (SVs_GMG|SVs_SMG|SVs_RMG))
931 #define SvMAGICAL_off(sv)       (SvFLAGS(sv) &= ~(SVs_GMG|SVs_SMG|SVs_RMG))
932
933 #define SvGMAGICAL(sv)          (SvFLAGS(sv) & SVs_GMG)
934 #define SvGMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_GMG)
935 #define SvGMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_GMG)
936
937 #define SvSMAGICAL(sv)          (SvFLAGS(sv) & SVs_SMG)
938 #define SvSMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_SMG)
939 #define SvSMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_SMG)
940
941 #define SvRMAGICAL(sv)          (SvFLAGS(sv) & SVs_RMG)
942 #define SvRMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_RMG)
943 #define SvRMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_RMG)
944
945 #define SvAMAGIC(sv)            (SvROK(sv) && SvOBJECT(SvRV(sv)) &&     \
946                                  HvAMAGIC(SvSTASH(SvRV(sv))))
947
948 /* To be used on the stashes themselves: */
949 #define HvAMAGIC(hv)            (SvFLAGS(hv) & SVf_AMAGIC)
950 #define HvAMAGIC_on(hv)         (SvFLAGS(hv) |= SVf_AMAGIC)
951 #define HvAMAGIC_off(hv)        (SvFLAGS(hv) &=~ SVf_AMAGIC)
952
953
954 /* "nog" means "doesn't have get magic" */
955 #define SvPOK_nog(sv)           ((SvFLAGS(sv) & (SVf_POK|SVs_GMG)) == SVf_POK)
956 #define SvIOK_nog(sv)           ((SvFLAGS(sv) & (SVf_IOK|SVs_GMG)) == SVf_IOK)
957 #define SvUOK_nog(sv)           ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV|SVs_GMG)) == (SVf_IOK|SVf_IVisUV))
958 #define SvNOK_nog(sv)           ((SvFLAGS(sv) & (SVf_NOK|SVs_GMG)) == SVf_NOK)
959 #define SvNIOK_nog(sv)          (SvNIOK(sv) && !(SvFLAGS(sv) & SVs_GMG))
960
961 #define SvPOK_nogthink(sv)      ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
962 #define SvIOK_nogthink(sv)      ((SvFLAGS(sv) & (SVf_IOK|SVf_THINKFIRST|SVs_GMG)) == SVf_IOK)
963 #define SvUOK_nogthink(sv)      ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV|SVf_THINKFIRST|SVs_GMG)) == (SVf_IOK|SVf_IVisUV))
964 #define SvNOK_nogthink(sv)      ((SvFLAGS(sv) & (SVf_NOK|SVf_THINKFIRST|SVs_GMG)) == SVf_NOK)
965 #define SvNIOK_nogthink(sv)     (SvNIOK(sv) && !(SvFLAGS(sv) & (SVf_THINKFIRST|SVs_GMG)))
966
967 #define SvPOK_utf8_nog(sv)      ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVs_GMG)) == (SVf_POK|SVf_UTF8))
968 #define SvPOK_utf8_nogthink(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST|SVs_GMG)) == (SVf_POK|SVf_UTF8))
969
970 #define SvPOK_byte_nog(sv)      ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVs_GMG)) == SVf_POK)
971 #define SvPOK_byte_nogthink(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
972
973 #define SvPOK_pure_nogthink(sv) \
974     ((SvFLAGS(sv) & (SVf_POK|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
975 #define SvPOK_utf8_pure_nogthink(sv) \
976     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == (SVf_POK|SVf_UTF8))
977 #define SvPOK_byte_pure_nogthink(sv) \
978     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
979
980 /*
981 =for apidoc Am|U32|SvGAMAGIC|SV* sv
982
983 Returns true if the SV has get magic or
984 overloading.  If either is true then
985 the scalar is active data, and has the potential to return a new value every
986 time it is accessed.  Hence you must be careful to
987 only read it once per user logical operation and work
988 with that returned value.  If neither is true then
989 the scalar's value cannot change unless written to.
990
991 =cut
992 */
993
994 #define SvGAMAGIC(sv)           (SvGMAGICAL(sv) || SvAMAGIC(sv))
995
996 #define Gv_AMG(stash) \
997         (HvNAME(stash) && Gv_AMupdate(stash,FALSE) \
998             ? 1                                     \
999             : (HvAMAGIC_off(stash), 0))
1000
1001 #define SvWEAKREF(sv)           ((SvFLAGS(sv) & (SVf_ROK|SVprv_WEAKREF)) \
1002                                   == (SVf_ROK|SVprv_WEAKREF))
1003 #define SvWEAKREF_on(sv)        (SvFLAGS(sv) |=  (SVf_ROK|SVprv_WEAKREF))
1004 #define SvWEAKREF_off(sv)       (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_WEAKREF))
1005
1006 #define SvPCS_IMPORTED(sv)      ((SvFLAGS(sv) & (SVf_ROK|SVprv_PCS_IMPORTED)) \
1007                                  == (SVf_ROK|SVprv_PCS_IMPORTED))
1008 #define SvPCS_IMPORTED_on(sv)   (SvFLAGS(sv) |=  (SVf_ROK|SVprv_PCS_IMPORTED))
1009 #define SvPCS_IMPORTED_off(sv)  (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_PCS_IMPORTED))
1010
1011 /*
1012 =for apidoc m|U32|SvTHINKFIRST|SV *sv
1013
1014 A quick flag check to see whether an sv should be passed to sv_force_normal
1015 to be "downgraded" before SvIVX or SvPVX can be modified directly.
1016
1017 For example, if your scalar is a reference and you want to modify the SvIVX
1018 slot, you can't just do SvROK_off, as that will leak the referent.
1019
1020 This is used internally by various sv-modifying functions, such as
1021 sv_setsv, sv_setiv and sv_pvn_force.
1022
1023 One case that this does not handle is a gv without SvFAKE set.  After
1024
1025     if (SvTHINKFIRST(gv)) sv_force_normal(gv);
1026
1027 it will still be a gv.
1028
1029 SvTHINKFIRST sometimes produces false positives.  In those cases
1030 sv_force_normal does nothing.
1031
1032 =cut
1033 */
1034
1035 #define SvTHINKFIRST(sv)        (SvFLAGS(sv) & SVf_THINKFIRST)
1036
1037 #define SvPADMY(sv)             (SvFLAGS(sv) & SVs_PADMY)
1038 #define SvPADMY_on(sv)          (SvFLAGS(sv) |= SVs_PADMY)
1039
1040 /* SVs_PADTMP and SVs_PADSTALE share the same bit, mediated by SVs_PADMY */
1041
1042 #define SvPADTMP(sv)    ((SvFLAGS(sv) & (SVs_PADMY|SVs_PADTMP)) == SVs_PADTMP)
1043 #define SvPADSTALE(sv)  ((SvFLAGS(sv) & (SVs_PADMY|SVs_PADSTALE)) \
1044                                     == (SVs_PADMY|SVs_PADSTALE))
1045
1046 #define SvPADTMP_on(sv)         S_SvPADTMP_on(MUTABLE_SV(sv))
1047 #define SvPADTMP_off(sv)        S_SvPADTMP_off(MUTABLE_SV(sv))
1048 #define SvPADSTALE_on(sv)       S_SvPADSTALE_on(MUTABLE_SV(sv))
1049 #define SvPADSTALE_off(sv)      S_SvPADSTALE_off(MUTABLE_SV(sv))
1050
1051 #define SvTEMP(sv)              (SvFLAGS(sv) & SVs_TEMP)
1052 #define SvTEMP_on(sv)           (SvFLAGS(sv) |= SVs_TEMP)
1053 #define SvTEMP_off(sv)          (SvFLAGS(sv) &= ~SVs_TEMP)
1054
1055 #define SvOBJECT(sv)            (SvFLAGS(sv) & SVs_OBJECT)
1056 #define SvOBJECT_on(sv)         (SvFLAGS(sv) |= SVs_OBJECT)
1057 #define SvOBJECT_off(sv)        (SvFLAGS(sv) &= ~SVs_OBJECT)
1058
1059 #define SvREADONLY(sv)          (SvFLAGS(sv) & SVf_READONLY)
1060 #define SvREADONLY_on(sv)       (SvFLAGS(sv) |= SVf_READONLY)
1061 #define SvREADONLY_off(sv)      (SvFLAGS(sv) &= ~SVf_READONLY)
1062
1063 #define SvSCREAM(sv) ((SvFLAGS(sv) & (SVp_SCREAM|SVp_POK)) == (SVp_SCREAM|SVp_POK))
1064 #define SvSCREAM_on(sv)         (SvFLAGS(sv) |= SVp_SCREAM)
1065 #define SvSCREAM_off(sv)        (SvFLAGS(sv) &= ~SVp_SCREAM)
1066
1067 #ifndef PERL_CORE
1068 #  define SvCOMPILED(sv)        0
1069 #  define SvCOMPILED_on(sv)
1070 #  define SvCOMPILED_off(sv)
1071 #endif
1072
1073 #define SvEVALED(sv)            (SvFLAGS(sv) & SVrepl_EVAL)
1074 #define SvEVALED_on(sv)         (SvFLAGS(sv) |= SVrepl_EVAL)
1075 #define SvEVALED_off(sv)        (SvFLAGS(sv) &= ~SVrepl_EVAL)
1076
1077 #if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
1078 #  define SvVALID(sv)           ({ const SV *const _svvalid = (const SV*)(sv); \
1079                                    if (SvFLAGS(_svvalid) & SVpbm_VALID) \
1080                                        assert(!isGV_with_GP(_svvalid)); \
1081                                    (SvFLAGS(_svvalid) & SVpbm_VALID);   \
1082                                 })
1083 #  define SvVALID_on(sv)        ({ SV *const _svvalid = MUTABLE_SV(sv); \
1084                                    assert(!isGV_with_GP(_svvalid));     \
1085                                    (SvFLAGS(_svvalid) |= SVpbm_VALID);  \
1086                                 })
1087 #  define SvVALID_off(sv)       ({ SV *const _svvalid = MUTABLE_SV(sv); \
1088                                    assert(!isGV_with_GP(_svvalid));     \
1089                                    (SvFLAGS(_svvalid) &= ~SVpbm_VALID); \
1090                                 })
1091
1092 #  define SvTAIL(sv)    ({ const SV *const _svtail = (const SV *)(sv);  \
1093                             assert(SvTYPE(_svtail) != SVt_PVAV);                \
1094                             assert(SvTYPE(_svtail) != SVt_PVHV);                \
1095                             (SvFLAGS(sv) & (SVpbm_TAIL|SVpbm_VALID))    \
1096                                 == (SVpbm_TAIL|SVpbm_VALID);            \
1097                         })
1098 #else
1099 #  define SvVALID(sv)           (SvFLAGS(sv) & SVpbm_VALID)
1100 #  define SvVALID_on(sv)        (SvFLAGS(sv) |= SVpbm_VALID)
1101 #  define SvVALID_off(sv)       (SvFLAGS(sv) &= ~SVpbm_VALID)
1102 #  define SvTAIL(sv)        ((SvFLAGS(sv) & (SVpbm_TAIL|SVpbm_VALID))   \
1103                              == (SVpbm_TAIL|SVpbm_VALID))
1104
1105 #endif
1106 #define SvTAIL_on(sv)           (SvFLAGS(sv) |= SVpbm_TAIL)
1107 #define SvTAIL_off(sv)          (SvFLAGS(sv) &= ~SVpbm_TAIL)
1108
1109
1110 #define SvPAD_TYPED(sv) \
1111         ((SvFLAGS(sv) & (SVpad_NAME|SVpad_TYPED)) == (SVpad_NAME|SVpad_TYPED))
1112
1113 #define SvPAD_OUR(sv)   \
1114         ((SvFLAGS(sv) & (SVpad_NAME|SVpad_OUR)) == (SVpad_NAME|SVpad_OUR))
1115
1116 #define SvPAD_STATE(sv) \
1117         ((SvFLAGS(sv) & (SVpad_NAME|SVpad_STATE)) == (SVpad_NAME|SVpad_STATE))
1118
1119 #if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
1120 #  define SvPAD_TYPED_on(sv)    ({                                      \
1121             SV *const _svpad = MUTABLE_SV(sv);                          \
1122             assert(SvTYPE(_svpad) == SVt_PVMG);                         \
1123             (SvFLAGS(_svpad) |= SVpad_NAME|SVpad_TYPED);                \
1124         })
1125 #define SvPAD_OUR_on(sv)        ({                                      \
1126             SV *const _svpad = MUTABLE_SV(sv);                          \
1127             assert(SvTYPE(_svpad) == SVt_PVMG);                         \
1128             (SvFLAGS(_svpad) |= SVpad_NAME|SVpad_OUR);                  \
1129         })
1130 #define SvPAD_STATE_on(sv)      ({                                      \
1131             SV *const _svpad = MUTABLE_SV(sv);                          \
1132             assert(SvTYPE(_svpad) == SVt_PVNV || SvTYPE(_svpad) == SVt_PVMG); \
1133             (SvFLAGS(_svpad) |= SVpad_NAME|SVpad_STATE);                \
1134         })
1135 #else
1136 #  define SvPAD_TYPED_on(sv)    (SvFLAGS(sv) |= SVpad_NAME|SVpad_TYPED)
1137 #  define SvPAD_OUR_on(sv)      (SvFLAGS(sv) |= SVpad_NAME|SVpad_OUR)
1138 #  define SvPAD_STATE_on(sv)    (SvFLAGS(sv) |= SVpad_NAME|SVpad_STATE)
1139 #endif
1140
1141 #define SvOURSTASH(sv)  \
1142         (SvPAD_OUR(sv) ? ((XPVMG*) SvANY(sv))->xmg_u.xmg_ourstash : NULL)
1143 #define SvOURSTASH_set(sv, st)                                  \
1144         STMT_START {                                            \
1145             assert(SvTYPE(sv) == SVt_PVMG);                     \
1146             ((XPVMG*) SvANY(sv))->xmg_u.xmg_ourstash = st;      \
1147         } STMT_END
1148
1149 #define SvRVx(sv) SvRV(sv)
1150
1151 #ifdef PERL_DEBUG_COW
1152 /* Need -0.0 for SvNVX to preserve IEEE FP "negative zero" because
1153    +0.0 + -0.0 => +0.0 but -0.0 + -0.0 => -0.0 */
1154 #  define SvIVX(sv) (0 + ((XPVIV*) SvANY(sv))->xiv_iv)
1155 #  define SvUVX(sv) (0 + ((XPVUV*) SvANY(sv))->xuv_uv)
1156 #  define SvNVX(sv) (-0.0 + ((XPVNV*) SvANY(sv))->xnv_u.xnv_nv)
1157 #  define SvRV(sv) (0 + (sv)->sv_u.svu_rv)
1158 #  define SvRV_const(sv) (0 + (sv)->sv_u.svu_rv)
1159 /* Don't test the core XS code yet.  */
1160 #  if defined (PERL_CORE) && PERL_DEBUG_COW > 1
1161 #    define SvPVX(sv) (0 + (assert_(!SvREADONLY(sv)) (sv)->sv_u.svu_pv))
1162 #  else
1163 #  define SvPVX(sv) SvPVX_mutable(sv)
1164 #  endif
1165 #  define SvCUR(sv) (0 + ((XPV*) SvANY(sv))->xpv_cur)
1166 #  define SvLEN(sv) (0 + ((XPV*) SvANY(sv))->xpv_len)
1167 #  define SvEND(sv) ((sv)->sv_u.svu_pv + ((XPV*)SvANY(sv))->xpv_cur)
1168
1169 #  define SvMAGIC(sv)   (0 + *(assert_(SvTYPE(sv) >= SVt_PVMG) &((XPVMG*)  SvANY(sv))->xmg_u.xmg_magic))
1170 #  define SvSTASH(sv)   (0 + *(assert_(SvTYPE(sv) >= SVt_PVMG) &((XPVMG*)  SvANY(sv))->xmg_stash))
1171 #else
1172 #  define SvLEN(sv) ((XPV*) SvANY(sv))->xpv_len
1173 #  define SvEND(sv) ((sv)->sv_u.svu_pv + ((XPV*)SvANY(sv))->xpv_cur)
1174
1175 #  if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
1176 /* These get expanded inside other macros that already use a variable _sv  */
1177 #    define SvPVX(sv)                                                   \
1178         (*({ SV *const _svpvx = MUTABLE_SV(sv);                         \
1179             assert(PL_valid_types_PVX[SvTYPE(_svpvx) & SVt_MASK]);      \
1180             assert(!isGV_with_GP(_svpvx));                              \
1181             assert(!(SvTYPE(_svpvx) == SVt_PVIO                         \
1182                      && !(IoFLAGS(_svpvx) & IOf_FAKE_DIRP)));           \
1183             &((_svpvx)->sv_u.svu_pv);                                   \
1184          }))
1185 #    define SvCUR(sv)                                                   \
1186         (*({ const SV *const _svcur = (const SV *)(sv);                 \
1187             assert(PL_valid_types_PVX[SvTYPE(_svcur) & SVt_MASK]        \
1188                 || SvTYPE(_svcur) == SVt_REGEXP);                       \
1189             assert(!isGV_with_GP(_svcur));                              \
1190             assert(!(SvTYPE(_svcur) == SVt_PVIO                         \
1191                      && !(IoFLAGS(_svcur) & IOf_FAKE_DIRP)));           \
1192             &(((XPV*) MUTABLE_PTR(SvANY(_svcur)))->xpv_cur);            \
1193          }))
1194 #    define SvIVX(sv)                                                   \
1195         (*({ const SV *const _svivx = (const SV *)(sv);                 \
1196             assert(PL_valid_types_IVX[SvTYPE(_svivx) & SVt_MASK]);      \
1197             assert(!isGV_with_GP(_svivx));                              \
1198             &(((XPVIV*) MUTABLE_PTR(SvANY(_svivx)))->xiv_iv);           \
1199          }))
1200 #    define SvUVX(sv)                                                   \
1201         (*({ const SV *const _svuvx = (const SV *)(sv);                 \
1202             assert(PL_valid_types_IVX[SvTYPE(_svuvx) & SVt_MASK]);      \
1203             assert(!isGV_with_GP(_svuvx));                              \
1204             &(((XPVUV*) MUTABLE_PTR(SvANY(_svuvx)))->xuv_uv);           \
1205          }))
1206 #    define SvNVX(sv)                                                   \
1207         (*({ const SV *const _svnvx = (const SV *)(sv);                 \
1208             assert(PL_valid_types_NVX[SvTYPE(_svnvx) & SVt_MASK]);      \
1209             assert(!isGV_with_GP(_svnvx));                              \
1210             &(((XPVNV*) MUTABLE_PTR(SvANY(_svnvx)))->xnv_u.xnv_nv);     \
1211          }))
1212 #    define SvRV(sv)                                                    \
1213         (*({ SV *const _svrv = MUTABLE_SV(sv);                          \
1214             assert(PL_valid_types_RV[SvTYPE(_svrv) & SVt_MASK]);        \
1215             assert(!isGV_with_GP(_svrv));                               \
1216             assert(!(SvTYPE(_svrv) == SVt_PVIO                          \
1217                      && !(IoFLAGS(_svrv) & IOf_FAKE_DIRP)));            \
1218             &((_svrv)->sv_u.svu_rv);                                    \
1219          }))
1220 #    define SvRV_const(sv)                                              \
1221         ({ const SV *const _svrv = (const SV *)(sv);                    \
1222             assert(PL_valid_types_RV[SvTYPE(_svrv) & SVt_MASK]);        \
1223             assert(!isGV_with_GP(_svrv));                               \
1224             assert(!(SvTYPE(_svrv) == SVt_PVIO                          \
1225                      && !(IoFLAGS(_svrv) & IOf_FAKE_DIRP)));            \
1226             (_svrv)->sv_u.svu_rv;                                       \
1227          })
1228 #    define SvMAGIC(sv)                                                 \
1229         (*({ const SV *const _svmagic = (const SV *)(sv);               \
1230             assert(SvTYPE(_svmagic) >= SVt_PVMG);                       \
1231             if(SvTYPE(_svmagic) == SVt_PVMG)                            \
1232                 assert(!SvPAD_OUR(_svmagic));                           \
1233             &(((XPVMG*) MUTABLE_PTR(SvANY(_svmagic)))->xmg_u.xmg_magic); \
1234           }))
1235 #    define SvSTASH(sv)                                                 \
1236         (*({ const SV *const _svstash = (const SV *)(sv);               \
1237             assert(SvTYPE(_svstash) >= SVt_PVMG);                       \
1238             &(((XPVMG*) MUTABLE_PTR(SvANY(_svstash)))->xmg_stash);      \
1239           }))
1240 #  else
1241 #    define SvPVX(sv) ((sv)->sv_u.svu_pv)
1242 #    define SvCUR(sv) ((XPV*) SvANY(sv))->xpv_cur
1243 #    define SvIVX(sv) ((XPVIV*) SvANY(sv))->xiv_iv
1244 #    define SvUVX(sv) ((XPVUV*) SvANY(sv))->xuv_uv
1245 #    define SvNVX(sv) ((XPVNV*) SvANY(sv))->xnv_u.xnv_nv
1246 #    define SvRV(sv) ((sv)->sv_u.svu_rv)
1247 #    define SvRV_const(sv) (0 + (sv)->sv_u.svu_rv)
1248 #    define SvMAGIC(sv) ((XPVMG*)  SvANY(sv))->xmg_u.xmg_magic
1249 #    define SvSTASH(sv) ((XPVMG*)  SvANY(sv))->xmg_stash
1250 #  endif
1251 #endif
1252
1253 #ifndef PERL_POISON
1254 /* Given that these two are new, there can't be any existing code using them
1255  *  as LVALUEs  */
1256 #  define SvPVX_mutable(sv)     (0 + (sv)->sv_u.svu_pv)
1257 #  define SvPVX_const(sv)       ((const char*)(0 + (sv)->sv_u.svu_pv))
1258 #else
1259 /* Except for the poison code, which uses & to scribble over the pointer after
1260    free() is called.  */
1261 #  define SvPVX_mutable(sv)     ((sv)->sv_u.svu_pv)
1262 #  define SvPVX_const(sv)       ((const char*)((sv)->sv_u.svu_pv))
1263 #endif
1264
1265 #define SvIVXx(sv) SvIVX(sv)
1266 #define SvUVXx(sv) SvUVX(sv)
1267 #define SvNVXx(sv) SvNVX(sv)
1268 #define SvPVXx(sv) SvPVX(sv)
1269 #define SvLENx(sv) SvLEN(sv)
1270 #define SvENDx(sv) ((PL_Sv = (sv)), SvEND(PL_Sv))
1271
1272
1273 /* Ask a scalar nicely to try to become an IV, if possible.
1274    Not guaranteed to stay returning void */
1275 /* Macro won't actually call sv_2iv if already IOK */
1276 #define SvIV_please(sv) \
1277         STMT_START {if (!SvIOKp(sv) && (SvNOK(sv) || SvPOK(sv))) \
1278                 (void) SvIV(sv); } STMT_END
1279 #define SvIV_please_nomg(sv) \
1280         (!SvIOKp(sv) && (SvNOK(sv) || SvPOK(sv)) \
1281             ? (SvIV_nomg(sv), SvIOK(sv))          \
1282             : SvIOK(sv))
1283 #define SvIV_set(sv, val) \
1284         STMT_START { \
1285                 assert(PL_valid_types_IV_set[SvTYPE(sv) & SVt_MASK]);   \
1286                 assert(!isGV_with_GP(sv));              \
1287                 (((XPVIV*)  SvANY(sv))->xiv_iv = (val)); } STMT_END
1288 #define SvNV_set(sv, val) \
1289         STMT_START { \
1290                 assert(PL_valid_types_NV_set[SvTYPE(sv) & SVt_MASK]);   \
1291                 assert(!isGV_with_GP(sv));              \
1292                 (((XPVNV*)SvANY(sv))->xnv_u.xnv_nv = (val)); } STMT_END
1293 #define SvPV_set(sv, val) \
1294         STMT_START { \
1295                 assert(PL_valid_types_PVX[SvTYPE(sv) & SVt_MASK]);      \
1296                 assert(!isGV_with_GP(sv));              \
1297                 assert(!(SvTYPE(sv) == SVt_PVIO         \
1298                      && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1299                 ((sv)->sv_u.svu_pv = (val)); } STMT_END
1300 #define SvUV_set(sv, val) \
1301         STMT_START { \
1302                 assert(PL_valid_types_IV_set[SvTYPE(sv) & SVt_MASK]);   \
1303                 assert(!isGV_with_GP(sv));              \
1304                 (((XPVUV*)SvANY(sv))->xuv_uv = (val)); } STMT_END
1305 #define SvRV_set(sv, val) \
1306         STMT_START { \
1307                 assert(PL_valid_types_RV[SvTYPE(sv) & SVt_MASK]);       \
1308                 assert(!isGV_with_GP(sv));              \
1309                 assert(!(SvTYPE(sv) == SVt_PVIO         \
1310                      && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1311                 ((sv)->sv_u.svu_rv = (val)); } STMT_END
1312 #define SvMAGIC_set(sv, val) \
1313         STMT_START { assert(SvTYPE(sv) >= SVt_PVMG); \
1314                 (((XPVMG*)SvANY(sv))->xmg_u.xmg_magic = (val)); } STMT_END
1315 #define SvSTASH_set(sv, val) \
1316         STMT_START { assert(SvTYPE(sv) >= SVt_PVMG); \
1317                 (((XPVMG*)  SvANY(sv))->xmg_stash = (val)); } STMT_END
1318 #define SvCUR_set(sv, val) \
1319         STMT_START { \
1320                 assert(PL_valid_types_PVX[SvTYPE(sv) & SVt_MASK]        \
1321                         || SvTYPE(sv) == SVt_REGEXP);   \
1322                 assert(!isGV_with_GP(sv));              \
1323                 assert(!(SvTYPE(sv) == SVt_PVIO         \
1324                      && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1325                 (((XPV*)  SvANY(sv))->xpv_cur = (val)); } STMT_END
1326 #define SvLEN_set(sv, val) \
1327         STMT_START { \
1328                 assert(PL_valid_types_PVX[SvTYPE(sv) & SVt_MASK]);      \
1329                 assert(!isGV_with_GP(sv));      \
1330                 assert(!(SvTYPE(sv) == SVt_PVIO         \
1331                      && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1332                 (((XPV*)  SvANY(sv))->xpv_len = (val)); } STMT_END
1333 #define SvEND_set(sv, val) \
1334         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
1335                 SvCUR_set(sv, (val) - SvPVX(sv)); } STMT_END
1336
1337 #define SvPV_renew(sv,n) \
1338         STMT_START { SvLEN_set(sv, n); \
1339                 SvPV_set((sv), (MEM_WRAP_CHECK_(n,char)                 \
1340                                 (char*)saferealloc((Malloc_t)SvPVX(sv), \
1341                                                    (MEM_SIZE)((n)))));  \
1342                  } STMT_END
1343
1344 #define SvPV_shrink_to_cur(sv) STMT_START { \
1345                    const STRLEN _lEnGtH = SvCUR(sv) + 1; \
1346                    SvPV_renew(sv, _lEnGtH); \
1347                  } STMT_END
1348
1349 #define SvPV_free(sv)                                                   \
1350     STMT_START {                                                        \
1351                      assert(SvTYPE(sv) >= SVt_PV);                      \
1352                      if (SvLEN(sv)) {                                   \
1353                          assert(!SvROK(sv));                            \
1354                          if(SvOOK(sv)) {                                \
1355                              STRLEN zok;                                \
1356                              SvOOK_offset(sv, zok);                     \
1357                              SvPV_set(sv, SvPVX_mutable(sv) - zok);     \
1358                              SvFLAGS(sv) &= ~SVf_OOK;                   \
1359                          }                                              \
1360                          Safefree(SvPVX(sv));                           \
1361                      }                                                  \
1362                  } STMT_END
1363
1364 #ifdef PERL_CORE
1365 /* Code that crops up in three places to take a scalar and ready it to hold
1366    a reference */
1367 #  define prepare_SV_for_RV(sv)                                         \
1368     STMT_START {                                                        \
1369                     if (SvTYPE(sv) < SVt_PV && SvTYPE(sv) != SVt_IV)    \
1370                         sv_upgrade(sv, SVt_IV);                         \
1371                     else if (SvTYPE(sv) >= SVt_PV) {                    \
1372                         SvPV_free(sv);                                  \
1373                         SvLEN_set(sv, 0);                               \
1374                         SvCUR_set(sv, 0);                               \
1375                     }                                                   \
1376                  } STMT_END
1377 #endif
1378
1379 #ifndef PERL_CORE
1380 #  define BmFLAGS(sv)           (SvTAIL(sv) ? FBMcf_TAIL : 0)
1381 #endif
1382
1383 #if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
1384 #  define BmUSEFUL(sv)                                                  \
1385         (*({ SV *const _bmuseful = MUTABLE_SV(sv);                      \
1386             assert(SvTYPE(_bmuseful) == SVt_PVMG);                      \
1387             assert(SvVALID(_bmuseful));                                 \
1388             assert(!SvNOK(_bmuseful));                                  \
1389             &(((XPVMG*) SvANY(_bmuseful))->xnv_u.xbm_useful);           \
1390          }))
1391 #else
1392 #  define BmUSEFUL(sv)          ((XPVMG*) SvANY(sv))->xnv_u.xbm_useful
1393
1394 #endif
1395
1396 #ifndef PERL_CORE
1397 # define BmRARE(sv)     0
1398 # define BmPREVIOUS(sv) 0
1399 #endif
1400
1401 #define FmLINES(sv)     ((XPVIV*)  SvANY(sv))->xiv_iv
1402
1403 #define LvTYPE(sv)      ((XPVLV*)  SvANY(sv))->xlv_type
1404 #define LvTARG(sv)      ((XPVLV*)  SvANY(sv))->xlv_targ
1405 #define LvTARGOFF(sv)   ((XPVLV*)  SvANY(sv))->xlv_targoff
1406 #define LvTARGLEN(sv)   ((XPVLV*)  SvANY(sv))->xlv_targlen
1407 #define LvFLAGS(sv)     ((XPVLV*)  SvANY(sv))->xlv_flags
1408
1409 #define IoIFP(sv)       (sv)->sv_u.svu_fp
1410 #define IoOFP(sv)       ((XPVIO*)  SvANY(sv))->xio_ofp
1411 #define IoDIRP(sv)      ((XPVIO*)  SvANY(sv))->xio_dirp
1412 #define IoANY(sv)       ((XPVIO*)  SvANY(sv))->xio_any
1413 #define IoLINES(sv)     ((XPVIO*)  SvANY(sv))->xiv_u.xivu_iv
1414 #define IoPAGE(sv)      ((XPVIO*)  SvANY(sv))->xio_page
1415 #define IoPAGE_LEN(sv)  ((XPVIO*)  SvANY(sv))->xio_page_len
1416 #define IoLINES_LEFT(sv)((XPVIO*)  SvANY(sv))->xio_lines_left
1417 #define IoTOP_NAME(sv)  ((XPVIO*)  SvANY(sv))->xio_top_name
1418 #define IoTOP_GV(sv)    ((XPVIO*)  SvANY(sv))->xio_top_gv
1419 #define IoFMT_NAME(sv)  ((XPVIO*)  SvANY(sv))->xio_fmt_name
1420 #define IoFMT_GV(sv)    ((XPVIO*)  SvANY(sv))->xio_fmt_gv
1421 #define IoBOTTOM_NAME(sv)((XPVIO*) SvANY(sv))->xio_bottom_name
1422 #define IoBOTTOM_GV(sv) ((XPVIO*)  SvANY(sv))->xio_bottom_gv
1423 #define IoTYPE(sv)      ((XPVIO*)  SvANY(sv))->xio_type
1424 #define IoFLAGS(sv)     ((XPVIO*)  SvANY(sv))->xio_flags
1425
1426 /* IoTYPE(sv) is a single character telling the type of I/O connection. */
1427 #define IoTYPE_RDONLY           '<'
1428 #define IoTYPE_WRONLY           '>'
1429 #define IoTYPE_RDWR             '+'
1430 #define IoTYPE_APPEND           'a'
1431 #define IoTYPE_PIPE             '|'
1432 #define IoTYPE_STD              '-'     /* stdin or stdout */
1433 #define IoTYPE_SOCKET           's'
1434 #define IoTYPE_CLOSED           ' '
1435 #define IoTYPE_IMPLICIT         'I'     /* stdin or stdout or stderr */
1436 #define IoTYPE_NUMERIC          '#'     /* fdopen */
1437
1438 /*
1439 =for apidoc Am|bool|SvTAINTED|SV* sv
1440 Checks to see if an SV is tainted.  Returns TRUE if it is, FALSE if
1441 not.
1442
1443 =for apidoc Am|void|SvTAINTED_on|SV* sv
1444 Marks an SV as tainted if tainting is enabled.
1445
1446 =for apidoc Am|void|SvTAINTED_off|SV* sv
1447 Untaints an SV.  Be I<very> careful with this routine, as it short-circuits
1448 some of Perl's fundamental security features.  XS module authors should not
1449 use this function unless they fully understand all the implications of
1450 unconditionally untainting the value. Untainting should be done in the
1451 standard perl fashion, via a carefully crafted regexp, rather than directly
1452 untainting variables.
1453
1454 =for apidoc Am|void|SvTAINT|SV* sv
1455 Taints an SV if tainting is enabled, and if some input to the current
1456 expression is tainted--usually a variable, but possibly also implicit
1457 inputs such as locale settings.  C<SvTAINT> propagates that taintedness to
1458 the outputs of an expression in a pessimistic fashion; i.e., without paying
1459 attention to precisely which outputs are influenced by which inputs.
1460
1461 =cut
1462 */
1463
1464 #define sv_taint(sv)      sv_magic((sv), NULL, PERL_MAGIC_taint, NULL, 0)
1465
1466 #if NO_TAINT_SUPPORT
1467 #   define SvTAINTED(sv) 0
1468 #else
1469 #   define SvTAINTED(sv)          (SvMAGICAL(sv) && sv_tainted(sv))
1470 #endif
1471 #define SvTAINTED_on(sv)  STMT_START{ if(TAINTING_get){sv_taint(sv);}   }STMT_END
1472 #define SvTAINTED_off(sv) STMT_START{ if(TAINTING_get){sv_untaint(sv);} }STMT_END
1473
1474 #define SvTAINT(sv)                     \
1475     STMT_START {                        \
1476         if (TAINTING_get) {             \
1477             if (TAINT_get)              \
1478                 SvTAINTED_on(sv);       \
1479         }                               \
1480     } STMT_END
1481
1482 /*
1483 =for apidoc Am|char*|SvPV_force|SV* sv|STRLEN len
1484 Like C<SvPV> but will force the SV into containing a string (C<SvPOK>), and
1485 only a string (C<SvPOK_only>), by hook or by crook.  You need force if you are
1486 going to update the C<SvPVX> directly.  Processes get magic.
1487
1488 Note that coercing an arbitrary scalar into a plain PV will potentially
1489 strip useful data from it. For example if the SV was C<SvROK>, then the
1490 referent will have its reference count decremented, and the SV itself may
1491 be converted to an C<SvPOK> scalar with a string buffer containing a value
1492 such as C<"ARRAY(0x1234)">.
1493
1494 =for apidoc Am|char*|SvPV_force_nomg|SV* sv|STRLEN len
1495 Like C<SvPV_force>, but doesn't process get magic.
1496
1497 =for apidoc Am|char*|SvPV|SV* sv|STRLEN len
1498 Returns a pointer to the string in the SV, or a stringified form of
1499 the SV if the SV does not contain a string.  The SV may cache the
1500 stringified version becoming C<SvPOK>.  Handles 'get' magic.  See also
1501 C<SvPVx> for a version which guarantees to evaluate sv only once.
1502
1503 Note that there is no guarantee that the return value of C<SvPV()> is
1504 equal to C<SvPVX(sv)>, or that C<SvPVX(sv)> contains valid data, or that
1505 successive calls to C<SvPV(sv)> will return the same pointer value each
1506 time. This is due to the way that things like overloading and
1507 Copy-On-Write are handled.  In these cases, the return value may point to
1508 a temporary buffer or similar.  If you absolutely need the SvPVX field to
1509 be valid (for example, if you intend to write to it), then see
1510 L</SvPV_force>.
1511
1512 =for apidoc Am|char*|SvPVx|SV* sv|STRLEN len
1513 A version of C<SvPV> which guarantees to evaluate C<sv> only once.
1514 Only use this if C<sv> is an expression with side effects, otherwise use the
1515 more efficient C<SvPV>.
1516
1517 =for apidoc Am|char*|SvPV_nomg|SV* sv|STRLEN len
1518 Like C<SvPV> but doesn't process magic.
1519
1520 =for apidoc Am|char*|SvPV_nolen|SV* sv
1521 Like C<SvPV> but doesn't set a length variable.
1522
1523 =for apidoc Am|char*|SvPV_nomg_nolen|SV* sv
1524 Like C<SvPV_nolen> but doesn't process magic.
1525
1526 =for apidoc Am|IV|SvIV|SV* sv
1527 Coerces the given SV to an integer and returns it.  See C<SvIVx> for a
1528 version which guarantees to evaluate sv only once.
1529
1530 =for apidoc Am|IV|SvIV_nomg|SV* sv
1531 Like C<SvIV> but doesn't process magic.
1532
1533 =for apidoc Am|IV|SvIVx|SV* sv
1534 Coerces the given SV to an integer and returns it.
1535 Guarantees to evaluate C<sv> only once.  Only use
1536 this if C<sv> is an expression with side effects,
1537 otherwise use the more efficient C<SvIV>.
1538
1539 =for apidoc Am|NV|SvNV|SV* sv
1540 Coerce the given SV to a double and return it.  See C<SvNVx> for a version
1541 which guarantees to evaluate sv only once.
1542
1543 =for apidoc Am|NV|SvNV_nomg|SV* sv
1544 Like C<SvNV> but doesn't process magic.
1545
1546 =for apidoc Am|NV|SvNVx|SV* sv
1547 Coerces the given SV to a double and returns it.
1548 Guarantees to evaluate C<sv> only once.  Only use
1549 this if C<sv> is an expression with side effects,
1550 otherwise use the more efficient C<SvNV>.
1551
1552 =for apidoc Am|UV|SvUV|SV* sv
1553 Coerces the given SV to an unsigned integer and returns it.  See C<SvUVx>
1554 for a version which guarantees to evaluate sv only once.
1555
1556 =for apidoc Am|UV|SvUV_nomg|SV* sv
1557 Like C<SvUV> but doesn't process magic.
1558
1559 =for apidoc Am|UV|SvUVx|SV* sv
1560 Coerces the given SV to an unsigned integer and
1561 returns it.  Guarantees to evaluate C<sv> only once.  Only
1562 use this if C<sv> is an expression with side effects,
1563 otherwise use the more efficient C<SvUV>.
1564
1565 =for apidoc Am|bool|SvTRUE|SV* sv
1566 Returns a boolean indicating whether Perl would evaluate the SV as true or
1567 false.  See SvOK() for a defined/undefined test.  Handles 'get' magic
1568 unless the scalar is already SvPOK, SvIOK or SvNOK (the public, not the
1569 private flags).
1570
1571 =for apidoc Am|bool|SvTRUE_nomg|SV* sv
1572 Returns a boolean indicating whether Perl would evaluate the SV as true or
1573 false.  See SvOK() for a defined/undefined test.  Does not handle 'get' magic.
1574
1575 =for apidoc Am|char*|SvPVutf8_force|SV* sv|STRLEN len
1576 Like C<SvPV_force>, but converts sv to utf8 first if necessary.
1577
1578 =for apidoc Am|char*|SvPVutf8|SV* sv|STRLEN len
1579 Like C<SvPV>, but converts sv to utf8 first if necessary.
1580
1581 =for apidoc Am|char*|SvPVutf8_nolen|SV* sv
1582 Like C<SvPV_nolen>, but converts sv to utf8 first if necessary.
1583
1584 =for apidoc Am|char*|SvPVbyte_force|SV* sv|STRLEN len
1585 Like C<SvPV_force>, but converts sv to byte representation first if necessary.
1586
1587 =for apidoc Am|char*|SvPVbyte|SV* sv|STRLEN len
1588 Like C<SvPV>, but converts sv to byte representation first if necessary.
1589
1590 =for apidoc Am|char*|SvPVbyte_nolen|SV* sv
1591 Like C<SvPV_nolen>, but converts sv to byte representation first if necessary.
1592
1593 =for apidoc Am|char*|SvPVutf8x_force|SV* sv|STRLEN len
1594 Like C<SvPV_force>, but converts sv to utf8 first if necessary.
1595 Guarantees to evaluate sv only once; use the more efficient C<SvPVutf8_force>
1596 otherwise.
1597
1598 =for apidoc Am|char*|SvPVutf8x|SV* sv|STRLEN len
1599 Like C<SvPV>, but converts sv to utf8 first if necessary.
1600 Guarantees to evaluate sv only once; use the more efficient C<SvPVutf8>
1601 otherwise.
1602
1603 =for apidoc Am|char*|SvPVbytex_force|SV* sv|STRLEN len
1604 Like C<SvPV_force>, but converts sv to byte representation first if necessary.
1605 Guarantees to evaluate sv only once; use the more efficient C<SvPVbyte_force>
1606 otherwise.
1607
1608 =for apidoc Am|char*|SvPVbytex|SV* sv|STRLEN len
1609 Like C<SvPV>, but converts sv to byte representation first if necessary.
1610 Guarantees to evaluate sv only once; use the more efficient C<SvPVbyte>
1611 otherwise.
1612
1613 =for apidoc Am|bool|SvIsCOW|SV* sv
1614 Returns a boolean indicating whether the SV is Copy-On-Write (either shared
1615 hash key scalars, or full Copy On Write scalars if 5.9.0 is configured for
1616 COW).
1617
1618 =for apidoc Am|bool|SvIsCOW_shared_hash|SV* sv
1619 Returns a boolean indicating whether the SV is Copy-On-Write shared hash key
1620 scalar.
1621
1622 =for apidoc Am|void|sv_catpvn_nomg|SV* sv|const char* ptr|STRLEN len
1623 Like C<sv_catpvn> but doesn't process magic.
1624
1625 =for apidoc Am|void|sv_catpv_nomg|SV* sv|const char* ptr
1626 Like C<sv_catpv> but doesn't process magic.
1627
1628 =for apidoc Am|void|sv_setsv_nomg|SV* dsv|SV* ssv
1629 Like C<sv_setsv> but doesn't process magic.
1630
1631 =for apidoc Am|void|sv_catsv_nomg|SV* dsv|SV* ssv
1632 Like C<sv_catsv> but doesn't process magic.
1633
1634 =for apidoc Amdb|STRLEN|sv_utf8_upgrade_nomg|NN SV *sv
1635
1636 Like sv_utf8_upgrade, but doesn't do magic on C<sv>.
1637
1638 =cut
1639 */
1640
1641 /* Let us hope that bitmaps for UV and IV are the same */
1642 #define SvIV(sv) (SvIOK_nog(sv) ? SvIVX(sv) : sv_2iv(sv))
1643 #define SvUV(sv) (SvUOK_nog(sv) ? SvUVX(sv) : sv_2uv(sv))
1644 #define SvNV(sv) (SvNOK_nog(sv) ? SvNVX(sv) : sv_2nv(sv))
1645
1646 #define SvIV_nomg(sv) (SvIOK(sv) ? SvIVX(sv) : sv_2iv_flags(sv, 0))
1647 #define SvUV_nomg(sv) (SvIOK(sv) ? SvUVX(sv) : sv_2uv_flags(sv, 0))
1648 #define SvNV_nomg(sv) (SvNOK(sv) ? SvNVX(sv) : sv_2nv_flags(sv, 0))
1649
1650 /* ----*/
1651
1652 #define SvPV(sv, lp)         SvPV_flags(sv, lp, SV_GMAGIC)
1653 #define SvPV_const(sv, lp)   SvPV_flags_const(sv, lp, SV_GMAGIC)
1654 #define SvPV_mutable(sv, lp) SvPV_flags_mutable(sv, lp, SV_GMAGIC)
1655
1656 #define SvPV_flags(sv, lp, flags) \
1657     (SvPOK_nog(sv) \
1658      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pv_flags(sv, &lp, flags))
1659 #define SvPV_flags_const(sv, lp, flags) \
1660     (SvPOK_nog(sv) \
1661      ? ((lp = SvCUR(sv)), SvPVX_const(sv)) : \
1662      (const char*) sv_2pv_flags(sv, &lp, flags|SV_CONST_RETURN))
1663 #define SvPV_flags_const_nolen(sv, flags) \
1664     (SvPOK_nog(sv) \
1665      ? SvPVX_const(sv) : \
1666      (const char*) sv_2pv_flags(sv, 0, flags|SV_CONST_RETURN))
1667 #define SvPV_flags_mutable(sv, lp, flags) \
1668     (SvPOK_nog(sv) \
1669      ? ((lp = SvCUR(sv)), SvPVX_mutable(sv)) : \
1670      sv_2pv_flags(sv, &lp, flags|SV_MUTABLE_RETURN))
1671
1672 #define SvPV_force(sv, lp) SvPV_force_flags(sv, lp, SV_GMAGIC)
1673 #define SvPV_force_nolen(sv) SvPV_force_flags_nolen(sv, SV_GMAGIC)
1674 #define SvPV_force_mutable(sv, lp) SvPV_force_flags_mutable(sv, lp, SV_GMAGIC)
1675
1676 #define SvPV_force_nomg(sv, lp) SvPV_force_flags(sv, lp, 0)
1677 #define SvPV_force_nomg_nolen(sv) SvPV_force_flags_nolen(sv, 0)
1678
1679 #define SvPV_force_flags(sv, lp, flags) \
1680     (SvPOK_pure_nogthink(sv) \
1681      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvn_force_flags(sv, &lp, flags))
1682
1683 #define SvPV_force_flags_nolen(sv, flags) \
1684     (SvPOK_pure_nogthink(sv) \
1685      ? SvPVX(sv) : sv_pvn_force_flags(sv, 0, flags))
1686
1687 #define SvPV_force_flags_mutable(sv, lp, flags) \
1688     (SvPOK_pure_nogthink(sv) \
1689      ? ((lp = SvCUR(sv)), SvPVX_mutable(sv)) \
1690      : sv_pvn_force_flags(sv, &lp, flags|SV_MUTABLE_RETURN))
1691
1692 #define SvPV_nolen(sv) \
1693     (SvPOK_nog(sv) \
1694      ? SvPVX(sv) : sv_2pv_flags(sv, 0, SV_GMAGIC))
1695
1696 #define SvPV_nomg_nolen(sv) \
1697     (SvPOK_nog(sv) \
1698      ? SvPVX(sv) : sv_2pv_flags(sv, 0, 0))
1699
1700 #define SvPV_nolen_const(sv) \
1701     (SvPOK_nog(sv) \
1702      ? SvPVX_const(sv) : sv_2pv_flags(sv, 0, SV_GMAGIC|SV_CONST_RETURN))
1703
1704 #define SvPV_nomg(sv, lp) SvPV_flags(sv, lp, 0)
1705 #define SvPV_nomg_const(sv, lp) SvPV_flags_const(sv, lp, 0)
1706 #define SvPV_nomg_const_nolen(sv) SvPV_flags_const_nolen(sv, 0)
1707
1708 /* ----*/
1709
1710 #define SvPVutf8(sv, lp) \
1711     (SvPOK_utf8_nog(sv) \
1712      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvutf8(sv, &lp))
1713
1714 #define SvPVutf8_force(sv, lp) \
1715     (SvPOK_utf8_pure_nogthink(sv) \
1716      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvutf8n_force(sv, &lp))
1717
1718 #define SvPVutf8_nolen(sv) \
1719     (SvPOK_utf8_nog(sv) \
1720      ? SvPVX(sv) : sv_2pvutf8(sv, 0))
1721
1722 /* ----*/
1723
1724 #define SvPVbyte(sv, lp) \
1725     (SvPOK_byte_nog(sv) \
1726      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvbyte(sv, &lp))
1727
1728 #define SvPVbyte_force(sv, lp) \
1729     (SvPOK_byte_pure_nogthink(sv) \
1730      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvbyten_force(sv, &lp))
1731
1732 #define SvPVbyte_nolen(sv) \
1733     (SvPOK_byte_nog(sv) \
1734      ? SvPVX(sv) : sv_2pvbyte(sv, 0))
1735
1736     
1737 /* define FOOx(): idempotent versions of FOO(). If possible, use a local
1738  * var to evaluate the arg once; failing that, use a global if possible;
1739  * failing that, call a function to do the work
1740  */
1741
1742 #define SvPVx_force(sv, lp) sv_pvn_force(sv, &lp)
1743 #define SvPVutf8x_force(sv, lp) sv_pvutf8n_force(sv, &lp)
1744 #define SvPVbytex_force(sv, lp) sv_pvbyten_force(sv, &lp)
1745
1746 #define SvTRUE(sv)        ((sv) && (SvGMAGICAL(sv) ? sv_2bool(sv) : SvTRUE_common(sv, sv_2bool_nomg(sv))))
1747 #define SvTRUE_nomg(sv)   ((sv) && (                                SvTRUE_common(sv, sv_2bool_nomg(sv))))
1748 #define SvTRUE_NN(sv)              (SvGMAGICAL(sv) ? sv_2bool(sv) : SvTRUE_common(sv, sv_2bool_nomg(sv)))
1749 #define SvTRUE_nomg_NN(sv) (                                        SvTRUE_common(sv, sv_2bool_nomg(sv)))
1750 #define SvTRUE_common(sv,fallback) (                    \
1751       !SvOK(sv)                                         \
1752         ? 0                                             \
1753     : SvPOK(sv)                                         \
1754         ? SvPVXtrue(sv)                                 \
1755     : (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))                 \
1756         ? (   (SvIOK(sv) && SvIVX(sv) != 0)             \
1757            || (SvNOK(sv) && SvNVX(sv) != 0.0))          \
1758     : (fallback))
1759
1760 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
1761
1762 #  define SvIVx(sv) ({SV *_sv = MUTABLE_SV(sv); SvIV(_sv); })
1763 #  define SvUVx(sv) ({SV *_sv = MUTABLE_SV(sv); SvUV(_sv); })
1764 #  define SvNVx(sv) ({SV *_sv = MUTABLE_SV(sv); SvNV(_sv); })
1765 #  define SvPVx(sv, lp) ({SV *_sv = (sv); SvPV(_sv, lp); })
1766 #  define SvPVx_const(sv, lp) ({SV *_sv = (sv); SvPV_const(_sv, lp); })
1767 #  define SvPVx_nolen(sv) ({SV *_sv = (sv); SvPV_nolen(_sv); })
1768 #  define SvPVx_nolen_const(sv) ({SV *_sv = (sv); SvPV_nolen_const(_sv); })
1769 #  define SvPVutf8x(sv, lp) ({SV *_sv = (sv); SvPVutf8(_sv, lp); })
1770 #  define SvPVbytex(sv, lp) ({SV *_sv = (sv); SvPVbyte(_sv, lp); })
1771 #  define SvPVbytex_nolen(sv) ({SV *_sv = (sv); SvPVbyte_nolen(_sv); })
1772 #  define SvTRUEx(sv)      ({SV *_sv = (sv); SvTRUE(_sv); })
1773 #  define SvTRUEx_nomg(sv) ({SV *_sv = (sv); SvTRUE_nomg(_sv); })
1774
1775 #else /* __GNUC__ */
1776
1777 /* These inlined macros use globals, which will require a thread
1778  * declaration in user code, so we avoid them under threads */
1779
1780 #  define SvIVx(sv) ((PL_Sv = (sv)), SvIV(PL_Sv))
1781 #  define SvUVx(sv) ((PL_Sv = (sv)), SvUV(PL_Sv))
1782 #  define SvNVx(sv) ((PL_Sv = (sv)), SvNV(PL_Sv))
1783 #  define SvPVx(sv, lp) ((PL_Sv = (sv)), SvPV(PL_Sv, lp))
1784 #  define SvPVx_const(sv, lp) ((PL_Sv = (sv)), SvPV_const(PL_Sv, lp))
1785 #  define SvPVx_nolen(sv) ((PL_Sv = (sv)), SvPV_nolen(PL_Sv))
1786 #  define SvPVx_nolen_const(sv) ((PL_Sv = (sv)), SvPV_nolen_const(PL_Sv))
1787 #  define SvPVutf8x(sv, lp) ((PL_Sv = (sv)), SvPVutf8(PL_Sv, lp))
1788 #  define SvPVbytex(sv, lp) ((PL_Sv = (sv)), SvPVbyte(PL_Sv, lp))
1789 #  define SvPVbytex_nolen(sv) ((PL_Sv = (sv)), SvPVbyte_nolen(PL_Sv))
1790 #  define SvTRUEx(sv)      ((PL_Sv = (sv)), SvTRUE(PL_Sv))
1791 #  define SvTRUEx_nomg(sv) ((PL_Sv = (sv)), SvTRUE_nomg(PL_Sv))
1792 #endif /* __GNU__ */
1793
1794 #define SvPVXtrue(sv)   (                                       \
1795     ((XPV*)SvANY((sv)))                                         \
1796      && (                                                       \
1797         ((XPV*)SvANY((sv)))->xpv_cur > 1                        \
1798         || (                                                    \
1799             ((XPV*)SvANY((sv)))->xpv_cur                        \
1800             && *(sv)->sv_u.svu_pv != '0'                                \
1801         )                                                       \
1802     )                                                           \
1803 )
1804
1805 #define SvIsCOW(sv)             (SvFLAGS(sv) & SVf_IsCOW)
1806 #define SvIsCOW_on(sv)          (SvFLAGS(sv) |= SVf_IsCOW)
1807 #define SvIsCOW_off(sv)         (SvFLAGS(sv) &= ~SVf_IsCOW)
1808 #define SvIsCOW_shared_hash(sv) (SvIsCOW(sv) && SvLEN(sv) == 0)
1809
1810 #define SvSHARED_HEK_FROM_PV(pvx) \
1811         ((struct hek*)(pvx - STRUCT_OFFSET(struct hek, hek_key)))
1812 #define SvSHARED_HASH(sv) (0 + SvSHARED_HEK_FROM_PV(SvPVX_const(sv))->hek_hash)
1813
1814 /* flag values for sv_*_flags functions */
1815 #define SV_IMMEDIATE_UNREF      1
1816 #define SV_GMAGIC               2
1817 #define SV_COW_DROP_PV          4
1818 #define SV_UTF8_NO_ENCODING     8
1819 #define SV_NOSTEAL              16
1820 #define SV_CONST_RETURN         32
1821 #define SV_MUTABLE_RETURN       64
1822 #define SV_SMAGIC               128
1823 #define SV_HAS_TRAILING_NUL     256
1824 #define SV_COW_SHARED_HASH_KEYS 512
1825 /* This one is only enabled for PERL_OLD_COPY_ON_WRITE */
1826 #define SV_COW_OTHER_PVS        1024
1827 /* Make sv_2pv_flags return NULL if something is undefined.  */
1828 #define SV_UNDEF_RETURNS_NULL   2048
1829 /* Tell sv_utf8_upgrade() to not check to see if an upgrade is really needed.
1830  * This is used when the caller has already determined it is, and avoids
1831  * redundant work */
1832 #define SV_FORCE_UTF8_UPGRADE   4096
1833 /* if (after resolving magic etc), the SV is found to be overloaded,
1834  * don't call the overload magic, just return as-is */
1835 #define SV_SKIP_OVERLOAD        8192
1836 /* It is not yet clear whether we want this as an API, or what the
1837  * constants should be named. */
1838 #ifdef PERL_CORE
1839 # define SV_CATBYTES            16384
1840 # define SV_CATUTF8             32768
1841 #endif
1842
1843 /* The core is safe for this COW optimisation. XS code on CPAN may not be.
1844    So only default to doing the COW setup if we're in the core.
1845  */
1846 #ifdef PERL_CORE
1847 #  ifndef SV_DO_COW_SVSETSV
1848 #    define SV_DO_COW_SVSETSV   SV_COW_SHARED_HASH_KEYS|SV_COW_OTHER_PVS
1849 #  endif
1850 #endif
1851
1852 #ifndef SV_DO_COW_SVSETSV
1853 #  define SV_DO_COW_SVSETSV     0
1854 #endif
1855
1856
1857 #define sv_unref(sv)            sv_unref_flags(sv, 0)
1858 #define sv_force_normal(sv)     sv_force_normal_flags(sv, 0)
1859 #define sv_usepvn(sv, p, l)     sv_usepvn_flags(sv, p, l, 0)
1860 #define sv_usepvn_mg(sv, p, l)  sv_usepvn_flags(sv, p, l, SV_SMAGIC)
1861
1862 /* We are about to replace the SV's current value. So if it's copy on write
1863    we need to normalise it. Use the SV_COW_DROP_PV flag hint to say that
1864    the value is about to get thrown away, so drop the PV rather than go to
1865    the effort of making a read-write copy only for it to get immediately
1866    discarded.  */
1867
1868 #define SV_CHECK_THINKFIRST_COW_DROP(sv) if (SvTHINKFIRST(sv)) \
1869                                     sv_force_normal_flags(sv, SV_COW_DROP_PV)
1870
1871 #ifdef PERL_OLD_COPY_ON_WRITE
1872 #define SvRELEASE_IVX(sv)   \
1873     ((SvIsCOW(sv) ? sv_force_normal_flags(sv, 0) : (void) 0), 0)
1874 #  define SvIsCOW_normal(sv)    (SvIsCOW(sv) && SvLEN(sv))
1875 #  define SvRELEASE_IVX_(sv)    SvRELEASE_IVX(sv),
1876 #  define SvCANCOW(sv) \
1877         (SvIsCOW(sv) || (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS)
1878 /* This is a pessimistic view. Scalar must be purely a read-write PV to copy-
1879    on-write.  */
1880 #  define CAN_COW_MASK  (SVs_OBJECT|SVs_GMG|SVs_SMG|SVs_RMG|SVf_IOK|SVf_NOK| \
1881                          SVf_POK|SVf_ROK|SVp_IOK|SVp_NOK|SVp_POK|SVf_FAKE| \
1882                          SVf_OOK|SVf_BREAK|SVf_READONLY)
1883 #else
1884 #  define SvRELEASE_IVX(sv)   0
1885 /* This little game brought to you by the need to shut this warning up:
1886 mg.c: In function 'Perl_magic_get':
1887 mg.c:1024: warning: left-hand operand of comma expression has no effect
1888 */
1889 #  define SvRELEASE_IVX_(sv)  /**/
1890 #  ifdef PERL_NEW_COPY_ON_WRITE
1891 #   define SvCANCOW(sv)                                     \
1892         (SvIsCOW(sv)                                         \
1893          ? SvLEN(sv) ? CowREFCNT(sv) != SV_COW_REFCNT_MAX : 1 \
1894          : (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS       \
1895                             && SvCUR(sv)+1 < SvLEN(sv))
1896    /* Note: To allow 256 COW "copies", a refcnt of 0 means 1. */
1897 #   define CowREFCNT(sv)        (*(U8 *)(SvPVX(sv)+SvLEN(sv)-1))
1898 #   define SV_COW_REFCNT_MAX    ((1 << sizeof(U8)*8) - 1)
1899 #   ifndef SV_COW_THRESHOLD
1900 #    define SV_COW_THRESHOLD    0       /* min string length for cow */
1901 #   endif
1902 #   ifndef SV_COWBUF_THRESHOLD
1903 #    define SV_COWBUF_THRESHOLD 1250    /* min string length for cow */
1904 #   endif                               /* over existing buffer */
1905 #   define CAN_COW_MASK (SVf_POK|SVf_ROK|SVp_POK|SVf_FAKE| \
1906                          SVf_OOK|SVf_BREAK|SVf_READONLY)
1907 #  endif
1908 #endif /* PERL_OLD_COPY_ON_WRITE */
1909
1910 #define CAN_COW_FLAGS   (SVp_POK|SVf_POK)
1911
1912 #define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) \
1913                                     sv_force_normal_flags(sv, 0)
1914
1915
1916 /* all these 'functions' are now just macros */
1917
1918 #define sv_pv(sv) SvPV_nolen(sv)
1919 #define sv_pvutf8(sv) SvPVutf8_nolen(sv)
1920 #define sv_pvbyte(sv) SvPVbyte_nolen(sv)
1921
1922 #define sv_pvn_force_nomg(sv, lp) sv_pvn_force_flags(sv, lp, 0)
1923 #define sv_utf8_upgrade_flags(sv, flags) sv_utf8_upgrade_flags_grow(sv, flags, 0)
1924 #define sv_utf8_upgrade_nomg(sv) sv_utf8_upgrade_flags(sv, 0)
1925 #define sv_catpvn_nomg(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, 0)
1926 #define sv_catpv_nomg(dsv, sstr) sv_catpv_flags(dsv, sstr, 0)
1927 #define sv_setsv(dsv, ssv) \
1928         sv_setsv_flags(dsv, ssv, SV_GMAGIC|SV_DO_COW_SVSETSV)
1929 #define sv_setsv_nomg(dsv, ssv) sv_setsv_flags(dsv, ssv, SV_DO_COW_SVSETSV)
1930 #define sv_catsv(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC)
1931 #define sv_catsv_nomg(dsv, ssv) sv_catsv_flags(dsv, ssv, 0)
1932 #define sv_catsv_mg(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC|SV_SMAGIC)
1933 #define sv_catpvn(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC)
1934 #define sv_catpvn_mg(sv, sstr, slen) sv_catpvn_flags(sv, sstr, slen, SV_GMAGIC|SV_SMAGIC);
1935 #define sv_copypv(dsv, ssv) sv_copypv_flags(dsv, ssv, SV_GMAGIC)
1936 #define sv_copypv_nomg(dsv, ssv) sv_copypv_flags(dsv, ssv, 0)
1937 #define sv_2pv(sv, lp) sv_2pv_flags(sv, lp, SV_GMAGIC)
1938 #define sv_2pv_nolen(sv) sv_2pv(sv, 0)
1939 #define sv_2pvbyte_nolen(sv) sv_2pvbyte(sv, 0)
1940 #define sv_2pvutf8_nolen(sv) sv_2pvutf8(sv, 0)
1941 #define sv_2pv_nomg(sv, lp) sv_2pv_flags(sv, lp, 0)
1942 #define sv_pvn_force(sv, lp) sv_pvn_force_flags(sv, lp, SV_GMAGIC)
1943 #define sv_utf8_upgrade(sv) sv_utf8_upgrade_flags(sv, SV_GMAGIC)
1944 #define sv_2iv(sv) sv_2iv_flags(sv, SV_GMAGIC)
1945 #define sv_2uv(sv) sv_2uv_flags(sv, SV_GMAGIC)
1946 #define sv_2nv(sv) sv_2nv_flags(sv, SV_GMAGIC)
1947 #define sv_eq(sv1, sv2) sv_eq_flags(sv1, sv2, SV_GMAGIC)
1948 #define sv_cmp(sv1, sv2) sv_cmp_flags(sv1, sv2, SV_GMAGIC)
1949 #define sv_cmp_locale(sv1, sv2) sv_cmp_locale_flags(sv1, sv2, SV_GMAGIC)
1950 #define sv_collxfrm(sv, nxp) sv_cmp_flags(sv, nxp, SV_GMAGIC)
1951 #define sv_2bool(sv) sv_2bool_flags(sv, SV_GMAGIC)
1952 #define sv_2bool_nomg(sv) sv_2bool_flags(sv, 0)
1953 #define sv_insert(bigstr, offset, len, little, littlelen)               \
1954         Perl_sv_insert_flags(aTHX_ (bigstr),(offset), (len), (little),  \
1955                              (littlelen), SV_GMAGIC)
1956 #define sv_mortalcopy(sv) \
1957         Perl_sv_mortalcopy_flags(aTHX_ sv, SV_GMAGIC|SV_DO_COW_SVSETSV)
1958
1959 /* Should be named SvCatPVN_utf8_upgrade? */
1960 #define sv_catpvn_nomg_utf8_upgrade(dsv, sstr, slen, nsv)       \
1961         STMT_START {                                    \
1962             if (!(nsv))                                 \
1963                 nsv = newSVpvn_flags(sstr, slen, SVs_TEMP);     \
1964             else                                        \
1965                 sv_setpvn(nsv, sstr, slen);             \
1966             SvUTF8_off(nsv);                            \
1967             sv_utf8_upgrade(nsv);                       \
1968             sv_catsv_nomg(dsv, nsv);                    \
1969         } STMT_END
1970 #define sv_catpvn_nomg_maybeutf8(dsv, sstr, slen, is_utf8) \
1971         sv_catpvn_flags(dsv, sstr, slen, (is_utf8)?SV_CATUTF8:SV_CATBYTES)
1972
1973 #ifdef PERL_CORE
1974 # define sv_or_pv_len_utf8(sv, pv, bytelen)           \
1975     (SvGAMAGIC(sv)                                     \
1976         ? utf8_length((U8 *)(pv), (U8 *)(pv)+(bytelen)) \
1977         : sv_len_utf8(sv))
1978 # define sv_or_pv_pos_u2b(sv,s,p,lp) S_sv_or_pv_pos_u2b(aTHX_ sv,s,p,lp)
1979 #endif
1980
1981 /*
1982 =for apidoc Am|SV*|newRV_inc|SV* sv
1983
1984 Creates an RV wrapper for an SV.  The reference count for the original SV is
1985 incremented.
1986
1987 =cut
1988 */
1989
1990 #define newRV_inc(sv)   newRV(sv)
1991
1992 /* the following macros update any magic values this sv is associated with */
1993
1994 /*
1995 =head1 Magical Functions
1996
1997 =for apidoc Am|void|SvGETMAGIC|SV* sv
1998 Invokes C<mg_get> on an SV if it has 'get' magic.  For example, this
1999 will call C<FETCH> on a tied variable.  This macro evaluates its
2000 argument more than once.
2001
2002 =for apidoc Am|void|SvSETMAGIC|SV* sv
2003 Invokes C<mg_set> on an SV if it has 'set' magic.  This is necessary
2004 after modifying a scalar, in case it is a magical variable like C<$|>
2005 or a tied variable (it calls C<STORE>).  This macro evaluates its
2006 argument more than once.
2007
2008 =for apidoc Am|void|SvSetSV|SV* dsb|SV* ssv
2009 Calls C<sv_setsv> if dsv is not the same as ssv.  May evaluate arguments
2010 more than once.
2011
2012 =for apidoc Am|void|SvSetSV_nosteal|SV* dsv|SV* ssv
2013 Calls a non-destructive version of C<sv_setsv> if dsv is not the same as
2014 ssv.  May evaluate arguments more than once.
2015
2016 =for apidoc Am|void|SvSetMagicSV|SV* dsb|SV* ssv
2017 Like C<SvSetSV>, but does any set magic required afterwards.
2018
2019 =for apidoc Am|void|SvSetMagicSV_nosteal|SV* dsv|SV* ssv
2020 Like C<SvSetSV_nosteal>, but does any set magic required afterwards.
2021
2022 =for apidoc Am|void|SvSHARE|SV* sv
2023 Arranges for sv to be shared between threads if a suitable module
2024 has been loaded.
2025
2026 =for apidoc Am|void|SvLOCK|SV* sv
2027 Arranges for a mutual exclusion lock to be obtained on sv if a suitable module
2028 has been loaded.
2029
2030 =for apidoc Am|void|SvUNLOCK|SV* sv
2031 Releases a mutual exclusion lock on sv if a suitable module
2032 has been loaded.
2033
2034 =head1 SV Manipulation Functions
2035
2036 =for apidoc Am|char *|SvGROW|SV* sv|STRLEN len
2037 Expands the character buffer in the SV so that it has room for the
2038 indicated number of bytes (remember to reserve space for an extra trailing
2039 NUL character).  Calls C<sv_grow> to perform the expansion if necessary.
2040 Returns a pointer to the character buffer. SV must be of type >= SVt_PV. One
2041 alternative is to call C<sv_grow> if you are not sure of the type of SV.
2042
2043 =cut
2044 */
2045
2046 #define SvSHARE(sv) PL_sharehook(aTHX_ sv)
2047 #define SvLOCK(sv) PL_lockhook(aTHX_ sv)
2048 #define SvUNLOCK(sv) PL_unlockhook(aTHX_ sv)
2049 #define SvDESTROYABLE(sv) PL_destroyhook(aTHX_ sv)
2050
2051 #define SvGETMAGIC(x) ((void)(SvGMAGICAL(x) && mg_get(x)))
2052 #define SvSETMAGIC(x) STMT_START { if (SvSMAGICAL(x)) mg_set(x); } STMT_END
2053
2054 #define SvSetSV_and(dst,src,finally) \
2055         STMT_START {                                    \
2056             if ((dst) != (src)) {                       \
2057                 sv_setsv(dst, src);                     \
2058                 finally;                                \
2059             }                                           \
2060         } STMT_END
2061 #define SvSetSV_nosteal_and(dst,src,finally) \
2062         STMT_START {                                    \
2063             if ((dst) != (src)) {                       \
2064                 sv_setsv_flags(dst, src, SV_GMAGIC | SV_NOSTEAL | SV_DO_COW_SVSETSV);   \
2065                 finally;                                \
2066             }                                           \
2067         } STMT_END
2068
2069 #define SvSetSV(dst,src) \
2070                 SvSetSV_and(dst,src,/*nothing*/;)
2071 #define SvSetSV_nosteal(dst,src) \
2072                 SvSetSV_nosteal_and(dst,src,/*nothing*/;)
2073
2074 #define SvSetMagicSV(dst,src) \
2075                 SvSetSV_and(dst,src,SvSETMAGIC(dst))
2076 #define SvSetMagicSV_nosteal(dst,src) \
2077                 SvSetSV_nosteal_and(dst,src,SvSETMAGIC(dst))
2078
2079
2080 #if !defined(SKIP_DEBUGGING)
2081 #define SvPEEK(sv) sv_peek(sv)
2082 #else
2083 #define SvPEEK(sv) ""
2084 #endif
2085
2086 #define SvIMMORTAL(sv) (SvREADONLY(sv) && ((sv)==&PL_sv_undef || (sv)==&PL_sv_yes || (sv)==&PL_sv_no || (sv)==&PL_sv_placeholder))
2087
2088 #ifdef DEBUGGING
2089    /* exercise the immortal resurrection code in sv_free2() */
2090 #  define SvREFCNT_IMMORTAL 1000
2091 #else
2092 #  define SvREFCNT_IMMORTAL ((~(U32)0)/2)
2093 #endif
2094
2095 /*
2096 =for apidoc Am|SV *|boolSV|bool b
2097
2098 Returns a true SV if C<b> is a true value, or a false SV if C<b> is 0.
2099
2100 See also C<PL_sv_yes> and C<PL_sv_no>.
2101
2102 =cut
2103 */
2104
2105 #define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
2106
2107 #define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
2108 /* If I give every macro argument a different name, then there won't be bugs
2109    where nested macros get confused. Been there, done that.  */
2110 #define isGV_with_GP(pwadak) \
2111         (((SvFLAGS(pwadak) & (SVp_POK|SVpgv_GP)) == SVpgv_GP)   \
2112         && (SvTYPE(pwadak) == SVt_PVGV || SvTYPE(pwadak) == SVt_PVLV))
2113 #define isGV_with_GP_on(sv)     STMT_START {                           \
2114         assert (SvTYPE(sv) == SVt_PVGV || SvTYPE(sv) == SVt_PVLV); \
2115         assert (!SvPOKp(sv));                                          \
2116         assert (!SvIOKp(sv));                                          \
2117         (SvFLAGS(sv) |= SVpgv_GP);                                     \
2118     } STMT_END
2119 #define isGV_with_GP_off(sv)    STMT_START {                           \
2120         assert (SvTYPE(sv) == SVt_PVGV || SvTYPE(sv) == SVt_PVLV); \
2121         assert (!SvPOKp(sv));                                          \
2122         assert (!SvIOKp(sv));                                          \
2123         (SvFLAGS(sv) &= ~SVpgv_GP);                                    \
2124     } STMT_END
2125 #define isREGEXP(sv) \
2126     (SvTYPE(sv) == SVt_REGEXP                                 \
2127      || (SvFLAGS(sv) & (SVTYPEMASK|SVp_POK|SVpgv_GP|SVf_FAKE)) \
2128          == (SVt_PVLV|SVf_FAKE))
2129
2130
2131 #ifdef PERL_NEW_COPY_ON_WRITE
2132 # define SvGROW(sv,len) \
2133         (SvIsCOW(sv) || SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
2134 #else
2135 # define SvGROW(sv,len) (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
2136 #endif
2137 #define SvGROW_mutable(sv,len) \
2138     (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX_mutable(sv))
2139 #define Sv_Grow sv_grow
2140
2141 #define CLONEf_COPY_STACKS 1
2142 #define CLONEf_KEEP_PTR_TABLE 2
2143 #define CLONEf_CLONE_HOST 4
2144 #define CLONEf_JOIN_IN 8
2145
2146 struct clone_params {
2147   AV* stashes;
2148   UV  flags;
2149   PerlInterpreter *proto_perl;
2150   PerlInterpreter *new_perl;
2151   AV *unreferenced;
2152 };
2153
2154 /*
2155 =for apidoc Am|SV*|newSVpvn_utf8|NULLOK const char* s|STRLEN len|U32 utf8
2156
2157 Creates a new SV and copies a string into it.  If utf8 is true, calls
2158 C<SvUTF8_on> on the new SV.  Implemented as a wrapper around C<newSVpvn_flags>.
2159
2160 =cut
2161 */
2162
2163 #define newSVpvn_utf8(s, len, u) newSVpvn_flags((s), (len), (u) ? SVf_UTF8 : 0)
2164
2165 /*
2166 =for apidoc Amx|SV*|newSVpadname|PADNAME *pn
2167
2168 Creates a new SV containing the pad name.  This is currently identical
2169 to C<newSVsv>, but pad names may cease being SVs at some point, so
2170 C<newSVpadname> is preferable.
2171
2172 =cut
2173 */
2174
2175 #define newSVpadname(pn) newSVsv(pn)
2176
2177 /*
2178 =for apidoc Am|void|SvOOK_offset|NN SV*sv|STRLEN len
2179
2180 Reads into I<len> the offset from SvPVX back to the true start of the
2181 allocated buffer, which will be non-zero if C<sv_chop> has been used to
2182 efficiently remove characters from start of the buffer.  Implemented as a
2183 macro, which takes the address of I<len>, which must be of type C<STRLEN>.
2184 Evaluates I<sv> more than once.  Sets I<len> to 0 if C<SvOOK(sv)> is false.
2185
2186 =cut
2187 */
2188
2189 #ifdef DEBUGGING
2190 /* Does the bot know something I don't?
2191 10:28 <@Nicholas> metabatman
2192 10:28 <+meta> Nicholas: crash
2193 */
2194 #  define SvOOK_offset(sv, offset) STMT_START {                         \
2195         assert(sizeof(offset) == sizeof(STRLEN));                       \
2196         if (SvOOK(sv)) {                                                \
2197             const U8 *_crash = (U8*)SvPVX_const(sv);                    \
2198             (offset) = *--_crash;                                       \
2199             if (!(offset)) {                                            \
2200                 _crash -= sizeof(STRLEN);                               \
2201                 Copy(_crash, (U8 *)&(offset), sizeof(STRLEN), U8);      \
2202             }                                                           \
2203             {                                                           \
2204                 /* Validate the preceding buffer's sentinels to         \
2205                    verify that no-one is using it.  */                  \
2206                 const U8 *const _bonk = (U8*)SvPVX_const(sv) - (offset);\
2207                 while (_crash > _bonk) {                                \
2208                     --_crash;                                           \
2209                     assert (*_crash == (U8)PTR2UV(_crash));             \
2210                 }                                                       \
2211             }                                                           \
2212         } else {                                                        \
2213             (offset) = 0;                                               \
2214         }                                                               \
2215     } STMT_END
2216 #else
2217     /* This is the same code, but avoids using any temporary variables:  */
2218 #  define SvOOK_offset(sv, offset) STMT_START {                         \
2219         assert(sizeof(offset) == sizeof(STRLEN));                       \
2220         if (SvOOK(sv)) {                                                \
2221             (offset) = ((U8*)SvPVX_const(sv))[-1];                      \
2222             if (!(offset)) {                                            \
2223                 Copy(SvPVX_const(sv) - 1 - sizeof(STRLEN),              \
2224                      (U8*)&(offset), sizeof(STRLEN), U8);               \
2225             }                                                           \
2226         } else {                                                        \
2227             (offset) = 0;                                               \
2228         }                                                               \
2229     } STMT_END
2230 #endif
2231
2232 #define newIO() MUTABLE_IO(newSV_type(SVt_PVIO))
2233
2234 #define SV_CONST(name) \
2235         PL_sv_consts[SV_CONST_##name] \
2236                 ? PL_sv_consts[SV_CONST_##name] \
2237                 : (PL_sv_consts[SV_CONST_##name] = newSVpv_share(#name, 0))
2238
2239 #define SV_CONST_TIESCALAR 0
2240 #define SV_CONST_TIEARRAY 1
2241 #define SV_CONST_TIEHASH 2
2242 #define SV_CONST_TIEHANDLE 3
2243
2244 #define SV_CONST_FETCH 4
2245 #define SV_CONST_FETCHSIZE 5
2246 #define SV_CONST_STORE 6
2247 #define SV_CONST_STORESIZE 7
2248 #define SV_CONST_EXISTS 8
2249
2250 #define SV_CONST_PUSH 9
2251 #define SV_CONST_POP 10
2252 #define SV_CONST_SHIFT 11
2253 #define SV_CONST_UNSHIFT 12
2254 #define SV_CONST_SPLICE 13
2255 #define SV_CONST_EXTEND 14
2256
2257 #define SV_CONST_FIRSTKEY 15
2258 #define SV_CONST_NEXTKEY 16
2259 #define SV_CONST_SCALAR 17
2260
2261 #define SV_CONST_OPEN 18
2262 #define SV_CONST_WRITE 19
2263 #define SV_CONST_PRINT 20
2264 #define SV_CONST_PRINTF 21
2265 #define SV_CONST_READ 22
2266 #define SV_CONST_READLINE 23
2267 #define SV_CONST_GETC 24
2268 #define SV_CONST_SEEK 25
2269 #define SV_CONST_TELL 26
2270 #define SV_CONST_EOF 27
2271 #define SV_CONST_BINMODE 28
2272 #define SV_CONST_FILENO 29
2273 #define SV_CONST_CLOSE 30
2274
2275 #define SV_CONST_DELETE 31
2276 #define SV_CONST_CLEAR 32
2277 #define SV_CONST_UNTIE 33
2278 #define SV_CONST_DESTROY 34
2279
2280 #define SV_CONSTS_COUNT 35
2281
2282 /*
2283  * Local variables:
2284  * c-indentation-style: bsd
2285  * c-basic-offset: 4
2286  * indent-tabs-mode: nil
2287  * End:
2288  *
2289  * ex: set ts=8 sts=4 sw=4 et:
2290  */