3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by Larry Wall
7 * You may distribute under the terms of either the GNU General Public
8 * License or the Artistic License, as specified in the README file.
13 * 'I wonder what the Entish is for "yes" and "no",' he thought.
16 * [p.480 of _The Lord of the Rings_, III/iv: "Treebeard"]
22 * This file contains the code that creates, manipulates and destroys
23 * scalar values (SVs). The other types (AV, HV, GV, etc.) reuse the
24 * structure of an SV, so their creation and destruction is handled
25 * here; higher-level functions are in av.c, hv.c, and so on. Opcode
26 * level functions (eg. substr, split, join) for each of the types are
39 /* Missing proto on LynxOS */
40 char *gconvert(double, int, int, char *);
44 # define SNPRINTF_G(nv, buffer, size, ndig) \
45 quadmath_snprintf(buffer, size, "%.*Qg", (int)ndig, (NV)(nv))
47 # define SNPRINTF_G(nv, buffer, size, ndig) \
48 PERL_UNUSED_RESULT(Gconvert((NV)(nv), (int)ndig, 0, buffer))
51 #ifndef SV_COW_THRESHOLD
52 # define SV_COW_THRESHOLD 0 /* COW iff len > K */
54 #ifndef SV_COWBUF_THRESHOLD
55 # define SV_COWBUF_THRESHOLD 1250 /* COW iff len > K */
57 #ifndef SV_COW_MAX_WASTE_THRESHOLD
58 # define SV_COW_MAX_WASTE_THRESHOLD 80 /* COW iff (len - cur) < K */
60 #ifndef SV_COWBUF_WASTE_THRESHOLD
61 # define SV_COWBUF_WASTE_THRESHOLD 80 /* COW iff (len - cur) < K */
63 #ifndef SV_COW_MAX_WASTE_FACTOR_THRESHOLD
64 # define SV_COW_MAX_WASTE_FACTOR_THRESHOLD 2 /* COW iff len < (cur * K) */
66 #ifndef SV_COWBUF_WASTE_FACTOR_THRESHOLD
67 # define SV_COWBUF_WASTE_FACTOR_THRESHOLD 2 /* COW iff len < (cur * K) */
69 /* Work around compiler warnings about unsigned >= THRESHOLD when thres-
72 # define GE_COW_THRESHOLD(cur) ((cur) >= SV_COW_THRESHOLD)
74 # define GE_COW_THRESHOLD(cur) 1
76 #if SV_COWBUF_THRESHOLD
77 # define GE_COWBUF_THRESHOLD(cur) ((cur) >= SV_COWBUF_THRESHOLD)
79 # define GE_COWBUF_THRESHOLD(cur) 1
81 #if SV_COW_MAX_WASTE_THRESHOLD
82 # define GE_COW_MAX_WASTE_THRESHOLD(cur,len) (((len)-(cur)) < SV_COW_MAX_WASTE_THRESHOLD)
84 # define GE_COW_MAX_WASTE_THRESHOLD(cur,len) 1
86 #if SV_COWBUF_WASTE_THRESHOLD
87 # define GE_COWBUF_WASTE_THRESHOLD(cur,len) (((len)-(cur)) < SV_COWBUF_WASTE_THRESHOLD)
89 # define GE_COWBUF_WASTE_THRESHOLD(cur,len) 1
91 #if SV_COW_MAX_WASTE_FACTOR_THRESHOLD
92 # define GE_COW_MAX_WASTE_FACTOR_THRESHOLD(cur,len) ((len) < SV_COW_MAX_WASTE_FACTOR_THRESHOLD * (cur))
94 # define GE_COW_MAX_WASTE_FACTOR_THRESHOLD(cur,len) 1
96 #if SV_COWBUF_WASTE_FACTOR_THRESHOLD
97 # define GE_COWBUF_WASTE_FACTOR_THRESHOLD(cur,len) ((len) < SV_COWBUF_WASTE_FACTOR_THRESHOLD * (cur))
99 # define GE_COWBUF_WASTE_FACTOR_THRESHOLD(cur,len) 1
102 #define CHECK_COW_THRESHOLD(cur,len) (\
103 GE_COW_THRESHOLD((cur)) && \
104 GE_COW_MAX_WASTE_THRESHOLD((cur),(len)) && \
105 GE_COW_MAX_WASTE_FACTOR_THRESHOLD((cur),(len)) \
107 #define CHECK_COWBUF_THRESHOLD(cur,len) (\
108 GE_COWBUF_THRESHOLD((cur)) && \
109 GE_COWBUF_WASTE_THRESHOLD((cur),(len)) && \
110 GE_COWBUF_WASTE_FACTOR_THRESHOLD((cur),(len)) \
113 #ifdef PERL_UTF8_CACHE_ASSERT
114 /* if adding more checks watch out for the following tests:
115 * t/op/index.t t/op/length.t t/op/pat.t t/op/substr.t
116 * lib/utf8.t lib/Unicode/Collate/t/index.t
119 # define ASSERT_UTF8_CACHE(cache) \
120 STMT_START { if (cache) { assert((cache)[0] <= (cache)[1]); \
121 assert((cache)[2] <= (cache)[3]); \
122 assert((cache)[3] <= (cache)[1]);} \
125 # define ASSERT_UTF8_CACHE(cache) NOOP
128 static const char S_destroy[] = "DESTROY";
129 #define S_destroy_len (sizeof(S_destroy)-1)
131 /* ============================================================================
133 An SV (or AV, HV, etc.) is allocated in two parts: the head (struct
134 sv, av, hv...) contains type and reference count information, and for
135 many types, a pointer to the body (struct xrv, xpv, xpviv...), which
136 contains fields specific to each type. Some types store all they need
137 in the head, so don't have a body.
139 In all but the most memory-paranoid configurations (ex: PURIFY), heads
140 and bodies are allocated out of arenas, which by default are
141 approximately 4K chunks of memory parcelled up into N heads or bodies.
142 Sv-bodies are allocated by their sv-type, guaranteeing size
143 consistency needed to allocate safely from arrays.
145 For SV-heads, the first slot in each arena is reserved, and holds a
146 link to the next arena, some flags, and a note of the number of slots.
147 Snaked through each arena chain is a linked list of free items; when
148 this becomes empty, an extra arena is allocated and divided up into N
149 items which are threaded into the free list.
151 SV-bodies are similar, but they use arena-sets by default, which
152 separate the link and info from the arena itself, and reclaim the 1st
153 slot in the arena. SV-bodies are further described later.
155 The following global variables are associated with arenas:
157 PL_sv_arenaroot pointer to list of SV arenas
158 PL_sv_root pointer to list of free SV structures
160 PL_body_arenas head of linked-list of body arenas
161 PL_body_roots[] array of pointers to list of free bodies of svtype
162 arrays are indexed by the svtype needed
164 A few special SV heads are not allocated from an arena, but are
165 instead directly created in the interpreter structure, eg PL_sv_undef.
166 The size of arenas can be changed from the default by setting
167 PERL_ARENA_SIZE appropriately at compile time.
169 The SV arena serves the secondary purpose of allowing still-live SVs
170 to be located and destroyed during final cleanup.
172 At the lowest level, the macros new_SV() and del_SV() grab and free
173 an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv()
174 to return the SV to the free list with error checking.) new_SV() calls
175 more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
176 SVs in the free list have their SvTYPE field set to all ones.
178 At the time of very final cleanup, sv_free_arenas() is called from
179 perl_destruct() to physically free all the arenas allocated since the
180 start of the interpreter.
182 The internal function visit() scans the SV arenas list, and calls a specified
183 function for each SV it finds which is still live, I<i.e.> which has an SvTYPE
184 other than all 1's, and a non-zero SvREFCNT. visit() is used by the
185 following functions (specified as [function that calls visit()] / [function
186 called by visit() for each SV]):
188 sv_report_used() / do_report_used()
189 dump all remaining SVs (debugging aid)
191 sv_clean_objs() / do_clean_objs(),do_clean_named_objs(),
192 do_clean_named_io_objs(),do_curse()
193 Attempt to free all objects pointed to by RVs,
194 try to do the same for all objects indir-
195 ectly referenced by typeglobs too, and
196 then do a final sweep, cursing any
197 objects that remain. Called once from
198 perl_destruct(), prior to calling sv_clean_all()
201 sv_clean_all() / do_clean_all()
202 SvREFCNT_dec(sv) each remaining SV, possibly
203 triggering an sv_free(). It also sets the
204 SVf_BREAK flag on the SV to indicate that the
205 refcnt has been artificially lowered, and thus
206 stopping sv_free() from giving spurious warnings
207 about SVs which unexpectedly have a refcnt
208 of zero. called repeatedly from perl_destruct()
209 until there are no SVs left.
211 =head2 Arena allocator API Summary
213 Private API to rest of sv.c
217 new_XPVNV(), del_body()
222 sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
226 * ========================================================================= */
229 * "A time to plant, and a time to uproot what was planted..."
232 #ifdef DEBUG_LEAKING_SCALARS
233 # define FREE_SV_DEBUG_FILE(sv) STMT_START { \
234 if ((sv)->sv_debug_file) { \
235 PerlMemShared_free((sv)->sv_debug_file); \
236 sv->sv_debug_file = NULL; \
239 # define DEBUG_SV_SERIAL(sv) \
240 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) del_SV\n", \
241 PTR2UV(sv), (long)(sv)->sv_debug_serial))
243 # define FREE_SV_DEBUG_FILE(sv)
244 # define DEBUG_SV_SERIAL(sv) NOOP
247 /* Mark an SV head as unused, and add to free list.
249 * If SVf_BREAK is set, skip adding it to the free list, as this SV had
250 * its refcount artificially decremented during global destruction, so
251 * there may be dangling pointers to it. The last thing we want in that
252 * case is for it to be reused. */
254 #define plant_SV(p) \
256 const U32 old_flags = SvFLAGS(p); \
257 MEM_LOG_DEL_SV(p, __FILE__, __LINE__, FUNCTION__); \
258 DEBUG_SV_SERIAL(p); \
259 FREE_SV_DEBUG_FILE(p); \
261 SvFLAGS(p) = SVTYPEMASK; \
262 if (!(old_flags & SVf_BREAK)) { \
263 SvARENA_CHAIN_SET(p, PL_sv_root); \
270 /* make some more SVs by adding another arena */
276 char *chunk; /* must use New here to match call to */
277 Newx(chunk,PERL_ARENA_SIZE,char); /* Safefree() in sv_free_arenas() */
278 sv_add_arena(chunk, PERL_ARENA_SIZE, 0);
283 /* del_SV(): return an empty SV head to the free list */
296 S_del_sv(pTHX_ SV *p)
298 PERL_ARGS_ASSERT_DEL_SV;
303 for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) {
304 const SV * const sv = sva + 1;
305 const SV * const svend = &sva[SvREFCNT(sva)];
306 if (p >= sv && p < svend) {
312 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
313 "Attempt to free non-arena SV: 0x%" UVxf
314 pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
321 #else /* ! DEBUGGING */
323 #define del_SV(p) plant_SV(p)
325 #endif /* DEBUGGING */
329 =for apidoc_section $SV
331 =for apidoc sv_add_arena
333 Given a chunk of memory, link it to the head of the list of arenas,
334 and split it into a list of free SVs.
340 S_sv_add_arena(pTHX_ char *const ptr, const U32 size, const U32 flags)
342 SV *const sva = MUTABLE_SV(ptr);
346 PERL_ARGS_ASSERT_SV_ADD_ARENA;
348 /* The first SV in an arena isn't an SV. */
349 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
350 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
351 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
353 PL_sv_arenaroot = sva;
354 PL_sv_root = sva + 1;
356 svend = &sva[SvREFCNT(sva) - 1];
359 SvARENA_CHAIN_SET(sv, (sv + 1));
363 /* Must always set typemask because it's always checked in on cleanup
364 when the arenas are walked looking for objects. */
365 SvFLAGS(sv) = SVTYPEMASK;
368 SvARENA_CHAIN_SET(sv, 0);
372 SvFLAGS(sv) = SVTYPEMASK;
375 /* visit(): call the named function for each non-free SV in the arenas
376 * whose flags field matches the flags/mask args. */
379 S_visit(pTHX_ SVFUNC_t f, const U32 flags, const U32 mask)
384 PERL_ARGS_ASSERT_VISIT;
386 for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) {
387 const SV * const svend = &sva[SvREFCNT(sva)];
389 for (sv = sva + 1; sv < svend; ++sv) {
391 && (sv->sv_flags & mask) == flags
404 /* called by sv_report_used() for each live SV */
407 do_report_used(pTHX_ SV *const sv)
409 if (!SvIS_FREED(sv)) {
410 PerlIO_printf(Perl_debug_log, "****\n");
417 =for apidoc sv_report_used
419 Dump the contents of all SVs not yet freed (debugging aid).
425 Perl_sv_report_used(pTHX)
428 visit(do_report_used, 0, 0);
434 /* called by sv_clean_objs() for each live SV */
437 do_clean_objs(pTHX_ SV *const ref)
441 SV * const target = SvRV(ref);
442 if (SvOBJECT(target)) {
443 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(ref)));
444 if (SvWEAKREF(ref)) {
445 sv_del_backref(target, ref);
451 SvREFCNT_dec_NN(target);
458 /* clear any slots in a GV which hold objects - except IO;
459 * called by sv_clean_objs() for each live GV */
462 do_clean_named_objs(pTHX_ SV *const sv)
465 assert(SvTYPE(sv) == SVt_PVGV);
466 assert(isGV_with_GP(sv));
470 /* freeing GP entries may indirectly free the current GV;
471 * hold onto it while we mess with the GP slots */
474 if ( ((obj = GvSV(sv) )) && SvOBJECT(obj)) {
475 DEBUG_D((PerlIO_printf(Perl_debug_log,
476 "Cleaning named glob SV object:\n "), sv_dump(obj)));
478 SvREFCNT_dec_NN(obj);
480 if ( ((obj = MUTABLE_SV(GvAV(sv)) )) && SvOBJECT(obj)) {
481 DEBUG_D((PerlIO_printf(Perl_debug_log,
482 "Cleaning named glob AV object:\n "), sv_dump(obj)));
484 SvREFCNT_dec_NN(obj);
486 if ( ((obj = MUTABLE_SV(GvHV(sv)) )) && SvOBJECT(obj)) {
487 DEBUG_D((PerlIO_printf(Perl_debug_log,
488 "Cleaning named glob HV object:\n "), sv_dump(obj)));
490 SvREFCNT_dec_NN(obj);
492 if ( ((obj = MUTABLE_SV(GvCV(sv)) )) && SvOBJECT(obj)) {
493 DEBUG_D((PerlIO_printf(Perl_debug_log,
494 "Cleaning named glob CV object:\n "), sv_dump(obj)));
496 SvREFCNT_dec_NN(obj);
498 SvREFCNT_dec_NN(sv); /* undo the inc above */
501 /* clear any IO slots in a GV which hold objects (except stderr, defout);
502 * called by sv_clean_objs() for each live GV */
505 do_clean_named_io_objs(pTHX_ SV *const sv)
508 assert(SvTYPE(sv) == SVt_PVGV);
509 assert(isGV_with_GP(sv));
510 if (!GvGP(sv) || sv == (SV*)PL_stderrgv || sv == (SV*)PL_defoutgv)
514 if ( ((obj = MUTABLE_SV(GvIO(sv)) )) && SvOBJECT(obj)) {
515 DEBUG_D((PerlIO_printf(Perl_debug_log,
516 "Cleaning named glob IO object:\n "), sv_dump(obj)));
518 SvREFCNT_dec_NN(obj);
520 SvREFCNT_dec_NN(sv); /* undo the inc above */
523 /* Void wrapper to pass to visit() */
525 do_curse(pTHX_ SV * const sv) {
526 if ((PL_stderrgv && GvGP(PL_stderrgv) && (SV*)GvIO(PL_stderrgv) == sv)
527 || (PL_defoutgv && GvGP(PL_defoutgv) && (SV*)GvIO(PL_defoutgv) == sv))
533 =for apidoc sv_clean_objs
535 Attempt to destroy all objects not yet freed.
541 Perl_sv_clean_objs(pTHX)
544 PL_in_clean_objs = TRUE;
545 visit(do_clean_objs, SVf_ROK, SVf_ROK);
546 /* Some barnacles may yet remain, clinging to typeglobs.
547 * Run the non-IO destructors first: they may want to output
548 * error messages, close files etc */
549 visit(do_clean_named_objs, SVt_PVGV|SVpgv_GP, SVTYPEMASK|SVp_POK|SVpgv_GP);
550 visit(do_clean_named_io_objs, SVt_PVGV|SVpgv_GP, SVTYPEMASK|SVp_POK|SVpgv_GP);
551 /* And if there are some very tenacious barnacles clinging to arrays,
552 closures, or what have you.... */
553 visit(do_curse, SVs_OBJECT, SVs_OBJECT);
554 olddef = PL_defoutgv;
555 PL_defoutgv = NULL; /* disable skip of PL_defoutgv */
556 if (olddef && isGV_with_GP(olddef))
557 do_clean_named_io_objs(aTHX_ MUTABLE_SV(olddef));
558 olderr = PL_stderrgv;
559 PL_stderrgv = NULL; /* disable skip of PL_stderrgv */
560 if (olderr && isGV_with_GP(olderr))
561 do_clean_named_io_objs(aTHX_ MUTABLE_SV(olderr));
562 SvREFCNT_dec(olddef);
563 PL_in_clean_objs = FALSE;
566 /* called by sv_clean_all() for each live SV */
569 do_clean_all(pTHX_ SV *const sv)
571 if (sv == (const SV *) PL_fdpid || sv == (const SV *)PL_strtab) {
572 /* don't clean pid table and strtab */
575 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%" UVxf "\n", PTR2UV(sv)) ));
576 SvFLAGS(sv) |= SVf_BREAK;
581 =for apidoc sv_clean_all
583 Decrement the refcnt of each remaining SV, possibly triggering a
584 cleanup. This function may have to be called multiple times to free
585 SVs which are in complex self-referential hierarchies.
591 Perl_sv_clean_all(pTHX)
594 PL_in_clean_all = TRUE;
595 cleaned = visit(do_clean_all, 0,0);
600 ARENASETS: a meta-arena implementation which separates arena-info
601 into struct arena_set, which contains an array of struct
602 arena_descs, each holding info for a single arena. By separating
603 the meta-info from the arena, we recover the 1st slot, formerly
604 borrowed for list management. The arena_set is about the size of an
605 arena, avoiding the needless malloc overhead of a naive linked-list.
607 The cost is 1 arena-set malloc per ~320 arena-mallocs, + the unused
608 memory in the last arena-set (1/2 on average). In trade, we get
609 back the 1st slot in each arena (ie 1.7% of a CV-arena, less for
610 smaller types). The recovery of the wasted space allows use of
611 small arenas for large, rare body types, by changing array* fields
612 in body_details_by_type[] below.
615 char *arena; /* the raw storage, allocated aligned */
616 size_t size; /* its size ~4k typ */
617 svtype utype; /* bodytype stored in arena */
622 /* Get the maximum number of elements in set[] such that struct arena_set
623 will fit within PERL_ARENA_SIZE, which is probably just under 4K, and
624 therefore likely to be 1 aligned memory page. */
626 #define ARENAS_PER_SET ((PERL_ARENA_SIZE - sizeof(struct arena_set*) \
627 - 2 * sizeof(int)) / sizeof (struct arena_desc))
630 struct arena_set* next;
631 unsigned int set_size; /* ie ARENAS_PER_SET */
632 unsigned int curr; /* index of next available arena-desc */
633 struct arena_desc set[ARENAS_PER_SET];
637 =for apidoc sv_free_arenas
639 Deallocate the memory used by all arenas. Note that all the individual SV
640 heads and bodies within the arenas must already have been freed.
646 Perl_sv_free_arenas(pTHX)
652 /* Free arenas here, but be careful about fake ones. (We assume
653 contiguity of the fake ones with the corresponding real ones.) */
655 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
656 svanext = MUTABLE_SV(SvANY(sva));
657 while (svanext && SvFAKE(svanext))
658 svanext = MUTABLE_SV(SvANY(svanext));
665 struct arena_set *aroot = (struct arena_set*) PL_body_arenas;
668 struct arena_set *current = aroot;
671 assert(aroot->set[i].arena);
672 Safefree(aroot->set[i].arena);
680 i = PERL_ARENA_ROOTS_SIZE;
682 PL_body_roots[i] = 0;
689 Historically, here were mid-level routines that manage the
690 allocation of bodies out of the various arenas. Some of these
691 routines and related definitions remain here, but others were
692 moved into sv_inline.h to facilitate inlining of newSV_type().
694 There are 4 kinds of arenas:
696 1. SV-head arenas, which are discussed and handled above
697 2. regular body arenas
698 3. arenas for reduced-size bodies
701 Arena types 2 & 3 are chained by body-type off an array of
702 arena-root pointers, which is indexed by svtype. Some of the
703 larger/less used body types are malloced singly, since a large
704 unused block of them is wasteful. Also, several svtypes don't have
705 bodies; the data fits into the sv-head itself. The arena-root
706 pointer thus has a few unused root-pointers (which may be hijacked
707 later for arena type 4)
709 3 differs from 2 as an optimization; some body types have several
710 unused fields in the front of the structure (which are kept in-place
711 for consistency). These bodies can be allocated in smaller chunks,
712 because the leading fields arent accessed. Pointers to such bodies
713 are decremented to point at the unused 'ghost' memory, knowing that
714 the pointers are used with offsets to the real memory.
716 Allocation of SV-bodies is similar to SV-heads, differing as follows;
717 the allocation mechanism is used for many body types, so is somewhat
718 more complicated, it uses arena-sets, and has no need for still-live
721 At the outermost level, (new|del)_X*V macros return bodies of the
722 appropriate type. These macros call either (new|del)_body_type or
723 (new|del)_body_allocated macro pairs, depending on specifics of the
724 type. Most body types use the former pair, the latter pair is used to
725 allocate body types with "ghost fields".
727 "ghost fields" are fields that are unused in certain types, and
728 consequently don't need to actually exist. They are declared because
729 they're part of a "base type", which allows use of functions as
730 methods. The simplest examples are AVs and HVs, 2 aggregate types
731 which don't use the fields which support SCALAR semantics.
733 For these types, the arenas are carved up into appropriately sized
734 chunks, we thus avoid wasted memory for those unaccessed members.
735 When bodies are allocated, we adjust the pointer back in memory by the
736 size of the part not allocated, so it's as if we allocated the full
737 structure. (But things will all go boom if you write to the part that
738 is "not there", because you'll be overwriting the last members of the
739 preceding structure in memory.)
741 We calculate the correction using the STRUCT_OFFSET macro on the first
742 member present. If the allocated structure is smaller (no initial NV
743 actually allocated) then the net effect is to subtract the size of the NV
744 from the pointer, to return a new pointer as if an initial NV were actually
745 allocated. (We were using structures named *_allocated for this, but
746 this turned out to be a subtle bug, because a structure without an NV
747 could have a lower alignment constraint, but the compiler is allowed to
748 optimised accesses based on the alignment constraint of the actual pointer
749 to the full structure, for example, using a single 64 bit load instruction
750 because it "knows" that two adjacent 32 bit members will be 8-byte aligned.)
752 This is the same trick as was used for NV and IV bodies. Ironically it
753 doesn't need to be used for NV bodies any more, because NV is now at
754 the start of the structure. IV bodies, and also in some builds NV bodies,
755 don't need it either, because they are no longer allocated.
757 In turn, the new_body_* allocators call S_new_body(), which invokes
758 new_body_from_arena macro, which takes a lock, and takes a body off the
759 linked list at PL_body_roots[sv_type], calling Perl_more_bodies() if
760 necessary to refresh an empty list. Then the lock is released, and
761 the body is returned.
763 Perl_more_bodies allocates a new arena, and carves it up into an array of N
764 bodies, which it strings into a linked list. It looks up arena-size
765 and body-size from the body_details table described below, thus
766 supporting the multiple body-types.
768 If PURIFY is defined, or PERL_ARENA_SIZE=0, arenas are not used, and
769 the (new|del)_X*V macros are mapped directly to malloc/free.
771 For each sv-type, struct body_details bodies_by_type[] carries
772 parameters which control these aspects of SV handling:
774 Arena_size determines whether arenas are used for this body type, and if
775 so, how big they are. PURIFY or PERL_ARENA_SIZE=0 set this field to
776 zero, forcing individual mallocs and frees.
778 Body_size determines how big a body is, and therefore how many fit into
779 each arena. Offset carries the body-pointer adjustment needed for
780 "ghost fields", and is used in *_allocated macros.
782 But its main purpose is to parameterize info needed in
783 Perl_sv_upgrade(). The info here dramatically simplifies the function
784 vs the implementation in 5.8.8, making it table-driven. All fields
785 are used for this, except for arena_size.
787 For the sv-types that have no bodies, arenas are not used, so those
788 PL_body_roots[sv_type] are unused, and can be overloaded. In
789 something of a special case, SVt_NULL is borrowed for HE arenas;
790 PL_body_roots[HE_ARENA_ROOT_IX=SVt_NULL] is filled by S_more_he, but the
791 bodies_by_type[SVt_NULL] slot is not used, as the table is not
792 available in hv.c. Similarly SVt_IV is re-used for HVAUX_ARENA_ROOT_IX.
796 /* return a thing to the free list */
798 #define del_body(thing, root) \
800 void ** const thing_copy = (void **)thing; \
801 *thing_copy = *root; \
802 *root = (void*)thing_copy; \
807 Perl_more_bodies (pTHX_ const svtype sv_type, const size_t body_size,
808 const size_t arena_size)
810 void ** const root = &PL_body_roots[sv_type];
811 struct arena_desc *adesc;
812 struct arena_set *aroot = (struct arena_set *) PL_body_arenas;
816 const size_t good_arena_size = Perl_malloc_good_size(arena_size);
817 #if defined(DEBUGGING)
818 static bool done_sanity_check;
820 if (!done_sanity_check) {
821 unsigned int i = SVt_LAST;
823 done_sanity_check = TRUE;
826 assert (bodies_by_type[i].type == i);
832 /* may need new arena-set to hold new arena */
833 if (!aroot || aroot->curr >= aroot->set_size) {
834 struct arena_set *newroot;
835 Newxz(newroot, 1, struct arena_set);
836 newroot->set_size = ARENAS_PER_SET;
837 newroot->next = aroot;
839 PL_body_arenas = (void *) newroot;
840 DEBUG_m(PerlIO_printf(Perl_debug_log, "new arenaset %p\n", (void*)aroot));
843 /* ok, now have arena-set with at least 1 empty/available arena-desc */
844 curr = aroot->curr++;
845 adesc = &(aroot->set[curr]);
846 assert(!adesc->arena);
848 Newx(adesc->arena, good_arena_size, char);
849 adesc->size = good_arena_size;
850 adesc->utype = sv_type;
851 DEBUG_m(PerlIO_printf(Perl_debug_log, "arena %d added: %p size %" UVuf "\n",
852 curr, (void*)adesc->arena, (UV)good_arena_size));
854 start = (char *) adesc->arena;
856 /* Get the address of the byte after the end of the last body we can fit.
857 Remember, this is integer division: */
858 end = start + good_arena_size / body_size * body_size;
860 /* computed count doesn't reflect the 1st slot reservation */
861 #if defined(MYMALLOC) || defined(HAS_MALLOC_GOOD_SIZE)
862 DEBUG_m(PerlIO_printf(Perl_debug_log,
863 "arena %p end %p arena-size %d (from %d) type %d "
865 (void*)start, (void*)end, (int)good_arena_size,
866 (int)arena_size, sv_type, (int)body_size,
867 (int)good_arena_size / (int)body_size));
869 DEBUG_m(PerlIO_printf(Perl_debug_log,
870 "arena %p end %p arena-size %d type %d size %d ct %d\n",
871 (void*)start, (void*)end,
872 (int)arena_size, sv_type, (int)body_size,
873 (int)good_arena_size / (int)body_size));
875 *root = (void *)start;
878 /* Where the next body would start: */
879 char * const next = start + body_size;
882 /* This is the last body: */
889 *(void**) start = (void *)next;
895 =for apidoc sv_upgrade
897 Upgrade an SV to a more complex form. Generally adds a new body type to the
898 SV, then copies across as much information as possible from the old body.
899 It croaks if the SV is already in a more complex form than requested. You
900 generally want to use the C<SvUPGRADE> macro wrapper, which checks the type
901 before calling C<sv_upgrade>, and hence does not croak. See also
908 Perl_sv_upgrade(pTHX_ SV *const sv, svtype new_type)
912 const svtype old_type = SvTYPE(sv);
913 const struct body_details *new_type_details;
914 const struct body_details *old_type_details
915 = bodies_by_type + old_type;
918 PERL_ARGS_ASSERT_SV_UPGRADE;
920 if (old_type == new_type)
923 /* This clause was purposefully added ahead of the early return above to
924 the shared string hackery for (sort {$a <=> $b} keys %hash), with the
925 inference by Nick I-S that it would fix other troublesome cases. See
926 changes 7162, 7163 (f130fd4589cf5fbb24149cd4db4137c8326f49c1 and parent)
928 Given that shared hash key scalars are no longer PVIV, but PV, there is
929 no longer need to unshare so as to free up the IVX slot for its proper
930 purpose. So it's safe to move the early return earlier. */
932 if (new_type > SVt_PVMG && SvIsCOW(sv)) {
933 sv_force_normal_flags(sv, 0);
936 old_body = SvANY(sv);
938 /* Copying structures onto other structures that have been neatly zeroed
939 has a subtle gotcha. Consider XPVMG
941 +------+------+------+------+------+-------+-------+
942 | NV | CUR | LEN | IV | MAGIC | STASH |
943 +------+------+------+------+------+-------+-------+
946 where NVs are aligned to 8 bytes, so that sizeof that structure is
947 actually 32 bytes long, with 4 bytes of padding at the end:
949 +------+------+------+------+------+-------+-------+------+
950 | NV | CUR | LEN | IV | MAGIC | STASH | ??? |
951 +------+------+------+------+------+-------+-------+------+
952 0 4 8 12 16 20 24 28 32
954 so what happens if you allocate memory for this structure:
956 +------+------+------+------+------+-------+-------+------+------+...
957 | NV | CUR | LEN | IV | MAGIC | STASH | GP | NAME |
958 +------+------+------+------+------+-------+-------+------+------+...
959 0 4 8 12 16 20 24 28 32 36
961 zero it, then copy sizeof(XPVMG) bytes on top of it? Not quite what you
962 expect, because you copy the area marked ??? onto GP. Now, ??? may have
963 started out as zero once, but it's quite possible that it isn't. So now,
964 rather than a nicely zeroed GP, you have it pointing somewhere random.
967 (In fact, GP ends up pointing at a previous GP structure, because the
968 principle cause of the padding in XPVMG getting garbage is a copy of
969 sizeof(XPVMG) bytes from a XPVGV structure in sv_unglob. Right now
970 this happens to be moot because XPVGV has been re-ordered, with GP
971 no longer after STASH)
973 So we are careful and work out the size of used parts of all the
982 old_type_details = &fake_rv;
983 if (new_type == SVt_NV)
986 if (new_type < SVt_PVIV) {
987 new_type = (new_type == SVt_NV)
988 ? SVt_PVNV : SVt_PVIV;
993 if (new_type < SVt_PVNV) {
998 assert(new_type > SVt_PV);
999 STATIC_ASSERT_STMT(SVt_IV < SVt_PV);
1000 STATIC_ASSERT_STMT(SVt_NV < SVt_PV);
1007 /* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
1008 there's no way that it can be safely upgraded, because perl.c
1009 expects to Safefree(SvANY(PL_mess_sv)) */
1010 assert(sv != PL_mess_sv);
1013 if (UNLIKELY(old_type_details->cant_upgrade))
1014 Perl_croak(aTHX_ "Can't upgrade %s (%" UVuf ") to %" UVuf,
1015 sv_reftype(sv, 0), (UV) old_type, (UV) new_type);
1018 if (UNLIKELY(old_type > new_type))
1019 Perl_croak(aTHX_ "sv_upgrade from type %d down to type %d",
1020 (int)old_type, (int)new_type);
1022 new_type_details = bodies_by_type + new_type;
1024 SvFLAGS(sv) &= ~SVTYPEMASK;
1025 SvFLAGS(sv) |= new_type;
1027 /* This can't happen, as SVt_NULL is <= all values of new_type, so one of
1028 the return statements above will have triggered. */
1029 assert (new_type != SVt_NULL);
1032 assert(old_type == SVt_NULL);
1033 SET_SVANY_FOR_BODYLESS_IV(sv);
1037 assert(old_type == SVt_NULL);
1038 #if NVSIZE <= IVSIZE
1039 SET_SVANY_FOR_BODYLESS_NV(sv);
1041 SvANY(sv) = new_XNV();
1048 assert(new_type_details->body_size);
1051 assert(new_type_details->arena);
1052 assert(new_type_details->arena_size);
1053 /* This points to the start of the allocated area. */
1054 new_body = S_new_body(aTHX_ new_type);
1055 /* xpvav and xpvhv have no offset, so no need to adjust new_body */
1056 assert(!(new_type_details->offset));
1058 /* We always allocated the full length item with PURIFY. To do this
1059 we fake things so that arena is false for all 16 types.. */
1060 new_body = new_NOARENAZ(new_type_details);
1062 SvANY(sv) = new_body;
1068 .xmg_u = {.xmg_magic = NULL},
1069 .xav_fill = -1, .xav_max = -1, .xav_alloc = 0
1071 *((XPVAV*) SvANY(sv)) = pvav;
1080 .xmg_u = {.xmg_magic = NULL},
1082 /* start with PERL_HASH_DEFAULT_HvMAX+1 buckets: */
1083 .xhv_max = PERL_HASH_DEFAULT_HvMAX
1085 *((XPVHV*) SvANY(sv)) = pvhv;
1090 #ifndef NODEFAULT_SHAREKEYS
1091 HvSHAREKEYS_on(sv); /* key-sharing on by default */
1097 .xmg_stash = NULL, .xmg_u = {.xmg_magic = NULL},
1098 .xobject_maxfield = -1,
1099 .xobject_iter_sv_at = 0,
1100 .xobject_fields = NULL,
1102 *((XPVOBJ*) SvANY(sv)) = pvo;
1109 /* SVt_NULL isn't the only thing upgraded to AV or HV.
1110 The target created by newSVrv also is, and it can have magic.
1111 However, it never has SvPVX set.
1113 if (old_type == SVt_IV) {
1115 } else if (old_type >= SVt_PV) {
1116 assert(SvPVX_const(sv) == 0);
1119 if (old_type >= SVt_PVMG) {
1120 SvMAGIC_set(sv, ((XPVMG*)old_body)->xmg_u.xmg_magic);
1121 SvSTASH_set(sv, ((XPVMG*)old_body)->xmg_stash);
1123 sv->sv_u.svu_array = NULL; /* or svu_hash */
1128 /* XXX Is this still needed? Was it ever needed? Surely as there is
1129 no route from NV to PVIV, NOK can never be true */
1130 assert(!SvNOKp(sv));
1144 assert(new_type_details->body_size);
1145 /* We always allocated the full length item with PURIFY. To do this
1146 we fake things so that arena is false for all 16 types.. */
1148 if(new_type_details->arena) {
1149 /* This points to the start of the allocated area. */
1150 new_body = S_new_body(aTHX_ new_type);
1151 Zero(new_body, new_type_details->body_size, char);
1152 new_body = ((char *)new_body) - new_type_details->offset;
1156 new_body = new_NOARENAZ(new_type_details);
1158 SvANY(sv) = new_body;
1160 if (old_type_details->copy) {
1161 /* There is now the potential for an upgrade from something without
1162 an offset (PVNV or PVMG) to something with one (PVCV, PVFM) */
1163 int offset = old_type_details->offset;
1164 int length = old_type_details->copy;
1166 if (new_type_details->offset > old_type_details->offset) {
1167 const int difference
1168 = new_type_details->offset - old_type_details->offset;
1169 offset += difference;
1170 length -= difference;
1172 assert (length >= 0);
1174 Copy((char *)old_body + offset, (char *)new_body + offset, length,
1178 #ifndef NV_ZERO_IS_ALLBITS_ZERO
1179 /* If NV 0.0 is stores as all bits 0 then Zero() already creates a
1180 * correct 0.0 for us. Otherwise, if the old body didn't have an
1181 * NV slot, but the new one does, then we need to initialise the
1182 * freshly created NV slot with whatever the correct bit pattern is
1184 if (old_type_details->zero_nv && !new_type_details->zero_nv
1185 && !isGV_with_GP(sv))
1189 if (UNLIKELY(new_type == SVt_PVIO)) {
1190 IO * const io = MUTABLE_IO(sv);
1191 GV *iogv = gv_fetchpvs("IO::File::", GV_ADD, SVt_PVHV);
1194 /* Clear the stashcache because a new IO could overrule a package
1196 DEBUG_o(Perl_deb(aTHX_ "sv_upgrade clearing PL_stashcache\n"));
1197 hv_clear(PL_stashcache);
1199 SvSTASH_set(io, MUTABLE_HV(SvREFCNT_inc(GvHV(iogv))));
1200 IoPAGE_LEN(sv) = 60;
1202 if (old_type < SVt_PV) {
1203 /* referent will be NULL unless the old type was SVt_IV emulating
1205 sv->sv_u.svu_rv = referent;
1209 Perl_croak(aTHX_ "panic: sv_upgrade to unknown type %lu",
1210 (unsigned long)new_type);
1213 /* if this is zero, this is a body-less SVt_NULL, SVt_IV/SVt_RV,
1214 and sometimes SVt_NV */
1215 if (old_type_details->body_size) {
1219 /* Note that there is an assumption that all bodies of types that
1220 can be upgraded came from arenas. Only the more complex non-
1221 upgradable types are allowed to be directly malloc()ed. */
1222 assert(old_type_details->arena);
1223 del_body((void*)((char*)old_body + old_type_details->offset),
1224 &PL_body_roots[old_type]);
1230 Perl_hv_auxalloc(pTHX_ HV *hv) {
1231 const struct body_details *old_type_details = bodies_by_type + SVt_PVHV;
1235 PERL_ARGS_ASSERT_HV_AUXALLOC;
1236 assert(SvTYPE(hv) == SVt_PVHV);
1237 assert(!HvHasAUX(hv));
1240 new_body = new_NOARENAZ(&fake_hv_with_aux);
1242 new_body_from_arena(new_body, HVAUX_ARENA_ROOT_IX, fake_hv_with_aux);
1245 old_body = SvANY(hv);
1247 Copy((char *)old_body + old_type_details->offset,
1248 (char *)new_body + fake_hv_with_aux.offset,
1249 old_type_details->copy,
1255 assert(old_type_details->arena);
1256 del_body((void*)((char*)old_body + old_type_details->offset),
1257 &PL_body_roots[SVt_PVHV]);
1260 SvANY(hv) = (XPVHV *) new_body;
1261 SvFLAGS(hv) |= SVphv_HasAUX;
1266 =for apidoc sv_backoff
1268 Remove any string offset. You should normally use the C<SvOOK_off> macro
1274 /* prior to 5.000 stable, this function returned the new OOK-less SvFLAGS
1275 prior to 5.23.4 this function always returned 0
1279 Perl_sv_backoff(SV *const sv)
1282 const char * const s = SvPVX_const(sv);
1284 PERL_ARGS_ASSERT_SV_BACKOFF;
1287 assert(SvTYPE(sv) != SVt_PVHV);
1288 assert(SvTYPE(sv) != SVt_PVAV);
1290 SvOOK_offset(sv, delta);
1292 SvLEN_set(sv, SvLEN(sv) + delta);
1293 SvPV_set(sv, SvPVX(sv) - delta);
1294 SvFLAGS(sv) &= ~SVf_OOK;
1295 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
1300 /* forward declaration */
1301 static void S_sv_uncow(pTHX_ SV * const sv, const U32 flags);
1307 Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
1308 upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1309 Use the C<SvGROW> wrapper instead.
1316 Perl_sv_grow(pTHX_ SV *const sv, STRLEN newlen)
1320 PERL_ARGS_ASSERT_SV_GROW;
1324 if (SvTYPE(sv) < SVt_PV) {
1325 sv_upgrade(sv, SVt_PV);
1326 s = SvPVX_mutable(sv);
1328 else if (SvOOK(sv)) { /* pv is offset? */
1330 s = SvPVX_mutable(sv);
1331 if (newlen > SvLEN(sv))
1332 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
1336 if (SvIsCOW(sv)) S_sv_uncow(aTHX_ sv, 0);
1337 s = SvPVX_mutable(sv);
1340 #ifdef PERL_COPY_ON_WRITE
1341 /* the new COW scheme uses SvPVX(sv)[SvLEN(sv)-1] (if spare)
1342 * to store the COW count. So in general, allocate one more byte than
1343 * asked for, to make it likely this byte is always spare: and thus
1344 * make more strings COW-able.
1346 * Only increment if the allocation isn't MEM_SIZE_MAX,
1347 * otherwise it will wrap to 0.
1349 if ( newlen != MEM_SIZE_MAX )
1353 #if defined(PERL_USE_MALLOC_SIZE) && defined(Perl_safesysmalloc_size)
1354 #define PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC
1357 if (newlen > SvLEN(sv)) { /* need more room? */
1358 STRLEN minlen = SvCUR(sv);
1359 minlen += (minlen >> PERL_STRLEN_EXPAND_SHIFT) + PERL_STRLEN_NEW_MIN;
1360 if (newlen < minlen)
1362 #ifndef PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC
1364 /* Don't round up on the first allocation, as odds are pretty good that
1365 * the initial request is accurate as to what is really needed */
1367 STRLEN rounded = PERL_STRLEN_ROUNDUP(newlen);
1368 if (rounded > newlen)
1372 if (SvLEN(sv) && s) {
1373 s = (char*)saferealloc(s, newlen);
1376 s = (char*)safemalloc(newlen);
1377 if (SvPVX_const(sv) && SvCUR(sv)) {
1378 Move(SvPVX_const(sv), s, SvCUR(sv), char);
1382 #ifdef PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC
1383 /* Do this here, do it once, do it right, and then we will never get
1384 called back into sv_grow() unless there really is some growing
1386 SvLEN_set(sv, Perl_safesysmalloc_size(s));
1388 SvLEN_set(sv, newlen);
1395 =for apidoc sv_grow_fresh
1397 A cut-down version of sv_grow intended only for when sv is a freshly-minted
1398 SVt_PV, SVt_PVIV, SVt_PVNV, or SVt_PVMG. i.e. sv has the default flags, has
1399 never been any other type, and does not have an existing string. Basically,
1400 just assigns a char buffer and returns a pointer to it.
1407 Perl_sv_grow_fresh(pTHX_ SV *const sv, STRLEN newlen)
1411 PERL_ARGS_ASSERT_SV_GROW_FRESH;
1413 assert(SvTYPE(sv) >= SVt_PV && SvTYPE(sv) <= SVt_PVMG);
1416 assert(!SvIsCOW(sv));
1420 #ifdef PERL_COPY_ON_WRITE
1421 /* the new COW scheme uses SvPVX(sv)[SvLEN(sv)-1] (if spare)
1422 * to store the COW count. So in general, allocate one more byte than
1423 * asked for, to make it likely this byte is always spare: and thus
1424 * make more strings COW-able.
1426 * Only increment if the allocation isn't MEM_SIZE_MAX,
1427 * otherwise it will wrap to 0.
1429 if ( newlen != MEM_SIZE_MAX )
1433 if (newlen < PERL_STRLEN_NEW_MIN)
1434 newlen = PERL_STRLEN_NEW_MIN;
1436 s = (char*)safemalloc(newlen);
1439 /* No PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC here, since many strings */
1440 /* will never be grown once set. Let the real sv_grow worry about that. */
1441 SvLEN_set(sv, newlen);
1446 =for apidoc sv_setiv
1447 =for apidoc_item sv_setiv_mg
1449 These copy an integer into the given SV, upgrading first if necessary.
1451 They differ only in that C<sv_setiv_mg> handles 'set' magic; C<sv_setiv> does
1458 Perl_sv_setiv(pTHX_ SV *const sv, const IV i)
1460 PERL_ARGS_ASSERT_SV_SETIV;
1462 SV_CHECK_THINKFIRST_COW_DROP(sv);
1463 switch (SvTYPE(sv)) {
1464 #if NVSIZE <= IVSIZE
1467 SET_SVANY_FOR_BODYLESS_IV(sv);
1468 SvFLAGS(sv) &= ~SVTYPEMASK;
1469 SvFLAGS(sv) |= SVt_IV;
1473 SET_SVANY_FOR_BODYLESS_IV(sv);
1474 SvFLAGS(sv) &= ~SVTYPEMASK;
1475 SvFLAGS(sv) |= SVt_IV;
1478 sv_upgrade(sv, SVt_IV);
1482 sv_upgrade(sv, SVt_PVIV);
1486 if (!isGV_with_GP(sv))
1494 /* diag_listed_as: Can't coerce %s to %s in %s */
1495 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
1497 NOT_REACHED; /* NOTREACHED */
1501 (void)SvIOK_only(sv); /* validate number */
1507 Perl_sv_setiv_mg(pTHX_ SV *const sv, const IV i)
1509 PERL_ARGS_ASSERT_SV_SETIV_MG;
1516 =for apidoc sv_setuv
1517 =for apidoc_item sv_setuv_mg
1519 These copy an unsigned integer into the given SV, upgrading first if necessary.
1522 They differ only in that C<sv_setuv_mg> handles 'set' magic; C<sv_setuv> does
1529 Perl_sv_setuv(pTHX_ SV *const sv, const UV u)
1531 PERL_ARGS_ASSERT_SV_SETUV;
1533 /* With the if statement to ensure that integers are stored as IVs whenever
1535 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
1538 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
1540 If you wish to remove the following if statement, so that this routine
1541 (and its callers) always return UVs, please benchmark to see what the
1542 effect is. Modern CPUs may be different. Or may not :-)
1544 if (u <= (UV)IV_MAX) {
1545 sv_setiv(sv, (IV)u);
1554 Perl_sv_setuv_mg(pTHX_ SV *const sv, const UV u)
1556 PERL_ARGS_ASSERT_SV_SETUV_MG;
1563 =for apidoc sv_setnv
1564 =for apidoc_item sv_setnv_mg
1566 These copy a double into the given SV, upgrading first if necessary.
1568 They differ only in that C<sv_setnv_mg> handles 'set' magic; C<sv_setnv> does
1575 Perl_sv_setnv(pTHX_ SV *const sv, const NV num)
1577 PERL_ARGS_ASSERT_SV_SETNV;
1579 SV_CHECK_THINKFIRST_COW_DROP(sv);
1580 switch (SvTYPE(sv)) {
1583 #if NVSIZE <= IVSIZE
1584 SET_SVANY_FOR_BODYLESS_NV(sv);
1585 SvFLAGS(sv) &= ~SVTYPEMASK;
1586 SvFLAGS(sv) |= SVt_NV;
1589 sv_upgrade(sv, SVt_NV);
1594 sv_upgrade(sv, SVt_PVNV);
1598 if (!isGV_with_GP(sv))
1606 /* diag_listed_as: Can't coerce %s to %s in %s */
1607 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
1609 NOT_REACHED; /* NOTREACHED */
1614 (void)SvNOK_only(sv); /* validate number */
1619 Perl_sv_setnv_mg(pTHX_ SV *const sv, const NV num)
1621 PERL_ARGS_ASSERT_SV_SETNV_MG;
1628 =for apidoc sv_setrv_noinc
1629 =for apidoc_item sv_setrv_noinc_mg
1631 Copies an SV pointer into the given SV as an SV reference, upgrading it if
1632 necessary. After this, C<SvRV(sv)> is equal to I<ref>. This does not adjust
1633 the reference count of I<ref>. The reference I<ref> must not be NULL.
1635 C<sv_setrv_noinc_mg> will invoke 'set' magic on the SV; C<sv_setrv_noinc> will
1642 Perl_sv_setrv_noinc(pTHX_ SV *const sv, SV *const ref)
1644 PERL_ARGS_ASSERT_SV_SETRV_NOINC;
1646 SV_CHECK_THINKFIRST_COW_DROP(sv);
1647 prepare_SV_for_RV(sv);
1655 Perl_sv_setrv_noinc_mg(pTHX_ SV *const sv, SV *const ref)
1657 PERL_ARGS_ASSERT_SV_SETRV_NOINC_MG;
1659 sv_setrv_noinc(sv, ref);
1664 =for apidoc sv_setrv_inc
1665 =for apidoc_item sv_setrv_inc_mg
1667 As C<sv_setrv_noinc> but increments the reference count of I<ref>.
1669 C<sv_setrv_inc_mg> will invoke 'set' magic on the SV; C<sv_setrv_inc> will
1676 Perl_sv_setrv_inc(pTHX_ SV *const sv, SV *const ref)
1678 PERL_ARGS_ASSERT_SV_SETRV_INC;
1680 sv_setrv_noinc(sv, SvREFCNT_inc_simple_NN(ref));
1684 Perl_sv_setrv_inc_mg(pTHX_ SV *const sv, SV *const ref)
1686 PERL_ARGS_ASSERT_SV_SETRV_INC_MG;
1688 sv_setrv_noinc(sv, SvREFCNT_inc_simple_NN(ref));
1692 /* Return a cleaned-up, printable version of sv, for non-numeric, or
1693 * not incrementable warning display.
1694 * Originally part of S_not_a_number().
1695 * The return value may be != tmpbuf.
1699 S_sv_display(pTHX_ SV *const sv, char *tmpbuf, STRLEN tmpbuf_size) {
1702 PERL_ARGS_ASSERT_SV_DISPLAY;
1705 SV *dsv = newSVpvs_flags("", SVs_TEMP);
1706 pv = sv_uni_display(dsv, sv, 32, UNI_DISPLAY_ISPRINT);
1709 const char * const limit = tmpbuf + tmpbuf_size - 8;
1710 /* each *s can expand to 4 chars + "...\0",
1711 i.e. need room for 8 chars */
1713 const char *s = SvPVX_const(sv);
1714 const char * const end = s + SvCUR(sv);
1715 for ( ; s < end && d < limit; s++ ) {
1717 if (! isASCII(ch) && !isPRINT_LC(ch)) {
1721 /* Map to ASCII "equivalent" of Latin1 */
1722 ch = LATIN1_TO_NATIVE(NATIVE_TO_LATIN1(ch) & 127);
1728 else if (ch == '\r') {
1732 else if (ch == '\f') {
1736 else if (ch == '\\') {
1740 else if (ch == '\0') {
1744 else if (isPRINT_LC(ch))
1763 /* Print an "isn't numeric" warning, using a cleaned-up,
1764 * printable version of the offending string
1768 S_not_a_number(pTHX_ SV *const sv)
1773 PERL_ARGS_ASSERT_NOT_A_NUMBER;
1775 pv = sv_display(sv, tmpbuf, sizeof(tmpbuf));
1778 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1779 /* diag_listed_as: Argument "%s" isn't numeric%s */
1780 "Argument \"%s\" isn't numeric in %s", pv,
1783 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1784 /* diag_listed_as: Argument "%s" isn't numeric%s */
1785 "Argument \"%s\" isn't numeric", pv);
1789 S_not_incrementable(pTHX_ SV *const sv) {
1793 PERL_ARGS_ASSERT_NOT_INCREMENTABLE;
1795 pv = sv_display(sv, tmpbuf, sizeof(tmpbuf));
1797 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1798 "Argument \"%s\" treated as 0 in increment (++)", pv);
1802 =for apidoc looks_like_number
1804 Test if the content of an SV looks like a number (or is a number).
1805 C<Inf> and C<Infinity> are treated as numbers (so will not issue a
1806 non-numeric warning), even if your C<atof()> doesn't grok them. Get-magic is
1813 Perl_looks_like_number(pTHX_ SV *const sv)
1819 PERL_ARGS_ASSERT_LOOKS_LIKE_NUMBER;
1821 if (SvPOK(sv) || SvPOKp(sv)) {
1822 sbegin = SvPV_nomg_const(sv, len);
1825 return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
1826 numtype = grok_number(sbegin, len, NULL);
1827 return ((numtype & IS_NUMBER_TRAILING)) ? 0 : numtype;
1831 S_glob_2number(pTHX_ GV * const gv)
1833 PERL_ARGS_ASSERT_GLOB_2NUMBER;
1835 /* We know that all GVs stringify to something that is not-a-number,
1836 so no need to test that. */
1837 if (ckWARN(WARN_NUMERIC))
1839 SV *const buffer = sv_newmortal();
1840 gv_efullname3(buffer, gv, "*");
1841 not_a_number(buffer);
1843 /* We just want something true to return, so that S_sv_2iuv_common
1844 can tail call us and return true. */
1848 /* Actually, ISO C leaves conversion of UV to IV undefined, but
1849 until proven guilty, assume that things are not that bad... */
1854 As 64 bit platforms often have an NV that doesn't preserve all bits of
1855 an IV (an assumption perl has been based on to date) it becomes necessary
1856 to remove the assumption that the NV always carries enough precision to
1857 recreate the IV whenever needed, and that the NV is the canonical form.
1858 Instead, IV/UV and NV need to be given equal rights. So as to not lose
1859 precision as a side effect of conversion (which would lead to insanity
1860 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
1861 1) to distinguish between IV/UV/NV slots that have a valid conversion cached
1862 where precision was lost, and IV/UV/NV slots that have a valid conversion
1863 which has lost no precision
1864 2) to ensure that if a numeric conversion to one form is requested that
1865 would lose precision, the precise conversion (or differently
1866 imprecise conversion) is also performed and cached, to prevent
1867 requests for different numeric formats on the same SV causing
1868 lossy conversion chains. (lossless conversion chains are perfectly
1873 SvIOKp is true if the IV slot contains a valid value
1874 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
1875 SvNOKp is true if the NV slot contains a valid value
1876 SvNOK is true only if the NV value is accurate
1879 while converting from PV to NV, check to see if converting that NV to an
1880 IV(or UV) would lose accuracy over a direct conversion from PV to
1881 IV(or UV). If it would, cache both conversions, return NV, but mark
1882 SV as IOK NOKp (ie not NOK).
1884 While converting from PV to IV, check to see if converting that IV to an
1885 NV would lose accuracy over a direct conversion from PV to NV. If it
1886 would, cache both conversions, flag similarly.
1888 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
1889 correctly because if IV & NV were set NV *always* overruled.
1890 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
1891 changes - now IV and NV together means that the two are interchangeable:
1892 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
1894 The benefit of this is that operations such as pp_add know that if
1895 SvIOK is true for both left and right operands, then integer addition
1896 can be used instead of floating point (for cases where the result won't
1897 overflow). Before, floating point was always used, which could lead to
1898 loss of precision compared with integer addition.
1900 * making IV and NV equal status should make maths accurate on 64 bit
1902 * may speed up maths somewhat if pp_add and friends start to use
1903 integers when possible instead of fp. (Hopefully the overhead in
1904 looking for SvIOK and checking for overflow will not outweigh the
1905 fp to integer speedup)
1906 * will slow down integer operations (callers of SvIV) on "inaccurate"
1907 values, as the change from SvIOK to SvIOKp will cause a call into
1908 sv_2iv each time rather than a macro access direct to the IV slot
1909 * should speed up number->string conversion on integers as IV is
1910 favoured when IV and NV are equally accurate
1912 ####################################################################
1913 You had better be using SvIOK_notUV if you want an IV for arithmetic:
1914 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
1915 On the other hand, SvUOK is true iff UV.
1916 ####################################################################
1918 Your mileage will vary depending your CPU's relative fp to integer
1922 #ifndef NV_PRESERVES_UV
1923 # define IS_NUMBER_UNDERFLOW_IV 1
1924 # define IS_NUMBER_UNDERFLOW_UV 2
1925 # define IS_NUMBER_IV_AND_UV 2
1926 # define IS_NUMBER_OVERFLOW_IV 4
1927 # define IS_NUMBER_OVERFLOW_UV 5
1929 /* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
1931 /* For sv_2nv these three cases are "SvNOK and don't bother casting" */
1933 S_sv_2iuv_non_preserve(pTHX_ SV *const sv
1939 PERL_ARGS_ASSERT_SV_2IUV_NON_PRESERVE;
1940 PERL_UNUSED_CONTEXT;
1942 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_2iuv_non '%s', IV=0x%" UVxf " NV=%" NVgf " inttype=%" UVXf "\n", SvPVX_const(sv), SvIVX(sv), SvNVX(sv), (UV)numtype));
1943 if (SvNVX(sv) < (NV)IV_MIN) {
1944 (void)SvIOKp_on(sv);
1946 SvIV_set(sv, IV_MIN);
1947 return IS_NUMBER_UNDERFLOW_IV;
1949 if (SvNVX(sv) > (NV)UV_MAX) {
1950 (void)SvIOKp_on(sv);
1953 SvUV_set(sv, UV_MAX);
1954 return IS_NUMBER_OVERFLOW_UV;
1956 (void)SvIOKp_on(sv);
1958 /* Can't use strtol etc to convert this string. (See truth table in
1960 if (SvNVX(sv) < IV_MAX_P1) {
1961 SvIV_set(sv, I_V(SvNVX(sv)));
1962 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
1963 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
1965 /* Integer is imprecise. NOK, IOKp */
1967 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
1970 SvUV_set(sv, U_V(SvNVX(sv)));
1971 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
1972 if (SvUVX(sv) == UV_MAX) {
1973 /* As we know that NVs don't preserve UVs, UV_MAX cannot
1974 possibly be preserved by NV. Hence, it must be overflow.
1976 return IS_NUMBER_OVERFLOW_UV;
1978 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
1980 /* Integer is imprecise. NOK, IOKp */
1982 return IS_NUMBER_OVERFLOW_IV;
1984 #endif /* !NV_PRESERVES_UV*/
1986 /* If numtype is infnan, set the NV of the sv accordingly.
1987 * If numtype is anything else, try setting the NV using Atof(PV). */
1989 S_sv_setnv(pTHX_ SV* sv, int numtype)
1991 bool pok = cBOOL(SvPOK(sv));
1994 if ((numtype & IS_NUMBER_INFINITY)) {
1995 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -NV_INF : NV_INF);
2000 if ((numtype & IS_NUMBER_NAN)) {
2001 SvNV_set(sv, NV_NAN);
2006 SvNV_set(sv, Atof(SvPVX_const(sv)));
2007 /* Purposefully no true nok here, since we don't want to blow
2008 * away the possible IOK/UV of an existing sv. */
2011 SvNOK_only(sv); /* No IV or UV please, this is pure infnan. */
2013 SvPOK_on(sv); /* PV is okay, though. */
2018 S_sv_2iuv_common(pTHX_ SV *const sv)
2020 PERL_ARGS_ASSERT_SV_2IUV_COMMON;
2023 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2024 * without also getting a cached IV/UV from it at the same time
2025 * (ie PV->NV conversion should detect loss of accuracy and cache
2026 * IV or UV at same time to avoid this. */
2027 /* IV-over-UV optimisation - choose to cache IV if possible */
2029 if (SvTYPE(sv) == SVt_NV)
2030 sv_upgrade(sv, SVt_PVNV);
2033 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2034 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
2035 certainly cast into the IV range at IV_MAX, whereas the correct
2036 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
2038 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2039 if (Perl_isnan(SvNVX(sv))) {
2045 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2046 SvIV_set(sv, I_V(SvNVX(sv)));
2047 if (SvNVX(sv) == (NV) SvIVX(sv)
2048 #ifndef NV_PRESERVES_UV
2049 && SvIVX(sv) != IV_MIN /* avoid negating IV_MIN below */
2050 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2051 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2052 /* Don't flag it as "accurately an integer" if the number
2053 came from a (by definition imprecise) NV operation, and
2054 we're outside the range of NV integer precision */
2058 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2060 /* scalar has trailing garbage, eg "42a" */
2062 DEBUG_c(PerlIO_printf(Perl_debug_log,
2063 "0x%" UVxf " iv(%" NVgf " => %" IVdf ") (precise)\n",
2069 /* IV not precise. No need to convert from PV, as NV
2070 conversion would already have cached IV if it detected
2071 that PV->IV would be better than PV->NV->IV
2072 flags already correct - don't set public IOK. */
2073 DEBUG_c(PerlIO_printf(Perl_debug_log,
2074 "0x%" UVxf " iv(%" NVgf " => %" IVdf ") (imprecise)\n",
2079 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2080 but the cast (NV)IV_MIN rounds to a the value less (more
2081 negative) than IV_MIN which happens to be equal to SvNVX ??
2082 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2083 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2084 (NV)UVX == NVX are both true, but the values differ. :-(
2085 Hopefully for 2s complement IV_MIN is something like
2086 0x8000000000000000 which will be exact. NWC */
2089 SvUV_set(sv, U_V(SvNVX(sv)));
2091 (SvNVX(sv) == (NV) SvUVX(sv))
2092 #ifndef NV_PRESERVES_UV
2093 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2094 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2095 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2096 /* Don't flag it as "accurately an integer" if the number
2097 came from a (by definition imprecise) NV operation, and
2098 we're outside the range of NV integer precision */
2104 DEBUG_c(PerlIO_printf(Perl_debug_log,
2105 "0x%" UVxf " 2iv(%" UVuf " => %" IVdf ") (as unsigned)\n",
2111 else if (SvPOKp(sv)) {
2114 const char *s = SvPVX_const(sv);
2115 const STRLEN cur = SvCUR(sv);
2117 /* short-cut for a single digit string like "1" */
2122 if (SvTYPE(sv) < SVt_PVIV)
2123 sv_upgrade(sv, SVt_PVIV);
2125 SvIV_set(sv, (IV)(c - '0'));
2130 numtype = grok_number(s, cur, &value);
2131 /* We want to avoid a possible problem when we cache an IV/ a UV which
2132 may be later translated to an NV, and the resulting NV is not
2133 the same as the direct translation of the initial string
2134 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2135 be careful to ensure that the value with the .456 is around if the
2136 NV value is requested in the future).
2138 This means that if we cache such an IV/a UV, we need to cache the
2139 NV as well. Moreover, we trade speed for space, and do not
2140 cache the NV if we are sure it's not needed.
2143 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2144 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2145 == IS_NUMBER_IN_UV) {
2146 /* It's definitely an integer, only upgrade to PVIV */
2147 if (SvTYPE(sv) < SVt_PVIV)
2148 sv_upgrade(sv, SVt_PVIV);
2150 } else if (SvTYPE(sv) < SVt_PVNV)
2151 sv_upgrade(sv, SVt_PVNV);
2153 if ((numtype & (IS_NUMBER_INFINITY | IS_NUMBER_NAN))) {
2154 if (ckWARN(WARN_NUMERIC) && ((numtype & IS_NUMBER_TRAILING)))
2156 S_sv_setnv(aTHX_ sv, numtype);
2157 goto got_nv; /* Fill IV/UV slot and set IOKp */
2160 /* If NVs preserve UVs then we only use the UV value if we know that
2161 we aren't going to call atof() below. If NVs don't preserve UVs
2162 then the value returned may have more precision than atof() will
2163 return, even though value isn't perfectly accurate. */
2164 if ((numtype & (IS_NUMBER_IN_UV
2165 #ifdef NV_PRESERVES_UV
2168 )) == IS_NUMBER_IN_UV) {
2169 /* This won't turn off the public IOK flag if it was set above */
2170 (void)SvIOKp_on(sv);
2172 if (!(numtype & IS_NUMBER_NEG)) {
2174 if (value <= (UV)IV_MAX) {
2175 SvIV_set(sv, (IV)value);
2177 /* it didn't overflow, and it was positive. */
2178 SvUV_set(sv, value);
2182 /* 2s complement assumption */
2183 if (value <= (UV)IV_MIN) {
2184 SvIV_set(sv, value == (UV)IV_MIN
2185 ? IV_MIN : -(IV)value);
2187 /* Too negative for an IV. This is a double upgrade, but
2188 I'm assuming it will be rare. */
2189 if (SvTYPE(sv) < SVt_PVNV)
2190 sv_upgrade(sv, SVt_PVNV);
2194 SvNV_set(sv, -(NV)value);
2195 SvIV_set(sv, IV_MIN);
2199 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2200 will be in the previous block to set the IV slot, and the next
2201 block to set the NV slot. So no else here. */
2203 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2204 != IS_NUMBER_IN_UV) {
2205 /* It wasn't an (integer that doesn't overflow the UV). */
2206 S_sv_setnv(aTHX_ sv, numtype);
2208 if (! numtype && ckWARN(WARN_NUMERIC))
2211 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2iv(%" NVgf ")\n",
2212 PTR2UV(sv), SvNVX(sv)));
2214 #ifdef NV_PRESERVES_UV
2218 goto got_nv; /* Fill IV/UV slot and set IOKp, maybe IOK */
2219 #else /* NV_PRESERVES_UV */
2220 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2221 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2222 /* The IV/UV slot will have been set from value returned by
2223 grok_number above. The NV slot has just been set using
2226 assert (SvIOKp(sv));
2228 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2229 U_V(Perl_fabs(SvNVX(sv)))) {
2230 /* Small enough to preserve all bits. */
2231 (void)SvIOKp_on(sv);
2233 SvIV_set(sv, I_V(SvNVX(sv)));
2234 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2236 /* There had been runtime checking for
2237 "U_V(Perl_fabs(SvNVX(sv))) < (UV)IV_MAX" here to ensure
2238 that this NV is in the preserved range, but this should
2239 be always true if the following assertion is true: */
2240 STATIC_ASSERT_STMT(((UV)1 << NV_PRESERVES_UV_BITS) <=
2244 0 0 already failed to read UV.
2245 0 1 already failed to read UV.
2246 1 0 you won't get here in this case. IV/UV
2247 slot set, public IOK, Atof() unneeded.
2248 1 1 already read UV.
2249 so there's no point in sv_2iuv_non_preserve() attempting
2250 to use atol, strtol, strtoul etc. */
2252 sv_2iuv_non_preserve (sv, numtype);
2254 sv_2iuv_non_preserve (sv);
2258 /* It might be more code efficient to go through the entire logic above
2259 and conditionally set with SvIOKp_on() rather than SvIOK(), but it
2260 gets complex and potentially buggy, so more programmer efficient
2261 to do it this way, by turning off the public flags: */
2263 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK);
2264 #endif /* NV_PRESERVES_UV */
2268 if (isGV_with_GP(sv))
2269 return glob_2number(MUTABLE_GV(sv));
2271 if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
2273 if (SvTYPE(sv) < SVt_IV)
2274 /* Typically the caller expects that sv_any is not NULL now. */
2275 sv_upgrade(sv, SVt_IV);
2276 /* Return 0 from the caller. */
2283 =for apidoc sv_2iv_flags
2285 Return the integer value of an SV, doing any necessary string
2286 conversion. If C<flags> has the C<SV_GMAGIC> bit set, does an C<mg_get()> first.
2287 Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
2293 Perl_sv_2iv_flags(pTHX_ SV *const sv, const I32 flags)
2295 PERL_ARGS_ASSERT_SV_2IV_FLAGS;
2297 assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
2298 && SvTYPE(sv) != SVt_PVFM);
2300 if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
2306 if (flags & SV_SKIP_OVERLOAD)
2308 tmpstr = AMG_CALLunary(sv, numer_amg);
2309 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2310 return SvIV(tmpstr);
2313 return PTR2IV(SvRV(sv));
2316 if (SvVALID(sv) || isREGEXP(sv)) {
2317 /* FBMs use the space for SvIVX and SvNVX for other purposes, so
2318 must not let them cache IVs.
2319 In practice they are extremely unlikely to actually get anywhere
2320 accessible by user Perl code - the only way that I'm aware of is when
2321 a constant subroutine which is used as the second argument to index.
2323 Regexps have no SvIVX and SvNVX fields.
2328 const char * const ptr =
2329 isREGEXP(sv) ? RX_WRAPPED((REGEXP*)sv) : SvPVX_const(sv);
2331 = grok_number(ptr, SvCUR(sv), &value);
2333 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2334 == IS_NUMBER_IN_UV) {
2335 /* It's definitely an integer */
2336 if (numtype & IS_NUMBER_NEG) {
2337 if (value < (UV)IV_MIN)
2340 if (value < (UV)IV_MAX)
2345 /* Quite wrong but no good choices. */
2346 if ((numtype & IS_NUMBER_INFINITY)) {
2347 return (numtype & IS_NUMBER_NEG) ? IV_MIN : IV_MAX;
2348 } else if ((numtype & IS_NUMBER_NAN)) {
2349 return 0; /* So wrong. */
2353 if (ckWARN(WARN_NUMERIC))
2356 return I_V(Atof(ptr));
2360 if (SvTHINKFIRST(sv)) {
2361 if (SvREADONLY(sv) && !SvOK(sv)) {
2362 if (ckWARN(WARN_UNINITIALIZED))
2369 if (S_sv_2iuv_common(aTHX_ sv))
2373 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2iv(%" IVdf ")\n",
2374 PTR2UV(sv),SvIVX(sv)));
2375 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
2379 =for apidoc sv_2uv_flags
2381 Return the unsigned integer value of an SV, doing any necessary string
2382 conversion. If C<flags> has the C<SV_GMAGIC> bit set, does an C<mg_get()> first.
2383 Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros.
2385 =for apidoc Amnh||SV_GMAGIC
2391 Perl_sv_2uv_flags(pTHX_ SV *const sv, const I32 flags)
2393 PERL_ARGS_ASSERT_SV_2UV_FLAGS;
2395 if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
2401 if (flags & SV_SKIP_OVERLOAD)
2403 tmpstr = AMG_CALLunary(sv, numer_amg);
2404 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2405 return SvUV(tmpstr);
2408 return PTR2UV(SvRV(sv));
2411 if (SvVALID(sv) || isREGEXP(sv)) {
2412 /* FBMs use the space for SvIVX and SvNVX for other purposes, and use
2413 the same flag bit as SVf_IVisUV, so must not let them cache IVs.
2414 Regexps have no SvIVX and SvNVX fields. */
2418 const char * const ptr =
2419 isREGEXP(sv) ? RX_WRAPPED((REGEXP*)sv) : SvPVX_const(sv);
2421 = grok_number(ptr, SvCUR(sv), &value);
2423 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2424 == IS_NUMBER_IN_UV) {
2425 /* It's definitely an integer */
2426 if (!(numtype & IS_NUMBER_NEG))
2430 /* Quite wrong but no good choices. */
2431 if ((numtype & IS_NUMBER_INFINITY)) {
2432 return UV_MAX; /* So wrong. */
2433 } else if ((numtype & IS_NUMBER_NAN)) {
2434 return 0; /* So wrong. */
2438 if (ckWARN(WARN_NUMERIC))
2441 return U_V(Atof(ptr));
2445 if (SvTHINKFIRST(sv)) {
2446 if (SvREADONLY(sv) && !SvOK(sv)) {
2447 if (ckWARN(WARN_UNINITIALIZED))
2454 if (S_sv_2iuv_common(aTHX_ sv))
2458 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2uv(%" UVuf ")\n",
2459 PTR2UV(sv),SvUVX(sv)));
2460 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
2464 =for apidoc sv_2nv_flags
2466 Return the num value of an SV, doing any necessary string or integer
2467 conversion. If C<flags> has the C<SV_GMAGIC> bit set, does an C<mg_get()> first.
2468 Normally used via the C<SvNV(sv)> and C<SvNVx(sv)> macros.
2474 Perl_sv_2nv_flags(pTHX_ SV *const sv, const I32 flags)
2476 PERL_ARGS_ASSERT_SV_2NV_FLAGS;
2478 assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
2479 && SvTYPE(sv) != SVt_PVFM);
2480 if (SvGMAGICAL(sv) || SvVALID(sv) || isREGEXP(sv)) {
2481 /* FBMs use the space for SvIVX and SvNVX for other purposes, and use
2482 the same flag bit as SVf_IVisUV, so must not let them cache NVs.
2483 Regexps have no SvIVX and SvNVX fields. */
2485 if (flags & SV_GMAGIC)
2489 if (SvPOKp(sv) && !SvIOKp(sv)) {
2490 ptr = SvPVX_const(sv);
2491 if (!SvIOKp(sv) && ckWARN(WARN_NUMERIC) &&
2492 !grok_number(ptr, SvCUR(sv), NULL))
2498 return (NV)SvUVX(sv);
2500 return (NV)SvIVX(sv);
2505 assert(SvTYPE(sv) >= SVt_PVMG);
2506 /* This falls through to the report_uninit near the end of the
2508 } else if (SvTHINKFIRST(sv)) {
2513 if (flags & SV_SKIP_OVERLOAD)
2515 tmpstr = AMG_CALLunary(sv, numer_amg);
2516 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2517 return SvNV(tmpstr);
2520 return PTR2NV(SvRV(sv));
2522 if (SvREADONLY(sv) && !SvOK(sv)) {
2523 if (ckWARN(WARN_UNINITIALIZED))
2528 if (SvTYPE(sv) < SVt_NV) {
2529 /* The logic to use SVt_PVNV if necessary is in sv_upgrade. */
2530 sv_upgrade(sv, SVt_NV);
2531 CLANG_DIAG_IGNORE_STMT(-Wthread-safety);
2533 DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
2534 STORE_LC_NUMERIC_SET_STANDARD();
2535 PerlIO_printf(Perl_debug_log,
2536 "0x%" UVxf " num(%" NVgf ")\n",
2537 PTR2UV(sv), SvNVX(sv));
2538 RESTORE_LC_NUMERIC();
2540 CLANG_DIAG_RESTORE_STMT;
2543 else if (SvTYPE(sv) < SVt_PVNV)
2544 sv_upgrade(sv, SVt_PVNV);
2549 SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
2550 #ifdef NV_PRESERVES_UV
2556 /* Only set the public NV OK flag if this NV preserves the IV */
2557 /* Check it's not 0xFFFFFFFFFFFFFFFF */
2559 SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
2560 : (SvIVX(sv) == I_V(SvNVX(sv))))
2566 else if (SvPOKp(sv)) {
2568 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2569 if (!SvIOKp(sv) && !numtype && ckWARN(WARN_NUMERIC))
2571 #ifdef NV_PRESERVES_UV
2572 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2573 == IS_NUMBER_IN_UV) {
2574 /* It's definitely an integer */
2575 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
2577 S_sv_setnv(aTHX_ sv, numtype);
2584 SvNV_set(sv, Atof(SvPVX_const(sv)));
2585 /* Only set the public NV OK flag if this NV preserves the value in
2586 the PV at least as well as an IV/UV would.
2587 Not sure how to do this 100% reliably. */
2588 /* if that shift count is out of range then Configure's test is
2589 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
2591 if (((UV)1 << NV_PRESERVES_UV_BITS) > U_V(Perl_fabs(SvNVX(sv)))) {
2592 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
2593 } else if (!(numtype & IS_NUMBER_IN_UV)) {
2594 /* Can't use strtol etc to convert this string, so don't try.
2595 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
2598 /* value has been set. It may not be precise. */
2599 if ((numtype & IS_NUMBER_NEG) && (value >= (UV)IV_MIN)) {
2600 /* 2s complement assumption for (UV)IV_MIN */
2601 SvNOK_on(sv); /* Integer is too negative. */
2606 if (numtype & IS_NUMBER_NEG) {
2607 /* -IV_MIN is undefined, but we should never reach
2608 * this point with both IS_NUMBER_NEG and value ==
2610 assert(value != (UV)IV_MIN);
2611 SvIV_set(sv, -(IV)value);
2612 } else if (value <= (UV)IV_MAX) {
2613 SvIV_set(sv, (IV)value);
2615 SvUV_set(sv, value);
2619 if (numtype & IS_NUMBER_NOT_INT) {
2620 /* I believe that even if the original PV had decimals,
2621 they are lost beyond the limit of the FP precision.
2622 However, neither is canonical, so both only get p
2623 flags. NWC, 2000/11/25 */
2624 /* Both already have p flags, so do nothing */
2626 const NV nv = SvNVX(sv);
2627 /* XXX should this spot have NAN_COMPARE_BROKEN, too? */
2628 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2629 if (SvIVX(sv) == I_V(nv)) {
2632 /* It had no "." so it must be integer. */
2636 /* between IV_MAX and NV(UV_MAX).
2637 Could be slightly > UV_MAX */
2639 if (numtype & IS_NUMBER_NOT_INT) {
2640 /* UV and NV both imprecise. */
2642 const UV nv_as_uv = U_V(nv);
2644 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
2653 /* It might be more code efficient to go through the entire logic above
2654 and conditionally set with SvNOKp_on() rather than SvNOK(), but it
2655 gets complex and potentially buggy, so more programmer efficient
2656 to do it this way, by turning off the public flags: */
2658 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK);
2659 #endif /* NV_PRESERVES_UV */
2662 if (isGV_with_GP(sv)) {
2663 glob_2number(MUTABLE_GV(sv));
2667 if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
2669 assert (SvTYPE(sv) >= SVt_NV);
2670 /* Typically the caller expects that sv_any is not NULL now. */
2671 /* XXX Ilya implies that this is a bug in callers that assume this
2672 and ideally should be fixed. */
2675 CLANG_DIAG_IGNORE_STMT(-Wthread-safety);
2677 DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
2678 STORE_LC_NUMERIC_SET_STANDARD();
2679 PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2nv(%" NVgf ")\n",
2680 PTR2UV(sv), SvNVX(sv));
2681 RESTORE_LC_NUMERIC();
2683 CLANG_DIAG_RESTORE_STMT;
2690 Return an SV with the numeric value of the source SV, doing any necessary
2691 reference or overload conversion. The caller is expected to have handled
2698 Perl_sv_2num(pTHX_ SV *const sv)
2700 PERL_ARGS_ASSERT_SV_2NUM;
2705 SV * const tmpsv = AMG_CALLunary(sv, numer_amg);
2706 TAINT_IF(tmpsv && SvTAINTED(tmpsv));
2707 if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
2708 return sv_2num(tmpsv);
2710 return sv_2mortal(newSVuv(PTR2UV(SvRV(sv))));
2713 /* int2str_table: lookup table containing string representations of all
2714 * two digit numbers. For example, int2str_table.arr[0] is "00" and
2715 * int2str_table.arr[12*2] is "12".
2717 * We are going to read two bytes at a time, so we have to ensure that
2718 * the array is aligned to a 2 byte boundary. That's why it was made a
2719 * union with a dummy U16 member. */
2720 static const union {
2723 } int2str_table = {{
2724 '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5', '0', '6',
2725 '0', '7', '0', '8', '0', '9', '1', '0', '1', '1', '1', '2', '1', '3',
2726 '1', '4', '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', '2', '0',
2727 '2', '1', '2', '2', '2', '3', '2', '4', '2', '5', '2', '6', '2', '7',
2728 '2', '8', '2', '9', '3', '0', '3', '1', '3', '2', '3', '3', '3', '4',
2729 '3', '5', '3', '6', '3', '7', '3', '8', '3', '9', '4', '0', '4', '1',
2730 '4', '2', '4', '3', '4', '4', '4', '5', '4', '6', '4', '7', '4', '8',
2731 '4', '9', '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', '5', '5',
2732 '5', '6', '5', '7', '5', '8', '5', '9', '6', '0', '6', '1', '6', '2',
2733 '6', '3', '6', '4', '6', '5', '6', '6', '6', '7', '6', '8', '6', '9',
2734 '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', '7', '5', '7', '6',
2735 '7', '7', '7', '8', '7', '9', '8', '0', '8', '1', '8', '2', '8', '3',
2736 '8', '4', '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', '9', '0',
2737 '9', '1', '9', '2', '9', '3', '9', '4', '9', '5', '9', '6', '9', '7',
2741 /* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
2742 * UV as a string towards the end of buf, and return pointers to start and
2745 * We assume that buf is at least TYPE_CHARS(UV) long.
2748 PERL_STATIC_INLINE char *
2749 S_uiv_2buf(char *const buf, const IV iv, UV uv, const int is_uv, char **const peob)
2751 char *ptr = buf + TYPE_CHARS(UV);
2752 char * const ebuf = ptr;
2754 U16 *word_ptr, *word_table;
2756 PERL_ARGS_ASSERT_UIV_2BUF;
2758 /* ptr has to be properly aligned, because we will cast it to U16* */
2759 assert(PTR2nat(ptr) % 2 == 0);
2760 /* we are going to read/write two bytes at a time */
2761 word_ptr = (U16*)ptr;
2762 word_table = (U16*)int2str_table.arr;
2764 if (UNLIKELY(is_uv))
2770 /* Using 0- here to silence bogus warning from MS VC */
2771 uv = (UV) (0 - (UV) iv);
2776 *--word_ptr = word_table[uv % 100];
2779 ptr = (char*)word_ptr;
2782 *--ptr = (char)uv + '0';
2784 *--word_ptr = word_table[uv];
2785 ptr = (char*)word_ptr;
2795 /* Helper for sv_2pv_flags and sv_vcatpvfn_flags. If the NV is an
2796 * infinity or a not-a-number, writes the appropriate strings to the
2797 * buffer, including a zero byte. On success returns the written length,
2798 * excluding the zero byte, on failure (not an infinity, not a nan)
2799 * returns zero, assert-fails on maxlen being too short.
2801 * XXX for "Inf", "-Inf", and "NaN", we could have three read-only
2802 * shared string constants we point to, instead of generating a new
2803 * string for each instance. */
2805 S_infnan_2pv(NV nv, char* buffer, size_t maxlen, char plus) {
2807 assert(maxlen >= 4);
2808 if (Perl_isinf(nv)) {
2810 if (maxlen < 5) /* "-Inf\0" */
2820 else if (Perl_isnan(nv)) {
2824 /* XXX optionally output the payload mantissa bits as
2825 * "(unsigned)" (to match the nan("...") C99 function,
2826 * or maybe as "(0xhhh...)" would make more sense...
2827 * provide a format string so that the user can decide?
2828 * NOTE: would affect the maxlen and assert() logic.*/
2833 assert((s == buffer + 3) || (s == buffer + 4));
2840 =for apidoc_item sv_2pv_flags
2842 These implement the various forms of the L<perlapi/C<SvPV>> macros.
2843 The macros are the preferred interface.
2845 These return a pointer to the string value of an SV (coercing it to a string if
2846 necessary), and set C<*lp> to its length in bytes.
2848 The forms differ in that plain C<sv_2pvbyte> always processes 'get' magic; and
2849 C<sv_2pvbyte_flags> processes 'get' magic if and only if C<flags> contains
2852 =for apidoc Amnh||SV_GMAGIC
2858 Perl_sv_2pv_flags(pTHX_ SV *const sv, STRLEN *const lp, const U32 flags)
2861 bool done_gmagic = FALSE;
2863 PERL_ARGS_ASSERT_SV_2PV_FLAGS;
2865 assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
2866 && SvTYPE(sv) != SVt_PVFM);
2867 if (SvGMAGICAL(sv) && (flags & SV_GMAGIC)) {
2876 if (flags & SV_SKIP_OVERLOAD)
2879 nsv = sv_mortalcopy_flags(sv,0);
2880 tmpstr = AMG_CALLunary(nsv, string_amg);
2881 TAINT_IF(tmpstr && SvTAINTED(tmpstr));
2882 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(nsv)))) {
2884 /* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr);
2888 if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) {
2889 if (flags & SV_CONST_RETURN) {
2890 pv = (char *) SvPVX_const(tmpstr);
2892 pv = (flags & SV_MUTABLE_RETURN)
2893 ? SvPVX_mutable(tmpstr) : SvPVX(tmpstr);
2896 *lp = SvCUR(tmpstr);
2898 pv = sv_2pv_flags(tmpstr, lp, flags);
2911 SV *const referent = SvRV(sv);
2915 retval = buffer = savepvn("NULLREF", len);
2916 } else if (SvTYPE(referent) == SVt_REGEXP &&
2917 (!(PL_curcop->cop_hints & HINT_NO_AMAGIC) ||
2918 amagic_is_enabled(string_amg))) {
2919 REGEXP * const re = (REGEXP *)MUTABLE_PTR(referent);
2923 /* If the regex is UTF-8 we want the containing scalar to
2924 have an UTF-8 flag too */
2931 *lp = RX_WRAPLEN(re);
2933 return RX_WRAPPED(re);
2935 const char *const typestring = sv_reftype(referent, 0);
2936 const STRLEN typelen = strlen(typestring);
2937 UV addr = PTR2UV(referent);
2938 const char *stashname = NULL;
2939 STRLEN stashnamelen = 0; /* hush, gcc */
2940 const char *buffer_end;
2942 if (SvOBJECT(referent)) {
2943 const HEK *const name = HvNAME_HEK(SvSTASH(referent));
2946 stashname = HEK_KEY(name);
2947 stashnamelen = HEK_LEN(name);
2949 if (HEK_UTF8(name)) {
2955 stashname = "__ANON__";
2958 len = stashnamelen + 1 /* = */ + typelen + 3 /* (0x */
2959 + 2 * sizeof(UV) + 2 /* )\0 */;
2961 len = typelen + 3 /* (0x */
2962 + 2 * sizeof(UV) + 2 /* )\0 */;
2965 Newx(buffer, len, char);
2966 buffer_end = retval = buffer + len;
2968 /* Working backwards */
2972 *--retval = PL_hexdigit[addr & 15];
2973 } while (addr >>= 4);
2979 memcpy(retval, typestring, typelen);
2983 retval -= stashnamelen;
2984 memcpy(retval, stashname, stashnamelen);
2986 /* retval may not necessarily have reached the start of the
2988 assert (retval >= buffer);
2990 len = buffer_end - retval - 1; /* -1 for that \0 */
3002 if (flags & SV_MUTABLE_RETURN)
3003 return SvPVX_mutable(sv);
3004 if (flags & SV_CONST_RETURN)
3005 return (char *)SvPVX_const(sv);
3010 /* I'm assuming that if both IV and NV are equally valid then
3011 converting the IV is going to be more efficient */
3012 const U32 isUIOK = SvIsUV(sv);
3013 /* The purpose of this union is to ensure that arr is aligned on
3014 a 2 byte boundary, because that is what uiv_2buf() requires */
3016 char arr[TYPE_CHARS(UV)];
3022 if (SvTYPE(sv) < SVt_PVIV)
3023 sv_upgrade(sv, SVt_PVIV);
3024 ptr = uiv_2buf(buf.arr, SvIVX(sv), SvUVX(sv), isUIOK, &ebuf);
3026 /* inlined from sv_setpvn */
3027 s = SvGROW_mutable(sv, len + 1);
3028 Move(ptr, s, len, char);
3031 /* We used to call SvPOK_on(). Whilst this is fine for (most) Perl code,
3032 it means that after this stringification is cached, there is no way
3033 to distinguish between values originally assigned as $a = 42; and
3034 $a = "42"; (or results of string operators vs numeric operators)
3035 where the value has subsequently been used in the other sense
3036 and had a value cached.
3037 This (somewhat) hack means that we retain the cached stringification,
3038 but don't set SVf_POK. Hence if a value is SVf_IOK|SVf_POK then it
3039 originated as "42", whereas if it's SVf_IOK then it originated as 42.
3040 (ignore SVp_IOK and SVp_POK)
3041 The SvPV macros are now updated to recognise this specific case
3042 (and that there isn't overloading or magic that could alter the
3043 cached value) and so return the cached value immediately without
3044 re-entering this function, getting back here to this block of code,
3045 and repeating the same conversion. */
3048 else if (SvNOK(sv)) {
3049 if (SvTYPE(sv) < SVt_PVNV)
3050 sv_upgrade(sv, SVt_PVNV);
3051 if (SvNVX(sv) == 0.0
3052 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
3053 && !Perl_isnan(SvNVX(sv))
3056 s = SvGROW_mutable(sv, 2);
3061 STRLEN size = 5; /* "-Inf\0" */
3063 s = SvGROW_mutable(sv, size);
3064 len = S_infnan_2pv(SvNVX(sv), s, size, 0);
3070 /* some Xenix systems wipe out errno here */
3079 5 + /* exponent digits */
3083 s = SvGROW_mutable(sv, size);
3084 #ifndef USE_LOCALE_NUMERIC
3085 SNPRINTF_G(SvNVX(sv), s, SvLEN(sv), NV_DIG);
3091 DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
3092 STORE_LC_NUMERIC_SET_TO_NEEDED();
3094 local_radix = NOT_IN_NUMERIC_STANDARD_;
3095 if (local_radix && SvCUR(PL_numeric_radix_sv) > 1) {
3096 size += SvCUR(PL_numeric_radix_sv) - 1;
3097 s = SvGROW_mutable(sv, size);
3100 SNPRINTF_G(SvNVX(sv), s, SvLEN(sv), NV_DIG);
3102 /* If the radix character is UTF-8, and actually is in the
3103 * output, turn on the UTF-8 flag for the scalar */
3105 && SvUTF8(PL_numeric_radix_sv)
3106 && instr(s, SvPVX_const(PL_numeric_radix_sv)))
3111 RESTORE_LC_NUMERIC();
3114 /* We don't call SvPOK_on(), because it may come to
3115 * pass that the locale changes so that the
3116 * stringification we just did is no longer correct. We
3117 * will have to re-stringify every time it is needed */
3124 else if (isGV_with_GP(sv)) {
3125 GV *const gv = MUTABLE_GV(sv);
3126 SV *const buffer = sv_newmortal();
3128 gv_efullname3(buffer, gv, "*");
3130 assert(SvPOK(buffer));
3136 *lp = SvCUR(buffer);
3137 return SvPVX(buffer);
3142 if (flags & SV_UNDEF_RETURNS_NULL)
3144 if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
3146 /* Typically the caller expects that sv_any is not NULL now. */
3147 if (!SvREADONLY(sv) && SvTYPE(sv) < SVt_PV)
3148 sv_upgrade(sv, SVt_PV);
3153 const STRLEN len = s - SvPVX_const(sv);
3158 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2pv(%s)\n",
3159 PTR2UV(sv),SvPVX_const(sv)));
3160 if (flags & SV_CONST_RETURN)
3161 return (char *)SvPVX_const(sv);
3162 if (flags & SV_MUTABLE_RETURN)
3163 return SvPVX_mutable(sv);
3168 =for apidoc sv_copypv
3169 =for apidoc_item sv_copypv_flags
3170 =for apidoc_item sv_copypv_nomg
3172 These copy a stringified representation of the source SV into the
3173 destination SV. They automatically perform coercion of numeric values into
3174 strings. Guaranteed to preserve the C<UTF8> flag even from overloaded objects.
3175 Similar in nature to C<sv_2pv[_flags]> but they operate directly on an SV
3176 instead of just the string. Mostly they use L</C<sv_2pv_flags>> to
3177 do the work, except when that would lose the UTF-8'ness of the PV.
3179 The three forms differ only in whether or not they perform 'get magic' on
3180 C<sv>. C<sv_copypv_nomg> skips 'get magic'; C<sv_copypv> performs it; and
3181 C<sv_copypv_flags> either performs it (if the C<SV_GMAGIC> bit is set in
3182 C<flags>) or doesn't (if that bit is cleared).
3188 Perl_sv_copypv_flags(pTHX_ SV *const dsv, SV *const ssv, const I32 flags)
3193 PERL_ARGS_ASSERT_SV_COPYPV_FLAGS;
3195 s = SvPV_flags_const(ssv,len,(flags & SV_GMAGIC));
3196 sv_setpvn(dsv,s,len);
3204 =for apidoc sv_2pvbyte
3205 =for apidoc_item sv_2pvbyte_flags
3207 These implement the various forms of the L<perlapi/C<SvPVbyte>> macros.
3208 The macros are the preferred interface.
3210 These return a pointer to the byte-encoded representation of the SV, and set
3211 C<*lp> to its length. If the SV is marked as being encoded as UTF-8, it will
3212 be downgraded, if possible, to a byte string. If the SV cannot be downgraded,
3215 The forms differ in that plain C<sv_2pvbyte> always processes 'get' magic; and
3216 C<sv_2pvbyte_flags> processes 'get' magic if and only if C<flags> contains
3219 =for apidoc Amnh||SV_GMAGIC
3225 Perl_sv_2pvbyte_flags(pTHX_ SV *sv, STRLEN *const lp, const U32 flags)
3227 PERL_ARGS_ASSERT_SV_2PVBYTE_FLAGS;
3229 if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
3231 if (((SvREADONLY(sv) || SvFAKE(sv)) && !SvIsCOW(sv))
3232 || isGV_with_GP(sv) || SvROK(sv)) {
3233 SV *sv2 = sv_newmortal();
3234 sv_copypv_nomg(sv2,sv);
3237 sv_utf8_downgrade_nomg(sv,0);
3238 return lp ? SvPV_nomg(sv,*lp) : SvPV_nomg_nolen(sv);
3242 =for apidoc sv_2pvutf8
3243 =for apidoc_item sv_2pvutf8_flags
3245 These implement the various forms of the L<perlapi/C<SvPVutf8>> macros.
3246 The macros are the preferred interface.
3248 These return a pointer to the UTF-8-encoded representation of the SV, and set
3249 C<*lp> to its length in bytes. They may cause the SV to be upgraded to UTF-8
3252 The forms differ in that plain C<sv_2pvutf8> always processes 'get' magic; and
3253 C<sv_2pvutf8_flags> processes 'get' magic if and only if C<flags> contains
3260 Perl_sv_2pvutf8_flags(pTHX_ SV *sv, STRLEN *const lp, const U32 flags)
3262 PERL_ARGS_ASSERT_SV_2PVUTF8_FLAGS;
3264 if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
3266 if (((SvREADONLY(sv) || SvFAKE(sv)) && !SvIsCOW(sv))
3267 || isGV_with_GP(sv) || SvROK(sv)) {
3268 SV *sv2 = sv_newmortal();
3269 sv_copypv_nomg(sv2,sv);
3272 sv_utf8_upgrade_nomg(sv);
3273 return lp ? SvPV_nomg(sv,*lp) : SvPV_nomg_nolen(sv);
3278 =for apidoc sv_2bool
3280 This macro is only used by C<sv_true()> or its macro equivalent, and only if
3281 the latter's argument is neither C<SvPOK>, C<SvIOK> nor C<SvNOK>.
3282 It calls C<sv_2bool_flags> with the C<SV_GMAGIC> flag.
3284 =for apidoc sv_2bool_flags
3286 This function is only used by C<sv_true()> and friends, and only if
3287 the latter's argument is neither C<SvPOK>, C<SvIOK> nor C<SvNOK>. If the flags
3288 contain C<SV_GMAGIC>, then it does an C<mg_get()> first.
3295 Perl_sv_2bool_flags(pTHX_ SV *sv, I32 flags)
3297 PERL_ARGS_ASSERT_SV_2BOOL_FLAGS;
3300 if(flags & SV_GMAGIC) SvGETMAGIC(sv);
3306 SV * const tmpsv = AMG_CALLunary(sv, bool__amg);
3307 if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv)))) {
3310 if(SvGMAGICAL(sv)) {
3312 goto restart; /* call sv_2bool */
3314 /* expanded SvTRUE_common(sv, (flags = 0, goto restart)) */
3315 else if(!SvOK(sv)) {
3318 else if(SvPOK(sv)) {
3319 svb = SvPVXtrue(sv);
3321 else if((SvFLAGS(sv) & (SVf_IOK|SVf_NOK))) {
3322 svb = (SvIOK(sv) && SvIVX(sv) != 0)
3323 || (SvNOK(sv) && SvNVX(sv) != 0.0);
3327 goto restart; /* call sv_2bool_nomg */
3337 RX_WRAPLEN(sv) > 1 || (RX_WRAPLEN(sv) && *RX_WRAPPED(sv) != '0');
3339 if (SvNOK(sv) && !SvPOK(sv))
3340 return SvNVX(sv) != 0.0;
3342 return SvTRUE_common(sv, 0);
3346 =for apidoc sv_utf8_upgrade
3347 =for apidoc_item sv_utf8_upgrade_flags
3348 =for apidoc_item sv_utf8_upgrade_flags_grow
3349 =for apidoc_item sv_utf8_upgrade_nomg
3351 These convert the PV of an SV to its UTF-8-encoded form.
3352 The SV is forced to string form if it is not already.
3353 They always set the C<SvUTF8> flag to avoid future validity checks even if the
3354 whole string is the same in UTF-8 as not.
3355 They return the number of bytes in the converted string
3357 The forms differ in just two ways. The main difference is whether or not they
3358 perform 'get magic' on C<sv>. C<sv_utf8_upgrade_nomg> skips 'get magic';
3359 C<sv_utf8_upgrade> performs it; and C<sv_utf8_upgrade_flags> and
3360 C<sv_utf8_upgrade_flags_grow> either perform it (if the C<SV_GMAGIC> bit is set
3361 in C<flags>) or don't (if that bit is cleared).
3363 The other difference is that C<sv_utf8_upgrade_flags_grow> has an additional
3364 parameter, C<extra>, which allows the caller to specify an amount of space to
3365 be reserved as spare beyond what is needed for the actual conversion. This is
3366 used when the caller knows it will soon be needing yet more space, and it is
3367 more efficient to request space from the system in a single call.
3368 This form is otherwise identical to C<sv_utf8_upgrade_flags>.
3370 These are not a general purpose byte encoding to Unicode interface: use the
3371 Encode extension for that.
3373 The C<SV_FORCE_UTF8_UPGRADE> flag is now ignored.
3375 =for apidoc Amnh||SV_GMAGIC|
3376 =for apidoc Amnh||SV_FORCE_UTF8_UPGRADE|
3380 If the routine itself changes the string, it adds a trailing C<NUL>. Such a
3381 C<NUL> isn't guaranteed due to having other routines do the work in some input
3382 cases, or if the input is already flagged as being in utf8.
3387 Perl_sv_utf8_upgrade_flags_grow(pTHX_ SV *const sv, const I32 flags, STRLEN extra)
3389 PERL_ARGS_ASSERT_SV_UTF8_UPGRADE_FLAGS_GROW;
3391 if (sv == &PL_sv_undef)
3393 if (!SvPOK_nog(sv)) {
3395 if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
3396 (void) sv_2pv_flags(sv,&len, flags);
3398 if (extra) SvGROW(sv, SvCUR(sv) + extra);
3402 (void) SvPV_force_flags(sv,len,flags & SV_GMAGIC);
3406 /* SVt_REGEXP's shouldn't be upgraded to UTF8 - they're already
3407 * compiled and individual nodes will remain non-utf8 even if the
3408 * stringified version of the pattern gets upgraded. Whether the
3409 * PVX of a REGEXP should be grown or we should just croak, I don't
3411 if (SvUTF8(sv) || isREGEXP(sv)) {
3412 if (extra) SvGROW(sv, SvCUR(sv) + extra);
3417 S_sv_uncow(aTHX_ sv, 0);
3420 if (SvCUR(sv) == 0) {
3421 if (extra) SvGROW(sv, extra + 1); /* Make sure is room for a trailing
3423 } else { /* Assume Latin-1/EBCDIC */
3424 /* This function could be much more efficient if we
3425 * had a FLAG in SVs to signal if there are any variant
3426 * chars in the PV. Given that there isn't such a flag
3427 * make the loop as fast as possible. */
3428 U8 * s = (U8 *) SvPVX_const(sv);
3431 if (is_utf8_invariant_string_loc(s, SvCUR(sv), (const U8 **) &t)) {
3433 /* utf8 conversion not needed because all are invariants. Mark
3434 * as UTF-8 even if no variant - saves scanning loop */
3436 if (extra) SvGROW(sv, SvCUR(sv) + extra);
3440 /* Here, there is at least one variant (t points to the first one), so
3441 * the string should be converted to utf8. Everything from 's' to
3442 * 't - 1' will occupy only 1 byte each on output.
3444 * Note that the incoming SV may not have a trailing '\0', as certain
3445 * code in pp_formline can send us partially built SVs.
3447 * There are two main ways to convert. One is to create a new string
3448 * and go through the input starting from the beginning, appending each
3449 * converted value onto the new string as we go along. Going this
3450 * route, it's probably best to initially allocate enough space in the
3451 * string rather than possibly running out of space and having to
3452 * reallocate and then copy what we've done so far. Since everything
3453 * from 's' to 't - 1' is invariant, the destination can be initialized
3454 * with these using a fast memory copy. To be sure to allocate enough
3455 * space, one could use the worst case scenario, where every remaining
3456 * byte expands to two under UTF-8, or one could parse it and count
3457 * exactly how many do expand.
3459 * The other way is to unconditionally parse the remainder of the
3460 * string to figure out exactly how big the expanded string will be,
3461 * growing if needed. Then start at the end of the string and place
3462 * the character there at the end of the unfilled space in the expanded
3463 * one, working backwards until reaching 't'.
3465 * The problem with assuming the worst case scenario is that for very
3466 * long strings, we could allocate much more memory than actually
3467 * needed, which can create performance problems. If we have to parse
3468 * anyway, the second method is the winner as it may avoid an extra
3469 * copy. The code used to use the first method under some
3470 * circumstances, but now that there is faster variant counting on
3471 * ASCII platforms, the second method is used exclusively, eliminating
3472 * some code that no longer has to be maintained. */
3475 /* Count the total number of variants there are. We can start
3476 * just beyond the first one, which is known to be at 't' */
3477 const Size_t invariant_length = t - s;
3478 U8 * e = (U8 *) SvEND(sv);
3480 /* The length of the left overs, plus 1. */
3481 const Size_t remaining_length_p1 = e - t;
3483 /* We expand by 1 for the variant at 't' and one for each remaining
3484 * variant (we start looking at 't+1') */
3485 Size_t expansion = 1 + variant_under_utf8_count(t + 1, e);
3487 /* +1 = trailing NUL */
3488 Size_t need = SvCUR(sv) + expansion + extra + 1;
3491 /* Grow if needed */
3492 if (SvLEN(sv) < need) {
3493 t = invariant_length + (U8*) SvGROW(sv, need);
3494 e = t + remaining_length_p1;
3496 SvCUR_set(sv, invariant_length + remaining_length_p1 + expansion);
3498 /* Set the NUL at the end */
3499 d = (U8 *) SvEND(sv);
3502 /* Having decremented d, it points to the position to put the
3503 * very last byte of the expanded string. Go backwards through
3504 * the string, copying and expanding as we go, stopping when we
3505 * get to the part that is invariant the rest of the way down */
3509 if (NATIVE_BYTE_IS_INVARIANT(*e)) {
3512 *d-- = UTF8_EIGHT_BIT_LO(*e);
3513 *d-- = UTF8_EIGHT_BIT_HI(*e);
3518 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
3519 /* Update pos. We do it at the end rather than during
3520 * the upgrade, to avoid slowing down the common case
3521 * (upgrade without pos).
3522 * pos can be stored as either bytes or characters. Since
3523 * this was previously a byte string we can just turn off
3524 * the bytes flag. */
3525 MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
3527 mg->mg_flags &= ~MGf_BYTES;
3529 if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
3530 magic_setutf8(sv,mg); /* clear UTF8 cache */
3540 =for apidoc sv_utf8_downgrade
3541 =for apidoc_item sv_utf8_downgrade_flags
3542 =for apidoc_item sv_utf8_downgrade_nomg
3544 These attempt to convert the PV of an SV from characters to bytes. If the PV
3545 contains a character that cannot fit in a byte, this conversion will fail; in
3546 this case, C<FALSE> is returned if C<fail_ok> is true; otherwise they croak.
3548 They are not a general purpose Unicode to byte encoding interface:
3549 use the C<Encode> extension for that.
3551 They differ only in that:
3553 C<sv_utf8_downgrade> processes 'get' magic on C<sv>.
3555 C<sv_utf8_downgrade_nomg> does not.
3557 C<sv_utf8_downgrade_flags> has an additional C<flags> parameter in which you can specify
3558 C<SV_GMAGIC> to process 'get' magic, or leave it cleared to not process 'get' magic.
3564 Perl_sv_utf8_downgrade_flags(pTHX_ SV *const sv, const bool fail_ok, const U32 flags)
3566 PERL_ARGS_ASSERT_SV_UTF8_DOWNGRADE_FLAGS;
3568 if (SvPOKp(sv) && SvUTF8(sv)) {
3572 U32 mg_flags = flags & SV_GMAGIC;
3575 S_sv_uncow(aTHX_ sv, 0);
3577 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
3579 MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
3580 if (mg && mg->mg_len > 0 && mg->mg_flags & MGf_BYTES) {
3581 mg->mg_len = sv_pos_b2u_flags(sv, mg->mg_len,
3582 mg_flags|SV_CONST_RETURN);
3583 mg_flags = 0; /* sv_pos_b2u does get magic */
3585 if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
3586 magic_setutf8(sv,mg); /* clear UTF8 cache */
3589 s = (U8 *) SvPV_flags(sv, len, mg_flags);
3591 if (!utf8_to_bytes(s, &len)) {
3596 Perl_croak(aTHX_ "Wide character in %s",
3599 Perl_croak(aTHX_ "Wide character");
3610 =for apidoc sv_utf8_encode
3612 Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8>
3613 flag off so that it looks like octets again.
3619 Perl_sv_utf8_encode(pTHX_ SV *const sv)
3621 PERL_ARGS_ASSERT_SV_UTF8_ENCODE;
3623 if (SvREADONLY(sv)) {
3624 sv_force_normal_flags(sv, 0);
3626 (void) sv_utf8_upgrade(sv);
3631 =for apidoc sv_utf8_decode
3633 If the PV of the SV is an octet sequence in Perl's extended UTF-8
3634 and contains a multiple-byte character, the C<SvUTF8> flag is turned on
3635 so that it looks like a character. If the PV contains only single-byte
3636 characters, the C<SvUTF8> flag stays off.
3637 Scans PV for validity and returns FALSE if the PV is invalid UTF-8.
3643 Perl_sv_utf8_decode(pTHX_ SV *const sv)
3645 PERL_ARGS_ASSERT_SV_UTF8_DECODE;
3648 const U8 *start, *c, *first_variant;
3650 /* The octets may have got themselves encoded - get them back as
3653 if (!sv_utf8_downgrade(sv, TRUE))
3656 /* it is actually just a matter of turning the utf8 flag on, but
3657 * we want to make sure everything inside is valid utf8 first.
3659 c = start = (const U8 *) SvPVX_const(sv);
3660 if (! is_utf8_invariant_string_loc(c, SvCUR(sv), &first_variant)) {
3661 if (!is_utf8_string(first_variant, SvCUR(sv) - (first_variant -c)))
3665 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
3666 /* XXX Is this dead code? XS_utf8_decode calls SvSETMAGIC
3667 after this, clearing pos. Does anything on CPAN
3669 /* adjust pos to the start of a UTF8 char sequence */
3670 MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
3672 SSize_t pos = mg->mg_len;
3674 for (c = start + pos; c > start; c--) {
3675 if (UTF8_IS_START(*c))
3678 mg->mg_len = c - start;
3681 if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
3682 magic_setutf8(sv,mg); /* clear UTF8 cache */
3689 =for apidoc sv_setsv
3690 =for apidoc_item sv_setsv_flags
3691 =for apidoc_item sv_setsv_mg
3692 =for apidoc_item sv_setsv_nomg
3694 These copy the contents of the source SV C<ssv> into the destination SV C<dsv>.
3695 C<ssv> may be destroyed if it is mortal, so don't use these functions if
3696 the source SV needs to be reused.
3697 Loosely speaking, they perform a copy-by-value, obliterating any previous
3698 content of the destination.
3700 They differ only in that:
3702 C<sv_setsv> calls 'get' magic on C<ssv>, but skips 'set' magic on C<dsv>.
3704 C<sv_setsv_mg> calls both 'get' magic on C<ssv> and 'set' magic on C<dsv>.
3706 C<sv_setsv_nomg> skips all magic.
3708 C<sv_setsv_flags> has a C<flags> parameter which you can use to specify any
3709 combination of magic handling, and also you can specify C<SV_NOSTEAL> so that
3710 the buffers of temps will not be stolen.
3712 You probably want to instead use one of the assortment of wrappers, such as
3713 C<L</SvSetSV>>, C<L</SvSetSV_nosteal>>, C<L</SvSetMagicSV>> and
3714 C<L</SvSetMagicSV_nosteal>>.
3716 C<sv_setsv_flags> is the primary function for copying scalars, and most other
3717 copy-ish functions and macros use it underneath.
3719 =for apidoc Amnh||SV_NOSTEAL
3725 S_glob_assign_glob(pTHX_ SV *const dsv, SV *const ssv, const int dtype)
3727 I32 mro_changes = 0; /* 1 = method, 2 = isa, 3 = recursive isa */
3728 HV *old_stash = NULL;
3730 PERL_ARGS_ASSERT_GLOB_ASSIGN_GLOB;
3732 if (dtype != SVt_PVGV && !isGV_with_GP(dsv)) {
3733 const char * const name = GvNAME(ssv);
3734 const STRLEN len = GvNAMELEN(ssv);
3736 if (dtype >= SVt_PV) {
3742 SvUPGRADE(dsv, SVt_PVGV);
3743 (void)SvOK_off(dsv);
3744 isGV_with_GP_on(dsv);
3746 GvSTASH(dsv) = GvSTASH(ssv);
3748 Perl_sv_add_backref(aTHX_ MUTABLE_SV(GvSTASH(dsv)), dsv);
3749 gv_name_set(MUTABLE_GV(dsv), name, len,
3750 GV_ADD | (GvNAMEUTF8(ssv) ? SVf_UTF8 : 0 ));
3751 SvFAKE_on(dsv); /* can coerce to non-glob */
3754 if(GvGP(MUTABLE_GV(ssv))) {
3755 /* If source has method cache entry, clear it */
3757 SvREFCNT_dec(GvCV(ssv));
3758 GvCV_set(ssv, NULL);
3761 /* If source has a real method, then a method is
3764 GvCV((const GV *)ssv) && GvSTASH(dsv) && HvHasENAME(GvSTASH(dsv))
3770 /* If dest already had a real method, that's a change as well */
3772 !mro_changes && GvGP(MUTABLE_GV(dsv)) && GvCVu((const GV *)dsv)
3773 && GvSTASH(dsv) && HvHasENAME(GvSTASH(dsv))
3778 /* We don't need to check the name of the destination if it was not a
3779 glob to begin with. */
3780 if(dtype == SVt_PVGV) {
3781 const char * const name = GvNAME((const GV *)dsv);
3782 const STRLEN len = GvNAMELEN(dsv);
3783 if(memEQs(name, len, "ISA")
3784 /* The stash may have been detached from the symbol table, so
3786 && GvSTASH(dsv) && HvHasENAME(GvSTASH(dsv))
3790 if ((len > 1 && name[len-2] == ':' && name[len-1] == ':')
3791 || (len == 1 && name[0] == ':')) {
3794 /* Set aside the old stash, so we can reset isa caches on
3796 if((old_stash = GvHV(dsv)))
3797 /* Make sure we do not lose it early. */
3798 SvREFCNT_inc_simple_void_NN(
3799 sv_2mortal((SV *)old_stash)
3804 SvREFCNT_inc_simple_void_NN(sv_2mortal(dsv));
3807 /* freeing dsv's GP might free ssv (e.g. *x = $x),
3808 * so temporarily protect it */
3810 SAVEFREESV(SvREFCNT_inc_simple_NN(ssv));
3811 gp_free(MUTABLE_GV(dsv));
3812 GvINTRO_off(dsv); /* one-shot flag */
3813 GvGP_set(dsv, gp_ref(GvGP(ssv)));
3818 if (GvIMPORTED(dsv) != GVf_IMPORTED
3819 && CopSTASH_ne(PL_curcop, GvSTASH(dsv)))
3824 if(mro_changes == 2) {
3825 if (GvAV((const GV *)ssv)) {
3827 SV * const sref = (SV *)GvAV((const GV *)dsv);
3828 if (SvSMAGICAL(sref) && (mg = mg_find(sref, PERL_MAGIC_isa))) {
3829 if (SvTYPE(mg->mg_obj) != SVt_PVAV) {
3830 AV * const ary = newAV_alloc_x(2);
3831 av_push_simple(ary, mg->mg_obj); /* takes the refcount */
3832 av_push_simple(ary, SvREFCNT_inc_simple_NN(dsv));
3833 mg->mg_obj = (SV *)ary;
3835 av_push((AV *)mg->mg_obj, SvREFCNT_inc_simple_NN(dsv));
3838 else sv_magic(sref, dsv, PERL_MAGIC_isa, NULL, 0);
3840 mro_isa_changed_in(GvSTASH(dsv));
3842 else if(mro_changes == 3) {
3843 HV * const stash = GvHV(dsv);
3844 if(old_stash ? HvHasENAME(old_stash) : cBOOL(stash))
3850 else if(mro_changes) mro_method_changed_in(GvSTASH(dsv));
3851 if (GvIO(dsv) && dtype == SVt_PVGV) {
3852 DEBUG_o(Perl_deb(aTHX_
3853 "glob_assign_glob clearing PL_stashcache\n"));
3854 /* It's a cache. It will rebuild itself quite happily.
3855 It's a lot of effort to work out exactly which key (or keys)
3856 might be invalidated by the creation of the this file handle.
3858 hv_clear(PL_stashcache);
3864 Perl_gv_setref(pTHX_ SV *const dsv, SV *const ssv)
3866 SV * const sref = SvRV(ssv);
3868 const int intro = GvINTRO(dsv);
3871 const U32 stype = SvTYPE(sref);
3873 PERL_ARGS_ASSERT_GV_SETREF;
3876 GvINTRO_off(dsv); /* one-shot flag */
3877 GvLINE(dsv) = CopLINE(PL_curcop);
3878 GvEGV(dsv) = MUTABLE_GV(dsv);
3883 location = (SV **) &(GvGP(dsv)->gp_cv); /* XXX bypassing GvCV_set */
3884 import_flag = GVf_IMPORTED_CV;
3887 location = (SV **) &GvHV(dsv);
3888 import_flag = GVf_IMPORTED_HV;
3891 location = (SV **) &GvAV(dsv);
3892 import_flag = GVf_IMPORTED_AV;
3895 location = (SV **) &GvIOp(dsv);
3898 location = (SV **) &GvFORM(dsv);
3901 location = &GvSV(dsv);
3902 import_flag = GVf_IMPORTED_SV;
3905 if (stype == SVt_PVCV) {
3906 /*if (GvCVGEN(dsv) && (GvCV(dsv) != (const CV *)sref || GvCVGEN(dsv))) {*/
3908 SvREFCNT_dec(GvCV(dsv));
3909 GvCV_set(dsv, NULL);
3910 GvCVGEN(dsv) = 0; /* Switch off cacheness. */
3913 /* SAVEt_GVSLOT takes more room on the savestack and has more
3914 overhead in leave_scope than SAVEt_GENERIC_SV. But for CVs
3915 leave_scope needs access to the GV so it can reset method
3916 caches. We must use SAVEt_GVSLOT whenever the type is
3917 SVt_PVCV, even if the stash is anonymous, as the stash may
3918 gain a name somehow before leave_scope. */
3919 if (stype == SVt_PVCV) {
3920 /* There is no save_pushptrptrptr. Creating it for this
3921 one call site would be overkill. So inline the ss add
3925 SS_ADD_PTR(location);
3926 SS_ADD_PTR(SvREFCNT_inc(*location));
3927 SS_ADD_UV(SAVEt_GVSLOT);
3930 else SAVEGENERICSV(*location);
3933 if (stype == SVt_PVCV && (*location != sref || GvCVGEN(dsv))) {
3934 CV* const cv = MUTABLE_CV(*location);
3936 if (!GvCVGEN((const GV *)dsv) &&
3937 (CvROOT(cv) || CvXSUB(cv)) &&
3938 /* redundant check that avoids creating the extra SV
3939 most of the time: */
3940 (CvCONST(cv) || (ckWARN(WARN_REDEFINE) && !intro)))
3942 SV * const new_const_sv =
3943 CvCONST((const CV *)sref)
3944 ? cv_const_sv_or_av((const CV *)sref)
3946 HV * const stash = GvSTASH((const GV *)dsv);
3947 report_redefined_cv(
3950 ? Perl_newSVpvf(aTHX_
3951 "%" HEKf "::%" HEKf,
3952 HEKfARG(HvNAME_HEK(stash)),
3953 HEKfARG(GvENAME_HEK(MUTABLE_GV(dsv))))
3954 : Perl_newSVpvf(aTHX_
3956 HEKfARG(GvENAME_HEK(MUTABLE_GV(dsv))))
3959 CvCONST((const CV *)sref) ? &new_const_sv : NULL
3963 cv_ckproto_len_flags(cv, (const GV *)dsv,
3964 SvPOK(sref) ? CvPROTO(sref) : NULL,
3965 SvPOK(sref) ? CvPROTOLEN(sref) : 0,
3966 SvPOK(sref) ? SvUTF8(sref) : 0);
3968 GvCVGEN(dsv) = 0; /* Switch off cacheness. */
3970 if(GvSTASH(dsv)) { /* sub foo { 1 } sub bar { 2 } *bar = \&foo */
3971 if (intro && GvREFCNT(dsv) > 1) {
3972 /* temporary remove extra savestack's ref */
3974 gv_method_changed(dsv);
3977 else gv_method_changed(dsv);
3980 *location = SvREFCNT_inc_simple_NN(sref);
3981 if (import_flag && !(GvFLAGS(dsv) & import_flag)
3982 && CopSTASH_ne(PL_curcop, GvSTASH(dsv))) {
3983 GvFLAGS(dsv) |= import_flag;
3986 if (stype == SVt_PVHV) {
3987 const char * const name = GvNAME((GV*)dsv);
3988 const STRLEN len = GvNAMELEN(dsv);
3991 (len > 1 && name[len-2] == ':' && name[len-1] == ':')
3992 || (len == 1 && name[0] == ':')
3994 && (!dref || HvHasENAME(dref))
3997 (HV *)sref, (HV *)dref,
4003 stype == SVt_PVAV && sref != dref
4004 && memEQs(GvNAME((GV*)dsv), GvNAMELEN((GV*)dsv), "ISA")
4005 /* The stash may have been detached from the symbol table, so
4006 check its name before doing anything. */
4007 && GvSTASH(dsv) && HvHasENAME(GvSTASH(dsv))
4010 MAGIC * const omg = dref && SvSMAGICAL(dref)
4011 ? mg_find(dref, PERL_MAGIC_isa)
4013 if (SvSMAGICAL(sref) && (mg = mg_find(sref, PERL_MAGIC_isa))) {
4014 if (SvTYPE(mg->mg_obj) != SVt_PVAV) {
4015 AV * const ary = newAV_alloc_xz(4);
4016 av_push_simple(ary, mg->mg_obj); /* takes the refcount */
4017 mg->mg_obj = (SV *)ary;
4020 if (SvTYPE(omg->mg_obj) == SVt_PVAV) {
4021 SV **svp = AvARRAY((AV *)omg->mg_obj);
4022 I32 items = AvFILLp((AV *)omg->mg_obj) + 1;
4026 SvREFCNT_inc_simple_NN(*svp++)
4032 SvREFCNT_inc_simple_NN(omg->mg_obj)
4036 av_push((AV *)mg->mg_obj,SvREFCNT_inc_simple_NN(dsv));
4042 sref, omg ? omg->mg_obj : dsv, PERL_MAGIC_isa, NULL, 0
4044 for (i = 0; i <= AvFILL(sref); ++i) {
4045 SV **elem = av_fetch ((AV*)sref, i, 0);
4048 *elem, sref, PERL_MAGIC_isaelem, NULL, i
4052 mg = mg_find(sref, PERL_MAGIC_isa);
4054 /* Since the *ISA assignment could have affected more than
4055 one stash, don't call mro_isa_changed_in directly, but let
4056 magic_clearisa do it for us, as it already has the logic for
4057 dealing with globs vs arrays of globs. */
4059 Perl_magic_clearisa(aTHX_ NULL, mg);
4061 else if (stype == SVt_PVIO) {
4062 DEBUG_o(Perl_deb(aTHX_ "gv_setref clearing PL_stashcache\n"));
4063 /* It's a cache. It will rebuild itself quite happily.
4064 It's a lot of effort to work out exactly which key (or keys)
4065 might be invalidated by the creation of the this file handle.
4067 hv_clear(PL_stashcache);
4071 if (!intro) SvREFCNT_dec(dref);
4080 #ifdef PERL_DEBUG_READONLY_COW
4081 # include <sys/mman.h>
4083 # ifndef PERL_MEMORY_DEBUG_HEADER_SIZE
4084 # define PERL_MEMORY_DEBUG_HEADER_SIZE 0
4088 Perl_sv_buf_to_ro(pTHX_ SV *sv)
4090 struct perl_memory_debug_header * const header =
4091 (struct perl_memory_debug_header *)(SvPVX(sv)-PERL_MEMORY_DEBUG_HEADER_SIZE);
4092 const MEM_SIZE len = header->size;
4093 PERL_ARGS_ASSERT_SV_BUF_TO_RO;
4094 # ifdef PERL_TRACK_MEMPOOL
4095 if (!header->readonly) header->readonly = 1;
4097 if (mprotect(header, len, PROT_READ))
4098 Perl_warn(aTHX_ "mprotect RW for COW string %p %lu failed with %d",
4099 header, len, errno);
4103 S_sv_buf_to_rw(pTHX_ SV *sv)
4105 struct perl_memory_debug_header * const header =
4106 (struct perl_memory_debug_header *)(SvPVX(sv)-PERL_MEMORY_DEBUG_HEADER_SIZE);
4107 const MEM_SIZE len = header->size;
4108 PERL_ARGS_ASSERT_SV_BUF_TO_RW;
4109 if (mprotect(header, len, PROT_READ|PROT_WRITE))
4110 Perl_warn(aTHX_ "mprotect for COW string %p %lu failed with %d",
4111 header, len, errno);
4112 # ifdef PERL_TRACK_MEMPOOL
4113 header->readonly = 0;
4118 # define sv_buf_to_ro(sv) NOOP
4119 # define sv_buf_to_rw(sv) NOOP
4123 Perl_sv_setsv_flags(pTHX_ SV *dsv, SV* ssv, const I32 flags)
4128 unsigned int both_type;
4130 PERL_ARGS_ASSERT_SV_SETSV_FLAGS;
4132 if (UNLIKELY( ssv == dsv ))
4135 if (UNLIKELY( !ssv ))
4138 stype = SvTYPE(ssv);
4139 dtype = SvTYPE(dsv);
4140 both_type = (stype | dtype);
4142 /* with these values, we can check that both SVs are NULL/IV (and not
4143 * freed) just by testing the or'ed types */
4144 STATIC_ASSERT_STMT(SVt_NULL == 0);
4145 STATIC_ASSERT_STMT(SVt_IV == 1);
4146 STATIC_ASSERT_STMT(SVt_NV == 2);
4147 #if NVSIZE <= IVSIZE
4148 if (both_type <= 2) {
4150 if (both_type <= 1) {
4152 /* both src and dst are UNDEF/IV/RV - maybe NV depending on config,
4153 * so we can do a lot of special-casing */
4158 /* minimal subset of SV_CHECK_THINKFIRST_COW_DROP(dsv) */
4159 if (SvREADONLY(dsv))
4160 Perl_croak_no_modify();
4163 sv_unref_flags(dsv, 0);
4169 assert(!SvGMAGICAL(ssv));
4170 assert(!SvGMAGICAL(dsv));
4172 sflags = SvFLAGS(ssv);
4173 if (sflags & (SVf_IOK|SVf_ROK)) {
4174 SET_SVANY_FOR_BODYLESS_IV(dsv);
4175 new_dflags = SVt_IV;
4177 if (sflags & SVf_ROK) {
4178 dsv->sv_u.svu_rv = SvREFCNT_inc(SvRV(ssv));
4179 new_dflags |= SVf_ROK;
4182 /* both src and dst are <= SVt_IV, so sv_any points to the
4183 * head; so access the head directly
4185 assert( &(ssv->sv_u.svu_iv)
4186 == &(((XPVIV*) SvANY(ssv))->xiv_iv));
4187 assert( &(dsv->sv_u.svu_iv)
4188 == &(((XPVIV*) SvANY(dsv))->xiv_iv));
4189 dsv->sv_u.svu_iv = ssv->sv_u.svu_iv;
4190 new_dflags |= (SVf_IOK|SVp_IOK|(sflags & SVf_IVisUV));
4193 #if NVSIZE <= IVSIZE
4194 else if (sflags & SVf_NOK) {
4195 SET_SVANY_FOR_BODYLESS_NV(dsv);
4196 new_dflags = (SVt_NV|SVf_NOK|SVp_NOK);
4198 /* both src and dst are <= SVt_MV, so sv_any points to the
4199 * head; so access the head directly
4201 assert( &(ssv->sv_u.svu_nv)
4202 == &(((XPVNV*) SvANY(ssv))->xnv_u.xnv_nv));
4203 assert( &(dsv->sv_u.svu_nv)
4204 == &(((XPVNV*) SvANY(dsv))->xnv_u.xnv_nv));
4205 dsv->sv_u.svu_nv = ssv->sv_u.svu_nv;
4209 new_dflags = dtype; /* turn off everything except the type */
4211 /* Should preserve some dsv flags - at least SVs_TEMP, */
4212 /* so cannot just set SvFLAGS(dsv) = new_dflags */
4213 /* First clear the flags that we do want to clobber */
4214 (void)SvOK_off(dsv);
4215 SvFLAGS(dsv) &= ~SVTYPEMASK;
4216 /* Now set the new flags */
4217 SvFLAGS(dsv) |= new_dflags;
4219 SvREFCNT_dec(old_rv);
4223 if (UNLIKELY(both_type == SVTYPEMASK)) {
4224 if (SvIS_FREED(dsv)) {
4225 Perl_croak(aTHX_ "panic: attempt to copy value %" SVf
4226 " to a freed scalar %p", SVfARG(ssv), (void *)dsv);
4228 if (SvIS_FREED(ssv)) {
4229 Perl_croak(aTHX_ "panic: attempt to copy freed scalar %p to %p",
4230 (void*)ssv, (void*)dsv);
4236 SV_CHECK_THINKFIRST_COW_DROP(dsv);
4237 dtype = SvTYPE(dsv); /* THINKFIRST may have changed type */
4239 /* There's a lot of redundancy below but we're going for speed here
4240 * Note: some of the cases below do return; rather than break; so the
4241 * if-elseif-else logic below this switch does not see all cases. */
4246 if (LIKELY( dtype != SVt_PVGV && dtype != SVt_PVLV )) {
4247 (void)SvOK_off(dsv);
4255 /* For performance, we inline promoting to type SVt_IV. */
4256 /* We're starting from SVt_NULL, so provided that define is
4257 * actual 0, we don't have to unset any SV type flags
4258 * to promote to SVt_IV. */
4259 STATIC_ASSERT_STMT(SVt_NULL == 0);
4260 SET_SVANY_FOR_BODYLESS_IV(dsv);
4261 SvFLAGS(dsv) |= SVt_IV;
4265 sv_upgrade(dsv, SVt_PVIV);
4269 goto end_of_first_switch;
4271 (void)SvIOK_only(dsv);
4272 SvIV_set(dsv, SvIVX(ssv));
4275 /* SvTAINTED can only be true if the SV has taint magic, which in
4276 turn means that the SV type is PVMG (or greater). This is the
4277 case statement for SVt_IV, so this cannot be true (whatever gcov
4279 assert(!SvTAINTED(ssv));
4284 if (dtype < SVt_PV && dtype != SVt_IV)
4285 sv_upgrade(dsv, SVt_IV);
4289 if (LIKELY( SvNOK(ssv) )) {
4293 sv_upgrade(dsv, SVt_NV);
4297 sv_upgrade(dsv, SVt_PVNV);
4301 goto end_of_first_switch;
4303 SvNV_set(dsv, SvNVX(ssv));
4304 (void)SvNOK_only(dsv);
4305 /* SvTAINTED can only be true if the SV has taint magic, which in
4306 turn means that the SV type is PVMG (or greater). This is the
4307 case statement for SVt_NV, so this cannot be true (whatever gcov
4309 assert(!SvTAINTED(ssv));
4316 sv_upgrade(dsv, SVt_PV);
4319 if (dtype < SVt_PVIV)
4320 sv_upgrade(dsv, SVt_PVIV);
4323 if (dtype < SVt_PVNV)
4324 sv_upgrade(dsv, SVt_PVNV);
4328 invlist_clone(ssv, dsv);
4332 const char * const type = sv_reftype(ssv,0);
4334 /* diag_listed_as: Bizarre copy of %s */
4335 Perl_croak(aTHX_ "Bizarre copy of %s in %s", type, OP_DESC(PL_op));
4337 Perl_croak(aTHX_ "Bizarre copy of %s", type);
4339 NOT_REACHED; /* NOTREACHED */
4343 if (dtype < SVt_REGEXP)
4344 sv_upgrade(dsv, SVt_REGEXP);
4350 if (SvGMAGICAL(ssv) && (flags & SV_GMAGIC)) {
4352 if (SvTYPE(ssv) != stype)
4353 stype = SvTYPE(ssv);
4355 if (isGV_with_GP(ssv) && dtype <= SVt_PVLV) {
4356 glob_assign_glob(dsv, ssv, dtype);
4359 if (stype == SVt_PVLV)
4361 if (isREGEXP(ssv)) goto upgregexp;
4362 SvUPGRADE(dsv, SVt_PVNV);
4365 SvUPGRADE(dsv, (svtype)stype);
4367 end_of_first_switch:
4369 /* dsv may have been upgraded. */
4370 dtype = SvTYPE(dsv);
4371 sflags = SvFLAGS(ssv);
4373 if (UNLIKELY( dtype == SVt_PVCV )) {
4374 /* Assigning to a subroutine sets the prototype. */
4377 const char *const ptr = SvPV_const(ssv, len);
4379 SvGROW(dsv, len + 1);
4380 Copy(ptr, SvPVX(dsv), len + 1, char);
4381 SvCUR_set(dsv, len);
4383 SvFLAGS(dsv) |= sflags & SVf_UTF8;
4384 CvAUTOLOAD_off(dsv);
4389 else if (UNLIKELY(dtype == SVt_PVAV || dtype == SVt_PVHV
4390 || dtype == SVt_PVFM))
4392 const char * const type = sv_reftype(dsv,0);
4394 /* diag_listed_as: Cannot copy to %s */
4395 Perl_croak(aTHX_ "Cannot copy to %s in %s", type, OP_DESC(PL_op));
4397 Perl_croak(aTHX_ "Cannot copy to %s", type);
4398 } else if (sflags & SVf_ROK) {
4399 if (isGV_with_GP(dsv)
4400 && SvTYPE(SvRV(ssv)) == SVt_PVGV && isGV_with_GP(SvRV(ssv))) {
4403 if (GvIMPORTED(dsv) != GVf_IMPORTED
4404 && CopSTASH_ne(PL_curcop, GvSTASH(dsv)))
4411 glob_assign_glob(dsv, ssv, dtype);
4415 if (dtype >= SVt_PV) {
4416 if (isGV_with_GP(dsv)) {
4417 gv_setref(dsv, ssv);
4420 if (SvPVX_const(dsv)) {
4426 (void)SvOK_off(dsv);
4427 SvRV_set(dsv, SvREFCNT_inc(SvRV(ssv)));
4428 SvFLAGS(dsv) |= sflags & SVf_ROK;
4429 assert(!(sflags & SVp_NOK));
4430 assert(!(sflags & SVp_IOK));
4431 assert(!(sflags & SVf_NOK));
4432 assert(!(sflags & SVf_IOK));
4434 else if (isGV_with_GP(dsv)) {
4435 if (!(sflags & SVf_OK)) {
4436 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
4437 "Undefined value assigned to typeglob");
4440 GV *gv = gv_fetchsv_nomg(ssv, GV_ADD, SVt_PVGV);
4441 if (dsv != (const SV *)gv) {
4442 const char * const name = GvNAME((const GV *)dsv);
4443 const STRLEN len = GvNAMELEN(dsv);
4444 HV *old_stash = NULL;
4445 bool reset_isa = FALSE;
4446 if ((len > 1 && name[len-2] == ':' && name[len-1] == ':')
4447 || (len == 1 && name[0] == ':')) {
4448 /* Set aside the old stash, so we can reset isa caches
4449 on its subclasses. */
4450 if((old_stash = GvHV(dsv))) {
4451 /* Make sure we do not lose it early. */
4452 SvREFCNT_inc_simple_void_NN(
4453 sv_2mortal((SV *)old_stash)
4460 SvREFCNT_inc_simple_void_NN(sv_2mortal(dsv));
4461 gp_free(MUTABLE_GV(dsv));
4463 GvGP_set(dsv, gp_ref(GvGP(gv)));
4466 HV * const stash = GvHV(dsv);
4468 old_stash ? HvHasENAME(old_stash) : cBOOL(stash)
4478 else if ((dtype == SVt_REGEXP || dtype == SVt_PVLV)
4479 && (stype == SVt_REGEXP || isREGEXP(ssv))) {
4480 reg_temp_copy((REGEXP*)dsv, (REGEXP*)ssv);
4482 else if (sflags & SVp_POK) {
4483 const STRLEN cur = SvCUR(ssv);
4484 const STRLEN len = SvLEN(ssv);
4487 * We have three basic ways to copy the string:
4493 * Which we choose is based on various factors. The following
4494 * things are listed in order of speed, fastest to slowest:
4496 * - Copying a short string
4497 * - Copy-on-write bookkeeping
4499 * - Copying a long string
4501 * We swipe the string (steal the string buffer) if the SV on the
4502 * rhs is about to be freed anyway (TEMP and refcnt==1). This is a
4503 * big win on long strings. It should be a win on short strings if
4504 * SvPVX_const(dsv) has to be allocated. If not, it should not
4505 * slow things down, as SvPVX_const(ssv) would have been freed
4508 * We also steal the buffer from a PADTMP (operator target) if it
4509 * is ‘long enough’. For short strings, a swipe does not help
4510 * here, as it causes more malloc calls the next time the target
4511 * is used. Benchmarks show that even if SvPVX_const(dsv) has to
4512 * be allocated it is still not worth swiping PADTMPs for short
4513 * strings, as the savings here are small.
4515 * If swiping is not an option, then we see whether it is worth using
4516 * copy-on-write. If the lhs already has a buffer big enough and the
4517 * string is short, we skip it and fall back to method 3, since memcpy
4518 * is faster for short strings than the later bookkeeping overhead that
4519 * copy-on-write entails.
4521 * If the rhs is not a copy-on-write string yet, then we also
4522 * consider whether the buffer is too large relative to the string
4523 * it holds. Some operations such as readline allocate a large
4524 * buffer in the expectation of reusing it. But turning such into
4525 * a COW buffer is counter-productive because it increases memory
4526 * usage by making readline allocate a new large buffer the sec-
4527 * ond time round. So, if the buffer is too large, again, we use
4530 * Finally, if there is no buffer on the left, or the buffer is too
4531 * small, then we use copy-on-write and make both SVs share the
4536 /* Whichever path we take through the next code, we want this true,
4537 and doing it now facilitates the COW check. */
4538 (void)SvPOK_only(dsv);
4542 /* slated for free anyway (and not COW)? */
4543 (sflags & (SVs_TEMP|SVf_IsCOW)) == SVs_TEMP
4544 /* or a swipable TARG */
4546 (SVs_PADTMP|SVf_READONLY|SVf_PROTECT|SVf_IsCOW))
4548 /* whose buffer is worth stealing */
4549 && CHECK_COWBUF_THRESHOLD(cur,len)
4552 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
4553 (!(flags & SV_NOSTEAL)) &&
4554 /* and we're allowed to steal temps */
4555 SvREFCNT(ssv) == 1 && /* and no other references to it? */
4556 len) /* and really is a string */
4557 { /* Passes the swipe test. */
4558 if (SvPVX_const(dsv)) /* we know that dtype >= SVt_PV */
4560 SvPV_set(dsv, SvPVX_mutable(ssv));
4561 SvLEN_set(dsv, SvLEN(ssv));
4562 SvCUR_set(dsv, SvCUR(ssv));
4565 (void)SvOK_off(ssv); /* NOTE: nukes most SvFLAGS on ssv */
4566 SvPV_set(ssv, NULL);
4571 /* We must check for SvIsCOW_static() even without
4572 * SV_COW_SHARED_HASH_KEYS being set or else we'll break SvIsBOOL()
4574 else if (SvIsCOW_static(ssv)) {
4575 if (SvPVX_const(dsv)) { /* we know that dtype >= SVt_PV */
4578 SvPV_set(dsv, SvPVX(ssv));
4580 SvCUR_set(dsv, cur);
4581 SvFLAGS(dsv) |= (SVf_IsCOW|SVppv_STATIC);
4583 else if (flags & SV_COW_SHARED_HASH_KEYS
4585 #ifdef PERL_COPY_ON_WRITE
4588 ( (CHECK_COWBUF_THRESHOLD(cur,len) || SvLEN(dsv) < cur+1)
4589 /* If this is a regular (non-hek) COW, only so
4590 many COW "copies" are possible. */
4591 && CowREFCNT(ssv) != SV_COW_REFCNT_MAX ))
4592 : ( (sflags & CAN_COW_MASK) == CAN_COW_FLAGS
4593 && !(SvFLAGS(dsv) & SVf_BREAK)
4594 && CHECK_COW_THRESHOLD(cur,len) && cur+1 < len
4595 && (CHECK_COWBUF_THRESHOLD(cur,len) || SvLEN(dsv) < cur+1)
4599 && !(SvFLAGS(dsv) & SVf_BREAK)
4602 /* Either it's a shared hash key, or it's suitable for
4606 PerlIO_printf(Perl_debug_log, "Copy on write: ssv --> dsv\n");
4612 if (!(sflags & SVf_IsCOW)) {
4617 if (SvPVX_const(dsv)) { /* we know that dtype >= SVt_PV */
4623 if (sflags & SVf_IsCOW) {
4627 SvPV_set(dsv, SvPVX_mutable(ssv));
4632 /* SvIsCOW_shared_hash */
4633 DEBUG_C(PerlIO_printf(Perl_debug_log,
4634 "Copy on write: Sharing hash\n"));
4636 assert (SvTYPE(dsv) >= SVt_PV);
4638 HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(ssv)))));
4640 SvLEN_set(dsv, len);
4641 SvCUR_set(dsv, cur);
4644 /* Failed the swipe test, and we cannot do copy-on-write either.
4645 Have to copy the string. */
4646 SvGROW(dsv, cur + 1); /* inlined from sv_setpvn */
4647 Move(SvPVX_const(ssv),SvPVX(dsv),cur,char);
4648 SvCUR_set(dsv, cur);
4651 if (sflags & SVp_NOK) {
4652 SvNV_set(dsv, SvNVX(ssv));
4653 if ((sflags & SVf_NOK) && !(sflags & SVf_POK)) {
4654 /* Source was SVf_NOK|SVp_NOK|SVp_POK but not SVf_POK, meaning
4655 a value set as floating point and later stringified, where
4656 the value happens to be one of the few that we know aren't
4657 affected by the numeric locale, hence we can cache the
4658 stringification. Currently that's +Inf, -Inf and NaN, but
4659 conceivably we might extend this to -9 .. +9 (excluding -0).
4660 So mark destination the same: */
4661 SvFLAGS(dsv) &= ~SVf_POK;
4664 if (sflags & SVp_IOK) {
4665 SvIV_set(dsv, SvIVX(ssv));
4666 if (sflags & SVf_IVisUV)
4668 if ((sflags & SVf_IOK) && !(sflags & SVf_POK)) {
4669 /* Source was SVf_IOK|SVp_IOK|SVp_POK but not SVf_POK, meaning
4670 a value set as an integer and later stringified. So mark
4671 destination the same: */
4672 SvFLAGS(dsv) &= ~SVf_POK;
4675 SvFLAGS(dsv) |= sflags & (SVf_IOK|SVp_IOK|SVf_NOK|SVp_NOK|SVf_UTF8);
4677 const MAGIC * const smg = SvVSTRING_mg(ssv);
4679 sv_magic(dsv, NULL, PERL_MAGIC_vstring,
4680 smg->mg_ptr, smg->mg_len);
4685 else if (sflags & (SVp_IOK|SVp_NOK)) {
4686 (void)SvOK_off(dsv);
4687 SvFLAGS(dsv) |= sflags & (SVf_IOK|SVp_IOK|SVf_IVisUV|SVf_NOK|SVp_NOK);
4688 if (sflags & SVp_IOK) {
4689 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
4690 SvIV_set(dsv, SvIVX(ssv));
4692 if (sflags & SVp_NOK) {
4693 SvNV_set(dsv, SvNVX(ssv));
4697 if (isGV_with_GP(ssv)) {
4698 gv_efullname3(dsv, MUTABLE_GV(ssv), "*");
4701 (void)SvOK_off(dsv);
4709 =for apidoc sv_set_undef
4711 Equivalent to C<sv_setsv(sv, &PL_sv_undef)>, but more efficient.
4712 Doesn't handle set magic.
4714 The perl equivalent is C<$sv = undef;>. Note that it doesn't free any string
4715 buffer, unlike C<undef $sv>.
4717 Introduced in perl 5.25.12.
4723 Perl_sv_set_undef(pTHX_ SV *sv)
4725 U32 type = SvTYPE(sv);
4727 PERL_ARGS_ASSERT_SV_SET_UNDEF;
4729 /* shortcut, NULL, IV, RV */
4731 if (type <= SVt_IV) {
4732 assert(!SvGMAGICAL(sv));
4733 if (SvREADONLY(sv)) {
4734 /* does undeffing PL_sv_undef count as modifying a read-only
4735 * variable? Some XS code does this */
4736 if (sv == &PL_sv_undef)
4738 Perl_croak_no_modify();
4743 sv_unref_flags(sv, 0);
4746 SvFLAGS(sv) = type; /* quickly turn off all flags */
4747 SvREFCNT_dec_NN(rv);
4751 SvFLAGS(sv) = type; /* quickly turn off all flags */
4756 Perl_croak(aTHX_ "panic: attempt to undefine a freed scalar %p",
4759 SV_CHECK_THINKFIRST_COW_DROP(sv);
4761 if (isGV_with_GP(sv))
4762 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
4763 "Undefined value assigned to typeglob");
4769 =for apidoc sv_set_true
4771 Equivalent to C<sv_setsv(sv, &PL_sv_yes)>, but may be made more
4772 efficient in the future. Doesn't handle set magic.
4774 The perl equivalent is C<$sv = !0;>.
4776 Introduced in perl 5.35.11.
4782 Perl_sv_set_true(pTHX_ SV *sv)
4784 PERL_ARGS_ASSERT_SV_SET_TRUE;
4785 sv_setsv(sv, &PL_sv_yes);
4789 =for apidoc sv_set_false
4791 Equivalent to C<sv_setsv(sv, &PL_sv_no)>, but may be made more
4792 efficient in the future. Doesn't handle set magic.
4794 The perl equivalent is C<$sv = !1;>.
4796 Introduced in perl 5.35.11.
4802 Perl_sv_set_false(pTHX_ SV *sv)
4804 PERL_ARGS_ASSERT_SV_SET_FALSE;
4805 sv_setsv(sv, &PL_sv_no);
4809 =for apidoc sv_set_bool
4811 Equivalent to C<sv_setsv(sv, bool_val ? &Pl_sv_yes : &PL_sv_no)>, but
4812 may be made more efficient in the future. Doesn't handle set magic.
4814 The perl equivalent is C<$sv = !!$expr;>.
4816 Introduced in perl 5.35.11.
4822 Perl_sv_set_bool(pTHX_ SV *sv, const bool bool_val)
4824 PERL_ARGS_ASSERT_SV_SET_BOOL;
4825 sv_setsv(sv, bool_val ? &PL_sv_yes : &PL_sv_no);
4830 Perl_sv_setsv_mg(pTHX_ SV *const dsv, SV *const ssv)
4832 PERL_ARGS_ASSERT_SV_SETSV_MG;
4839 # define SVt_COW SVt_PV
4841 Perl_sv_setsv_cow(pTHX_ SV *dsv, SV *ssv)
4843 STRLEN cur = SvCUR(ssv);
4844 STRLEN len = SvLEN(ssv);
4846 U32 new_flags = (SVt_COW|SVf_POK|SVp_POK|SVf_IsCOW);
4847 #if defined(PERL_DEBUG_READONLY_COW) && defined(PERL_COPY_ON_WRITE)
4848 const bool already = cBOOL(SvIsCOW(ssv));
4851 PERL_ARGS_ASSERT_SV_SETSV_COW;
4854 PerlIO_printf(Perl_debug_log, "Fast copy on write: %p -> %p\n",
4855 (void*)ssv, (void*)dsv);
4862 if (SvTHINKFIRST(dsv))
4863 sv_force_normal_flags(dsv, SV_COW_DROP_PV);
4864 else if (SvPVX_const(dsv))
4865 Safefree(SvPVX_mutable(dsv));
4866 SvUPGRADE(dsv, SVt_COW);
4869 dsv = newSV_type(SVt_COW);
4871 assert (SvPOK(ssv));
4872 assert (SvPOKp(ssv));
4875 if (SvIsCOW_shared_hash(ssv)) {
4876 /* source is a COW shared hash key. */
4877 DEBUG_C(PerlIO_printf(Perl_debug_log,
4878 "Fast copy on write: Sharing hash\n"));
4879 new_pv = HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(ssv))));
4882 else if (SvIsCOW_static(ssv)) {
4883 /* source is static constant; preserve this */
4884 new_pv = SvPVX(ssv);
4885 new_flags |= SVppv_STATIC;
4888 assert(SvCUR(ssv)+1 < SvLEN(ssv));
4889 assert(CowREFCNT(ssv) < SV_COW_REFCNT_MAX);
4891 assert ((SvFLAGS(ssv) & CAN_COW_MASK) == CAN_COW_FLAGS);
4892 SvUPGRADE(ssv, SVt_COW);
4894 DEBUG_C(PerlIO_printf(Perl_debug_log,
4895 "Fast copy on write: Converting ssv to COW\n"));
4898 # ifdef PERL_DEBUG_READONLY_COW
4899 if (already) sv_buf_to_rw(ssv);
4902 new_pv = SvPVX_mutable(ssv);
4906 SvPV_set(dsv, new_pv);
4907 SvFLAGS(dsv) = new_flags;
4910 SvLEN_set(dsv, len);
4911 SvCUR_set(dsv, cur);
4921 =for apidoc sv_setpv_bufsize
4923 Sets the SV to be a string of cur bytes length, with at least
4924 len bytes available. Ensures that there is a null byte at SvEND.
4925 Returns a char * pointer to the SvPV buffer.
4931 Perl_sv_setpv_bufsize(pTHX_ SV *const sv, const STRLEN cur, const STRLEN len)
4935 PERL_ARGS_ASSERT_SV_SETPV_BUFSIZE;
4937 SV_CHECK_THINKFIRST_COW_DROP(sv);
4938 SvUPGRADE(sv, SVt_PV);
4939 pv = SvGROW(sv, len + 1);
4942 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4945 if (SvTYPE(sv) == SVt_PVCV) CvAUTOLOAD_off(sv);
4950 =for apidoc sv_setpv
4951 =for apidoc_item sv_setpv_mg
4952 =for apidoc_item sv_setpvn
4953 =for apidoc_item sv_setpvn_fresh
4954 =for apidoc_item sv_setpvn_mg
4955 =for apidoc_item |void|sv_setpvs|SV* sv|"literal string"
4956 =for apidoc_item |void|sv_setpvs_mg|SV* sv|"literal string"
4958 These copy a string into the SV C<sv>, making sure it is C<L</SvPOK_only>>.
4960 In the C<pvs> forms, the string must be a C literal string, enclosed in double
4963 In the C<pvn> forms, the first byte of the string is pointed to by C<ptr>, and
4964 C<len> indicates the number of bytes to be copied, potentially including
4965 embedded C<NUL> characters.
4967 In the plain C<pv> forms, C<ptr> points to a NUL-terminated C string. That is,
4968 it points to the first byte of the string, and the copy proceeds up through the
4969 first encountered C<NUL> byte.
4971 In the forms that take a C<ptr> argument, if it is NULL, the SV will become
4974 The UTF-8 flag is not changed by these functions. A terminating NUL byte is
4975 guaranteed in the result.
4977 The C<_mg> forms handle 'set' magic; the other forms skip all magic.
4979 C<sv_setpvn_fresh> is a cut-down alternative to C<sv_setpvn>, intended ONLY
4980 to be used with a fresh sv that has been upgraded to a SVt_PV, SVt_PVIV,
4981 SVt_PVNV, or SVt_PVMG.
4987 Perl_sv_setpvn(pTHX_ SV *const sv, const char *const ptr, const STRLEN len)
4991 PERL_ARGS_ASSERT_SV_SETPVN;
4993 SV_CHECK_THINKFIRST_COW_DROP(sv);
4994 if (isGV_with_GP(sv))
4995 Perl_croak_no_modify();
5001 /* len is STRLEN which is unsigned, need to copy to signed */
5004 Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen %"
5007 SvUPGRADE(sv, SVt_PV);
5009 dptr = SvGROW(sv, len + 1);
5010 Move(ptr,dptr,len,char);
5013 (void)SvPOK_only_UTF8(sv); /* validate pointer */
5015 if (SvTYPE(sv) == SVt_PVCV) CvAUTOLOAD_off(sv);
5019 Perl_sv_setpvn_mg(pTHX_ SV *const sv, const char *const ptr, const STRLEN len)
5021 PERL_ARGS_ASSERT_SV_SETPVN_MG;
5023 sv_setpvn(sv,ptr,len);
5028 Perl_sv_setpvn_fresh(pTHX_ SV *const sv, const char *const ptr, const STRLEN len)
5032 PERL_ARGS_ASSERT_SV_SETPVN_FRESH;
5033 assert(SvTYPE(sv) >= SVt_PV && SvTYPE(sv) <= SVt_PVMG);
5034 assert(!SvTHINKFIRST(sv));
5035 assert(!isGV_with_GP(sv));
5039 /* len is STRLEN which is unsigned, need to copy to signed */
5041 Perl_croak(aTHX_ "panic: sv_setpvn_fresh called with negative strlen %"
5044 dptr = sv_grow_fresh(sv, len + 1);
5045 Move(ptr,dptr,len,char);
5054 Perl_sv_setpv(pTHX_ SV *const sv, const char *const ptr)
5058 PERL_ARGS_ASSERT_SV_SETPV;
5060 SV_CHECK_THINKFIRST_COW_DROP(sv);
5066 SvUPGRADE(sv, SVt_PV);
5068 SvGROW(sv, len + 1);
5069 Move(ptr,SvPVX(sv),len+1,char);
5071 (void)SvPOK_only_UTF8(sv); /* validate pointer */
5073 if (SvTYPE(sv) == SVt_PVCV) CvAUTOLOAD_off(sv);
5077 Perl_sv_setpv_mg(pTHX_ SV *const sv, const char *const ptr)
5079 PERL_ARGS_ASSERT_SV_SETPV_MG;
5086 Perl_sv_sethek(pTHX_ SV *const sv, const HEK *const hek)
5088 PERL_ARGS_ASSERT_SV_SETHEK;
5094 if (HEK_LEN(hek) == HEf_SVKEY) {
5095 sv_setsv(sv, *(SV**)HEK_KEY(hek));
5098 const int flags = HEK_FLAGS(hek);
5099 if (flags & HVhek_WASUTF8) {
5100 STRLEN utf8_len = HEK_LEN(hek);
5101 char *as_utf8 = (char *)bytes_to_utf8((U8*)HEK_KEY(hek), &utf8_len);
5102 sv_usepvn_flags(sv, as_utf8, utf8_len, SV_HAS_TRAILING_NUL);
5105 } else if (flags & HVhek_NOTSHARED) {
5106 sv_setpvn(sv, HEK_KEY(hek), HEK_LEN(hek));
5109 else SvUTF8_off(sv);
5113 SV_CHECK_THINKFIRST_COW_DROP(sv);
5114 SvUPGRADE(sv, SVt_PV);
5116 SvPV_set(sv,(char *)HEK_KEY(share_hek_hek(hek)));
5117 SvCUR_set(sv, HEK_LEN(hek));
5123 else SvUTF8_off(sv);
5131 =for apidoc sv_usepvn
5132 =for apidoc_item sv_usepvn_flags
5133 =for apidoc_item sv_usepvn_mg
5135 These tell an SV to use C<ptr> for its string value. Normally SVs have
5136 their string stored inside the SV, but these tell the SV to use an
5137 external string instead.
5139 C<ptr> should point to memory that was allocated
5140 by L</C<Newx>>. It must be
5141 the start of a C<Newx>-ed block of memory, and not a pointer to the
5142 middle of it (beware of L<C<OOK>|perlguts/Offsets> and copy-on-write),
5143 and not be from a non-C<Newx> memory allocator like C<malloc>. The
5144 string length, C<len>, must be supplied. By default this function
5145 will L</C<Renew>> (i.e. realloc, move) the memory pointed to by C<ptr>,
5146 so that the pointer should not be freed or used by the programmer after giving
5147 it to C<sv_usepvn>, and neither should any pointers from "behind" that pointer
5148 (I<e.g.>, S<C<ptr> + 1>) be used.
5150 In the C<sv_usepvn_flags> form, if S<C<flags & SV_SMAGIC>> is true,
5151 C<SvSETMAGIC> is called before returning.
5152 And if S<C<flags & SV_HAS_TRAILING_NUL>> is true, then C<ptr[len]> must be
5153 C<NUL>, and the realloc will be skipped (I<i.e.>, the buffer is actually at
5154 least 1 byte longer than C<len>, and already meets the requirements for storing
5157 C<sv_usepvn> is merely C<sv_usepvn_flags> with C<flags> set to 0, so 'set'
5160 C<sv_usepvn_mg> is merely C<sv_usepvn_flags> with C<flags> set to C<SV_SMAGIC>,
5161 so 'set' magic is performed.
5163 =for apidoc Amnh||SV_SMAGIC
5164 =for apidoc Amnh||SV_HAS_TRAILING_NUL
5170 Perl_sv_usepvn_flags(pTHX_ SV *const sv, char *ptr, const STRLEN len, const U32 flags)
5174 PERL_ARGS_ASSERT_SV_USEPVN_FLAGS;
5176 SV_CHECK_THINKFIRST_COW_DROP(sv);
5177 SvUPGRADE(sv, SVt_PV);
5180 if (flags & SV_SMAGIC)
5184 if (SvPVX_const(sv))
5188 if (flags & SV_HAS_TRAILING_NUL)
5189 assert(ptr[len] == '\0');
5192 allocate = (flags & SV_HAS_TRAILING_NUL)
5194 #ifdef Perl_safesysmalloc_size
5197 PERL_STRLEN_ROUNDUP(len + 1);
5199 if (flags & SV_HAS_TRAILING_NUL) {
5200 /* It's long enough - do nothing.
5201 Specifically Perl_newCONSTSUB is relying on this. */
5204 /* Force a move to shake out bugs in callers. */
5205 char *new_ptr = (char*)safemalloc(allocate);
5206 Copy(ptr, new_ptr, len, char);
5207 PoisonFree(ptr,len,char);
5211 ptr = (char*) saferealloc (ptr, allocate);
5214 #ifdef Perl_safesysmalloc_size
5215 SvLEN_set(sv, Perl_safesysmalloc_size(ptr));
5217 SvLEN_set(sv, allocate);
5221 if (!(flags & SV_HAS_TRAILING_NUL)) {
5224 (void)SvPOK_only_UTF8(sv); /* validate pointer */
5226 if (flags & SV_SMAGIC)
5232 S_sv_uncow(pTHX_ SV * const sv, const U32 flags)
5234 assert(SvIsCOW(sv));
5237 const char * const pvx = SvPVX_const(sv);
5238 const STRLEN len = SvLEN(sv);
5239 const STRLEN cur = SvCUR(sv);
5240 const bool was_shared_hek = SvIsCOW_shared_hash(sv);
5244 PerlIO_printf(Perl_debug_log,
5245 "Copy on write: Force normal %ld\n",
5251 # ifdef PERL_COPY_ON_WRITE
5253 /* Must do this first, since the CowREFCNT uses SvPVX and
5254 we need to write to CowREFCNT, or de-RO the whole buffer if we are
5255 the only owner left of the buffer. */
5256 sv_buf_to_rw(sv); /* NOOP if RO-ing not supported */
5258 U8 cowrefcnt = CowREFCNT(sv);
5259 if(cowrefcnt != 0) {
5261 CowREFCNT(sv) = cowrefcnt;
5266 /* Else we are the only owner of the buffer. */
5271 /* This SV doesn't own the buffer, so need to Newx() a new one: */
5276 if (flags & SV_COW_DROP_PV) {
5277 /* OK, so we don't need to copy our buffer. */
5280 SvGROW(sv, cur + 1);
5281 Move(pvx,SvPVX(sv),cur,char);
5285 if (was_shared_hek) {
5286 unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
5294 const char * const pvx = SvPVX_const(sv);
5295 const STRLEN len = SvCUR(sv);
5299 if (flags & SV_COW_DROP_PV) {
5300 /* OK, so we don't need to copy our buffer. */
5303 SvGROW(sv, len + 1);
5304 Move(pvx,SvPVX(sv),len,char);
5307 unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
5314 =for apidoc sv_force_normal_flags
5316 Undo various types of fakery on an SV, where fakery means
5317 "more than" a string: if the PV is a shared string, make
5318 a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
5319 an C<xpvmg>; if we're a copy-on-write scalar, this is the on-write time when
5320 we do the copy, and is also used locally; if this is a
5321 vstring, drop the vstring magic. If C<SV_COW_DROP_PV> is set
5322 then a copy-on-write scalar drops its PV buffer (if any) and becomes
5323 C<SvPOK_off> rather than making a copy. (Used where this
5324 scalar is about to be set to some other value.) In addition,
5325 the C<flags> parameter gets passed to C<sv_unref_flags()>
5326 when unreffing. C<sv_force_normal> calls this function
5327 with flags set to 0.
5329 This function is expected to be used to signal to perl that this SV is
5330 about to be written to, and any extra book-keeping needs to be taken care
5331 of. Hence, it croaks on read-only values.
5333 =for apidoc Amnh||SV_COW_DROP_PV
5339 Perl_sv_force_normal_flags(pTHX_ SV *const sv, const U32 flags)
5341 PERL_ARGS_ASSERT_SV_FORCE_NORMAL_FLAGS;
5344 Perl_croak_no_modify();
5345 else if (SvIsCOW(sv) && LIKELY(SvTYPE(sv) != SVt_PVHV))
5346 S_sv_uncow(aTHX_ sv, flags);
5348 sv_unref_flags(sv, flags);
5349 else if (SvFAKE(sv) && isGV_with_GP(sv))
5350 sv_unglob(sv, flags);
5351 else if (SvFAKE(sv) && isREGEXP(sv)) {
5352 /* Need to downgrade the REGEXP to a simple(r) scalar. This is analogous
5353 to sv_unglob. We only need it here, so inline it. */
5354 const bool islv = SvTYPE(sv) == SVt_PVLV;
5355 const svtype new_type =
5356 islv ? SVt_NULL : SvMAGIC(sv) || SvSTASH(sv) ? SVt_PVMG : SVt_PV;
5357 SV *const temp = newSV_type(new_type);
5358 regexp *old_rx_body;
5360 if (new_type == SVt_PVMG) {
5361 SvMAGIC_set(temp, SvMAGIC(sv));
5362 SvMAGIC_set(sv, NULL);
5363 SvSTASH_set(temp, SvSTASH(sv));
5364 SvSTASH_set(sv, NULL);
5367 SvCUR_set(temp, SvCUR(sv));
5368 /* Remember that SvPVX is in the head, not the body. */
5369 assert(ReANY((REGEXP *)sv)->mother_re);
5372 /* LV-as-regex has sv->sv_any pointing to an XPVLV body,
5373 * whose xpvlenu_rx field points to the regex body */
5374 XPV *xpv = (XPV*)(SvANY(sv));
5375 old_rx_body = xpv->xpv_len_u.xpvlenu_rx;
5376 xpv->xpv_len_u.xpvlenu_rx = NULL;
5379 old_rx_body = ReANY((REGEXP *)sv);
5381 /* Their buffer is already owned by someone else. */
5382 if (flags & SV_COW_DROP_PV) {
5383 /* SvLEN is already 0. For SVt_REGEXP, we have a brand new
5384 zeroed body. For SVt_PVLV, we zeroed it above (len field
5385 a union with xpvlenu_rx) */
5386 assert(!SvLEN(islv ? sv : temp));
5387 sv->sv_u.svu_pv = 0;
5390 sv->sv_u.svu_pv = savepvn(RX_WRAPPED((REGEXP *)sv), SvCUR(sv));
5391 SvLEN_set(islv ? sv : temp, SvCUR(sv)+1);
5395 /* Now swap the rest of the bodies. */
5399 SvFLAGS(sv) &= ~SVTYPEMASK;
5400 SvFLAGS(sv) |= new_type;
5401 SvANY(sv) = SvANY(temp);
5404 SvFLAGS(temp) &= ~(SVTYPEMASK);
5405 SvFLAGS(temp) |= SVt_REGEXP|SVf_FAKE;
5406 SvANY(temp) = old_rx_body;
5408 /* temp is now rebuilt as a correctly structured SVt_REGEXP, so this
5409 * will trigger a call to sv_clear() which will correctly free the
5411 SvREFCNT_dec_NN(temp);
5413 else if (SvVOK(sv)) sv_unmagic(sv, PERL_MAGIC_vstring);
5419 Efficient removal of characters from the beginning of the string buffer.
5420 C<SvPOK(sv)>, or at least C<SvPOKp(sv)>, must be true and C<ptr> must be a
5421 pointer to somewhere inside the string buffer. C<ptr> becomes the first
5422 character of the adjusted string. Uses the C<OOK> hack. On return, only
5423 C<SvPOK(sv)> and C<SvPOKp(sv)> among the C<OK> flags will be true.
5425 Beware: after this function returns, C<ptr> and SvPVX_const(sv) may no longer
5426 refer to the same chunk of data.
5428 The unfortunate similarity of this function's name to that of Perl's C<chop>
5429 operator is strictly coincidental. This function works from the left;
5430 C<chop> works from the right.
5436 Perl_sv_chop(pTHX_ SV *const sv, const char *const ptr)
5447 PERL_ARGS_ASSERT_SV_CHOP;
5449 if (!ptr || !SvPOKp(sv))
5451 delta = ptr - SvPVX_const(sv);
5453 /* Nothing to do. */
5456 max_delta = SvLEN(sv) ? SvLEN(sv) : SvCUR(sv);
5457 if (delta > max_delta)
5458 Perl_croak(aTHX_ "panic: sv_chop ptr=%p, start=%p, end=%p",
5459 ptr, SvPVX_const(sv), SvPVX_const(sv) + max_delta);
5460 /* SvPVX(sv) may move in SV_CHECK_THINKFIRST(sv), so don't use ptr any more */
5461 SV_CHECK_THINKFIRST(sv);
5462 SvPOK_only_UTF8(sv);
5465 if (!SvLEN(sv)) { /* make copy of shared string */
5466 const char *pvx = SvPVX_const(sv);
5467 const STRLEN len = SvCUR(sv);
5468 SvGROW(sv, len + 1);
5469 Move(pvx,SvPVX(sv),len,char);
5475 SvOOK_offset(sv, old_delta);
5477 SvLEN_set(sv, SvLEN(sv) - delta);
5478 SvCUR_set(sv, SvCUR(sv) - delta);
5479 SvPV_set(sv, SvPVX(sv) + delta);
5481 p = (U8 *)SvPVX_const(sv);
5484 /* how many bytes were evacuated? we will fill them with sentinel
5485 bytes, except for the part holding the new offset of course. */
5488 evacn += (old_delta < 0x100 ? 1 : 1 + sizeof(STRLEN));
5490 assert(evacn <= delta + old_delta);
5494 /* This sets 'delta' to the accumulated value of all deltas so far */
5498 /* If 'delta' fits in a byte, store it just prior to the new beginning of
5499 * the string; otherwise store a 0 byte there and store 'delta' just prior
5500 * to that, using as many bytes as a STRLEN occupies. Thus it overwrites a
5501 * portion of the chopped part of the string */
5502 if (delta < 0x100) {
5506 p -= sizeof(STRLEN);
5507 Copy((U8*)&delta, p, sizeof(STRLEN), U8);
5511 /* Fill the preceding buffer with sentinals to verify that no-one is
5521 =for apidoc sv_catpvn
5522 =for apidoc_item sv_catpvn_flags
5523 =for apidoc_item sv_catpvn_mg
5524 =for apidoc_item sv_catpvn_nomg
5526 These concatenate the C<len> bytes of the string beginning at C<ptr> onto the
5527 end of the string which is in C<dsv>. The caller must make sure C<ptr>
5528 contains at least C<len> bytes.
5530 For all but C<sv_catpvn_flags>, the string appended is assumed to be valid
5531 UTF-8 if the SV has the UTF-8 status set, and a string of bytes otherwise.
5533 They differ in that:
5535 C<sv_catpvn_mg> performs both 'get' and 'set' magic on C<dsv>.
5537 C<sv_catpvn> performs only 'get' magic.
5539 C<sv_catpvn_nomg> skips all magic.
5541 C<sv_catpvn_flags> has an extra C<flags> parameter which allows you to specify
5542 any combination of magic handling (using C<SV_GMAGIC> and/or C<SV_SMAGIC>) and
5543 to also override the UTF-8 handling. By supplying the C<SV_CATBYTES> flag, the
5544 appended string is interpreted as plain bytes; by supplying instead the
5545 C<SV_CATUTF8> flag, it will be interpreted as UTF-8, and the C<dsv> will be
5546 upgraded to UTF-8 if necessary.
5548 C<sv_catpvn>, C<sv_catpvn_mg>, and C<sv_catpvn_nomg> are implemented
5549 in terms of C<sv_catpvn_flags>.
5551 =for apidoc Amnh||SV_CATUTF8
5552 =for apidoc Amnh||SV_CATBYTES
5558 Perl_sv_catpvn_flags(pTHX_ SV *const dsv, const char *sstr, const STRLEN slen, const I32 flags)
5561 const char * const dstr = SvPV_force_flags(dsv, dlen, flags);
5563 PERL_ARGS_ASSERT_SV_CATPVN_FLAGS;
5564 assert((flags & (SV_CATBYTES|SV_CATUTF8)) != (SV_CATBYTES|SV_CATUTF8));
5566 if (!(flags & SV_CATBYTES) || !SvUTF8(dsv)) {
5567 if (flags & SV_CATUTF8 && !SvUTF8(dsv)) {
5568 sv_utf8_upgrade_flags_grow(dsv, 0, slen + 1);
5571 else SvGROW(dsv, dlen + slen + 3);
5573 sstr = SvPVX_const(dsv);
5574 Move(sstr, SvPVX(dsv) + dlen, slen, char);
5575 SvCUR_set(dsv, SvCUR(dsv) + slen);
5578 /* We inline bytes_to_utf8, to avoid an extra malloc. */
5579 const char * const send = sstr + slen;
5582 /* Something this code does not account for, which I think is
5583 impossible; it would require the same pv to be treated as
5584 bytes *and* utf8, which would indicate a bug elsewhere. */
5585 assert(sstr != dstr);
5587 SvGROW(dsv, dlen + slen * 2 + 3);
5588 d = (U8 *)SvPVX(dsv) + dlen;
5590 while (sstr < send) {
5591 append_utf8_from_native_byte(*sstr, &d);
5594 SvCUR_set(dsv, d-(const U8 *)SvPVX(dsv));
5597 (void)SvPOK_only_UTF8(dsv); /* validate pointer */
5599 if (flags & SV_SMAGIC)
5604 =for apidoc sv_catsv
5605 =for apidoc_item sv_catsv_flags
5606 =for apidoc_item sv_catsv_mg
5607 =for apidoc_item sv_catsv_nomg
5609 These concatenate the string from SV C<sstr> onto the end of the string in SV
5610 C<dsv>. If C<sstr> is null, these are no-ops; otherwise only C<dsv> is
5613 They differ only in what magic they perform:
5615 C<sv_catsv_mg> performs 'get' magic on both SVs before the copy, and 'set' magic
5616 on C<dsv> afterwards.
5618 C<sv_catsv> performs just 'get' magic, on both SVs.
5620 C<sv_catsv_nomg> skips all magic.
5622 C<sv_catsv_flags> has an extra C<flags> parameter which allows you to use
5623 C<SV_GMAGIC> and/or C<SV_SMAGIC> to specify any combination of magic handling
5624 (although either both or neither SV will have 'get' magic applied to it.)
5626 C<sv_catsv>, C<sv_catsv_mg>, and C<sv_catsv_nomg> are implemented
5627 in terms of C<sv_catsv_flags>.
5632 Perl_sv_catsv_flags(pTHX_ SV *const dsv, SV *const sstr, const I32 flags)
5634 PERL_ARGS_ASSERT_SV_CATSV_FLAGS;
5638 const char *spv = SvPV_flags_const(sstr, slen, flags);
5639 if (flags & SV_GMAGIC)
5641 sv_catpvn_flags(dsv, spv, slen,
5642 DO_UTF8(sstr) ? SV_CATUTF8 : SV_CATBYTES);
5643 if (flags & SV_SMAGIC)
5649 =for apidoc sv_catpv
5650 =for apidoc_item sv_catpv_flags
5651 =for apidoc_item sv_catpv_mg
5652 =for apidoc_item sv_catpv_nomg
5654 These concatenate the C<NUL>-terminated string C<sstr> onto the end of the
5655 string which is in the SV.
5656 If the SV has the UTF-8 status set, then the bytes appended should be
5659 They differ only in how they handle magic:
5661 C<sv_catpv_mg> performs both 'get' and 'set' magic.
5663 C<sv_catpv> performs only 'get' magic.
5665 C<sv_catpv_nomg> skips all magic.
5667 C<sv_catpv_flags> has an extra C<flags> parameter which allows you to specify
5668 any combination of magic handling (using C<SV_GMAGIC> and/or C<SV_SMAGIC>), and
5669 to also override the UTF-8 handling. By supplying the C<SV_CATUTF8> flag, the
5670 appended string is forced to be interpreted as UTF-8; by supplying instead the
5671 C<SV_CATBYTES> flag, it will be interpreted as just bytes. Either the SV or
5672 the string appended will be upgraded to UTF-8 if necessary.
5678 Perl_sv_catpv(pTHX_ SV *const dsv, const char *sstr)
5684 PERL_ARGS_ASSERT_SV_CATPV;
5688 junk = SvPV_force(dsv, tlen);
5690 SvGROW(dsv, tlen + len + 1);
5692 sstr = SvPVX_const(dsv);
5693 Move(sstr,SvPVX(dsv)+tlen,len+1,char);
5694 SvCUR_set(dsv, SvCUR(dsv) + len);
5695 (void)SvPOK_only_UTF8(dsv); /* validate pointer */
5700 Perl_sv_catpv_flags(pTHX_ SV *dsv, const char *sstr, const I32 flags)
5702 PERL_ARGS_ASSERT_SV_CATPV_FLAGS;
5703 sv_catpvn_flags(dsv, sstr, strlen(sstr), flags);
5707 Perl_sv_catpv_mg(pTHX_ SV *const dsv, const char *const sstr)
5709 PERL_ARGS_ASSERT_SV_CATPV_MG;
5718 Creates a new SV. A non-zero C<len> parameter indicates the number of
5719 bytes of preallocated string space the SV should have. An extra byte for a
5720 trailing C<NUL> is also reserved. (C<SvPOK> is not set for the SV even if string
5721 space is allocated.) The reference count for the new SV is set to 1.
5723 In 5.9.3, C<newSV()> replaces the older C<NEWSV()> API, and drops the first
5724 parameter, I<x>, a debug aid which allowed callers to identify themselves.
5725 This aid has been superseded by a new build option, C<PERL_MEM_LOG> (see
5726 L<perlhacktips/PERL_MEM_LOG>). The older API is still there for use in XS
5727 modules supporting older perls.
5733 Perl_newSV(pTHX_ const STRLEN len)
5740 sv = newSV_type(SVt_PV);
5741 sv_grow_fresh(sv, len + 1);
5746 =for apidoc sv_magicext
5748 Adds magic to an SV, upgrading it if necessary. Applies the
5749 supplied C<vtable> and returns a pointer to the magic added.
5751 Note that C<sv_magicext> will allow things that C<sv_magic> will not.
5752 In particular, you can add magic to C<SvREADONLY> SVs, and add more than
5753 one instance of the same C<how>.
5755 If C<namlen> is greater than zero then a C<savepvn> I<copy> of C<name> is
5756 stored, if C<namlen> is zero then C<name> is stored as-is and - as another
5757 special case - if C<(name && namlen == HEf_SVKEY)> then C<name> is assumed
5758 to contain an SV* and is stored as-is with its C<REFCNT> incremented.
5760 (This is now used as a subroutine by C<sv_magic>.)
5765 Perl_sv_magicext(pTHX_ SV *const sv, SV *const obj, const int how,
5766 const MGVTBL *const vtable, const char *const name, const I32 namlen)
5770 PERL_ARGS_ASSERT_SV_MAGICEXT;
5772 SvUPGRADE(sv, SVt_PVMG);
5773 Newxz(mg, 1, MAGIC);
5774 mg->mg_moremagic = SvMAGIC(sv);
5775 SvMAGIC_set(sv, mg);
5777 /* Sometimes a magic contains a reference loop, where the sv and
5778 object refer to each other. To prevent a reference loop that
5779 would prevent such objects being freed, we look for such loops
5780 and if we find one we avoid incrementing the object refcount.
5782 Note we cannot do this to avoid self-tie loops as intervening RV must
5783 have its REFCNT incremented to keep it in existence.
5786 if (!obj || obj == sv ||
5787 how == PERL_MAGIC_arylen ||
5788 how == PERL_MAGIC_regdata ||
5789 how == PERL_MAGIC_regdatum ||
5790 how == PERL_MAGIC_symtab ||
5791 (SvTYPE(obj) == SVt_PVGV &&
5792 (GvSV(obj) == sv || GvHV(obj) == (const HV *)sv
5793 || GvAV(obj) == (const AV *)sv || GvCV(obj) == (const CV *)sv
5794 || GvIOp(obj) == (const IO *)sv || GvFORM(obj) == (const CV *)sv)))
5799 mg->mg_obj = SvREFCNT_inc_simple(obj);
5800 mg->mg_flags |= MGf_REFCOUNTED;
5803 /* Normal self-ties simply pass a null object, and instead of
5804 using mg_obj directly, use the SvTIED_obj macro to produce a
5805 new RV as needed. For glob "self-ties", we are tieing the PVIO
5806 with an RV obj pointing to the glob containing the PVIO. In
5807 this case, to avoid a reference loop, we need to weaken the
5811 if (how == PERL_MAGIC_tiedscalar && SvTYPE(sv) == SVt_PVIO &&
5812 obj && SvROK(obj) && GvIO(SvRV(obj)) == (const IO *)sv)
5818 mg->mg_len = namlen;
5821 mg->mg_ptr = savepvn(name, namlen);
5822 else if (namlen == HEf_SVKEY) {
5823 /* Yes, this is casting away const. This is only for the case of
5824 HEf_SVKEY. I think we need to document this aberration of the
5825 constness of the API, rather than making name non-const, as
5826 that change propagating outwards a long way. */
5827 mg->mg_ptr = (char*)SvREFCNT_inc_simple_NN((SV *)name);
5829 mg->mg_ptr = (char *) name;
5831 mg->mg_virtual = (MGVTBL *) vtable;
5838 Perl_sv_magicext_mglob(pTHX_ SV *sv)
5840 PERL_ARGS_ASSERT_SV_MAGICEXT_MGLOB;
5841 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
5842 /* This sv is only a delegate. //g magic must be attached to
5847 return sv_magicext(sv, NULL, PERL_MAGIC_regex_global,
5848 &PL_vtbl_mglob, 0, 0);
5852 =for apidoc sv_magic
5854 Adds magic to an SV. First upgrades C<sv> to type C<SVt_PVMG> if
5855 necessary, then adds a new magic item of type C<how> to the head of the
5858 See C<L</sv_magicext>> (which C<sv_magic> now calls) for a description of the
5859 handling of the C<name> and C<namlen> arguments.
5861 You need to use C<sv_magicext> to add magic to C<SvREADONLY> SVs and also
5862 to add more than one instance of the same C<how>.
5868 Perl_sv_magic(pTHX_ SV *const sv, SV *const obj, const int how,
5869 const char *const name, const I32 namlen)
5871 const MGVTBL *vtable;
5874 unsigned int vtable_index;
5876 PERL_ARGS_ASSERT_SV_MAGIC;
5878 if (how < 0 || (unsigned)how >= C_ARRAY_LENGTH(PL_magic_data)
5879 || ((flags = PL_magic_data[how]),
5880 (vtable_index = flags & PERL_MAGIC_VTABLE_MASK)
5881 > magic_vtable_max))
5882 Perl_croak(aTHX_ "Don't know how to handle magic of type \\%o", how);
5884 /* PERL_MAGIC_ext is reserved for use by extensions not perl internals.
5885 Useful for attaching extension internal data to perl vars.
5886 Note that multiple extensions may clash if magical scalars
5887 etc holding private data from one are passed to another. */
5889 vtable = (vtable_index == magic_vtable_max)
5890 ? NULL : PL_magic_vtables + vtable_index;
5892 if (SvREADONLY(sv)) {
5894 !PERL_MAGIC_TYPE_READONLY_ACCEPTABLE(how)
5897 Perl_croak_no_modify();
5900 if (SvMAGICAL(sv) || (how == PERL_MAGIC_taint && SvTYPE(sv) >= SVt_PVMG)) {
5901 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
5902 /* sv_magic() refuses to add a magic of the same 'how' as an
5905 if (how == PERL_MAGIC_taint)
5911 /* Rest of work is done else where */
5912 mg = sv_magicext(sv,obj,how,vtable,name,namlen);
5915 case PERL_MAGIC_taint:
5918 case PERL_MAGIC_ext:
5919 case PERL_MAGIC_dbfile:
5926 S_sv_unmagicext_flags(pTHX_ SV *const sv, const int type, const MGVTBL *vtbl, const U32 flags)
5933 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
5935 mgp = &(((XPVMG*) SvANY(sv))->xmg_u.xmg_magic);
5936 for (mg = *mgp; mg; mg = *mgp) {
5937 const MGVTBL* const virt = mg->mg_virtual;
5938 if (mg->mg_type == type && (!flags || virt == vtbl)) {
5939 *mgp = mg->mg_moremagic;
5940 if (virt && virt->svt_free)
5941 virt->svt_free(aTHX_ sv, mg);
5942 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
5944 Safefree(mg->mg_ptr);
5945 else if (mg->mg_len == HEf_SVKEY)
5946 SvREFCNT_dec(MUTABLE_SV(mg->mg_ptr));
5947 else if (mg->mg_type == PERL_MAGIC_utf8)
5948 Safefree(mg->mg_ptr);
5950 if (mg->mg_flags & MGf_REFCOUNTED)
5951 SvREFCNT_dec(mg->mg_obj);
5955 mgp = &mg->mg_moremagic;
5958 if (SvMAGICAL(sv)) /* if we're under save_magic, wait for restore_magic; */
5959 mg_magical(sv); /* else fix the flags now */
5968 =for apidoc sv_unmagic
5970 Removes all magic of type C<type> from an SV.
5976 Perl_sv_unmagic(pTHX_ SV *const sv, const int type)
5978 PERL_ARGS_ASSERT_SV_UNMAGIC;
5979 return S_sv_unmagicext_flags(aTHX_ sv, type, NULL, 0);
5983 =for apidoc sv_unmagicext
5985 Removes all magic of type C<type> with the specified C<vtbl> from an SV.
5991 Perl_sv_unmagicext(pTHX_ SV *const sv, const int type, const MGVTBL *vtbl)
5993 PERL_ARGS_ASSERT_SV_UNMAGICEXT;
5994 return S_sv_unmagicext_flags(aTHX_ sv, type, vtbl, 1);
5998 =for apidoc sv_rvweaken
6000 Weaken a reference: set the C<SvWEAKREF> flag on this RV; give the
6001 referred-to SV C<PERL_MAGIC_backref> magic if it hasn't already; and
6002 push a back-reference to this RV onto the array of backreferences
6003 associated with that magic. If the RV is magical, set magic will be
6004 called after the RV is cleared. Silently ignores C<undef> and warns
6005 on already-weak references.
6011 Perl_sv_rvweaken(pTHX_ SV *const sv)
6015 PERL_ARGS_ASSERT_SV_RVWEAKEN;
6017 if (!SvOK(sv)) /* let undefs pass */
6020 Perl_croak(aTHX_ "Can't weaken a nonreference");
6021 else if (SvWEAKREF(sv)) {
6022 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Reference is already weak");
6025 else if (SvREADONLY(sv)) croak_no_modify();
6027 Perl_sv_add_backref(aTHX_ tsv, sv);
6029 SvREFCNT_dec_NN(tsv);
6034 =for apidoc sv_rvunweaken
6036 Unweaken a reference: Clear the C<SvWEAKREF> flag on this RV; remove
6037 the backreference to this RV from the array of backreferences
6038 associated with the target SV, increment the refcount of the target.
6039 Silently ignores C<undef> and warns on non-weak references.
6045 Perl_sv_rvunweaken(pTHX_ SV *const sv)
6049 PERL_ARGS_ASSERT_SV_RVUNWEAKEN;
6051 if (!SvOK(sv)) /* let undefs pass */
6054 Perl_croak(aTHX_ "Can't unweaken a nonreference");
6055 else if (!SvWEAKREF(sv)) {
6056 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Reference is not weak");
6059 else if (SvREADONLY(sv)) croak_no_modify();
6064 SvREFCNT_inc_NN(tsv);
6065 Perl_sv_del_backref(aTHX_ tsv, sv);
6070 =for apidoc sv_get_backrefs
6072 If C<sv> is the target of a weak reference then it returns the back
6073 references structure associated with the sv; otherwise return C<NULL>.
6075 When returning a non-null result the type of the return is relevant. If it
6076 is an AV then the elements of the AV are the weak reference RVs which
6077 point at this item. If it is any other type then the item itself is the
6080 See also C<Perl_sv_add_backref()>, C<Perl_sv_del_backref()>,
6081 C<Perl_sv_kill_backrefs()>
6087 Perl_sv_get_backrefs(SV *const sv)
6091 PERL_ARGS_ASSERT_SV_GET_BACKREFS;
6093 /* find slot to store array or singleton backref */
6095 if (SvTYPE(sv) == SVt_PVHV) {
6097 struct xpvhv_aux * const iter = HvAUX((HV *)sv);
6098 backrefs = (SV *)iter->xhv_backreferences;
6100 } else if (SvMAGICAL(sv)) {
6101 MAGIC *mg = mg_find(sv, PERL_MAGIC_backref);
6103 backrefs = mg->mg_obj;
6108 /* Give tsv backref magic if it hasn't already got it, then push a
6109 * back-reference to sv onto the array associated with the backref magic.
6111 * As an optimisation, if there's only one backref and it's not an AV,
6112 * store it directly in the HvAUX or mg_obj slot, avoiding the need to
6113 * allocate an AV. (Whether the slot holds an AV tells us whether this is
6117 /* A discussion about the backreferences array and its refcount:
6119 * The AV holding the backreferences is pointed to either as the mg_obj of
6120 * PERL_MAGIC_backref, or in the specific case of a HV, from the
6121 * xhv_backreferences field. The array is created with a refcount
6122 * of 2. This means that if during global destruction the array gets
6123 * picked on before its parent to have its refcount decremented by the
6124 * random zapper, it won't actually be freed, meaning it's still there for
6125 * when its parent gets freed.
6127 * When the parent SV is freed, the extra ref is killed by
6128 * Perl_sv_kill_backrefs. The other ref is killed, in the case of magic,
6129 * by mg_free() / MGf_REFCOUNTED, or for a hash, by Perl_hv_kill_backrefs.
6131 * When a single backref SV is stored directly, it is not reference
6136 Perl_sv_add_backref(pTHX_ SV *const tsv, SV *const sv)
6142 PERL_ARGS_ASSERT_SV_ADD_BACKREF;
6144 /* find slot to store array or singleton backref */
6146 if (SvTYPE(tsv) == SVt_PVHV) {
6147 svp = (SV**)Perl_hv_backreferences_p(aTHX_ MUTABLE_HV(tsv));
6150 mg = mg_find(tsv, PERL_MAGIC_backref);
6152 mg = sv_magicext(tsv, NULL, PERL_MAGIC_backref, &PL_vtbl_backref, NULL, 0);
6153 svp = &(mg->mg_obj);
6156 /* create or retrieve the array */
6158 if ( (!*svp && SvTYPE(sv) == SVt_PVAV)
6159 || (*svp && SvTYPE(*svp) != SVt_PVAV)
6163 mg->mg_flags |= MGf_REFCOUNTED;
6166 SvREFCNT_inc_simple_void_NN(av);
6167 /* av now has a refcnt of 2; see discussion above */
6168 av_extend(av, *svp ? 2 : 1);
6170 /* move single existing backref to the array */
6171 AvARRAY(av)[++AvFILLp(av)] = *svp; /* av_push() */
6176 av = MUTABLE_AV(*svp);
6178 /* optimisation: store single backref directly in HvAUX or mg_obj */
6182 assert(SvTYPE(av) == SVt_PVAV);
6183 if (AvFILLp(av) >= AvMAX(av)) {
6184 av_extend(av, AvFILLp(av)+1);
6187 /* push new backref */
6188 AvARRAY(av)[++AvFILLp(av)] = sv; /* av_push() */
6191 /* delete a back-reference to ourselves from the backref magic associated
6192 * with the SV we point to.
6196 Perl_sv_del_backref(pTHX_ SV *const tsv, SV *const sv)
6200 PERL_ARGS_ASSERT_SV_DEL_BACKREF;
6202 if (SvTYPE(tsv) == SVt_PVHV) {
6204 svp = (SV**)Perl_hv_backreferences_p(aTHX_ MUTABLE_HV(tsv));
6206 else if (SvIS_FREED(tsv) && PL_phase == PERL_PHASE_DESTRUCT) {
6207 /* It's possible for the last (strong) reference to tsv to have
6208 become freed *before* the last thing holding a weak reference.
6209 If both survive longer than the backreferences array, then when
6210 the referent's reference count drops to 0 and it is freed, it's
6211 not able to chase the backreferences, so they aren't NULLed.
6213 For example, a CV holds a weak reference to its stash. If both the
6214 CV and the stash survive longer than the backreferences array,
6215 and the CV gets picked for the SvBREAK() treatment first,
6216 *and* it turns out that the stash is only being kept alive because
6217 of an our variable in the pad of the CV, then midway during CV
6218 destruction the stash gets freed, but CvSTASH() isn't set to NULL.
6219 It ends up pointing to the freed HV. Hence it's chased in here, and
6220 if this block wasn't here, it would hit the !svp panic just below.
6222 I don't believe that "better" destruction ordering is going to help
6223 here - during global destruction there's always going to be the
6224 chance that something goes out of order. We've tried to make it
6225 foolproof before, and it only resulted in evolutionary pressure on
6226 fools. Which made us look foolish for our hubris. :-(
6232 = SvMAGICAL(tsv) ? mg_find(tsv, PERL_MAGIC_backref) : NULL;
6233 svp = mg ? &(mg->mg_obj) : NULL;
6237 Perl_croak(aTHX_ "panic: del_backref, svp=0");
6239 /* It's possible that sv is being freed recursively part way through the
6240 freeing of tsv. If this happens, the backreferences array of tsv has
6241 already been freed, and so svp will be NULL. If this is the case,
6242 we should not panic. Instead, nothing needs doing, so return. */
6243 if (PL_phase == PERL_PHASE_DESTRUCT && SvREFCNT(tsv) == 0)
6245 Perl_croak(aTHX_ "panic: del_backref, *svp=%p phase=%s refcnt=%" UVuf,
6246 (void*)*svp, PL_phase_names[PL_phase], (UV)SvREFCNT(tsv));
6249 if (SvTYPE(*svp) == SVt_PVAV) {
6253 AV * const av = (AV*)*svp;
6255 assert(!SvIS_FREED(av));
6259 /* for an SV with N weak references to it, if all those
6260 * weak refs are deleted, then sv_del_backref will be called
6261 * N times and O(N^2) compares will be done within the backref
6262 * array. To ameliorate this potential slowness, we:
6263 * 1) make sure this code is as tight as possible;
6264 * 2) when looking for SV, look for it at both the head and tail of the
6265 * array first before searching the rest, since some create/destroy
6266 * patterns will cause the backrefs to be freed in order.
6273 SV **p = &svp[fill];
6274 SV *const topsv = *p;
6281 /* We weren't the last entry.
6282 An unordered list has this property that you
6283 can take the last element off the end to fill
6284 the hole, and it's still an unordered list :-)
6290 break; /* should only be one */
6297 AvFILLp(av) = fill-1;
6299 else if (SvIS_FREED(*svp) && PL_phase == PERL_PHASE_DESTRUCT) {
6300 /* freed AV; skip */
6303 /* optimisation: only a single backref, stored directly */
6305 Perl_croak(aTHX_ "panic: del_backref, *svp=%p, sv=%p",
6306 (void*)*svp, (void*)sv);
6313 Perl_sv_kill_backrefs(pTHX_ SV *const sv, AV *const av)
6319 PERL_ARGS_ASSERT_SV_KILL_BACKREFS;
6324 /* after multiple passes through Perl_sv_clean_all() for a thingy
6325 * that has badly leaked, the backref array may have gotten freed,
6326 * since we only protect it against 1 round of cleanup */
6327 if (SvIS_FREED(av)) {
6328 if (PL_in_clean_all) /* All is fair */
6331 "panic: magic_killbackrefs (freed backref AV/SV)");
6335 is_array = (SvTYPE(av) == SVt_PVAV);
6337 assert(!SvIS_FREED(av));
6340 last = svp + AvFILLp(av);
6343 /* optimisation: only a single backref, stored directly */
6349 while (svp <= last) {
6351 SV *const referrer = *svp;
6352 if (SvWEAKREF(referrer)) {
6353 /* XXX Should we check that it hasn't changed? */
6354 assert(SvROK(referrer));
6355 SvRV_set(referrer, 0);
6357 SvWEAKREF_off(referrer);
6358 SvSETMAGIC(referrer);
6359 } else if (SvTYPE(referrer) == SVt_PVGV ||
6360 SvTYPE(referrer) == SVt_PVLV) {
6361 assert(SvTYPE(sv) == SVt_PVHV); /* stash backref */
6362 /* You lookin' at me? */
6363 assert(GvSTASH(referrer));
6364 assert(GvSTASH(referrer) == (const HV *)sv);
6365 GvSTASH(referrer) = 0;
6366 } else if (SvTYPE(referrer) == SVt_PVCV ||
6367 SvTYPE(referrer) == SVt_PVFM) {
6368 if (SvTYPE(sv) == SVt_PVHV) { /* stash backref */
6369 /* You lookin' at me? */
6370 assert(CvSTASH(referrer));
6371 assert(CvSTASH(referrer) == (const HV *)sv);
6372 SvANY(MUTABLE_CV(referrer))->xcv_stash = 0;
6375 assert(SvTYPE(sv) == SVt_PVGV);
6376 /* You lookin' at me? */
6377 assert(CvGV(referrer));
6378 assert(CvGV(referrer) == (const GV *)sv);
6379 anonymise_cv_maybe(MUTABLE_GV(sv),
6380 MUTABLE_CV(referrer));
6385 "panic: magic_killbackrefs (flags=%" UVxf ")",
6386 (UV)SvFLAGS(referrer));
6397 SvREFCNT_dec_NN(av); /* remove extra count added by sv_add_backref() */
6403 =for apidoc sv_insert
6405 Inserts and/or replaces a string at the specified offset/length within the SV.
6406 Similar to the Perl C<substr()> function, with C<littlelen> bytes starting at
6407 C<little> replacing C<len> bytes of the string in C<bigstr> starting at
6408 C<offset>. Handles get magic.
6410 =for apidoc sv_insert_flags
6412 Same as C<sv_insert>, but the extra C<flags> are passed to the
6413 C<SvPV_force_flags> that applies to C<bigstr>.
6419 Perl_sv_insert_flags(pTHX_ SV *const bigstr, const STRLEN offset, const STRLEN len, const char *little, const STRLEN littlelen, const U32 flags)
6425 SSize_t i; /* better be sizeof(STRLEN) or bad things happen */
6428 PERL_ARGS_ASSERT_SV_INSERT_FLAGS;
6430 SvPV_force_flags(bigstr, curlen, flags);
6431 (void)SvPOK_only_UTF8(bigstr);
6433 if (little >= SvPVX(bigstr) &&
6434 little < SvPVX(bigstr) + (SvLEN(bigstr) ? SvLEN(bigstr) : SvCUR(bigstr))) {
6435 /* little is a pointer to within bigstr, since we can reallocate bigstr,
6436 or little...little+littlelen might overlap offset...offset+len we make a copy
6438 little = savepvn(little, littlelen);
6442 if (offset + len > curlen) {
6443 SvGROW(bigstr, offset+len+1);
6444 Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
6445 SvCUR_set(bigstr, offset+len);
6449 i = littlelen - len;
6450 if (i > 0) { /* string might grow */
6451 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
6452 mid = big + offset + len;
6453 midend = bigend = big + SvCUR(bigstr);
6456 while (midend > mid) /* shove everything down */
6457 *--bigend = *--midend;
6458 Move(little,big+offset,littlelen,char);
6459 SvCUR_set(bigstr, SvCUR(bigstr) + i);
6464 Move(little,SvPVX(bigstr)+offset,len,char);
6469 big = SvPVX(bigstr);
6472 bigend = big + SvCUR(bigstr);
6474 if (midend > bigend)
6475 Perl_croak(aTHX_ "panic: sv_insert, midend=%p, bigend=%p",
6478 if (mid - big > bigend - midend) { /* faster to shorten from end */
6480 Move(little, mid, littlelen,char);
6483 i = bigend - midend;
6485 Move(midend, mid, i,char);
6489 SvCUR_set(bigstr, mid - big);
6491 else if ((i = mid - big)) { /* faster from front */
6492 midend -= littlelen;
6494 Move(big, midend - i, i, char);
6495 sv_chop(bigstr,midend-i);
6497 Move(little, mid, littlelen,char);
6499 else if (littlelen) {
6500 midend -= littlelen;
6501 sv_chop(bigstr,midend);
6502 Move(little,midend,littlelen,char);
6505 sv_chop(bigstr,midend);
6511 =for apidoc sv_replace
6513 Make the first argument a copy of the second, then delete the original.
6514 The target SV physically takes over ownership of the body of the source SV
6515 and inherits its flags; however, the target keeps any magic it owns,
6516 and any magic in the source is discarded.
6517 Note that this is a rather specialist SV copying operation; most of the
6518 time you'll want to use C<sv_setsv> or one of its many macro front-ends.
6524 Perl_sv_replace(pTHX_ SV *const sv, SV *const nsv)
6526 const U32 refcnt = SvREFCNT(sv);
6528 PERL_ARGS_ASSERT_SV_REPLACE;
6530 SV_CHECK_THINKFIRST_COW_DROP(sv);
6531 if (SvREFCNT(nsv) != 1) {
6532 Perl_croak(aTHX_ "panic: reference miscount on nsv in sv_replace()"
6533 " (%" UVuf " != 1)", (UV) SvREFCNT(nsv));
6535 if (SvMAGICAL(sv)) {
6539 sv_upgrade(nsv, SVt_PVMG);
6540 SvMAGIC_set(nsv, SvMAGIC(sv));
6541 SvFLAGS(nsv) |= SvMAGICAL(sv);
6543 SvMAGIC_set(sv, NULL);
6547 assert(!SvREFCNT(sv));
6548 #ifdef DEBUG_LEAKING_SCALARS
6549 sv->sv_flags = nsv->sv_flags;
6550 sv->sv_any = nsv->sv_any;
6551 sv->sv_refcnt = nsv->sv_refcnt;
6552 sv->sv_u = nsv->sv_u;
6554 StructCopy(nsv,sv,SV);
6556 if(SvTYPE(sv) == SVt_IV) {
6557 SET_SVANY_FOR_BODYLESS_IV(sv);
6561 SvREFCNT(sv) = refcnt;
6562 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
6567 /* We're about to free a GV which has a CV that refers back to us.
6568 * If that CV will outlive us, make it anonymous (i.e. fix up its CvGV
6572 S_anonymise_cv_maybe(pTHX_ GV *gv, CV* cv)
6577 PERL_ARGS_ASSERT_ANONYMISE_CV_MAYBE;
6580 assert(SvREFCNT(gv) == 0);
6581 assert(isGV(gv) && isGV_with_GP(gv));
6583 assert(!CvANON(cv));
6584 assert(CvGV(cv) == gv);
6585 assert(!CvNAMED(cv));
6587 /* will the CV shortly be freed by gp_free() ? */
6588 if (GvCV(gv) == cv && GvGP(gv)->gp_refcnt < 2 && SvREFCNT(cv) < 2) {
6589 SvANY(cv)->xcv_gv_u.xcv_gv = NULL;
6593 /* if not, anonymise: */
6594 gvname = (GvSTASH(gv) && HvHasNAME(GvSTASH(gv)) && HvHasENAME(GvSTASH(gv)))
6595 ? newSVhek(HvENAME_HEK(GvSTASH(gv)))
6596 : newSVpvn_flags( "__ANON__", 8, 0 );
6597 sv_catpvs(gvname, "::__ANON__");
6598 anongv = gv_fetchsv(gvname, GV_ADDMULTI, SVt_PVCV);
6599 SvREFCNT_dec_NN(gvname);
6603 SvANY(cv)->xcv_gv_u.xcv_gv = MUTABLE_GV(SvREFCNT_inc(anongv));
6608 =for apidoc sv_clear
6610 Clear an SV: call any destructors, free up any memory used by the body,
6611 and free the body itself. The SV's head is I<not> freed, although
6612 its type is set to all 1's so that it won't inadvertently be assumed
6613 to be live during global destruction etc.
6614 This function should only be called when C<REFCNT> is zero. Most of the time
6615 you'll want to call C<SvREFCNT_dec> instead.
6621 Perl_sv_clear(pTHX_ SV *const orig_sv)
6626 STRLEN hash_index = 0; /* initialise to make Coverity et al happy.
6627 Not strictly necessary */
6629 PERL_ARGS_ASSERT_SV_CLEAR;
6631 /* within this loop, sv is the SV currently being freed, and
6632 * iter_sv is the most recent AV or whatever that's being iterated
6633 * over to provide more SVs */
6636 U32 type = SvTYPE(sv);
6639 assert(SvREFCNT(sv) == 0);
6640 assert(!SvIS_FREED(sv));
6641 #if NVSIZE <= IVSIZE
6642 if (type <= SVt_NV) {
6644 if (type <= SVt_IV) {
6646 /* Historically this check on type was needed so that the code to
6647 * free bodies wasn't reached for these types, because the arena
6648 * slots were re-used for HEs and pointer table entries. The
6649 * metadata table `bodies_by_type` had the information for the sizes
6650 * for HEs and PTEs, hence the code here had to have a special-case
6651 * check to ensure that the "regular" body freeing code wasn't
6652 * reached, and get confused by the "lies" in `bodies_by_type`.
6654 * However, it hasn't actually been needed for that reason since
6655 * Aug 2010 (commit 829cd18aa7f45221), because `bodies_by_type` was
6656 * changed to always hold the accurate metadata for the SV types.
6657 * This was possible because PTEs were no longer allocated from the
6658 * "SVt_IV" arena, and the code to allocate HEs from the "SVt_NULL"
6659 * arena is entirely in hv.c, so doesn't access the table.
6661 * Some sort of check is still needed to handle SVt_IVs - pure RVs
6662 * need to take one code path which is common with RVs stored in
6663 * SVt_PV (or larger), but pure IVs mustn't take the "PV but not RV"
6664 * path, as SvPVX() doesn't point to valid memory.
6666 * Hence this code is still the most efficient way to handle this.
6668 * Additionally, for bodyless NVs, riding this branch is more
6669 * efficient than stepping through the general logic.
6674 SvFLAGS(sv) &= SVf_BREAK;
6675 SvFLAGS(sv) |= SVTYPEMASK;
6679 /* objs are always >= MG, but pad names use the SVs_OBJECT flag
6680 for another purpose */
6681 assert(!SvOBJECT(sv) || type >= SVt_PVMG);
6683 if (type >= SVt_PVMG) {
6685 if (!curse(sv, 1)) goto get_next_sv;
6686 type = SvTYPE(sv); /* destructor may have changed it */
6688 /* Free back-references before magic, in case the magic calls
6689 * Perl code that has weak references to sv. */
6690 if (type == SVt_PVHV) {
6691 Perl_hv_kill_backrefs(aTHX_ MUTABLE_HV(sv));
6695 else if (SvMAGIC(sv)) {
6696 /* Free back-references before other types of magic. */
6697 sv_unmagic(sv, PERL_MAGIC_backref);
6703 /* case SVt_INVLIST: */
6706 IoIFP(sv) != PerlIO_stdin() &&
6707 IoIFP(sv) != PerlIO_stdout() &&
6708 IoIFP(sv) != PerlIO_stderr() &&
6709 !(IoFLAGS(sv) & IOf_FAKE_DIRP))
6711 io_close(MUTABLE_IO(sv), NULL, FALSE,
6712 (IoTYPE(sv) == IoTYPE_WRONLY ||
6713 IoTYPE(sv) == IoTYPE_RDWR ||
6714 IoTYPE(sv) == IoTYPE_APPEND));
6716 if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP))
6717 PerlDir_close(IoDIRP(sv));
6718 IoDIRP(sv) = (DIR*)NULL;
6719 Safefree(IoTOP_NAME(sv));
6720 Safefree(IoFMT_NAME(sv));
6721 Safefree(IoBOTTOM_NAME(sv));
6722 if ((const GV *)sv == PL_statgv)
6726 /* FIXME for plugins */
6727 pregfree2((REGEXP*) sv);
6731 cv_undef(MUTABLE_CV(sv));
6732 /* If we're in a stash, we don't own a reference to it.
6733 * However it does have a back reference to us, which needs to
6735 if ((stash = CvSTASH(sv)))
6736 sv_del_backref(MUTABLE_SV(stash), sv);
6739 if (HvTOTALKEYS((HV*)sv) > 0) {
6741 /* this statement should match the one at the beginning of
6742 * hv_undef_flags() */
6743 if ( PL_phase != PERL_PHASE_DESTRUCT
6744 && (hek = HvNAME_HEK((HV*)sv)))
6746 if (PL_stashcache) {
6747 DEBUG_o(Perl_deb(aTHX_
6748 "sv_clear clearing PL_stashcache for '%" HEKf
6751 (void)hv_deletehek(PL_stashcache,
6754 hv_name_set((HV*)sv, NULL, 0, 0);
6757 /* save old iter_sv in unused SvSTASH field */
6758 assert(!SvOBJECT(sv));
6759 SvSTASH(sv) = (HV*)iter_sv;
6762 /* save old hash_index in unused SvMAGIC field */
6763 assert(!SvMAGICAL(sv));
6764 assert(!SvMAGIC(sv));
6765 ((XPVMG*) SvANY(sv))->xmg_u.xmg_hash_index = hash_index;
6768 next_sv = Perl_hfree_next_entry(aTHX_ (HV*)sv, &hash_index);
6769 goto get_next_sv; /* process this new sv */
6771 /* free empty hash */
6772 Perl_hv_undef_flags(aTHX_ MUTABLE_HV(sv), HV_NAME_SETALL);
6773 assert(!HvARRAY((HV*)sv));
6777 AV* av = MUTABLE_AV(sv);
6778 if (PL_comppad == av) {
6782 if (AvREAL(av) && AvFILLp(av) > -1) {
6783 next_sv = AvARRAY(av)[AvFILLp(av)--];
6784 /* save old iter_sv in top-most slot of AV,
6785 * and pray that it doesn't get wiped in the meantime */
6786 AvARRAY(av)[AvMAX(av)] = iter_sv;
6788 goto get_next_sv; /* process this new sv */
6790 Safefree(AvALLOC(av));
6795 if(ObjectMAXFIELD(sv) > -1) {
6796 next_sv = ObjectFIELDS(sv)[ObjectMAXFIELD(sv)--];
6797 /* save old iter_sv in top-most field, and pray that it
6798 * doesn't get wiped in the meantime */
6799 ObjectFIELDS(sv)[(ObjectITERSVAT(sv) = ObjectMAXFIELD(sv) + 1)] = iter_sv;
6803 Safefree(ObjectFIELDS(sv));
6806 if (LvTYPE(sv) == 'T') { /* for tie: return HE to pool */
6807 SvREFCNT_dec(HeKEY_sv((HE*)LvTARG(sv)));
6808 HeNEXT((HE*)LvTARG(sv)) = PL_hv_fetch_ent_mh;
6809 PL_hv_fetch_ent_mh = (HE*)LvTARG(sv);
6811 else if (LvTYPE(sv) != 't') /* unless tie: unrefcnted fake SV** */
6812 SvREFCNT_dec(LvTARG(sv));
6814 /* This PVLV has had a REGEXP assigned to it - the memory
6815 * normally used to store SvLEN instead points to a regex body.
6816 * Retrieving the pointer to the regex body from the correct
6817 * location is normally abstracted by ReANY(), which handles
6818 * both SVt_PVLV and SVt_REGEXP
6820 * This code is unwinding the storage specific to SVt_PVLV.
6821 * We get the body pointer directly from the union, free it,
6822 * then set SvLEN to whatever value was in the now-freed regex
6823 * body. The PVX buffer is shared by multiple re's and only
6824 * freed once, by the re whose SvLEN is non-null.
6826 * Perl_sv_force_normal_flags() also has code to free this
6827 * hidden body - it swaps the body into a temporary SV it has
6828 * just allocated, then frees that SV. That causes execution
6829 * to reach the SVt_REGEXP: case about 60 lines earlier in this
6832 * See Perl_reg_temp_copy() for the code that sets up this
6833 * REGEXP body referenced by the PVLV. */
6834 struct regexp *r = ((XPV*)SvANY(sv))->xpv_len_u.xpvlenu_rx;
6835 STRLEN len = r->xpv_len;
6836 pregfree2((REGEXP*) sv);
6837 del_body_by_type(r, SVt_REGEXP);
6838 SvLEN_set((sv), len);
6843 if (isGV_with_GP(sv)) {
6844 if(GvCVu((const GV *)sv) && (stash = GvSTASH(MUTABLE_GV(sv)))
6845 && HvHasENAME(stash))
6846 mro_method_changed_in(stash);
6847 gp_free(MUTABLE_GV(sv));
6849 unshare_hek(GvNAME_HEK(sv));
6850 /* If we're in a stash, we don't own a reference to it.
6851 * However it does have a back reference to us, which
6852 * needs to be cleared. */
6853 if ((stash = GvSTASH(sv)))
6854 sv_del_backref(MUTABLE_SV(stash), sv);
6856 /* FIXME. There are probably more unreferenced pointers to SVs
6857 * in the interpreter struct that we should check and tidy in
6858 * a similar fashion to this: */
6859 /* See also S_sv_unglob, which does the same thing. */
6860 if ((const GV *)sv == PL_last_in_gv)
6861 PL_last_in_gv = NULL;
6862 else if ((const GV *)sv == PL_statgv)
6864 else if ((const GV *)sv == PL_stderrgv)
6873 /* Don't bother with SvOOK_off(sv); as we're only going to
6877 SvOOK_offset(sv, offset);
6878 SvPV_set(sv, SvPVX_mutable(sv) - offset);
6879 /* Don't even bother with turning off the OOK flag. */
6884 SV * const target = SvRV(sv);
6886 sv_del_backref(target, sv);
6892 else if (SvPVX_const(sv)
6893 && !(SvTYPE(sv) == SVt_PVIO
6894 && !(IoFLAGS(sv) & IOf_FAKE_DIRP)))
6899 PerlIO_printf(Perl_debug_log, "Copy on write: clear\n");
6903 if (SvIsCOW_static(sv)) {
6906 else if (SvIsCOW_shared_hash(sv)) {
6907 unshare_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sv)));
6910 if (CowREFCNT(sv)) {
6919 Safefree(SvPVX_mutable(sv));
6923 else if (SvPVX_const(sv) && SvLEN(sv)
6924 && !(SvTYPE(sv) == SVt_PVIO
6925 && !(IoFLAGS(sv) & IOf_FAKE_DIRP)))
6926 Safefree(SvPVX_mutable(sv));
6927 else if (SvPVX_const(sv) && SvIsCOW(sv)) {
6928 unshare_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sv)));
6940 const struct body_details *sv_type_details;
6942 if (type == SVt_PVHV && HvHasAUX(sv)) {
6943 arena_index = HVAUX_ARENA_ROOT_IX;
6944 sv_type_details = &fake_hv_with_aux;
6948 sv_type_details = bodies_by_type + arena_index;
6951 SvFLAGS(sv) &= SVf_BREAK;
6952 SvFLAGS(sv) |= SVTYPEMASK;
6954 if (sv_type_details->arena) {
6955 del_body(((char *)SvANY(sv) + sv_type_details->offset),
6956 &PL_body_roots[arena_index]);
6958 else if (sv_type_details->body_size) {
6959 safefree(SvANY(sv));
6964 /* caller is responsible for freeing the head of the original sv */
6965 if (sv != orig_sv && !SvREFCNT(sv))
6968 /* grab and free next sv, if any */
6976 else if (!iter_sv) {
6978 } else if (SvTYPE(iter_sv) == SVt_PVAV) {
6979 AV *const av = (AV*)iter_sv;
6980 if (AvFILLp(av) > -1) {
6981 sv = AvARRAY(av)[AvFILLp(av)--];
6983 else { /* no more elements of current AV to free */
6986 /* restore previous value, squirrelled away */
6987 iter_sv = AvARRAY(av)[AvMAX(av)];
6988 Safefree(AvALLOC(av));
6991 } else if (SvTYPE(iter_sv) == SVt_PVOBJ) {
6992 if (ObjectMAXFIELD(iter_sv) > -1) {
6993 sv = ObjectFIELDS(iter_sv)[ObjectMAXFIELD(iter_sv)--];
6995 else { /* no more fields in the current SV to free */
6998 iter_sv = ObjectFIELDS(sv)[ObjectITERSVAT(sv)];
6999 Safefree(ObjectFIELDS(sv));
7002 } else if (SvTYPE(iter_sv) == SVt_PVHV) {
7003 sv = Perl_hfree_next_entry(aTHX_ (HV*)iter_sv, &hash_index);
7004 if (!sv && !HvTOTALKEYS((HV *)iter_sv)) {
7005 /* no more elements of current HV to free */
7008 /* Restore previous values of iter_sv and hash_index,
7009 * squirrelled away */
7010 assert(!SvOBJECT(sv));
7011 iter_sv = (SV*)SvSTASH(sv);
7012 assert(!SvMAGICAL(sv));
7013 hash_index = ((XPVMG*) SvANY(sv))->xmg_u.xmg_hash_index;
7015 /* perl -DA does not like rubbish in SvMAGIC. */
7019 /* free any remaining detritus from the hash struct */
7020 Perl_hv_undef_flags(aTHX_ MUTABLE_HV(sv), HV_NAME_SETALL);
7021 assert(!HvARRAY((HV*)sv));
7026 /* unrolled SvREFCNT_dec and sv_free2 follows: */
7030 if (!SvREFCNT(sv)) {
7034 if (--(SvREFCNT(sv)))
7038 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEBUGGING),
7039 "Attempt to free temp prematurely: SV 0x%" UVxf
7040 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
7044 if (SvIMMORTAL(sv)) {
7045 /* make sure SvREFCNT(sv)==0 happens very seldom */
7046 SvREFCNT(sv) = SvREFCNT_IMMORTAL;
7055 /* This routine curses the sv itself, not the object referenced by sv. So
7056 sv does not have to be ROK. */
7059 S_curse(pTHX_ SV * const sv, const bool check_refcnt) {
7060 PERL_ARGS_ASSERT_CURSE;
7061 assert(SvOBJECT(sv));
7063 if (PL_defstash && /* Still have a symbol table? */
7069 stash = SvSTASH(sv);
7070 assert(SvTYPE(stash) == SVt_PVHV);
7071 if (HvNAME(stash)) {
7072 CV* destructor = NULL;
7073 struct mro_meta *meta;
7075 assert (HvHasAUX(stash));
7077 DEBUG_o( Perl_deb(aTHX_ "Looking for DESTROY method for %s\n",
7080 /* don't make this an initialization above the assert, since it needs
7082 meta = HvMROMETA(stash);
7083 if (meta->destroy_gen && meta->destroy_gen == PL_sub_generation) {
7084 destructor = meta->destroy;
7085 DEBUG_o( Perl_deb(aTHX_ "Using cached DESTROY method %p for %s\n",
7086 (void *)destructor, HvNAME(stash)) );
7089 bool autoload = FALSE;
7091 gv_fetchmeth_pvn(stash, S_destroy, S_destroy_len, -1, 0);
7093 destructor = GvCV(gv);
7095 gv = gv_autoload_pvn(stash, S_destroy, S_destroy_len,
7096 GV_AUTOLOAD_ISMETHOD);
7098 destructor = GvCV(gv);
7102 /* we don't cache AUTOLOAD for DESTROY, since this code
7103 would then need to set $__PACKAGE__::AUTOLOAD, or the
7104 equivalent for XS AUTOLOADs */
7106 meta->destroy_gen = PL_sub_generation;
7107 meta->destroy = destructor;
7109 DEBUG_o( Perl_deb(aTHX_ "Set cached DESTROY method %p for %s\n",
7110 (void *)destructor, HvNAME(stash)) );
7113 DEBUG_o( Perl_deb(aTHX_ "Not caching AUTOLOAD for DESTROY method for %s\n",
7117 assert(!destructor || SvTYPE(destructor) == SVt_PVCV);
7119 /* A constant subroutine can have no side effects, so
7120 don't bother calling it. */
7121 && !CvCONST(destructor)
7122 /* Don't bother calling an empty destructor or one that
7123 returns immediately. */
7124 && (CvISXSUB(destructor)
7125 || (CvSTART(destructor)
7126 && (CvSTART(destructor)->op_next->op_type
7128 && (CvSTART(destructor)->op_next->op_type
7130 || CvSTART(destructor)->op_next->op_next->op_type
7136 SV* const tmpref = newRV(sv);
7137 SvREADONLY_on(tmpref); /* DESTROY() could be naughty */
7139 PUSHSTACKi(PERLSI_DESTROY);
7144 call_sv(MUTABLE_SV(destructor),
7145 G_DISCARD|G_EVAL|G_KEEPERR|G_VOID);
7149 if(SvREFCNT(tmpref) < 2) {
7150 /* tmpref is not kept alive! */
7152 SvRV_set(tmpref, NULL);
7155 SvREFCNT_dec_NN(tmpref);
7158 } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
7161 if (check_refcnt && SvREFCNT(sv)) {
7162 if (PL_in_clean_objs)
7164 "DESTROY created new reference to dead object '%" HEKf "'",
7165 HEKfARG(HvNAME_HEK(stash)));
7166 /* DESTROY gave object new lease on life */
7172 HV * const stash = SvSTASH(sv);
7173 /* Curse before freeing the stash, as freeing the stash could cause
7174 a recursive call into S_curse. */
7175 SvOBJECT_off(sv); /* Curse the object. */
7176 SvSTASH_set(sv,0); /* SvREFCNT_dec may try to read this */
7177 SvREFCNT_dec(stash); /* possibly of changed persuasion */
7183 =for apidoc sv_newref
7185 Increment an SV's reference count. Use the C<SvREFCNT_inc()> wrapper
7192 Perl_sv_newref(pTHX_ SV *const sv)
7194 PERL_UNUSED_CONTEXT;
7203 Decrement an SV's reference count, and if it drops to zero, call
7204 C<sv_clear> to invoke destructors and free up any memory used by
7205 the body; finally, deallocating the SV's head itself.
7206 Normally called via a wrapper macro C<SvREFCNT_dec>.
7212 Perl_sv_free(pTHX_ SV *const sv)
7218 /* Private helper function for SvREFCNT_dec().
7219 * Called with rc set to original SvREFCNT(sv), where rc == 0 or 1 */
7222 Perl_sv_free2(pTHX_ SV *const sv, const U32 rc)
7225 PERL_ARGS_ASSERT_SV_FREE2;
7227 if (LIKELY( rc == 1 )) {
7233 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEBUGGING),
7234 "Attempt to free temp prematurely: SV 0x%" UVxf
7235 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
7239 if (SvIMMORTAL(sv)) {
7240 /* make sure SvREFCNT(sv)==0 happens very seldom */
7241 SvREFCNT(sv) = SvREFCNT_IMMORTAL;
7245 if (! SvREFCNT(sv)) /* may have have been resurrected */
7250 /* handle exceptional cases */
7254 if (SvFLAGS(sv) & SVf_BREAK)
7255 /* this SV's refcnt has been artificially decremented to
7256 * trigger cleanup */
7258 if (PL_in_clean_all) /* All is fair */
7260 if (SvIMMORTAL(sv)) {
7261 /* make sure SvREFCNT(sv)==0 happens very seldom */
7262 SvREFCNT(sv) = SvREFCNT_IMMORTAL;
7265 if (ckWARN_d(WARN_INTERNAL)) {
7266 #ifdef DEBUG_LEAKING_SCALARS_FORK_DUMP
7267 Perl_dump_sv_child(aTHX_ sv);
7269 #ifdef DEBUG_LEAKING_SCALARS
7272 #ifdef DEBUG_LEAKING_SCALARS_ABORT
7273 if (PL_warnhook == PERL_WARNHOOK_FATAL
7274 || ckDEAD(packWARN(WARN_INTERNAL))) {
7275 /* Don't let Perl_warner cause us to escape our fate: */
7279 /* This may not return: */
7280 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
7281 "Attempt to free unreferenced scalar: SV 0x%" UVxf
7282 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
7285 #ifdef DEBUG_LEAKING_SCALARS_ABORT
7295 Returns the length of the string in the SV. Handles magic and type
7296 coercion and sets the UTF8 flag appropriately. See also C<L</SvCUR>>, which
7297 gives raw access to the C<xpv_cur> slot.
7303 Perl_sv_len(pTHX_ SV *const sv)
7310 (void)SvPV_const(sv, len);
7315 =for apidoc sv_len_utf8
7316 =for apidoc_item sv_len_utf8_nomg
7318 These return the number of characters in the string in an SV, counting wide
7319 UTF-8 bytes as a single character. Both handle type coercion.
7320 They differ only in that C<sv_len_utf8> performs 'get' magic;
7321 C<sv_len_utf8_nomg> skips any magic.
7327 * The length is cached in PERL_MAGIC_utf8, in the mg_len field. Also the
7328 * mg_ptr is used, by sv_pos_u2b() and sv_pos_b2u() - see the comments below.
7329 * (Note that the mg_len is not the length of the mg_ptr field.
7330 * This allows the cache to store the character length of the string without
7331 * needing to malloc() extra storage to attach to the mg_ptr.)
7336 Perl_sv_len_utf8(pTHX_ SV *const sv)
7342 return sv_len_utf8_nomg(sv);
7346 Perl_sv_len_utf8_nomg(pTHX_ SV * const sv)
7349 const U8 *s = (U8*)SvPV_nomg_const(sv, len);
7351 PERL_ARGS_ASSERT_SV_LEN_UTF8_NOMG;
7353 if (PL_utf8cache && SvUTF8(sv)) {
7355 MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : NULL;
7357 if (mg && (mg->mg_len != -1 || mg->mg_ptr)) {
7358 if (mg->mg_len != -1)
7361 /* We can use the offset cache for a headstart.
7362 The longer value is stored in the first pair. */
7363 STRLEN *cache = (STRLEN *) mg->mg_ptr;
7365 ulen = cache[0] + Perl_utf8_length(aTHX_ s + cache[1],
7369 if (PL_utf8cache < 0) {
7370 const STRLEN real = Perl_utf8_length(aTHX_ s, s + len);
7371 assert_uft8_cache_coherent("sv_len_utf8", ulen, real, sv);
7375 ulen = Perl_utf8_length(aTHX_ s, s + len);
7376 utf8_mg_len_cache_update(sv, &mg, ulen);
7380 return SvUTF8(sv) ? Perl_utf8_length(aTHX_ s, s + len) : len;
7383 /* Walk forwards to find the byte corresponding to the passed in UTF-8
7386 S_sv_pos_u2b_forwards(const U8 *const start, const U8 *const send,
7387 STRLEN *const uoffset_p, bool *const at_end,
7388 bool* canonical_position)
7390 const U8 *s = start;
7391 STRLEN uoffset = *uoffset_p;
7393 PERL_ARGS_ASSERT_SV_POS_U2B_FORWARDS;
7395 while (s < send && uoffset) {
7402 else if (s > send) {
7404 /* This is the existing behaviour. Possibly it should be a croak, as
7405 it's actually a bounds error */
7408 /* If the unicode position is beyond the end, we return the end but
7409 shouldn't cache that position */
7410 *canonical_position = (uoffset == 0);
7411 *uoffset_p -= uoffset;
7415 /* Given the length of the string in both bytes and UTF-8 characters, decide
7416 whether to walk forwards or backwards to find the byte corresponding to
7417 the passed in UTF-8 offset. */
7419 S_sv_pos_u2b_midway(const U8 *const start, const U8 *send,
7420 STRLEN uoffset, const STRLEN uend)
7422 STRLEN backw = uend - uoffset;
7424 PERL_ARGS_ASSERT_SV_POS_U2B_MIDWAY;
7426 if (uoffset < 2 * backw) {
7427 /* The assumption is that the average size of a character is 2 bytes,
7428 * so going forwards is twice the speed of going backwards (that's
7429 * where the 2 * backw comes from). (The real figure of course depends
7430 * on the UTF-8 data.) */
7431 const U8 *s = start;
7433 s = utf8_hop_forward(s, uoffset, send);
7440 send = utf8_hop_back(send, -backw, start);
7441 return send - start;
7444 /* For the string representation of the given scalar, find the byte
7445 corresponding to the passed in UTF-8 offset. uoffset0 and boffset0
7446 give another position in the string, *before* the sought offset, which
7447 (which is always true, as 0, 0 is a valid pair of positions), which should
7448 help reduce the amount of linear searching.
7449 If *mgp is non-NULL, it should point to the UTF-8 cache magic, which
7450 will be used to reduce the amount of linear searching. The cache will be
7451 created if necessary, and the found value offered to it for update. */
7453 S_sv_pos_u2b_cached(pTHX_ SV *const sv, MAGIC **const mgp, const U8 *const start,
7454 const U8 *const send, STRLEN uoffset,
7455 STRLEN uoffset0, STRLEN boffset0)
7457 STRLEN boffset = 0; /* Actually always set, but let's keep gcc happy. */
7459 bool at_end = FALSE;
7460 bool canonical_position = FALSE;
7462 PERL_ARGS_ASSERT_SV_POS_U2B_CACHED;
7464 assert (uoffset >= uoffset0);
7469 if (!SvREADONLY(sv) && !SvGMAGICAL(sv) && SvPOK(sv)
7471 && (*mgp || (SvTYPE(sv) >= SVt_PVMG &&
7472 (*mgp = mg_find(sv, PERL_MAGIC_utf8))))) {
7473 if ((*mgp)->mg_ptr) {
7474 STRLEN *cache = (STRLEN *) (*mgp)->mg_ptr;
7475 if (cache[0] == uoffset) {
7476 /* An exact match. */
7479 if (cache[2] == uoffset) {
7480 /* An exact match. */
7484 if (cache[0] < uoffset) {
7485 /* The cache already knows part of the way. */
7486 if (cache[0] > uoffset0) {
7487 /* The cache knows more than the passed in pair */
7488 uoffset0 = cache[0];
7489 boffset0 = cache[1];
7491 if ((*mgp)->mg_len != -1) {
7492 /* And we know the end too. */
7494 + sv_pos_u2b_midway(start + boffset0, send,
7496 (*mgp)->mg_len - uoffset0);
7498 uoffset -= uoffset0;
7500 + sv_pos_u2b_forwards(start + boffset0,
7501 send, &uoffset, &at_end,
7502 &canonical_position);
7503 uoffset += uoffset0;
7506 else if (cache[2] < uoffset) {
7507 /* We're between the two cache entries. */
7508 if (cache[2] > uoffset0) {
7509 /* and the cache knows more than the passed in pair */
7510 uoffset0 = cache[2];
7511 boffset0 = cache[3];
7515 + sv_pos_u2b_midway(start + boffset0,
7518 cache[0] - uoffset0);
7521 + sv_pos_u2b_midway(start + boffset0,
7524 cache[2] - uoffset0);
7528 else if ((*mgp)->mg_len != -1) {
7529 /* If we can take advantage of a passed in offset, do so. */
7530 /* In fact, offset0 is either 0, or less than offset, so don't
7531 need to worry about the other possibility. */
7533 + sv_pos_u2b_midway(start + boffset0, send,
7535 (*mgp)->mg_len - uoffset0);
7540 if (!found || PL_utf8cache < 0) {
7541 STRLEN real_boffset;
7542 uoffset -= uoffset0;
7543 real_boffset = boffset0 + sv_pos_u2b_forwards(start + boffset0,
7544 send, &uoffset, &at_end,
7545 &canonical_position);
7546 uoffset += uoffset0;
7548 if (found && PL_utf8cache < 0)
7549 assert_uft8_cache_coherent("sv_pos_u2b_cache", boffset,
7551 boffset = real_boffset;
7554 if (PL_utf8cache && canonical_position && !SvGMAGICAL(sv) && SvPOK(sv)) {
7556 utf8_mg_len_cache_update(sv, mgp, uoffset);
7558 utf8_mg_pos_cache_update(sv, mgp, boffset, uoffset, send - start);
7565 =for apidoc sv_pos_u2b_flags
7567 Converts the offset from a count of UTF-8 chars from
7568 the start of the string, to a count of the equivalent number of bytes; if
7569 C<lenp> is non-zero, it does the same to C<lenp>, but this time starting from
7570 C<offset>, rather than from the start
7571 of the string. Handles type coercion.
7572 C<flags> is passed to C<SvPV_flags>, and usually should be
7573 C<SV_GMAGIC|SV_CONST_RETURN> to handle magic.
7579 * sv_pos_u2b_flags() uses, like sv_pos_b2u(), the mg_ptr of the potential
7580 * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
7581 * byte offsets. See also the comments of S_utf8_mg_pos_cache_update().
7586 Perl_sv_pos_u2b_flags(pTHX_ SV *const sv, STRLEN uoffset, STRLEN *const lenp,
7593 PERL_ARGS_ASSERT_SV_POS_U2B_FLAGS;
7595 start = (U8*)SvPV_flags(sv, len, flags);
7597 const U8 * const send = start + len;
7599 boffset = sv_pos_u2b_cached(sv, &mg, start, send, uoffset, 0, 0);
7602 && *lenp /* don't bother doing work for 0, as its bytes equivalent
7603 is 0, and *lenp is already set to that. */) {
7604 /* Convert the relative offset to absolute. */
7605 const STRLEN uoffset2 = uoffset + *lenp;
7606 const STRLEN boffset2
7607 = sv_pos_u2b_cached(sv, &mg, start, send, uoffset2,
7608 uoffset, boffset) - boffset;
7622 =for apidoc sv_pos_u2b
7624 Converts the value pointed to by C<offsetp> from a count of UTF-8 chars from
7625 the start of the string, to a count of the equivalent number of bytes; if
7626 C<lenp> is non-zero, it does the same to C<lenp>, but this time starting from
7627 the offset, rather than from the start of the string. Handles magic and
7630 Use C<sv_pos_u2b_flags> in preference, which correctly handles strings longer
7637 * sv_pos_u2b() uses, like sv_pos_b2u(), the mg_ptr of the potential
7638 * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
7639 * byte offsets. See also the comments of S_utf8_mg_pos_cache_update().
7643 /* This function is subject to size and sign problems */
7646 Perl_sv_pos_u2b(pTHX_ SV *const sv, I32 *const offsetp, I32 *const lenp)
7648 PERL_ARGS_ASSERT_SV_POS_U2B;
7651 STRLEN ulen = (STRLEN)*lenp;
7652 *offsetp = (I32)sv_pos_u2b_flags(sv, (STRLEN)*offsetp, &ulen,
7653 SV_GMAGIC|SV_CONST_RETURN);
7656 *offsetp = (I32)sv_pos_u2b_flags(sv, (STRLEN)*offsetp, NULL,
7657 SV_GMAGIC|SV_CONST_RETURN);
7662 S_utf8_mg_len_cache_update(pTHX_ SV *const sv, MAGIC **const mgp,
7665 PERL_ARGS_ASSERT_UTF8_MG_LEN_CACHE_UPDATE;
7666 if (SvREADONLY(sv) || SvGMAGICAL(sv) || !SvPOK(sv))
7669 if (!*mgp && (SvTYPE(sv) < SVt_PVMG ||
7670 !(*mgp = mg_find(sv, PERL_MAGIC_utf8)))) {
7671 *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, &PL_vtbl_utf8, 0, 0);
7675 (*mgp)->mg_len = ulen;
7678 /* Create and update the UTF8 magic offset cache, with the proffered utf8/
7679 byte length pairing. The (byte) length of the total SV is passed in too,
7680 as blen, because for some (more esoteric) SVs, the call to SvPV_const()
7681 may not have updated SvCUR, so we can't rely on reading it directly.
7683 The proffered utf8/byte length pairing isn't used if the cache already has
7684 two pairs, and swapping either for the proffered pair would increase the
7685 RMS of the intervals between known byte offsets.
7687 The cache itself consists of 4 STRLEN values
7688 0: larger UTF-8 offset
7689 1: corresponding byte offset
7690 2: smaller UTF-8 offset
7691 3: corresponding byte offset
7693 Unused cache pairs have the value 0, 0.
7694 Keeping the cache "backwards" means that the invariant of
7695 cache[0] >= cache[2] is maintained even with empty slots, which means that
7696 the code that uses it doesn't need to worry if only 1 entry has actually
7697 been set to non-zero. It also makes the "position beyond the end of the
7698 cache" logic much simpler, as the first slot is always the one to start
7702 S_utf8_mg_pos_cache_update(pTHX_ SV *const sv, MAGIC **const mgp, const STRLEN byte,
7703 const STRLEN utf8, const STRLEN blen)
7707 PERL_ARGS_ASSERT_UTF8_MG_POS_CACHE_UPDATE;
7712 if (!*mgp && (SvTYPE(sv) < SVt_PVMG ||
7713 !(*mgp = mg_find(sv, PERL_MAGIC_utf8)))) {
7714 *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, (MGVTBL*)&PL_vtbl_utf8, 0,
7716 (*mgp)->mg_len = -1;
7720 if (!(cache = (STRLEN *)(*mgp)->mg_ptr)) {
7721 Newxz(cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
7722 (*mgp)->mg_ptr = (char *) cache;
7726 if (PL_utf8cache < 0 && SvPOKp(sv)) {
7727 /* SvPOKp() because, if sv is a reference, then SvPVX() is actually
7728 a pointer. Note that we no longer cache utf8 offsets on refer-
7729 ences, but this check is still a good idea, for robustness. */
7730 const U8 *start = (const U8 *) SvPVX_const(sv);
7731 const STRLEN realutf8 = utf8_length(start, start + byte);
7733 assert_uft8_cache_coherent("utf8_mg_pos_cache_update", utf8, realutf8,
7737 /* Cache is held with the later position first, to simplify the code
7738 that deals with unbounded ends. */
7740 ASSERT_UTF8_CACHE(cache);
7741 if (cache[1] == 0) {
7742 /* Cache is totally empty */
7745 } else if (cache[3] == 0) {
7746 if (byte > cache[1]) {
7747 /* New one is larger, so goes first. */
7748 cache[2] = cache[0];
7749 cache[3] = cache[1];
7757 /* float casts necessary? XXX */
7758 #define THREEWAY_SQUARE(a,b,c,d) \
7759 ((float)((d) - (c))) * ((float)((d) - (c))) \
7760 + ((float)((c) - (b))) * ((float)((c) - (b))) \
7761 + ((float)((b) - (a))) * ((float)((b) - (a)))
7763 /* Cache has 2 slots in use, and we know three potential pairs.
7764 Keep the two that give the lowest RMS distance. Do the
7765 calculation in bytes simply because we always know the byte
7766 length. squareroot has the same ordering as the positive value,
7767 so don't bother with the actual square root. */
7768 if (byte > cache[1]) {
7769 /* New position is after the existing pair of pairs. */
7770 const float keep_earlier
7771 = THREEWAY_SQUARE(0, cache[3], byte, blen);
7772 const float keep_later
7773 = THREEWAY_SQUARE(0, cache[1], byte, blen);
7775 if (keep_later < keep_earlier) {
7776 cache[2] = cache[0];
7777 cache[3] = cache[1];
7783 const float keep_later = THREEWAY_SQUARE(0, byte, cache[1], blen);
7784 float b, c, keep_earlier;
7785 if (byte > cache[3]) {
7786 /* New position is between the existing pair of pairs. */
7787 b = (float)cache[3];
7790 /* New position is before the existing pair of pairs. */
7792 c = (float)cache[3];
7794 keep_earlier = THREEWAY_SQUARE(0, b, c, blen);
7795 if (byte > cache[3]) {
7796 if (keep_later < keep_earlier) {
7806 if (! (keep_later < keep_earlier)) {
7807 cache[0] = cache[2];
7808 cache[1] = cache[3];
7815 ASSERT_UTF8_CACHE(cache);
7818 /* We already know all of the way, now we may be able to walk back. The same
7819 assumption is made as in S_sv_pos_u2b_midway(), namely that walking
7820 backward is half the speed of walking forward. */
7822 S_sv_pos_b2u_midway(pTHX_ const U8 *const s, const U8 *const target,
7823 const U8 *end, STRLEN endu)
7825 const STRLEN forw = target - s;
7826 STRLEN backw = end - target;
7828 PERL_ARGS_ASSERT_SV_POS_B2U_MIDWAY;
7830 if (forw < 2 * backw) {
7831 return utf8_length(s, target);
7834 while (end > target) {
7835 end = utf8_hop_back(end, -1, target);
7842 =for apidoc sv_pos_b2u_flags
7844 Converts C<offset> from a count of bytes from the start of the string, to
7845 a count of the equivalent number of UTF-8 chars. Handles type coercion.
7846 C<flags> is passed to C<SvPV_flags>, and usually should be
7847 C<SV_GMAGIC|SV_CONST_RETURN> to handle magic.
7853 * sv_pos_b2u_flags() uses, like sv_pos_u2b_flags(), the mg_ptr of the
7854 * potential PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8
7859 Perl_sv_pos_b2u_flags(pTHX_ SV *const sv, STRLEN const offset, U32 flags)
7862 STRLEN len = 0; /* Actually always set, but let's keep gcc happy. */
7868 PERL_ARGS_ASSERT_SV_POS_B2U_FLAGS;
7870 s = (const U8*)SvPV_flags(sv, blen, flags);
7873 Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset, blen=%" UVuf
7874 ", byte=%" UVuf, (UV)blen, (UV)offset);
7880 && SvTYPE(sv) >= SVt_PVMG
7881 && (mg = mg_find(sv, PERL_MAGIC_utf8)))
7884 STRLEN * const cache = (STRLEN *) mg->mg_ptr;
7885 if (cache[1] == offset) {
7886 /* An exact match. */
7889 if (cache[3] == offset) {
7890 /* An exact match. */
7894 if (cache[1] < offset) {
7895 /* We already know part of the way. */
7896 if (mg->mg_len != -1) {
7897 /* Actually, we know the end too. */
7899 + S_sv_pos_b2u_midway(aTHX_ s + cache[1], send,
7900 s + blen, mg->mg_len - cache[0]);
7902 len = cache[0] + utf8_length(s + cache[1], send);
7905 else if (cache[3] < offset) {
7906 /* We're between the two cached pairs, so we do the calculation
7907 offset by the byte/utf-8 positions for the earlier pair,
7908 then add the utf-8 characters from the string start to
7910 len = S_sv_pos_b2u_midway(aTHX_ s + cache[3], send,
7911 s + cache[1], cache[0] - cache[2])
7915 else { /* cache[3] > offset */
7916 len = S_sv_pos_b2u_midway(aTHX_ s, send, s + cache[3],
7920 ASSERT_UTF8_CACHE(cache);
7922 } else if (mg->mg_len != -1) {
7923 len = S_sv_pos_b2u_midway(aTHX_ s, send, s + blen, mg->mg_len);
7927 if (!found || PL_utf8cache < 0) {
7928 const STRLEN real_len = utf8_length(s, send);
7930 if (found && PL_utf8cache < 0)
7931 assert_uft8_cache_coherent("sv_pos_b2u", len, real_len, sv);
7937 utf8_mg_len_cache_update(sv, &mg, len);
7939 utf8_mg_pos_cache_update(sv, &mg, offset, len, blen);
7946 =for apidoc sv_pos_b2u
7948 Converts the value pointed to by C<offsetp> from a count of bytes from the
7949 start of the string, to a count of the equivalent number of UTF-8 chars.
7950 Handles magic and type coercion.
7952 Use C<sv_pos_b2u_flags> in preference, which correctly handles strings
7959 * sv_pos_b2u() uses, like sv_pos_u2b(), the mg_ptr of the potential
7960 * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
7965 Perl_sv_pos_b2u(pTHX_ SV *const sv, I32 *const offsetp)
7967 PERL_ARGS_ASSERT_SV_POS_B2U;
7972 *offsetp = (I32)sv_pos_b2u_flags(sv, (STRLEN)*offsetp,
7973 SV_GMAGIC|SV_CONST_RETURN);
7977 S_assert_uft8_cache_coherent(pTHX_ const char *const func, STRLEN from_cache,
7978 STRLEN real, SV *const sv)
7980 PERL_ARGS_ASSERT_ASSERT_UFT8_CACHE_COHERENT;
7982 /* As this is debugging only code, save space by keeping this test here,
7983 rather than inlining it in all the callers. */
7984 if (from_cache == real)
7987 /* Need to turn the assertions off otherwise we may recurse infinitely
7988 while printing error messages. */
7989 SAVEI8(PL_utf8cache);
7991 Perl_croak(aTHX_ "panic: %s cache %" UVuf " real %" UVuf " for %" SVf,
7992 func, (UV) from_cache, (UV) real, SVfARG(sv));
7997 =for apidoc_item sv_eq_flags
7999 These each return a boolean indicating whether or not the strings in the two
8000 SVs are equal. If S<C<'use bytes'>> is in effect, the comparison is
8001 byte-by-byte; otherwise character-by-character. Each will coerce its args to
8002 strings if necessary.
8004 They differ only in that C<sv_eq> always processes get magic, while
8005 C<sv_eq_flags> processes get magic only when the C<flags> parameter has the
8006 C<SV_GMAGIC> bit set.
8008 These functions do not handle operator overloading. For versions that do,
8009 see instead C<L</sv_streq>> or C<L</sv_streq_flags>>.
8015 Perl_sv_eq_flags(pTHX_ SV *sv1, SV *sv2, const U32 flags)
8027 /* if pv1 and pv2 are the same, second SvPV_const call may
8028 * invalidate pv1 (if we are handling magic), so we may need to
8030 if (sv1 == sv2 && flags & SV_GMAGIC
8031 && (SvTHINKFIRST(sv1) || SvGMAGICAL(sv1))) {
8032 pv1 = SvPV_const(sv1, cur1);
8033 sv1 = newSVpvn_flags(pv1, cur1, SVs_TEMP | SvUTF8(sv2));
8035 pv1 = SvPV_flags_const(sv1, cur1, flags);
8043 pv2 = SvPV_flags_const(sv2, cur2, flags);
8045 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
8046 /* Differing utf8ness. */
8048 /* sv1 is the UTF-8 one */
8049 return bytes_cmp_utf8((const U8*)pv2, cur2,
8050 (const U8*)pv1, cur1) == 0;
8053 /* sv2 is the UTF-8 one */
8054 return bytes_cmp_utf8((const U8*)pv1, cur1,
8055 (const U8*)pv2, cur2) == 0;
8060 return (pv1 == pv2) || memEQ(pv1, pv2, cur1);
8066 =for apidoc sv_streq_flags
8068 Returns a boolean indicating whether the strings in the two SVs are
8069 identical. If the flags argument has the C<SV_GMAGIC> bit set, it handles
8070 get-magic too. Will coerce its args to strings if necessary. Treats
8071 C<NULL> as undef. Correctly handles the UTF8 flag.
8073 If flags does not have the C<SV_SKIP_OVERLOAD> bit set, an attempt to use
8074 C<eq> overloading will be made. If such overloading does not exist or the
8075 flag is set, then regular string comparison will be used instead.
8077 =for apidoc sv_streq
8079 A convenient shortcut for calling C<sv_streq_flags> with the C<SV_GMAGIC>
8080 flag. This function basically behaves like the Perl code C<$sv1 eq $sv2>.
8086 Perl_sv_streq_flags(pTHX_ SV *sv1, SV *sv2, const U32 flags)
8088 PERL_ARGS_ASSERT_SV_STREQ_FLAGS;
8090 if(flags & SV_GMAGIC) {
8097 /* Treat NULL as undef */
8103 if(!(flags & SV_SKIP_OVERLOAD) &&
8104 (SvAMAGIC(sv1) || SvAMAGIC(sv2))) {
8105 SV *ret = amagic_call(sv1, sv2, seq_amg, 0);
8110 return sv_eq_flags(sv1, sv2, 0);
8114 =for apidoc sv_numeq_flags
8116 Returns a boolean indicating whether the numbers in the two SVs are
8117 identical. If the flags argument has the C<SV_GMAGIC> bit set, it handles
8118 get-magic too. Will coerce its args to numbers if necessary. Treats
8121 If flags does not have the C<SV_SKIP_OVERLOAD> bit set, an attempt to use
8122 C<==> overloading will be made. If such overloading does not exist or the
8123 flag is set, then regular numerical comparison will be used instead.
8125 =for apidoc sv_numeq
8127 A convenient shortcut for calling C<sv_numeq_flags> with the C<SV_GMAGIC>
8128 flag. This function basically behaves like the Perl code C<$sv1 == $sv2>.
8134 Perl_sv_numeq_flags(pTHX_ SV *sv1, SV *sv2, const U32 flags)
8136 PERL_ARGS_ASSERT_SV_NUMEQ_FLAGS;
8138 if(flags & SV_GMAGIC) {
8145 /* Treat NULL as undef */
8151 if(!(flags & SV_SKIP_OVERLOAD) &&
8152 (SvAMAGIC(sv1) || SvAMAGIC(sv2))) {
8153 SV *ret = amagic_call(sv1, sv2, eq_amg, 0);
8158 return do_ncmp(sv1, sv2) == 0;
8164 Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
8165 string in C<sv1> is less than, equal to, or greater than the string in
8166 C<sv2>. Is UTF-8 and S<C<'use bytes'>> aware, handles get magic, and will
8167 coerce its args to strings if necessary. See also C<L</sv_cmp_locale>>.
8169 =for apidoc sv_cmp_flags
8171 Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
8172 string in C<sv1> is less than, equal to, or greater than the string in
8173 C<sv2>. Is UTF-8 and S<C<'use bytes'>> aware and will coerce its args to strings
8174 if necessary. If the flags has the C<SV_GMAGIC> bit set, it handles get magic. See
8175 also C<L</sv_cmp_locale_flags>>.
8181 Perl_sv_cmp(pTHX_ SV *const sv1, SV *const sv2)
8183 return sv_cmp_flags(sv1, sv2, SV_GMAGIC);
8187 Perl_sv_cmp_flags(pTHX_ SV *const sv1, SV *const sv2,
8191 const char *pv1, *pv2;
8193 SV *svrecode = NULL;
8200 pv1 = SvPV_flags_const(sv1, cur1, flags);
8207 pv2 = SvPV_flags_const(sv2, cur2, flags);
8209 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
8210 /* Differing utf8ness. */
8212 const int retval = -bytes_cmp_utf8((const U8*)pv2, cur2,
8213 (const U8*)pv1, cur1);
8214 return retval ? retval < 0 ? -1 : +1 : 0;
8217 const int retval = bytes_cmp_utf8((const U8*)pv1, cur1,
8218 (const U8*)pv2, cur2);
8219 return retval ? retval < 0 ? -1 : +1 : 0;
8223 /* Here, if both are non-NULL, then they have the same UTF8ness. */
8226 cmp = cur2 ? -1 : 0;
8230 STRLEN shortest_len = cur1 < cur2 ? cur1 : cur2;
8233 if (! DO_UTF8(sv1)) {
8235 const I32 retval = memcmp((const void*)pv1,
8239 cmp = retval < 0 ? -1 : 1;
8240 } else if (cur1 == cur2) {
8243 cmp = cur1 < cur2 ? -1 : 1;
8247 else { /* Both are to be treated as UTF-EBCDIC */
8249 /* EBCDIC UTF-8 is complicated by the fact that it is based on I8
8250 * which remaps code points 0-255. We therefore generally have to
8251 * unmap back to the original values to get an accurate comparison.
8252 * But we don't have to do that for UTF-8 invariants, as by
8253 * definition, they aren't remapped, nor do we have to do it for
8254 * above-latin1 code points, as they also aren't remapped. (This
8255 * code also works on ASCII platforms, but the memcmp() above is
8258 const char *e = pv1 + shortest_len;
8260 /* Find the first bytes that differ between the two strings */
8261 while (pv1 < e && *pv1 == *pv2) {
8267 if (pv1 == e) { /* Are the same all the way to the end */
8271 cmp = cur1 < cur2 ? -1 : 1;
8274 else /* Here *pv1 and *pv2 are not equal, but all bytes earlier
8275 * in the strings were. The current bytes may or may not be
8276 * at the beginning of a character. But neither or both are
8277 * (or else earlier bytes would have been different). And
8278 * if we are in the middle of a character, the two
8279 * characters have the same number of bytes
8280 * (because in this case the start bytes are the same, and
8281 * the start bytes encode the character's length). */
8282 if (UTF8_IS_INVARIANT(*pv1))
8284 /* If both are invariants; can just compare directly */
8285 if (UTF8_IS_INVARIANT(*pv2)) {
8286 cmp = ((U8) *pv1 < (U8) *pv2) ? -1 : 1;
8288 else /* Since *pv1 is invariant, it is the whole character,
8289 which means it is at the beginning of a character.
8290 That means pv2 is also at the beginning of a
8291 character (see earlier comment). Since it isn't
8292 invariant, it must be a start byte. If it starts a
8293 character whose code point is above 255, that
8294 character is greater than any single-byte char, which
8296 if (UTF8_IS_ABOVE_LATIN1_START(*pv2))
8301 /* Here, pv2 points to a character composed of 2 bytes
8302 * whose code point is < 256. Get its code point and
8303 * compare with *pv1 */
8304 cmp = ((U8) *pv1 < EIGHT_BIT_UTF8_TO_NATIVE(*pv2, *(pv2 + 1)))
8309 else /* The code point starting at pv1 isn't a single byte */
8310 if (UTF8_IS_INVARIANT(*pv2))
8312 /* But here, the code point starting at *pv2 is a single byte,
8313 * and so *pv1 must begin a character, hence is a start byte.
8314 * If that character is above 255, it is larger than any
8315 * single-byte char, which *pv2 is */
8316 if (UTF8_IS_ABOVE_LATIN1_START(*pv1)) {
8320 /* Here, pv1 points to a character composed of 2 bytes
8321 * whose code point is < 256. Get its code point and
8322 * compare with the single byte character *pv2 */
8323 cmp = (EIGHT_BIT_UTF8_TO_NATIVE(*pv1, *(pv1 + 1)) < (U8) *pv2)
8328 else /* Here, we've ruled out either *pv1 and *pv2 being
8329 invariant. That means both are part of variants, but not
8330 necessarily at the start of a character */
8331 if ( UTF8_IS_ABOVE_LATIN1_START(*pv1)
8332 || UTF8_IS_ABOVE_LATIN1_START(*pv2))
8334 /* Here, at least one is the start of a character, which means
8335 * the other is also a start byte. And the code point of at
8336 * least one of the characters is above 255. It is a
8337 * characteristic of UTF-EBCDIC that all start bytes for
8338 * above-latin1 code points are well behaved as far as code
8339 * point comparisons go, and all are larger than all other
8340 * start bytes, so the comparison with those is also well
8342 cmp = ((U8) *pv1 < (U8) *pv2) ? -1 : 1;
8345 /* Here both *pv1 and *pv2 are part of variant characters.
8346 * They could be both continuations, or both start characters.
8347 * (One or both could even be an illegal start character (for
8348 * an overlong) which for the purposes of sorting we treat as
8350 if (UTF8_IS_CONTINUATION(*pv1)) {
8352 /* If they are continuations for code points above 255,
8353 * then comparing the current byte is sufficient, as there
8354 * is no remapping of these and so the comparison is
8355 * well-behaved. We determine if they are such
8356 * continuations by looking at the preceding byte. It
8357 * could be a start byte, from which we can tell if it is
8358 * for an above 255 code point. Or it could be a
8359 * continuation, which means the character occupies at
8360 * least 3 bytes, so must be above 255. */
8361 if ( UTF8_IS_CONTINUATION(*(pv2 - 1))
8362 || UTF8_IS_ABOVE_LATIN1_START(*(pv2 -1)))
8364 cmp = ((U8) *pv1 < (U8) *pv2) ? -1 : 1;
8368 /* Here, the continuations are for code points below 256;
8369 * back up one to get to the start byte */
8374 /* We need to get the actual native code point of each of these
8375 * variants in order to compare them */
8376 cmp = ( EIGHT_BIT_UTF8_TO_NATIVE(*pv1, *(pv1 + 1))
8377 < EIGHT_BIT_UTF8_TO_NATIVE(*pv2, *(pv2 + 1)))
8386 SvREFCNT_dec(svrecode);
8392 =for apidoc sv_cmp_locale
8394 Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and
8395 S<C<'use bytes'>> aware, handles get magic, and will coerce its args to strings
8396 if necessary. See also C<L</sv_cmp>>.
8398 =for apidoc sv_cmp_locale_flags
8400 Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and
8401 S<C<'use bytes'>> aware and will coerce its args to strings if necessary. If
8402 the flags contain C<SV_GMAGIC>, it handles get magic. See also
8403 C<L</sv_cmp_flags>>.
8409 Perl_sv_cmp_locale(pTHX_ SV *const sv1, SV *const sv2)
8411 return sv_cmp_locale_flags(sv1, sv2, SV_GMAGIC);
8415 Perl_sv_cmp_locale_flags(pTHX_ SV *const sv1, SV *const sv2,
8418 #ifdef USE_LOCALE_COLLATE
8424 if (PL_collation_standard)
8429 /* Revert to using raw compare if both operands exist, but either one
8430 * doesn't transform properly for collation */
8432 pv1 = sv_collxfrm_flags(sv1, &len1, flags);
8436 pv2 = sv_collxfrm_flags(sv2, &len2, flags);
8442 pv1 = sv1 ? sv_collxfrm_flags(sv1, &len1, flags) : (char *) NULL;
8443 pv2 = sv2 ? sv_collxfrm_flags(sv2, &len2, flags) : (char *) NULL;
8446 if (!pv1 || !len1) {
8457 retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
8460 return retval < 0 ? -1 : 1;
8463 * When the result of collation is equality, that doesn't mean
8464 * that there are no differences -- some locales exclude some
8465 * characters from consideration. So to avoid false equalities,
8466 * we use the raw string as a tiebreaker.
8473 PERL_UNUSED_ARG(flags);
8474 #endif /* USE_LOCALE_COLLATE */
8476 return sv_cmp(sv1, sv2);
8480 #ifdef USE_LOCALE_COLLATE
8483 =for apidoc sv_collxfrm
8485 This calls C<sv_collxfrm_flags> with the SV_GMAGIC flag. See
8486 C<L</sv_collxfrm_flags>>.
8488 =for apidoc sv_collxfrm_flags
8490 Add Collate Transform magic to an SV if it doesn't already have it. If the
8491 flags contain C<SV_GMAGIC>, it handles get-magic.
8493 Any scalar variable may carry C<PERL_MAGIC_collxfrm> magic that contains the
8494 scalar data of the variable, but transformed to such a format that a normal
8495 memory comparison can be used to compare the data according to the locale
8502 Perl_sv_collxfrm_flags(pTHX_ SV *const sv, STRLEN *const nxp, const I32 flags)
8506 PERL_ARGS_ASSERT_SV_COLLXFRM_FLAGS;
8508 mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_collxfrm) : (MAGIC *) NULL;
8510 /* If we don't have collation magic on 'sv', or the locale has changed
8511 * since the last time we calculated it, get it and save it now */
8512 if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
8517 /* Free the old space */
8519 Safefree(mg->mg_ptr);
8521 s = SvPV_flags_const(sv, len, flags);
8522 if ((xf = mem_collxfrm_(s, len, &xlen, cBOOL(SvUTF8(sv))))) {
8524 mg = sv_magicext(sv, 0, PERL_MAGIC_collxfrm, &PL_vtbl_collxfrm,
8539 if (mg && mg->mg_ptr) {
8541 return mg->mg_ptr + sizeof(PL_collation_ix);
8549 #endif /* USE_LOCALE_COLLATE */
8552 S_sv_gets_append_to_utf8(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
8554 SV * const tsv = newSV_type(SVt_NULL);
8557 sv_gets(tsv, fp, 0);
8558 sv_utf8_upgrade_nomg(tsv);
8559 SvCUR_set(sv,append);
8562 return (SvCUR(sv) - append) ? SvPVX(sv) : NULL;
8566 S_sv_gets_read_record(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
8569 const STRLEN recsize = SvUV(SvRV(PL_rs)); /* RsRECORD() guarantees > 0. */
8570 /* Grab the size of the record we're getting */
8571 char *buffer = SvGROW(sv, (STRLEN)(recsize + append + 1)) + append;
8578 /* With a true, record-oriented file on VMS, we need to use read directly
8579 * to ensure that we respect RMS record boundaries. The user is responsible
8580 * for providing a PL_rs value that corresponds to the FAB$W_MRS (maximum
8581 * record size) field. N.B. This is likely to produce invalid results on
8582 * varying-width character data when a record ends mid-character.
8584 fd = PerlIO_fileno(fp);
8586 && PerlLIO_fstat(fd, &st) == 0
8587 && (st.st_fab_rfm == FAB$C_VAR
8588 || st.st_fab_rfm == FAB$C_VFC
8589 || st.st_fab_rfm == FAB$C_FIX)) {
8591 bytesread = PerlLIO_read(fd, buffer, recsize);
8593 else /* in-memory file from PerlIO::Scalar
8594 * or not a record-oriented file
8598 bytesread = PerlIO_read(fp, buffer, recsize);
8600 /* At this point, the logic in sv_get() means that sv will
8601 be treated as utf-8 if the handle is utf8.
8603 if (PerlIO_isutf8(fp) && bytesread > 0) {
8604 char *bend = buffer + bytesread;
8605 char *bufp = buffer;
8606 size_t charcount = 0;
8607 bool charstart = TRUE;
8610 while (charcount < recsize) {
8611 /* count accumulated characters */
8612 while (bufp < bend) {
8614 skip = UTF8SKIP(bufp);
8616 if (bufp + skip > bend) {
8617 /* partial at the end */
8628 if (charcount < recsize) {
8630 STRLEN bufp_offset = bufp - buffer;
8631 SSize_t morebytesread;
8633 /* originally I read enough to fill any incomplete
8634 character and the first byte of the next
8635 character if needed, but if there's many
8636 multi-byte encoded characters we're going to be
8637 making a read call for every character beyond
8638 the original read size.
8640 So instead, read the rest of the character if
8641 any, and enough bytes to match at least the
8642 start bytes for each character we're going to
8646 readsize = recsize - charcount;
8648 readsize = skip - (bend - bufp) + recsize - charcount - 1;
8649 buffer = SvGROW(sv, append + bytesread + readsize + 1) + append;
8650 bend = buffer + bytesread;
8651 morebytesread = PerlIO_read(fp, bend, readsize);
8652 if (morebytesread <= 0) {
8653 /* we're done, if we still have incomplete
8654 characters the check code in sv_gets() will
8657 I'd originally considered doing
8658 PerlIO_ungetc() on all but the lead
8659 character of the incomplete character, but
8660 read() doesn't do that, so I don't.
8665 /* prepare to scan some more */
8666 bytesread += morebytesread;
8667 bend = buffer + bytesread;
8668 bufp = buffer + bufp_offset;
8676 SvCUR_set(sv, bytesread + append);
8677 buffer[bytesread] = '\0';
8678 return (SvCUR(sv) - append) ? SvPVX(sv) : NULL;
8684 Get a line from the filehandle and store it into the SV, optionally
8685 appending to the currently-stored string. If C<append> is not 0, the
8686 line is appended to the SV instead of overwriting it. C<append> should
8687 be set to the byte offset that the appended string should start at
8688 in the SV (typically, C<SvCUR(sv)> is a suitable choice).
8694 Perl_sv_gets(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
8704 PERL_ARGS_ASSERT_SV_GETS;
8706 if (SvTHINKFIRST(sv))
8707 sv_force_normal_flags(sv, append ? 0 : SV_COW_DROP_PV);
8708 /* XXX. If you make this PVIV, then copy on write can copy scalars read
8710 However, perlbench says it's slower, because the existing swipe code
8711 is faster than copy on write.
8712 Swings and roundabouts. */
8713 SvUPGRADE(sv, SVt_PV);
8716 /* line is going to be appended to the existing buffer in the sv */
8717 if (PerlIO_isutf8(fp)) {
8719 sv_utf8_upgrade_nomg(sv);
8720 sv_pos_u2b(sv,&append,0);
8722 } else if (SvUTF8(sv)) {
8723 return S_sv_gets_append_to_utf8(aTHX_ sv, fp, append);
8729 /* not appending - "clear" the string by setting SvCUR to 0,
8730 * the pv is still available. */
8733 if (PerlIO_isutf8(fp))
8736 if (IN_PERL_COMPILETIME) {
8737 /* we always read code in line mode */
8741 else if (RsSNARF(PL_rs)) {
8742 /* If it is a regular disk file use size from stat() as estimate
8743 of amount we are going to read -- may result in mallocing
8744 more memory than we really need if the layers below reduce
8745 the size we read (e.g. CRLF or a gzip layer).
8748 int fd = PerlIO_fileno(fp);
8749 if (fd >= 0 && (PerlLIO_fstat(fd, &st) == 0) && S_ISREG(st.st_mode)) {
8750 const Off_t offset = PerlIO_tell(fp);
8751 if (offset != (Off_t) -1 && st.st_size + append > offset) {
8752 #ifdef PERL_COPY_ON_WRITE
8753 /* Add an extra byte for the sake of copy-on-write's
8754 * buffer reference count. */
8755 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 2));
8757 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1));
8764 else if (RsRECORD(PL_rs)) {
8765 return S_sv_gets_read_record(aTHX_ sv, fp, append);
8767 else if (RsPARA(PL_rs)) {
8773 /* Get $/ i.e. PL_rs into same encoding as stream wants */
8774 if (PerlIO_isutf8(fp)) {
8775 rsptr = SvPVutf8(PL_rs, rslen);
8778 if (SvUTF8(PL_rs)) {
8779 if (!sv_utf8_downgrade(PL_rs, TRUE)) {
8780 Perl_croak(aTHX_ "Wide character in $/");
8783 /* extract the raw pointer to the record separator */
8784 rsptr = SvPV_const(PL_rs, rslen);
8788 /* rslast is the last character in the record separator
8789 * note we don't use rslast except when rslen is true, so the
8790 * null assign is a placeholder. */
8791 rslast = rslen ? rsptr[rslen - 1] : '\0';
8793 if (rspara) { /* have to do this both before and after */
8794 /* to make sure file boundaries work right */
8798 i = PerlIO_getc(fp);
8802 PerlIO_ungetc(fp,i);
8808 /* See if we know enough about I/O mechanism to cheat it ! */
8810 /* This used to be #ifdef test - it is made run-time test for ease
8811 of abstracting out stdio interface. One call should be cheap
8812 enough here - and may even be a macro allowing compile
8816 if (PerlIO_fast_gets(fp)) {
8818 * We can do buffer based IO operations on this filehandle.
8820 * This means we can bypass a lot of subcalls and process
8821 * the buffer directly, it also means we know the upper bound
8822 * on the amount of data we might read of the current buffer
8823 * into our sv. Knowing this allows us to preallocate the pv
8824 * to be able to hold that maximum, which allows us to simplify
8825 * a lot of logic. */
8828 * We're going to steal some values from the stdio struct
8829 * and put EVERYTHING in the innermost loop into registers.
8831 STDCHAR *ptr; /* pointer into fp's read-ahead buffer */
8832 STRLEN bpx; /* length of the data in the target sv
8833 used to fix pointers after a SvGROW */
8834 I32 shortbuffered; /* If the pv buffer is shorter than the amount
8835 of data left in the read-ahead buffer.
8836 If 0 then the pv buffer can hold the full
8837 amount left, otherwise this is the amount it
8840 /* Here is some breathtakingly efficient cheating */
8842 /* When you read the following logic resist the urge to think
8843 * of record separators that are 1 byte long. They are an
8844 * uninteresting special (simple) case.
8846 * Instead think of record separators which are at least 2 bytes
8847 * long, and keep in mind that we need to deal with such
8848 * separators when they cross a read-ahead buffer boundary.
8850 * Also consider that we need to gracefully deal with separators
8851 * that may be longer than a single read ahead buffer.
8853 * Lastly do not forget we want to copy the delimiter as well. We
8854 * are copying all data in the file _up_to_and_including_ the separator
8857 * Now that you have all that in mind here is what is happening below:
8859 * 1. When we first enter the loop we do some memory book keeping to see
8860 * how much free space there is in the target SV. (This sub assumes that
8861 * it is operating on the same SV most of the time via $_ and that it is
8862 * going to be able to reuse the same pv buffer each call.) If there is
8863 * "enough" room then we set "shortbuffered" to how much space there is
8864 * and start reading forward.
8866 * 2. When we scan forward we copy from the read-ahead buffer to the target
8867 * SV's pv buffer. While we go we watch for the end of the read-ahead buffer,
8868 * and the end of the of pv, as well as for the "rslast", which is the last
8869 * char of the separator.
8871 * 3. When scanning forward if we see rslast then we jump backwards in *pv*
8872 * (which has a "complete" record up to the point we saw rslast) and check
8873 * it to see if it matches the separator. If it does we are done. If it doesn't
8874 * we continue on with the scan/copy.
8876 * 4. If we run out of read-ahead buffer (cnt goes to 0) then we have to get
8877 * the IO system to read the next buffer. We do this by doing a getc(), which
8878 * returns a single char read (or EOF), and prefills the buffer, and also
8879 * allows us to find out how full the buffer is. We use this information to
8880 * SvGROW() the sv to the size remaining in the buffer, after which we copy
8881 * the returned single char into the target sv, and then go back into scan
8884 * 5. If we run out of write-buffer then we SvGROW() it by the size of the
8885 * remaining space in the read-buffer.
8887 * Note that this code despite its twisty-turny nature is pretty darn slick.
8888 * It manages single byte separators, multi-byte cross boundary separators,
8889 * and cross-read-buffer separators cleanly and efficiently at the cost
8890 * of potentially greatly overallocating the target SV.
8896 /* get the number of bytes remaining in the read-ahead buffer
8897 * on first call on a given fp this will return 0.*/
8898 cnt = PerlIO_get_cnt(fp);
8900 /* make sure we have the room */
8901 if ((I32)(SvLEN(sv) - append) <= cnt + 1) {
8902 /* Not room for all of it
8903 if we are looking for a separator and room for some
8905 if (rslen && cnt > 80 && (I32)SvLEN(sv) > append) {
8906 /* just process what we have room for */
8907 shortbuffered = cnt - SvLEN(sv) + append + 1;
8908 cnt -= shortbuffered;
8911 /* ensure that the target sv has enough room to hold
8912 * the rest of the read-ahead buffer */
8914 /* remember that cnt can be negative */
8915 SvGROW(sv, (STRLEN)(append + (cnt <= 0 ? 2 : (cnt + 1))));
8919 /* we have enough room to hold the full buffer, lets scream */
8923 /* extract the pointer to sv's string buffer, offset by append as necessary */
8924 bp = (STDCHAR*)SvPVX_const(sv) + append; /* move these two too to registers */
8925 /* extract the point to the read-ahead buffer */
8926 ptr = (STDCHAR*)PerlIO_get_ptr(fp);
8928 /* some trace debug output */
8929 DEBUG_P(PerlIO_printf(Perl_debug_log,
8930 "Screamer: entering, ptr=%" UVuf ", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
8931 DEBUG_P(PerlIO_printf(Perl_debug_log,
8932 "Screamer: entering: PerlIO * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%"
8934 PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
8935 PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
8939 /* if there is stuff left in the read-ahead buffer */
8941 /* if there is a separator */
8943 /* find next rslast */
8946 /* shortcut common case of blank line */
8948 if ((*bp++ = *ptr++) == rslast)
8949 goto thats_all_folks;
8951 p = (STDCHAR *)memchr(ptr, rslast, cnt);
8953 SSize_t got = p - ptr + 1;
8954 Copy(ptr, bp, got, STDCHAR);
8958 goto thats_all_folks;
8960 Copy(ptr, bp, cnt, STDCHAR);
8966 /* no separator, slurp the full buffer */
8967 Copy(ptr, bp, cnt, char); /* this | eat */
8968 bp += cnt; /* screams | dust */
8969 ptr += cnt; /* louder | sed :-) */
8971 assert (!shortbuffered);
8972 goto cannot_be_shortbuffered;
8976 if (shortbuffered) { /* oh well, must extend */
8977 /* we didn't have enough room to fit the line into the target buffer
8978 * so we must extend the target buffer and keep going */
8979 cnt = shortbuffered;
8981 bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */
8983 /* extned the target sv's buffer so it can hold the full read-ahead buffer */
8984 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
8985 bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */
8989 cannot_be_shortbuffered:
8990 /* we need to refill the read-ahead buffer if possible */
8992 DEBUG_P(PerlIO_printf(Perl_debug_log,
8993 "Screamer: going to getc, ptr=%" UVuf ", cnt=%" IVdf "\n",
8994 PTR2UV(ptr),(IV)cnt));
8995 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* deregisterize cnt and ptr */
8997 DEBUG_Pv(PerlIO_printf(Perl_debug_log,
8998 "Screamer: pre: FILE * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%" UVuf "\n",
8999 PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
9000 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
9003 call PerlIO_getc() to let it prefill the lookahead buffer
9005 This used to call 'filbuf' in stdio form, but as that behaves like
9006 getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
9007 another abstraction.
9009 Note we have to deal with the char in 'i' if we are not at EOF
9011 bpx = bp - (STDCHAR*)SvPVX_const(sv);
9012 /* signals might be called here, possibly modifying sv */
9013 i = PerlIO_getc(fp); /* get more characters */
9014 bp = (STDCHAR*)SvPVX_const(sv) + bpx;
9016 DEBUG_Pv(PerlIO_printf(Perl_debug_log,
9017 "Screamer: post: FILE * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%" UVuf "\n",
9018 PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
9019 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
9021 /* find out how much is left in the read-ahead buffer, and rextract its pointer */
9022 cnt = PerlIO_get_cnt(fp);
9023 ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */
9024 DEBUG_P(PerlIO_printf(Perl_debug_log,
9025 "Screamer: after getc, ptr=%" UVuf ", cnt=%" IVdf "\n",
9026 PTR2UV(ptr),(IV)cnt));
9028 if (i == EOF) /* all done for ever? */
9029 goto thats_really_all_folks;
9031 /* make sure we have enough space in the target sv */
9032 bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */
9034 SvGROW(sv, bpx + cnt + 2);
9035 bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */
9037 /* copy of the char we got from getc() */
9038 *bp++ = (STDCHAR)i; /* store character from PerlIO_getc */
9040 /* make sure we deal with the i being the last character of a separator */
9041 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
9042 goto thats_all_folks;
9046 /* check if we have actually found the separator - only really applies
9048 if ((rslen > 1 && (STRLEN)(bp - (STDCHAR*)SvPVX_const(sv)) < rslen) ||
9049 memNE((char*)bp - rslen, rsptr, rslen))
9050 goto screamer; /* go back to the fray */
9051 thats_really_all_folks:
9053 cnt += shortbuffered;
9054 DEBUG_P(PerlIO_printf(Perl_debug_log,
9055 "Screamer: quitting, ptr=%" UVuf ", cnt=%" IVdf "\n",PTR2UV(ptr),(IV)cnt));
9056 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* put these back or we're in trouble */
9057 DEBUG_P(PerlIO_printf(Perl_debug_log,
9058 "Screamer: end: FILE * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%" UVuf
9060 PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
9061 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
9063 SvCUR_set(sv, bp - (STDCHAR*)SvPVX_const(sv)); /* set length */
9064 DEBUG_P(PerlIO_printf(Perl_debug_log,
9065 "Screamer: done, len=%ld, string=|%.*s|\n",
9066 (long)SvCUR(sv),(int)SvCUR(sv),SvPVX_const(sv)));
9070 /*The big, slow, and stupid way. */
9075 const STDCHAR * const bpe = buf + sizeof(buf);
9077 while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = (STDCHAR)i) != rslast && bp < bpe)
9078 ; /* keep reading */
9082 cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
9083 /* Accommodate broken VAXC compiler, which applies U8 cast to
9084 * both args of ?: operator, causing EOF to change into 255
9087 i = (U8)buf[cnt - 1];
9093 cnt = 0; /* we do need to re-set the sv even when cnt <= 0 */
9095 sv_catpvn_nomg(sv, (char *) buf, cnt);
9097 sv_setpvn(sv, (char *) buf, cnt); /* "nomg" is implied */
9099 if (i != EOF && /* joy */
9101 SvCUR(sv) < rslen ||
9102 memNE(SvPVX_const(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
9106 * If we're reading from a TTY and we get a short read,
9107 * indicating that the user hit his EOF character, we need
9108 * to notice it now, because if we try to read from the TTY
9109 * again, the EOF condition will disappear.
9111 * The comparison of cnt to sizeof(buf) is an optimization
9112 * that prevents unnecessary calls to feof().
9116 if (!(cnt < (I32)sizeof(buf) && PerlIO_eof(fp)))
9122 if (rspara) { /* have to do this both before and after */
9123 while (i != EOF) { /* to make sure file boundaries work right */
9124 i = PerlIO_getc(fp);
9126 PerlIO_ungetc(fp,i);
9132 return (SvCUR(sv) - append) ? SvPVX(sv) : NULL;
9137 =for apidoc_item sv_inc_nomg
9139 These auto-increment the value in the SV, doing string to numeric conversion
9140 if necessary. They both handle operator overloading.
9142 They differ only in that C<sv_inc> performs 'get' magic; C<sv_inc_nomg> skips
9149 Perl_sv_inc(pTHX_ SV *const sv)
9158 Perl_sv_inc_nomg(pTHX_ SV *const sv)
9165 if (SvTHINKFIRST(sv)) {
9166 if (SvREADONLY(sv)) {
9167 Perl_croak_no_modify();
9171 if (SvAMAGIC(sv) && AMG_CALLunary(sv, inc_amg))
9173 i = PTR2IV(SvRV(sv));
9177 else sv_force_normal_flags(sv, 0);
9179 flags = SvFLAGS(sv);
9180 if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) {
9181 /* It's (privately or publicly) a float, but not tested as an
9182 integer, so test it to see. */
9184 flags = SvFLAGS(sv);
9186 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
9187 /* It's publicly an integer, or privately an integer-not-float */
9188 #ifdef PERL_PRESERVE_IVUV
9192 if (SvUVX(sv) == UV_MAX)
9193 sv_setnv(sv, UV_MAX_P1);
9195 (void)SvIOK_only_UV(sv);
9196 SvUV_set(sv, SvUVX(sv) + 1);
9199 if (SvIVX(sv) == IV_MAX)
9200 sv_setuv(sv, (UV)IV_MAX + 1);
9202 (void)SvIOK_only(sv);
9203 SvIV_set(sv, SvIVX(sv) + 1);
9208 if (flags & SVp_NOK) {
9209 const NV was = SvNVX(sv);
9210 if (NV_OVERFLOWS_INTEGERS_AT != 0.0 &&
9211 /* If NVX was NaN, the following comparisons return always false */
9212 UNLIKELY(was >= NV_OVERFLOWS_INTEGERS_AT ||
9213 was < -NV_OVERFLOWS_INTEGERS_AT) &&
9214 #if defined(NAN_COMPARE_BROKEN)
9215 LIKELY(!Perl_isinfnan(was))
9217 LIKELY(!Perl_isinf(was))
9220 /* diag_listed_as: Lost precision when %s %f by 1 */
9221 Perl_ck_warner(aTHX_ packWARN(WARN_IMPRECISION),
9222 "Lost precision when incrementing %" NVff " by 1",
9225 (void)SvNOK_only(sv);
9226 SvNV_set(sv, was + 1.0);
9230 /* treat AV/HV/CV/FM/IO and non-fake GVs as immutable */
9231 if (SvTYPE(sv) >= SVt_PVAV || (isGV_with_GP(sv) && !SvFAKE(sv)))
9232 Perl_croak_no_modify();
9234 if (!(flags & SVp_POK) || !*SvPVX_const(sv)) {
9235 if ((flags & SVTYPEMASK) < SVt_PVIV)
9236 sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV ? SVt_PVIV : SVt_IV));
9237 (void)SvIOK_only(sv);
9242 while (isALPHA(*d)) d++;
9243 while (isDIGIT(*d)) d++;
9244 if (d < SvEND(sv)) {
9245 const int numtype = grok_number_flags(SvPVX_const(sv), SvCUR(sv), NULL, PERL_SCAN_TRAILING);
9246 #ifdef PERL_PRESERVE_IVUV
9247 /* Got to punt this as an integer if needs be, but we don't issue
9248 warnings. Probably ought to make the sv_iv_please() that does
9249 the conversion if possible, and silently. */
9250 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
9251 /* Need to try really hard to see if it's an integer.
9252 9.22337203685478e+18 is an integer.
9253 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
9254 so $a="9.22337203685478e+18"; $a+0; $a++
9255 needs to be the same as $a="9.22337203685478e+18"; $a++
9262 /* sv_2iv *should* have made this an NV */
9263 if (flags & SVp_NOK) {
9264 (void)SvNOK_only(sv);
9265 SvNV_set(sv, SvNVX(sv) + 1.0);
9268 /* I don't think we can get here. Maybe I should assert this
9269 And if we do get here I suspect that sv_setnv will croak. NWC
9271 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%" UVxf " NV=%" NVgf "\n",
9272 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
9274 #endif /* PERL_PRESERVE_IVUV */
9275 if (!numtype && ckWARN(WARN_NUMERIC))
9276 not_incrementable(sv);
9277 sv_setnv(sv,Atof(SvPVX_const(sv)) + 1.0);
9281 while (d >= SvPVX_const(sv)) {
9289 /* MKS: The original code here died if letters weren't consecutive.
9290 * at least it didn't have to worry about non-C locales. The
9291 * new code assumes that ('z'-'a')==('Z'-'A'), letters are
9292 * arranged in order (although not consecutively) and that only
9293 * [A-Za-z] are accepted by isALPHA in the C locale.
9295 if (isALPHA_FOLD_NE(*d, 'z')) {
9296 do { ++*d; } while (!isALPHA(*d));
9299 *(d--) -= 'z' - 'a';
9304 *(d--) -= 'z' - 'a' + 1;
9308 /* oh,oh, the number grew */
9309 SvGROW(sv, SvCUR(sv) + 2);
9310 SvCUR_set(sv, SvCUR(sv) + 1);
9311 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX_const(sv); d--)
9321 =for apidoc_item sv_dec_nomg
9323 These auto-decrement the value in the SV, doing string to numeric conversion
9324 if necessary. They both handle operator overloading.
9326 They differ only in that:
9328 C<sv_dec> handles 'get' magic; C<sv_dec_nomg> skips 'get' magic.
9334 Perl_sv_dec(pTHX_ SV *const sv)
9343 Perl_sv_dec_nomg(pTHX_ SV *const sv)
9349 if (SvTHINKFIRST(sv)) {
9350 if (SvREADONLY(sv)) {
9351 Perl_croak_no_modify();
9355 if (SvAMAGIC(sv) && AMG_CALLunary(sv, dec_amg))
9357 i = PTR2IV(SvRV(sv));
9361 else sv_force_normal_flags(sv, 0);
9363 /* Unlike sv_inc we don't have to worry about string-never-numbers
9364 and keeping them magic. But we mustn't warn on punting */
9365 flags = SvFLAGS(sv);
9366 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
9367 /* It's publicly an integer, or privately an integer-not-float */
9368 #ifdef PERL_PRESERVE_IVUV
9372 if (SvUVX(sv) == 0) {
9373 (void)SvIOK_only(sv);
9377 (void)SvIOK_only_UV(sv);
9378 SvUV_set(sv, SvUVX(sv) - 1);
9381 if (SvIVX(sv) == IV_MIN) {
9382 sv_setnv(sv, (NV)IV_MIN);
9386 (void)SvIOK_only(sv);
9387 SvIV_set(sv, SvIVX(sv) - 1);
9392 if (flags & SVp_NOK) {
9395 const NV was = SvNVX(sv);
9396 if (NV_OVERFLOWS_INTEGERS_AT != 0.0 &&
9397 /* If NVX was NaN, these comparisons return always false */
9398 UNLIKELY(was <= -NV_OVERFLOWS_INTEGERS_AT ||
9399 was > NV_OVERFLOWS_INTEGERS_AT) &&
9400 #if defined(NAN_COMPARE_BROKEN)
9401 LIKELY(!Perl_isinfnan(was))
9403 LIKELY(!Perl_isinf(was))
9406 /* diag_listed_as: Lost precision when %s %f by 1 */
9407 Perl_ck_warner(aTHX_ packWARN(WARN_IMPRECISION),
9408 "Lost precision when decrementing %" NVff " by 1",
9411 (void)SvNOK_only(sv);
9412 SvNV_set(sv, was - 1.0);
9417 /* treat AV/HV/CV/FM/IO and non-fake GVs as immutable */
9418 if (SvTYPE(sv) >= SVt_PVAV || (isGV_with_GP(sv) && !SvFAKE(sv)))
9419 Perl_croak_no_modify();
9421 if (!(flags & SVp_POK)) {
9422 if ((flags & SVTYPEMASK) < SVt_PVIV)
9423 sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV) ? SVt_PVIV : SVt_IV);
9425 (void)SvIOK_only(sv);
9428 #ifdef PERL_PRESERVE_IVUV
9430 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), NULL);
9431 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
9432 /* Need to try really hard to see if it's an integer.
9433 9.22337203685478e+18 is an integer.
9434 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
9435 so $a="9.22337203685478e+18"; $a+0; $a--
9436 needs to be the same as $a="9.22337203685478e+18"; $a--
9443 /* sv_2iv *should* have made this an NV */
9444 if (flags & SVp_NOK) {
9445 (void)SvNOK_only(sv);
9446 SvNV_set(sv, SvNVX(sv) - 1.0);
9449 /* I don't think we can get here. Maybe I should assert this
9450 And if we do get here I suspect that sv_setnv will croak. NWC
9452 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%" UVxf " NV=%" NVgf "\n",
9453 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
9456 #endif /* PERL_PRESERVE_IVUV */
9457 sv_setnv(sv,Atof(SvPVX_const(sv)) - 1.0); /* punt */
9460 /* this define is used to eliminate a chunk of duplicated but shared logic
9461 * it has the suffix __SV_C to signal that it isnt API, and isnt meant to be
9462 * used anywhere but here - yves
9464 #define PUSH_EXTEND_MORTAL__SV_C(AnSv) \
9466 SSize_t ix = ++PL_tmps_ix; \
9467 if (UNLIKELY(ix >= PL_tmps_max)) \
9468 ix = tmps_grow_p(ix); \
9469 PL_tmps_stack[ix] = (AnSv); \
9473 =for apidoc sv_mortalcopy
9475 Creates a new SV which is a copy of the original SV (using C<sv_setsv>).
9476 The new SV is marked as mortal. It will be destroyed "soon", either by an
9477 explicit call to C<FREETMPS>, or by an implicit call at places such as
9478 statement boundaries. See also C<L</sv_newmortal>> and C<L</sv_2mortal>>.
9480 =for apidoc sv_mortalcopy_flags
9482 Like C<sv_mortalcopy>, but the extra C<flags> are passed to the
9488 /* Make a string that will exist for the duration of the expression
9489 * evaluation. Actually, it may have to last longer than that, but
9490 * hopefully we won't free it until it has been assigned to a
9491 * permanent location. */
9494 Perl_sv_mortalcopy_flags(pTHX_ SV *const oldstr, U32 flags)
9498 if (flags & SV_GMAGIC)
9499 SvGETMAGIC(oldstr); /* before new_SV, in case it dies */
9501 sv_setsv_flags(sv,oldstr,flags & ~SV_GMAGIC);
9502 PUSH_EXTEND_MORTAL__SV_C(sv);
9508 =for apidoc sv_newmortal
9510 Creates a new null SV which is mortal. The reference count of the SV is
9511 set to 1. It will be destroyed "soon", either by an explicit call to
9512 C<FREETMPS>, or by an implicit call at places such as statement boundaries.
9513 See also C<L</sv_mortalcopy>> and C<L</sv_2mortal>>.
9519 Perl_sv_newmortal(pTHX)
9524 SvFLAGS(sv) = SVs_TEMP;
9525 PUSH_EXTEND_MORTAL__SV_C(sv);
9531 =for apidoc newSVpvn_flags
9533 Creates a new SV and copies a string (which may contain C<NUL> (C<\0>)
9534 characters) into it. The reference count for the
9535 SV is set to 1. Note that if C<len> is zero, Perl will create a zero length
9536 string. You are responsible for ensuring that the source string is at least
9537 C<len> bytes long. If the C<s> argument is NULL the new SV will be undefined.
9538 Currently the only flag bits accepted are C<SVf_UTF8> and C<SVs_TEMP>.
9539 If C<SVs_TEMP> is set, then C<sv_2mortal()> is called on the result before
9540 returning. If C<SVf_UTF8> is set, C<s>
9541 is considered to be in UTF-8 and the
9542 C<SVf_UTF8> flag will be set on the new SV.
9543 C<newSVpvn_utf8()> is a convenience wrapper for this function, defined as
9545 #define newSVpvn_utf8(s, len, u) \
9546 newSVpvn_flags((s), (len), (u) ? SVf_UTF8 : 0)
9548 =for apidoc Amnh||SVs_TEMP
9554 Perl_newSVpvn_flags(pTHX_ const char *const s, const STRLEN len, const U32 flags)
9558 /* All the flags we don't support must be zero.
9559 And we're new code so I'm going to assert this from the start. */
9560 assert(!(flags & ~(SVf_UTF8|SVs_TEMP)));
9561 sv = newSV_type(SVt_PV);
9562 sv_setpvn_fresh(sv,s,len);
9564 /* This code used to do a sv_2mortal(), however we now unroll the call to
9565 * sv_2mortal() and do what it does ourselves here. Since we have asserted
9566 * that flags can only have the SVf_UTF8 and/or SVs_TEMP flags set above we
9567 * can use it to enable the sv flags directly (bypassing SvTEMP_on), which
9568 * in turn means we don't need to mask out the SVf_UTF8 flag below, which
9569 * means that we eliminate quite a few steps than it looks - Yves
9570 * (explaining patch by gfx) */
9572 SvFLAGS(sv) |= flags;
9574 if(flags & SVs_TEMP){
9575 PUSH_EXTEND_MORTAL__SV_C(sv);
9582 =for apidoc sv_2mortal
9584 Marks an existing SV as mortal. The SV will be destroyed "soon", either
9585 by an explicit call to C<FREETMPS>, or by an implicit call at places such as
9586 statement boundaries. C<SvTEMP()> is turned on which means that the SV's
9587 string buffer can be "stolen" if this SV is copied. See also
9588 C<L</sv_newmortal>> and C<L</sv_mortalcopy>>.
9594 Perl_sv_2mortal(pTHX_ SV *const sv)
9600 PUSH_EXTEND_MORTAL__SV_C(sv);
9608 Creates a new SV and copies a string (which may contain C<NUL> (C<\0>)
9609 characters) into it. The reference count for the
9610 SV is set to 1. If C<len> is zero, Perl will compute the length using
9611 C<strlen()>, (which means if you use this option, that C<s> can't have embedded
9612 C<NUL> characters and has to have a terminating C<NUL> byte).
9614 This function can cause reliability issues if you are likely to pass in
9615 empty strings that are not null terminated, because it will run
9616 strlen on the string and potentially run past valid memory.
9618 Using L</newSVpvn> is a safer alternative for non C<NUL> terminated strings.
9619 For string literals use L</newSVpvs> instead. This function will work fine for
9620 C<NUL> terminated strings, but if you want to avoid the if statement on whether
9621 to call C<strlen> use C<newSVpvn> instead (calling C<strlen> yourself).
9627 Perl_newSVpv(pTHX_ const char *const s, const STRLEN len)
9629 SV *sv = newSV_type(SVt_PV);
9630 sv_setpvn_fresh(sv, s, len || s == NULL ? len : strlen(s));
9635 =for apidoc newSVpvn
9637 Creates a new SV and copies a string into it, which may contain C<NUL> characters
9638 (C<\0>) and other binary data. The reference count for the SV is set to 1.
9639 Note that if C<len> is zero, Perl will create a zero length (Perl) string. You
9640 are responsible for ensuring that the source buffer is at least
9641 C<len> bytes long. If the C<buffer> argument is NULL the new SV will be
9648 Perl_newSVpvn(pTHX_ const char *const buffer, const STRLEN len)
9650 SV *sv = newSV_type(SVt_PV);
9651 sv_setpvn_fresh(sv,buffer,len);
9656 =for apidoc newSVhek_mortal
9658 Creates a new mortal SV from the hash key structure. It will generate
9659 scalars that point to the shared string table where possible. Returns
9660 a new (undefined) SV if C<hek> is NULL.
9662 This is more efficient than using sv_2mortal(newSVhek( ... ))
9668 Perl_newSVhek_mortal(pTHX_ const HEK *const hek)
9670 SV * const sv = newSVhek(hek);
9672 assert(!SvIMMORTAL(sv));
9674 PUSH_EXTEND_MORTAL__SV_C(sv);
9680 =for apidoc newSVhek
9682 Creates a new SV from the hash key structure. It will generate scalars that
9683 point to the shared string table where possible. Returns a new (undefined)
9684 SV if C<hek> is NULL.
9690 Perl_newSVhek(pTHX_ const HEK *const hek)
9699 if (HEK_LEN(hek) == HEf_SVKEY) {
9700 return newSVsv(*(SV**)HEK_KEY(hek));
9702 const int flags = HEK_FLAGS(hek);
9703 if (flags & HVhek_WASUTF8) {
9705 Andreas would like keys he put in as utf8 to come back as utf8
9707 STRLEN utf8_len = HEK_LEN(hek);
9708 SV * const sv = newSV_type(SVt_PV);
9709 char *as_utf8 = (char *)bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
9710 /* bytes_to_utf8() allocates a new string, which we can repurpose: */
9711 sv_usepvn_flags(sv, as_utf8, utf8_len, SV_HAS_TRAILING_NUL);
9714 } else if (flags & HVhek_NOTSHARED) {
9715 /* A hash that isn't using shared hash keys has to have
9716 the flag in every key so that we know not to try to call
9717 share_hek_hek on it. */
9719 SV * const sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
9724 /* This will be overwhelmingly the most common case. */
9726 /* Inline most of newSVpvn_share(), because share_hek_hek() is far
9727 more efficient than sharepvn(). */
9728 SV *sv = newSV_type(SVt_PV);
9730 SvPV_set(sv, (char *)HEK_KEY(share_hek_hek(hek)));
9731 SvCUR_set(sv, HEK_LEN(hek));
9743 =for apidoc newSVpvn_share
9745 Creates a new SV with its C<SvPVX_const> pointing to a shared string in the string
9746 table. If the string does not already exist in the table, it is
9747 created first. Turns on the C<SvIsCOW> flag (or C<READONLY>
9748 and C<FAKE> in 5.16 and earlier). If the C<hash> parameter
9749 is non-zero, that value is used; otherwise the hash is computed.
9750 The string's hash can later be retrieved from the SV
9751 with the C<L</SvSHARED_HASH>> macro. The idea here is
9752 that as the string table is used for shared hash keys these strings will have
9753 C<SvPVX_const == HeKEY> and hash lookup will avoid string compare.
9759 Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash)
9762 bool is_utf8 = FALSE;
9763 const char *const orig_src = src;
9766 STRLEN tmplen = -len;
9768 /* See the note in hv.c:hv_fetch() --jhi */
9769 src = (char*)bytes_from_utf8((const U8*)src, &tmplen, &is_utf8);
9773 PERL_HASH(hash, src, len);
9774 sv = newSV_type(SVt_PV);
9775 /* The logic for this is inlined in S_mro_get_linear_isa_dfs(), so if it
9776 changes here, update it there too. */
9777 SvPV_set(sv, sharepvn(src, is_utf8?-len:len, hash));
9784 if (src != orig_src)
9790 =for apidoc newSVpv_share
9792 Like C<newSVpvn_share>, but takes a C<NUL>-terminated string instead of a
9799 Perl_newSVpv_share(pTHX_ const char *src, U32 hash)
9801 return newSVpvn_share(src, strlen(src), hash);
9804 #if defined(MULTIPLICITY)
9806 /* pTHX_ magic can't cope with varargs, so this is a no-context
9807 * version of the main function, (which may itself be aliased to us).
9808 * Don't access this version directly.
9812 Perl_newSVpvf_nocontext(const char *const pat, ...)
9818 PERL_ARGS_ASSERT_NEWSVPVF_NOCONTEXT;
9820 va_start(args, pat);
9821 sv = vnewSVpvf(pat, &args);
9828 =for apidoc newSVpvf
9830 Creates a new SV and initializes it with the string formatted like
9833 =for apidoc newSVpvf_nocontext
9834 Like C<L</newSVpvf>> but does not take a thread context (C<aTHX>) parameter,
9835 so is used in situations where the caller doesn't already have the thread
9838 =for apidoc vnewSVpvf
9839 Like C<L</newSVpvf>> but the arguments are an encapsulated argument list.
9845 Perl_newSVpvf(pTHX_ const char *const pat, ...)
9850 PERL_ARGS_ASSERT_NEWSVPVF;
9852 va_start(args, pat);
9853 sv = vnewSVpvf(pat, &args);
9858 /* backend for newSVpvf() and newSVpvf_nocontext() */
9861 Perl_vnewSVpvf(pTHX_ const char *const pat, va_list *const args)
9865 PERL_ARGS_ASSERT_VNEWSVPVF;
9868 SvPVCLEAR_FRESH(sv);
9869 sv_vcatpvfn_flags(sv, pat, strlen(pat), args, NULL, 0, NULL, 0);
9876 Creates a new SV and copies a floating point value into it.
9877 The reference count for the SV is set to 1.
9883 Perl_newSVnv(pTHX_ const NV n)
9885 SV *sv = newSV_type(SVt_NV);
9897 Creates a new SV and copies an integer into it. The reference count for the
9904 Perl_newSViv(pTHX_ const IV i)
9906 SV *sv = newSV_type(SVt_IV);
9918 Creates a new SV and copies an unsigned integer into it.
9919 The reference count for the SV is set to 1.
9925 Perl_newSVuv(pTHX_ const UV u)
9929 /* Inlining ONLY the small relevant subset of sv_setuv here
9930 * for performance. Makes a significant difference. */
9932 /* Using ivs is more efficient than using uvs - see sv_setuv */
9933 if (u <= (UV)IV_MAX) {
9934 return newSViv((IV)u);
9939 /* We're starting from SVt_FIRST, so provided that's
9940 * actual 0, we don't have to unset any SV type flags
9941 * to promote to SVt_IV. */
9942 STATIC_ASSERT_STMT(SVt_FIRST == 0);
9944 SET_SVANY_FOR_BODYLESS_IV(sv);
9945 SvFLAGS(sv) |= SVt_IV;
9947 (void)SvIsUV_on(sv);
9956 =for apidoc newSVbool
9958 Creates a new SV boolean.
9964 Perl_newSVbool(pTHX_ bool bool_val)
9966 PERL_ARGS_ASSERT_NEWSVBOOL;
9967 SV *sv = newSVsv(bool_val ? &PL_sv_yes : &PL_sv_no);
9973 =for apidoc newSV_true
9975 Creates a new SV that is a boolean true.
9980 Perl_newSV_true(pTHX)
9982 PERL_ARGS_ASSERT_NEWSV_TRUE;
9983 SV *sv = newSVsv(&PL_sv_yes);
9989 =for apidoc newSV_false
9991 Creates a new SV that is a boolean false.
9997 Perl_newSV_false(pTHX)
9999 PERL_ARGS_ASSERT_NEWSV_FALSE;
10000 SV *sv = newSVsv(&PL_sv_no);
10005 /* newRV_inc is the official function name to use now.
10006 * newRV_inc is in fact #defined to newRV in sv.h
10010 Perl_newRV(pTHX_ SV *const sv)
10012 PERL_ARGS_ASSERT_NEWRV;
10014 return newRV_noinc(SvREFCNT_inc_simple_NN(sv));
10018 =for apidoc newSVsv
10019 =for apidoc_item newSVsv_flags
10020 =for apidoc_item newSVsv_nomg
10022 These create a new SV which is an exact duplicate of the original SV
10023 (using C<sv_setsv>.)
10025 They differ only in that C<newSVsv> performs 'get' magic; C<newSVsv_nomg> skips
10026 any magic; and C<newSVsv_flags> allows you to explicitly set a C<flags>
10033 Perl_newSVsv_flags(pTHX_ SV *const old, I32 flags)
10039 if (SvIS_FREED(old)) {
10040 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "semi-panic: attempt to dup freed string");
10043 /* Do this here, otherwise we leak the new SV if this croaks. */
10044 if (flags & SV_GMAGIC)
10047 sv_setsv_flags(sv, old, flags & ~SV_GMAGIC);
10052 =for apidoc sv_reset
10054 Underlying implementation for the C<reset> Perl function.
10055 Note that the perl-level function is vaguely deprecated.
10061 Perl_sv_reset(pTHX_ const char *s, HV *const stash)
10063 PERL_ARGS_ASSERT_SV_RESET;
10065 sv_resetpvn(*s ? s : NULL, strlen(s), stash);
10069 Perl_sv_resetpvn(pTHX_ const char *s, STRLEN len, HV * const stash)
10071 char todo[PERL_UCHAR_MAX+1];
10074 if (!stash || SvTYPE(stash) != SVt_PVHV)
10077 if (!s) { /* reset ?? searches */
10078 MAGIC * const mg = mg_find((const SV *)stash, PERL_MAGIC_symtab);
10079 if (mg && mg->mg_len) {
10080 const U32 count = mg->mg_len / sizeof(PMOP**);
10081 PMOP **pmp = (PMOP**) mg->mg_ptr;
10082 PMOP *const *const end = pmp + count;
10084 while (pmp < end) {
10085 #ifdef USE_ITHREADS
10086 SvREADONLY_off(PL_regex_pad[(*pmp)->op_pmoffset]);
10088 (*pmp)->op_pmflags &= ~PMf_USED;
10096 /* reset variables */
10098 if (!HvTOTALKEYS(stash))
10101 Zero(todo, 256, char);
10105 I32 i = (unsigned char)*s;
10109 max = (unsigned char)*s++;
10110 for ( ; i <= max; i++) {
10113 for (i = 0; i <= (I32) HvMAX(stash); i++) {
10115 for (entry = HvARRAY(stash)[i];
10117 entry = HeNEXT(entry))
10122 if (!todo[(U8)*HeKEY(entry)])
10124 gv = MUTABLE_GV(HeVAL(entry));
10128 if (sv && !SvREADONLY(sv)) {
10129 SV_CHECK_THINKFIRST_COW_DROP(sv);
10136 av_clear(GvAV(gv));
10138 if (GvHV(gv) && !HvHasNAME(GvHV(gv))) {
10139 hv_clear(GvHV(gv));
10149 Using various gambits, try to get an IO from an SV: the IO slot if its a
10150 GV; or the recursive result if we're an RV; or the IO slot of the symbol
10151 named after the PV if we're a string.
10153 'Get' magic is ignored on the C<sv> passed in, but will be called on
10154 C<SvRV(sv)> if C<sv> is an RV.
10160 Perl_sv_2io(pTHX_ SV *const sv)
10165 PERL_ARGS_ASSERT_SV_2IO;
10167 switch (SvTYPE(sv)) {
10169 io = MUTABLE_IO(sv);
10173 if (isGV_with_GP(sv)) {
10174 gv = MUTABLE_GV(sv);
10177 Perl_croak(aTHX_ "Bad filehandle: %" HEKf,
10178 HEKfARG(GvNAME_HEK(gv)));
10184 Perl_croak(aTHX_ PL_no_usym, "filehandle");
10186 SvGETMAGIC(SvRV(sv));
10187 return sv_2io(SvRV(sv));
10189 gv = gv_fetchsv_nomg(sv, 0, SVt_PVIO);
10196 if (SvGMAGICAL(sv)) {
10197 newsv = sv_newmortal();
10198 sv_setsv_nomg(newsv, sv);
10200 Perl_croak(aTHX_ "Bad filehandle: %" SVf, SVfARG(newsv));
10210 Using various gambits, try to get a CV from an SV; in addition, try if
10211 possible to set C<*st> and C<*gvp> to the stash and GV associated with it.
10212 The flags in C<lref> are passed to C<gv_fetchsv>.
10218 Perl_sv_2cv(pTHX_ SV *sv, HV **const st, GV **const gvp, const I32 lref)
10223 PERL_ARGS_ASSERT_SV_2CV;
10230 switch (SvTYPE(sv)) {
10234 return MUTABLE_CV(sv);
10244 sv = amagic_deref_call(sv, to_cv_amg);
10247 if (SvTYPE(sv) == SVt_PVCV) {
10248 cv = MUTABLE_CV(sv);
10253 else if(SvGETMAGIC(sv), isGV_with_GP(sv))
10254 gv = MUTABLE_GV(sv);
10256 Perl_croak(aTHX_ "Not a subroutine reference");
10258 else if (isGV_with_GP(sv)) {
10259 gv = MUTABLE_GV(sv);
10262 gv = gv_fetchsv_nomg(sv, lref, SVt_PVCV);
10269 /* Some flags to gv_fetchsv mean don't really create the GV */
10270 if (!isGV_with_GP(gv)) {
10274 *st = GvESTASH(gv);
10275 if (lref & ~GV_ADDMG && !GvCVu(gv)) {
10276 /* XXX this is probably not what they think they're getting.
10277 * It has the same effect as "sub name;", i.e. just a forward
10286 =for apidoc sv_true
10288 Returns true if the SV has a true value by Perl's rules.
10289 Use the C<SvTRUE> macro instead, which may call C<sv_true()> or may
10290 instead use an in-line version.
10296 Perl_sv_true(pTHX_ SV *const sv)
10301 const XPV* const tXpv = (XPV*)SvANY(sv);
10303 (tXpv->xpv_cur > 1 ||
10304 (tXpv->xpv_cur && *sv->sv_u.svu_pv != '0')))
10311 return SvIVX(sv) != 0;
10314 return SvNVX(sv) != 0.0;
10316 return sv_2bool(sv);
10322 =for apidoc sv_pvn_force
10324 Get a sensible string out of the SV somehow.
10325 A private implementation of the C<SvPV_force> macro for compilers which
10326 can't cope with complex macro expressions. Always use the macro instead.
10328 =for apidoc sv_pvn_force_flags
10330 Get a sensible string out of the SV somehow.
10331 If C<flags> has the C<SV_GMAGIC> bit set, will C<L</mg_get>> on C<sv> if
10332 appropriate, else not. C<sv_pvn_force> and C<sv_pvn_force_nomg> are
10333 implemented in terms of this function.
10334 You normally want to use the various wrapper macros instead: see
10335 C<L</SvPV_force>> and C<L</SvPV_force_nomg>>.
10341 Perl_sv_pvn_force_flags(pTHX_ SV *const sv, STRLEN *const lp, const U32 flags)
10343 PERL_ARGS_ASSERT_SV_PVN_FORCE_FLAGS;
10345 if (flags & SV_GMAGIC) SvGETMAGIC(sv);
10346 if (SvTHINKFIRST(sv) && (!SvROK(sv) || SvREADONLY(sv)))
10347 sv_force_normal_flags(sv, 0);
10357 if (SvTYPE(sv) > SVt_PVLV
10358 || isGV_with_GP(sv))
10359 /* diag_listed_as: Can't coerce %s to %s in %s */
10360 Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0),
10362 s = sv_2pv_flags(sv, &len, flags &~ SV_GMAGIC);
10369 if (SvTYPE(sv) < SVt_PV ||
10370 s != SvPVX_const(sv)) { /* Almost, but not quite, sv_setpvn() */
10373 SvUPGRADE(sv, SVt_PV); /* Never FALSE */
10374 SvGROW(sv, len + 1);
10375 Move(s,SvPVX(sv),len,char);
10376 SvCUR_set(sv, len);
10377 SvPVX(sv)[len] = '\0';
10380 SvPOK_on(sv); /* validate pointer */
10382 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2pv(%s)\n",
10383 PTR2UV(sv),SvPVX_const(sv)));
10386 (void)SvPOK_only_UTF8(sv);
10387 return SvPVX_mutable(sv);
10391 =for apidoc sv_pvbyten_force
10393 The backend for the C<SvPVbytex_force> macro. Always use the macro
10394 instead. If the SV cannot be downgraded from UTF-8, this croaks.
10400 Perl_sv_pvbyten_force(pTHX_ SV *const sv, STRLEN *const lp)
10402 PERL_ARGS_ASSERT_SV_PVBYTEN_FORCE;
10404 sv_pvn_force(sv,lp);
10405 (void)sv_utf8_downgrade(sv,0);
10411 =for apidoc sv_pvutf8n_force
10413 The backend for the C<SvPVutf8x_force> macro. Always use the macro
10420 Perl_sv_pvutf8n_force(pTHX_ SV *const sv, STRLEN *const lp)
10422 PERL_ARGS_ASSERT_SV_PVUTF8N_FORCE;
10424 sv_pvn_force(sv,0);
10425 sv_utf8_upgrade_nomg(sv);
10431 =for apidoc sv_reftype
10433 Returns a string describing what the SV is a reference to.
10435 If ob is true and the SV is blessed, the string is the class name,
10436 otherwise it is the type of the SV, "SCALAR", "ARRAY" etc.
10442 Perl_sv_reftype(pTHX_ const SV *const sv, const int ob)
10444 PERL_ARGS_ASSERT_SV_REFTYPE;
10445 if (ob && SvOBJECT(sv)) {
10446 return SvPV_nolen_const(sv_ref(NULL, sv, ob));
10449 /* WARNING - There is code, for instance in mg.c, that assumes that
10450 * the only reason that sv_reftype(sv,0) would return a string starting
10451 * with 'L' or 'S' is that it is a LVALUE or a SCALAR.
10452 * Yes this a dodgy way to do type checking, but it saves practically reimplementing
10453 * this routine inside other subs, and it saves time.
10454 * Do not change this assumption without searching for "dodgy type check" in
10457 switch (SvTYPE(sv)) {
10472 case SVt_PVLV: return (char *) (SvROK(sv) ? "REF"
10473 /* tied lvalues should appear to be
10474 * scalars for backwards compatibility */
10475 : (isALPHA_FOLD_EQ(LvTYPE(sv), 't'))
10476 ? "SCALAR" : "LVALUE");
10477 case SVt_PVAV: return "ARRAY";
10478 case SVt_PVHV: return "HASH";
10479 case SVt_PVCV: return "CODE";
10480 case SVt_PVGV: return (char *) (isGV_with_GP(sv)
10481 ? "GLOB" : "SCALAR");
10482 case SVt_PVFM: return "FORMAT";
10483 case SVt_PVIO: return "IO";
10484 case SVt_INVLIST: return "INVLIST";
10485 case SVt_REGEXP: return "REGEXP";
10486 case SVt_PVOBJ: return "OBJECT";
10487 default: return "UNKNOWN";
10495 Returns a SV describing what the SV passed in is a reference to.
10497 dst can be a SV to be set to the description or NULL, in which case a
10498 mortal SV is returned.
10500 If ob is true and the SV is blessed, the description is the class
10501 name, otherwise it is the type of the SV, "SCALAR", "ARRAY" etc.
10507 Perl_sv_ref(pTHX_ SV *dst, const SV *const sv, const int ob)
10509 PERL_ARGS_ASSERT_SV_REF;
10512 dst = sv_newmortal();
10514 if (ob && SvOBJECT(sv)) {
10515 if (HvHasNAME(SvSTASH(sv)))
10516 sv_sethek(dst, HvNAME_HEK(SvSTASH(sv)));
10518 sv_setpvs(dst, "__ANON__");
10521 const char * reftype = sv_reftype(sv, 0);
10522 sv_setpv(dst, reftype);
10528 =for apidoc sv_isobject
10530 Returns a boolean indicating whether the SV is an RV pointing to a blessed
10531 object. If the SV is not an RV, or if the object is not blessed, then this
10538 Perl_sv_isobject(pTHX_ SV *sv)
10554 Returns a boolean indicating whether the SV is blessed into the specified
10557 This does not check for subtypes or method overloading. Use C<sv_isa_sv> to
10558 verify an inheritance relationship in the same way as the C<isa> operator by
10559 respecting any C<isa()> method overloading; or C<sv_derived_from_sv> to test
10560 directly on the actual object type.
10566 Perl_sv_isa(pTHX_ SV *sv, const char *const name)
10568 const char *hvname;
10570 PERL_ARGS_ASSERT_SV_ISA;
10580 hvname = HvNAME_get(SvSTASH(sv));
10584 return strEQ(hvname, name);
10588 =for apidoc newSVrv
10590 Creates a new SV for the existing RV, C<rv>, to point to. If C<rv> is not an
10591 RV then it will be upgraded to one. If C<classname> is non-null then the new
10592 SV will be blessed in the specified package. The new SV is returned and its
10593 reference count is 1. The reference count 1 is owned by C<rv>. See also
10594 newRV_inc() and newRV_noinc() for creating a new RV properly.
10600 Perl_newSVrv(pTHX_ SV *const rv, const char *const classname)
10604 PERL_ARGS_ASSERT_NEWSVRV;
10608 SV_CHECK_THINKFIRST_COW_DROP(rv);
10610 if (UNLIKELY( SvTYPE(rv) >= SVt_PVMG )) {
10611 const U32 refcnt = SvREFCNT(rv);
10615 SvREFCNT(rv) = refcnt;
10617 sv_upgrade(rv, SVt_IV);
10618 } else if (SvROK(rv)) {
10619 SvREFCNT_dec(SvRV(rv));
10621 prepare_SV_for_RV(rv);
10629 HV* const stash = gv_stashpv(classname, GV_ADD);
10630 (void)sv_bless(rv, stash);
10636 Perl_newSVavdefelem(pTHX_ AV *av, SSize_t ix, bool extendible)
10638 SV * const lv = newSV_type(SVt_PVLV);
10639 PERL_ARGS_ASSERT_NEWSVAVDEFELEM;
10641 sv_magic(lv, NULL, PERL_MAGIC_defelem, NULL, 0);
10642 LvTARG(lv) = SvREFCNT_inc_simple_NN(av);
10643 LvSTARGOFF(lv) = ix;
10644 LvTARGLEN(lv) = extendible ? 1 : (STRLEN)UV_MAX;
10649 =for apidoc sv_setref_pv
10651 Copies a pointer into a new SV, optionally blessing the SV. The C<rv>
10652 argument will be upgraded to an RV. That RV will be modified to point to
10653 the new SV. If the C<pv> argument is C<NULL>, then C<PL_sv_undef> will be placed
10654 into the SV. The C<classname> argument indicates the package for the
10655 blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
10656 will have a reference count of 1, and the RV will be returned.
10658 Do not use with other Perl types such as HV, AV, SV, CV, because those
10659 objects will become corrupted by the pointer copy process.
10661 Note that C<sv_setref_pvn> copies the string while this copies the pointer.
10667 Perl_sv_setref_pv(pTHX_ SV *const rv, const char *const classname, void *const pv)
10669 PERL_ARGS_ASSERT_SV_SETREF_PV;
10676 sv_setiv(newSVrv(rv,classname), PTR2IV(pv));
10681 =for apidoc sv_setref_iv
10683 Copies an integer into a new SV, optionally blessing the SV. The C<rv>
10684 argument will be upgraded to an RV. That RV will be modified to point to
10685 the new SV. The C<classname> argument indicates the package for the
10686 blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
10687 will have a reference count of 1, and the RV will be returned.
10693 Perl_sv_setref_iv(pTHX_ SV *const rv, const char *const classname, const IV iv)
10695 PERL_ARGS_ASSERT_SV_SETREF_IV;
10697 sv_setiv(newSVrv(rv,classname), iv);
10702 =for apidoc sv_setref_uv
10704 Copies an unsigned integer into a new SV, optionally blessing the SV. The C<rv>
10705 argument will be upgraded to an RV. That RV will be modified to point to
10706 the new SV. The C<classname> argument indicates the package for the
10707 blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
10708 will have a reference count of 1, and the RV will be returned.
10714 Perl_sv_setref_uv(pTHX_ SV *const rv, const char *const classname, const UV uv)
10716 PERL_ARGS_ASSERT_SV_SETREF_UV;
10718 sv_setuv(newSVrv(rv,classname), uv);
10723 =for apidoc sv_setref_nv
10725 Copies a double into a new SV, optionally blessing the SV. The C<rv>
10726 argument will be upgraded to an RV. That RV will be modified to point to
10727 the new SV. The C<classname> argument indicates the package for the
10728 blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
10729 will have a reference count of 1, and the RV will be returned.
10735 Perl_sv_setref_nv(pTHX_ SV *const rv, const char *const classname, const NV nv)
10737 PERL_ARGS_ASSERT_SV_SETREF_NV;
10739 sv_setnv(newSVrv(rv,classname), nv);
10744 =for apidoc sv_setref_pvn
10746 Copies a string into a new SV, optionally blessing the SV. The length of the
10747 string must be specified with C<n>. The C<rv> argument will be upgraded to
10748 an RV. That RV will be modified to point to the new SV. The C<classname>
10749 argument indicates the package for the blessing. Set C<classname> to
10750 C<NULL> to avoid the blessing. The new SV will have a reference count
10751 of 1, and the RV will be returned.
10753 Note that C<sv_setref_pv> copies the pointer while this copies the string.
10759 Perl_sv_setref_pvn(pTHX_ SV *const rv, const char *const classname,
10760 const char *const pv, const STRLEN n)
10762 PERL_ARGS_ASSERT_SV_SETREF_PVN;
10764 sv_setpvn(newSVrv(rv,classname), pv, n);
10769 =for apidoc sv_bless
10771 Blesses an SV into a specified package. The SV must be an RV. The package
10772 must be designated by its stash (see C<L</gv_stashpv>>). The reference count
10773 of the SV is unaffected.
10779 Perl_sv_bless(pTHX_ SV *const sv, HV *const stash)
10782 HV *oldstash = NULL;
10784 PERL_ARGS_ASSERT_SV_BLESS;
10788 Perl_croak(aTHX_ "Can't bless non-reference value");
10789 if (HvSTASH_IS_CLASS(stash))
10790 Perl_croak(aTHX_ "Attempt to bless into a class");
10793 if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY|SVf_PROTECT)) {
10794 if (SvREADONLY(tmpRef))
10795 Perl_croak_no_modify();
10796 if (SvTYPE(tmpRef) == SVt_PVOBJ)
10797 Perl_croak(aTHX_ "Can't bless an object reference");
10798 if (SvOBJECT(tmpRef)) {
10799 oldstash = SvSTASH(tmpRef);
10802 SvOBJECT_on(tmpRef);
10803 SvUPGRADE(tmpRef, SVt_PVMG);
10804 SvSTASH_set(tmpRef, MUTABLE_HV(SvREFCNT_inc_simple(stash)));
10805 SvREFCNT_dec(oldstash);
10807 if(SvSMAGICAL(tmpRef))
10808 if(mg_find(tmpRef, PERL_MAGIC_ext) || mg_find(tmpRef, PERL_MAGIC_uvar))
10816 /* Downgrades a PVGV to a PVMG. If it's actually a PVLV, we leave the type
10817 * as it is after unglobbing it.
10820 PERL_STATIC_INLINE void
10821 S_sv_unglob(pTHX_ SV *const sv, U32 flags)
10825 SV * const temp = flags & SV_COW_DROP_PV ? NULL : sv_newmortal();
10827 PERL_ARGS_ASSERT_SV_UNGLOB;
10829 assert(SvTYPE(sv) == SVt_PVGV || SvTYPE(sv) == SVt_PVLV);
10831 if (!(flags & SV_COW_DROP_PV))
10832 gv_efullname3(temp, MUTABLE_GV(sv), "*");
10834 SvREFCNT_inc_simple_void_NN(sv_2mortal(sv));
10836 if(GvCVu((const GV *)sv) && (stash = GvSTASH(MUTABLE_GV(sv)))
10837 && HvHasNAME(stash))
10838 mro_method_changed_in(stash);
10839 gp_free(MUTABLE_GV(sv));
10842 sv_del_backref(MUTABLE_SV(GvSTASH(sv)), sv);
10843 GvSTASH(sv) = NULL;
10846 if (GvNAME_HEK(sv)) {
10847 unshare_hek(GvNAME_HEK(sv));
10849 isGV_with_GP_off(sv);
10851 if(SvTYPE(sv) == SVt_PVGV) {
10852 /* need to keep SvANY(sv) in the right arena */
10853 xpvmg = new_XPVMG();
10854 StructCopy(SvANY(sv), xpvmg, XPVMG);
10855 del_body_by_type(SvANY(sv), SVt_PVGV);
10858 SvFLAGS(sv) &= ~SVTYPEMASK;
10859 SvFLAGS(sv) |= SVt_PVMG;
10862 /* Intentionally not calling any local SET magic, as this isn't so much a
10863 set operation as merely an internal storage change. */
10864 if (flags & SV_COW_DROP_PV) SvOK_off(sv);
10865 else sv_setsv_flags(sv, temp, 0);
10867 if ((const GV *)sv == PL_last_in_gv)
10868 PL_last_in_gv = NULL;
10869 else if ((const GV *)sv == PL_statgv)
10874 =for apidoc sv_unref_flags
10876 Unsets the RV status of the SV, and decrements the reference count of
10877 whatever was being referenced by the RV. This can almost be thought of
10878 as a reversal of C<newSVrv>. The C<cflags> argument can contain
10879 C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented
10880 (otherwise the decrementing is conditional on the reference count being
10881 different from one or the reference being a readonly SV).
10882 See C<L</SvROK_off>>.
10884 =for apidoc Amnh||SV_IMMEDIATE_UNREF
10890 Perl_sv_unref_flags(pTHX_ SV *const ref, const U32 flags)
10892 SV* const target = SvRV(ref);
10894 PERL_ARGS_ASSERT_SV_UNREF_FLAGS;
10896 if (SvWEAKREF(ref)) {
10897 sv_del_backref(target, ref);
10898 SvWEAKREF_off(ref);
10899 SvRV_set(ref, NULL);
10902 SvRV_set(ref, NULL);
10904 /* You can't have a || SvREADONLY(target) here, as $a = $$a, where $a was
10905 assigned to as BEGIN {$a = \"Foo"} will fail. */
10906 if (SvREFCNT(target) != 1 || (flags & SV_IMMEDIATE_UNREF))
10907 SvREFCNT_dec_NN(target);
10908 else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */
10909 sv_2mortal(target); /* Schedule for freeing later */
10913 =for apidoc sv_untaint
10915 Untaint an SV. Use C<SvTAINTED_off> instead.
10921 Perl_sv_untaint(pTHX_ SV *const sv)
10923 PERL_ARGS_ASSERT_SV_UNTAINT;
10924 PERL_UNUSED_CONTEXT;
10926 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
10927 MAGIC * const mg = mg_find(sv, PERL_MAGIC_taint);
10934 =for apidoc sv_tainted
10936 Test an SV for taintedness. Use C<SvTAINTED> instead.
10942 Perl_sv_tainted(pTHX_ SV *const sv)
10944 PERL_ARGS_ASSERT_SV_TAINTED;
10945 PERL_UNUSED_CONTEXT;
10947 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
10948 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_taint);
10949 if (mg && (mg->mg_len & 1) )
10955 #if defined(MULTIPLICITY)
10957 /* pTHX_ magic can't cope with varargs, so this is a no-context
10958 * version of the main function, (which may itself be aliased to us).
10959 * Don't access this version directly.
10963 Perl_sv_setpvf_nocontext(SV *const sv, const char *const pat, ...)
10968 PERL_ARGS_ASSERT_SV_SETPVF_NOCONTEXT;
10970 va_start(args, pat);
10971 sv_vsetpvf(sv, pat, &args);
10975 /* pTHX_ magic can't cope with varargs, so this is a no-context
10976 * version of the main function, (which may itself be aliased to us).
10977 * Don't access this version directly.
10981 Perl_sv_setpvf_mg_nocontext(SV *const sv, const char *const pat, ...)
10986 PERL_ARGS_ASSERT_SV_SETPVF_MG_NOCONTEXT;
10988 va_start(args, pat);
10989 sv_vsetpvf_mg(sv, pat, &args);
10995 =for apidoc sv_setpvf
10996 =for apidoc_item sv_setpvf_mg
10997 =for apidoc_item sv_setpvf_mg_nocontext
10998 =for apidoc_item sv_setpvf_nocontext
11000 These work like C<L</sv_catpvf>> but copy the text into the SV instead of
11003 The differences between these are:
11005 C<sv_setpvf_mg> and C<sv_setpvf_mg_nocontext> perform 'set' magic; C<sv_setpvf>
11006 and C<sv_setpvf_nocontext> skip all magic.
11008 C<sv_setpvf_nocontext> and C<sv_setpvf_mg_nocontext> do not take a thread
11009 context (C<aTHX>) parameter, so are used in situations where the caller
11010 doesn't already have the thread context.
11016 Perl_sv_setpvf(pTHX_ SV *const sv, const char *const pat, ...)
11020 PERL_ARGS_ASSERT_SV_SETPVF;
11022 va_start(args, pat);
11023 sv_vsetpvf(sv, pat, &args);
11028 =for apidoc sv_vsetpvf
11029 =for apidoc_item sv_vsetpvf_mg
11031 These work like C<L</sv_vcatpvf>> but copy the text into the SV instead of
11034 They differ only in that C<sv_vsetpvf_mg> performs 'set' magic;
11035 C<sv_vsetpvf> skips all magic.
11037 They are usually used via their frontends, C<L</sv_setpvf>> and
11038 C<L</sv_setpvf_mg>>.
11044 Perl_sv_vsetpvf(pTHX_ SV *const sv, const char *const pat, va_list *const args)
11046 PERL_ARGS_ASSERT_SV_VSETPVF;
11048 sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
11052 Perl_sv_setpvf_mg(pTHX_ SV *const sv, const char *const pat, ...)
11056 PERL_ARGS_ASSERT_SV_SETPVF_MG;
11058 va_start(args, pat);
11059 sv_vsetpvf_mg(sv, pat, &args);
11064 Perl_sv_vsetpvf_mg(pTHX_ SV *const sv, const char *const pat, va_list *const args)
11066 PERL_ARGS_ASSERT_SV_VSETPVF_MG;
11068 sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
11072 #if defined(MULTIPLICITY)
11074 /* pTHX_ magic can't cope with varargs, so this is a no-context
11075 * version of the main function, (which may itself be aliased to us).
11076 * Don't access this version directly.
11080 Perl_sv_catpvf_nocontext(SV *const sv, const char *const pat, ...)
11085 PERL_ARGS_ASSERT_SV_CATPVF_NOCONTEXT;
11087 va_start(args, pat);
11088 sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11092 /* pTHX_ magic can't cope with varargs, so this is a no-context
11093 * version of the main function, (which may itself be aliased to us).
11094 * Don't access this version directly.
11098 Perl_sv_catpvf_mg_nocontext(SV *const sv, const char *const pat, ...)
11103 PERL_ARGS_ASSERT_SV_CATPVF_MG_NOCONTEXT;
11105 va_start(args, pat);
11106 sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11113 =for apidoc sv_catpvf
11114 =for apidoc_item sv_catpvf_mg
11115 =for apidoc_item sv_catpvf_mg_nocontext
11116 =for apidoc_item sv_catpvf_nocontext
11118 These process their arguments like C<sprintf>, and append the formatted
11119 output to an SV. As with C<sv_vcatpvfn>, argument reordering is not supporte
11120 when called with a non-null C-style variable argument list.
11122 If the appended data contains "wide" characters
11123 (including, but not limited to, SVs with a UTF-8 PV formatted with C<%s>,
11124 and characters >255 formatted with C<%c>), the original SV might get
11127 If the original SV was UTF-8, the pattern should be
11128 valid UTF-8; if the original SV was bytes, the pattern should be too.
11130 All perform 'get' magic, but only C<sv_catpvf_mg> and C<sv_catpvf_mg_nocontext>
11131 perform 'set' magic.
11133 C<sv_catpvf_nocontext> and C<sv_catpvf_mg_nocontext> do not take a thread
11134 context (C<aTHX>) parameter, so are used in situations where the caller
11135 doesn't already have the thread context.
11141 Perl_sv_catpvf(pTHX_ SV *const sv, const char *const pat, ...)
11145 PERL_ARGS_ASSERT_SV_CATPVF;
11147 va_start(args, pat);
11148 sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11153 =for apidoc sv_vcatpvf
11154 =for apidoc_item sv_vcatpvf_mg
11156 These process their arguments like C<sv_vcatpvfn> called with a non-null
11157 C-style variable argument list, and append the formatted output to C<sv>.
11159 They differ only in that C<sv_vcatpvf_mg> performs 'set' magic;
11160 C<sv_vcatpvf> skips 'set' magic.
11162 Both perform 'get' magic.
11164 They are usually accessed via their frontends C<L</sv_catpvf>> and
11165 C<L</sv_catpvf_mg>>.
11171 Perl_sv_vcatpvf(pTHX_ SV *const sv, const char *const pat, va_list *const args)
11173 PERL_ARGS_ASSERT_SV_VCATPVF;
11175 sv_vcatpvfn_flags(sv, pat, strlen(pat), args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11179 Perl_sv_catpvf_mg(pTHX_ SV *const sv, const char *const pat, ...)
11183 PERL_ARGS_ASSERT_SV_CATPVF_MG;
11185 va_start(args, pat);
11186 sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11192 Perl_sv_vcatpvf_mg(pTHX_ SV *const sv, const char *const pat, va_list *const args)
11194 PERL_ARGS_ASSERT_SV_VCATPVF_MG;
11196 sv_vcatpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
11201 =for apidoc sv_vsetpvfn
11203 Works like C<sv_vcatpvfn> but copies the text into the SV instead of
11206 Usually used via one of its frontends L</C<sv_vsetpvf>> and
11207 L</C<sv_vsetpvf_mg>>.
11213 Perl_sv_vsetpvfn(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
11214 va_list *const args, SV **const svargs, const Size_t sv_count, bool *const maybe_tainted)
11216 PERL_ARGS_ASSERT_SV_VSETPVFN;
11219 sv_vcatpvfn_flags(sv, pat, patlen, args, svargs, sv_count, maybe_tainted, 0);
11223 /* simplified inline Perl_sv_catpvn_nomg() when you know the SV's SvPOK */
11225 PERL_STATIC_INLINE void
11226 S_sv_catpvn_simple(pTHX_ SV *const sv, const char* const buf, const STRLEN len)
11228 STRLEN const need = len + SvCUR(sv) + 1;
11231 /* can't wrap as both len and SvCUR() are allocated in
11232 * memory and together can't consume all the address space
11234 assert(need > len);
11239 Copy(buf, end, len, char);
11242 SvCUR_set(sv, need - 1);
11247 * Warn of missing argument to sprintf. The value used in place of such
11248 * arguments should be &PL_sv_no; an undefined value would yield
11249 * inappropriate "use of uninit" warnings [perl #71000].
11252 S_warn_vcatpvfn_missing_argument(pTHX) {
11253 if (ckWARN(WARN_MISSING)) {
11254 Perl_warner(aTHX_ packWARN(WARN_MISSING), "Missing argument in %s",
11255 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
11264 Perl_croak(aTHX_ "Integer overflow in format string for %s",
11265 (PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn"));
11269 /* Given an int i from the next arg (if args is true) or an sv from an arg
11270 * (if args is false), try to extract a STRLEN-ranged value from the arg,
11271 * with overflow checking.
11272 * Sets *neg to true if the value was negative (untouched otherwise.
11273 * Returns the absolute value.
11274 * As an extra margin of safety, it croaks if the returned value would
11275 * exceed the maximum value of a STRLEN / 4.
11279 S_sprintf_arg_num_val(pTHX_ va_list *const args, int i, SV *sv, bool *neg)
11293 if (UNLIKELY(SvIsUV(sv))) {
11294 UV uv = SvUV_nomg(sv);
11296 S_croak_overflow();
11300 iv = SvIV_nomg(sv);
11304 S_croak_overflow();
11310 if (iv > (IV)(((STRLEN)~0) / 4))
11311 S_croak_overflow();
11316 /* Read in and return a number. Updates *pattern to point to the char
11317 * following the number. Expects the first char to 1..9.
11318 * Croaks if the number exceeds 1/4 of the maximum value of STRLEN.
11319 * This is a belt-and-braces safety measure to complement any
11320 * overflow/wrap checks done in the main body of sv_vcatpvfn_flags.
11321 * It means that e.g. on a 32-bit system the width/precision can't be more
11322 * than 1G, which seems reasonable.
11326 S_expect_number(pTHX_ const char **const pattern)
11330 PERL_ARGS_ASSERT_EXPECT_NUMBER;
11332 assert(inRANGE(**pattern, '1', '9'));
11334 var = *(*pattern)++ - '0';
11335 while (isDIGIT(**pattern)) {
11336 /* if var * 10 + 9 would exceed 1/4 max strlen, croak */
11337 if (var > ((((STRLEN)~0) / 4 - 9) / 10))
11338 S_croak_overflow();
11339 var = var * 10 + (*(*pattern)++ - '0');
11344 /* Implement a fast "%.0f": given a pointer to the end of a buffer (caller
11345 * ensures it's big enough), back fill it with the rounded integer part of
11346 * nv. Returns ptr to start of string, and sets *len to its length.
11347 * Returns NULL if not convertible.
11351 S_F0convert(NV nv, char *const endbuf, STRLEN *const len)
11353 const int neg = nv < 0;
11356 PERL_ARGS_ASSERT_F0CONVERT;
11358 assert(!Perl_isinfnan(nv));
11361 if (nv != 0.0 && nv < (NV) UV_MAX) {
11367 if (uv & 1 && uv == nv)
11368 uv--; /* Round to even */
11371 const unsigned dig = uv % 10;
11373 } while (uv /= 10);
11383 /* XXX maybe_tainted is never assigned to, so the doc above is lying. */
11386 Perl_sv_vcatpvfn(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
11387 va_list *const args, SV **const svargs, const Size_t sv_count, bool *const maybe_tainted)
11389 PERL_ARGS_ASSERT_SV_VCATPVFN;
11391 sv_vcatpvfn_flags(sv, pat, patlen, args, svargs, sv_count, maybe_tainted, SV_GMAGIC|SV_SMAGIC);
11395 /* For the vcatpvfn code, we need a long double target in case
11396 * HAS_LONG_DOUBLE, even without USE_LONG_DOUBLE, so that we can printf
11397 * with long double formats, even without NV being long double. But we
11398 * call the target 'fv' instead of 'nv', since most of the time it is not
11399 * (most compilers these days recognize "long double", even if only as a
11400 * synonym for "double").
11402 #if defined(HAS_LONG_DOUBLE) && LONG_DOUBLESIZE > DOUBLESIZE && \
11403 defined(PERL_PRIgldbl) && !defined(USE_QUADMATH)
11404 # define VCATPVFN_FV_GF PERL_PRIgldbl
11405 # if defined(__VMS) && defined(__ia64) && defined(__IEEE_FLOAT)
11406 /* Work around breakage in OTS$CVT_FLOAT_T_X */
11407 # define VCATPVFN_NV_TO_FV(nv,fv) \
11410 fv = Perl_isnan(_dv) ? LDBL_QNAN : _dv; \
11413 # define VCATPVFN_NV_TO_FV(nv,fv) (fv)=(nv)
11415 typedef long double vcatpvfn_long_double_t;
11417 # define VCATPVFN_FV_GF NVgf
11418 # define VCATPVFN_NV_TO_FV(nv,fv) (fv)=(nv)
11419 typedef NV vcatpvfn_long_double_t;
11422 #ifdef LONGDOUBLE_DOUBLEDOUBLE
11423 /* The first double can be as large as 2**1023, or '1' x '0' x 1023.
11424 * The second double can be as small as 2**-1074, or '0' x 1073 . '1'.
11425 * The sum of them can be '1' . '0' x 2096 . '1', with implied radix point
11426 * after the first 1023 zero bits.
11428 * XXX The 2098 is quite large (262.25 bytes) and therefore some sort
11429 * of dynamically growing buffer might be better, start at just 16 bytes
11430 * (for example) and grow only when necessary. Or maybe just by looking
11431 * at the exponents of the two doubles? */
11432 # define DOUBLEDOUBLE_MAXBITS 2098
11435 /* vhex will contain the values (0..15) of the hex digits ("nybbles"
11436 * of 4 bits); 1 for the implicit 1, and the mantissa bits, four bits
11437 * per xdigit. For the double-double case, this can be rather many.
11438 * The non-double-double-long-double overshoots since all bits of NV
11439 * are not mantissa bits, there are also exponent bits. */
11440 #ifdef LONGDOUBLE_DOUBLEDOUBLE
11441 # define VHEX_SIZE (3+DOUBLEDOUBLE_MAXBITS/4)
11443 # define VHEX_SIZE (1+(NVSIZE * 8)/4)
11446 /* If we do not have a known long double format, (including not using
11447 * long doubles, or long doubles being equal to doubles) then we will
11448 * fall back to the ldexp/frexp route, with which we can retrieve at
11449 * most as many bits as our widest unsigned integer type is. We try
11450 * to get a 64-bit unsigned integer even if we are not using a 64-bit UV.
11452 * (If you want to test the case of UVSIZE == 4, NVSIZE == 8,
11453 * set the MANTISSATYPE to int and the MANTISSASIZE to 4.)
11455 #if defined(HAS_QUAD) && defined(Uquad_t)
11456 # define MANTISSATYPE Uquad_t
11457 # define MANTISSASIZE 8
11459 # define MANTISSATYPE UV
11460 # define MANTISSASIZE UVSIZE
11463 #if defined(DOUBLE_LITTLE_ENDIAN) || defined(LONGDOUBLE_LITTLE_ENDIAN)
11464 # define HEXTRACT_LITTLE_ENDIAN
11465 #elif defined(DOUBLE_BIG_ENDIAN) || defined(LONGDOUBLE_BIG_ENDIAN)
11466 # define HEXTRACT_BIG_ENDIAN
11468 # define HEXTRACT_MIX_ENDIAN
11471 /* S_hextract() is a helper for S_format_hexfp, for extracting
11472 * the hexadecimal values (for %a/%A). The nv is the NV where the value
11473 * are being extracted from (either directly from the long double in-memory
11474 * presentation, or from the uquad computed via frexp+ldexp). frexp also
11475 * is used to update the exponent. The subnormal is set to true
11476 * for IEEE 754 subnormals/denormals (including the x86 80-bit format).
11477 * The vhex is the pointer to the beginning of the output buffer of VHEX_SIZE.
11479 * The tricky part is that S_hextract() needs to be called twice:
11480 * the first time with vend as NULL, and the second time with vend as
11481 * the pointer returned by the first call. What happens is that on
11482 * the first round the output size is computed, and the intended
11483 * extraction sanity checked. On the second round the actual output
11484 * (the extraction of the hexadecimal values) takes place.
11485 * Sanity failures cause fatal failures during both rounds. */
11487 S_hextract(pTHX_ const NV nv, int* exponent, bool *subnormal,
11488 U8* vhex, U8* vend)
11492 int ixmin = 0, ixmax = 0;
11494 /* XXX Inf/NaN are not handled here, since it is
11495 * assumed they are to be output as "Inf" and "NaN". */
11497 /* These macros are just to reduce typos, they have multiple
11498 * repetitions below, but usually only one (or sometimes two)
11499 * of them is really being used. */
11500 /* HEXTRACT_OUTPUT() extracts the high nybble first. */
11501 #define HEXTRACT_OUTPUT_HI(ix) (*v++ = nvp[ix] >> 4)
11502 #define HEXTRACT_OUTPUT_LO(ix) (*v++ = nvp[ix] & 0xF)
11503 #define HEXTRACT_OUTPUT(ix) \
11505 HEXTRACT_OUTPUT_HI(ix); HEXTRACT_OUTPUT_LO(ix); \
11507 #define HEXTRACT_COUNT(ix, c) \
11509 v += c; if (ix < ixmin) ixmin = ix; else if (ix > ixmax) ixmax = ix; \
11511 #define HEXTRACT_BYTE(ix) \
11513 if (vend) HEXTRACT_OUTPUT(ix); else HEXTRACT_COUNT(ix, 2); \
11515 #define HEXTRACT_LO_NYBBLE(ix) \
11517 if (vend) HEXTRACT_OUTPUT_LO(ix); else HEXTRACT_COUNT(ix, 1); \
11519 /* HEXTRACT_TOP_NYBBLE is just convenience disguise,
11520 * to make it look less odd when the top bits of a NV
11521 * are extracted using HEXTRACT_LO_NYBBLE: the highest
11522 * order bits can be in the "low nybble" of a byte. */
11523 #define HEXTRACT_TOP_NYBBLE(ix) HEXTRACT_LO_NYBBLE(ix)
11524 #define HEXTRACT_BYTES_LE(a, b) \
11525 for (ix = a; ix >= b; ix--) { HEXTRACT_BYTE(ix); }
11526 #define HEXTRACT_BYTES_BE(a, b) \
11527 for (ix = a; ix <= b; ix++) { HEXTRACT_BYTE(ix); }
11528 #define HEXTRACT_GET_SUBNORMAL(nv) *subnormal = Perl_fp_class_denorm(nv)
11529 #define HEXTRACT_IMPLICIT_BIT(nv) \
11531 if (!*subnormal) { \
11532 if (vend) *v++ = ((nv) == 0.0) ? 0 : 1; else v++; \
11536 /* Most formats do. Those which don't should undef this.
11538 * But also note that IEEE 754 subnormals do not have it, or,
11539 * expressed alternatively, their implicit bit is zero. */
11540 #define HEXTRACT_HAS_IMPLICIT_BIT
11542 /* Many formats do. Those which don't should undef this. */
11543 #define HEXTRACT_HAS_TOP_NYBBLE
11545 /* HEXTRACTSIZE is the maximum number of xdigits. */
11546 #if defined(USE_LONG_DOUBLE) && defined(LONGDOUBLE_DOUBLEDOUBLE)
11547 # define HEXTRACTSIZE (2+DOUBLEDOUBLE_MAXBITS/4)
11549 # define HEXTRACTSIZE 2 * NVSIZE
11552 const U8* vmaxend = vhex + HEXTRACTSIZE;
11554 assert(HEXTRACTSIZE <= VHEX_SIZE);
11556 PERL_UNUSED_VAR(ix); /* might happen */
11557 (void)Perl_frexp(PERL_ABS(nv), exponent);
11558 *subnormal = FALSE;
11559 if (vend && (vend <= vhex || vend > vmaxend)) {
11560 /* diag_listed_as: Hexadecimal float: internal error (%s) */
11561 Perl_croak(aTHX_ "Hexadecimal float: internal error (entry)");
11564 /* First check if using long doubles. */
11565 #if defined(USE_LONG_DOUBLE) && (NVSIZE > DOUBLESIZE)
11566 # if LONG_DOUBLEKIND == LONG_DOUBLE_IS_IEEE_754_128_BIT_LITTLE_ENDIAN
11567 /* Used in e.g. VMS and HP-UX IA-64, e.g. -0.1L:
11568 * 9a 99 99 99 99 99 99 99 99 99 99 99 99 99 fb bf */
11569 /* The bytes 13..0 are the mantissa/fraction,
11570 * the 15,14 are the sign+exponent. */
11571 const U8* nvp = (const U8*)(&nv);
11572 HEXTRACT_GET_SUBNORMAL(nv);
11573 HEXTRACT_IMPLICIT_BIT(nv);
11574 # undef HEXTRACT_HAS_TOP_NYBBLE
11575 HEXTRACT_BYTES_LE(13, 0);
11576 # elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_IEEE_754_128_BIT_BIG_ENDIAN
11577 /* Used in e.g. Solaris Sparc and HP-UX PA-RISC, e.g. -0.1L:
11578 * bf fb 99 99 99 99 99 99 99 99 99 99 99 99 99 9a */
11579 /* The bytes 2..15 are the mantissa/fraction,
11580 * the 0,1 are the sign+exponent. */
11581 const U8* nvp = (const U8*)(&nv);
11582 HEXTRACT_GET_SUBNORMAL(nv);
11583 HEXTRACT_IMPLICIT_BIT(nv);
11584 # undef HEXTRACT_HAS_TOP_NYBBLE
11585 HEXTRACT_BYTES_BE(2, 15);
11586 # elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_LITTLE_ENDIAN
11587 /* x86 80-bit "extended precision", 64 bits of mantissa / fraction /
11588 * significand, 15 bits of exponent, 1 bit of sign. No implicit bit.
11589 * NVSIZE can be either 12 (ILP32, Solaris x86) or 16 (LP64, Linux
11590 * and OS X), meaning that 2 or 6 bytes are empty padding. */
11591 /* The bytes 0..1 are the sign+exponent,
11592 * the bytes 2..9 are the mantissa/fraction. */
11593 const U8* nvp = (const U8*)(&nv);
11594 # undef HEXTRACT_HAS_IMPLICIT_BIT
11595 # undef HEXTRACT_HAS_TOP_NYBBLE
11596 HEXTRACT_GET_SUBNORMAL(nv);
11597 HEXTRACT_BYTES_LE(7, 0);
11598 # elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_BIG_ENDIAN
11599 /* Does this format ever happen? (Wikipedia says the Motorola
11600 * 6888x math coprocessors used format _like_ this but padded
11601 * to 96 bits with 16 unused bits between the exponent and the
11603 const U8* nvp = (const U8*)(&nv);
11604 # undef HEXTRACT_HAS_IMPLICIT_BIT
11605 # undef HEXTRACT_HAS_TOP_NYBBLE
11606 HEXTRACT_GET_SUBNORMAL(nv);
11607 HEXTRACT_BYTES_BE(0, 7);
11609 # define HEXTRACT_FALLBACK
11610 /* Double-double format: two doubles next to each other.
11611 * The first double is the high-order one, exactly like
11612 * it would be for a "lone" double. The second double
11613 * is shifted down using the exponent so that that there
11614 * are no common bits. The tricky part is that the value
11615 * of the double-double is the SUM of the two doubles and
11616 * the second one can be also NEGATIVE.
11618 * Because of this tricky construction the bytewise extraction we
11619 * use for the other long double formats doesn't work, we must
11620 * extract the values bit by bit.
11622 * The little-endian double-double is used .. somewhere?
11624 * The big endian double-double is used in e.g. PPC/Power (AIX)
11627 * The mantissa bits are in two separate stretches, e.g. for -0.1L:
11628 * 9a 99 99 99 99 99 59 bc 9a 99 99 99 99 99 b9 3f (LE)
11629 * 3f b9 99 99 99 99 99 9a bc 59 99 99 99 99 99 9a (BE)
11632 #else /* #if defined(USE_LONG_DOUBLE) && (NVSIZE > DOUBLESIZE) */
11633 /* Using normal doubles, not long doubles.
11635 * We generate 4-bit xdigits (nybble/nibble) instead of 8-bit
11636 * bytes, since we might need to handle printf precision, and
11637 * also need to insert the radix. */
11639 # ifdef HEXTRACT_LITTLE_ENDIAN
11640 /* 0 1 2 3 4 5 6 7 (MSB = 7, LSB = 0, 6+7 = exponent+sign) */
11641 const U8* nvp = (const U8*)(&nv);
11642 HEXTRACT_GET_SUBNORMAL(nv);
11643 HEXTRACT_IMPLICIT_BIT(nv);
11644 HEXTRACT_TOP_NYBBLE(6);
11645 HEXTRACT_BYTES_LE(5, 0);
11646 # elif defined(HEXTRACT_BIG_ENDIAN)
11647 /* 7 6 5 4 3 2 1 0 (MSB = 7, LSB = 0, 6+7 = exponent+sign) */
11648 const U8* nvp = (const U8*)(&nv);
11649 HEXTRACT_GET_SUBNORMAL(nv);
11650 HEXTRACT_IMPLICIT_BIT(nv);
11651 HEXTRACT_TOP_NYBBLE(1);
11652 HEXTRACT_BYTES_BE(2, 7);
11653 # elif DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_MIXED_ENDIAN_LE_BE
11654 /* 4 5 6 7 0 1 2 3 (MSB = 7, LSB = 0, 6:7 = nybble:exponent:sign) */
11655 const U8* nvp = (const U8*)(&nv);
11656 HEXTRACT_GET_SUBNORMAL(nv);
11657 HEXTRACT_IMPLICIT_BIT(nv);
11658 HEXTRACT_TOP_NYBBLE(2); /* 6 */
11659 HEXTRACT_BYTE(1); /* 5 */
11660 HEXTRACT_BYTE(0); /* 4 */
11661 HEXTRACT_BYTE(7); /* 3 */
11662 HEXTRACT_BYTE(6); /* 2 */
11663 HEXTRACT_BYTE(5); /* 1 */
11664 HEXTRACT_BYTE(4); /* 0 */
11665 # elif DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_MIXED_ENDIAN_BE_LE
11666 /* 3 2 1 0 7 6 5 4 (MSB = 7, LSB = 0, 7:6 = sign:exponent:nybble) */
11667 const U8* nvp = (const U8*)(&nv);
11668 HEXTRACT_GET_SUBNORMAL(nv);
11669 HEXTRACT_IMPLICIT_BIT(nv);
11670 HEXTRACT_TOP_NYBBLE(5); /* 6 */
11671 HEXTRACT_BYTE(6); /* 5 */
11672 HEXTRACT_BYTE(7); /* 4 */
11673 HEXTRACT_BYTE(0); /* 3 */
11674 HEXTRACT_BYTE(1); /* 2 */
11675 HEXTRACT_BYTE(2); /* 1 */
11676 HEXTRACT_BYTE(3); /* 0 */
11678 # define HEXTRACT_FALLBACK
11681 # define HEXTRACT_FALLBACK
11683 #endif /* #if defined(USE_LONG_DOUBLE) && (NVSIZE > DOUBLESIZE) #else */
11685 #ifdef HEXTRACT_FALLBACK
11686 HEXTRACT_GET_SUBNORMAL(nv);
11687 # undef HEXTRACT_HAS_TOP_NYBBLE /* Meaningless, but consistent. */
11688 /* The fallback is used for the double-double format, and
11689 * for unknown long double formats, and for unknown double
11690 * formats, or in general unknown NV formats. */
11691 if (nv == (NV)0.0) {
11699 NV d = nv < 0 ? -nv : nv;
11701 U8 ha = 0x0; /* hexvalue accumulator */
11702 U8 hd = 0x8; /* hexvalue digit */
11704 /* Shift d and e (and update exponent) so that e <= d < 2*e,
11705 * this is essentially manual frexp(). Multiplying by 0.5 and
11706 * doubling should be lossless in binary floating point. */
11716 while (d >= e + e) {
11720 /* Now e <= d < 2*e */
11722 /* First extract the leading hexdigit (the implicit bit). */
11738 /* Then extract the remaining hexdigits. */
11739 while (d > (NV)0.0) {
11745 /* Output or count in groups of four bits,
11746 * that is, when the hexdigit is down to one. */
11751 /* Reset the hexvalue. */
11760 /* Flush possible pending hexvalue. */
11770 /* Croak for various reasons: if the output pointer escaped the
11771 * output buffer, if the extraction index escaped the extraction
11772 * buffer, or if the ending output pointer didn't match the
11773 * previously computed value. */
11774 if (v <= vhex || v - vhex >= VHEX_SIZE ||
11775 /* For double-double the ixmin and ixmax stay at zero,
11776 * which is convenient since the HEXTRACTSIZE is tricky
11777 * for double-double. */
11778 ixmin < 0 || ixmax >= NVSIZE ||
11779 (vend && v != vend)) {
11780 /* diag_listed_as: Hexadecimal float: internal error (%s) */
11781 Perl_croak(aTHX_ "Hexadecimal float: internal error (overflow)");
11787 /* S_format_hexfp(): helper function for Perl_sv_vcatpvfn_flags().
11789 * Processes the %a/%A hexadecimal floating-point format, since the
11790 * built-in snprintf()s which are used for most of the f/p formats, don't
11791 * universally handle %a/%A.
11792 * Populates buf of length bufsize, and returns the length of the created
11794 * The rest of the args have the same meaning as the local vars of the
11795 * same name within Perl_sv_vcatpvfn_flags().
11797 * The caller's determination of IN_LC(LC_NUMERIC), passed as in_lc_numeric,
11798 * is used to ensure we do the right thing when we need to access the locale's
11801 * It requires the caller to make buf large enough.
11805 S_format_hexfp(pTHX_ char * const buf, const STRLEN bufsize, const char c,
11806 const NV nv, const vcatpvfn_long_double_t fv,
11807 bool has_precis, STRLEN precis, STRLEN width,
11808 bool alt, char plus, bool left, bool fill, bool in_lc_numeric)
11810 /* Hexadecimal floating point. */
11812 U8 vhex[VHEX_SIZE];
11813 U8* v = vhex; /* working pointer to vhex */
11814 U8* vend; /* pointer to one beyond last digit of vhex */
11815 U8* vfnz = NULL; /* first non-zero */
11816 U8* vlnz = NULL; /* last non-zero */
11817 U8* v0 = NULL; /* first output */
11818 const bool lower = (c == 'a');
11819 /* At output the values of vhex (up to vend) will
11820 * be mapped through the xdig to get the actual
11821 * human-readable xdigits. */
11822 const char* xdig = PL_hexdigit;
11823 STRLEN zerotail = 0; /* how many extra zeros to append */
11824 int exponent = 0; /* exponent of the floating point input */
11825 bool hexradix = FALSE; /* should we output the radix */
11826 bool subnormal = FALSE; /* IEEE 754 subnormal/denormal */
11827 bool negative = FALSE;
11830 /* XXX: NaN, Inf -- though they are printed as "NaN" and "Inf".
11832 * For example with denormals, (assuming the vanilla
11833 * 64-bit double): the exponent is zero. 1xp-1074 is
11834 * the smallest denormal and the smallest double, it
11835 * could be output also as 0x0.0000000000001p-1022 to
11836 * match its internal structure. */
11838 vend = S_hextract(aTHX_ nv, &exponent, &subnormal, vhex, NULL);
11839 S_hextract(aTHX_ nv, &exponent, &subnormal, vhex, vend);
11841 #if NVSIZE > DOUBLESIZE
11842 # ifdef HEXTRACT_HAS_IMPLICIT_BIT
11843 /* In this case there is an implicit bit,
11844 * and therefore the exponent is shifted by one. */
11846 # elif defined(NV_X86_80_BIT)
11848 /* The subnormals of the x86-80 have a base exponent of -16382,
11849 * (while the physical exponent bits are zero) but the frexp()
11850 * returned the scientific-style floating exponent. We want
11851 * to map the last one as:
11852 * -16831..-16384 -> -16382 (the last normal is 0x1p-16382)
11853 * -16835..-16388 -> -16384
11854 * since we want to keep the first hexdigit
11855 * as one of the [8421]. */
11856 exponent = -4 * ( (exponent + 1) / -4) - 2;
11860 /* TBD: other non-implicit-bit platforms than the x86-80. */
11864 negative = fv < 0 || Perl_signbit(nv);
11875 xdig += 16; /* Use uppercase hex. */
11878 /* Find the first non-zero xdigit. */
11879 for (v = vhex; v < vend; v++) {
11887 /* Find the last non-zero xdigit. */
11888 for (v = vend - 1; v >= vhex; v--) {
11895 #if NVSIZE == DOUBLESIZE
11901 #ifndef NV_X86_80_BIT
11903 /* IEEE 754 subnormals (but not the x86 80-bit):
11904 * we want "normalize" the subnormal,
11905 * so we need to right shift the hex nybbles
11906 * so that the output of the subnormal starts
11907 * from the first true bit. (Another, equally
11908 * valid, policy would be to dump the subnormal
11909 * nybbles as-is, to display the "physical" layout.) */
11912 /* Find the ceil(log2(v[0])) of
11913 * the top non-zero nybble. */
11914 for (i = vfnz[0], n = 0; i > 1; i >>= 1, n++) { }
11918 for (vshr = vlnz; vshr >= vfnz; vshr--) {
11919 vshr[1] |= (vshr[0] & (0xF >> (4 - n))) << (4 - n);
11933 U8* ve = (subnormal ? vlnz + 1 : vend);
11934 SSize_t vn = ve - v0;
11936 if (precis < (Size_t)(vn - 1)) {
11937 bool overflow = FALSE;
11938 if (v0[precis + 1] < 0x8) {
11939 /* Round down, nothing to do. */
11940 } else if (v0[precis + 1] > 0x8) {
11943 overflow = v0[precis] > 0xF;
11945 } else { /* v0[precis] == 0x8 */
11946 /* Half-point: round towards the one
11947 * with the even least-significant digit:
11955 * 78 -> 8 f8 -> 10 */
11956 if ((v0[precis] & 0x1)) {
11959 overflow = v0[precis] > 0xF;
11964 for (v = v0 + precis - 1; v >= v0; v--) {
11966 overflow = *v > 0xF;
11972 if (v == v0 - 1 && overflow) {
11973 /* If the overflow goes all the
11974 * way to the front, we need to
11975 * insert 0x1 in front, and adjust
11977 Move(v0, v0 + 1, vn - 1, char);
11983 /* The new effective "last non zero". */
11984 vlnz = v0 + precis;
11988 subnormal ? precis - vn + 1 :
11989 precis - (vlnz - vhex);
11996 /* If there are non-zero xdigits, the radix
11997 * is output after the first one. */
12005 zerotail = has_precis ? precis : 0;
12008 /* The radix is always output if precis, or if alt. */
12009 if ((has_precis && precis > 0) || alt) {
12014 #ifndef USE_LOCALE_NUMERIC
12015 PERL_UNUSED_ARG(in_lc_numeric);
12019 if (in_lc_numeric) {
12021 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(TRUE, {
12022 const char* r = SvPV(PL_numeric_radix_sv, n);
12023 Copy(r, p, n, char);
12038 if (zerotail > 0) {
12039 while (zerotail--) {
12046 /* sanity checks */
12047 if (elen >= bufsize || width >= bufsize)
12048 /* diag_listed_as: Hexadecimal float: internal error (%s) */
12049 Perl_croak(aTHX_ "Hexadecimal float: internal error (overflow)");
12051 elen += my_snprintf(p, bufsize - elen,
12052 "%c%+d", lower ? 'p' : 'P',
12055 if (elen < width) {
12056 STRLEN gap = (STRLEN)(width - elen);
12058 /* Pad the back with spaces. */
12059 memset(buf + elen, ' ', gap);
12062 /* Insert the zeros after the "0x" and the
12063 * the potential sign, but before the digits,
12064 * otherwise we end up with "0000xH.HHH...",
12065 * when we want "0x000H.HHH..." */
12066 STRLEN nzero = gap;
12067 char* zerox = buf + 2;
12068 STRLEN nmove = elen - 2;
12069 if (negative || plus) {
12073 Move(zerox, zerox + nzero, nmove, char);
12074 memset(zerox, fill ? '0' : ' ', nzero);
12077 /* Move it to the right. */
12078 Move(buf, buf + gap,
12080 /* Pad the front with spaces. */
12081 memset(buf, ' ', gap);
12089 =for apidoc sv_vcatpvfn
12090 =for apidoc_item sv_vcatpvfn_flags
12092 These process their arguments like C<L<vsprintf(3)>> and append the formatted output
12093 to an SV. They use an array of SVs if the C-style variable argument list is
12094 missing (C<NULL>). Argument reordering (using format specifiers like C<%2$d> or
12095 C<%*2$d>) is supported only when using an array of SVs; using a C-style
12096 C<va_list> argument list with a format string that uses argument reordering
12097 will yield an exception.
12099 When running with taint checks enabled, they indicate via C<maybe_tainted> if
12100 results are untrustworthy (often due to the use of locales).
12102 They assume that C<pat> has the same utf8-ness as C<sv>. It's the caller's
12103 responsibility to ensure that this is so.
12105 They differ in that C<sv_vcatpvfn_flags> has a C<flags> parameter in which you
12106 can set or clear the C<SV_GMAGIC> and/or S<SV_SMAGIC> flags, to specify which
12107 magic to handle or not handle; whereas plain C<sv_vcatpvfn> always specifies
12108 both 'get' and 'set' magic.
12110 They are usually used via one of the frontends L</C<sv_vcatpvf>> and
12111 L</C<sv_vcatpvf_mg>>.
12118 Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
12119 va_list *const args, SV **const svargs, const Size_t sv_count, bool *const maybe_tainted,
12122 const char *fmtstart; /* character following the current '%' */
12123 const char *q; /* current position within format */
12124 const char *patend;
12127 static const char nullstr[] = "(null)";
12128 bool has_utf8 = DO_UTF8(sv); /* has the result utf8? */
12129 const bool pat_utf8 = has_utf8; /* the pattern is in utf8? */
12130 /* Times 4: a decimal digit takes more than 3 binary digits.
12131 * NV_DIG: mantissa takes that many decimal digits.
12132 * Plus 32: Playing safe. */
12133 char ebuf[IV_DIG * 4 + NV_DIG + 32];
12134 bool no_redundant_warning = FALSE; /* did we use any explicit format parameter index? */
12135 #ifdef USE_LOCALE_NUMERIC
12136 bool have_in_lc_numeric = FALSE;
12138 /* we never change this unless USE_LOCALE_NUMERIC */
12139 bool in_lc_numeric = FALSE;
12142 PERL_ARGS_ASSERT_SV_VCATPVFN_FLAGS;
12143 PERL_UNUSED_ARG(maybe_tainted);
12145 if (flags & SV_GMAGIC)
12148 /* no matter what, this is a string now */
12149 (void)SvPV_force_nomg(sv, origlen);
12151 /* the code that scans for flags etc following a % relies on
12152 * a '\0' being present to avoid falling off the end. Ideally that
12153 * should be fixed */
12154 assert(pat[patlen] == '\0');
12157 /* Special-case "", "%s", "%-p" (SVf - see below) and "%.0f".
12158 * In each case, if there isn't the correct number of args, instead
12159 * fall through to the main code to handle the issuing of any
12163 if (patlen == 0 && (args || sv_count == 0))
12166 if (patlen <= 4 && pat[0] == '%' && (args || sv_count == 1)) {
12169 if (patlen == 2 && pat[1] == 's') {
12171 const char * const s = va_arg(*args, char*);
12172 sv_catpv_nomg(sv, s ? s : nullstr);
12175 /* we want get magic on the source but not the target.
12176 * sv_catsv can't do that, though */
12177 SvGETMAGIC(*svargs);
12178 sv_catsv_nomg(sv, *svargs);
12185 if (patlen == 3 && pat[1] == '-' && pat[2] == 'p') {
12186 SV *asv = MUTABLE_SV(va_arg(*args, void*));
12187 sv_catsv_nomg(sv, asv);
12191 #if !defined(USE_LONG_DOUBLE) && !defined(USE_QUADMATH)
12192 /* special-case "%.0f" */
12193 else if ( patlen == 4
12194 && pat[1] == '.' && pat[2] == '0' && pat[3] == 'f')
12196 const NV nv = SvNV(*svargs);
12197 if (LIKELY(!Perl_isinfnan(nv))) {
12201 if ((p = F0convert(nv, ebuf + sizeof ebuf, &l))) {
12202 sv_catpvn_nomg(sv, p, l);
12207 #endif /* !USE_LONG_DOUBLE */
12211 patend = (char*)pat + patlen;
12212 for (fmtstart = pat; fmtstart < patend; fmtstart = q) {
12213 char intsize = 0; /* size qualifier in "%hi..." etc */
12214 bool alt = FALSE; /* has "%#..." */
12215 bool left = FALSE; /* has "%-..." */
12216 bool fill = FALSE; /* has "%0..." */
12217 char plus = 0; /* has "%+..." */
12218 STRLEN width = 0; /* value of "%NNN..." */
12219 bool has_precis = FALSE; /* has "%.NNN..." */
12220 STRLEN precis = 0; /* value of "%.NNN..." */
12221 int base = 0; /* base to print in, e.g. 8 for %o */
12222 UV uv = 0; /* the value to print of int-ish args */
12224 bool vectorize = FALSE; /* has "%v..." */
12225 bool vec_utf8 = FALSE; /* SvUTF8(vec arg) */
12226 const U8 *vecstr = NULL; /* SvPVX(vec arg) */
12227 STRLEN veclen = 0; /* SvCUR(vec arg) */
12228 const char *dotstr = NULL; /* separator string for %v */
12229 STRLEN dotstrlen; /* length of separator string for %v */
12231 Size_t efix = 0; /* explicit format parameter index */
12232 const Size_t osvix = svix; /* original index in case of bad fmt */
12235 bool is_utf8 = FALSE; /* is this item utf8? */
12236 bool arg_missing = FALSE; /* give "Missing argument" warning */
12237 char esignbuf[4]; /* holds sign prefix, e.g. "-0x" */
12238 STRLEN esignlen = 0; /* length of e.g. "-0x" */
12239 STRLEN zeros = 0; /* how many '0' to prepend */
12241 const char *eptr = NULL; /* the address of the element string */
12242 STRLEN elen = 0; /* the length of the element string */
12244 char c; /* the actual format ('d', s' etc) */
12246 bool escape_it = FALSE; /* if this is a string should we quote and escape it? */
12249 /* echo everything up to the next format specification */
12250 for (q = fmtstart; q < patend && *q != '%'; ++q)
12253 if (q > fmtstart) {
12254 if (has_utf8 && !pat_utf8) {
12255 /* upgrade and copy the bytes of fmtstart..q-1 to utf8 on
12259 STRLEN need = SvCUR(sv) + (q - fmtstart) + 1;
12261 for (p = fmtstart; p < q; p++)
12262 if (!NATIVE_BYTE_IS_INVARIANT(*p))
12267 for (p = fmtstart; p < q; p++)
12268 append_utf8_from_native_byte((U8)*p, (U8**)&dst);
12270 SvCUR_set(sv, need - 1);
12273 S_sv_catpvn_simple(aTHX_ sv, fmtstart, q - fmtstart);
12278 fmtstart = q; /* fmtstart is char following the '%' */
12281 We allow format specification elements in this order:
12282 \d+\$ explicit format parameter index
12284 v|\*(\d+\$)?v vector with optional (optionally specified) arg
12285 0 flag (as above): repeated to allow "v02"
12286 \d+|\*(\d+\$)? width using optional (optionally specified) arg
12287 \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg
12289 [%bcdefginopsuxDFOUX] format (mandatory)
12292 if (inRANGE(*q, '1', '9')) {
12293 width = expect_number(&q);
12296 Perl_croak_nocontext(
12297 "Cannot yet reorder sv_vcatpvfn() arguments from va_list");
12299 efix = (Size_t)width;
12301 no_redundant_warning = TRUE;
12313 if (plus == '+' && *q == ' ') /* '+' over ' ' */
12340 /* at this point we can expect one of:
12342 * 123 an explicit width
12343 * * width taken from next arg
12344 * *12$ width taken from 12th arg
12347 * But any width specification may be preceded by a v, in one of its
12352 * So an asterisk may be either a width specifier or a vector
12353 * separator arg specifier, and we don't know which initially
12358 STRLEN ix; /* explicit width/vector separator index */
12360 if (inRANGE(*q, '1', '9')) {
12361 ix = expect_number(&q);
12364 Perl_croak_nocontext(
12365 "Cannot yet reorder sv_vcatpvfn() arguments from va_list");
12366 no_redundant_warning = TRUE;
12375 /* The asterisk was for *v, *NNN$v: vectorizing, but not
12376 * with the default "." */
12381 vecsv = va_arg(*args, SV*);
12383 ix = ix ? ix - 1 : svix++;
12384 vecsv = ix < sv_count ? svargs[ix]
12385 : (arg_missing = TRUE, &PL_sv_no);
12387 dotstr = SvPV_const(vecsv, dotstrlen);
12388 /* Keep the DO_UTF8 test *after* the SvPV call, else things go
12389 bad with tied or overloaded values that return UTF8. */
12390 if (DO_UTF8(vecsv))
12392 else if (has_utf8) {
12393 vecsv = sv_mortalcopy(vecsv);
12394 sv_utf8_upgrade(vecsv);
12395 dotstr = SvPV_const(vecsv, dotstrlen);
12402 /* the asterisk specified a width */
12405 SV *width_sv = NULL;
12407 i = va_arg(*args, int);
12409 ix = ix ? ix - 1 : svix++;
12410 width_sv = (ix < sv_count) ? svargs[ix]
12411 : (arg_missing = TRUE, (SV*)NULL);
12413 width = S_sprintf_arg_num_val(aTHX_ args, i, width_sv, &left);
12416 else if (*q == 'v') {
12427 /* explicit width? */
12432 if (inRANGE(*q, '1', '9'))
12433 width = expect_number(&q);
12443 STRLEN ix; /* explicit precision index */
12445 if (inRANGE(*q, '1', '9')) {
12446 ix = expect_number(&q);
12449 Perl_croak_nocontext(
12450 "Cannot yet reorder sv_vcatpvfn() arguments from va_list");
12451 no_redundant_warning = TRUE;
12460 SV *width_sv = NULL;
12464 i = va_arg(*args, int);
12466 ix = ix ? ix - 1 : svix++;
12467 width_sv = (ix < sv_count) ? svargs[ix]
12468 : (arg_missing = TRUE, (SV*)NULL);
12470 precis = S_sprintf_arg_num_val(aTHX_ args, i, width_sv, &neg);
12472 /* ignore negative precision */
12478 /* although it doesn't seem documented, this code has long
12480 * no digits following the '.' is treated like '.0'
12481 * the number may be preceded by any number of zeroes,
12482 * e.g. "%.0001f", which is the same as "%.1f"
12483 * so I've kept that behaviour. DAPM May 2017
12487 precis = inRANGE(*q, '1', '9') ? expect_number(&q) : 0;
12496 case 'I': /* Ix, I32x, and I64x */
12497 # ifdef USE_64_BIT_INT
12498 if (q[1] == '6' && q[2] == '4') {
12504 if (q[1] == '3' && q[2] == '2') {
12508 # ifdef USE_64_BIT_INT
12514 #if (IVSIZE >= 8 || defined(HAS_LONG_DOUBLE)) || \
12515 (IVSIZE == 4 && !defined(HAS_LONG_DOUBLE))
12527 #if (IVSIZE >= 8 || defined(HAS_LONG_DOUBLE)) || \
12528 (IVSIZE == 4 && !defined(HAS_LONG_DOUBLE))
12529 if (*q == 'l') { /* lld, llf */
12538 if (*++q == 'h') { /* hhd, hhu */
12545 #ifdef USE_QUADMATH
12558 c = *q++; /* c now holds the conversion type */
12560 /* '%' doesn't have an arg, so skip arg processing */
12569 if (vectorize && !memCHRs("BbDdiOouUXx", c))
12572 /* get next arg (individual branches do their own va_arg()
12573 * handling for the args case) */
12576 efix = efix ? efix - 1 : svix++;
12577 argsv = efix < sv_count ? svargs[efix]
12578 : (arg_missing = TRUE, &PL_sv_no);
12588 eptr = va_arg(*args, char*);
12591 elen = my_strnlen(eptr, precis);
12593 elen = strlen(eptr);
12595 eptr = (char *)nullstr;
12596 elen = sizeof nullstr - 1;
12600 eptr = SvPV_const(argsv, elen);
12601 if (DO_UTF8(argsv)) {
12602 STRLEN old_precis = precis;
12603 if (has_precis && precis < elen) {
12604 STRLEN ulen = sv_or_pv_len_utf8(argsv, eptr, elen);
12605 STRLEN p = precis > ulen ? ulen : precis;
12606 precis = sv_or_pv_pos_u2b(argsv, eptr, p, 0);
12607 /* sticks at end */
12609 if (width) { /* fudge width (can't fudge elen) */
12610 if (has_precis && precis < elen)
12611 width += precis - old_precis;
12614 elen - sv_or_pv_len_utf8(argsv,eptr,elen);
12622 U32 flags = PERL_PV_PRETTY_QUOTEDPREFIX;
12624 flags |= PERL_PV_ESCAPE_UNI;
12627 /* "blah"... where blah might be made up
12628 * of characters like \x{1234} */
12629 tmp_sv = newSV(1 + (PERL_QUOTEDPREFIX_LEN * 8) + 1 + 3);
12630 sv_2mortal(tmp_sv);
12632 pv_pretty(tmp_sv, eptr, elen, PERL_QUOTEDPREFIX_LEN,
12633 NULL, NULL, flags);
12634 eptr = SvPV_const(tmp_sv, elen);
12636 if (has_precis && precis < elen)
12646 * We want to extend the C level sprintf format API with
12647 * custom formats for specific types (eg SV*) and behavior.
12648 * However some C compilers are "sprintf aware" and will
12649 * throw compile time exceptions when an illegal sprintf is
12650 * encountered, so we can't just add new format letters.
12652 * However it turns out the length argument to the %p format
12653 * is more or less useless (the size of a pointer does not
12654 * change over time) and is not really used in the C level
12655 * code. Accordingly we can map our special behavior to
12656 * specific "length" options to the %p format. We hide these
12657 * mappings behind defines anyway, so nobody needs to know
12658 * that HEKf is actually %2p. This keeps the C compiler
12659 * happy while allowing us to add new formats.
12661 * Note the existing logic for which number is used for what
12662 * is torturous. All negative values are used for SVf, and
12663 * non-negative values have arbitrary meanings with no
12664 * structure to them. This may change in the future.
12666 * NEVER use the raw %p values directly. Always use the define
12667 * as the underlying mapping may change in the future.
12673 * "%...p" is normally treated like "%...x", except that the
12674 * number to print is the SV's address (or a pointer address
12675 * for C-ish sprintf).
12677 * However, the C-ish sprintf variant allows a few special
12678 * extensions. These are currently:
12680 * %-p (SVf) Like %s, but gets the string from an SV*
12681 * arg rather than a char* arg. Use C<SVfARG()>
12682 * to set up the argument properly.
12683 * (This was previously %_).
12685 * %-<num>p Ditto but like %.<num>s (i.e. num is max
12686 * width), there is no escaped and quoted version
12689 * %1p (PVf_QUOTEDPREFIX). Like raw %s, but it is escaped
12692 * %5p (SVf_QUOTEDPREFIX) Like SVf, but length restricted,
12693 * escaped and quoted with pv_pretty. Intended
12694 * for error messages.
12696 * %2p (HEKf) Like %s, but using the key string in a HEK
12697 * %7p (HEKf_QUOTEDPREFIX) ... but escaped and quoted.
12699 * %3p (HEKf256) Ditto but like %.256s
12700 * %8p (HEKf256_QUOTEDPREFIX) ... but escaped and quoted
12702 * %d%lu%4p (UTF8f) A utf8 string. Consumes 3 args:
12703 * (cBOOL(utf8), len, string_buf).
12704 * It's handled by the "case 'd'" branch
12705 * rather than here.
12706 * %d%lu%9p (UTF8f_QUOTEDPREFIX) .. but escaped and quoted.
12708 * %6p (HvNAMEf) Like %s, but using the HvNAME() and HvNAMELEN()
12709 * %10p (HvNAMEf_QUOTEDPREFIX) ... but escaped and quoted
12711 * %<num>p where num is > 9: reserved for future
12712 * extensions. Warns, but then is treated as a
12713 * general %p (print hex address) format.
12715 * NOTE: If you add a new magic %p value you will
12716 * need to update F<t/porting/diag.t> to be aware of it
12717 * on top of adding the various defines and etc. Do not
12718 * forget to add it to F<pod/perlguts.pod> as well.
12726 /* not %*p or %*1$p - any width was explicit */
12730 if (left || width == 5) { /* %-p (SVf), %-NNNp, %5p */
12731 if (left && width) {
12734 } else if (width == 5) {
12737 argsv = MUTABLE_SV(va_arg(*args, void*));
12738 eptr = SvPV_const(argsv, elen);
12739 if (DO_UTF8(argsv))
12744 else if (width == 2 || width == 3 ||
12745 width == 7 || width == 8)
12746 { /* HEKf, HEKf256, HEKf_QUOTEDPREFIX, HEKf256_QUOTEDPREFIX */
12747 HEK * const hek = va_arg(*args, HEK *);
12748 eptr = HEK_KEY(hek);
12749 elen = HEK_LEN(hek);
12761 else if (width == 1) {
12762 eptr = va_arg(*args,char *);
12763 elen = strlen(eptr);
12768 else if (width == 6 || width == 10) {
12769 HV *hv = va_arg(*args, HV *);
12771 elen = HvNAMELEN(hv);
12772 if (HvNAMEUTF8(hv))
12780 /* note width=4 or width=9 is handled under %d */
12781 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
12782 "internal %%<num>p might conflict with future printf extensions");
12786 /* treat as normal %...p */
12788 uv = PTR2UV(args ? va_arg(*args, void*) : argsv);
12790 c = 'x'; /* in case the format string contains '#' */
12794 /* Ignore any size specifiers, since they're not documented as
12795 * being allowed for %c (ideally we should warn on e.g. '%hc').
12796 * Setting a default intsize, along with a positive
12797 * (which signals unsigned) base, causes, for C-ish use, the
12798 * va_arg to be interpreted as an unsigned int, when it's
12799 * actually signed, which will convert -ve values to high +ve
12800 * values. Note that unlike the libc %c, values > 255 will
12801 * convert to high unicode points rather than being truncated
12802 * to 8 bits. For perlish use, it will do SvUV(argsv), which
12803 * will again convert -ve args to high -ve values.
12806 base = 1; /* special value that indicates we're doing a 'c' */
12807 goto get_int_arg_val;
12816 goto get_int_arg_val;
12819 /* probably just a plain %d, but it might be the start of the
12820 * special UTF8f format, which usually looks something like
12821 * "%d%lu%4p" (the lu may vary by platform) or
12822 * "%d%lu%9p" for an escaped version.
12824 assert((UTF8f)[0] == 'd');
12825 assert((UTF8f)[1] == '%');
12827 if ( args /* UTF8f only valid for C-ish sprintf */
12828 && q == fmtstart + 1 /* plain %d, not %....d */
12829 && patend >= fmtstart + sizeof(UTF8f) - 1 /* long enough */
12831 && strnEQ(q + 1, (UTF8f) + 2, sizeof(UTF8f) - 5)
12832 && q[sizeof(UTF8f)-3] == 'p'
12833 && (q[sizeof(UTF8f)-4] == '4' ||
12834 q[sizeof(UTF8f)-4] == '9'))
12836 /* The argument has already gone through cBOOL, so the cast
12838 if (q[sizeof(UTF8f)-4] == '9')
12840 is_utf8 = (bool)va_arg(*args, int);
12841 elen = va_arg(*args, UV);
12842 /* if utf8 length is larger than 0x7ffff..., then it might
12843 * have been a signed value that wrapped */
12844 if (elen > ((~(STRLEN)0) >> 1)) {
12845 assert(0); /* in DEBUGGING build we want to crash */
12846 elen = 0; /* otherwise we want to treat this as an empty string */
12848 eptr = va_arg(*args, char *);
12849 q += sizeof(UTF8f) - 2;
12856 goto get_int_arg_val;
12867 goto get_int_arg_val;
12872 goto get_int_arg_val;
12883 goto get_int_arg_val;
12898 esignbuf[esignlen++] = plus;
12901 /* initialise the vector string to iterate over */
12903 vecsv = args ? va_arg(*args, SV*) : argsv;
12905 /* if this is a version object, we need to convert
12906 * back into v-string notation and then let the
12907 * vectorize happen normally
12909 if (sv_isobject(vecsv) && sv_derived_from(vecsv, "version")) {
12910 if ( hv_existss(MUTABLE_HV(SvRV(vecsv)), "alpha") ) {
12911 Perl_ck_warner_d(aTHX_ packWARN(WARN_PRINTF),
12912 "vector argument not supported with alpha versions");
12916 vecstr = (U8*)SvPV_const(vecsv,veclen);
12917 vecsv = sv_newmortal();
12918 scan_vstring((char *)vecstr, (char *)vecstr + veclen,
12922 vecstr = (U8*)SvPV_const(vecsv, veclen);
12923 vec_utf8 = DO_UTF8(vecsv);
12925 /* This is the re-entry point for when we're iterating
12926 * over the individual characters of a vector arg */
12929 goto done_valid_conversion;
12931 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
12941 /* test arg for inf/nan. This can trigger an unwanted
12942 * 'str' overload, so manually force 'num' overload first
12946 if (UNLIKELY(SvAMAGIC(argsv)))
12947 argsv = sv_2num(argsv);
12948 if (UNLIKELY(isinfnansv(argsv)))
12949 goto handle_infnan_argsv;
12953 /* signed int type */
12958 case 'c': iv = (char)va_arg(*args, int); break;
12959 case 'h': iv = (short)va_arg(*args, int); break;
12960 case 'l': iv = va_arg(*args, long); break;
12961 case 'V': iv = va_arg(*args, IV); break;
12962 case 'z': iv = va_arg(*args, SSize_t); break;
12963 #ifdef HAS_PTRDIFF_T
12964 case 't': iv = va_arg(*args, ptrdiff_t); break;
12966 default: iv = va_arg(*args, int); break;
12967 case 'j': iv = (IV) va_arg(*args, PERL_INTMAX_T); break;
12970 iv = va_arg(*args, Quad_t); break;
12977 /* assign to tiv then cast to iv to work around
12978 * 2003 GCC cast bug (gnu.org bugzilla #13488) */
12979 IV tiv = SvIV_nomg(argsv);
12981 case 'c': iv = (char)tiv; break;
12982 case 'h': iv = (short)tiv; break;
12983 case 'l': iv = (long)tiv; break;
12985 default: iv = tiv; break;
12988 iv = (Quad_t)tiv; break;
12995 /* now convert iv to uv */
12999 esignbuf[esignlen++] = plus;
13002 /* Using 0- here to silence bogus warning from MS VC */
13003 uv = (UV) (0 - (UV) iv);
13004 esignbuf[esignlen++] = '-';
13008 /* unsigned int type */
13011 case 'c': uv = (unsigned char)va_arg(*args, unsigned);
13013 case 'h': uv = (unsigned short)va_arg(*args, unsigned);
13015 case 'l': uv = va_arg(*args, unsigned long); break;
13016 case 'V': uv = va_arg(*args, UV); break;
13017 case 'z': uv = va_arg(*args, Size_t); break;
13018 #ifdef HAS_PTRDIFF_T
13019 /* will sign extend, but there is no
13020 * uptrdiff_t, so oh well */
13021 case 't': uv = va_arg(*args, ptrdiff_t); break;
13023 case 'j': uv = (UV) va_arg(*args, PERL_UINTMAX_T); break;
13024 default: uv = va_arg(*args, unsigned); break;
13027 uv = va_arg(*args, Uquad_t); break;
13034 /* assign to tiv then cast to iv to work around
13035 * 2003 GCC cast bug (gnu.org bugzilla #13488) */
13036 UV tuv = SvUV_nomg(argsv);
13038 case 'c': uv = (unsigned char)tuv; break;
13039 case 'h': uv = (unsigned short)tuv; break;
13040 case 'l': uv = (unsigned long)tuv; break;
13042 default: uv = tuv; break;
13045 uv = (Uquad_t)tuv; break;
13056 char *ptr = ebuf + sizeof ebuf;
13063 const char * const p =
13064 (c == 'X') ? PL_hexdigit + 16 : PL_hexdigit;
13069 } while (uv >>= 4);
13070 if (alt && *ptr != '0') {
13071 esignbuf[esignlen++] = '0';
13072 esignbuf[esignlen++] = c; /* 'x' or 'X' */
13079 *--ptr = '0' + dig;
13080 } while (uv >>= 3);
13081 if (alt && *ptr != '0')
13087 *--ptr = '0' + dig;
13088 } while (uv >>= 1);
13089 if (alt && *ptr != '0') {
13090 esignbuf[esignlen++] = '0';
13091 esignbuf[esignlen++] = c; /* 'b' or 'B' */
13096 /* special-case: base 1 indicates a 'c' format:
13097 * we use the common code for extracting a uv,
13098 * but handle that value differently here than
13099 * all the other int types */
13101 (!UVCHR_IS_INVARIANT(uv) && SvUTF8(sv)))
13104 STATIC_ASSERT_STMT(sizeof(ebuf) >= UTF8_MAXBYTES + 1);
13106 elen = uvchr_to_utf8((U8*)eptr, uv) - (U8*)ebuf;
13111 ebuf[0] = (char)uv;
13116 default: /* it had better be ten or less */
13119 *--ptr = '0' + dig;
13120 } while (uv /= base);
13123 elen = (ebuf + sizeof ebuf) - ptr;
13127 zeros = precis - elen;
13128 else if (precis == 0 && elen == 1 && *eptr == '0'
13129 && !(base == 8 && alt)) /* "%#.0o" prints "0" */
13132 /* a precision nullifies the 0 flag. */
13138 /* FLOATING POINT */
13141 c = 'f'; /* maybe %F isn't supported here */
13143 case 'e': case 'E':
13145 case 'g': case 'G':
13146 case 'a': case 'A':
13149 STRLEN float_need; /* what PL_efloatsize needs to become */
13150 bool hexfp; /* hexadecimal floating point? */
13152 vcatpvfn_long_double_t fv;
13155 /* This is evil, but floating point is even more evil */
13157 /* for SV-style calling, we can only get NV
13158 for C-style calling, we assume %f is double;
13159 for simplicity we allow any of %Lf, %llf, %qf for long double
13162 #if defined(USE_QUADMATH)
13167 #if defined(USE_LONG_DOUBLE) || defined(USE_QUADMATH)
13171 /* [perl #20339] - we should accept and ignore %lf rather than die */
13175 #if defined(USE_LONG_DOUBLE) || defined(USE_QUADMATH)
13176 intsize = args ? 0 : 'q';
13180 #if defined(HAS_LONG_DOUBLE)
13193 /* Now we need (long double) if intsize == 'q', else (double). */
13195 /* Note: do not pull NVs off the va_list with va_arg()
13196 * (pull doubles instead) because if you have a build
13197 * with long doubles, you would always be pulling long
13198 * doubles, which would badly break anyone using only
13199 * doubles (i.e. the majority of builds). In other
13200 * words, you cannot mix doubles and long doubles.
13201 * The only case where you can pull off long doubles
13202 * is when the format specifier explicitly asks so with
13204 #ifdef USE_QUADMATH
13205 nv = intsize == 'Q' ? va_arg(*args, NV) :
13206 intsize == 'q' ? va_arg(*args, long double) :
13207 va_arg(*args, double);
13209 #elif LONG_DOUBLESIZE > DOUBLESIZE
13210 if (intsize == 'q') {
13211 fv = va_arg(*args, long double);
13214 nv = va_arg(*args, double);
13215 VCATPVFN_NV_TO_FV(nv, fv);
13218 nv = va_arg(*args, double);
13225 /* we jump here if an int-ish format encountered an
13226 * infinite/Nan argsv. After setting nv/fv, it falls
13227 * into the isinfnan block which follows */
13228 handle_infnan_argsv:
13229 nv = SvNV_nomg(argsv);
13230 VCATPVFN_NV_TO_FV(nv, fv);
13233 if (Perl_isinfnan(nv)) {
13235 Perl_croak(aTHX_ "Cannot printf %" NVgf " with '%c'",
13238 elen = S_infnan_2pv(nv, ebuf, sizeof(ebuf), plus);
13247 /* special-case "%.0f" */
13251 && !(width || left || plus || alt)
13254 && ((eptr = F0convert(nv, ebuf + sizeof ebuf, &elen)))
13258 /* Determine the buffer size needed for the various
13259 * floating-point formats.
13261 * The basic possibilities are:
13264 * %f 1111111.123456789
13265 * %e 1.111111123e+06
13266 * %a 0x1.0f4471f9bp+20
13268 * %g 1.11111112e+15
13270 * where P is the value of the precision in the format, or 6
13271 * if not specified. Note the two possible output formats of
13272 * %g; in both cases the number of significant digits is <=
13275 * For most of the format types the maximum buffer size needed
13276 * is precision, plus: any leading 1 or 0x1, the radix
13277 * point, and an exponent. The difficult one is %f: for a
13278 * large positive exponent it can have many leading digits,
13279 * which needs to be calculated specially. Also %a is slightly
13280 * different in that in the absence of a specified precision,
13281 * it uses as many digits as necessary to distinguish
13282 * different values.
13284 * First, here are the constant bits. For ease of calculation
13285 * we over-estimate the needed buffer size, for example by
13286 * assuming all formats have an exponent and a leading 0x1.
13288 * Also for production use, add a little extra overhead for
13289 * safety's sake. Under debugging don't, as it means we're
13290 * more likely to quickly spot issues during development.
13293 float_need = 1 /* possible unary minus */
13294 + 4 /* "0x1" plus very unlikely carry */
13295 + 1 /* default radix point '.' */
13296 + 2 /* "e-", "p+" etc */
13297 + 6 /* exponent: up to 16383 (quad fp) */
13299 + 20 /* safety net */
13304 /* determine the radix point len, e.g. length(".") in "1.2" */
13305 #ifdef USE_LOCALE_NUMERIC
13306 /* note that we may either explicitly use PL_numeric_radix_sv
13307 * below, or implicitly, via an snprintf() variant.
13308 * Note also things like ps_AF.utf8 which has
13309 * "\N{ARABIC DECIMAL SEPARATOR} as a radix point */
13310 if (! have_in_lc_numeric) {
13311 in_lc_numeric = IN_LC(LC_NUMERIC);
13312 have_in_lc_numeric = TRUE;
13315 if (in_lc_numeric) {
13316 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(TRUE, {
13317 /* this can't wrap unless PL_numeric_radix_sv is a string
13318 * consuming virtually all the 32-bit or 64-bit address
13321 float_need += (SvCUR(PL_numeric_radix_sv) - 1);
13323 /* floating-point formats only get utf8 if the radix point
13324 * is utf8. All other characters in the string are < 128
13325 * and so can be safely appended to both a non-utf8 and utf8
13327 * Note that this will convert the output to utf8 even if
13328 * the radix point didn't get output.
13330 if (SvUTF8(PL_numeric_radix_sv) && !has_utf8) {
13331 sv_utf8_upgrade(sv);
13340 if (isALPHA_FOLD_EQ(c, 'f')) {
13341 /* Determine how many digits before the radix point
13342 * might be emitted. frexp() (or frexpl) has some
13343 * unspecified behaviour for nan/inf/-inf, so lucky we've
13344 * already handled them above */
13346 int i = PERL_INT_MIN;
13347 (void)Perl_frexp((NV)fv, &i);
13348 if (i == PERL_INT_MIN)
13349 Perl_die(aTHX_ "panic: frexp: %" VCATPVFN_FV_GF, fv);
13352 digits = BIT_DIGITS(i);
13353 /* this can't overflow. 'digits' will only be a few
13354 * thousand even for the largest floating-point types.
13355 * And up until now float_need is just some small
13356 * constants plus radix len, which can't be in
13357 * overflow territory unless the radix SV is consuming
13358 * over 1/2 the address space */
13359 assert(float_need < ((STRLEN)~0) - digits);
13360 float_need += digits;
13363 else if (UNLIKELY(isALPHA_FOLD_EQ(c, 'a'))) {
13366 /* %a in the absence of precision may print as many
13367 * digits as needed to represent the entire mantissa
13369 * This estimate seriously overshoots in most cases,
13370 * but better the undershooting. Firstly, all bytes
13371 * of the NV are not mantissa, some of them are
13372 * exponent. Secondly, for the reasonably common
13373 * long doubles case, the "80-bit extended", two
13374 * or six bytes of the NV are unused. Also, we'll
13375 * still pick up an extra +6 from the default
13376 * precision calculation below. */
13378 #ifdef LONGDOUBLE_DOUBLEDOUBLE
13379 /* For the "double double", we need more.
13380 * Since each double has their own exponent, the
13381 * doubles may float (haha) rather far from each
13382 * other, and the number of required bits is much
13383 * larger, up to total of DOUBLEDOUBLE_MAXBITS bits.
13384 * See the definition of DOUBLEDOUBLE_MAXBITS.
13386 * Need 2 hexdigits for each byte. */
13387 (DOUBLEDOUBLE_MAXBITS/8 + 1) * 2;
13389 NVSIZE * 2; /* 2 hexdigits for each byte */
13391 /* see "this can't overflow" comment above */
13392 assert(float_need < ((STRLEN)~0) - digits);
13393 float_need += digits;
13396 /* special-case "%.<number>g" if it will fit in ebuf */
13398 && precis /* See earlier comment about buggy Gconvert
13399 when digits, aka precis, is 0 */
13401 /* check that "%.<number>g" formatting will fit in ebuf */
13402 && sizeof(ebuf) - float_need > precis
13403 /* sizeof(ebuf) - float_need will have wrapped if float_need > sizeof(ebuf). *
13404 * Therefore we should check that float_need < sizeof(ebuf). Normally, we would *
13405 * have run this check first, but that triggers incorrect -Wformat-overflow *
13406 * compilation warnings with some versions of gcc if Gconvert invokes sprintf(). *
13407 * ( See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89161 ) *
13408 * So, instead, we check it next: */
13409 && float_need < sizeof(ebuf)
13410 && !(width || left || plus || alt)
13414 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13415 SNPRINTF_G(fv, ebuf, sizeof(ebuf), precis)
13417 elen = strlen(ebuf);
13424 STRLEN pr = has_precis ? precis : 6; /* known default */
13425 /* this probably can't wrap, since precis is limited
13426 * to 1/4 address space size, but better safe than sorry
13428 if (float_need >= ((STRLEN)~0) - pr)
13429 croak_memory_wrap();
13433 if (float_need < width)
13434 float_need = width;
13436 if (float_need > INT_MAX) {
13437 /* snprintf() returns an int, and we use that return value,
13438 so die horribly if the expected size is too large for int
13440 Perl_croak(aTHX_ "Numeric format result too large");
13443 if (PL_efloatsize <= float_need) {
13444 /* PL_efloatbuf should be at least 1 greater than
13445 * float_need to allow a trailing \0 to be returned by
13446 * snprintf(). If we need to grow, overgrow for the
13447 * benefit of future generations */
13448 const STRLEN extra = 0x20;
13449 if (float_need >= ((STRLEN)~0) - extra)
13450 croak_memory_wrap();
13451 float_need += extra;
13452 Safefree(PL_efloatbuf);
13453 PL_efloatsize = float_need;
13454 Newx(PL_efloatbuf, PL_efloatsize, char);
13455 PL_efloatbuf[0] = '\0';
13458 if (UNLIKELY(hexfp)) {
13459 elen = S_format_hexfp(aTHX_ PL_efloatbuf, PL_efloatsize, c,
13460 nv, fv, has_precis, precis, width,
13461 alt, plus, left, fill, in_lc_numeric);
13464 char *ptr = ebuf + sizeof ebuf;
13467 #if defined(USE_QUADMATH)
13468 /* always use Q here. my_snprint() throws an exception if we
13469 fallthrough to the double/long double code, even when the
13470 format is correct, presumably to avoid any accidentally
13474 /* FIXME: what to do if HAS_LONG_DOUBLE but not PERL_PRIfldbl? */
13475 #elif defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl)
13476 /* Note that this is HAS_LONG_DOUBLE and PERL_PRIfldbl,
13477 * not USE_LONG_DOUBLE and NVff. In other words,
13478 * this needs to work without USE_LONG_DOUBLE. */
13479 if (intsize == 'q') {
13480 /* Copy the one or more characters in a long double
13481 * format before the 'base' ([efgEFG]) character to
13482 * the format string. */
13483 static char const ldblf[] = PERL_PRIfldbl;
13484 char const *p = ldblf + sizeof(ldblf) - 3;
13485 while (p >= ldblf) { *--ptr = *p--; }
13490 do { *--ptr = '0' + (base % 10); } while (base /= 10);
13495 do { *--ptr = '0' + (base % 10); } while (base /= 10);
13507 /* No taint. Otherwise we are in the strange situation
13508 * where printf() taints but print($float) doesn't.
13511 /* hopefully the above makes ptr a very constrained format
13512 * that is safe to use, even though it's not literal */
13513 GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral);
13514 #ifdef USE_QUADMATH
13516 if (!quadmath_format_valid(ptr))
13517 Perl_croak_nocontext("panic: quadmath invalid format \"%s\"", ptr);
13518 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13519 elen = quadmath_snprintf(PL_efloatbuf, PL_efloatsize,
13522 if ((IV)elen == -1) {
13523 Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", ptr);
13526 #elif defined(HAS_LONG_DOUBLE)
13527 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13528 elen = ((intsize == 'q')
13529 ? my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, fv)
13530 : my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, (double)fv))
13533 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13534 elen = my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, fv)
13537 GCC_DIAG_RESTORE_STMT;
13540 eptr = PL_efloatbuf;
13544 /* Since floating-point formats do their own formatting and
13545 * padding, we skip the main block of code at the end of this
13546 * loop which handles appending eptr to sv, and do our own
13547 * stripped-down version */
13552 assert(elen >= width);
13554 S_sv_catpvn_simple(aTHX_ sv, eptr, elen);
13556 goto done_valid_conversion;
13564 /* XXX ideally we should warn if any flags etc have been
13565 * set, e.g. "%-4.5n" */
13566 /* XXX if sv was originally non-utf8 with a char in the
13567 * range 0x80-0xff, then if it got upgraded, we should
13568 * calculate char len rather than byte len here */
13569 len = SvCUR(sv) - origlen;
13571 int i = (len > PERL_INT_MAX) ? PERL_INT_MAX : (int)len;
13574 case 'c': *(va_arg(*args, char*)) = i; break;
13575 case 'h': *(va_arg(*args, short*)) = i; break;
13576 default: *(va_arg(*args, int*)) = i; break;
13577 case 'l': *(va_arg(*args, long*)) = i; break;
13578 case 'V': *(va_arg(*args, IV*)) = i; break;
13579 case 'z': *(va_arg(*args, SSize_t*)) = i; break;
13580 #ifdef HAS_PTRDIFF_T
13581 case 't': *(va_arg(*args, ptrdiff_t*)) = i; break;
13583 case 'j': *(va_arg(*args, PERL_INTMAX_T*)) = i; break;
13586 *(va_arg(*args, Quad_t*)) = i; break;
13594 Perl_croak_nocontext(
13595 "Missing argument for %%n in %s",
13596 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
13597 sv_setuv_mg(argsv, has_utf8
13598 ? (UV)utf8_length((U8*)SvPVX(sv), (U8*)SvEND(sv))
13601 goto done_valid_conversion;
13609 && (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)
13610 && ckWARN(WARN_PRINTF))
13612 SV * const msg = sv_newmortal();
13613 Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %sprintf: ",
13614 (PL_op->op_type == OP_PRTF) ? "" : "s");
13615 if (fmtstart < patend) {
13616 const char * const fmtend = q < patend ? q : patend;
13618 sv_catpvs(msg, "\"%");
13619 for (f = fmtstart; f < fmtend; f++) {
13621 sv_catpvn_nomg(msg, f, 1);
13623 Perl_sv_catpvf(aTHX_ msg, "\\%03o", (U8) *f);
13626 sv_catpvs(msg, "\"");
13628 sv_catpvs(msg, "end of string");
13630 Perl_warner(aTHX_ packWARN(WARN_PRINTF), "%" SVf, SVfARG(msg)); /* yes, this is reentrant */
13633 /* mangled format: output the '%', then continue from the
13634 * character following that */
13635 sv_catpvn_nomg(sv, fmtstart-1, 1);
13638 /* Any "redundant arg" warning from now onwards will probably
13639 * just be misleading, so don't bother. */
13640 no_redundant_warning = TRUE;
13641 continue; /* not "break" */
13644 if (is_utf8 != has_utf8) {
13647 sv_utf8_upgrade(sv);
13650 const STRLEN old_elen = elen;
13651 SV * const nsv = newSVpvn_flags(eptr, elen, SVs_TEMP);
13652 sv_utf8_upgrade(nsv);
13653 eptr = SvPVX_const(nsv);
13656 if (width) { /* fudge width (can't fudge elen) */
13657 width += elen - old_elen;
13664 /* append esignbuf, filler, zeros, eptr and dotstr to sv */
13667 STRLEN need, have, gap;
13671 /* signed value that's wrapped? */
13672 assert(elen <= ((~(STRLEN)0) >> 1));
13674 /* if zeros is non-zero, then it represents filler between
13675 * elen and precis. So adding elen and zeros together will
13676 * always be <= precis, and the addition can never wrap */
13677 assert(!zeros || (precis > elen && precis - elen == zeros));
13678 have = elen + zeros;
13680 if (have >= (((STRLEN)~0) - esignlen))
13681 croak_memory_wrap();
13684 need = (have > width ? have : width);
13687 if (need >= (((STRLEN)~0) - (SvCUR(sv) + 1)))
13688 croak_memory_wrap();
13689 need += (SvCUR(sv) + 1);
13696 for (i = 0; i < esignlen; i++)
13697 *s++ = esignbuf[i];
13698 for (i = zeros; i; i--)
13700 Copy(eptr, s, elen, char);
13702 for (i = gap; i; i--)
13707 for (i = 0; i < esignlen; i++)
13708 *s++ = esignbuf[i];
13713 for (i = gap; i; i--)
13715 for (i = 0; i < esignlen; i++)
13716 *s++ = esignbuf[i];
13719 for (i = zeros; i; i--)
13721 Copy(eptr, s, elen, char);
13726 SvCUR_set(sv, s - SvPVX_const(sv));
13734 if (vectorize && veclen) {
13735 /* we append the vector separator separately since %v isn't
13736 * very common: don't slow down the general case by adding
13737 * dotstrlen to need etc */
13738 sv_catpvn_nomg(sv, dotstr, dotstrlen);
13740 goto vector; /* do next iteration */
13743 done_valid_conversion:
13746 S_warn_vcatpvfn_missing_argument(aTHX);
13749 /* Now that we've consumed all our printf format arguments (svix)
13750 * do we have things left on the stack that we didn't use?
13752 if (!no_redundant_warning && sv_count >= svix + 1 && ckWARN(WARN_REDUNDANT)) {
13753 Perl_warner(aTHX_ packWARN(WARN_REDUNDANT), "Redundant argument in %s",
13754 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
13757 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
13758 /* while we shouldn't set the cache, it may have been previously
13759 set in the caller, so clear it */
13760 MAGIC *mg = mg_find(sv, PERL_MAGIC_utf8);
13762 magic_setutf8(sv,mg); /* clear UTF8 cache */
13767 /* =========================================================================
13769 =for apidoc_section $embedding
13773 All the macros and functions in this section are for the private use of
13774 the main function, perl_clone().
13776 The foo_dup() functions make an exact copy of an existing foo thingy.
13777 During the course of a cloning, a hash table is used to map old addresses
13778 to new addresses. The table is created and manipulated with the
13779 ptr_table_* functions.
13781 * =========================================================================*/
13784 #if defined(USE_ITHREADS)
13786 /* XXX Remove this so it doesn't have to go thru the macro and return for nothing */
13787 #ifndef GpREFCNT_inc
13788 # define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL)
13792 #define SAVEPV(p) ((p) ? savepv(p) : NULL)
13793 #define SAVEPVN(p,n) ((p) ? savepvn(p,n) : NULL)
13795 /* clone a parser */
13798 Perl_parser_dup(pTHX_ const yy_parser *const proto, CLONE_PARAMS *const param)
13802 PERL_ARGS_ASSERT_PARSER_DUP;
13807 /* look for it in the table first */
13808 parser = (yy_parser *)ptr_table_fetch(PL_ptr_table, proto);
13812 /* create anew and remember what it is */
13813 Newxz(parser, 1, yy_parser);
13814 ptr_table_store(PL_ptr_table, proto, parser);
13816 /* XXX eventually, just Copy() most of the parser struct ? */
13818 parser->lex_brackets = proto->lex_brackets;
13819 parser->lex_casemods = proto->lex_casemods;
13820 parser->lex_brackstack = savepvn(proto->lex_brackstack,
13821 (proto->lex_brackets < 120 ? 120 : proto->lex_brackets));
13822 parser->lex_casestack = savepvn(proto->lex_casestack,
13823 (proto->lex_casemods < 12 ? 12 : proto->lex_casemods));
13824 parser->lex_defer = proto->lex_defer;
13825 parser->lex_dojoin = proto->lex_dojoin;
13826 parser->lex_formbrack = proto->lex_formbrack;
13827 parser->lex_inpat = proto->lex_inpat;
13828 parser->lex_inwhat = proto->lex_inwhat;
13829 parser->lex_op = proto->lex_op;
13830 parser->lex_repl = sv_dup_inc(proto->lex_repl, param);
13831 parser->lex_starts = proto->lex_starts;
13832 parser->lex_stuff = sv_dup_inc(proto->lex_stuff, param);
13833 parser->multi_close = proto->multi_close;
13834 parser->multi_open = proto->multi_open;
13835 parser->multi_start = proto->multi_start;
13836 parser->multi_end = proto->multi_end;
13837 parser->preambled = proto->preambled;
13838 parser->lex_super_state = proto->lex_super_state;
13839 parser->lex_sub_inwhat = proto->lex_sub_inwhat;
13840 parser->lex_sub_op = proto->lex_sub_op;
13841 parser->lex_sub_repl= sv_dup_inc(proto->lex_sub_repl, param);
13842 parser->linestr = sv_dup_inc(proto->linestr, param);
13843 parser->expect = proto->expect;
13844 parser->copline = proto->copline;
13845 parser->last_lop_op = proto->last_lop_op;
13846 parser->lex_state = proto->lex_state;
13847 parser->rsfp = fp_dup(proto->rsfp, '<', param);
13848 /* rsfp_filters entries have fake IoDIRP() */
13849 parser->rsfp_filters= av_dup_inc(proto->rsfp_filters, param);
13850 parser->in_my = proto->in_my;
13851 parser->in_my_stash = hv_dup(proto->in_my_stash, param);
13852 parser->error_count = proto->error_count;
13853 parser->sig_elems = proto->sig_elems;
13854 parser->sig_optelems= proto->sig_optelems;
13855 parser->sig_slurpy = proto->sig_slurpy;
13856 parser->recheck_utf8_validity = proto->recheck_utf8_validity;
13859 char * const ols = SvPVX(proto->linestr);
13860 char * const ls = SvPVX(parser->linestr);
13862 parser->bufptr = ls + (proto->bufptr >= ols ?
13863 proto->bufptr - ols : 0);
13864 parser->oldbufptr = ls + (proto->oldbufptr >= ols ?
13865 proto->oldbufptr - ols : 0);
13866 parser->oldoldbufptr= ls + (proto->oldoldbufptr >= ols ?
13867 proto->oldoldbufptr - ols : 0);
13868 parser->linestart = ls + (proto->linestart >= ols ?
13869 proto->linestart - ols : 0);
13870 parser->last_uni = ls + (proto->last_uni >= ols ?
13871 proto->last_uni - ols : 0);
13872 parser->last_lop = ls + (proto->last_lop >= ols ?
13873 proto->last_lop - ols : 0);
13875 parser->bufend = ls + SvCUR(parser->linestr);
13878 Copy(proto->tokenbuf, parser->tokenbuf, 256, char);
13881 Copy(proto->nextval, parser->nextval, 5, YYSTYPE);
13882 Copy(proto->nexttype, parser->nexttype, 5, I32);
13883 parser->nexttoke = proto->nexttoke;
13885 /* XXX should clone saved_curcop here, but we aren't passed
13886 * proto_perl; so do it in perl_clone_using instead */
13892 =for apidoc_section $io
13895 Duplicate a file handle, returning a pointer to the cloned object.
13901 Perl_fp_dup(pTHX_ PerlIO *const fp, const char type, CLONE_PARAMS *const param)
13905 PERL_ARGS_ASSERT_FP_DUP;
13906 PERL_UNUSED_ARG(type);
13909 return (PerlIO*)NULL;
13911 /* look for it in the table first */
13912 ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp);
13916 /* create anew and remember what it is */
13917 #ifdef __amigaos4__
13918 ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE|PERLIO_DUP_FD);
13920 ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE);
13922 ptr_table_store(PL_ptr_table, fp, ret);
13927 =for apidoc_section $io
13928 =for apidoc dirp_dup
13930 Duplicate a directory handle, returning a pointer to the cloned object.
13936 Perl_dirp_dup(pTHX_ DIR *const dp, CLONE_PARAMS *const param)
13940 #if defined(HAS_FCHDIR) && defined(HAS_TELLDIR) && defined(HAS_SEEKDIR)
13942 const Direntry_t *dirent;
13943 char smallbuf[256]; /* XXX MAXPATHLEN, surely? */
13949 PERL_UNUSED_CONTEXT;
13950 PERL_ARGS_ASSERT_DIRP_DUP;
13955 /* look for it in the table first */
13956 ret = (DIR*)ptr_table_fetch(PL_ptr_table, dp);
13960 #if defined(HAS_FCHDIR) && defined(HAS_TELLDIR) && defined(HAS_SEEKDIR)
13962 PERL_UNUSED_ARG(param);
13966 /* open the current directory (so we can switch back) */
13967 if (!(pwd = PerlDir_open("."))) return (DIR *)NULL;
13969 /* chdir to our dir handle and open the present working directory */
13970 if (fchdir(my_dirfd(dp)) < 0 || !(ret = PerlDir_open("."))) {
13971 PerlDir_close(pwd);
13972 return (DIR *)NULL;
13974 /* Now we should have two dir handles pointing to the same dir. */
13976 /* Be nice to the calling code and chdir back to where we were. */
13977 /* XXX If this fails, then what? */
13978 PERL_UNUSED_RESULT(fchdir(my_dirfd(pwd)));
13980 /* We have no need of the pwd handle any more. */
13981 PerlDir_close(pwd);
13984 # define d_namlen(d) (d)->d_namlen
13986 # define d_namlen(d) strlen((d)->d_name)
13988 /* Iterate once through dp, to get the file name at the current posi-
13989 tion. Then step back. */
13990 pos = PerlDir_tell(dp);
13991 if ((dirent = PerlDir_read(dp))) {
13992 len = d_namlen(dirent);
13993 if (len > sizeof(dirent->d_name) && sizeof(dirent->d_name) > PTRSIZE) {
13994 /* If the len is somehow magically longer than the
13995 * maximum length of the directory entry, even though
13996 * we could fit it in a buffer, we could not copy it
13997 * from the dirent. Bail out. */
13998 PerlDir_close(ret);
14001 if (len <= sizeof smallbuf) name = smallbuf;
14002 else Newx(name, len, char);
14003 Move(dirent->d_name, name, len, char);
14005 PerlDir_seek(dp, pos);
14007 /* Iterate through the new dir handle, till we find a file with the
14009 if (!dirent) /* just before the end */
14011 pos = PerlDir_tell(ret);
14012 if (PerlDir_read(ret)) continue; /* not there yet */
14013 PerlDir_seek(ret, pos); /* step back */
14017 const long pos0 = PerlDir_tell(ret);
14019 pos = PerlDir_tell(ret);
14020 if ((dirent = PerlDir_read(ret))) {
14021 if (len == (STRLEN)d_namlen(dirent)
14022 && memEQ(name, dirent->d_name, len)) {
14024 PerlDir_seek(ret, pos); /* step back */
14027 /* else we are not there yet; keep iterating */
14029 else { /* This is not meant to happen. The best we can do is
14030 reset the iterator to the beginning. */
14031 PerlDir_seek(ret, pos0);
14038 if (name && name != smallbuf)
14043 ret = win32_dirp_dup(dp, param);
14046 /* pop it in the pointer table */
14048 ptr_table_store(PL_ptr_table, dp, ret);
14054 =for apidoc_section $GV
14057 Duplicate a typeglob, returning a pointer to the cloned object.
14063 Perl_gp_dup(pTHX_ GP *const gp, CLONE_PARAMS *const param)
14067 PERL_ARGS_ASSERT_GP_DUP;
14071 /* look for it in the table first */
14072 ret = (GP*)ptr_table_fetch(PL_ptr_table, gp);
14076 /* create anew and remember what it is */
14078 ptr_table_store(PL_ptr_table, gp, ret);
14081 /* ret->gp_refcnt must be 0 before any other dups are called. We're relying
14082 on Newxz() to do this for us. */
14083 ret->gp_sv = sv_dup_inc(gp->gp_sv, param);
14084 ret->gp_io = io_dup_inc(gp->gp_io, param);
14085 ret->gp_form = cv_dup_inc(gp->gp_form, param);
14086 ret->gp_av = av_dup_inc(gp->gp_av, param);
14087 ret->gp_hv = hv_dup_inc(gp->gp_hv, param);
14088 ret->gp_egv = gv_dup(gp->gp_egv, param);/* GvEGV is not refcounted */
14089 ret->gp_cv = cv_dup_inc(gp->gp_cv, param);
14090 ret->gp_cvgen = gp->gp_cvgen;
14091 ret->gp_line = gp->gp_line;
14092 ret->gp_file_hek = hek_dup(gp->gp_file_hek, param);
14098 =for apidoc_section $magic
14101 Duplicate a chain of magic, returning a pointer to the cloned object.
14107 Perl_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *const param)
14109 MAGIC *mgret = NULL;
14110 MAGIC **mgprev_p = &mgret;
14112 PERL_ARGS_ASSERT_MG_DUP;
14114 for (; mg; mg = mg->mg_moremagic) {
14117 if ((param->flags & CLONEf_JOIN_IN)
14118 && mg->mg_type == PERL_MAGIC_backref)
14119 /* when joining, we let the individual SVs add themselves to
14120 * backref as needed. */
14123 Newx(nmg, 1, MAGIC);
14125 mgprev_p = &(nmg->mg_moremagic);
14127 /* There was a comment "XXX copy dynamic vtable?" but as we don't have
14128 dynamic vtables, I'm not sure why Sarathy wrote it. The comment dates
14129 from the original commit adding Perl_mg_dup() - revision 4538.
14130 Similarly there is the annotation "XXX random ptr?" next to the
14131 assignment to nmg->mg_ptr. */
14134 /* FIXME for plugins
14135 if (nmg->mg_type == PERL_MAGIC_qr) {
14136 nmg->mg_obj = MUTABLE_SV(CALLREGDUPE((REGEXP*)nmg->mg_obj, param));
14140 nmg->mg_obj = (nmg->mg_flags & MGf_REFCOUNTED)
14141 ? nmg->mg_type == PERL_MAGIC_backref
14142 /* The backref AV has its reference
14143 * count deliberately bumped by 1 */
14144 ? SvREFCNT_inc(av_dup_inc((const AV *)
14145 nmg->mg_obj, param))
14146 : sv_dup_inc(nmg->mg_obj, param)
14147 : (nmg->mg_type == PERL_MAGIC_regdatum ||
14148 nmg->mg_type == PERL_MAGIC_regdata)
14150 : sv_dup(nmg->mg_obj, param);
14152 if (nmg->mg_ptr && nmg->mg_type != PERL_MAGIC_regex_global) {
14153 if (nmg->mg_len > 0) {
14154 nmg->mg_ptr = SAVEPVN(nmg->mg_ptr, nmg->mg_len);
14155 if (nmg->mg_type == PERL_MAGIC_overload_table &&
14156 AMT_AMAGIC((AMT*)nmg->mg_ptr))
14158 AMT * const namtp = (AMT*)nmg->mg_ptr;
14159 sv_dup_inc_multiple((SV**)(namtp->table),
14160 (SV**)(namtp->table), NofAMmeth, param);
14163 else if (nmg->mg_len == HEf_SVKEY)
14164 nmg->mg_ptr = (char*)sv_dup_inc((const SV *)nmg->mg_ptr, param);
14166 if ((nmg->mg_flags & MGf_DUP) && nmg->mg_virtual && nmg->mg_virtual->svt_dup) {
14167 nmg->mg_virtual->svt_dup(aTHX_ nmg, param);
14173 #endif /* USE_ITHREADS */
14175 struct ptr_tbl_arena {
14176 struct ptr_tbl_arena *next;
14177 struct ptr_tbl_ent array[1023/3]; /* as ptr_tbl_ent has 3 pointers. */
14181 =for apidoc_section $embedding
14182 =for apidoc ptr_table_new
14184 Create a new pointer-mapping table
14190 Perl_ptr_table_new(pTHX)
14193 PERL_UNUSED_CONTEXT;
14195 Newx(tbl, 1, PTR_TBL_t);
14196 tbl->tbl_max = 511;
14197 tbl->tbl_items = 0;
14198 tbl->tbl_arena = NULL;
14199 tbl->tbl_arena_next = NULL;
14200 tbl->tbl_arena_end = NULL;
14201 Newxz(tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*);
14205 #define PTR_TABLE_HASH(ptr) \
14206 ((PTR2UV(ptr) >> 3) ^ (PTR2UV(ptr) >> (3 + 7)) ^ (PTR2UV(ptr) >> (3 + 17)))
14208 /* map an existing pointer using a table */
14210 STATIC PTR_TBL_ENT_t *
14211 S_ptr_table_find(PTR_TBL_t *const tbl, const void *const sv)
14213 PTR_TBL_ENT_t *tblent;
14214 const UV hash = PTR_TABLE_HASH(sv);
14216 PERL_ARGS_ASSERT_PTR_TABLE_FIND;
14218 tblent = tbl->tbl_ary[hash & tbl->tbl_max];
14219 for (; tblent; tblent = tblent->next) {
14220 if (tblent->oldval == sv)
14227 =for apidoc ptr_table_fetch
14229 Look for C<sv> in the pointer-mapping table C<tbl>, returning its value, or
14236 Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *const tbl, const void *const sv)
14238 PTR_TBL_ENT_t const *const tblent = ptr_table_find(tbl, sv);
14240 PERL_ARGS_ASSERT_PTR_TABLE_FETCH;
14241 PERL_UNUSED_CONTEXT;
14243 return tblent ? tblent->newval : NULL;
14247 =for apidoc ptr_table_store
14249 Add a new entry to a pointer-mapping table C<tbl>.
14250 In hash terms, C<oldsv> is the key; Cnewsv> is the value.
14252 The names "old" and "new" are specific to the core's typical use of ptr_tables
14259 Perl_ptr_table_store(pTHX_ PTR_TBL_t *const tbl, const void *const oldsv, void *const newsv)
14261 PTR_TBL_ENT_t *tblent = ptr_table_find(tbl, oldsv);
14263 PERL_ARGS_ASSERT_PTR_TABLE_STORE;
14264 PERL_UNUSED_CONTEXT;
14267 tblent->newval = newsv;
14269 const UV entry = PTR_TABLE_HASH(oldsv) & tbl->tbl_max;
14271 if (tbl->tbl_arena_next == tbl->tbl_arena_end) {
14272 struct ptr_tbl_arena *new_arena;
14274 Newx(new_arena, 1, struct ptr_tbl_arena);
14275 new_arena->next = tbl->tbl_arena;
14276 tbl->tbl_arena = new_arena;
14277 tbl->tbl_arena_next = new_arena->array;
14278 tbl->tbl_arena_end = C_ARRAY_END(new_arena->array);
14281 tblent = tbl->tbl_arena_next++;
14283 tblent->oldval = oldsv;
14284 tblent->newval = newsv;
14285 tblent->next = tbl->tbl_ary[entry];
14286 tbl->tbl_ary[entry] = tblent;
14288 if (tblent->next && tbl->tbl_items > tbl->tbl_max)
14289 ptr_table_split(tbl);
14294 =for apidoc ptr_table_split
14296 Double the hash bucket size of an existing ptr table
14302 Perl_ptr_table_split(pTHX_ PTR_TBL_t *const tbl)
14304 PTR_TBL_ENT_t **ary = tbl->tbl_ary;
14305 const UV oldsize = tbl->tbl_max + 1;
14306 UV newsize = oldsize * 2;
14309 PERL_ARGS_ASSERT_PTR_TABLE_SPLIT;
14310 PERL_UNUSED_CONTEXT;
14312 Renew(ary, newsize, PTR_TBL_ENT_t*);
14313 Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*);
14314 tbl->tbl_max = --newsize;
14315 tbl->tbl_ary = ary;
14316 for (i=0; i < oldsize; i++, ary++) {
14317 PTR_TBL_ENT_t **entp = ary;
14318 PTR_TBL_ENT_t *ent = *ary;
14319 PTR_TBL_ENT_t **curentp;
14322 curentp = ary + oldsize;
14324 if ((newsize & PTR_TABLE_HASH(ent->oldval)) != i) {
14326 ent->next = *curentp;
14337 =for apidoc ptr_table_free
14339 Clear and free a ptr table
14345 Perl_ptr_table_free(pTHX_ PTR_TBL_t *const tbl)
14347 struct ptr_tbl_arena *arena;
14349 PERL_UNUSED_CONTEXT;
14355 arena = tbl->tbl_arena;
14358 struct ptr_tbl_arena *next = arena->next;
14364 Safefree(tbl->tbl_ary);
14368 #if defined(USE_ITHREADS)
14371 Perl_rvpv_dup(pTHX_ SV *const dsv, const SV *const ssv, CLONE_PARAMS *const param)
14373 PERL_ARGS_ASSERT_RVPV_DUP;
14375 assert(!isREGEXP(ssv));
14377 if (SvWEAKREF(ssv)) {
14378 SvRV_set(dsv, sv_dup(SvRV_const(ssv), param));
14379 if (param->flags & CLONEf_JOIN_IN) {
14380 /* if joining, we add any back references individually rather
14381 * than copying the whole backref array */
14382 Perl_sv_add_backref(aTHX_ SvRV(dsv), dsv);
14386 SvRV_set(dsv, sv_dup_inc(SvRV_const(ssv), param));
14388 else if (SvPVX_const(ssv)) {
14389 /* Has something there */
14391 /* Normal PV - clone whole allocated space */
14392 SvPV_set(dsv, SAVEPVN(SvPVX_const(ssv), SvLEN(ssv)-1));
14393 /* ssv may not be that normal, but actually copy on write.
14394 But we are a true, independent SV, so: */
14398 /* Special case - not normally malloced for some reason */
14399 if (isGV_with_GP(ssv)) {
14400 /* Don't need to do anything here. */
14402 else if ((SvIsCOW_shared_hash(ssv))) {
14403 /* A "shared" PV - clone it as "shared" PV */
14405 HEK_KEY(hek_dup(SvSHARED_HEK_FROM_PV(SvPVX_const(ssv)),
14409 /* Some other special case - random pointer */
14410 SvPV_set(dsv, (char *) SvPVX_const(ssv));
14415 /* Copy the NULL */
14416 SvPV_set(dsv, NULL);
14420 /* duplicate a list of SVs. source and dest may point to the same memory. */
14422 S_sv_dup_inc_multiple(pTHX_ SV *const *source, SV **dest,
14423 SSize_t items, CLONE_PARAMS *const param)
14425 PERL_ARGS_ASSERT_SV_DUP_INC_MULTIPLE;
14427 while (items-- > 0) {
14428 *dest++ = sv_dup_inc(*source++, param);
14434 /* duplicate the HvAUX of an HV */
14436 S_sv_dup_hvaux(pTHX_ const SV *const ssv, SV *dsv, CLONE_PARAMS *const param)
14438 PERL_ARGS_ASSERT_SV_DUP_HVAUX;
14440 const struct xpvhv_aux * const saux = HvAUX(ssv);
14441 struct xpvhv_aux * const daux = HvAUX(dsv);
14442 /* This flag isn't copied. */
14443 SvFLAGS(dsv) |= SVphv_HasAUX;
14445 if (saux->xhv_name_count) {
14446 HEK ** const sname = saux->xhv_name_u.xhvnameu_names;
14447 const I32 count = saux->xhv_name_count < 0
14448 ? -saux->xhv_name_count
14449 : saux->xhv_name_count;
14450 HEK **shekp = sname + count;
14452 Newx(daux->xhv_name_u.xhvnameu_names, count, HEK *);
14453 dhekp = daux->xhv_name_u.xhvnameu_names + count;
14454 while (shekp-- > sname) {
14456 *dhekp = hek_dup(*shekp, param);
14460 daux->xhv_name_u.xhvnameu_name = hek_dup(saux->xhv_name_u.xhvnameu_name, param);
14462 daux->xhv_name_count = saux->xhv_name_count;
14464 daux->xhv_aux_flags = saux->xhv_aux_flags;
14465 #ifdef PERL_HASH_RANDOMIZE_KEYS
14466 daux->xhv_rand = saux->xhv_rand;
14467 daux->xhv_last_rand = saux->xhv_last_rand;
14469 daux->xhv_riter = saux->xhv_riter;
14470 daux->xhv_eiter = saux->xhv_eiter ? he_dup(saux->xhv_eiter, FALSE, param) : 0;
14471 /* backref array needs refcnt=2; see sv_add_backref */
14472 daux->xhv_backreferences =
14473 (param->flags & CLONEf_JOIN_IN)
14474 /* when joining, we let the individual GVs and
14475 * CVs add themselves to backref as
14476 * needed. This avoids pulling in stuff
14477 * that isn't required, and simplifies the
14478 * case where stashes aren't cloned back
14479 * if they already exist in the parent
14482 : saux->xhv_backreferences
14483 ? (SvTYPE(saux->xhv_backreferences) == SVt_PVAV)
14484 ? MUTABLE_AV(SvREFCNT_inc(
14485 sv_dup_inc((const SV *)
14486 saux->xhv_backreferences, param)))
14487 : MUTABLE_AV(sv_dup((const SV *)
14488 saux->xhv_backreferences, param))
14491 daux->xhv_mro_meta = saux->xhv_mro_meta
14492 ? mro_meta_dup(saux->xhv_mro_meta, param)
14495 /* Record stashes for possible cloning in Perl_clone(). */
14497 av_push(param->stashes, dsv);
14499 if (HvSTASH_IS_CLASS(ssv)) {
14500 daux->xhv_class_superclass = hv_dup_inc(saux->xhv_class_superclass, param);
14501 daux->xhv_class_initfields_cv = cv_dup_inc(saux->xhv_class_initfields_cv, param);
14502 daux->xhv_class_adjust_blocks = av_dup_inc(saux->xhv_class_adjust_blocks, param);
14503 daux->xhv_class_fields = padnamelist_dup_inc(saux->xhv_class_fields, param);
14504 daux->xhv_class_next_fieldix = saux->xhv_class_next_fieldix;
14505 daux->xhv_class_param_map = hv_dup_inc(saux->xhv_class_param_map, param);
14507 /* TODO: This does mean that we can't compile more `field` expressions
14508 * in the cloned thread, but surely we're done with compiletime now..?
14510 daux->xhv_class_suspended_initfields_compcv = NULL;
14514 /* duplicate an SV of any type (including AV, HV etc) */
14517 S_sv_dup_common(pTHX_ const SV *const ssv, CLONE_PARAMS *const param)
14521 PERL_ARGS_ASSERT_SV_DUP_COMMON;
14523 if (SvIS_FREED(ssv)) {
14524 #ifdef DEBUG_LEAKING_SCALARS_ABORT
14529 /* look for it in the table first */
14530 dsv = MUTABLE_SV(ptr_table_fetch(PL_ptr_table, ssv));
14534 if(param->flags & CLONEf_JOIN_IN) {
14535 /** We are joining here so we don't want do clone
14536 something that is bad **/
14537 if (SvTYPE(ssv) == SVt_PVHV) {
14538 const HEK * const hvname = HvNAME_HEK(ssv);
14540 /** don't clone stashes if they already exist **/
14541 dsv = MUTABLE_SV(gv_stashpvn(HEK_KEY(hvname), HEK_LEN(hvname),
14542 HEK_UTF8(hvname) ? SVf_UTF8 : 0));
14543 ptr_table_store(PL_ptr_table, ssv, dsv);
14547 else if (SvTYPE(ssv) == SVt_PVGV && !SvFAKE(ssv)) {
14548 HV *stash = GvSTASH(ssv);
14549 const HEK * hvname;
14550 if (stash && (hvname = HvNAME_HEK(stash))) {
14551 /** don't clone GVs if they already exist **/
14553 stash = gv_stashpvn(HEK_KEY(hvname), HEK_LEN(hvname),
14554 HEK_UTF8(hvname) ? SVf_UTF8 : 0);
14556 stash, GvNAME(ssv),
14562 if (svp && *svp && SvTYPE(*svp) == SVt_PVGV) {
14563 ptr_table_store(PL_ptr_table, ssv, *svp);
14570 /* create anew and remember what it is */
14573 #ifdef DEBUG_LEAKING_SCALARS
14574 dsv->sv_debug_optype = ssv->sv_debug_optype;
14575 dsv->sv_debug_line = ssv->sv_debug_line;
14576 dsv->sv_debug_inpad = ssv->sv_debug_inpad;
14577 dsv->sv_debug_parent = (SV*)ssv;
14578 FREE_SV_DEBUG_FILE(dsv);
14579 dsv->sv_debug_file = savesharedpv(ssv->sv_debug_file);
14582 ptr_table_store(PL_ptr_table, ssv, dsv);
14585 SvFLAGS(dsv) = SvFLAGS(ssv);
14586 SvFLAGS(dsv) &= ~SVf_OOK; /* don't propagate OOK hack */
14587 SvREFCNT(dsv) = 0; /* must be before any other dups! */
14590 if (SvANY(ssv) && PL_watch_pvx && SvPVX_const(ssv) == PL_watch_pvx)
14591 PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n",
14592 (void*)PL_watch_pvx, SvPVX_const(ssv));
14595 /* don't clone objects whose class has asked us not to */
14597 && ! (SvFLAGS(SvSTASH(ssv)) & SVphv_CLONEABLE))
14603 switch (SvTYPE(ssv)) {
14608 SET_SVANY_FOR_BODYLESS_IV(dsv);
14610 Perl_rvpv_dup(aTHX_ dsv, ssv, param);
14612 SvIV_set(dsv, SvIVX(ssv));
14616 #if NVSIZE <= IVSIZE
14617 SET_SVANY_FOR_BODYLESS_NV(dsv);
14619 SvANY(dsv) = new_XNV();
14621 SvNV_set(dsv, SvNVX(ssv));
14625 /* These are all the types that need complex bodies allocating. */
14627 const svtype sv_type = SvTYPE(ssv);
14628 const struct body_details *sv_type_details
14629 = bodies_by_type + sv_type;
14633 Perl_croak(param->proto_perl, "Bizarre SvTYPE [%" IVdf "]", (IV)SvTYPE(ssv));
14634 NOT_REACHED; /* NOTREACHED */
14638 if (HvHasAUX(ssv)) {
14639 sv_type_details = &fake_hv_with_aux;
14641 new_body = new_NOARENA(sv_type_details);
14643 new_body_from_arena(new_body, HVAUX_ARENA_ROOT_IX, fake_hv_with_aux);
14661 assert(sv_type_details->body_size);
14663 if (sv_type_details->arena) {
14664 new_body = S_new_body(aTHX_ sv_type);
14666 = (void*)((char*)new_body - sv_type_details->offset);
14670 new_body = new_NOARENA(sv_type_details);
14675 SvANY(dsv) = new_body;
14678 Copy(((char*)SvANY(ssv)) + sv_type_details->offset,
14679 ((char*)SvANY(dsv)) + sv_type_details->offset,
14680 sv_type_details->copy, char);
14682 Copy(((char*)SvANY(ssv)),
14683 ((char*)SvANY(dsv)),
14684 sv_type_details->body_size + sv_type_details->offset, char);
14687 if (sv_type != SVt_PVAV && sv_type != SVt_PVHV && sv_type != SVt_PVOBJ
14688 && !isGV_with_GP(dsv)
14690 && !(sv_type == SVt_PVIO && !(IoFLAGS(dsv) & IOf_FAKE_DIRP)))
14691 Perl_rvpv_dup(aTHX_ dsv, ssv, param);
14693 /* The Copy above means that all the source (unduplicated) pointers
14694 are now in the destination. We can check the flags and the
14695 pointers in either, but it's possible that there's less cache
14696 missing by always going for the destination.
14697 FIXME - instrument and check that assumption */
14698 if (sv_type >= SVt_PVMG) {
14700 SvMAGIC_set(dsv, mg_dup(SvMAGIC(dsv), param));
14701 if (SvOBJECT(dsv) && SvSTASH(dsv))
14702 SvSTASH_set(dsv, hv_dup_inc(SvSTASH(dsv), param));
14703 else SvSTASH_set(dsv, 0); /* don't copy DESTROY cache */
14706 /* The cast silences a GCC warning about unhandled types. */
14707 switch ((int)sv_type) {
14718 /* FIXME for plugins */
14719 re_dup_guts((REGEXP*) ssv, (REGEXP*) dsv, param);
14722 /* XXX LvTARGOFF sometimes holds PMOP* when DEBUGGING */
14723 if (LvTYPE(dsv) == 't') /* for tie: unrefcnted fake (SV**) */
14725 else if (LvTYPE(dsv) == 'T') /* for tie: fake HE */
14726 LvTARG(dsv) = MUTABLE_SV(he_dup((HE*)LvTARG(dsv), FALSE, param));
14728 LvTARG(dsv) = sv_dup_inc(LvTARG(dsv), param);
14729 if (isREGEXP(ssv)) goto duprex;
14732 /* non-GP case already handled above */
14733 if(isGV_with_GP(ssv)) {
14734 GvNAME_HEK(dsv) = hek_dup(GvNAME_HEK(dsv), param);
14735 /* Don't call sv_add_backref here as it's going to be
14736 created as part of the magic cloning of the symbol
14737 table--unless this is during a join and the stash
14738 is not actually being cloned. */
14739 /* Danger Will Robinson - GvGP(dsv) isn't initialised
14740 at the point of this comment. */
14741 GvSTASH(dsv) = hv_dup(GvSTASH(dsv), param);
14742 if (param->flags & CLONEf_JOIN_IN)
14743 Perl_sv_add_backref(aTHX_ MUTABLE_SV(GvSTASH(dsv)), dsv);
14744 GvGP_set(dsv, gp_dup(GvGP(ssv), param));
14745 (void)GpREFCNT_inc(GvGP(dsv));
14749 /* PL_parser->rsfp_filters entries have fake IoDIRP() */
14750 if(IoFLAGS(dsv) & IOf_FAKE_DIRP) {
14751 /* I have no idea why fake dirp (rsfps)
14752 should be treated differently but otherwise
14753 we end up with leaks -- sky*/
14754 IoTOP_GV(dsv) = gv_dup_inc(IoTOP_GV(dsv), param);
14755 IoFMT_GV(dsv) = gv_dup_inc(IoFMT_GV(dsv), param);
14756 IoBOTTOM_GV(dsv) = gv_dup_inc(IoBOTTOM_GV(dsv), param);
14758 IoTOP_GV(dsv) = gv_dup(IoTOP_GV(dsv), param);
14759 IoFMT_GV(dsv) = gv_dup(IoFMT_GV(dsv), param);
14760 IoBOTTOM_GV(dsv) = gv_dup(IoBOTTOM_GV(dsv), param);
14762 IoDIRP(dsv) = dirp_dup(IoDIRP(dsv), param);
14765 /* IoDIRP(dsv) is already a copy of IoDIRP(ssv) */
14767 IoIFP(dsv) = fp_dup(IoIFP(ssv), IoTYPE(dsv), param);
14769 if (IoOFP(dsv) == IoIFP(ssv))
14770 IoOFP(dsv) = IoIFP(dsv);
14772 IoOFP(dsv) = fp_dup(IoOFP(dsv), IoTYPE(dsv), param);
14773 IoTOP_NAME(dsv) = SAVEPV(IoTOP_NAME(dsv));
14774 IoFMT_NAME(dsv) = SAVEPV(IoFMT_NAME(dsv));
14775 IoBOTTOM_NAME(dsv) = SAVEPV(IoBOTTOM_NAME(dsv));
14778 /* avoid cloning an empty array */
14779 if (AvARRAY((const AV *)ssv) && AvFILLp((const AV *)ssv) >= 0) {
14780 SV **dst_ary, **src_ary;
14781 SSize_t items = AvFILLp((const AV *)ssv) + 1;
14783 src_ary = AvARRAY((const AV *)ssv);
14784 Newx(dst_ary, AvMAX((const AV *)ssv)+1, SV*);
14785 ptr_table_store(PL_ptr_table, src_ary, dst_ary);
14786 AvARRAY(MUTABLE_AV(dsv)) = dst_ary;
14787 AvALLOC((const AV *)dsv) = dst_ary;
14788 if (AvREAL((const AV *)ssv)) {
14789 dst_ary = sv_dup_inc_multiple(src_ary, dst_ary, items,
14793 while (items-- > 0)
14794 *dst_ary++ = sv_dup(*src_ary++, param);
14796 items = AvMAX((const AV *)ssv) - AvFILLp((const AV *)ssv);
14797 while (items-- > 0) {
14802 AvARRAY(MUTABLE_AV(dsv)) = NULL;
14803 AvALLOC((const AV *)dsv) = (SV**)NULL;
14804 AvMAX( (const AV *)dsv) = -1;
14805 AvFILLp((const AV *)dsv) = -1;
14809 if (HvARRAY((const HV *)ssv)) {
14811 XPVHV * const dxhv = (XPVHV*)SvANY(dsv);
14812 XPVHV * const sxhv = (XPVHV*)SvANY(ssv);
14814 Newx(darray, PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1),
14816 HvARRAY(dsv) = (HE**)darray;
14817 while (i <= sxhv->xhv_max) {
14818 const HE * const source = HvARRAY(ssv)[i];
14819 HvARRAY(dsv)[i] = source
14820 ? he_dup(source, FALSE, param) : 0;
14824 sv_dup_hvaux(ssv, dsv, param);
14827 HvARRAY(MUTABLE_HV(dsv)) = NULL;
14830 if (!(param->flags & CLONEf_COPY_STACKS)) {
14835 /* NOTE: not refcounted */
14836 SvANY(MUTABLE_CV(dsv))->xcv_stash =
14837 hv_dup(CvSTASH(dsv), param);
14838 if ((param->flags & CLONEf_JOIN_IN) && CvSTASH(dsv))
14839 Perl_sv_add_backref(aTHX_ MUTABLE_SV(CvSTASH(dsv)), dsv);
14840 if (!CvISXSUB(dsv)) {
14842 CvROOT(dsv) = OpREFCNT_inc(CvROOT(dsv));
14844 CvSLABBED_off(dsv);
14845 } else if (CvCONST(dsv)) {
14846 CvXSUBANY(dsv).any_ptr =
14847 sv_dup_inc((const SV *)CvXSUBANY(dsv).any_ptr, param);
14848 } else if (CvREFCOUNTED_ANYSV(dsv)) {
14849 CvXSUBANY(dsv).any_sv =
14850 sv_dup_inc((const SV *)CvXSUBANY(dsv).any_sv, param);
14852 assert(!CvSLABBED(dsv));
14853 if (CvDYNFILE(dsv)) CvFILE(dsv) = SAVEPV(CvFILE(dsv));
14855 SvANY((CV *)dsv)->xcv_gv_u.xcv_hek =
14856 hek_dup(CvNAME_HEK((CV *)ssv), param);
14857 /* don't dup if copying back - CvGV isn't refcounted, so the
14858 * duped GV may never be freed. A bit of a hack! DAPM */
14860 SvANY(MUTABLE_CV(dsv))->xcv_gv_u.xcv_gv =
14862 ? gv_dup_inc(CvGV(ssv), param)
14863 : (param->flags & CLONEf_JOIN_IN)
14865 : gv_dup(CvGV(ssv), param);
14867 if (!CvISXSUB(ssv)) {
14868 PADLIST * padlist = CvPADLIST(ssv);
14870 padlist = padlist_dup(padlist, param);
14871 CvPADLIST_set(dsv, padlist);
14873 /* unthreaded perl can't sv_dup so we don't support unthreaded's CvHSCXT */
14874 PoisonPADLIST(dsv);
14878 ? cv_dup( CvOUTSIDE(dsv), param)
14879 : cv_dup_inc(CvOUTSIDE(dsv), param);
14883 Size_t fieldcount = ObjectMAXFIELD(ssv) + 1;
14885 Newx(ObjectFIELDS(dsv), fieldcount, SV *);
14886 ObjectMAXFIELD(dsv) = fieldcount - 1;
14888 sv_dup_inc_multiple(ObjectFIELDS(ssv), ObjectFIELDS(dsv), fieldcount, param);
14899 Perl_sv_dup_inc(pTHX_ const SV *const ssv, CLONE_PARAMS *const param)
14901 PERL_ARGS_ASSERT_SV_DUP_INC;
14902 return ssv ? SvREFCNT_inc(sv_dup_common(ssv, param)) : NULL;
14906 Perl_sv_dup(pTHX_ const SV *const ssv, CLONE_PARAMS *const param)
14908 SV *dsv = ssv ? sv_dup_common(ssv, param) : NULL;
14909 PERL_ARGS_ASSERT_SV_DUP;
14911 /* Track every SV that (at least initially) had a reference count of 0.
14912 We need to do this by holding an actual reference to it in this array.
14913 If we attempt to cheat, turn AvREAL_off(), and store only pointers
14914 (akin to the stashes hash, and the perl stack), we come unstuck if
14915 a weak reference (or other SV legitimately SvREFCNT() == 0 for this
14916 thread) is manipulated in a CLONE method, because CLONE runs before the
14917 unreferenced array is walked to find SVs still with SvREFCNT() == 0
14918 (and fix things up by giving each a reference via the temps stack).
14919 Instead, during CLONE, if the 0-referenced SV has SvREFCNT_inc() and
14920 then SvREFCNT_dec(), it will be cleaned up (and added to the free list)
14921 before the walk of unreferenced happens and a reference to that is SV
14922 added to the temps stack. At which point we have the same SV considered
14923 to be in use, and free to be re-used. Not good.
14925 if (dsv && !(param->flags & CLONEf_COPY_STACKS) && !SvREFCNT(dsv)) {
14926 assert(param->unreferenced);
14927 av_push(param->unreferenced, SvREFCNT_inc(dsv));
14933 /* duplicate a context */
14936 Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max, CLONE_PARAMS* param)
14938 PERL_CONTEXT *ncxs;
14940 PERL_ARGS_ASSERT_CX_DUP;
14943 return (PERL_CONTEXT*)NULL;
14945 /* look for it in the table first */
14946 ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs);
14950 /* create anew and remember what it is */
14951 Newx(ncxs, max + 1, PERL_CONTEXT);
14952 ptr_table_store(PL_ptr_table, cxs, ncxs);
14953 Copy(cxs, ncxs, max + 1, PERL_CONTEXT);
14956 PERL_CONTEXT * const ncx = &ncxs[ix];
14957 if (CxTYPE(ncx) == CXt_SUBST) {
14958 Perl_croak(aTHX_ "Cloning substitution context is unimplemented");
14961 ncx->blk_oldcop = (COP*)any_dup(ncx->blk_oldcop, param->proto_perl);
14962 switch (CxTYPE(ncx)) {
14964 ncx->blk_sub.cv = cv_dup_inc(ncx->blk_sub.cv, param);
14965 if(CxHASARGS(ncx)){
14966 ncx->blk_sub.savearray = av_dup_inc(ncx->blk_sub.savearray,param);
14968 ncx->blk_sub.savearray = NULL;
14970 ncx->blk_sub.prevcomppad = (PAD*)ptr_table_fetch(PL_ptr_table,
14971 ncx->blk_sub.prevcomppad);
14974 ncx->blk_eval.old_namesv = sv_dup_inc(ncx->blk_eval.old_namesv,
14976 /* XXX should this sv_dup_inc? Or only if CxEVAL_TXT_REFCNTED ???? */
14977 ncx->blk_eval.cur_text = sv_dup(ncx->blk_eval.cur_text, param);
14978 ncx->blk_eval.cv = cv_dup(ncx->blk_eval.cv, param);
14979 /* XXX what to do with cur_top_env ???? */
14981 case CXt_LOOP_LAZYSV:
14982 ncx->blk_loop.state_u.lazysv.end
14983 = sv_dup_inc(ncx->blk_loop.state_u.lazysv.end, param);
14984 /* Fallthrough: duplicate lazysv.cur by using the ary.ary
14985 duplication code instead.
14986 We are taking advantage of (1) av_dup_inc and sv_dup_inc
14987 actually being the same function, and (2) order
14988 equivalence of the two unions.
14989 We can assert the later [but only at run time :-(] */
14990 assert ((void *) &ncx->blk_loop.state_u.ary.ary ==
14991 (void *) &ncx->blk_loop.state_u.lazysv.cur);
14994 ncx->blk_loop.state_u.ary.ary
14995 = av_dup_inc(ncx->blk_loop.state_u.ary.ary, param);
14997 case CXt_LOOP_LIST:
14998 case CXt_LOOP_LAZYIV:
14999 /* code common to all 'for' CXt_LOOP_* types */
15000 ncx->blk_loop.itersave =
15001 sv_dup_inc(ncx->blk_loop.itersave, param);
15002 if (CxPADLOOP(ncx)) {
15003 PADOFFSET off = ncx->blk_loop.itervar_u.svp
15004 - &CX_CURPAD_SV(ncx->blk_loop, 0);
15005 ncx->blk_loop.oldcomppad =
15006 (PAD*)ptr_table_fetch(PL_ptr_table,
15007 ncx->blk_loop.oldcomppad);
15008 ncx->blk_loop.itervar_u.svp =
15009 &CX_CURPAD_SV(ncx->blk_loop, off);
15012 /* this copies the GV if CXp_FOR_GV, or the SV for an
15013 * alias (for \$x (...)) - relies on gv_dup being the
15014 * same as sv_dup */
15015 ncx->blk_loop.itervar_u.gv
15016 = gv_dup((const GV *)ncx->blk_loop.itervar_u.gv,
15020 case CXt_LOOP_PLAIN:
15023 ncx->blk_format.prevcomppad =
15024 (PAD*)ptr_table_fetch(PL_ptr_table,
15025 ncx->blk_format.prevcomppad);
15026 ncx->blk_format.cv = cv_dup_inc(ncx->blk_format.cv, param);
15027 ncx->blk_format.gv = gv_dup(ncx->blk_format.gv, param);
15028 ncx->blk_format.dfoutgv = gv_dup_inc(ncx->blk_format.dfoutgv,
15032 ncx->blk_givwhen.defsv_save =
15033 sv_dup_inc(ncx->blk_givwhen.defsv_save, param);
15050 Duplicate a stack info structure, returning a pointer to the cloned object.
15056 Perl_si_dup(pTHX_ PERL_SI *si, CLONE_PARAMS* param)
15060 PERL_ARGS_ASSERT_SI_DUP;
15063 return (PERL_SI*)NULL;
15065 /* look for it in the table first */
15066 nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si);
15070 /* create anew and remember what it is */
15071 Newx(nsi, 1, PERL_SI);
15072 ptr_table_store(PL_ptr_table, si, nsi);
15074 nsi->si_stack = av_dup_inc(si->si_stack, param);
15075 nsi->si_cxix = si->si_cxix;
15076 nsi->si_cxsubix = si->si_cxsubix;
15077 nsi->si_cxmax = si->si_cxmax;
15078 nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax, param);
15079 nsi->si_type = si->si_type;
15080 nsi->si_prev = si_dup(si->si_prev, param);
15081 nsi->si_next = si_dup(si->si_next, param);
15082 nsi->si_markoff = si->si_markoff;
15083 #ifdef PERL_RC_STACK
15084 nsi->si_stack_nonrc_base = si->si_stack_nonrc_base;
15086 #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
15087 nsi->si_stack_hwm = 0;
15093 #define POPINT(ss,ix) ((ss)[--(ix)].any_i32)
15094 #define TOPINT(ss,ix) ((ss)[ix].any_i32)
15095 #define POPLONG(ss,ix) ((ss)[--(ix)].any_long)
15096 #define TOPLONG(ss,ix) ((ss)[ix].any_long)
15097 #define POPIV(ss,ix) ((ss)[--(ix)].any_iv)
15098 #define TOPIV(ss,ix) ((ss)[ix].any_iv)
15099 #define POPUV(ss,ix) ((ss)[--(ix)].any_uv)
15100 #define TOPUV(ss,ix) ((ss)[ix].any_uv)
15101 #define POPBOOL(ss,ix) ((ss)[--(ix)].any_bool)
15102 #define TOPBOOL(ss,ix) ((ss)[ix].any_bool)
15103 #define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr)
15104 #define TOPPTR(ss,ix) ((ss)[ix].any_ptr)
15105 #define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr)
15106 #define TOPDPTR(ss,ix) ((ss)[ix].any_dptr)
15107 #define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr)
15108 #define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr)
15111 #define pv_dup_inc(p) SAVEPV(p)
15112 #define pv_dup(p) SAVEPV(p)
15113 #define svp_dup_inc(p,pp) any_dup(p,pp)
15115 /* map any object to the new equivalent - either something in the
15116 * ptr table, or something in the interpreter structure
15120 Perl_any_dup(pTHX_ void *v, const PerlInterpreter *proto_perl)
15124 PERL_ARGS_ASSERT_ANY_DUP;
15127 return (void*)NULL;
15129 /* look for it in the table first */
15130 ret = ptr_table_fetch(PL_ptr_table, v);
15134 /* see if it is part of the interpreter structure */
15135 if (v >= (void*)proto_perl && v < (void*)(proto_perl+1))
15136 ret = (void*)(((char*)aTHX) + (((char*)v) - (char*)proto_perl));
15147 Duplicate the save stack, returning a pointer to the cloned object.
15153 Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl, CLONE_PARAMS* param)
15155 ANY * const ss = proto_perl->Isavestack;
15156 const I32 max = proto_perl->Isavestack_max + SS_MAXPUSH;
15157 I32 ix = proto_perl->Isavestack_ix;
15163 char *pv; /* no const deliberately */
15171 void (*dptr) (void*);
15172 void (*dxptr) (pTHX_ void*);
15174 PERL_ARGS_ASSERT_SS_DUP;
15176 Newx(nss, max, ANY);
15179 const UV uv = POPUV(ss,ix);
15180 const U8 type = (U8)uv & SAVE_MASK;
15182 TOPUV(nss,ix) = uv;
15184 case SAVEt_CLEARSV:
15185 case SAVEt_CLEARPADRANGE:
15187 case SAVEt_HELEM: /* hash element */
15188 case SAVEt_SV: /* scalar reference */
15189 sv = (const SV *)POPPTR(ss,ix);
15190 TOPPTR(nss,ix) = SvREFCNT_inc(sv_dup_inc(sv, param));
15192 case SAVEt_ITEM: /* normal string */
15193 case SAVEt_GVSV: /* scalar slot in GV */
15194 sv = (const SV *)POPPTR(ss,ix);
15195 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15196 if (type == SAVEt_SV)
15200 case SAVEt_MORTALIZESV:
15201 case SAVEt_READONLY_OFF:
15202 sv = (const SV *)POPPTR(ss,ix);
15203 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15205 case SAVEt_FREEPADNAME:
15206 ptr = POPPTR(ss,ix);
15207 TOPPTR(nss,ix) = padname_dup((PADNAME *)ptr, param);
15208 PadnameREFCNT((PADNAME *)TOPPTR(nss,ix))++;
15210 case SAVEt_SHARED_PVREF: /* char* in shared space */
15211 c = (char*)POPPTR(ss,ix);
15212 TOPPTR(nss,ix) = savesharedpv(c);
15213 ptr = POPPTR(ss,ix);
15214 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15216 case SAVEt_GENERIC_SVREF: /* generic sv */
15217 case SAVEt_SVREF: /* scalar reference */
15218 sv = (const SV *)POPPTR(ss,ix);
15219 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15220 if (type == SAVEt_SVREF)
15221 SvREFCNT_inc_simple_void((SV *)TOPPTR(nss,ix));
15222 ptr = POPPTR(ss,ix);
15223 TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
15224 /* this feels very strange, we have a **SV from one thread,
15225 * we copy the SV, but dont change the **SV. But in this thread
15226 * the target of the **SV could be something from the *other* thread.
15227 * So how can this possibly work correctly? */
15230 pv = (char *)POPPTR(ss,ix);
15231 TOPPTR(nss,ix) = rcpv_copy(pv);
15232 ptr = POPPTR(ss,ix);
15233 (void)rcpv_copy(*((char **)ptr));
15234 TOPPTR(nss,ix) = ptr;
15235 /* XXXXX: see comment above. */
15237 case SAVEt_GVSLOT: /* any slot in GV */
15238 sv = (const SV *)POPPTR(ss,ix);
15239 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15240 ptr = POPPTR(ss,ix);
15241 TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
15242 sv = (const SV *)POPPTR(ss,ix);
15243 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15245 case SAVEt_HV: /* hash reference */
15246 case SAVEt_AV: /* array reference */
15247 sv = (const SV *) POPPTR(ss,ix);
15248 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15250 case SAVEt_COMPPAD:
15252 sv = (const SV *) POPPTR(ss,ix);
15253 TOPPTR(nss,ix) = sv_dup(sv, param);
15255 case SAVEt_INT: /* int reference */
15256 ptr = POPPTR(ss,ix);
15257 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15258 intval = (int)POPINT(ss,ix);
15259 TOPINT(nss,ix) = intval;
15261 case SAVEt_LONG: /* long reference */
15262 ptr = POPPTR(ss,ix);
15263 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15264 longval = (long)POPLONG(ss,ix);
15265 TOPLONG(nss,ix) = longval;
15267 case SAVEt_I32: /* I32 reference */
15268 ptr = POPPTR(ss,ix);
15269 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15271 TOPINT(nss,ix) = i;
15273 case SAVEt_IV: /* IV reference */
15274 case SAVEt_STRLEN: /* STRLEN/size_t ref */
15275 ptr = POPPTR(ss,ix);
15276 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15278 TOPIV(nss,ix) = iv;
15280 case SAVEt_TMPSFLOOR:
15282 TOPIV(nss,ix) = iv;
15284 case SAVEt_HPTR: /* HV* reference */
15285 case SAVEt_APTR: /* AV* reference */
15286 case SAVEt_SPTR: /* SV* reference */
15287 ptr = POPPTR(ss,ix);
15288 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15289 sv = (const SV *)POPPTR(ss,ix);
15290 TOPPTR(nss,ix) = sv_dup(sv, param);
15292 case SAVEt_VPTR: /* random* reference */
15293 ptr = POPPTR(ss,ix);
15294 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15296 case SAVEt_STRLEN_SMALL:
15297 case SAVEt_INT_SMALL:
15298 case SAVEt_I32_SMALL:
15299 case SAVEt_I16: /* I16 reference */
15300 case SAVEt_I8: /* I8 reference */
15302 ptr = POPPTR(ss,ix);
15303 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15305 case SAVEt_GENERIC_PVREF: /* generic char* */
15306 case SAVEt_PPTR: /* char* reference */
15307 ptr = POPPTR(ss,ix);
15308 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15309 c = (char*)POPPTR(ss,ix);
15310 TOPPTR(nss,ix) = pv_dup(c);
15312 case SAVEt_GP: /* scalar reference */
15313 gp = (GP*)POPPTR(ss,ix);
15314 TOPPTR(nss,ix) = gp = gp_dup(gp, param);
15315 (void)GpREFCNT_inc(gp);
15316 gv = (const GV *)POPPTR(ss,ix);
15317 TOPPTR(nss,ix) = gv_dup_inc(gv, param);
15320 ptr = POPPTR(ss,ix);
15321 if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) {
15322 /* these are assumed to be refcounted properly */
15324 switch (((OP*)ptr)->op_type) {
15326 case OP_LEAVESUBLV:
15330 case OP_LEAVEWRITE:
15331 TOPPTR(nss,ix) = ptr;
15334 (void) OpREFCNT_inc(o);
15338 TOPPTR(nss,ix) = NULL;
15343 TOPPTR(nss,ix) = NULL;
15345 case SAVEt_FREECOPHH:
15346 ptr = POPPTR(ss,ix);
15347 TOPPTR(nss,ix) = cophh_copy((COPHH *)ptr);
15349 case SAVEt_ADELETE:
15350 av = (const AV *)POPPTR(ss,ix);
15351 TOPPTR(nss,ix) = av_dup_inc(av, param);
15353 TOPINT(nss,ix) = i;
15356 hv = (const HV *)POPPTR(ss,ix);
15357 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
15359 TOPINT(nss,ix) = i;
15362 c = (char*)POPPTR(ss,ix);
15363 TOPPTR(nss,ix) = pv_dup_inc(c);
15365 case SAVEt_FREERCPV:
15366 c = (char *)POPPTR(ss,ix);
15367 TOPPTR(nss,ix) = rcpv_copy(c);
15369 case SAVEt_STACK_POS: /* Position on Perl stack */
15371 TOPINT(nss,ix) = i;
15373 case SAVEt_DESTRUCTOR:
15374 ptr = POPPTR(ss,ix);
15375 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
15376 dptr = POPDPTR(ss,ix);
15377 TOPDPTR(nss,ix) = DPTR2FPTR(void (*)(void*),
15378 any_dup(FPTR2DPTR(void *, dptr),
15381 case SAVEt_DESTRUCTOR_X:
15382 ptr = POPPTR(ss,ix);
15383 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
15384 dxptr = POPDXPTR(ss,ix);
15385 TOPDXPTR(nss,ix) = DPTR2FPTR(void (*)(pTHX_ void*),
15386 any_dup(FPTR2DPTR(void *, dxptr),
15389 case SAVEt_REGCONTEXT:
15391 ix -= uv >> SAVE_TIGHT_SHIFT;
15393 case SAVEt_AELEM: /* array element */
15394 sv = (const SV *)POPPTR(ss,ix);
15395 TOPPTR(nss,ix) = SvREFCNT_inc(sv_dup_inc(sv, param));
15397 TOPIV(nss,ix) = iv;
15398 av = (const AV *)POPPTR(ss,ix);
15399 TOPPTR(nss,ix) = av_dup_inc(av, param);
15402 ptr = POPPTR(ss,ix);
15403 TOPPTR(nss,ix) = ptr;
15405 case SAVEt_HINTS_HH:
15406 hv = (const HV *)POPPTR(ss,ix);
15407 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
15410 ptr = POPPTR(ss,ix);
15411 ptr = cophh_copy((COPHH*)ptr);
15412 TOPPTR(nss,ix) = ptr;
15414 TOPINT(nss,ix) = i;
15416 case SAVEt_PADSV_AND_MORTALIZE:
15417 longval = (long)POPLONG(ss,ix);
15418 TOPLONG(nss,ix) = longval;
15419 ptr = POPPTR(ss,ix);
15420 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15421 sv = (const SV *)POPPTR(ss,ix);
15422 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15424 case SAVEt_SET_SVFLAGS:
15426 TOPINT(nss,ix) = i;
15428 TOPINT(nss,ix) = i;
15429 sv = (const SV *)POPPTR(ss,ix);
15430 TOPPTR(nss,ix) = sv_dup(sv, param);
15432 case SAVEt_CURCOP_WARNINGS:
15434 case SAVEt_COMPILE_WARNINGS:
15435 ptr = POPPTR(ss,ix);
15436 TOPPTR(nss,ix) = DUP_WARNINGS((char*)ptr);
15439 ptr = POPPTR(ss,ix);
15440 TOPPTR(nss,ix) = parser_dup((const yy_parser*)ptr, param);
15444 "panic: ss_dup inconsistency (%" IVdf ")", (IV) type);
15452 /* if sv is a stash, call $class->CLONE_SKIP(), and set the SVphv_CLONEABLE
15453 * flag to the result. This is done for each stash before cloning starts,
15454 * so we know which stashes want their objects cloned */
15457 do_mark_cloneable_stash(pTHX_ SV *const sv)
15459 const HEK * const hvname = HvNAME_HEK((const HV *)sv);
15461 GV* const cloner = gv_fetchmethod_autoload(MUTABLE_HV(sv), "CLONE_SKIP", 0);
15462 SvFLAGS(sv) |= SVphv_CLONEABLE; /* clone objects by default */
15463 if (cloner && GvCV(cloner)) {
15470 mXPUSHs(newSVhek(hvname));
15472 call_sv(MUTABLE_SV(GvCV(cloner)), G_SCALAR);
15479 SvFLAGS(sv) &= ~SVphv_CLONEABLE;
15487 =for apidoc perl_clone
15489 Create and return a new interpreter by cloning the current one.
15491 C<perl_clone> takes these flags as parameters:
15493 C<CLONEf_COPY_STACKS> - is used to, well, copy the stacks also,
15494 without it we only clone the data and zero the stacks,
15495 with it we copy the stacks and the new perl interpreter is
15496 ready to run at the exact same point as the previous one.
15497 The pseudo-fork code uses C<COPY_STACKS> while the
15498 threads->create doesn't.
15500 C<CLONEf_KEEP_PTR_TABLE> -
15501 C<perl_clone> keeps a ptr_table with the pointer of the old
15502 variable as a key and the new variable as a value,
15503 this allows it to check if something has been cloned and not
15504 clone it again, but rather just use the value and increase the
15506 If C<KEEP_PTR_TABLE> is not set then C<perl_clone> will kill the ptr_table
15507 using the function S<C<ptr_table_free(PL_ptr_table); PL_ptr_table = NULL;>>.
15508 A reason to keep it around is if you want to dup some of your own
15509 variables which are outside the graph that perl scans.
15511 C<CLONEf_CLONE_HOST> -
15512 This is a win32 thing, it is ignored on unix, it tells perl's
15513 win32host code (which is c++) to clone itself, this is needed on
15514 win32 if you want to run two threads at the same time,
15515 if you just want to do some stuff in a separate perl interpreter
15516 and then throw it away and return to the original one,
15517 you don't need to do anything.
15522 /* XXX the above needs expanding by someone who actually understands it ! */
15523 EXTERN_C PerlInterpreter *
15524 perl_clone_host(PerlInterpreter* proto_perl, UV flags);
15527 perl_clone(PerlInterpreter *proto_perl, UV flags)
15529 #ifdef PERL_IMPLICIT_SYS
15531 PERL_ARGS_ASSERT_PERL_CLONE;
15533 /* perlhost.h so we need to call into it
15534 to clone the host, CPerlHost should have a c interface, sky */
15536 #ifndef __amigaos4__
15537 if (flags & CLONEf_CLONE_HOST) {
15538 return perl_clone_host(proto_perl,flags);
15541 return perl_clone_using(proto_perl, flags,
15543 proto_perl->IMemShared,
15544 proto_perl->IMemParse,
15546 proto_perl->IStdIO,
15550 proto_perl->IProc);
15554 perl_clone_using(PerlInterpreter *proto_perl, UV flags,
15555 struct IPerlMem* ipM, struct IPerlMem* ipMS,
15556 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
15557 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
15558 struct IPerlDir* ipD, struct IPerlSock* ipS,
15559 struct IPerlProc* ipP)
15561 /* XXX many of the string copies here can be optimized if they're
15562 * constants; they need to be allocated as common memory and just
15563 * their pointers copied. */
15566 CLONE_PARAMS clone_params;
15567 CLONE_PARAMS* const param = &clone_params;
15569 PerlInterpreter * const my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
15571 PERL_ARGS_ASSERT_PERL_CLONE_USING;
15572 #else /* !PERL_IMPLICIT_SYS */
15574 CLONE_PARAMS clone_params;
15575 CLONE_PARAMS* param = &clone_params;
15576 PerlInterpreter * const my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
15578 PERL_ARGS_ASSERT_PERL_CLONE;
15579 #endif /* PERL_IMPLICIT_SYS */
15581 /* for each stash, determine whether its objects should be cloned */
15582 S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
15583 my_perl->Iphase = PERL_PHASE_CONSTRUCT;
15584 PERL_SET_THX(my_perl);
15587 PoisonNew(my_perl, 1, PerlInterpreter);
15590 PL_defstash = NULL; /* may be used by perl malloc() */
15593 PL_scopestack_name = 0;
15595 PL_savestack_ix = 0;
15596 PL_savestack_max = -1;
15597 PL_sig_pending = 0;
15599 PL_eval_begin_nest_depth = proto_perl->Ieval_begin_nest_depth;
15600 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
15601 Zero(&PL_padname_undef, 1, PADNAME);
15602 Zero(&PL_padname_const, 1, PADNAME);
15603 # ifdef DEBUG_LEAKING_SCALARS
15604 PL_sv_serial = (((UV)my_perl >> 2) & 0xfff) * 1000000;
15606 # ifdef PERL_TRACE_OPS
15607 Zero(PL_op_exec_cnt, OP_max+2, UV);
15609 #else /* !DEBUGGING */
15610 Zero(my_perl, 1, PerlInterpreter);
15611 #endif /* DEBUGGING */
15613 #ifdef PERL_IMPLICIT_SYS
15614 /* host pointers */
15616 PL_MemShared = ipMS;
15617 PL_MemParse = ipMP;
15624 #endif /* PERL_IMPLICIT_SYS */
15627 param->flags = flags;
15628 /* Nothing in the core code uses this, but we make it available to
15629 extensions (using mg_dup). */
15630 param->proto_perl = proto_perl;
15631 /* Likely nothing will use this, but it is initialised to be consistent
15632 with Perl_clone_params_new(). */
15633 param->new_perl = my_perl;
15634 param->unreferenced = NULL;
15637 INIT_TRACK_MEMPOOL(my_perl->Imemory_debug_header, my_perl);
15639 PL_body_arenas = NULL;
15640 Zero(&PL_body_roots, 1, PL_body_roots);
15644 PL_sv_arenaroot = NULL;
15646 PL_debug = proto_perl->Idebug;
15648 /* dbargs array probably holds garbage */
15651 PL_compiling = proto_perl->Icompiling;
15653 /* pseudo environmental stuff */
15654 PL_origargc = proto_perl->Iorigargc;
15655 PL_origargv = proto_perl->Iorigargv;
15657 #ifndef NO_TAINT_SUPPORT
15658 /* Set tainting stuff before PerlIO_debug can possibly get called */
15659 PL_tainting = proto_perl->Itainting;
15660 PL_taint_warn = proto_perl->Itaint_warn;
15662 PL_tainting = FALSE;
15663 PL_taint_warn = FALSE;
15666 PL_minus_c = proto_perl->Iminus_c;
15668 PL_localpatches = proto_perl->Ilocalpatches;
15669 PL_splitstr = SAVEPV(proto_perl->Isplitstr);
15670 PL_minus_n = proto_perl->Iminus_n;
15671 PL_minus_p = proto_perl->Iminus_p;
15672 PL_minus_l = proto_perl->Iminus_l;
15673 PL_minus_a = proto_perl->Iminus_a;
15674 PL_minus_E = proto_perl->Iminus_E;
15675 PL_minus_F = proto_perl->Iminus_F;
15676 PL_doswitches = proto_perl->Idoswitches;
15677 PL_dowarn = proto_perl->Idowarn;
15678 #ifdef PERL_SAWAMPERSAND
15679 PL_sawampersand = proto_perl->Isawampersand;
15681 PL_unsafe = proto_perl->Iunsafe;
15682 PL_perldb = proto_perl->Iperldb;
15683 PL_perl_destruct_level = proto_perl->Iperl_destruct_level;
15684 PL_exit_flags = proto_perl->Iexit_flags;
15686 /* XXX time(&PL_basetime) when asked for? */
15687 PL_basetime = proto_perl->Ibasetime;
15689 PL_maxsysfd = proto_perl->Imaxsysfd;
15690 PL_statusvalue = proto_perl->Istatusvalue;
15692 PL_statusvalue_vms = proto_perl->Istatusvalue_vms;
15694 PL_statusvalue_posix = proto_perl->Istatusvalue_posix;
15697 /* RE engine related */
15698 PL_regmatch_slab = NULL;
15699 PL_reg_curpm = NULL;
15701 PL_sub_generation = proto_perl->Isub_generation;
15703 /* funky return mechanisms */
15704 PL_forkprocess = proto_perl->Iforkprocess;
15706 /* internal state */
15707 PL_main_start = proto_perl->Imain_start;
15708 PL_eval_root = proto_perl->Ieval_root;
15709 PL_eval_start = proto_perl->Ieval_start;
15711 PL_filemode = proto_perl->Ifilemode;
15712 PL_lastfd = proto_perl->Ilastfd;
15713 PL_oldname = proto_perl->Ioldname; /* XXX not quite right */
15714 PL_gensym = proto_perl->Igensym;
15716 PL_laststatval = proto_perl->Ilaststatval;
15717 PL_laststype = proto_perl->Ilaststype;
15720 PL_profiledata = NULL;
15722 PL_generation = proto_perl->Igeneration;
15724 PL_in_clean_objs = proto_perl->Iin_clean_objs;
15725 PL_in_clean_all = proto_perl->Iin_clean_all;
15727 PL_delaymagic_uid = proto_perl->Idelaymagic_uid;
15728 PL_delaymagic_euid = proto_perl->Idelaymagic_euid;
15729 PL_delaymagic_gid = proto_perl->Idelaymagic_gid;
15730 PL_delaymagic_egid = proto_perl->Idelaymagic_egid;
15731 PL_nomemok = proto_perl->Inomemok;
15732 PL_an = proto_perl->Ian;
15733 PL_evalseq = proto_perl->Ievalseq;
15734 PL_origalen = proto_perl->Iorigalen;
15736 PL_sighandlerp = proto_perl->Isighandlerp;
15737 PL_sighandler1p = proto_perl->Isighandler1p;
15738 PL_sighandler3p = proto_perl->Isighandler3p;
15740 PL_runops = proto_perl->Irunops;
15742 PL_subline = proto_perl->Isubline;
15744 PL_cv_has_eval = proto_perl->Icv_has_eval;
15745 /* Unicode features (see perlrun/-C) */
15746 PL_unicode = proto_perl->Iunicode;
15748 /* Pre-5.8 signals control */
15749 PL_signals = proto_perl->Isignals;
15751 /* times() ticks per second */
15752 PL_clocktick = proto_perl->Iclocktick;
15754 /* Recursion stopper for PerlIO_find_layer */
15755 PL_in_load_module = proto_perl->Iin_load_module;
15757 /* Not really needed/useful since the reenrant_retint is "volatile",
15758 * but do it for consistency's sake. */
15759 PL_reentrant_retint = proto_perl->Ireentrant_retint;
15761 /* Hooks to shared SVs and locks. */
15762 PL_sharehook = proto_perl->Isharehook;
15763 PL_lockhook = proto_perl->Ilockhook;
15764 PL_unlockhook = proto_perl->Iunlockhook;
15765 PL_threadhook = proto_perl->Ithreadhook;
15766 PL_destroyhook = proto_perl->Idestroyhook;
15767 PL_signalhook = proto_perl->Isignalhook;
15769 PL_globhook = proto_perl->Iglobhook;
15771 PL_srand_called = proto_perl->Isrand_called;
15772 Copy(&(proto_perl->Irandom_state), &PL_random_state, 1, PL_RANDOM_STATE_TYPE);
15773 PL_srand_override = proto_perl->Isrand_override;
15774 PL_srand_override_next = proto_perl->Isrand_override_next;
15776 if (flags & CLONEf_COPY_STACKS) {
15777 /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */
15778 PL_tmps_ix = proto_perl->Itmps_ix;
15779 PL_tmps_max = proto_perl->Itmps_max;
15780 PL_tmps_floor = proto_perl->Itmps_floor;
15782 /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
15783 * NOTE: unlike the others! */
15784 PL_scopestack_ix = proto_perl->Iscopestack_ix;
15785 PL_scopestack_max = proto_perl->Iscopestack_max;
15787 /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix]
15788 * NOTE: unlike the others! */
15789 PL_savestack_ix = proto_perl->Isavestack_ix;
15790 PL_savestack_max = proto_perl->Isavestack_max;
15793 PL_start_env = proto_perl->Istart_env; /* XXXXXX */
15794 PL_top_env = &PL_start_env;
15796 PL_op = proto_perl->Iop;
15799 PL_Xpv = (XPV*)NULL;
15800 my_perl->Ina = proto_perl->Ina;
15802 PL_statcache = proto_perl->Istatcache;
15804 #ifndef NO_TAINT_SUPPORT
15805 PL_tainted = proto_perl->Itainted;
15807 PL_tainted = FALSE;
15809 PL_curpm = proto_perl->Icurpm; /* XXX No PMOP ref count */
15811 PL_chopset = proto_perl->Ichopset; /* XXX never deallocated */
15813 PL_restartjmpenv = proto_perl->Irestartjmpenv;
15814 PL_restartop = proto_perl->Irestartop;
15815 PL_in_eval = proto_perl->Iin_eval;
15816 PL_delaymagic = proto_perl->Idelaymagic;
15817 PL_phase = proto_perl->Iphase;
15818 PL_localizing = proto_perl->Ilocalizing;
15820 PL_hv_fetch_ent_mh = NULL;
15821 PL_modcount = proto_perl->Imodcount;
15822 PL_lastgotoprobe = NULL;
15823 PL_dumpindent = proto_perl->Idumpindent;
15825 PL_efloatbuf = NULL; /* reinits on demand */
15826 PL_efloatsize = 0; /* reinits on demand */
15830 PL_colorset = 0; /* reinits PL_colors[] */
15831 /*PL_colors[6] = {0,0,0,0,0,0};*/
15833 /* Pluggable optimizer */
15834 PL_peepp = proto_perl->Ipeepp;
15835 PL_rpeepp = proto_perl->Irpeepp;
15836 /* op_free() hook */
15837 PL_opfreehook = proto_perl->Iopfreehook;
15839 # ifdef PERL_MEM_LOG
15840 Zero(PL_mem_log, sizeof(PL_mem_log), char);
15843 #ifdef USE_REENTRANT_API
15844 /* XXX: things like -Dm will segfault here in perlio, but doing
15845 * PERL_SET_CONTEXT(proto_perl);
15846 * breaks too many other things
15848 Perl_reentrant_init(aTHX);
15851 /* create SV map for pointer relocation */
15852 PL_ptr_table = ptr_table_new();
15854 /* initialize these special pointers as early as possible */
15856 ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef);
15857 ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no);
15858 ptr_table_store(PL_ptr_table, &proto_perl->Isv_zero, &PL_sv_zero);
15859 ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes);
15860 ptr_table_store(PL_ptr_table, &proto_perl->Ipadname_const,
15861 &PL_padname_const);
15863 /* create (a non-shared!) shared string table */
15864 PL_strtab = newHV();
15865 HvSHAREKEYS_off(PL_strtab);
15866 hv_ksplit(PL_strtab, HvTOTALKEYS(proto_perl->Istrtab));
15867 ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab);
15869 Zero(PL_sv_consts, SV_CONSTS_COUNT, SV*);
15871 PL_compiling.cop_file = rcpv_copy(proto_perl->Icompiling.cop_file);
15873 ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling);
15874 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
15875 CopHINTHASH_set(&PL_compiling, cophh_copy(CopHINTHASH_get(&PL_compiling)));
15876 PL_curcop = (COP*)any_dup(proto_perl->Icurcop, proto_perl);
15878 param->stashes = newAV(); /* Setup array of objects to call clone on */
15879 /* This makes no difference to the implementation, as it always pushes
15880 and shifts pointers to other SVs without changing their reference
15881 count, with the array becoming empty before it is freed. However, it
15882 makes it conceptually clear what is going on, and will avoid some
15883 work inside av.c, filling slots between AvFILL() and AvMAX() with
15884 &PL_sv_undef, and SvREFCNT_dec()ing those. */
15885 AvREAL_off(param->stashes);
15887 if (!(flags & CLONEf_COPY_STACKS)) {
15888 param->unreferenced = newAV();
15891 #ifdef PERLIO_LAYERS
15892 /* Clone PerlIO tables as soon as we can handle general xx_dup() */
15893 PerlIO_clone(aTHX_ proto_perl, param);
15896 PL_envgv = gv_dup_inc(proto_perl->Ienvgv, param);
15897 PL_incgv = gv_dup_inc(proto_perl->Iincgv, param);
15898 PL_hintgv = gv_dup_inc(proto_perl->Ihintgv, param);
15899 PL_origfilename = SAVEPV(proto_perl->Iorigfilename);
15900 PL_xsubfilename = proto_perl->Ixsubfilename;
15901 PL_diehook = sv_dup_inc(proto_perl->Idiehook, param);
15902 PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook, param);
15904 PL_hook__require__before = sv_dup_inc(proto_perl->Ihook__require__before, param);
15905 PL_hook__require__after = sv_dup_inc(proto_perl->Ihook__require__after, param);
15908 PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel, param);
15909 PL_inplace = SAVEPV(proto_perl->Iinplace);
15910 PL_e_script = sv_dup_inc(proto_perl->Ie_script, param);
15912 /* magical thingies */
15914 SvPVCLEAR(PERL_DEBUG_PAD(0)); /* For regex debugging. */
15915 SvPVCLEAR(PERL_DEBUG_PAD(1)); /* ext/re needs these */
15916 SvPVCLEAR(PERL_DEBUG_PAD(2)); /* even without DEBUGGING. */
15919 /* Clone the regex array */
15920 /* ORANGE FIXME for plugins, probably in the SV dup code.
15921 newSViv(PTR2IV(CALLREGDUPE(
15922 INT2PTR(REGEXP *, SvIVX(regex)), param))))
15924 PL_regex_padav = av_dup_inc(proto_perl->Iregex_padav, param);
15925 PL_regex_pad = AvARRAY(PL_regex_padav);
15927 PL_stashpadmax = proto_perl->Istashpadmax;
15928 PL_stashpadix = proto_perl->Istashpadix ;
15929 Newx(PL_stashpad, PL_stashpadmax, HV *);
15932 for (; o < PL_stashpadmax; ++o)
15933 PL_stashpad[o] = hv_dup(proto_perl->Istashpad[o], param);
15936 /* shortcuts to various I/O objects */
15937 PL_ofsgv = gv_dup_inc(proto_perl->Iofsgv, param);
15938 PL_stdingv = gv_dup(proto_perl->Istdingv, param);
15939 PL_stderrgv = gv_dup(proto_perl->Istderrgv, param);
15940 PL_defgv = gv_dup(proto_perl->Idefgv, param);
15941 PL_argvgv = gv_dup_inc(proto_perl->Iargvgv, param);
15942 PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv, param);
15943 PL_argvout_stack = av_dup_inc(proto_perl->Iargvout_stack, param);
15945 /* shortcuts to regexp stuff */
15946 PL_replgv = gv_dup_inc(proto_perl->Ireplgv, param);
15948 /* shortcuts to misc objects */
15949 PL_errgv = gv_dup(proto_perl->Ierrgv, param);
15951 /* shortcuts to debugging objects */
15952 PL_DBgv = gv_dup_inc(proto_perl->IDBgv, param);
15953 PL_DBline = gv_dup_inc(proto_perl->IDBline, param);
15954 PL_DBsub = gv_dup_inc(proto_perl->IDBsub, param);
15955 PL_DBsingle = sv_dup(proto_perl->IDBsingle, param);
15956 PL_DBtrace = sv_dup(proto_perl->IDBtrace, param);
15957 PL_DBsignal = sv_dup(proto_perl->IDBsignal, param);
15958 Copy(proto_perl->IDBcontrol, PL_DBcontrol, DBVARMG_COUNT, IV);
15960 /* symbol tables */
15961 PL_defstash = hv_dup_inc(proto_perl->Idefstash, param);
15962 PL_curstash = hv_dup_inc(proto_perl->Icurstash, param);
15963 PL_debstash = hv_dup(proto_perl->Idebstash, param);
15964 PL_globalstash = hv_dup(proto_perl->Iglobalstash, param);
15965 PL_curstname = sv_dup_inc(proto_perl->Icurstname, param);
15967 PL_beginav = av_dup_inc(proto_perl->Ibeginav, param);
15968 PL_beginav_save = av_dup_inc(proto_perl->Ibeginav_save, param);
15969 PL_checkav_save = av_dup_inc(proto_perl->Icheckav_save, param);
15970 PL_unitcheckav = av_dup_inc(proto_perl->Iunitcheckav, param);
15971 PL_unitcheckav_save = av_dup_inc(proto_perl->Iunitcheckav_save, param);
15972 PL_endav = av_dup_inc(proto_perl->Iendav, param);
15973 PL_checkav = av_dup_inc(proto_perl->Icheckav, param);
15974 PL_initav = av_dup_inc(proto_perl->Iinitav, param);
15975 PL_savebegin = proto_perl->Isavebegin;
15977 PL_isarev = hv_dup_inc(proto_perl->Iisarev, param);
15979 /* subprocess state */
15980 PL_fdpid = av_dup_inc(proto_perl->Ifdpid, param);
15982 if (proto_perl->Iop_mask)
15983 PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo);
15986 /* PL_asserting = proto_perl->Iasserting; */
15988 /* current interpreter roots */
15989 PL_main_cv = cv_dup_inc(proto_perl->Imain_cv, param);
15991 PL_main_root = OpREFCNT_inc(proto_perl->Imain_root);
15994 /* runtime control stuff */
15995 PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl);
15997 PL_preambleav = av_dup_inc(proto_perl->Ipreambleav, param);
15999 PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv, param);
16001 /* interpreter atexit processing */
16002 PL_exitlistlen = proto_perl->Iexitlistlen;
16003 if (PL_exitlistlen) {
16004 Newx(PL_exitlist, PL_exitlistlen, PerlExitListEntry);
16005 Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
16008 PL_exitlist = (PerlExitListEntry*)NULL;
16010 PL_my_cxt_size = proto_perl->Imy_cxt_size;
16011 if (PL_my_cxt_size) {
16012 Newx(PL_my_cxt_list, PL_my_cxt_size, void *);
16013 Copy(proto_perl->Imy_cxt_list, PL_my_cxt_list, PL_my_cxt_size, void *);
16016 PL_my_cxt_list = (void**)NULL;
16018 PL_modglobal = hv_dup_inc(proto_perl->Imodglobal, param);
16019 PL_custom_op_names = hv_dup_inc(proto_perl->Icustom_op_names,param);
16020 PL_custom_op_descs = hv_dup_inc(proto_perl->Icustom_op_descs,param);
16021 PL_custom_ops = hv_dup_inc(proto_perl->Icustom_ops, param);
16023 PL_compcv = cv_dup(proto_perl->Icompcv, param);
16025 PAD_CLONE_VARS(proto_perl, param);
16027 #ifdef HAVE_INTERP_INTERN
16028 sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern);
16031 PL_DBcv = cv_dup(proto_perl->IDBcv, param);
16033 #ifdef PERL_USES_PL_PIDSTATUS
16034 PL_pidstatus = newHV(); /* XXX flag for cloning? */
16036 PL_osname = SAVEPV(proto_perl->Iosname);
16037 PL_parser = parser_dup(proto_perl->Iparser, param);
16039 /* XXX this only works if the saved cop has already been cloned */
16040 if (proto_perl->Iparser) {
16041 PL_parser->saved_curcop = (COP*)any_dup(
16042 proto_perl->Iparser->saved_curcop,
16046 PL_subname = sv_dup_inc(proto_perl->Isubname, param);
16048 #ifdef USE_PL_CURLOCALES
16049 for (i = 0; i < (int) C_ARRAY_LENGTH(PL_curlocales); i++) {
16050 PL_curlocales[i] = SAVEPV("C");
16053 #ifdef USE_PL_CUR_LC_ALL
16054 PL_cur_LC_ALL = SAVEPV("C");
16056 #ifdef USE_LOCALE_CTYPE
16057 Copy(PL_fold, PL_fold_locale, 256, U8);
16059 /* Should we warn if uses locale? */
16060 PL_ctype_name = SAVEPV("C");
16061 PL_warn_locale = sv_dup_inc(proto_perl->Iwarn_locale, param);
16062 PL_in_utf8_CTYPE_locale = false;
16063 PL_in_utf8_turkic_locale = false;
16066 /* Did the locale setup indicate UTF-8? */
16067 PL_utf8locale = false;
16069 #ifdef USE_LOCALE_COLLATE
16070 PL_in_utf8_COLLATE_locale = false;
16071 PL_collation_name = SAVEPV("C");
16072 PL_collation_ix = proto_perl->Icollation_ix;
16073 PL_collation_standard = true;
16074 PL_collxfrm_base = 0;
16075 PL_collxfrm_mult = 0;
16076 PL_strxfrm_max_cp = 0;
16077 PL_strxfrm_is_behaved = proto_perl->Istrxfrm_is_behaved;
16078 PL_strxfrm_NUL_replacement = '\0';
16079 #endif /* USE_LOCALE_COLLATE */
16081 #ifdef USE_LOCALE_THREADS
16082 assert(PL_locale_mutex_depth <= 0);
16083 PL_locale_mutex_depth = 0;
16086 #ifdef USE_LOCALE_NUMERIC
16087 PL_numeric_name = SAVEPV("C");
16088 PL_numeric_radix_sv = newSVpvs(".");
16089 PL_underlying_radix_sv = newSVpvs(".");
16090 PL_numeric_standard = true;
16091 PL_numeric_underlying = true;
16092 PL_numeric_underlying_is_standard = true;
16094 #endif /* !USE_LOCALE_NUMERIC */
16095 #if defined(USE_POSIX_2008_LOCALE)
16096 PL_scratch_locale_obj = NULL;
16097 PL_cur_locale_obj = PL_C_locale_obj;
16101 PL_mbrlen_ps = proto_perl->Imbrlen_ps;
16104 PL_mbrtowc_ps = proto_perl->Imbrtowc_ps;
16107 PL_wcrtomb_ps = proto_perl->Iwcrtomb_ps;
16110 PL_langinfo_buf = NULL;
16111 PL_langinfo_bufsize = 0;
16113 PL_setlocale_buf = NULL;
16114 PL_setlocale_bufsize = 0;
16116 #if defined(USE_LOCALE_THREADS) && ! defined(USE_THREAD_SAFE_LOCALE)
16117 PL_less_dicey_locale_buf = NULL;
16118 PL_less_dicey_locale_bufsize = 0;
16120 #if ! defined(HAS_NL_LANGINFO) \
16121 && defined(USE_LOCALE_CTYPE) \
16122 && ! defined(WIN32) \
16123 && ! defined(HAS_MBTOWC) \
16124 && ! defined(HAS_MBRTOWC)
16125 PL_langinfo_recursed = 0;
16128 /* Unicode inversion lists */
16130 PL_AboveLatin1 = sv_dup_inc(proto_perl->IAboveLatin1, param);
16131 PL_Assigned_invlist = sv_dup_inc(proto_perl->IAssigned_invlist, param);
16132 PL_GCB_invlist = sv_dup_inc(proto_perl->IGCB_invlist, param);
16133 PL_HasMultiCharFold = sv_dup_inc(proto_perl->IHasMultiCharFold, param);
16134 PL_InMultiCharFold = sv_dup_inc(proto_perl->IInMultiCharFold, param);
16135 PL_Latin1 = sv_dup_inc(proto_perl->ILatin1, param);
16136 PL_LB_invlist = sv_dup_inc(proto_perl->ILB_invlist, param);
16137 PL_SB_invlist = sv_dup_inc(proto_perl->ISB_invlist, param);
16138 PL_SCX_invlist = sv_dup_inc(proto_perl->ISCX_invlist, param);
16139 PL_UpperLatin1 = sv_dup_inc(proto_perl->IUpperLatin1, param);
16140 PL_in_some_fold = sv_dup_inc(proto_perl->Iin_some_fold, param);
16141 PL_utf8_foldclosures = sv_dup_inc(proto_perl->Iutf8_foldclosures, param);
16142 PL_utf8_idcont = sv_dup_inc(proto_perl->Iutf8_idcont, param);
16143 PL_utf8_idstart = sv_dup_inc(proto_perl->Iutf8_idstart, param);
16144 PL_utf8_perl_idcont = sv_dup_inc(proto_perl->Iutf8_perl_idcont, param);
16145 PL_utf8_perl_idstart = sv_dup_inc(proto_perl->Iutf8_perl_idstart, param);
16146 PL_utf8_xidcont = sv_dup_inc(proto_perl->Iutf8_xidcont, param);
16147 PL_utf8_xidstart = sv_dup_inc(proto_perl->Iutf8_xidstart, param);
16148 PL_WB_invlist = sv_dup_inc(proto_perl->IWB_invlist, param);
16149 for (i = 0; i < POSIX_CC_COUNT; i++) {
16150 PL_XPosix_ptrs[i] = sv_dup_inc(proto_perl->IXPosix_ptrs[i], param);
16151 if (i != CC_CASED_ && i != CC_VERTSPACE_) {
16152 PL_Posix_ptrs[i] = sv_dup_inc(proto_perl->IPosix_ptrs[i], param);
16155 PL_Posix_ptrs[CC_CASED_] = PL_Posix_ptrs[CC_ALPHA_];
16156 PL_Posix_ptrs[CC_VERTSPACE_] = NULL;
16158 PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper, param);
16159 PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle, param);
16160 PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower, param);
16161 PL_utf8_tofold = sv_dup_inc(proto_perl->Iutf8_tofold, param);
16162 PL_utf8_tosimplefold = sv_dup_inc(proto_perl->Iutf8_tosimplefold, param);
16163 PL_utf8_charname_begin = sv_dup_inc(proto_perl->Iutf8_charname_begin, param);
16164 PL_utf8_charname_continue = sv_dup_inc(proto_perl->Iutf8_charname_continue, param);
16165 PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark, param);
16166 PL_InBitmap = sv_dup_inc(proto_perl->IInBitmap, param);
16167 PL_CCC_non0_non230 = sv_dup_inc(proto_perl->ICCC_non0_non230, param);
16168 PL_Private_Use = sv_dup_inc(proto_perl->IPrivate_Use, param);
16171 PL_seen_deprecated_macro = hv_dup_inc(proto_perl->Iseen_deprecated_macro, param);
16174 if (proto_perl->Ipsig_pend) {
16175 Newxz(PL_psig_pend, SIG_SIZE, int);
16178 PL_psig_pend = (int*)NULL;
16181 if (proto_perl->Ipsig_name) {
16182 Newx(PL_psig_name, 2 * SIG_SIZE, SV*);
16183 sv_dup_inc_multiple(proto_perl->Ipsig_name, PL_psig_name, 2 * SIG_SIZE,
16185 PL_psig_ptr = PL_psig_name + SIG_SIZE;
16188 PL_psig_ptr = (SV**)NULL;
16189 PL_psig_name = (SV**)NULL;
16192 if (flags & CLONEf_COPY_STACKS) {
16193 Newx(PL_tmps_stack, PL_tmps_max, SV*);
16194 sv_dup_inc_multiple(proto_perl->Itmps_stack, PL_tmps_stack,
16195 PL_tmps_ix+1, param);
16197 /* next PUSHMARK() sets *(PL_markstack_ptr+1) */
16198 i = proto_perl->Imarkstack_max - proto_perl->Imarkstack;
16199 Newx(PL_markstack, i, Stack_off_t);
16200 PL_markstack_max = PL_markstack + (proto_perl->Imarkstack_max
16201 - proto_perl->Imarkstack);
16202 PL_markstack_ptr = PL_markstack + (proto_perl->Imarkstack_ptr
16203 - proto_perl->Imarkstack);
16204 Copy(proto_perl->Imarkstack, PL_markstack,
16205 PL_markstack_ptr - PL_markstack + 1, Stack_off_t);
16207 /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
16208 * NOTE: unlike the others! */
16209 Newx(PL_scopestack, PL_scopestack_max, I32);
16210 Copy(proto_perl->Iscopestack, PL_scopestack, PL_scopestack_ix, I32);
16213 Newx(PL_scopestack_name, PL_scopestack_max, const char *);
16214 Copy(proto_perl->Iscopestack_name, PL_scopestack_name, PL_scopestack_ix, const char *);
16216 /* reset stack AV to correct length before its duped via
16217 * PL_curstackinfo */
16218 AvFILLp(proto_perl->Icurstack) =
16219 proto_perl->Istack_sp - proto_perl->Istack_base;
16221 /* NOTE: si_dup() looks at PL_markstack */
16222 PL_curstackinfo = si_dup(proto_perl->Icurstackinfo, param);
16224 /* PL_curstack = PL_curstackinfo->si_stack; */
16225 PL_curstack = av_dup(proto_perl->Icurstack, param);
16226 PL_mainstack = av_dup(proto_perl->Imainstack, param);
16228 /* next PUSHs() etc. set *(PL_stack_sp+1) */
16229 PL_stack_base = AvARRAY(PL_curstack);
16230 PL_stack_sp = PL_stack_base + (proto_perl->Istack_sp
16231 - proto_perl->Istack_base);
16232 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
16234 /*Newxz(PL_savestack, PL_savestack_max, ANY);*/
16235 PL_savestack = ss_dup(proto_perl, param);
16239 ENTER; /* perl_destruct() wants to LEAVE; */
16242 PL_statgv = gv_dup(proto_perl->Istatgv, param);
16243 PL_statname = sv_dup_inc(proto_perl->Istatname, param);
16245 PL_rs = sv_dup_inc(proto_perl->Irs, param);
16246 PL_last_in_gv = gv_dup(proto_perl->Ilast_in_gv, param);
16247 PL_defoutgv = gv_dup_inc(proto_perl->Idefoutgv, param);
16248 PL_toptarget = sv_dup_inc(proto_perl->Itoptarget, param);
16249 PL_bodytarget = sv_dup_inc(proto_perl->Ibodytarget, param);
16250 PL_formtarget = sv_dup(proto_perl->Iformtarget, param);
16252 PL_errors = sv_dup_inc(proto_perl->Ierrors, param);
16254 PL_sortcop = (OP*)any_dup(proto_perl->Isortcop, proto_perl);
16255 PL_firstgv = gv_dup_inc(proto_perl->Ifirstgv, param);
16256 PL_secondgv = gv_dup_inc(proto_perl->Isecondgv, param);
16258 PL_stashcache = newHV();
16260 PL_watchaddr = (char **) ptr_table_fetch(PL_ptr_table,
16261 proto_perl->Iwatchaddr);
16262 PL_watchok = PL_watchaddr ? * PL_watchaddr : NULL;
16263 if (PL_debug && PL_watchaddr) {
16264 PerlIO_printf(Perl_debug_log,
16265 "WATCHING: %" UVxf " cloned as %" UVxf " with value %" UVxf "\n",
16266 PTR2UV(proto_perl->Iwatchaddr), PTR2UV(PL_watchaddr),
16267 PTR2UV(PL_watchok));
16270 PL_registered_mros = hv_dup_inc(proto_perl->Iregistered_mros, param);
16271 PL_blockhooks = av_dup_inc(proto_perl->Iblockhooks, param);
16273 /* Call the ->CLONE method, if it exists, for each of the stashes
16274 identified by sv_dup() above.
16276 while(av_count(param->stashes) != 0) {
16277 HV* const stash = MUTABLE_HV(av_shift(param->stashes));
16278 GV* const cloner = gv_fetchmethod_autoload(stash, "CLONE", 0);
16279 if (cloner && GvCV(cloner)) {
16282 PUSHMARK(PL_stack_sp);
16284 SV *newsv = newSVhek(HvNAME_HEK(stash));
16285 *++PL_stack_sp = newsv;
16286 if (!rpp_stack_is_rc())
16288 call_sv(MUTABLE_SV(GvCV(cloner)), G_DISCARD);
16294 if (!(flags & CLONEf_KEEP_PTR_TABLE)) {
16295 ptr_table_free(PL_ptr_table);
16296 PL_ptr_table = NULL;
16299 if (!(flags & CLONEf_COPY_STACKS)) {
16300 unreferenced_to_tmp_stack(param->unreferenced);
16303 SvREFCNT_dec(param->stashes);
16305 /* orphaned? eg threads->new inside BEGIN or use */
16306 if (PL_compcv && ! SvREFCNT(PL_compcv)) {
16307 SvREFCNT_inc_simple_void(PL_compcv);
16308 SAVEFREESV(PL_compcv);
16315 S_unreferenced_to_tmp_stack(pTHX_ AV *const unreferenced)
16317 PERL_ARGS_ASSERT_UNREFERENCED_TO_TMP_STACK;
16319 if (AvFILLp(unreferenced) > -1) {
16320 SV **svp = AvARRAY(unreferenced);
16321 SV **const last = svp + AvFILLp(unreferenced);
16325 if (SvREFCNT(*svp) == 1)
16327 } while (++svp <= last);
16329 EXTEND_MORTAL(count);
16330 svp = AvARRAY(unreferenced);
16333 if (SvREFCNT(*svp) == 1) {
16334 /* Our reference is the only one to this SV. This means that
16335 in this thread, the scalar effectively has a 0 reference.
16336 That doesn't work (cleanup never happens), so donate our
16337 reference to it onto the save stack. */
16338 PL_tmps_stack[++PL_tmps_ix] = *svp;
16340 /* As an optimisation, because we are already walking the
16341 entire array, instead of above doing either
16342 SvREFCNT_inc(*svp) or *svp = &PL_sv_undef, we can instead
16343 release our reference to the scalar, so that at the end of
16344 the array owns zero references to the scalars it happens to
16345 point to. We are effectively converting the array from
16346 AvREAL() on to AvREAL() off. This saves the av_clear()
16347 (triggered by the SvREFCNT_dec(unreferenced) below) from
16348 walking the array a second time. */
16349 SvREFCNT_dec(*svp);
16352 } while (++svp <= last);
16353 AvREAL_off(unreferenced);
16355 SvREFCNT_dec_NN(unreferenced);
16359 Perl_clone_params_del(CLONE_PARAMS *param)
16361 PerlInterpreter *const was = PERL_GET_THX;
16362 PerlInterpreter *const to = param->new_perl;
16365 PERL_ARGS_ASSERT_CLONE_PARAMS_DEL;
16371 SvREFCNT_dec(param->stashes);
16372 if (param->unreferenced)
16373 unreferenced_to_tmp_stack(param->unreferenced);
16383 Perl_clone_params_new(PerlInterpreter *const from, PerlInterpreter *const to)
16385 /* Need to play this game, as newAV() can call safesysmalloc(), and that
16386 does a dTHX; to get the context from thread local storage.
16387 FIXME - under PERL_CORE Newx(), Safefree() and friends should expand to
16388 a version that passes in my_perl. */
16389 PerlInterpreter *const was = PERL_GET_THX;
16390 CLONE_PARAMS *param;
16392 PERL_ARGS_ASSERT_CLONE_PARAMS_NEW;
16398 /* Given that we've set the context, we can do this unshared. */
16399 Newx(param, 1, CLONE_PARAMS);
16402 param->proto_perl = from;
16403 param->new_perl = to;
16404 param->stashes = (AV *)Perl_newSV_type(to, SVt_PVAV);
16405 AvREAL_off(param->stashes);
16406 param->unreferenced = (AV *)Perl_newSV_type(to, SVt_PVAV);
16414 #endif /* USE_ITHREADS */
16417 Perl_init_constants(pTHX)
16420 SvREFCNT(&PL_sv_undef) = SvREFCNT_IMMORTAL;
16421 SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVf_PROTECT|SVt_NULL;
16422 SvANY(&PL_sv_undef) = NULL;
16424 SvANY(&PL_sv_no) = new_XPVNV();
16425 SvREFCNT(&PL_sv_no) = SvREFCNT_IMMORTAL;
16426 SvFLAGS(&PL_sv_no) = SVt_PVNV|SVf_READONLY|SVf_PROTECT
16427 |SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
16428 |SVp_POK|SVf_POK|SVf_IsCOW|SVppv_STATIC;
16430 SvANY(&PL_sv_yes) = new_XPVNV();
16431 SvREFCNT(&PL_sv_yes) = SvREFCNT_IMMORTAL;
16432 SvFLAGS(&PL_sv_yes) = SVt_PVNV|SVf_READONLY|SVf_PROTECT
16433 |SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
16434 |SVp_POK|SVf_POK|SVf_IsCOW|SVppv_STATIC;
16436 SvANY(&PL_sv_zero) = new_XPVNV();
16437 SvREFCNT(&PL_sv_zero) = SvREFCNT_IMMORTAL;
16438 SvFLAGS(&PL_sv_zero) = SVt_PVNV|SVf_READONLY|SVf_PROTECT
16439 |SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
16443 SvPV_set(&PL_sv_no, (char*)PL_No);
16444 SvCUR_set(&PL_sv_no, 0);
16445 SvLEN_set(&PL_sv_no, 0);
16446 SvIV_set(&PL_sv_no, 0);
16447 SvNV_set(&PL_sv_no, 0);
16449 SvPV_set(&PL_sv_yes, (char*)PL_Yes);
16450 SvCUR_set(&PL_sv_yes, 1);
16451 SvLEN_set(&PL_sv_yes, 0);
16452 SvIV_set(&PL_sv_yes, 1);
16453 SvNV_set(&PL_sv_yes, 1);
16455 SvPV_set(&PL_sv_zero, (char*)PL_Zero);
16456 SvCUR_set(&PL_sv_zero, 1);
16457 SvLEN_set(&PL_sv_zero, 0);
16458 SvIV_set(&PL_sv_zero, 0);
16459 SvNV_set(&PL_sv_zero, 0);
16461 PadnamePV(&PL_padname_const) = (char *)PL_No;
16463 assert(SvIMMORTAL_INTERP(&PL_sv_yes));
16464 assert(SvIMMORTAL_INTERP(&PL_sv_undef));
16465 assert(SvIMMORTAL_INTERP(&PL_sv_no));
16466 assert(SvIMMORTAL_INTERP(&PL_sv_zero));
16468 assert(SvIMMORTAL(&PL_sv_yes));
16469 assert(SvIMMORTAL(&PL_sv_undef));
16470 assert(SvIMMORTAL(&PL_sv_no));
16471 assert(SvIMMORTAL(&PL_sv_zero));
16473 assert( SvIMMORTAL_TRUE(&PL_sv_yes));
16474 assert(!SvIMMORTAL_TRUE(&PL_sv_undef));
16475 assert(!SvIMMORTAL_TRUE(&PL_sv_no));
16476 assert(!SvIMMORTAL_TRUE(&PL_sv_zero));
16478 assert( SvTRUE_nomg_NN(&PL_sv_yes));
16479 assert(!SvTRUE_nomg_NN(&PL_sv_undef));
16480 assert(!SvTRUE_nomg_NN(&PL_sv_no));
16481 assert(!SvTRUE_nomg_NN(&PL_sv_zero));
16485 =for apidoc_section $unicode
16487 =for apidoc sv_recode_to_utf8
16489 C<encoding> is assumed to be an C<Encode> object, on entry the PV
16490 of C<sv> is assumed to be octets in that encoding, and C<sv>
16491 will be converted into Unicode (and UTF-8).
16493 If C<sv> already is UTF-8 (or if it is not C<POK>), or if C<encoding>
16494 is not a reference, nothing is done to C<sv>. If C<encoding> is not
16495 an C<Encode::XS> Encoding object, bad things will happen.
16496 (See L<encoding> and L<Encode>.)
16498 The PV of C<sv> is returned.
16503 Perl_sv_recode_to_utf8(pTHX_ SV *sv, SV *encoding)
16505 PERL_ARGS_ASSERT_SV_RECODE_TO_UTF8;
16507 if (SvPOK(sv) && !SvUTF8(sv) && !IN_BYTES && SvROK(encoding)) {
16516 if (SvPADTMP(nsv)) {
16517 nsv = sv_newmortal();
16518 SvSetSV_nosteal(nsv, sv);
16527 Passing sv_yes is wrong - it needs to be or'ed set of constants
16528 for Encode::XS, while UTf-8 decode (currently) assumes a true value means
16529 remove converted chars from source.
16531 Both will default the value - let them.
16533 XPUSHs(&PL_sv_yes);
16536 call_method("decode", G_SCALAR);
16540 s = SvPV_const(uni, len);
16541 if (s != SvPVX_const(sv)) {
16542 SvGROW(sv, len + 1);
16543 Move(s, SvPVX(sv), len + 1, char);
16544 SvCUR_set(sv, len);
16549 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
16550 /* clear pos and any utf8 cache */
16551 MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
16554 if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
16555 magic_setutf8(sv,mg); /* clear UTF8 cache */
16560 return SvPOKp(sv) ? SvPVX(sv) : NULL;
16564 =for apidoc sv_cat_decode
16566 C<encoding> is assumed to be an C<Encode> object, the PV of C<ssv> is
16567 assumed to be octets in that encoding and decoding the input starts
16568 from the position which S<C<(PV + *offset)>> pointed to. C<dsv> will be
16569 concatenated with the decoded UTF-8 string from C<ssv>. Decoding will terminate
16570 when the string C<tstr> appears in decoding output or the input ends on
16571 the PV of C<ssv>. The value which C<offset> points will be modified
16572 to the last input position on C<ssv>.
16574 Returns TRUE if the terminator was found, else returns FALSE.
16579 Perl_sv_cat_decode(pTHX_ SV *dsv, SV *encoding,
16580 SV *ssv, int *offset, char *tstr, int tlen)
16584 PERL_ARGS_ASSERT_SV_CAT_DECODE;
16586 if (SvPOK(ssv) && SvPOK(dsv) && SvROK(encoding)) {
16597 offsv = newSViv(*offset);
16599 mPUSHp(tstr, tlen);
16601 call_method("cat_decode", G_SCALAR);
16603 ret = SvTRUE(TOPs);
16604 *offset = SvIV(offsv);
16610 Perl_croak(aTHX_ "Invalid argument to sv_cat_decode");
16615 /* ---------------------------------------------------------------------
16617 * support functions for report_uninit()
16620 /* the maxiumum size of array or hash where we will scan looking
16621 * for the undefined element that triggered the warning */
16623 #define FUV_MAX_SEARCH_SIZE 1000
16625 /* Look for an entry in the hash whose value has the same SV as val;
16626 * If so, return a mortal copy of the key. */
16629 S_find_hash_subscript(pTHX_ const HV *const hv, const SV *const val)
16634 PERL_ARGS_ASSERT_FIND_HASH_SUBSCRIPT;
16636 if (!hv || SvMAGICAL(hv) || !HvTOTALKEYS(hv) ||
16637 (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
16640 if (val == &PL_sv_undef || val == &PL_sv_placeholder)
16643 array = HvARRAY(hv);
16645 for (i=HvMAX(hv); i>=0; i--) {
16647 for (entry = array[i]; entry; entry = HeNEXT(entry)) {
16648 if (HeVAL(entry) == val)
16649 return newSVhek_mortal(HeKEY_hek(entry));
16655 /* Look for an entry in the array whose value has the same SV as val;
16656 * If so, return the index, otherwise return -1. */
16659 S_find_array_subscript(pTHX_ const AV *const av, const SV *const val)
16661 PERL_ARGS_ASSERT_FIND_ARRAY_SUBSCRIPT;
16663 if (!av || SvMAGICAL(av) || !AvARRAY(av) ||
16664 (AvFILLp(av) > FUV_MAX_SEARCH_SIZE))
16667 if (val != &PL_sv_undef) {
16668 SV ** const svp = AvARRAY(av);
16671 for (i=AvFILLp(av); i>=0; i--)
16678 /* varname(): return the name of a variable, optionally with a subscript.
16679 * If gv is non-zero, use the name of that global, along with gvtype (one
16680 * of "$", "@", "%"); otherwise use the name of the lexical at pad offset
16681 * targ. Depending on the value of the subscript_type flag, return:
16684 #define FUV_SUBSCRIPT_NONE 1 /* "@foo" */
16685 #define FUV_SUBSCRIPT_ARRAY 2 /* "$foo[aindex]" */
16686 #define FUV_SUBSCRIPT_HASH 3 /* "$foo{keyname}" */
16687 #define FUV_SUBSCRIPT_WITHIN 4 /* "within @foo" */
16690 Perl_varname(pTHX_ const GV *const gv, const char gvtype, PADOFFSET targ,
16691 const SV *const keyname, SSize_t aindex, int subscript_type)
16694 SV * const name = sv_newmortal();
16695 if (gv && isGV(gv)) {
16697 buffer[0] = gvtype;
16700 /* as gv_fullname4(), but add literal '^' for $^FOO names */
16702 gv_fullname4(name, gv, buffer, 0);
16704 if ((unsigned int)SvPVX(name)[1] <= 26) {
16706 buffer[1] = SvPVX(name)[1] + 'A' - 1;
16708 /* Swap the 1 unprintable control character for the 2 byte pretty
16709 version - ie substr($name, 1, 1) = $buffer; */
16710 sv_insert(name, 1, 1, buffer, 2);
16714 CV * const cv = gv ? ((CV *)gv) : find_runcv(NULL);
16717 assert(!cv || SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM);
16719 if (!cv || !CvPADLIST(cv))
16721 sv = padnamelist_fetch(PadlistNAMES(CvPADLIST(cv)), targ);
16722 sv_setpvn(name, PadnamePV(sv), PadnameLEN(sv));
16726 if (subscript_type == FUV_SUBSCRIPT_HASH) {
16727 SV * const sv = newSV_type(SVt_NULL);
16729 const char * const pv = SvPV_nomg_const((SV*)keyname, len);
16731 *SvPVX(name) = '$';
16732 Perl_sv_catpvf(aTHX_ name, "{%s}",
16733 pv_pretty(sv, pv, len, 32, NULL, NULL,
16734 PERL_PV_PRETTY_DUMP | PERL_PV_ESCAPE_UNI_DETECT ));
16735 SvREFCNT_dec_NN(sv);
16737 else if (subscript_type == FUV_SUBSCRIPT_ARRAY) {
16738 *SvPVX(name) = '$';
16739 Perl_sv_catpvf(aTHX_ name, "[%" IVdf "]", (IV)aindex);
16741 else if (subscript_type == FUV_SUBSCRIPT_WITHIN) {
16742 /* We know that name has no magic, so can use 0 instead of SV_GMAGIC */
16743 Perl_sv_insert_flags(aTHX_ name, 0, 0, STR_WITH_LEN("within "), 0);
16751 =apidoc_section $warning
16752 =for apidoc find_uninit_var
16754 Find the name of the undefined variable (if any) that caused the operator
16755 to issue a "Use of uninitialized value" warning.
16756 If match is true, only return a name if its value matches C<uninit_sv>.
16757 So roughly speaking, if a unary operator (such as C<OP_COS>) generates a
16758 warning, then following the direct child of the op may yield an
16759 C<OP_PADSV> or C<OP_GV> that gives the name of the undefined variable. On the
16760 other hand, with C<OP_ADD> there are two branches to follow, so we only print
16761 the variable name if we get an exact match.
16762 C<desc_p> points to a string pointer holding the description of the op.
16763 This may be updated if needed.
16765 The name is returned as a mortal SV.
16767 Assumes that C<PL_op> is the OP that originally triggered the error, and that
16768 C<PL_comppad>/C<PL_curpad> points to the currently executing pad.
16774 S_find_uninit_var(pTHX_ const OP *const obase, const SV *const uninit_sv,
16775 bool match, const char **desc_p)
16779 const OP *o, *o2, *kid;
16781 PERL_ARGS_ASSERT_FIND_UNINIT_VAR;
16783 if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef ||
16784 uninit_sv == &PL_sv_placeholder)))
16787 switch (obase->op_type) {
16790 /* the optimizer rewrites '$x = undef' to 'undef $x' for lexical
16791 * variables, which can occur as the source of warnings:
16792 * ($x = undef) =~ s/a/b/;
16793 * The OPpUNDEF_KEEP_PV flag indicates that this used to be an
16795 * Otherwise undef should not care if its args are undef - any warnings
16796 * will be from tied/magic vars */
16798 (obase->op_private & (OPpTARGET_MY | OPpUNDEF_KEEP_PV)) == (OPpTARGET_MY | OPpUNDEF_KEEP_PV)
16799 && (!match || PAD_SVl(obase->op_targ) == uninit_sv)
16801 return varname(NULL, '$', obase->op_targ, NULL, 0, FUV_SUBSCRIPT_NONE);
16810 const bool pad = ( obase->op_type == OP_PADAV
16811 || obase->op_type == OP_PADHV
16812 || obase->op_type == OP_PADRANGE
16815 const bool hash = ( obase->op_type == OP_PADHV
16816 || obase->op_type == OP_RV2HV
16817 || (obase->op_type == OP_PADRANGE
16818 && SvTYPE(PAD_SVl(obase->op_targ)) == SVt_PVHV)
16822 int subscript_type = FUV_SUBSCRIPT_WITHIN;
16824 if (pad) { /* @lex, %lex */
16825 sv = PAD_SVl(obase->op_targ);
16829 if (cUNOPx(obase)->op_first->op_type == OP_GV) {
16830 /* @global, %global */
16831 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
16834 sv = hash ? MUTABLE_SV(GvHV(gv)): MUTABLE_SV(GvAV(gv));
16836 else if (obase == PL_op) /* @{expr}, %{expr} */
16837 return find_uninit_var(cUNOPx(obase)->op_first,
16838 uninit_sv, match, desc_p);
16839 else /* @{expr}, %{expr} as a sub-expression */
16843 /* attempt to find a match within the aggregate */
16845 keysv = find_hash_subscript((const HV*)sv, uninit_sv);
16847 subscript_type = FUV_SUBSCRIPT_HASH;
16850 index = find_array_subscript((const AV *)sv, uninit_sv);
16852 subscript_type = FUV_SUBSCRIPT_ARRAY;
16855 if (match && subscript_type == FUV_SUBSCRIPT_WITHIN)
16858 return varname(gv, (char)(hash ? '%' : '@'), obase->op_targ,
16859 keysv, index, subscript_type);
16863 if (cUNOPx(obase)->op_first->op_type == OP_GV) {
16865 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
16866 if (!gv || !GvSTASH(gv))
16868 if (match && (GvSV(gv) != uninit_sv))
16870 return varname(gv, '$', 0, NULL, 0, FUV_SUBSCRIPT_NONE);
16873 return find_uninit_var(cUNOPx(obase)->op_first, uninit_sv, 1, desc_p);
16876 if (match && PAD_SVl(obase->op_targ) != uninit_sv)
16878 return varname(NULL, '$', obase->op_targ,
16879 NULL, 0, FUV_SUBSCRIPT_NONE);
16881 case OP_PADSV_STORE:
16882 if (match && PAD_SVl(obase->op_targ) != uninit_sv)
16884 return varname(NULL, '$', obase->op_targ,
16885 NULL, 0, FUV_SUBSCRIPT_NONE);
16888 gv = cGVOPx_gv(obase);
16889 if (!gv || (match && GvSV(gv) != uninit_sv) || !GvSTASH(gv))
16891 return varname(gv, '$', 0, NULL, 0, FUV_SUBSCRIPT_NONE);
16893 case OP_AELEMFAST_LEX:
16896 AV *av = MUTABLE_AV(PAD_SV(obase->op_targ));
16897 if (!av || SvRMAGICAL(av))
16899 svp = av_fetch(av, (I8)obase->op_private, FALSE);
16900 if (!svp || *svp != uninit_sv)
16903 return varname(NULL, '$', obase->op_targ,
16904 NULL, (I8)obase->op_private, FUV_SUBSCRIPT_ARRAY);
16906 case OP_AELEMFASTLEX_STORE:
16909 AV *av = MUTABLE_AV(PAD_SV(obase->op_targ));
16910 if (!av || SvRMAGICAL(av))
16912 svp = av_fetch(av, (I8)obase->op_private, FALSE);
16913 if (!svp || *svp != uninit_sv)
16916 return varname(NULL, '$', obase->op_targ,
16917 NULL, (I8)obase->op_private, FUV_SUBSCRIPT_ARRAY);
16921 gv = cGVOPx_gv(obase);
16926 AV *const av = GvAV(gv);
16927 if (!av || SvRMAGICAL(av))
16929 svp = av_fetch(av, (I8)obase->op_private, FALSE);
16930 if (!svp || *svp != uninit_sv)
16933 return varname(gv, '$', 0,
16934 NULL, (I8)obase->op_private, FUV_SUBSCRIPT_ARRAY);
16936 NOT_REACHED; /* NOTREACHED */
16939 o = cUNOPx(obase)->op_first;
16940 if (!o || o->op_type != OP_NULL ||
16941 ! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM))
16943 return find_uninit_var(cBINOPo->op_last, uninit_sv, match, desc_p);
16948 bool negate = FALSE;
16950 if (PL_op == obase)
16951 /* $a[uninit_expr] or $h{uninit_expr} */
16952 return find_uninit_var(cBINOPx(obase)->op_last,
16953 uninit_sv, match, desc_p);
16956 o = cBINOPx(obase)->op_first;
16957 kid = cBINOPx(obase)->op_last;
16959 /* get the av or hv, and optionally the gv */
16961 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) {
16962 sv = PAD_SV(o->op_targ);
16964 else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV)
16965 && cUNOPo->op_first->op_type == OP_GV)
16967 gv = cGVOPx_gv(cUNOPo->op_first);
16971 == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(GvAV(gv));
16976 if (kid && kid->op_type == OP_NEGATE) {
16978 kid = cUNOPx(kid)->op_first;
16981 if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) {
16982 /* index is constant */
16985 kidsv = newSVpvs_flags("-", SVs_TEMP);
16986 sv_catsv(kidsv, cSVOPx_sv(kid));
16989 kidsv = cSVOPx_sv(kid);
16993 if (obase->op_type == OP_HELEM) {
16994 HE* he = hv_fetch_ent(MUTABLE_HV(sv), kidsv, 0, 0);
16995 if (!he || HeVAL(he) != uninit_sv)
16999 SV * const opsv = cSVOPx_sv(kid);
17000 const IV opsviv = SvIV(opsv);
17001 SV * const * const svp = av_fetch(MUTABLE_AV(sv),
17002 negate ? - opsviv : opsviv,
17004 if (!svp || *svp != uninit_sv)
17008 if (obase->op_type == OP_HELEM)
17009 return varname(gv, '%', o->op_targ,
17010 kidsv, 0, FUV_SUBSCRIPT_HASH);
17012 return varname(gv, '@', o->op_targ, NULL,
17013 negate ? - SvIV(cSVOPx_sv(kid)) : SvIV(cSVOPx_sv(kid)),
17014 FUV_SUBSCRIPT_ARRAY);
17017 /* index is an expression;
17018 * attempt to find a match within the aggregate */
17019 if (obase->op_type == OP_HELEM) {
17020 SV * const keysv = find_hash_subscript((const HV*)sv, uninit_sv);
17022 return varname(gv, '%', o->op_targ,
17023 keysv, 0, FUV_SUBSCRIPT_HASH);
17026 const SSize_t index
17027 = find_array_subscript((const AV *)sv, uninit_sv);
17029 return varname(gv, '@', o->op_targ,
17030 NULL, index, FUV_SUBSCRIPT_ARRAY);
17035 (char)((o->op_type == OP_PADAV || o->op_type == OP_RV2AV)
17037 o->op_targ, NULL, 0, FUV_SUBSCRIPT_WITHIN);
17039 NOT_REACHED; /* NOTREACHED */
17042 case OP_MULTIDEREF: {
17043 /* If we were executing OP_MULTIDEREF when the undef warning
17044 * triggered, then it must be one of the index values within
17045 * that triggered it. If not, then the only possibility is that
17046 * the value retrieved by the last aggregate index might be the
17047 * culprit. For the former, we set PL_multideref_pc each time before
17048 * using an index, so work though the item list until we reach
17049 * that point. For the latter, just work through the entire item
17050 * list; the last aggregate retrieved will be the candidate.
17051 * There is a third rare possibility: something triggered
17052 * magic while fetching an array/hash element. Just display
17053 * nothing in this case.
17056 /* the named aggregate, if any */
17057 PADOFFSET agg_targ = 0;
17059 /* the last-seen index */
17061 PADOFFSET index_targ;
17063 IV index_const_iv = 0; /* init for spurious compiler warn */
17064 SV *index_const_sv;
17065 int depth = 0; /* how many array/hash lookups we've done */
17067 UNOP_AUX_item *items = cUNOP_AUXx(obase)->op_aux;
17068 UNOP_AUX_item *last = NULL;
17069 UV actions = items->uv;
17072 if (PL_op == obase) {
17073 last = PL_multideref_pc;
17074 assert(last >= items && last <= items + items[-1].uv);
17081 switch (actions & MDEREF_ACTION_MASK) {
17083 case MDEREF_reload:
17084 actions = (++items)->uv;
17087 case MDEREF_HV_padhv_helem: /* $lex{...} */
17090 case MDEREF_AV_padav_aelem: /* $lex[...] */
17091 agg_targ = (++items)->pad_offset;
17095 case MDEREF_HV_gvhv_helem: /* $pkg{...} */
17098 case MDEREF_AV_gvav_aelem: /* $pkg[...] */
17100 agg_gv = (GV*)UNOP_AUX_item_sv(++items);
17101 assert(isGV_with_GP(agg_gv));
17104 case MDEREF_HV_gvsv_vivify_rv2hv_helem: /* $pkg->{...} */
17105 case MDEREF_HV_padsv_vivify_rv2hv_helem: /* $lex->{...} */
17108 case MDEREF_HV_pop_rv2hv_helem: /* expr->{...} */
17109 case MDEREF_HV_vivify_rv2hv_helem: /* vivify, ->{...} */
17115 case MDEREF_AV_gvsv_vivify_rv2av_aelem: /* $pkg->[...] */
17116 case MDEREF_AV_padsv_vivify_rv2av_aelem: /* $lex->[...] */
17119 case MDEREF_AV_pop_rv2av_aelem: /* expr->[...] */
17120 case MDEREF_AV_vivify_rv2av_aelem: /* vivify, ->[...] */
17127 index_const_sv = NULL;
17129 index_type = (actions & MDEREF_INDEX_MASK);
17130 switch (index_type) {
17131 case MDEREF_INDEX_none:
17133 case MDEREF_INDEX_const:
17135 index_const_sv = UNOP_AUX_item_sv(++items)
17137 index_const_iv = (++items)->iv;
17139 case MDEREF_INDEX_padsv:
17140 index_targ = (++items)->pad_offset;
17142 case MDEREF_INDEX_gvsv:
17143 index_gv = (GV*)UNOP_AUX_item_sv(++items);
17144 assert(isGV_with_GP(index_gv));
17148 if (index_type != MDEREF_INDEX_none)
17151 if ( index_type == MDEREF_INDEX_none
17152 || (actions & MDEREF_FLAG_last)
17153 || (last && items >= last)
17157 actions >>= MDEREF_SHIFT;
17160 if (PL_op == obase) {
17161 /* most likely index was undef */
17163 *desc_p = ( (actions & MDEREF_FLAG_last)
17164 && (obase->op_private
17165 & (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE)))
17167 (obase->op_private & OPpMULTIDEREF_EXISTS)
17170 : is_hv ? "hash element" : "array element";
17171 assert(index_type != MDEREF_INDEX_none);
17173 if (GvSV(index_gv) == uninit_sv)
17174 return varname(index_gv, '$', 0, NULL, 0,
17175 FUV_SUBSCRIPT_NONE);
17180 if (PL_curpad[index_targ] == uninit_sv)
17181 return varname(NULL, '$', index_targ,
17182 NULL, 0, FUV_SUBSCRIPT_NONE);
17186 /* If we got to this point it was undef on a const subscript,
17187 * so magic probably involved, e.g. $ISA[0]. Give up. */
17191 /* the SV returned by pp_multideref() was undef, if anything was */
17197 sv = PAD_SV(agg_targ);
17199 sv = is_hv ? MUTABLE_SV(GvHV(agg_gv)) : MUTABLE_SV(GvAV(agg_gv));
17206 if (index_type == MDEREF_INDEX_const) {
17211 HE* he = hv_fetch_ent(MUTABLE_HV(sv), index_const_sv, 0, 0);
17212 if (!he || HeVAL(he) != uninit_sv)
17216 SV * const * const svp =
17217 av_fetch(MUTABLE_AV(sv), index_const_iv, FALSE);
17218 if (!svp || *svp != uninit_sv)
17223 ? varname(agg_gv, '%', agg_targ,
17224 index_const_sv, 0, FUV_SUBSCRIPT_HASH)
17225 : varname(agg_gv, '@', agg_targ,
17226 NULL, index_const_iv, FUV_SUBSCRIPT_ARRAY);
17229 /* index is an var */
17231 SV * const keysv = find_hash_subscript((const HV*)sv, uninit_sv);
17233 return varname(agg_gv, '%', agg_targ,
17234 keysv, 0, FUV_SUBSCRIPT_HASH);
17237 const SSize_t index
17238 = find_array_subscript((const AV *)sv, uninit_sv);
17240 return varname(agg_gv, '@', agg_targ,
17241 NULL, index, FUV_SUBSCRIPT_ARRAY);
17243 /* look for an element not found */
17244 if (!SvMAGICAL(sv)) {
17245 SV *index_sv = NULL;
17247 index_sv = PL_curpad[index_targ];
17249 else if (index_gv) {
17250 index_sv = GvSV(index_gv);
17252 if (index_sv && !SvMAGICAL(index_sv) && !SvROK(index_sv)) {
17254 SV *report_index_sv = SvOK(index_sv) ? index_sv : &PL_sv_no;
17255 HE *he = hv_fetch_ent(MUTABLE_HV(sv), report_index_sv, 0, 0);
17257 return varname(agg_gv, '%', agg_targ,
17258 report_index_sv, 0, FUV_SUBSCRIPT_HASH);
17262 SSize_t index = SvOK(index_sv) ? SvIV(index_sv) : 0;
17263 SV * const * const svp =
17264 av_fetch(MUTABLE_AV(sv), index, FALSE);
17266 return varname(agg_gv, '@', agg_targ,
17267 NULL, index, FUV_SUBSCRIPT_ARRAY);
17274 return varname(agg_gv,
17276 agg_targ, NULL, 0, FUV_SUBSCRIPT_WITHIN);
17278 NOT_REACHED; /* NOTREACHED */
17282 /* only examine RHS */
17283 return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv,
17287 o = cUNOPx(obase)->op_first;
17288 if ( o->op_type == OP_PUSHMARK
17289 || (o->op_type == OP_NULL && o->op_targ == OP_PUSHMARK)
17293 if (!OpHAS_SIBLING(o)) {
17294 /* one-arg version of open is highly magical */
17296 if (o->op_type == OP_GV) { /* open FOO; */
17298 if (match && GvSV(gv) != uninit_sv)
17300 return varname(gv, '$', 0,
17301 NULL, 0, FUV_SUBSCRIPT_NONE);
17303 /* other possibilities not handled are:
17304 * open $x; or open my $x; should return '${*$x}'
17305 * open expr; should return '$'.expr ideally
17312 /* ops where $_ may be an implicit arg */
17317 if ( !(obase->op_flags & OPf_STACKED)) {
17318 if (uninit_sv == DEFSV)
17319 return newSVpvs_flags("$_", SVs_TEMP);
17320 else if (obase->op_targ
17321 && uninit_sv == PAD_SVl(obase->op_targ))
17322 return varname(NULL, '$', obase->op_targ, NULL, 0,
17323 FUV_SUBSCRIPT_NONE);
17330 match = 1; /* print etc can return undef on defined args */
17331 /* skip filehandle as it can't produce 'undef' warning */
17332 o = cUNOPx(obase)->op_first;
17333 if ((obase->op_flags & OPf_STACKED)
17335 ( o->op_type == OP_PUSHMARK
17336 || (o->op_type == OP_NULL && o->op_targ == OP_PUSHMARK)))
17337 o = OpSIBLING(OpSIBLING(o));
17341 case OP_ENTEREVAL: /* could be eval $undef or $x='$undef'; eval $x */
17342 case OP_CUSTOM: /* XS or custom code could trigger random warnings */
17344 /* the following ops are capable of returning PL_sv_undef even for
17345 * defined arg(s) */
17364 case OP_GETPEERNAME:
17411 case OP_SMARTMATCH:
17420 /* XXX tmp hack: these two may call an XS sub, and currently
17421 XS subs don't have a SUB entry on the context stack, so CV and
17422 pad determination goes wrong, and BAD things happen. So, just
17423 don't try to determine the value under those circumstances.
17424 Need a better fix at dome point. DAPM 11/2007 */
17430 GV * const gv = gv_fetchpvs(".", GV_NOTQUAL, SVt_PV);
17431 if (gv && GvSV(gv) == uninit_sv)
17432 return newSVpvs_flags("$.", SVs_TEMP);
17437 /* def-ness of rval pos() is independent of the def-ness of its arg */
17438 if ( !(obase->op_flags & OPf_MOD))
17444 if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs))
17445 return newSVpvs_flags("${$/}", SVs_TEMP);
17450 if (!(obase->op_flags & OPf_KIDS))
17452 o = cUNOPx(obase)->op_first;
17458 /* This loop checks all the kid ops, skipping any that cannot pos-
17459 * sibly be responsible for the uninitialized value; i.e., defined
17460 * constants and ops that return nothing. If there is only one op
17461 * left that is not skipped, then we *know* it is responsible for
17462 * the uninitialized value. If there is more than one op left, we
17463 * have to look for an exact match in the while() loop below.
17464 * Note that we skip padrange, because the individual pad ops that
17465 * it replaced are still in the tree, so we work on them instead.
17468 for (kid=o; kid; kid = OpSIBLING(kid)) {
17469 const OPCODE type = kid->op_type;
17470 if ( (type == OP_CONST && SvOK(cSVOPx_sv(kid)))
17471 || (type == OP_NULL && ! (kid->op_flags & OPf_KIDS))
17472 || (type == OP_PUSHMARK)
17473 || (type == OP_PADRANGE)
17477 if (o2) { /* more than one found */
17484 return find_uninit_var(o2, uninit_sv, match, desc_p);
17486 /* scan all args */
17488 sv = find_uninit_var(o, uninit_sv, 1, desc_p);
17500 =for apidoc_section $warning
17501 =for apidoc report_uninit
17503 Print appropriate "Use of uninitialized variable" warning.
17509 Perl_report_uninit(pTHX_ const SV *uninit_sv)
17511 const char *desc = NULL;
17512 SV* varname = NULL;
17515 desc = PL_op->op_type == OP_STRINGIFY && PL_op->op_folded
17517 : PL_op->op_type == OP_MULTICONCAT
17518 && (PL_op->op_private & OPpMULTICONCAT_FAKE)
17521 if (uninit_sv && PL_curpad) {
17522 varname = find_uninit_var(PL_op, uninit_sv, 0, &desc);
17524 sv_insert(varname, 0, 0, " ", 1);
17527 else if (PL_curstackinfo->si_type == PERLSI_SORT && cxstack_ix == 0)
17528 /* we've reached the end of a sort block or sub,
17529 * and the uninit value is probably what that code returned */
17532 /* PL_warn_uninit_sv is constant */
17533 GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral);
17535 /* diag_listed_as: Use of uninitialized value%s */
17536 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit_sv,
17537 SVfARG(varname ? varname : &PL_sv_no),
17540 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
17542 GCC_DIAG_RESTORE_STMT;
17546 * ex: set ts=8 sts=4 sw=4 et: