3 * Copyright (c) 1991-2002, 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(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(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);
2893 =for apidoc sv_2pv_flags
2895 Returns a pointer to the string value of an SV, and sets *lp to its length.
2896 If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a string
2898 Normally invoked via the C<SvPV_flags> macro. C<sv_2pv()> and C<sv_2pv_nomg>
2899 usually end up here too.
2905 Perl_sv_2pv_flags(pTHX_ register SV *sv, STRLEN *lp, I32 flags)
2910 char tbuf[64]; /* Must fit sprintf/Gconvert of longest IV/NV */
2911 char *tmpbuf = tbuf;
2917 if (SvGMAGICAL(sv)) {
2918 if (flags & SV_GMAGIC)
2926 (void)sprintf(tmpbuf,"%"UVuf, (UV)SvUVX(sv));
2928 (void)sprintf(tmpbuf,"%"IVdf, (IV)SvIVX(sv));
2933 Gconvert(SvNVX(sv), NV_DIG, 0, tmpbuf);
2938 if (!(SvFLAGS(sv) & SVs_PADTMP)) {
2939 if (ckWARN(WARN_UNINITIALIZED) && !PL_localizing)
2946 if (SvTHINKFIRST(sv)) {
2949 if (SvAMAGIC(sv) && (tmpstr=AMG_CALLun(sv,string)) &&
2950 (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2951 char *pv = SvPV(tmpstr, *lp);
2965 switch (SvTYPE(sv)) {
2967 if ( ((SvFLAGS(sv) &
2968 (SVs_OBJECT|SVf_OK|SVs_GMG|SVs_SMG|SVs_RMG))
2969 == (SVs_OBJECT|SVs_RMG))
2970 && (mg = mg_find(sv, PERL_MAGIC_qr))) {
2971 regexp *re = (regexp *)mg->mg_obj;
2974 char *fptr = "msix";
2979 char need_newline = 0;
2980 U16 reganch = (U16)((re->reganch & PMf_COMPILETIME) >> 12);
2982 while((ch = *fptr++)) {
2984 reflags[left++] = ch;
2987 reflags[right--] = ch;
2992 reflags[left] = '-';
2996 mg->mg_len = re->prelen + 4 + left;
2998 * If /x was used, we have to worry about a regex
2999 * ending with a comment later being embedded
3000 * within another regex. If so, we don't want this
3001 * regex's "commentization" to leak out to the
3002 * right part of the enclosing regex, we must cap
3003 * it with a newline.
3005 * So, if /x was used, we scan backwards from the
3006 * end of the regex. If we find a '#' before we
3007 * find a newline, we need to add a newline
3008 * ourself. If we find a '\n' first (or if we
3009 * don't find '#' or '\n'), we don't need to add
3010 * anything. -jfriedl
3012 if (PMf_EXTENDED & re->reganch)
3014 char *endptr = re->precomp + re->prelen;
3015 while (endptr >= re->precomp)
3017 char c = *(endptr--);
3019 break; /* don't need another */
3021 /* we end while in a comment, so we
3023 mg->mg_len++; /* save space for it */
3024 need_newline = 1; /* note to add it */
3030 New(616, mg->mg_ptr, mg->mg_len + 1 + left, char);
3031 Copy("(?", mg->mg_ptr, 2, char);
3032 Copy(reflags, mg->mg_ptr+2, left, char);
3033 Copy(":", mg->mg_ptr+left+2, 1, char);
3034 Copy(re->precomp, mg->mg_ptr+3+left, re->prelen, char);
3036 mg->mg_ptr[mg->mg_len - 2] = '\n';
3037 mg->mg_ptr[mg->mg_len - 1] = ')';
3038 mg->mg_ptr[mg->mg_len] = 0;
3040 PL_reginterp_cnt += re->program[0].next_off;
3042 if (re->reganch & ROPT_UTF8)
3057 case SVt_PVBM: if (SvROK(sv))
3060 s = "SCALAR"; break;
3061 case SVt_PVLV: s = "LVALUE"; break;
3062 case SVt_PVAV: s = "ARRAY"; break;
3063 case SVt_PVHV: s = "HASH"; break;
3064 case SVt_PVCV: s = "CODE"; break;
3065 case SVt_PVGV: s = "GLOB"; break;
3066 case SVt_PVFM: s = "FORMAT"; break;
3067 case SVt_PVIO: s = "IO"; break;
3068 default: s = "UNKNOWN"; break;
3072 Perl_sv_setpvf(aTHX_ tsv, "%s=%s", HvNAME(SvSTASH(sv)), s);
3075 Perl_sv_catpvf(aTHX_ tsv, "(0x%"UVxf")", PTR2UV(sv));
3081 if (SvREADONLY(sv) && !SvOK(sv)) {
3082 if (ckWARN(WARN_UNINITIALIZED))
3088 if (SvIOK(sv) || ((SvIOKp(sv) && !SvNOKp(sv)))) {
3089 /* I'm assuming that if both IV and NV are equally valid then
3090 converting the IV is going to be more efficient */
3091 U32 isIOK = SvIOK(sv);
3092 U32 isUIOK = SvIsUV(sv);
3093 char buf[TYPE_CHARS(UV)];
3096 if (SvTYPE(sv) < SVt_PVIV)
3097 sv_upgrade(sv, SVt_PVIV);
3099 ptr = uiv_2buf(buf, 0, SvUVX(sv), 1, &ebuf);
3101 ptr = uiv_2buf(buf, SvIVX(sv), 0, 0, &ebuf);
3102 SvGROW(sv, (STRLEN)(ebuf - ptr + 1)); /* inlined from sv_setpvn */
3103 Move(ptr,SvPVX(sv),ebuf - ptr,char);
3104 SvCUR_set(sv, ebuf - ptr);
3114 else if (SvNOKp(sv)) {
3115 if (SvTYPE(sv) < SVt_PVNV)
3116 sv_upgrade(sv, SVt_PVNV);
3117 /* The +20 is pure guesswork. Configure test needed. --jhi */
3118 SvGROW(sv, NV_DIG + 20);
3120 olderrno = errno; /* some Xenix systems wipe out errno here */
3122 if (SvNVX(sv) == 0.0)
3123 (void)strcpy(s,"0");
3127 Gconvert(SvNVX(sv), NV_DIG, 0, s);
3130 #ifdef FIXNEGATIVEZERO
3131 if (*s == '-' && s[1] == '0' && !s[2])
3141 if (ckWARN(WARN_UNINITIALIZED)
3142 && !PL_localizing && !(SvFLAGS(sv) & SVs_PADTMP))
3145 if (SvTYPE(sv) < SVt_PV)
3146 /* Typically the caller expects that sv_any is not NULL now. */
3147 sv_upgrade(sv, SVt_PV);
3150 *lp = s - SvPVX(sv);
3153 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3154 PTR2UV(sv),SvPVX(sv)));
3158 if (SvROK(sv)) { /* XXX Skip this when sv_pvn_force calls */
3159 /* Sneaky stuff here */
3163 tsv = newSVpv(tmpbuf, 0);
3179 len = strlen(tmpbuf);
3181 #ifdef FIXNEGATIVEZERO
3182 if (len == 2 && t[0] == '-' && t[1] == '0') {
3187 (void)SvUPGRADE(sv, SVt_PV);
3189 s = SvGROW(sv, len + 1);
3198 =for apidoc sv_copypv
3200 Copies a stringified representation of the source SV into the
3201 destination SV. Automatically performs any necessary mg_get and
3202 coercion of numeric values into strings. Guaranteed to preserve
3203 UTF-8 flag even from overloaded objects. Similar in nature to
3204 sv_2pv[_flags] but operates directly on an SV instead of just the
3205 string. Mostly uses sv_2pv_flags to do its work, except when that
3206 would lose the UTF-8'ness of the PV.
3212 Perl_sv_copypv(pTHX_ SV *dsv, register SV *ssv)
3217 sv_setpvn(dsv,s,len);
3225 =for apidoc sv_2pvbyte_nolen
3227 Return a pointer to the byte-encoded representation of the SV.
3228 May cause the SV to be downgraded from UTF8 as a side-effect.
3230 Usually accessed via the C<SvPVbyte_nolen> macro.
3236 Perl_sv_2pvbyte_nolen(pTHX_ register SV *sv)
3239 return sv_2pvbyte(sv, &n_a);
3243 =for apidoc sv_2pvbyte
3245 Return a pointer to the byte-encoded representation of the SV, and set *lp
3246 to its length. May cause the SV to be downgraded from UTF8 as a
3249 Usually accessed via the C<SvPVbyte> macro.
3255 Perl_sv_2pvbyte(pTHX_ register SV *sv, STRLEN *lp)
3257 sv_utf8_downgrade(sv,0);
3258 return SvPV(sv,*lp);
3262 =for apidoc sv_2pvutf8_nolen
3264 Return a pointer to the UTF8-encoded representation of the SV.
3265 May cause the SV to be upgraded to UTF8 as a side-effect.
3267 Usually accessed via the C<SvPVutf8_nolen> macro.
3273 Perl_sv_2pvutf8_nolen(pTHX_ register SV *sv)
3276 return sv_2pvutf8(sv, &n_a);
3280 =for apidoc sv_2pvutf8
3282 Return a pointer to the UTF8-encoded representation of the SV, and set *lp
3283 to its length. May cause the SV to be upgraded to UTF8 as a side-effect.
3285 Usually accessed via the C<SvPVutf8> macro.
3291 Perl_sv_2pvutf8(pTHX_ register SV *sv, STRLEN *lp)
3293 sv_utf8_upgrade(sv);
3294 return SvPV(sv,*lp);
3298 =for apidoc sv_2bool
3300 This function is only called on magical items, and is only used by
3301 sv_true() or its macro equivalent.
3307 Perl_sv_2bool(pTHX_ register SV *sv)
3316 if (SvAMAGIC(sv) && (tmpsv=AMG_CALLun(sv,bool_)) &&
3317 (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
3318 return (bool)SvTRUE(tmpsv);
3319 return SvRV(sv) != 0;
3322 register XPV* Xpvtmp;
3323 if ((Xpvtmp = (XPV*)SvANY(sv)) &&
3324 (*Xpvtmp->xpv_pv > '0' ||
3325 Xpvtmp->xpv_cur > 1 ||
3326 (Xpvtmp->xpv_cur && *Xpvtmp->xpv_pv != '0')))
3333 return SvIVX(sv) != 0;
3336 return SvNVX(sv) != 0.0;
3344 =for apidoc sv_utf8_upgrade
3346 Convert the PV of an SV to its UTF8-encoded form.
3347 Forces the SV to string form if it is not already.
3348 Always sets the SvUTF8 flag to avoid future validity checks even
3349 if all the bytes have hibit clear.
3351 This is not as a general purpose byte encoding to Unicode interface:
3352 use the Encode extension for that.
3354 =for apidoc sv_utf8_upgrade_flags
3356 Convert the PV of an SV to its UTF8-encoded form.
3357 Forces the SV to string form if it is not already.
3358 Always sets the SvUTF8 flag to avoid future validity checks even
3359 if all the bytes have hibit clear. If C<flags> has C<SV_GMAGIC> bit set,
3360 will C<mg_get> on C<sv> if appropriate, else not. C<sv_utf8_upgrade> and
3361 C<sv_utf8_upgrade_nomg> are implemented in terms of this function.
3363 This is not as a general purpose byte encoding to Unicode interface:
3364 use the Encode extension for that.
3370 Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags)
3380 (void) sv_2pv_flags(sv,&len, flags);
3389 sv_force_normal_flags(sv, 0);
3392 if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING))
3393 sv_recode_to_utf8(sv, PL_encoding);
3394 else { /* Assume Latin-1/EBCDIC */
3395 /* This function could be much more efficient if we
3396 * had a FLAG in SVs to signal if there are any hibit
3397 * chars in the PV. Given that there isn't such a flag
3398 * make the loop as fast as possible. */
3399 s = (U8 *) SvPVX(sv);
3400 e = (U8 *) SvEND(sv);
3404 if ((hibit = !NATIVE_IS_INVARIANT(ch)))
3410 len = SvCUR(sv) + 1; /* Plus the \0 */
3411 SvPVX(sv) = (char*)bytes_to_utf8((U8*)s, &len);
3412 SvCUR(sv) = len - 1;
3414 Safefree(s); /* No longer using what was there before. */
3415 SvLEN(sv) = len; /* No longer know the real size. */
3417 /* Mark as UTF-8 even if no hibit - saves scanning loop */
3424 =for apidoc sv_utf8_downgrade
3426 Attempt to convert the PV of an SV from UTF8-encoded to byte encoding.
3427 This may not be possible if the PV contains non-byte encoding characters;
3428 if this is the case, either returns false or, if C<fail_ok> is not
3431 This is not as a general purpose Unicode to byte encoding interface:
3432 use the Encode extension for that.
3438 Perl_sv_utf8_downgrade(pTHX_ register SV* sv, bool fail_ok)
3440 if (SvPOK(sv) && SvUTF8(sv)) {
3446 sv_force_normal_flags(sv, 0);
3448 s = (U8 *) SvPV(sv, len);
3449 if (!utf8_to_bytes(s, &len)) {
3454 Perl_croak(aTHX_ "Wide character in %s",
3457 Perl_croak(aTHX_ "Wide character");
3468 =for apidoc sv_utf8_encode
3470 Convert the PV of an SV to UTF8-encoded, but then turn off the C<SvUTF8>
3471 flag so that it looks like octets again. Used as a building block
3472 for encode_utf8 in Encode.xs
3478 Perl_sv_utf8_encode(pTHX_ register SV *sv)
3480 (void) sv_utf8_upgrade(sv);
3485 =for apidoc sv_utf8_decode
3487 Convert the octets in the PV from UTF-8 to chars. Scan for validity and then
3488 turn off SvUTF8 if needed so that we see characters. Used as a building block
3489 for decode_utf8 in Encode.xs
3495 Perl_sv_utf8_decode(pTHX_ register SV *sv)
3501 /* The octets may have got themselves encoded - get them back as
3504 if (!sv_utf8_downgrade(sv, TRUE))
3507 /* it is actually just a matter of turning the utf8 flag on, but
3508 * we want to make sure everything inside is valid utf8 first.
3510 c = (U8 *) SvPVX(sv);
3511 if (!is_utf8_string(c, SvCUR(sv)+1))
3513 e = (U8 *) SvEND(sv);
3516 if (!UTF8_IS_INVARIANT(ch)) {
3526 =for apidoc sv_setsv
3528 Copies the contents of the source SV C<ssv> into the destination SV
3529 C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3530 function if the source SV needs to be reused. Does not handle 'set' magic.
3531 Loosely speaking, it performs a copy-by-value, obliterating any previous
3532 content of the destination.
3534 You probably want to use one of the assortment of wrappers, such as
3535 C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3536 C<SvSetMagicSV_nosteal>.
3538 =for apidoc sv_setsv_flags
3540 Copies the contents of the source SV C<ssv> into the destination SV
3541 C<dsv>. The source SV may be destroyed if it is mortal, so don't use this
3542 function if the source SV needs to be reused. Does not handle 'set' magic.
3543 Loosely speaking, it performs a copy-by-value, obliterating any previous
3544 content of the destination.
3545 If the C<flags> parameter has the C<SV_GMAGIC> bit set, will C<mg_get> on
3546 C<ssv> if appropriate, else not. C<sv_setsv> and C<sv_setsv_nomg> are
3547 implemented in terms of this function.
3549 You probably want to use one of the assortment of wrappers, such as
3550 C<SvSetSV>, C<SvSetSV_nosteal>, C<SvSetMagicSV> and
3551 C<SvSetMagicSV_nosteal>.
3553 This is the primary function for copying scalars, and most other
3554 copy-ish functions and macros use this underneath.
3560 Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags)
3562 register U32 sflags;
3568 SV_CHECK_THINKFIRST_COW_DROP(dstr);
3570 sstr = &PL_sv_undef;
3571 stype = SvTYPE(sstr);
3572 dtype = SvTYPE(dstr);
3577 /* need to nuke the magic */
3579 SvRMAGICAL_off(dstr);
3582 /* There's a lot of redundancy below but we're going for speed here */
3587 if (dtype != SVt_PVGV) {
3588 (void)SvOK_off(dstr);
3596 sv_upgrade(dstr, SVt_IV);
3599 sv_upgrade(dstr, SVt_PVNV);
3603 sv_upgrade(dstr, SVt_PVIV);
3606 (void)SvIOK_only(dstr);
3607 SvIVX(dstr) = SvIVX(sstr);
3610 if (SvTAINTED(sstr))
3621 sv_upgrade(dstr, SVt_NV);
3626 sv_upgrade(dstr, SVt_PVNV);
3629 SvNVX(dstr) = SvNVX(sstr);
3630 (void)SvNOK_only(dstr);
3631 if (SvTAINTED(sstr))
3639 sv_upgrade(dstr, SVt_RV);
3640 else if (dtype == SVt_PVGV &&
3641 SvTYPE(SvRV(sstr)) == SVt_PVGV) {
3644 if (GvIMPORTED(dstr) != GVf_IMPORTED
3645 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3647 GvIMPORTED_on(dstr);
3658 sv_upgrade(dstr, SVt_PV);
3661 if (dtype < SVt_PVIV)
3662 sv_upgrade(dstr, SVt_PVIV);
3665 if (dtype < SVt_PVNV)
3666 sv_upgrade(dstr, SVt_PVNV);
3673 Perl_croak(aTHX_ "Bizarre copy of %s in %s", sv_reftype(sstr, 0),
3676 Perl_croak(aTHX_ "Bizarre copy of %s", sv_reftype(sstr, 0));
3680 if (dtype <= SVt_PVGV) {
3682 if (dtype != SVt_PVGV) {
3683 char *name = GvNAME(sstr);
3684 STRLEN len = GvNAMELEN(sstr);
3685 sv_upgrade(dstr, SVt_PVGV);
3686 sv_magic(dstr, dstr, PERL_MAGIC_glob, Nullch, 0);
3687 GvSTASH(dstr) = (HV*)SvREFCNT_inc(GvSTASH(sstr));
3688 GvNAME(dstr) = savepvn(name, len);
3689 GvNAMELEN(dstr) = len;
3690 SvFAKE_on(dstr); /* can coerce to non-glob */
3692 /* ahem, death to those who redefine active sort subs */
3693 else if (PL_curstackinfo->si_type == PERLSI_SORT
3694 && GvCV(dstr) && PL_sortcop == CvSTART(GvCV(dstr)))
3695 Perl_croak(aTHX_ "Can't redefine active sort subroutine %s",
3698 #ifdef GV_UNIQUE_CHECK
3699 if (GvUNIQUE((GV*)dstr)) {
3700 Perl_croak(aTHX_ PL_no_modify);
3704 (void)SvOK_off(dstr);
3705 GvINTRO_off(dstr); /* one-shot flag */
3707 GvGP(dstr) = gp_ref(GvGP(sstr));
3708 if (SvTAINTED(sstr))
3710 if (GvIMPORTED(dstr) != GVf_IMPORTED
3711 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3713 GvIMPORTED_on(dstr);
3721 if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) {
3723 if ((int)SvTYPE(sstr) != stype) {
3724 stype = SvTYPE(sstr);
3725 if (stype == SVt_PVGV && dtype <= SVt_PVGV)
3729 if (stype == SVt_PVLV)
3730 (void)SvUPGRADE(dstr, SVt_PVNV);
3732 (void)SvUPGRADE(dstr, (U32)stype);
3735 sflags = SvFLAGS(sstr);
3737 if (sflags & SVf_ROK) {
3738 if (dtype >= SVt_PV) {
3739 if (dtype == SVt_PVGV) {
3740 SV *sref = SvREFCNT_inc(SvRV(sstr));
3742 int intro = GvINTRO(dstr);
3744 #ifdef GV_UNIQUE_CHECK
3745 if (GvUNIQUE((GV*)dstr)) {
3746 Perl_croak(aTHX_ PL_no_modify);
3751 GvINTRO_off(dstr); /* one-shot flag */
3752 GvLINE(dstr) = CopLINE(PL_curcop);
3753 GvEGV(dstr) = (GV*)dstr;
3756 switch (SvTYPE(sref)) {
3759 SAVEGENERICSV(GvAV(dstr));
3761 dref = (SV*)GvAV(dstr);
3762 GvAV(dstr) = (AV*)sref;
3763 if (!GvIMPORTED_AV(dstr)
3764 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3766 GvIMPORTED_AV_on(dstr);
3771 SAVEGENERICSV(GvHV(dstr));
3773 dref = (SV*)GvHV(dstr);
3774 GvHV(dstr) = (HV*)sref;
3775 if (!GvIMPORTED_HV(dstr)
3776 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3778 GvIMPORTED_HV_on(dstr);
3783 if (GvCVGEN(dstr) && GvCV(dstr) != (CV*)sref) {
3784 SvREFCNT_dec(GvCV(dstr));
3785 GvCV(dstr) = Nullcv;
3786 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3787 PL_sub_generation++;
3789 SAVEGENERICSV(GvCV(dstr));
3792 dref = (SV*)GvCV(dstr);
3793 if (GvCV(dstr) != (CV*)sref) {
3794 CV* cv = GvCV(dstr);
3796 if (!GvCVGEN((GV*)dstr) &&
3797 (CvROOT(cv) || CvXSUB(cv)))
3799 /* ahem, death to those who redefine
3800 * active sort subs */
3801 if (PL_curstackinfo->si_type == PERLSI_SORT &&
3802 PL_sortcop == CvSTART(cv))
3804 "Can't redefine active sort subroutine %s",
3805 GvENAME((GV*)dstr));
3806 /* Redefining a sub - warning is mandatory if
3807 it was a const and its value changed. */
3808 if (ckWARN(WARN_REDEFINE)
3810 && (!CvCONST((CV*)sref)
3811 || sv_cmp(cv_const_sv(cv),
3812 cv_const_sv((CV*)sref)))))
3814 Perl_warner(aTHX_ packWARN(WARN_REDEFINE),
3816 ? "Constant subroutine %s::%s redefined"
3817 : "Subroutine %s::%s redefined",
3818 HvNAME(GvSTASH((GV*)dstr)),
3819 GvENAME((GV*)dstr));
3823 cv_ckproto(cv, (GV*)dstr,
3824 SvPOK(sref) ? SvPVX(sref) : Nullch);
3826 GvCV(dstr) = (CV*)sref;
3827 GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3828 GvASSUMECV_on(dstr);
3829 PL_sub_generation++;
3831 if (!GvIMPORTED_CV(dstr)
3832 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3834 GvIMPORTED_CV_on(dstr);
3839 SAVEGENERICSV(GvIOp(dstr));
3841 dref = (SV*)GvIOp(dstr);
3842 GvIOp(dstr) = (IO*)sref;
3846 SAVEGENERICSV(GvFORM(dstr));
3848 dref = (SV*)GvFORM(dstr);
3849 GvFORM(dstr) = (CV*)sref;
3853 SAVEGENERICSV(GvSV(dstr));
3855 dref = (SV*)GvSV(dstr);
3857 if (!GvIMPORTED_SV(dstr)
3858 && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3860 GvIMPORTED_SV_on(dstr);
3866 if (SvTAINTED(sstr))
3871 (void)SvOOK_off(dstr); /* backoff */
3873 Safefree(SvPVX(dstr));
3874 SvLEN(dstr)=SvCUR(dstr)=0;
3877 (void)SvOK_off(dstr);
3878 SvRV(dstr) = SvREFCNT_inc(SvRV(sstr));
3880 if (sflags & SVp_NOK) {
3882 /* Only set the public OK flag if the source has public OK. */
3883 if (sflags & SVf_NOK)
3884 SvFLAGS(dstr) |= SVf_NOK;
3885 SvNVX(dstr) = SvNVX(sstr);
3887 if (sflags & SVp_IOK) {
3888 (void)SvIOKp_on(dstr);
3889 if (sflags & SVf_IOK)
3890 SvFLAGS(dstr) |= SVf_IOK;
3891 if (sflags & SVf_IVisUV)
3893 SvIVX(dstr) = SvIVX(sstr);
3895 if (SvAMAGIC(sstr)) {
3899 else if (sflags & SVp_POK) {
3903 * Check to see if we can just swipe the string. If so, it's a
3904 * possible small lose on short strings, but a big win on long ones.
3905 * It might even be a win on short strings if SvPVX(dstr)
3906 * has to be allocated and SvPVX(sstr) has to be freed.
3910 #ifdef PERL_COPY_ON_WRITE
3911 (sflags & (SVf_FAKE | SVf_READONLY)) != (SVf_FAKE | SVf_READONLY)
3915 (sflags & SVs_TEMP) && /* slated for free anyway? */
3916 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
3917 SvREFCNT(sstr) == 1 && /* and no other references to it? */
3918 SvLEN(sstr) && /* and really is a string */
3919 /* and won't be needed again, potentially */
3920 !(PL_op && PL_op->op_type == OP_AASSIGN))
3921 #ifdef PERL_COPY_ON_WRITE
3922 && !((sflags & CAN_COW_MASK) == CAN_COW_FLAGS
3923 && SvTYPE(sstr) >= SVt_PVIV)
3926 /* Failed the swipe test, and it's not a shared hash key either.
3927 Have to copy the string. */
3928 STRLEN len = SvCUR(sstr);
3929 SvGROW(dstr, len + 1); /* inlined from sv_setpvn */
3930 Move(SvPVX(sstr),SvPVX(dstr),len,char);
3931 SvCUR_set(dstr, len);
3932 *SvEND(dstr) = '\0';
3933 (void)SvPOK_only(dstr);
3935 /* If PERL_COPY_ON_WRITE is not defined, then isSwipe will always
3937 #ifdef PERL_COPY_ON_WRITE
3938 /* Either it's a shared hash key, or it's suitable for
3939 copy-on-write or we can swipe the string. */
3941 PerlIO_printf(Perl_debug_log, "Copy on write: sstr --> dstr\n");
3946 /* I believe I should acquire a global SV mutex if
3947 it's a COW sv (not a shared hash key) to stop
3948 it going un copy-on-write.
3949 If the source SV has gone un copy on write between up there
3950 and down here, then (assert() that) it is of the correct
3951 form to make it copy on write again */
3952 if ((sflags & (SVf_FAKE | SVf_READONLY))
3953 != (SVf_FAKE | SVf_READONLY)) {
3954 SvREADONLY_on(sstr);
3956 /* Make the source SV into a loop of 1.
3957 (about to become 2) */
3958 SV_COW_NEXT_SV_SET(sstr, sstr);
3962 /* Initial code is common. */
3963 if (SvPVX(dstr)) { /* we know that dtype >= SVt_PV */
3965 SvFLAGS(dstr) &= ~SVf_OOK;
3966 Safefree(SvPVX(dstr) - SvIVX(dstr));
3968 else if (SvLEN(dstr))
3969 Safefree(SvPVX(dstr));
3971 (void)SvPOK_only(dstr);
3973 #ifdef PERL_COPY_ON_WRITE
3975 /* making another shared SV. */
3976 STRLEN cur = SvCUR(sstr);
3977 STRLEN len = SvLEN(sstr);
3979 /* SvIsCOW_normal */
3980 /* splice us in between source and next-after-source. */
3981 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
3982 SV_COW_NEXT_SV_SET(sstr, dstr);
3983 SvPV_set(dstr, SvPVX(sstr));
3985 /* SvIsCOW_shared_hash */
3986 UV hash = SvUVX(sstr);
3987 DEBUG_C(PerlIO_printf(Perl_debug_log,
3988 "Copy on write: Sharing hash\n"));
3990 sharepvn(SvPVX(sstr),
3991 (sflags & SVf_UTF8?-cur:cur), hash));
3996 SvREADONLY_on(dstr);
3998 /* Relesase a global SV mutex. */
4002 { /* Passes the swipe test. */
4003 SvPV_set(dstr, SvPVX(sstr));
4004 SvLEN_set(dstr, SvLEN(sstr));
4005 SvCUR_set(dstr, SvCUR(sstr));
4008 (void)SvOK_off(sstr); /* NOTE: nukes most SvFLAGS on sstr */
4009 SvPV_set(sstr, Nullch);
4015 if (sflags & SVf_UTF8)
4018 if (sflags & SVp_NOK) {
4020 if (sflags & SVf_NOK)
4021 SvFLAGS(dstr) |= SVf_NOK;
4022 SvNVX(dstr) = SvNVX(sstr);
4024 if (sflags & SVp_IOK) {
4025 (void)SvIOKp_on(dstr);
4026 if (sflags & SVf_IOK)
4027 SvFLAGS(dstr) |= SVf_IOK;
4028 if (sflags & SVf_IVisUV)
4030 SvIVX(dstr) = SvIVX(sstr);
4033 MAGIC *smg = mg_find(sstr,PERL_MAGIC_vstring);
4034 sv_magic(dstr, NULL, PERL_MAGIC_vstring,
4035 smg->mg_ptr, smg->mg_len);
4036 SvRMAGICAL_on(dstr);
4039 else if (sflags & SVp_IOK) {
4040 if (sflags & SVf_IOK)
4041 (void)SvIOK_only(dstr);
4043 (void)SvOK_off(dstr);
4044 (void)SvIOKp_on(dstr);
4046 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
4047 if (sflags & SVf_IVisUV)
4049 SvIVX(dstr) = SvIVX(sstr);
4050 if (sflags & SVp_NOK) {
4051 if (sflags & SVf_NOK)
4052 (void)SvNOK_on(dstr);
4054 (void)SvNOKp_on(dstr);
4055 SvNVX(dstr) = SvNVX(sstr);
4058 else if (sflags & SVp_NOK) {
4059 if (sflags & SVf_NOK)
4060 (void)SvNOK_only(dstr);
4062 (void)SvOK_off(dstr);
4065 SvNVX(dstr) = SvNVX(sstr);
4068 if (dtype == SVt_PVGV) {
4069 if (ckWARN(WARN_MISC))
4070 Perl_warner(aTHX_ packWARN(WARN_MISC), "Undefined value assigned to typeglob");
4073 (void)SvOK_off(dstr);
4075 if (SvTAINTED(sstr))
4080 =for apidoc sv_setsv_mg
4082 Like C<sv_setsv>, but also handles 'set' magic.
4088 Perl_sv_setsv_mg(pTHX_ SV *dstr, register SV *sstr)
4090 sv_setsv(dstr,sstr);
4094 #ifdef PERL_COPY_ON_WRITE
4096 Perl_sv_setsv_cow(pTHX_ SV *dstr, SV *sstr)
4098 STRLEN cur = SvCUR(sstr);
4099 STRLEN len = SvLEN(sstr);
4100 register char *new_pv;
4103 PerlIO_printf(Perl_debug_log, "Fast copy on write: %p -> %p\n",
4111 if (SvTHINKFIRST(dstr))
4112 sv_force_normal_flags(dstr, SV_COW_DROP_PV);
4113 else if (SvPVX(dstr))
4114 Safefree(SvPVX(dstr));
4118 SvUPGRADE (dstr, SVt_PVIV);
4120 assert (SvPOK(sstr));
4121 assert (SvPOKp(sstr));
4122 assert (!SvIOK(sstr));
4123 assert (!SvIOKp(sstr));
4124 assert (!SvNOK(sstr));
4125 assert (!SvNOKp(sstr));
4127 if (SvIsCOW(sstr)) {
4129 if (SvLEN(sstr) == 0) {
4130 /* source is a COW shared hash key. */
4131 UV hash = SvUVX(sstr);
4132 DEBUG_C(PerlIO_printf(Perl_debug_log,
4133 "Fast copy on write: Sharing hash\n"));
4135 new_pv = sharepvn(SvPVX(sstr), (SvUTF8(sstr)?-cur:cur), hash);
4138 SV_COW_NEXT_SV_SET(dstr, SV_COW_NEXT_SV(sstr));
4140 assert ((SvFLAGS(sstr) & CAN_COW_MASK) == CAN_COW_FLAGS);
4141 SvUPGRADE (sstr, SVt_PVIV);
4142 SvREADONLY_on(sstr);
4144 DEBUG_C(PerlIO_printf(Perl_debug_log,
4145 "Fast copy on write: Converting sstr to COW\n"));
4146 SV_COW_NEXT_SV_SET(dstr, sstr);
4148 SV_COW_NEXT_SV_SET(sstr, dstr);
4149 new_pv = SvPVX(sstr);
4152 SvPV_set(dstr, new_pv);
4153 SvFLAGS(dstr) = (SVt_PVIV|SVf_POK|SVp_POK|SVf_FAKE|SVf_READONLY);
4166 =for apidoc sv_setpvn
4168 Copies a string into an SV. The C<len> parameter indicates the number of
4169 bytes to be copied. Does not handle 'set' magic. See C<sv_setpvn_mg>.
4175 Perl_sv_setpvn(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
4177 register char *dptr;
4179 SV_CHECK_THINKFIRST_COW_DROP(sv);
4185 /* len is STRLEN which is unsigned, need to copy to signed */
4188 Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen");
4190 (void)SvUPGRADE(sv, SVt_PV);
4192 SvGROW(sv, len + 1);
4194 Move(ptr,dptr,len,char);
4197 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4202 =for apidoc sv_setpvn_mg
4204 Like C<sv_setpvn>, but also handles 'set' magic.
4210 Perl_sv_setpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
4212 sv_setpvn(sv,ptr,len);
4217 =for apidoc sv_setpv
4219 Copies a string into an SV. The string must be null-terminated. Does not
4220 handle 'set' magic. See C<sv_setpv_mg>.
4226 Perl_sv_setpv(pTHX_ register SV *sv, register const char *ptr)
4228 register STRLEN len;
4230 SV_CHECK_THINKFIRST_COW_DROP(sv);
4236 (void)SvUPGRADE(sv, SVt_PV);
4238 SvGROW(sv, len + 1);
4239 Move(ptr,SvPVX(sv),len+1,char);
4241 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4246 =for apidoc sv_setpv_mg
4248 Like C<sv_setpv>, but also handles 'set' magic.
4254 Perl_sv_setpv_mg(pTHX_ register SV *sv, register const char *ptr)
4261 =for apidoc sv_usepvn
4263 Tells an SV to use C<ptr> to find its string value. Normally the string is
4264 stored inside the SV but sv_usepvn allows the SV to use an outside string.
4265 The C<ptr> should point to memory that was allocated by C<malloc>. The
4266 string length, C<len>, must be supplied. This function will realloc the
4267 memory pointed to by C<ptr>, so that pointer should not be freed or used by
4268 the programmer after giving it to sv_usepvn. Does not handle 'set' magic.
4269 See C<sv_usepvn_mg>.
4275 Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
4277 SV_CHECK_THINKFIRST_COW_DROP(sv);
4278 (void)SvUPGRADE(sv, SVt_PV);
4283 (void)SvOOK_off(sv);
4284 if (SvPVX(sv) && SvLEN(sv))
4285 Safefree(SvPVX(sv));
4286 Renew(ptr, len+1, char);
4289 SvLEN_set(sv, len+1);
4291 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4296 =for apidoc sv_usepvn_mg
4298 Like C<sv_usepvn>, but also handles 'set' magic.
4304 Perl_sv_usepvn_mg(pTHX_ register SV *sv, register char *ptr, register STRLEN len)
4306 sv_usepvn(sv,ptr,len);
4310 #ifdef PERL_COPY_ON_WRITE
4311 /* Need to do this *after* making the SV normal, as we need the buffer
4312 pointer to remain valid until after we've copied it. If we let go too early,
4313 another thread could invalidate it by unsharing last of the same hash key
4314 (which it can do by means other than releasing copy-on-write Svs)
4315 or by changing the other copy-on-write SVs in the loop. */
4317 S_sv_release_COW(pTHX_ register SV *sv, char *pvx, STRLEN cur, STRLEN len,
4318 U32 hash, SV *after)
4320 if (len) { /* this SV was SvIsCOW_normal(sv) */
4321 /* we need to find the SV pointing to us. */
4322 SV *current = SV_COW_NEXT_SV(after);
4324 if (current == sv) {
4325 /* The SV we point to points back to us (there were only two of us
4327 Hence other SV is no longer copy on write either. */
4329 SvREADONLY_off(after);
4331 /* We need to follow the pointers around the loop. */
4333 while ((next = SV_COW_NEXT_SV(current)) != sv) {
4336 /* don't loop forever if the structure is bust, and we have
4337 a pointer into a closed loop. */
4338 assert (current != after);
4339 assert (SvPVX(current) == pvx);
4341 /* Make the SV before us point to the SV after us. */
4342 SV_COW_NEXT_SV_SET(current, after);
4345 unsharepvn(pvx, SvUTF8(sv) ? -(I32)cur : cur, hash);
4350 Perl_sv_release_IVX(pTHX_ register SV *sv)
4353 sv_force_normal_flags(sv, 0);
4354 return SvOOK_off(sv);
4358 =for apidoc sv_force_normal_flags
4360 Undo various types of fakery on an SV: if the PV is a shared string, make
4361 a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
4362 an xpvmg; if we're a copy-on-write scalar, this is the on-write time when
4363 we do the copy, and is also used locally. If C<SV_COW_DROP_PV> is set
4364 then a copy-on-write scalar drops its PV buffer (if any) and becomes
4365 SvPOK_off rather than making a copy. (Used where this scalar is about to be
4366 set to some other value.) In addtion, the C<flags> parameter gets passed to
4367 C<sv_unref_flags()> when unrefing. C<sv_force_normal> calls this function
4368 with flags set to 0.
4374 Perl_sv_force_normal_flags(pTHX_ register SV *sv, U32 flags)
4376 #ifdef PERL_COPY_ON_WRITE
4377 if (SvREADONLY(sv)) {
4378 /* At this point I believe I should acquire a global SV mutex. */
4380 char *pvx = SvPVX(sv);
4381 STRLEN len = SvLEN(sv);
4382 STRLEN cur = SvCUR(sv);
4383 U32 hash = SvUVX(sv);
4384 SV *next = SV_COW_NEXT_SV(sv); /* next COW sv in the loop. */
4386 PerlIO_printf(Perl_debug_log,
4387 "Copy on write: Force normal %ld\n",
4393 /* This SV doesn't own the buffer, so need to New() a new one: */
4396 if (flags & SV_COW_DROP_PV) {
4397 /* OK, so we don't need to copy our buffer. */
4400 SvGROW(sv, cur + 1);
4401 Move(pvx,SvPVX(sv),cur,char);
4405 sv_release_COW(sv, pvx, cur, len, hash, next);
4410 else if (PL_curcop != &PL_compiling)
4411 Perl_croak(aTHX_ PL_no_modify);
4412 /* At this point I believe that I can drop the global SV mutex. */
4415 if (SvREADONLY(sv)) {
4417 char *pvx = SvPVX(sv);
4418 STRLEN len = SvCUR(sv);
4419 U32 hash = SvUVX(sv);
4420 SvGROW(sv, len + 1);
4421 Move(pvx,SvPVX(sv),len,char);
4425 unsharepvn(pvx, SvUTF8(sv) ? -(I32)len : len, hash);
4427 else if (PL_curcop != &PL_compiling)
4428 Perl_croak(aTHX_ PL_no_modify);
4432 sv_unref_flags(sv, flags);
4433 else if (SvFAKE(sv) && SvTYPE(sv) == SVt_PVGV)
4438 =for apidoc sv_force_normal
4440 Undo various types of fakery on an SV: if the PV is a shared string, make
4441 a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
4442 an xpvmg. See also C<sv_force_normal_flags>.
4448 Perl_sv_force_normal(pTHX_ register SV *sv)
4450 sv_force_normal_flags(sv, 0);
4456 Efficient removal of characters from the beginning of the string buffer.
4457 SvPOK(sv) must be true and the C<ptr> must be a pointer to somewhere inside
4458 the string buffer. The C<ptr> becomes the first character of the adjusted
4459 string. Uses the "OOK hack".
4465 Perl_sv_chop(pTHX_ register SV *sv, register char *ptr)
4467 register STRLEN delta;
4469 if (!ptr || !SvPOKp(sv))
4471 SV_CHECK_THINKFIRST(sv);
4472 if (SvTYPE(sv) < SVt_PVIV)
4473 sv_upgrade(sv,SVt_PVIV);
4476 if (!SvLEN(sv)) { /* make copy of shared string */
4477 char *pvx = SvPVX(sv);
4478 STRLEN len = SvCUR(sv);
4479 SvGROW(sv, len + 1);
4480 Move(pvx,SvPVX(sv),len,char);
4484 /* Same SvOOK_on but SvOOK_on does a SvIOK_off
4485 and we do that anyway inside the SvNIOK_off
4487 SvFLAGS(sv) |= SVf_OOK;
4490 delta = ptr - SvPVX(sv);
4498 =for apidoc sv_catpvn
4500 Concatenates the string onto the end of the string which is in the SV. The
4501 C<len> indicates number of bytes to copy. If the SV has the UTF8
4502 status set, then the bytes appended should be valid UTF8.
4503 Handles 'get' magic, but not 'set' magic. See C<sv_catpvn_mg>.
4505 =for apidoc sv_catpvn_flags
4507 Concatenates the string onto the end of the string which is in the SV. The
4508 C<len> indicates number of bytes to copy. If the SV has the UTF8
4509 status set, then the bytes appended should be valid UTF8.
4510 If C<flags> has C<SV_GMAGIC> bit set, will C<mg_get> on C<dsv> if
4511 appropriate, else not. C<sv_catpvn> and C<sv_catpvn_nomg> are implemented
4512 in terms of this function.
4518 Perl_sv_catpvn_flags(pTHX_ register SV *dsv, register const char *sstr, register STRLEN slen, I32 flags)
4523 dstr = SvPV_force_flags(dsv, dlen, flags);
4524 SvGROW(dsv, dlen + slen + 1);
4527 Move(sstr, SvPVX(dsv) + dlen, slen, char);
4530 (void)SvPOK_only_UTF8(dsv); /* validate pointer */
4535 =for apidoc sv_catpvn_mg
4537 Like C<sv_catpvn>, but also handles 'set' magic.
4543 Perl_sv_catpvn_mg(pTHX_ register SV *sv, register const char *ptr, register STRLEN len)
4545 sv_catpvn(sv,ptr,len);
4550 =for apidoc sv_catsv
4552 Concatenates the string from SV C<ssv> onto the end of the string in
4553 SV C<dsv>. Modifies C<dsv> but not C<ssv>. Handles 'get' magic, but
4554 not 'set' magic. See C<sv_catsv_mg>.
4556 =for apidoc sv_catsv_flags
4558 Concatenates the string from SV C<ssv> onto the end of the string in
4559 SV C<dsv>. Modifies C<dsv> but not C<ssv>. If C<flags> has C<SV_GMAGIC>
4560 bit set, will C<mg_get> on the SVs if appropriate, else not. C<sv_catsv>
4561 and C<sv_catsv_nomg> are implemented in terms of this function.
4566 Perl_sv_catsv_flags(pTHX_ SV *dsv, register SV *ssv, I32 flags)
4572 if ((spv = SvPV(ssv, slen))) {
4573 /* sutf8 and dutf8 were type bool, but under USE_ITHREADS,
4574 gcc version 2.95.2 20000220 (Debian GNU/Linux) for
4575 Linux xxx 2.2.17 on sparc64 with gcc -O2, we erroneously
4576 get dutf8 = 0x20000000, (i.e. SVf_UTF8) even though
4577 dsv->sv_flags doesn't have that bit set.
4578 Andy Dougherty 12 Oct 2001
4580 I32 sutf8 = DO_UTF8(ssv);
4583 if (SvGMAGICAL(dsv) && (flags & SV_GMAGIC))
4585 dutf8 = DO_UTF8(dsv);
4587 if (dutf8 != sutf8) {
4589 /* Not modifying source SV, so taking a temporary copy. */
4590 SV* csv = sv_2mortal(newSVpvn(spv, slen));
4592 sv_utf8_upgrade(csv);
4593 spv = SvPV(csv, slen);
4596 sv_utf8_upgrade_nomg(dsv);
4598 sv_catpvn_nomg(dsv, spv, slen);
4603 =for apidoc sv_catsv_mg
4605 Like C<sv_catsv>, but also handles 'set' magic.
4611 Perl_sv_catsv_mg(pTHX_ SV *dsv, register SV *ssv)
4618 =for apidoc sv_catpv
4620 Concatenates the string onto the end of the string which is in the SV.
4621 If the SV has the UTF8 status set, then the bytes appended should be
4622 valid UTF8. Handles 'get' magic, but not 'set' magic. See C<sv_catpv_mg>.
4627 Perl_sv_catpv(pTHX_ register SV *sv, register const char *ptr)
4629 register STRLEN len;
4635 junk = SvPV_force(sv, tlen);
4637 SvGROW(sv, tlen + len + 1);
4640 Move(ptr,SvPVX(sv)+tlen,len+1,char);
4642 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4647 =for apidoc sv_catpv_mg
4649 Like C<sv_catpv>, but also handles 'set' magic.
4655 Perl_sv_catpv_mg(pTHX_ register SV *sv, register const char *ptr)
4664 Create a new null SV, or if len > 0, create a new empty SVt_PV type SV
4665 with an initial PV allocation of len+1. Normally accessed via the C<NEWSV>
4672 Perl_newSV(pTHX_ STRLEN len)
4678 sv_upgrade(sv, SVt_PV);
4679 SvGROW(sv, len + 1);
4684 =for apidoc sv_magicext
4686 Adds magic to an SV, upgrading it if necessary. Applies the
4687 supplied vtable and returns pointer to the magic added.
4689 Note that sv_magicext will allow things that sv_magic will not.
4690 In particular you can add magic to SvREADONLY SVs and and more than
4691 one instance of the same 'how'
4693 I C<namelen> is greater then zero then a savepvn() I<copy> of C<name> is stored,
4694 if C<namelen> is zero then C<name> is stored as-is and - as another special
4695 case - if C<(name && namelen == HEf_SVKEY)> then C<name> is assumed to contain
4696 an C<SV*> and has its REFCNT incremented
4698 (This is now used as a subroutine by sv_magic.)
4703 Perl_sv_magicext(pTHX_ SV* sv, SV* obj, int how, MGVTBL *vtable,
4704 const char* name, I32 namlen)
4708 if (SvTYPE(sv) < SVt_PVMG) {
4709 (void)SvUPGRADE(sv, SVt_PVMG);
4711 Newz(702,mg, 1, MAGIC);
4712 mg->mg_moremagic = SvMAGIC(sv);
4715 /* Some magic sontains a reference loop, where the sv and object refer to
4716 each other. To prevent a reference loop that would prevent such
4717 objects being freed, we look for such loops and if we find one we
4718 avoid incrementing the object refcount.
4720 Note we cannot do this to avoid self-tie loops as intervening RV must
4721 have its REFCNT incremented to keep it in existence.
4724 if (!obj || obj == sv ||
4725 how == PERL_MAGIC_arylen ||
4726 how == PERL_MAGIC_qr ||
4727 (SvTYPE(obj) == SVt_PVGV &&
4728 (GvSV(obj) == sv || GvHV(obj) == (HV*)sv || GvAV(obj) == (AV*)sv ||
4729 GvCV(obj) == (CV*)sv || GvIOp(obj) == (IO*)sv ||
4730 GvFORM(obj) == (CV*)sv)))
4735 mg->mg_obj = SvREFCNT_inc(obj);
4736 mg->mg_flags |= MGf_REFCOUNTED;
4739 /* Normal self-ties simply pass a null object, and instead of
4740 using mg_obj directly, use the SvTIED_obj macro to produce a
4741 new RV as needed. For glob "self-ties", we are tieing the PVIO
4742 with an RV obj pointing to the glob containing the PVIO. In
4743 this case, to avoid a reference loop, we need to weaken the
4747 if (how == PERL_MAGIC_tiedscalar && SvTYPE(sv) == SVt_PVIO &&
4748 obj && SvROK(obj) && GvIO(SvRV(obj)) == (IO*)sv)
4754 mg->mg_len = namlen;
4757 mg->mg_ptr = savepvn(name, namlen);
4758 else if (namlen == HEf_SVKEY)
4759 mg->mg_ptr = (char*)SvREFCNT_inc((SV*)name);
4761 mg->mg_ptr = (char *) name;
4763 mg->mg_virtual = vtable;
4767 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK|SVf_POK);
4772 =for apidoc sv_magic
4774 Adds magic to an SV. First upgrades C<sv> to type C<SVt_PVMG> if necessary,
4775 then adds a new magic item of type C<how> to the head of the magic list.
4781 Perl_sv_magic(pTHX_ register SV *sv, SV *obj, int how, const char *name, I32 namlen)
4786 #ifdef PERL_COPY_ON_WRITE
4788 sv_force_normal_flags(sv, 0);
4790 if (SvREADONLY(sv)) {
4791 if (PL_curcop != &PL_compiling
4792 && how != PERL_MAGIC_regex_global
4793 && how != PERL_MAGIC_bm