This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Jumbo Configure and large file support update.
[perl5.git] / sv.h
1 /*    sv.h
2  *
3  *    Copyright (c) 1991-1999, 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 typedef enum {
15         SVt_NULL,       /* 0 */
16         SVt_IV,         /* 1 */
17         SVt_NV,         /* 2 */
18         SVt_RV,         /* 3 */
19         SVt_PV,         /* 4 */
20         SVt_PVIV,       /* 5 */
21         SVt_PVNV,       /* 6 */
22         SVt_PVMG,       /* 7 */
23         SVt_PVBM,       /* 8 */
24         SVt_PVLV,       /* 9 */
25         SVt_PVAV,       /* 10 */
26         SVt_PVHV,       /* 11 */
27         SVt_PVCV,       /* 12 */
28         SVt_PVGV,       /* 13 */
29         SVt_PVFM,       /* 14 */
30         SVt_PVIO        /* 15 */
31 } svtype;
32
33 /* Using C's structural equivalence to help emulate C++ inheritance here... */
34
35 struct sv {
36     void*       sv_any;         /* pointer to something */
37     U32         sv_refcnt;      /* how many references to us */
38     U32         sv_flags;       /* what we are */
39 };
40
41 struct gv {
42     XPVGV*      sv_any;         /* pointer to something */
43     U32         sv_refcnt;      /* how many references to us */
44     U32         sv_flags;       /* what we are */
45 };
46
47 struct cv {
48     XPVCV*      sv_any;         /* pointer to something */
49     U32         sv_refcnt;      /* how many references to us */
50     U32         sv_flags;       /* what we are */
51 };
52
53 struct av {
54     XPVAV*      sv_any;         /* pointer to something */
55     U32         sv_refcnt;      /* how many references to us */
56     U32         sv_flags;       /* what we are */
57 };
58
59 struct hv {
60     XPVHV*      sv_any;         /* pointer to something */
61     U32         sv_refcnt;      /* how many references to us */
62     U32         sv_flags;       /* what we are */
63 };
64
65 struct io {
66     XPVIO*      sv_any;         /* pointer to something */
67     U32         sv_refcnt;      /* how many references to us */
68     U32         sv_flags;       /* what we are */
69 };
70
71 #define SvANY(sv)       (sv)->sv_any
72 #define SvFLAGS(sv)     (sv)->sv_flags
73 #define SvREFCNT(sv)    (sv)->sv_refcnt
74
75 #ifdef USE_THREADS
76
77 #  ifdef EMULATE_ATOMIC_REFCOUNTS
78 #    define ATOMIC_INC(count) STMT_START {      \
79         MUTEX_LOCK(&PL_svref_mutex);            \
80         ++count;                                \
81         MUTEX_UNLOCK(&PL_svref_mutex);          \
82      } STMT_END
83 #    define ATOMIC_DEC_AND_TEST(res,count) STMT_START { \
84         MUTEX_LOCK(&PL_svref_mutex);                    \
85         res = (--count == 0);                           \
86         MUTEX_UNLOCK(&PL_svref_mutex);                  \
87      } STMT_END
88 #  else
89 #    define ATOMIC_INC(count) atomic_inc(&count)
90 #    define ATOMIC_DEC_AND_TEST(res,count) (res = atomic_dec_and_test(&count))
91 #  endif /* EMULATE_ATOMIC_REFCOUNTS */
92 #else
93 #  define ATOMIC_INC(count) (++count)
94 #  define ATOMIC_DEC_AND_TEST(res, count) (res = (--count == 0))
95 #endif /* USE_THREADS */
96
97 #ifdef __GNUC__
98 #  define SvREFCNT_inc(sv)              \
99     ({                                  \
100         SV *nsv = (SV*)(sv);            \
101         if (nsv)                        \
102              ATOMIC_INC(SvREFCNT(nsv)); \
103         nsv;                            \
104     })
105 #else
106 #  if defined(CRIPPLED_CC) || defined(USE_THREADS)
107 #    define SvREFCNT_inc(sv) sv_newref((SV*)sv)
108 #  else
109 #    define SvREFCNT_inc(sv)    \
110         ((PL_Sv=(SV*)(sv)), (PL_Sv && ATOMIC_INC(SvREFCNT(PL_Sv))), (SV*)PL_Sv)
111 #  endif
112 #endif
113
114 #define SvREFCNT_dec(sv)        sv_free((SV*)sv)
115
116 #define SVTYPEMASK      0xff
117 #define SvTYPE(sv)      ((sv)->sv_flags & SVTYPEMASK)
118
119 #define SvUPGRADE(sv, mt) (SvTYPE(sv) >= mt || sv_upgrade(sv, mt))
120
121 #define SVs_PADBUSY     0x00000100      /* reserved for tmp or my already */
122 #define SVs_PADTMP      0x00000200      /* in use as tmp */
123 #define SVs_PADMY       0x00000400      /* in use a "my" variable */
124 #define SVs_TEMP        0x00000800      /* string is stealable? */
125 #define SVs_OBJECT      0x00001000      /* is "blessed" */
126 #define SVs_GMG         0x00002000      /* has magical get method */
127 #define SVs_SMG         0x00004000      /* has magical set method */
128 #define SVs_RMG         0x00008000      /* has random magical methods */
129
130 #define SVf_IOK         0x00010000      /* has valid public integer value */
131 #define SVf_NOK         0x00020000      /* has valid public numeric value */
132 #define SVf_POK         0x00040000      /* has valid public pointer value */
133 #define SVf_ROK         0x00080000      /* has a valid reference pointer */
134
135 #define SVf_FAKE        0x00100000      /* glob or lexical is just a copy */
136 #define SVf_OOK         0x00200000      /* has valid offset value */
137 #define SVf_BREAK       0x00400000      /* refcnt is artificially low */
138 #define SVf_READONLY    0x00800000      /* may not be modified */
139
140 #define SVf_THINKFIRST  (SVf_READONLY|SVf_ROK|SVf_FAKE)
141
142 #define SVp_IOK         0x01000000      /* has valid non-public integer value */
143 #define SVp_NOK         0x02000000      /* has valid non-public numeric value */
144 #define SVp_POK         0x04000000      /* has valid non-public pointer value */
145 #define SVp_SCREAM      0x08000000      /* has been studied? */
146
147 #define SVf_OK          (SVf_IOK|SVf_NOK|SVf_POK|SVf_ROK| \
148                          SVp_IOK|SVp_NOK|SVp_POK)
149
150 #define SVf_AMAGIC      0x10000000      /* has magical overloaded methods */
151
152 #define PRIVSHIFT 8
153
154 /* Some private flags. */
155
156 #define SVf_IVisUV      0x80000000      /* use XPVUV instead of XPVIV */
157
158 #define SVpfm_COMPILED  0x80000000      /* FORMLINE is compiled */
159
160 #define SVpbm_VALID     0x80000000
161 #define SVpbm_TAIL      0x40000000
162
163 #define SVrepl_EVAL     0x40000000      /* Replacement part of s///e */
164
165 #define SVphv_SHAREKEYS 0x20000000      /* keys live on shared string table */
166 #define SVphv_LAZYDEL   0x40000000      /* entry in xhv_eiter must be deleted */
167
168 #define SVprv_WEAKREF   0x80000000      /* Weak reference */
169
170 struct xrv {
171     SV *        xrv_rv;         /* pointer to another SV */
172 };
173
174 struct xpv {
175     char *      xpv_pv;         /* pointer to malloced string */
176     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
177     STRLEN      xpv_len;        /* allocated size */
178 };
179
180 struct xpviv {
181     char *      xpv_pv;         /* pointer to malloced string */
182     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
183     STRLEN      xpv_len;        /* allocated size */
184     IV          xiv_iv;         /* integer value or pv offset */
185 };
186
187 struct xpvuv {
188     char *      xpv_pv;         /* pointer to malloced string */
189     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
190     STRLEN      xpv_len;        /* allocated size */
191     UV          xuv_uv;         /* unsigned value or pv offset */
192 };
193
194 struct xpvnv {
195     char *      xpv_pv;         /* pointer to malloced string */
196     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
197     STRLEN      xpv_len;        /* allocated size */
198     IV          xiv_iv;         /* integer value or pv offset */
199     NV          xnv_nv;         /* numeric value, if any */
200 };
201
202 /* These structure must match the beginning of struct xpvhv in hv.h. */
203 struct xpvmg {
204     char *      xpv_pv;         /* pointer to malloced string */
205     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
206     STRLEN      xpv_len;        /* allocated size */
207     IV          xiv_iv;         /* integer value or pv offset */
208     NV          xnv_nv;         /* numeric value, if any */
209     MAGIC*      xmg_magic;      /* linked list of magicalness */
210     HV*         xmg_stash;      /* class package */
211 };
212
213 struct xpvlv {
214     char *      xpv_pv;         /* pointer to malloced string */
215     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
216     STRLEN      xpv_len;        /* allocated size */
217     IV          xiv_iv;         /* integer value or pv offset */
218     NV          xnv_nv;         /* numeric value, if any */
219     MAGIC*      xmg_magic;      /* linked list of magicalness */
220     HV*         xmg_stash;      /* class package */
221
222     STRLEN      xlv_targoff;
223     STRLEN      xlv_targlen;
224     SV*         xlv_targ;
225     char        xlv_type;
226 };
227
228 struct xpvgv {
229     char *      xpv_pv;         /* pointer to malloced string */
230     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
231     STRLEN      xpv_len;        /* allocated size */
232     IV          xiv_iv;         /* integer value or pv offset */
233     NV          xnv_nv;         /* numeric value, if any */
234     MAGIC*      xmg_magic;      /* linked list of magicalness */
235     HV*         xmg_stash;      /* class package */
236
237     GP*         xgv_gp;
238     char*       xgv_name;
239     STRLEN      xgv_namelen;
240     HV*         xgv_stash;
241     U8          xgv_flags;
242 };
243
244 struct xpvbm {
245     char *      xpv_pv;         /* pointer to malloced string */
246     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
247     STRLEN      xpv_len;        /* allocated size */
248     IV          xiv_iv;         /* integer value or pv offset */
249     NV          xnv_nv;         /* numeric value, if any */
250     MAGIC*      xmg_magic;      /* linked list of magicalness */
251     HV*         xmg_stash;      /* class package */
252
253     I32         xbm_useful;     /* is this constant pattern being useful? */
254     U16         xbm_previous;   /* how many characters in string before rare? */
255     U8          xbm_rare;       /* rarest character in string */
256 };
257
258 /* This structure much match XPVCV */
259
260 typedef U16 cv_flags_t;
261
262 struct xpvfm {
263     char *      xpv_pv;         /* pointer to malloced string */
264     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
265     STRLEN      xpv_len;        /* allocated size */
266     IV          xiv_iv;         /* integer value or pv offset */
267     NV          xnv_nv;         /* numeric value, if any */
268     MAGIC*      xmg_magic;      /* linked list of magicalness */
269     HV*         xmg_stash;      /* class package */
270
271     HV *        xcv_stash;
272     OP *        xcv_start;
273     OP *        xcv_root;
274     void      (*xcv_xsub)(pTHXo_ CV*);
275     ANY         xcv_xsubany;
276     GV *        xcv_gv;
277     GV *        xcv_filegv;
278     long        xcv_depth;              /* >= 2 indicates recursive call */
279     AV *        xcv_padlist;
280     CV *        xcv_outside;
281 #ifdef USE_THREADS
282     perl_mutex *xcv_mutexp;     /* protects xcv_owner */
283     struct perl_thread *xcv_owner;      /* current owner thread */
284 #endif /* USE_THREADS */
285     cv_flags_t  xcv_flags;
286
287     I32         xfm_lines;
288 };
289
290 struct xpvio {
291     char *      xpv_pv;         /* pointer to malloced string */
292     STRLEN      xpv_cur;        /* length of xpv_pv as a C string */
293     STRLEN      xpv_len;        /* allocated size */
294     IV          xiv_iv;         /* integer value or pv offset */
295     NV          xnv_nv;         /* numeric value, if any */
296     MAGIC*      xmg_magic;      /* linked list of magicalness */
297     HV*         xmg_stash;      /* class package */
298
299     PerlIO *    xio_ifp;        /* ifp and ofp are normally the same */
300     PerlIO *    xio_ofp;        /* but sockets need separate streams */
301     DIR *       xio_dirp;       /* for opendir, readdir, etc */
302     long        xio_lines;      /* $. */
303     long        xio_page;       /* $% */
304     long        xio_page_len;   /* $= */
305     long        xio_lines_left; /* $- */
306     char *      xio_top_name;   /* $^ */
307     GV *        xio_top_gv;     /* $^ */
308     char *      xio_fmt_name;   /* $~ */
309     GV *        xio_fmt_gv;     /* $~ */
310     char *      xio_bottom_name;/* $^B */
311     GV *        xio_bottom_gv;  /* $^B */
312     short       xio_subprocess; /* -| or |- */
313     char        xio_type;
314     char        xio_flags;
315 };
316
317 #define IOf_ARGV 1      /* this fp iterates over ARGV */
318 #define IOf_START 2     /* check for null ARGV and substitute '-' */
319 #define IOf_FLUSH 4     /* this fp wants a flush after write op */
320 #define IOf_DIDTOP 8    /* just did top of form */
321 #define IOf_UNTAINT 16  /* consider this fp (and its data) "safe" */
322 #define IOf_NOLINE  32  /* slurped a pseudo-line from empty file */
323
324 /* The following macros define implementation-independent predicates on SVs. */
325
326 #define SvNIOK(sv)              (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
327 #define SvNIOKp(sv)             (SvFLAGS(sv) & (SVp_IOK|SVp_NOK))
328 #define SvNIOK_off(sv)          (SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK| \
329                                                   SVp_IOK|SVp_NOK|SVf_IVisUV))
330
331 #define SvOK(sv)                (SvFLAGS(sv) & SVf_OK)
332 #define SvOK_off(sv)            (SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|    \
333                                                   SVf_IVisUV),          \
334                                                         SvOOK_off(sv))
335 #define SvOK_off_exc_UV(sv)     (SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC),   \
336                                                         SvOOK_off(sv))
337
338 #define SvOKp(sv)               (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
339 #define SvIOKp(sv)              (SvFLAGS(sv) & SVp_IOK)
340 #define SvIOKp_on(sv)           (SvOOK_off(sv), SvFLAGS(sv) |= SVp_IOK)
341 #define SvNOKp(sv)              (SvFLAGS(sv) & SVp_NOK)
342 #define SvNOKp_on(sv)           (SvFLAGS(sv) |= SVp_NOK)
343 #define SvPOKp(sv)              (SvFLAGS(sv) & SVp_POK)
344 #define SvPOKp_on(sv)           (SvFLAGS(sv) |= SVp_POK)
345
346 #define SvIOK(sv)               (SvFLAGS(sv) & SVf_IOK)
347 #define SvIOK_on(sv)            (SvOOK_off(sv), \
348                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
349 #define SvIOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV))
350 #define SvIOK_only(sv)          (SvOK_off(sv), \
351                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
352 #define SvIOK_only_UV(sv)       (SvOK_off_exc_UV(sv), \
353                                     SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
354  
355 #define SvIOK_UV(sv)            ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))   \
356                                  == (SVf_IOK|SVf_IVisUV))
357 #define SvIOK_notUV(sv)         ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))   \
358                                  == SVf_IOK)
359
360 #define SvIsUV(sv)              (SvFLAGS(sv) & SVf_IVisUV)
361 #define SvIsUV_on(sv)           (SvFLAGS(sv) |= SVf_IVisUV)
362 #define SvIsUV_off(sv)          (SvFLAGS(sv) &= ~SVf_IVisUV)
363
364 #define SvNOK(sv)               (SvFLAGS(sv) & SVf_NOK)
365 #define SvNOK_on(sv)            (SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
366 #define SvNOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_NOK|SVp_NOK))
367 #define SvNOK_only(sv)          (SvOK_off(sv), \
368                                     SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
369
370 #define SvPOK(sv)               (SvFLAGS(sv) & SVf_POK)
371 #define SvPOK_on(sv)            (SvFLAGS(sv) |= (SVf_POK|SVp_POK))
372 #define SvPOK_off(sv)           (SvFLAGS(sv) &= ~(SVf_POK|SVp_POK))
373 #define SvPOK_only(sv)          (SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|SVf_IVisUV),        \
374                                     SvFLAGS(sv) |= (SVf_POK|SVp_POK))
375
376 #define SvOOK(sv)               (SvFLAGS(sv) & SVf_OOK)
377 #define SvOOK_on(sv)            (SvIOK_off(sv), SvFLAGS(sv) |= SVf_OOK)
378 #define SvOOK_off(sv)           (SvOOK(sv) && sv_backoff(sv))
379
380 #define SvFAKE(sv)              (SvFLAGS(sv) & SVf_FAKE)
381 #define SvFAKE_on(sv)           (SvFLAGS(sv) |= SVf_FAKE)
382 #define SvFAKE_off(sv)          (SvFLAGS(sv) &= ~SVf_FAKE)
383
384 #define SvROK(sv)               (SvFLAGS(sv) & SVf_ROK)
385 #define SvROK_on(sv)            (SvFLAGS(sv) |= SVf_ROK)
386 #define SvROK_off(sv)           (SvFLAGS(sv) &= ~(SVf_ROK|SVf_AMAGIC))
387
388 #define SvMAGICAL(sv)           (SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG))
389 #define SvMAGICAL_on(sv)        (SvFLAGS(sv) |= (SVs_GMG|SVs_SMG|SVs_RMG))
390 #define SvMAGICAL_off(sv)       (SvFLAGS(sv) &= ~(SVs_GMG|SVs_SMG|SVs_RMG))
391
392 #define SvGMAGICAL(sv)          (SvFLAGS(sv) & SVs_GMG)
393 #define SvGMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_GMG)
394 #define SvGMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_GMG)
395
396 #define SvSMAGICAL(sv)          (SvFLAGS(sv) & SVs_SMG)
397 #define SvSMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_SMG)
398 #define SvSMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_SMG)
399
400 #define SvRMAGICAL(sv)          (SvFLAGS(sv) & SVs_RMG)
401 #define SvRMAGICAL_on(sv)       (SvFLAGS(sv) |= SVs_RMG)
402 #define SvRMAGICAL_off(sv)      (SvFLAGS(sv) &= ~SVs_RMG)
403
404 #define SvAMAGIC(sv)            (SvFLAGS(sv) & SVf_AMAGIC)
405 #define SvAMAGIC_on(sv)         (SvFLAGS(sv) |= SVf_AMAGIC)
406 #define SvAMAGIC_off(sv)        (SvFLAGS(sv) &= ~SVf_AMAGIC)
407
408 /*
409 #define Gv_AMG(stash) \
410         (HV_AMAGICmb(stash) && \
411          ((!HV_AMAGICbad(stash) && HV_AMAGIC(stash)) || Gv_AMupdate(stash)))
412 */
413 #define Gv_AMG(stash)           (PL_amagic_generation && Gv_AMupdate(stash))
414
415 #define SvWEAKREF(sv)           ((SvFLAGS(sv) & (SVf_ROK|SVprv_WEAKREF)) \
416                                   == (SVf_ROK|SVprv_WEAKREF))
417 #define SvWEAKREF_on(sv)        (SvFLAGS(sv) |=  (SVf_ROK|SVprv_WEAKREF))
418 #define SvWEAKREF_off(sv)       (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_WEAKREF))
419
420 #define SvTHINKFIRST(sv)        (SvFLAGS(sv) & SVf_THINKFIRST)
421
422 #define SvPADBUSY(sv)           (SvFLAGS(sv) & SVs_PADBUSY)
423
424 #define SvPADTMP(sv)            (SvFLAGS(sv) & SVs_PADTMP)
425 #define SvPADTMP_on(sv)         (SvFLAGS(sv) |= SVs_PADTMP|SVs_PADBUSY)
426 #define SvPADTMP_off(sv)        (SvFLAGS(sv) &= ~SVs_PADTMP)
427
428 #define SvPADMY(sv)             (SvFLAGS(sv) & SVs_PADMY)
429 #define SvPADMY_on(sv)          (SvFLAGS(sv) |= SVs_PADMY|SVs_PADBUSY)
430
431 #define SvTEMP(sv)              (SvFLAGS(sv) & SVs_TEMP)
432 #define SvTEMP_on(sv)           (SvFLAGS(sv) |= SVs_TEMP)
433 #define SvTEMP_off(sv)          (SvFLAGS(sv) &= ~SVs_TEMP)
434
435 #define SvOBJECT(sv)            (SvFLAGS(sv) & SVs_OBJECT)
436 #define SvOBJECT_on(sv)         (SvFLAGS(sv) |= SVs_OBJECT)
437 #define SvOBJECT_off(sv)        (SvFLAGS(sv) &= ~SVs_OBJECT)
438
439 #define SvREADONLY(sv)          (SvFLAGS(sv) & SVf_READONLY)
440 #define SvREADONLY_on(sv)       (SvFLAGS(sv) |= SVf_READONLY)
441 #define SvREADONLY_off(sv)      (SvFLAGS(sv) &= ~SVf_READONLY)
442
443 #define SvSCREAM(sv)            (SvFLAGS(sv) & SVp_SCREAM)
444 #define SvSCREAM_on(sv)         (SvFLAGS(sv) |= SVp_SCREAM)
445 #define SvSCREAM_off(sv)        (SvFLAGS(sv) &= ~SVp_SCREAM)
446
447 #define SvCOMPILED(sv)          (SvFLAGS(sv) & SVpfm_COMPILED)
448 #define SvCOMPILED_on(sv)       (SvFLAGS(sv) |= SVpfm_COMPILED)
449 #define SvCOMPILED_off(sv)      (SvFLAGS(sv) &= ~SVpfm_COMPILED)
450
451 #define SvEVALED(sv)            (SvFLAGS(sv) & SVrepl_EVAL)
452 #define SvEVALED_on(sv)         (SvFLAGS(sv) |= SVrepl_EVAL)
453 #define SvEVALED_off(sv)        (SvFLAGS(sv) &= ~SVrepl_EVAL)
454
455 #define SvTAIL(sv)              (SvFLAGS(sv) & SVpbm_TAIL)
456 #define SvTAIL_on(sv)           (SvFLAGS(sv) |= SVpbm_TAIL)
457 #define SvTAIL_off(sv)          (SvFLAGS(sv) &= ~SVpbm_TAIL)
458
459 #define SvVALID(sv)             (SvFLAGS(sv) & SVpbm_VALID)
460 #define SvVALID_on(sv)          (SvFLAGS(sv) |= SVpbm_VALID)
461 #define SvVALID_off(sv)         (SvFLAGS(sv) &= ~SVpbm_VALID)
462
463 #define SvRV(sv) ((XRV*)  SvANY(sv))->xrv_rv
464 #define SvRVx(sv) SvRV(sv)
465
466 #define SvIVX(sv) ((XPVIV*)  SvANY(sv))->xiv_iv
467 #define SvIVXx(sv) SvIVX(sv)
468 #define SvUVX(sv) ((XPVUV*)  SvANY(sv))->xuv_uv
469 #define SvUVXx(sv) SvUVX(sv)
470 #define SvNVX(sv)  ((XPVNV*)SvANY(sv))->xnv_nv
471 #define SvNVXx(sv) SvNVX(sv)
472 #define SvPVX(sv)  ((XPV*)  SvANY(sv))->xpv_pv
473 #define SvPVXx(sv) SvPVX(sv)
474 #define SvCUR(sv) ((XPV*)  SvANY(sv))->xpv_cur
475 #define SvLEN(sv) ((XPV*)  SvANY(sv))->xpv_len
476 #define SvLENx(sv) SvLEN(sv)
477 #define SvEND(sv)(((XPV*)  SvANY(sv))->xpv_pv + ((XPV*)SvANY(sv))->xpv_cur)
478 #define SvENDx(sv) ((PL_Sv = (sv)), SvEND(PL_Sv))
479 #define SvMAGIC(sv)     ((XPVMG*)  SvANY(sv))->xmg_magic
480 #define SvSTASH(sv)     ((XPVMG*)  SvANY(sv))->xmg_stash
481
482 #define SvIV_set(sv, val) \
483         STMT_START { assert(SvTYPE(sv) == SVt_IV || SvTYPE(sv) >= SVt_PVIV); \
484                 (((XPVIV*)  SvANY(sv))->xiv_iv = val); } STMT_END
485 #define SvNV_set(sv, val) \
486         STMT_START { assert(SvTYPE(sv) == SVt_NV || SvTYPE(sv) >= SVt_PVNV); \
487                 (((XPVNV*)  SvANY(sv))->xnv_nv = val); } STMT_END
488 #define SvPV_set(sv, val) \
489         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
490                 (((XPV*)  SvANY(sv))->xpv_pv = val); } STMT_END
491 #define SvCUR_set(sv, val) \
492         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
493                 (((XPV*)  SvANY(sv))->xpv_cur = val); } STMT_END
494 #define SvLEN_set(sv, val) \
495         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
496                 (((XPV*)  SvANY(sv))->xpv_len = val); } STMT_END
497 #define SvEND_set(sv, val) \
498         STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
499                 (((XPV*)  SvANY(sv))->xpv_cur = val - SvPVX(sv)); } STMT_END
500
501 #define BmRARE(sv)      ((XPVBM*)  SvANY(sv))->xbm_rare
502 #define BmUSEFUL(sv)    ((XPVBM*)  SvANY(sv))->xbm_useful
503 #define BmPREVIOUS(sv)  ((XPVBM*)  SvANY(sv))->xbm_previous
504
505 #define FmLINES(sv)     ((XPVFM*)  SvANY(sv))->xfm_lines
506
507 #define LvTYPE(sv)      ((XPVLV*)  SvANY(sv))->xlv_type
508 #define LvTARG(sv)      ((XPVLV*)  SvANY(sv))->xlv_targ
509 #define LvTARGOFF(sv)   ((XPVLV*)  SvANY(sv))->xlv_targoff
510 #define LvTARGLEN(sv)   ((XPVLV*)  SvANY(sv))->xlv_targlen
511
512 #define IoIFP(sv)       ((XPVIO*)  SvANY(sv))->xio_ifp
513 #define IoOFP(sv)       ((XPVIO*)  SvANY(sv))->xio_ofp
514 #define IoDIRP(sv)      ((XPVIO*)  SvANY(sv))->xio_dirp
515 #define IoLINES(sv)     ((XPVIO*)  SvANY(sv))->xio_lines
516 #define IoPAGE(sv)      ((XPVIO*)  SvANY(sv))->xio_page
517 #define IoPAGE_LEN(sv)  ((XPVIO*)  SvANY(sv))->xio_page_len
518 #define IoLINES_LEFT(sv)((XPVIO*)  SvANY(sv))->xio_lines_left
519 #define IoTOP_NAME(sv)  ((XPVIO*)  SvANY(sv))->xio_top_name
520 #define IoTOP_GV(sv)    ((XPVIO*)  SvANY(sv))->xio_top_gv
521 #define IoFMT_NAME(sv)  ((XPVIO*)  SvANY(sv))->xio_fmt_name
522 #define IoFMT_GV(sv)    ((XPVIO*)  SvANY(sv))->xio_fmt_gv
523 #define IoBOTTOM_NAME(sv)((XPVIO*) SvANY(sv))->xio_bottom_name
524 #define IoBOTTOM_GV(sv) ((XPVIO*)  SvANY(sv))->xio_bottom_gv
525 #define IoSUBPROCESS(sv)((XPVIO*)  SvANY(sv))->xio_subprocess
526 #define IoTYPE(sv)      ((XPVIO*)  SvANY(sv))->xio_type
527 #define IoFLAGS(sv)     ((XPVIO*)  SvANY(sv))->xio_flags
528
529 #define SvTAINTED(sv)     (SvMAGICAL(sv) && sv_tainted(sv))
530 #define SvTAINTED_on(sv)  STMT_START{ if(PL_tainting){sv_taint(sv);}   }STMT_END
531 #define SvTAINTED_off(sv) STMT_START{ if(PL_tainting){sv_untaint(sv);} }STMT_END
532
533 #define SvTAINT(sv)                     \
534     STMT_START {                        \
535         if (PL_tainting) {              \
536             dTHR;                       \
537             if (PL_tainted)             \
538                 SvTAINTED_on(sv);       \
539         }                               \
540     } STMT_END
541
542 #define SvPV_force(sv, lp) sv_pvn_force(sv, &lp)
543 #define SvPV(sv, lp) sv_pvn(sv, &lp)
544 #define SvPV_nolen(sv) sv_pv(sv)
545 #define SvIVx(sv) sv_iv(sv)
546 #define SvUVx(sv) sv_uv(sv)
547 #define SvNVx(sv) sv_nv(sv)
548 #define SvPVx(sv, lp) sv_pvn(sv, &lp)
549 #define SvPVx_force(sv, lp) sv_pvn_force(sv, &lp)
550 #define SvTRUEx(sv) sv_true(sv)
551
552 #define SvIV(sv) SvIVx(sv)
553 #define SvNV(sv) SvNVx(sv)
554 #define SvUV(sv) SvUVx(sv)
555 #define SvTRUE(sv) SvTRUEx(sv)
556
557 #ifndef CRIPPLED_CC
558 /* redefine some things to more efficient inlined versions */
559
560 /* Let us hope that bitmaps for UV and IV are the same */
561 #undef SvIV
562 #define SvIV(sv) (SvIOK(sv) ? SvIVX(sv) : sv_2iv(sv))
563
564 #undef SvUV
565 #define SvUV(sv) (SvIOK(sv) ? SvUVX(sv) : sv_2uv(sv))
566
567 #undef SvNV
568 #define SvNV(sv) (SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv))
569
570 #undef SvPV
571 #define SvPV(sv, lp) \
572     (SvPOK(sv) ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pv(sv, &lp))
573
574 #undef SvPV_force
575 #define SvPV_force(sv, lp) \
576     ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
577      ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvn_force(sv, &lp))
578
579 #undef SvPV_nolen
580 #define SvPV_nolen(sv) \
581     (SvPOK(sv) ? SvPVX(sv) : sv_2pv_nolen(sv))
582
583 #ifdef __GNUC__
584 #  undef SvIVx
585 #  undef SvUVx
586 #  undef SvNVx
587 #  undef SvPVx
588 #  undef SvTRUE
589 #  undef SvTRUEx
590 #  define SvIVx(sv) ({SV *nsv = (SV*)(sv); SvIV(nsv); })
591 #  define SvUVx(sv) ({SV *nsv = (SV*)(sv); SvUV(nsv); })
592 #  define SvNVx(sv) ({SV *nsv = (SV*)(sv); SvNV(nsv); })
593 #  define SvPVx(sv, lp) ({SV *nsv = (sv); SvPV(nsv, lp); })
594 #  define SvTRUE(sv) (                                          \
595     !sv                                                         \
596     ? 0                                                         \
597     :    SvPOK(sv)                                              \
598         ?   (({XPV *nxpv = (XPV*)SvANY(sv);                     \
599              nxpv &&                                            \
600              (*nxpv->xpv_pv > '0' ||                            \
601               nxpv->xpv_cur > 1 ||                              \
602               (nxpv->xpv_cur && *nxpv->xpv_pv != '0')); })      \
603              ? 1                                                \
604              : 0)                                               \
605         :                                                       \
606             SvIOK(sv)                                           \
607             ? SvIVX(sv) != 0                                    \
608             :   SvNOK(sv)                                       \
609                 ? SvNVX(sv) != 0.0                              \
610                 : sv_2bool(sv) )
611 #  define SvTRUEx(sv) ({SV *nsv = (sv); SvTRUE(nsv); })
612 #else /* __GNUC__ */
613 #ifndef USE_THREADS
614 /* These inlined macros use globals, which will require a thread
615  * declaration in user code, so we avoid them under threads */
616
617 #  undef SvIVx
618 #  undef SvUVx
619 #  undef SvNVx
620 #  undef SvPVx
621 #  undef SvTRUE
622 #  undef SvTRUEx
623 #  define SvIVx(sv) ((PL_Sv = (sv)), SvIV(PL_Sv))
624 #  define SvUVx(sv) ((PL_Sv = (sv)), SvUV(PL_Sv))
625 #  define SvNVx(sv) ((PL_Sv = (sv)), SvNV(PL_Sv))
626 #  define SvPVx(sv, lp) ((PL_Sv = (sv)), SvPV(PL_Sv, lp))
627 #  define SvTRUE(sv) (                                          \
628     !sv                                                         \
629     ? 0                                                         \
630     :    SvPOK(sv)                                              \
631         ?   ((PL_Xpv = (XPV*)SvANY(sv)) &&                      \
632              (*PL_Xpv->xpv_pv > '0' ||                          \
633               PL_Xpv->xpv_cur > 1 ||                            \
634               (PL_Xpv->xpv_cur && *PL_Xpv->xpv_pv != '0'))      \
635              ? 1                                                \
636              : 0)                                               \
637         :                                                       \
638             SvIOK(sv)                                           \
639             ? SvIVX(sv) != 0                                    \
640             :   SvNOK(sv)                                       \
641                 ? SvNVX(sv) != 0.0                              \
642                 : sv_2bool(sv) )
643 #  define SvTRUEx(sv) ((PL_Sv = (sv)), SvTRUE(PL_Sv))
644 #endif /* !USE_THREADS */
645 #endif /* !__GNU__ */
646 #endif /* !CRIPPLED_CC */
647
648 #define newRV_inc(sv)   newRV(sv)
649
650 /* the following macros update any magic values this sv is associated with */
651
652 #define SvGETMAGIC(x) STMT_START { if (SvGMAGICAL(x)) mg_get(x); } STMT_END
653 #define SvSETMAGIC(x) STMT_START { if (SvSMAGICAL(x)) mg_set(x); } STMT_END
654
655 #define SvSetSV_and(dst,src,finally) \
656         STMT_START {                                    \
657             if ((dst) != (src)) {                       \
658                 sv_setsv(dst, src);                     \
659                 finally;                                \
660             }                                           \
661         } STMT_END
662 #define SvSetSV_nosteal_and(dst,src,finally) \
663         STMT_START {                                    \
664             if ((dst) != (src)) {                       \
665                 U32 tMpF = SvFLAGS(src) & SVs_TEMP;     \
666                 SvTEMP_off(src);                        \
667                 sv_setsv(dst, src);                     \
668                 SvFLAGS(src) |= tMpF;                   \
669                 finally;                                \
670             }                                           \
671         } STMT_END
672
673 #define SvSetSV(dst,src) \
674                 SvSetSV_and(dst,src,/*nothing*/;)
675 #define SvSetSV_nosteal(dst,src) \
676                 SvSetSV_nosteal_and(dst,src,/*nothing*/;)
677
678 #define SvSetMagicSV(dst,src) \
679                 SvSetSV_and(dst,src,SvSETMAGIC(dst))
680 #define SvSetMagicSV_nosteal(dst,src) \
681                 SvSetSV_nosteal_and(dst,src,SvSETMAGIC(dst))
682
683 #ifdef DEBUGGING
684 #define SvPEEK(sv) sv_peek(sv)
685 #else
686 #define SvPEEK(sv) ""
687 #endif
688
689 #define SvIMMORTAL(sv) ((sv)==&PL_sv_undef || (sv)==&PL_sv_yes || (sv)==&PL_sv_no)
690
691 #define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
692
693 #define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
694
695 #if !defined(DOSISH) || defined(WIN32) || defined(OS2)
696 #  define SvGROW(sv,len) (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
697 #  define Sv_Grow sv_grow
698 #else
699     /* extra parentheses intentionally NOT placed around "len"! */
700 #  define SvGROW(sv,len) ((SvLEN(sv) < (unsigned long)len) \
701                 ? sv_grow(sv,(unsigned long)len) : SvPVX(sv))
702 #  define Sv_Grow(sv,len) sv_grow(sv,(unsigned long)(len))
703 #endif /* DOSISH */