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