This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate:
[perl5.git] / sv.h
1 /*    sv.h
2  *
3  *    Copyright (c) 1991-2003, 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 #if defined(__GNUC__) && !defined(__STRICT_ANSI__) && !defined(PERL_GCC_PEDANTIC)
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      /* SvPV 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     /* (SVp_?OK >> PRIVSHIFT) == SVf_?OK */
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     U32         xcv_outside_seq; /* the COP sequence (at the point of our
359                                   * compilation) in the lexically enclosing
360                                   * sub */
361     IV          xfm_lines;
362 };
363
364 struct xpvio {
365     char *      xpv_pv;         /* pointer to malloced string */
366     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
367     STRLEN      xpv_len;        /* allocated size */
368     IV          xiv_iv;         /* integer value or pv offset */
369     NV          xnv_nv;         /* numeric value, if any */
370     MAGIC*      xmg_magic;      /* linked list of magicalness */
371     HV*         xmg_stash;      /* class package */
372
373     PerlIO *    xio_ifp;        /* ifp and ofp are normally the same */
374     PerlIO *    xio_ofp;        /* but sockets need separate streams */
375     /* Cray addresses everything by word boundaries (64 bits) and
376      * code and data pointers cannot be mixed (which is exactly what
377      * Perl_filter_add() tries to do with the dirp), hence the following
378      * union trick (as suggested by Gurusamy Sarathy).
379      * For further information see Geir Johansen's problem report titled
380        [ID 20000612.002] Perl problem on Cray system
381      * The any pointer (known as IoANY()) will also be a good place
382      * to hang any IO disciplines to.
383      */
384     union {
385         DIR *   xiou_dirp;      /* for opendir, readdir, etc */
386         void *  xiou_any;       /* for alignment */
387     } xio_dirpu;
388     IV          xio_lines;      /* $. */
389     IV          xio_page;       /* $% */
390     IV          xio_page_len;   /* $= */
391     IV          xio_lines_left; /* $- */
392     char *      xio_top_name;   /* $^ */
393     GV *        xio_top_gv;     /* $^ */
394     char *      xio_fmt_name;   /* $~ */
395     GV *        xio_fmt_gv;     /* $~ */
396     char *      xio_bottom_name;/* $^B */
397     GV *        xio_bottom_gv;  /* $^B */
398     short       xio_subprocess; /* -| or |- */
399     char        xio_type;
400     char        xio_flags;
401 };
402 #define xio_dirp        xio_dirpu.xiou_dirp
403 #define xio_any         xio_dirpu.xiou_any
404
405 #define IOf_ARGV        1       /* this fp iterates over ARGV */
406 #define IOf_START       2       /* check for null ARGV and substitute '-' */
407 #define IOf_FLUSH       4       /* this fp wants a flush after write op */
408 #define IOf_DIDTOP      8       /* just did top of form */
409 #define IOf_UNTAINT     16      /* consider this fp (and its data) "safe" */
410 #define IOf_NOLINE      32      /* slurped a pseudo-line from empty file */
411 #define IOf_FAKE_DIRP   64      /* xio_dirp is fake (source filters kludge) */
412
413 /* The following macros define implementation-independent predicates on SVs. */
414
415 /*
416 =for apidoc Am|bool|SvNIOK|SV* sv
417 Returns a boolean indicating whether the SV contains a number, integer or
418 double.
419
420 =for apidoc Am|bool|SvNIOKp|SV* sv
421 Returns a boolean indicating whether the SV contains a number, integer or
422 double.  Checks the B<private> setting.  Use C<SvNIOK>.
423
424 =for apidoc Am|void|SvNIOK_off|SV* sv
425 Unsets the NV/IV status of an SV.
426
427 =for apidoc Am|bool|SvOK|SV* sv
428 Returns a boolean indicating whether the value is an SV.
429
430 =for apidoc Am|bool|SvIOKp|SV* sv
431 Returns a boolean indicating whether the SV contains an integer.  Checks
432 the B<private> setting.  Use C<SvIOK>.
433
434 =for apidoc Am|bool|SvNOKp|SV* sv
435 Returns a boolean indicating whether the SV contains a double.  Checks the
436 B<private> setting.  Use C<SvNOK>.
437
438 =for apidoc Am|bool|SvPOKp|SV* sv
439 Returns a boolean indicating whether the SV contains a character string.
440 Checks the B<private> setting.  Use C<SvPOK>.
441
442 =for apidoc Am|bool|SvIOK|SV* sv
443 Returns a boolean indicating whether the SV contains an integer.
444
445 =for apidoc Am|void|SvIOK_on|SV* sv
446 Tells an SV that it is an integer.
447
448 =for apidoc Am|void|SvIOK_off|SV* sv
449 Unsets the IV status of an SV.
450
451 =for apidoc Am|void|SvIOK_only|SV* sv
452 Tells an SV that it is an integer and disables all other OK bits.
453
454 =for apidoc Am|void|SvIOK_only_UV|SV* sv
455 Tells and SV that it is an unsigned integer and disables all other OK bits.
456
457 =for apidoc Am|void|SvIOK_UV|SV* sv
458 Returns a boolean indicating whether the SV contains an unsigned integer.
459
460 =for apidoc Am|void|SvUOK|SV* sv
461 Returns a boolean indicating whether the SV contains an unsigned integer.
462
463 =for apidoc Am|void|SvIOK_notUV|SV* sv
464 Returns a boolean indicating whether the SV contains a signed integer.
465
466 =for apidoc Am|bool|SvNOK|SV* sv
467 Returns a boolean indicating whether the SV contains a double.
468
469 =for apidoc Am|void|SvNOK_on|SV* sv
470 Tells an SV that it is a double.
471
472 =for apidoc Am|void|SvNOK_off|SV* sv
473 Unsets the NV status of an SV.
474
475 =for apidoc Am|void|SvNOK_only|SV* sv
476 Tells an SV that it is a double and disables all other OK bits.
477
478 =for apidoc Am|bool|SvPOK|SV* sv
479 Returns a boolean indicating whether the SV contains a character
480 string.
481
482 =for apidoc Am|void|SvPOK_on|SV* sv
483 Tells an SV that it is a string.
484
485 =for apidoc Am|void|SvPOK_off|SV* sv
486 Unsets the PV status of an SV.
487
488 =for apidoc Am|void|SvPOK_only|SV* sv
489 Tells an SV that it is a string and disables all other OK bits.
490 Will also turn off the UTF8 status.
491
492 =for apidoc Am|bool|SvOOK|SV* sv
493 Returns a boolean indicating whether the SvIVX is a valid offset value for
494 the SvPVX.  This hack is used internally to speed up removal of characters
495 from the beginning of a SvPV.  When SvOOK is true, then the start of the
496 allocated string buffer is really (SvPVX - SvIVX).
497
498 =for apidoc Am|bool|SvROK|SV* sv
499 Tests if the SV is an RV.
500
501 =for apidoc Am|void|SvROK_on|SV* sv
502 Tells an SV that it is an RV.
503
504 =for apidoc Am|void|SvROK_off|SV* sv
505 Unsets the RV status of an SV.
506
507 =for apidoc Am|SV*|SvRV|SV* sv
508 Dereferences an RV to return the SV.
509
510 =for apidoc Am|IV|SvIVX|SV* sv
511 Returns the raw value in the SV's IV slot, without checks or conversions.
512 Only use when you are sure SvIOK is true. See also C<SvIV()>.
513
514 =for apidoc Am|UV|SvUVX|SV* sv
515 Returns the raw value in the SV's UV slot, without checks or conversions.
516 Only use when you are sure SvIOK is true. See also C<SvUV()>.
517
518 =for apidoc Am|NV|SvNVX|SV* sv
519 Returns the raw value in the SV's NV slot, without checks or conversions.
520 Only use when you are sure SvNOK is true. See also C<SvNV()>.
521
522 =for apidoc Am|char*|SvPVX|SV* sv
523 Returns a pointer to the physical string in the SV.  The SV must contain a
524 string.
525
526 =for apidoc Am|STRLEN|SvCUR|SV* sv
527 Returns the length of the string which is in the SV.  See C<SvLEN>.
528
529 =for apidoc Am|STRLEN|SvLEN|SV* sv
530 Returns the size of the string buffer in the SV, not including any part
531 attributable to C<SvOOK>.  See C<SvCUR>.
532
533 =for apidoc Am|char*|SvEND|SV* sv
534 Returns a pointer to the last character in the string which is in the SV.
535 See C<SvCUR>.  Access the character as *(SvEND(sv)).
536
537 =for apidoc Am|HV*|SvSTASH|SV* sv
538 Returns the stash of the SV.
539
540 =for apidoc Am|void|SvCUR_set|SV* sv|STRLEN len
541 Set the length of the string which is in the SV.  See C<SvCUR>.
542
543 =cut
544 */
545
546 #define SvNIOK(sv)              (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
547 #define SvNIOKp(sv)             (SvFLAGS(sv) & (SVp_IOK|SVp_NOK))
548 #define SvNIOK_off(sv)          (SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK| \
549                                                   SVp_IOK|SVp_NOK|SVf_IVisUV))
550
551 #ifdef __GNUC__
552 #define assert_not_ROK(sv)      ({assert(!SvROK(sv) || !SvRV(sv))}),
553 #else
554 #define assert_not_ROK(sv)      
555 #endif
556
557 #define SvOK(sv)                (SvFLAGS(sv) & SVf_OK)
558 #define SvOK_off(sv)            (assert_not_ROK(sv)                     \
559                                  SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|    \
560                                                   SVf_IVisUV|SVf_UTF8), \
561                                                         SvOOK_off(sv))
562 #define SvOK_off_exc_UV(sv)     (assert_not_ROK(sv)                     \
563                                  SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|    \
564                                                   SVf_UTF8),            \
565                                                         SvOOK_off(sv))
566
567 #define SvOKp(sv)               (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
568 #define SvIOKp(sv)              (SvFLAGS(sv) & SVp_IOK)
569 #define SvIOKp_on(sv)           ((void)SvOOK_off(sv), SvFLAGS(sv) |= SVp_IOK)
570 #define SvNOKp(sv)              (SvFLAGS(sv) & SVp_NOK)
571 #define SvNOKp_on(sv)           (SvFLAGS(sv) |= SVp_NOK)
572 #define SvPOKp(sv)              (SvFLAGS(sv) & SVp_POK)
573 #define SvPOKp_on(sv)           (assert_not_ROK(sv)                     \
574                                  SvFLAGS(sv) |= SVp_POK)
575
576 #define SvIOK(sv)               (SvFLAGS(sv) & SVf_IOK)
577 #define SvIOK_on(sv)            ((void)SvOOK_off(sv), \
578                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
579 #define SvIOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV))
580 #define SvIOK_only(sv)          ((void)SvOK_off(sv), \
581                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
582 #define SvIOK_only_UV(sv)       ((void)SvOK_off_exc_UV(sv), \
583                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
584
585 #define SvIOK_UV(sv)            ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))   \
586                                  == (SVf_IOK|SVf_IVisUV))
587 #define SvUOK(sv)               SvIOK_UV(sv)
588 #define SvIOK_notUV(sv)         ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))   \
589                                  == SVf_IOK)
590
591 #define SvVOK(sv)               (SvMAGICAL(sv) && mg_find(sv,'V'))
592 #define SvIsUV(sv)              (SvFLAGS(sv) & SVf_IVisUV)
593 #define SvIsUV_on(sv)           (SvFLAGS(sv) |= SVf_IVisUV)
594 #define SvIsUV_off(sv)          (SvFLAGS(sv) &= ~SVf_IVisUV)
595
596 #define SvNOK(sv)               (SvFLAGS(sv) & SVf_NOK)
597 #define SvNOK_on(sv)            (SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
598 #define SvNOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_NOK|SVp_NOK))
599 #define SvNOK_only(sv)          ((void)SvOK_off(sv), \
600                                     SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
601
602 /*
603 =for apidoc Am|void|SvUTF8|SV* sv
604 Returns a boolean indicating whether the SV contains UTF-8 encoded data.
605
606 =for apidoc Am|void|SvUTF8_on|SV *sv
607 Turn on the UTF8 status of an SV (the data is not changed, just the flag).
608 Do not use frivolously.
609
610 =for apidoc Am|void|SvUTF8_off|SV *sv
611 Unsets the UTF8 status of an SV.
612
613 =for apidoc Am|void|SvPOK_only_UTF8|SV* sv
614 Tells an SV that it is a string and disables all other OK bits,
615 and leaves the UTF8 status as it was.
616
617 =cut
618  */
619
620 #define SvUTF8(sv)              (SvFLAGS(sv) & SVf_UTF8)
621 #define SvUTF8_on(sv)           (SvFLAGS(sv) |= (SVf_UTF8))
622 #define SvUTF8_off(sv)          (SvFLAGS(sv) &= ~(SVf_UTF8))
623
624 #define SvPOK(sv)               (SvFLAGS(sv) & SVf_POK)
625 #define SvPOK_on(sv)            (assert_not_ROK(sv)                     \
626                                  SvFLAGS(sv) |= (SVf_POK|SVp_POK))
627 #define SvPOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_POK|SVp_POK))
628 #define SvPOK_only(sv)          (assert_not_ROK(sv)                     \
629                                  SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|    \
630                                                   SVf_IVisUV|SVf_UTF8), \
631                                     SvFLAGS(sv) |= (SVf_POK|SVp_POK))
632 #define SvPOK_only_UTF8(sv)     (assert_not_ROK(sv)                     \
633                                  SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|    \
634                                                   SVf_IVisUV),          \
635                                     SvFLAGS(sv) |= (SVf_POK|SVp_POK))
636
637 #define SvOOK(sv)               (SvFLAGS(sv) & SVf_OOK)
638 #define SvOOK_on(sv)            ((void)SvIOK_off(sv), SvFLAGS(sv) |= SVf_OOK)
639 #define SvOOK_off(sv)           (SvOOK(sv) && sv_backoff(sv))
640
641 #define SvFAKE(sv)              (SvFLAGS(sv) & SVf_FAKE)
642 #define SvFAKE_on(sv)           (SvFLAGS(sv) |= SVf_FAKE)
643 #define SvFAKE_off(sv)          (SvFLAGS(sv) &= ~SVf_FAKE)
644
645 #define SvROK(sv)               (SvFLAGS(sv) & SVf_ROK)
646 #define SvROK_on(sv)            (SvFLAGS(sv) |= SVf_ROK)
647 #define SvROK_off(sv)           (SvFLAGS(sv) &= ~(SVf_ROK|SVf_AMAGIC))
648
649 #define SvMAGICAL(sv)           (SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG))
650 #define SvMAGICAL_on(sv)        (SvFLAGS(sv) |= (SVs_GMG|SVs_SMG|SVs_RMG))
651 #define SvMAGICAL_off(sv)       (SvFLAGS(sv) &= ~(SVs_GMG|SVs_SMG|SVs_RMG))
652
653 #define SvGMAGICAL(sv)          (SvFLAGS(sv) & SVs_GMG)
654 #define SvGMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_GMG)
655 #define SvGMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_GMG)
656
657 #define SvSMAGICAL(sv)          (SvFLAGS(sv) & SVs_SMG)
658 #define SvSMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_SMG)
659 #define SvSMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_SMG)
660
661 #define SvRMAGICAL(sv)          (SvFLAGS(sv) & SVs_RMG)
662 #define SvRMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_RMG)
663 #define SvRMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_RMG)
664
665 #define SvAMAGIC(sv)            (SvFLAGS(sv) & SVf_AMAGIC)
666 #define SvAMAGIC_on(sv)         (SvFLAGS(sv) |= SVf_AMAGIC)
667 #define SvAMAGIC_off(sv)        (SvFLAGS(sv) &= ~SVf_AMAGIC)
668
669 #define SvGAMAGIC(sv)           (SvFLAGS(sv) & (SVs_GMG|SVf_AMAGIC))
670
671 /*
672 #define Gv_AMG(stash) \
673         (HV_AMAGICmb(stash) && \
674          ((!HV_AMAGICbad(stash) && HV_AMAGIC(stash)) || Gv_AMupdate(stash)))
675 */
676 #define Gv_AMG(stash)           (PL_amagic_generation && Gv_AMupdate(stash))
677
678 #define SvWEAKREF(sv)           ((SvFLAGS(sv) & (SVf_ROK|SVprv_WEAKREF)) \
679                                   == (SVf_ROK|SVprv_WEAKREF))
680 #define SvWEAKREF_on(sv)        (SvFLAGS(sv) |=  (SVf_ROK|SVprv_WEAKREF))
681 #define SvWEAKREF_off(sv)       (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_WEAKREF))
682
683 #define SvTHINKFIRST(sv)        (SvFLAGS(sv) & SVf_THINKFIRST)
684
685 #define SvPADBUSY(sv)           (SvFLAGS(sv) & SVs_PADBUSY)
686
687 #define SvPADTMP(sv)            (SvFLAGS(sv) & SVs_PADTMP)
688 #define SvPADTMP_on(sv)         (SvFLAGS(sv) |= SVs_PADTMP|SVs_PADBUSY)
689 #define SvPADTMP_off(sv)        (SvFLAGS(sv) &= ~SVs_PADTMP)
690
691 #define SvPADMY(sv)             (SvFLAGS(sv) & SVs_PADMY)
692 #define SvPADMY_on(sv)          (SvFLAGS(sv) |= SVs_PADMY|SVs_PADBUSY)
693
694 #define SvTEMP(sv)              (SvFLAGS(sv) & SVs_TEMP)
695 #define SvTEMP_on(sv)           (SvFLAGS(sv) |= SVs_TEMP)
696 #define SvTEMP_off(sv)          (SvFLAGS(sv) &= ~SVs_TEMP)
697
698 #define SvOBJECT(sv)            (SvFLAGS(sv) & SVs_OBJECT)
699 #define SvOBJECT_on(sv)         (SvFLAGS(sv) |= SVs_OBJECT)
700 #define SvOBJECT_off(sv)        (SvFLAGS(sv) &= ~SVs_OBJECT)
701
702 #define SvREADONLY(sv)          (SvFLAGS(sv) & SVf_READONLY)
703 #define SvREADONLY_on(sv)       (SvFLAGS(sv) |= SVf_READONLY)
704 #define SvREADONLY_off(sv)      (SvFLAGS(sv) &= ~SVf_READONLY)
705
706 #define SvSCREAM(sv)            (SvFLAGS(sv) & SVp_SCREAM)
707 #define SvSCREAM_on(sv)         (SvFLAGS(sv) |= SVp_SCREAM)
708 #define SvSCREAM_off(sv)        (SvFLAGS(sv) &= ~SVp_SCREAM)
709
710 #define SvCOMPILED(sv)          (SvFLAGS(sv) & SVpfm_COMPILED)
711 #define SvCOMPILED_on(sv)       (SvFLAGS(sv) |= SVpfm_COMPILED)
712 #define SvCOMPILED_off(sv)      (SvFLAGS(sv) &= ~SVpfm_COMPILED)
713
714 #define SvEVALED(sv)            (SvFLAGS(sv) & SVrepl_EVAL)
715 #define SvEVALED_on(sv)         (SvFLAGS(sv) |= SVrepl_EVAL)
716 #define SvEVALED_off(sv)        (SvFLAGS(sv) &= ~SVrepl_EVAL)
717
718 #define SvTAIL(sv)              (SvFLAGS(sv) & SVpbm_TAIL)
719 #define SvTAIL_on(sv)           (SvFLAGS(sv) |= SVpbm_TAIL)
720 #define SvTAIL_off(sv)          (SvFLAGS(sv) &= ~SVpbm_TAIL)
721
722 #define SvVALID(sv)             (SvFLAGS(sv) & SVpbm_VALID)
723 #define SvVALID_on(sv)          (SvFLAGS(sv) |= SVpbm_VALID)
724 #define SvVALID_off(sv)         (SvFLAGS(sv) &= ~SVpbm_VALID)
725
726 #ifdef USE_ITHREADS
727 /* The following uses the FAKE flag to show that a regex pointer is infact
728    its own offset in the regexpad for ithreads */
729 #define SvREPADTMP(sv)          (SvFLAGS(sv) & SVf_FAKE)
730 #define SvREPADTMP_on(sv)       (SvFLAGS(sv) |= SVf_FAKE)
731 #define SvREPADTMP_off(sv)      (SvFLAGS(sv) &= ~SVf_FAKE)
732 #endif
733
734 #define SvRV(sv) ((XRV*)  SvANY(sv))->xrv_rv
735 #define SvRVx(sv) SvRV(sv)
736
737 #define SvIVX(sv) ((XPVIV*)  SvANY(sv))->xiv_iv
738 #define SvIVXx(sv) SvIVX(sv)
739 #define SvUVX(sv) ((XPVUV*)  SvANY(sv))->xuv_uv
740 #define SvUVXx(sv) SvUVX(sv)
741 #define SvNVX(sv)  ((XPVNV*)SvANY(sv))->xnv_nv
742 #define SvNVXx(sv) SvNVX(sv)
743 #define SvPVX(sv)  ((XPV*)  SvANY(sv))->xpv_pv
744 #define SvPVXx(sv) SvPVX(sv)
745 #define SvCUR(sv) ((XPV*)  SvANY(sv))->xpv_cur
746 #define SvLEN(sv) ((XPV*)  SvANY(sv))->xpv_len
747 #define SvLENx(sv) SvLEN(sv)
748 #define SvEND(sv)(((XPV*)  SvANY(sv))->xpv_pv + ((XPV*)SvANY(sv))->xpv_cur)
749 #define SvENDx(sv) ((PL_Sv = (sv)), SvEND(PL_Sv))
750 #define SvMAGIC(sv)     ((XPVMG*)  SvANY(sv))->xmg_magic
751 #define SvSTASH(sv)     ((XPVMG*)  SvANY(sv))->xmg_stash
752
753 /* Ask a scalar nicely to try to become an IV, if possible.
754    Not guaranteed to stay returning void */
755 /* Macro won't actually call sv_2iv if already IOK */
756 #define SvIV_please(sv) \
757         STMT_START {if (!SvIOKp(sv) && (SvNOK(sv) || SvPOK(sv))) \
758                 (void) SvIV(sv); } STMT_END
759 #define SvIV_set(sv, val) \
760         STMT_START { assert(SvTYPE(sv) == SVt_IV || SvTYPE(sv) >= SVt_PVIV); \
761                 (((XPVIV*)  SvANY(sv))->xiv_iv = val); } STMT_END
762 #define SvNV_set(sv, val) \
763         STMT_START { assert(SvTYPE(sv) == SVt_NV || SvTYPE(sv) >= SVt_PVNV); \
764                 (((XPVNV*)  SvANY(sv))->xnv_nv = val); } STMT_END
765 #define SvPV_set(sv, val) \
766         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
767                 (((XPV*)  SvANY(sv))->xpv_pv = val); } STMT_END
768 #define SvCUR_set(sv, val) \
769         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
770                 (((XPV*)  SvANY(sv))->xpv_cur = val); } STMT_END
771 #define SvLEN_set(sv, val) \
772         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
773                 (((XPV*)  SvANY(sv))->xpv_len = val); } STMT_END
774 #define SvEND_set(sv, val) \
775         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
776                 (((XPV*)  SvANY(sv))->xpv_cur = val - SvPVX(sv)); } STMT_END
777
778 #define BmRARE(sv)      ((XPVBM*)  SvANY(sv))->xbm_rare
779 #define BmUSEFUL(sv)    ((XPVBM*)  SvANY(sv))->xbm_useful
780 #define BmPREVIOUS(sv)  ((XPVBM*)  SvANY(sv))->xbm_previous
781
782 #define FmLINES(sv)     ((XPVFM*)  SvANY(sv))->xfm_lines
783
784 #define LvTYPE(sv)      ((XPVLV*)  SvANY(sv))->xlv_type
785 #define LvTARG(sv)      ((XPVLV*)  SvANY(sv))->xlv_targ
786 #define LvTARGOFF(sv)   ((XPVLV*)  SvANY(sv))->xlv_targoff
787 #define LvTARGLEN(sv)   ((XPVLV*)  SvANY(sv))->xlv_targlen
788
789 #define IoIFP(sv)       ((XPVIO*)  SvANY(sv))->xio_ifp
790 #define IoOFP(sv)       ((XPVIO*)  SvANY(sv))->xio_ofp
791 #define IoDIRP(sv)      ((XPVIO*)  SvANY(sv))->xio_dirp
792 #define IoANY(sv)       ((XPVIO*)  SvANY(sv))->xio_any
793 #define IoLINES(sv)     ((XPVIO*)  SvANY(sv))->xio_lines
794 #define IoPAGE(sv)      ((XPVIO*)  SvANY(sv))->xio_page
795 #define IoPAGE_LEN(sv)  ((XPVIO*)  SvANY(sv))->xio_page_len
796 #define IoLINES_LEFT(sv)((XPVIO*)  SvANY(sv))->xio_lines_left
797 #define IoTOP_NAME(sv)  ((XPVIO*)  SvANY(sv))->xio_top_name
798 #define IoTOP_GV(sv)    ((XPVIO*)  SvANY(sv))->xio_top_gv
799 #define IoFMT_NAME(sv)  ((XPVIO*)  SvANY(sv))->xio_fmt_name
800 #define IoFMT_GV(sv)    ((XPVIO*)  SvANY(sv))->xio_fmt_gv
801 #define IoBOTTOM_NAME(sv)((XPVIO*) SvANY(sv))->xio_bottom_name
802 #define IoBOTTOM_GV(sv) ((XPVIO*)  SvANY(sv))->xio_bottom_gv
803 #define IoSUBPROCESS(sv)((XPVIO*)  SvANY(sv))->xio_subprocess
804 #define IoTYPE(sv)      ((XPVIO*)  SvANY(sv))->xio_type
805 #define IoFLAGS(sv)     ((XPVIO*)  SvANY(sv))->xio_flags
806
807 /* IoTYPE(sv) is a single character telling the type of I/O connection. */
808 #define IoTYPE_RDONLY   '<'
809 #define IoTYPE_WRONLY   '>'
810 #define IoTYPE_RDWR     '+'
811 #define IoTYPE_APPEND   'a'
812 #define IoTYPE_PIPE     '|'
813 #define IoTYPE_STD      '-'     /* stdin or stdout */
814 #define IoTYPE_SOCKET   's'
815 #define IoTYPE_CLOSED   ' '
816
817 /*
818 =for apidoc Am|bool|SvTAINTED|SV* sv
819 Checks to see if an SV is tainted. Returns TRUE if it is, FALSE if
820 not.
821
822 =for apidoc Am|void|SvTAINTED_on|SV* sv
823 Marks an SV as tainted.
824
825 =for apidoc Am|void|SvTAINTED_off|SV* sv
826 Untaints an SV. Be I<very> careful with this routine, as it short-circuits
827 some of Perl's fundamental security features. XS module authors should not
828 use this function unless they fully understand all the implications of
829 unconditionally untainting the value. Untainting should be done in the
830 standard perl fashion, via a carefully crafted regexp, rather than directly
831 untainting variables.
832
833 =for apidoc Am|void|SvTAINT|SV* sv
834 Taints an SV if tainting is enabled
835
836 =cut
837 */
838
839 #define SvTAINTED(sv)     (SvMAGICAL(sv) && sv_tainted(sv))
840 #define SvTAINTED_on(sv)  STMT_START{ if(PL_tainting){sv_taint(sv);}   }STMT_END
841 #define SvTAINTED_off(sv) STMT_START{ if(PL_tainting){sv_untaint(sv);} }STMT_END
842
843 #define SvTAINT(sv)                     \
844     STMT_START {                        \
845         if (PL_tainting) {              \
846             if (PL_tainted)             \
847                 SvTAINTED_on(sv);       \
848         }                               \
849     } STMT_END
850
851 /*
852 =for apidoc Am|char*|SvPV_force|SV* sv|STRLEN len
853 Like C<SvPV> but will force the SV into containing just a string
854 (C<SvPOK_only>).  You want force if you are going to update the C<SvPVX>
855 directly.
856
857 =for apidoc Am|char*|SvPV_force_nomg|SV* sv|STRLEN len
858 Like C<SvPV> but will force the SV into containing just a string
859 (C<SvPOK_only>).  You want force if you are going to update the C<SvPVX>
860 directly. Doesn't process magic.
861
862 =for apidoc Am|char*|SvPV|SV* sv|STRLEN len
863 Returns a pointer to the string in the SV, or a stringified form of
864 the SV if the SV does not contain a string.  The SV may cache the
865 stringified version becoming C<SvPOK>.  Handles 'get' magic. See also
866 C<SvPVx> for a version which guarantees to evaluate sv only once.
867
868 =for apidoc Am|char*|SvPVx|SV* sv|STRLEN len
869 A version of C<SvPV> which guarantees to evaluate sv only once.
870
871 =for apidoc Am|char*|SvPV_nolen|SV* sv
872 Returns a pointer to the string in the SV, or a stringified form of
873 the SV if the SV does not contain a string.  The SV may cache the
874 stringified form becoming C<SvPOK>.  Handles 'get' magic.
875
876 =for apidoc Am|IV|SvIV|SV* sv
877 Coerces the given SV to an integer and returns it. See  C<SvIVx> for a
878 version which guarantees to evaluate sv only once.
879
880 =for apidoc Am|IV|SvIVx|SV* sv
881 Coerces the given SV to an integer and returns it. Guarantees to evaluate
882 sv only once. Use the more efficient C<SvIV> otherwise.
883
884 =for apidoc Am|NV|SvNV|SV* sv
885 Coerce the given SV to a double and return it. See  C<SvNVx> for a version
886 which guarantees to evaluate sv only once.
887
888 =for apidoc Am|NV|SvNVx|SV* sv
889 Coerces the given SV to a double and returns it. Guarantees to evaluate
890 sv only once. Use the more efficient C<SvNV> otherwise.
891
892 =for apidoc Am|UV|SvUV|SV* sv
893 Coerces the given SV to an unsigned integer and returns it.  See C<SvUVx>
894 for a version which guarantees to evaluate sv only once.
895
896 =for apidoc Am|UV|SvUVx|SV* sv
897 Coerces the given SV to an unsigned integer and returns it. Guarantees to
898 evaluate sv only once. Use the more efficient C<SvUV> otherwise.
899
900 =for apidoc Am|bool|SvTRUE|SV* sv
901 Returns a boolean indicating whether Perl would evaluate the SV as true or
902 false, defined or undefined.  Does not handle 'get' magic.
903
904 =for apidoc Am|char*|SvPVutf8_force|SV* sv|STRLEN len
905 Like C<SvPV_force>, but converts sv to utf8 first if necessary.
906
907 =for apidoc Am|char*|SvPVutf8|SV* sv|STRLEN len
908 Like C<SvPV>, but converts sv to utf8 first if necessary.
909
910 =for apidoc Am|char*|SvPVutf8_nolen|SV* sv
911 Like C<SvPV_nolen>, but converts sv to utf8 first if necessary.
912
913 =for apidoc Am|char*|SvPVbyte_force|SV* sv|STRLEN len
914 Like C<SvPV_force>, but converts sv to byte representation first if necessary.
915
916 =for apidoc Am|char*|SvPVbyte|SV* sv|STRLEN len
917 Like C<SvPV>, but converts sv to byte representation first if necessary.
918
919 =for apidoc Am|char*|SvPVbyte_nolen|SV* sv
920 Like C<SvPV_nolen>, but converts sv to byte representation first if necessary.
921
922 =for apidoc Am|char*|SvPVutf8x_force|SV* sv|STRLEN len
923 Like C<SvPV_force>, but converts sv to utf8 first if necessary.
924 Guarantees to evaluate sv only once; use the more efficient C<SvPVutf8_force>
925 otherwise.
926
927 =for apidoc Am|char*|SvPVutf8x|SV* sv|STRLEN len
928 Like C<SvPV>, but converts sv to utf8 first if necessary.
929 Guarantees to evaluate sv only once; use the more efficient C<SvPVutf8>
930 otherwise.
931
932 =for apidoc Am|char*|SvPVbytex_force|SV* sv|STRLEN len
933 Like C<SvPV_force>, but converts sv to byte representation first if necessary.
934 Guarantees to evaluate sv only once; use the more efficient C<SvPVbyte_force>
935 otherwise.
936
937 =for apidoc Am|char*|SvPVbytex|SV* sv|STRLEN len
938 Like C<SvPV>, but converts sv to byte representation first if necessary.
939 Guarantees to evaluate sv only once; use the more efficient C<SvPVbyte>
940 otherwise.
941
942 =for apidoc Am|bool|SvIsCOW|SV* sv
943 Returns a boolean indicating whether the SV is Copy-On-Write. (either shared
944 hash key scalars, or full Copy On Write scalars if 5.9.0 is configured for
945 COW)
946
947 =for apidoc Am|bool|SvIsCOW_shared_hash|SV* sv
948 Returns a boolean indicating whether the SV is Copy-On-Write shared hash key
949 scalar.
950
951 =cut
952 */
953
954 /* Let us hope that bitmaps for UV and IV are the same */
955 #define SvIV(sv) (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv))
956 #define SvUV(sv) (SvIOK(sv) ? SvUVX(sv) : sv_2uv(sv))
957 #define SvNV(sv) (SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv))
958
959 /* ----*/
960
961 #define SvPV(sv, lp) SvPV_flags(sv, lp, SV_GMAGIC)
962
963 #define SvPV_flags(sv, lp, flags) \
964     ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
965      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pv_flags(sv, &lp, flags))
966
967 #define SvPV_force(sv, lp) SvPV_force_flags(sv, lp, SV_GMAGIC)
968
969 #define SvPV_force_nomg(sv, lp) SvPV_force_flags(sv, lp, 0)
970
971 #define SvPV_force_flags(sv, lp, flags) \
972     ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
973     ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvn_force_flags(sv, &lp, flags))
974
975 #define SvPV_nolen(sv) \
976     ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
977      ? SvPVX(sv) : sv_2pv_nolen(sv))
978
979 #define SvPV_nomg(sv, lp) SvPV_flags(sv, lp, 0)
980
981 /* ----*/
982
983 #define SvPVutf8(sv, lp) \
984     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK|SVf_UTF8) \
985      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvutf8(sv, &lp))
986
987 #define SvPVutf8_force(sv, lp) \
988     ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == (SVf_POK|SVf_UTF8) \
989      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvutf8n_force(sv, &lp))
990
991
992 #define SvPVutf8_nolen(sv) \
993     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK|SVf_UTF8)\
994      ? SvPVX(sv) : sv_2pvutf8_nolen(sv))
995
996 /* ----*/
997
998 #define SvPVbyte(sv, lp) \
999     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK) \
1000      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pvbyte(sv, &lp))
1001
1002 #define SvPVbyte_force(sv, lp) \
1003     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST)) == (SVf_POK) \
1004      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvbyte_force(sv, &lp))
1005
1006 #define SvPVbyte_nolen(sv) \
1007     ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8)) == (SVf_POK)\
1008      ? SvPVX(sv) : sv_2pvbyte_nolen(sv))
1009
1010
1011     
1012 /* define FOOx(): idempotent versions of FOO(). If possible, use a local
1013  * var to evaluate the arg once; failing that, use a global if possible;
1014  * failing that, call a function to do the work
1015  */
1016
1017 #define SvPVx_force(sv, lp) sv_pvn_force(sv, &lp)
1018 #define SvPVutf8x_force(sv, lp) sv_pvutf8n_force(sv, &lp)
1019 #define SvPVbytex_force(sv, lp) sv_pvbyten_force(sv, &lp)
1020
1021 #if defined(__GNUC__) && !defined(__STRICT_ANSI__) && !defined(PERL_GCC_PEDANTIC)
1022
1023 #  define SvIVx(sv) ({SV *nsv = (SV*)(sv); SvIV(nsv); })
1024 #  define SvUVx(sv) ({SV *nsv = (SV*)(sv); SvUV(nsv); })
1025 #  define SvNVx(sv) ({SV *nsv = (SV*)(sv); SvNV(nsv); })
1026 #  define SvPVx(sv, lp) ({SV *nsv = (sv); SvPV(nsv, lp); })
1027 #  define SvPVutf8x(sv, lp) ({SV *nsv = (sv); SvPVutf8(nsv, lp); })
1028 #  define SvPVbytex(sv, lp) ({SV *nsv = (sv); SvPVbyte(nsv, lp); })
1029 #  define SvTRUE(sv) (                                          \
1030     !sv                                                         \
1031     ? 0                                                         \
1032     :    SvPOK(sv)                                              \
1033         ?   (({XPV *nxpv = (XPV*)SvANY(sv);                     \
1034              nxpv &&                                            \
1035              (nxpv->xpv_cur > 1 ||                              \
1036               (nxpv->xpv_cur && *nxpv->xpv_pv != '0')); })      \
1037              ? 1                                                \
1038              : 0)                                               \
1039         :                                                       \
1040             SvIOK(sv)                                           \
1041             ? SvIVX(sv) != 0                                    \
1042             :   SvNOK(sv)                                       \
1043                 ? SvNVX(sv) != 0.0                              \
1044                 : sv_2bool(sv) )
1045 #  define SvTRUEx(sv) ({SV *nsv = (sv); SvTRUE(nsv); })
1046
1047 #else /* __GNUC__ */
1048
1049 #  ifdef USE_5005THREADS
1050 #    define SvIVx(sv) sv_iv(sv)
1051 #    define SvUVx(sv) sv_uv(sv)
1052 #    define SvNVx(sv) sv_nv(sv)
1053 #    define SvPVx(sv, lp) sv_pvn(sv, &lp)
1054 #    define SvPVutf8x(sv, lp) sv_pvutf8n(sv, &lp)
1055 #    define SvPVbytex(sv, lp) sv_pvbyten(sv, &lp)
1056 #    define SvTRUE(sv) SvTRUEx(sv)
1057 #    define SvTRUEx(sv) sv_true(sv)
1058
1059 #  else /* USE_5005THREADS */
1060
1061 /* These inlined macros use globals, which will require a thread
1062  * declaration in user code, so we avoid them under threads */
1063
1064 #    define SvIVx(sv) ((PL_Sv = (sv)), SvIV(PL_Sv))
1065 #    define SvUVx(sv) ((PL_Sv = (sv)), SvUV(PL_Sv))
1066 #    define SvNVx(sv) ((PL_Sv = (sv)), SvNV(PL_Sv))
1067 #    define SvPVx(sv, lp) ((PL_Sv = (sv)), SvPV(PL_Sv, lp))
1068 #    define SvPVutf8x(sv, lp) ((PL_Sv = (sv)), SvPVutf8(PL_Sv, lp))
1069 #    define SvPVbytex(sv, lp) ((PL_Sv = (sv)), SvPVbyte(PL_Sv, lp))
1070 #    define SvTRUE(sv) (                                                \
1071     !sv                                                         \
1072     ? 0                                                         \
1073     :    SvPOK(sv)                                              \
1074         ?   ((PL_Xpv = (XPV*)SvANY(sv)) &&                      \
1075              (PL_Xpv->xpv_cur > 1 ||                            \
1076               (PL_Xpv->xpv_cur && *PL_Xpv->xpv_pv != '0'))      \
1077              ? 1                                                \
1078              : 0)                                               \
1079         :                                                       \
1080             SvIOK(sv)                                           \
1081             ? SvIVX(sv) != 0                                    \
1082             :   SvNOK(sv)                                       \
1083                 ? SvNVX(sv) != 0.0                              \
1084                 : sv_2bool(sv) )
1085 #    define SvTRUEx(sv) ((PL_Sv = (sv)), SvTRUE(PL_Sv))
1086 #  endif /* USE_5005THREADS */
1087 #endif /* __GNU__ */
1088
1089
1090 /* flag values for sv_*_flags functions */
1091 #define SV_IMMEDIATE_UNREF      1
1092 #define SV_GMAGIC               2
1093 #define SV_COW_DROP_PV          4       /* Unused in Perl 5.8.x */
1094 #define SV_UTF8_NO_ENCODING     8
1095
1096 /* all these 'functions' are now just macros */
1097
1098 #define sv_pv(sv) SvPV_nolen(sv)
1099 #define sv_pvutf8(sv) SvPVutf8_nolen(sv)
1100 #define sv_pvbyte(sv) SvPVbyte_nolen(sv)
1101
1102 #define sv_pvn_force_nomg(sv, lp) sv_pvn_force_flags(sv, lp, 0)
1103 #define sv_utf8_upgrade_nomg(sv) sv_utf8_upgrade_flags(sv, 0)
1104 #define sv_catpvn_nomg(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, 0)
1105 #define sv_setsv(dsv, ssv) sv_setsv_flags(dsv, ssv, SV_GMAGIC)
1106 #define sv_setsv_nomg(dsv, ssv) sv_setsv_flags(dsv, ssv, 0)
1107 #define sv_catsv(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC)
1108 #define sv_catsv_nomg(dsv, ssv) sv_catsv_flags(dsv, ssv, 0)
1109 #define sv_catpvn(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC)
1110 #define sv_2pv(sv, lp) sv_2pv_flags(sv, lp, SV_GMAGIC)
1111 #define sv_2pv_nomg(sv, lp) sv_2pv_flags(sv, lp, 0)
1112 #define sv_pvn_force(sv, lp) sv_pvn_force_flags(sv, lp, SV_GMAGIC)
1113 #define sv_utf8_upgrade(sv) sv_utf8_upgrade_flags(sv, SV_GMAGIC)
1114
1115 /* Should be named SvCatPVN_utf8_upgrade? */
1116 #define sv_catpvn_utf8_upgrade(dsv, sstr, slen, nsv)    \
1117         STMT_START {                                    \
1118             if (!(nsv))                                 \
1119                 nsv = sv_2mortal(newSVpvn(sstr, slen)); \
1120             else                                        \
1121                 sv_setpvn(nsv, sstr, slen);             \
1122             SvUTF8_off(nsv);                            \
1123             sv_utf8_upgrade(nsv);                       \
1124             sv_catsv(dsv, nsv); \
1125         } STMT_END
1126
1127 /*
1128 =for apidoc Am|SV*|newRV_inc|SV* sv
1129
1130 Creates an RV wrapper for an SV.  The reference count for the original SV is
1131 incremented.
1132
1133 =cut
1134 */
1135
1136 #define newRV_inc(sv)   newRV(sv)
1137
1138 /* the following macros update any magic values this sv is associated with */
1139
1140 /*
1141 =head1 Magical Functions
1142
1143 =for apidoc Am|void|SvGETMAGIC|SV* sv
1144 Invokes C<mg_get> on an SV if it has 'get' magic.  This macro evaluates its
1145 argument more than once.
1146
1147 =for apidoc Am|void|SvSETMAGIC|SV* sv
1148 Invokes C<mg_set> on an SV if it has 'set' magic.  This macro evaluates its
1149 argument more than once.
1150
1151 =for apidoc Am|void|SvSetSV|SV* dsb|SV* ssv
1152 Calls C<sv_setsv> if dsv is not the same as ssv.  May evaluate arguments
1153 more than once.
1154
1155 =for apidoc Am|void|SvSetSV_nosteal|SV* dsv|SV* ssv
1156 Calls a non-destructive version of C<sv_setsv> if dsv is not the same as
1157 ssv. May evaluate arguments more than once.
1158
1159 =for apidoc Am|void|SvSetMagicSV|SV* dsb|SV* ssv
1160 Like C<SvSetSV>, but does any set magic required afterwards.
1161
1162 =for apidoc Am|void|SvSetMagicSV_nosteal|SV* dsv|SV* ssv
1163 Like C<SvSetMagicSV>, but does any set magic required afterwards.
1164
1165 =for apidoc Am|void|SvSHARE|SV* sv
1166 Arranges for sv to be shared between threads if a suitable module
1167 has been loaded.
1168
1169 =for apidoc Am|void|SvLOCK|SV* sv
1170 Arranges for a mutual exclusion lock to be obtained on sv if a suitable module
1171 has been loaded.
1172
1173 =for apidoc Am|void|SvUNLOCK|SV* sv
1174 Releases a mutual exclusion lock on sv if a suitable module
1175 has been loaded.
1176
1177 =head1 SV Manipulation Functions
1178
1179 =for apidoc Am|char *|SvGROW|SV* sv|STRLEN len
1180 Expands the character buffer in the SV so that it has room for the
1181 indicated number of bytes (remember to reserve space for an extra trailing
1182 NUL character).  Calls C<sv_grow> to perform the expansion if necessary.
1183 Returns a pointer to the character buffer.
1184
1185 =cut
1186 */
1187
1188 #define SvSHARE(sv) CALL_FPTR(PL_sharehook)(aTHX_ sv)
1189 #define SvLOCK(sv) CALL_FPTR(PL_lockhook)(aTHX_ sv)
1190 #define SvUNLOCK(sv) CALL_FPTR(PL_unlockhook)(aTHX_ sv)
1191
1192 #define SvGETMAGIC(x) STMT_START { if (SvGMAGICAL(x)) mg_get(x); } STMT_END
1193 #define SvSETMAGIC(x) STMT_START { if (SvSMAGICAL(x)) mg_set(x); } STMT_END
1194
1195 #define SvSetSV_and(dst,src,finally) \
1196         STMT_START {                                    \
1197             if ((dst) != (src)) {                       \
1198                 sv_setsv(dst, src);                     \
1199                 finally;                                \
1200             }                                           \
1201         } STMT_END
1202 #define SvSetSV_nosteal_and(dst,src,finally) \
1203         STMT_START {                                    \
1204             if ((dst) != (src)) {                       \
1205                 U32 tMpF = SvFLAGS(src) & SVs_TEMP;     \
1206                 SvTEMP_off(src);                        \
1207                 sv_setsv(dst, src);                     \
1208                 SvFLAGS(src) |= tMpF;                   \
1209                 finally;                                \
1210             }                                           \
1211         } STMT_END
1212
1213 #define SvSetSV(dst,src) \
1214                 SvSetSV_and(dst,src,/*nothing*/;)
1215 #define SvSetSV_nosteal(dst,src) \
1216                 SvSetSV_nosteal_and(dst,src,/*nothing*/;)
1217
1218 #define SvSetMagicSV(dst,src) \
1219                 SvSetSV_and(dst,src,SvSETMAGIC(dst))
1220 #define SvSetMagicSV_nosteal(dst,src) \
1221                 SvSetSV_nosteal_and(dst,src,SvSETMAGIC(dst))
1222
1223
1224 #if !defined(SKIP_DEBUGGING)
1225 #define SvPEEK(sv) sv_peek(sv)
1226 #else
1227 #define SvPEEK(sv) ""
1228 #endif
1229
1230 #define SvIMMORTAL(sv) ((sv)==&PL_sv_undef || (sv)==&PL_sv_yes || (sv)==&PL_sv_no)
1231
1232 #define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
1233
1234 #define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
1235
1236 #define SvGROW(sv,len) (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
1237 #define Sv_Grow sv_grow
1238
1239 #define CLONEf_COPY_STACKS 1
1240 #define CLONEf_KEEP_PTR_TABLE 2
1241 #define CLONEf_CLONE_HOST 4
1242 #define CLONEf_JOIN_IN 8
1243
1244 struct clone_params {
1245   AV* stashes;
1246   UV  flags;
1247   PerlInterpreter *proto_perl;
1248 };
1249
1250 #define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) sv_force_normal(sv)