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 I32 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 going forwards is twice the speed of going
7428 forward (that's where the 2 * backw comes from).
7429 (The real figure of course depends on the UTF-8 data.) */
7430 const U8 *s = start;
7432 while (s < send && uoffset--)
7442 while (UTF8_IS_CONTINUATION(*send))
7445 return send - start;
7448 /* For the string representation of the given scalar, find the byte
7449 corresponding to the passed in UTF-8 offset. uoffset0 and boffset0
7450 give another position in the string, *before* the sought offset, which
7451 (which is always true, as 0, 0 is a valid pair of positions), which should
7452 help reduce the amount of linear searching.
7453 If *mgp is non-NULL, it should point to the UTF-8 cache magic, which
7454 will be used to reduce the amount of linear searching. The cache will be
7455 created if necessary, and the found value offered to it for update. */
7457 S_sv_pos_u2b_cached(pTHX_ SV *const sv, MAGIC **const mgp, const U8 *const start,
7458 const U8 *const send, STRLEN uoffset,
7459 STRLEN uoffset0, STRLEN boffset0)
7461 STRLEN boffset = 0; /* Actually always set, but let's keep gcc happy. */
7463 bool at_end = FALSE;
7464 bool canonical_position = FALSE;
7466 PERL_ARGS_ASSERT_SV_POS_U2B_CACHED;
7468 assert (uoffset >= uoffset0);
7473 if (!SvREADONLY(sv) && !SvGMAGICAL(sv) && SvPOK(sv)
7475 && (*mgp || (SvTYPE(sv) >= SVt_PVMG &&
7476 (*mgp = mg_find(sv, PERL_MAGIC_utf8))))) {
7477 if ((*mgp)->mg_ptr) {
7478 STRLEN *cache = (STRLEN *) (*mgp)->mg_ptr;
7479 if (cache[0] == uoffset) {
7480 /* An exact match. */
7483 if (cache[2] == uoffset) {
7484 /* An exact match. */
7488 if (cache[0] < uoffset) {
7489 /* The cache already knows part of the way. */
7490 if (cache[0] > uoffset0) {
7491 /* The cache knows more than the passed in pair */
7492 uoffset0 = cache[0];
7493 boffset0 = cache[1];
7495 if ((*mgp)->mg_len != -1) {
7496 /* And we know the end too. */
7498 + sv_pos_u2b_midway(start + boffset0, send,
7500 (*mgp)->mg_len - uoffset0);
7502 uoffset -= uoffset0;
7504 + sv_pos_u2b_forwards(start + boffset0,
7505 send, &uoffset, &at_end,
7506 &canonical_position);
7507 uoffset += uoffset0;
7510 else if (cache[2] < uoffset) {
7511 /* We're between the two cache entries. */
7512 if (cache[2] > uoffset0) {
7513 /* and the cache knows more than the passed in pair */
7514 uoffset0 = cache[2];
7515 boffset0 = cache[3];
7519 + sv_pos_u2b_midway(start + boffset0,
7522 cache[0] - uoffset0);
7525 + sv_pos_u2b_midway(start + boffset0,
7528 cache[2] - uoffset0);
7532 else if ((*mgp)->mg_len != -1) {
7533 /* If we can take advantage of a passed in offset, do so. */
7534 /* In fact, offset0 is either 0, or less than offset, so don't
7535 need to worry about the other possibility. */
7537 + sv_pos_u2b_midway(start + boffset0, send,
7539 (*mgp)->mg_len - uoffset0);
7544 if (!found || PL_utf8cache < 0) {
7545 STRLEN real_boffset;
7546 uoffset -= uoffset0;
7547 real_boffset = boffset0 + sv_pos_u2b_forwards(start + boffset0,
7548 send, &uoffset, &at_end,
7549 &canonical_position);
7550 uoffset += uoffset0;
7552 if (found && PL_utf8cache < 0)
7553 assert_uft8_cache_coherent("sv_pos_u2b_cache", boffset,
7555 boffset = real_boffset;
7558 if (PL_utf8cache && canonical_position && !SvGMAGICAL(sv) && SvPOK(sv)) {
7560 utf8_mg_len_cache_update(sv, mgp, uoffset);
7562 utf8_mg_pos_cache_update(sv, mgp, boffset, uoffset, send - start);
7569 =for apidoc sv_pos_u2b_flags
7571 Converts the offset from a count of UTF-8 chars from
7572 the start of the string, to a count of the equivalent number of bytes; if
7573 C<lenp> is non-zero, it does the same to C<lenp>, but this time starting from
7574 C<offset>, rather than from the start
7575 of the string. Handles type coercion.
7576 C<flags> is passed to C<SvPV_flags>, and usually should be
7577 C<SV_GMAGIC|SV_CONST_RETURN> to handle magic.
7583 * sv_pos_u2b_flags() uses, like sv_pos_b2u(), the mg_ptr of the potential
7584 * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
7585 * byte offsets. See also the comments of S_utf8_mg_pos_cache_update().
7590 Perl_sv_pos_u2b_flags(pTHX_ SV *const sv, STRLEN uoffset, STRLEN *const lenp,
7597 PERL_ARGS_ASSERT_SV_POS_U2B_FLAGS;
7599 start = (U8*)SvPV_flags(sv, len, flags);
7601 const U8 * const send = start + len;
7603 boffset = sv_pos_u2b_cached(sv, &mg, start, send, uoffset, 0, 0);
7606 && *lenp /* don't bother doing work for 0, as its bytes equivalent
7607 is 0, and *lenp is already set to that. */) {
7608 /* Convert the relative offset to absolute. */
7609 const STRLEN uoffset2 = uoffset + *lenp;
7610 const STRLEN boffset2
7611 = sv_pos_u2b_cached(sv, &mg, start, send, uoffset2,
7612 uoffset, boffset) - boffset;
7626 =for apidoc sv_pos_u2b
7628 Converts the value pointed to by C<offsetp> from a count of UTF-8 chars from
7629 the start of the string, to a count of the equivalent number of bytes; if
7630 C<lenp> is non-zero, it does the same to C<lenp>, but this time starting from
7631 the offset, rather than from the start of the string. Handles magic and
7634 Use C<sv_pos_u2b_flags> in preference, which correctly handles strings longer
7641 * sv_pos_u2b() uses, like sv_pos_b2u(), the mg_ptr of the potential
7642 * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
7643 * byte offsets. See also the comments of S_utf8_mg_pos_cache_update().
7647 /* This function is subject to size and sign problems */
7650 Perl_sv_pos_u2b(pTHX_ SV *const sv, I32 *const offsetp, I32 *const lenp)
7652 PERL_ARGS_ASSERT_SV_POS_U2B;
7655 STRLEN ulen = (STRLEN)*lenp;
7656 *offsetp = (I32)sv_pos_u2b_flags(sv, (STRLEN)*offsetp, &ulen,
7657 SV_GMAGIC|SV_CONST_RETURN);
7660 *offsetp = (I32)sv_pos_u2b_flags(sv, (STRLEN)*offsetp, NULL,
7661 SV_GMAGIC|SV_CONST_RETURN);
7666 S_utf8_mg_len_cache_update(pTHX_ SV *const sv, MAGIC **const mgp,
7669 PERL_ARGS_ASSERT_UTF8_MG_LEN_CACHE_UPDATE;
7670 if (SvREADONLY(sv) || SvGMAGICAL(sv) || !SvPOK(sv))
7673 if (!*mgp && (SvTYPE(sv) < SVt_PVMG ||
7674 !(*mgp = mg_find(sv, PERL_MAGIC_utf8)))) {
7675 *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, &PL_vtbl_utf8, 0, 0);
7679 (*mgp)->mg_len = ulen;
7682 /* Create and update the UTF8 magic offset cache, with the proffered utf8/
7683 byte length pairing. The (byte) length of the total SV is passed in too,
7684 as blen, because for some (more esoteric) SVs, the call to SvPV_const()
7685 may not have updated SvCUR, so we can't rely on reading it directly.
7687 The proffered utf8/byte length pairing isn't used if the cache already has
7688 two pairs, and swapping either for the proffered pair would increase the
7689 RMS of the intervals between known byte offsets.
7691 The cache itself consists of 4 STRLEN values
7692 0: larger UTF-8 offset
7693 1: corresponding byte offset
7694 2: smaller UTF-8 offset
7695 3: corresponding byte offset
7697 Unused cache pairs have the value 0, 0.
7698 Keeping the cache "backwards" means that the invariant of
7699 cache[0] >= cache[2] is maintained even with empty slots, which means that
7700 the code that uses it doesn't need to worry if only 1 entry has actually
7701 been set to non-zero. It also makes the "position beyond the end of the
7702 cache" logic much simpler, as the first slot is always the one to start
7706 S_utf8_mg_pos_cache_update(pTHX_ SV *const sv, MAGIC **const mgp, const STRLEN byte,
7707 const STRLEN utf8, const STRLEN blen)
7711 PERL_ARGS_ASSERT_UTF8_MG_POS_CACHE_UPDATE;
7716 if (!*mgp && (SvTYPE(sv) < SVt_PVMG ||
7717 !(*mgp = mg_find(sv, PERL_MAGIC_utf8)))) {
7718 *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, (MGVTBL*)&PL_vtbl_utf8, 0,
7720 (*mgp)->mg_len = -1;
7724 if (!(cache = (STRLEN *)(*mgp)->mg_ptr)) {
7725 Newxz(cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
7726 (*mgp)->mg_ptr = (char *) cache;
7730 if (PL_utf8cache < 0 && SvPOKp(sv)) {
7731 /* SvPOKp() because, if sv is a reference, then SvPVX() is actually
7732 a pointer. Note that we no longer cache utf8 offsets on refer-
7733 ences, but this check is still a good idea, for robustness. */
7734 const U8 *start = (const U8 *) SvPVX_const(sv);
7735 const STRLEN realutf8 = utf8_length(start, start + byte);
7737 assert_uft8_cache_coherent("utf8_mg_pos_cache_update", utf8, realutf8,
7741 /* Cache is held with the later position first, to simplify the code
7742 that deals with unbounded ends. */
7744 ASSERT_UTF8_CACHE(cache);
7745 if (cache[1] == 0) {
7746 /* Cache is totally empty */
7749 } else if (cache[3] == 0) {
7750 if (byte > cache[1]) {
7751 /* New one is larger, so goes first. */
7752 cache[2] = cache[0];
7753 cache[3] = cache[1];
7761 /* float casts necessary? XXX */
7762 #define THREEWAY_SQUARE(a,b,c,d) \
7763 ((float)((d) - (c))) * ((float)((d) - (c))) \
7764 + ((float)((c) - (b))) * ((float)((c) - (b))) \
7765 + ((float)((b) - (a))) * ((float)((b) - (a)))
7767 /* Cache has 2 slots in use, and we know three potential pairs.
7768 Keep the two that give the lowest RMS distance. Do the
7769 calculation in bytes simply because we always know the byte
7770 length. squareroot has the same ordering as the positive value,
7771 so don't bother with the actual square root. */
7772 if (byte > cache[1]) {
7773 /* New position is after the existing pair of pairs. */
7774 const float keep_earlier
7775 = THREEWAY_SQUARE(0, cache[3], byte, blen);
7776 const float keep_later
7777 = THREEWAY_SQUARE(0, cache[1], byte, blen);
7779 if (keep_later < keep_earlier) {
7780 cache[2] = cache[0];
7781 cache[3] = cache[1];
7787 const float keep_later = THREEWAY_SQUARE(0, byte, cache[1], blen);
7788 float b, c, keep_earlier;
7789 if (byte > cache[3]) {
7790 /* New position is between the existing pair of pairs. */
7791 b = (float)cache[3];
7794 /* New position is before the existing pair of pairs. */
7796 c = (float)cache[3];
7798 keep_earlier = THREEWAY_SQUARE(0, b, c, blen);
7799 if (byte > cache[3]) {
7800 if (keep_later < keep_earlier) {
7810 if (! (keep_later < keep_earlier)) {
7811 cache[0] = cache[2];
7812 cache[1] = cache[3];
7819 ASSERT_UTF8_CACHE(cache);
7822 /* We already know all of the way, now we may be able to walk back. The same
7823 assumption is made as in S_sv_pos_u2b_midway(), namely that walking
7824 backward is half the speed of walking forward. */
7826 S_sv_pos_b2u_midway(pTHX_ const U8 *const s, const U8 *const target,
7827 const U8 *end, STRLEN endu)
7829 const STRLEN forw = target - s;
7830 STRLEN backw = end - target;
7832 PERL_ARGS_ASSERT_SV_POS_B2U_MIDWAY;
7834 if (forw < 2 * backw) {
7835 return utf8_length(s, target);
7838 while (end > target) {
7840 while (UTF8_IS_CONTINUATION(*end)) {
7849 =for apidoc sv_pos_b2u_flags
7851 Converts C<offset> from a count of bytes from the start of the string, to
7852 a count of the equivalent number of UTF-8 chars. Handles type coercion.
7853 C<flags> is passed to C<SvPV_flags>, and usually should be
7854 C<SV_GMAGIC|SV_CONST_RETURN> to handle magic.
7860 * sv_pos_b2u_flags() uses, like sv_pos_u2b_flags(), the mg_ptr of the
7861 * potential PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8
7866 Perl_sv_pos_b2u_flags(pTHX_ SV *const sv, STRLEN const offset, U32 flags)
7869 STRLEN len = 0; /* Actually always set, but let's keep gcc happy. */
7875 PERL_ARGS_ASSERT_SV_POS_B2U_FLAGS;
7877 s = (const U8*)SvPV_flags(sv, blen, flags);
7880 Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset, blen=%" UVuf
7881 ", byte=%" UVuf, (UV)blen, (UV)offset);
7887 && SvTYPE(sv) >= SVt_PVMG
7888 && (mg = mg_find(sv, PERL_MAGIC_utf8)))
7891 STRLEN * const cache = (STRLEN *) mg->mg_ptr;
7892 if (cache[1] == offset) {
7893 /* An exact match. */
7896 if (cache[3] == offset) {
7897 /* An exact match. */
7901 if (cache[1] < offset) {
7902 /* We already know part of the way. */
7903 if (mg->mg_len != -1) {
7904 /* Actually, we know the end too. */
7906 + S_sv_pos_b2u_midway(aTHX_ s + cache[1], send,
7907 s + blen, mg->mg_len - cache[0]);
7909 len = cache[0] + utf8_length(s + cache[1], send);
7912 else if (cache[3] < offset) {
7913 /* We're between the two cached pairs, so we do the calculation
7914 offset by the byte/utf-8 positions for the earlier pair,
7915 then add the utf-8 characters from the string start to
7917 len = S_sv_pos_b2u_midway(aTHX_ s + cache[3], send,
7918 s + cache[1], cache[0] - cache[2])
7922 else { /* cache[3] > offset */
7923 len = S_sv_pos_b2u_midway(aTHX_ s, send, s + cache[3],
7927 ASSERT_UTF8_CACHE(cache);
7929 } else if (mg->mg_len != -1) {
7930 len = S_sv_pos_b2u_midway(aTHX_ s, send, s + blen, mg->mg_len);
7934 if (!found || PL_utf8cache < 0) {
7935 const STRLEN real_len = utf8_length(s, send);
7937 if (found && PL_utf8cache < 0)
7938 assert_uft8_cache_coherent("sv_pos_b2u", len, real_len, sv);
7944 utf8_mg_len_cache_update(sv, &mg, len);
7946 utf8_mg_pos_cache_update(sv, &mg, offset, len, blen);
7953 =for apidoc sv_pos_b2u
7955 Converts the value pointed to by C<offsetp> from a count of bytes from the
7956 start of the string, to a count of the equivalent number of UTF-8 chars.
7957 Handles magic and type coercion.
7959 Use C<sv_pos_b2u_flags> in preference, which correctly handles strings
7966 * sv_pos_b2u() uses, like sv_pos_u2b(), the mg_ptr of the potential
7967 * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
7972 Perl_sv_pos_b2u(pTHX_ SV *const sv, I32 *const offsetp)
7974 PERL_ARGS_ASSERT_SV_POS_B2U;
7979 *offsetp = (I32)sv_pos_b2u_flags(sv, (STRLEN)*offsetp,
7980 SV_GMAGIC|SV_CONST_RETURN);
7984 S_assert_uft8_cache_coherent(pTHX_ const char *const func, STRLEN from_cache,
7985 STRLEN real, SV *const sv)
7987 PERL_ARGS_ASSERT_ASSERT_UFT8_CACHE_COHERENT;
7989 /* As this is debugging only code, save space by keeping this test here,
7990 rather than inlining it in all the callers. */
7991 if (from_cache == real)
7994 /* Need to turn the assertions off otherwise we may recurse infinitely
7995 while printing error messages. */
7996 SAVEI8(PL_utf8cache);
7998 Perl_croak(aTHX_ "panic: %s cache %" UVuf " real %" UVuf " for %" SVf,
7999 func, (UV) from_cache, (UV) real, SVfARG(sv));
8005 Returns a boolean indicating whether the strings in the two SVs are
8006 identical. Is UTF-8 and S<C<'use bytes'>> aware, handles get magic, and will
8007 coerce its args to strings if necessary.
8009 This function does not handle operator overloading. For a version that does,
8010 see instead C<sv_streq>.
8012 =for apidoc sv_eq_flags
8014 Returns a boolean indicating whether the strings in the two SVs are
8015 identical. Is UTF-8 and S<C<'use bytes'>> aware and coerces its args to strings
8016 if necessary. If the flags has the C<SV_GMAGIC> bit set, it handles get-magic, too.
8018 This function does not handle operator overloading. For a version that does,
8019 see instead C<sv_streq_flags>.
8025 Perl_sv_eq_flags(pTHX_ SV *sv1, SV *sv2, const U32 flags)
8037 /* if pv1 and pv2 are the same, second SvPV_const call may
8038 * invalidate pv1 (if we are handling magic), so we may need to
8040 if (sv1 == sv2 && flags & SV_GMAGIC
8041 && (SvTHINKFIRST(sv1) || SvGMAGICAL(sv1))) {
8042 pv1 = SvPV_const(sv1, cur1);
8043 sv1 = newSVpvn_flags(pv1, cur1, SVs_TEMP | SvUTF8(sv2));
8045 pv1 = SvPV_flags_const(sv1, cur1, flags);
8053 pv2 = SvPV_flags_const(sv2, cur2, flags);
8055 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
8056 /* Differing utf8ness. */
8058 /* sv1 is the UTF-8 one */
8059 return bytes_cmp_utf8((const U8*)pv2, cur2,
8060 (const U8*)pv1, cur1) == 0;
8063 /* sv2 is the UTF-8 one */
8064 return bytes_cmp_utf8((const U8*)pv1, cur1,
8065 (const U8*)pv2, cur2) == 0;
8070 return (pv1 == pv2) || memEQ(pv1, pv2, cur1);
8076 =for apidoc sv_streq_flags
8078 Returns a boolean indicating whether the strings in the two SVs are
8079 identical. If the flags argument has the C<SV_GMAGIC> bit set, it handles
8080 get-magic too. Will coerce its args to strings if necessary. Treats
8081 C<NULL> as undef. Correctly handles the UTF8 flag.
8083 If flags does not have the C<SV_SKIP_OVERLOAD> bit set, an attempt to use
8084 C<eq> overloading will be made. If such overloading does not exist or the
8085 flag is set, then regular string comparison will be used instead.
8087 =for apidoc sv_streq
8089 A convenient shortcut for calling C<sv_streq_flags> with the C<SV_GMAGIC>
8090 flag. This function basically behaves like the Perl code C<$sv1 eq $sv2>.
8096 Perl_sv_streq_flags(pTHX_ SV *sv1, SV *sv2, const U32 flags)
8098 PERL_ARGS_ASSERT_SV_STREQ_FLAGS;
8100 if(flags & SV_GMAGIC) {
8107 /* Treat NULL as undef */
8113 if(!(flags & SV_SKIP_OVERLOAD) &&
8114 (SvAMAGIC(sv1) || SvAMAGIC(sv2))) {
8115 SV *ret = amagic_call(sv1, sv2, seq_amg, 0);
8120 return sv_eq_flags(sv1, sv2, 0);
8124 =for apidoc sv_numeq_flags
8126 Returns a boolean indicating whether the numbers in the two SVs are
8127 identical. If the flags argument has the C<SV_GMAGIC> bit set, it handles
8128 get-magic too. Will coerce its args to numbers if necessary. Treats
8131 If flags does not have the C<SV_SKIP_OVERLOAD> bit set, an attempt to use
8132 C<==> overloading will be made. If such overloading does not exist or the
8133 flag is set, then regular numerical comparison will be used instead.
8135 =for apidoc sv_numeq
8137 A convenient shortcut for calling C<sv_numeq_flags> with the C<SV_GMAGIC>
8138 flag. This function basically behaves like the Perl code C<$sv1 == $sv2>.
8144 Perl_sv_numeq_flags(pTHX_ SV *sv1, SV *sv2, const U32 flags)
8146 PERL_ARGS_ASSERT_SV_NUMEQ_FLAGS;
8148 if(flags & SV_GMAGIC) {
8155 /* Treat NULL as undef */
8161 if(!(flags & SV_SKIP_OVERLOAD) &&
8162 (SvAMAGIC(sv1) || SvAMAGIC(sv2))) {
8163 SV *ret = amagic_call(sv1, sv2, eq_amg, 0);
8168 return do_ncmp(sv1, sv2) == 0;
8174 Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
8175 string in C<sv1> is less than, equal to, or greater than the string in
8176 C<sv2>. Is UTF-8 and S<C<'use bytes'>> aware, handles get magic, and will
8177 coerce its args to strings if necessary. See also C<L</sv_cmp_locale>>.
8179 =for apidoc sv_cmp_flags
8181 Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
8182 string in C<sv1> is less than, equal to, or greater than the string in
8183 C<sv2>. Is UTF-8 and S<C<'use bytes'>> aware and will coerce its args to strings
8184 if necessary. If the flags has the C<SV_GMAGIC> bit set, it handles get magic. See
8185 also C<L</sv_cmp_locale_flags>>.
8191 Perl_sv_cmp(pTHX_ SV *const sv1, SV *const sv2)
8193 return sv_cmp_flags(sv1, sv2, SV_GMAGIC);
8197 Perl_sv_cmp_flags(pTHX_ SV *const sv1, SV *const sv2,
8201 const char *pv1, *pv2;
8203 SV *svrecode = NULL;
8210 pv1 = SvPV_flags_const(sv1, cur1, flags);
8217 pv2 = SvPV_flags_const(sv2, cur2, flags);
8219 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
8220 /* Differing utf8ness. */
8222 const int retval = -bytes_cmp_utf8((const U8*)pv2, cur2,
8223 (const U8*)pv1, cur1);
8224 return retval ? retval < 0 ? -1 : +1 : 0;
8227 const int retval = bytes_cmp_utf8((const U8*)pv1, cur1,
8228 (const U8*)pv2, cur2);
8229 return retval ? retval < 0 ? -1 : +1 : 0;
8233 /* Here, if both are non-NULL, then they have the same UTF8ness. */
8236 cmp = cur2 ? -1 : 0;
8240 STRLEN shortest_len = cur1 < cur2 ? cur1 : cur2;
8243 if (! DO_UTF8(sv1)) {
8245 const I32 retval = memcmp((const void*)pv1,
8249 cmp = retval < 0 ? -1 : 1;
8250 } else if (cur1 == cur2) {
8253 cmp = cur1 < cur2 ? -1 : 1;
8257 else { /* Both are to be treated as UTF-EBCDIC */
8259 /* EBCDIC UTF-8 is complicated by the fact that it is based on I8
8260 * which remaps code points 0-255. We therefore generally have to
8261 * unmap back to the original values to get an accurate comparison.
8262 * But we don't have to do that for UTF-8 invariants, as by
8263 * definition, they aren't remapped, nor do we have to do it for
8264 * above-latin1 code points, as they also aren't remapped. (This
8265 * code also works on ASCII platforms, but the memcmp() above is
8268 const char *e = pv1 + shortest_len;
8270 /* Find the first bytes that differ between the two strings */
8271 while (pv1 < e && *pv1 == *pv2) {
8277 if (pv1 == e) { /* Are the same all the way to the end */
8281 cmp = cur1 < cur2 ? -1 : 1;
8284 else /* Here *pv1 and *pv2 are not equal, but all bytes earlier
8285 * in the strings were. The current bytes may or may not be
8286 * at the beginning of a character. But neither or both are
8287 * (or else earlier bytes would have been different). And
8288 * if we are in the middle of a character, the two
8289 * characters are comprised of the same number of bytes
8290 * (because in this case the start bytes are the same, and
8291 * the start bytes encode the character's length). */
8292 if (UTF8_IS_INVARIANT(*pv1))
8294 /* If both are invariants; can just compare directly */
8295 if (UTF8_IS_INVARIANT(*pv2)) {
8296 cmp = ((U8) *pv1 < (U8) *pv2) ? -1 : 1;
8298 else /* Since *pv1 is invariant, it is the whole character,
8299 which means it is at the beginning of a character.
8300 That means pv2 is also at the beginning of a
8301 character (see earlier comment). Since it isn't
8302 invariant, it must be a start byte. If it starts a
8303 character whose code point is above 255, that
8304 character is greater than any single-byte char, which
8306 if (UTF8_IS_ABOVE_LATIN1_START(*pv2))
8311 /* Here, pv2 points to a character composed of 2 bytes
8312 * whose code point is < 256. Get its code point and
8313 * compare with *pv1 */
8314 cmp = ((U8) *pv1 < EIGHT_BIT_UTF8_TO_NATIVE(*pv2, *(pv2 + 1)))
8319 else /* The code point starting at pv1 isn't a single byte */
8320 if (UTF8_IS_INVARIANT(*pv2))
8322 /* But here, the code point starting at *pv2 is a single byte,
8323 * and so *pv1 must begin a character, hence is a start byte.
8324 * If that character is above 255, it is larger than any
8325 * single-byte char, which *pv2 is */
8326 if (UTF8_IS_ABOVE_LATIN1_START(*pv1)) {
8330 /* Here, pv1 points to a character composed of 2 bytes
8331 * whose code point is < 256. Get its code point and
8332 * compare with the single byte character *pv2 */
8333 cmp = (EIGHT_BIT_UTF8_TO_NATIVE(*pv1, *(pv1 + 1)) < (U8) *pv2)
8338 else /* Here, we've ruled out either *pv1 and *pv2 being
8339 invariant. That means both are part of variants, but not
8340 necessarily at the start of a character */
8341 if ( UTF8_IS_ABOVE_LATIN1_START(*pv1)
8342 || UTF8_IS_ABOVE_LATIN1_START(*pv2))
8344 /* Here, at least one is the start of a character, which means
8345 * the other is also a start byte. And the code point of at
8346 * least one of the characters is above 255. It is a
8347 * characteristic of UTF-EBCDIC that all start bytes for
8348 * above-latin1 code points are well behaved as far as code
8349 * point comparisons go, and all are larger than all other
8350 * start bytes, so the comparison with those is also well
8352 cmp = ((U8) *pv1 < (U8) *pv2) ? -1 : 1;
8355 /* Here both *pv1 and *pv2 are part of variant characters.
8356 * They could be both continuations, or both start characters.
8357 * (One or both could even be an illegal start character (for
8358 * an overlong) which for the purposes of sorting we treat as
8360 if (UTF8_IS_CONTINUATION(*pv1)) {
8362 /* If they are continuations for code points above 255,
8363 * then comparing the current byte is sufficient, as there
8364 * is no remapping of these and so the comparison is
8365 * well-behaved. We determine if they are such
8366 * continuations by looking at the preceding byte. It
8367 * could be a start byte, from which we can tell if it is
8368 * for an above 255 code point. Or it could be a
8369 * continuation, which means the character occupies at
8370 * least 3 bytes, so must be above 255. */
8371 if ( UTF8_IS_CONTINUATION(*(pv2 - 1))
8372 || UTF8_IS_ABOVE_LATIN1_START(*(pv2 -1)))
8374 cmp = ((U8) *pv1 < (U8) *pv2) ? -1 : 1;
8378 /* Here, the continuations are for code points below 256;
8379 * back up one to get to the start byte */
8384 /* We need to get the actual native code point of each of these
8385 * variants in order to compare them */
8386 cmp = ( EIGHT_BIT_UTF8_TO_NATIVE(*pv1, *(pv1 + 1))
8387 < EIGHT_BIT_UTF8_TO_NATIVE(*pv2, *(pv2 + 1)))
8396 SvREFCNT_dec(svrecode);
8402 =for apidoc sv_cmp_locale
8404 Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and
8405 S<C<'use bytes'>> aware, handles get magic, and will coerce its args to strings
8406 if necessary. See also C<L</sv_cmp>>.
8408 =for apidoc sv_cmp_locale_flags
8410 Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and
8411 S<C<'use bytes'>> aware and will coerce its args to strings if necessary. If
8412 the flags contain C<SV_GMAGIC>, it handles get magic. See also
8413 C<L</sv_cmp_flags>>.
8419 Perl_sv_cmp_locale(pTHX_ SV *const sv1, SV *const sv2)
8421 return sv_cmp_locale_flags(sv1, sv2, SV_GMAGIC);
8425 Perl_sv_cmp_locale_flags(pTHX_ SV *const sv1, SV *const sv2,
8428 #ifdef USE_LOCALE_COLLATE
8434 if (PL_collation_standard)
8439 /* Revert to using raw compare if both operands exist, but either one
8440 * doesn't transform properly for collation */
8442 pv1 = sv_collxfrm_flags(sv1, &len1, flags);
8446 pv2 = sv_collxfrm_flags(sv2, &len2, flags);
8452 pv1 = sv1 ? sv_collxfrm_flags(sv1, &len1, flags) : (char *) NULL;
8453 pv2 = sv2 ? sv_collxfrm_flags(sv2, &len2, flags) : (char *) NULL;
8456 if (!pv1 || !len1) {
8467 retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
8470 return retval < 0 ? -1 : 1;
8473 * When the result of collation is equality, that doesn't mean
8474 * that there are no differences -- some locales exclude some
8475 * characters from consideration. So to avoid false equalities,
8476 * we use the raw string as a tiebreaker.
8483 PERL_UNUSED_ARG(flags);
8484 #endif /* USE_LOCALE_COLLATE */
8486 return sv_cmp(sv1, sv2);
8490 #ifdef USE_LOCALE_COLLATE
8493 =for apidoc sv_collxfrm
8495 This calls C<sv_collxfrm_flags> with the SV_GMAGIC flag. See
8496 C<L</sv_collxfrm_flags>>.
8498 =for apidoc sv_collxfrm_flags
8500 Add Collate Transform magic to an SV if it doesn't already have it. If the
8501 flags contain C<SV_GMAGIC>, it handles get-magic.
8503 Any scalar variable may carry C<PERL_MAGIC_collxfrm> magic that contains the
8504 scalar data of the variable, but transformed to such a format that a normal
8505 memory comparison can be used to compare the data according to the locale
8512 Perl_sv_collxfrm_flags(pTHX_ SV *const sv, STRLEN *const nxp, const I32 flags)
8516 PERL_ARGS_ASSERT_SV_COLLXFRM_FLAGS;
8518 mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_collxfrm) : (MAGIC *) NULL;
8520 /* If we don't have collation magic on 'sv', or the locale has changed
8521 * since the last time we calculated it, get it and save it now */
8522 if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
8527 /* Free the old space */
8529 Safefree(mg->mg_ptr);
8531 s = SvPV_flags_const(sv, len, flags);
8532 if ((xf = mem_collxfrm_(s, len, &xlen, cBOOL(SvUTF8(sv))))) {
8534 mg = sv_magicext(sv, 0, PERL_MAGIC_collxfrm, &PL_vtbl_collxfrm,
8549 if (mg && mg->mg_ptr) {
8551 return mg->mg_ptr + sizeof(PL_collation_ix);
8559 #endif /* USE_LOCALE_COLLATE */
8562 S_sv_gets_append_to_utf8(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
8564 SV * const tsv = newSV_type(SVt_NULL);
8567 sv_gets(tsv, fp, 0);
8568 sv_utf8_upgrade_nomg(tsv);
8569 SvCUR_set(sv,append);
8572 return (SvCUR(sv) - append) ? SvPVX(sv) : NULL;
8576 S_sv_gets_read_record(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
8579 const STRLEN recsize = SvUV(SvRV(PL_rs)); /* RsRECORD() guarantees > 0. */
8580 /* Grab the size of the record we're getting */
8581 char *buffer = SvGROW(sv, (STRLEN)(recsize + append + 1)) + append;
8588 /* With a true, record-oriented file on VMS, we need to use read directly
8589 * to ensure that we respect RMS record boundaries. The user is responsible
8590 * for providing a PL_rs value that corresponds to the FAB$W_MRS (maximum
8591 * record size) field. N.B. This is likely to produce invalid results on
8592 * varying-width character data when a record ends mid-character.
8594 fd = PerlIO_fileno(fp);
8596 && PerlLIO_fstat(fd, &st) == 0
8597 && (st.st_fab_rfm == FAB$C_VAR
8598 || st.st_fab_rfm == FAB$C_VFC
8599 || st.st_fab_rfm == FAB$C_FIX)) {
8601 bytesread = PerlLIO_read(fd, buffer, recsize);
8603 else /* in-memory file from PerlIO::Scalar
8604 * or not a record-oriented file
8608 bytesread = PerlIO_read(fp, buffer, recsize);
8610 /* At this point, the logic in sv_get() means that sv will
8611 be treated as utf-8 if the handle is utf8.
8613 if (PerlIO_isutf8(fp) && bytesread > 0) {
8614 char *bend = buffer + bytesread;
8615 char *bufp = buffer;
8616 size_t charcount = 0;
8617 bool charstart = TRUE;
8620 while (charcount < recsize) {
8621 /* count accumulated characters */
8622 while (bufp < bend) {
8624 skip = UTF8SKIP(bufp);
8626 if (bufp + skip > bend) {
8627 /* partial at the end */
8638 if (charcount < recsize) {
8640 STRLEN bufp_offset = bufp - buffer;
8641 SSize_t morebytesread;
8643 /* originally I read enough to fill any incomplete
8644 character and the first byte of the next
8645 character if needed, but if there's many
8646 multi-byte encoded characters we're going to be
8647 making a read call for every character beyond
8648 the original read size.
8650 So instead, read the rest of the character if
8651 any, and enough bytes to match at least the
8652 start bytes for each character we're going to
8656 readsize = recsize - charcount;
8658 readsize = skip - (bend - bufp) + recsize - charcount - 1;
8659 buffer = SvGROW(sv, append + bytesread + readsize + 1) + append;
8660 bend = buffer + bytesread;
8661 morebytesread = PerlIO_read(fp, bend, readsize);
8662 if (morebytesread <= 0) {
8663 /* we're done, if we still have incomplete
8664 characters the check code in sv_gets() will
8667 I'd originally considered doing
8668 PerlIO_ungetc() on all but the lead
8669 character of the incomplete character, but
8670 read() doesn't do that, so I don't.
8675 /* prepare to scan some more */
8676 bytesread += morebytesread;
8677 bend = buffer + bytesread;
8678 bufp = buffer + bufp_offset;
8686 SvCUR_set(sv, bytesread + append);
8687 buffer[bytesread] = '\0';
8688 return (SvCUR(sv) - append) ? SvPVX(sv) : NULL;
8694 Get a line from the filehandle and store it into the SV, optionally
8695 appending to the currently-stored string. If C<append> is not 0, the
8696 line is appended to the SV instead of overwriting it. C<append> should
8697 be set to the byte offset that the appended string should start at
8698 in the SV (typically, C<SvCUR(sv)> is a suitable choice).
8704 Perl_sv_gets(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
8714 PERL_ARGS_ASSERT_SV_GETS;
8716 if (SvTHINKFIRST(sv))
8717 sv_force_normal_flags(sv, append ? 0 : SV_COW_DROP_PV);
8718 /* XXX. If you make this PVIV, then copy on write can copy scalars read
8720 However, perlbench says it's slower, because the existing swipe code
8721 is faster than copy on write.
8722 Swings and roundabouts. */
8723 SvUPGRADE(sv, SVt_PV);
8726 /* line is going to be appended to the existing buffer in the sv */
8727 if (PerlIO_isutf8(fp)) {
8729 sv_utf8_upgrade_nomg(sv);
8730 sv_pos_u2b(sv,&append,0);
8732 } else if (SvUTF8(sv)) {
8733 return S_sv_gets_append_to_utf8(aTHX_ sv, fp, append);
8739 /* not appending - "clear" the string by setting SvCUR to 0,
8740 * the pv is still available. */
8743 if (PerlIO_isutf8(fp))
8746 if (IN_PERL_COMPILETIME) {
8747 /* we always read code in line mode */
8751 else if (RsSNARF(PL_rs)) {
8752 /* If it is a regular disk file use size from stat() as estimate
8753 of amount we are going to read -- may result in mallocing
8754 more memory than we really need if the layers below reduce
8755 the size we read (e.g. CRLF or a gzip layer).
8758 int fd = PerlIO_fileno(fp);
8759 if (fd >= 0 && (PerlLIO_fstat(fd, &st) == 0) && S_ISREG(st.st_mode)) {
8760 const Off_t offset = PerlIO_tell(fp);
8761 if (offset != (Off_t) -1 && st.st_size + append > offset) {
8762 #ifdef PERL_COPY_ON_WRITE
8763 /* Add an extra byte for the sake of copy-on-write's
8764 * buffer reference count. */
8765 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 2));
8767 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1));
8774 else if (RsRECORD(PL_rs)) {
8775 return S_sv_gets_read_record(aTHX_ sv, fp, append);
8777 else if (RsPARA(PL_rs)) {
8783 /* Get $/ i.e. PL_rs into same encoding as stream wants */
8784 if (PerlIO_isutf8(fp)) {
8785 rsptr = SvPVutf8(PL_rs, rslen);
8788 if (SvUTF8(PL_rs)) {
8789 if (!sv_utf8_downgrade(PL_rs, TRUE)) {
8790 Perl_croak(aTHX_ "Wide character in $/");
8793 /* extract the raw pointer to the record separator */
8794 rsptr = SvPV_const(PL_rs, rslen);
8798 /* rslast is the last character in the record separator
8799 * note we don't use rslast except when rslen is true, so the
8800 * null assign is a placeholder. */
8801 rslast = rslen ? rsptr[rslen - 1] : '\0';
8803 if (rspara) { /* have to do this both before and after */
8804 /* to make sure file boundaries work right */
8808 i = PerlIO_getc(fp);
8812 PerlIO_ungetc(fp,i);
8818 /* See if we know enough about I/O mechanism to cheat it ! */
8820 /* This used to be #ifdef test - it is made run-time test for ease
8821 of abstracting out stdio interface. One call should be cheap
8822 enough here - and may even be a macro allowing compile
8826 if (PerlIO_fast_gets(fp)) {
8828 * We can do buffer based IO operations on this filehandle.
8830 * This means we can bypass a lot of subcalls and process
8831 * the buffer directly, it also means we know the upper bound
8832 * on the amount of data we might read of the current buffer
8833 * into our sv. Knowing this allows us to preallocate the pv
8834 * to be able to hold that maximum, which allows us to simplify
8835 * a lot of logic. */
8838 * We're going to steal some values from the stdio struct
8839 * and put EVERYTHING in the innermost loop into registers.
8841 STDCHAR *ptr; /* pointer into fp's read-ahead buffer */
8842 STRLEN bpx; /* length of the data in the target sv
8843 used to fix pointers after a SvGROW */
8844 I32 shortbuffered; /* If the pv buffer is shorter than the amount
8845 of data left in the read-ahead buffer.
8846 If 0 then the pv buffer can hold the full
8847 amount left, otherwise this is the amount it
8850 /* Here is some breathtakingly efficient cheating */
8852 /* When you read the following logic resist the urge to think
8853 * of record separators that are 1 byte long. They are an
8854 * uninteresting special (simple) case.
8856 * Instead think of record separators which are at least 2 bytes
8857 * long, and keep in mind that we need to deal with such
8858 * separators when they cross a read-ahead buffer boundary.
8860 * Also consider that we need to gracefully deal with separators
8861 * that may be longer than a single read ahead buffer.
8863 * Lastly do not forget we want to copy the delimiter as well. We
8864 * are copying all data in the file _up_to_and_including_ the separator
8867 * Now that you have all that in mind here is what is happening below:
8869 * 1. When we first enter the loop we do some memory book keeping to see
8870 * how much free space there is in the target SV. (This sub assumes that
8871 * it is operating on the same SV most of the time via $_ and that it is
8872 * going to be able to reuse the same pv buffer each call.) If there is
8873 * "enough" room then we set "shortbuffered" to how much space there is
8874 * and start reading forward.
8876 * 2. When we scan forward we copy from the read-ahead buffer to the target
8877 * SV's pv buffer. While we go we watch for the end of the read-ahead buffer,
8878 * and the end of the of pv, as well as for the "rslast", which is the last
8879 * char of the separator.
8881 * 3. When scanning forward if we see rslast then we jump backwards in *pv*
8882 * (which has a "complete" record up to the point we saw rslast) and check
8883 * it to see if it matches the separator. If it does we are done. If it doesn't
8884 * we continue on with the scan/copy.
8886 * 4. If we run out of read-ahead buffer (cnt goes to 0) then we have to get
8887 * the IO system to read the next buffer. We do this by doing a getc(), which
8888 * returns a single char read (or EOF), and prefills the buffer, and also
8889 * allows us to find out how full the buffer is. We use this information to
8890 * SvGROW() the sv to the size remaining in the buffer, after which we copy
8891 * the returned single char into the target sv, and then go back into scan
8894 * 5. If we run out of write-buffer then we SvGROW() it by the size of the
8895 * remaining space in the read-buffer.
8897 * Note that this code despite its twisty-turny nature is pretty darn slick.
8898 * It manages single byte separators, multi-byte cross boundary separators,
8899 * and cross-read-buffer separators cleanly and efficiently at the cost
8900 * of potentially greatly overallocating the target SV.
8906 /* get the number of bytes remaining in the read-ahead buffer
8907 * on first call on a given fp this will return 0.*/
8908 cnt = PerlIO_get_cnt(fp);
8910 /* make sure we have the room */
8911 if ((I32)(SvLEN(sv) - append) <= cnt + 1) {
8912 /* Not room for all of it
8913 if we are looking for a separator and room for some
8915 if (rslen && cnt > 80 && (I32)SvLEN(sv) > append) {
8916 /* just process what we have room for */
8917 shortbuffered = cnt - SvLEN(sv) + append + 1;
8918 cnt -= shortbuffered;
8921 /* ensure that the target sv has enough room to hold
8922 * the rest of the read-ahead buffer */
8924 /* remember that cnt can be negative */
8925 SvGROW(sv, (STRLEN)(append + (cnt <= 0 ? 2 : (cnt + 1))));
8929 /* we have enough room to hold the full buffer, lets scream */
8933 /* extract the pointer to sv's string buffer, offset by append as necessary */
8934 bp = (STDCHAR*)SvPVX_const(sv) + append; /* move these two too to registers */
8935 /* extract the point to the read-ahead buffer */
8936 ptr = (STDCHAR*)PerlIO_get_ptr(fp);
8938 /* some trace debug output */
8939 DEBUG_P(PerlIO_printf(Perl_debug_log,
8940 "Screamer: entering, ptr=%" UVuf ", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
8941 DEBUG_P(PerlIO_printf(Perl_debug_log,
8942 "Screamer: entering: PerlIO * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%"
8944 PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
8945 PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
8949 /* if there is stuff left in the read-ahead buffer */
8951 /* if there is a separator */
8953 /* find next rslast */
8956 /* shortcut common case of blank line */
8958 if ((*bp++ = *ptr++) == rslast)
8959 goto thats_all_folks;
8961 p = (STDCHAR *)memchr(ptr, rslast, cnt);
8963 SSize_t got = p - ptr + 1;
8964 Copy(ptr, bp, got, STDCHAR);
8968 goto thats_all_folks;
8970 Copy(ptr, bp, cnt, STDCHAR);
8976 /* no separator, slurp the full buffer */
8977 Copy(ptr, bp, cnt, char); /* this | eat */
8978 bp += cnt; /* screams | dust */
8979 ptr += cnt; /* louder | sed :-) */
8981 assert (!shortbuffered);
8982 goto cannot_be_shortbuffered;
8986 if (shortbuffered) { /* oh well, must extend */
8987 /* we didn't have enough room to fit the line into the target buffer
8988 * so we must extend the target buffer and keep going */
8989 cnt = shortbuffered;
8991 bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */
8993 /* extned the target sv's buffer so it can hold the full read-ahead buffer */
8994 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
8995 bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */
8999 cannot_be_shortbuffered:
9000 /* we need to refill the read-ahead buffer if possible */
9002 DEBUG_P(PerlIO_printf(Perl_debug_log,
9003 "Screamer: going to getc, ptr=%" UVuf ", cnt=%" IVdf "\n",
9004 PTR2UV(ptr),(IV)cnt));
9005 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* deregisterize cnt and ptr */
9007 DEBUG_Pv(PerlIO_printf(Perl_debug_log,
9008 "Screamer: pre: FILE * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%" UVuf "\n",
9009 PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
9010 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
9013 call PerlIO_getc() to let it prefill the lookahead buffer
9015 This used to call 'filbuf' in stdio form, but as that behaves like
9016 getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
9017 another abstraction.
9019 Note we have to deal with the char in 'i' if we are not at EOF
9021 bpx = bp - (STDCHAR*)SvPVX_const(sv);
9022 /* signals might be called here, possibly modifying sv */
9023 i = PerlIO_getc(fp); /* get more characters */
9024 bp = (STDCHAR*)SvPVX_const(sv) + bpx;
9026 DEBUG_Pv(PerlIO_printf(Perl_debug_log,
9027 "Screamer: post: FILE * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%" UVuf "\n",
9028 PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
9029 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
9031 /* find out how much is left in the read-ahead buffer, and rextract its pointer */
9032 cnt = PerlIO_get_cnt(fp);
9033 ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */
9034 DEBUG_P(PerlIO_printf(Perl_debug_log,
9035 "Screamer: after getc, ptr=%" UVuf ", cnt=%" IVdf "\n",
9036 PTR2UV(ptr),(IV)cnt));
9038 if (i == EOF) /* all done for ever? */
9039 goto thats_really_all_folks;
9041 /* make sure we have enough space in the target sv */
9042 bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */
9044 SvGROW(sv, bpx + cnt + 2);
9045 bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */
9047 /* copy of the char we got from getc() */
9048 *bp++ = (STDCHAR)i; /* store character from PerlIO_getc */
9050 /* make sure we deal with the i being the last character of a separator */
9051 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
9052 goto thats_all_folks;
9056 /* check if we have actually found the separator - only really applies
9058 if ((rslen > 1 && (STRLEN)(bp - (STDCHAR*)SvPVX_const(sv)) < rslen) ||
9059 memNE((char*)bp - rslen, rsptr, rslen))
9060 goto screamer; /* go back to the fray */
9061 thats_really_all_folks:
9063 cnt += shortbuffered;
9064 DEBUG_P(PerlIO_printf(Perl_debug_log,
9065 "Screamer: quitting, ptr=%" UVuf ", cnt=%" IVdf "\n",PTR2UV(ptr),(IV)cnt));
9066 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* put these back or we're in trouble */
9067 DEBUG_P(PerlIO_printf(Perl_debug_log,
9068 "Screamer: end: FILE * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%" UVuf
9070 PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
9071 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
9073 SvCUR_set(sv, bp - (STDCHAR*)SvPVX_const(sv)); /* set length */
9074 DEBUG_P(PerlIO_printf(Perl_debug_log,
9075 "Screamer: done, len=%ld, string=|%.*s|\n",
9076 (long)SvCUR(sv),(int)SvCUR(sv),SvPVX_const(sv)));
9080 /*The big, slow, and stupid way. */
9085 const STDCHAR * const bpe = buf + sizeof(buf);
9087 while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = (STDCHAR)i) != rslast && bp < bpe)
9088 ; /* keep reading */
9092 cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
9093 /* Accommodate broken VAXC compiler, which applies U8 cast to
9094 * both args of ?: operator, causing EOF to change into 255
9097 i = (U8)buf[cnt - 1];
9103 cnt = 0; /* we do need to re-set the sv even when cnt <= 0 */
9105 sv_catpvn_nomg(sv, (char *) buf, cnt);
9107 sv_setpvn(sv, (char *) buf, cnt); /* "nomg" is implied */
9109 if (i != EOF && /* joy */
9111 SvCUR(sv) < rslen ||
9112 memNE(SvPVX_const(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
9116 * If we're reading from a TTY and we get a short read,
9117 * indicating that the user hit his EOF character, we need
9118 * to notice it now, because if we try to read from the TTY
9119 * again, the EOF condition will disappear.
9121 * The comparison of cnt to sizeof(buf) is an optimization
9122 * that prevents unnecessary calls to feof().
9126 if (!(cnt < (I32)sizeof(buf) && PerlIO_eof(fp)))
9132 if (rspara) { /* have to do this both before and after */
9133 while (i != EOF) { /* to make sure file boundaries work right */
9134 i = PerlIO_getc(fp);
9136 PerlIO_ungetc(fp,i);
9142 return (SvCUR(sv) - append) ? SvPVX(sv) : NULL;
9147 =for apidoc_item sv_inc_nomg
9149 These auto-increment the value in the SV, doing string to numeric conversion
9150 if necessary. They both handle operator overloading.
9152 They differ only in that C<sv_inc> performs 'get' magic; C<sv_inc_nomg> skips
9159 Perl_sv_inc(pTHX_ SV *const sv)
9168 Perl_sv_inc_nomg(pTHX_ SV *const sv)
9175 if (SvTHINKFIRST(sv)) {
9176 if (SvREADONLY(sv)) {
9177 Perl_croak_no_modify();
9181 if (SvAMAGIC(sv) && AMG_CALLunary(sv, inc_amg))
9183 i = PTR2IV(SvRV(sv));
9187 else sv_force_normal_flags(sv, 0);
9189 flags = SvFLAGS(sv);
9190 if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) {
9191 /* It's (privately or publicly) a float, but not tested as an
9192 integer, so test it to see. */
9194 flags = SvFLAGS(sv);
9196 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
9197 /* It's publicly an integer, or privately an integer-not-float */
9198 #ifdef PERL_PRESERVE_IVUV
9202 if (SvUVX(sv) == UV_MAX)
9203 sv_setnv(sv, UV_MAX_P1);
9205 (void)SvIOK_only_UV(sv);
9206 SvUV_set(sv, SvUVX(sv) + 1);
9209 if (SvIVX(sv) == IV_MAX)
9210 sv_setuv(sv, (UV)IV_MAX + 1);
9212 (void)SvIOK_only(sv);
9213 SvIV_set(sv, SvIVX(sv) + 1);
9218 if (flags & SVp_NOK) {
9219 const NV was = SvNVX(sv);
9220 if (NV_OVERFLOWS_INTEGERS_AT != 0.0 &&
9221 /* If NVX was NaN, the following comparisons return always false */
9222 UNLIKELY(was >= NV_OVERFLOWS_INTEGERS_AT ||
9223 was < -NV_OVERFLOWS_INTEGERS_AT) &&
9224 #if defined(NAN_COMPARE_BROKEN)
9225 LIKELY(!Perl_isinfnan(was))
9227 LIKELY(!Perl_isinf(was))
9230 /* diag_listed_as: Lost precision when %s %f by 1 */
9231 Perl_ck_warner(aTHX_ packWARN(WARN_IMPRECISION),
9232 "Lost precision when incrementing %" NVff " by 1",
9235 (void)SvNOK_only(sv);
9236 SvNV_set(sv, was + 1.0);
9240 /* treat AV/HV/CV/FM/IO and non-fake GVs as immutable */
9241 if (SvTYPE(sv) >= SVt_PVAV || (isGV_with_GP(sv) && !SvFAKE(sv)))
9242 Perl_croak_no_modify();
9244 if (!(flags & SVp_POK) || !*SvPVX_const(sv)) {
9245 if ((flags & SVTYPEMASK) < SVt_PVIV)
9246 sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV ? SVt_PVIV : SVt_IV));
9247 (void)SvIOK_only(sv);
9252 while (isALPHA(*d)) d++;
9253 while (isDIGIT(*d)) d++;
9254 if (d < SvEND(sv)) {
9255 const int numtype = grok_number_flags(SvPVX_const(sv), SvCUR(sv), NULL, PERL_SCAN_TRAILING);
9256 #ifdef PERL_PRESERVE_IVUV
9257 /* Got to punt this as an integer if needs be, but we don't issue
9258 warnings. Probably ought to make the sv_iv_please() that does
9259 the conversion if possible, and silently. */
9260 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
9261 /* Need to try really hard to see if it's an integer.
9262 9.22337203685478e+18 is an integer.
9263 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
9264 so $a="9.22337203685478e+18"; $a+0; $a++
9265 needs to be the same as $a="9.22337203685478e+18"; $a++
9272 /* sv_2iv *should* have made this an NV */
9273 if (flags & SVp_NOK) {
9274 (void)SvNOK_only(sv);
9275 SvNV_set(sv, SvNVX(sv) + 1.0);
9278 /* I don't think we can get here. Maybe I should assert this
9279 And if we do get here I suspect that sv_setnv will croak. NWC
9281 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%" UVxf " NV=%" NVgf "\n",
9282 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
9284 #endif /* PERL_PRESERVE_IVUV */
9285 if (!numtype && ckWARN(WARN_NUMERIC))
9286 not_incrementable(sv);
9287 sv_setnv(sv,Atof(SvPVX_const(sv)) + 1.0);
9291 while (d >= SvPVX_const(sv)) {
9299 /* MKS: The original code here died if letters weren't consecutive.
9300 * at least it didn't have to worry about non-C locales. The
9301 * new code assumes that ('z'-'a')==('Z'-'A'), letters are
9302 * arranged in order (although not consecutively) and that only
9303 * [A-Za-z] are accepted by isALPHA in the C locale.
9305 if (isALPHA_FOLD_NE(*d, 'z')) {
9306 do { ++*d; } while (!isALPHA(*d));
9309 *(d--) -= 'z' - 'a';
9314 *(d--) -= 'z' - 'a' + 1;
9318 /* oh,oh, the number grew */
9319 SvGROW(sv, SvCUR(sv) + 2);
9320 SvCUR_set(sv, SvCUR(sv) + 1);
9321 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX_const(sv); d--)
9331 =for apidoc_item sv_dec_nomg
9333 These auto-decrement the value in the SV, doing string to numeric conversion
9334 if necessary. They both handle operator overloading.
9336 They differ only in that:
9338 C<sv_dec> handles 'get' magic; C<sv_dec_nomg> skips 'get' magic.
9344 Perl_sv_dec(pTHX_ SV *const sv)
9353 Perl_sv_dec_nomg(pTHX_ SV *const sv)
9359 if (SvTHINKFIRST(sv)) {
9360 if (SvREADONLY(sv)) {
9361 Perl_croak_no_modify();
9365 if (SvAMAGIC(sv) && AMG_CALLunary(sv, dec_amg))
9367 i = PTR2IV(SvRV(sv));
9371 else sv_force_normal_flags(sv, 0);
9373 /* Unlike sv_inc we don't have to worry about string-never-numbers
9374 and keeping them magic. But we mustn't warn on punting */
9375 flags = SvFLAGS(sv);
9376 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
9377 /* It's publicly an integer, or privately an integer-not-float */
9378 #ifdef PERL_PRESERVE_IVUV
9382 if (SvUVX(sv) == 0) {
9383 (void)SvIOK_only(sv);
9387 (void)SvIOK_only_UV(sv);
9388 SvUV_set(sv, SvUVX(sv) - 1);
9391 if (SvIVX(sv) == IV_MIN) {
9392 sv_setnv(sv, (NV)IV_MIN);
9396 (void)SvIOK_only(sv);
9397 SvIV_set(sv, SvIVX(sv) - 1);
9402 if (flags & SVp_NOK) {
9405 const NV was = SvNVX(sv);
9406 if (NV_OVERFLOWS_INTEGERS_AT != 0.0 &&
9407 /* If NVX was NaN, these comparisons return always false */
9408 UNLIKELY(was <= -NV_OVERFLOWS_INTEGERS_AT ||
9409 was > NV_OVERFLOWS_INTEGERS_AT) &&
9410 #if defined(NAN_COMPARE_BROKEN)
9411 LIKELY(!Perl_isinfnan(was))
9413 LIKELY(!Perl_isinf(was))
9416 /* diag_listed_as: Lost precision when %s %f by 1 */
9417 Perl_ck_warner(aTHX_ packWARN(WARN_IMPRECISION),
9418 "Lost precision when decrementing %" NVff " by 1",
9421 (void)SvNOK_only(sv);
9422 SvNV_set(sv, was - 1.0);
9427 /* treat AV/HV/CV/FM/IO and non-fake GVs as immutable */
9428 if (SvTYPE(sv) >= SVt_PVAV || (isGV_with_GP(sv) && !SvFAKE(sv)))
9429 Perl_croak_no_modify();
9431 if (!(flags & SVp_POK)) {
9432 if ((flags & SVTYPEMASK) < SVt_PVIV)
9433 sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV) ? SVt_PVIV : SVt_IV);
9435 (void)SvIOK_only(sv);
9438 #ifdef PERL_PRESERVE_IVUV
9440 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), NULL);
9441 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
9442 /* Need to try really hard to see if it's an integer.
9443 9.22337203685478e+18 is an integer.
9444 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
9445 so $a="9.22337203685478e+18"; $a+0; $a--
9446 needs to be the same as $a="9.22337203685478e+18"; $a--
9453 /* sv_2iv *should* have made this an NV */
9454 if (flags & SVp_NOK) {
9455 (void)SvNOK_only(sv);
9456 SvNV_set(sv, SvNVX(sv) - 1.0);
9459 /* I don't think we can get here. Maybe I should assert this
9460 And if we do get here I suspect that sv_setnv will croak. NWC
9462 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%" UVxf " NV=%" NVgf "\n",
9463 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
9466 #endif /* PERL_PRESERVE_IVUV */
9467 sv_setnv(sv,Atof(SvPVX_const(sv)) - 1.0); /* punt */
9470 /* this define is used to eliminate a chunk of duplicated but shared logic
9471 * it has the suffix __SV_C to signal that it isnt API, and isnt meant to be
9472 * used anywhere but here - yves
9474 #define PUSH_EXTEND_MORTAL__SV_C(AnSv) \
9476 SSize_t ix = ++PL_tmps_ix; \
9477 if (UNLIKELY(ix >= PL_tmps_max)) \
9478 ix = tmps_grow_p(ix); \
9479 PL_tmps_stack[ix] = (AnSv); \
9483 =for apidoc sv_mortalcopy
9485 Creates a new SV which is a copy of the original SV (using C<sv_setsv>).
9486 The new SV is marked as mortal. It will be destroyed "soon", either by an
9487 explicit call to C<FREETMPS>, or by an implicit call at places such as
9488 statement boundaries. See also C<L</sv_newmortal>> and C<L</sv_2mortal>>.
9490 =for apidoc sv_mortalcopy_flags
9492 Like C<sv_mortalcopy>, but the extra C<flags> are passed to the
9498 /* Make a string that will exist for the duration of the expression
9499 * evaluation. Actually, it may have to last longer than that, but
9500 * hopefully we won't free it until it has been assigned to a
9501 * permanent location. */
9504 Perl_sv_mortalcopy_flags(pTHX_ SV *const oldstr, U32 flags)
9508 if (flags & SV_GMAGIC)
9509 SvGETMAGIC(oldstr); /* before new_SV, in case it dies */
9511 sv_setsv_flags(sv,oldstr,flags & ~SV_GMAGIC);
9512 PUSH_EXTEND_MORTAL__SV_C(sv);
9518 =for apidoc sv_newmortal
9520 Creates a new null SV which is mortal. The reference count of the SV is
9521 set to 1. It will be destroyed "soon", either by an explicit call to
9522 C<FREETMPS>, or by an implicit call at places such as statement boundaries.
9523 See also C<L</sv_mortalcopy>> and C<L</sv_2mortal>>.
9529 Perl_sv_newmortal(pTHX)
9534 SvFLAGS(sv) = SVs_TEMP;
9535 PUSH_EXTEND_MORTAL__SV_C(sv);
9541 =for apidoc newSVpvn_flags
9543 Creates a new SV and copies a string (which may contain C<NUL> (C<\0>)
9544 characters) into it. The reference count for the
9545 SV is set to 1. Note that if C<len> is zero, Perl will create a zero length
9546 string. You are responsible for ensuring that the source string is at least
9547 C<len> bytes long. If the C<s> argument is NULL the new SV will be undefined.
9548 Currently the only flag bits accepted are C<SVf_UTF8> and C<SVs_TEMP>.
9549 If C<SVs_TEMP> is set, then C<sv_2mortal()> is called on the result before
9550 returning. If C<SVf_UTF8> is set, C<s>
9551 is considered to be in UTF-8 and the
9552 C<SVf_UTF8> flag will be set on the new SV.
9553 C<newSVpvn_utf8()> is a convenience wrapper for this function, defined as
9555 #define newSVpvn_utf8(s, len, u) \
9556 newSVpvn_flags((s), (len), (u) ? SVf_UTF8 : 0)
9558 =for apidoc Amnh||SVs_TEMP
9564 Perl_newSVpvn_flags(pTHX_ const char *const s, const STRLEN len, const U32 flags)
9568 /* All the flags we don't support must be zero.
9569 And we're new code so I'm going to assert this from the start. */
9570 assert(!(flags & ~(SVf_UTF8|SVs_TEMP)));
9571 sv = newSV_type(SVt_PV);
9572 sv_setpvn_fresh(sv,s,len);
9574 /* This code used to do a sv_2mortal(), however we now unroll the call to
9575 * sv_2mortal() and do what it does ourselves here. Since we have asserted
9576 * that flags can only have the SVf_UTF8 and/or SVs_TEMP flags set above we
9577 * can use it to enable the sv flags directly (bypassing SvTEMP_on), which
9578 * in turn means we don't need to mask out the SVf_UTF8 flag below, which
9579 * means that we eliminate quite a few steps than it looks - Yves
9580 * (explaining patch by gfx) */
9582 SvFLAGS(sv) |= flags;
9584 if(flags & SVs_TEMP){
9585 PUSH_EXTEND_MORTAL__SV_C(sv);
9592 =for apidoc sv_2mortal
9594 Marks an existing SV as mortal. The SV will be destroyed "soon", either
9595 by an explicit call to C<FREETMPS>, or by an implicit call at places such as
9596 statement boundaries. C<SvTEMP()> is turned on which means that the SV's
9597 string buffer can be "stolen" if this SV is copied. See also
9598 C<L</sv_newmortal>> and C<L</sv_mortalcopy>>.
9604 Perl_sv_2mortal(pTHX_ SV *const sv)
9610 PUSH_EXTEND_MORTAL__SV_C(sv);
9618 Creates a new SV and copies a string (which may contain C<NUL> (C<\0>)
9619 characters) into it. The reference count for the
9620 SV is set to 1. If C<len> is zero, Perl will compute the length using
9621 C<strlen()>, (which means if you use this option, that C<s> can't have embedded
9622 C<NUL> characters and has to have a terminating C<NUL> byte).
9624 This function can cause reliability issues if you are likely to pass in
9625 empty strings that are not null terminated, because it will run
9626 strlen on the string and potentially run past valid memory.
9628 Using L</newSVpvn> is a safer alternative for non C<NUL> terminated strings.
9629 For string literals use L</newSVpvs> instead. This function will work fine for
9630 C<NUL> terminated strings, but if you want to avoid the if statement on whether
9631 to call C<strlen> use C<newSVpvn> instead (calling C<strlen> yourself).
9637 Perl_newSVpv(pTHX_ const char *const s, const STRLEN len)
9639 SV *sv = newSV_type(SVt_PV);
9640 sv_setpvn_fresh(sv, s, len || s == NULL ? len : strlen(s));
9645 =for apidoc newSVpvn
9647 Creates a new SV and copies a string into it, which may contain C<NUL> characters
9648 (C<\0>) and other binary data. The reference count for the SV is set to 1.
9649 Note that if C<len> is zero, Perl will create a zero length (Perl) string. You
9650 are responsible for ensuring that the source buffer is at least
9651 C<len> bytes long. If the C<buffer> argument is NULL the new SV will be
9658 Perl_newSVpvn(pTHX_ const char *const buffer, const STRLEN len)
9660 SV *sv = newSV_type(SVt_PV);
9661 sv_setpvn_fresh(sv,buffer,len);
9666 =for apidoc newSVhek_mortal
9668 Creates a new mortal SV from the hash key structure. It will generate
9669 scalars that point to the shared string table where possible. Returns
9670 a new (undefined) SV if C<hek> is NULL.
9672 This is more efficient than using sv_2mortal(newSVhek( ... ))
9678 Perl_newSVhek_mortal(pTHX_ const HEK *const hek)
9680 SV * const sv = newSVhek(hek);
9682 assert(!SvIMMORTAL(sv));
9684 PUSH_EXTEND_MORTAL__SV_C(sv);
9690 =for apidoc newSVhek
9692 Creates a new SV from the hash key structure. It will generate scalars that
9693 point to the shared string table where possible. Returns a new (undefined)
9694 SV if C<hek> is NULL.
9700 Perl_newSVhek(pTHX_ const HEK *const hek)
9709 if (HEK_LEN(hek) == HEf_SVKEY) {
9710 return newSVsv(*(SV**)HEK_KEY(hek));
9712 const int flags = HEK_FLAGS(hek);
9713 if (flags & HVhek_WASUTF8) {
9715 Andreas would like keys he put in as utf8 to come back as utf8
9717 STRLEN utf8_len = HEK_LEN(hek);
9718 SV * const sv = newSV_type(SVt_PV);
9719 char *as_utf8 = (char *)bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
9720 /* bytes_to_utf8() allocates a new string, which we can repurpose: */
9721 sv_usepvn_flags(sv, as_utf8, utf8_len, SV_HAS_TRAILING_NUL);
9724 } else if (flags & HVhek_NOTSHARED) {
9725 /* A hash that isn't using shared hash keys has to have
9726 the flag in every key so that we know not to try to call
9727 share_hek_hek on it. */
9729 SV * const sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
9734 /* This will be overwhelmingly the most common case. */
9736 /* Inline most of newSVpvn_share(), because share_hek_hek() is far
9737 more efficient than sharepvn(). */
9738 SV *sv = newSV_type(SVt_PV);
9740 SvPV_set(sv, (char *)HEK_KEY(share_hek_hek(hek)));
9741 SvCUR_set(sv, HEK_LEN(hek));
9753 =for apidoc newSVpvn_share
9755 Creates a new SV with its C<SvPVX_const> pointing to a shared string in the string
9756 table. If the string does not already exist in the table, it is
9757 created first. Turns on the C<SvIsCOW> flag (or C<READONLY>
9758 and C<FAKE> in 5.16 and earlier). If the C<hash> parameter
9759 is non-zero, that value is used; otherwise the hash is computed.
9760 The string's hash can later be retrieved from the SV
9761 with the C<L</SvSHARED_HASH>> macro. The idea here is
9762 that as the string table is used for shared hash keys these strings will have
9763 C<SvPVX_const == HeKEY> and hash lookup will avoid string compare.
9769 Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash)
9772 bool is_utf8 = FALSE;
9773 const char *const orig_src = src;
9776 STRLEN tmplen = -len;
9778 /* See the note in hv.c:hv_fetch() --jhi */
9779 src = (char*)bytes_from_utf8((const U8*)src, &tmplen, &is_utf8);
9783 PERL_HASH(hash, src, len);
9784 sv = newSV_type(SVt_PV);
9785 /* The logic for this is inlined in S_mro_get_linear_isa_dfs(), so if it
9786 changes here, update it there too. */
9787 SvPV_set(sv, sharepvn(src, is_utf8?-len:len, hash));
9794 if (src != orig_src)
9800 =for apidoc newSVpv_share
9802 Like C<newSVpvn_share>, but takes a C<NUL>-terminated string instead of a
9809 Perl_newSVpv_share(pTHX_ const char *src, U32 hash)
9811 return newSVpvn_share(src, strlen(src), hash);
9814 #if defined(MULTIPLICITY)
9816 /* pTHX_ magic can't cope with varargs, so this is a no-context
9817 * version of the main function, (which may itself be aliased to us).
9818 * Don't access this version directly.
9822 Perl_newSVpvf_nocontext(const char *const pat, ...)
9828 PERL_ARGS_ASSERT_NEWSVPVF_NOCONTEXT;
9830 va_start(args, pat);
9831 sv = vnewSVpvf(pat, &args);
9838 =for apidoc newSVpvf
9840 Creates a new SV and initializes it with the string formatted like
9843 =for apidoc newSVpvf_nocontext
9844 Like C<L</newSVpvf>> but does not take a thread context (C<aTHX>) parameter,
9845 so is used in situations where the caller doesn't already have the thread
9848 =for apidoc vnewSVpvf
9849 Like C<L</newSVpvf>> but the arguments are an encapsulated argument list.
9855 Perl_newSVpvf(pTHX_ const char *const pat, ...)
9860 PERL_ARGS_ASSERT_NEWSVPVF;
9862 va_start(args, pat);
9863 sv = vnewSVpvf(pat, &args);
9868 /* backend for newSVpvf() and newSVpvf_nocontext() */
9871 Perl_vnewSVpvf(pTHX_ const char *const pat, va_list *const args)
9875 PERL_ARGS_ASSERT_VNEWSVPVF;
9878 SvPVCLEAR_FRESH(sv);
9879 sv_vcatpvfn_flags(sv, pat, strlen(pat), args, NULL, 0, NULL, 0);
9886 Creates a new SV and copies a floating point value into it.
9887 The reference count for the SV is set to 1.
9893 Perl_newSVnv(pTHX_ const NV n)
9895 SV *sv = newSV_type(SVt_NV);
9907 Creates a new SV and copies an integer into it. The reference count for the
9914 Perl_newSViv(pTHX_ const IV i)
9916 SV *sv = newSV_type(SVt_IV);
9928 Creates a new SV and copies an unsigned integer into it.
9929 The reference count for the SV is set to 1.
9935 Perl_newSVuv(pTHX_ const UV u)
9939 /* Inlining ONLY the small relevant subset of sv_setuv here
9940 * for performance. Makes a significant difference. */
9942 /* Using ivs is more efficient than using uvs - see sv_setuv */
9943 if (u <= (UV)IV_MAX) {
9944 return newSViv((IV)u);
9949 /* We're starting from SVt_FIRST, so provided that's
9950 * actual 0, we don't have to unset any SV type flags
9951 * to promote to SVt_IV. */
9952 STATIC_ASSERT_STMT(SVt_FIRST == 0);
9954 SET_SVANY_FOR_BODYLESS_IV(sv);
9955 SvFLAGS(sv) |= SVt_IV;
9957 (void)SvIsUV_on(sv);
9966 =for apidoc newSVbool
9968 Creates a new SV boolean.
9974 Perl_newSVbool(pTHX_ bool bool_val)
9976 PERL_ARGS_ASSERT_NEWSVBOOL;
9977 SV *sv = newSVsv(bool_val ? &PL_sv_yes : &PL_sv_no);
9983 =for apidoc newSV_true
9985 Creates a new SV that is a boolean true.
9990 Perl_newSV_true(pTHX)
9992 PERL_ARGS_ASSERT_NEWSV_TRUE;
9993 SV *sv = newSVsv(&PL_sv_yes);
9999 =for apidoc newSV_false
10001 Creates a new SV that is a boolean false.
10007 Perl_newSV_false(pTHX)
10009 PERL_ARGS_ASSERT_NEWSV_FALSE;
10010 SV *sv = newSVsv(&PL_sv_no);
10015 /* newRV_inc is the official function name to use now.
10016 * newRV_inc is in fact #defined to newRV in sv.h
10020 Perl_newRV(pTHX_ SV *const sv)
10022 PERL_ARGS_ASSERT_NEWRV;
10024 return newRV_noinc(SvREFCNT_inc_simple_NN(sv));
10028 =for apidoc newSVsv
10029 =for apidoc_item newSVsv_flags
10030 =for apidoc_item newSVsv_nomg
10032 These create a new SV which is an exact duplicate of the original SV
10033 (using C<sv_setsv>.)
10035 They differ only in that C<newSVsv> performs 'get' magic; C<newSVsv_nomg> skips
10036 any magic; and C<newSVsv_flags> allows you to explicitly set a C<flags>
10043 Perl_newSVsv_flags(pTHX_ SV *const old, I32 flags)
10049 if (SvIS_FREED(old)) {
10050 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "semi-panic: attempt to dup freed string");
10053 /* Do this here, otherwise we leak the new SV if this croaks. */
10054 if (flags & SV_GMAGIC)
10057 sv_setsv_flags(sv, old, flags & ~SV_GMAGIC);
10062 =for apidoc sv_reset
10064 Underlying implementation for the C<reset> Perl function.
10065 Note that the perl-level function is vaguely deprecated.
10071 Perl_sv_reset(pTHX_ const char *s, HV *const stash)
10073 PERL_ARGS_ASSERT_SV_RESET;
10075 sv_resetpvn(*s ? s : NULL, strlen(s), stash);
10079 Perl_sv_resetpvn(pTHX_ const char *s, STRLEN len, HV * const stash)
10081 char todo[PERL_UCHAR_MAX+1];
10084 if (!stash || SvTYPE(stash) != SVt_PVHV)
10087 if (!s) { /* reset ?? searches */
10088 MAGIC * const mg = mg_find((const SV *)stash, PERL_MAGIC_symtab);
10089 if (mg && mg->mg_len) {
10090 const U32 count = mg->mg_len / sizeof(PMOP**);
10091 PMOP **pmp = (PMOP**) mg->mg_ptr;
10092 PMOP *const *const end = pmp + count;
10094 while (pmp < end) {
10095 #ifdef USE_ITHREADS
10096 SvREADONLY_off(PL_regex_pad[(*pmp)->op_pmoffset]);
10098 (*pmp)->op_pmflags &= ~PMf_USED;
10106 /* reset variables */
10108 if (!HvTOTALKEYS(stash))
10111 Zero(todo, 256, char);
10115 I32 i = (unsigned char)*s;
10119 max = (unsigned char)*s++;
10120 for ( ; i <= max; i++) {
10123 for (i = 0; i <= (I32) HvMAX(stash); i++) {
10125 for (entry = HvARRAY(stash)[i];
10127 entry = HeNEXT(entry))
10132 if (!todo[(U8)*HeKEY(entry)])
10134 gv = MUTABLE_GV(HeVAL(entry));
10138 if (sv && !SvREADONLY(sv)) {
10139 SV_CHECK_THINKFIRST_COW_DROP(sv);
10140 if (!isGV(sv)) SvOK_off(sv);
10143 av_clear(GvAV(gv));
10145 if (GvHV(gv) && !HvHasNAME(GvHV(gv))) {
10146 hv_clear(GvHV(gv));
10156 Using various gambits, try to get an IO from an SV: the IO slot if its a
10157 GV; or the recursive result if we're an RV; or the IO slot of the symbol
10158 named after the PV if we're a string.
10160 'Get' magic is ignored on the C<sv> passed in, but will be called on
10161 C<SvRV(sv)> if C<sv> is an RV.
10167 Perl_sv_2io(pTHX_ SV *const sv)
10172 PERL_ARGS_ASSERT_SV_2IO;
10174 switch (SvTYPE(sv)) {
10176 io = MUTABLE_IO(sv);
10180 if (isGV_with_GP(sv)) {
10181 gv = MUTABLE_GV(sv);
10184 Perl_croak(aTHX_ "Bad filehandle: %" HEKf,
10185 HEKfARG(GvNAME_HEK(gv)));
10191 Perl_croak(aTHX_ PL_no_usym, "filehandle");
10193 SvGETMAGIC(SvRV(sv));
10194 return sv_2io(SvRV(sv));
10196 gv = gv_fetchsv_nomg(sv, 0, SVt_PVIO);
10203 if (SvGMAGICAL(sv)) {
10204 newsv = sv_newmortal();
10205 sv_setsv_nomg(newsv, sv);
10207 Perl_croak(aTHX_ "Bad filehandle: %" SVf, SVfARG(newsv));
10217 Using various gambits, try to get a CV from an SV; in addition, try if
10218 possible to set C<*st> and C<*gvp> to the stash and GV associated with it.
10219 The flags in C<lref> are passed to C<gv_fetchsv>.
10225 Perl_sv_2cv(pTHX_ SV *sv, HV **const st, GV **const gvp, const I32 lref)
10230 PERL_ARGS_ASSERT_SV_2CV;
10237 switch (SvTYPE(sv)) {
10241 return MUTABLE_CV(sv);
10251 sv = amagic_deref_call(sv, to_cv_amg);
10254 if (SvTYPE(sv) == SVt_PVCV) {
10255 cv = MUTABLE_CV(sv);
10260 else if(SvGETMAGIC(sv), isGV_with_GP(sv))
10261 gv = MUTABLE_GV(sv);
10263 Perl_croak(aTHX_ "Not a subroutine reference");
10265 else if (isGV_with_GP(sv)) {
10266 gv = MUTABLE_GV(sv);
10269 gv = gv_fetchsv_nomg(sv, lref, SVt_PVCV);
10276 /* Some flags to gv_fetchsv mean don't really create the GV */
10277 if (!isGV_with_GP(gv)) {
10281 *st = GvESTASH(gv);
10282 if (lref & ~GV_ADDMG && !GvCVu(gv)) {
10283 /* XXX this is probably not what they think they're getting.
10284 * It has the same effect as "sub name;", i.e. just a forward
10293 =for apidoc sv_true
10295 Returns true if the SV has a true value by Perl's rules.
10296 Use the C<SvTRUE> macro instead, which may call C<sv_true()> or may
10297 instead use an in-line version.
10303 Perl_sv_true(pTHX_ SV *const sv)
10308 const XPV* const tXpv = (XPV*)SvANY(sv);
10310 (tXpv->xpv_cur > 1 ||
10311 (tXpv->xpv_cur && *sv->sv_u.svu_pv != '0')))
10318 return SvIVX(sv) != 0;
10321 return SvNVX(sv) != 0.0;
10323 return sv_2bool(sv);
10329 =for apidoc sv_pvn_force
10331 Get a sensible string out of the SV somehow.
10332 A private implementation of the C<SvPV_force> macro for compilers which
10333 can't cope with complex macro expressions. Always use the macro instead.
10335 =for apidoc sv_pvn_force_flags
10337 Get a sensible string out of the SV somehow.
10338 If C<flags> has the C<SV_GMAGIC> bit set, will C<L</mg_get>> on C<sv> if
10339 appropriate, else not. C<sv_pvn_force> and C<sv_pvn_force_nomg> are
10340 implemented in terms of this function.
10341 You normally want to use the various wrapper macros instead: see
10342 C<L</SvPV_force>> and C<L</SvPV_force_nomg>>.
10348 Perl_sv_pvn_force_flags(pTHX_ SV *const sv, STRLEN *const lp, const U32 flags)
10350 PERL_ARGS_ASSERT_SV_PVN_FORCE_FLAGS;
10352 if (flags & SV_GMAGIC) SvGETMAGIC(sv);
10353 if (SvTHINKFIRST(sv) && (!SvROK(sv) || SvREADONLY(sv)))
10354 sv_force_normal_flags(sv, 0);
10364 if (SvTYPE(sv) > SVt_PVLV
10365 || isGV_with_GP(sv))
10366 /* diag_listed_as: Can't coerce %s to %s in %s */
10367 Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0),
10369 s = sv_2pv_flags(sv, &len, flags &~ SV_GMAGIC);
10376 if (SvTYPE(sv) < SVt_PV ||
10377 s != SvPVX_const(sv)) { /* Almost, but not quite, sv_setpvn() */
10380 SvUPGRADE(sv, SVt_PV); /* Never FALSE */
10381 SvGROW(sv, len + 1);
10382 Move(s,SvPVX(sv),len,char);
10383 SvCUR_set(sv, len);
10384 SvPVX(sv)[len] = '\0';
10387 SvPOK_on(sv); /* validate pointer */
10389 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2pv(%s)\n",
10390 PTR2UV(sv),SvPVX_const(sv)));
10393 (void)SvPOK_only_UTF8(sv);
10394 return SvPVX_mutable(sv);
10398 =for apidoc sv_pvbyten_force
10400 The backend for the C<SvPVbytex_force> macro. Always use the macro
10401 instead. If the SV cannot be downgraded from UTF-8, this croaks.
10407 Perl_sv_pvbyten_force(pTHX_ SV *const sv, STRLEN *const lp)
10409 PERL_ARGS_ASSERT_SV_PVBYTEN_FORCE;
10411 sv_pvn_force(sv,lp);
10412 (void)sv_utf8_downgrade(sv,0);
10418 =for apidoc sv_pvutf8n_force
10420 The backend for the C<SvPVutf8x_force> macro. Always use the macro
10427 Perl_sv_pvutf8n_force(pTHX_ SV *const sv, STRLEN *const lp)
10429 PERL_ARGS_ASSERT_SV_PVUTF8N_FORCE;
10431 sv_pvn_force(sv,0);
10432 sv_utf8_upgrade_nomg(sv);
10438 =for apidoc sv_reftype
10440 Returns a string describing what the SV is a reference to.
10442 If ob is true and the SV is blessed, the string is the class name,
10443 otherwise it is the type of the SV, "SCALAR", "ARRAY" etc.
10449 Perl_sv_reftype(pTHX_ const SV *const sv, const int ob)
10451 PERL_ARGS_ASSERT_SV_REFTYPE;
10452 if (ob && SvOBJECT(sv)) {
10453 return SvPV_nolen_const(sv_ref(NULL, sv, ob));
10456 /* WARNING - There is code, for instance in mg.c, that assumes that
10457 * the only reason that sv_reftype(sv,0) would return a string starting
10458 * with 'L' or 'S' is that it is a LVALUE or a SCALAR.
10459 * Yes this a dodgy way to do type checking, but it saves practically reimplementing
10460 * this routine inside other subs, and it saves time.
10461 * Do not change this assumption without searching for "dodgy type check" in
10464 switch (SvTYPE(sv)) {
10479 case SVt_PVLV: return (char *) (SvROK(sv) ? "REF"
10480 /* tied lvalues should appear to be
10481 * scalars for backwards compatibility */
10482 : (isALPHA_FOLD_EQ(LvTYPE(sv), 't'))
10483 ? "SCALAR" : "LVALUE");
10484 case SVt_PVAV: return "ARRAY";
10485 case SVt_PVHV: return "HASH";
10486 case SVt_PVCV: return "CODE";
10487 case SVt_PVGV: return (char *) (isGV_with_GP(sv)
10488 ? "GLOB" : "SCALAR");
10489 case SVt_PVFM: return "FORMAT";
10490 case SVt_PVIO: return "IO";
10491 case SVt_INVLIST: return "INVLIST";
10492 case SVt_REGEXP: return "REGEXP";
10493 case SVt_PVOBJ: return "OBJECT";
10494 default: return "UNKNOWN";
10502 Returns a SV describing what the SV passed in is a reference to.
10504 dst can be a SV to be set to the description or NULL, in which case a
10505 mortal SV is returned.
10507 If ob is true and the SV is blessed, the description is the class
10508 name, otherwise it is the type of the SV, "SCALAR", "ARRAY" etc.
10514 Perl_sv_ref(pTHX_ SV *dst, const SV *const sv, const int ob)
10516 PERL_ARGS_ASSERT_SV_REF;
10519 dst = sv_newmortal();
10521 if (ob && SvOBJECT(sv)) {
10522 if (HvHasNAME(SvSTASH(sv)))
10523 sv_sethek(dst, HvNAME_HEK(SvSTASH(sv)));
10525 sv_setpvs(dst, "__ANON__");
10528 const char * reftype = sv_reftype(sv, 0);
10529 sv_setpv(dst, reftype);
10535 =for apidoc sv_isobject
10537 Returns a boolean indicating whether the SV is an RV pointing to a blessed
10538 object. If the SV is not an RV, or if the object is not blessed, then this
10545 Perl_sv_isobject(pTHX_ SV *sv)
10561 Returns a boolean indicating whether the SV is blessed into the specified
10564 This does not check for subtypes or method overloading. Use C<sv_isa_sv> to
10565 verify an inheritance relationship in the same way as the C<isa> operator by
10566 respecting any C<isa()> method overloading; or C<sv_derived_from_sv> to test
10567 directly on the actual object type.
10573 Perl_sv_isa(pTHX_ SV *sv, const char *const name)
10575 const char *hvname;
10577 PERL_ARGS_ASSERT_SV_ISA;
10587 hvname = HvNAME_get(SvSTASH(sv));
10591 return strEQ(hvname, name);
10595 =for apidoc newSVrv
10597 Creates a new SV for the existing RV, C<rv>, to point to. If C<rv> is not an
10598 RV then it will be upgraded to one. If C<classname> is non-null then the new
10599 SV will be blessed in the specified package. The new SV is returned and its
10600 reference count is 1. The reference count 1 is owned by C<rv>. See also
10601 newRV_inc() and newRV_noinc() for creating a new RV properly.
10607 Perl_newSVrv(pTHX_ SV *const rv, const char *const classname)
10611 PERL_ARGS_ASSERT_NEWSVRV;
10615 SV_CHECK_THINKFIRST_COW_DROP(rv);
10617 if (UNLIKELY( SvTYPE(rv) >= SVt_PVMG )) {
10618 const U32 refcnt = SvREFCNT(rv);
10622 SvREFCNT(rv) = refcnt;
10624 sv_upgrade(rv, SVt_IV);
10625 } else if (SvROK(rv)) {
10626 SvREFCNT_dec(SvRV(rv));
10628 prepare_SV_for_RV(rv);
10636 HV* const stash = gv_stashpv(classname, GV_ADD);
10637 (void)sv_bless(rv, stash);
10643 Perl_newSVavdefelem(pTHX_ AV *av, SSize_t ix, bool extendible)
10645 SV * const lv = newSV_type(SVt_PVLV);
10646 PERL_ARGS_ASSERT_NEWSVAVDEFELEM;
10648 sv_magic(lv, NULL, PERL_MAGIC_defelem, NULL, 0);
10649 LvTARG(lv) = SvREFCNT_inc_simple_NN(av);
10650 LvSTARGOFF(lv) = ix;
10651 LvTARGLEN(lv) = extendible ? 1 : (STRLEN)UV_MAX;
10656 =for apidoc sv_setref_pv
10658 Copies a pointer into a new SV, optionally blessing the SV. The C<rv>
10659 argument will be upgraded to an RV. That RV will be modified to point to
10660 the new SV. If the C<pv> argument is C<NULL>, then C<PL_sv_undef> will be placed
10661 into the SV. The C<classname> argument indicates the package for the
10662 blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
10663 will have a reference count of 1, and the RV will be returned.
10665 Do not use with other Perl types such as HV, AV, SV, CV, because those
10666 objects will become corrupted by the pointer copy process.
10668 Note that C<sv_setref_pvn> copies the string while this copies the pointer.
10674 Perl_sv_setref_pv(pTHX_ SV *const rv, const char *const classname, void *const pv)
10676 PERL_ARGS_ASSERT_SV_SETREF_PV;
10683 sv_setiv(newSVrv(rv,classname), PTR2IV(pv));
10688 =for apidoc sv_setref_iv
10690 Copies an integer into a new SV, optionally blessing the SV. The C<rv>
10691 argument will be upgraded to an RV. That RV will be modified to point to
10692 the new SV. The C<classname> argument indicates the package for the
10693 blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
10694 will have a reference count of 1, and the RV will be returned.
10700 Perl_sv_setref_iv(pTHX_ SV *const rv, const char *const classname, const IV iv)
10702 PERL_ARGS_ASSERT_SV_SETREF_IV;
10704 sv_setiv(newSVrv(rv,classname), iv);
10709 =for apidoc sv_setref_uv
10711 Copies an unsigned integer into a new SV, optionally blessing the SV. The C<rv>
10712 argument will be upgraded to an RV. That RV will be modified to point to
10713 the new SV. The C<classname> argument indicates the package for the
10714 blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
10715 will have a reference count of 1, and the RV will be returned.
10721 Perl_sv_setref_uv(pTHX_ SV *const rv, const char *const classname, const UV uv)
10723 PERL_ARGS_ASSERT_SV_SETREF_UV;
10725 sv_setuv(newSVrv(rv,classname), uv);
10730 =for apidoc sv_setref_nv
10732 Copies a double into a new SV, optionally blessing the SV. The C<rv>
10733 argument will be upgraded to an RV. That RV will be modified to point to
10734 the new SV. The C<classname> argument indicates the package for the
10735 blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
10736 will have a reference count of 1, and the RV will be returned.
10742 Perl_sv_setref_nv(pTHX_ SV *const rv, const char *const classname, const NV nv)
10744 PERL_ARGS_ASSERT_SV_SETREF_NV;
10746 sv_setnv(newSVrv(rv,classname), nv);
10751 =for apidoc sv_setref_pvn
10753 Copies a string into a new SV, optionally blessing the SV. The length of the
10754 string must be specified with C<n>. The C<rv> argument will be upgraded to
10755 an RV. That RV will be modified to point to the new SV. The C<classname>
10756 argument indicates the package for the blessing. Set C<classname> to
10757 C<NULL> to avoid the blessing. The new SV will have a reference count
10758 of 1, and the RV will be returned.
10760 Note that C<sv_setref_pv> copies the pointer while this copies the string.
10766 Perl_sv_setref_pvn(pTHX_ SV *const rv, const char *const classname,
10767 const char *const pv, const STRLEN n)
10769 PERL_ARGS_ASSERT_SV_SETREF_PVN;
10771 sv_setpvn(newSVrv(rv,classname), pv, n);
10776 =for apidoc sv_bless
10778 Blesses an SV into a specified package. The SV must be an RV. The package
10779 must be designated by its stash (see C<L</gv_stashpv>>). The reference count
10780 of the SV is unaffected.
10786 Perl_sv_bless(pTHX_ SV *const sv, HV *const stash)
10789 HV *oldstash = NULL;
10791 PERL_ARGS_ASSERT_SV_BLESS;
10795 Perl_croak(aTHX_ "Can't bless non-reference value");
10796 if (HvSTASH_IS_CLASS(stash))
10797 Perl_croak(aTHX_ "Attempt to bless into a class");
10800 if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY|SVf_PROTECT)) {
10801 if (SvREADONLY(tmpRef))
10802 Perl_croak_no_modify();
10803 if (SvTYPE(tmpRef) == SVt_PVOBJ)
10804 Perl_croak(aTHX_ "Can't bless an object reference");
10805 if (SvOBJECT(tmpRef)) {
10806 oldstash = SvSTASH(tmpRef);
10809 SvOBJECT_on(tmpRef);
10810 SvUPGRADE(tmpRef, SVt_PVMG);
10811 SvSTASH_set(tmpRef, MUTABLE_HV(SvREFCNT_inc_simple(stash)));
10812 SvREFCNT_dec(oldstash);
10814 if(SvSMAGICAL(tmpRef))
10815 if(mg_find(tmpRef, PERL_MAGIC_ext) || mg_find(tmpRef, PERL_MAGIC_uvar))
10823 /* Downgrades a PVGV to a PVMG. If it's actually a PVLV, we leave the type
10824 * as it is after unglobbing it.
10827 PERL_STATIC_INLINE void
10828 S_sv_unglob(pTHX_ SV *const sv, U32 flags)
10832 SV * const temp = flags & SV_COW_DROP_PV ? NULL : sv_newmortal();
10834 PERL_ARGS_ASSERT_SV_UNGLOB;
10836 assert(SvTYPE(sv) == SVt_PVGV || SvTYPE(sv) == SVt_PVLV);
10838 if (!(flags & SV_COW_DROP_PV))
10839 gv_efullname3(temp, MUTABLE_GV(sv), "*");
10841 SvREFCNT_inc_simple_void_NN(sv_2mortal(sv));
10843 if(GvCVu((const GV *)sv) && (stash = GvSTASH(MUTABLE_GV(sv)))
10844 && HvHasNAME(stash))
10845 mro_method_changed_in(stash);
10846 gp_free(MUTABLE_GV(sv));
10849 sv_del_backref(MUTABLE_SV(GvSTASH(sv)), sv);
10850 GvSTASH(sv) = NULL;
10853 if (GvNAME_HEK(sv)) {
10854 unshare_hek(GvNAME_HEK(sv));
10856 isGV_with_GP_off(sv);
10858 if(SvTYPE(sv) == SVt_PVGV) {
10859 /* need to keep SvANY(sv) in the right arena */
10860 xpvmg = new_XPVMG();
10861 StructCopy(SvANY(sv), xpvmg, XPVMG);
10862 del_body_by_type(SvANY(sv), SVt_PVGV);
10865 SvFLAGS(sv) &= ~SVTYPEMASK;
10866 SvFLAGS(sv) |= SVt_PVMG;
10869 /* Intentionally not calling any local SET magic, as this isn't so much a
10870 set operation as merely an internal storage change. */
10871 if (flags & SV_COW_DROP_PV) SvOK_off(sv);
10872 else sv_setsv_flags(sv, temp, 0);
10874 if ((const GV *)sv == PL_last_in_gv)
10875 PL_last_in_gv = NULL;
10876 else if ((const GV *)sv == PL_statgv)
10881 =for apidoc sv_unref_flags
10883 Unsets the RV status of the SV, and decrements the reference count of
10884 whatever was being referenced by the RV. This can almost be thought of
10885 as a reversal of C<newSVrv>. The C<cflags> argument can contain
10886 C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented
10887 (otherwise the decrementing is conditional on the reference count being
10888 different from one or the reference being a readonly SV).
10889 See C<L</SvROK_off>>.
10891 =for apidoc Amnh||SV_IMMEDIATE_UNREF
10897 Perl_sv_unref_flags(pTHX_ SV *const ref, const U32 flags)
10899 SV* const target = SvRV(ref);
10901 PERL_ARGS_ASSERT_SV_UNREF_FLAGS;
10903 if (SvWEAKREF(ref)) {
10904 sv_del_backref(target, ref);
10905 SvWEAKREF_off(ref);
10906 SvRV_set(ref, NULL);
10909 SvRV_set(ref, NULL);
10911 /* You can't have a || SvREADONLY(target) here, as $a = $$a, where $a was
10912 assigned to as BEGIN {$a = \"Foo"} will fail. */
10913 if (SvREFCNT(target) != 1 || (flags & SV_IMMEDIATE_UNREF))
10914 SvREFCNT_dec_NN(target);
10915 else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */
10916 sv_2mortal(target); /* Schedule for freeing later */
10920 =for apidoc sv_untaint
10922 Untaint an SV. Use C<SvTAINTED_off> instead.
10928 Perl_sv_untaint(pTHX_ SV *const sv)
10930 PERL_ARGS_ASSERT_SV_UNTAINT;
10931 PERL_UNUSED_CONTEXT;
10933 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
10934 MAGIC * const mg = mg_find(sv, PERL_MAGIC_taint);
10941 =for apidoc sv_tainted
10943 Test an SV for taintedness. Use C<SvTAINTED> instead.
10949 Perl_sv_tainted(pTHX_ SV *const sv)
10951 PERL_ARGS_ASSERT_SV_TAINTED;
10952 PERL_UNUSED_CONTEXT;
10954 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
10955 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_taint);
10956 if (mg && (mg->mg_len & 1) )
10962 #if defined(MULTIPLICITY)
10964 /* pTHX_ magic can't cope with varargs, so this is a no-context
10965 * version of the main function, (which may itself be aliased to us).
10966 * Don't access this version directly.
10970 Perl_sv_setpvf_nocontext(SV *const sv, const char *const pat, ...)
10975 PERL_ARGS_ASSERT_SV_SETPVF_NOCONTEXT;
10977 va_start(args, pat);
10978 sv_vsetpvf(sv, pat, &args);
10982 /* pTHX_ magic can't cope with varargs, so this is a no-context
10983 * version of the main function, (which may itself be aliased to us).
10984 * Don't access this version directly.
10988 Perl_sv_setpvf_mg_nocontext(SV *const sv, const char *const pat, ...)
10993 PERL_ARGS_ASSERT_SV_SETPVF_MG_NOCONTEXT;
10995 va_start(args, pat);
10996 sv_vsetpvf_mg(sv, pat, &args);
11002 =for apidoc sv_setpvf
11003 =for apidoc_item sv_setpvf_mg
11004 =for apidoc_item sv_setpvf_mg_nocontext
11005 =for apidoc_item sv_setpvf_nocontext
11007 These work like C<L</sv_catpvf>> but copy the text into the SV instead of
11010 The differences between these are:
11012 C<sv_setpvf_mg> and C<sv_setpvf_mg_nocontext> perform 'set' magic; C<sv_setpvf>
11013 and C<sv_setpvf_nocontext> skip all magic.
11015 C<sv_setpvf_nocontext> and C<sv_setpvf_mg_nocontext> do not take a thread
11016 context (C<aTHX>) parameter, so are used in situations where the caller
11017 doesn't already have the thread context.
11023 Perl_sv_setpvf(pTHX_ SV *const sv, const char *const pat, ...)
11027 PERL_ARGS_ASSERT_SV_SETPVF;
11029 va_start(args, pat);
11030 sv_vsetpvf(sv, pat, &args);
11035 =for apidoc sv_vsetpvf
11036 =for apidoc_item sv_vsetpvf_mg
11038 These work like C<L</sv_vcatpvf>> but copy the text into the SV instead of
11041 They differ only in that C<sv_vsetpvf_mg> performs 'set' magic;
11042 C<sv_vsetpvf> skips all magic.
11044 They are usually used via their frontends, C<L</sv_setpvf>> and
11045 C<L</sv_setpvf_mg>>.
11051 Perl_sv_vsetpvf(pTHX_ SV *const sv, const char *const pat, va_list *const args)
11053 PERL_ARGS_ASSERT_SV_VSETPVF;
11055 sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
11059 Perl_sv_setpvf_mg(pTHX_ SV *const sv, const char *const pat, ...)
11063 PERL_ARGS_ASSERT_SV_SETPVF_MG;
11065 va_start(args, pat);
11066 sv_vsetpvf_mg(sv, pat, &args);
11071 Perl_sv_vsetpvf_mg(pTHX_ SV *const sv, const char *const pat, va_list *const args)
11073 PERL_ARGS_ASSERT_SV_VSETPVF_MG;
11075 sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
11079 #if defined(MULTIPLICITY)
11081 /* pTHX_ magic can't cope with varargs, so this is a no-context
11082 * version of the main function, (which may itself be aliased to us).
11083 * Don't access this version directly.
11087 Perl_sv_catpvf_nocontext(SV *const sv, const char *const pat, ...)
11092 PERL_ARGS_ASSERT_SV_CATPVF_NOCONTEXT;
11094 va_start(args, pat);
11095 sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11099 /* pTHX_ magic can't cope with varargs, so this is a no-context
11100 * version of the main function, (which may itself be aliased to us).
11101 * Don't access this version directly.
11105 Perl_sv_catpvf_mg_nocontext(SV *const sv, const char *const pat, ...)
11110 PERL_ARGS_ASSERT_SV_CATPVF_MG_NOCONTEXT;
11112 va_start(args, pat);
11113 sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11120 =for apidoc sv_catpvf
11121 =for apidoc_item sv_catpvf_mg
11122 =for apidoc_item sv_catpvf_mg_nocontext
11123 =for apidoc_item sv_catpvf_nocontext
11125 These process their arguments like C<sprintf>, and append the formatted
11126 output to an SV. As with C<sv_vcatpvfn>, argument reordering is not supporte
11127 when called with a non-null C-style variable argument list.
11129 If the appended data contains "wide" characters
11130 (including, but not limited to, SVs with a UTF-8 PV formatted with C<%s>,
11131 and characters >255 formatted with C<%c>), the original SV might get
11134 If the original SV was UTF-8, the pattern should be
11135 valid UTF-8; if the original SV was bytes, the pattern should be too.
11137 All perform 'get' magic, but only C<sv_catpvf_mg> and C<sv_catpvf_mg_nocontext>
11138 perform 'set' magic.
11140 C<sv_catpvf_nocontext> and C<sv_catpvf_mg_nocontext> do not take a thread
11141 context (C<aTHX>) parameter, so are used in situations where the caller
11142 doesn't already have the thread context.
11148 Perl_sv_catpvf(pTHX_ SV *const sv, const char *const pat, ...)
11152 PERL_ARGS_ASSERT_SV_CATPVF;
11154 va_start(args, pat);
11155 sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11160 =for apidoc sv_vcatpvf
11161 =for apidoc_item sv_vcatpvf_mg
11163 These process their arguments like C<sv_vcatpvfn> called with a non-null
11164 C-style variable argument list, and append the formatted output to C<sv>.
11166 They differ only in that C<sv_vcatpvf_mg> performs 'set' magic;
11167 C<sv_vcatpvf> skips 'set' magic.
11169 Both perform 'get' magic.
11171 They are usually accessed via their frontends C<L</sv_catpvf>> and
11172 C<L</sv_catpvf_mg>>.
11178 Perl_sv_vcatpvf(pTHX_ SV *const sv, const char *const pat, va_list *const args)
11180 PERL_ARGS_ASSERT_SV_VCATPVF;
11182 sv_vcatpvfn_flags(sv, pat, strlen(pat), args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11186 Perl_sv_catpvf_mg(pTHX_ SV *const sv, const char *const pat, ...)
11190 PERL_ARGS_ASSERT_SV_CATPVF_MG;
11192 va_start(args, pat);
11193 sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11199 Perl_sv_vcatpvf_mg(pTHX_ SV *const sv, const char *const pat, va_list *const args)
11201 PERL_ARGS_ASSERT_SV_VCATPVF_MG;
11203 sv_vcatpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
11208 =for apidoc sv_vsetpvfn
11210 Works like C<sv_vcatpvfn> but copies the text into the SV instead of
11213 Usually used via one of its frontends L</C<sv_vsetpvf>> and
11214 L</C<sv_vsetpvf_mg>>.
11220 Perl_sv_vsetpvfn(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
11221 va_list *const args, SV **const svargs, const Size_t sv_count, bool *const maybe_tainted)
11223 PERL_ARGS_ASSERT_SV_VSETPVFN;
11226 sv_vcatpvfn_flags(sv, pat, patlen, args, svargs, sv_count, maybe_tainted, 0);
11230 /* simplified inline Perl_sv_catpvn_nomg() when you know the SV's SvPOK */
11232 PERL_STATIC_INLINE void
11233 S_sv_catpvn_simple(pTHX_ SV *const sv, const char* const buf, const STRLEN len)
11235 STRLEN const need = len + SvCUR(sv) + 1;
11238 /* can't wrap as both len and SvCUR() are allocated in
11239 * memory and together can't consume all the address space
11241 assert(need > len);
11246 Copy(buf, end, len, char);
11249 SvCUR_set(sv, need - 1);
11254 * Warn of missing argument to sprintf. The value used in place of such
11255 * arguments should be &PL_sv_no; an undefined value would yield
11256 * inappropriate "use of uninit" warnings [perl #71000].
11259 S_warn_vcatpvfn_missing_argument(pTHX) {
11260 if (ckWARN(WARN_MISSING)) {
11261 Perl_warner(aTHX_ packWARN(WARN_MISSING), "Missing argument in %s",
11262 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
11271 Perl_croak(aTHX_ "Integer overflow in format string for %s",
11272 (PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn"));
11276 /* Given an int i from the next arg (if args is true) or an sv from an arg
11277 * (if args is false), try to extract a STRLEN-ranged value from the arg,
11278 * with overflow checking.
11279 * Sets *neg to true if the value was negative (untouched otherwise.
11280 * Returns the absolute value.
11281 * As an extra margin of safety, it croaks if the returned value would
11282 * exceed the maximum value of a STRLEN / 4.
11286 S_sprintf_arg_num_val(pTHX_ va_list *const args, int i, SV *sv, bool *neg)
11300 if (UNLIKELY(SvIsUV(sv))) {
11301 UV uv = SvUV_nomg(sv);
11303 S_croak_overflow();
11307 iv = SvIV_nomg(sv);
11311 S_croak_overflow();
11317 if (iv > (IV)(((STRLEN)~0) / 4))
11318 S_croak_overflow();
11323 /* Read in and return a number. Updates *pattern to point to the char
11324 * following the number. Expects the first char to 1..9.
11325 * Croaks if the number exceeds 1/4 of the maximum value of STRLEN.
11326 * This is a belt-and-braces safety measure to complement any
11327 * overflow/wrap checks done in the main body of sv_vcatpvfn_flags.
11328 * It means that e.g. on a 32-bit system the width/precision can't be more
11329 * than 1G, which seems reasonable.
11333 S_expect_number(pTHX_ const char **const pattern)
11337 PERL_ARGS_ASSERT_EXPECT_NUMBER;
11339 assert(inRANGE(**pattern, '1', '9'));
11341 var = *(*pattern)++ - '0';
11342 while (isDIGIT(**pattern)) {
11343 /* if var * 10 + 9 would exceed 1/4 max strlen, croak */
11344 if (var > ((((STRLEN)~0) / 4 - 9) / 10))
11345 S_croak_overflow();
11346 var = var * 10 + (*(*pattern)++ - '0');
11351 /* Implement a fast "%.0f": given a pointer to the end of a buffer (caller
11352 * ensures it's big enough), back fill it with the rounded integer part of
11353 * nv. Returns ptr to start of string, and sets *len to its length.
11354 * Returns NULL if not convertible.
11358 S_F0convert(NV nv, char *const endbuf, STRLEN *const len)
11360 const int neg = nv < 0;
11363 PERL_ARGS_ASSERT_F0CONVERT;
11365 assert(!Perl_isinfnan(nv));
11368 if (nv != 0.0 && nv < (NV) UV_MAX) {
11374 if (uv & 1 && uv == nv)
11375 uv--; /* Round to even */
11378 const unsigned dig = uv % 10;
11380 } while (uv /= 10);
11390 /* XXX maybe_tainted is never assigned to, so the doc above is lying. */
11393 Perl_sv_vcatpvfn(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
11394 va_list *const args, SV **const svargs, const Size_t sv_count, bool *const maybe_tainted)
11396 PERL_ARGS_ASSERT_SV_VCATPVFN;
11398 sv_vcatpvfn_flags(sv, pat, patlen, args, svargs, sv_count, maybe_tainted, SV_GMAGIC|SV_SMAGIC);
11402 /* For the vcatpvfn code, we need a long double target in case
11403 * HAS_LONG_DOUBLE, even without USE_LONG_DOUBLE, so that we can printf
11404 * with long double formats, even without NV being long double. But we
11405 * call the target 'fv' instead of 'nv', since most of the time it is not
11406 * (most compilers these days recognize "long double", even if only as a
11407 * synonym for "double").
11409 #if defined(HAS_LONG_DOUBLE) && LONG_DOUBLESIZE > DOUBLESIZE && \
11410 defined(PERL_PRIgldbl) && !defined(USE_QUADMATH)
11411 # define VCATPVFN_FV_GF PERL_PRIgldbl
11412 # if defined(__VMS) && defined(__ia64) && defined(__IEEE_FLOAT)
11413 /* Work around breakage in OTS$CVT_FLOAT_T_X */
11414 # define VCATPVFN_NV_TO_FV(nv,fv) \
11417 fv = Perl_isnan(_dv) ? LDBL_QNAN : _dv; \
11420 # define VCATPVFN_NV_TO_FV(nv,fv) (fv)=(nv)
11422 typedef long double vcatpvfn_long_double_t;
11424 # define VCATPVFN_FV_GF NVgf
11425 # define VCATPVFN_NV_TO_FV(nv,fv) (fv)=(nv)
11426 typedef NV vcatpvfn_long_double_t;
11429 #ifdef LONGDOUBLE_DOUBLEDOUBLE
11430 /* The first double can be as large as 2**1023, or '1' x '0' x 1023.
11431 * The second double can be as small as 2**-1074, or '0' x 1073 . '1'.
11432 * The sum of them can be '1' . '0' x 2096 . '1', with implied radix point
11433 * after the first 1023 zero bits.
11435 * XXX The 2098 is quite large (262.25 bytes) and therefore some sort
11436 * of dynamically growing buffer might be better, start at just 16 bytes
11437 * (for example) and grow only when necessary. Or maybe just by looking
11438 * at the exponents of the two doubles? */
11439 # define DOUBLEDOUBLE_MAXBITS 2098
11442 /* vhex will contain the values (0..15) of the hex digits ("nybbles"
11443 * of 4 bits); 1 for the implicit 1, and the mantissa bits, four bits
11444 * per xdigit. For the double-double case, this can be rather many.
11445 * The non-double-double-long-double overshoots since all bits of NV
11446 * are not mantissa bits, there are also exponent bits. */
11447 #ifdef LONGDOUBLE_DOUBLEDOUBLE
11448 # define VHEX_SIZE (3+DOUBLEDOUBLE_MAXBITS/4)
11450 # define VHEX_SIZE (1+(NVSIZE * 8)/4)
11453 /* If we do not have a known long double format, (including not using
11454 * long doubles, or long doubles being equal to doubles) then we will
11455 * fall back to the ldexp/frexp route, with which we can retrieve at
11456 * most as many bits as our widest unsigned integer type is. We try
11457 * to get a 64-bit unsigned integer even if we are not using a 64-bit UV.
11459 * (If you want to test the case of UVSIZE == 4, NVSIZE == 8,
11460 * set the MANTISSATYPE to int and the MANTISSASIZE to 4.)
11462 #if defined(HAS_QUAD) && defined(Uquad_t)
11463 # define MANTISSATYPE Uquad_t
11464 # define MANTISSASIZE 8
11466 # define MANTISSATYPE UV
11467 # define MANTISSASIZE UVSIZE
11470 #if defined(DOUBLE_LITTLE_ENDIAN) || defined(LONGDOUBLE_LITTLE_ENDIAN)
11471 # define HEXTRACT_LITTLE_ENDIAN
11472 #elif defined(DOUBLE_BIG_ENDIAN) || defined(LONGDOUBLE_BIG_ENDIAN)
11473 # define HEXTRACT_BIG_ENDIAN
11475 # define HEXTRACT_MIX_ENDIAN
11478 /* S_hextract() is a helper for S_format_hexfp, for extracting
11479 * the hexadecimal values (for %a/%A). The nv is the NV where the value
11480 * are being extracted from (either directly from the long double in-memory
11481 * presentation, or from the uquad computed via frexp+ldexp). frexp also
11482 * is used to update the exponent. The subnormal is set to true
11483 * for IEEE 754 subnormals/denormals (including the x86 80-bit format).
11484 * The vhex is the pointer to the beginning of the output buffer of VHEX_SIZE.
11486 * The tricky part is that S_hextract() needs to be called twice:
11487 * the first time with vend as NULL, and the second time with vend as
11488 * the pointer returned by the first call. What happens is that on
11489 * the first round the output size is computed, and the intended
11490 * extraction sanity checked. On the second round the actual output
11491 * (the extraction of the hexadecimal values) takes place.
11492 * Sanity failures cause fatal failures during both rounds. */
11494 S_hextract(pTHX_ const NV nv, int* exponent, bool *subnormal,
11495 U8* vhex, U8* vend)
11499 int ixmin = 0, ixmax = 0;
11501 /* XXX Inf/NaN are not handled here, since it is
11502 * assumed they are to be output as "Inf" and "NaN". */
11504 /* These macros are just to reduce typos, they have multiple
11505 * repetitions below, but usually only one (or sometimes two)
11506 * of them is really being used. */
11507 /* HEXTRACT_OUTPUT() extracts the high nybble first. */
11508 #define HEXTRACT_OUTPUT_HI(ix) (*v++ = nvp[ix] >> 4)
11509 #define HEXTRACT_OUTPUT_LO(ix) (*v++ = nvp[ix] & 0xF)
11510 #define HEXTRACT_OUTPUT(ix) \
11512 HEXTRACT_OUTPUT_HI(ix); HEXTRACT_OUTPUT_LO(ix); \
11514 #define HEXTRACT_COUNT(ix, c) \
11516 v += c; if (ix < ixmin) ixmin = ix; else if (ix > ixmax) ixmax = ix; \
11518 #define HEXTRACT_BYTE(ix) \
11520 if (vend) HEXTRACT_OUTPUT(ix); else HEXTRACT_COUNT(ix, 2); \
11522 #define HEXTRACT_LO_NYBBLE(ix) \
11524 if (vend) HEXTRACT_OUTPUT_LO(ix); else HEXTRACT_COUNT(ix, 1); \
11526 /* HEXTRACT_TOP_NYBBLE is just convenience disguise,
11527 * to make it look less odd when the top bits of a NV
11528 * are extracted using HEXTRACT_LO_NYBBLE: the highest
11529 * order bits can be in the "low nybble" of a byte. */
11530 #define HEXTRACT_TOP_NYBBLE(ix) HEXTRACT_LO_NYBBLE(ix)
11531 #define HEXTRACT_BYTES_LE(a, b) \
11532 for (ix = a; ix >= b; ix--) { HEXTRACT_BYTE(ix); }
11533 #define HEXTRACT_BYTES_BE(a, b) \
11534 for (ix = a; ix <= b; ix++) { HEXTRACT_BYTE(ix); }
11535 #define HEXTRACT_GET_SUBNORMAL(nv) *subnormal = Perl_fp_class_denorm(nv)
11536 #define HEXTRACT_IMPLICIT_BIT(nv) \
11538 if (!*subnormal) { \
11539 if (vend) *v++ = ((nv) == 0.0) ? 0 : 1; else v++; \
11543 /* Most formats do. Those which don't should undef this.
11545 * But also note that IEEE 754 subnormals do not have it, or,
11546 * expressed alternatively, their implicit bit is zero. */
11547 #define HEXTRACT_HAS_IMPLICIT_BIT
11549 /* Many formats do. Those which don't should undef this. */
11550 #define HEXTRACT_HAS_TOP_NYBBLE
11552 /* HEXTRACTSIZE is the maximum number of xdigits. */
11553 #if defined(USE_LONG_DOUBLE) && defined(LONGDOUBLE_DOUBLEDOUBLE)
11554 # define HEXTRACTSIZE (2+DOUBLEDOUBLE_MAXBITS/4)
11556 # define HEXTRACTSIZE 2 * NVSIZE
11559 const U8* vmaxend = vhex + HEXTRACTSIZE;
11561 assert(HEXTRACTSIZE <= VHEX_SIZE);
11563 PERL_UNUSED_VAR(ix); /* might happen */
11564 (void)Perl_frexp(PERL_ABS(nv), exponent);
11565 *subnormal = FALSE;
11566 if (vend && (vend <= vhex || vend > vmaxend)) {
11567 /* diag_listed_as: Hexadecimal float: internal error (%s) */
11568 Perl_croak(aTHX_ "Hexadecimal float: internal error (entry)");
11571 /* First check if using long doubles. */
11572 #if defined(USE_LONG_DOUBLE) && (NVSIZE > DOUBLESIZE)
11573 # if LONG_DOUBLEKIND == LONG_DOUBLE_IS_IEEE_754_128_BIT_LITTLE_ENDIAN
11574 /* Used in e.g. VMS and HP-UX IA-64, e.g. -0.1L:
11575 * 9a 99 99 99 99 99 99 99 99 99 99 99 99 99 fb bf */
11576 /* The bytes 13..0 are the mantissa/fraction,
11577 * the 15,14 are the sign+exponent. */
11578 const U8* nvp = (const U8*)(&nv);
11579 HEXTRACT_GET_SUBNORMAL(nv);
11580 HEXTRACT_IMPLICIT_BIT(nv);
11581 # undef HEXTRACT_HAS_TOP_NYBBLE
11582 HEXTRACT_BYTES_LE(13, 0);
11583 # elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_IEEE_754_128_BIT_BIG_ENDIAN
11584 /* Used in e.g. Solaris Sparc and HP-UX PA-RISC, e.g. -0.1L:
11585 * bf fb 99 99 99 99 99 99 99 99 99 99 99 99 99 9a */
11586 /* The bytes 2..15 are the mantissa/fraction,
11587 * the 0,1 are the sign+exponent. */
11588 const U8* nvp = (const U8*)(&nv);
11589 HEXTRACT_GET_SUBNORMAL(nv);
11590 HEXTRACT_IMPLICIT_BIT(nv);
11591 # undef HEXTRACT_HAS_TOP_NYBBLE
11592 HEXTRACT_BYTES_BE(2, 15);
11593 # elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_LITTLE_ENDIAN
11594 /* x86 80-bit "extended precision", 64 bits of mantissa / fraction /
11595 * significand, 15 bits of exponent, 1 bit of sign. No implicit bit.
11596 * NVSIZE can be either 12 (ILP32, Solaris x86) or 16 (LP64, Linux
11597 * and OS X), meaning that 2 or 6 bytes are empty padding. */
11598 /* The bytes 0..1 are the sign+exponent,
11599 * the bytes 2..9 are the mantissa/fraction. */
11600 const U8* nvp = (const U8*)(&nv);
11601 # undef HEXTRACT_HAS_IMPLICIT_BIT
11602 # undef HEXTRACT_HAS_TOP_NYBBLE
11603 HEXTRACT_GET_SUBNORMAL(nv);
11604 HEXTRACT_BYTES_LE(7, 0);
11605 # elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_BIG_ENDIAN
11606 /* Does this format ever happen? (Wikipedia says the Motorola
11607 * 6888x math coprocessors used format _like_ this but padded
11608 * to 96 bits with 16 unused bits between the exponent and the
11610 const U8* nvp = (const U8*)(&nv);
11611 # undef HEXTRACT_HAS_IMPLICIT_BIT
11612 # undef HEXTRACT_HAS_TOP_NYBBLE
11613 HEXTRACT_GET_SUBNORMAL(nv);
11614 HEXTRACT_BYTES_BE(0, 7);
11616 # define HEXTRACT_FALLBACK
11617 /* Double-double format: two doubles next to each other.
11618 * The first double is the high-order one, exactly like
11619 * it would be for a "lone" double. The second double
11620 * is shifted down using the exponent so that that there
11621 * are no common bits. The tricky part is that the value
11622 * of the double-double is the SUM of the two doubles and
11623 * the second one can be also NEGATIVE.
11625 * Because of this tricky construction the bytewise extraction we
11626 * use for the other long double formats doesn't work, we must
11627 * extract the values bit by bit.
11629 * The little-endian double-double is used .. somewhere?
11631 * The big endian double-double is used in e.g. PPC/Power (AIX)
11634 * The mantissa bits are in two separate stretches, e.g. for -0.1L:
11635 * 9a 99 99 99 99 99 59 bc 9a 99 99 99 99 99 b9 3f (LE)
11636 * 3f b9 99 99 99 99 99 9a bc 59 99 99 99 99 99 9a (BE)
11639 #else /* #if defined(USE_LONG_DOUBLE) && (NVSIZE > DOUBLESIZE) */
11640 /* Using normal doubles, not long doubles.
11642 * We generate 4-bit xdigits (nybble/nibble) instead of 8-bit
11643 * bytes, since we might need to handle printf precision, and
11644 * also need to insert the radix. */
11646 # ifdef HEXTRACT_LITTLE_ENDIAN
11647 /* 0 1 2 3 4 5 6 7 (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(6);
11652 HEXTRACT_BYTES_LE(5, 0);
11653 # elif defined(HEXTRACT_BIG_ENDIAN)
11654 /* 7 6 5 4 3 2 1 0 (MSB = 7, LSB = 0, 6+7 = exponent+sign) */
11655 const U8* nvp = (const U8*)(&nv);
11656 HEXTRACT_GET_SUBNORMAL(nv);
11657 HEXTRACT_IMPLICIT_BIT(nv);
11658 HEXTRACT_TOP_NYBBLE(1);
11659 HEXTRACT_BYTES_BE(2, 7);
11660 # elif DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_MIXED_ENDIAN_LE_BE
11661 /* 4 5 6 7 0 1 2 3 (MSB = 7, LSB = 0, 6:7 = nybble:exponent:sign) */
11662 const U8* nvp = (const U8*)(&nv);
11663 HEXTRACT_GET_SUBNORMAL(nv);
11664 HEXTRACT_IMPLICIT_BIT(nv);
11665 HEXTRACT_TOP_NYBBLE(2); /* 6 */
11666 HEXTRACT_BYTE(1); /* 5 */
11667 HEXTRACT_BYTE(0); /* 4 */
11668 HEXTRACT_BYTE(7); /* 3 */
11669 HEXTRACT_BYTE(6); /* 2 */
11670 HEXTRACT_BYTE(5); /* 1 */
11671 HEXTRACT_BYTE(4); /* 0 */
11672 # elif DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_MIXED_ENDIAN_BE_LE
11673 /* 3 2 1 0 7 6 5 4 (MSB = 7, LSB = 0, 7:6 = sign:exponent:nybble) */
11674 const U8* nvp = (const U8*)(&nv);
11675 HEXTRACT_GET_SUBNORMAL(nv);
11676 HEXTRACT_IMPLICIT_BIT(nv);
11677 HEXTRACT_TOP_NYBBLE(5); /* 6 */
11678 HEXTRACT_BYTE(6); /* 5 */
11679 HEXTRACT_BYTE(7); /* 4 */
11680 HEXTRACT_BYTE(0); /* 3 */
11681 HEXTRACT_BYTE(1); /* 2 */
11682 HEXTRACT_BYTE(2); /* 1 */
11683 HEXTRACT_BYTE(3); /* 0 */
11685 # define HEXTRACT_FALLBACK
11688 # define HEXTRACT_FALLBACK
11690 #endif /* #if defined(USE_LONG_DOUBLE) && (NVSIZE > DOUBLESIZE) #else */
11692 #ifdef HEXTRACT_FALLBACK
11693 HEXTRACT_GET_SUBNORMAL(nv);
11694 # undef HEXTRACT_HAS_TOP_NYBBLE /* Meaningless, but consistent. */
11695 /* The fallback is used for the double-double format, and
11696 * for unknown long double formats, and for unknown double
11697 * formats, or in general unknown NV formats. */
11698 if (nv == (NV)0.0) {
11706 NV d = nv < 0 ? -nv : nv;
11708 U8 ha = 0x0; /* hexvalue accumulator */
11709 U8 hd = 0x8; /* hexvalue digit */
11711 /* Shift d and e (and update exponent) so that e <= d < 2*e,
11712 * this is essentially manual frexp(). Multiplying by 0.5 and
11713 * doubling should be lossless in binary floating point. */
11723 while (d >= e + e) {
11727 /* Now e <= d < 2*e */
11729 /* First extract the leading hexdigit (the implicit bit). */
11745 /* Then extract the remaining hexdigits. */
11746 while (d > (NV)0.0) {
11752 /* Output or count in groups of four bits,
11753 * that is, when the hexdigit is down to one. */
11758 /* Reset the hexvalue. */
11767 /* Flush possible pending hexvalue. */
11777 /* Croak for various reasons: if the output pointer escaped the
11778 * output buffer, if the extraction index escaped the extraction
11779 * buffer, or if the ending output pointer didn't match the
11780 * previously computed value. */
11781 if (v <= vhex || v - vhex >= VHEX_SIZE ||
11782 /* For double-double the ixmin and ixmax stay at zero,
11783 * which is convenient since the HEXTRACTSIZE is tricky
11784 * for double-double. */
11785 ixmin < 0 || ixmax >= NVSIZE ||
11786 (vend && v != vend)) {
11787 /* diag_listed_as: Hexadecimal float: internal error (%s) */
11788 Perl_croak(aTHX_ "Hexadecimal float: internal error (overflow)");
11794 /* S_format_hexfp(): helper function for Perl_sv_vcatpvfn_flags().
11796 * Processes the %a/%A hexadecimal floating-point format, since the
11797 * built-in snprintf()s which are used for most of the f/p formats, don't
11798 * universally handle %a/%A.
11799 * Populates buf of length bufsize, and returns the length of the created
11801 * The rest of the args have the same meaning as the local vars of the
11802 * same name within Perl_sv_vcatpvfn_flags().
11804 * The caller's determination of IN_LC(LC_NUMERIC), passed as in_lc_numeric,
11805 * is used to ensure we do the right thing when we need to access the locale's
11808 * It requires the caller to make buf large enough.
11812 S_format_hexfp(pTHX_ char * const buf, const STRLEN bufsize, const char c,
11813 const NV nv, const vcatpvfn_long_double_t fv,
11814 bool has_precis, STRLEN precis, STRLEN width,
11815 bool alt, char plus, bool left, bool fill, bool in_lc_numeric)
11817 /* Hexadecimal floating point. */
11819 U8 vhex[VHEX_SIZE];
11820 U8* v = vhex; /* working pointer to vhex */
11821 U8* vend; /* pointer to one beyond last digit of vhex */
11822 U8* vfnz = NULL; /* first non-zero */
11823 U8* vlnz = NULL; /* last non-zero */
11824 U8* v0 = NULL; /* first output */
11825 const bool lower = (c == 'a');
11826 /* At output the values of vhex (up to vend) will
11827 * be mapped through the xdig to get the actual
11828 * human-readable xdigits. */
11829 const char* xdig = PL_hexdigit;
11830 STRLEN zerotail = 0; /* how many extra zeros to append */
11831 int exponent = 0; /* exponent of the floating point input */
11832 bool hexradix = FALSE; /* should we output the radix */
11833 bool subnormal = FALSE; /* IEEE 754 subnormal/denormal */
11834 bool negative = FALSE;
11837 /* XXX: NaN, Inf -- though they are printed as "NaN" and "Inf".
11839 * For example with denormals, (assuming the vanilla
11840 * 64-bit double): the exponent is zero. 1xp-1074 is
11841 * the smallest denormal and the smallest double, it
11842 * could be output also as 0x0.0000000000001p-1022 to
11843 * match its internal structure. */
11845 vend = S_hextract(aTHX_ nv, &exponent, &subnormal, vhex, NULL);
11846 S_hextract(aTHX_ nv, &exponent, &subnormal, vhex, vend);
11848 #if NVSIZE > DOUBLESIZE
11849 # ifdef HEXTRACT_HAS_IMPLICIT_BIT
11850 /* In this case there is an implicit bit,
11851 * and therefore the exponent is shifted by one. */
11853 # elif defined(NV_X86_80_BIT)
11855 /* The subnormals of the x86-80 have a base exponent of -16382,
11856 * (while the physical exponent bits are zero) but the frexp()
11857 * returned the scientific-style floating exponent. We want
11858 * to map the last one as:
11859 * -16831..-16384 -> -16382 (the last normal is 0x1p-16382)
11860 * -16835..-16388 -> -16384
11861 * since we want to keep the first hexdigit
11862 * as one of the [8421]. */
11863 exponent = -4 * ( (exponent + 1) / -4) - 2;
11867 /* TBD: other non-implicit-bit platforms than the x86-80. */
11871 negative = fv < 0 || Perl_signbit(nv);
11882 xdig += 16; /* Use uppercase hex. */
11885 /* Find the first non-zero xdigit. */
11886 for (v = vhex; v < vend; v++) {
11894 /* Find the last non-zero xdigit. */
11895 for (v = vend - 1; v >= vhex; v--) {
11902 #if NVSIZE == DOUBLESIZE
11908 #ifndef NV_X86_80_BIT
11910 /* IEEE 754 subnormals (but not the x86 80-bit):
11911 * we want "normalize" the subnormal,
11912 * so we need to right shift the hex nybbles
11913 * so that the output of the subnormal starts
11914 * from the first true bit. (Another, equally
11915 * valid, policy would be to dump the subnormal
11916 * nybbles as-is, to display the "physical" layout.) */
11919 /* Find the ceil(log2(v[0])) of
11920 * the top non-zero nybble. */
11921 for (i = vfnz[0], n = 0; i > 1; i >>= 1, n++) { }
11925 for (vshr = vlnz; vshr >= vfnz; vshr--) {
11926 vshr[1] |= (vshr[0] & (0xF >> (4 - n))) << (4 - n);
11940 U8* ve = (subnormal ? vlnz + 1 : vend);
11941 SSize_t vn = ve - v0;
11943 if (precis < (Size_t)(vn - 1)) {
11944 bool overflow = FALSE;
11945 if (v0[precis + 1] < 0x8) {
11946 /* Round down, nothing to do. */
11947 } else if (v0[precis + 1] > 0x8) {
11950 overflow = v0[precis] > 0xF;
11952 } else { /* v0[precis] == 0x8 */
11953 /* Half-point: round towards the one
11954 * with the even least-significant digit:
11962 * 78 -> 8 f8 -> 10 */
11963 if ((v0[precis] & 0x1)) {
11966 overflow = v0[precis] > 0xF;
11971 for (v = v0 + precis - 1; v >= v0; v--) {
11973 overflow = *v > 0xF;
11979 if (v == v0 - 1 && overflow) {
11980 /* If the overflow goes all the
11981 * way to the front, we need to
11982 * insert 0x1 in front, and adjust
11984 Move(v0, v0 + 1, vn - 1, char);
11990 /* The new effective "last non zero". */
11991 vlnz = v0 + precis;
11995 subnormal ? precis - vn + 1 :
11996 precis - (vlnz - vhex);
12003 /* If there are non-zero xdigits, the radix
12004 * is output after the first one. */
12012 zerotail = has_precis ? precis : 0;
12015 /* The radix is always output if precis, or if alt. */
12016 if ((has_precis && precis > 0) || alt) {
12021 #ifndef USE_LOCALE_NUMERIC
12022 PERL_UNUSED_ARG(in_lc_numeric);
12026 if (in_lc_numeric) {
12028 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(TRUE, {
12029 const char* r = SvPV(PL_numeric_radix_sv, n);
12030 Copy(r, p, n, char);
12045 if (zerotail > 0) {
12046 while (zerotail--) {
12053 /* sanity checks */
12054 if (elen >= bufsize || width >= bufsize)
12055 /* diag_listed_as: Hexadecimal float: internal error (%s) */
12056 Perl_croak(aTHX_ "Hexadecimal float: internal error (overflow)");
12058 elen += my_snprintf(p, bufsize - elen,
12059 "%c%+d", lower ? 'p' : 'P',
12062 if (elen < width) {
12063 STRLEN gap = (STRLEN)(width - elen);
12065 /* Pad the back with spaces. */
12066 memset(buf + elen, ' ', gap);
12069 /* Insert the zeros after the "0x" and the
12070 * the potential sign, but before the digits,
12071 * otherwise we end up with "0000xH.HHH...",
12072 * when we want "0x000H.HHH..." */
12073 STRLEN nzero = gap;
12074 char* zerox = buf + 2;
12075 STRLEN nmove = elen - 2;
12076 if (negative || plus) {
12080 Move(zerox, zerox + nzero, nmove, char);
12081 memset(zerox, fill ? '0' : ' ', nzero);
12084 /* Move it to the right. */
12085 Move(buf, buf + gap,
12087 /* Pad the front with spaces. */
12088 memset(buf, ' ', gap);
12096 =for apidoc sv_vcatpvfn
12097 =for apidoc_item sv_vcatpvfn_flags
12099 These process their arguments like C<L<vsprintf(3)>> and append the formatted output
12100 to an SV. They use an array of SVs if the C-style variable argument list is
12101 missing (C<NULL>). Argument reordering (using format specifiers like C<%2$d> or
12102 C<%*2$d>) is supported only when using an array of SVs; using a C-style
12103 C<va_list> argument list with a format string that uses argument reordering
12104 will yield an exception.
12106 When running with taint checks enabled, they indicate via C<maybe_tainted> if
12107 results are untrustworthy (often due to the use of locales).
12109 They assume that C<pat> has the same utf8-ness as C<sv>. It's the caller's
12110 responsibility to ensure that this is so.
12112 They differ in that C<sv_vcatpvfn_flags> has a C<flags> parameter in which you
12113 can set or clear the C<SV_GMAGIC> and/or S<SV_SMAGIC> flags, to specify which
12114 magic to handle or not handle; whereas plain C<sv_vcatpvfn> always specifies
12115 both 'get' and 'set' magic.
12117 They are usually used via one of the frontends L</C<sv_vcatpvf>> and
12118 L</C<sv_vcatpvf_mg>>.
12125 Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
12126 va_list *const args, SV **const svargs, const Size_t sv_count, bool *const maybe_tainted,
12129 const char *fmtstart; /* character following the current '%' */
12130 const char *q; /* current position within format */
12131 const char *patend;
12134 static const char nullstr[] = "(null)";
12135 bool has_utf8 = DO_UTF8(sv); /* has the result utf8? */
12136 const bool pat_utf8 = has_utf8; /* the pattern is in utf8? */
12137 /* Times 4: a decimal digit takes more than 3 binary digits.
12138 * NV_DIG: mantissa takes that many decimal digits.
12139 * Plus 32: Playing safe. */
12140 char ebuf[IV_DIG * 4 + NV_DIG + 32];
12141 bool no_redundant_warning = FALSE; /* did we use any explicit format parameter index? */
12142 #ifdef USE_LOCALE_NUMERIC
12143 bool have_in_lc_numeric = FALSE;
12145 /* we never change this unless USE_LOCALE_NUMERIC */
12146 bool in_lc_numeric = FALSE;
12149 PERL_ARGS_ASSERT_SV_VCATPVFN_FLAGS;
12150 PERL_UNUSED_ARG(maybe_tainted);
12152 if (flags & SV_GMAGIC)
12155 /* no matter what, this is a string now */
12156 (void)SvPV_force_nomg(sv, origlen);
12158 /* the code that scans for flags etc following a % relies on
12159 * a '\0' being present to avoid falling off the end. Ideally that
12160 * should be fixed */
12161 assert(pat[patlen] == '\0');
12164 /* Special-case "", "%s", "%-p" (SVf - see below) and "%.0f".
12165 * In each case, if there isn't the correct number of args, instead
12166 * fall through to the main code to handle the issuing of any
12170 if (patlen == 0 && (args || sv_count == 0))
12173 if (patlen <= 4 && pat[0] == '%' && (args || sv_count == 1)) {
12176 if (patlen == 2 && pat[1] == 's') {
12178 const char * const s = va_arg(*args, char*);
12179 sv_catpv_nomg(sv, s ? s : nullstr);
12182 /* we want get magic on the source but not the target.
12183 * sv_catsv can't do that, though */
12184 SvGETMAGIC(*svargs);
12185 sv_catsv_nomg(sv, *svargs);
12192 if (patlen == 3 && pat[1] == '-' && pat[2] == 'p') {
12193 SV *asv = MUTABLE_SV(va_arg(*args, void*));
12194 sv_catsv_nomg(sv, asv);
12198 #if !defined(USE_LONG_DOUBLE) && !defined(USE_QUADMATH)
12199 /* special-case "%.0f" */
12200 else if ( patlen == 4
12201 && pat[1] == '.' && pat[2] == '0' && pat[3] == 'f')
12203 const NV nv = SvNV(*svargs);
12204 if (LIKELY(!Perl_isinfnan(nv))) {
12208 if ((p = F0convert(nv, ebuf + sizeof ebuf, &l))) {
12209 sv_catpvn_nomg(sv, p, l);
12214 #endif /* !USE_LONG_DOUBLE */
12218 patend = (char*)pat + patlen;
12219 for (fmtstart = pat; fmtstart < patend; fmtstart = q) {
12220 char intsize = 0; /* size qualifier in "%hi..." etc */
12221 bool alt = FALSE; /* has "%#..." */
12222 bool left = FALSE; /* has "%-..." */
12223 bool fill = FALSE; /* has "%0..." */
12224 char plus = 0; /* has "%+..." */
12225 STRLEN width = 0; /* value of "%NNN..." */
12226 bool has_precis = FALSE; /* has "%.NNN..." */
12227 STRLEN precis = 0; /* value of "%.NNN..." */
12228 int base = 0; /* base to print in, e.g. 8 for %o */
12229 UV uv = 0; /* the value to print of int-ish args */
12231 bool vectorize = FALSE; /* has "%v..." */
12232 bool vec_utf8 = FALSE; /* SvUTF8(vec arg) */
12233 const U8 *vecstr = NULL; /* SvPVX(vec arg) */
12234 STRLEN veclen = 0; /* SvCUR(vec arg) */
12235 const char *dotstr = NULL; /* separator string for %v */
12236 STRLEN dotstrlen; /* length of separator string for %v */
12238 Size_t efix = 0; /* explicit format parameter index */
12239 const Size_t osvix = svix; /* original index in case of bad fmt */
12242 bool is_utf8 = FALSE; /* is this item utf8? */
12243 bool arg_missing = FALSE; /* give "Missing argument" warning */
12244 char esignbuf[4]; /* holds sign prefix, e.g. "-0x" */
12245 STRLEN esignlen = 0; /* length of e.g. "-0x" */
12246 STRLEN zeros = 0; /* how many '0' to prepend */
12248 const char *eptr = NULL; /* the address of the element string */
12249 STRLEN elen = 0; /* the length of the element string */
12251 char c; /* the actual format ('d', s' etc) */
12253 bool escape_it = FALSE; /* if this is a string should we quote and escape it? */
12256 /* echo everything up to the next format specification */
12257 for (q = fmtstart; q < patend && *q != '%'; ++q)
12260 if (q > fmtstart) {
12261 if (has_utf8 && !pat_utf8) {
12262 /* upgrade and copy the bytes of fmtstart..q-1 to utf8 on
12266 STRLEN need = SvCUR(sv) + (q - fmtstart) + 1;
12268 for (p = fmtstart; p < q; p++)
12269 if (!NATIVE_BYTE_IS_INVARIANT(*p))
12274 for (p = fmtstart; p < q; p++)
12275 append_utf8_from_native_byte((U8)*p, (U8**)&dst);
12277 SvCUR_set(sv, need - 1);
12280 S_sv_catpvn_simple(aTHX_ sv, fmtstart, q - fmtstart);
12285 fmtstart = q; /* fmtstart is char following the '%' */
12288 We allow format specification elements in this order:
12289 \d+\$ explicit format parameter index
12291 v|\*(\d+\$)?v vector with optional (optionally specified) arg
12292 0 flag (as above): repeated to allow "v02"
12293 \d+|\*(\d+\$)? width using optional (optionally specified) arg
12294 \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg
12296 [%bcdefginopsuxDFOUX] format (mandatory)
12299 if (inRANGE(*q, '1', '9')) {
12300 width = expect_number(&q);
12303 Perl_croak_nocontext(
12304 "Cannot yet reorder sv_vcatpvfn() arguments from va_list");
12306 efix = (Size_t)width;
12308 no_redundant_warning = TRUE;
12320 if (plus == '+' && *q == ' ') /* '+' over ' ' */
12347 /* at this point we can expect one of:
12349 * 123 an explicit width
12350 * * width taken from next arg
12351 * *12$ width taken from 12th arg
12354 * But any width specification may be preceded by a v, in one of its
12359 * So an asterisk may be either a width specifier or a vector
12360 * separator arg specifier, and we don't know which initially
12365 STRLEN ix; /* explicit width/vector separator index */
12367 if (inRANGE(*q, '1', '9')) {
12368 ix = expect_number(&q);
12371 Perl_croak_nocontext(
12372 "Cannot yet reorder sv_vcatpvfn() arguments from va_list");
12373 no_redundant_warning = TRUE;
12382 /* The asterisk was for *v, *NNN$v: vectorizing, but not
12383 * with the default "." */
12388 vecsv = va_arg(*args, SV*);
12390 ix = ix ? ix - 1 : svix++;
12391 vecsv = ix < sv_count ? svargs[ix]
12392 : (arg_missing = TRUE, &PL_sv_no);
12394 dotstr = SvPV_const(vecsv, dotstrlen);
12395 /* Keep the DO_UTF8 test *after* the SvPV call, else things go
12396 bad with tied or overloaded values that return UTF8. */
12397 if (DO_UTF8(vecsv))
12399 else if (has_utf8) {
12400 vecsv = sv_mortalcopy(vecsv);
12401 sv_utf8_upgrade(vecsv);
12402 dotstr = SvPV_const(vecsv, dotstrlen);
12409 /* the asterisk specified a width */
12412 SV *width_sv = NULL;
12414 i = va_arg(*args, int);
12416 ix = ix ? ix - 1 : svix++;
12417 width_sv = (ix < sv_count) ? svargs[ix]
12418 : (arg_missing = TRUE, (SV*)NULL);
12420 width = S_sprintf_arg_num_val(aTHX_ args, i, width_sv, &left);
12423 else if (*q == 'v') {
12434 /* explicit width? */
12439 if (inRANGE(*q, '1', '9'))
12440 width = expect_number(&q);
12450 STRLEN ix; /* explicit precision index */
12452 if (inRANGE(*q, '1', '9')) {
12453 ix = expect_number(&q);
12456 Perl_croak_nocontext(
12457 "Cannot yet reorder sv_vcatpvfn() arguments from va_list");
12458 no_redundant_warning = TRUE;
12467 SV *width_sv = NULL;
12471 i = va_arg(*args, int);
12473 ix = ix ? ix - 1 : svix++;
12474 width_sv = (ix < sv_count) ? svargs[ix]
12475 : (arg_missing = TRUE, (SV*)NULL);
12477 precis = S_sprintf_arg_num_val(aTHX_ args, i, width_sv, &neg);
12479 /* ignore negative precision */
12485 /* although it doesn't seem documented, this code has long
12487 * no digits following the '.' is treated like '.0'
12488 * the number may be preceded by any number of zeroes,
12489 * e.g. "%.0001f", which is the same as "%.1f"
12490 * so I've kept that behaviour. DAPM May 2017
12494 precis = inRANGE(*q, '1', '9') ? expect_number(&q) : 0;
12503 case 'I': /* Ix, I32x, and I64x */
12504 # ifdef USE_64_BIT_INT
12505 if (q[1] == '6' && q[2] == '4') {
12511 if (q[1] == '3' && q[2] == '2') {
12515 # ifdef USE_64_BIT_INT
12521 #if (IVSIZE >= 8 || defined(HAS_LONG_DOUBLE)) || \
12522 (IVSIZE == 4 && !defined(HAS_LONG_DOUBLE))
12534 #if (IVSIZE >= 8 || defined(HAS_LONG_DOUBLE)) || \
12535 (IVSIZE == 4 && !defined(HAS_LONG_DOUBLE))
12536 if (*q == 'l') { /* lld, llf */
12545 if (*++q == 'h') { /* hhd, hhu */
12552 #ifdef USE_QUADMATH
12565 c = *q++; /* c now holds the conversion type */
12567 /* '%' doesn't have an arg, so skip arg processing */
12576 if (vectorize && !memCHRs("BbDdiOouUXx", c))
12579 /* get next arg (individual branches do their own va_arg()
12580 * handling for the args case) */
12583 efix = efix ? efix - 1 : svix++;
12584 argsv = efix < sv_count ? svargs[efix]
12585 : (arg_missing = TRUE, &PL_sv_no);
12595 eptr = va_arg(*args, char*);
12598 elen = my_strnlen(eptr, precis);
12600 elen = strlen(eptr);
12602 eptr = (char *)nullstr;
12603 elen = sizeof nullstr - 1;
12607 eptr = SvPV_const(argsv, elen);
12608 if (DO_UTF8(argsv)) {
12609 STRLEN old_precis = precis;
12610 if (has_precis && precis < elen) {
12611 STRLEN ulen = sv_or_pv_len_utf8(argsv, eptr, elen);
12612 STRLEN p = precis > ulen ? ulen : precis;
12613 precis = sv_or_pv_pos_u2b(argsv, eptr, p, 0);
12614 /* sticks at end */
12616 if (width) { /* fudge width (can't fudge elen) */
12617 if (has_precis && precis < elen)
12618 width += precis - old_precis;
12621 elen - sv_or_pv_len_utf8(argsv,eptr,elen);
12629 U32 flags = PERL_PV_PRETTY_QUOTEDPREFIX;
12631 flags |= PERL_PV_ESCAPE_UNI;
12634 /* "blah"... where blah might be made up
12635 * of characters like \x{1234} */
12636 tmp_sv = newSV(1 + (PERL_QUOTEDPREFIX_LEN * 8) + 1 + 3);
12637 sv_2mortal(tmp_sv);
12639 pv_pretty(tmp_sv, eptr, elen, PERL_QUOTEDPREFIX_LEN,
12640 NULL, NULL, flags);
12641 eptr = SvPV_const(tmp_sv, elen);
12643 if (has_precis && precis < elen)
12653 * We want to extend the C level sprintf format API with
12654 * custom formats for specific types (eg SV*) and behavior.
12655 * However some C compilers are "sprintf aware" and will
12656 * throw compile time exceptions when an illegal sprintf is
12657 * encountered, so we can't just add new format letters.
12659 * However it turns out the length argument to the %p format
12660 * is more or less useless (the size of a pointer does not
12661 * change over time) and is not really used in the C level
12662 * code. Accordingly we can map our special behavior to
12663 * specific "length" options to the %p format. We hide these
12664 * mappings behind defines anyway, so nobody needs to know
12665 * that HEKf is actually %2p. This keeps the C compiler
12666 * happy while allowing us to add new formats.
12668 * Note the existing logic for which number is used for what
12669 * is torturous. All negative values are used for SVf, and
12670 * non-negative values have arbitrary meanings with no
12671 * structure to them. This may change in the future.
12673 * NEVER use the raw %p values directly. Always use the define
12674 * as the underlying mapping may change in the future.
12680 * "%...p" is normally treated like "%...x", except that the
12681 * number to print is the SV's address (or a pointer address
12682 * for C-ish sprintf).
12684 * However, the C-ish sprintf variant allows a few special
12685 * extensions. These are currently:
12687 * %-p (SVf) Like %s, but gets the string from an SV*
12688 * arg rather than a char* arg. Use C<SVfARG()>
12689 * to set up the argument properly.
12690 * (This was previously %_).
12692 * %-<num>p Ditto but like %.<num>s (i.e. num is max
12693 * width), there is no escaped and quoted version
12696 * %1p (PVf_QUOTEDPREFIX). Like raw %s, but it is escaped
12699 * %5p (SVf_QUOTEDPREFIX) Like SVf, but length restricted,
12700 * escaped and quoted with pv_pretty. Intended
12701 * for error messages.
12703 * %2p (HEKf) Like %s, but using the key string in a HEK
12704 * %7p (HEKf_QUOTEDPREFIX) ... but escaped and quoted.
12706 * %3p (HEKf256) Ditto but like %.256s
12707 * %8p (HEKf256_QUOTEDPREFIX) ... but escaped and quoted
12709 * %d%lu%4p (UTF8f) A utf8 string. Consumes 3 args:
12710 * (cBOOL(utf8), len, string_buf).
12711 * It's handled by the "case 'd'" branch
12712 * rather than here.
12713 * %d%lu%9p (UTF8f_QUOTEDPREFIX) .. but escaped and quoted.
12715 * %6p (HvNAMEf) Like %s, but using the HvNAME() and HvNAMELEN()
12716 * %10p (HvNAMEf_QUOTEDPREFIX) ... but escaped and quoted
12718 * %<num>p where num is > 9: reserved for future
12719 * extensions. Warns, but then is treated as a
12720 * general %p (print hex address) format.
12722 * NOTE: If you add a new magic %p value you will
12723 * need to update F<t/porting/diag.t> to be aware of it
12724 * on top of adding the various defines and etc. Do not
12725 * forget to add it to F<pod/perlguts.pod> as well.
12733 /* not %*p or %*1$p - any width was explicit */
12737 if (left || width == 5) { /* %-p (SVf), %-NNNp, %5p */
12738 if (left && width) {
12741 } else if (width == 5) {
12744 argsv = MUTABLE_SV(va_arg(*args, void*));
12745 eptr = SvPV_const(argsv, elen);
12746 if (DO_UTF8(argsv))
12751 else if (width == 2 || width == 3 ||
12752 width == 7 || width == 8)
12753 { /* HEKf, HEKf256, HEKf_QUOTEDPREFIX, HEKf256_QUOTEDPREFIX */
12754 HEK * const hek = va_arg(*args, HEK *);
12755 eptr = HEK_KEY(hek);
12756 elen = HEK_LEN(hek);
12768 else if (width == 1) {
12769 eptr = va_arg(*args,char *);
12770 elen = strlen(eptr);
12775 else if (width == 6 || width == 10) {
12776 HV *hv = va_arg(*args, HV *);
12778 elen = HvNAMELEN(hv);
12779 if (HvNAMEUTF8(hv))
12787 /* note width=4 or width=9 is handled under %d */
12788 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
12789 "internal %%<num>p might conflict with future printf extensions");
12793 /* treat as normal %...p */
12795 uv = PTR2UV(args ? va_arg(*args, void*) : argsv);
12797 c = 'x'; /* in case the format string contains '#' */
12801 /* Ignore any size specifiers, since they're not documented as
12802 * being allowed for %c (ideally we should warn on e.g. '%hc').
12803 * Setting a default intsize, along with a positive
12804 * (which signals unsigned) base, causes, for C-ish use, the
12805 * va_arg to be interpreted as an unsigned int, when it's
12806 * actually signed, which will convert -ve values to high +ve
12807 * values. Note that unlike the libc %c, values > 255 will
12808 * convert to high unicode points rather than being truncated
12809 * to 8 bits. For perlish use, it will do SvUV(argsv), which
12810 * will again convert -ve args to high -ve values.
12813 base = 1; /* special value that indicates we're doing a 'c' */
12814 goto get_int_arg_val;
12823 goto get_int_arg_val;
12826 /* probably just a plain %d, but it might be the start of the
12827 * special UTF8f format, which usually looks something like
12828 * "%d%lu%4p" (the lu may vary by platform) or
12829 * "%d%lu%9p" for an escaped version.
12831 assert((UTF8f)[0] == 'd');
12832 assert((UTF8f)[1] == '%');
12834 if ( args /* UTF8f only valid for C-ish sprintf */
12835 && q == fmtstart + 1 /* plain %d, not %....d */
12836 && patend >= fmtstart + sizeof(UTF8f) - 1 /* long enough */
12838 && strnEQ(q + 1, (UTF8f) + 2, sizeof(UTF8f) - 5)
12839 && q[sizeof(UTF8f)-3] == 'p'
12840 && (q[sizeof(UTF8f)-4] == '4' ||
12841 q[sizeof(UTF8f)-4] == '9'))
12843 /* The argument has already gone through cBOOL, so the cast
12845 if (q[sizeof(UTF8f)-4] == '9')
12847 is_utf8 = (bool)va_arg(*args, int);
12848 elen = va_arg(*args, UV);
12849 /* if utf8 length is larger than 0x7ffff..., then it might
12850 * have been a signed value that wrapped */
12851 if (elen > ((~(STRLEN)0) >> 1)) {
12852 assert(0); /* in DEBUGGING build we want to crash */
12853 elen = 0; /* otherwise we want to treat this as an empty string */
12855 eptr = va_arg(*args, char *);
12856 q += sizeof(UTF8f) - 2;
12863 goto get_int_arg_val;
12874 goto get_int_arg_val;
12879 goto get_int_arg_val;
12890 goto get_int_arg_val;
12905 esignbuf[esignlen++] = plus;
12908 /* initialise the vector string to iterate over */
12910 vecsv = args ? va_arg(*args, SV*) : argsv;
12912 /* if this is a version object, we need to convert
12913 * back into v-string notation and then let the
12914 * vectorize happen normally
12916 if (sv_isobject(vecsv) && sv_derived_from(vecsv, "version")) {
12917 if ( hv_existss(MUTABLE_HV(SvRV(vecsv)), "alpha") ) {
12918 Perl_ck_warner_d(aTHX_ packWARN(WARN_PRINTF),
12919 "vector argument not supported with alpha versions");
12923 vecstr = (U8*)SvPV_const(vecsv,veclen);
12924 vecsv = sv_newmortal();
12925 scan_vstring((char *)vecstr, (char *)vecstr + veclen,
12929 vecstr = (U8*)SvPV_const(vecsv, veclen);
12930 vec_utf8 = DO_UTF8(vecsv);
12932 /* This is the re-entry point for when we're iterating
12933 * over the individual characters of a vector arg */
12936 goto done_valid_conversion;
12938 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
12948 /* test arg for inf/nan. This can trigger an unwanted
12949 * 'str' overload, so manually force 'num' overload first
12953 if (UNLIKELY(SvAMAGIC(argsv)))
12954 argsv = sv_2num(argsv);
12955 if (UNLIKELY(isinfnansv(argsv)))
12956 goto handle_infnan_argsv;
12960 /* signed int type */
12965 case 'c': iv = (char)va_arg(*args, int); break;
12966 case 'h': iv = (short)va_arg(*args, int); break;
12967 case 'l': iv = va_arg(*args, long); break;
12968 case 'V': iv = va_arg(*args, IV); break;
12969 case 'z': iv = va_arg(*args, SSize_t); break;
12970 #ifdef HAS_PTRDIFF_T
12971 case 't': iv = va_arg(*args, ptrdiff_t); break;
12973 default: iv = va_arg(*args, int); break;
12974 case 'j': iv = (IV) va_arg(*args, PERL_INTMAX_T); break;
12977 iv = va_arg(*args, Quad_t); break;
12984 /* assign to tiv then cast to iv to work around
12985 * 2003 GCC cast bug (gnu.org bugzilla #13488) */
12986 IV tiv = SvIV_nomg(argsv);
12988 case 'c': iv = (char)tiv; break;
12989 case 'h': iv = (short)tiv; break;
12990 case 'l': iv = (long)tiv; break;
12992 default: iv = tiv; break;
12995 iv = (Quad_t)tiv; break;
13002 /* now convert iv to uv */
13006 esignbuf[esignlen++] = plus;
13009 /* Using 0- here to silence bogus warning from MS VC */
13010 uv = (UV) (0 - (UV) iv);
13011 esignbuf[esignlen++] = '-';
13015 /* unsigned int type */
13018 case 'c': uv = (unsigned char)va_arg(*args, unsigned);
13020 case 'h': uv = (unsigned short)va_arg(*args, unsigned);
13022 case 'l': uv = va_arg(*args, unsigned long); break;
13023 case 'V': uv = va_arg(*args, UV); break;
13024 case 'z': uv = va_arg(*args, Size_t); break;
13025 #ifdef HAS_PTRDIFF_T
13026 /* will sign extend, but there is no
13027 * uptrdiff_t, so oh well */
13028 case 't': uv = va_arg(*args, ptrdiff_t); break;
13030 case 'j': uv = (UV) va_arg(*args, PERL_UINTMAX_T); break;
13031 default: uv = va_arg(*args, unsigned); break;
13034 uv = va_arg(*args, Uquad_t); break;
13041 /* assign to tiv then cast to iv to work around
13042 * 2003 GCC cast bug (gnu.org bugzilla #13488) */
13043 UV tuv = SvUV_nomg(argsv);
13045 case 'c': uv = (unsigned char)tuv; break;
13046 case 'h': uv = (unsigned short)tuv; break;
13047 case 'l': uv = (unsigned long)tuv; break;
13049 default: uv = tuv; break;
13052 uv = (Uquad_t)tuv; break;
13063 char *ptr = ebuf + sizeof ebuf;
13070 const char * const p =
13071 (c == 'X') ? PL_hexdigit + 16 : PL_hexdigit;
13076 } while (uv >>= 4);
13077 if (alt && *ptr != '0') {
13078 esignbuf[esignlen++] = '0';
13079 esignbuf[esignlen++] = c; /* 'x' or 'X' */
13086 *--ptr = '0' + dig;
13087 } while (uv >>= 3);
13088 if (alt && *ptr != '0')
13094 *--ptr = '0' + dig;
13095 } while (uv >>= 1);
13096 if (alt && *ptr != '0') {
13097 esignbuf[esignlen++] = '0';
13098 esignbuf[esignlen++] = c; /* 'b' or 'B' */
13103 /* special-case: base 1 indicates a 'c' format:
13104 * we use the common code for extracting a uv,
13105 * but handle that value differently here than
13106 * all the other int types */
13108 (!UVCHR_IS_INVARIANT(uv) && SvUTF8(sv)))
13111 STATIC_ASSERT_STMT(sizeof(ebuf) >= UTF8_MAXBYTES + 1);
13113 elen = uvchr_to_utf8((U8*)eptr, uv) - (U8*)ebuf;
13118 ebuf[0] = (char)uv;
13123 default: /* it had better be ten or less */
13126 *--ptr = '0' + dig;
13127 } while (uv /= base);
13130 elen = (ebuf + sizeof ebuf) - ptr;
13134 zeros = precis - elen;
13135 else if (precis == 0 && elen == 1 && *eptr == '0'
13136 && !(base == 8 && alt)) /* "%#.0o" prints "0" */
13139 /* a precision nullifies the 0 flag. */
13145 /* FLOATING POINT */
13148 c = 'f'; /* maybe %F isn't supported here */
13150 case 'e': case 'E':
13152 case 'g': case 'G':
13153 case 'a': case 'A':
13156 STRLEN float_need; /* what PL_efloatsize needs to become */
13157 bool hexfp; /* hexadecimal floating point? */
13159 vcatpvfn_long_double_t fv;
13162 /* This is evil, but floating point is even more evil */
13164 /* for SV-style calling, we can only get NV
13165 for C-style calling, we assume %f is double;
13166 for simplicity we allow any of %Lf, %llf, %qf for long double
13169 #if defined(USE_QUADMATH)
13174 #if defined(USE_LONG_DOUBLE) || defined(USE_QUADMATH)
13178 /* [perl #20339] - we should accept and ignore %lf rather than die */
13182 #if defined(USE_LONG_DOUBLE) || defined(USE_QUADMATH)
13183 intsize = args ? 0 : 'q';
13187 #if defined(HAS_LONG_DOUBLE)
13200 /* Now we need (long double) if intsize == 'q', else (double). */
13202 /* Note: do not pull NVs off the va_list with va_arg()
13203 * (pull doubles instead) because if you have a build
13204 * with long doubles, you would always be pulling long
13205 * doubles, which would badly break anyone using only
13206 * doubles (i.e. the majority of builds). In other
13207 * words, you cannot mix doubles and long doubles.
13208 * The only case where you can pull off long doubles
13209 * is when the format specifier explicitly asks so with
13211 #ifdef USE_QUADMATH
13212 nv = intsize == 'Q' ? va_arg(*args, NV) :
13213 intsize == 'q' ? va_arg(*args, long double) :
13214 va_arg(*args, double);
13216 #elif LONG_DOUBLESIZE > DOUBLESIZE
13217 if (intsize == 'q') {
13218 fv = va_arg(*args, long double);
13221 nv = va_arg(*args, double);
13222 VCATPVFN_NV_TO_FV(nv, fv);
13225 nv = va_arg(*args, double);
13232 /* we jump here if an int-ish format encountered an
13233 * infinite/Nan argsv. After setting nv/fv, it falls
13234 * into the isinfnan block which follows */
13235 handle_infnan_argsv:
13236 nv = SvNV_nomg(argsv);
13237 VCATPVFN_NV_TO_FV(nv, fv);
13240 if (Perl_isinfnan(nv)) {
13242 Perl_croak(aTHX_ "Cannot printf %" NVgf " with '%c'",
13245 elen = S_infnan_2pv(nv, ebuf, sizeof(ebuf), plus);
13254 /* special-case "%.0f" */
13258 && !(width || left || plus || alt)
13261 && ((eptr = F0convert(nv, ebuf + sizeof ebuf, &elen)))
13265 /* Determine the buffer size needed for the various
13266 * floating-point formats.
13268 * The basic possibilities are:
13271 * %f 1111111.123456789
13272 * %e 1.111111123e+06
13273 * %a 0x1.0f4471f9bp+20
13275 * %g 1.11111112e+15
13277 * where P is the value of the precision in the format, or 6
13278 * if not specified. Note the two possible output formats of
13279 * %g; in both cases the number of significant digits is <=
13282 * For most of the format types the maximum buffer size needed
13283 * is precision, plus: any leading 1 or 0x1, the radix
13284 * point, and an exponent. The difficult one is %f: for a
13285 * large positive exponent it can have many leading digits,
13286 * which needs to be calculated specially. Also %a is slightly
13287 * different in that in the absence of a specified precision,
13288 * it uses as many digits as necessary to distinguish
13289 * different values.
13291 * First, here are the constant bits. For ease of calculation
13292 * we over-estimate the needed buffer size, for example by
13293 * assuming all formats have an exponent and a leading 0x1.
13295 * Also for production use, add a little extra overhead for
13296 * safety's sake. Under debugging don't, as it means we're
13297 * more likely to quickly spot issues during development.
13300 float_need = 1 /* possible unary minus */
13301 + 4 /* "0x1" plus very unlikely carry */
13302 + 1 /* default radix point '.' */
13303 + 2 /* "e-", "p+" etc */
13304 + 6 /* exponent: up to 16383 (quad fp) */
13306 + 20 /* safety net */
13311 /* determine the radix point len, e.g. length(".") in "1.2" */
13312 #ifdef USE_LOCALE_NUMERIC
13313 /* note that we may either explicitly use PL_numeric_radix_sv
13314 * below, or implicitly, via an snprintf() variant.
13315 * Note also things like ps_AF.utf8 which has
13316 * "\N{ARABIC DECIMAL SEPARATOR} as a radix point */
13317 if (! have_in_lc_numeric) {
13318 in_lc_numeric = IN_LC(LC_NUMERIC);
13319 have_in_lc_numeric = TRUE;
13322 if (in_lc_numeric) {
13323 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(TRUE, {
13324 /* this can't wrap unless PL_numeric_radix_sv is a string
13325 * consuming virtually all the 32-bit or 64-bit address
13328 float_need += (SvCUR(PL_numeric_radix_sv) - 1);
13330 /* floating-point formats only get utf8 if the radix point
13331 * is utf8. All other characters in the string are < 128
13332 * and so can be safely appended to both a non-utf8 and utf8
13334 * Note that this will convert the output to utf8 even if
13335 * the radix point didn't get output.
13337 if (SvUTF8(PL_numeric_radix_sv) && !has_utf8) {
13338 sv_utf8_upgrade(sv);
13347 if (isALPHA_FOLD_EQ(c, 'f')) {
13348 /* Determine how many digits before the radix point
13349 * might be emitted. frexp() (or frexpl) has some
13350 * unspecified behaviour for nan/inf/-inf, so lucky we've
13351 * already handled them above */
13353 int i = PERL_INT_MIN;
13354 (void)Perl_frexp((NV)fv, &i);
13355 if (i == PERL_INT_MIN)
13356 Perl_die(aTHX_ "panic: frexp: %" VCATPVFN_FV_GF, fv);
13359 digits = BIT_DIGITS(i);
13360 /* this can't overflow. 'digits' will only be a few
13361 * thousand even for the largest floating-point types.
13362 * And up until now float_need is just some small
13363 * constants plus radix len, which can't be in
13364 * overflow territory unless the radix SV is consuming
13365 * over 1/2 the address space */
13366 assert(float_need < ((STRLEN)~0) - digits);
13367 float_need += digits;
13370 else if (UNLIKELY(isALPHA_FOLD_EQ(c, 'a'))) {
13373 /* %a in the absence of precision may print as many
13374 * digits as needed to represent the entire mantissa
13376 * This estimate seriously overshoots in most cases,
13377 * but better the undershooting. Firstly, all bytes
13378 * of the NV are not mantissa, some of them are
13379 * exponent. Secondly, for the reasonably common
13380 * long doubles case, the "80-bit extended", two
13381 * or six bytes of the NV are unused. Also, we'll
13382 * still pick up an extra +6 from the default
13383 * precision calculation below. */
13385 #ifdef LONGDOUBLE_DOUBLEDOUBLE
13386 /* For the "double double", we need more.
13387 * Since each double has their own exponent, the
13388 * doubles may float (haha) rather far from each
13389 * other, and the number of required bits is much
13390 * larger, up to total of DOUBLEDOUBLE_MAXBITS bits.
13391 * See the definition of DOUBLEDOUBLE_MAXBITS.
13393 * Need 2 hexdigits for each byte. */
13394 (DOUBLEDOUBLE_MAXBITS/8 + 1) * 2;
13396 NVSIZE * 2; /* 2 hexdigits for each byte */
13398 /* see "this can't overflow" comment above */
13399 assert(float_need < ((STRLEN)~0) - digits);
13400 float_need += digits;
13403 /* special-case "%.<number>g" if it will fit in ebuf */
13405 && precis /* See earlier comment about buggy Gconvert
13406 when digits, aka precis, is 0 */
13408 /* check that "%.<number>g" formatting will fit in ebuf */
13409 && sizeof(ebuf) - float_need > precis
13410 /* sizeof(ebuf) - float_need will have wrapped if float_need > sizeof(ebuf). *
13411 * Therefore we should check that float_need < sizeof(ebuf). Normally, we would *
13412 * have run this check first, but that triggers incorrect -Wformat-overflow *
13413 * compilation warnings with some versions of gcc if Gconvert invokes sprintf(). *
13414 * ( See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89161 ) *
13415 * So, instead, we check it next: */
13416 && float_need < sizeof(ebuf)
13417 && !(width || left || plus || alt)
13421 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13422 SNPRINTF_G(fv, ebuf, sizeof(ebuf), precis)
13424 elen = strlen(ebuf);
13431 STRLEN pr = has_precis ? precis : 6; /* known default */
13432 /* this probably can't wrap, since precis is limited
13433 * to 1/4 address space size, but better safe than sorry
13435 if (float_need >= ((STRLEN)~0) - pr)
13436 croak_memory_wrap();
13440 if (float_need < width)
13441 float_need = width;
13443 if (float_need > INT_MAX) {
13444 /* snprintf() returns an int, and we use that return value,
13445 so die horribly if the expected size is too large for int
13447 Perl_croak(aTHX_ "Numeric format result too large");
13450 if (PL_efloatsize <= float_need) {
13451 /* PL_efloatbuf should be at least 1 greater than
13452 * float_need to allow a trailing \0 to be returned by
13453 * snprintf(). If we need to grow, overgrow for the
13454 * benefit of future generations */
13455 const STRLEN extra = 0x20;
13456 if (float_need >= ((STRLEN)~0) - extra)
13457 croak_memory_wrap();
13458 float_need += extra;
13459 Safefree(PL_efloatbuf);
13460 PL_efloatsize = float_need;
13461 Newx(PL_efloatbuf, PL_efloatsize, char);
13462 PL_efloatbuf[0] = '\0';
13465 if (UNLIKELY(hexfp)) {
13466 elen = S_format_hexfp(aTHX_ PL_efloatbuf, PL_efloatsize, c,
13467 nv, fv, has_precis, precis, width,
13468 alt, plus, left, fill, in_lc_numeric);
13471 char *ptr = ebuf + sizeof ebuf;
13474 #if defined(USE_QUADMATH)
13475 /* always use Q here. my_snprint() throws an exception if we
13476 fallthrough to the double/long double code, even when the
13477 format is correct, presumably to avoid any accidentally
13481 /* FIXME: what to do if HAS_LONG_DOUBLE but not PERL_PRIfldbl? */
13482 #elif defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl)
13483 /* Note that this is HAS_LONG_DOUBLE and PERL_PRIfldbl,
13484 * not USE_LONG_DOUBLE and NVff. In other words,
13485 * this needs to work without USE_LONG_DOUBLE. */
13486 if (intsize == 'q') {
13487 /* Copy the one or more characters in a long double
13488 * format before the 'base' ([efgEFG]) character to
13489 * the format string. */
13490 static char const ldblf[] = PERL_PRIfldbl;
13491 char const *p = ldblf + sizeof(ldblf) - 3;
13492 while (p >= ldblf) { *--ptr = *p--; }
13497 do { *--ptr = '0' + (base % 10); } while (base /= 10);
13502 do { *--ptr = '0' + (base % 10); } while (base /= 10);
13514 /* No taint. Otherwise we are in the strange situation
13515 * where printf() taints but print($float) doesn't.
13518 /* hopefully the above makes ptr a very constrained format
13519 * that is safe to use, even though it's not literal */
13520 GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral);
13521 #ifdef USE_QUADMATH
13523 if (!quadmath_format_valid(ptr))
13524 Perl_croak_nocontext("panic: quadmath invalid format \"%s\"", ptr);
13525 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13526 elen = quadmath_snprintf(PL_efloatbuf, PL_efloatsize,
13529 if ((IV)elen == -1) {
13530 Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", ptr);
13533 #elif defined(HAS_LONG_DOUBLE)
13534 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13535 elen = ((intsize == 'q')
13536 ? my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, fv)
13537 : my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, (double)fv))
13540 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13541 elen = my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, fv)
13544 GCC_DIAG_RESTORE_STMT;
13547 eptr = PL_efloatbuf;
13551 /* Since floating-point formats do their own formatting and
13552 * padding, we skip the main block of code at the end of this
13553 * loop which handles appending eptr to sv, and do our own
13554 * stripped-down version */
13559 assert(elen >= width);
13561 S_sv_catpvn_simple(aTHX_ sv, eptr, elen);
13563 goto done_valid_conversion;
13571 /* XXX ideally we should warn if any flags etc have been
13572 * set, e.g. "%-4.5n" */
13573 /* XXX if sv was originally non-utf8 with a char in the
13574 * range 0x80-0xff, then if it got upgraded, we should
13575 * calculate char len rather than byte len here */
13576 len = SvCUR(sv) - origlen;
13578 int i = (len > PERL_INT_MAX) ? PERL_INT_MAX : (int)len;
13581 case 'c': *(va_arg(*args, char*)) = i; break;
13582 case 'h': *(va_arg(*args, short*)) = i; break;
13583 default: *(va_arg(*args, int*)) = i; break;
13584 case 'l': *(va_arg(*args, long*)) = i; break;
13585 case 'V': *(va_arg(*args, IV*)) = i; break;
13586 case 'z': *(va_arg(*args, SSize_t*)) = i; break;
13587 #ifdef HAS_PTRDIFF_T
13588 case 't': *(va_arg(*args, ptrdiff_t*)) = i; break;
13590 case 'j': *(va_arg(*args, PERL_INTMAX_T*)) = i; break;
13593 *(va_arg(*args, Quad_t*)) = i; break;
13601 Perl_croak_nocontext(
13602 "Missing argument for %%n in %s",
13603 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
13604 sv_setuv_mg(argsv, has_utf8
13605 ? (UV)utf8_length((U8*)SvPVX(sv), (U8*)SvEND(sv))
13608 goto done_valid_conversion;
13616 && (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)
13617 && ckWARN(WARN_PRINTF))
13619 SV * const msg = sv_newmortal();
13620 Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %sprintf: ",
13621 (PL_op->op_type == OP_PRTF) ? "" : "s");
13622 if (fmtstart < patend) {
13623 const char * const fmtend = q < patend ? q : patend;
13625 sv_catpvs(msg, "\"%");
13626 for (f = fmtstart; f < fmtend; f++) {
13628 sv_catpvn_nomg(msg, f, 1);
13630 Perl_sv_catpvf(aTHX_ msg, "\\%03o", (U8) *f);
13633 sv_catpvs(msg, "\"");
13635 sv_catpvs(msg, "end of string");
13637 Perl_warner(aTHX_ packWARN(WARN_PRINTF), "%" SVf, SVfARG(msg)); /* yes, this is reentrant */
13640 /* mangled format: output the '%', then continue from the
13641 * character following that */
13642 sv_catpvn_nomg(sv, fmtstart-1, 1);
13645 /* Any "redundant arg" warning from now onwards will probably
13646 * just be misleading, so don't bother. */
13647 no_redundant_warning = TRUE;
13648 continue; /* not "break" */
13651 if (is_utf8 != has_utf8) {
13654 sv_utf8_upgrade(sv);
13657 const STRLEN old_elen = elen;
13658 SV * const nsv = newSVpvn_flags(eptr, elen, SVs_TEMP);
13659 sv_utf8_upgrade(nsv);
13660 eptr = SvPVX_const(nsv);
13663 if (width) { /* fudge width (can't fudge elen) */
13664 width += elen - old_elen;
13671 /* append esignbuf, filler, zeros, eptr and dotstr to sv */
13674 STRLEN need, have, gap;
13678 /* signed value that's wrapped? */
13679 assert(elen <= ((~(STRLEN)0) >> 1));
13681 /* if zeros is non-zero, then it represents filler between
13682 * elen and precis. So adding elen and zeros together will
13683 * always be <= precis, and the addition can never wrap */
13684 assert(!zeros || (precis > elen && precis - elen == zeros));
13685 have = elen + zeros;
13687 if (have >= (((STRLEN)~0) - esignlen))
13688 croak_memory_wrap();
13691 need = (have > width ? have : width);
13694 if (need >= (((STRLEN)~0) - (SvCUR(sv) + 1)))
13695 croak_memory_wrap();
13696 need += (SvCUR(sv) + 1);
13703 for (i = 0; i < esignlen; i++)
13704 *s++ = esignbuf[i];
13705 for (i = zeros; i; i--)
13707 Copy(eptr, s, elen, char);
13709 for (i = gap; i; i--)
13714 for (i = 0; i < esignlen; i++)
13715 *s++ = esignbuf[i];
13720 for (i = gap; i; i--)
13722 for (i = 0; i < esignlen; i++)
13723 *s++ = esignbuf[i];
13726 for (i = zeros; i; i--)
13728 Copy(eptr, s, elen, char);
13733 SvCUR_set(sv, s - SvPVX_const(sv));
13741 if (vectorize && veclen) {
13742 /* we append the vector separator separately since %v isn't
13743 * very common: don't slow down the general case by adding
13744 * dotstrlen to need etc */
13745 sv_catpvn_nomg(sv, dotstr, dotstrlen);
13747 goto vector; /* do next iteration */
13750 done_valid_conversion:
13753 S_warn_vcatpvfn_missing_argument(aTHX);
13756 /* Now that we've consumed all our printf format arguments (svix)
13757 * do we have things left on the stack that we didn't use?
13759 if (!no_redundant_warning && sv_count >= svix + 1 && ckWARN(WARN_REDUNDANT)) {
13760 Perl_warner(aTHX_ packWARN(WARN_REDUNDANT), "Redundant argument in %s",
13761 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
13764 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
13765 /* while we shouldn't set the cache, it may have been previously
13766 set in the caller, so clear it */
13767 MAGIC *mg = mg_find(sv, PERL_MAGIC_utf8);
13769 magic_setutf8(sv,mg); /* clear UTF8 cache */
13774 /* =========================================================================
13776 =for apidoc_section $embedding
13780 All the macros and functions in this section are for the private use of
13781 the main function, perl_clone().
13783 The foo_dup() functions make an exact copy of an existing foo thingy.
13784 During the course of a cloning, a hash table is used to map old addresses
13785 to new addresses. The table is created and manipulated with the
13786 ptr_table_* functions.
13788 * =========================================================================*/
13791 #if defined(USE_ITHREADS)
13793 /* XXX Remove this so it doesn't have to go thru the macro and return for nothing */
13794 #ifndef GpREFCNT_inc
13795 # define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL)
13799 #define SAVEPV(p) ((p) ? savepv(p) : NULL)
13800 #define SAVEPVN(p,n) ((p) ? savepvn(p,n) : NULL)
13802 /* clone a parser */
13805 Perl_parser_dup(pTHX_ const yy_parser *const proto, CLONE_PARAMS *const param)
13809 PERL_ARGS_ASSERT_PARSER_DUP;
13814 /* look for it in the table first */
13815 parser = (yy_parser *)ptr_table_fetch(PL_ptr_table, proto);
13819 /* create anew and remember what it is */
13820 Newxz(parser, 1, yy_parser);
13821 ptr_table_store(PL_ptr_table, proto, parser);
13823 /* XXX eventually, just Copy() most of the parser struct ? */
13825 parser->lex_brackets = proto->lex_brackets;
13826 parser->lex_casemods = proto->lex_casemods;
13827 parser->lex_brackstack = savepvn(proto->lex_brackstack,
13828 (proto->lex_brackets < 120 ? 120 : proto->lex_brackets));
13829 parser->lex_casestack = savepvn(proto->lex_casestack,
13830 (proto->lex_casemods < 12 ? 12 : proto->lex_casemods));
13831 parser->lex_defer = proto->lex_defer;
13832 parser->lex_dojoin = proto->lex_dojoin;
13833 parser->lex_formbrack = proto->lex_formbrack;
13834 parser->lex_inpat = proto->lex_inpat;
13835 parser->lex_inwhat = proto->lex_inwhat;
13836 parser->lex_op = proto->lex_op;
13837 parser->lex_repl = sv_dup_inc(proto->lex_repl, param);
13838 parser->lex_starts = proto->lex_starts;
13839 parser->lex_stuff = sv_dup_inc(proto->lex_stuff, param);
13840 parser->multi_close = proto->multi_close;
13841 parser->multi_open = proto->multi_open;
13842 parser->multi_start = proto->multi_start;
13843 parser->multi_end = proto->multi_end;
13844 parser->preambled = proto->preambled;
13845 parser->lex_super_state = proto->lex_super_state;
13846 parser->lex_sub_inwhat = proto->lex_sub_inwhat;
13847 parser->lex_sub_op = proto->lex_sub_op;
13848 parser->lex_sub_repl= sv_dup_inc(proto->lex_sub_repl, param);
13849 parser->linestr = sv_dup_inc(proto->linestr, param);
13850 parser->expect = proto->expect;
13851 parser->copline = proto->copline;
13852 parser->last_lop_op = proto->last_lop_op;
13853 parser->lex_state = proto->lex_state;
13854 parser->rsfp = fp_dup(proto->rsfp, '<', param);
13855 /* rsfp_filters entries have fake IoDIRP() */
13856 parser->rsfp_filters= av_dup_inc(proto->rsfp_filters, param);
13857 parser->in_my = proto->in_my;
13858 parser->in_my_stash = hv_dup(proto->in_my_stash, param);
13859 parser->error_count = proto->error_count;
13860 parser->sig_elems = proto->sig_elems;
13861 parser->sig_optelems= proto->sig_optelems;
13862 parser->sig_slurpy = proto->sig_slurpy;
13863 parser->recheck_utf8_validity = proto->recheck_utf8_validity;
13866 char * const ols = SvPVX(proto->linestr);
13867 char * const ls = SvPVX(parser->linestr);
13869 parser->bufptr = ls + (proto->bufptr >= ols ?
13870 proto->bufptr - ols : 0);
13871 parser->oldbufptr = ls + (proto->oldbufptr >= ols ?
13872 proto->oldbufptr - ols : 0);
13873 parser->oldoldbufptr= ls + (proto->oldoldbufptr >= ols ?
13874 proto->oldoldbufptr - ols : 0);
13875 parser->linestart = ls + (proto->linestart >= ols ?
13876 proto->linestart - ols : 0);
13877 parser->last_uni = ls + (proto->last_uni >= ols ?
13878 proto->last_uni - ols : 0);
13879 parser->last_lop = ls + (proto->last_lop >= ols ?
13880 proto->last_lop - ols : 0);
13882 parser->bufend = ls + SvCUR(parser->linestr);
13885 Copy(proto->tokenbuf, parser->tokenbuf, 256, char);
13888 Copy(proto->nextval, parser->nextval, 5, YYSTYPE);
13889 Copy(proto->nexttype, parser->nexttype, 5, I32);
13890 parser->nexttoke = proto->nexttoke;
13892 /* XXX should clone saved_curcop here, but we aren't passed
13893 * proto_perl; so do it in perl_clone_using instead */
13899 =for apidoc_section $io
13902 Duplicate a file handle, returning a pointer to the cloned object.
13908 Perl_fp_dup(pTHX_ PerlIO *const fp, const char type, CLONE_PARAMS *const param)
13912 PERL_ARGS_ASSERT_FP_DUP;
13913 PERL_UNUSED_ARG(type);
13916 return (PerlIO*)NULL;
13918 /* look for it in the table first */
13919 ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp);
13923 /* create anew and remember what it is */
13924 #ifdef __amigaos4__
13925 ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE|PERLIO_DUP_FD);
13927 ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE);
13929 ptr_table_store(PL_ptr_table, fp, ret);
13934 =for apidoc_section $io
13935 =for apidoc dirp_dup
13937 Duplicate a directory handle, returning a pointer to the cloned object.
13943 Perl_dirp_dup(pTHX_ DIR *const dp, CLONE_PARAMS *const param)
13947 #if defined(HAS_FCHDIR) && defined(HAS_TELLDIR) && defined(HAS_SEEKDIR)
13949 const Direntry_t *dirent;
13950 char smallbuf[256]; /* XXX MAXPATHLEN, surely? */
13956 PERL_UNUSED_CONTEXT;
13957 PERL_ARGS_ASSERT_DIRP_DUP;
13962 /* look for it in the table first */
13963 ret = (DIR*)ptr_table_fetch(PL_ptr_table, dp);
13967 #if defined(HAS_FCHDIR) && defined(HAS_TELLDIR) && defined(HAS_SEEKDIR)
13969 PERL_UNUSED_ARG(param);
13973 /* open the current directory (so we can switch back) */
13974 if (!(pwd = PerlDir_open("."))) return (DIR *)NULL;
13976 /* chdir to our dir handle and open the present working directory */
13977 if (fchdir(my_dirfd(dp)) < 0 || !(ret = PerlDir_open("."))) {
13978 PerlDir_close(pwd);
13979 return (DIR *)NULL;
13981 /* Now we should have two dir handles pointing to the same dir. */
13983 /* Be nice to the calling code and chdir back to where we were. */
13984 /* XXX If this fails, then what? */
13985 PERL_UNUSED_RESULT(fchdir(my_dirfd(pwd)));
13987 /* We have no need of the pwd handle any more. */
13988 PerlDir_close(pwd);
13991 # define d_namlen(d) (d)->d_namlen
13993 # define d_namlen(d) strlen((d)->d_name)
13995 /* Iterate once through dp, to get the file name at the current posi-
13996 tion. Then step back. */
13997 pos = PerlDir_tell(dp);
13998 if ((dirent = PerlDir_read(dp))) {
13999 len = d_namlen(dirent);
14000 if (len > sizeof(dirent->d_name) && sizeof(dirent->d_name) > PTRSIZE) {
14001 /* If the len is somehow magically longer than the
14002 * maximum length of the directory entry, even though
14003 * we could fit it in a buffer, we could not copy it
14004 * from the dirent. Bail out. */
14005 PerlDir_close(ret);
14008 if (len <= sizeof smallbuf) name = smallbuf;
14009 else Newx(name, len, char);
14010 Move(dirent->d_name, name, len, char);
14012 PerlDir_seek(dp, pos);
14014 /* Iterate through the new dir handle, till we find a file with the
14016 if (!dirent) /* just before the end */
14018 pos = PerlDir_tell(ret);
14019 if (PerlDir_read(ret)) continue; /* not there yet */
14020 PerlDir_seek(ret, pos); /* step back */
14024 const long pos0 = PerlDir_tell(ret);
14026 pos = PerlDir_tell(ret);
14027 if ((dirent = PerlDir_read(ret))) {
14028 if (len == (STRLEN)d_namlen(dirent)
14029 && memEQ(name, dirent->d_name, len)) {
14031 PerlDir_seek(ret, pos); /* step back */
14034 /* else we are not there yet; keep iterating */
14036 else { /* This is not meant to happen. The best we can do is
14037 reset the iterator to the beginning. */
14038 PerlDir_seek(ret, pos0);
14045 if (name && name != smallbuf)
14050 ret = win32_dirp_dup(dp, param);
14053 /* pop it in the pointer table */
14055 ptr_table_store(PL_ptr_table, dp, ret);
14061 =for apidoc_section $GV
14064 Duplicate a typeglob, returning a pointer to the cloned object.
14070 Perl_gp_dup(pTHX_ GP *const gp, CLONE_PARAMS *const param)
14074 PERL_ARGS_ASSERT_GP_DUP;
14078 /* look for it in the table first */
14079 ret = (GP*)ptr_table_fetch(PL_ptr_table, gp);
14083 /* create anew and remember what it is */
14085 ptr_table_store(PL_ptr_table, gp, ret);
14088 /* ret->gp_refcnt must be 0 before any other dups are called. We're relying
14089 on Newxz() to do this for us. */
14090 ret->gp_sv = sv_dup_inc(gp->gp_sv, param);
14091 ret->gp_io = io_dup_inc(gp->gp_io, param);
14092 ret->gp_form = cv_dup_inc(gp->gp_form, param);
14093 ret->gp_av = av_dup_inc(gp->gp_av, param);
14094 ret->gp_hv = hv_dup_inc(gp->gp_hv, param);
14095 ret->gp_egv = gv_dup(gp->gp_egv, param);/* GvEGV is not refcounted */
14096 ret->gp_cv = cv_dup_inc(gp->gp_cv, param);
14097 ret->gp_cvgen = gp->gp_cvgen;
14098 ret->gp_line = gp->gp_line;
14099 ret->gp_file_hek = hek_dup(gp->gp_file_hek, param);
14105 =for apidoc_section $magic
14108 Duplicate a chain of magic, returning a pointer to the cloned object.
14114 Perl_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *const param)
14116 MAGIC *mgret = NULL;
14117 MAGIC **mgprev_p = &mgret;
14119 PERL_ARGS_ASSERT_MG_DUP;
14121 for (; mg; mg = mg->mg_moremagic) {
14124 if ((param->flags & CLONEf_JOIN_IN)
14125 && mg->mg_type == PERL_MAGIC_backref)
14126 /* when joining, we let the individual SVs add themselves to
14127 * backref as needed. */
14130 Newx(nmg, 1, MAGIC);
14132 mgprev_p = &(nmg->mg_moremagic);
14134 /* There was a comment "XXX copy dynamic vtable?" but as we don't have
14135 dynamic vtables, I'm not sure why Sarathy wrote it. The comment dates
14136 from the original commit adding Perl_mg_dup() - revision 4538.
14137 Similarly there is the annotation "XXX random ptr?" next to the
14138 assignment to nmg->mg_ptr. */
14141 /* FIXME for plugins
14142 if (nmg->mg_type == PERL_MAGIC_qr) {
14143 nmg->mg_obj = MUTABLE_SV(CALLREGDUPE((REGEXP*)nmg->mg_obj, param));
14147 nmg->mg_obj = (nmg->mg_flags & MGf_REFCOUNTED)
14148 ? nmg->mg_type == PERL_MAGIC_backref
14149 /* The backref AV has its reference
14150 * count deliberately bumped by 1 */
14151 ? SvREFCNT_inc(av_dup_inc((const AV *)
14152 nmg->mg_obj, param))
14153 : sv_dup_inc(nmg->mg_obj, param)
14154 : (nmg->mg_type == PERL_MAGIC_regdatum ||
14155 nmg->mg_type == PERL_MAGIC_regdata)
14157 : sv_dup(nmg->mg_obj, param);
14159 if (nmg->mg_ptr && nmg->mg_type != PERL_MAGIC_regex_global) {
14160 if (nmg->mg_len > 0) {
14161 nmg->mg_ptr = SAVEPVN(nmg->mg_ptr, nmg->mg_len);
14162 if (nmg->mg_type == PERL_MAGIC_overload_table &&
14163 AMT_AMAGIC((AMT*)nmg->mg_ptr))
14165 AMT * const namtp = (AMT*)nmg->mg_ptr;
14166 sv_dup_inc_multiple((SV**)(namtp->table),
14167 (SV**)(namtp->table), NofAMmeth, param);
14170 else if (nmg->mg_len == HEf_SVKEY)
14171 nmg->mg_ptr = (char*)sv_dup_inc((const SV *)nmg->mg_ptr, param);
14173 if ((nmg->mg_flags & MGf_DUP) && nmg->mg_virtual && nmg->mg_virtual->svt_dup) {
14174 nmg->mg_virtual->svt_dup(aTHX_ nmg, param);
14180 #endif /* USE_ITHREADS */
14182 struct ptr_tbl_arena {
14183 struct ptr_tbl_arena *next;
14184 struct ptr_tbl_ent array[1023/3]; /* as ptr_tbl_ent has 3 pointers. */
14188 =for apidoc_section $embedding
14189 =for apidoc ptr_table_new
14191 Create a new pointer-mapping table
14197 Perl_ptr_table_new(pTHX)
14200 PERL_UNUSED_CONTEXT;
14202 Newx(tbl, 1, PTR_TBL_t);
14203 tbl->tbl_max = 511;
14204 tbl->tbl_items = 0;
14205 tbl->tbl_arena = NULL;
14206 tbl->tbl_arena_next = NULL;
14207 tbl->tbl_arena_end = NULL;
14208 Newxz(tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*);
14212 #define PTR_TABLE_HASH(ptr) \
14213 ((PTR2UV(ptr) >> 3) ^ (PTR2UV(ptr) >> (3 + 7)) ^ (PTR2UV(ptr) >> (3 + 17)))
14215 /* map an existing pointer using a table */
14217 STATIC PTR_TBL_ENT_t *
14218 S_ptr_table_find(PTR_TBL_t *const tbl, const void *const sv)
14220 PTR_TBL_ENT_t *tblent;
14221 const UV hash = PTR_TABLE_HASH(sv);
14223 PERL_ARGS_ASSERT_PTR_TABLE_FIND;
14225 tblent = tbl->tbl_ary[hash & tbl->tbl_max];
14226 for (; tblent; tblent = tblent->next) {
14227 if (tblent->oldval == sv)
14234 =for apidoc ptr_table_fetch
14236 Look for C<sv> in the pointer-mapping table C<tbl>, returning its value, or
14243 Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *const tbl, const void *const sv)
14245 PTR_TBL_ENT_t const *const tblent = ptr_table_find(tbl, sv);
14247 PERL_ARGS_ASSERT_PTR_TABLE_FETCH;
14248 PERL_UNUSED_CONTEXT;
14250 return tblent ? tblent->newval : NULL;
14254 =for apidoc ptr_table_store
14256 Add a new entry to a pointer-mapping table C<tbl>.
14257 In hash terms, C<oldsv> is the key; Cnewsv> is the value.
14259 The names "old" and "new" are specific to the core's typical use of ptr_tables
14266 Perl_ptr_table_store(pTHX_ PTR_TBL_t *const tbl, const void *const oldsv, void *const newsv)
14268 PTR_TBL_ENT_t *tblent = ptr_table_find(tbl, oldsv);
14270 PERL_ARGS_ASSERT_PTR_TABLE_STORE;
14271 PERL_UNUSED_CONTEXT;
14274 tblent->newval = newsv;
14276 const UV entry = PTR_TABLE_HASH(oldsv) & tbl->tbl_max;
14278 if (tbl->tbl_arena_next == tbl->tbl_arena_end) {
14279 struct ptr_tbl_arena *new_arena;
14281 Newx(new_arena, 1, struct ptr_tbl_arena);
14282 new_arena->next = tbl->tbl_arena;
14283 tbl->tbl_arena = new_arena;
14284 tbl->tbl_arena_next = new_arena->array;
14285 tbl->tbl_arena_end = C_ARRAY_END(new_arena->array);
14288 tblent = tbl->tbl_arena_next++;
14290 tblent->oldval = oldsv;
14291 tblent->newval = newsv;
14292 tblent->next = tbl->tbl_ary[entry];
14293 tbl->tbl_ary[entry] = tblent;
14295 if (tblent->next && tbl->tbl_items > tbl->tbl_max)
14296 ptr_table_split(tbl);
14301 =for apidoc ptr_table_split
14303 Double the hash bucket size of an existing ptr table
14309 Perl_ptr_table_split(pTHX_ PTR_TBL_t *const tbl)
14311 PTR_TBL_ENT_t **ary = tbl->tbl_ary;
14312 const UV oldsize = tbl->tbl_max + 1;
14313 UV newsize = oldsize * 2;
14316 PERL_ARGS_ASSERT_PTR_TABLE_SPLIT;
14317 PERL_UNUSED_CONTEXT;
14319 Renew(ary, newsize, PTR_TBL_ENT_t*);
14320 Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*);
14321 tbl->tbl_max = --newsize;
14322 tbl->tbl_ary = ary;
14323 for (i=0; i < oldsize; i++, ary++) {
14324 PTR_TBL_ENT_t **entp = ary;
14325 PTR_TBL_ENT_t *ent = *ary;
14326 PTR_TBL_ENT_t **curentp;
14329 curentp = ary + oldsize;
14331 if ((newsize & PTR_TABLE_HASH(ent->oldval)) != i) {
14333 ent->next = *curentp;
14344 =for apidoc ptr_table_free
14346 Clear and free a ptr table
14352 Perl_ptr_table_free(pTHX_ PTR_TBL_t *const tbl)
14354 struct ptr_tbl_arena *arena;
14356 PERL_UNUSED_CONTEXT;
14362 arena = tbl->tbl_arena;
14365 struct ptr_tbl_arena *next = arena->next;
14371 Safefree(tbl->tbl_ary);
14375 #if defined(USE_ITHREADS)
14378 Perl_rvpv_dup(pTHX_ SV *const dsv, const SV *const ssv, CLONE_PARAMS *const param)
14380 PERL_ARGS_ASSERT_RVPV_DUP;
14382 assert(!isREGEXP(ssv));
14384 if (SvWEAKREF(ssv)) {
14385 SvRV_set(dsv, sv_dup(SvRV_const(ssv), param));
14386 if (param->flags & CLONEf_JOIN_IN) {
14387 /* if joining, we add any back references individually rather
14388 * than copying the whole backref array */
14389 Perl_sv_add_backref(aTHX_ SvRV(dsv), dsv);
14393 SvRV_set(dsv, sv_dup_inc(SvRV_const(ssv), param));
14395 else if (SvPVX_const(ssv)) {
14396 /* Has something there */
14398 /* Normal PV - clone whole allocated space */
14399 SvPV_set(dsv, SAVEPVN(SvPVX_const(ssv), SvLEN(ssv)-1));
14400 /* ssv may not be that normal, but actually copy on write.
14401 But we are a true, independent SV, so: */
14405 /* Special case - not normally malloced for some reason */
14406 if (isGV_with_GP(ssv)) {
14407 /* Don't need to do anything here. */
14409 else if ((SvIsCOW_shared_hash(ssv))) {
14410 /* A "shared" PV - clone it as "shared" PV */
14412 HEK_KEY(hek_dup(SvSHARED_HEK_FROM_PV(SvPVX_const(ssv)),
14416 /* Some other special case - random pointer */
14417 SvPV_set(dsv, (char *) SvPVX_const(ssv));
14422 /* Copy the NULL */
14423 SvPV_set(dsv, NULL);
14427 /* duplicate a list of SVs. source and dest may point to the same memory. */
14429 S_sv_dup_inc_multiple(pTHX_ SV *const *source, SV **dest,
14430 SSize_t items, CLONE_PARAMS *const param)
14432 PERL_ARGS_ASSERT_SV_DUP_INC_MULTIPLE;
14434 while (items-- > 0) {
14435 *dest++ = sv_dup_inc(*source++, param);
14441 /* duplicate the HvAUX of an HV */
14443 S_sv_dup_hvaux(pTHX_ const SV *const ssv, SV *dsv, CLONE_PARAMS *const param)
14445 PERL_ARGS_ASSERT_SV_DUP_HVAUX;
14447 const struct xpvhv_aux * const saux = HvAUX(ssv);
14448 struct xpvhv_aux * const daux = HvAUX(dsv);
14449 /* This flag isn't copied. */
14450 SvFLAGS(dsv) |= SVphv_HasAUX;
14452 if (saux->xhv_name_count) {
14453 HEK ** const sname = saux->xhv_name_u.xhvnameu_names;
14454 const I32 count = saux->xhv_name_count < 0
14455 ? -saux->xhv_name_count
14456 : saux->xhv_name_count;
14457 HEK **shekp = sname + count;
14459 Newx(daux->xhv_name_u.xhvnameu_names, count, HEK *);
14460 dhekp = daux->xhv_name_u.xhvnameu_names + count;
14461 while (shekp-- > sname) {
14463 *dhekp = hek_dup(*shekp, param);
14467 daux->xhv_name_u.xhvnameu_name = hek_dup(saux->xhv_name_u.xhvnameu_name, param);
14469 daux->xhv_name_count = saux->xhv_name_count;
14471 daux->xhv_aux_flags = saux->xhv_aux_flags;
14472 #ifdef PERL_HASH_RANDOMIZE_KEYS
14473 daux->xhv_rand = saux->xhv_rand;
14474 daux->xhv_last_rand = saux->xhv_last_rand;
14476 daux->xhv_riter = saux->xhv_riter;
14477 daux->xhv_eiter = saux->xhv_eiter ? he_dup(saux->xhv_eiter, FALSE, param) : 0;
14478 /* backref array needs refcnt=2; see sv_add_backref */
14479 daux->xhv_backreferences =
14480 (param->flags & CLONEf_JOIN_IN)
14481 /* when joining, we let the individual GVs and
14482 * CVs add themselves to backref as
14483 * needed. This avoids pulling in stuff
14484 * that isn't required, and simplifies the
14485 * case where stashes aren't cloned back
14486 * if they already exist in the parent
14489 : saux->xhv_backreferences
14490 ? (SvTYPE(saux->xhv_backreferences) == SVt_PVAV)
14491 ? MUTABLE_AV(SvREFCNT_inc(
14492 sv_dup_inc((const SV *)
14493 saux->xhv_backreferences, param)))
14494 : MUTABLE_AV(sv_dup((const SV *)
14495 saux->xhv_backreferences, param))
14498 daux->xhv_mro_meta = saux->xhv_mro_meta
14499 ? mro_meta_dup(saux->xhv_mro_meta, param)
14502 /* Record stashes for possible cloning in Perl_clone(). */
14504 av_push(param->stashes, dsv);
14506 if (HvSTASH_IS_CLASS(ssv)) {
14507 daux->xhv_class_superclass = hv_dup_inc(saux->xhv_class_superclass, param);
14508 daux->xhv_class_initfields_cv = cv_dup_inc(saux->xhv_class_initfields_cv, param);
14509 daux->xhv_class_adjust_blocks = av_dup_inc(saux->xhv_class_adjust_blocks, param);
14510 daux->xhv_class_fields = padnamelist_dup_inc(saux->xhv_class_fields, param);
14511 daux->xhv_class_next_fieldix = saux->xhv_class_next_fieldix;
14512 daux->xhv_class_param_map = hv_dup_inc(saux->xhv_class_param_map, param);
14514 /* TODO: This does mean that we can't compile more `field` expressions
14515 * in the cloned thread, but surely we're done with compiletime now..?
14517 daux->xhv_class_suspended_initfields_compcv = NULL;
14521 /* duplicate an SV of any type (including AV, HV etc) */
14524 S_sv_dup_common(pTHX_ const SV *const ssv, CLONE_PARAMS *const param)
14528 PERL_ARGS_ASSERT_SV_DUP_COMMON;
14530 if (SvIS_FREED(ssv)) {
14531 #ifdef DEBUG_LEAKING_SCALARS_ABORT
14536 /* look for it in the table first */
14537 dsv = MUTABLE_SV(ptr_table_fetch(PL_ptr_table, ssv));
14541 if(param->flags & CLONEf_JOIN_IN) {
14542 /** We are joining here so we don't want do clone
14543 something that is bad **/
14544 if (SvTYPE(ssv) == SVt_PVHV) {
14545 const HEK * const hvname = HvNAME_HEK(ssv);
14547 /** don't clone stashes if they already exist **/
14548 dsv = MUTABLE_SV(gv_stashpvn(HEK_KEY(hvname), HEK_LEN(hvname),
14549 HEK_UTF8(hvname) ? SVf_UTF8 : 0));
14550 ptr_table_store(PL_ptr_table, ssv, dsv);
14554 else if (SvTYPE(ssv) == SVt_PVGV && !SvFAKE(ssv)) {
14555 HV *stash = GvSTASH(ssv);
14556 const HEK * hvname;
14557 if (stash && (hvname = HvNAME_HEK(stash))) {
14558 /** don't clone GVs if they already exist **/
14560 stash = gv_stashpvn(HEK_KEY(hvname), HEK_LEN(hvname),
14561 HEK_UTF8(hvname) ? SVf_UTF8 : 0);
14563 stash, GvNAME(ssv),
14569 if (svp && *svp && SvTYPE(*svp) == SVt_PVGV) {
14570 ptr_table_store(PL_ptr_table, ssv, *svp);
14577 /* create anew and remember what it is */
14580 #ifdef DEBUG_LEAKING_SCALARS
14581 dsv->sv_debug_optype = ssv->sv_debug_optype;
14582 dsv->sv_debug_line = ssv->sv_debug_line;
14583 dsv->sv_debug_inpad = ssv->sv_debug_inpad;
14584 dsv->sv_debug_parent = (SV*)ssv;
14585 FREE_SV_DEBUG_FILE(dsv);
14586 dsv->sv_debug_file = savesharedpv(ssv->sv_debug_file);
14589 ptr_table_store(PL_ptr_table, ssv, dsv);
14592 SvFLAGS(dsv) = SvFLAGS(ssv);
14593 SvFLAGS(dsv) &= ~SVf_OOK; /* don't propagate OOK hack */
14594 SvREFCNT(dsv) = 0; /* must be before any other dups! */
14597 if (SvANY(ssv) && PL_watch_pvx && SvPVX_const(ssv) == PL_watch_pvx)
14598 PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n",
14599 (void*)PL_watch_pvx, SvPVX_const(ssv));
14602 /* don't clone objects whose class has asked us not to */
14604 && ! (SvFLAGS(SvSTASH(ssv)) & SVphv_CLONEABLE))
14610 switch (SvTYPE(ssv)) {
14615 SET_SVANY_FOR_BODYLESS_IV(dsv);
14617 Perl_rvpv_dup(aTHX_ dsv, ssv, param);
14619 SvIV_set(dsv, SvIVX(ssv));
14623 #if NVSIZE <= IVSIZE
14624 SET_SVANY_FOR_BODYLESS_NV(dsv);
14626 SvANY(dsv) = new_XNV();
14628 SvNV_set(dsv, SvNVX(ssv));
14632 /* These are all the types that need complex bodies allocating. */
14634 const svtype sv_type = SvTYPE(ssv);
14635 const struct body_details *sv_type_details
14636 = bodies_by_type + sv_type;
14640 Perl_croak(param->proto_perl, "Bizarre SvTYPE [%" IVdf "]", (IV)SvTYPE(ssv));
14641 NOT_REACHED; /* NOTREACHED */
14645 if (HvHasAUX(ssv)) {
14646 sv_type_details = &fake_hv_with_aux;
14648 new_body = new_NOARENA(sv_type_details);
14650 new_body_from_arena(new_body, HVAUX_ARENA_ROOT_IX, fake_hv_with_aux);
14668 assert(sv_type_details->body_size);
14670 if (sv_type_details->arena) {
14671 new_body = S_new_body(aTHX_ sv_type);
14673 = (void*)((char*)new_body - sv_type_details->offset);
14677 new_body = new_NOARENA(sv_type_details);
14682 SvANY(dsv) = new_body;
14685 Copy(((char*)SvANY(ssv)) + sv_type_details->offset,
14686 ((char*)SvANY(dsv)) + sv_type_details->offset,
14687 sv_type_details->copy, char);
14689 Copy(((char*)SvANY(ssv)),
14690 ((char*)SvANY(dsv)),
14691 sv_type_details->body_size + sv_type_details->offset, char);
14694 if (sv_type != SVt_PVAV && sv_type != SVt_PVHV && sv_type != SVt_PVOBJ
14695 && !isGV_with_GP(dsv)
14697 && !(sv_type == SVt_PVIO && !(IoFLAGS(dsv) & IOf_FAKE_DIRP)))
14698 Perl_rvpv_dup(aTHX_ dsv, ssv, param);
14700 /* The Copy above means that all the source (unduplicated) pointers
14701 are now in the destination. We can check the flags and the
14702 pointers in either, but it's possible that there's less cache
14703 missing by always going for the destination.
14704 FIXME - instrument and check that assumption */
14705 if (sv_type >= SVt_PVMG) {
14707 SvMAGIC_set(dsv, mg_dup(SvMAGIC(dsv), param));
14708 if (SvOBJECT(dsv) && SvSTASH(dsv))
14709 SvSTASH_set(dsv, hv_dup_inc(SvSTASH(dsv), param));
14710 else SvSTASH_set(dsv, 0); /* don't copy DESTROY cache */
14713 /* The cast silences a GCC warning about unhandled types. */
14714 switch ((int)sv_type) {
14725 /* FIXME for plugins */
14726 re_dup_guts((REGEXP*) ssv, (REGEXP*) dsv, param);
14729 /* XXX LvTARGOFF sometimes holds PMOP* when DEBUGGING */
14730 if (LvTYPE(dsv) == 't') /* for tie: unrefcnted fake (SV**) */
14732 else if (LvTYPE(dsv) == 'T') /* for tie: fake HE */
14733 LvTARG(dsv) = MUTABLE_SV(he_dup((HE*)LvTARG(dsv), FALSE, param));
14735 LvTARG(dsv) = sv_dup_inc(LvTARG(dsv), param);
14736 if (isREGEXP(ssv)) goto duprex;
14739 /* non-GP case already handled above */
14740 if(isGV_with_GP(ssv)) {
14741 GvNAME_HEK(dsv) = hek_dup(GvNAME_HEK(dsv), param);
14742 /* Don't call sv_add_backref here as it's going to be
14743 created as part of the magic cloning of the symbol
14744 table--unless this is during a join and the stash
14745 is not actually being cloned. */
14746 /* Danger Will Robinson - GvGP(dsv) isn't initialised
14747 at the point of this comment. */
14748 GvSTASH(dsv) = hv_dup(GvSTASH(dsv), param);
14749 if (param->flags & CLONEf_JOIN_IN)
14750 Perl_sv_add_backref(aTHX_ MUTABLE_SV(GvSTASH(dsv)), dsv);
14751 GvGP_set(dsv, gp_dup(GvGP(ssv), param));
14752 (void)GpREFCNT_inc(GvGP(dsv));
14756 /* PL_parser->rsfp_filters entries have fake IoDIRP() */
14757 if(IoFLAGS(dsv) & IOf_FAKE_DIRP) {
14758 /* I have no idea why fake dirp (rsfps)
14759 should be treated differently but otherwise
14760 we end up with leaks -- sky*/
14761 IoTOP_GV(dsv) = gv_dup_inc(IoTOP_GV(dsv), param);
14762 IoFMT_GV(dsv) = gv_dup_inc(IoFMT_GV(dsv), param);
14763 IoBOTTOM_GV(dsv) = gv_dup_inc(IoBOTTOM_GV(dsv), param);
14765 IoTOP_GV(dsv) = gv_dup(IoTOP_GV(dsv), param);
14766 IoFMT_GV(dsv) = gv_dup(IoFMT_GV(dsv), param);
14767 IoBOTTOM_GV(dsv) = gv_dup(IoBOTTOM_GV(dsv), param);
14769 IoDIRP(dsv) = dirp_dup(IoDIRP(dsv), param);
14772 /* IoDIRP(dsv) is already a copy of IoDIRP(ssv) */
14774 IoIFP(dsv) = fp_dup(IoIFP(ssv), IoTYPE(dsv), param);
14776 if (IoOFP(dsv) == IoIFP(ssv))
14777 IoOFP(dsv) = IoIFP(dsv);
14779 IoOFP(dsv) = fp_dup(IoOFP(dsv), IoTYPE(dsv), param);
14780 IoTOP_NAME(dsv) = SAVEPV(IoTOP_NAME(dsv));
14781 IoFMT_NAME(dsv) = SAVEPV(IoFMT_NAME(dsv));
14782 IoBOTTOM_NAME(dsv) = SAVEPV(IoBOTTOM_NAME(dsv));
14785 /* avoid cloning an empty array */
14786 if (AvARRAY((const AV *)ssv) && AvFILLp((const AV *)ssv) >= 0) {
14787 SV **dst_ary, **src_ary;
14788 SSize_t items = AvFILLp((const AV *)ssv) + 1;
14790 src_ary = AvARRAY((const AV *)ssv);
14791 Newx(dst_ary, AvMAX((const AV *)ssv)+1, SV*);
14792 ptr_table_store(PL_ptr_table, src_ary, dst_ary);
14793 AvARRAY(MUTABLE_AV(dsv)) = dst_ary;
14794 AvALLOC((const AV *)dsv) = dst_ary;
14795 if (AvREAL((const AV *)ssv)) {
14796 dst_ary = sv_dup_inc_multiple(src_ary, dst_ary, items,
14800 while (items-- > 0)
14801 *dst_ary++ = sv_dup(*src_ary++, param);
14803 items = AvMAX((const AV *)ssv) - AvFILLp((const AV *)ssv);
14804 while (items-- > 0) {
14809 AvARRAY(MUTABLE_AV(dsv)) = NULL;
14810 AvALLOC((const AV *)dsv) = (SV**)NULL;
14811 AvMAX( (const AV *)dsv) = -1;
14812 AvFILLp((const AV *)dsv) = -1;
14816 if (HvARRAY((const HV *)ssv)) {
14818 XPVHV * const dxhv = (XPVHV*)SvANY(dsv);
14819 XPVHV * const sxhv = (XPVHV*)SvANY(ssv);
14821 Newx(darray, PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1),
14823 HvARRAY(dsv) = (HE**)darray;
14824 while (i <= sxhv->xhv_max) {
14825 const HE * const source = HvARRAY(ssv)[i];
14826 HvARRAY(dsv)[i] = source
14827 ? he_dup(source, FALSE, param) : 0;
14831 sv_dup_hvaux(ssv, dsv, param);
14834 HvARRAY(MUTABLE_HV(dsv)) = NULL;
14837 if (!(param->flags & CLONEf_COPY_STACKS)) {
14842 /* NOTE: not refcounted */
14843 SvANY(MUTABLE_CV(dsv))->xcv_stash =
14844 hv_dup(CvSTASH(dsv), param);
14845 if ((param->flags & CLONEf_JOIN_IN) && CvSTASH(dsv))
14846 Perl_sv_add_backref(aTHX_ MUTABLE_SV(CvSTASH(dsv)), dsv);
14847 if (!CvISXSUB(dsv)) {
14849 CvROOT(dsv) = OpREFCNT_inc(CvROOT(dsv));
14851 CvSLABBED_off(dsv);
14852 } else if (CvCONST(dsv)) {
14853 CvXSUBANY(dsv).any_ptr =
14854 sv_dup_inc((const SV *)CvXSUBANY(dsv).any_ptr, param);
14855 } else if (CvREFCOUNTED_ANYSV(dsv)) {
14856 CvXSUBANY(dsv).any_sv =
14857 sv_dup_inc((const SV *)CvXSUBANY(dsv).any_sv, param);
14859 assert(!CvSLABBED(dsv));
14860 if (CvDYNFILE(dsv)) CvFILE(dsv) = SAVEPV(CvFILE(dsv));
14862 SvANY((CV *)dsv)->xcv_gv_u.xcv_hek =
14863 hek_dup(CvNAME_HEK((CV *)ssv), param);
14864 /* don't dup if copying back - CvGV isn't refcounted, so the
14865 * duped GV may never be freed. A bit of a hack! DAPM */
14867 SvANY(MUTABLE_CV(dsv))->xcv_gv_u.xcv_gv =
14869 ? gv_dup_inc(CvGV(ssv), param)
14870 : (param->flags & CLONEf_JOIN_IN)
14872 : gv_dup(CvGV(ssv), param);
14874 if (!CvISXSUB(ssv)) {
14875 PADLIST * padlist = CvPADLIST(ssv);
14877 padlist = padlist_dup(padlist, param);
14878 CvPADLIST_set(dsv, padlist);
14880 /* unthreaded perl can't sv_dup so we don't support unthreaded's CvHSCXT */
14881 PoisonPADLIST(dsv);
14885 ? cv_dup( CvOUTSIDE(dsv), param)
14886 : cv_dup_inc(CvOUTSIDE(dsv), param);
14890 Size_t fieldcount = ObjectMAXFIELD(ssv) + 1;
14892 Newx(ObjectFIELDS(dsv), fieldcount, SV *);
14893 ObjectMAXFIELD(dsv) = fieldcount - 1;
14895 sv_dup_inc_multiple(ObjectFIELDS(ssv), ObjectFIELDS(dsv), fieldcount, param);
14906 Perl_sv_dup_inc(pTHX_ const SV *const ssv, CLONE_PARAMS *const param)
14908 PERL_ARGS_ASSERT_SV_DUP_INC;
14909 return ssv ? SvREFCNT_inc(sv_dup_common(ssv, param)) : NULL;
14913 Perl_sv_dup(pTHX_ const SV *const ssv, CLONE_PARAMS *const param)
14915 SV *dsv = ssv ? sv_dup_common(ssv, param) : NULL;
14916 PERL_ARGS_ASSERT_SV_DUP;
14918 /* Track every SV that (at least initially) had a reference count of 0.
14919 We need to do this by holding an actual reference to it in this array.
14920 If we attempt to cheat, turn AvREAL_off(), and store only pointers
14921 (akin to the stashes hash, and the perl stack), we come unstuck if
14922 a weak reference (or other SV legitimately SvREFCNT() == 0 for this
14923 thread) is manipulated in a CLONE method, because CLONE runs before the
14924 unreferenced array is walked to find SVs still with SvREFCNT() == 0
14925 (and fix things up by giving each a reference via the temps stack).
14926 Instead, during CLONE, if the 0-referenced SV has SvREFCNT_inc() and
14927 then SvREFCNT_dec(), it will be cleaned up (and added to the free list)
14928 before the walk of unreferenced happens and a reference to that is SV
14929 added to the temps stack. At which point we have the same SV considered
14930 to be in use, and free to be re-used. Not good.
14932 if (dsv && !(param->flags & CLONEf_COPY_STACKS) && !SvREFCNT(dsv)) {
14933 assert(param->unreferenced);
14934 av_push(param->unreferenced, SvREFCNT_inc(dsv));
14940 /* duplicate a context */
14943 Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max, CLONE_PARAMS* param)
14945 PERL_CONTEXT *ncxs;
14947 PERL_ARGS_ASSERT_CX_DUP;
14950 return (PERL_CONTEXT*)NULL;
14952 /* look for it in the table first */
14953 ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs);
14957 /* create anew and remember what it is */
14958 Newx(ncxs, max + 1, PERL_CONTEXT);
14959 ptr_table_store(PL_ptr_table, cxs, ncxs);
14960 Copy(cxs, ncxs, max + 1, PERL_CONTEXT);
14963 PERL_CONTEXT * const ncx = &ncxs[ix];
14964 if (CxTYPE(ncx) == CXt_SUBST) {
14965 Perl_croak(aTHX_ "Cloning substitution context is unimplemented");
14968 ncx->blk_oldcop = (COP*)any_dup(ncx->blk_oldcop, param->proto_perl);
14969 switch (CxTYPE(ncx)) {
14971 ncx->blk_sub.cv = cv_dup_inc(ncx->blk_sub.cv, param);
14972 if(CxHASARGS(ncx)){
14973 ncx->blk_sub.savearray = av_dup_inc(ncx->blk_sub.savearray,param);
14975 ncx->blk_sub.savearray = NULL;
14977 ncx->blk_sub.prevcomppad = (PAD*)ptr_table_fetch(PL_ptr_table,
14978 ncx->blk_sub.prevcomppad);
14981 ncx->blk_eval.old_namesv = sv_dup_inc(ncx->blk_eval.old_namesv,
14983 /* XXX should this sv_dup_inc? Or only if CxEVAL_TXT_REFCNTED ???? */
14984 ncx->blk_eval.cur_text = sv_dup(ncx->blk_eval.cur_text, param);
14985 ncx->blk_eval.cv = cv_dup(ncx->blk_eval.cv, param);
14986 /* XXX what to do with cur_top_env ???? */
14988 case CXt_LOOP_LAZYSV:
14989 ncx->blk_loop.state_u.lazysv.end
14990 = sv_dup_inc(ncx->blk_loop.state_u.lazysv.end, param);
14991 /* Fallthrough: duplicate lazysv.cur by using the ary.ary
14992 duplication code instead.
14993 We are taking advantage of (1) av_dup_inc and sv_dup_inc
14994 actually being the same function, and (2) order
14995 equivalence of the two unions.
14996 We can assert the later [but only at run time :-(] */
14997 assert ((void *) &ncx->blk_loop.state_u.ary.ary ==
14998 (void *) &ncx->blk_loop.state_u.lazysv.cur);
15001 ncx->blk_loop.state_u.ary.ary
15002 = av_dup_inc(ncx->blk_loop.state_u.ary.ary, param);
15004 case CXt_LOOP_LIST:
15005 case CXt_LOOP_LAZYIV:
15006 /* code common to all 'for' CXt_LOOP_* types */
15007 ncx->blk_loop.itersave =
15008 sv_dup_inc(ncx->blk_loop.itersave, param);
15009 if (CxPADLOOP(ncx)) {
15010 PADOFFSET off = ncx->blk_loop.itervar_u.svp
15011 - &CX_CURPAD_SV(ncx->blk_loop, 0);
15012 ncx->blk_loop.oldcomppad =
15013 (PAD*)ptr_table_fetch(PL_ptr_table,
15014 ncx->blk_loop.oldcomppad);
15015 ncx->blk_loop.itervar_u.svp =
15016 &CX_CURPAD_SV(ncx->blk_loop, off);
15019 /* this copies the GV if CXp_FOR_GV, or the SV for an
15020 * alias (for \$x (...)) - relies on gv_dup being the
15021 * same as sv_dup */
15022 ncx->blk_loop.itervar_u.gv
15023 = gv_dup((const GV *)ncx->blk_loop.itervar_u.gv,
15027 case CXt_LOOP_PLAIN:
15030 ncx->blk_format.prevcomppad =
15031 (PAD*)ptr_table_fetch(PL_ptr_table,
15032 ncx->blk_format.prevcomppad);
15033 ncx->blk_format.cv = cv_dup_inc(ncx->blk_format.cv, param);
15034 ncx->blk_format.gv = gv_dup(ncx->blk_format.gv, param);
15035 ncx->blk_format.dfoutgv = gv_dup_inc(ncx->blk_format.dfoutgv,
15039 ncx->blk_givwhen.defsv_save =
15040 sv_dup_inc(ncx->blk_givwhen.defsv_save, param);
15057 Duplicate a stack info structure, returning a pointer to the cloned object.
15063 Perl_si_dup(pTHX_ PERL_SI *si, CLONE_PARAMS* param)
15067 PERL_ARGS_ASSERT_SI_DUP;
15070 return (PERL_SI*)NULL;
15072 /* look for it in the table first */
15073 nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si);
15077 /* create anew and remember what it is */
15078 Newx(nsi, 1, PERL_SI);
15079 ptr_table_store(PL_ptr_table, si, nsi);
15081 nsi->si_stack = av_dup_inc(si->si_stack, param);
15082 nsi->si_cxix = si->si_cxix;
15083 nsi->si_cxsubix = si->si_cxsubix;
15084 nsi->si_cxmax = si->si_cxmax;
15085 nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax, param);
15086 nsi->si_type = si->si_type;
15087 nsi->si_prev = si_dup(si->si_prev, param);
15088 nsi->si_next = si_dup(si->si_next, param);
15089 nsi->si_markoff = si->si_markoff;
15090 #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
15091 nsi->si_stack_hwm = 0;
15097 #define POPINT(ss,ix) ((ss)[--(ix)].any_i32)
15098 #define TOPINT(ss,ix) ((ss)[ix].any_i32)
15099 #define POPLONG(ss,ix) ((ss)[--(ix)].any_long)
15100 #define TOPLONG(ss,ix) ((ss)[ix].any_long)
15101 #define POPIV(ss,ix) ((ss)[--(ix)].any_iv)
15102 #define TOPIV(ss,ix) ((ss)[ix].any_iv)
15103 #define POPUV(ss,ix) ((ss)[--(ix)].any_uv)
15104 #define TOPUV(ss,ix) ((ss)[ix].any_uv)
15105 #define POPBOOL(ss,ix) ((ss)[--(ix)].any_bool)
15106 #define TOPBOOL(ss,ix) ((ss)[ix].any_bool)
15107 #define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr)
15108 #define TOPPTR(ss,ix) ((ss)[ix].any_ptr)
15109 #define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr)
15110 #define TOPDPTR(ss,ix) ((ss)[ix].any_dptr)
15111 #define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr)
15112 #define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr)
15115 #define pv_dup_inc(p) SAVEPV(p)
15116 #define pv_dup(p) SAVEPV(p)
15117 #define svp_dup_inc(p,pp) any_dup(p,pp)
15119 /* map any object to the new equivalent - either something in the
15120 * ptr table, or something in the interpreter structure
15124 Perl_any_dup(pTHX_ void *v, const PerlInterpreter *proto_perl)
15128 PERL_ARGS_ASSERT_ANY_DUP;
15131 return (void*)NULL;
15133 /* look for it in the table first */
15134 ret = ptr_table_fetch(PL_ptr_table, v);
15138 /* see if it is part of the interpreter structure */
15139 if (v >= (void*)proto_perl && v < (void*)(proto_perl+1))
15140 ret = (void*)(((char*)aTHX) + (((char*)v) - (char*)proto_perl));
15151 Duplicate the save stack, returning a pointer to the cloned object.
15157 Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl, CLONE_PARAMS* param)
15159 ANY * const ss = proto_perl->Isavestack;
15160 const I32 max = proto_perl->Isavestack_max + SS_MAXPUSH;
15161 I32 ix = proto_perl->Isavestack_ix;
15167 char *pv; /* no const deliberately */
15175 void (*dptr) (void*);
15176 void (*dxptr) (pTHX_ void*);
15178 PERL_ARGS_ASSERT_SS_DUP;
15180 Newx(nss, max, ANY);
15183 const UV uv = POPUV(ss,ix);
15184 const U8 type = (U8)uv & SAVE_MASK;
15186 TOPUV(nss,ix) = uv;
15188 case SAVEt_CLEARSV:
15189 case SAVEt_CLEARPADRANGE:
15191 case SAVEt_HELEM: /* hash element */
15192 case SAVEt_SV: /* scalar reference */
15193 sv = (const SV *)POPPTR(ss,ix);
15194 TOPPTR(nss,ix) = SvREFCNT_inc(sv_dup_inc(sv, param));
15196 case SAVEt_ITEM: /* normal string */
15197 case SAVEt_GVSV: /* scalar slot in GV */
15198 sv = (const SV *)POPPTR(ss,ix);
15199 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15200 if (type == SAVEt_SV)
15204 case SAVEt_MORTALIZESV:
15205 case SAVEt_READONLY_OFF:
15206 sv = (const SV *)POPPTR(ss,ix);
15207 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15209 case SAVEt_FREEPADNAME:
15210 ptr = POPPTR(ss,ix);
15211 TOPPTR(nss,ix) = padname_dup((PADNAME *)ptr, param);
15212 PadnameREFCNT((PADNAME *)TOPPTR(nss,ix))++;
15214 case SAVEt_SHARED_PVREF: /* char* in shared space */
15215 c = (char*)POPPTR(ss,ix);
15216 TOPPTR(nss,ix) = savesharedpv(c);
15217 ptr = POPPTR(ss,ix);
15218 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15220 case SAVEt_GENERIC_SVREF: /* generic sv */
15221 case SAVEt_SVREF: /* scalar reference */
15222 sv = (const SV *)POPPTR(ss,ix);
15223 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15224 if (type == SAVEt_SVREF)
15225 SvREFCNT_inc_simple_void((SV *)TOPPTR(nss,ix));
15226 ptr = POPPTR(ss,ix);
15227 TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
15228 /* this feels very strange, we have a **SV from one thread,
15229 * we copy the SV, but dont change the **SV. But in this thread
15230 * the target of the **SV could be something from the *other* thread.
15231 * So how can this possibly work correctly? */
15234 pv = (char *)POPPTR(ss,ix);
15235 TOPPTR(nss,ix) = rcpv_copy(pv);
15236 ptr = POPPTR(ss,ix);
15237 (void)rcpv_copy(*((char **)ptr));
15238 TOPPTR(nss,ix) = ptr;
15239 /* XXXXX: see comment above. */
15241 case SAVEt_GVSLOT: /* any slot in GV */
15242 sv = (const SV *)POPPTR(ss,ix);
15243 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15244 ptr = POPPTR(ss,ix);
15245 TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
15246 sv = (const SV *)POPPTR(ss,ix);
15247 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15249 case SAVEt_HV: /* hash reference */
15250 case SAVEt_AV: /* array reference */
15251 sv = (const SV *) POPPTR(ss,ix);
15252 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15254 case SAVEt_COMPPAD:
15256 sv = (const SV *) POPPTR(ss,ix);
15257 TOPPTR(nss,ix) = sv_dup(sv, param);
15259 case SAVEt_INT: /* int reference */
15260 ptr = POPPTR(ss,ix);
15261 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15262 intval = (int)POPINT(ss,ix);
15263 TOPINT(nss,ix) = intval;
15265 case SAVEt_LONG: /* long reference */
15266 ptr = POPPTR(ss,ix);
15267 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15268 longval = (long)POPLONG(ss,ix);
15269 TOPLONG(nss,ix) = longval;
15271 case SAVEt_I32: /* I32 reference */
15272 ptr = POPPTR(ss,ix);
15273 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15275 TOPINT(nss,ix) = i;
15277 case SAVEt_IV: /* IV reference */
15278 case SAVEt_STRLEN: /* STRLEN/size_t ref */
15279 ptr = POPPTR(ss,ix);
15280 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15282 TOPIV(nss,ix) = iv;
15284 case SAVEt_TMPSFLOOR:
15286 TOPIV(nss,ix) = iv;
15288 case SAVEt_HPTR: /* HV* reference */
15289 case SAVEt_APTR: /* AV* reference */
15290 case SAVEt_SPTR: /* SV* reference */
15291 ptr = POPPTR(ss,ix);
15292 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15293 sv = (const SV *)POPPTR(ss,ix);
15294 TOPPTR(nss,ix) = sv_dup(sv, param);
15296 case SAVEt_VPTR: /* random* reference */
15297 ptr = POPPTR(ss,ix);
15298 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15300 case SAVEt_STRLEN_SMALL:
15301 case SAVEt_INT_SMALL:
15302 case SAVEt_I32_SMALL:
15303 case SAVEt_I16: /* I16 reference */
15304 case SAVEt_I8: /* I8 reference */
15306 ptr = POPPTR(ss,ix);
15307 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15309 case SAVEt_GENERIC_PVREF: /* generic char* */
15310 case SAVEt_PPTR: /* char* reference */
15311 ptr = POPPTR(ss,ix);
15312 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15313 c = (char*)POPPTR(ss,ix);
15314 TOPPTR(nss,ix) = pv_dup(c);
15316 case SAVEt_GP: /* scalar reference */
15317 gp = (GP*)POPPTR(ss,ix);
15318 TOPPTR(nss,ix) = gp = gp_dup(gp, param);
15319 (void)GpREFCNT_inc(gp);
15320 gv = (const GV *)POPPTR(ss,ix);
15321 TOPPTR(nss,ix) = gv_dup_inc(gv, param);
15324 ptr = POPPTR(ss,ix);
15325 if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) {
15326 /* these are assumed to be refcounted properly */
15328 switch (((OP*)ptr)->op_type) {
15330 case OP_LEAVESUBLV:
15334 case OP_LEAVEWRITE:
15335 TOPPTR(nss,ix) = ptr;
15338 (void) OpREFCNT_inc(o);
15342 TOPPTR(nss,ix) = NULL;
15347 TOPPTR(nss,ix) = NULL;
15349 case SAVEt_FREECOPHH:
15350 ptr = POPPTR(ss,ix);
15351 TOPPTR(nss,ix) = cophh_copy((COPHH *)ptr);
15353 case SAVEt_ADELETE:
15354 av = (const AV *)POPPTR(ss,ix);
15355 TOPPTR(nss,ix) = av_dup_inc(av, param);
15357 TOPINT(nss,ix) = i;
15360 hv = (const HV *)POPPTR(ss,ix);
15361 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
15363 TOPINT(nss,ix) = i;
15366 c = (char*)POPPTR(ss,ix);
15367 TOPPTR(nss,ix) = pv_dup_inc(c);
15369 case SAVEt_FREERCPV:
15370 c = (char *)POPPTR(ss,ix);
15371 TOPPTR(nss,ix) = rcpv_copy(c);
15373 case SAVEt_STACK_POS: /* Position on Perl stack */
15375 TOPINT(nss,ix) = i;
15377 case SAVEt_DESTRUCTOR:
15378 ptr = POPPTR(ss,ix);
15379 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
15380 dptr = POPDPTR(ss,ix);
15381 TOPDPTR(nss,ix) = DPTR2FPTR(void (*)(void*),
15382 any_dup(FPTR2DPTR(void *, dptr),
15385 case SAVEt_DESTRUCTOR_X:
15386 ptr = POPPTR(ss,ix);
15387 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
15388 dxptr = POPDXPTR(ss,ix);
15389 TOPDXPTR(nss,ix) = DPTR2FPTR(void (*)(pTHX_ void*),
15390 any_dup(FPTR2DPTR(void *, dxptr),
15393 case SAVEt_REGCONTEXT:
15395 ix -= uv >> SAVE_TIGHT_SHIFT;
15397 case SAVEt_AELEM: /* array element */
15398 sv = (const SV *)POPPTR(ss,ix);
15399 TOPPTR(nss,ix) = SvREFCNT_inc(sv_dup_inc(sv, param));
15401 TOPIV(nss,ix) = iv;
15402 av = (const AV *)POPPTR(ss,ix);
15403 TOPPTR(nss,ix) = av_dup_inc(av, param);
15406 ptr = POPPTR(ss,ix);
15407 TOPPTR(nss,ix) = ptr;
15409 case SAVEt_HINTS_HH:
15410 hv = (const HV *)POPPTR(ss,ix);
15411 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
15414 ptr = POPPTR(ss,ix);
15415 ptr = cophh_copy((COPHH*)ptr);
15416 TOPPTR(nss,ix) = ptr;
15418 TOPINT(nss,ix) = i;
15420 case SAVEt_PADSV_AND_MORTALIZE:
15421 longval = (long)POPLONG(ss,ix);
15422 TOPLONG(nss,ix) = longval;
15423 ptr = POPPTR(ss,ix);
15424 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15425 sv = (const SV *)POPPTR(ss,ix);
15426 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15428 case SAVEt_SET_SVFLAGS:
15430 TOPINT(nss,ix) = i;
15432 TOPINT(nss,ix) = i;
15433 sv = (const SV *)POPPTR(ss,ix);
15434 TOPPTR(nss,ix) = sv_dup(sv, param);
15436 case SAVEt_CURCOP_WARNINGS:
15438 case SAVEt_COMPILE_WARNINGS:
15439 ptr = POPPTR(ss,ix);
15440 TOPPTR(nss,ix) = DUP_WARNINGS((char*)ptr);
15443 ptr = POPPTR(ss,ix);
15444 TOPPTR(nss,ix) = parser_dup((const yy_parser*)ptr, param);
15448 "panic: ss_dup inconsistency (%" IVdf ")", (IV) type);
15456 /* if sv is a stash, call $class->CLONE_SKIP(), and set the SVphv_CLONEABLE
15457 * flag to the result. This is done for each stash before cloning starts,
15458 * so we know which stashes want their objects cloned */
15461 do_mark_cloneable_stash(pTHX_ SV *const sv)
15463 const HEK * const hvname = HvNAME_HEK((const HV *)sv);
15465 GV* const cloner = gv_fetchmethod_autoload(MUTABLE_HV(sv), "CLONE_SKIP", 0);
15466 SvFLAGS(sv) |= SVphv_CLONEABLE; /* clone objects by default */
15467 if (cloner && GvCV(cloner)) {
15474 mXPUSHs(newSVhek(hvname));
15476 call_sv(MUTABLE_SV(GvCV(cloner)), G_SCALAR);
15483 SvFLAGS(sv) &= ~SVphv_CLONEABLE;
15491 =for apidoc perl_clone
15493 Create and return a new interpreter by cloning the current one.
15495 C<perl_clone> takes these flags as parameters:
15497 C<CLONEf_COPY_STACKS> - is used to, well, copy the stacks also,
15498 without it we only clone the data and zero the stacks,
15499 with it we copy the stacks and the new perl interpreter is
15500 ready to run at the exact same point as the previous one.
15501 The pseudo-fork code uses C<COPY_STACKS> while the
15502 threads->create doesn't.
15504 C<CLONEf_KEEP_PTR_TABLE> -
15505 C<perl_clone> keeps a ptr_table with the pointer of the old
15506 variable as a key and the new variable as a value,
15507 this allows it to check if something has been cloned and not
15508 clone it again, but rather just use the value and increase the
15510 If C<KEEP_PTR_TABLE> is not set then C<perl_clone> will kill the ptr_table
15511 using the function S<C<ptr_table_free(PL_ptr_table); PL_ptr_table = NULL;>>.
15512 A reason to keep it around is if you want to dup some of your own
15513 variables which are outside the graph that perl scans.
15515 C<CLONEf_CLONE_HOST> -
15516 This is a win32 thing, it is ignored on unix, it tells perl's
15517 win32host code (which is c++) to clone itself, this is needed on
15518 win32 if you want to run two threads at the same time,
15519 if you just want to do some stuff in a separate perl interpreter
15520 and then throw it away and return to the original one,
15521 you don't need to do anything.
15526 /* XXX the above needs expanding by someone who actually understands it ! */
15527 EXTERN_C PerlInterpreter *
15528 perl_clone_host(PerlInterpreter* proto_perl, UV flags);
15531 perl_clone(PerlInterpreter *proto_perl, UV flags)
15533 #ifdef PERL_IMPLICIT_SYS
15535 PERL_ARGS_ASSERT_PERL_CLONE;
15537 /* perlhost.h so we need to call into it
15538 to clone the host, CPerlHost should have a c interface, sky */
15540 #ifndef __amigaos4__
15541 if (flags & CLONEf_CLONE_HOST) {
15542 return perl_clone_host(proto_perl,flags);
15545 return perl_clone_using(proto_perl, flags,
15547 proto_perl->IMemShared,
15548 proto_perl->IMemParse,
15550 proto_perl->IStdIO,
15554 proto_perl->IProc);
15558 perl_clone_using(PerlInterpreter *proto_perl, UV flags,
15559 struct IPerlMem* ipM, struct IPerlMem* ipMS,
15560 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
15561 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
15562 struct IPerlDir* ipD, struct IPerlSock* ipS,
15563 struct IPerlProc* ipP)
15565 /* XXX many of the string copies here can be optimized if they're
15566 * constants; they need to be allocated as common memory and just
15567 * their pointers copied. */
15570 CLONE_PARAMS clone_params;
15571 CLONE_PARAMS* const param = &clone_params;
15573 PerlInterpreter * const my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
15575 PERL_ARGS_ASSERT_PERL_CLONE_USING;
15576 #else /* !PERL_IMPLICIT_SYS */
15578 CLONE_PARAMS clone_params;
15579 CLONE_PARAMS* param = &clone_params;
15580 PerlInterpreter * const my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
15582 PERL_ARGS_ASSERT_PERL_CLONE;
15583 #endif /* PERL_IMPLICIT_SYS */
15585 /* for each stash, determine whether its objects should be cloned */
15586 S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
15587 my_perl->Iphase = PERL_PHASE_CONSTRUCT;
15588 PERL_SET_THX(my_perl);
15591 PoisonNew(my_perl, 1, PerlInterpreter);
15594 PL_defstash = NULL; /* may be used by perl malloc() */
15597 PL_scopestack_name = 0;
15599 PL_savestack_ix = 0;
15600 PL_savestack_max = -1;
15601 PL_sig_pending = 0;
15603 PL_eval_begin_nest_depth = proto_perl->Ieval_begin_nest_depth;
15604 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
15605 Zero(&PL_padname_undef, 1, PADNAME);
15606 Zero(&PL_padname_const, 1, PADNAME);
15607 # ifdef DEBUG_LEAKING_SCALARS
15608 PL_sv_serial = (((UV)my_perl >> 2) & 0xfff) * 1000000;
15610 # ifdef PERL_TRACE_OPS
15611 Zero(PL_op_exec_cnt, OP_max+2, UV);
15613 #else /* !DEBUGGING */
15614 Zero(my_perl, 1, PerlInterpreter);
15615 #endif /* DEBUGGING */
15617 #ifdef PERL_IMPLICIT_SYS
15618 /* host pointers */
15620 PL_MemShared = ipMS;
15621 PL_MemParse = ipMP;
15628 #endif /* PERL_IMPLICIT_SYS */
15631 param->flags = flags;
15632 /* Nothing in the core code uses this, but we make it available to
15633 extensions (using mg_dup). */
15634 param->proto_perl = proto_perl;
15635 /* Likely nothing will use this, but it is initialised to be consistent
15636 with Perl_clone_params_new(). */
15637 param->new_perl = my_perl;
15638 param->unreferenced = NULL;
15641 INIT_TRACK_MEMPOOL(my_perl->Imemory_debug_header, my_perl);
15643 PL_body_arenas = NULL;
15644 Zero(&PL_body_roots, 1, PL_body_roots);
15648 PL_sv_arenaroot = NULL;
15650 PL_debug = proto_perl->Idebug;
15652 /* dbargs array probably holds garbage */
15655 PL_compiling = proto_perl->Icompiling;
15657 /* pseudo environmental stuff */
15658 PL_origargc = proto_perl->Iorigargc;
15659 PL_origargv = proto_perl->Iorigargv;
15661 #ifndef NO_TAINT_SUPPORT
15662 /* Set tainting stuff before PerlIO_debug can possibly get called */
15663 PL_tainting = proto_perl->Itainting;
15664 PL_taint_warn = proto_perl->Itaint_warn;
15666 PL_tainting = FALSE;
15667 PL_taint_warn = FALSE;
15670 PL_minus_c = proto_perl->Iminus_c;
15672 PL_localpatches = proto_perl->Ilocalpatches;
15673 PL_splitstr = SAVEPV(proto_perl->Isplitstr);
15674 PL_minus_n = proto_perl->Iminus_n;
15675 PL_minus_p = proto_perl->Iminus_p;
15676 PL_minus_l = proto_perl->Iminus_l;
15677 PL_minus_a = proto_perl->Iminus_a;
15678 PL_minus_E = proto_perl->Iminus_E;
15679 PL_minus_F = proto_perl->Iminus_F;
15680 PL_doswitches = proto_perl->Idoswitches;
15681 PL_dowarn = proto_perl->Idowarn;
15682 #ifdef PERL_SAWAMPERSAND
15683 PL_sawampersand = proto_perl->Isawampersand;
15685 PL_unsafe = proto_perl->Iunsafe;
15686 PL_perldb = proto_perl->Iperldb;
15687 PL_perl_destruct_level = proto_perl->Iperl_destruct_level;
15688 PL_exit_flags = proto_perl->Iexit_flags;
15690 /* XXX time(&PL_basetime) when asked for? */
15691 PL_basetime = proto_perl->Ibasetime;
15693 PL_maxsysfd = proto_perl->Imaxsysfd;
15694 PL_statusvalue = proto_perl->Istatusvalue;
15696 PL_statusvalue_vms = proto_perl->Istatusvalue_vms;
15698 PL_statusvalue_posix = proto_perl->Istatusvalue_posix;
15701 /* RE engine related */
15702 PL_regmatch_slab = NULL;
15703 PL_reg_curpm = NULL;
15705 PL_sub_generation = proto_perl->Isub_generation;
15707 /* funky return mechanisms */
15708 PL_forkprocess = proto_perl->Iforkprocess;
15710 /* internal state */
15711 PL_main_start = proto_perl->Imain_start;
15712 PL_eval_root = proto_perl->Ieval_root;
15713 PL_eval_start = proto_perl->Ieval_start;
15715 PL_filemode = proto_perl->Ifilemode;
15716 PL_lastfd = proto_perl->Ilastfd;
15717 PL_oldname = proto_perl->Ioldname; /* XXX not quite right */
15718 PL_gensym = proto_perl->Igensym;
15720 PL_laststatval = proto_perl->Ilaststatval;
15721 PL_laststype = proto_perl->Ilaststype;
15724 PL_profiledata = NULL;
15726 PL_generation = proto_perl->Igeneration;
15728 PL_in_clean_objs = proto_perl->Iin_clean_objs;
15729 PL_in_clean_all = proto_perl->Iin_clean_all;
15731 PL_delaymagic_uid = proto_perl->Idelaymagic_uid;
15732 PL_delaymagic_euid = proto_perl->Idelaymagic_euid;
15733 PL_delaymagic_gid = proto_perl->Idelaymagic_gid;
15734 PL_delaymagic_egid = proto_perl->Idelaymagic_egid;
15735 PL_nomemok = proto_perl->Inomemok;
15736 PL_an = proto_perl->Ian;
15737 PL_evalseq = proto_perl->Ievalseq;
15738 PL_origalen = proto_perl->Iorigalen;
15740 PL_sighandlerp = proto_perl->Isighandlerp;
15741 PL_sighandler1p = proto_perl->Isighandler1p;
15742 PL_sighandler3p = proto_perl->Isighandler3p;
15744 PL_runops = proto_perl->Irunops;
15746 PL_subline = proto_perl->Isubline;
15748 PL_cv_has_eval = proto_perl->Icv_has_eval;
15749 /* Unicode features (see perlrun/-C) */
15750 PL_unicode = proto_perl->Iunicode;
15752 /* Pre-5.8 signals control */
15753 PL_signals = proto_perl->Isignals;
15755 /* times() ticks per second */
15756 PL_clocktick = proto_perl->Iclocktick;
15758 /* Recursion stopper for PerlIO_find_layer */
15759 PL_in_load_module = proto_perl->Iin_load_module;
15761 /* Not really needed/useful since the reenrant_retint is "volatile",
15762 * but do it for consistency's sake. */
15763 PL_reentrant_retint = proto_perl->Ireentrant_retint;
15765 /* Hooks to shared SVs and locks. */
15766 PL_sharehook = proto_perl->Isharehook;
15767 PL_lockhook = proto_perl->Ilockhook;
15768 PL_unlockhook = proto_perl->Iunlockhook;
15769 PL_threadhook = proto_perl->Ithreadhook;
15770 PL_destroyhook = proto_perl->Idestroyhook;
15771 PL_signalhook = proto_perl->Isignalhook;
15773 PL_globhook = proto_perl->Iglobhook;
15775 PL_srand_called = proto_perl->Isrand_called;
15776 Copy(&(proto_perl->Irandom_state), &PL_random_state, 1, PL_RANDOM_STATE_TYPE);
15777 PL_srand_override = proto_perl->Isrand_override;
15778 PL_srand_override_next = proto_perl->Isrand_override_next;
15780 if (flags & CLONEf_COPY_STACKS) {
15781 /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */
15782 PL_tmps_ix = proto_perl->Itmps_ix;
15783 PL_tmps_max = proto_perl->Itmps_max;
15784 PL_tmps_floor = proto_perl->Itmps_floor;
15786 /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
15787 * NOTE: unlike the others! */
15788 PL_scopestack_ix = proto_perl->Iscopestack_ix;
15789 PL_scopestack_max = proto_perl->Iscopestack_max;
15791 /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix]
15792 * NOTE: unlike the others! */
15793 PL_savestack_ix = proto_perl->Isavestack_ix;
15794 PL_savestack_max = proto_perl->Isavestack_max;
15797 PL_start_env = proto_perl->Istart_env; /* XXXXXX */
15798 PL_top_env = &PL_start_env;
15800 PL_op = proto_perl->Iop;
15803 PL_Xpv = (XPV*)NULL;
15804 my_perl->Ina = proto_perl->Ina;
15806 PL_statcache = proto_perl->Istatcache;
15808 #ifndef NO_TAINT_SUPPORT
15809 PL_tainted = proto_perl->Itainted;
15811 PL_tainted = FALSE;
15813 PL_curpm = proto_perl->Icurpm; /* XXX No PMOP ref count */
15815 PL_chopset = proto_perl->Ichopset; /* XXX never deallocated */
15817 PL_restartjmpenv = proto_perl->Irestartjmpenv;
15818 PL_restartop = proto_perl->Irestartop;
15819 PL_in_eval = proto_perl->Iin_eval;
15820 PL_delaymagic = proto_perl->Idelaymagic;
15821 PL_phase = proto_perl->Iphase;
15822 PL_localizing = proto_perl->Ilocalizing;
15824 PL_hv_fetch_ent_mh = NULL;
15825 PL_modcount = proto_perl->Imodcount;
15826 PL_lastgotoprobe = NULL;
15827 PL_dumpindent = proto_perl->Idumpindent;
15829 PL_efloatbuf = NULL; /* reinits on demand */
15830 PL_efloatsize = 0; /* reinits on demand */
15834 PL_colorset = 0; /* reinits PL_colors[] */
15835 /*PL_colors[6] = {0,0,0,0,0,0};*/
15837 /* Pluggable optimizer */
15838 PL_peepp = proto_perl->Ipeepp;
15839 PL_rpeepp = proto_perl->Irpeepp;
15840 /* op_free() hook */
15841 PL_opfreehook = proto_perl->Iopfreehook;
15843 # ifdef PERL_MEM_LOG
15844 Zero(PL_mem_log, sizeof(PL_mem_log), char);
15847 #ifdef USE_REENTRANT_API
15848 /* XXX: things like -Dm will segfault here in perlio, but doing
15849 * PERL_SET_CONTEXT(proto_perl);
15850 * breaks too many other things
15852 Perl_reentrant_init(aTHX);
15855 /* create SV map for pointer relocation */
15856 PL_ptr_table = ptr_table_new();
15858 /* initialize these special pointers as early as possible */
15860 ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef);
15861 ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no);
15862 ptr_table_store(PL_ptr_table, &proto_perl->Isv_zero, &PL_sv_zero);
15863 ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes);
15864 ptr_table_store(PL_ptr_table, &proto_perl->Ipadname_const,
15865 &PL_padname_const);
15867 /* create (a non-shared!) shared string table */
15868 PL_strtab = newHV();
15869 HvSHAREKEYS_off(PL_strtab);
15870 hv_ksplit(PL_strtab, HvTOTALKEYS(proto_perl->Istrtab));
15871 ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab);
15873 Zero(PL_sv_consts, SV_CONSTS_COUNT, SV*);
15875 PL_compiling.cop_file = rcpv_copy(proto_perl->Icompiling.cop_file);
15877 ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling);
15878 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
15879 CopHINTHASH_set(&PL_compiling, cophh_copy(CopHINTHASH_get(&PL_compiling)));
15880 PL_curcop = (COP*)any_dup(proto_perl->Icurcop, proto_perl);
15882 param->stashes = newAV(); /* Setup array of objects to call clone on */
15883 /* This makes no difference to the implementation, as it always pushes
15884 and shifts pointers to other SVs without changing their reference
15885 count, with the array becoming empty before it is freed. However, it
15886 makes it conceptually clear what is going on, and will avoid some
15887 work inside av.c, filling slots between AvFILL() and AvMAX() with
15888 &PL_sv_undef, and SvREFCNT_dec()ing those. */
15889 AvREAL_off(param->stashes);
15891 if (!(flags & CLONEf_COPY_STACKS)) {
15892 param->unreferenced = newAV();
15895 #ifdef PERLIO_LAYERS
15896 /* Clone PerlIO tables as soon as we can handle general xx_dup() */
15897 PerlIO_clone(aTHX_ proto_perl, param);
15900 PL_envgv = gv_dup_inc(proto_perl->Ienvgv, param);
15901 PL_incgv = gv_dup_inc(proto_perl->Iincgv, param);
15902 PL_hintgv = gv_dup_inc(proto_perl->Ihintgv, param);
15903 PL_origfilename = SAVEPV(proto_perl->Iorigfilename);
15904 PL_xsubfilename = proto_perl->Ixsubfilename;
15905 PL_diehook = sv_dup_inc(proto_perl->Idiehook, param);
15906 PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook, param);
15908 PL_hook__require__before = sv_dup_inc(proto_perl->Ihook__require__before, param);
15909 PL_hook__require__after = sv_dup_inc(proto_perl->Ihook__require__after, param);
15912 PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel, param);
15913 PL_inplace = SAVEPV(proto_perl->Iinplace);
15914 PL_e_script = sv_dup_inc(proto_perl->Ie_script, param);
15916 /* magical thingies */
15918 SvPVCLEAR(PERL_DEBUG_PAD(0)); /* For regex debugging. */
15919 SvPVCLEAR(PERL_DEBUG_PAD(1)); /* ext/re needs these */
15920 SvPVCLEAR(PERL_DEBUG_PAD(2)); /* even without DEBUGGING. */
15923 /* Clone the regex array */
15924 /* ORANGE FIXME for plugins, probably in the SV dup code.
15925 newSViv(PTR2IV(CALLREGDUPE(
15926 INT2PTR(REGEXP *, SvIVX(regex)), param))))
15928 PL_regex_padav = av_dup_inc(proto_perl->Iregex_padav, param);
15929 PL_regex_pad = AvARRAY(PL_regex_padav);
15931 PL_stashpadmax = proto_perl->Istashpadmax;
15932 PL_stashpadix = proto_perl->Istashpadix ;
15933 Newx(PL_stashpad, PL_stashpadmax, HV *);
15936 for (; o < PL_stashpadmax; ++o)
15937 PL_stashpad[o] = hv_dup(proto_perl->Istashpad[o], param);
15940 /* shortcuts to various I/O objects */
15941 PL_ofsgv = gv_dup_inc(proto_perl->Iofsgv, param);
15942 PL_stdingv = gv_dup(proto_perl->Istdingv, param);
15943 PL_stderrgv = gv_dup(proto_perl->Istderrgv, param);
15944 PL_defgv = gv_dup(proto_perl->Idefgv, param);
15945 PL_argvgv = gv_dup_inc(proto_perl->Iargvgv, param);
15946 PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv, param);
15947 PL_argvout_stack = av_dup_inc(proto_perl->Iargvout_stack, param);
15949 /* shortcuts to regexp stuff */
15950 PL_replgv = gv_dup_inc(proto_perl->Ireplgv, param);
15952 /* shortcuts to misc objects */
15953 PL_errgv = gv_dup(proto_perl->Ierrgv, param);
15955 /* shortcuts to debugging objects */
15956 PL_DBgv = gv_dup_inc(proto_perl->IDBgv, param);
15957 PL_DBline = gv_dup_inc(proto_perl->IDBline, param);
15958 PL_DBsub = gv_dup_inc(proto_perl->IDBsub, param);
15959 PL_DBsingle = sv_dup(proto_perl->IDBsingle, param);
15960 PL_DBtrace = sv_dup(proto_perl->IDBtrace, param);
15961 PL_DBsignal = sv_dup(proto_perl->IDBsignal, param);
15962 Copy(proto_perl->IDBcontrol, PL_DBcontrol, DBVARMG_COUNT, IV);
15964 /* symbol tables */
15965 PL_defstash = hv_dup_inc(proto_perl->Idefstash, param);
15966 PL_curstash = hv_dup_inc(proto_perl->Icurstash, param);
15967 PL_debstash = hv_dup(proto_perl->Idebstash, param);
15968 PL_globalstash = hv_dup(proto_perl->Iglobalstash, param);
15969 PL_curstname = sv_dup_inc(proto_perl->Icurstname, param);
15971 PL_beginav = av_dup_inc(proto_perl->Ibeginav, param);
15972 PL_beginav_save = av_dup_inc(proto_perl->Ibeginav_save, param);
15973 PL_checkav_save = av_dup_inc(proto_perl->Icheckav_save, param);
15974 PL_unitcheckav = av_dup_inc(proto_perl->Iunitcheckav, param);
15975 PL_unitcheckav_save = av_dup_inc(proto_perl->Iunitcheckav_save, param);
15976 PL_endav = av_dup_inc(proto_perl->Iendav, param);
15977 PL_checkav = av_dup_inc(proto_perl->Icheckav, param);
15978 PL_initav = av_dup_inc(proto_perl->Iinitav, param);
15979 PL_savebegin = proto_perl->Isavebegin;
15981 PL_isarev = hv_dup_inc(proto_perl->Iisarev, param);
15983 /* subprocess state */
15984 PL_fdpid = av_dup_inc(proto_perl->Ifdpid, param);
15986 if (proto_perl->Iop_mask)
15987 PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo);
15990 /* PL_asserting = proto_perl->Iasserting; */
15992 /* current interpreter roots */
15993 PL_main_cv = cv_dup_inc(proto_perl->Imain_cv, param);
15995 PL_main_root = OpREFCNT_inc(proto_perl->Imain_root);
15998 /* runtime control stuff */
15999 PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl);
16001 PL_preambleav = av_dup_inc(proto_perl->Ipreambleav, param);
16003 PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv, param);
16005 /* interpreter atexit processing */
16006 PL_exitlistlen = proto_perl->Iexitlistlen;
16007 if (PL_exitlistlen) {
16008 Newx(PL_exitlist, PL_exitlistlen, PerlExitListEntry);
16009 Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
16012 PL_exitlist = (PerlExitListEntry*)NULL;
16014 PL_my_cxt_size = proto_perl->Imy_cxt_size;
16015 if (PL_my_cxt_size) {
16016 Newx(PL_my_cxt_list, PL_my_cxt_size, void *);
16017 Copy(proto_perl->Imy_cxt_list, PL_my_cxt_list, PL_my_cxt_size, void *);
16020 PL_my_cxt_list = (void**)NULL;
16022 PL_modglobal = hv_dup_inc(proto_perl->Imodglobal, param);
16023 PL_custom_op_names = hv_dup_inc(proto_perl->Icustom_op_names,param);
16024 PL_custom_op_descs = hv_dup_inc(proto_perl->Icustom_op_descs,param);
16025 PL_custom_ops = hv_dup_inc(proto_perl->Icustom_ops, param);
16027 PL_compcv = cv_dup(proto_perl->Icompcv, param);
16029 PAD_CLONE_VARS(proto_perl, param);
16031 #ifdef HAVE_INTERP_INTERN
16032 sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern);
16035 PL_DBcv = cv_dup(proto_perl->IDBcv, param);
16037 #ifdef PERL_USES_PL_PIDSTATUS
16038 PL_pidstatus = newHV(); /* XXX flag for cloning? */
16040 PL_osname = SAVEPV(proto_perl->Iosname);
16041 PL_parser = parser_dup(proto_perl->Iparser, param);
16043 /* XXX this only works if the saved cop has already been cloned */
16044 if (proto_perl->Iparser) {
16045 PL_parser->saved_curcop = (COP*)any_dup(
16046 proto_perl->Iparser->saved_curcop,
16050 PL_subname = sv_dup_inc(proto_perl->Isubname, param);
16052 #ifdef USE_PL_CURLOCALES
16053 for (i = 0; i < (int) C_ARRAY_LENGTH(PL_curlocales); i++) {
16054 PL_curlocales[i] = SAVEPV("C");
16057 #ifdef USE_PL_CUR_LC_ALL
16058 PL_cur_LC_ALL = SAVEPV("C");
16060 #ifdef USE_LOCALE_CTYPE
16061 Copy(PL_fold, PL_fold_locale, 256, U8);
16063 /* Should we warn if uses locale? */
16064 PL_ctype_name = SAVEPV("C");
16065 PL_warn_locale = sv_dup_inc(proto_perl->Iwarn_locale, param);
16066 PL_in_utf8_CTYPE_locale = false;
16067 PL_in_utf8_turkic_locale = false;
16070 /* Did the locale setup indicate UTF-8? */
16071 PL_utf8locale = false;
16073 #ifdef USE_LOCALE_COLLATE
16074 PL_in_utf8_COLLATE_locale = false;
16075 PL_collation_name = SAVEPV("C");
16076 PL_collation_ix = proto_perl->Icollation_ix;
16077 PL_collation_standard = true;
16078 PL_collxfrm_base = 0;
16079 PL_collxfrm_mult = 0;
16080 PL_strxfrm_max_cp = 0;
16081 PL_strxfrm_is_behaved = proto_perl->Istrxfrm_is_behaved;
16082 PL_strxfrm_NUL_replacement = '\0';
16083 #endif /* USE_LOCALE_COLLATE */
16085 #ifdef USE_LOCALE_THREADS
16086 assert(PL_locale_mutex_depth <= 0);
16087 PL_locale_mutex_depth = 0;
16090 #ifdef USE_LOCALE_NUMERIC
16091 PL_numeric_name = SAVEPV("C");
16092 PL_numeric_radix_sv = newSVpvs(".");
16093 PL_underlying_radix_sv = newSVpvs(".");
16094 PL_numeric_standard = true;
16095 PL_numeric_underlying = true;
16096 PL_numeric_underlying_is_standard = true;
16098 # if defined(USE_POSIX_2008_LOCALE)
16099 PL_underlying_numeric_obj = NULL;
16101 #endif /* !USE_LOCALE_NUMERIC */
16102 #if defined(USE_POSIX_2008_LOCALE)
16103 PL_scratch_locale_obj = NULL;
16104 PL_cur_locale_obj = PL_C_locale_obj;
16108 PL_mbrlen_ps = proto_perl->Imbrlen_ps;
16111 PL_mbrtowc_ps = proto_perl->Imbrtowc_ps;
16114 PL_wcrtomb_ps = proto_perl->Iwcrtomb_ps;
16117 PL_langinfo_buf = NULL;
16118 PL_langinfo_bufsize = 0;
16120 PL_setlocale_buf = NULL;
16121 PL_setlocale_bufsize = 0;
16123 PL_stdize_locale_buf = NULL;
16124 PL_stdize_locale_bufsize = 0;
16126 /* Unicode inversion lists */
16128 PL_AboveLatin1 = sv_dup_inc(proto_perl->IAboveLatin1, param);
16129 PL_Assigned_invlist = sv_dup_inc(proto_perl->IAssigned_invlist, param);
16130 PL_GCB_invlist = sv_dup_inc(proto_perl->IGCB_invlist, param);
16131 PL_HasMultiCharFold = sv_dup_inc(proto_perl->IHasMultiCharFold, param);
16132 PL_InMultiCharFold = sv_dup_inc(proto_perl->IInMultiCharFold, param);
16133 PL_Latin1 = sv_dup_inc(proto_perl->ILatin1, param);
16134 PL_LB_invlist = sv_dup_inc(proto_perl->ILB_invlist, param);
16135 PL_SB_invlist = sv_dup_inc(proto_perl->ISB_invlist, param);
16136 PL_SCX_invlist = sv_dup_inc(proto_perl->ISCX_invlist, param);
16137 PL_UpperLatin1 = sv_dup_inc(proto_perl->IUpperLatin1, param);
16138 PL_in_some_fold = sv_dup_inc(proto_perl->Iin_some_fold, param);
16139 PL_utf8_foldclosures = sv_dup_inc(proto_perl->Iutf8_foldclosures, param);
16140 PL_utf8_idcont = sv_dup_inc(proto_perl->Iutf8_idcont, param);
16141 PL_utf8_idstart = sv_dup_inc(proto_perl->Iutf8_idstart, param);
16142 PL_utf8_perl_idcont = sv_dup_inc(proto_perl->Iutf8_perl_idcont, param);
16143 PL_utf8_perl_idstart = sv_dup_inc(proto_perl->Iutf8_perl_idstart, param);
16144 PL_utf8_xidcont = sv_dup_inc(proto_perl->Iutf8_xidcont, param);
16145 PL_utf8_xidstart = sv_dup_inc(proto_perl->Iutf8_xidstart, param);
16146 PL_WB_invlist = sv_dup_inc(proto_perl->IWB_invlist, param);
16147 for (i = 0; i < POSIX_CC_COUNT; i++) {
16148 PL_XPosix_ptrs[i] = sv_dup_inc(proto_perl->IXPosix_ptrs[i], param);
16149 if (i != CC_CASED_ && i != CC_VERTSPACE_) {
16150 PL_Posix_ptrs[i] = sv_dup_inc(proto_perl->IPosix_ptrs[i], param);
16153 PL_Posix_ptrs[CC_CASED_] = PL_Posix_ptrs[CC_ALPHA_];
16154 PL_Posix_ptrs[CC_VERTSPACE_] = NULL;
16156 PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper, param);
16157 PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle, param);
16158 PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower, param);
16159 PL_utf8_tofold = sv_dup_inc(proto_perl->Iutf8_tofold, param);
16160 PL_utf8_tosimplefold = sv_dup_inc(proto_perl->Iutf8_tosimplefold, param);
16161 PL_utf8_charname_begin = sv_dup_inc(proto_perl->Iutf8_charname_begin, param);
16162 PL_utf8_charname_continue = sv_dup_inc(proto_perl->Iutf8_charname_continue, param);
16163 PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark, param);
16164 PL_InBitmap = sv_dup_inc(proto_perl->IInBitmap, param);
16165 PL_CCC_non0_non230 = sv_dup_inc(proto_perl->ICCC_non0_non230, param);
16166 PL_Private_Use = sv_dup_inc(proto_perl->IPrivate_Use, param);
16169 PL_seen_deprecated_macro = hv_dup_inc(proto_perl->Iseen_deprecated_macro, param);
16172 if (proto_perl->Ipsig_pend) {
16173 Newxz(PL_psig_pend, SIG_SIZE, int);
16176 PL_psig_pend = (int*)NULL;
16179 if (proto_perl->Ipsig_name) {
16180 Newx(PL_psig_name, 2 * SIG_SIZE, SV*);
16181 sv_dup_inc_multiple(proto_perl->Ipsig_name, PL_psig_name, 2 * SIG_SIZE,
16183 PL_psig_ptr = PL_psig_name + SIG_SIZE;
16186 PL_psig_ptr = (SV**)NULL;
16187 PL_psig_name = (SV**)NULL;
16190 if (flags & CLONEf_COPY_STACKS) {
16191 Newx(PL_tmps_stack, PL_tmps_max, SV*);
16192 sv_dup_inc_multiple(proto_perl->Itmps_stack, PL_tmps_stack,
16193 PL_tmps_ix+1, param);
16195 /* next PUSHMARK() sets *(PL_markstack_ptr+1) */
16196 i = proto_perl->Imarkstack_max - proto_perl->Imarkstack;
16197 Newx(PL_markstack, i, I32);
16198 PL_markstack_max = PL_markstack + (proto_perl->Imarkstack_max
16199 - proto_perl->Imarkstack);
16200 PL_markstack_ptr = PL_markstack + (proto_perl->Imarkstack_ptr
16201 - proto_perl->Imarkstack);
16202 Copy(proto_perl->Imarkstack, PL_markstack,
16203 PL_markstack_ptr - PL_markstack + 1, I32);
16205 /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
16206 * NOTE: unlike the others! */
16207 Newx(PL_scopestack, PL_scopestack_max, I32);
16208 Copy(proto_perl->Iscopestack, PL_scopestack, PL_scopestack_ix, I32);
16211 Newx(PL_scopestack_name, PL_scopestack_max, const char *);
16212 Copy(proto_perl->Iscopestack_name, PL_scopestack_name, PL_scopestack_ix, const char *);
16214 /* reset stack AV to correct length before its duped via
16215 * PL_curstackinfo */
16216 AvFILLp(proto_perl->Icurstack) =
16217 proto_perl->Istack_sp - proto_perl->Istack_base;
16219 /* NOTE: si_dup() looks at PL_markstack */
16220 PL_curstackinfo = si_dup(proto_perl->Icurstackinfo, param);
16222 /* PL_curstack = PL_curstackinfo->si_stack; */
16223 PL_curstack = av_dup(proto_perl->Icurstack, param);
16224 PL_mainstack = av_dup(proto_perl->Imainstack, param);
16226 /* next PUSHs() etc. set *(PL_stack_sp+1) */
16227 PL_stack_base = AvARRAY(PL_curstack);
16228 PL_stack_sp = PL_stack_base + (proto_perl->Istack_sp
16229 - proto_perl->Istack_base);
16230 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
16232 /*Newxz(PL_savestack, PL_savestack_max, ANY);*/
16233 PL_savestack = ss_dup(proto_perl, param);
16237 ENTER; /* perl_destruct() wants to LEAVE; */
16240 PL_statgv = gv_dup(proto_perl->Istatgv, param);
16241 PL_statname = sv_dup_inc(proto_perl->Istatname, param);
16243 PL_rs = sv_dup_inc(proto_perl->Irs, param);
16244 PL_last_in_gv = gv_dup(proto_perl->Ilast_in_gv, param);
16245 PL_defoutgv = gv_dup_inc(proto_perl->Idefoutgv, param);
16246 PL_toptarget = sv_dup_inc(proto_perl->Itoptarget, param);
16247 PL_bodytarget = sv_dup_inc(proto_perl->Ibodytarget, param);
16248 PL_formtarget = sv_dup(proto_perl->Iformtarget, param);
16250 PL_errors = sv_dup_inc(proto_perl->Ierrors, param);
16252 PL_sortcop = (OP*)any_dup(proto_perl->Isortcop, proto_perl);
16253 PL_firstgv = gv_dup_inc(proto_perl->Ifirstgv, param);
16254 PL_secondgv = gv_dup_inc(proto_perl->Isecondgv, param);
16256 PL_stashcache = newHV();
16258 PL_watchaddr = (char **) ptr_table_fetch(PL_ptr_table,
16259 proto_perl->Iwatchaddr);
16260 PL_watchok = PL_watchaddr ? * PL_watchaddr : NULL;
16261 if (PL_debug && PL_watchaddr) {
16262 PerlIO_printf(Perl_debug_log,
16263 "WATCHING: %" UVxf " cloned as %" UVxf " with value %" UVxf "\n",
16264 PTR2UV(proto_perl->Iwatchaddr), PTR2UV(PL_watchaddr),
16265 PTR2UV(PL_watchok));
16268 PL_registered_mros = hv_dup_inc(proto_perl->Iregistered_mros, param);
16269 PL_blockhooks = av_dup_inc(proto_perl->Iblockhooks, param);
16271 /* Call the ->CLONE method, if it exists, for each of the stashes
16272 identified by sv_dup() above.
16274 while(av_count(param->stashes) != 0) {
16275 HV* const stash = MUTABLE_HV(av_shift(param->stashes));
16276 GV* const cloner = gv_fetchmethod_autoload(stash, "CLONE", 0);
16277 if (cloner && GvCV(cloner)) {
16282 mXPUSHs(newSVhek(HvNAME_HEK(stash)));
16284 call_sv(MUTABLE_SV(GvCV(cloner)), G_DISCARD);
16290 if (!(flags & CLONEf_KEEP_PTR_TABLE)) {
16291 ptr_table_free(PL_ptr_table);
16292 PL_ptr_table = NULL;
16295 if (!(flags & CLONEf_COPY_STACKS)) {
16296 unreferenced_to_tmp_stack(param->unreferenced);
16299 SvREFCNT_dec(param->stashes);
16301 /* orphaned? eg threads->new inside BEGIN or use */
16302 if (PL_compcv && ! SvREFCNT(PL_compcv)) {
16303 SvREFCNT_inc_simple_void(PL_compcv);
16304 SAVEFREESV(PL_compcv);
16311 S_unreferenced_to_tmp_stack(pTHX_ AV *const unreferenced)
16313 PERL_ARGS_ASSERT_UNREFERENCED_TO_TMP_STACK;
16315 if (AvFILLp(unreferenced) > -1) {
16316 SV **svp = AvARRAY(unreferenced);
16317 SV **const last = svp + AvFILLp(unreferenced);
16321 if (SvREFCNT(*svp) == 1)
16323 } while (++svp <= last);
16325 EXTEND_MORTAL(count);
16326 svp = AvARRAY(unreferenced);
16329 if (SvREFCNT(*svp) == 1) {
16330 /* Our reference is the only one to this SV. This means that
16331 in this thread, the scalar effectively has a 0 reference.
16332 That doesn't work (cleanup never happens), so donate our
16333 reference to it onto the save stack. */
16334 PL_tmps_stack[++PL_tmps_ix] = *svp;
16336 /* As an optimisation, because we are already walking the
16337 entire array, instead of above doing either
16338 SvREFCNT_inc(*svp) or *svp = &PL_sv_undef, we can instead
16339 release our reference to the scalar, so that at the end of
16340 the array owns zero references to the scalars it happens to
16341 point to. We are effectively converting the array from
16342 AvREAL() on to AvREAL() off. This saves the av_clear()
16343 (triggered by the SvREFCNT_dec(unreferenced) below) from
16344 walking the array a second time. */
16345 SvREFCNT_dec(*svp);
16348 } while (++svp <= last);
16349 AvREAL_off(unreferenced);
16351 SvREFCNT_dec_NN(unreferenced);
16355 Perl_clone_params_del(CLONE_PARAMS *param)
16357 PerlInterpreter *const was = PERL_GET_THX;
16358 PerlInterpreter *const to = param->new_perl;
16361 PERL_ARGS_ASSERT_CLONE_PARAMS_DEL;
16367 SvREFCNT_dec(param->stashes);
16368 if (param->unreferenced)
16369 unreferenced_to_tmp_stack(param->unreferenced);
16379 Perl_clone_params_new(PerlInterpreter *const from, PerlInterpreter *const to)
16381 /* Need to play this game, as newAV() can call safesysmalloc(), and that
16382 does a dTHX; to get the context from thread local storage.
16383 FIXME - under PERL_CORE Newx(), Safefree() and friends should expand to
16384 a version that passes in my_perl. */
16385 PerlInterpreter *const was = PERL_GET_THX;
16386 CLONE_PARAMS *param;
16388 PERL_ARGS_ASSERT_CLONE_PARAMS_NEW;
16394 /* Given that we've set the context, we can do this unshared. */
16395 Newx(param, 1, CLONE_PARAMS);
16398 param->proto_perl = from;
16399 param->new_perl = to;
16400 param->stashes = (AV *)Perl_newSV_type(to, SVt_PVAV);
16401 AvREAL_off(param->stashes);
16402 param->unreferenced = (AV *)Perl_newSV_type(to, SVt_PVAV);
16410 #endif /* USE_ITHREADS */
16413 Perl_init_constants(pTHX)
16416 SvREFCNT(&PL_sv_undef) = SvREFCNT_IMMORTAL;
16417 SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVf_PROTECT|SVt_NULL;
16418 SvANY(&PL_sv_undef) = NULL;
16420 SvANY(&PL_sv_no) = new_XPVNV();
16421 SvREFCNT(&PL_sv_no) = SvREFCNT_IMMORTAL;
16422 SvFLAGS(&PL_sv_no) = SVt_PVNV|SVf_READONLY|SVf_PROTECT
16423 |SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
16424 |SVp_POK|SVf_POK|SVf_IsCOW|SVppv_STATIC;
16426 SvANY(&PL_sv_yes) = new_XPVNV();
16427 SvREFCNT(&PL_sv_yes) = SvREFCNT_IMMORTAL;
16428 SvFLAGS(&PL_sv_yes) = SVt_PVNV|SVf_READONLY|SVf_PROTECT
16429 |SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
16430 |SVp_POK|SVf_POK|SVf_IsCOW|SVppv_STATIC;
16432 SvANY(&PL_sv_zero) = new_XPVNV();
16433 SvREFCNT(&PL_sv_zero) = SvREFCNT_IMMORTAL;
16434 SvFLAGS(&PL_sv_zero) = SVt_PVNV|SVf_READONLY|SVf_PROTECT
16435 |SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
16439 SvPV_set(&PL_sv_no, (char*)PL_No);
16440 SvCUR_set(&PL_sv_no, 0);
16441 SvLEN_set(&PL_sv_no, 0);
16442 SvIV_set(&PL_sv_no, 0);
16443 SvNV_set(&PL_sv_no, 0);
16445 SvPV_set(&PL_sv_yes, (char*)PL_Yes);
16446 SvCUR_set(&PL_sv_yes, 1);
16447 SvLEN_set(&PL_sv_yes, 0);
16448 SvIV_set(&PL_sv_yes, 1);
16449 SvNV_set(&PL_sv_yes, 1);
16451 SvPV_set(&PL_sv_zero, (char*)PL_Zero);
16452 SvCUR_set(&PL_sv_zero, 1);
16453 SvLEN_set(&PL_sv_zero, 0);
16454 SvIV_set(&PL_sv_zero, 0);
16455 SvNV_set(&PL_sv_zero, 0);
16457 PadnamePV(&PL_padname_const) = (char *)PL_No;
16459 assert(SvIMMORTAL_INTERP(&PL_sv_yes));
16460 assert(SvIMMORTAL_INTERP(&PL_sv_undef));
16461 assert(SvIMMORTAL_INTERP(&PL_sv_no));
16462 assert(SvIMMORTAL_INTERP(&PL_sv_zero));
16464 assert(SvIMMORTAL(&PL_sv_yes));
16465 assert(SvIMMORTAL(&PL_sv_undef));
16466 assert(SvIMMORTAL(&PL_sv_no));
16467 assert(SvIMMORTAL(&PL_sv_zero));
16469 assert( SvIMMORTAL_TRUE(&PL_sv_yes));
16470 assert(!SvIMMORTAL_TRUE(&PL_sv_undef));
16471 assert(!SvIMMORTAL_TRUE(&PL_sv_no));
16472 assert(!SvIMMORTAL_TRUE(&PL_sv_zero));
16474 assert( SvTRUE_nomg_NN(&PL_sv_yes));
16475 assert(!SvTRUE_nomg_NN(&PL_sv_undef));
16476 assert(!SvTRUE_nomg_NN(&PL_sv_no));
16477 assert(!SvTRUE_nomg_NN(&PL_sv_zero));
16481 =for apidoc_section $unicode
16483 =for apidoc sv_recode_to_utf8
16485 C<encoding> is assumed to be an C<Encode> object, on entry the PV
16486 of C<sv> is assumed to be octets in that encoding, and C<sv>
16487 will be converted into Unicode (and UTF-8).
16489 If C<sv> already is UTF-8 (or if it is not C<POK>), or if C<encoding>
16490 is not a reference, nothing is done to C<sv>. If C<encoding> is not
16491 an C<Encode::XS> Encoding object, bad things will happen.
16492 (See L<encoding> and L<Encode>.)
16494 The PV of C<sv> is returned.
16499 Perl_sv_recode_to_utf8(pTHX_ SV *sv, SV *encoding)
16501 PERL_ARGS_ASSERT_SV_RECODE_TO_UTF8;
16503 if (SvPOK(sv) && !SvUTF8(sv) && !IN_BYTES && SvROK(encoding)) {
16512 if (SvPADTMP(nsv)) {
16513 nsv = sv_newmortal();
16514 SvSetSV_nosteal(nsv, sv);
16523 Passing sv_yes is wrong - it needs to be or'ed set of constants
16524 for Encode::XS, while UTf-8 decode (currently) assumes a true value means
16525 remove converted chars from source.
16527 Both will default the value - let them.
16529 XPUSHs(&PL_sv_yes);
16532 call_method("decode", G_SCALAR);
16536 s = SvPV_const(uni, len);
16537 if (s != SvPVX_const(sv)) {
16538 SvGROW(sv, len + 1);
16539 Move(s, SvPVX(sv), len + 1, char);
16540 SvCUR_set(sv, len);
16545 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
16546 /* clear pos and any utf8 cache */
16547 MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
16550 if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
16551 magic_setutf8(sv,mg); /* clear UTF8 cache */
16556 return SvPOKp(sv) ? SvPVX(sv) : NULL;
16560 =for apidoc sv_cat_decode
16562 C<encoding> is assumed to be an C<Encode> object, the PV of C<ssv> is
16563 assumed to be octets in that encoding and decoding the input starts
16564 from the position which S<C<(PV + *offset)>> pointed to. C<dsv> will be
16565 concatenated with the decoded UTF-8 string from C<ssv>. Decoding will terminate
16566 when the string C<tstr> appears in decoding output or the input ends on
16567 the PV of C<ssv>. The value which C<offset> points will be modified
16568 to the last input position on C<ssv>.
16570 Returns TRUE if the terminator was found, else returns FALSE.
16575 Perl_sv_cat_decode(pTHX_ SV *dsv, SV *encoding,
16576 SV *ssv, int *offset, char *tstr, int tlen)
16580 PERL_ARGS_ASSERT_SV_CAT_DECODE;
16582 if (SvPOK(ssv) && SvPOK(dsv) && SvROK(encoding)) {
16593 offsv = newSViv(*offset);
16595 mPUSHp(tstr, tlen);
16597 call_method("cat_decode", G_SCALAR);
16599 ret = SvTRUE(TOPs);
16600 *offset = SvIV(offsv);
16606 Perl_croak(aTHX_ "Invalid argument to sv_cat_decode");
16611 /* ---------------------------------------------------------------------
16613 * support functions for report_uninit()
16616 /* the maxiumum size of array or hash where we will scan looking
16617 * for the undefined element that triggered the warning */
16619 #define FUV_MAX_SEARCH_SIZE 1000
16621 /* Look for an entry in the hash whose value has the same SV as val;
16622 * If so, return a mortal copy of the key. */
16625 S_find_hash_subscript(pTHX_ const HV *const hv, const SV *const val)
16630 PERL_ARGS_ASSERT_FIND_HASH_SUBSCRIPT;
16632 if (!hv || SvMAGICAL(hv) || !HvTOTALKEYS(hv) ||
16633 (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
16636 if (val == &PL_sv_undef || val == &PL_sv_placeholder)
16639 array = HvARRAY(hv);
16641 for (i=HvMAX(hv); i>=0; i--) {
16643 for (entry = array[i]; entry; entry = HeNEXT(entry)) {
16644 if (HeVAL(entry) == val)
16645 return newSVhek_mortal(HeKEY_hek(entry));
16651 /* Look for an entry in the array whose value has the same SV as val;
16652 * If so, return the index, otherwise return -1. */
16655 S_find_array_subscript(pTHX_ const AV *const av, const SV *const val)
16657 PERL_ARGS_ASSERT_FIND_ARRAY_SUBSCRIPT;
16659 if (!av || SvMAGICAL(av) || !AvARRAY(av) ||
16660 (AvFILLp(av) > FUV_MAX_SEARCH_SIZE))
16663 if (val != &PL_sv_undef) {
16664 SV ** const svp = AvARRAY(av);
16667 for (i=AvFILLp(av); i>=0; i--)
16674 /* varname(): return the name of a variable, optionally with a subscript.
16675 * If gv is non-zero, use the name of that global, along with gvtype (one
16676 * of "$", "@", "%"); otherwise use the name of the lexical at pad offset
16677 * targ. Depending on the value of the subscript_type flag, return:
16680 #define FUV_SUBSCRIPT_NONE 1 /* "@foo" */
16681 #define FUV_SUBSCRIPT_ARRAY 2 /* "$foo[aindex]" */
16682 #define FUV_SUBSCRIPT_HASH 3 /* "$foo{keyname}" */
16683 #define FUV_SUBSCRIPT_WITHIN 4 /* "within @foo" */
16686 Perl_varname(pTHX_ const GV *const gv, const char gvtype, PADOFFSET targ,
16687 const SV *const keyname, SSize_t aindex, int subscript_type)
16690 SV * const name = sv_newmortal();
16691 if (gv && isGV(gv)) {
16693 buffer[0] = gvtype;
16696 /* as gv_fullname4(), but add literal '^' for $^FOO names */
16698 gv_fullname4(name, gv, buffer, 0);
16700 if ((unsigned int)SvPVX(name)[1] <= 26) {
16702 buffer[1] = SvPVX(name)[1] + 'A' - 1;
16704 /* Swap the 1 unprintable control character for the 2 byte pretty
16705 version - ie substr($name, 1, 1) = $buffer; */
16706 sv_insert(name, 1, 1, buffer, 2);
16710 CV * const cv = gv ? ((CV *)gv) : find_runcv(NULL);
16713 assert(!cv || SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM);
16715 if (!cv || !CvPADLIST(cv))
16717 sv = padnamelist_fetch(PadlistNAMES(CvPADLIST(cv)), targ);
16718 sv_setpvn(name, PadnamePV(sv), PadnameLEN(sv));
16722 if (subscript_type == FUV_SUBSCRIPT_HASH) {
16723 SV * const sv = newSV_type(SVt_NULL);
16725 const char * const pv = SvPV_nomg_const((SV*)keyname, len);
16727 *SvPVX(name) = '$';
16728 Perl_sv_catpvf(aTHX_ name, "{%s}",
16729 pv_pretty(sv, pv, len, 32, NULL, NULL,
16730 PERL_PV_PRETTY_DUMP | PERL_PV_ESCAPE_UNI_DETECT ));
16731 SvREFCNT_dec_NN(sv);
16733 else if (subscript_type == FUV_SUBSCRIPT_ARRAY) {
16734 *SvPVX(name) = '$';
16735 Perl_sv_catpvf(aTHX_ name, "[%" IVdf "]", (IV)aindex);
16737 else if (subscript_type == FUV_SUBSCRIPT_WITHIN) {
16738 /* We know that name has no magic, so can use 0 instead of SV_GMAGIC */
16739 Perl_sv_insert_flags(aTHX_ name, 0, 0, STR_WITH_LEN("within "), 0);
16747 =apidoc_section $warning
16748 =for apidoc find_uninit_var
16750 Find the name of the undefined variable (if any) that caused the operator
16751 to issue a "Use of uninitialized value" warning.
16752 If match is true, only return a name if its value matches C<uninit_sv>.
16753 So roughly speaking, if a unary operator (such as C<OP_COS>) generates a
16754 warning, then following the direct child of the op may yield an
16755 C<OP_PADSV> or C<OP_GV> that gives the name of the undefined variable. On the
16756 other hand, with C<OP_ADD> there are two branches to follow, so we only print
16757 the variable name if we get an exact match.
16758 C<desc_p> points to a string pointer holding the description of the op.
16759 This may be updated if needed.
16761 The name is returned as a mortal SV.
16763 Assumes that C<PL_op> is the OP that originally triggered the error, and that
16764 C<PL_comppad>/C<PL_curpad> points to the currently executing pad.
16770 S_find_uninit_var(pTHX_ const OP *const obase, const SV *const uninit_sv,
16771 bool match, const char **desc_p)
16775 const OP *o, *o2, *kid;
16777 PERL_ARGS_ASSERT_FIND_UNINIT_VAR;
16779 if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef ||
16780 uninit_sv == &PL_sv_placeholder)))
16783 switch (obase->op_type) {
16786 /* the optimizer rewrites '$x = undef' to 'undef $x' for lexical
16787 * variables, which can occur as the source of warnings:
16788 * ($x = undef) =~ s/a/b/;
16789 * The OPpUNDEF_KEEP_PV flag indicates that this used to be an
16791 * Otherwise undef should not care if its args are undef - any warnings
16792 * will be from tied/magic vars */
16794 (obase->op_private & (OPpTARGET_MY | OPpUNDEF_KEEP_PV)) == (OPpTARGET_MY | OPpUNDEF_KEEP_PV)
16795 && (!match || PAD_SVl(obase->op_targ) == uninit_sv)
16797 return varname(NULL, '$', obase->op_targ, NULL, 0, FUV_SUBSCRIPT_NONE);
16806 const bool pad = ( obase->op_type == OP_PADAV
16807 || obase->op_type == OP_PADHV
16808 || obase->op_type == OP_PADRANGE
16811 const bool hash = ( obase->op_type == OP_PADHV
16812 || obase->op_type == OP_RV2HV
16813 || (obase->op_type == OP_PADRANGE
16814 && SvTYPE(PAD_SVl(obase->op_targ)) == SVt_PVHV)
16818 int subscript_type = FUV_SUBSCRIPT_WITHIN;
16820 if (pad) { /* @lex, %lex */
16821 sv = PAD_SVl(obase->op_targ);
16825 if (cUNOPx(obase)->op_first->op_type == OP_GV) {
16826 /* @global, %global */
16827 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
16830 sv = hash ? MUTABLE_SV(GvHV(gv)): MUTABLE_SV(GvAV(gv));
16832 else if (obase == PL_op) /* @{expr}, %{expr} */
16833 return find_uninit_var(cUNOPx(obase)->op_first,
16834 uninit_sv, match, desc_p);
16835 else /* @{expr}, %{expr} as a sub-expression */
16839 /* attempt to find a match within the aggregate */
16841 keysv = find_hash_subscript((const HV*)sv, uninit_sv);
16843 subscript_type = FUV_SUBSCRIPT_HASH;
16846 index = find_array_subscript((const AV *)sv, uninit_sv);
16848 subscript_type = FUV_SUBSCRIPT_ARRAY;
16851 if (match && subscript_type == FUV_SUBSCRIPT_WITHIN)
16854 return varname(gv, (char)(hash ? '%' : '@'), obase->op_targ,
16855 keysv, index, subscript_type);
16859 if (cUNOPx(obase)->op_first->op_type == OP_GV) {
16861 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
16862 if (!gv || !GvSTASH(gv))
16864 if (match && (GvSV(gv) != uninit_sv))
16866 return varname(gv, '$', 0, NULL, 0, FUV_SUBSCRIPT_NONE);
16869 return find_uninit_var(cUNOPx(obase)->op_first, uninit_sv, 1, desc_p);
16872 if (match && PAD_SVl(obase->op_targ) != uninit_sv)
16874 return varname(NULL, '$', obase->op_targ,
16875 NULL, 0, FUV_SUBSCRIPT_NONE);
16877 case OP_PADSV_STORE:
16878 if (match && PAD_SVl(obase->op_targ) != uninit_sv)
16880 return varname(NULL, '$', obase->op_targ,
16881 NULL, 0, FUV_SUBSCRIPT_NONE);
16884 gv = cGVOPx_gv(obase);
16885 if (!gv || (match && GvSV(gv) != uninit_sv) || !GvSTASH(gv))
16887 return varname(gv, '$', 0, NULL, 0, FUV_SUBSCRIPT_NONE);
16889 case OP_AELEMFAST_LEX:
16892 AV *av = MUTABLE_AV(PAD_SV(obase->op_targ));
16893 if (!av || SvRMAGICAL(av))
16895 svp = av_fetch(av, (I8)obase->op_private, FALSE);
16896 if (!svp || *svp != uninit_sv)
16899 return varname(NULL, '$', obase->op_targ,
16900 NULL, (I8)obase->op_private, FUV_SUBSCRIPT_ARRAY);
16902 case OP_AELEMFASTLEX_STORE:
16905 AV *av = MUTABLE_AV(PAD_SV(obase->op_targ));
16906 if (!av || SvRMAGICAL(av))
16908 svp = av_fetch(av, (I8)obase->op_private, FALSE);
16909 if (!svp || *svp != uninit_sv)
16912 return varname(NULL, '$', obase->op_targ,
16913 NULL, (I8)obase->op_private, FUV_SUBSCRIPT_ARRAY);
16917 gv = cGVOPx_gv(obase);
16922 AV *const av = GvAV(gv);
16923 if (!av || SvRMAGICAL(av))
16925 svp = av_fetch(av, (I8)obase->op_private, FALSE);
16926 if (!svp || *svp != uninit_sv)
16929 return varname(gv, '$', 0,
16930 NULL, (I8)obase->op_private, FUV_SUBSCRIPT_ARRAY);
16932 NOT_REACHED; /* NOTREACHED */
16935 o = cUNOPx(obase)->op_first;
16936 if (!o || o->op_type != OP_NULL ||
16937 ! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM))
16939 return find_uninit_var(cBINOPo->op_last, uninit_sv, match, desc_p);
16944 bool negate = FALSE;
16946 if (PL_op == obase)
16947 /* $a[uninit_expr] or $h{uninit_expr} */
16948 return find_uninit_var(cBINOPx(obase)->op_last,
16949 uninit_sv, match, desc_p);
16952 o = cBINOPx(obase)->op_first;
16953 kid = cBINOPx(obase)->op_last;
16955 /* get the av or hv, and optionally the gv */
16957 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) {
16958 sv = PAD_SV(o->op_targ);
16960 else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV)
16961 && cUNOPo->op_first->op_type == OP_GV)
16963 gv = cGVOPx_gv(cUNOPo->op_first);
16967 == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(GvAV(gv));
16972 if (kid && kid->op_type == OP_NEGATE) {
16974 kid = cUNOPx(kid)->op_first;
16977 if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) {
16978 /* index is constant */
16981 kidsv = newSVpvs_flags("-", SVs_TEMP);
16982 sv_catsv(kidsv, cSVOPx_sv(kid));
16985 kidsv = cSVOPx_sv(kid);
16989 if (obase->op_type == OP_HELEM) {
16990 HE* he = hv_fetch_ent(MUTABLE_HV(sv), kidsv, 0, 0);
16991 if (!he || HeVAL(he) != uninit_sv)
16995 SV * const opsv = cSVOPx_sv(kid);
16996 const IV opsviv = SvIV(opsv);
16997 SV * const * const svp = av_fetch(MUTABLE_AV(sv),
16998 negate ? - opsviv : opsviv,
17000 if (!svp || *svp != uninit_sv)
17004 if (obase->op_type == OP_HELEM)
17005 return varname(gv, '%', o->op_targ,
17006 kidsv, 0, FUV_SUBSCRIPT_HASH);
17008 return varname(gv, '@', o->op_targ, NULL,
17009 negate ? - SvIV(cSVOPx_sv(kid)) : SvIV(cSVOPx_sv(kid)),
17010 FUV_SUBSCRIPT_ARRAY);
17013 /* index is an expression;
17014 * attempt to find a match within the aggregate */
17015 if (obase->op_type == OP_HELEM) {
17016 SV * const keysv = find_hash_subscript((const HV*)sv, uninit_sv);
17018 return varname(gv, '%', o->op_targ,
17019 keysv, 0, FUV_SUBSCRIPT_HASH);
17022 const SSize_t index
17023 = find_array_subscript((const AV *)sv, uninit_sv);
17025 return varname(gv, '@', o->op_targ,
17026 NULL, index, FUV_SUBSCRIPT_ARRAY);
17031 (char)((o->op_type == OP_PADAV || o->op_type == OP_RV2AV)
17033 o->op_targ, NULL, 0, FUV_SUBSCRIPT_WITHIN);
17035 NOT_REACHED; /* NOTREACHED */
17038 case OP_MULTIDEREF: {
17039 /* If we were executing OP_MULTIDEREF when the undef warning
17040 * triggered, then it must be one of the index values within
17041 * that triggered it. If not, then the only possibility is that
17042 * the value retrieved by the last aggregate index might be the
17043 * culprit. For the former, we set PL_multideref_pc each time before
17044 * using an index, so work though the item list until we reach
17045 * that point. For the latter, just work through the entire item
17046 * list; the last aggregate retrieved will be the candidate.
17047 * There is a third rare possibility: something triggered
17048 * magic while fetching an array/hash element. Just display
17049 * nothing in this case.
17052 /* the named aggregate, if any */
17053 PADOFFSET agg_targ = 0;
17055 /* the last-seen index */
17057 PADOFFSET index_targ;
17059 IV index_const_iv = 0; /* init for spurious compiler warn */
17060 SV *index_const_sv;
17061 int depth = 0; /* how many array/hash lookups we've done */
17063 UNOP_AUX_item *items = cUNOP_AUXx(obase)->op_aux;
17064 UNOP_AUX_item *last = NULL;
17065 UV actions = items->uv;
17068 if (PL_op == obase) {
17069 last = PL_multideref_pc;
17070 assert(last >= items && last <= items + items[-1].uv);
17077 switch (actions & MDEREF_ACTION_MASK) {
17079 case MDEREF_reload:
17080 actions = (++items)->uv;
17083 case MDEREF_HV_padhv_helem: /* $lex{...} */
17086 case MDEREF_AV_padav_aelem: /* $lex[...] */
17087 agg_targ = (++items)->pad_offset;
17091 case MDEREF_HV_gvhv_helem: /* $pkg{...} */
17094 case MDEREF_AV_gvav_aelem: /* $pkg[...] */
17096 agg_gv = (GV*)UNOP_AUX_item_sv(++items);
17097 assert(isGV_with_GP(agg_gv));
17100 case MDEREF_HV_gvsv_vivify_rv2hv_helem: /* $pkg->{...} */
17101 case MDEREF_HV_padsv_vivify_rv2hv_helem: /* $lex->{...} */
17104 case MDEREF_HV_pop_rv2hv_helem: /* expr->{...} */
17105 case MDEREF_HV_vivify_rv2hv_helem: /* vivify, ->{...} */
17111 case MDEREF_AV_gvsv_vivify_rv2av_aelem: /* $pkg->[...] */
17112 case MDEREF_AV_padsv_vivify_rv2av_aelem: /* $lex->[...] */
17115 case MDEREF_AV_pop_rv2av_aelem: /* expr->[...] */
17116 case MDEREF_AV_vivify_rv2av_aelem: /* vivify, ->[...] */
17123 index_const_sv = NULL;
17125 index_type = (actions & MDEREF_INDEX_MASK);
17126 switch (index_type) {
17127 case MDEREF_INDEX_none:
17129 case MDEREF_INDEX_const:
17131 index_const_sv = UNOP_AUX_item_sv(++items)
17133 index_const_iv = (++items)->iv;
17135 case MDEREF_INDEX_padsv:
17136 index_targ = (++items)->pad_offset;
17138 case MDEREF_INDEX_gvsv:
17139 index_gv = (GV*)UNOP_AUX_item_sv(++items);
17140 assert(isGV_with_GP(index_gv));
17144 if (index_type != MDEREF_INDEX_none)
17147 if ( index_type == MDEREF_INDEX_none
17148 || (actions & MDEREF_FLAG_last)
17149 || (last && items >= last)
17153 actions >>= MDEREF_SHIFT;
17156 if (PL_op == obase) {
17157 /* most likely index was undef */
17159 *desc_p = ( (actions & MDEREF_FLAG_last)
17160 && (obase->op_private
17161 & (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE)))
17163 (obase->op_private & OPpMULTIDEREF_EXISTS)
17166 : is_hv ? "hash element" : "array element";
17167 assert(index_type != MDEREF_INDEX_none);
17169 if (GvSV(index_gv) == uninit_sv)
17170 return varname(index_gv, '$', 0, NULL, 0,
17171 FUV_SUBSCRIPT_NONE);
17176 if (PL_curpad[index_targ] == uninit_sv)
17177 return varname(NULL, '$', index_targ,
17178 NULL, 0, FUV_SUBSCRIPT_NONE);
17182 /* If we got to this point it was undef on a const subscript,
17183 * so magic probably involved, e.g. $ISA[0]. Give up. */
17187 /* the SV returned by pp_multideref() was undef, if anything was */
17193 sv = PAD_SV(agg_targ);
17195 sv = is_hv ? MUTABLE_SV(GvHV(agg_gv)) : MUTABLE_SV(GvAV(agg_gv));
17202 if (index_type == MDEREF_INDEX_const) {
17207 HE* he = hv_fetch_ent(MUTABLE_HV(sv), index_const_sv, 0, 0);
17208 if (!he || HeVAL(he) != uninit_sv)
17212 SV * const * const svp =
17213 av_fetch(MUTABLE_AV(sv), index_const_iv, FALSE);
17214 if (!svp || *svp != uninit_sv)
17219 ? varname(agg_gv, '%', agg_targ,
17220 index_const_sv, 0, FUV_SUBSCRIPT_HASH)
17221 : varname(agg_gv, '@', agg_targ,
17222 NULL, index_const_iv, FUV_SUBSCRIPT_ARRAY);
17225 /* index is an var */
17227 SV * const keysv = find_hash_subscript((const HV*)sv, uninit_sv);
17229 return varname(agg_gv, '%', agg_targ,
17230 keysv, 0, FUV_SUBSCRIPT_HASH);
17233 const SSize_t index
17234 = find_array_subscript((const AV *)sv, uninit_sv);
17236 return varname(agg_gv, '@', agg_targ,
17237 NULL, index, FUV_SUBSCRIPT_ARRAY);
17239 /* look for an element not found */
17240 if (!SvMAGICAL(sv)) {
17241 SV *index_sv = NULL;
17243 index_sv = PL_curpad[index_targ];
17245 else if (index_gv) {
17246 index_sv = GvSV(index_gv);
17248 if (index_sv && !SvMAGICAL(index_sv) && !SvROK(index_sv)) {
17250 SV *report_index_sv = SvOK(index_sv) ? index_sv : &PL_sv_no;
17251 HE *he = hv_fetch_ent(MUTABLE_HV(sv), report_index_sv, 0, 0);
17253 return varname(agg_gv, '%', agg_targ,
17254 report_index_sv, 0, FUV_SUBSCRIPT_HASH);
17258 SSize_t index = SvOK(index_sv) ? SvIV(index_sv) : 0;
17259 SV * const * const svp =
17260 av_fetch(MUTABLE_AV(sv), index, FALSE);
17262 return varname(agg_gv, '@', agg_targ,
17263 NULL, index, FUV_SUBSCRIPT_ARRAY);
17270 return varname(agg_gv,
17272 agg_targ, NULL, 0, FUV_SUBSCRIPT_WITHIN);
17274 NOT_REACHED; /* NOTREACHED */
17278 /* only examine RHS */
17279 return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv,
17283 o = cUNOPx(obase)->op_first;
17284 if ( o->op_type == OP_PUSHMARK
17285 || (o->op_type == OP_NULL && o->op_targ == OP_PUSHMARK)
17289 if (!OpHAS_SIBLING(o)) {
17290 /* one-arg version of open is highly magical */
17292 if (o->op_type == OP_GV) { /* open FOO; */
17294 if (match && GvSV(gv) != uninit_sv)
17296 return varname(gv, '$', 0,
17297 NULL, 0, FUV_SUBSCRIPT_NONE);
17299 /* other possibilities not handled are:
17300 * open $x; or open my $x; should return '${*$x}'
17301 * open expr; should return '$'.expr ideally
17308 /* ops where $_ may be an implicit arg */
17313 if ( !(obase->op_flags & OPf_STACKED)) {
17314 if (uninit_sv == DEFSV)
17315 return newSVpvs_flags("$_", SVs_TEMP);
17316 else if (obase->op_targ
17317 && uninit_sv == PAD_SVl(obase->op_targ))
17318 return varname(NULL, '$', obase->op_targ, NULL, 0,
17319 FUV_SUBSCRIPT_NONE);
17326 match = 1; /* print etc can return undef on defined args */
17327 /* skip filehandle as it can't produce 'undef' warning */
17328 o = cUNOPx(obase)->op_first;
17329 if ((obase->op_flags & OPf_STACKED)
17331 ( o->op_type == OP_PUSHMARK
17332 || (o->op_type == OP_NULL && o->op_targ == OP_PUSHMARK)))
17333 o = OpSIBLING(OpSIBLING(o));
17337 case OP_ENTEREVAL: /* could be eval $undef or $x='$undef'; eval $x */
17338 case OP_CUSTOM: /* XS or custom code could trigger random warnings */
17340 /* the following ops are capable of returning PL_sv_undef even for
17341 * defined arg(s) */
17360 case OP_GETPEERNAME:
17407 case OP_SMARTMATCH:
17416 /* XXX tmp hack: these two may call an XS sub, and currently
17417 XS subs don't have a SUB entry on the context stack, so CV and
17418 pad determination goes wrong, and BAD things happen. So, just
17419 don't try to determine the value under those circumstances.
17420 Need a better fix at dome point. DAPM 11/2007 */
17426 GV * const gv = gv_fetchpvs(".", GV_NOTQUAL, SVt_PV);
17427 if (gv && GvSV(gv) == uninit_sv)
17428 return newSVpvs_flags("$.", SVs_TEMP);
17433 /* def-ness of rval pos() is independent of the def-ness of its arg */
17434 if ( !(obase->op_flags & OPf_MOD))
17440 if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs))
17441 return newSVpvs_flags("${$/}", SVs_TEMP);
17446 if (!(obase->op_flags & OPf_KIDS))
17448 o = cUNOPx(obase)->op_first;
17454 /* This loop checks all the kid ops, skipping any that cannot pos-
17455 * sibly be responsible for the uninitialized value; i.e., defined
17456 * constants and ops that return nothing. If there is only one op
17457 * left that is not skipped, then we *know* it is responsible for
17458 * the uninitialized value. If there is more than one op left, we
17459 * have to look for an exact match in the while() loop below.
17460 * Note that we skip padrange, because the individual pad ops that
17461 * it replaced are still in the tree, so we work on them instead.
17464 for (kid=o; kid; kid = OpSIBLING(kid)) {
17465 const OPCODE type = kid->op_type;
17466 if ( (type == OP_CONST && SvOK(cSVOPx_sv(kid)))
17467 || (type == OP_NULL && ! (kid->op_flags & OPf_KIDS))
17468 || (type == OP_PUSHMARK)
17469 || (type == OP_PADRANGE)
17473 if (o2) { /* more than one found */
17480 return find_uninit_var(o2, uninit_sv, match, desc_p);
17482 /* scan all args */
17484 sv = find_uninit_var(o, uninit_sv, 1, desc_p);
17496 =for apidoc_section $warning
17497 =for apidoc report_uninit
17499 Print appropriate "Use of uninitialized variable" warning.
17505 Perl_report_uninit(pTHX_ const SV *uninit_sv)
17507 const char *desc = NULL;
17508 SV* varname = NULL;
17511 desc = PL_op->op_type == OP_STRINGIFY && PL_op->op_folded
17513 : PL_op->op_type == OP_MULTICONCAT
17514 && (PL_op->op_private & OPpMULTICONCAT_FAKE)
17517 if (uninit_sv && PL_curpad) {
17518 varname = find_uninit_var(PL_op, uninit_sv, 0, &desc);
17520 sv_insert(varname, 0, 0, " ", 1);
17523 else if (PL_curstackinfo->si_type == PERLSI_SORT && cxstack_ix == 0)
17524 /* we've reached the end of a sort block or sub,
17525 * and the uninit value is probably what that code returned */
17528 /* PL_warn_uninit_sv is constant */
17529 GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral);
17531 /* diag_listed_as: Use of uninitialized value%s */
17532 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit_sv,
17533 SVfARG(varname ? varname : &PL_sv_no),
17536 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
17538 GCC_DIAG_RESTORE_STMT;
17542 * ex: set ts=8 sts=4 sw=4 et: