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