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