3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 #undef sv_flags /* Convex has this in <signal.h> for sigvec() */
16 =for apidoc_section $SV_flags
18 =for apidoc Ay||svtype
19 An enum of flags for Perl types. These are found in the file F<sv.h>
20 in the C<svtype> enum. Test these flags with the C<SvTYPE> macro.
43 These are most easily explained from the bottom up.
45 C<SVt_PVOBJ> is for object instances of the new `use feature 'class'` kind.
46 C<SVt_PVIO> is for I/O objects, C<SVt_PVFM> for formats, C<SVt_PVCV> for
47 subroutines, C<SVt_PVHV> for hashes and C<SVt_PVAV> for arrays.
49 All the others are scalar types, that is, things that can be bound to a
50 C<$> variable. For these, the internal types are mostly orthogonal to
51 types in the Perl language.
53 Hence, checking C<< SvTYPE(sv) < SVt_PVAV >> is the best way to see whether
54 something is a scalar.
56 C<SVt_PVGV> represents a typeglob. If C<!SvFAKE(sv)>, then it is a real,
57 incoercible typeglob. If C<SvFAKE(sv)>, then it is a scalar to which a
58 typeglob has been assigned. Assigning to it again will stop it from being
59 a typeglob. C<SVt_PVLV> represents a scalar that delegates to another scalar
60 behind the scenes. It is used, e.g., for the return value of C<substr> and
61 for tied hash and array elements. It can hold any scalar value, including
62 a typeglob. C<SVt_REGEXP> is for regular
63 expressions. C<SVt_INVLIST> is for Perl
64 core internal use only.
66 C<SVt_PVMG> represents a "normal" scalar (not a typeglob, regular expression,
67 or delegate). Since most scalars do not need all the internal fields of a
68 PVMG, we save memory by allocating smaller structs when possible. All the
69 other types are just simpler forms of C<SVt_PVMG>, with fewer internal fields.
70 C<SVt_NULL> can only hold undef. C<SVt_IV> can hold undef, an integer, or a
71 reference. (C<SVt_RV> is an alias for C<SVt_IV>, which exists for backward
72 compatibility.) C<SVt_NV> can hold undef or a double. (In builds that support
73 headless NVs, these could also hold a reference via a suitable offset, in the
74 same way that SVt_IV does, but this is not currently supported and seems to
75 be a rare use case.) C<SVt_PV> can hold C<undef>, a string, or a reference.
76 C<SVt_PVIV> is a superset of C<SVt_PV> and C<SVt_IV>. C<SVt_PVNV> is a
77 superset of C<SVt_PV> and C<SVt_NV>. C<SVt_PVMG> can hold anything C<SVt_PVNV>
78 can hold, but it may also be blessed or magical.
80 =for apidoc AmnU||SVt_NULL
81 Type flag for scalars. See L</svtype>.
83 =for apidoc AmnU||SVt_IV
84 Type flag for scalars. See L</svtype>.
86 =for apidoc AmnU||SVt_NV
87 Type flag for scalars. See L</svtype>.
89 =for apidoc AmnU||SVt_PV
90 Type flag for scalars. See L</svtype>.
92 =for apidoc AmnU||SVt_PVIV
93 Type flag for scalars. See L</svtype>.
95 =for apidoc AmnU||SVt_PVNV
96 Type flag for scalars. See L</svtype>.
98 =for apidoc AmnU||SVt_PVMG
99 Type flag for scalars. See L</svtype>.
101 =for apidoc CmnU||SVt_INVLIST
102 Type flag for scalars. See L<perlapi/svtype>.
104 =for apidoc AmnU||SVt_REGEXP
105 Type flag for regular expressions. See L</svtype>.
107 =for apidoc AmnU||SVt_PVGV
108 Type flag for typeglobs. See L</svtype>.
110 =for apidoc AmnU||SVt_PVLV
111 Type flag for scalars. See L</svtype>.
113 =for apidoc AmnU||SVt_PVAV
114 Type flag for arrays. See L</svtype>.
116 =for apidoc AmnU||SVt_PVHV
117 Type flag for hashes. See L</svtype>.
119 =for apidoc AmnU||SVt_PVCV
120 Type flag for subroutines. See L</svtype>.
122 =for apidoc AmnU||SVt_PVFM
123 Type flag for formats. See L</svtype>.
125 =for apidoc AmnU||SVt_PVIO
126 Type flag for I/O objects. See L</svtype>.
128 =for apidoc AmnUx||SVt_PVOBJ
129 Type flag for object instances. See L</svtype>.
133 These are ordered so that the simpler types have a lower value; SvUPGRADE
134 doesn't allow you to upgrade from a higher numbered type to a lower numbered
135 one; also there is code that assumes that anything that has as a PV component
136 has a type numbered >= SVt_PV.
142 /* BIND was here, before INVLIST replaced it. */
145 /* RV was here, before it was merged with IV. */
147 SVt_INVLIST, /* 4, implemented as a PV */
152 /* PVBM was here, before BIND replaced it. */
161 /* 17-31: Unused, though one should be reserved for a
162 * freed sv, if the other 3 bits below the flags ones
164 SVt_LAST /* keep last in enum. used to size arrays */
167 /* *** any alterations to the SV types above need to be reflected in
168 * SVt_MASK and the various PL_valid_types_* tables. As of this writing those
169 * tables are in perl.h. There are also two affected names tables in dump.c,
170 * one in B.xs, and 'bodies_by_type[]' in sv_inline.h.
172 * The bits that match 0xe0 are CURRENTLY UNUSED
173 * The bits above that are for flags, like SVf_IOK */
175 #define SVt_MASK 0x1f /* smallest bitmask that covers all types */
178 /* Fast Boyer Moore tables are now stored in magic attached to PVMGs */
179 # define SVt_PVBM SVt_PVMG
180 /* Anything wanting to create a reference from clean should ensure that it has
181 a scalar of type SVt_IV now: */
182 # define SVt_RV SVt_IV
185 /* The array of arena roots for SV bodies is indexed by SvTYPE. SVt_NULL doesn't
186 * use a body, so that arena root is re-used for HEs. SVt_IV also doesn't, so
187 * that arena root is used for HVs with struct xpvhv_aux. */
189 #if defined(PERL_IN_HV_C) || defined(PERL_IN_XS_APITEST)
190 # define HE_ARENA_ROOT_IX SVt_NULL
192 #if defined(PERL_IN_HV_C) || defined(PERL_IN_SV_C)
193 # define HVAUX_ARENA_ROOT_IX SVt_IV
196 # define SVt_FIRST SVt_NULL /* the type of SV that new_SV() in sv.c returns */
199 #define PERL_ARENA_ROOTS_SIZE (SVt_LAST)
201 /* typedefs to eliminate some typing */
202 typedef struct he HE;
203 typedef struct hek HEK;
205 /* Using C's structural equivalence to help emulate C++ inheritance here... */
207 /* start with 2 sv-head building blocks */
208 #define _SV_HEAD(ptrtype) \
209 ptrtype sv_any; /* pointer to body */ \
210 U32 sv_refcnt; /* how many references to us */ \
211 U32 sv_flags /* what we are */
214 # define _NV_BODYLESS_UNION NV svu_nv;
216 # define _NV_BODYLESS_UNION
219 #define _SV_HEAD_UNION \
221 char* svu_pv; /* pointer to malloced string */ \
225 SV* svu_rv; /* pointer to another SV */ \
233 #ifdef DEBUG_LEAKING_SCALARS
234 #define _SV_HEAD_DEBUG ;\
235 PERL_BITFIELD32 sv_debug_optype:9; /* the type of OP that allocated us */ \
236 PERL_BITFIELD32 sv_debug_inpad:1; /* was allocated in a pad for an OP */ \
237 PERL_BITFIELD32 sv_debug_line:16; /* the line where we were allocated */ \
238 UV sv_debug_serial; /* serial number of sv allocation */ \
239 char * sv_debug_file; /* the file where we were allocated */ \
240 SV * sv_debug_parent /* what we were cloned from (ithreads)*/
242 #define _SV_HEAD_DEBUG
245 struct STRUCT_SV { /* struct sv { */
251 _SV_HEAD(XPVGV*); /* pointer to xpvgv body */
256 _SV_HEAD(XPVCV*); /* pointer to xpvcv body */
261 _SV_HEAD(XPVAV*); /* pointer to xpvav body */
266 _SV_HEAD(XPVHV*); /* pointer to xpvhv body */
271 _SV_HEAD(XPVIO*); /* pointer to xpvio body */
276 _SV_HEAD(struct regexp*); /* pointer to regexp body */
281 _SV_HEAD(XINVLIST*); /* pointer to xpvinvlist body */
286 _SV_HEAD(XPVOBJ*); /* pointer to xobject body */
291 #undef _SV_HEAD_UNION /* ensure no pollution */
294 =for apidoc_section $SV
296 =for apidoc Am|U32|SvREFCNT|SV* sv
297 Returns the value of the object's reference count. Exposed
298 to perl code via Internals::SvREFCNT().
300 =for apidoc SvREFCNT_inc
301 =for apidoc_item SvREFCNT_inc_NN
302 =for apidoc_item |SV* |SvREFCNT_inc_simple|SV* sv
303 =for apidoc_item |SV* |SvREFCNT_inc_simple_NN|SV* sv
304 =for apidoc_item |void|SvREFCNT_inc_simple_void|SV* sv
305 =for apidoc_item |void|SvREFCNT_inc_simple_void_NN|SV* sv
306 =for apidoc_item SvREFCNT_inc_void
307 =for apidoc_item |void|SvREFCNT_inc_void_NN|SV* sv
309 These all increment the reference count of the given SV.
310 The ones without C<void> in their names return the SV.
312 C<SvREFCNT_inc> is the base operation; the rest are optimizations if various
313 input constraints are known to be true; hence, all can be replaced with
316 C<SvREFCNT_inc_NN> can only be used if you know C<sv> is not C<NULL>. Since we
317 don't have to check the NULLness, it's faster and smaller.
319 C<SvREFCNT_inc_void> can only be used if you don't need the
320 return value. The macro doesn't need to return a meaningful value.
322 C<SvREFCNT_inc_void_NN> can only be used if you both don't need the return
323 value, and you know that C<sv> is not C<NULL>. The macro doesn't need to
324 return a meaningful value, or check for NULLness, so it's smaller and faster.
326 C<SvREFCNT_inc_simple> can only be used with expressions without side
327 effects. Since we don't have to store a temporary value, it's faster.
329 C<SvREFCNT_inc_simple_NN> can only be used with expressions without side
330 effects and you know C<sv> is not C<NULL>. Since we don't have to store a
331 temporary value, nor check for NULLness, it's faster and smaller.
333 C<SvREFCNT_inc_simple_void> can only be used with expressions without side
334 effects and you don't need the return value.
336 C<SvREFCNT_inc_simple_void_NN> can only be used with expressions without side
337 effects, you don't need the return value, and you know C<sv> is not C<NULL>.
339 =for apidoc SvREFCNT_dec
340 =for apidoc_item SvREFCNT_dec_set_NULL
341 =for apidoc_item SvREFCNT_dec_ret_NULL
342 =for apidoc_item SvREFCNT_dec_NN
344 These decrement the reference count of the given SV.
346 C<SvREFCNT_dec_NN> may only be used when C<sv> is known to not be C<NULL>.
348 The function C<SvREFCNT_dec_ret_NULL()> is identical to the
349 C<SvREFCNT_dec()> except it returns a NULL C<SV *>. It is used by
350 C<SvREFCNT_dec_set_NULL()> which is a macro which will, when passed a
351 non-NULL argument, decrement the reference count of its argument and
352 then set it to NULL. You can replace code of the following form:
361 SvREFCNT_dec_set_NULL(sv);
363 =for apidoc Am|svtype|SvTYPE|SV* sv
364 Returns the type of the SV. See C<L</svtype>>.
366 =for apidoc Am|void|SvUPGRADE|SV* sv|svtype type
367 Used to upgrade an SV to a more complex form. Uses C<sv_upgrade> to
368 perform the upgrade if necessary. See C<L</svtype>>.
373 #define SvANY(sv) (sv)->sv_any
374 #define SvFLAGS(sv) (sv)->sv_flags
375 #define SvREFCNT(sv) (sv)->sv_refcnt
377 #define SvREFCNT_inc(sv) Perl_SvREFCNT_inc(MUTABLE_SV(sv))
378 #define SvREFCNT_inc_simple(sv) SvREFCNT_inc(sv)
379 #define SvREFCNT_inc_NN(sv) Perl_SvREFCNT_inc_NN(MUTABLE_SV(sv))
380 #define SvREFCNT_inc_void(sv) Perl_SvREFCNT_inc_void(MUTABLE_SV(sv))
382 /* These guys don't need the curly blocks */
383 #define SvREFCNT_inc_simple_void(sv) \
385 SV * sv_ = MUTABLE_SV(sv); \
390 #define SvREFCNT_inc_simple_NN(sv) (++(SvREFCNT(sv)),MUTABLE_SV(sv))
391 #define SvREFCNT_inc_void_NN(sv) (void)(++SvREFCNT(MUTABLE_SV(sv)))
392 #define SvREFCNT_inc_simple_void_NN(sv) (void)(++SvREFCNT(MUTABLE_SV(sv)))
394 #define SvREFCNT_dec(sv) Perl_SvREFCNT_dec(aTHX_ MUTABLE_SV(sv))
395 #define SvREFCNT_dec_set_NULL(sv) \
397 sv = Perl_SvREFCNT_dec_ret_NULL(aTHX_ MUTABLE_SV(sv)); \
399 #define SvREFCNT_dec_NN(sv) Perl_SvREFCNT_dec_NN(aTHX_ MUTABLE_SV(sv))
401 #define SVTYPEMASK 0xff
402 #define SvTYPE(sv) ((svtype)((sv)->sv_flags & SVTYPEMASK))
404 /* Sadly there are some parts of the core that have pointers to already-freed
405 SV heads, and rely on being able to tell that they are now free. So mark
406 them all by using a consistent macro. */
407 #define SvIS_FREED(sv) UNLIKELY(((sv)->sv_flags == SVTYPEMASK))
409 /* this is defined in this peculiar way to avoid compiler warnings.
410 * See the <20121213131428.GD1842@iabyn.com> thread in p5p */
411 #define SvUPGRADE(sv, mt) \
412 ((void)(SvTYPE(sv) >= (mt) || (sv_upgrade(sv, mt),1)))
414 #define SVf_IOK 0x00000100 /* has valid public integer value */
415 #define SVf_NOK 0x00000200 /* has valid public numeric value */
416 #define SVf_POK 0x00000400 /* has valid public pointer value */
417 #define SVf_ROK 0x00000800 /* has a valid reference pointer */
419 #define SVp_IOK 0x00001000 /* has valid non-public integer value */
420 #define SVp_NOK 0x00002000 /* has valid non-public numeric value */
421 #define SVp_POK 0x00004000 /* has valid non-public pointer value */
422 #define SVp_SCREAM 0x00008000 /* currently unused on plain scalars */
423 #define SVphv_CLONEABLE SVp_SCREAM /* PVHV (stashes) clone its objects */
424 #define SVpgv_GP SVp_SCREAM /* GV has a valid GP */
425 #define SVprv_PCS_IMPORTED SVp_SCREAM /* RV is a proxy for a constant
426 subroutine in another package. Set the
427 GvIMPORTED_CV_on() if it needs to be
428 expanded to a real GV */
430 /* SVf_PROTECT is what SVf_READONLY should have been: i.e. modifying
431 * this SV is completely illegal. However, SVf_READONLY (via
432 * Internals::SvREADONLY()) has come to be seen as a flag that can be
433 * temporarily set and unset by the user to indicate e.g. whether a hash
434 * is "locked". Now, Hash::Util et al only set SVf_READONLY, while core
435 * sets both (SVf_READONLY|SVf_PROTECT) to indicate both to core and user
436 * code that this SV should not be messed with.
438 #define SVf_PROTECT 0x00010000 /* very read-only */
439 #define SVs_PADTMP 0x00020000 /* in use as tmp */
440 #define SVs_PADSTALE 0x00040000 /* lexical has gone out of scope;
441 only used when !PADTMP */
442 #define SVs_TEMP 0x00080000 /* mortal (implies string is stealable) */
443 #define SVs_OBJECT 0x00100000 /* is "blessed" */
444 #define SVs_GMG 0x00200000 /* has magical get method */
445 #define SVs_SMG 0x00400000 /* has magical set method */
446 #define SVs_RMG 0x00800000 /* has random magical methods */
448 #define SVf_FAKE 0x01000000 /* 0: glob is just a copy
449 1: SV head arena wasn't malloc()ed
450 2: For PVCV, whether CvUNIQUE(cv)
451 refers to an eval or once only
452 [CvEVAL(cv), CvSPECIAL(cv)]
453 3: HV: informally reserved by DAPM
455 4: Together with other flags (or
456 lack thereof) indicates a regex,
457 including PVLV-as-regex. See
460 #define SVf_OOK 0x02000000 /* has valid offset value */
461 #define SVphv_HasAUX SVf_OOK /* PVHV has an additional hv_aux struct */
462 #define SVf_BREAK 0x04000000 /* refcnt is artificially low - used by
463 SVs in final arena cleanup.
464 Set in S_regtry on PL_reg_curpm, so that
465 perl_destruct will skip it.
466 Used for mark and sweep by OP_AASSIGN
468 #define SVf_READONLY 0x08000000 /* may not be modified */
473 #define SVf_THINKFIRST (SVf_READONLY|SVf_PROTECT|SVf_ROK|SVf_FAKE \
476 #define SVf_OK (SVf_IOK|SVf_NOK|SVf_POK|SVf_ROK| \
477 SVp_IOK|SVp_NOK|SVp_POK|SVpgv_GP)
479 #define PRIVSHIFT 4 /* (SVp_?OK >> PRIVSHIFT) == SVf_?OK */
481 /* SVf_AMAGIC means that the stash *may* have overload methods. It's
482 * set each time a function is compiled into a stash, and is reset by the
483 * overload code when called for the first time and finds that there are
484 * no overload methods. Note that this used to be set on the object; but
485 * is now only set on stashes.
487 #define SVf_AMAGIC 0x10000000 /* has magical overloaded methods */
488 #define SVf_IsCOW 0x10000000 /* copy on write (shared hash key if
491 /* Ensure this value does not clash with the GV_ADD* flags in gv.h, or the
492 CV_CKPROTO_* flags in op.c, or the padadd_* flags in pad.h: */
493 #define SVf_UTF8 0x20000000 /* SvPV is UTF-8 encoded
494 This is also set on RVs whose overloaded
495 stringification is UTF-8. This might
496 only happen as a side effect of SvPV() */
498 #define SVphv_SHAREKEYS 0x20000000 /* PVHV keys live on shared string table */
500 /* PVAV could probably use 0x2000000 without conflict. I assume that PVFM can
501 be UTF-8 encoded, and PVCVs could well have UTF-8 prototypes. PVIOs haven't
502 been restructured, so sometimes get used as string buffers. */
505 /* Some private flags. */
508 /* scalar SVs with SVp_POK */
509 #define SVppv_STATIC 0x40000000 /* PV is pointer to static const; must be set with SVf_IsCOW */
511 #define SVpav_REAL 0x40000000 /* free old entries */
513 #define SVphv_LAZYDEL 0x40000000 /* entry in xhv_eiter must be deleted */
515 /* IV, PVIV, PVNV, PVMG, PVGV and (I assume) PVLV */
516 #define SVf_IVisUV 0x80000000 /* use XPVUV instead of XPVIV */
518 #define SVpav_REIFY 0x80000000 /* can become real */
520 #define SVphv_HASKFLAGS 0x80000000 /* keys have flag byte after hash */
521 /* RV upwards. However, SVf_ROK and SVp_IOK are exclusive */
522 #define SVprv_WEAKREF 0x80000000 /* Weak reference */
523 /* pad name vars only */
526 HV* xmg_stash; /* class package */ \
528 STRLEN xpv_cur; /* length of svu_pv as a C string */ \
530 STRLEN xpvlenu_len; /* allocated size */ \
531 struct regexp* xpvlenu_rx; /* regex when SV body is XPVLV */ \
534 #define xpv_len xpv_len_u.xpvlenu_len
537 NV xnv_nv; /* numeric value, if any */
539 line_t xnv_lines; /* used internally by S_scan_subst() */
540 bool xnv_bm_tail; /* an SvVALID (BM) SV has an implicit "\n" */
544 IV xivu_iv; /* integer value */
546 HEK * xivu_namehek; /* xpvlv, xpvgv: GvNAME */
547 bool xivu_eval_seen; /* used internally by S_scan_subst() */
552 MAGIC* xmg_magic; /* linked list of magicalness */
553 STRLEN xmg_hash_index; /* used while freeing hash entries */
565 #define xiv_iv xiv_u.xivu_iv
572 #define xuv_uv xuv_u.xivu_uv
580 /* This structure must match the beginning of struct xpvhv in hv.h. */
593 SSize_t xlvu_stargoff;
597 char xlv_type; /* k=keys .=pos x=substr v=vec /=join/re
598 * y=alem/helem/iter t=tie T=tied HE */
599 char xlv_flags; /* 1 = negative offset 2 = negative len
600 4 = out of range (vec) */
603 #define xlv_targoff xlv_targoff_u.xlvu_targoff
607 IV prev_index; /* caches result of previous invlist_search() */
608 STRLEN iterator; /* Stores where we are in iterating */
609 bool is_offset; /* The data structure for all inversion lists
610 begins with an element for code point U+0000.
611 If this bool is set, the actual list contains
612 that 0; otherwise, the list actually begins
613 with the following element. Thus to invert
614 the list, merely toggle this flag */
617 /* This structure works in 2 ways - regular scalar, or GV with GP */
625 typedef U32 cv_flags_t;
627 #define _XPVCV_COMMON \
635 void (*xcv_xsub) (pTHX_ CV*); \
643 PADLIST * xcv_padlist; \
647 U32 xcv_outside_seq; /* the COP sequence (at the point of our \
648 * compilation) in the lexically enclosing \
650 cv_flags_t xcv_flags; \
651 I32 xcv_depth /* >= 2 indicates recursive call */
653 /* This structure must match XPVCV in cv.h */
664 /* ifp and ofp are normally the same, but sockets need separate streams */
666 /* Cray addresses everything by word boundaries (64 bits) and
667 * code and data pointers cannot be mixed (which is exactly what
668 * Perl_filter_add() tries to do with the dirp), hence the
669 * following union trick (as suggested by Gurusamy Sarathy).
670 * For further information see Geir Johansen's problem report
671 * titled [ID 20000612.002 (#3366)] Perl problem on Cray system
672 * The any pointer (known as IoANY()) will also be a good place
673 * to hang any IO disciplines to.
676 DIR * xiou_dirp; /* for opendir, readdir, etc */
677 void * xiou_any; /* for alignment */
679 /* IV xio_lines is now in IVX $. */
680 IV xio_page; /* $% */
681 IV xio_page_len; /* $= */
682 IV xio_lines_left; /* $- */
683 char * xio_top_name; /* $^ */
684 GV * xio_top_gv; /* $^ */
685 char * xio_fmt_name; /* $~ */
686 GV * xio_fmt_gv; /* $~ */
687 char * xio_bottom_name;/* $^B */
688 GV * xio_bottom_gv; /* $^B */
693 #define xio_dirp xio_dirpu.xiou_dirp
694 #define xio_any xio_dirpu.xiou_any
696 #define IOf_ARGV 1 /* this fp iterates over ARGV */
697 #define IOf_START 2 /* check for null ARGV and substitute '-' */
698 #define IOf_FLUSH 4 /* this fp wants a flush after write op */
699 #define IOf_DIDTOP 8 /* just did top of form */
700 #define IOf_UNTAINT 16 /* consider this fp (and its data) "safe" */
701 #define IOf_NOLINE 32 /* slurped a pseudo-line from empty file */
702 #define IOf_FAKE_DIRP 64 /* xio_dirp is fake (source filters kludge)
703 Also, when this is set, SvPVX() is valid */
708 SSize_t xobject_maxfield;
709 SSize_t xobject_iter_sv_at; /* this is only used by Perl_sv_clear() */
713 #define ObjectMAXFIELD(inst) ((XPVOBJ *)SvANY(inst))->xobject_maxfield
714 #define ObjectITERSVAT(inst) ((XPVOBJ *)SvANY(inst))->xobject_iter_sv_at
715 #define ObjectFIELDS(inst) ((XPVOBJ *)SvANY(inst))->xobject_fields
717 /* The following macros define implementation-independent predicates on SVs. */
720 =for apidoc Am|U32|SvNIOK|SV* sv
721 Returns a U32 value indicating whether the SV contains a number, integer or
724 =for apidoc Am|U32|SvNIOKp|SV* sv
725 Returns a U32 value indicating whether the SV contains a number, integer or
726 double. Checks the B<private> setting. Use C<SvNIOK> instead.
728 =for apidoc Am|void|SvNIOK_off|SV* sv
729 Unsets the NV/IV status of an SV.
731 =for apidoc Am|U32|SvOK|SV* sv
732 Returns a U32 value indicating whether the value is defined. This is
733 only meaningful for scalars.
735 =for apidoc Am|U32|SvIOKp|SV* sv
736 Returns a U32 value indicating whether the SV contains an integer. Checks
737 the B<private> setting. Use C<SvIOK> instead.
739 =for apidoc Am|U32|SvNOKp|SV* sv
740 Returns a U32 value indicating whether the SV contains a double. Checks the
741 B<private> setting. Use C<SvNOK> instead.
743 =for apidoc Am|U32|SvPOKp|SV* sv
744 Returns a U32 value indicating whether the SV contains a character string.
745 Checks the B<private> setting. Use C<SvPOK> instead.
747 =for apidoc Am|U32|SvIOK|SV* sv
748 Returns a U32 value indicating whether the SV contains an integer.
750 =for apidoc Am|void|SvIOK_on|SV* sv
751 Tells an SV that it is an integer.
753 =for apidoc Am|void|SvIOK_off|SV* sv
754 Unsets the IV status of an SV.
756 =for apidoc Am|void|SvIOK_only|SV* sv
757 Tells an SV that it is an integer and disables all other C<OK> bits.
759 =for apidoc Am|void|SvIOK_only_UV|SV* sv
760 Tells an SV that it is an unsigned integer and disables all other C<OK> bits.
762 =for apidoc Am|bool|SvIOK_UV|SV* sv
763 Returns a boolean indicating whether the SV contains an integer that must be
764 interpreted as unsigned. A non-negative integer whose value is within the
765 range of both an IV and a UV may be flagged as either C<SvUOK> or C<SvIOK>.
767 =for apidoc Am|bool|SvUOK|SV* sv
768 Returns a boolean indicating whether the SV contains an integer that must be
769 interpreted as unsigned. A non-negative integer whose value is within the
770 range of both an IV and a UV may be flagged as either C<SvUOK> or C<SvIOK>.
772 =for apidoc Am|bool|SvIOK_notUV|SV* sv
773 Returns a boolean indicating whether the SV contains a signed integer.
775 =for apidoc Am|U32|SvNOK|SV* sv
776 Returns a U32 value indicating whether the SV contains a double.
778 =for apidoc Am|void|SvNOK_on|SV* sv
779 Tells an SV that it is a double.
781 =for apidoc Am|void|SvNOK_off|SV* sv
782 Unsets the NV status of an SV.
784 =for apidoc Am|void|SvNOK_only|SV* sv
785 Tells an SV that it is a double and disables all other OK bits.
787 =for apidoc Am|U32|SvPOK|SV* sv
788 Returns a U32 value indicating whether the SV contains a character
791 =for apidoc Am|void|SvPOK_on|SV* sv
792 Tells an SV that it is a string.
794 =for apidoc Am|void|SvPOK_off|SV* sv
795 Unsets the PV status of an SV.
797 =for apidoc Am|void|SvPOK_only|SV* sv
798 Tells an SV that it is a string and disables all other C<OK> bits.
799 Will also turn off the UTF-8 status.
801 =for apidoc Am|U32|SvBoolFlagsOK|SV* sv
802 Returns a bool indicating whether the SV has the right flags set such
803 that it is safe to call C<BOOL_INTERNALS_sv_isbool()> or
804 C<BOOL_INTERNALS_sv_isbool_true()> or
805 C<BOOL_INTERNALS_sv_isbool_false()>. Currently equivalent to
806 C<SvIandPOK(sv)> or C<SvIOK(sv) && SvPOK(sv)>. Serialization may want to
807 unroll this check. If so you are strongly recommended to add code like
808 C<assert(SvBoolFlagsOK(sv));> B<before> calling using any of the
809 BOOL_INTERNALS macros.
811 =for apidoc Am|U32|SvIandPOK|SV* sv
812 Returns a bool indicating whether the SV is both C<SvPOK()> and
813 C<SvIOK()> at the same time. Equivalent to C<SvIOK(sv) && SvPOK(sv)> but
816 =for apidoc Am|void|SvIandPOK_on|SV* sv
817 Tells an SV that is a string and a number in one operation. Equivalent
818 to C<SvIOK_on(sv); SvPOK_on(sv);> but more efficient.
820 =for apidoc Am|void|SvIandPOK_off|SV* sv
821 Unsets the PV and IV status of an SV in one operation. Equivalent to
822 C<SvIOK_off(sv); SvPK_off(v);> but more efficient.
824 =for apidoc Am|bool|BOOL_INTERNALS_sv_isbool|SV* sv
825 Checks if a C<SvBoolFlagsOK()> sv is a bool. B<Note> that it is the
826 caller's responsibility to ensure that the sv is C<SvBoolFlagsOK()> before
827 calling this. This is only useful in specialized logic like
828 serialization code where performance is critical and the flags have
829 already been checked to be correct. Almost always you should be using
830 C<sv_isbool(sv)> instead.
832 =for apidoc Am|bool|BOOL_INTERNALS_sv_isbool_true|SV* sv
833 Checks if a C<SvBoolFlagsOK()> sv is a true bool. B<Note> that it is
834 the caller's responsibility to ensure that the sv is C<SvBoolFlagsOK()>
835 before calling this. This is only useful in specialized logic like
836 serialization code where performance is critical and the flags have
837 already been checked to be correct. This is B<NOT> what you should use
838 to check if an SV is "true", for that you should be using
839 C<SvTRUE(sv)> instead.
841 =for apidoc Am|bool|BOOL_INTERNALS_sv_isbool_false|SV* sv
842 Checks if a C<SvBoolFlagsOK()> sv is a false bool. B<Note> that it is
843 the caller's responsibility to ensure that the sv is C<SvBoolFlagsOK()>
844 before calling this. This is only useful in specialized logic like
845 serialization code where performance is critical and the flags have
846 already been checked to be correct. This is B<NOT> what you should use
847 to check if an SV is "false", for that you should be using
848 C<!SvTRUE(sv)> instead.
850 =for apidoc Am|bool|SvVOK|SV* sv
851 Returns a boolean indicating whether the SV contains a v-string.
853 =for apidoc Am|U32|SvOOK|SV* sv
854 Returns a U32 indicating whether the pointer to the string buffer is offset.
855 This hack is used internally to speed up removal of characters from the
856 beginning of a C<L</SvPV>>. When C<SvOOK> is true, then the start of the
857 allocated string buffer is actually C<SvOOK_offset()> bytes before C<SvPVX>.
858 This offset used to be stored in C<SvIVX>, but is now stored within the spare
861 =for apidoc Am|U32|SvROK|SV* sv
862 Tests if the SV is an RV.
864 =for apidoc Am|void|SvROK_on|SV* sv
865 Tells an SV that it is an RV.
867 =for apidoc Am|void|SvROK_off|SV* sv
868 Unsets the RV status of an SV.
870 =for apidoc Am|SV*|SvRV|SV* sv
871 Dereferences an RV to return the SV.
873 =for apidoc Am|IV|SvIVX|SV* sv
874 Returns the raw value in the SV's IV slot, without checks or conversions.
875 Only use when you are sure C<SvIOK> is true. See also C<L</SvIV>>.
877 =for apidoc Am|UV|SvUVX|SV* sv
878 Returns the raw value in the SV's UV slot, without checks or conversions.
879 Only use when you are sure C<SvIOK> is true. See also C<L</SvUV>>.
881 =for apidoc AmD|UV|SvUVXx|SV* sv
882 This is an unnecessary synonym for L</SvUVX>
884 =for apidoc Am|NV|SvNVX|SV* sv
885 Returns the raw value in the SV's NV slot, without checks or conversions.
886 Only use when you are sure C<SvNOK> is true. See also C<L</SvNV>>.
888 =for apidoc Am |char* |SvPVX|SV* sv
889 =for apidoc_item |const char*|SvPVX_const|SV* sv
890 =for apidoc_item |char* |SvPVX_mutable|SV* sv
891 =for apidoc_item |char* |SvPVXx|SV* sv
893 These return a pointer to the physical string in the SV. The SV must contain a
894 string. Prior to 5.9.3 it is not safe to execute these unless the SV's
897 These are also used to store the name of an autoloaded subroutine in an XS
898 AUTOLOAD routine. See L<perlguts/Autoloading with XSUBs>.
900 C<SvPVXx> is identical to C<SvPVX>.
902 C<SvPVX_mutable> is merely a synonym for C<SvPVX>, but its name emphasizes that
903 the string is modifiable by the caller.
905 C<SvPVX_const> differs in that the return value has been cast so that the
906 compiler will complain if you were to try to modify the contents of the string,
907 (unless you cast away const yourself).
909 =for apidoc Am|STRLEN|SvCUR|SV* sv
910 Returns the length, in bytes, of the PV inside the SV.
911 Note that this may not match Perl's C<length>; for that, use
912 C<sv_len_utf8(sv)>. See C<L</SvLEN>> also.
914 =for apidoc Am|STRLEN|SvLEN|SV* sv
915 Returns the size of the string buffer in the SV, not including any part
916 attributable to C<SvOOK>. See C<L</SvCUR>>.
918 =for apidoc Am|char*|SvEND|SV* sv
919 Returns a pointer to the spot just after the last character in
920 the string which is in the SV, where there is usually a trailing
921 C<NUL> character (even though Perl scalars do not strictly require it).
922 See C<L</SvCUR>>. Access the character as C<*(SvEND(sv))>.
924 Warning: If C<SvCUR> is equal to C<SvLEN>, then C<SvEND> points to
927 =for apidoc Am|HV*|SvSTASH|SV* sv
928 Returns the stash of the SV.
930 =for apidoc Am|void|SvIV_set|SV* sv|IV val
931 Set the value of the IV pointer in sv to val. It is possible to perform
932 the same function of this macro with an lvalue assignment to C<SvIVX>.
933 With future Perls, however, it will be more efficient to use
934 C<SvIV_set> instead of the lvalue assignment to C<SvIVX>.
936 =for apidoc Am|void|SvNV_set|SV* sv|NV val
937 Set the value of the NV pointer in C<sv> to val. See C<L</SvIV_set>>.
939 =for apidoc Am|void|SvPV_set|SV* sv|char* val
940 This is probably not what you want to use, you probably wanted
941 L</sv_usepvn_flags> or L</sv_setpvn> or L</sv_setpvs>.
943 Set the value of the PV pointer in C<sv> to the Perl allocated
944 C<NUL>-terminated string C<val>. See also C<L</SvIV_set>>.
946 Remember to free the previous PV buffer. There are many things to check.
947 Beware that the existing pointer may be involved in copy-on-write or other
948 mischief, so do C<SvOOK_off(sv)> and use C<sv_force_normal> or
949 C<SvPV_force> (or check the C<SvIsCOW> flag) first to make sure this
950 modification is safe. Then finally, if it is not a COW, call
951 C<L</SvPV_free>> to free the previous PV buffer.
953 =for apidoc Am|void|SvUV_set|SV* sv|UV val
954 Set the value of the UV pointer in C<sv> to val. See C<L</SvIV_set>>.
956 =for apidoc Am|void|SvRV_set|SV* sv|SV* val
957 Set the value of the RV pointer in C<sv> to val. See C<L</SvIV_set>>.
959 =for apidoc Am|void|SvMAGIC_set|SV* sv|MAGIC* val
960 Set the value of the MAGIC pointer in C<sv> to val. See C<L</SvIV_set>>.
962 =for apidoc Am|void|SvSTASH_set|SV* sv|HV* val
963 Set the value of the STASH pointer in C<sv> to val. See C<L</SvIV_set>>.
965 =for apidoc Am|void|SvCUR_set|SV* sv|STRLEN len
966 Sets the current length, in bytes, of the C string which is in the SV.
967 See C<L</SvCUR>> and C<SvIV_set>>.
969 =for apidoc Am|void|SvLEN_set|SV* sv|STRLEN len
970 Set the size of the string buffer for the SV. See C<L</SvLEN>>.
975 #define SvNIOK(sv) (SvFLAGS(sv) & (SVf_IOK|SVf_NOK))
976 #define SvNIOKp(sv) (SvFLAGS(sv) & (SVp_IOK|SVp_NOK))
977 #define SvNIOK_off(sv) (SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK| \
978 SVp_IOK|SVp_NOK|SVf_IVisUV))
980 #define assert_not_ROK(sv) assert_(!SvROK(sv) || !SvRV(sv))
981 #define assert_not_glob(sv) assert_(!isGV_with_GP(sv))
983 #define SvOK(sv) (SvFLAGS(sv) & SVf_OK)
984 #define SvOK_off(sv) (assert_not_ROK(sv) assert_not_glob(sv) \
985 SvFLAGS(sv) &= ~(SVf_OK| \
986 SVf_IVisUV|SVf_UTF8), \
988 #define SvOK_off_exc_UV(sv) (assert_not_ROK(sv) \
989 SvFLAGS(sv) &= ~(SVf_OK| \
993 #define SvOKp(sv) (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
994 #define SvIOKp(sv) (SvFLAGS(sv) & SVp_IOK)
995 #define SvIOKp_on(sv) (assert_not_glob(sv) \
996 SvFLAGS(sv) |= SVp_IOK)
997 #define SvNOKp(sv) (SvFLAGS(sv) & SVp_NOK)
998 #define SvNOKp_on(sv) (assert_not_glob(sv) SvFLAGS(sv) |= SVp_NOK)
999 #define SvPOKp(sv) (SvFLAGS(sv) & SVp_POK)
1000 #define SvPOKp_on(sv) (assert_not_ROK(sv) assert_not_glob(sv) \
1001 SvFLAGS(sv) |= SVp_POK)
1003 #define SvIOK(sv) (SvFLAGS(sv) & SVf_IOK)
1004 #define SvIOK_on(sv) (assert_not_glob(sv) \
1005 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
1006 #define SvIOK_off(sv) (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV))
1007 #define SvIOK_only(sv) (SvOK_off(sv), \
1008 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
1009 #define SvIOK_only_UV(sv) (assert_not_glob(sv) SvOK_off_exc_UV(sv), \
1010 SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
1012 #define SvIOK_UV(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV)) \
1013 == (SVf_IOK|SVf_IVisUV))
1014 #define SvUOK(sv) SvIOK_UV(sv)
1015 #define SvIOK_notUV(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV)) \
1018 #define SvIandPOK(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_POK)) == (SVf_IOK|SVf_POK))
1019 #define SvIandPOK_on(sv) (assert_not_glob(sv) \
1020 (SvFLAGS(sv) |= (SVf_IOK|SVp_IOK|SVf_POK|SVp_POK)))
1021 #define SvIandPOK_off(sv) (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV|SVf_POK|SVp_POK))
1023 #define SvBoolFlagsOK(sv) SvIandPOK(sv)
1025 #define BOOL_INTERNALS_sv_isbool(sv) (SvIsCOW_static(sv) && \
1026 (SvPVX_const(sv) == PL_Yes || SvPVX_const(sv) == PL_No))
1027 #define BOOL_INTERNALS_sv_isbool_true(sv) (SvIsCOW_static(sv) && \
1028 (SvPVX_const(sv) == PL_Yes))
1029 #define BOOL_INTERNALS_sv_isbool_false(sv) (SvIsCOW_static(sv) && \
1030 (SvPVX_const(sv) == PL_No))
1032 #define SvIsUV(sv) (SvFLAGS(sv) & SVf_IVisUV)
1033 #define SvIsUV_on(sv) (SvFLAGS(sv) |= SVf_IVisUV)
1034 #define SvIsUV_off(sv) (SvFLAGS(sv) &= ~SVf_IVisUV)
1036 #define SvNOK(sv) (SvFLAGS(sv) & SVf_NOK)
1037 #define SvNOK_on(sv) (assert_not_glob(sv) \
1038 SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
1039 #define SvNOK_off(sv) (SvFLAGS(sv) &= ~(SVf_NOK|SVp_NOK))
1040 #define SvNOK_only(sv) (SvOK_off(sv), \
1041 SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
1044 =for apidoc Am|U32|SvUTF8|SV* sv
1045 Returns a U32 value indicating the UTF-8 status of an SV. If things are set-up
1046 properly, this indicates whether or not the SV contains UTF-8 encoded data.
1047 You should use this I<after> a call to C<L</SvPV>> or one of its variants, in
1048 case any call to string overloading updates the internal flag.
1050 If you want to take into account the L<bytes> pragma, use C<L</DO_UTF8>>
1053 =for apidoc Am|void|SvUTF8_on|SV *sv
1054 Turn on the UTF-8 status of an SV (the data is not changed, just the flag).
1055 Do not use frivolously.
1057 =for apidoc Am|void|SvUTF8_off|SV *sv
1058 Unsets the UTF-8 status of an SV (the data is not changed, just the flag).
1059 Do not use frivolously.
1061 =for apidoc Am|void|SvPOK_only_UTF8|SV* sv
1062 Tells an SV that it is a string and disables all other C<OK> bits,
1063 and leaves the UTF-8 status as it was.
1068 /* Ensure the return value of this macro does not clash with the GV_ADD* flags
1070 #define SvUTF8(sv) (SvFLAGS(sv) & SVf_UTF8)
1071 #define SvUTF8_on(sv) (SvFLAGS(sv) |= (SVf_UTF8))
1072 #define SvUTF8_off(sv) (SvFLAGS(sv) &= ~(SVf_UTF8))
1074 #define SvPOK(sv) (SvFLAGS(sv) & SVf_POK)
1075 #define SvPOK_on(sv) (assert_not_ROK(sv) assert_not_glob(sv) \
1076 SvFLAGS(sv) |= (SVf_POK|SVp_POK))
1077 #define SvPOK_off(sv) (SvFLAGS(sv) &= ~(SVf_POK|SVp_POK))
1078 #define SvPOK_only(sv) (assert_not_ROK(sv) assert_not_glob(sv) \
1079 SvFLAGS(sv) &= ~(SVf_OK| \
1080 SVf_IVisUV|SVf_UTF8), \
1081 SvFLAGS(sv) |= (SVf_POK|SVp_POK))
1082 #define SvPOK_only_UTF8(sv) (assert_not_ROK(sv) assert_not_glob(sv) \
1083 SvFLAGS(sv) &= ~(SVf_OK| \
1085 SvFLAGS(sv) |= (SVf_POK|SVp_POK))
1087 #define SvVOK(sv) (SvMAGICAL(sv) \
1088 && mg_find(sv,PERL_MAGIC_vstring))
1090 =for apidoc Am|MAGIC*|SvVSTRING_mg|SV * sv
1092 Returns the vstring magic, or NULL if none
1096 #define SvVSTRING_mg(sv) (SvMAGICAL(sv) \
1097 ? mg_find(sv,PERL_MAGIC_vstring) : NULL)
1099 #define SvOOK(sv) (SvFLAGS(sv) & SVf_OOK)
1100 #define SvOOK_on(sv) (SvFLAGS(sv) |= SVf_OOK)
1104 =for apidoc Am|void|SvOOK_off|SV * sv
1106 Remove any string offset.
1111 #define SvOOK_off(sv) ((void)(SvOOK(sv) && (sv_backoff(sv),0)))
1113 #define SvFAKE(sv) (SvFLAGS(sv) & SVf_FAKE)
1114 #define SvFAKE_on(sv) (SvFLAGS(sv) |= SVf_FAKE)
1115 #define SvFAKE_off(sv) (SvFLAGS(sv) &= ~SVf_FAKE)
1117 #define SvROK(sv) (SvFLAGS(sv) & SVf_ROK)
1118 #define SvROK_on(sv) (SvFLAGS(sv) |= SVf_ROK)
1119 #define SvROK_off(sv) (SvFLAGS(sv) &= ~(SVf_ROK))
1121 #define SvMAGICAL(sv) (SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG))
1122 #define SvMAGICAL_on(sv) (SvFLAGS(sv) |= (SVs_GMG|SVs_SMG|SVs_RMG))
1123 #define SvMAGICAL_off(sv) (SvFLAGS(sv) &= ~(SVs_GMG|SVs_SMG|SVs_RMG))
1125 #define SvGMAGICAL(sv) (SvFLAGS(sv) & SVs_GMG)
1126 #define SvGMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_GMG)
1127 #define SvGMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_GMG)
1129 #define SvSMAGICAL(sv) (SvFLAGS(sv) & SVs_SMG)
1130 #define SvSMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_SMG)
1131 #define SvSMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_SMG)
1133 #define SvRMAGICAL(sv) (SvFLAGS(sv) & SVs_RMG)
1134 #define SvRMAGICAL_on(sv) (SvFLAGS(sv) |= SVs_RMG)
1135 #define SvRMAGICAL_off(sv) (SvFLAGS(sv) &= ~SVs_RMG)
1138 =for apidoc Am|bool|SvAMAGIC|SV * sv
1140 Returns a boolean as to whether C<sv> has overloading (active magic) enabled or
1146 #define SvAMAGIC(sv) (SvROK(sv) && SvOBJECT(SvRV(sv)) && \
1147 HvAMAGIC(SvSTASH(SvRV(sv))))
1149 /* To be used on the stashes themselves: */
1150 #define HvAMAGIC(hv) (SvFLAGS(hv) & SVf_AMAGIC)
1151 #define HvAMAGIC_on(hv) (SvFLAGS(hv) |= SVf_AMAGIC)
1152 #define HvAMAGIC_off(hv) (SvFLAGS(hv) &=~ SVf_AMAGIC)
1155 /* "nog" means "doesn't have get magic" */
1156 #define SvPOK_nog(sv) ((SvFLAGS(sv) & (SVf_POK|SVs_GMG)) == SVf_POK)
1157 #define SvIOK_nog(sv) ((SvFLAGS(sv) & (SVf_IOK|SVs_GMG)) == SVf_IOK)
1158 #define SvUOK_nog(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV|SVs_GMG)) == (SVf_IOK|SVf_IVisUV))
1159 #define SvNOK_nog(sv) ((SvFLAGS(sv) & (SVf_NOK|SVs_GMG)) == SVf_NOK)
1160 #define SvNIOK_nog(sv) (SvNIOK(sv) && !(SvFLAGS(sv) & SVs_GMG))
1162 #define SvPOK_nogthink(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
1163 #define SvIOK_nogthink(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_THINKFIRST|SVs_GMG)) == SVf_IOK)
1164 #define SvUOK_nogthink(sv) ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV|SVf_THINKFIRST|SVs_GMG)) == (SVf_IOK|SVf_IVisUV))
1165 #define SvNOK_nogthink(sv) ((SvFLAGS(sv) & (SVf_NOK|SVf_THINKFIRST|SVs_GMG)) == SVf_NOK)
1166 #define SvNIOK_nogthink(sv) (SvNIOK(sv) && !(SvFLAGS(sv) & (SVf_THINKFIRST|SVs_GMG)))
1168 #define SvPOK_utf8_nog(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVs_GMG)) == (SVf_POK|SVf_UTF8))
1169 #define SvPOK_utf8_nogthink(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST|SVs_GMG)) == (SVf_POK|SVf_UTF8))
1171 #define SvPOK_byte_nog(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVs_GMG)) == SVf_POK)
1172 #define SvPOK_byte_nogthink(sv) ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
1174 #define SvPOK_pure_nogthink(sv) \
1175 ((SvFLAGS(sv) & (SVf_POK|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
1176 #define SvPOK_utf8_pure_nogthink(sv) \
1177 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == (SVf_POK|SVf_UTF8))
1178 #define SvPOK_byte_pure_nogthink(sv) \
1179 ((SvFLAGS(sv) & (SVf_POK|SVf_UTF8|SVf_IOK|SVf_NOK|SVf_ROK|SVpgv_GP|SVf_THINKFIRST|SVs_GMG)) == SVf_POK)
1182 =for apidoc Am|bool|SvIsBOOL|SV* sv
1184 Returns true if the SV is one of the special boolean constants (PL_sv_yes or
1185 PL_sv_no), or is a regular SV whose last assignment stored a copy of one.
1190 #define SvIsBOOL(sv) Perl_sv_isbool(aTHX_ sv)
1193 =for apidoc Am|U32|SvGAMAGIC|SV* sv
1195 Returns true if the SV has get magic or
1196 overloading. If either is true then
1197 the scalar is active data, and has the potential to return a new value every
1198 time it is accessed. Hence you must be careful to
1199 only read it once per user logical operation and work
1200 with that returned value. If neither is true then
1201 the scalar's value cannot change unless written to.
1206 #define SvGAMAGIC(sv) (SvGMAGICAL(sv) || SvAMAGIC(sv))
1208 #define Gv_AMG(stash) \
1209 (HvNAME(stash) && Gv_AMupdate(stash,FALSE) \
1211 : (HvAMAGIC_off(stash), 0))
1213 #define SvWEAKREF(sv) ((SvFLAGS(sv) & (SVf_ROK|SVprv_WEAKREF)) \
1214 == (SVf_ROK|SVprv_WEAKREF))
1215 #define SvWEAKREF_on(sv) (SvFLAGS(sv) |= (SVf_ROK|SVprv_WEAKREF))
1216 #define SvWEAKREF_off(sv) (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_WEAKREF))
1218 #define SvPCS_IMPORTED(sv) ((SvFLAGS(sv) & (SVf_ROK|SVprv_PCS_IMPORTED)) \
1219 == (SVf_ROK|SVprv_PCS_IMPORTED))
1220 #define SvPCS_IMPORTED_on(sv) (SvFLAGS(sv) |= (SVf_ROK|SVprv_PCS_IMPORTED))
1221 #define SvPCS_IMPORTED_off(sv) (SvFLAGS(sv) &= ~(SVf_ROK|SVprv_PCS_IMPORTED))
1224 =for apidoc m|U32|SvTHINKFIRST|SV *sv
1226 A quick flag check to see whether an C<sv> should be passed to C<sv_force_normal>
1227 to be "downgraded" before C<SvIVX> or C<SvPVX> can be modified directly.
1229 For example, if your scalar is a reference and you want to modify the C<SvIVX>
1230 slot, you can't just do C<SvROK_off>, as that will leak the referent.
1232 This is used internally by various sv-modifying functions, such as
1233 C<sv_setsv>, C<sv_setiv> and C<sv_pvn_force>.
1235 One case that this does not handle is a gv without SvFAKE set. After
1237 if (SvTHINKFIRST(gv)) sv_force_normal(gv);
1239 it will still be a gv.
1241 C<SvTHINKFIRST> sometimes produces false positives. In those cases
1242 C<sv_force_normal> does nothing.
1247 #define SvTHINKFIRST(sv) (SvFLAGS(sv) & SVf_THINKFIRST)
1250 #define SvPADMY(sv) (!(SvFLAGS(sv) & SVs_PADTMP))
1252 # define SvPADMY_on(sv) SvPADTMP_off(sv)
1255 #define SvPADTMP(sv) (SvFLAGS(sv) & (SVs_PADTMP))
1256 #define SvPADSTALE(sv) (SvFLAGS(sv) & (SVs_PADSTALE))
1258 #define SvPADTMP_on(sv) (SvFLAGS(sv) |= SVs_PADTMP)
1259 #define SvPADTMP_off(sv) (SvFLAGS(sv) &= ~SVs_PADTMP)
1260 #define SvPADSTALE_on(sv) Perl_SvPADSTALE_on(MUTABLE_SV(sv))
1261 #define SvPADSTALE_off(sv) Perl_SvPADSTALE_off(MUTABLE_SV(sv))
1263 #define SvTEMP(sv) (SvFLAGS(sv) & SVs_TEMP)
1264 #define SvTEMP_on(sv) (SvFLAGS(sv) |= SVs_TEMP)
1265 #define SvTEMP_off(sv) (SvFLAGS(sv) &= ~SVs_TEMP)
1267 #define SvOBJECT(sv) (SvFLAGS(sv) & SVs_OBJECT)
1268 #define SvOBJECT_on(sv) (SvFLAGS(sv) |= SVs_OBJECT)
1269 #define SvOBJECT_off(sv) (SvFLAGS(sv) &= ~SVs_OBJECT)
1272 =for apidoc Am|U32|SvREADONLY|SV* sv
1273 Returns true if the argument is readonly, otherwise returns false.
1274 Exposed to perl code via Internals::SvREADONLY().
1276 =for apidoc Am|U32|SvREADONLY_on|SV* sv
1277 Mark an object as readonly. Exactly what this means depends on the object
1278 type. Exposed to perl code via Internals::SvREADONLY().
1280 =for apidoc Am|U32|SvREADONLY_off|SV* sv
1281 Mark an object as not-readonly. Exactly what this mean depends on the
1282 object type. Exposed to perl code via Internals::SvREADONLY().
1287 #define SvREADONLY(sv) (SvFLAGS(sv) & (SVf_READONLY|SVf_PROTECT))
1289 # define SvREADONLY_on(sv) (SvFLAGS(sv) |= (SVf_READONLY|SVf_PROTECT))
1290 # define SvREADONLY_off(sv) (SvFLAGS(sv) &=~(SVf_READONLY|SVf_PROTECT))
1292 # define SvREADONLY_on(sv) (SvFLAGS(sv) |= SVf_READONLY)
1293 # define SvREADONLY_off(sv) (SvFLAGS(sv) &= ~SVf_READONLY)
1296 #define SvSCREAM(sv) ((SvFLAGS(sv) & (SVp_SCREAM|SVp_POK)) == (SVp_SCREAM|SVp_POK))
1297 #define SvSCREAM_on(sv) (SvFLAGS(sv) |= SVp_SCREAM)
1298 #define SvSCREAM_off(sv) (SvFLAGS(sv) &= ~SVp_SCREAM)
1301 # define SvCOMPILED(sv) 0
1302 # define SvCOMPILED_on(sv)
1303 # define SvCOMPILED_off(sv)
1307 #if defined (DEBUGGING) && defined(PERL_USE_GCC_BRACE_GROUPS)
1308 # define SvTAIL(sv) ({ const SV *const _svtail = (const SV *)(sv); \
1309 assert(SvTYPE(_svtail) != SVt_PVAV); \
1310 assert(SvTYPE(_svtail) != SVt_PVHV); \
1311 assert(!(SvFLAGS(_svtail) & (SVf_NOK|SVp_NOK))); \
1312 assert(SvVALID(_svtail)); \
1313 ((XPVNV*)SvANY(_svtail))->xnv_u.xnv_bm_tail; \
1316 # define SvTAIL(_svtail) (((XPVNV*)SvANY(_svtail))->xnv_u.xnv_bm_tail)
1319 /* Does the SV have a Boyer-Moore table attached as magic?
1320 * 'VALID' is a poor name, but is kept for historical reasons. */
1321 #define SvVALID(_svvalid) ( \
1323 && SvSMAGICAL(_svvalid) \
1324 && SvMAGIC(_svvalid) \
1325 && (SvMAGIC(_svvalid)->mg_type == PERL_MAGIC_bm \
1326 || mg_find(_svvalid, PERL_MAGIC_bm)) \
1329 #define SvRVx(sv) SvRV(sv)
1331 #ifdef PERL_DEBUG_COW
1332 /* Need -0.0 for SvNVX to preserve IEEE FP "negative zero" because
1333 +0.0 + -0.0 => +0.0 but -0.0 + -0.0 => -0.0 */
1334 # define SvIVX(sv) (0 + ((XPVIV*) SvANY(sv))->xiv_iv)
1335 # define SvUVX(sv) (0 + ((XPVUV*) SvANY(sv))->xuv_uv)
1336 # define SvNVX(sv) (-0.0 + ((XPVNV*) SvANY(sv))->xnv_u.xnv_nv)
1337 # define SvRV(sv) (0 + (sv)->sv_u.svu_rv)
1338 # define SvRV_const(sv) (0 + (sv)->sv_u.svu_rv)
1339 /* Don't test the core XS code yet. */
1340 # if defined (PERL_CORE) && PERL_DEBUG_COW > 1
1341 # define SvPVX(sv) (0 + (assert_(!SvREADONLY(sv)) (sv)->sv_u.svu_pv))
1343 # define SvPVX(sv) SvPVX_mutable(sv)
1345 # define SvCUR(sv) (0 + ((XPV*) SvANY(sv))->xpv_cur)
1346 # define SvLEN(sv) (0 + ((XPV*) SvANY(sv))->xpv_len)
1347 # define SvEND(sv) ((sv)->sv_u.svu_pv + ((XPV*)SvANY(sv))->xpv_cur)
1349 # define SvMAGIC(sv) (0 + *(assert_(SvTYPE(sv) >= SVt_PVMG) &((XPVMG*) SvANY(sv))->xmg_u.xmg_magic))
1350 # define SvSTASH(sv) (0 + *(assert_(SvTYPE(sv) >= SVt_PVMG) &((XPVMG*) SvANY(sv))->xmg_stash))
1351 #else /* Below is not PERL_DEBUG_COW */
1353 # define SvLEN(sv) (0 + ((XPV*) SvANY(sv))->xpv_len)
1355 # define SvLEN(sv) ((XPV*) SvANY(sv))->xpv_len
1357 # define SvEND(sv) ((sv)->sv_u.svu_pv + ((XPV*)SvANY(sv))->xpv_cur)
1359 # if defined (DEBUGGING) && defined(PERL_USE_GCC_BRACE_GROUPS)
1360 /* These get expanded inside other macros that already use a variable _sv */
1361 # define SvPVX(sv) \
1362 (*({ SV *const _svpvx = MUTABLE_SV(sv); \
1363 assert(PL_valid_types_PVX[SvTYPE(_svpvx) & SVt_MASK]); \
1364 assert(!isGV_with_GP(_svpvx)); \
1365 assert(!(SvTYPE(_svpvx) == SVt_PVIO \
1366 && !(IoFLAGS(_svpvx) & IOf_FAKE_DIRP))); \
1367 &((_svpvx)->sv_u.svu_pv); \
1370 # define SvCUR(sv) \
1371 ({ const SV *const _svcur = (const SV *)(sv); \
1372 assert(PL_valid_types_PVX[SvTYPE(_svcur) & SVt_MASK]); \
1373 assert(!isGV_with_GP(_svcur)); \
1374 assert(!(SvTYPE(_svcur) == SVt_PVIO \
1375 && !(IoFLAGS(_svcur) & IOf_FAKE_DIRP))); \
1376 (((XPV*) MUTABLE_PTR(SvANY(_svcur)))->xpv_cur); \
1379 # define SvCUR(sv) \
1380 (*({ const SV *const _svcur = (const SV *)(sv); \
1381 assert(PL_valid_types_PVX[SvTYPE(_svcur) & SVt_MASK]); \
1382 assert(!isGV_with_GP(_svcur)); \
1383 assert(!(SvTYPE(_svcur) == SVt_PVIO \
1384 && !(IoFLAGS(_svcur) & IOf_FAKE_DIRP))); \
1385 &(((XPV*) MUTABLE_PTR(SvANY(_svcur)))->xpv_cur); \
1388 # define SvIVX(sv) \
1389 (*({ const SV *const _svivx = (const SV *)(sv); \
1390 assert(PL_valid_types_IVX[SvTYPE(_svivx) & SVt_MASK]); \
1391 assert(!isGV_with_GP(_svivx)); \
1392 &(((XPVIV*) MUTABLE_PTR(SvANY(_svivx)))->xiv_iv); \
1394 # define SvUVX(sv) \
1395 (*({ const SV *const _svuvx = (const SV *)(sv); \
1396 assert(PL_valid_types_IVX[SvTYPE(_svuvx) & SVt_MASK]); \
1397 assert(!isGV_with_GP(_svuvx)); \
1398 &(((XPVUV*) MUTABLE_PTR(SvANY(_svuvx)))->xuv_uv); \
1400 # define SvNVX(sv) \
1401 (*({ const SV *const _svnvx = (const SV *)(sv); \
1402 assert(PL_valid_types_NVX[SvTYPE(_svnvx) & SVt_MASK]); \
1403 assert(!isGV_with_GP(_svnvx)); \
1404 &(((XPVNV*) MUTABLE_PTR(SvANY(_svnvx)))->xnv_u.xnv_nv); \
1407 (*({ SV *const _svrv = MUTABLE_SV(sv); \
1408 assert(PL_valid_types_RV[SvTYPE(_svrv) & SVt_MASK]); \
1409 assert(!isGV_with_GP(_svrv)); \
1410 assert(!(SvTYPE(_svrv) == SVt_PVIO \
1411 && !(IoFLAGS(_svrv) & IOf_FAKE_DIRP))); \
1412 &((_svrv)->sv_u.svu_rv); \
1414 # define SvRV_const(sv) \
1415 ({ const SV *const _svrv = (const SV *)(sv); \
1416 assert(PL_valid_types_RV[SvTYPE(_svrv) & SVt_MASK]); \
1417 assert(!isGV_with_GP(_svrv)); \
1418 assert(!(SvTYPE(_svrv) == SVt_PVIO \
1419 && !(IoFLAGS(_svrv) & IOf_FAKE_DIRP))); \
1420 (_svrv)->sv_u.svu_rv; \
1422 # define SvMAGIC(sv) \
1423 (*({ const SV *const _svmagic = (const SV *)(sv); \
1424 assert(SvTYPE(_svmagic) >= SVt_PVMG); \
1425 &(((XPVMG*) MUTABLE_PTR(SvANY(_svmagic)))->xmg_u.xmg_magic); \
1427 # define SvSTASH(sv) \
1428 (*({ const SV *const _svstash = (const SV *)(sv); \
1429 assert(SvTYPE(_svstash) >= SVt_PVMG); \
1430 &(((XPVMG*) MUTABLE_PTR(SvANY(_svstash)))->xmg_stash); \
1432 # else /* Below is not DEBUGGING or can't use brace groups */
1433 # define SvPVX(sv) ((sv)->sv_u.svu_pv)
1434 # define SvCUR(sv) ((XPV*) SvANY(sv))->xpv_cur
1435 # define SvIVX(sv) ((XPVIV*) SvANY(sv))->xiv_iv
1436 # define SvUVX(sv) ((XPVUV*) SvANY(sv))->xuv_uv
1437 # define SvNVX(sv) ((XPVNV*) SvANY(sv))->xnv_u.xnv_nv
1438 # define SvRV(sv) ((sv)->sv_u.svu_rv)
1439 # define SvRV_const(sv) (0 + (sv)->sv_u.svu_rv)
1440 # define SvMAGIC(sv) ((XPVMG*) SvANY(sv))->xmg_u.xmg_magic
1441 # define SvSTASH(sv) ((XPVMG*) SvANY(sv))->xmg_stash
1446 /* Given that these two are new, there can't be any existing code using them
1447 * as LVALUEs, so prevent that from happening */
1448 # define SvPVX_mutable(sv) ((char *)((sv)->sv_u.svu_pv))
1449 # define SvPVX_const(sv) ((const char*)((sv)->sv_u.svu_pv))
1451 /* Except for the poison code, which uses & to scribble over the pointer after
1452 free() is called. */
1453 # define SvPVX_mutable(sv) ((sv)->sv_u.svu_pv)
1454 # define SvPVX_const(sv) ((const char*)((sv)->sv_u.svu_pv))
1457 #define SvIVXx(sv) SvIVX(sv)
1458 #define SvUVXx(sv) SvUVX(sv)
1459 #define SvNVXx(sv) SvNVX(sv)
1460 #define SvPVXx(sv) SvPVX(sv)
1461 #define SvLENx(sv) SvLEN(sv)
1462 #define SvENDx(sv) ((PL_Sv = (sv)), SvEND(PL_Sv))
1465 /* Ask a scalar nicely to try to become an IV, if possible.
1466 Not guaranteed to stay returning void */
1467 /* Macro won't actually call sv_2iv if already IOK */
1468 #define SvIV_please(sv) \
1470 SV * sv_ = MUTABLE_SV(sv); \
1471 if (!SvIOKp(sv_) && (SvFLAGS(sv_) & (SVf_NOK|SVf_POK))) \
1474 #define SvIV_please_nomg(sv) \
1475 (!(SvFLAGS(sv) & (SVf_IOK|SVp_IOK)) && (SvFLAGS(sv) & (SVf_NOK|SVf_POK)) \
1476 ? (sv_2iv_flags(sv, 0), SvIOK(sv)) \
1479 #define SvIV_set(sv, val) \
1481 SV * sv_ = MUTABLE_SV(sv); \
1482 assert(PL_valid_types_IV_set[SvTYPE(sv_) & SVt_MASK]); \
1483 assert(!isGV_with_GP(sv_)); \
1484 (((XPVIV*) SvANY(sv_))->xiv_iv = (val)); \
1487 #define SvNV_set(sv, val) \
1489 SV * sv_ = MUTABLE_SV(sv); \
1490 assert(PL_valid_types_NV_set[SvTYPE(sv_) & SVt_MASK]); \
1491 assert(!isGV_with_GP(sv_)); \
1492 (((XPVNV*)SvANY(sv_))->xnv_u.xnv_nv = (val)); \
1495 #define SvPV_set(sv, val) \
1497 SV * sv_ = MUTABLE_SV(sv); \
1498 assert(PL_valid_types_PVX[SvTYPE(sv_) & SVt_MASK]); \
1499 assert(!isGV_with_GP(sv_)); \
1500 assert(!(SvTYPE(sv_) == SVt_PVIO \
1501 && !(IoFLAGS(sv_) & IOf_FAKE_DIRP))); \
1502 ((sv_)->sv_u.svu_pv = (val)); \
1505 #define SvUV_set(sv, val) \
1507 SV * sv_ = MUTABLE_SV(sv); \
1508 assert(PL_valid_types_IV_set[SvTYPE(sv_) & SVt_MASK]); \
1509 assert(!isGV_with_GP(sv_)); \
1510 (((XPVUV*)SvANY(sv_))->xuv_uv = (val)); \
1513 #define SvRV_set(sv, val) \
1515 SV * sv_ = MUTABLE_SV(sv); \
1516 assert(PL_valid_types_RV[SvTYPE(sv_) & SVt_MASK]); \
1517 assert(!isGV_with_GP(sv_)); \
1518 assert(!(SvTYPE(sv_) == SVt_PVIO \
1519 && !(IoFLAGS(sv_) & IOf_FAKE_DIRP))); \
1520 ((sv_)->sv_u.svu_rv = (val)); \
1522 #define SvMAGIC_set(sv, val) \
1523 STMT_START { assert(SvTYPE(sv) >= SVt_PVMG); \
1524 (((XPVMG*)SvANY(sv))->xmg_u.xmg_magic = (val)); } STMT_END
1525 #define SvSTASH_set(sv, val) \
1526 STMT_START { assert(SvTYPE(sv) >= SVt_PVMG); \
1527 (((XPVMG*) SvANY(sv))->xmg_stash = (val)); } STMT_END
1528 #define SvCUR_set(sv, val) \
1530 assert(PL_valid_types_PVX[SvTYPE(sv) & SVt_MASK]); \
1531 assert(!isGV_with_GP(sv)); \
1532 assert(!(SvTYPE(sv) == SVt_PVIO \
1533 && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1534 (((XPV*) SvANY(sv))->xpv_cur = (val)); } STMT_END
1535 #define SvLEN_set(sv, val) \
1537 assert(PL_valid_types_PVX[SvTYPE(sv) & SVt_MASK]); \
1538 assert(!isGV_with_GP(sv)); \
1539 assert(!(SvTYPE(sv) == SVt_PVIO \
1540 && !(IoFLAGS(sv) & IOf_FAKE_DIRP))); \
1541 (((XPV*) SvANY(sv))->xpv_len = (val)); } STMT_END
1542 #define SvEND_set(sv, val) \
1543 STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
1544 SvCUR_set(sv, (val) - SvPVX(sv)); } STMT_END
1547 =for apidoc Am|void|SvPV_renew|SV* sv|STRLEN len
1548 Low level micro optimization of C<L</SvGROW>>. It is generally better to use
1549 C<SvGROW> instead. This is because C<SvPV_renew> ignores potential issues that
1550 C<SvGROW> handles. C<sv> needs to have a real C<PV> that is unencumbered by
1551 things like COW. Using C<SV_CHECK_THINKFIRST> or
1552 C<SV_CHECK_THINKFIRST_COW_DROP> before calling this should clean it up, but
1553 why not just use C<SvGROW> if you're not sure about the provenance?
1557 #define SvPV_renew(sv,n) \
1558 STMT_START { SvLEN_set(sv, n); \
1559 SvPV_set((sv), (MEM_WRAP_CHECK_(n,char) \
1560 (char*)saferealloc((Malloc_t)SvPVX(sv), \
1561 (MEM_SIZE)((n))))); \
1564 =for apidoc Am|void|SvPV_shrink_to_cur|SV* sv
1566 Trim any trailing unused memory in the PV of C<sv>, which needs to have a real
1567 C<PV> that is unencumbered by things like COW. Think first before using this
1568 functionality. Is the space saving really worth giving up COW? Will the
1569 needed size of C<sv> stay the same?
1571 If the answers are both yes, then use L</C<SV_CHECK_THINKFIRST>> or
1572 L</C<SV_CHECK_THINKFIRST_COW_DROP>> before calling this.
1577 #define SvPV_shrink_to_cur(sv) STMT_START { \
1578 const STRLEN _lEnGtH = SvCUR(sv) + 1; \
1579 SvPV_renew(sv, _lEnGtH); \
1583 =for apidoc Am|void|SvPV_free|SV * sv
1585 Frees the PV buffer in C<sv>, leaving things in a precarious state, so should
1586 only be used as part of a larger operation
1590 #define SvPV_free(sv) \
1592 assert(SvTYPE(sv) >= SVt_PV); \
1594 assert(!SvROK(sv)); \
1595 if(UNLIKELY(SvOOK(sv))) { \
1597 SvOOK_offset(sv, zok); \
1598 SvPV_set(sv, SvPVX_mutable(sv) - zok); \
1599 SvFLAGS(sv) &= ~SVf_OOK; \
1601 Safefree(SvPVX(sv)); \
1606 /* Code that crops up in three places to take a scalar and ready it to hold
1608 # define prepare_SV_for_RV(sv) \
1610 if (SvTYPE(sv) < SVt_PV && SvTYPE(sv) != SVt_IV) \
1611 sv_upgrade(sv, SVt_IV); \
1612 else if (SvTYPE(sv) >= SVt_PV) { \
1621 # define BmFLAGS(sv) (SvTAIL(sv) ? FBMcf_TAIL : 0)
1624 #if defined (DEBUGGING) && defined(PERL_USE_GCC_BRACE_GROUPS)
1625 # define BmUSEFUL(sv) \
1626 (*({ SV *const _bmuseful = MUTABLE_SV(sv); \
1627 assert(SvTYPE(_bmuseful) >= SVt_PVIV); \
1628 assert(SvVALID(_bmuseful)); \
1629 assert(!SvIOK(_bmuseful)); \
1630 &(((XPVIV*) SvANY(_bmuseful))->xiv_u.xivu_iv); \
1633 # define BmUSEFUL(sv) ((XPVIV*) SvANY(sv))->xiv_u.xivu_iv
1638 # define BmRARE(sv) 0
1639 # define BmPREVIOUS(sv) 0
1642 #define FmLINES(sv) ((XPVIV*) SvANY(sv))->xiv_iv
1644 #define LvTYPE(sv) ((XPVLV*) SvANY(sv))->xlv_type
1645 #define LvTARG(sv) ((XPVLV*) SvANY(sv))->xlv_targ
1646 #define LvTARGOFF(sv) ((XPVLV*) SvANY(sv))->xlv_targoff
1647 #define LvSTARGOFF(sv) ((XPVLV*) SvANY(sv))->xlv_targoff_u.xlvu_stargoff
1648 #define LvTARGLEN(sv) ((XPVLV*) SvANY(sv))->xlv_targlen
1649 #define LvFLAGS(sv) ((XPVLV*) SvANY(sv))->xlv_flags
1651 #define LVf_NEG_OFF 0x1
1652 #define LVf_NEG_LEN 0x2
1653 #define LVf_OUT_OF_RANGE 0x4
1655 #define IoIFP(sv) (sv)->sv_u.svu_fp
1656 #define IoOFP(sv) ((XPVIO*) SvANY(sv))->xio_ofp
1657 #define IoDIRP(sv) ((XPVIO*) SvANY(sv))->xio_dirp
1658 #define IoANY(sv) ((XPVIO*) SvANY(sv))->xio_any
1659 #define IoLINES(sv) ((XPVIO*) SvANY(sv))->xiv_u.xivu_iv
1660 #define IoPAGE(sv) ((XPVIO*) SvANY(sv))->xio_page
1661 #define IoPAGE_LEN(sv) ((XPVIO*) SvANY(sv))->xio_page_len
1662 #define IoLINES_LEFT(sv)((XPVIO*) SvANY(sv))->xio_lines_left
1663 #define IoTOP_NAME(sv) ((XPVIO*) SvANY(sv))->xio_top_name
1664 #define IoTOP_GV(sv) ((XPVIO*) SvANY(sv))->xio_top_gv
1665 #define IoFMT_NAME(sv) ((XPVIO*) SvANY(sv))->xio_fmt_name
1666 #define IoFMT_GV(sv) ((XPVIO*) SvANY(sv))->xio_fmt_gv
1667 #define IoBOTTOM_NAME(sv)((XPVIO*) SvANY(sv))->xio_bottom_name
1668 #define IoBOTTOM_GV(sv) ((XPVIO*) SvANY(sv))->xio_bottom_gv
1669 #define IoTYPE(sv) ((XPVIO*) SvANY(sv))->xio_type
1670 #define IoFLAGS(sv) ((XPVIO*) SvANY(sv))->xio_flags
1672 /* IoTYPE(sv) is a single character telling the type of I/O connection. */
1673 #define IoTYPE_RDONLY '<'
1674 #define IoTYPE_WRONLY '>'
1675 #define IoTYPE_RDWR '+'
1676 #define IoTYPE_APPEND 'a'
1677 #define IoTYPE_PIPE '|'
1678 #define IoTYPE_STD '-' /* stdin or stdout */
1679 #define IoTYPE_SOCKET 's'
1680 #define IoTYPE_CLOSED ' '
1681 #define IoTYPE_IMPLICIT 'I' /* stdin or stdout or stderr */
1682 #define IoTYPE_NUMERIC '#' /* fdopen */
1685 =for apidoc_section $tainting
1686 =for apidoc Am|bool|SvTAINTED|SV* sv
1687 Checks to see if an SV is tainted. Returns TRUE if it is, FALSE if
1690 =for apidoc Am|void|SvTAINTED_on|SV* sv
1691 Marks an SV as tainted if tainting is enabled.
1693 =for apidoc Am|void|SvTAINTED_off|SV* sv
1694 Untaints an SV. Be I<very> careful with this routine, as it short-circuits
1695 some of Perl's fundamental security features. XS module authors should not
1696 use this function unless they fully understand all the implications of
1697 unconditionally untainting the value. Untainting should be done in the
1698 standard perl fashion, via a carefully crafted regexp, rather than directly
1699 untainting variables.
1701 =for apidoc Am|void|SvTAINT|SV* sv
1702 Taints an SV if tainting is enabled, and if some input to the current
1703 expression is tainted--usually a variable, but possibly also implicit
1704 inputs such as locale settings. C<SvTAINT> propagates that taintedness to
1705 the outputs of an expression in a pessimistic fashion; i.e., without paying
1706 attention to precisely which outputs are influenced by which inputs.
1711 #define sv_taint(sv) sv_magic((sv), NULL, PERL_MAGIC_taint, NULL, 0)
1713 #ifdef NO_TAINT_SUPPORT
1714 # define SvTAINTED(sv) 0
1716 # define SvTAINTED(sv) (SvMAGICAL(sv) && sv_tainted(sv))
1718 #define SvTAINTED_on(sv) STMT_START{ if(UNLIKELY(TAINTING_get)){sv_taint(sv);} }STMT_END
1719 #define SvTAINTED_off(sv) STMT_START{ if(UNLIKELY(TAINTING_get)){sv_untaint(sv);} }STMT_END
1721 #define SvTAINT(sv) \
1723 assert(TAINTING_get || !TAINT_get); \
1724 if (UNLIKELY(TAINT_get)) \
1729 =for apidoc_section $SV
1730 =for apidoc Am|char*|SvPV_force |SV* sv|STRLEN len
1731 =for apidoc_item ||SvPV_force_flags |SV * sv|STRLEN len|U32 flags
1732 =for apidoc_item ||SvPV_force_flags_mutable|SV * sv|STRLEN len|U32 flags
1733 =for apidoc_item ||SvPV_force_flags_nolen |SV * sv |U32 flags
1734 =for apidoc_item ||SvPV_force_mutable |SV * sv|STRLEN len
1735 =for apidoc_item ||SvPV_force_nolen |SV* sv
1736 =for apidoc_item ||SvPV_force_nomg |SV* sv|STRLEN len
1737 =for apidoc_item ||SvPV_force_nomg_nolen |SV * sv
1738 =for apidoc_item ||SvPVbyte_force |SV * sv|STRLEN len
1739 =for apidoc_item ||SvPVbytex_force |SV * sv|STRLEN len
1740 =for apidoc_item ||SvPVutf8_force |SV * sv|STRLEN len
1741 =for apidoc_item ||SvPVutf8x_force |SV * sv|STRLEN len
1742 =for apidoc_item ||SvPVx_force |SV* sv|STRLEN len
1744 These are like C<L</SvPV>>, returning the string in the SV, but will force the
1745 SV into containing a string (C<L</SvPOK>>), and only a string
1746 (C<L</SvPOK_only>>), by hook or by crook. You need to use one of these
1747 C<force> routines if you are going to update the C<L</SvPVX>> directly.
1749 Note that coercing an arbitrary scalar into a plain PV will potentially
1750 strip useful data from it. For example if the SV was C<SvROK>, then the
1751 referent will have its reference count decremented, and the SV itself may
1752 be converted to an C<SvPOK> scalar with a string buffer containing a value
1753 such as C<"ARRAY(0x1234)">.
1755 The differences between the forms are:
1757 The forms with C<flags> in their names allow you to use the C<flags> parameter
1758 to specify to perform 'get' magic (by setting the C<SV_GMAGIC> flag) or to skip
1759 'get' magic (by clearing it). The other forms do perform 'get' magic, except
1760 for the ones with C<nomg> in their names, which skip 'get' magic.
1762 The forms that take a C<len> parameter will set that variable to the byte
1763 length of the resultant string (these are macros, so don't use C<&len>).
1765 The forms with C<nolen> in their names indicate they don't have a C<len>
1766 parameter. They should be used only when it is known that the PV is a C
1767 string, terminated by a NUL byte, and without intermediate NUL characters; or
1768 when you don't care about its length.
1770 The forms with C<mutable> in their names are effectively the same as those without,
1771 but the name emphasizes that the string is modifiable by the caller, which it is
1774 C<SvPVutf8_force> is like C<SvPV_force>, but converts C<sv> to UTF-8 first if
1777 C<SvPVutf8x_force> is like C<SvPVutf8_force>, but guarantees to evaluate C<sv>
1778 only once; use the more efficient C<SvPVutf8_force> otherwise.
1780 C<SvPVbyte_force> is like C<SvPV_force>, but converts C<sv> to byte
1781 representation first if currently encoded as UTF-8. If the SV cannot be
1782 downgraded from UTF-8, this croaks.
1784 C<SvPVbytex_force> is like C<SvPVbyte_force>, but guarantees to evaluate C<sv>
1785 only once; use the more efficient C<SvPVbyte_force> otherwise.
1787 =for apidoc Am | char*|SvPV |SV* sv|STRLEN len
1788 =for apidoc_item |const char*|SvPV_const |SV* sv|STRLEN len
1789 =for apidoc_item | char*|SvPV_flags |SV* sv|STRLEN len|U32 flags
1790 =for apidoc_item |const char*|SvPV_flags_const |SV* sv|STRLEN len|U32 flags
1791 =for apidoc_item | char*|SvPV_flags_mutable |SV* sv|STRLEN len|U32 flags
1792 =for apidoc_item | char*|SvPV_mutable |SV* sv|STRLEN len
1793 =for apidoc_item | char*|SvPV_nolen |SV* sv
1794 =for apidoc_item |const char*|SvPV_nolen_const |SV* sv
1795 =for apidoc_item | char*|SvPV_nomg |SV* sv|STRLEN len
1796 =for apidoc_item |const char*|SvPV_nomg_const |SV* sv|STRLEN len
1797 =for apidoc_item |const char*|SvPV_nomg_const_nolen|SV* sv
1798 =for apidoc_item | char*|SvPV_nomg_nolen |SV* sv
1799 =for apidoc_item | char*|SvPVbyte |SV* sv|STRLEN len
1800 =for apidoc_item | char*|SvPVbyte_nolen |SV* sv
1801 =for apidoc_item | char*|SvPVbyte_nomg |SV* sv|STRLEN len
1802 =for apidoc_item | char*|SvPVbyte_or_null |SV* sv|STRLEN len
1803 =for apidoc_item | char*|SvPVbyte_or_null_nomg|SV* sv|STRLEN len
1804 =for apidoc_item | char*|SvPVbytex |SV* sv|STRLEN len
1805 =for apidoc_item | char*|SvPVbytex_nolen |SV* sv
1806 =for apidoc_item | char*|SvPVutf8 |SV* sv|STRLEN len
1807 =for apidoc_item | char*|SvPVutf8_nolen |SV* sv
1808 =for apidoc_item | char*|SvPVutf8_nomg |SV* sv|STRLEN len
1809 =for apidoc_item | char*|SvPVutf8_or_null |SV* sv|STRLEN len
1810 =for apidoc_item | char*|SvPVutf8_or_null_nomg|SV* sv|STRLEN len
1811 =for apidoc_item | char*|SvPVutf8x |SV* sv|STRLEN len
1812 =for apidoc_item | char*|SvPVx |SV* sv|STRLEN len
1813 =for apidoc_item |const char*|SvPVx_const |SV* sv|STRLEN len
1814 =for apidoc_item | char*|SvPVx_nolen |SV* sv
1815 =for apidoc_item |const char*|SvPVx_nolen_const |SV* sv
1817 These each return a pointer to the string in C<sv>, or a stringified form of
1818 C<sv> if it does not contain a string. The SV may cache the stringified
1819 version becoming C<SvPOK>.
1821 This is a very basic and common operation, so there are lots of slightly
1822 different versions of it.
1824 Note that there is no guarantee that the return value of C<SvPV(sv)>, for
1825 example, is equal to C<SvPVX(sv)>, or that C<SvPVX(sv)> contains valid data, or
1826 that successive calls to C<SvPV(sv)> (or another of these forms) will return
1827 the same pointer value each time. This is due to the way that things like
1828 overloading and Copy-On-Write are handled. In these cases, the return value
1829 may point to a temporary buffer or similar. If you absolutely need the
1830 C<SvPVX> field to be valid (for example, if you intend to write to it), then
1831 see C<L</SvPV_force>>.
1833 The differences between the forms are:
1835 The forms with neither C<byte> nor C<utf8> in their names (e.g., C<SvPV> or
1836 C<SvPV_nolen>) can expose the SV's internal string buffer. If
1837 that buffer consists entirely of bytes 0-255 and includes any bytes above
1838 127, then you B<MUST> consult C<SvUTF8> to determine the actual code points
1839 the string is meant to contain. Generally speaking, it is probably safer to
1840 prefer C<SvPVbyte>, C<SvPVutf8>, and the like. See
1841 L<perlguts/How do I pass a Perl string to a C library?> for more details.
1843 The forms with C<flags> in their names allow you to use the C<flags> parameter
1844 to specify to process 'get' magic (by setting the C<SV_GMAGIC> flag) or to skip
1845 'get' magic (by clearing it). The other forms process 'get' magic, except for
1846 the ones with C<nomg> in their names, which skip 'get' magic.
1848 The forms that take a C<len> parameter will set that variable to the byte
1849 length of the resultant string (these are macros, so don't use C<&len>).
1851 The forms with C<nolen> in their names indicate they don't have a C<len>
1852 parameter. They should be used only when it is known that the PV is a C
1853 string, terminated by a NUL byte, and without intermediate NUL characters; or
1854 when you don't care about its length.
1856 The forms with C<const> in their names return S<C<const char *>> so that the
1857 compiler will hopefully complain if you were to try to modify the contents of
1858 the string (unless you cast away const yourself).
1860 The other forms return a mutable pointer so that the string is modifiable by
1861 the caller; this is emphasized for the ones with C<mutable> in their names.
1863 As of 5.38, all forms are guaranteed to evaluate C<sv> exactly once. For
1864 earlier Perls, use a form whose name ends with C<x> for single evaluation.
1866 C<SvPVutf8> is like C<SvPV>, but converts C<sv> to UTF-8 first if not already
1867 UTF-8. Similarly, the other forms with C<utf8> in their names correspond to
1868 their respective forms without.
1870 C<SvPVutf8_or_null> and C<SvPVutf8_or_null_nomg> don't have corresponding
1871 non-C<utf8> forms. Instead they are like C<SvPVutf8_nomg>, but when C<sv> is
1872 undef, they return C<NULL>.
1874 C<SvPVbyte> is like C<SvPV>, but converts C<sv> to byte representation first if
1875 currently encoded as UTF-8. If C<sv> cannot be downgraded from UTF-8, it
1876 croaks. Similarly, the other forms with C<byte> in their names correspond to
1877 their respective forms without.
1879 C<SvPVbyte_or_null> doesn't have a corresponding non-C<byte> form. Instead it
1880 is like C<SvPVbyte>, but when C<sv> is undef, it returns C<NULL>.
1883 =for apidoc_item SvTRUE_NN
1884 =for apidoc_item SvTRUE_nomg
1885 =for apidoc_item SvTRUE_nomg_NN
1886 =for apidoc_item SvTRUEx
1888 These return a boolean indicating whether Perl would evaluate the SV as true or
1889 false. See C<L</SvOK>> for a defined/undefined test.
1891 As of Perl 5.32, all are guaranteed to evaluate C<sv> only once. Prior to that
1892 release, only C<SvTRUEx> guaranteed single evaluation; now C<SvTRUEx> is
1893 identical to C<SvTRUE>.
1895 C<SvTRUE_nomg> and C<TRUE_nomg_NN> do not perform 'get' magic; the others do
1896 unless the scalar is already C<SvPOK>, C<SvIOK>, or C<SvNOK> (the public, not
1899 C<SvTRUE_NN> is like C<L</SvTRUE>>, but C<sv> is assumed to be
1900 non-null (NN). If there is a possibility that it is NULL, use plain
1903 C<SvTRUE_nomg_NN> is like C<L</SvTRUE_nomg>>, but C<sv> is assumed to be
1904 non-null (NN). If there is a possibility that it is NULL, use plain
1907 =for apidoc Am|U32|SvIsCOW|SV* sv
1908 Returns a U32 value indicating whether the SV is Copy-On-Write (either shared
1909 hash key scalars, or full Copy On Write scalars if 5.9.0 is configured for
1912 =for apidoc Am|bool|SvIsCOW_shared_hash|SV* sv
1913 Returns a boolean indicating whether the SV is Copy-On-Write shared hash key
1919 /* To pass the action to the functions called by the following macros */
1925 SvPVutf8_pure_type_,
1931 /* When this code was written, embed.fnc could not handle function pointer
1932 * parameters; perhaps it still can't */
1933 #ifndef PERL_NO_INLINE_FUNCTIONS
1934 PERL_STATIC_INLINE char*
1935 Perl_SvPV_helper(pTHX_ SV *const sv, STRLEN *const lp, const U32 flags, const PL_SvPVtype type, char * (*non_trivial)(pTHX_ SV *, STRLEN * const, const U32), const bool or_null, const U32 return_flags);
1940 /* This test is "is there a cached PV that we can use directly?"
1942 * a) SVf_POK is true and there's definitely no get magic on the scalar
1943 * b) SVp_POK is true, there's no get magic, and we know that the cached PV
1944 * came from an IV conversion.
1945 * For the latter case, we don't set SVf_POK so that we can distinguish whether
1946 * the value originated as a string or as an integer, before we cached the
1947 * second representation. */
1948 #define SvPOK_or_cached_IV(sv) \
1949 (((SvFLAGS(sv) & (SVf_POK|SVs_GMG)) == SVf_POK) || ((SvFLAGS(sv) & (SVf_IOK|SVp_POK|SVs_GMG)) == (SVf_IOK|SVp_POK)))
1951 #define SvPV_flags(sv, len, flags) \
1952 Perl_SvPV_helper(aTHX_ sv, &len, flags, SvPVnormal_type_, \
1953 Perl_sv_2pv_flags, FALSE, 0)
1954 #define SvPV_flags_const(sv, len, flags) \
1955 ((const char*) Perl_SvPV_helper(aTHX_ sv, &len, flags, SvPVnormal_type_, \
1956 Perl_sv_2pv_flags, FALSE, \
1958 #define SvPV_flags_const_nolen(sv, flags) \
1959 ((const char*) Perl_SvPV_helper(aTHX_ sv, NULL, flags, SvPVnormal_type_, \
1960 Perl_sv_2pv_flags, FALSE, \
1962 #define SvPV_flags_mutable(sv, len, flags) \
1963 Perl_SvPV_helper(aTHX_ sv, &len, flags, SvPVnormal_type_, \
1964 Perl_sv_2pv_flags, FALSE, SV_MUTABLE_RETURN)
1966 #define SvPV_nolen(sv) \
1967 Perl_SvPV_helper(aTHX_ sv, NULL, SV_GMAGIC, SvPVnormal_type_, \
1968 Perl_sv_2pv_flags, FALSE, 0)
1970 #define SvPV_nolen_const(sv) SvPV_flags_const_nolen(sv, SV_GMAGIC)
1972 #define SvPV(sv, len) SvPV_flags(sv, len, SV_GMAGIC)
1973 #define SvPV_const(sv, len) SvPV_flags_const(sv, len, SV_GMAGIC)
1974 #define SvPV_mutable(sv, len) SvPV_flags_mutable(sv, len, SV_GMAGIC)
1976 #define SvPV_nomg_nolen(sv) \
1977 Perl_SvPV_helper(aTHX_ sv, NULL, 0, SvPVnormal_type_,Perl_sv_2pv_flags, \
1979 #define SvPV_nomg(sv, len) SvPV_flags(sv, len, 0)
1980 #define SvPV_nomg_const(sv, len) SvPV_flags_const(sv, len, 0)
1981 #define SvPV_nomg_const_nolen(sv) SvPV_flags_const_nolen(sv, 0)
1983 #define SvPV_force_flags(sv, len, flags) \
1984 Perl_SvPV_helper(aTHX_ sv, &len, flags, SvPVforce_type_, \
1985 Perl_sv_pvn_force_flags, FALSE, 0)
1986 #define SvPV_force_flags_nolen(sv, flags) \
1987 Perl_SvPV_helper(aTHX_ sv, NULL, flags, SvPVforce_type_, \
1988 Perl_sv_pvn_force_flags, FALSE, 0)
1989 #define SvPV_force_flags_mutable(sv, len, flags) \
1990 Perl_SvPV_helper(aTHX_ sv, &len, flags, SvPVforce_type_, \
1991 Perl_sv_pvn_force_flags, FALSE, SV_MUTABLE_RETURN)
1993 #define SvPV_force(sv, len) SvPV_force_flags(sv, len, SV_GMAGIC)
1994 #define SvPV_force_nolen(sv) SvPV_force_flags_nolen(sv, SV_GMAGIC)
1995 #define SvPV_force_mutable(sv, len) SvPV_force_flags_mutable(sv, len, SV_GMAGIC)
1997 /* "_nomg" in these defines means no mg_get() */
1998 #define SvPV_force_nomg(sv, len) SvPV_force_flags(sv, len, 0)
1999 #define SvPV_force_nomg_nolen(sv) SvPV_force_flags_nolen(sv, 0)
2001 #define SvPVutf8(sv, len) \
2002 Perl_SvPV_helper(aTHX_ sv, &len, SV_GMAGIC, SvPVutf8_type_, \
2003 Perl_sv_2pvutf8_flags, FALSE, 0)
2004 #define SvPVutf8_nomg(sv, len) \
2005 Perl_SvPV_helper(aTHX_ sv, &len, 0, SvPVutf8_type_, \
2006 Perl_sv_2pvutf8_flags, FALSE, 0)
2007 #define SvPVutf8_nolen(sv) \
2008 Perl_SvPV_helper(aTHX_ sv, NULL, SV_GMAGIC, SvPVutf8_type_, \
2009 Perl_sv_2pvutf8_flags, FALSE, 0)
2010 #define SvPVutf8_or_null(sv, len) \
2011 Perl_SvPV_helper(aTHX_ sv, &len, SV_GMAGIC, SvPVutf8_type_, \
2012 Perl_sv_2pvutf8_flags, TRUE, 0)
2013 #define SvPVutf8_or_null_nomg(sv, len) \
2014 Perl_SvPV_helper(aTHX_ sv, &len, 0, SvPVutf8_type_, \
2015 Perl_sv_2pvutf8_flags, TRUE, 0)
2017 #define SvPVbyte(sv, len) \
2018 Perl_SvPV_helper(aTHX_ sv, &len, SV_GMAGIC, SvPVbyte_type_, \
2019 Perl_sv_2pvbyte_flags, FALSE, 0)
2020 #define SvPVbyte_nomg(sv, len) \
2021 Perl_SvPV_helper(aTHX_ sv, &len, 0, SvPVbyte_type_, \
2022 Perl_sv_2pvbyte_flags, FALSE, 0)
2023 #define SvPVbyte_nolen(sv) \
2024 Perl_SvPV_helper(aTHX_ sv, NULL, SV_GMAGIC, SvPVbyte_type_, \
2025 Perl_sv_2pvbyte_flags, FALSE, 0)
2026 #define SvPVbyte_or_null(sv, len) \
2027 Perl_SvPV_helper(aTHX_ sv, &len, SV_GMAGIC, SvPVbyte_type_, \
2028 Perl_sv_2pvbyte_flags, TRUE, 0)
2029 #define SvPVbyte_or_null_nomg(sv, len) \
2030 Perl_SvPV_helper(aTHX_ sv, &len, 0, SvPVbyte_type_, \
2031 Perl_sv_2pvbyte_flags, TRUE, 0)
2033 #define SvPVutf8_force(sv, len) \
2034 Perl_SvPV_helper(aTHX_ sv, &len, 0, SvPVutf8_pure_type_, \
2035 Perl_sv_pvutf8n_force_wrapper, FALSE, 0)
2037 #define SvPVbyte_force(sv, len) \
2038 Perl_SvPV_helper(aTHX_ sv, &len, 0, SvPVbyte_pure_type_, \
2039 Perl_sv_pvbyten_force_wrapper, FALSE, 0)
2041 /* define FOOx(): Before FOO(x) was inlined, these were idempotent versions of
2044 #define SvPVx_force(sv, len) sv_pvn_force(sv, &len)
2045 #define SvPVutf8x_force(sv, len) sv_pvutf8n_force(sv, &len)
2046 #define SvPVbytex_force(sv, len) sv_pvbyten_force(sv, &len)
2048 #define SvTRUEx(sv) SvTRUE(sv)
2049 #define SvTRUEx_nomg(sv) SvTRUE_nomg(sv)
2050 #define SvTRUE_nomg_NN(sv) SvTRUE_common(sv, TRUE)
2052 # define SvIVx(sv) SvIV(sv)
2053 # define SvUVx(sv) SvUV(sv)
2054 # define SvNVx(sv) SvNV(sv)
2056 #if defined(PERL_USE_GCC_BRACE_GROUPS)
2058 # define SvPVx(sv, len) ({SV *_sv = (sv); SvPV(_sv, len); })
2059 # define SvPVx_const(sv, len) ({SV *_sv = (sv); SvPV_const(_sv, len); })
2060 # define SvPVx_nolen(sv) ({SV *_sv = (sv); SvPV_nolen(_sv); })
2061 # define SvPVx_nolen_const(sv) ({SV *_sv = (sv); SvPV_nolen_const(_sv); })
2062 # define SvPVutf8x(sv, len) ({SV *_sv = (sv); SvPVutf8(_sv, len); })
2063 # define SvPVbytex(sv, len) ({SV *_sv = (sv); SvPVbyte(_sv, len); })
2064 # define SvPVbytex_nolen(sv) ({SV *_sv = (sv); SvPVbyte_nolen(_sv); })
2066 #else /* __GNUC__ */
2068 /* These inlined macros use globals, which will require a thread
2069 * declaration in user code, so we avoid them under threads */
2071 # define SvPVx(sv, len) ((PL_Sv = (sv)), SvPV(PL_Sv, len))
2072 # define SvPVx_const(sv, len) ((PL_Sv = (sv)), SvPV_const(PL_Sv, len))
2073 # define SvPVx_nolen(sv) ((PL_Sv = (sv)), SvPV_nolen(PL_Sv))
2074 # define SvPVx_nolen_const(sv) ((PL_Sv = (sv)), SvPV_nolen_const(PL_Sv))
2075 # define SvPVutf8x(sv, len) ((PL_Sv = (sv)), SvPVutf8(PL_Sv, len))
2076 # define SvPVbytex(sv, len) ((PL_Sv = (sv)), SvPVbyte(PL_Sv, len))
2077 # define SvPVbytex_nolen(sv) ((PL_Sv = (sv)), SvPVbyte_nolen(PL_Sv))
2078 #endif /* __GNU__ */
2080 #define SvIsCOW(sv) (SvFLAGS(sv) & SVf_IsCOW)
2081 #define SvIsCOW_on(sv) (SvFLAGS(sv) |= SVf_IsCOW)
2082 #define SvIsCOW_off(sv) (SvFLAGS(sv) &= ~(SVf_IsCOW|SVppv_STATIC))
2083 #define SvIsCOW_shared_hash(sv) ((SvFLAGS(sv) & (SVf_IsCOW|SVppv_STATIC)) == (SVf_IsCOW) && SvLEN(sv) == 0)
2084 #define SvIsCOW_static(sv) ((SvFLAGS(sv) & (SVf_IsCOW|SVppv_STATIC)) == (SVf_IsCOW|SVppv_STATIC))
2086 #define SvSHARED_HEK_FROM_PV(pvx) \
2087 ((struct hek*)(pvx - STRUCT_OFFSET(struct hek, hek_key)))
2089 =for apidoc Am|struct hek*|SvSHARED_HASH|SV * sv
2090 Returns the hash for C<sv> created by C<L</newSVpvn_share>>.
2094 #define SvSHARED_HASH(sv) (0 + SvSHARED_HEK_FROM_PV(SvPVX_const(sv))->hek_hash)
2096 /* flag values for sv_*_flags functions */
2097 #define SV_UTF8_NO_ENCODING 0 /* No longer used */
2100 =for apidoc AmnhD||SV_UTF8_NO_ENCODING
2105 /* Flags used as `U32 flags` arguments to various functions */
2106 #define SV_IMMEDIATE_UNREF (1 << 0) /* 0x0001 - 1 */
2107 #define SV_GMAGIC (1 << 1) /* 0x0002 - 2 */
2108 #define SV_COW_DROP_PV (1 << 2) /* 0x0004 - 4 */
2109 /* SV_NOT_USED (1 << 3) 0x0008 - 8 */
2110 #define SV_NOSTEAL (1 << 4) /* 0x0010 - 16 */
2111 #define SV_CONST_RETURN (1 << 5) /* 0x0020 - 32 */
2112 #define SV_MUTABLE_RETURN (1 << 6) /* 0x0040 - 64 */
2113 #define SV_SMAGIC (1 << 7) /* 0x0080 - 128 */
2114 #define SV_HAS_TRAILING_NUL (1 << 8) /* 0x0100 - 256 */
2115 #define SV_COW_SHARED_HASH_KEYS (1 << 9) /* 0x0200 - 512 */
2116 /* This one is only enabled for PERL_OLD_COPY_ON_WRITE */
2117 /* XXX This flag actually enabled for any COW. But it appears not to do
2118 anything. Can we just remove it? Or will it serve some future
2120 #define SV_COW_OTHER_PVS (1 << 10) /* 0x0400 - 1024 */
2121 /* Make sv_2pv_flags return NULL if something is undefined. */
2122 #define SV_UNDEF_RETURNS_NULL (1 << 11) /* 0x0800 - 2048 */
2123 /* Tell sv_utf8_upgrade() to not check to see if an upgrade is really needed.
2124 * This is used when the caller has already determined it is, and avoids
2126 #define SV_FORCE_UTF8_UPGRADE (1 << 12) /* 0x1000 - 4096 */
2127 /* if (after resolving magic etc), the SV is found to be overloaded,
2128 * don't call the overload magic, just return as-is */
2129 #define SV_SKIP_OVERLOAD (1 << 13) /* 0x2000 - 8192 */
2130 #define SV_CATBYTES (1 << 14) /* 0x4000 - 16384 */
2131 #define SV_CATUTF8 (1 << 15) /* 0x8000 - 32768 */
2133 /* The core is safe for this COW optimisation. XS code on CPAN may not be.
2134 So only default to doing the COW setup if we're in the core.
2137 # ifndef SV_DO_COW_SVSETSV
2138 # define SV_DO_COW_SVSETSV SV_COW_SHARED_HASH_KEYS|SV_COW_OTHER_PVS
2142 #ifndef SV_DO_COW_SVSETSV
2143 # define SV_DO_COW_SVSETSV 0
2147 #define sv_unref(sv) sv_unref_flags(sv, 0)
2148 #define sv_force_normal(sv) sv_force_normal_flags(sv, 0)
2149 #define sv_usepvn(sv, p, l) sv_usepvn_flags(sv, p, l, 0)
2150 #define sv_usepvn_mg(sv, p, l) sv_usepvn_flags(sv, p, l, SV_SMAGIC)
2153 =for apidoc Am|void|SV_CHECK_THINKFIRST_COW_DROP|SV * sv
2155 Call this when you are about to replace the PV value in C<sv>, which is
2156 potentially copy-on-write. It stops any sharing with other SVs, so that no
2157 Copy on Write (COW) actually happens. This COW would be useless, as it would
2158 immediately get changed to something else. This function also removes any
2159 other encumbrances that would be problematic when changing C<sv>.
2164 #define SV_CHECK_THINKFIRST_COW_DROP(sv) if (SvTHINKFIRST(sv)) \
2165 sv_force_normal_flags(sv, SV_COW_DROP_PV)
2167 #ifdef PERL_COPY_ON_WRITE
2168 # define SvCANCOW(sv) \
2170 ? SvLEN(sv) ? CowREFCNT(sv) != SV_COW_REFCNT_MAX : 1 \
2171 : (SvFLAGS(sv) & CAN_COW_MASK) == CAN_COW_FLAGS \
2172 && SvCUR(sv)+1 < SvLEN(sv))
2173 /* Note: To allow 256 COW "copies", a refcnt of 0 means 1. */
2174 # define CowREFCNT(sv) (*(U8 *)(SvPVX(sv)+SvLEN(sv)-1))
2175 # define SV_COW_REFCNT_MAX nBIT_UMAX(sizeof(U8) * CHARBITS)
2176 # define CAN_COW_MASK (SVf_POK|SVf_ROK|SVp_POK|SVf_FAKE| \
2177 SVf_OOK|SVf_BREAK|SVf_READONLY|SVf_PROTECT)
2180 #define CAN_COW_FLAGS (SVp_POK|SVf_POK)
2183 =for apidoc Am|void|SV_CHECK_THINKFIRST|SV * sv
2185 Remove any encumbrances from C<sv>, that need to be taken care of before it
2186 is modifiable. For example if it is Copy on Write (COW), now is the time to
2189 If you know that you are about to change the PV value of C<sv>, instead use
2190 L</C<SV_CHECK_THINKFIRST_COW_DROP>> to avoid the write that would be
2191 immediately written again.
2195 #define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) \
2196 sv_force_normal_flags(sv, 0)
2199 /* all these 'functions' are now just macros */
2201 #define sv_pv(sv) SvPV_nolen(sv)
2202 #define sv_pvutf8(sv) SvPVutf8_nolen(sv)
2203 #define sv_pvbyte(sv) SvPVbyte_nolen(sv)
2205 #define sv_pvn_force_nomg(sv, lp) sv_pvn_force_flags(sv, lp, 0)
2206 #define sv_utf8_upgrade_flags(sv, flags) sv_utf8_upgrade_flags_grow(sv, flags, 0)
2207 #define sv_utf8_upgrade_nomg(sv) sv_utf8_upgrade_flags(sv, 0)
2208 #define sv_utf8_downgrade(sv, fail_ok) sv_utf8_downgrade_flags(sv, fail_ok, SV_GMAGIC)
2209 #define sv_utf8_downgrade_nomg(sv, fail_ok) sv_utf8_downgrade_flags(sv, fail_ok, 0)
2210 #define sv_catpvn_nomg(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, 0)
2211 #define sv_catpv_nomg(dsv, sstr) sv_catpv_flags(dsv, sstr, 0)
2212 #define sv_setsv(dsv, ssv) \
2213 sv_setsv_flags(dsv, ssv, SV_GMAGIC|SV_DO_COW_SVSETSV)
2214 #define sv_setsv_nomg(dsv, ssv) sv_setsv_flags(dsv, ssv, SV_DO_COW_SVSETSV)
2215 #define sv_catsv(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC)
2216 #define sv_catsv_nomg(dsv, ssv) sv_catsv_flags(dsv, ssv, 0)
2217 #define sv_catsv_mg(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC|SV_SMAGIC)
2218 #define sv_catpvn(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC)
2219 #define sv_catpvn_mg(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC|SV_SMAGIC);
2220 #define sv_copypv(dsv, ssv) sv_copypv_flags(dsv, ssv, SV_GMAGIC)
2221 #define sv_copypv_nomg(dsv, ssv) sv_copypv_flags(dsv, ssv, 0)
2222 #define sv_2pv(sv, lp) sv_2pv_flags(sv, lp, SV_GMAGIC)
2223 #define sv_2pv_nolen(sv) sv_2pv(sv, 0)
2224 #define sv_2pvbyte(sv, lp) sv_2pvbyte_flags(sv, lp, SV_GMAGIC)
2225 #define sv_2pvbyte_nolen(sv) sv_2pvbyte(sv, 0)
2226 #define sv_2pvutf8(sv, lp) sv_2pvutf8_flags(sv, lp, SV_GMAGIC)
2227 #define sv_2pvutf8_nolen(sv) sv_2pvutf8(sv, 0)
2228 #define sv_2pv_nomg(sv, lp) sv_2pv_flags(sv, lp, 0)
2229 #define sv_pvn_force(sv, lp) sv_pvn_force_flags(sv, lp, SV_GMAGIC)
2230 #define sv_utf8_upgrade(sv) sv_utf8_upgrade_flags(sv, SV_GMAGIC)
2231 #define sv_2iv(sv) sv_2iv_flags(sv, SV_GMAGIC)
2232 #define sv_2uv(sv) sv_2uv_flags(sv, SV_GMAGIC)
2233 #define sv_2nv(sv) sv_2nv_flags(sv, SV_GMAGIC)
2234 #define sv_eq(sv1, sv2) sv_eq_flags(sv1, sv2, SV_GMAGIC)
2235 #define sv_cmp(sv1, sv2) sv_cmp_flags(sv1, sv2, SV_GMAGIC)
2236 #define sv_cmp_locale(sv1, sv2) sv_cmp_locale_flags(sv1, sv2, SV_GMAGIC)
2237 #define sv_numeq(sv1, sv2) sv_numeq_flags(sv1, sv2, SV_GMAGIC)
2238 #define sv_streq(sv1, sv2) sv_streq_flags(sv1, sv2, SV_GMAGIC)
2239 #define sv_collxfrm(sv, nxp) sv_collxfrm_flags(sv, nxp, SV_GMAGIC)
2240 #define sv_2bool(sv) sv_2bool_flags(sv, SV_GMAGIC)
2241 #define sv_2bool_nomg(sv) sv_2bool_flags(sv, 0)
2242 #define sv_insert(bigstr, offset, len, little, littlelen) \
2243 Perl_sv_insert_flags(aTHX_ (bigstr),(offset), (len), (little), \
2244 (littlelen), SV_GMAGIC)
2245 #define sv_mortalcopy(sv) \
2246 Perl_sv_mortalcopy_flags(aTHX_ sv, SV_GMAGIC|SV_DO_COW_SVSETSV)
2247 #define sv_cathek(sv,hek) \
2249 HEK * const bmxk = hek; \
2250 sv_catpvn_flags(sv, HEK_KEY(bmxk), HEK_LEN(bmxk), \
2251 HEK_UTF8(bmxk) ? SV_CATUTF8 : SV_CATBYTES); \
2254 /* Should be named SvCatPVN_utf8_upgrade? */
2255 #define sv_catpvn_nomg_utf8_upgrade(dsv, sstr, slen, nsv) \
2258 nsv = newSVpvn_flags(sstr, slen, SVs_TEMP); \
2260 sv_setpvn(nsv, sstr, slen); \
2262 sv_utf8_upgrade(nsv); \
2263 sv_catsv_nomg(dsv, nsv); \
2265 #define sv_catpvn_nomg_maybeutf8(dsv, sstr, len, is_utf8) \
2266 sv_catpvn_flags(dsv, sstr, len, (is_utf8)?SV_CATUTF8:SV_CATBYTES)
2268 #if defined(PERL_CORE) || defined(PERL_EXT)
2269 # define sv_or_pv_len_utf8(sv, pv, bytelen) \
2271 ? utf8_length((U8 *)(pv), (U8 *)(pv)+(bytelen)) \
2277 =for apidoc_item ||newRV_inc|
2279 These are identical. They create an RV wrapper for an SV. The reference count
2280 for the original SV is incremented.
2285 #define newRV_inc(sv) newRV(sv)
2287 /* the following macros update any magic values this C<sv> is associated with */
2290 =for apidoc_section $SV
2292 =for apidoc Am|void|SvSETMAGIC|SV* sv
2293 Invokes C<L</mg_set>> on an SV if it has 'set' magic. This is necessary
2294 after modifying a scalar, in case it is a magical variable like C<$|>
2295 or a tied variable (it calls C<STORE>). This macro evaluates its
2296 argument more than once.
2298 =for apidoc Am|void|SvSetMagicSV|SV* dsv|SV* ssv
2299 =for apidoc_item SvSetMagicSV_nosteal
2300 =for apidoc_item SvSetSV
2301 =for apidoc_item SvSetSV_nosteal
2303 if C<dsv> is the same as C<ssv>, these do nothing. Otherwise they all call
2304 some form of C<L</sv_setsv>>. They may evaluate their arguments more than
2307 The only differences are:
2309 C<SvSetMagicSV> and C<SvSetMagicSV_nosteal> perform any required 'set' magic
2310 afterwards on the destination SV; C<SvSetSV> and C<SvSetSV_nosteal> do not.
2312 C<SvSetSV_nosteal> C<SvSetMagicSV_nosteal> call a non-destructive version of
2315 =for apidoc Am|void|SvSHARE|SV* sv
2316 Arranges for C<sv> to be shared between threads if a suitable module
2319 =for apidoc Am|void|SvLOCK|SV* sv
2320 Arranges for a mutual exclusion lock to be obtained on C<sv> if a suitable module
2323 =for apidoc Am|void|SvUNLOCK|SV* sv
2324 Releases a mutual exclusion lock on C<sv> if a suitable module
2327 =for apidoc_section $SV
2329 =for apidoc Am|char *|SvGROW|SV* sv|STRLEN len
2330 Expands the character buffer in the SV so that it has room for the
2331 indicated number of bytes (remember to reserve space for an extra trailing
2332 C<NUL> character). Calls C<sv_grow> to perform the expansion if necessary.
2333 Returns a pointer to the character
2334 buffer. SV must be of type >= C<SVt_PV>. One
2335 alternative is to call C<sv_grow> if you are not sure of the type of SV.
2337 You might mistakenly think that C<len> is the number of bytes to add to the
2338 existing size, but instead it is the total size C<sv> should be.
2340 =for apidoc Am|char *|SvPVCLEAR|SV* sv
2341 Ensures that sv is a SVt_PV and that its SvCUR is 0, and that it is
2342 properly null terminated. Equivalent to sv_setpvs(""), but more efficient.
2344 =for apidoc Am|char *|SvPVCLEAR_FRESH|SV* sv
2346 Like SvPVCLEAR, but optimized for newly-minted SVt_PV/PVIV/PVNV/PVMG
2347 that already have a PV buffer allocated, but no SvTHINKFIRST.
2352 #define SvPVCLEAR(sv) sv_setpv_bufsize(sv,0,0)
2353 #define SvPVCLEAR_FRESH(sv) sv_setpv_freshbuf(sv)
2354 #define SvSHARE(sv) PL_sharehook(aTHX_ sv)
2355 #define SvLOCK(sv) PL_lockhook(aTHX_ sv)
2356 #define SvUNLOCK(sv) PL_unlockhook(aTHX_ sv)
2357 #define SvDESTROYABLE(sv) PL_destroyhook(aTHX_ sv)
2359 #define SvSETMAGIC(x) STMT_START { if (UNLIKELY(SvSMAGICAL(x))) mg_set(x); } STMT_END
2361 #define SvSetSV_and(dst,src,finally) \
2365 if (LIKELY((dst_) != (src_))) { \
2366 sv_setsv(dst_, src_); \
2371 #define SvSetSV_nosteal_and(dst,src,finally) \
2375 if (LIKELY((dst_) != (src_))) { \
2376 sv_setsv_flags(dst_, src_, \
2379 | SV_DO_COW_SVSETSV); \
2384 #define SvSetSV(dst,src) \
2385 SvSetSV_and(dst,src,/*nothing*/;)
2386 #define SvSetSV_nosteal(dst,src) \
2387 SvSetSV_nosteal_and(dst,src,/*nothing*/;)
2389 #define SvSetMagicSV(dst,src) \
2390 SvSetSV_and(dst,src,SvSETMAGIC(dst))
2391 #define SvSetMagicSV_nosteal(dst,src) \
2392 SvSetSV_nosteal_and(dst,src,SvSETMAGIC(dst))
2395 #if !defined(SKIP_DEBUGGING)
2396 #define SvPEEK(sv) sv_peek(sv)
2398 #define SvPEEK(sv) ""
2401 /* Is this a per-interpreter immortal SV (rather than global)?
2402 * These should either occupy adjacent entries in the interpreter struct
2403 * (MULTIPLICITY) or adjacent elements of PL_sv_immortals[] otherwise.
2404 * The unsigned (Size_t) cast avoids the need for a second < 0 condition.
2406 #define SvIMMORTAL_INTERP(sv) ((Size_t)((sv) - &PL_sv_yes) < 4)
2408 /* Does this immortal have a true value? Currently only PL_sv_yes does. */
2409 #define SvIMMORTAL_TRUE(sv) ((sv) == &PL_sv_yes)
2411 /* the SvREADONLY() test is to quickly reject most SVs */
2412 #define SvIMMORTAL(sv) \
2414 && (SvIMMORTAL_INTERP(sv) || (sv) == &PL_sv_placeholder))
2417 /* exercise the immortal resurrection code in sv_free2() */
2418 # ifdef PERL_RC_STACK
2419 /* When the stack is ref-counted, the code tends to take a lot of
2420 * short cuts with immortals, such as skipping the bump of the ref
2421 * count of PL_sv_undef when pushing it on the stack. Exercise that
2422 * this doesn't cause problems, especially on code which
2423 * special-cases RC==1 etc.
2425 # define SvREFCNT_IMMORTAL 10
2427 # define SvREFCNT_IMMORTAL 1000
2430 # define SvREFCNT_IMMORTAL ((~(U32)0)/2)
2434 =for apidoc Am|SV *|boolSV|bool b
2436 Returns a true SV if C<b> is a true value, or a false SV if C<b> is 0.
2438 See also C<L</PL_sv_yes>> and C<L</PL_sv_no>>.
2443 #define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
2446 =for apidoc Am|void|sv_setbool|SV *sv|bool b
2447 =for apidoc_item |void|sv_setbool_mg|SV *sv|bool b
2449 These set an SV to a true or false boolean value, upgrading first if necessary.
2451 They differ only in that C<sv_setbool_mg> handles 'set' magic; C<sv_setbool>
2457 #define sv_setbool(sv, b) sv_setsv(sv, boolSV(b))
2458 #define sv_setbool_mg(sv, b) sv_setsv_mg(sv, boolSV(b))
2460 #define isGV(sv) (SvTYPE(sv) == SVt_PVGV)
2461 /* If I give every macro argument a different name, then there won't be bugs
2462 where nested macros get confused. Been there, done that. */
2464 =for apidoc Am|bool|isGV_with_GP|SV * sv
2465 Returns a boolean as to whether or not C<sv> is a GV with a pointer to a GP
2470 #define isGV_with_GP(pwadak) \
2471 (((SvFLAGS(pwadak) & (SVp_POK|SVpgv_GP)) == SVpgv_GP) \
2472 && (SvTYPE(pwadak) == SVt_PVGV || SvTYPE(pwadak) == SVt_PVLV))
2474 #define isGV_with_GP_on(sv) \
2476 SV * sv_ = MUTABLE_SV(sv); \
2477 assert (SvTYPE(sv_) == SVt_PVGV || SvTYPE(sv_) == SVt_PVLV); \
2478 assert (!SvPOKp(sv_)); \
2479 assert (!SvIOKp(sv_)); \
2480 (SvFLAGS(sv_) |= SVpgv_GP); \
2483 #define isGV_with_GP_off(sv) \
2485 SV * sv_ = MUTABLE_SV(sv); \
2486 assert (SvTYPE(sv_) == SVt_PVGV || SvTYPE(sv_) == SVt_PVLV); \
2487 assert (!SvPOKp(sv_)); \
2488 assert (!SvIOKp(sv_)); \
2489 (SvFLAGS(sv_) &= ~SVpgv_GP); \
2493 # define isGV_or_RVCV(kadawp) \
2494 (isGV(kadawp) || (SvROK(kadawp) && SvTYPE(SvRV(kadawp)) == SVt_PVCV))
2496 #define isREGEXP(sv) \
2497 (SvTYPE(sv) == SVt_REGEXP \
2498 || (SvFLAGS(sv) & (SVTYPEMASK|SVpgv_GP|SVf_FAKE)) \
2499 == (SVt_PVLV|SVf_FAKE))
2503 # define SvGROW(sv,len) \
2504 (SvIsCOW(sv) || SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
2506 # define SvGROW(sv,len) (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
2508 #define SvGROW_mutable(sv,len) \
2509 (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX_mutable(sv))
2510 #define Sv_Grow sv_grow
2512 #define CLONEf_COPY_STACKS 1
2513 #define CLONEf_KEEP_PTR_TABLE 2
2514 #define CLONEf_CLONE_HOST 4
2515 #define CLONEf_JOIN_IN 8
2517 struct clone_params {
2520 PerlInterpreter *proto_perl;
2521 PerlInterpreter *new_perl;
2525 /* SV_NOSTEAL prevents TEMP buffers being, well, stolen, and saves games
2526 with SvTEMP_off and SvTEMP_on round a call to sv_setsv. */
2527 #define newSVsv(sv) newSVsv_flags((sv), SV_GMAGIC|SV_NOSTEAL)
2528 #define newSVsv_nomg(sv) newSVsv_flags((sv), SV_NOSTEAL)
2531 =for apidoc Am|SV*|newSVpvn_utf8|const char* s|STRLEN len|U32 utf8
2533 Creates a new SV and copies a string (which may contain C<NUL> (C<\0>)
2534 characters) into it. If C<utf8> is true, calls
2535 C<SvUTF8_on> on the new SV. Implemented as a wrapper around C<newSVpvn_flags>.
2540 #define newSVpvn_utf8(s, len, u) newSVpvn_flags((s), (len), (u) ? SVf_UTF8 : 0)
2543 =for apidoc Amx|SV*|newSVpadname|PADNAME *pn
2545 Creates a new SV containing the pad name.
2550 #define newSVpadname(pn) newSVpvn_utf8(PadnamePV(pn), PadnameLEN(pn), TRUE)
2553 =for apidoc Am|void|SvOOK_offset|SV*sv|STRLEN len
2555 Reads into C<len> the offset from C<SvPVX> back to the true start of the
2556 allocated buffer, which will be non-zero if C<sv_chop> has been used to
2557 efficiently remove characters from start of the buffer. Implemented as a
2558 macro, which takes the address of C<len>, which must be of type C<STRLEN>.
2559 Evaluates C<sv> more than once. Sets C<len> to 0 if C<SvOOK(sv)> is false.
2565 /* Does the bot know something I don't?
2566 10:28 <@Nicholas> metabatman
2567 10:28 <+meta> Nicholas: crash
2569 # define SvOOK_offset(sv, offset) STMT_START { \
2570 STATIC_ASSERT_STMT(sizeof(offset) == sizeof(STRLEN)); \
2572 const U8 *_crash = (U8*)SvPVX_const(sv); \
2573 (offset) = *--_crash; \
2575 _crash -= sizeof(STRLEN); \
2576 Copy(_crash, (U8 *)&(offset), sizeof(STRLEN), U8); \
2579 /* Validate the preceding buffer's sentinels to \
2580 verify that no-one is using it. */ \
2581 const U8 *const _bonk = (U8*)SvPVX_const(sv) - (offset);\
2582 while (_crash > _bonk) { \
2584 assert (*_crash == (U8)PTR2UV(_crash)); \
2592 /* This is the same code, but avoids using any temporary variables: */
2593 # define SvOOK_offset(sv, offset) STMT_START { \
2594 STATIC_ASSERT_STMT(sizeof(offset) == sizeof(STRLEN)); \
2596 (offset) = ((U8*)SvPVX_const(sv))[-1]; \
2598 Copy(SvPVX_const(sv) - 1 - sizeof(STRLEN), \
2599 (U8*)&(offset), sizeof(STRLEN), U8); \
2608 =for apidoc_section $io
2611 Create a new IO, setting the reference count to 1.
2615 #define newIO() MUTABLE_IO(newSV_type(SVt_PVIO))
2617 #if defined(PERL_CORE) || defined(PERL_EXT)
2619 # define SV_CONST(name) \
2620 PL_sv_consts[SV_CONST_##name] \
2621 ? PL_sv_consts[SV_CONST_##name] \
2622 : (PL_sv_consts[SV_CONST_##name] = newSVpv_share(#name, 0))
2624 # define SV_CONST_TIESCALAR 0
2625 # define SV_CONST_TIEARRAY 1
2626 # define SV_CONST_TIEHASH 2
2627 # define SV_CONST_TIEHANDLE 3
2629 # define SV_CONST_FETCH 4
2630 # define SV_CONST_FETCHSIZE 5
2631 # define SV_CONST_STORE 6
2632 # define SV_CONST_STORESIZE 7
2633 # define SV_CONST_EXISTS 8
2635 # define SV_CONST_PUSH 9
2636 # define SV_CONST_POP 10
2637 # define SV_CONST_SHIFT 11
2638 # define SV_CONST_UNSHIFT 12
2639 # define SV_CONST_SPLICE 13
2640 # define SV_CONST_EXTEND 14
2642 # define SV_CONST_FIRSTKEY 15
2643 # define SV_CONST_NEXTKEY 16
2644 # define SV_CONST_SCALAR 17
2646 # define SV_CONST_OPEN 18
2647 # define SV_CONST_WRITE 19
2648 # define SV_CONST_PRINT 20
2649 # define SV_CONST_PRINTF 21
2650 # define SV_CONST_READ 22
2651 # define SV_CONST_READLINE 23
2652 # define SV_CONST_GETC 24
2653 # define SV_CONST_SEEK 25
2654 # define SV_CONST_TELL 26
2655 # define SV_CONST_EOF 27
2656 # define SV_CONST_BINMODE 28
2657 # define SV_CONST_FILENO 29
2658 # define SV_CONST_CLOSE 30
2660 # define SV_CONST_DELETE 31
2661 # define SV_CONST_CLEAR 32
2662 # define SV_CONST_UNTIE 33
2663 # define SV_CONST_DESTROY 34
2666 #define SV_CONSTS_COUNT 35
2669 * Bodyless IVs and NVs!
2671 * Since 5.9.2, we can avoid allocating a body for SVt_IV-type SVs.
2672 * Since the larger IV-holding variants of SVs store their integer
2673 * values in their respective bodies, the family of SvIV() accessor
2674 * macros would naively have to branch on the SV type to find the
2675 * integer value either in the HEAD or BODY. In order to avoid this
2676 * expensive branch, a clever soul has deployed a great hack:
2677 * We set up the SvANY pointer such that instead of pointing to a
2678 * real body, it points into the memory before the location of the
2679 * head. We compute this pointer such that the location of
2680 * the integer member of the hypothetical body struct happens to
2681 * be the same as the location of the integer member of the bodyless
2682 * SV head. This now means that the SvIV() family of accessors can
2683 * always read from the (hypothetical or real) body via SvANY.
2685 * Since the 5.21 dev series, we employ the same trick for NVs
2686 * if the architecture can support it (NVSIZE <= IVSIZE).
2689 /* The following two macros compute the necessary offsets for the above
2690 * trick and store them in SvANY for SvIV() (and friends) to use. */
2692 # define SET_SVANY_FOR_BODYLESS_IV(sv) \
2694 SV * sv_ = MUTABLE_SV(sv); \
2695 SvANY(sv_) = (XPVIV*)((char*)&(sv_->sv_u.svu_iv) \
2696 - STRUCT_OFFSET(XPVIV, xiv_iv)); \
2699 # define SET_SVANY_FOR_BODYLESS_NV(sv) \
2701 SV * sv_ = MUTABLE_SV(sv); \
2702 SvANY(sv_) = (XPVNV*)((char*)&(sv_->sv_u.svu_nv) \
2703 - STRUCT_OFFSET(XPVNV, xnv_u.xnv_nv)); \
2706 #if defined(PERL_CORE) && defined(USE_ITHREADS)
2707 /* Certain cases in Perl_ss_dup have been merged, by relying on the fact
2708 that currently av_dup, gv_dup and hv_dup are the same as sv_dup.
2709 If this changes, please unmerge ss_dup.
2710 Likewise, sv_dup_inc_multiple() relies on this fact. */
2711 # define sv_dup_inc_NN(s,t) SvREFCNT_inc_NN(sv_dup_inc(s,t))
2712 # define av_dup(s,t) MUTABLE_AV(sv_dup((const SV *)s,t))
2713 # define av_dup_inc(s,t) MUTABLE_AV(sv_dup_inc((const SV *)s,t))
2714 # define hv_dup(s,t) MUTABLE_HV(sv_dup((const SV *)s,t))
2715 # define hv_dup_inc(s,t) MUTABLE_HV(sv_dup_inc((const SV *)s,t))
2716 # define cv_dup(s,t) MUTABLE_CV(sv_dup((const SV *)s,t))
2717 # define cv_dup_inc(s,t) MUTABLE_CV(sv_dup_inc((const SV *)s,t))
2718 # define io_dup(s,t) MUTABLE_IO(sv_dup((const SV *)s,t))
2719 # define io_dup_inc(s,t) MUTABLE_IO(sv_dup_inc((const SV *)s,t))
2720 # define gv_dup(s,t) MUTABLE_GV(sv_dup((const SV *)s,t))
2721 # define gv_dup_inc(s,t) MUTABLE_GV(sv_dup_inc((const SV *)s,t))
2725 * ex: set ts=8 sts=4 sw=4 et: