3 * Copyright (c) 1991-2003, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
8 * "I wonder what the Entish is for 'yes' and 'no'," he thought.
11 * This file contains the code that creates, manipulates and destroys
12 * scalar values (SVs). The other types (AV, HV, GV, etc.) reuse the
13 * structure of an SV, so their creation and destruction is handled
14 * here; higher-level functions are in av.c, hv.c, and so on. Opcode
15 * level functions (eg. substr, split, join) for each of the types are
26 #ifdef PERL_COPY_ON_WRITE
27 #define SV_COW_NEXT_SV(sv) INT2PTR(SV *,SvUVX(sv))
28 #define SV_COW_NEXT_SV_SET(current,next) SvUVX(current) = PTR2UV(next)
29 /* This is a pessimistic view. Scalar must be purely a read-write PV to copy-
33 /* ============================================================================
35 =head1 Allocation and deallocation of SVs.
37 An SV (or AV, HV, etc.) is allocated in two parts: the head (struct sv,
38 av, hv...) contains type and reference count information, as well as a
39 pointer to the body (struct xrv, xpv, xpviv...), which contains fields
40 specific to each type.
42 Normally, this allocation is done using arenas, which are approximately
43 1K chunks of memory parcelled up into N heads or bodies. The first slot
44 in each arena is reserved, and is used to hold a link to the next arena.
45 In the case of heads, the unused first slot also contains some flags and
46 a note of the number of slots. Snaked through each arena chain is a
47 linked list of free items; when this becomes empty, an extra arena is
48 allocated and divided up into N items which are threaded into the free
51 The following global variables are associated with arenas:
53 PL_sv_arenaroot pointer to list of SV arenas
54 PL_sv_root pointer to list of free SV structures
56 PL_foo_arenaroot pointer to list of foo arenas,
57 PL_foo_root pointer to list of free foo bodies
58 ... for foo in xiv, xnv, xrv, xpv etc.
60 Note that some of the larger and more rarely used body types (eg xpvio)
61 are not allocated using arenas, but are instead just malloc()/free()ed as
62 required. Also, if PURIFY is defined, arenas are abandoned altogether,
63 with all items individually malloc()ed. In addition, a few SV heads are
64 not allocated from an arena, but are instead directly created as static
65 or auto variables, eg PL_sv_undef.
67 The SV arena serves the secondary purpose of allowing still-live SVs
68 to be located and destroyed during final cleanup.
70 At the lowest level, the macros new_SV() and del_SV() grab and free
71 an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv()
72 to return the SV to the free list with error checking.) new_SV() calls
73 more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
74 SVs in the free list have their SvTYPE field set to all ones.
76 Similarly, there are macros new_XIV()/del_XIV(), new_XNV()/del_XNV() etc
77 that allocate and return individual body types. Normally these are mapped
78 to the arena-manipulating functions new_xiv()/del_xiv() etc, but may be
79 instead mapped directly to malloc()/free() if PURIFY is defined. The
80 new/del functions remove from, or add to, the appropriate PL_foo_root
81 list, and call more_xiv() etc to add a new arena if the list is empty.
83 At the time of very final cleanup, sv_free_arenas() is called from
84 perl_destruct() to physically free all the arenas allocated since the
85 start of the interpreter. Note that this also clears PL_he_arenaroot,
86 which is otherwise dealt with in hv.c.
88 Manipulation of any of the PL_*root pointers is protected by enclosing
89 LOCK_SV_MUTEX; ... UNLOCK_SV_MUTEX calls which should Do the Right Thing
90 if threads are enabled.
92 The function visit() scans the SV arenas list, and calls a specified
93 function for each SV it finds which is still live - ie which has an SvTYPE
94 other than all 1's, and a non-zero SvREFCNT. visit() is used by the
95 following functions (specified as [function that calls visit()] / [function
96 called by visit() for each SV]):
98 sv_report_used() / do_report_used()
99 dump all remaining SVs (debugging aid)
101 sv_clean_objs() / do_clean_objs(),do_clean_named_objs()
102 Attempt to free all objects pointed to by RVs,
103 and, unless DISABLE_DESTRUCTOR_KLUDGE is defined,
104 try to do the same for all objects indirectly
105 referenced by typeglobs too. Called once from
106 perl_destruct(), prior to calling sv_clean_all()
109 sv_clean_all() / do_clean_all()
110 SvREFCNT_dec(sv) each remaining SV, possibly
111 triggering an sv_free(). It also sets the
112 SVf_BREAK flag on the SV to indicate that the
113 refcnt has been artificially lowered, and thus
114 stopping sv_free() from giving spurious warnings
115 about SVs which unexpectedly have a refcnt
116 of zero. called repeatedly from perl_destruct()
117 until there are no SVs left.
121 Private API to rest of sv.c
125 new_XIV(), del_XIV(),
126 new_XNV(), del_XNV(),
131 sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
136 ============================================================================ */
141 * "A time to plant, and a time to uproot what was planted..."
144 #define plant_SV(p) \
146 SvANY(p) = (void *)PL_sv_root; \
147 SvFLAGS(p) = SVTYPEMASK; \
152 /* sv_mutex must be held while calling uproot_SV() */
153 #define uproot_SV(p) \
156 PL_sv_root = (SV*)SvANY(p); \
161 /* new_SV(): return a new, empty SV head */
163 #ifdef DEBUG_LEAKING_SCALARS
164 /* provide a real function for a debugger to play with */
181 # define new_SV(p) (p)=S_new_SV(aTHX)
199 /* del_SV(): return an empty SV head to the free list */
214 S_del_sv(pTHX_ SV *p)
221 for (sva = PL_sv_arenaroot; sva; sva = (SV *) SvANY(sva)) {
223 svend = &sva[SvREFCNT(sva)];
224 if (p >= sv && p < svend)
228 if (ckWARN_d(WARN_INTERNAL))
229 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
230 "Attempt to free non-arena SV: 0x%"UVxf,
238 #else /* ! DEBUGGING */
240 #define del_SV(p) plant_SV(p)
242 #endif /* DEBUGGING */
246 =head1 SV Manipulation Functions
248 =for apidoc sv_add_arena
250 Given a chunk of memory, link it to the head of the list of arenas,
251 and split it into a list of free SVs.
257 Perl_sv_add_arena(pTHX_ char *ptr, U32 size, U32 flags)
262 Zero(ptr, size, char);
264 /* The first SV in an arena isn't an SV. */
265 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
266 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
267 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
269 PL_sv_arenaroot = sva;
270 PL_sv_root = sva + 1;
272 svend = &sva[SvREFCNT(sva) - 1];
275 SvANY(sv) = (void *)(SV*)(sv + 1);
276 SvFLAGS(sv) = SVTYPEMASK;
280 SvFLAGS(sv) = SVTYPEMASK;
283 /* make some more SVs by adding another arena */
285 /* sv_mutex must be held while calling more_sv() */
292 sv_add_arena(PL_nice_chunk, PL_nice_chunk_size, 0);
293 PL_nice_chunk = Nullch;
294 PL_nice_chunk_size = 0;
297 char *chunk; /* must use New here to match call to */
298 New(704,chunk,1008,char); /* Safefree() in sv_free_arenas() */
299 sv_add_arena(chunk, 1008, 0);
305 /* visit(): call the named function for each non-free SV in the arenas. */
308 S_visit(pTHX_ SVFUNC_t f)
315 for (sva = PL_sv_arenaroot; sva; sva = (SV*)SvANY(sva)) {
316 svend = &sva[SvREFCNT(sva)];
317 for (sv = sva + 1; sv < svend; ++sv) {
318 if (SvTYPE(sv) != SVTYPEMASK && SvREFCNT(sv)) {
329 /* called by sv_report_used() for each live SV */
332 do_report_used(pTHX_ SV *sv)
334 if (SvTYPE(sv) != SVTYPEMASK) {
335 PerlIO_printf(Perl_debug_log, "****\n");
342 =for apidoc sv_report_used
344 Dump the contents of all SVs not yet freed. (Debugging aid).
350 Perl_sv_report_used(pTHX)
353 visit(do_report_used);
357 /* called by sv_clean_objs() for each live SV */
360 do_clean_objs(pTHX_ SV *sv)
364 if (SvROK(sv) && SvOBJECT(rv = SvRV(sv))) {
365 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(sv)));
377 /* XXX Might want to check arrays, etc. */
380 /* called by sv_clean_objs() for each live SV */
382 #ifndef DISABLE_DESTRUCTOR_KLUDGE
384 do_clean_named_objs(pTHX_ SV *sv)
386 if (SvTYPE(sv) == SVt_PVGV && GvGP(sv)) {
387 if ( SvOBJECT(GvSV(sv)) ||
388 (GvAV(sv) && SvOBJECT(GvAV(sv))) ||
389 (GvHV(sv) && SvOBJECT(GvHV(sv))) ||
390 (GvIO(sv) && SvOBJECT(GvIO(sv))) ||
391 (GvCV(sv) && SvOBJECT(GvCV(sv))) )
393 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning named glob object:\n "), sv_dump(sv)));
401 =for apidoc sv_clean_objs
403 Attempt to destroy all objects not yet freed
409 Perl_sv_clean_objs(pTHX)
411 PL_in_clean_objs = TRUE;
412 visit(do_clean_objs);
413 #ifndef DISABLE_DESTRUCTOR_KLUDGE
414 /* some barnacles may yet remain, clinging to typeglobs */
415 visit(do_clean_named_objs);
417 PL_in_clean_objs = FALSE;
420 /* called by sv_clean_all() for each live SV */
423 do_clean_all(pTHX_ SV *sv)
425 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
426 SvFLAGS(sv) |= SVf_BREAK;
431 =for apidoc sv_clean_all
433 Decrement the refcnt of each remaining SV, possibly triggering a
434 cleanup. This function may have to be called multiple times to free
435 SVs which are in complex self-referential hierarchies.
441 Perl_sv_clean_all(pTHX)
444 PL_in_clean_all = TRUE;
445 cleaned = visit(do_clean_all);
446 PL_in_clean_all = FALSE;
451 =for apidoc sv_free_arenas
453 Deallocate the memory used by all arenas. Note that all the individual SV
454 heads and bodies within the arenas must already have been freed.
460 Perl_sv_free_arenas(pTHX)
464 XPV *arena, *arenanext;
466 /* Free arenas here, but be careful about fake ones. (We assume
467 contiguity of the fake ones with the corresponding real ones.) */
469 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
470 svanext = (SV*) SvANY(sva);
471 while (svanext && SvFAKE(svanext))
472 svanext = (SV*) SvANY(svanext);
475 Safefree((void *)sva);
478 for (arena = PL_xiv_arenaroot; arena; arena = arenanext) {
479 arenanext = (XPV*)arena->xpv_pv;
482 PL_xiv_arenaroot = 0;
484 for (arena = PL_xnv_arenaroot; arena; arena = arenanext) {
485 arenanext = (XPV*)arena->xpv_pv;
488 PL_xnv_arenaroot = 0;
490 for (arena = PL_xrv_arenaroot; arena; arena = arenanext) {
491 arenanext = (XPV*)arena->xpv_pv;
494 PL_xrv_arenaroot = 0;
496 for (arena = PL_xpv_arenaroot; arena; arena = arenanext) {
497 arenanext = (XPV*)arena->xpv_pv;
500 PL_xpv_arenaroot = 0;
502 for (arena = (XPV*)PL_xpviv_arenaroot; arena; arena = arenanext) {
503 arenanext = (XPV*)arena->xpv_pv;
506 PL_xpviv_arenaroot = 0;
508 for (arena = (XPV*)PL_xpvnv_arenaroot; arena; arena = arenanext) {
509 arenanext = (XPV*)arena->xpv_pv;
512 PL_xpvnv_arenaroot = 0;
514 for (arena = (XPV*)PL_xpvcv_arenaroot; arena; arena = arenanext) {
515 arenanext = (XPV*)arena->xpv_pv;
518 PL_xpvcv_arenaroot = 0;
520 for (arena = (XPV*)PL_xpvav_arenaroot; arena; arena = arenanext) {
521 arenanext = (XPV*)arena->xpv_pv;
524 PL_xpvav_arenaroot = 0;
526 for (arena = (XPV*)PL_xpvhv_arenaroot; arena; arena = arenanext) {
527 arenanext = (XPV*)arena->xpv_pv;
530 PL_xpvhv_arenaroot = 0;
532 for (arena = (XPV*)PL_xpvmg_arenaroot; arena; arena = arenanext) {
533 arenanext = (XPV*)arena->xpv_pv;
536 PL_xpvmg_arenaroot = 0;
538 for (arena = (XPV*)PL_xpvlv_arenaroot; arena; arena = arenanext) {
539 arenanext = (XPV*)arena->xpv_pv;
542 PL_xpvlv_arenaroot = 0;
544 for (arena = (XPV*)PL_xpvbm_arenaroot; arena; arena = arenanext) {
545 arenanext = (XPV*)arena->xpv_pv;
548 PL_xpvbm_arenaroot = 0;
550 for (arena = (XPV*)PL_he_arenaroot; arena; arena = arenanext) {
551 arenanext = (XPV*)arena->xpv_pv;
557 Safefree(PL_nice_chunk);
558 PL_nice_chunk = Nullch;
559 PL_nice_chunk_size = 0;
565 =for apidoc report_uninit
567 Print appropriate "Use of uninitialized variable" warning
573 Perl_report_uninit(pTHX)
576 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
577 " in ", OP_DESC(PL_op));
579 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit, "", "");
582 /* grab a new IV body from the free list, allocating more if necessary */
593 * See comment in more_xiv() -- RAM.
595 PL_xiv_root = *(IV**)xiv;
597 return (XPVIV*)((char*)xiv - STRUCT_OFFSET(XPVIV, xiv_iv));
600 /* return an IV body to the free list */
603 S_del_xiv(pTHX_ XPVIV *p)
605 IV* xiv = (IV*)((char*)(p) + STRUCT_OFFSET(XPVIV, xiv_iv));
607 *(IV**)xiv = PL_xiv_root;
612 /* allocate another arena's worth of IV bodies */
620 New(705, ptr, 1008/sizeof(XPV), XPV);
621 ptr->xpv_pv = (char*)PL_xiv_arenaroot; /* linked list of xiv arenas */
622 PL_xiv_arenaroot = ptr; /* to keep Purify happy */
625 xivend = &xiv[1008 / sizeof(IV) - 1];
626 xiv += (sizeof(XPV) - 1) / sizeof(IV) + 1; /* fudge by size of XPV */
628 while (xiv < xivend) {
629 *(IV**)xiv = (IV *)(xiv + 1);
635 /* grab a new NV body from the free list, allocating more if necessary */
645 PL_xnv_root = *(NV**)xnv;
647 return (XPVNV*)((char*)xnv - STRUCT_OFFSET(XPVNV, xnv_nv));
650 /* return an NV body to the free list */
653 S_del_xnv(pTHX_ XPVNV *p)
655 NV* xnv = (NV*)((char*)(p) + STRUCT_OFFSET(XPVNV, xnv_nv));
657 *(NV**)xnv = PL_xnv_root;
662 /* allocate another arena's worth of NV bodies */
670 New(711, ptr, 1008/sizeof(XPV), XPV);
671 ptr->xpv_pv = (char*)PL_xnv_arenaroot;
672 PL_xnv_arenaroot = ptr;
675 xnvend = &xnv[1008 / sizeof(NV) - 1];
676 xnv += (sizeof(XPVIV) - 1) / sizeof(NV) + 1; /* fudge by sizeof XPVIV */
678 while (xnv < xnvend) {
679 *(NV**)xnv = (NV*)(xnv + 1);
685 /* grab a new struct xrv from the free list, allocating more if necessary */
695 PL_xrv_root = (XRV*)xrv->xrv_rv;
700 /* return a struct xrv to the free list */
703 S_del_xrv(pTHX_ XRV *p)
706 p->xrv_rv = (SV*)PL_xrv_root;
711 /* allocate another arena's worth of struct xrv */
717 register XRV* xrvend;
719 New(712, ptr, 1008/sizeof(XPV), XPV);
720 ptr->xpv_pv = (char*)PL_xrv_arenaroot;
721 PL_xrv_arenaroot = ptr;
724 xrvend = &xrv[1008 / sizeof(XRV) - 1];
725 xrv += (sizeof(XPV) - 1) / sizeof(XRV) + 1;
727 while (xrv < xrvend) {
728 xrv->xrv_rv = (SV*)(xrv + 1);
734 /* grab a new struct xpv from the free list, allocating more if necessary */
744 PL_xpv_root = (XPV*)xpv->xpv_pv;
749 /* return a struct xpv to the free list */
752 S_del_xpv(pTHX_ XPV *p)
755 p->xpv_pv = (char*)PL_xpv_root;
760 /* allocate another arena's worth of struct xpv */
766 register XPV* xpvend;
767 New(713, xpv, 1008/sizeof(XPV), XPV);
768 xpv->xpv_pv = (char*)PL_xpv_arenaroot;
769 PL_xpv_arenaroot = xpv;
771 xpvend = &xpv[1008 / sizeof(XPV) - 1];
773 while (xpv < xpvend) {
774 xpv->xpv_pv = (char*)(xpv + 1);
780 /* grab a new struct xpviv from the free list, allocating more if necessary */
789 xpviv = PL_xpviv_root;
790 PL_xpviv_root = (XPVIV*)xpviv->xpv_pv;
795 /* return a struct xpviv to the free list */
798 S_del_xpviv(pTHX_ XPVIV *p)
801 p->xpv_pv = (char*)PL_xpviv_root;
806 /* allocate another arena's worth of struct xpviv */
811 register XPVIV* xpviv;
812 register XPVIV* xpvivend;
813 New(714, xpviv, 1008/sizeof(XPVIV), XPVIV);
814 xpviv->xpv_pv = (char*)PL_xpviv_arenaroot;
815 PL_xpviv_arenaroot = xpviv;
817 xpvivend = &xpviv[1008 / sizeof(XPVIV) - 1];
818 PL_xpviv_root = ++xpviv;
819 while (xpviv < xpvivend) {
820 xpviv->xpv_pv = (char*)(xpviv + 1);
826 /* grab a new struct xpvnv from the free list, allocating more if necessary */
835 xpvnv = PL_xpvnv_root;
836 PL_xpvnv_root = (XPVNV*)xpvnv->xpv_pv;
841 /* return a struct xpvnv to the free list */
844 S_del_xpvnv(pTHX_ XPVNV *p)
847 p->xpv_pv = (char*)PL_xpvnv_root;
852 /* allocate another arena's worth of struct xpvnv */
857 register XPVNV* xpvnv;
858 register XPVNV* xpvnvend;
859 New(715, xpvnv, 1008/sizeof(XPVNV), XPVNV);
860 xpvnv->xpv_pv = (char*)PL_xpvnv_arenaroot;
861 PL_xpvnv_arenaroot = xpvnv;
863 xpvnvend = &xpvnv[1008 / sizeof(XPVNV) - 1];
864 PL_xpvnv_root = ++xpvnv;
865 while (xpvnv < xpvnvend) {
866 xpvnv->xpv_pv = (char*)(xpvnv + 1);
872 /* grab a new struct xpvcv from the free list, allocating more if necessary */
881 xpvcv = PL_xpvcv_root;
882 PL_xpvcv_root = (XPVCV*)xpvcv->xpv_pv;
887 /* return a struct xpvcv to the free list */
890 S_del_xpvcv(pTHX_ XPVCV *p)
893 p->xpv_pv = (char*)PL_xpvcv_root;
898 /* allocate another arena's worth of struct xpvcv */
903 register XPVCV* xpvcv;
904 register XPVCV* xpvcvend;
905 New(716, xpvcv, 1008/sizeof(XPVCV), XPVCV);
906 xpvcv->xpv_pv = (char*)PL_xpvcv_arenaroot;
907 PL_xpvcv_arenaroot = xpvcv;
909 xpvcvend = &xpvcv[1008 / sizeof(XPVCV) - 1];
910 PL_xpvcv_root = ++xpvcv;
911 while (xpvcv < xpvcvend) {
912 xpvcv->xpv_pv = (char*)(xpvcv + 1);
918 /* grab a new struct xpvav from the free list, allocating more if necessary */
927 xpvav = PL_xpvav_root;
928 PL_xpvav_root = (XPVAV*)xpvav->xav_array;
933 /* return a struct xpvav to the free list */
936 S_del_xpvav(pTHX_ XPVAV *p)
939 p->xav_array = (char*)PL_xpvav_root;
944 /* allocate another arena's worth of struct xpvav */
949 register XPVAV* xpvav;
950 register XPVAV* xpvavend;
951 New(717, xpvav, 1008/sizeof(XPVAV), XPVAV);
952 xpvav->xav_array = (char*)PL_xpvav_arenaroot;
953 PL_xpvav_arenaroot = xpvav;
955 xpvavend = &xpvav[1008 / sizeof(XPVAV) - 1];
956 PL_xpvav_root = ++xpvav;
957 while (xpvav < xpvavend) {
958 xpvav->xav_array = (char*)(xpvav + 1);
961 xpvav->xav_array = 0;
964 /* grab a new struct xpvhv from the free list, allocating more if necessary */
973 xpvhv = PL_xpvhv_root;
974 PL_xpvhv_root = (XPVHV*)xpvhv->xhv_array;
979 /* return a struct xpvhv to the free list */
982 S_del_xpvhv(pTHX_ XPVHV *p)
985 p->xhv_array = (char*)PL_xpvhv_root;
990 /* allocate another arena's worth of struct xpvhv */
995 register XPVHV* xpvhv;
996 register XPVHV* xpvhvend;
997 New(718, xpvhv, 1008/sizeof(XPVHV), XPVHV);
998 xpvhv->xhv_array = (char*)PL_xpvhv_arenaroot;
999 PL_xpvhv_arenaroot = xpvhv;
1001 xpvhvend = &xpvhv[1008 / sizeof(XPVHV) - 1];
1002 PL_xpvhv_root = ++xpvhv;
1003 while (xpvhv < xpvhvend) {
1004 xpvhv->xhv_array = (char*)(xpvhv + 1);
1007 xpvhv->xhv_array = 0;
1010 /* grab a new struct xpvmg from the free list, allocating more if necessary */
1019 xpvmg = PL_xpvmg_root;
1020 PL_xpvmg_root = (XPVMG*)xpvmg->xpv_pv;
1025 /* return a struct xpvmg to the free list */
1028 S_del_xpvmg(pTHX_ XPVMG *p)
1031 p->xpv_pv = (char*)PL_xpvmg_root;
1036 /* allocate another arena's worth of struct xpvmg */
1041 register XPVMG* xpvmg;
1042 register XPVMG* xpvmgend;
1043 New(719, xpvmg, 1008/sizeof(XPVMG), XPVMG);
1044 xpvmg->xpv_pv = (char*)PL_xpvmg_arenaroot;
1045 PL_xpvmg_arenaroot = xpvmg;
1047 xpvmgend = &xpvmg[1008 / sizeof(XPVMG) - 1];
1048 PL_xpvmg_root = ++xpvmg;
1049 while (xpvmg < xpvmgend) {
1050 xpvmg->xpv_pv = (char*)(xpvmg + 1);
1056 /* grab a new struct xpvlv from the free list, allocating more if necessary */
1065 xpvlv = PL_xpvlv_root;
1066 PL_xpvlv_root = (XPVLV*)xpvlv->xpv_pv;
1071 /* return a struct xpvlv to the free list */
1074 S_del_xpvlv(pTHX_ XPVLV *p)
1077 p->xpv_pv = (char*)PL_xpvlv_root;
1082 /* allocate another arena's worth of struct xpvlv */
1087 register XPVLV* xpvlv;
1088 register XPVLV* xpvlvend;
1089 New(720, xpvlv, 1008/sizeof(XPVLV), XPVLV);
1090 xpvlv->xpv_pv = (char*)PL_xpvlv_arenaroot;
1091 PL_xpvlv_arenaroot = xpvlv;
1093 xpvlvend = &xpvlv[1008 / sizeof(XPVLV) - 1];
1094 PL_xpvlv_root = ++xpvlv;
1095 while (xpvlv < xpvlvend) {
1096 xpvlv->xpv_pv = (char*)(xpvlv + 1);
1102 /* grab a new struct xpvbm from the free list, allocating more if necessary */
1111 xpvbm = PL_xpvbm_root;
1112 PL_xpvbm_root = (XPVBM*)xpvbm->xpv_pv;
1117 /* return a struct xpvbm to the free list */
1120 S_del_xpvbm(pTHX_ XPVBM *p)
1123 p->xpv_pv = (char*)PL_xpvbm_root;
1128 /* allocate another arena's worth of struct xpvbm */
1133 register XPVBM* xpvbm;
1134 register XPVBM* xpvbmend;
1135 New(721, xpvbm, 1008/sizeof(XPVBM), XPVBM);
1136 xpvbm->xpv_pv = (char*)PL_xpvbm_arenaroot;
1137 PL_xpvbm_arenaroot = xpvbm;
1139 xpvbmend = &xpvbm[1008 / sizeof(XPVBM) - 1];
1140 PL_xpvbm_root = ++xpvbm;
1141 while (xpvbm < xpvbmend) {
1142 xpvbm->xpv_pv = (char*)(xpvbm + 1);
1148 #define my_safemalloc(s) (void*)safemalloc(s)
1149 #define my_safefree(p) safefree((char*)p)
1153 #define new_XIV() my_safemalloc(sizeof(XPVIV))
1154 #define del_XIV(p) my_safefree(p)
1156 #define new_XNV() my_safemalloc(sizeof(XPVNV))
1157 #define del_XNV(p) my_safefree(p)
1159 #define new_XRV() my_safemalloc(sizeof(XRV))
1160 #define del_XRV(p) my_safefree(p)
1162 #define new_XPV() my_safemalloc(sizeof(XPV))
1163 #define del_XPV(p) my_safefree(p)
1165 #define new_XPVIV() my_safemalloc(sizeof(XPVIV))
1166 #define del_XPVIV(p) my_safefree(p)
1168 #define new_XPVNV() my_safemalloc(sizeof(XPVNV))
1169 #define del_XPVNV(p) my_safefree(p)
1171 #define new_XPVCV() my_safemalloc(sizeof(XPVCV))
1172 #define del_XPVCV(p) my_safefree(p)
1174 #define new_XPVAV() my_safemalloc(sizeof(XPVAV))
1175 #define del_XPVAV(p) my_safefree(p)
1177 #define new_XPVHV() my_safemalloc(sizeof(XPVHV))
1178 #define del_XPVHV(p) my_safefree(p)
1180 #define new_XPVMG() my_safemalloc(sizeof(XPVMG))
1181 #define del_XPVMG(p) my_safefree(p)
1183 #define new_XPVLV() my_safemalloc(sizeof(XPVLV))
1184 #define del_XPVLV(p) my_safefree(p)
1186 #define new_XPVBM() my_safemalloc(sizeof(XPVBM))
1187 #define del_XPVBM(p) my_safefree(p)
1191 #define new_XIV() (void*)new_xiv()
1192 #define del_XIV(p) del_xiv((XPVIV*) p)
1194 #define new_XNV() (void*)new_xnv()
1195 #define del_XNV(p) del_xnv((XPVNV*) p)
1197 #define new_XRV() (void*)new_xrv()
1198 #define del_XRV(p) del_xrv((XRV*) p)
1200 #define new_XPV() (void*)new_xpv()
1201 #define del_XPV(p) del_xpv((XPV *)p)
1203 #define new_XPVIV() (void*)new_xpviv()
1204 #define del_XPVIV(p) del_xpviv((XPVIV *)p)
1206 #define new_XPVNV() (void*)new_xpvnv()
1207 #define del_XPVNV(p) del_xpvnv((XPVNV *)p)
1209 #define new_XPVCV() (void*)new_xpvcv()
1210 #define del_XPVCV(p) del_xpvcv((XPVCV *)p)
1212 #define new_XPVAV() (void*)new_xpvav()
1213 #define del_XPVAV(p) del_xpvav((XPVAV *)p)
1215 #define new_XPVHV() (void*)new_xpvhv()
1216 #define del_XPVHV(p) del_xpvhv((XPVHV *)p)
1218 #define new_XPVMG() (void*)new_xpvmg()
1219 #define del_XPVMG(p) del_xpvmg((XPVMG *)p)
1221 #define new_XPVLV() (void*)new_xpvlv()
1222 #define del_XPVLV(p) del_xpvlv((XPVLV *)p)
1224 #define new_XPVBM() (void*)new_xpvbm()
1225 #define del_XPVBM(p) del_xpvbm((XPVBM *)p)
1229 #define new_XPVGV() my_safemalloc(sizeof(XPVGV))
1230 #define del_XPVGV(p) my_safefree(p)
1232 #define new_XPVFM() my_safemalloc(sizeof(XPVFM))
1233 #define del_XPVFM(p) my_safefree(p)
1235 #define new_XPVIO() my_safemalloc(sizeof(XPVIO))
1236 #define del_XPVIO(p) my_safefree(p)
1239 =for apidoc sv_upgrade
1241 Upgrade an SV to a more complex form. Generally adds a new body type to the
1242 SV, then copies across as much information as possible from the old body.
1243 You generally want to use the C<SvUPGRADE> macro wrapper. See also C<svtype>.
1249 Perl_sv_upgrade(pTHX_ register SV *sv, U32 mt)
1256 MAGIC* magic = NULL;
1259 if (mt != SVt_PV && SvIsCOW(sv)) {
1260 sv_force_normal_flags(sv, 0);
1263 if (SvTYPE(sv) == mt)
1267 (void)SvOOK_off(sv);
1269 switch (SvTYPE(sv)) {
1290 else if (mt < SVt_PVIV)
1307 pv = (char*)SvRV(sv);
1327 else if (mt == SVt_NV)
1338 del_XPVIV(SvANY(sv));
1348 del_XPVNV(SvANY(sv));
1356 magic = SvMAGIC(sv);
1357 stash = SvSTASH(sv);
1358 del_XPVMG(SvANY(sv));
1361 Perl_croak(aTHX_ "Can't upgrade that kind of scalar");
1366 Perl_croak(aTHX_ "Can't upgrade to undef");
1368 SvANY(sv) = new_XIV();
1372 SvANY(sv) = new_XNV();
1376 SvANY(sv) = new_XRV();
1380 SvANY(sv) = new_XPV();
1386 SvANY(sv) = new_XPVIV();
1396 SvANY(sv) = new_XPVNV();
1404 SvANY(sv) = new_XPVMG();
1410 SvMAGIC(sv) = magic;
1411 SvSTASH(sv) = stash;
1414 SvANY(sv) = new_XPVLV();
1420 SvMAGIC(sv) = magic;
1421 SvSTASH(sv) = stash;
1428 SvANY(sv) = new_XPVAV();
1436 SvMAGIC(sv) = magic;
1437 SvSTASH(sv) = stash;
1443 SvANY(sv) = new_XPVHV();
1449 HvTOTALKEYS(sv) = 0;
1450 HvPLACEHOLDERS(sv) = 0;
1451 SvMAGIC(sv) = magic;
1452 SvSTASH(sv) = stash;
1459 SvANY(sv) = new_XPVCV();
1460 Zero(SvANY(sv), 1, XPVCV);
1466 SvMAGIC(sv) = magic;
1467 SvSTASH(sv) = stash;
1470 SvANY(sv) = new_XPVGV();
1476 SvMAGIC(sv) = magic;
1477 SvSTASH(sv) = stash;
1485 SvANY(sv) = new_XPVBM();
1491 SvMAGIC(sv) = magic;
1492 SvSTASH(sv) = stash;
1498 SvANY(sv) = new_XPVFM();
1499 Zero(SvANY(sv), 1, XPVFM);
1505 SvMAGIC(sv) = magic;
1506 SvSTASH(sv) = stash;
1509 SvANY(sv) = new_XPVIO();
1510 Zero(SvANY(sv), 1, XPVIO);
1516 SvMAGIC(sv) = magic;
1517 SvSTASH(sv) = stash;
1518 IoPAGE_LEN(sv) = 60;
1521 SvFLAGS(sv) &= ~SVTYPEMASK;
1527 =for apidoc sv_backoff
1529 Remove any string offset. You should normally use the C<SvOOK_off> macro
1536 Perl_sv_backoff(pTHX_ register SV *sv)
1540 char *s = SvPVX(sv);
1541 SvLEN(sv) += SvIVX(sv);
1542 SvPVX(sv) -= SvIVX(sv);
1544 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
1546 SvFLAGS(sv) &= ~SVf_OOK;
1553 Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
1554 upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1555 Use the C<SvGROW> wrapper instead.
1561 Perl_sv_grow(pTHX_ register SV *sv, register STRLEN newlen)
1565 #ifdef HAS_64K_LIMIT
1566 if (newlen >= 0x10000) {
1567 PerlIO_printf(Perl_debug_log,
1568 "Allocation too large: %"UVxf"\n", (UV)newlen);
1571 #endif /* HAS_64K_LIMIT */
1574 if (SvTYPE(sv) < SVt_PV) {
1575 sv_upgrade(sv, SVt_PV);
1578 else if (SvOOK(sv)) { /* pv is offset? */
1581 if (newlen > SvLEN(sv))
1582 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
1583 #ifdef HAS_64K_LIMIT
1584 if (newlen >= 0x10000)
1591 if (newlen > SvLEN(sv)) { /* need more room? */
1592 if (SvLEN(sv) && s) {
1594 STRLEN l = malloced_size((void*)SvPVX(sv));
1600 Renew(s,newlen,char);
1603 New(703, s, newlen, char);
1604 if (SvPVX(sv) && SvCUR(sv)) {
1605 Move(SvPVX(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char);
1609 SvLEN_set(sv, newlen);
1615 =for apidoc sv_setiv
1617 Copies an integer into the given SV, upgrading first if necessary.
1618 Does not handle 'set' magic. See also C<sv_setiv_mg>.
1624 Perl_sv_setiv(pTHX_ register SV *sv, IV i)
1626 SV_CHECK_THINKFIRST_COW_DROP(sv);
1627 switch (SvTYPE(sv)) {
1629 sv_upgrade(sv, SVt_IV);
1632 sv_upgrade(sv, SVt_PVNV);
1636 sv_upgrade(sv, SVt_PVIV);
1645 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
1648 (void)SvIOK_only(sv); /* validate number */
1654 =for apidoc sv_setiv_mg
1656 Like C<sv_setiv>, but also handles 'set' magic.
1662 Perl_sv_setiv_mg(pTHX_ register SV *sv, IV i)
1669 =for apidoc sv_setuv
1671 Copies an unsigned integer into the given SV, upgrading first if necessary.
1672 Does not handle 'set' magic. See also C<sv_setuv_mg>.
1678 Perl_sv_setuv(pTHX_ register SV *sv, UV u)
1680 /* With these two if statements:
1681 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
1684 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
1686 If you wish to remove them, please benchmark to see what the effect is
1688 if (u <= (UV)IV_MAX) {
1689 sv_setiv(sv, (IV)u);
1698 =for apidoc sv_setuv_mg
1700 Like C<sv_setuv>, but also handles 'set' magic.
1706 Perl_sv_setuv_mg(pTHX_ register SV *sv, UV u)
1708 /* With these two if statements:
1709 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
1712 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
1714 If you wish to remove them, please benchmark to see what the effect is
1716 if (u <= (UV)IV_MAX) {
1717 sv_setiv(sv, (IV)u);
1727 =for apidoc sv_setnv
1729 Copies a double into the given SV, upgrading first if necessary.
1730 Does not handle 'set' magic. See also C<sv_setnv_mg>.
1736 Perl_sv_setnv(pTHX_ register SV *sv, NV num)
1738 SV_CHECK_THINKFIRST_COW_DROP(sv);
1739 switch (SvTYPE(sv)) {
1742 sv_upgrade(sv, SVt_NV);
1747 sv_upgrade(sv, SVt_PVNV);
1756 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
1760 (void)SvNOK_only(sv); /* validate number */
1765 =for apidoc sv_setnv_mg
1767 Like C<sv_setnv>, but also handles 'set' magic.
1773 Perl_sv_setnv_mg(pTHX_ register SV *sv, NV num)
1779 /* Print an "isn't numeric" warning, using a cleaned-up,
1780 * printable version of the offending string
1784 S_not_a_number(pTHX_ SV *sv)
1791 dsv = sv_2mortal(newSVpv("", 0));
1792 pv = sv_uni_display(dsv, sv, 10, 0);
1795 char *limit = tmpbuf + sizeof(tmpbuf) - 8;
1796 /* each *s can expand to 4 chars + "...\0",
1797 i.e. need room for 8 chars */
1800 for (s = SvPVX(sv), end = s + SvCUR(sv); s < end && d < limit; s++) {
1802 if (ch & 128 && !isPRINT_LC(ch)) {
1811 else if (ch == '\r') {
1815 else if (ch == '\f') {
1819 else if (ch == '\\') {
1823 else if (ch == '\0') {
1827 else if (isPRINT_LC(ch))
1844 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1845 "Argument \"%s\" isn't numeric in %s", pv,
1848 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1849 "Argument \"%s\" isn't numeric", pv);
1853 =for apidoc looks_like_number
1855 Test if the content of an SV looks like a number (or is a number).
1856 C<Inf> and C<Infinity> are treated as numbers (so will not issue a
1857 non-numeric warning), even if your atof() doesn't grok them.
1863 Perl_looks_like_number(pTHX_ SV *sv)
1865 register char *sbegin;
1872 else if (SvPOKp(sv))
1873 sbegin = SvPV(sv, len);
1875 return 1; /* Historic. Wrong? */
1876 return grok_number(sbegin, len, NULL);
1879 /* Actually, ISO C leaves conversion of UV to IV undefined, but
1880 until proven guilty, assume that things are not that bad... */
1885 As 64 bit platforms often have an NV that doesn't preserve all bits of
1886 an IV (an assumption perl has been based on to date) it becomes necessary
1887 to remove the assumption that the NV always carries enough precision to
1888 recreate the IV whenever needed, and that the NV is the canonical form.
1889 Instead, IV/UV and NV need to be given equal rights. So as to not lose
1890 precision as a side effect of conversion (which would lead to insanity
1891 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
1892 1) to distinguish between IV/UV/NV slots that have cached a valid
1893 conversion where precision was lost and IV/UV/NV slots that have a
1894 valid conversion which has lost no precision
1895 2) to ensure that if a numeric conversion to one form is requested that
1896 would lose precision, the precise conversion (or differently
1897 imprecise conversion) is also performed and cached, to prevent
1898 requests for different numeric formats on the same SV causing
1899 lossy conversion chains. (lossless conversion chains are perfectly
1904 SvIOKp is true if the IV slot contains a valid value
1905 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
1906 SvNOKp is true if the NV slot contains a valid value
1907 SvNOK is true only if the NV value is accurate
1910 while converting from PV to NV, check to see if converting that NV to an
1911 IV(or UV) would lose accuracy over a direct conversion from PV to
1912 IV(or UV). If it would, cache both conversions, return NV, but mark
1913 SV as IOK NOKp (ie not NOK).
1915 While converting from PV to IV, check to see if converting that IV to an
1916 NV would lose accuracy over a direct conversion from PV to NV. If it
1917 would, cache both conversions, flag similarly.
1919 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
1920 correctly because if IV & NV were set NV *always* overruled.
1921 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
1922 changes - now IV and NV together means that the two are interchangeable:
1923 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
1925 The benefit of this is that operations such as pp_add know that if
1926 SvIOK is true for both left and right operands, then integer addition
1927 can be used instead of floating point (for cases where the result won't
1928 overflow). Before, floating point was always used, which could lead to
1929 loss of precision compared with integer addition.
1931 * making IV and NV equal status should make maths accurate on 64 bit
1933 * may speed up maths somewhat if pp_add and friends start to use
1934 integers when possible instead of fp. (Hopefully the overhead in
1935 looking for SvIOK and checking for overflow will not outweigh the
1936 fp to integer speedup)
1937 * will slow down integer operations (callers of SvIV) on "inaccurate"
1938 values, as the change from SvIOK to SvIOKp will cause a call into
1939 sv_2iv each time rather than a macro access direct to the IV slot
1940 * should speed up number->string conversion on integers as IV is
1941 favoured when IV and NV are equally accurate
1943 ####################################################################
1944 You had better be using SvIOK_notUV if you want an IV for arithmetic:
1945 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
1946 On the other hand, SvUOK is true iff UV.
1947 ####################################################################
1949 Your mileage will vary depending your CPU's relative fp to integer
1953 #ifndef NV_PRESERVES_UV
1954 # define IS_NUMBER_UNDERFLOW_IV 1
1955 # define IS_NUMBER_UNDERFLOW_UV 2
1956 # define IS_NUMBER_IV_AND_UV 2
1957 # define IS_NUMBER_OVERFLOW_IV 4
1958 # define IS_NUMBER_OVERFLOW_UV 5
1960 /* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
1962 /* For sv_2nv these three cases are "SvNOK and don't bother casting" */
1964 S_sv_2iuv_non_preserve(pTHX_ register SV *sv, I32 numtype)
1966 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2iuv_non '%s', IV=0x%"UVxf" NV=%"NVgf" inttype=%"UVXf"\n", SvPVX(sv), SvIVX(sv), SvNVX(sv), (UV)numtype));
1967 if (SvNVX(sv) < (NV)IV_MIN) {
1968 (void)SvIOKp_on(sv);
1971 return IS_NUMBER_UNDERFLOW_IV;
1973 if (SvNVX(sv) > (NV)UV_MAX) {
1974 (void)SvIOKp_on(sv);
1978 return IS_NUMBER_OVERFLOW_UV;
1980 (void)SvIOKp_on(sv);
1982 /* Can't use strtol etc to convert this string. (See truth table in
1984 if (SvNVX(sv) <= (UV)IV_MAX) {
1985 SvIVX(sv) = I_V(SvNVX(sv));
1986 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
1987 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
1989 /* Integer is imprecise. NOK, IOKp */
1991 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
1994 SvUVX(sv) = U_V(SvNVX(sv));
1995 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
1996 if (SvUVX(sv) == UV_MAX) {
1997 /* As we know that NVs don't preserve UVs, UV_MAX cannot
1998 possibly be preserved by NV. Hence, it must be overflow.
2000 return IS_NUMBER_OVERFLOW_UV;
2002 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
2004 /* Integer is imprecise. NOK, IOKp */
2006 return IS_NUMBER_OVERFLOW_IV;
2008 #endif /* !NV_PRESERVES_UV*/
2013 Return the integer value of an SV, doing any necessary string conversion,
2014 magic etc. Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
2020 Perl_sv_2iv(pTHX_ register SV *sv)
2024 if (SvGMAGICAL(sv)) {
2029 return I_V(SvNVX(sv));
2031 if (SvPOKp(sv) && SvLEN(sv))
2034 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2035 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2041 if (SvTHINKFIRST(sv)) {
2044 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
2045 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
2046 return SvIV(tmpstr);
2047 return PTR2IV(SvRV(sv));
2050 sv_force_normal_flags(sv, 0);
2052 if (SvREADONLY(sv) && !SvOK(sv)) {
2053 if (ckWARN(WARN_UNINITIALIZED))
2060 return (IV)(SvUVX(sv));
2067 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2068 * without also getting a cached IV/UV from it at the same time
2069 * (ie PV->NV conversion should detect loss of accuracy and cache
2070 * IV or UV at same time to avoid this. NWC */
2072 if (SvTYPE(sv) == SVt_NV)
2073 sv_upgrade(sv, SVt_PVNV);
2075 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2076 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
2077 certainly cast into the IV range at IV_MAX, whereas the correct
2078 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
2080 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2081 SvIVX(sv) = I_V(SvNVX(sv));
2082 if (SvNVX(sv) == (NV) SvIVX(sv)
2083 #ifndef NV_PRESERVES_UV
2084 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2085 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2086 /* Don't flag it as "accurately an integer" if the number
2087 came from a (by definition imprecise) NV operation, and
2088 we're outside the range of NV integer precision */
2091 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2092 DEBUG_c(PerlIO_printf(Perl_debug_log,
2093 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
2099 /* IV not precise. No need to convert from PV, as NV
2100 conversion would already have cached IV if it detected
2101 that PV->IV would be better than PV->NV->IV
2102 flags already correct - don't set public IOK. */
2103 DEBUG_c(PerlIO_printf(Perl_debug_log,
2104 "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
2109 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2110 but the cast (NV)IV_MIN rounds to a the value less (more
2111 negative) than IV_MIN which happens to be equal to SvNVX ??
2112 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2113 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2114 (NV)UVX == NVX are both true, but the values differ. :-(
2115 Hopefully for 2s complement IV_MIN is something like
2116 0x8000000000000000 which will be exact. NWC */
2119 SvUVX(sv) = U_V(SvNVX(sv));
2121 (SvNVX(sv) == (NV) SvUVX(sv))
2122 #ifndef NV_PRESERVES_UV
2123 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2124 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2125 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2126 /* Don't flag it as "accurately an integer" if the number
2127 came from a (by definition imprecise) NV operation, and
2128 we're outside the range of NV integer precision */
2134 DEBUG_c(PerlIO_printf(Perl_debug_log,
2135 "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
2139 return (IV)SvUVX(sv);
2142 else if (SvPOKp(sv) && SvLEN(sv)) {
2144 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
2145 /* We want to avoid a possible problem when we cache an IV which
2146 may be later translated to an NV, and the resulting NV is not
2147 the same as the direct translation of the initial string
2148 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2149 be careful to ensure that the value with the .456 is around if the
2150 NV value is requested in the future).
2152 This means that if we cache such an IV, we need to cache the
2153 NV as well. Moreover, we trade speed for space, and do not
2154 cache the NV if we are sure it's not needed.
2157 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2158 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2159 == IS_NUMBER_IN_UV) {
2160 /* It's definitely an integer, only upgrade to PVIV */
2161 if (SvTYPE(sv) < SVt_PVIV)
2162 sv_upgrade(sv, SVt_PVIV);
2164 } else if (SvTYPE(sv) < SVt_PVNV)
2165 sv_upgrade(sv, SVt_PVNV);
2167 /* If NV preserves UV then we only use the UV value if we know that
2168 we aren't going to call atof() below. If NVs don't preserve UVs
2169 then the value returned may have more precision than atof() will
2170 return, even though value isn't perfectly accurate. */
2171 if ((numtype & (IS_NUMBER_IN_UV
2172 #ifdef NV_PRESERVES_UV
2175 )) == IS_NUMBER_IN_UV) {
2176 /* This won't turn off the public IOK flag if it was set above */
2177 (void)SvIOKp_on(sv);
2179 if (!(numtype & IS_NUMBER_NEG)) {
2181 if (value <= (UV)IV_MAX) {
2182 SvIVX(sv) = (IV)value;
2188 /* 2s complement assumption */
2189 if (value <= (UV)IV_MIN) {
2190 SvIVX(sv) = -(IV)value;
2192 /* Too negative for an IV. This is a double upgrade, but
2193 I'm assuming it will be rare. */
2194 if (SvTYPE(sv) < SVt_PVNV)
2195 sv_upgrade(sv, SVt_PVNV);
2199 SvNVX(sv) = -(NV)value;
2204 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2205 will be in the previous block to set the IV slot, and the next
2206 block to set the NV slot. So no else here. */
2208 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2209 != IS_NUMBER_IN_UV) {
2210 /* It wasn't an (integer that doesn't overflow the UV). */
2211 SvNVX(sv) = Atof(SvPVX(sv));
2213 if (! numtype && ckWARN(WARN_NUMERIC))
2216 #if defined(USE_LONG_DOUBLE)
2217 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
2218 PTR2UV(sv), SvNVX(sv)));
2220 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n",
2221 PTR2UV(sv), SvNVX(sv)));
2225 #ifdef NV_PRESERVES_UV
2226 (void)SvIOKp_on(sv);
2228 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2229 SvIVX(sv) = I_V(SvNVX(sv));
2230 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2233 /* Integer is imprecise. NOK, IOKp */
2235 /* UV will not work better than IV */
2237 if (SvNVX(sv) > (NV)UV_MAX) {
2239 /* Integer is inaccurate. NOK, IOKp, is UV */
2243 SvUVX(sv) = U_V(SvNVX(sv));
2244 /* 0xFFFFFFFFFFFFFFFF not an issue in here */
2245 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2249 /* Integer is imprecise. NOK, IOKp, is UV */
2255 #else /* NV_PRESERVES_UV */
2256 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2257 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2258 /* The IV slot will have been set from value returned by
2259 grok_number above. The NV slot has just been set using
2262 assert (SvIOKp(sv));
2264 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2265 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2266 /* Small enough to preserve all bits. */
2267 (void)SvIOKp_on(sv);
2269 SvIVX(sv) = I_V(SvNVX(sv));
2270 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2272 /* Assumption: first non-preserved integer is < IV_MAX,
2273 this NV is in the preserved range, therefore: */
2274 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2276 Perl_croak(aTHX_ "sv_2iv assumed (U_V(fabs((double)SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%"NVgf" U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX);
2280 0 0 already failed to read UV.
2281 0 1 already failed to read UV.
2282 1 0 you won't get here in this case. IV/UV
2283 slot set, public IOK, Atof() unneeded.
2284 1 1 already read UV.
2285 so there's no point in sv_2iuv_non_preserve() attempting
2286 to use atol, strtol, strtoul etc. */
2287 if (sv_2iuv_non_preserve (sv, numtype)
2288 >= IS_NUMBER_OVERFLOW_IV)
2292 #endif /* NV_PRESERVES_UV */
2295 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
2297 if (SvTYPE(sv) < SVt_IV)
2298 /* Typically the caller expects that sv_any is not NULL now. */
2299 sv_upgrade(sv, SVt_IV);
2302 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
2303 PTR2UV(sv),SvIVX(sv)));
2304 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
2310 Return the unsigned integer value of an SV, doing any necessary string
2311 conversion, magic etc. Normally used via the C<SvUV(sv)> and C<SvUVx(sv)>
2318 Perl_sv_2uv(pTHX_ register SV *sv)
2322 if (SvGMAGICAL(sv)) {
2327 return U_V(SvNVX(sv));
2328 if (SvPOKp(sv) && SvLEN(sv))
2331 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2332 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2338 if (SvTHINKFIRST(sv)) {
2341 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
2342 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
2343 return SvUV(tmpstr);
2344 return PTR2UV(SvRV(sv));
2347 sv_force_normal_flags(sv, 0);
2349 if (SvREADONLY(sv) && !SvOK(sv)) {
2350 if (ckWARN(WARN_UNINITIALIZED))
2360 return (UV)SvIVX(sv);
2364 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2365 * without also getting a cached IV/UV from it at the same time
2366 * (ie PV->NV conversion should detect loss of accuracy and cache
2367 * IV or UV at same time to avoid this. */
2368 /* IV-over-UV optimisation - choose to cache IV if possible */
2370 if (SvTYPE(sv) == SVt_NV)
2371 sv_upgrade(sv, SVt_PVNV);
2373 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2374 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2375 SvIVX(sv) = I_V(SvNVX(sv));
2376 if (SvNVX(sv) == (NV) SvIVX(sv)
2377 #ifndef NV_PRESERVES_UV
2378 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2379 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2380 /* Don't flag it as "accurately an integer" if the number
2381 came from a (by definition imprecise) NV operation, and
2382 we're outside the range of NV integer precision */
2385 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2386 DEBUG_c(PerlIO_printf(Perl_debug_log,
2387 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (precise)\n",
2393 /* IV not precise. No need to convert from PV, as NV
2394 conversion would already have cached IV if it detected
2395 that PV->IV would be better than PV->NV->IV
2396 flags already correct - don't set public IOK. */
2397 DEBUG_c(PerlIO_printf(Perl_debug_log,
2398 "0x%"UVxf" uv(%"NVgf" => %"IVdf") (imprecise)\n",
2403 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2404 but the cast (NV)IV_MIN rounds to a the value less (more
2405 negative) than IV_MIN which happens to be equal to SvNVX ??
2406 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2407 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2408 (NV)UVX == NVX are both true, but the values differ. :-(
2409 Hopefully for 2s complement IV_MIN is something like
2410 0x8000000000000000 which will be exact. NWC */
2413 SvUVX(sv) = U_V(SvNVX(sv));
2415 (SvNVX(sv) == (NV) SvUVX(sv))
2416 #ifndef NV_PRESERVES_UV
2417 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2418 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2419 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2420 /* Don't flag it as "accurately an integer" if the number
2421 came from a (by definition imprecise) NV operation, and
2422 we're outside the range of NV integer precision */
2427 DEBUG_c(PerlIO_printf(Perl_debug_log,
2428 "0x%"UVxf" 2uv(%"UVuf" => %"IVdf") (as unsigned)\n",
2434 else if (SvPOKp(sv) && SvLEN(sv)) {
2436 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
2438 /* We want to avoid a possible problem when we cache a UV which
2439 may be later translated to an NV, and the resulting NV is not
2440 the translation of the initial data.
2442 This means that if we cache such a UV, we need to cache the
2443 NV as well. Moreover, we trade speed for space, and do not
2444 cache the NV if not needed.
2447 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2448 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2449 == IS_NUMBER_IN_UV) {
2450 /* It's definitely an integer, only upgrade to PVIV */
2451 if (SvTYPE(sv) < SVt_PVIV)
2452 sv_upgrade(sv, SVt_PVIV);
2454 } else if (SvTYPE(sv) < SVt_PVNV)
2455 sv_upgrade(sv, SVt_PVNV);
2457 /* If NV preserves UV then we only use the UV value if we know that
2458 we aren't going to call atof() below. If NVs don't preserve UVs
2459 then the value returned may have more precision than atof() will
2460 return, even though it isn't accurate. */
2461 if ((numtype & (IS_NUMBER_IN_UV
2462 #ifdef NV_PRESERVES_UV
2465 )) == IS_NUMBER_IN_UV) {
2466 /* This won't turn off the public IOK flag if it was set above */
2467 (void)SvIOKp_on(sv);
2469 if (!(numtype & IS_NUMBER_NEG)) {
2471 if (value <= (UV)IV_MAX) {
2472 SvIVX(sv) = (IV)value;
2474 /* it didn't overflow, and it was positive. */
2479 /* 2s complement assumption */
2480 if (value <= (UV)IV_MIN) {
2481 SvIVX(sv) = -(IV)value;
2483 /* Too negative for an IV. This is a double upgrade, but
2484 I'm assuming it will be rare. */
2485 if (SvTYPE(sv) < SVt_PVNV)
2486 sv_upgrade(sv, SVt_PVNV);
2490 SvNVX(sv) = -(NV)value;
2496 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2497 != IS_NUMBER_IN_UV) {
2498 /* It wasn't an integer, or it overflowed the UV. */
2499 SvNVX(sv) = Atof(SvPVX(sv));
2501 if (! numtype && ckWARN(WARN_NUMERIC))
2504 #if defined(USE_LONG_DOUBLE)
2505 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%" PERL_PRIgldbl ")\n",
2506 PTR2UV(sv), SvNVX(sv)));
2508 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"NVgf")\n",
2509 PTR2UV(sv), SvNVX(sv)));
2512 #ifdef NV_PRESERVES_UV
2513 (void)SvIOKp_on(sv);
2515 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2516 SvIVX(sv) = I_V(SvNVX(sv));
2517 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2520 /* Integer is imprecise. NOK, IOKp */
2522 /* UV will not work better than IV */
2524 if (SvNVX(sv) > (NV)UV_MAX) {
2526 /* Integer is inaccurate. NOK, IOKp, is UV */
2530 SvUVX(sv) = U_V(SvNVX(sv));
2531 /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
2532 NV preservse UV so can do correct comparison. */
2533 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2537 /* Integer is imprecise. NOK, IOKp, is UV */
2542 #else /* NV_PRESERVES_UV */
2543 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2544 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2545 /* The UV slot will have been set from value returned by
2546 grok_number above. The NV slot has just been set using
2549 assert (SvIOKp(sv));
2551 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2552 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2553 /* Small enough to preserve all bits. */
2554 (void)SvIOKp_on(sv);
2556 SvIVX(sv) = I_V(SvNVX(sv));
2557 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2559 /* Assumption: first non-preserved integer is < IV_MAX,
2560 this NV is in the preserved range, therefore: */
2561 if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
2563 Perl_croak(aTHX_ "sv_2uv assumed (U_V(fabs((double)SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%"NVgf" U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX);
2566 sv_2iuv_non_preserve (sv, numtype);
2568 #endif /* NV_PRESERVES_UV */
2572 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2573 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2576 if (SvTYPE(sv) < SVt_IV)
2577 /* Typically the caller expects that sv_any is not NULL now. */
2578 sv_upgrade(sv, SVt_IV);
2582 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
2583 PTR2UV(sv),SvUVX(sv)));
2584 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
2590 Return the num value of an SV, doing any necessary string or integer
2591 conversion, magic etc. Normally used via the C<SvNV(sv)> and C<SvNVx(sv)>
2598 Perl_sv_2nv(pTHX_ register SV *sv)
2602 if (SvGMAGICAL(sv)) {
2606 if (SvPOKp(sv) && SvLEN(sv)) {
2607 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) &&
2608 !grok_number(SvPVX(sv), SvCUR(sv), NULL))
2610 return Atof(SvPVX(sv));
2614 return (NV)SvUVX(sv);
2616 return (NV)SvIVX(sv);
2619 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2620 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2626 if (SvTHINKFIRST(sv)) {
2629 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,numer)) &&
2630 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv))))
2631 return SvNV(tmpstr);
2632 return PTR2NV(SvRV(sv));
2635 sv_force_normal_flags(sv, 0);
2637 if (SvREADONLY(sv) && !SvOK(sv)) {
2638 if (ckWARN(WARN_UNINITIALIZED))
2643 if (SvTYPE(sv) < SVt_NV) {
2644 if (SvTYPE(sv) == SVt_IV)
2645 sv_upgrade(sv, SVt_PVNV);
2647 sv_upgrade(sv, SVt_NV);
2648 #ifdef USE_LONG_DOUBLE
2650 STORE_NUMERIC_LOCAL_SET_STANDARD();
2651 PerlIO_printf(Perl_debug_log,
2652 "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
2653 PTR2UV(sv), SvNVX(sv));
2654 RESTORE_NUMERIC_LOCAL();
2658 STORE_NUMERIC_LOCAL_SET_STANDARD();
2659 PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n",
2660 PTR2UV(sv), SvNVX(sv));
2661 RESTORE_NUMERIC_LOCAL();
2665 else if (SvTYPE(sv) < SVt_PVNV)
2666 sv_upgrade(sv, SVt_PVNV);
2671 SvNVX(sv) = SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv);
2672 #ifdef NV_PRESERVES_UV
2675 /* Only set the public NV OK flag if this NV preserves the IV */
2676 /* Check it's not 0xFFFFFFFFFFFFFFFF */
2677 if (SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
2678 : (SvIVX(sv) == I_V(SvNVX(sv))))
2684 else if (SvPOKp(sv) && SvLEN(sv)) {
2686 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
2687 if (ckWARN(WARN_NUMERIC) && !SvIOKp(sv) && !numtype)
2689 #ifdef NV_PRESERVES_UV
2690 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2691 == IS_NUMBER_IN_UV) {
2692 /* It's definitely an integer */
2693 SvNVX(sv) = (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value;
2695 SvNVX(sv) = Atof(SvPVX(sv));
2698 SvNVX(sv) = Atof(SvPVX(sv));
2699 /* Only set the public NV OK flag if this NV preserves the value in
2700 the PV at least as well as an IV/UV would.
2701 Not sure how to do this 100% reliably. */
2702 /* if that shift count is out of range then Configure's test is
2703 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
2705 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2706 U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2707 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
2708 } else if (!(numtype & IS_NUMBER_IN_UV)) {
2709 /* Can't use strtol etc to convert this string, so don't try.
2710 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
2713 /* value has been set. It may not be precise. */
2714 if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
2715 /* 2s complement assumption for (UV)IV_MIN */
2716 SvNOK_on(sv); /* Integer is too negative. */
2721 if (numtype & IS_NUMBER_NEG) {
2722 SvIVX(sv) = -(IV)value;
2723 } else if (value <= (UV)IV_MAX) {
2724 SvIVX(sv) = (IV)value;
2730 if (numtype & IS_NUMBER_NOT_INT) {
2731 /* I believe that even if the original PV had decimals,
2732 they are lost beyond the limit of the FP precision.
2733 However, neither is canonical, so both only get p
2734 flags. NWC, 2000/11/25 */
2735 /* Both already have p flags, so do nothing */
2738 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2739 if (SvIVX(sv) == I_V(nv)) {
2744 /* It had no "." so it must be integer. */
2747 /* between IV_MAX and NV(UV_MAX).
2748 Could be slightly > UV_MAX */
2750 if (numtype & IS_NUMBER_NOT_INT) {
2751 /* UV and NV both imprecise. */
2753 UV nv_as_uv = U_V(nv);
2755 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
2766 #endif /* NV_PRESERVES_UV */
2769 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
2771 if (SvTYPE(sv) < SVt_NV)
2772 /* Typically the caller expects that sv_any is not NULL now. */
2773 /* XXX Ilya implies that this is a bug in callers that assume this
2774 and ideally should be fixed. */
2775 sv_upgrade(sv, SVt_NV);
2778 #if defined(USE_LONG_DOUBLE)
2780 STORE_NUMERIC_LOCAL_SET_STANDARD();
2781 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
2782 PTR2UV(sv), SvNVX(sv));
2783 RESTORE_NUMERIC_LOCAL();
2787 STORE_NUMERIC_LOCAL_SET_STANDARD();
2788 PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n",
2789 PTR2UV(sv), SvNVX(sv));
2790 RESTORE_NUMERIC_LOCAL();
2796 /* asIV(): extract an integer from the string value of an SV.
2797 * Caller must validate PVX */
2800 S_asIV(pTHX_ SV *sv)
2803 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
2805 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2806 == IS_NUMBER_IN_UV) {
2807 /* It's definitely an integer */
2808 if (numtype & IS_NUMBER_NEG) {
2809 if (value < (UV)IV_MIN)
2812 if (value < (UV)IV_MAX)
2817 if (ckWARN(WARN_NUMERIC))
2820 return I_V(Atof(SvPVX(sv)));
2823 /* asUV(): extract an unsigned integer from the string value of an SV
2824 * Caller must validate PVX */
2827 S_asUV(pTHX_ SV *sv)
2830 int numtype = grok_number(SvPVX(sv), SvCUR(sv), &value);
2832 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2833 == IS_NUMBER_IN_UV) {
2834 /* It's definitely an integer */
2835 if (!(numtype & IS_NUMBER_NEG))
2839 if (ckWARN(WARN_NUMERIC))
2842 return U_V(Atof(SvPVX(sv)));
2846 =for apidoc sv_2pv_nolen
2848 Like C<sv_2pv()>, but doesn't return the length too. You should usually
2849 use the macro wrapper C<SvPV_nolen(sv)> instead.
2854 Perl_sv_2pv_nolen(pTHX_ register SV *sv)
2857 return sv_2pv(sv, &n_a);
2860 /* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
2861 * UV as a string towards the end of buf, and return pointers to start and
2864 * We assume that buf is at least TYPE_CHARS(UV) long.
2868 uiv_2buf(char *buf, IV iv, UV uv, int is_uv, char **peob)
2870 char *ptr = buf + TYPE_CHARS(UV);
2884 *--ptr = '0' + (char)(uv % 10);
2892 /* sv_2pv() is now a macro using Perl_sv_2pv_flags();
2893 * this function provided for binary compatibility only
2897 Perl_sv_2pv(pTHX_ register SV *sv, STRLEN *lp)
2899 return sv_2pv_flags(sv, lp, SV_GMAGIC);
2903 =for apidoc sv_2pv_flags
2905 Returns a pointer to the string value of an SV, and sets *lp to its length.
2906 If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string
2908 Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg>
2909 usually end up here too.
2915 Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags)
2920 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
2921 char *tmpbuf = tbuf;
2927 if (SvGMAGICAL(sv)) {
2928 if (flags & SV_GMAGIC)
2936 (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
2938 (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
2943 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
2948 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2949 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2956 if (SvTHINKFIRST(sv)) {
2959 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) &&
2960 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2961 char *pv = SvPV(tmpstr, *lp);
2975 switch (SvTYPE(sv)) {
2977 if ( ((SvFLAGS(sv) &
2978 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
2979 == (SVs_OBJECT|SVs_SMG))
2980 && (mg = mg_find(sv, PERL_MAGIC_qr))) {
2981 regexp *re = (regexp *)mg->mg_obj;
2984 char *fptr = "msix";
2989 char need_newline = 0;
2990 U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12);
2992 while((ch = *fptr++)) {
2994 reflags[left++] = ch;
2997 reflags[right--] = ch;
3002 reflags[left] = '-';
3006 mg->mg_len = re->prelen + 4 + left;
3008 * If /x was used, we have to worry about a regex
3009 * ending with a comment later being embedded
3010 * within another regex. If so, we don't want this
3011 * regex's "commentization" to leak out to the
3012 * right part of the enclosing regex, we must cap
3013 * it with a newline.
3015 * So, if /x was used, we scan backwards from the
3016 * end of the regex. If we find a '#' before we
3017 * find a newline, we need to add a newline
3018 * ourself. If we find a '\n' first (or if we
3019 * don't find '#' or '\n'), we don't need to add
3020 * anything. -jfriedl
3022 if (PMf_EXTENDED & re->reganch)
3024 char *endptr = re->precomp + re->prelen;
3025 while (endptr >= re->precomp)
3027 char c = *(endptr--);
3029 break; /* don't need another */
3031 /* we end while in a comment, so we
3033 mg->mg_len++; /* save space for it */
3034 need_newline = 1; /* note to add it */
3040 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
3041 Copy("(?", mg->mg_ptr, 2, char);
3042 Copy(reflags, mg->mg_ptr+2, left, char);
3043 Copy(":", mg->mg_ptr+left+2, 1, char);
3044 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
3046 mg->mg_ptr[mg->mg_len - 2] = '\n';
3047 mg->mg_ptr[mg->mg_len - 1] = ')';
3048 mg->mg_ptr[mg->mg_len] = 0;
3050 PL_reginterp_cnt += re->program[0].next_off;
3052 if (re->reganch & ROPT_UTF8)
3067 case SVt_PVBM: if (SvROK(sv))
3070 s = "SCALAR"; break;
3071 case SVt_PVLV: s = "LVALUE"; break;
3072 case SVt_PVAV: s = "ARRAY"; break;
3073 case SVt_PVHV: s = "HASH"; break;
3074 case SVt_PVCV: s = "CODE"; break;
3075 case SVt_PVGV: s = "GLOB"; break;
3076 case SVt_PVFM: s = "FORMAT"; break;
3077 case SVt_PVIO: s = "IO"; break;
3078 default: s = "UNKNOWN"; break;
3082 Perl_sv_setpvf(aTHX_ tsv, "%s=%s", HvNAME(SvSTASH(sv)), s);
3085 Perl_sv_catpvf(aTHX_ tsv, "(0x%"UVxf")", PTR2UV(sv));
3091 if (SvREADONLY(sv) && !SvOK(sv)) {
3092 if (ckWARN(WARN_UNINITIALIZED))
3098 if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
3099 /* I'm assuming that if both IV and NV are equally valid then
3100 converting the IV is going to be more efficient */
3101 U32 isIOK = SvIOK(sv);
3102 U32 isUIOK = SvIsUV(sv);
3103 char buf[TYPE_CHARS(UV)];
3106 if (SvTYPE(sv) < SVt_PVIV)
3107 sv_upgrade(sv, SVt_PVIV);
3109 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
3111 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
3112 SvGROW(sv, (STRLEN)(ebuf - ptr + 1)); /* inlined from sv_setpvn */
3113 Move(ptr,SvPVX(sv),ebuf - ptr,char);
3114 SvCUR_set(sv, ebuf - ptr);
3124 else if (SvNOKp(sv)) {
3125 if (SvTYPE(sv) < SVt_PVNV)
3126 sv_upgrade(sv, SVt_PVNV);
3127 /* The +20 is pure guesswork. Configure test needed. --jhi */
3128 SvGROW(sv, NV_DIG + 20);
3130 olderrno = errno; /* some Xenix systems wipe out errno here */
3132 if (SvNVX(sv) == 0.0)
3133 (void)strcpy(s,"0");
3137 Gconvert(SvNVX(sv), NV_DIG, 0, s);
3140 #ifdef FIXNEGATIVEZERO
3141 if (*s == '-' && s[1] == '0' && !s[2])
3151 if (ckWARN(WARN_UNINITIALIZED)
3152 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
3155 if (SvTYPE(sv) < SVt_PV)
3156 /* Typically the caller expects that sv_any is not NULL now. */
3157 sv_upgrade(sv, SVt_PV);
3160 *lp = s - SvPVX(sv);
3163 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3164 PTR2UV(sv),SvPVX(sv)));
3168 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
3169 /* Sneaky stuff here */
3173 tsv = newSVpv(tmpbuf, 0);
3189 len = strlen(tmpbuf);
3191 #ifdef FIXNEGATIVEZERO
3192 if (len == 2 && t[0] == '-' && t[1] == '0') {
3197 (void)SvUPGRADE(sv, SVt_PV);
3199 s = SvGROW(sv, len + 1);
3208 =for apidoc sv_copypv
3210 Copies a stringified representation of the source SV into the
3211 destination SV. Automatically performs any necessary mg_get and
3212 coercion of numeric values into strings. Guaranteed to preserve
3213 UTF-8 flag even from overloaded objects. Similar in nature to
3214 sv_2pv[_flags] but operates directly on an SV instead of just the
3215 string. Mostly uses sv_2pv_flags to do its work, except when that
3216 would lose the UTF-8'ness of the PV.
3222 Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv)
3227 sv_setpvn(dsv,s,len);
3235 =for apidoc sv_2pvbyte_nolen
3237 Return a pointer to the byte-encoded representation of the SV.
3238 May cause the SV to be downgraded from UTF8 as a side-effect.
3240 Usually accessed via the C<SvPVbyte_nolen> macro.
3246 Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
3249 return sv_2pvbyte(sv, &n_a);
3253 =for apidoc sv_2pvbyte
3255 Return a pointer to the byte-encoded representation of the SV, and set *lp
3256 to its length. May cause the SV to be downgraded from UTF8 as a
3259 Usually accessed via the C<SvPVbyte> macro.
3265 Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
3267 sv_utf8_downgrade(sv,0);
3268 return SvPV(sv,*lp);
3272 =for apidoc sv_2pvutf8_nolen
3274 Return a pointer to the UTF8-encoded representation of the SV.
3275 May cause the SV to be upgraded to UTF8 as a side-effect.
3277 Usually accessed via the C<SvPVutf8_nolen> macro.
3283 Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
3286 return sv_2pvutf8(sv, &n_a);
3290 =for apidoc sv_2pvutf8
3292 Return a pointer to the UTF8-encoded representation of the SV, and set *lp
3293 to its length. May cause the SV to be upgraded to UTF8 as a side-effect.
3295 Usually accessed via the C<SvPVutf8> macro.
3301 Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
3303 sv_utf8_upgrade(sv);
3304 return SvPV(sv,*lp);
3308 =for apidoc sv_2bool
3310 This function is only called on magical items, and is only used by
3311 sv_true() or its macro equivalent.
3317 Perl_sv_2bool(pTHX_ register SV *sv)
3326 if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
3327 (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
3328 return (bool)SvTRUE(tmpsv);
3329 return SvRV(sv) != 0;
3332 register XPV* Xpvtmp;
3333 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
3334 (*Xpvtmp->xpv_pv > '0' ||
3335 Xpvtmp->xpv_cur > 1 ||
3336 (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0')))
3343 return SvIVX(sv) != 0;
3346 return SvNVX(sv) != 0.0;
3353 /* sv_utf8_upgrade() is now a macro using sv_utf8_upgrade_flags();
3354 * this function provided for binary compatibility only
3359 Perl_sv_utf8_upgrade(pTHX_ register SV *sv)
3361 return sv_utf8_upgrade_flags(sv, SV_GMAGIC);
3365 =for apidoc sv_utf8_upgrade
3367 Convert the PV of an SV to its UTF8-encoded form.
3368 Forces the SV to string form if it is not already.
3369 Always sets the SvUTF8 flag to avoid future validity checks even
3370 if all the bytes have hibit clear.
3372 This is not as a general purpose byte encoding to Unicode interface:
3373 use the Encode extension for that.
3375 =for apidoc sv_utf8_upgrade_flags
3377 Convert the PV of an SV to its UTF8-encoded form.
3378 Forces the SV to string form if it is not already.
3379 Always sets the SvUTF8 flag to avoid future validity checks even
3380 if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set,
3381 will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and
3382 C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
3384 This is not as a general purpose byte encoding to Unicode interface:
3385 use the Encode extension for that.
3391 Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags)
3401 (void) sv_2pv_flags(sv,&len, flags);
3410 sv_force_normal_flags(sv, 0);
3413 if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING))
3414 sv_recode_to_utf8(sv, PL_encoding);
3415 else { /* Assume Latin-1/EBCDIC */
3416 /* This function could be much more efficient if we
3417 * had a FLAG in SVs to signal if there are any hibit
3418 * chars in the PV. Given that there isn't such a flag
3419 * make the loop as fast as possible. */
3420 s = (U8 *) SvPVX(sv);
3421 e = (U8 *) SvEND(sv);
3425 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
3431 len = SvCUR(sv) + 1; /* Plus the \0 */
3432 SvPVX(sv) = (char*)bytes_to_utf8((U8*)s, &len);
3433 SvCUR(sv) = len - 1;
3435 Safefree(s); /* No longer using what was there before. */
3436 SvLEN(sv) = len; /* No longer know the real size. */
3438 /* Mark as UTF-8 even if no hibit - saves scanning loop */
3445 =for apidoc sv_utf8_downgrade
3447 Attempt to convert the PV of an SV from UTF8-encoded to byte encoding.
3448 This may not be possible if the PV contains non-byte encoding characters;
3449 if this is the case, either returns false or, if C<fail_ok> is not
3452 This is not as a general purpose Unicode to byte encoding interface:
3453 use the Encode extension for that.
3459 Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
3461 if (SvPOK(sv) && SvUTF8(sv)) {
3467 sv_force_normal_flags(sv, 0);
3469 s = (U8 *) SvPV(sv, len);
3470 if (!utf8_to_bytes(s, &len)) {
3475 Perl_croak(aTHX_ "Wide character in %s",
3478 Perl_croak(aTHX_ "Wide character");
3489 =for apidoc sv_utf8_encode
3491 Convert the PV of an SV to UTF8-encoded, but then turn off the C<SvUTF8>
3492 flag so that it looks like octets again. Used as a building block
3493 for encode_utf8 in Encode.xs
3499 Perl_sv_utf8_encode(pTHX_ register SV *sv)
3501 (void) sv_utf8_upgrade(sv);
3506 =for apidoc sv_utf8_decode
3508 Convert the octets in the PV from UTF-8 to chars. Scan for validity and then
3509 turn off SvUTF8 if needed so that we see characters. Used as a building block
3510 for decode_utf8 in Encode.xs
3516 Perl_sv_utf8_decode(pTHX_ register SV *sv)
3522 /* The octets may have got themselves encoded - get them back as
3525 if (!sv_utf8_downgrade(sv, TRUE))
3528 /* it is actually just a matter of turning the utf8 flag on, but
3529 * we want to make sure everything inside is valid utf8 first.
3531 c = (U8 *) SvPVX(sv);
3532 if (!is_utf8_string(c, SvCUR(sv)+1))
3534 e = (U8 *) SvEND(sv);
3537 if (!UTF8_IS_INVARIANT(ch)) {
3546 /* sv_setsv() is now a macro using Perl_sv_setsv_flags();
3547 * this function provided for binary compatibility only
3551 Perl_sv_setsv(pTHX_ SV *dstr, register SV *sstr)
3553 sv_setsv_flags(dstr, sstr, SV_GMAGIC);
3557 =for apidoc sv_setsv
3559 Copies the contents of the source SV C<ssv> into the destination SV
3560 C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3561 function if the source SV needs to be reused. Does not handle 'set' magic.
3562 Loosely speaking, it performs a copy-by-value, obliterating any previous
3563 content of the destination.
3565 You probably want to use one of the assortment of wrappers, such as
3566 C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3567 C<SvSetMagicSV_nosteal>.
3569 =for apidoc sv_setsv_flags
3571 Copies the contents of the source SV C<ssv> into the destination SV
3572 C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3573 function if the source SV needs to be reused. Does not handle 'set' magic.
3574 Loosely speaking, it performs a copy-by-value, obliterating any previous
3575 content of the destination.
3576 If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on
3577 C<ssv> if appropriate, else not. C<sv_setsv> and C<sv_setsv_nomg> are
3578 implemented in terms of this function.
3580 You probably want to use one of the assortment of wrappers, such as
3581 C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3582 C<SvSetMagicSV_nosteal>.
3584 This is the primary function for copying scalars, and most other
3585 copy-ish functions and macros use this underneath.
3591 Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags)
3593 register U32 sflags;
3599 SV_CHECK_THINKFIRST_COW_DROP(dstr);
3601 sstr = &PL_sv_undef;
3602 stype = SvTYPE(sstr);
3603 dtype = SvTYPE(dstr);
3608 /* need to nuke the magic */
3610 SvRMAGICAL_off(dstr);
3613 /* There's a lot of redundancy below but we're going for speed here */
3618 if (dtype != SVt_PVGV) {
3619 (void)SvOK_off(dstr);
3627 sv_upgrade(dstr, SVt_IV);
3630 sv_upgrade(dstr, SVt_PVNV);
3634 sv_upgrade(dstr, SVt_PVIV);
3637 (void)SvIOK_only(dstr);
3638 SvIVX(dstr) = SvIVX(sstr);
3641 if (SvTAINTED(sstr))
3652 sv_upgrade(dstr, SVt_NV);
3657 sv_upgrade(dstr, SVt_PVNV);
3660 SvNVX(dstr) = SvNVX(sstr);
3661 (void)SvNOK_only(dstr);
3662 if (SvTAINTED(sstr))
3670 sv_upgrade(dstr, SVt_RV);
3671 else if (dtype == SVt_PVGV &&
3672 SvTYPE(SvRV(sstr)) == SVt_PVGV) {
3675 if (GvIMPORTED(dstr) != GVf_IMPORTED
3676 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3678 GvIMPORTED_on(dstr);
3689 sv_upgrade(dstr, SVt_PV);
3692 if (dtype < SVt_PVIV)
3693 sv_upgrade(dstr, SVt_PVIV);
3696 if (dtype < SVt_PVNV)
3697 sv_upgrade(dstr, SVt_PVNV);
3704 Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0),
3707 Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0));
3711 if (dtype <= SVt_PVGV) {
3713 if (dtype != SVt_PVGV) {
3714 char *name = GvNAME(sstr);
3715 STRLEN len = GvNAMELEN(sstr);
3716 sv_upgrade(dstr, SVt_PVGV);
3717 sv_magic(dstr, dstr, PERL_MAGIC_glob, Nullch, 0);
3718 GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr));
3719 GvNAME(dstr) = savepvn(name, len);
3720 GvNAMELEN(dstr) = len;
3721 SvFAKE_on(dstr); /* can coerce to non-glob */
3723 /* ahem, death to those who redefine active sort subs */
3724 else if (PL_curstackinfo->si_type == PERLSI_SORT
3725 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
3726 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
3729 #ifdef GV_UNIQUE_CHECK
3730 if (GvUNIQUE((GV*)dstr)) {
3731 Perl_croak(aTHX_ PL_no_modify);
3735 (void)SvOK_off(dstr);
3736 GvINTRO_off(dstr); /* one-shot flag */
3738 GvGP(dstr) = gp_ref(GvGP(sstr));
3739 if (SvTAINTED(sstr))
3741 if (GvIMPORTED(dstr) != GVf_IMPORTED
3742 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3744 GvIMPORTED_on(dstr);
3752 if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) {
3754 if ((int)SvTYPE(sstr) != stype) {
3755 stype = SvTYPE(sstr);
3756 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
3760 if (stype == SVt_PVLV)
3761 (void)SvUPGRADE(dstr, SVt_PVNV);
3763 (void)SvUPGRADE(dstr, (U32)stype);
3766 sflags = SvFLAGS(sstr);
3768 if (sflags & SVf_ROK) {
3769 if (dtype >= SVt_PV) {
3770 if (dtype == SVt_PVGV) {
3771 SV *sref = SvREFCNT_inc(SvRV(sstr));
3773 int intro = GvINTRO(dstr);
3775 #ifdef GV_UNIQUE_CHECK
3776 if (GvUNIQUE((GV*)dstr)) {
3777 Perl_croak(aTHX_ PL_no_modify);
3782 GvINTRO_off(dstr); /* one-shot flag */
3783 GvLINE(dstr) = CopLINE(PL_curcop);
3784 GvEGV(dstr) = (GV*)dstr;
3787 switch (SvTYPE(sref)) {
3790 SAVEGENERICSV(GvAV(dstr));
3792 dref = (SV*)GvAV(dstr);
3793 GvAV(dstr) = (AV*)sref;
3794 if (!GvIMPORTED_AV(dstr)
3795 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3797 GvIMPORTED_AV_on(dstr);
3802 SAVEGENERICSV(GvHV(dstr));
3804 dref = (SV*)GvHV(dstr);
3805 GvHV(dstr) = (HV*)sref;
3806 if (!GvIMPORTED_HV(dstr)
3807 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3809 GvIMPORTED_HV_on(dstr);
3814 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
3815 SvREFCNT_dec(GvCV(dstr));
3816 GvCV(dstr) = Nullcv;
3817 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3818 PL_sub_generation++;
3820 SAVEGENERICSV(GvCV(dstr));
3823 dref = (SV*)GvCV(dstr);
3824 if (GvCV(dstr) != (CV*)sref) {
3825 CV* cv = GvCV(dstr);
3827 if (!GvCVGEN((GV*)dstr) &&
3828 (CvROOT(cv) || CvXSUB(cv)))
3830 /* ahem, death to those who redefine
3831 * active sort subs */
3832 if (PL_curstackinfo->si_type == PERLSI_SORT &&
3833 PL_sortcop == CvSTART(cv))
3835 "Can't redefine active sort subroutine %s",
3836 GvENAME((GV*)dstr));
3837 /* Redefining a sub - warning is mandatory if
3838 it was a const and its value changed. */
3839 if (ckWARN(WARN_REDEFINE)
3841 && (!CvCONST((CV*)sref)
3842 || sv_cmp(cv_const_sv(cv),
3843 cv_const_sv((CV*)sref)))))
3845 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
3847 ? "Constant subroutine %s::%s redefined"
3848 : "Subroutine %s::%s redefined",
3849 HvNAME(GvSTASH((GV*)dstr)),
3850 GvENAME((GV*)dstr));
3854 cv_ckproto(cv, (GV*)dstr,
3855 SvPOK(sref) ? SvPVX(sref) : Nullch);
3857 GvCV(dstr) = (CV*)sref;
3858 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3859 GvASSUMECV_on(dstr);
3860 PL_sub_generation++;
3862 if (!GvIMPORTED_CV(dstr)
3863 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3865 GvIMPORTED_CV_on(dstr);
3870 SAVEGENERICSV(GvIOp(dstr));
3872 dref = (SV*)GvIOp(dstr);
3873 GvIOp(dstr) = (IO*)sref;
3877 SAVEGENERICSV(GvFORM(dstr));
3879 dref = (SV*)GvFORM(dstr);
3880 GvFORM(dstr) = (CV*)sref;
3884 SAVEGENERICSV(GvSV(dstr));
3886 dref = (SV*)GvSV(dstr);
3888 if (!GvIMPORTED_SV(dstr)
3889 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3891 GvIMPORTED_SV_on(dstr);
3897 if (SvTAINTED(sstr))
3902 (void)SvOOK_off(dstr); /* backoff */
3904 Safefree(SvPVX(dstr));
3905 SvLEN(dstr)=SvCUR(dstr)=0;
3908 (void)SvOK_off(dstr);
3909 SvRV(dstr) = SvREFCNT_inc(SvRV(sstr));
3911 if (sflags & SVp_NOK) {
3913 /* Only set the public OK flag if the source has public OK. */
3914 if (sflags & SVf_NOK)
3915 SvFLAGS(dstr) |= SVf_NOK;
3916 SvNVX(dstr) = SvNVX(sstr);
3918 if (sflags & SVp_IOK) {
3919 (void)SvIOKp_on(dstr);
3920 if (sflags & SVf_IOK)
3921 SvFLAGS(dstr) |= SVf_IOK;
3922 if (sflags & SVf_IVisUV)
3924 SvIVX(dstr) = SvIVX(sstr);
3926 if (SvAMAGIC(sstr)) {
3930 else if (sflags & SVp_POK) {
3934 * Check to see if we can just swipe the string. If so, it's a
3935 * possible small lose on short strings, but a big win on long ones.
3936 * It might even be a win on short strings if SvPVX(dstr)
3937 * has to be allocated and SvPVX(sstr) has to be freed.
3941 #ifdef PERL_COPY_ON_WRITE
3942 (sflags & (SVf_FAKE | SVf_READONLY)) != (SVf_FAKE | SVf_READONLY)
3946 (sflags & SVs_TEMP) && /* slated for free anyway? */
3947 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
3948 SvREFCNT(sstr) == 1 && /* and no other references to it? */
3949 SvLEN(sstr) && /* and really is a string */
3950 /* and won't be needed again, potentially */
3951 !(PL_op && PL_op->op_type == OP_AASSIGN))
3952 #ifdef PERL_COPY_ON_WRITE
3953 && !((sflags & CAN_COW_MASK) == CAN_COW_FLAGS
3954 && SvTYPE(sstr) >= SVt_PVIV)
3957 /* Failed the swipe test, and it's not a shared hash key either.
3958 Have to copy the string. */
3959 STRLEN len = SvCUR(sstr);
3960 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
3961 Move(SvPVX(sstr),SvPVX(dstr),len,char);
3962 SvCUR_set(dstr, len);
3963 *SvEND(dstr) = '\0';
3964 (void)SvPOK_only(dstr);
3966 /* If PERL_COPY_ON_WRITE is not defined, then isSwipe will always
3968 #ifdef PERL_COPY_ON_WRITE
3969 /* Either it's a shared hash key, or it's suitable for
3970 copy-on-write or we can swipe the string. */
3972 PerlIO_printf(Perl_debug_log, "Copy on write: sstr --> dstr\n");
3977 /* I believe I should acquire a global SV mutex if
3978 it's a COW sv (not a shared hash key) to stop
3979 it going un copy-on-write.
3980 If the source SV has gone un copy on write between up there
3981 and down here, then (assert() that) it is of the correct
3982 form to make it copy on write again */
3983 if ((sflags & (SVf_FAKE | SVf_READONLY))
3984 != (SVf_FAKE | SVf_READONLY)) {
3985 SvREADONLY_on(sstr);
3987 /* Make the source SV into a loop of 1.
3988 (about to become 2) */
3989 SV_COW_NEXT_SV_SET(sstr, sstr);
3993 /* Initial code is common. */
3994 if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */
3996 SvFLAGS(dstr) &= ~SVf_OOK;
3997 Safefree(SvPVX(dstr) - SvIVX(dstr));
3999 else if (SvLEN(dstr))
4000 Safefree(SvPVX(dstr));
4002 (void)SvPOK_only(dstr);
4004 #ifdef PERL_COPY_ON_WRITE
4006 /* making another shared SV. */
4007 STRLEN cur = SvCUR(sstr);
4008 STRLEN len = SvLEN(sstr);
4010 /* SvIsCOW_normal */
4011 /* splice us in between source and next-after-source. */
4012 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
4013 SV_COW_NEXT_SV_SET(sstr, dstr);
4014 SvPV_set(dstr, SvPVX(sstr));
4016 /* SvIsCOW_shared_hash */
4017 UV hash = SvUVX(sstr);
4018 DEBUG_C(PerlIO_printf(Perl_debug_log,
4019 "Copy on write: Sharing hash\n"));
4021 sharepvn(SvPVX(sstr),
4022 (sflags & SVf_UTF8?-cur:cur), hash));
4027 SvREADONLY_on(dstr);
4029 /* Relesase a global SV mutex. */
4033 { /* Passes the swipe test. */
4034 SvPV_set(dstr, SvPVX(sstr));
4035 SvLEN_set(dstr, SvLEN(sstr));
4036 SvCUR_set(dstr, SvCUR(sstr));
4039 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
4040 SvPV_set(sstr, Nullch);
4046 if (sflags & SVf_UTF8)
4049 if (sflags & SVp_NOK) {
4051 if (sflags & SVf_NOK)
4052 SvFLAGS(dstr) |= SVf_NOK;
4053 SvNVX(dstr) = SvNVX(sstr);
4055 if (sflags & SVp_IOK) {
4056 (void)SvIOKp_on(dstr);
4057 if (sflags & SVf_IOK)
4058 SvFLAGS(dstr) |= SVf_IOK;
4059 if (sflags & SVf_IVisUV)
4061 SvIVX(dstr) = SvIVX(sstr);
4064 MAGIC *smg = mg_find(sstr,PERL_MAGIC_vstring);
4065 sv_magic(dstr, NULL, PERL_MAGIC_vstring,
4066 smg->mg_ptr, smg->mg_len);
4067 SvRMAGICAL_on(dstr);
4070 else if (sflags & SVp_IOK) {
4071 if (sflags & SVf_IOK)
4072 (void)SvIOK_only(dstr);
4074 (void)SvOK_off(dstr);
4075 (void)SvIOKp_on(dstr);
4077 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
4078 if (sflags & SVf_IVisUV)
4080 SvIVX(dstr) = SvIVX(sstr);
4081 if (sflags & SVp_NOK) {
4082 if (sflags & SVf_NOK)
4083 (void)SvNOK_on(dstr);
4085 (void)SvNOKp_on(dstr);
4086 SvNVX(dstr) = SvNVX(sstr);
4089 else if (sflags & SVp_NOK) {
4090 if (sflags & SVf_NOK)
4091 (void)SvNOK_only(dstr);
4093 (void)SvOK_off(dstr);
4096 SvNVX(dstr) = SvNVX(sstr);
4099 if (dtype == SVt_PVGV) {
4100 if (ckWARN(WARN_MISC))
4101 Perl_warner(aTHX_ packWARN(WARN_MISC), "Undefined value assigned to typeglob");
4104 (void)SvOK_off(dstr);
4106 if (SvTAINTED(sstr))
4111 =for apidoc sv_setsv_mg
4113 Like C<sv_setsv>, but also handles 'set' magic.
4119 Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
4121 sv_setsv(dstr,sstr);
4125 #ifdef PERL_COPY_ON_WRITE
4127 Perl_sv_setsv_cow(pTHX_ SV *dstr, SV *sstr)
4129 STRLEN cur = SvCUR(sstr);
4130 STRLEN len = SvLEN(sstr);
4131 register char *new_pv;
4134 PerlIO_printf(Perl_debug_log, "Fast copy on write: %p -> %p\n",
4142 if (SvTHINKFIRST(dstr))
4143 sv_force_normal_flags(dstr, SV_COW_DROP_PV);
4144 else if (SvPVX(dstr))
4145 Safefree(SvPVX(dstr));
4149 SvUPGRADE (dstr, SVt_PVIV);
4151 assert (SvPOK(sstr));
4152 assert (SvPOKp(sstr));
4153 assert (!SvIOK(sstr));
4154 assert (!SvIOKp(sstr));
4155 assert (!SvNOK(sstr));
4156 assert (!SvNOKp(sstr));
4158 if (SvIsCOW(sstr)) {
4160 if (SvLEN(sstr) == 0) {
4161 /* source is a COW shared hash key. */
4162 UV hash = SvUVX(sstr);
4163 DEBUG_C(PerlIO_printf(Perl_debug_log,
4164 "Fast copy on write: Sharing hash\n"));
4166 new_pv = sharepvn(SvPVX(sstr), (SvUTF8(sstr)?-cur:cur), hash);
4169 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
4171 assert ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS);
4172 SvUPGRADE (sstr, SVt_PVIV);
4173 SvREADONLY_on(sstr);
4175 DEBUG_C(PerlIO_printf(Perl_debug_log,
4176 "Fast copy on write: Converting sstr to COW\n"));
4177 SV_COW_NEXT_SV_SET(dstr, sstr);
4179 SV_COW_NEXT_SV_SET(sstr, dstr);
4180 new_pv = SvPVX(sstr);
4183 SvPV_set(dstr, new_pv);
4184 SvFLAGS(dstr) = (SVt_PVIV|SVf_POK|SVp_POK|SVf_FAKE|SVf_READONLY);
4197 =for apidoc sv_setpvn
4199 Copies a string into an SV. The C<len> parameter indicates the number of
4200 bytes to be copied. Does not handle 'set' magic. See C<sv_setpvn_mg>.
4206 Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
4208 register char *dptr;
4210 SV_CHECK_THINKFIRST_COW_DROP(sv);
4216 /* len is STRLEN which is unsigned, need to copy to signed */
4219 Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen");
4221 (void)SvUPGRADE(sv, SVt_PV);
4223 SvGROW(sv, len + 1);
4225 Move(ptr,dptr,len,char);
4228 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4233 =for apidoc sv_setpvn_mg
4235 Like C<sv_setpvn>, but also handles 'set' magic.
4241 Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
4243 sv_setpvn(sv,ptr,len);
4248 =for apidoc sv_setpv
4250 Copies a string into an SV. The string must be null-terminated. Does not
4251 handle 'set' magic. See C<sv_setpv_mg>.
4257 Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr)
4259 register STRLEN len;
4261 SV_CHECK_THINKFIRST_COW_DROP(sv);
4267 (void)SvUPGRADE(sv, SVt_PV);
4269 SvGROW(sv, len + 1);
4270 Move(ptr,SvPVX(sv),len+1,char);
4272 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4277 =for apidoc sv_setpv_mg
4279 Like C<sv_setpv>, but also handles 'set' magic.
4285 Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr)
4292 =for apidoc sv_usepvn
4294 Tells an SV to use C<ptr> to find its string value. Normally the string is
4295 stored inside the SV but sv_usepvn allows the SV to use an outside string.
4296 The C<ptr> should point to memory that was allocated by C<malloc>. The
4297 string length, C<len>, must be supplied. This function will realloc the
4298 memory pointed to by C<ptr>, so that pointer should not be freed or used by
4299 the programmer after giving it to sv_usepvn. Does not handle 'set' magic.
4300 See C<sv_usepvn_mg>.
4306 Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
4308 SV_CHECK_THINKFIRST_COW_DROP(sv);
4309 (void)SvUPGRADE(sv, SVt_PV);
4314 (void)SvOOK_off(sv);
4315 if (SvPVX(sv) && SvLEN(sv))
4316 Safefree(SvPVX(sv));
4317 Renew(ptr, len+1, char);
4320 SvLEN_set(sv, len+1);
4322 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4327 =for apidoc sv_usepvn_mg
4329 Like C<sv_usepvn>, but also handles 'set' magic.
4335 Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
4337 sv_usepvn(sv,ptr,len);
4341 #ifdef PERL_COPY_ON_WRITE
4342 /* Need to do this *after* making the SV normal, as we need the buffer
4343 pointer to remain valid until after we've copied it. If we let go too early,
4344 another thread could invalidate it by unsharing last of the same hash key
4345 (which it can do by means other than releasing copy-on-write Svs)
4346 or by changing the other copy-on-write SVs in the loop. */
4348 S_sv_release_COW(pTHX_ register SV *sv, char *pvx, STRLEN cur, STRLEN len,
4349 U32 hash, SV *after)
4351 if (len) { /* this SV was SvIsCOW_normal(sv) */
4352 /* we need to find the SV pointing to us. */
4353 SV *current = SV_COW_NEXT_SV(after);
4355 if (current == sv) {
4356 /* The SV we point to points back to us (there were only two of us
4358 Hence other SV is no longer copy on write either. */
4360 SvREADONLY_off(after);
4362 /* We need to follow the pointers around the loop. */
4364 while ((next = SV_COW_NEXT_SV(current)) != sv) {
4367 /* don't loop forever if the structure is bust, and we have
4368 a pointer into a closed loop. */
4369 assert (current != after);
4370 assert (SvPVX(current) == pvx);
4372 /* Make the SV before us point to the SV after us. */
4373 SV_COW_NEXT_SV_SET(current, after);
4376 unsharepvn(pvx, SvUTF8(sv) ? -(I32)cur : cur, hash);
4381 Perl_sv_release_IVX(pTHX_ register SV *sv)
4384 sv_force_normal_flags(sv, 0);
4385 return SvOOK_off(sv);
4389 =for apidoc sv_force_normal_flags
4391 Undo various types of fakery on an SV: if the PV is a shared string, make
4392 a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
4393 an xpvmg; if we're a copy-on-write scalar, this is the on-write time when
4394 we do the copy, and is also used locally. If C<SV_COW_DROP_PV> is set
4395 then a copy-on-write scalar drops its PV buffer (if any) and becomes
4396 SvPOK_off rather than making a copy. (Used where this scalar is about to be
4397 set to some other value.) In addition, the C<flags> parameter gets passed to
4398 C<sv_unref_flags()> when unrefing. C<sv_force_normal> calls this function
4399 with flags set to 0.
4405 Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags)
4407 #ifdef PERL_COPY_ON_WRITE
4408 if (SvREADONLY(sv)) {
4409 /* At this point I believe I should acquire a global SV mutex. */
4411 char *pvx = SvPVX(sv);
4412 STRLEN len = SvLEN(sv);
4413 STRLEN cur = SvCUR(sv);
4414 U32 hash = SvUVX(sv);
4415 SV *next = SV_COW_NEXT_SV(sv); /* next COW sv in the loop. */
4417 PerlIO_printf(Perl_debug_log,
4418 "Copy on write: Force normal %ld\n",
4424 /* This SV doesn't own the buffer, so need to New() a new one: */
4427 if (flags & SV_COW_DROP_PV) {
4428 /* OK, so we don't need to copy our buffer. */
4431 SvGROW(sv, cur + 1);
4432 Move(pvx,SvPVX(sv),cur,char);
4436 sv_release_COW(sv, pvx, cur, len, hash, next);
4441 else if (PL_curcop != &PL_compiling)
4442 Perl_croak(aTHX_ PL_no_modify);
4443 /* At this point I believe that I can drop the global SV mutex. */
4446 if (SvREADONLY(sv)) {
4448 char *pvx = SvPVX(sv);
4449 STRLEN len = SvCUR(sv);
4450 U32 hash = SvUVX(sv);
4451 SvGROW(sv, len + 1);
4452 Move(pvx,SvPVX(sv),len,char);
4456 unsharepvn(pvx, SvUTF8(sv) ? -(I32)len : len, hash);
4458 else if (PL_curcop != &PL_compiling)
4459 Perl_croak(aTHX_ PL_no_modify);
4463 sv_unref_flags(sv, flags);
4464 else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
4469 =for apidoc sv_force_normal
4471 Undo various types of fakery on an SV: if the PV is a shared string, make
4472 a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
4473 an xpvmg. See also C<sv_force_normal_flags>.
4479 Perl_sv_force_normal(pTHX_ register SV *sv)
4481 sv_force_normal_flags(sv, 0);
4487 Efficient removal of characters from the beginning of the string buffer.
4488 SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside
4489 the string buffer. The C<ptr> becomes the first character of the adjusted
4490 string. Uses the "OOK hack".
4496 Perl_sv_chop(pTHX_ register SV *sv, register char *ptr)
4498 register STRLEN delta;
4500 if (!ptr || !SvPOKp(sv))
4502 SV_CHECK_THINKFIRST(sv);
4503 if (SvTYPE(sv) < SVt_PVIV)
4504 sv_upgrade(sv,SVt_PVIV);
4507 if (!SvLEN(sv)) { /* make copy of shared string */
4508 char *pvx = SvPVX(sv);
4509 STRLEN len = SvCUR(sv);
4510 SvGROW(sv, len + 1);
4511 Move(pvx,SvPVX(sv),len,char);
4515 /* Same SvOOK_on but SvOOK_on does a SvIOK_off
4516 and we do that anyway inside the SvNIOK_off
4518 SvFLAGS(sv) |= SVf_OOK;
4521 delta = ptr - SvPVX(sv);
4528 /* sv_catpvn() is now a macro using Perl_sv_catpvn_flags();
4529 * this function provided for binary compatibility only
4533 Perl_sv_catpvn(pTHX_ SV *dsv, const char* sstr, STRLEN slen)
4535 sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC);
4539 =for apidoc sv_catpvn
4541 Concatenates the string onto the end of the string which is in the SV. The
4542 C<len> indicates number of bytes to copy. If the SV has the UTF8
4543 status set, then the bytes appended should be valid UTF8.
4544 Handles 'get' magic, but not 'set' magic. See C<sv_catpvn_mg>.
4546 =for apidoc sv_catpvn_flags
4548 Concatenates the string onto the end of the string which is in the SV. The
4549 C<len> indicates number of bytes to copy. If the SV has the UTF8
4550 status set, then the bytes appended should be valid UTF8.
4551 If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<dsv> if
4552 appropriate, else not. C<sv_catpvn> and C<sv_catpvn_nomg> are implemented
4553 in terms of this function.
4559 Perl_sv_catpvn_flags(pTHX_ register SV *dsv, register const char *sstr, register STRLEN slen, I32 flags)
4564 dstr = SvPV_force_flags(dsv, dlen, flags);
4565 SvGROW(dsv, dlen + slen + 1);
4568 Move(sstr, SvPVX(dsv) + dlen, slen, char);
4571 (void)SvPOK_only_UTF8(dsv); /* validate pointer */
4576 =for apidoc sv_catpvn_mg
4578 Like C<sv_catpvn>, but also handles 'set' magic.
4584 Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
4586 sv_catpvn(sv,ptr,len);
4590 /* sv_catsv() is now a macro using Perl_sv_catsv_flags();
4591 * this function provided for binary compatibility only
4595 Perl_sv_catsv(pTHX_ SV *dstr, register SV *sstr)
4597 sv_catsv_flags(dstr, sstr, SV_GMAGIC);
4601 =for apidoc sv_catsv
4603 Concatenates the string from SV C<ssv> onto the end of the string in
4604 SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but
4605 not 'set' magic. See C<sv_catsv_mg>.
4607 =for apidoc sv_catsv_flags
4609 Concatenates the string from SV C<ssv> onto the end of the string in
4610 SV C<dsv>. Modifies C<dsv> but not C<ssv>. If C<flags> has C<SV_GMAGIC>
4611 bit set, will C<mg_get> on the SVs if appropriate, else not. C<sv_catsv>
4612 and C<sv_catsv_nomg> are implemented in terms of this function.
4617 Perl_sv_catsv_flags(pTHX_ SV *dsv, register SV *ssv, I32 flags)
4623 if ((spv = SvPV(ssv, slen))) {
4624 /* sutf8 and dutf8 were type bool, but under USE_ITHREADS,
4625 gcc version 2.95.2 20000220 (Debian GNU/Linux) for
4626 Linux xxx 2.2.17 on sparc64 with gcc -O2, we erroneously
4627 get dutf8 = 0x20000000, (i.e. SVf_UTF8) even though
4628 dsv->sv_flags doesn't have that bit set.
4629 Andy Dougherty 12 Oct 2001
4631 I32 sutf8 = DO_UTF8(ssv);
4634 if (SvGMAGICAL(dsv) && (flags & SV_GMAGIC))
4636 dutf8 = DO_UTF8(dsv);
4638 if (dutf8 != sutf8) {
4640 /* Not modifying source SV, so taking a temporary copy. */
4641 SV* csv = sv_2mortal(newSVpvn(spv, slen));
4643 sv_utf8_upgrade(csv);
4644 spv = SvPV(csv, slen);
4647 sv_utf8_upgrade_nomg(dsv);
4649 sv_catpvn_nomg(dsv, spv, slen);
4654 =for apidoc sv_catsv_mg
4656 Like C<sv_catsv>, but also handles 'set' magic.
4662 Perl_sv_catsv_mg(pTHX_ SV *dsv, register SV *ssv)
4669 =for apidoc sv_catpv
4671 Concatenates the string onto the end of the string which is in the SV.
4672 If the SV has the UTF8 status set, then the bytes appended should be
4673 valid UTF8. Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>.
4678 Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr)
4680 register STRLEN len;
4686 junk = SvPV_force(sv, tlen);
4688 SvGROW(sv, tlen + len + 1);
4691 Move(ptr,SvPVX(sv)+tlen,len+1,char);
4693 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4698 =for apidoc sv_catpv_mg
4700 Like C<sv_catpv>, but also handles 'set' magic.
4706 Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr)
4715 Create a new null SV, or if len > 0, create a new empty SVt_PV type SV
4716 with an initial PV allocation of len+1. Normally accessed via the C<NEWSV>
4723 Perl_newSV(pTHX_ STRLEN len)
4729 sv_upgrade(sv, SVt_PV);
4730 SvGROW(sv, len + 1);
4735 =for apidoc sv_magicext
4737 Adds magic to an SV, upgrading it if necessary. Applies the
4738 supplied vtable and returns pointer to the magic added.
4740 Note that sv_magicext will allow things that sv_magic will not.
4741 In particular you can add magic to SvREADONLY SVs and and more than
4742 one instance of the same 'how'
4744 I C<namelen> is greater then zero then a savepvn() I<copy> of C<name> is stored,
4745 if C<namelen> is zero then C<name> is stored as-is and - as another special
4746 case - if C<(name && namelen == HEf_SVKEY)> then C<name> is assumed to contain
4747 an C<SV*> and has its REFCNT incremented
4749 (This is now used as a subroutine by sv_magic.)
4754 Perl_sv_magicext(pTHX_ SV* sv, SV* obj, int how, MGVTBL *vtable,
4755 const char* name, I32 namlen)
4759 if (SvTYPE(sv) < SVt_PVMG) {
4760 (void)SvUPGRADE(sv, SVt_PVMG);
4762 Newz(702,mg, 1, MAGIC);
4763 mg->mg_moremagic = SvMAGIC(sv);
4766 /* Some magic sontains a reference loop, where the sv and object refer to
4767 each other. To prevent a reference loop that would prevent such
4768 objects being freed, we look for such loops and if we find one we
4769 avoid incrementing the object refcount.
4771 Note we cannot do this to avoid self-tie loops as intervening RV must
4772 have its REFCNT incremented to keep it in existence.
4775 if (!obj || obj == sv ||
4776 how == PERL_MAGIC_arylen ||
4777 how == PERL_MAGIC_qr ||
4778 (SvTYPE(obj) == SVt_PVGV &&
4779 (GvSV(obj) == sv || GvHV(obj) == (HV*)sv || GvAV(obj) == (AV*)sv ||
4780 GvCV(obj) == (CV*)sv || GvIOp(obj) == (IO*)sv ||
4781 GvFORM(obj) == (CV*)sv)))
4786 mg->mg_obj = SvREFCNT_inc(obj);
4787 mg->mg_flags |= MGf_REFCOUNTED;
4790 /* Normal self-ties simply pass a null object, and instead of
4791 using mg_obj directly, use the SvTIED_obj macro to produce a
4792 new RV as needed. For glob "self-ties", we are tieing the PVIO
4793 with an RV obj pointing to the glob containing the PVIO. In
4794 this case, to avoid a reference loop, we need to weaken the
4798 if (how == PERL_MAGIC_tiedscalar && SvTYPE(sv) == SVt_PVIO &&