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) PerlMemShared_free((sv)->sv_debug_file); \
236 # define DEBUG_SV_SERIAL(sv) \
237 DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%" UVxf ": (%05ld) del_SV\n", \
238 PTR2UV(sv), (long)(sv)->sv_debug_serial))
240 # define FREE_SV_DEBUG_FILE(sv)
241 # define DEBUG_SV_SERIAL(sv) NOOP
244 /* Mark an SV head as unused, and add to free list.
246 * If SVf_BREAK is set, skip adding it to the free list, as this SV had
247 * its refcount artificially decremented during global destruction, so
248 * there may be dangling pointers to it. The last thing we want in that
249 * case is for it to be reused. */
251 #define plant_SV(p) \
253 const U32 old_flags = SvFLAGS(p); \
254 MEM_LOG_DEL_SV(p, __FILE__, __LINE__, FUNCTION__); \
255 DEBUG_SV_SERIAL(p); \
256 FREE_SV_DEBUG_FILE(p); \
258 SvFLAGS(p) = SVTYPEMASK; \
259 if (!(old_flags & SVf_BREAK)) { \
260 SvARENA_CHAIN_SET(p, PL_sv_root); \
267 /* make some more SVs by adding another arena */
273 char *chunk; /* must use New here to match call to */
274 Newx(chunk,PERL_ARENA_SIZE,char); /* Safefree() in sv_free_arenas() */
275 sv_add_arena(chunk, PERL_ARENA_SIZE, 0);
280 /* del_SV(): return an empty SV head to the free list */
293 S_del_sv(pTHX_ SV *p)
295 PERL_ARGS_ASSERT_DEL_SV;
300 for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) {
301 const SV * const sv = sva + 1;
302 const SV * const svend = &sva[SvREFCNT(sva)];
303 if (p >= sv && p < svend) {
309 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
310 "Attempt to free non-arena SV: 0x%" UVxf
311 pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
318 #else /* ! DEBUGGING */
320 #define del_SV(p) plant_SV(p)
322 #endif /* DEBUGGING */
326 =for apidoc_section $SV
328 =for apidoc sv_add_arena
330 Given a chunk of memory, link it to the head of the list of arenas,
331 and split it into a list of free SVs.
337 S_sv_add_arena(pTHX_ char *const ptr, const U32 size, const U32 flags)
339 SV *const sva = MUTABLE_SV(ptr);
343 PERL_ARGS_ASSERT_SV_ADD_ARENA;
345 /* The first SV in an arena isn't an SV. */
346 SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
347 SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
348 SvFLAGS(sva) = flags; /* FAKE if not to be freed */
350 PL_sv_arenaroot = sva;
351 PL_sv_root = sva + 1;
353 svend = &sva[SvREFCNT(sva) - 1];
356 SvARENA_CHAIN_SET(sv, (sv + 1));
360 /* Must always set typemask because it's always checked in on cleanup
361 when the arenas are walked looking for objects. */
362 SvFLAGS(sv) = SVTYPEMASK;
365 SvARENA_CHAIN_SET(sv, 0);
369 SvFLAGS(sv) = SVTYPEMASK;
372 /* visit(): call the named function for each non-free SV in the arenas
373 * whose flags field matches the flags/mask args. */
376 S_visit(pTHX_ SVFUNC_t f, const U32 flags, const U32 mask)
381 PERL_ARGS_ASSERT_VISIT;
383 for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) {
384 const SV * const svend = &sva[SvREFCNT(sva)];
386 for (sv = sva + 1; sv < svend; ++sv) {
387 if (SvTYPE(sv) != (svtype)SVTYPEMASK
388 && (sv->sv_flags & mask) == flags
401 /* called by sv_report_used() for each live SV */
404 do_report_used(pTHX_ SV *const sv)
406 if (SvTYPE(sv) != (svtype)SVTYPEMASK) {
407 PerlIO_printf(Perl_debug_log, "****\n");
414 =for apidoc sv_report_used
416 Dump the contents of all SVs not yet freed (debugging aid).
422 Perl_sv_report_used(pTHX)
425 visit(do_report_used, 0, 0);
431 /* called by sv_clean_objs() for each live SV */
434 do_clean_objs(pTHX_ SV *const ref)
438 SV * const target = SvRV(ref);
439 if (SvOBJECT(target)) {
440 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(ref)));
441 if (SvWEAKREF(ref)) {
442 sv_del_backref(target, ref);
448 SvREFCNT_dec_NN(target);
455 /* clear any slots in a GV which hold objects - except IO;
456 * called by sv_clean_objs() for each live GV */
459 do_clean_named_objs(pTHX_ SV *const sv)
462 assert(SvTYPE(sv) == SVt_PVGV);
463 assert(isGV_with_GP(sv));
467 /* freeing GP entries may indirectly free the current GV;
468 * hold onto it while we mess with the GP slots */
471 if ( ((obj = GvSV(sv) )) && SvOBJECT(obj)) {
472 DEBUG_D((PerlIO_printf(Perl_debug_log,
473 "Cleaning named glob SV object:\n "), sv_dump(obj)));
475 SvREFCNT_dec_NN(obj);
477 if ( ((obj = MUTABLE_SV(GvAV(sv)) )) && SvOBJECT(obj)) {
478 DEBUG_D((PerlIO_printf(Perl_debug_log,
479 "Cleaning named glob AV object:\n "), sv_dump(obj)));
481 SvREFCNT_dec_NN(obj);
483 if ( ((obj = MUTABLE_SV(GvHV(sv)) )) && SvOBJECT(obj)) {
484 DEBUG_D((PerlIO_printf(Perl_debug_log,
485 "Cleaning named glob HV object:\n "), sv_dump(obj)));
487 SvREFCNT_dec_NN(obj);
489 if ( ((obj = MUTABLE_SV(GvCV(sv)) )) && SvOBJECT(obj)) {
490 DEBUG_D((PerlIO_printf(Perl_debug_log,
491 "Cleaning named glob CV object:\n "), sv_dump(obj)));
493 SvREFCNT_dec_NN(obj);
495 SvREFCNT_dec_NN(sv); /* undo the inc above */
498 /* clear any IO slots in a GV which hold objects (except stderr, defout);
499 * called by sv_clean_objs() for each live GV */
502 do_clean_named_io_objs(pTHX_ SV *const sv)
505 assert(SvTYPE(sv) == SVt_PVGV);
506 assert(isGV_with_GP(sv));
507 if (!GvGP(sv) || sv == (SV*)PL_stderrgv || sv == (SV*)PL_defoutgv)
511 if ( ((obj = MUTABLE_SV(GvIO(sv)) )) && SvOBJECT(obj)) {
512 DEBUG_D((PerlIO_printf(Perl_debug_log,
513 "Cleaning named glob IO object:\n "), sv_dump(obj)));
515 SvREFCNT_dec_NN(obj);
517 SvREFCNT_dec_NN(sv); /* undo the inc above */
520 /* Void wrapper to pass to visit() */
522 do_curse(pTHX_ SV * const sv) {
523 if ((PL_stderrgv && GvGP(PL_stderrgv) && (SV*)GvIO(PL_stderrgv) == sv)
524 || (PL_defoutgv && GvGP(PL_defoutgv) && (SV*)GvIO(PL_defoutgv) == sv))
530 =for apidoc sv_clean_objs
532 Attempt to destroy all objects not yet freed.
538 Perl_sv_clean_objs(pTHX)
541 PL_in_clean_objs = TRUE;
542 visit(do_clean_objs, SVf_ROK, SVf_ROK);
543 /* Some barnacles may yet remain, clinging to typeglobs.
544 * Run the non-IO destructors first: they may want to output
545 * error messages, close files etc */
546 visit(do_clean_named_objs, SVt_PVGV|SVpgv_GP, SVTYPEMASK|SVp_POK|SVpgv_GP);
547 visit(do_clean_named_io_objs, SVt_PVGV|SVpgv_GP, SVTYPEMASK|SVp_POK|SVpgv_GP);
548 /* And if there are some very tenacious barnacles clinging to arrays,
549 closures, or what have you.... */
550 visit(do_curse, SVs_OBJECT, SVs_OBJECT);
551 olddef = PL_defoutgv;
552 PL_defoutgv = NULL; /* disable skip of PL_defoutgv */
553 if (olddef && isGV_with_GP(olddef))
554 do_clean_named_io_objs(aTHX_ MUTABLE_SV(olddef));
555 olderr = PL_stderrgv;
556 PL_stderrgv = NULL; /* disable skip of PL_stderrgv */
557 if (olderr && isGV_with_GP(olderr))
558 do_clean_named_io_objs(aTHX_ MUTABLE_SV(olderr));
559 SvREFCNT_dec(olddef);
560 PL_in_clean_objs = FALSE;
563 /* called by sv_clean_all() for each live SV */
566 do_clean_all(pTHX_ SV *const sv)
568 if (sv == (const SV *) PL_fdpid || sv == (const SV *)PL_strtab) {
569 /* don't clean pid table and strtab */
572 DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%" UVxf "\n", PTR2UV(sv)) ));
573 SvFLAGS(sv) |= SVf_BREAK;
578 =for apidoc sv_clean_all
580 Decrement the refcnt of each remaining SV, possibly triggering a
581 cleanup. This function may have to be called multiple times to free
582 SVs which are in complex self-referential hierarchies.
588 Perl_sv_clean_all(pTHX)
591 PL_in_clean_all = TRUE;
592 cleaned = visit(do_clean_all, 0,0);
597 ARENASETS: a meta-arena implementation which separates arena-info
598 into struct arena_set, which contains an array of struct
599 arena_descs, each holding info for a single arena. By separating
600 the meta-info from the arena, we recover the 1st slot, formerly
601 borrowed for list management. The arena_set is about the size of an
602 arena, avoiding the needless malloc overhead of a naive linked-list.
604 The cost is 1 arena-set malloc per ~320 arena-mallocs, + the unused
605 memory in the last arena-set (1/2 on average). In trade, we get
606 back the 1st slot in each arena (ie 1.7% of a CV-arena, less for
607 smaller types). The recovery of the wasted space allows use of
608 small arenas for large, rare body types, by changing array* fields
609 in body_details_by_type[] below.
612 char *arena; /* the raw storage, allocated aligned */
613 size_t size; /* its size ~4k typ */
614 svtype utype; /* bodytype stored in arena */
619 /* Get the maximum number of elements in set[] such that struct arena_set
620 will fit within PERL_ARENA_SIZE, which is probably just under 4K, and
621 therefore likely to be 1 aligned memory page. */
623 #define ARENAS_PER_SET ((PERL_ARENA_SIZE - sizeof(struct arena_set*) \
624 - 2 * sizeof(int)) / sizeof (struct arena_desc))
627 struct arena_set* next;
628 unsigned int set_size; /* ie ARENAS_PER_SET */
629 unsigned int curr; /* index of next available arena-desc */
630 struct arena_desc set[ARENAS_PER_SET];
634 =for apidoc sv_free_arenas
636 Deallocate the memory used by all arenas. Note that all the individual SV
637 heads and bodies within the arenas must already have been freed.
643 Perl_sv_free_arenas(pTHX)
649 /* Free arenas here, but be careful about fake ones. (We assume
650 contiguity of the fake ones with the corresponding real ones.) */
652 for (sva = PL_sv_arenaroot; sva; sva = svanext) {
653 svanext = MUTABLE_SV(SvANY(sva));
654 while (svanext && SvFAKE(svanext))
655 svanext = MUTABLE_SV(SvANY(svanext));
662 struct arena_set *aroot = (struct arena_set*) PL_body_arenas;
665 struct arena_set *current = aroot;
668 assert(aroot->set[i].arena);
669 Safefree(aroot->set[i].arena);
677 i = PERL_ARENA_ROOTS_SIZE;
679 PL_body_roots[i] = 0;
686 Historically, here were mid-level routines that manage the
687 allocation of bodies out of the various arenas. Some of these
688 routines and related definitions remain here, but otherse were
689 moved into sv_inline.h to facilitate inlining of newSV_type().
691 There are 4 kinds of arenas:
693 1. SV-head arenas, which are discussed and handled above
694 2. regular body arenas
695 3. arenas for reduced-size bodies
698 Arena types 2 & 3 are chained by body-type off an array of
699 arena-root pointers, which is indexed by svtype. Some of the
700 larger/less used body types are malloced singly, since a large
701 unused block of them is wasteful. Also, several svtypes dont have
702 bodies; the data fits into the sv-head itself. The arena-root
703 pointer thus has a few unused root-pointers (which may be hijacked
704 later for arena type 4)
706 3 differs from 2 as an optimization; some body types have several
707 unused fields in the front of the structure (which are kept in-place
708 for consistency). These bodies can be allocated in smaller chunks,
709 because the leading fields arent accessed. Pointers to such bodies
710 are decremented to point at the unused 'ghost' memory, knowing that
711 the pointers are used with offsets to the real memory.
713 Allocation of SV-bodies is similar to SV-heads, differing as follows;
714 the allocation mechanism is used for many body types, so is somewhat
715 more complicated, it uses arena-sets, and has no need for still-live
718 At the outermost level, (new|del)_X*V macros return bodies of the
719 appropriate type. These macros call either (new|del)_body_type or
720 (new|del)_body_allocated macro pairs, depending on specifics of the
721 type. Most body types use the former pair, the latter pair is used to
722 allocate body types with "ghost fields".
724 "ghost fields" are fields that are unused in certain types, and
725 consequently don't need to actually exist. They are declared because
726 they're part of a "base type", which allows use of functions as
727 methods. The simplest examples are AVs and HVs, 2 aggregate types
728 which don't use the fields which support SCALAR semantics.
730 For these types, the arenas are carved up into appropriately sized
731 chunks, we thus avoid wasted memory for those unaccessed members.
732 When bodies are allocated, we adjust the pointer back in memory by the
733 size of the part not allocated, so it's as if we allocated the full
734 structure. (But things will all go boom if you write to the part that
735 is "not there", because you'll be overwriting the last members of the
736 preceding structure in memory.)
738 We calculate the correction using the STRUCT_OFFSET macro on the first
739 member present. If the allocated structure is smaller (no initial NV
740 actually allocated) then the net effect is to subtract the size of the NV
741 from the pointer, to return a new pointer as if an initial NV were actually
742 allocated. (We were using structures named *_allocated for this, but
743 this turned out to be a subtle bug, because a structure without an NV
744 could have a lower alignment constraint, but the compiler is allowed to
745 optimised accesses based on the alignment constraint of the actual pointer
746 to the full structure, for example, using a single 64 bit load instruction
747 because it "knows" that two adjacent 32 bit members will be 8-byte aligned.)
749 This is the same trick as was used for NV and IV bodies. Ironically it
750 doesn't need to be used for NV bodies any more, because NV is now at
751 the start of the structure. IV bodies, and also in some builds NV bodies,
752 don't need it either, because they are no longer allocated.
754 In turn, the new_body_* allocators call S_new_body(), which invokes
755 new_body_from_arena macro, which takes a lock, and takes a body off the
756 linked list at PL_body_roots[sv_type], calling Perl_more_bodies() if
757 necessary to refresh an empty list. Then the lock is released, and
758 the body is returned.
760 Perl_more_bodies allocates a new arena, and carves it up into an array of N
761 bodies, which it strings into a linked list. It looks up arena-size
762 and body-size from the body_details table described below, thus
763 supporting the multiple body-types.
765 If PURIFY is defined, or PERL_ARENA_SIZE=0, arenas are not used, and
766 the (new|del)_X*V macros are mapped directly to malloc/free.
768 For each sv-type, struct body_details bodies_by_type[] carries
769 parameters which control these aspects of SV handling:
771 Arena_size determines whether arenas are used for this body type, and if
772 so, how big they are. PURIFY or PERL_ARENA_SIZE=0 set this field to
773 zero, forcing individual mallocs and frees.
775 Body_size determines how big a body is, and therefore how many fit into
776 each arena. Offset carries the body-pointer adjustment needed for
777 "ghost fields", and is used in *_allocated macros.
779 But its main purpose is to parameterize info needed in
780 Perl_sv_upgrade(). The info here dramatically simplifies the function
781 vs the implementation in 5.8.8, making it table-driven. All fields
782 are used for this, except for arena_size.
784 For the sv-types that have no bodies, arenas are not used, so those
785 PL_body_roots[sv_type] are unused, and can be overloaded. In
786 something of a special case, SVt_NULL is borrowed for HE arenas;
787 PL_body_roots[HE_ARENA_ROOT_IX=SVt_NULL] is filled by S_more_he, but the
788 bodies_by_type[SVt_NULL] slot is not used, as the table is not
789 available in hv.c. Similarly SVt_IV is re-used for HVAUX_ARENA_ROOT_IX.
793 /* return a thing to the free list */
795 #define del_body(thing, root) \
797 void ** const thing_copy = (void **)thing; \
798 *thing_copy = *root; \
799 *root = (void*)thing_copy; \
804 Perl_more_bodies (pTHX_ const svtype sv_type, const size_t body_size,
805 const size_t arena_size)
807 void ** const root = &PL_body_roots[sv_type];
808 struct arena_desc *adesc;
809 struct arena_set *aroot = (struct arena_set *) PL_body_arenas;
813 const size_t good_arena_size = Perl_malloc_good_size(arena_size);
814 #if defined(DEBUGGING)
815 static bool done_sanity_check;
817 if (!done_sanity_check) {
818 unsigned int i = SVt_LAST;
820 done_sanity_check = TRUE;
823 assert (bodies_by_type[i].type == i);
829 /* may need new arena-set to hold new arena */
830 if (!aroot || aroot->curr >= aroot->set_size) {
831 struct arena_set *newroot;
832 Newxz(newroot, 1, struct arena_set);
833 newroot->set_size = ARENAS_PER_SET;
834 newroot->next = aroot;
836 PL_body_arenas = (void *) newroot;
837 DEBUG_m(PerlIO_printf(Perl_debug_log, "new arenaset %p\n", (void*)aroot));
840 /* ok, now have arena-set with at least 1 empty/available arena-desc */
841 curr = aroot->curr++;
842 adesc = &(aroot->set[curr]);
843 assert(!adesc->arena);
845 Newx(adesc->arena, good_arena_size, char);
846 adesc->size = good_arena_size;
847 adesc->utype = sv_type;
848 DEBUG_m(PerlIO_printf(Perl_debug_log, "arena %d added: %p size %" UVuf "\n",
849 curr, (void*)adesc->arena, (UV)good_arena_size));
851 start = (char *) adesc->arena;
853 /* Get the address of the byte after the end of the last body we can fit.
854 Remember, this is integer division: */
855 end = start + good_arena_size / body_size * body_size;
857 /* computed count doesn't reflect the 1st slot reservation */
858 #if defined(MYMALLOC) || defined(HAS_MALLOC_GOOD_SIZE)
859 DEBUG_m(PerlIO_printf(Perl_debug_log,
860 "arena %p end %p arena-size %d (from %d) type %d "
862 (void*)start, (void*)end, (int)good_arena_size,
863 (int)arena_size, sv_type, (int)body_size,
864 (int)good_arena_size / (int)body_size));
866 DEBUG_m(PerlIO_printf(Perl_debug_log,
867 "arena %p end %p arena-size %d type %d size %d ct %d\n",
868 (void*)start, (void*)end,
869 (int)arena_size, sv_type, (int)body_size,
870 (int)good_arena_size / (int)body_size));
872 *root = (void *)start;
875 /* Where the next body would start: */
876 char * const next = start + body_size;
879 /* This is the last body: */
886 *(void**) start = (void *)next;
892 =for apidoc sv_upgrade
894 Upgrade an SV to a more complex form. Generally adds a new body type to the
895 SV, then copies across as much information as possible from the old body.
896 It croaks if the SV is already in a more complex form than requested. You
897 generally want to use the C<SvUPGRADE> macro wrapper, which checks the type
898 before calling C<sv_upgrade>, and hence does not croak. See also
905 Perl_sv_upgrade(pTHX_ SV *const sv, svtype new_type)
909 const svtype old_type = SvTYPE(sv);
910 const struct body_details *new_type_details;
911 const struct body_details *old_type_details
912 = bodies_by_type + old_type;
915 PERL_ARGS_ASSERT_SV_UPGRADE;
917 if (old_type == new_type)
920 /* This clause was purposefully added ahead of the early return above to
921 the shared string hackery for (sort {$a <=> $b} keys %hash), with the
922 inference by Nick I-S that it would fix other troublesome cases. See
923 changes 7162, 7163 (f130fd4589cf5fbb24149cd4db4137c8326f49c1 and parent)
925 Given that shared hash key scalars are no longer PVIV, but PV, there is
926 no longer need to unshare so as to free up the IVX slot for its proper
927 purpose. So it's safe to move the early return earlier. */
929 if (new_type > SVt_PVMG && SvIsCOW(sv)) {
930 sv_force_normal_flags(sv, 0);
933 old_body = SvANY(sv);
935 /* Copying structures onto other structures that have been neatly zeroed
936 has a subtle gotcha. Consider XPVMG
938 +------+------+------+------+------+-------+-------+
939 | NV | CUR | LEN | IV | MAGIC | STASH |
940 +------+------+------+------+------+-------+-------+
943 where NVs are aligned to 8 bytes, so that sizeof that structure is
944 actually 32 bytes long, with 4 bytes of padding at the end:
946 +------+------+------+------+------+-------+-------+------+
947 | NV | CUR | LEN | IV | MAGIC | STASH | ??? |
948 +------+------+------+------+------+-------+-------+------+
949 0 4 8 12 16 20 24 28 32
951 so what happens if you allocate memory for this structure:
953 +------+------+------+------+------+-------+-------+------+------+...
954 | NV | CUR | LEN | IV | MAGIC | STASH | GP | NAME |
955 +------+------+------+------+------+-------+-------+------+------+...
956 0 4 8 12 16 20 24 28 32 36
958 zero it, then copy sizeof(XPVMG) bytes on top of it? Not quite what you
959 expect, because you copy the area marked ??? onto GP. Now, ??? may have
960 started out as zero once, but it's quite possible that it isn't. So now,
961 rather than a nicely zeroed GP, you have it pointing somewhere random.
964 (In fact, GP ends up pointing at a previous GP structure, because the
965 principle cause of the padding in XPVMG getting garbage is a copy of
966 sizeof(XPVMG) bytes from a XPVGV structure in sv_unglob. Right now
967 this happens to be moot because XPVGV has been re-ordered, with GP
968 no longer after STASH)
970 So we are careful and work out the size of used parts of all the
979 old_type_details = &fake_rv;
980 if (new_type == SVt_NV)
983 if (new_type < SVt_PVIV) {
984 new_type = (new_type == SVt_NV)
985 ? SVt_PVNV : SVt_PVIV;
990 if (new_type < SVt_PVNV) {
995 assert(new_type > SVt_PV);
996 STATIC_ASSERT_STMT(SVt_IV < SVt_PV);
997 STATIC_ASSERT_STMT(SVt_NV < SVt_PV);
1004 /* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
1005 there's no way that it can be safely upgraded, because perl.c
1006 expects to Safefree(SvANY(PL_mess_sv)) */
1007 assert(sv != PL_mess_sv);
1010 if (UNLIKELY(old_type_details->cant_upgrade))
1011 Perl_croak(aTHX_ "Can't upgrade %s (%" UVuf ") to %" UVuf,
1012 sv_reftype(sv, 0), (UV) old_type, (UV) new_type);
1015 if (UNLIKELY(old_type > new_type))
1016 Perl_croak(aTHX_ "sv_upgrade from type %d down to type %d",
1017 (int)old_type, (int)new_type);
1019 new_type_details = bodies_by_type + new_type;
1021 SvFLAGS(sv) &= ~SVTYPEMASK;
1022 SvFLAGS(sv) |= new_type;
1024 /* This can't happen, as SVt_NULL is <= all values of new_type, so one of
1025 the return statements above will have triggered. */
1026 assert (new_type != SVt_NULL);
1029 assert(old_type == SVt_NULL);
1030 SET_SVANY_FOR_BODYLESS_IV(sv);
1034 assert(old_type == SVt_NULL);
1035 #if NVSIZE <= IVSIZE
1036 SET_SVANY_FOR_BODYLESS_NV(sv);
1038 SvANY(sv) = new_XNV();
1044 assert(new_type_details->body_size);
1047 assert(new_type_details->arena);
1048 assert(new_type_details->arena_size);
1049 /* This points to the start of the allocated area. */
1050 new_body = S_new_body(aTHX_ new_type);
1051 /* xpvav and xpvhv have no offset, so no need to adjust new_body */
1052 assert(!(new_type_details->offset));
1054 /* We always allocated the full length item with PURIFY. To do this
1055 we fake things so that arena is false for all 16 types.. */
1056 new_body = new_NOARENAZ(new_type_details);
1058 SvANY(sv) = new_body;
1059 if (new_type == SVt_PVAV) {
1060 *((XPVAV*) SvANY(sv)) = (XPVAV) {
1061 .xmg_stash = NULL, .xmg_u = {.xmg_magic = NULL},
1062 .xav_fill = -1, .xav_max = -1, .xav_alloc = 0
1067 *((XPVHV*) SvANY(sv)) = (XPVHV) {
1068 .xmg_stash = NULL, .xmg_u = {.xmg_magic = NULL},
1070 /* start with PERL_HASH_DEFAULT_HvMAX+1 buckets: */
1071 .xhv_max = PERL_HASH_DEFAULT_HvMAX
1076 #ifndef NODEFAULT_SHAREKEYS
1077 HvSHAREKEYS_on(sv); /* key-sharing on by default */
1081 /* SVt_NULL isn't the only thing upgraded to AV or HV.
1082 The target created by newSVrv also is, and it can have magic.
1083 However, it never has SvPVX set.
1085 if (old_type == SVt_IV) {
1087 } else if (old_type >= SVt_PV) {
1088 assert(SvPVX_const(sv) == 0);
1091 if (old_type >= SVt_PVMG) {
1092 SvMAGIC_set(sv, ((XPVMG*)old_body)->xmg_u.xmg_magic);
1093 SvSTASH_set(sv, ((XPVMG*)old_body)->xmg_stash);
1095 sv->sv_u.svu_array = NULL; /* or svu_hash */
1100 /* XXX Is this still needed? Was it ever needed? Surely as there is
1101 no route from NV to PVIV, NOK can never be true */
1102 assert(!SvNOKp(sv));
1116 assert(new_type_details->body_size);
1117 /* We always allocated the full length item with PURIFY. To do this
1118 we fake things so that arena is false for all 16 types.. */
1120 if(new_type_details->arena) {
1121 /* This points to the start of the allocated area. */
1122 new_body = S_new_body(aTHX_ new_type);
1123 Zero(new_body, new_type_details->body_size, char);
1124 new_body = ((char *)new_body) - new_type_details->offset;
1128 new_body = new_NOARENAZ(new_type_details);
1130 SvANY(sv) = new_body;
1132 if (old_type_details->copy) {
1133 /* There is now the potential for an upgrade from something without
1134 an offset (PVNV or PVMG) to something with one (PVCV, PVFM) */
1135 int offset = old_type_details->offset;
1136 int length = old_type_details->copy;
1138 if (new_type_details->offset > old_type_details->offset) {
1139 const int difference
1140 = new_type_details->offset - old_type_details->offset;
1141 offset += difference;
1142 length -= difference;
1144 assert (length >= 0);
1146 Copy((char *)old_body + offset, (char *)new_body + offset, length,
1150 #ifndef NV_ZERO_IS_ALLBITS_ZERO
1151 /* If NV 0.0 is stores as all bits 0 then Zero() already creates a
1152 * correct 0.0 for us. Otherwise, if the old body didn't have an
1153 * NV slot, but the new one does, then we need to initialise the
1154 * freshly created NV slot with whatever the correct bit pattern is
1156 if (old_type_details->zero_nv && !new_type_details->zero_nv
1157 && !isGV_with_GP(sv))
1161 if (UNLIKELY(new_type == SVt_PVIO)) {
1162 IO * const io = MUTABLE_IO(sv);
1163 GV *iogv = gv_fetchpvs("IO::File::", GV_ADD, SVt_PVHV);
1166 /* Clear the stashcache because a new IO could overrule a package
1168 DEBUG_o(Perl_deb(aTHX_ "sv_upgrade clearing PL_stashcache\n"));
1169 hv_clear(PL_stashcache);
1171 SvSTASH_set(io, MUTABLE_HV(SvREFCNT_inc(GvHV(iogv))));
1172 IoPAGE_LEN(sv) = 60;
1174 if (old_type < SVt_PV) {
1175 /* referent will be NULL unless the old type was SVt_IV emulating
1177 sv->sv_u.svu_rv = referent;
1181 Perl_croak(aTHX_ "panic: sv_upgrade to unknown type %lu",
1182 (unsigned long)new_type);
1185 /* if this is zero, this is a body-less SVt_NULL, SVt_IV/SVt_RV,
1186 and sometimes SVt_NV */
1187 if (old_type_details->body_size) {
1191 /* Note that there is an assumption that all bodies of types that
1192 can be upgraded came from arenas. Only the more complex non-
1193 upgradable types are allowed to be directly malloc()ed. */
1194 assert(old_type_details->arena);
1195 del_body((void*)((char*)old_body + old_type_details->offset),
1196 &PL_body_roots[old_type]);
1202 Perl_hv_auxalloc(pTHX_ HV *hv) {
1203 const struct body_details *old_type_details = bodies_by_type + SVt_PVHV;
1207 PERL_ARGS_ASSERT_HV_AUXALLOC;
1208 assert(SvTYPE(hv) == SVt_PVHV);
1209 assert(!HvHasAUX(hv));
1212 new_body = new_NOARENAZ(&fake_hv_with_aux);
1214 new_body_from_arena(new_body, HVAUX_ARENA_ROOT_IX, fake_hv_with_aux);
1217 old_body = SvANY(hv);
1219 Copy((char *)old_body + old_type_details->offset,
1220 (char *)new_body + fake_hv_with_aux.offset,
1221 old_type_details->copy,
1227 assert(old_type_details->arena);
1228 del_body((void*)((char*)old_body + old_type_details->offset),
1229 &PL_body_roots[SVt_PVHV]);
1232 SvANY(hv) = (XPVHV *) new_body;
1233 SvFLAGS(hv) |= SVphv_HasAUX;
1238 =for apidoc sv_backoff
1240 Remove any string offset. You should normally use the C<SvOOK_off> macro
1246 /* prior to 5.000 stable, this function returned the new OOK-less SvFLAGS
1247 prior to 5.23.4 this function always returned 0
1251 Perl_sv_backoff(SV *const sv)
1254 const char * const s = SvPVX_const(sv);
1256 PERL_ARGS_ASSERT_SV_BACKOFF;
1259 assert(SvTYPE(sv) != SVt_PVHV);
1260 assert(SvTYPE(sv) != SVt_PVAV);
1262 SvOOK_offset(sv, delta);
1264 SvLEN_set(sv, SvLEN(sv) + delta);
1265 SvPV_set(sv, SvPVX(sv) - delta);
1266 SvFLAGS(sv) &= ~SVf_OOK;
1267 Move(s, SvPVX(sv), SvCUR(sv)+1, char);
1272 /* forward declaration */
1273 static void S_sv_uncow(pTHX_ SV * const sv, const U32 flags);
1279 Expands the character buffer in the SV. If necessary, uses C<sv_unref> and
1280 upgrades the SV to C<SVt_PV>. Returns a pointer to the character buffer.
1281 Use the C<SvGROW> wrapper instead.
1288 Perl_sv_grow(pTHX_ SV *const sv, STRLEN newlen)
1292 PERL_ARGS_ASSERT_SV_GROW;
1296 if (SvTYPE(sv) < SVt_PV) {
1297 sv_upgrade(sv, SVt_PV);
1298 s = SvPVX_mutable(sv);
1300 else if (SvOOK(sv)) { /* pv is offset? */
1302 s = SvPVX_mutable(sv);
1303 if (newlen > SvLEN(sv))
1304 newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
1308 if (SvIsCOW(sv)) S_sv_uncow(aTHX_ sv, 0);
1309 s = SvPVX_mutable(sv);
1312 #ifdef PERL_COPY_ON_WRITE
1313 /* the new COW scheme uses SvPVX(sv)[SvLEN(sv)-1] (if spare)
1314 * to store the COW count. So in general, allocate one more byte than
1315 * asked for, to make it likely this byte is always spare: and thus
1316 * make more strings COW-able.
1318 * Only increment if the allocation isn't MEM_SIZE_MAX,
1319 * otherwise it will wrap to 0.
1321 if ( newlen != MEM_SIZE_MAX )
1325 #if defined(PERL_USE_MALLOC_SIZE) && defined(Perl_safesysmalloc_size)
1326 #define PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC
1329 if (newlen > SvLEN(sv)) { /* need more room? */
1330 STRLEN minlen = SvCUR(sv);
1331 minlen += (minlen >> PERL_STRLEN_EXPAND_SHIFT) + 10;
1332 if (newlen < minlen)
1334 #ifndef PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC
1336 /* Don't round up on the first allocation, as odds are pretty good that
1337 * the initial request is accurate as to what is really needed */
1339 STRLEN rounded = PERL_STRLEN_ROUNDUP(newlen);
1340 if (rounded > newlen)
1344 if (SvLEN(sv) && s) {
1345 s = (char*)saferealloc(s, newlen);
1348 s = (char*)safemalloc(newlen);
1349 if (SvPVX_const(sv) && SvCUR(sv)) {
1350 Move(SvPVX_const(sv), s, SvCUR(sv), char);
1354 #ifdef PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC
1355 /* Do this here, do it once, do it right, and then we will never get
1356 called back into sv_grow() unless there really is some growing
1358 SvLEN_set(sv, Perl_safesysmalloc_size(s));
1360 SvLEN_set(sv, newlen);
1367 =for apidoc sv_grow_fresh
1369 A cut-down version of sv_grow intended only for when sv is a freshly-minted
1370 SVt_PV, SVt_PVIV, SVt_PVNV, or SVt_PVMG. i.e. sv has the default flags, has
1371 never been any other type, and does not have an existing string. Basically,
1372 just assigns a char buffer and returns a pointer to it.
1379 Perl_sv_grow_fresh(pTHX_ SV *const sv, STRLEN newlen)
1383 PERL_ARGS_ASSERT_SV_GROW_FRESH;
1385 assert(SvTYPE(sv) >= SVt_PV && SvTYPE(sv) <= SVt_PVMG);
1388 assert(!SvIsCOW(sv));
1392 #ifdef PERL_COPY_ON_WRITE
1393 /* the new COW scheme uses SvPVX(sv)[SvLEN(sv)-1] (if spare)
1394 * to store the COW count. So in general, allocate one more byte than
1395 * asked for, to make it likely this byte is always spare: and thus
1396 * make more strings COW-able.
1398 * Only increment if the allocation isn't MEM_SIZE_MAX,
1399 * otherwise it will wrap to 0.
1401 if ( newlen != MEM_SIZE_MAX )
1405 /* 10 is a longstanding, hardcoded minimum length in sv_grow. */
1406 /* Just doing the same here for consistency. */
1410 s = (char*)safemalloc(newlen);
1413 /* No PERL_UNWARANTED_CHUMMINESS_WITH_MALLOC here, since many strings */
1414 /* will never be grown once set. Let the real sv_grow worry about that. */
1415 SvLEN_set(sv, newlen);
1420 =for apidoc sv_setiv
1421 =for apidoc_item sv_setiv_mg
1423 These copy an integer into the given SV, upgrading first if necessary.
1425 They differ only in that C<sv_setiv_mg> handles 'set' magic; C<sv_setiv> does
1432 Perl_sv_setiv(pTHX_ SV *const sv, const IV i)
1434 PERL_ARGS_ASSERT_SV_SETIV;
1436 SV_CHECK_THINKFIRST_COW_DROP(sv);
1437 switch (SvTYPE(sv)) {
1440 sv_upgrade(sv, SVt_IV);
1443 sv_upgrade(sv, SVt_PVIV);
1447 if (!isGV_with_GP(sv))
1455 /* diag_listed_as: Can't coerce %s to %s in %s */
1456 Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
1458 NOT_REACHED; /* NOTREACHED */
1462 (void)SvIOK_only(sv); /* validate number */
1468 Perl_sv_setiv_mg(pTHX_ SV *const sv, const IV i)
1470 PERL_ARGS_ASSERT_SV_SETIV_MG;
1477 =for apidoc sv_setuv
1478 =for apidoc_item sv_setuv_mg
1480 These copy an unsigned integer into the given SV, upgrading first if necessary.
1483 They differ only in that C<sv_setuv_mg> handles 'set' magic; C<sv_setuv> does
1490 Perl_sv_setuv(pTHX_ SV *const sv, const UV u)
1492 PERL_ARGS_ASSERT_SV_SETUV;
1494 /* With the if statement to ensure that integers are stored as IVs whenever
1496 u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
1499 u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
1501 If you wish to remove the following if statement, so that this routine
1502 (and its callers) always return UVs, please benchmark to see what the
1503 effect is. Modern CPUs may be different. Or may not :-)
1505 if (u <= (UV)IV_MAX) {
1506 sv_setiv(sv, (IV)u);
1515 Perl_sv_setuv_mg(pTHX_ SV *const sv, const UV u)
1517 PERL_ARGS_ASSERT_SV_SETUV_MG;
1524 =for apidoc sv_setnv
1525 =for apidoc_item sv_setnv_mg
1527 These copy a double into the given SV, upgrading first if necessary.
1529 They differ only in that C<sv_setnv_mg> handles 'set' magic; C<sv_setnv> does
1536 Perl_sv_setnv(pTHX_ SV *const sv, const NV num)
1538 PERL_ARGS_ASSERT_SV_SETNV;
1540 SV_CHECK_THINKFIRST_COW_DROP(sv);
1541 switch (SvTYPE(sv)) {
1544 sv_upgrade(sv, SVt_NV);
1548 sv_upgrade(sv, SVt_PVNV);
1552 if (!isGV_with_GP(sv))
1560 /* diag_listed_as: Can't coerce %s to %s in %s */
1561 Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
1563 NOT_REACHED; /* NOTREACHED */
1568 (void)SvNOK_only(sv); /* validate number */
1573 Perl_sv_setnv_mg(pTHX_ SV *const sv, const NV num)
1575 PERL_ARGS_ASSERT_SV_SETNV_MG;
1582 =for apidoc sv_setrv_noinc
1583 =for apidoc_item sv_setrv_noinc_mg
1585 Copies an SV pointer into the given SV as an SV reference, upgrading it if
1586 necessary. After this, C<SvRV(sv)> is equal to I<ref>. This does not adjust
1587 the reference count of I<ref>. The reference I<ref> must not be NULL.
1589 C<sv_setrv_noinc_mg> will invoke 'set' magic on the SV; C<sv_setrv_noinc> will
1596 Perl_sv_setrv_noinc(pTHX_ SV *const sv, SV *const ref)
1598 PERL_ARGS_ASSERT_SV_SETRV_NOINC;
1600 SV_CHECK_THINKFIRST_COW_DROP(sv);
1601 prepare_SV_for_RV(sv);
1609 Perl_sv_setrv_noinc_mg(pTHX_ SV *const sv, SV *const ref)
1611 PERL_ARGS_ASSERT_SV_SETRV_NOINC_MG;
1613 sv_setrv_noinc(sv, ref);
1618 =for apidoc sv_setrv_inc
1619 =for apidoc_item sv_setrv_inc_mg
1621 As C<sv_setrv_noinc> but increments the reference count of I<ref>.
1623 C<sv_setrv_inc_mg> will invoke 'set' magic on the SV; C<sv_setrv_inc> will
1630 Perl_sv_setrv_inc(pTHX_ SV *const sv, SV *const ref)
1632 PERL_ARGS_ASSERT_SV_SETRV_INC;
1634 sv_setrv_noinc(sv, SvREFCNT_inc_simple_NN(ref));
1638 Perl_sv_setrv_inc_mg(pTHX_ SV *const sv, SV *const ref)
1640 PERL_ARGS_ASSERT_SV_SETRV_INC_MG;
1642 sv_setrv_noinc(sv, SvREFCNT_inc_simple_NN(ref));
1646 /* Return a cleaned-up, printable version of sv, for non-numeric, or
1647 * not incrementable warning display.
1648 * Originally part of S_not_a_number().
1649 * The return value may be != tmpbuf.
1653 S_sv_display(pTHX_ SV *const sv, char *tmpbuf, STRLEN tmpbuf_size) {
1656 PERL_ARGS_ASSERT_SV_DISPLAY;
1659 SV *dsv = newSVpvs_flags("", SVs_TEMP);
1660 pv = sv_uni_display(dsv, sv, 32, UNI_DISPLAY_ISPRINT);
1663 const char * const limit = tmpbuf + tmpbuf_size - 8;
1664 /* each *s can expand to 4 chars + "...\0",
1665 i.e. need room for 8 chars */
1667 const char *s = SvPVX_const(sv);
1668 const char * const end = s + SvCUR(sv);
1669 for ( ; s < end && d < limit; s++ ) {
1671 if (! isASCII(ch) && !isPRINT_LC(ch)) {
1675 /* Map to ASCII "equivalent" of Latin1 */
1676 ch = LATIN1_TO_NATIVE(NATIVE_TO_LATIN1(ch) & 127);
1682 else if (ch == '\r') {
1686 else if (ch == '\f') {
1690 else if (ch == '\\') {
1694 else if (ch == '\0') {
1698 else if (isPRINT_LC(ch))
1717 /* Print an "isn't numeric" warning, using a cleaned-up,
1718 * printable version of the offending string
1722 S_not_a_number(pTHX_ SV *const sv)
1727 PERL_ARGS_ASSERT_NOT_A_NUMBER;
1729 pv = sv_display(sv, tmpbuf, sizeof(tmpbuf));
1732 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1733 /* diag_listed_as: Argument "%s" isn't numeric%s */
1734 "Argument \"%s\" isn't numeric in %s", pv,
1737 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1738 /* diag_listed_as: Argument "%s" isn't numeric%s */
1739 "Argument \"%s\" isn't numeric", pv);
1743 S_not_incrementable(pTHX_ SV *const sv) {
1747 PERL_ARGS_ASSERT_NOT_INCREMENTABLE;
1749 pv = sv_display(sv, tmpbuf, sizeof(tmpbuf));
1751 Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1752 "Argument \"%s\" treated as 0 in increment (++)", pv);
1756 =for apidoc looks_like_number
1758 Test if the content of an SV looks like a number (or is a number).
1759 C<Inf> and C<Infinity> are treated as numbers (so will not issue a
1760 non-numeric warning), even if your C<atof()> doesn't grok them. Get-magic is
1767 Perl_looks_like_number(pTHX_ SV *const sv)
1773 PERL_ARGS_ASSERT_LOOKS_LIKE_NUMBER;
1775 if (SvPOK(sv) || SvPOKp(sv)) {
1776 sbegin = SvPV_nomg_const(sv, len);
1779 return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
1780 numtype = grok_number(sbegin, len, NULL);
1781 return ((numtype & IS_NUMBER_TRAILING)) ? 0 : numtype;
1785 S_glob_2number(pTHX_ GV * const gv)
1787 PERL_ARGS_ASSERT_GLOB_2NUMBER;
1789 /* We know that all GVs stringify to something that is not-a-number,
1790 so no need to test that. */
1791 if (ckWARN(WARN_NUMERIC))
1793 SV *const buffer = sv_newmortal();
1794 gv_efullname3(buffer, gv, "*");
1795 not_a_number(buffer);
1797 /* We just want something true to return, so that S_sv_2iuv_common
1798 can tail call us and return true. */
1802 /* Actually, ISO C leaves conversion of UV to IV undefined, but
1803 until proven guilty, assume that things are not that bad... */
1808 As 64 bit platforms often have an NV that doesn't preserve all bits of
1809 an IV (an assumption perl has been based on to date) it becomes necessary
1810 to remove the assumption that the NV always carries enough precision to
1811 recreate the IV whenever needed, and that the NV is the canonical form.
1812 Instead, IV/UV and NV need to be given equal rights. So as to not lose
1813 precision as a side effect of conversion (which would lead to insanity
1814 and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
1815 1) to distinguish between IV/UV/NV slots that have a valid conversion cached
1816 where precision was lost, and IV/UV/NV slots that have a valid conversion
1817 which has lost no precision
1818 2) to ensure that if a numeric conversion to one form is requested that
1819 would lose precision, the precise conversion (or differently
1820 imprecise conversion) is also performed and cached, to prevent
1821 requests for different numeric formats on the same SV causing
1822 lossy conversion chains. (lossless conversion chains are perfectly
1827 SvIOKp is true if the IV slot contains a valid value
1828 SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
1829 SvNOKp is true if the NV slot contains a valid value
1830 SvNOK is true only if the NV value is accurate
1833 while converting from PV to NV, check to see if converting that NV to an
1834 IV(or UV) would lose accuracy over a direct conversion from PV to
1835 IV(or UV). If it would, cache both conversions, return NV, but mark
1836 SV as IOK NOKp (ie not NOK).
1838 While converting from PV to IV, check to see if converting that IV to an
1839 NV would lose accuracy over a direct conversion from PV to NV. If it
1840 would, cache both conversions, flag similarly.
1842 Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
1843 correctly because if IV & NV were set NV *always* overruled.
1844 Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
1845 changes - now IV and NV together means that the two are interchangeable:
1846 SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
1848 The benefit of this is that operations such as pp_add know that if
1849 SvIOK is true for both left and right operands, then integer addition
1850 can be used instead of floating point (for cases where the result won't
1851 overflow). Before, floating point was always used, which could lead to
1852 loss of precision compared with integer addition.
1854 * making IV and NV equal status should make maths accurate on 64 bit
1856 * may speed up maths somewhat if pp_add and friends start to use
1857 integers when possible instead of fp. (Hopefully the overhead in
1858 looking for SvIOK and checking for overflow will not outweigh the
1859 fp to integer speedup)
1860 * will slow down integer operations (callers of SvIV) on "inaccurate"
1861 values, as the change from SvIOK to SvIOKp will cause a call into
1862 sv_2iv each time rather than a macro access direct to the IV slot
1863 * should speed up number->string conversion on integers as IV is
1864 favoured when IV and NV are equally accurate
1866 ####################################################################
1867 You had better be using SvIOK_notUV if you want an IV for arithmetic:
1868 SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
1869 On the other hand, SvUOK is true iff UV.
1870 ####################################################################
1872 Your mileage will vary depending your CPU's relative fp to integer
1876 #ifndef NV_PRESERVES_UV
1877 # define IS_NUMBER_UNDERFLOW_IV 1
1878 # define IS_NUMBER_UNDERFLOW_UV 2
1879 # define IS_NUMBER_IV_AND_UV 2
1880 # define IS_NUMBER_OVERFLOW_IV 4
1881 # define IS_NUMBER_OVERFLOW_UV 5
1883 /* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
1885 /* For sv_2nv these three cases are "SvNOK and don't bother casting" */
1887 S_sv_2iuv_non_preserve(pTHX_ SV *const sv
1893 PERL_ARGS_ASSERT_SV_2IUV_NON_PRESERVE;
1894 PERL_UNUSED_CONTEXT;
1896 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));
1897 if (SvNVX(sv) < (NV)IV_MIN) {
1898 (void)SvIOKp_on(sv);
1900 SvIV_set(sv, IV_MIN);
1901 return IS_NUMBER_UNDERFLOW_IV;
1903 if (SvNVX(sv) > (NV)UV_MAX) {
1904 (void)SvIOKp_on(sv);
1907 SvUV_set(sv, UV_MAX);
1908 return IS_NUMBER_OVERFLOW_UV;
1910 (void)SvIOKp_on(sv);
1912 /* Can't use strtol etc to convert this string. (See truth table in
1914 if (SvNVX(sv) < IV_MAX_P1) {
1915 SvIV_set(sv, I_V(SvNVX(sv)));
1916 if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
1917 SvIOK_on(sv); /* Integer is precise. NOK, IOK */
1919 /* Integer is imprecise. NOK, IOKp */
1921 return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
1924 SvUV_set(sv, U_V(SvNVX(sv)));
1925 if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
1926 if (SvUVX(sv) == UV_MAX) {
1927 /* As we know that NVs don't preserve UVs, UV_MAX cannot
1928 possibly be preserved by NV. Hence, it must be overflow.
1930 return IS_NUMBER_OVERFLOW_UV;
1932 SvIOK_on(sv); /* Integer is precise. NOK, UOK */
1934 /* Integer is imprecise. NOK, IOKp */
1936 return IS_NUMBER_OVERFLOW_IV;
1938 #endif /* !NV_PRESERVES_UV*/
1940 /* If numtype is infnan, set the NV of the sv accordingly.
1941 * If numtype is anything else, try setting the NV using Atof(PV). */
1943 S_sv_setnv(pTHX_ SV* sv, int numtype)
1945 bool pok = cBOOL(SvPOK(sv));
1948 if ((numtype & IS_NUMBER_INFINITY)) {
1949 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -NV_INF : NV_INF);
1954 if ((numtype & IS_NUMBER_NAN)) {
1955 SvNV_set(sv, NV_NAN);
1960 SvNV_set(sv, Atof(SvPVX_const(sv)));
1961 /* Purposefully no true nok here, since we don't want to blow
1962 * away the possible IOK/UV of an existing sv. */
1965 SvNOK_only(sv); /* No IV or UV please, this is pure infnan. */
1967 SvPOK_on(sv); /* PV is okay, though. */
1972 S_sv_2iuv_common(pTHX_ SV *const sv)
1974 PERL_ARGS_ASSERT_SV_2IUV_COMMON;
1977 /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
1978 * without also getting a cached IV/UV from it at the same time
1979 * (ie PV->NV conversion should detect loss of accuracy and cache
1980 * IV or UV at same time to avoid this. */
1981 /* IV-over-UV optimisation - choose to cache IV if possible */
1983 if (SvTYPE(sv) == SVt_NV)
1984 sv_upgrade(sv, SVt_PVNV);
1987 (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
1988 /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
1989 certainly cast into the IV range at IV_MAX, whereas the correct
1990 answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
1992 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
1993 if (Perl_isnan(SvNVX(sv))) {
1999 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2000 SvIV_set(sv, I_V(SvNVX(sv)));
2001 if (SvNVX(sv) == (NV) SvIVX(sv)
2002 #ifndef NV_PRESERVES_UV
2003 && SvIVX(sv) != IV_MIN /* avoid negating IV_MIN below */
2004 && (((UV)1 << NV_PRESERVES_UV_BITS) >
2005 (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2006 /* Don't flag it as "accurately an integer" if the number
2007 came from a (by definition imprecise) NV operation, and
2008 we're outside the range of NV integer precision */
2012 SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2014 /* scalar has trailing garbage, eg "42a" */
2016 DEBUG_c(PerlIO_printf(Perl_debug_log,
2017 "0x%" UVxf " iv(%" NVgf " => %" IVdf ") (precise)\n",
2023 /* IV not precise. No need to convert from PV, as NV
2024 conversion would already have cached IV if it detected
2025 that PV->IV would be better than PV->NV->IV
2026 flags already correct - don't set public IOK. */
2027 DEBUG_c(PerlIO_printf(Perl_debug_log,
2028 "0x%" UVxf " iv(%" NVgf " => %" IVdf ") (imprecise)\n",
2033 /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2034 but the cast (NV)IV_MIN rounds to a the value less (more
2035 negative) than IV_MIN which happens to be equal to SvNVX ??
2036 Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2037 NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2038 (NV)UVX == NVX are both true, but the values differ. :-(
2039 Hopefully for 2s complement IV_MIN is something like
2040 0x8000000000000000 which will be exact. NWC */
2043 SvUV_set(sv, U_V(SvNVX(sv)));
2045 (SvNVX(sv) == (NV) SvUVX(sv))
2046 #ifndef NV_PRESERVES_UV
2047 /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2048 /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2049 && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2050 /* Don't flag it as "accurately an integer" if the number
2051 came from a (by definition imprecise) NV operation, and
2052 we're outside the range of NV integer precision */
2058 DEBUG_c(PerlIO_printf(Perl_debug_log,
2059 "0x%" UVxf " 2iv(%" UVuf " => %" IVdf ") (as unsigned)\n",
2065 else if (SvPOKp(sv)) {
2068 const char *s = SvPVX_const(sv);
2069 const STRLEN cur = SvCUR(sv);
2071 /* short-cut for a single digit string like "1" */
2076 if (SvTYPE(sv) < SVt_PVIV)
2077 sv_upgrade(sv, SVt_PVIV);
2079 SvIV_set(sv, (IV)(c - '0'));
2084 numtype = grok_number(s, cur, &value);
2085 /* We want to avoid a possible problem when we cache an IV/ a UV which
2086 may be later translated to an NV, and the resulting NV is not
2087 the same as the direct translation of the initial string
2088 (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2089 be careful to ensure that the value with the .456 is around if the
2090 NV value is requested in the future).
2092 This means that if we cache such an IV/a UV, we need to cache the
2093 NV as well. Moreover, we trade speed for space, and do not
2094 cache the NV if we are sure it's not needed.
2097 /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2098 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2099 == IS_NUMBER_IN_UV) {
2100 /* It's definitely an integer, only upgrade to PVIV */
2101 if (SvTYPE(sv) < SVt_PVIV)
2102 sv_upgrade(sv, SVt_PVIV);
2104 } else if (SvTYPE(sv) < SVt_PVNV)
2105 sv_upgrade(sv, SVt_PVNV);
2107 if ((numtype & (IS_NUMBER_INFINITY | IS_NUMBER_NAN))) {
2108 if (ckWARN(WARN_NUMERIC) && ((numtype & IS_NUMBER_TRAILING)))
2110 S_sv_setnv(aTHX_ sv, numtype);
2111 goto got_nv; /* Fill IV/UV slot and set IOKp */
2114 /* If NVs preserve UVs then we only use the UV value if we know that
2115 we aren't going to call atof() below. If NVs don't preserve UVs
2116 then the value returned may have more precision than atof() will
2117 return, even though value isn't perfectly accurate. */
2118 if ((numtype & (IS_NUMBER_IN_UV
2119 #ifdef NV_PRESERVES_UV
2122 )) == IS_NUMBER_IN_UV) {
2123 /* This won't turn off the public IOK flag if it was set above */
2124 (void)SvIOKp_on(sv);
2126 if (!(numtype & IS_NUMBER_NEG)) {
2128 if (value <= (UV)IV_MAX) {
2129 SvIV_set(sv, (IV)value);
2131 /* it didn't overflow, and it was positive. */
2132 SvUV_set(sv, value);
2136 /* 2s complement assumption */
2137 if (value <= (UV)IV_MIN) {
2138 SvIV_set(sv, value == (UV)IV_MIN
2139 ? IV_MIN : -(IV)value);
2141 /* Too negative for an IV. This is a double upgrade, but
2142 I'm assuming it will be rare. */
2143 if (SvTYPE(sv) < SVt_PVNV)
2144 sv_upgrade(sv, SVt_PVNV);
2148 SvNV_set(sv, -(NV)value);
2149 SvIV_set(sv, IV_MIN);
2153 /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2154 will be in the previous block to set the IV slot, and the next
2155 block to set the NV slot. So no else here. */
2157 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2158 != IS_NUMBER_IN_UV) {
2159 /* It wasn't an (integer that doesn't overflow the UV). */
2160 S_sv_setnv(aTHX_ sv, numtype);
2162 if (! numtype && ckWARN(WARN_NUMERIC))
2165 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2iv(%" NVgf ")\n",
2166 PTR2UV(sv), SvNVX(sv)));
2168 #ifdef NV_PRESERVES_UV
2172 goto got_nv; /* Fill IV/UV slot and set IOKp, maybe IOK */
2173 #else /* NV_PRESERVES_UV */
2174 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2175 == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2176 /* The IV/UV slot will have been set from value returned by
2177 grok_number above. The NV slot has just been set using
2180 assert (SvIOKp(sv));
2182 if (((UV)1 << NV_PRESERVES_UV_BITS) >
2183 U_V(Perl_fabs(SvNVX(sv)))) {
2184 /* Small enough to preserve all bits. */
2185 (void)SvIOKp_on(sv);
2187 SvIV_set(sv, I_V(SvNVX(sv)));
2188 if ((NV)(SvIVX(sv)) == SvNVX(sv))
2190 /* There had been runtime checking for
2191 "U_V(Perl_fabs(SvNVX(sv))) < (UV)IV_MAX" here to ensure
2192 that this NV is in the preserved range, but this should
2193 be always true if the following assertion is true: */
2194 STATIC_ASSERT_STMT(((UV)1 << NV_PRESERVES_UV_BITS) <=
2198 0 0 already failed to read UV.
2199 0 1 already failed to read UV.
2200 1 0 you won't get here in this case. IV/UV
2201 slot set, public IOK, Atof() unneeded.
2202 1 1 already read UV.
2203 so there's no point in sv_2iuv_non_preserve() attempting
2204 to use atol, strtol, strtoul etc. */
2206 sv_2iuv_non_preserve (sv, numtype);
2208 sv_2iuv_non_preserve (sv);
2212 /* It might be more code efficient to go through the entire logic above
2213 and conditionally set with SvIOKp_on() rather than SvIOK(), but it
2214 gets complex and potentially buggy, so more programmer efficient
2215 to do it this way, by turning off the public flags: */
2217 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK);
2218 #endif /* NV_PRESERVES_UV */
2222 if (isGV_with_GP(sv))
2223 return glob_2number(MUTABLE_GV(sv));
2225 if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
2227 if (SvTYPE(sv) < SVt_IV)
2228 /* Typically the caller expects that sv_any is not NULL now. */
2229 sv_upgrade(sv, SVt_IV);
2230 /* Return 0 from the caller. */
2237 =for apidoc sv_2iv_flags
2239 Return the integer value of an SV, doing any necessary string
2240 conversion. If C<flags> has the C<SV_GMAGIC> bit set, does an C<mg_get()> first.
2241 Normally used via the C<SvIV(sv)> and C<SvIVx(sv)> macros.
2247 Perl_sv_2iv_flags(pTHX_ SV *const sv, const I32 flags)
2249 PERL_ARGS_ASSERT_SV_2IV_FLAGS;
2251 assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
2252 && SvTYPE(sv) != SVt_PVFM);
2254 if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
2260 if (flags & SV_SKIP_OVERLOAD)
2262 tmpstr = AMG_CALLunary(sv, numer_amg);
2263 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2264 return SvIV(tmpstr);
2267 return PTR2IV(SvRV(sv));
2270 if (SvVALID(sv) || isREGEXP(sv)) {
2271 /* FBMs use the space for SvIVX and SvNVX for other purposes, so
2272 must not let them cache IVs.
2273 In practice they are extremely unlikely to actually get anywhere
2274 accessible by user Perl code - the only way that I'm aware of is when
2275 a constant subroutine which is used as the second argument to index.
2277 Regexps have no SvIVX and SvNVX fields.
2282 const char * const ptr =
2283 isREGEXP(sv) ? RX_WRAPPED((REGEXP*)sv) : SvPVX_const(sv);
2285 = grok_number(ptr, SvCUR(sv), &value);
2287 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2288 == IS_NUMBER_IN_UV) {
2289 /* It's definitely an integer */
2290 if (numtype & IS_NUMBER_NEG) {
2291 if (value < (UV)IV_MIN)
2294 if (value < (UV)IV_MAX)
2299 /* Quite wrong but no good choices. */
2300 if ((numtype & IS_NUMBER_INFINITY)) {
2301 return (numtype & IS_NUMBER_NEG) ? IV_MIN : IV_MAX;
2302 } else if ((numtype & IS_NUMBER_NAN)) {
2303 return 0; /* So wrong. */
2307 if (ckWARN(WARN_NUMERIC))
2310 return I_V(Atof(ptr));
2314 if (SvTHINKFIRST(sv)) {
2315 if (SvREADONLY(sv) && !SvOK(sv)) {
2316 if (ckWARN(WARN_UNINITIALIZED))
2323 if (S_sv_2iuv_common(aTHX_ sv))
2327 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2iv(%" IVdf ")\n",
2328 PTR2UV(sv),SvIVX(sv)));
2329 return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
2333 =for apidoc sv_2uv_flags
2335 Return the unsigned integer value of an SV, doing any necessary string
2336 conversion. If C<flags> has the C<SV_GMAGIC> bit set, does an C<mg_get()> first.
2337 Normally used via the C<SvUV(sv)> and C<SvUVx(sv)> macros.
2339 =for apidoc Amnh||SV_GMAGIC
2345 Perl_sv_2uv_flags(pTHX_ SV *const sv, const I32 flags)
2347 PERL_ARGS_ASSERT_SV_2UV_FLAGS;
2349 if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
2355 if (flags & SV_SKIP_OVERLOAD)
2357 tmpstr = AMG_CALLunary(sv, numer_amg);
2358 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2359 return SvUV(tmpstr);
2362 return PTR2UV(SvRV(sv));
2365 if (SvVALID(sv) || isREGEXP(sv)) {
2366 /* FBMs use the space for SvIVX and SvNVX for other purposes, and use
2367 the same flag bit as SVf_IVisUV, so must not let them cache IVs.
2368 Regexps have no SvIVX and SvNVX fields. */
2372 const char * const ptr =
2373 isREGEXP(sv) ? RX_WRAPPED((REGEXP*)sv) : SvPVX_const(sv);
2375 = grok_number(ptr, SvCUR(sv), &value);
2377 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2378 == IS_NUMBER_IN_UV) {
2379 /* It's definitely an integer */
2380 if (!(numtype & IS_NUMBER_NEG))
2384 /* Quite wrong but no good choices. */
2385 if ((numtype & IS_NUMBER_INFINITY)) {
2386 return UV_MAX; /* So wrong. */
2387 } else if ((numtype & IS_NUMBER_NAN)) {
2388 return 0; /* So wrong. */
2392 if (ckWARN(WARN_NUMERIC))
2395 return U_V(Atof(ptr));
2399 if (SvTHINKFIRST(sv)) {
2400 if (SvREADONLY(sv) && !SvOK(sv)) {
2401 if (ckWARN(WARN_UNINITIALIZED))
2408 if (S_sv_2iuv_common(aTHX_ sv))
2412 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2uv(%" UVuf ")\n",
2413 PTR2UV(sv),SvUVX(sv)));
2414 return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
2418 =for apidoc sv_2nv_flags
2420 Return the num value of an SV, doing any necessary string or integer
2421 conversion. If C<flags> has the C<SV_GMAGIC> bit set, does an C<mg_get()> first.
2422 Normally used via the C<SvNV(sv)> and C<SvNVx(sv)> macros.
2428 Perl_sv_2nv_flags(pTHX_ SV *const sv, const I32 flags)
2430 PERL_ARGS_ASSERT_SV_2NV_FLAGS;
2432 assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
2433 && SvTYPE(sv) != SVt_PVFM);
2434 if (SvGMAGICAL(sv) || SvVALID(sv) || isREGEXP(sv)) {
2435 /* FBMs use the space for SvIVX and SvNVX for other purposes, and use
2436 the same flag bit as SVf_IVisUV, so must not let them cache NVs.
2437 Regexps have no SvIVX and SvNVX fields. */
2439 if (flags & SV_GMAGIC)
2443 if (SvPOKp(sv) && !SvIOKp(sv)) {
2444 ptr = SvPVX_const(sv);
2445 if (!SvIOKp(sv) && ckWARN(WARN_NUMERIC) &&
2446 !grok_number(ptr, SvCUR(sv), NULL))
2452 return (NV)SvUVX(sv);
2454 return (NV)SvIVX(sv);
2459 assert(SvTYPE(sv) >= SVt_PVMG);
2460 /* This falls through to the report_uninit near the end of the
2462 } else if (SvTHINKFIRST(sv)) {
2467 if (flags & SV_SKIP_OVERLOAD)
2469 tmpstr = AMG_CALLunary(sv, numer_amg);
2470 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2471 return SvNV(tmpstr);
2474 return PTR2NV(SvRV(sv));
2476 if (SvREADONLY(sv) && !SvOK(sv)) {
2477 if (ckWARN(WARN_UNINITIALIZED))
2482 if (SvTYPE(sv) < SVt_NV) {
2483 /* The logic to use SVt_PVNV if necessary is in sv_upgrade. */
2484 sv_upgrade(sv, SVt_NV);
2485 CLANG_DIAG_IGNORE_STMT(-Wthread-safety);
2487 DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
2488 STORE_LC_NUMERIC_SET_STANDARD();
2489 PerlIO_printf(Perl_debug_log,
2490 "0x%" UVxf " num(%" NVgf ")\n",
2491 PTR2UV(sv), SvNVX(sv));
2492 RESTORE_LC_NUMERIC();
2494 CLANG_DIAG_RESTORE_STMT;
2497 else if (SvTYPE(sv) < SVt_PVNV)
2498 sv_upgrade(sv, SVt_PVNV);
2503 SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
2504 #ifdef NV_PRESERVES_UV
2510 /* Only set the public NV OK flag if this NV preserves the IV */
2511 /* Check it's not 0xFFFFFFFFFFFFFFFF */
2513 SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
2514 : (SvIVX(sv) == I_V(SvNVX(sv))))
2520 else if (SvPOKp(sv)) {
2522 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2523 if (!SvIOKp(sv) && !numtype && ckWARN(WARN_NUMERIC))
2525 #ifdef NV_PRESERVES_UV
2526 if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2527 == IS_NUMBER_IN_UV) {
2528 /* It's definitely an integer */
2529 SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
2531 S_sv_setnv(aTHX_ sv, numtype);
2538 SvNV_set(sv, Atof(SvPVX_const(sv)));
2539 /* Only set the public NV OK flag if this NV preserves the value in
2540 the PV at least as well as an IV/UV would.
2541 Not sure how to do this 100% reliably. */
2542 /* if that shift count is out of range then Configure's test is
2543 wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
2545 if (((UV)1 << NV_PRESERVES_UV_BITS) > U_V(Perl_fabs(SvNVX(sv)))) {
2546 SvNOK_on(sv); /* Definitely small enough to preserve all bits */
2547 } else if (!(numtype & IS_NUMBER_IN_UV)) {
2548 /* Can't use strtol etc to convert this string, so don't try.
2549 sv_2iv and sv_2uv will use the NV to convert, not the PV. */
2552 /* value has been set. It may not be precise. */
2553 if ((numtype & IS_NUMBER_NEG) && (value >= (UV)IV_MIN)) {
2554 /* 2s complement assumption for (UV)IV_MIN */
2555 SvNOK_on(sv); /* Integer is too negative. */
2560 if (numtype & IS_NUMBER_NEG) {
2561 /* -IV_MIN is undefined, but we should never reach
2562 * this point with both IS_NUMBER_NEG and value ==
2564 assert(value != (UV)IV_MIN);
2565 SvIV_set(sv, -(IV)value);
2566 } else if (value <= (UV)IV_MAX) {
2567 SvIV_set(sv, (IV)value);
2569 SvUV_set(sv, value);
2573 if (numtype & IS_NUMBER_NOT_INT) {
2574 /* I believe that even if the original PV had decimals,
2575 they are lost beyond the limit of the FP precision.
2576 However, neither is canonical, so both only get p
2577 flags. NWC, 2000/11/25 */
2578 /* Both already have p flags, so do nothing */
2580 const NV nv = SvNVX(sv);
2581 /* XXX should this spot have NAN_COMPARE_BROKEN, too? */
2582 if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2583 if (SvIVX(sv) == I_V(nv)) {
2586 /* It had no "." so it must be integer. */
2590 /* between IV_MAX and NV(UV_MAX).
2591 Could be slightly > UV_MAX */
2593 if (numtype & IS_NUMBER_NOT_INT) {
2594 /* UV and NV both imprecise. */
2596 const UV nv_as_uv = U_V(nv);
2598 if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
2607 /* It might be more code efficient to go through the entire logic above
2608 and conditionally set with SvNOKp_on() rather than SvNOK(), but it
2609 gets complex and potentially buggy, so more programmer efficient
2610 to do it this way, by turning off the public flags: */
2612 SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK);
2613 #endif /* NV_PRESERVES_UV */
2616 if (isGV_with_GP(sv)) {
2617 glob_2number(MUTABLE_GV(sv));
2621 if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
2623 assert (SvTYPE(sv) >= SVt_NV);
2624 /* Typically the caller expects that sv_any is not NULL now. */
2625 /* XXX Ilya implies that this is a bug in callers that assume this
2626 and ideally should be fixed. */
2629 CLANG_DIAG_IGNORE_STMT(-Wthread-safety);
2631 DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
2632 STORE_LC_NUMERIC_SET_STANDARD();
2633 PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2nv(%" NVgf ")\n",
2634 PTR2UV(sv), SvNVX(sv));
2635 RESTORE_LC_NUMERIC();
2637 CLANG_DIAG_RESTORE_STMT;
2644 Return an SV with the numeric value of the source SV, doing any necessary
2645 reference or overload conversion. The caller is expected to have handled
2652 Perl_sv_2num(pTHX_ SV *const sv)
2654 PERL_ARGS_ASSERT_SV_2NUM;
2659 SV * const tmpsv = AMG_CALLunary(sv, numer_amg);
2660 TAINT_IF(tmpsv && SvTAINTED(tmpsv));
2661 if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
2662 return sv_2num(tmpsv);
2664 return sv_2mortal(newSVuv(PTR2UV(SvRV(sv))));
2667 /* int2str_table: lookup table containing string representations of all
2668 * two digit numbers. For example, int2str_table.arr[0] is "00" and
2669 * int2str_table.arr[12*2] is "12".
2671 * We are going to read two bytes at a time, so we have to ensure that
2672 * the array is aligned to a 2 byte boundary. That's why it was made a
2673 * union with a dummy U16 member. */
2674 static const union {
2677 } int2str_table = {{
2678 '0', '0', '0', '1', '0', '2', '0', '3', '0', '4', '0', '5', '0', '6',
2679 '0', '7', '0', '8', '0', '9', '1', '0', '1', '1', '1', '2', '1', '3',
2680 '1', '4', '1', '5', '1', '6', '1', '7', '1', '8', '1', '9', '2', '0',
2681 '2', '1', '2', '2', '2', '3', '2', '4', '2', '5', '2', '6', '2', '7',
2682 '2', '8', '2', '9', '3', '0', '3', '1', '3', '2', '3', '3', '3', '4',
2683 '3', '5', '3', '6', '3', '7', '3', '8', '3', '9', '4', '0', '4', '1',
2684 '4', '2', '4', '3', '4', '4', '4', '5', '4', '6', '4', '7', '4', '8',
2685 '4', '9', '5', '0', '5', '1', '5', '2', '5', '3', '5', '4', '5', '5',
2686 '5', '6', '5', '7', '5', '8', '5', '9', '6', '0', '6', '1', '6', '2',
2687 '6', '3', '6', '4', '6', '5', '6', '6', '6', '7', '6', '8', '6', '9',
2688 '7', '0', '7', '1', '7', '2', '7', '3', '7', '4', '7', '5', '7', '6',
2689 '7', '7', '7', '8', '7', '9', '8', '0', '8', '1', '8', '2', '8', '3',
2690 '8', '4', '8', '5', '8', '6', '8', '7', '8', '8', '8', '9', '9', '0',
2691 '9', '1', '9', '2', '9', '3', '9', '4', '9', '5', '9', '6', '9', '7',
2695 /* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
2696 * UV as a string towards the end of buf, and return pointers to start and
2699 * We assume that buf is at least TYPE_CHARS(UV) long.
2702 PERL_STATIC_INLINE char *
2703 S_uiv_2buf(char *const buf, const IV iv, UV uv, const int is_uv, char **const peob)
2705 char *ptr = buf + TYPE_CHARS(UV);
2706 char * const ebuf = ptr;
2708 U16 *word_ptr, *word_table;
2710 PERL_ARGS_ASSERT_UIV_2BUF;
2712 /* ptr has to be properly aligned, because we will cast it to U16* */
2713 assert(PTR2nat(ptr) % 2 == 0);
2714 /* we are going to read/write two bytes at a time */
2715 word_ptr = (U16*)ptr;
2716 word_table = (U16*)int2str_table.arr;
2718 if (UNLIKELY(is_uv))
2724 /* Using 0- here to silence bogus warning from MS VC */
2725 uv = (UV) (0 - (UV) iv);
2730 *--word_ptr = word_table[uv % 100];
2733 ptr = (char*)word_ptr;
2736 *--ptr = (char)uv + '0';
2738 *--word_ptr = word_table[uv];
2739 ptr = (char*)word_ptr;
2749 /* Helper for sv_2pv_flags and sv_vcatpvfn_flags. If the NV is an
2750 * infinity or a not-a-number, writes the appropriate strings to the
2751 * buffer, including a zero byte. On success returns the written length,
2752 * excluding the zero byte, on failure (not an infinity, not a nan)
2753 * returns zero, assert-fails on maxlen being too short.
2755 * XXX for "Inf", "-Inf", and "NaN", we could have three read-only
2756 * shared string constants we point to, instead of generating a new
2757 * string for each instance. */
2759 S_infnan_2pv(NV nv, char* buffer, size_t maxlen, char plus) {
2761 assert(maxlen >= 4);
2762 if (Perl_isinf(nv)) {
2764 if (maxlen < 5) /* "-Inf\0" */
2774 else if (Perl_isnan(nv)) {
2778 /* XXX optionally output the payload mantissa bits as
2779 * "(unsigned)" (to match the nan("...") C99 function,
2780 * or maybe as "(0xhhh...)" would make more sense...
2781 * provide a format string so that the user can decide?
2782 * NOTE: would affect the maxlen and assert() logic.*/
2787 assert((s == buffer + 3) || (s == buffer + 4));
2794 =for apidoc_item sv_2pv_flags
2796 These implement the various forms of the L<perlapi/C<SvPV>> macros.
2797 The macros are the preferred interface.
2799 These return a pointer to the string value of an SV (coercing it to a string if
2800 necessary), and set C<*lp> to its length in bytes.
2802 The forms differ in that plain C<sv_2pvbyte> always processes 'get' magic; and
2803 C<sv_2pvbyte_flags> processes 'get' magic if and only if C<flags> contains
2806 =for apidoc Amnh||SV_GMAGIC
2812 Perl_sv_2pv_flags(pTHX_ SV *const sv, STRLEN *const lp, const U32 flags)
2816 PERL_ARGS_ASSERT_SV_2PV_FLAGS;
2818 assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
2819 && SvTYPE(sv) != SVt_PVFM);
2820 if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
2825 if (flags & SV_SKIP_OVERLOAD)
2827 tmpstr = AMG_CALLunary(sv, string_amg);
2828 TAINT_IF(tmpstr && SvTAINTED(tmpstr));
2829 if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
2831 /* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr);
2835 if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) {
2836 if (flags & SV_CONST_RETURN) {
2837 pv = (char *) SvPVX_const(tmpstr);
2839 pv = (flags & SV_MUTABLE_RETURN)
2840 ? SvPVX_mutable(tmpstr) : SvPVX(tmpstr);
2843 *lp = SvCUR(tmpstr);
2845 pv = sv_2pv_flags(tmpstr, lp, flags);
2858 SV *const referent = SvRV(sv);
2862 retval = buffer = savepvn("NULLREF", len);
2863 } else if (SvTYPE(referent) == SVt_REGEXP &&
2864 (!(PL_curcop->cop_hints & HINT_NO_AMAGIC) ||
2865 amagic_is_enabled(string_amg))) {
2866 REGEXP * const re = (REGEXP *)MUTABLE_PTR(referent);
2870 /* If the regex is UTF-8 we want the containing scalar to
2871 have an UTF-8 flag too */
2878 *lp = RX_WRAPLEN(re);
2880 return RX_WRAPPED(re);
2882 const char *const typestring = sv_reftype(referent, 0);
2883 const STRLEN typelen = strlen(typestring);
2884 UV addr = PTR2UV(referent);
2885 const char *stashname = NULL;
2886 STRLEN stashnamelen = 0; /* hush, gcc */
2887 const char *buffer_end;
2889 if (SvOBJECT(referent)) {
2890 const HEK *const name = HvNAME_HEK(SvSTASH(referent));
2893 stashname = HEK_KEY(name);
2894 stashnamelen = HEK_LEN(name);
2896 if (HEK_UTF8(name)) {
2902 stashname = "__ANON__";
2905 len = stashnamelen + 1 /* = */ + typelen + 3 /* (0x */
2906 + 2 * sizeof(UV) + 2 /* )\0 */;
2908 len = typelen + 3 /* (0x */
2909 + 2 * sizeof(UV) + 2 /* )\0 */;
2912 Newx(buffer, len, char);
2913 buffer_end = retval = buffer + len;
2915 /* Working backwards */
2919 *--retval = PL_hexdigit[addr & 15];
2920 } while (addr >>= 4);
2926 memcpy(retval, typestring, typelen);
2930 retval -= stashnamelen;
2931 memcpy(retval, stashname, stashnamelen);
2933 /* retval may not necessarily have reached the start of the
2935 assert (retval >= buffer);
2937 len = buffer_end - retval - 1; /* -1 for that \0 */
2949 if (flags & SV_MUTABLE_RETURN)
2950 return SvPVX_mutable(sv);
2951 if (flags & SV_CONST_RETURN)
2952 return (char *)SvPVX_const(sv);
2957 /* I'm assuming that if both IV and NV are equally valid then
2958 converting the IV is going to be more efficient */
2959 const U32 isUIOK = SvIsUV(sv);
2960 /* The purpose of this union is to ensure that arr is aligned on
2961 a 2 byte boundary, because that is what uiv_2buf() requires */
2963 char arr[TYPE_CHARS(UV)];
2969 if (SvTYPE(sv) < SVt_PVIV)
2970 sv_upgrade(sv, SVt_PVIV);
2971 ptr = uiv_2buf(buf.arr, SvIVX(sv), SvUVX(sv), isUIOK, &ebuf);
2973 /* inlined from sv_setpvn */
2974 s = SvGROW_mutable(sv, len + 1);
2975 Move(ptr, s, len, char);
2978 /* We used to call SvPOK_on(). Whilst this is fine for (most) Perl code,
2979 it means that after this stringification is cached, there is no way
2980 to distinguish between values originally assigned as $a = 42; and
2981 $a = "42"; (or results of string operators vs numeric operators)
2982 where the value has subsequently been used in the other sense
2983 and had a value cached.
2984 This (somewhat) hack means that we retain the cached stringification,
2985 but don't set SVf_POK. Hence if a value is SVf_IOK|SVf_POK then it
2986 originated as "42", whereas if it's SVf_IOK then it originated as 42.
2987 (ignore SVp_IOK and SVp_POK)
2988 The SvPV macros are now updated to recognise this specific case
2989 (and that there isn't overloading or magic that could alter the
2990 cached value) and so return the cached value immediately without
2991 re-entering this function, getting back here to this block of code,
2992 and repeating the same conversion. */
2995 else if (SvNOK(sv)) {
2996 if (SvTYPE(sv) < SVt_PVNV)
2997 sv_upgrade(sv, SVt_PVNV);
2998 if (SvNVX(sv) == 0.0
2999 #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
3000 && !Perl_isnan(SvNVX(sv))
3003 s = SvGROW_mutable(sv, 2);
3008 STRLEN size = 5; /* "-Inf\0" */
3010 s = SvGROW_mutable(sv, size);
3011 len = S_infnan_2pv(SvNVX(sv), s, size, 0);
3017 /* some Xenix systems wipe out errno here */
3026 5 + /* exponent digits */
3030 s = SvGROW_mutable(sv, size);
3031 #ifndef USE_LOCALE_NUMERIC
3032 SNPRINTF_G(SvNVX(sv), s, SvLEN(sv), NV_DIG);
3038 DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
3039 STORE_LC_NUMERIC_SET_TO_NEEDED();
3041 local_radix = NOT_IN_NUMERIC_STANDARD_;
3042 if (local_radix && SvCUR(PL_numeric_radix_sv) > 1) {
3043 size += SvCUR(PL_numeric_radix_sv) - 1;
3044 s = SvGROW_mutable(sv, size);
3047 SNPRINTF_G(SvNVX(sv), s, SvLEN(sv), NV_DIG);
3049 /* If the radix character is UTF-8, and actually is in the
3050 * output, turn on the UTF-8 flag for the scalar */
3052 && SvUTF8(PL_numeric_radix_sv)
3053 && instr(s, SvPVX_const(PL_numeric_radix_sv)))
3058 RESTORE_LC_NUMERIC();
3061 /* We don't call SvPOK_on(), because it may come to
3062 * pass that the locale changes so that the
3063 * stringification we just did is no longer correct. We
3064 * will have to re-stringify every time it is needed */
3071 else if (isGV_with_GP(sv)) {
3072 GV *const gv = MUTABLE_GV(sv);
3073 SV *const buffer = sv_newmortal();
3075 gv_efullname3(buffer, gv, "*");
3077 assert(SvPOK(buffer));
3083 *lp = SvCUR(buffer);
3084 return SvPVX(buffer);
3089 if (flags & SV_UNDEF_RETURNS_NULL)
3091 if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
3093 /* Typically the caller expects that sv_any is not NULL now. */
3094 if (!SvREADONLY(sv) && SvTYPE(sv) < SVt_PV)
3095 sv_upgrade(sv, SVt_PV);
3100 const STRLEN len = s - SvPVX_const(sv);
3105 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2pv(%s)\n",
3106 PTR2UV(sv),SvPVX_const(sv)));
3107 if (flags & SV_CONST_RETURN)
3108 return (char *)SvPVX_const(sv);
3109 if (flags & SV_MUTABLE_RETURN)
3110 return SvPVX_mutable(sv);
3115 =for apidoc sv_copypv
3116 =for apidoc_item sv_copypv_flags
3117 =for apidoc_item sv_copypv_nomg
3119 These copy a stringified representation of the source SV into the
3120 destination SV. They automatically perform coercion of numeric values into
3121 strings. Guaranteed to preserve the C<UTF8> flag even from overloaded objects.
3122 Similar in nature to C<sv_2pv[_flags]> but they operate directly on an SV
3123 instead of just the string. Mostly they use L</C<sv_2pv_flags>> to
3124 do the work, except when that would lose the UTF-8'ness of the PV.
3126 The three forms differ only in whether or not they perform 'get magic' on
3127 C<sv>. C<sv_copypv_nomg> skips 'get magic'; C<sv_copypv> performs it; and
3128 C<sv_copypv_flags> either performs it (if the C<SV_GMAGIC> bit is set in
3129 C<flags>) or doesn't (if that bit is cleared).
3135 Perl_sv_copypv_flags(pTHX_ SV *const dsv, SV *const ssv, const I32 flags)
3140 PERL_ARGS_ASSERT_SV_COPYPV_FLAGS;
3142 s = SvPV_flags_const(ssv,len,(flags & SV_GMAGIC));
3143 sv_setpvn(dsv,s,len);
3151 =for apidoc sv_2pvbyte
3152 =for apidoc_item sv_2pvbyte_flags
3154 These implement the various forms of the L<perlapi/C<SvPVbyte>> macros.
3155 The macros are the preferred interface.
3157 These return a pointer to the byte-encoded representation of the SV, and set
3158 C<*lp> to its length. If the SV is marked as being encoded as UTF-8, it will
3159 be downgraded, if possible, to a byte string. If the SV cannot be downgraded,
3162 The forms differ in that plain C<sv_2pvbyte> always processes 'get' magic; and
3163 C<sv_2pvbyte_flags> processes 'get' magic if and only if C<flags> contains
3166 =for apidoc Amnh||SV_GMAGIC
3172 Perl_sv_2pvbyte_flags(pTHX_ SV *sv, STRLEN *const lp, const U32 flags)
3174 PERL_ARGS_ASSERT_SV_2PVBYTE_FLAGS;
3176 if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
3178 if (((SvREADONLY(sv) || SvFAKE(sv)) && !SvIsCOW(sv))
3179 || isGV_with_GP(sv) || SvROK(sv)) {
3180 SV *sv2 = sv_newmortal();
3181 sv_copypv_nomg(sv2,sv);
3184 sv_utf8_downgrade_nomg(sv,0);
3185 return lp ? SvPV_nomg(sv,*lp) : SvPV_nomg_nolen(sv);
3189 =for apidoc sv_2pvutf8
3190 =for apidoc_item sv_2pvutf8_flags
3192 These implement the various forms of the L<perlapi/C<SvPVutf8>> macros.
3193 The macros are the preferred interface.
3195 These return a pointer to the UTF-8-encoded representation of the SV, and set
3196 C<*lp> to its length in bytes. They may cause the SV to be upgraded to UTF-8
3199 The forms differ in that plain C<sv_2pvutf8> always processes 'get' magic; and
3200 C<sv_2pvutf8_flags> processes 'get' magic if and only if C<flags> contains
3207 Perl_sv_2pvutf8_flags(pTHX_ SV *sv, STRLEN *const lp, const U32 flags)
3209 PERL_ARGS_ASSERT_SV_2PVUTF8_FLAGS;
3211 if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
3213 if (((SvREADONLY(sv) || SvFAKE(sv)) && !SvIsCOW(sv))
3214 || isGV_with_GP(sv) || SvROK(sv)) {
3215 SV *sv2 = sv_newmortal();
3216 sv_copypv_nomg(sv2,sv);
3219 sv_utf8_upgrade_nomg(sv);
3220 return lp ? SvPV_nomg(sv,*lp) : SvPV_nomg_nolen(sv);
3225 =for apidoc sv_2bool
3227 This macro is only used by C<sv_true()> or its macro equivalent, and only if
3228 the latter's argument is neither C<SvPOK>, C<SvIOK> nor C<SvNOK>.
3229 It calls C<sv_2bool_flags> with the C<SV_GMAGIC> flag.
3231 =for apidoc sv_2bool_flags
3233 This function is only used by C<sv_true()> and friends, and only if
3234 the latter's argument is neither C<SvPOK>, C<SvIOK> nor C<SvNOK>. If the flags
3235 contain C<SV_GMAGIC>, then it does an C<mg_get()> first.
3242 Perl_sv_2bool_flags(pTHX_ SV *sv, I32 flags)
3244 PERL_ARGS_ASSERT_SV_2BOOL_FLAGS;
3247 if(flags & SV_GMAGIC) SvGETMAGIC(sv);
3253 SV * const tmpsv = AMG_CALLunary(sv, bool__amg);
3254 if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv)))) {
3257 if(SvGMAGICAL(sv)) {
3259 goto restart; /* call sv_2bool */
3261 /* expanded SvTRUE_common(sv, (flags = 0, goto restart)) */
3262 else if(!SvOK(sv)) {
3265 else if(SvPOK(sv)) {
3266 svb = SvPVXtrue(sv);
3268 else if((SvFLAGS(sv) & (SVf_IOK|SVf_NOK))) {
3269 svb = (SvIOK(sv) && SvIVX(sv) != 0)
3270 || (SvNOK(sv) && SvNVX(sv) != 0.0);
3274 goto restart; /* call sv_2bool_nomg */
3284 RX_WRAPLEN(sv) > 1 || (RX_WRAPLEN(sv) && *RX_WRAPPED(sv) != '0');
3286 if (SvNOK(sv) && !SvPOK(sv))
3287 return SvNVX(sv) != 0.0;
3289 return SvTRUE_common(sv, 0);
3293 =for apidoc sv_utf8_upgrade
3294 =for apidoc_item sv_utf8_upgrade_flags
3295 =for apidoc_item sv_utf8_upgrade_flags_grow
3296 =for apidoc_item sv_utf8_upgrade_nomg
3298 These convert the PV of an SV to its UTF-8-encoded form.
3299 The SV is forced to string form if it is not already.
3300 They always set the C<SvUTF8> flag to avoid future validity checks even if the
3301 whole string is the same in UTF-8 as not.
3302 They return the number of bytes in the converted string
3304 The forms differ in just two ways. The main difference is whether or not they
3305 perform 'get magic' on C<sv>. C<sv_utf8_upgrade_nomg> skips 'get magic';
3306 C<sv_utf8_upgrade> performs it; and C<sv_utf8_upgrade_flags> and
3307 C<sv_utf8_upgrade_flags_grow> either perform it (if the C<SV_GMAGIC> bit is set
3308 in C<flags>) or don't (if that bit is cleared).
3310 The other difference is that C<sv_utf8_upgrade_flags_grow> has an additional
3311 parameter, C<extra>, which allows the caller to specify an amount of space to
3312 be reserved as spare beyond what is needed for the actual conversion. This is
3313 used when the caller knows it will soon be needing yet more space, and it is
3314 more efficient to request space from the system in a single call.
3315 This form is otherwise identical to C<sv_utf8_upgrade_flags>.
3317 These are not a general purpose byte encoding to Unicode interface: use the
3318 Encode extension for that.
3320 The C<SV_FORCE_UTF8_UPGRADE> flag is now ignored.
3322 =for apidoc Amnh||SV_GMAGIC|
3323 =for apidoc Amnh||SV_FORCE_UTF8_UPGRADE|
3327 If the routine itself changes the string, it adds a trailing C<NUL>. Such a
3328 C<NUL> isn't guaranteed due to having other routines do the work in some input
3329 cases, or if the input is already flagged as being in utf8.
3334 Perl_sv_utf8_upgrade_flags_grow(pTHX_ SV *const sv, const I32 flags, STRLEN extra)
3336 PERL_ARGS_ASSERT_SV_UTF8_UPGRADE_FLAGS_GROW;
3338 if (sv == &PL_sv_undef)
3340 if (!SvPOK_nog(sv)) {
3342 if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
3343 (void) sv_2pv_flags(sv,&len, flags);
3345 if (extra) SvGROW(sv, SvCUR(sv) + extra);
3349 (void) SvPV_force_flags(sv,len,flags & SV_GMAGIC);
3353 /* SVt_REGEXP's shouldn't be upgraded to UTF8 - they're already
3354 * compiled and individual nodes will remain non-utf8 even if the
3355 * stringified version of the pattern gets upgraded. Whether the
3356 * PVX of a REGEXP should be grown or we should just croak, I don't
3358 if (SvUTF8(sv) || isREGEXP(sv)) {
3359 if (extra) SvGROW(sv, SvCUR(sv) + extra);
3364 S_sv_uncow(aTHX_ sv, 0);
3367 if (SvCUR(sv) == 0) {
3368 if (extra) SvGROW(sv, extra + 1); /* Make sure is room for a trailing
3370 } else { /* Assume Latin-1/EBCDIC */
3371 /* This function could be much more efficient if we
3372 * had a FLAG in SVs to signal if there are any variant
3373 * chars in the PV. Given that there isn't such a flag
3374 * make the loop as fast as possible. */
3375 U8 * s = (U8 *) SvPVX_const(sv);
3378 if (is_utf8_invariant_string_loc(s, SvCUR(sv), (const U8 **) &t)) {
3380 /* utf8 conversion not needed because all are invariants. Mark
3381 * as UTF-8 even if no variant - saves scanning loop */
3383 if (extra) SvGROW(sv, SvCUR(sv) + extra);
3387 /* Here, there is at least one variant (t points to the first one), so
3388 * the string should be converted to utf8. Everything from 's' to
3389 * 't - 1' will occupy only 1 byte each on output.
3391 * Note that the incoming SV may not have a trailing '\0', as certain
3392 * code in pp_formline can send us partially built SVs.
3394 * There are two main ways to convert. One is to create a new string
3395 * and go through the input starting from the beginning, appending each
3396 * converted value onto the new string as we go along. Going this
3397 * route, it's probably best to initially allocate enough space in the
3398 * string rather than possibly running out of space and having to
3399 * reallocate and then copy what we've done so far. Since everything
3400 * from 's' to 't - 1' is invariant, the destination can be initialized
3401 * with these using a fast memory copy. To be sure to allocate enough
3402 * space, one could use the worst case scenario, where every remaining
3403 * byte expands to two under UTF-8, or one could parse it and count
3404 * exactly how many do expand.
3406 * The other way is to unconditionally parse the remainder of the
3407 * string to figure out exactly how big the expanded string will be,
3408 * growing if needed. Then start at the end of the string and place
3409 * the character there at the end of the unfilled space in the expanded
3410 * one, working backwards until reaching 't'.
3412 * The problem with assuming the worst case scenario is that for very
3413 * long strings, we could allocate much more memory than actually
3414 * needed, which can create performance problems. If we have to parse
3415 * anyway, the second method is the winner as it may avoid an extra
3416 * copy. The code used to use the first method under some
3417 * circumstances, but now that there is faster variant counting on
3418 * ASCII platforms, the second method is used exclusively, eliminating
3419 * some code that no longer has to be maintained. */
3422 /* Count the total number of variants there are. We can start
3423 * just beyond the first one, which is known to be at 't' */
3424 const Size_t invariant_length = t - s;
3425 U8 * e = (U8 *) SvEND(sv);
3427 /* The length of the left overs, plus 1. */
3428 const Size_t remaining_length_p1 = e - t;
3430 /* We expand by 1 for the variant at 't' and one for each remaining
3431 * variant (we start looking at 't+1') */
3432 Size_t expansion = 1 + variant_under_utf8_count(t + 1, e);
3434 /* +1 = trailing NUL */
3435 Size_t need = SvCUR(sv) + expansion + extra + 1;
3438 /* Grow if needed */
3439 if (SvLEN(sv) < need) {
3440 t = invariant_length + (U8*) SvGROW(sv, need);
3441 e = t + remaining_length_p1;
3443 SvCUR_set(sv, invariant_length + remaining_length_p1 + expansion);
3445 /* Set the NUL at the end */
3446 d = (U8 *) SvEND(sv);
3449 /* Having decremented d, it points to the position to put the
3450 * very last byte of the expanded string. Go backwards through
3451 * the string, copying and expanding as we go, stopping when we
3452 * get to the part that is invariant the rest of the way down */
3456 if (NATIVE_BYTE_IS_INVARIANT(*e)) {
3459 *d-- = UTF8_EIGHT_BIT_LO(*e);
3460 *d-- = UTF8_EIGHT_BIT_HI(*e);
3465 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
3466 /* Update pos. We do it at the end rather than during
3467 * the upgrade, to avoid slowing down the common case
3468 * (upgrade without pos).
3469 * pos can be stored as either bytes or characters. Since
3470 * this was previously a byte string we can just turn off
3471 * the bytes flag. */
3472 MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
3474 mg->mg_flags &= ~MGf_BYTES;
3476 if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
3477 magic_setutf8(sv,mg); /* clear UTF8 cache */
3487 =for apidoc sv_utf8_downgrade
3488 =for apidoc_item sv_utf8_downgrade_flags
3489 =for apidoc_item sv_utf8_downgrade_nomg
3491 These attempt to convert the PV of an SV from characters to bytes. If the PV
3492 contains a character that cannot fit in a byte, this conversion will fail; in
3493 this case, C<FALSE> is returned if C<fail_ok> is true; otherwise they croak.
3495 They are not a general purpose Unicode to byte encoding interface:
3496 use the C<Encode> extension for that.
3498 They differ only in that:
3500 C<sv_utf8_downgrade> processes 'get' magic on C<sv>.
3502 C<sv_utf8_downgrade_nomg> does not.
3504 C<sv_utf8_downgrade_flags> has an additional C<flags> parameter in which you can specify
3505 C<SV_GMAGIC> to process 'get' magic, or leave it cleared to not process 'get' magic.
3511 Perl_sv_utf8_downgrade_flags(pTHX_ SV *const sv, const bool fail_ok, const U32 flags)
3513 PERL_ARGS_ASSERT_SV_UTF8_DOWNGRADE_FLAGS;
3515 if (SvPOKp(sv) && SvUTF8(sv)) {
3519 U32 mg_flags = flags & SV_GMAGIC;
3522 S_sv_uncow(aTHX_ sv, 0);
3524 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
3526 MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
3527 if (mg && mg->mg_len > 0 && mg->mg_flags & MGf_BYTES) {
3528 mg->mg_len = sv_pos_b2u_flags(sv, mg->mg_len,
3529 mg_flags|SV_CONST_RETURN);
3530 mg_flags = 0; /* sv_pos_b2u does get magic */
3532 if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
3533 magic_setutf8(sv,mg); /* clear UTF8 cache */
3536 s = (U8 *) SvPV_flags(sv, len, mg_flags);
3538 if (!utf8_to_bytes(s, &len)) {
3543 Perl_croak(aTHX_ "Wide character in %s",
3546 Perl_croak(aTHX_ "Wide character");
3557 =for apidoc sv_utf8_encode
3559 Converts the PV of an SV to UTF-8, but then turns the C<SvUTF8>
3560 flag off so that it looks like octets again.
3566 Perl_sv_utf8_encode(pTHX_ SV *const sv)
3568 PERL_ARGS_ASSERT_SV_UTF8_ENCODE;
3570 if (SvREADONLY(sv)) {
3571 sv_force_normal_flags(sv, 0);
3573 (void) sv_utf8_upgrade(sv);
3578 =for apidoc sv_utf8_decode
3580 If the PV of the SV is an octet sequence in Perl's extended UTF-8
3581 and contains a multiple-byte character, the C<SvUTF8> flag is turned on
3582 so that it looks like a character. If the PV contains only single-byte
3583 characters, the C<SvUTF8> flag stays off.
3584 Scans PV for validity and returns FALSE if the PV is invalid UTF-8.
3590 Perl_sv_utf8_decode(pTHX_ SV *const sv)
3592 PERL_ARGS_ASSERT_SV_UTF8_DECODE;
3595 const U8 *start, *c, *first_variant;
3597 /* The octets may have got themselves encoded - get them back as
3600 if (!sv_utf8_downgrade(sv, TRUE))
3603 /* it is actually just a matter of turning the utf8 flag on, but
3604 * we want to make sure everything inside is valid utf8 first.
3606 c = start = (const U8 *) SvPVX_const(sv);
3607 if (! is_utf8_invariant_string_loc(c, SvCUR(sv), &first_variant)) {
3608 if (!is_utf8_string(first_variant, SvCUR(sv) - (first_variant -c)))
3612 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
3613 /* XXX Is this dead code? XS_utf8_decode calls SvSETMAGIC
3614 after this, clearing pos. Does anything on CPAN
3616 /* adjust pos to the start of a UTF8 char sequence */
3617 MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
3619 I32 pos = mg->mg_len;
3621 for (c = start + pos; c > start; c--) {
3622 if (UTF8_IS_START(*c))
3625 mg->mg_len = c - start;
3628 if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
3629 magic_setutf8(sv,mg); /* clear UTF8 cache */
3636 =for apidoc sv_setsv
3637 =for apidoc_item sv_setsv_flags
3638 =for apidoc_item sv_setsv_mg
3639 =for apidoc_item sv_setsv_nomg
3641 These copy the contents of the source SV C<ssv> into the destination SV C<dsv>.
3642 C<ssv> may be destroyed if it is mortal, so don't use these functions if
3643 the source SV needs to be reused.
3644 Loosely speaking, they perform a copy-by-value, obliterating any previous
3645 content of the destination.
3647 They differ only in that:
3649 C<sv_setsv> calls 'get' magic on C<ssv>, but skips 'set' magic on C<dsv>.
3651 C<sv_setsv_mg> calls both 'get' magic on C<ssv> and 'set' magic on C<dsv>.
3653 C<sv_setsv_nomg> skips all magic.
3655 C<sv_setsv_flags> has a C<flags> parameter which you can use to specify any
3656 combination of magic handling, and also you can specify C<SV_NOSTEAL> so that
3657 the buffers of temps will not be stolen.
3659 You probably want to instead use one of the assortment of wrappers, such as
3660 C<L</SvSetSV>>, C<L</SvSetSV_nosteal>>, C<L</SvSetMagicSV>> and
3661 C<L</SvSetMagicSV_nosteal>>.
3663 C<sv_setsv_flags> is the primary function for copying scalars, and most other
3664 copy-ish functions and macros use it underneath.
3666 =for apidoc Amnh||SV_NOSTEAL
3672 S_glob_assign_glob(pTHX_ SV *const dsv, SV *const ssv, const int dtype)
3674 I32 mro_changes = 0; /* 1 = method, 2 = isa, 3 = recursive isa */
3675 HV *old_stash = NULL;
3677 PERL_ARGS_ASSERT_GLOB_ASSIGN_GLOB;
3679 if (dtype != SVt_PVGV && !isGV_with_GP(dsv)) {
3680 const char * const name = GvNAME(ssv);
3681 const STRLEN len = GvNAMELEN(ssv);
3683 if (dtype >= SVt_PV) {
3689 SvUPGRADE(dsv, SVt_PVGV);
3690 (void)SvOK_off(dsv);
3691 isGV_with_GP_on(dsv);
3693 GvSTASH(dsv) = GvSTASH(ssv);
3695 Perl_sv_add_backref(aTHX_ MUTABLE_SV(GvSTASH(dsv)), dsv);
3696 gv_name_set(MUTABLE_GV(dsv), name, len,
3697 GV_ADD | (GvNAMEUTF8(ssv) ? SVf_UTF8 : 0 ));
3698 SvFAKE_on(dsv); /* can coerce to non-glob */
3701 if(GvGP(MUTABLE_GV(ssv))) {
3702 /* If source has method cache entry, clear it */
3704 SvREFCNT_dec(GvCV(ssv));
3705 GvCV_set(ssv, NULL);
3708 /* If source has a real method, then a method is
3711 GvCV((const GV *)ssv) && GvSTASH(dsv) && HvENAME(GvSTASH(dsv))
3717 /* If dest already had a real method, that's a change as well */
3719 !mro_changes && GvGP(MUTABLE_GV(dsv)) && GvCVu((const GV *)dsv)
3720 && GvSTASH(dsv) && HvENAME(GvSTASH(dsv))
3725 /* We don't need to check the name of the destination if it was not a
3726 glob to begin with. */
3727 if(dtype == SVt_PVGV) {
3728 const char * const name = GvNAME((const GV *)dsv);
3729 const STRLEN len = GvNAMELEN(dsv);
3730 if(memEQs(name, len, "ISA")
3731 /* The stash may have been detached from the symbol table, so
3733 && GvSTASH(dsv) && HvENAME(GvSTASH(dsv))
3737 if ((len > 1 && name[len-2] == ':' && name[len-1] == ':')
3738 || (len == 1 && name[0] == ':')) {
3741 /* Set aside the old stash, so we can reset isa caches on
3743 if((old_stash = GvHV(dsv)))
3744 /* Make sure we do not lose it early. */
3745 SvREFCNT_inc_simple_void_NN(
3746 sv_2mortal((SV *)old_stash)
3751 SvREFCNT_inc_simple_void_NN(sv_2mortal(dsv));
3754 /* freeing dsv's GP might free ssv (e.g. *x = $x),
3755 * so temporarily protect it */
3757 SAVEFREESV(SvREFCNT_inc_simple_NN(ssv));
3758 gp_free(MUTABLE_GV(dsv));
3759 GvINTRO_off(dsv); /* one-shot flag */
3760 GvGP_set(dsv, gp_ref(GvGP(ssv)));
3765 if (GvIMPORTED(dsv) != GVf_IMPORTED
3766 && CopSTASH_ne(PL_curcop, GvSTASH(dsv)))
3771 if(mro_changes == 2) {
3772 if (GvAV((const GV *)ssv)) {
3774 SV * const sref = (SV *)GvAV((const GV *)dsv);
3775 if (SvSMAGICAL(sref) && (mg = mg_find(sref, PERL_MAGIC_isa))) {
3776 if (SvTYPE(mg->mg_obj) != SVt_PVAV) {
3777 AV * const ary = newAV_alloc_x(2);
3778 av_push_simple(ary, mg->mg_obj); /* takes the refcount */
3779 av_push_simple(ary, SvREFCNT_inc_simple_NN(dsv));
3780 mg->mg_obj = (SV *)ary;
3782 av_push((AV *)mg->mg_obj, SvREFCNT_inc_simple_NN(dsv));
3785 else sv_magic(sref, dsv, PERL_MAGIC_isa, NULL, 0);
3787 mro_isa_changed_in(GvSTASH(dsv));
3789 else if(mro_changes == 3) {
3790 HV * const stash = GvHV(dsv);
3791 if(old_stash ? (HV *)HvENAME_get(old_stash) : stash)
3797 else if(mro_changes) mro_method_changed_in(GvSTASH(dsv));
3798 if (GvIO(dsv) && dtype == SVt_PVGV) {
3799 DEBUG_o(Perl_deb(aTHX_
3800 "glob_assign_glob clearing PL_stashcache\n"));
3801 /* It's a cache. It will rebuild itself quite happily.
3802 It's a lot of effort to work out exactly which key (or keys)
3803 might be invalidated by the creation of the this file handle.
3805 hv_clear(PL_stashcache);
3811 Perl_gv_setref(pTHX_ SV *const dsv, SV *const ssv)
3813 SV * const sref = SvRV(ssv);
3815 const int intro = GvINTRO(dsv);
3818 const U32 stype = SvTYPE(sref);
3820 PERL_ARGS_ASSERT_GV_SETREF;
3823 GvINTRO_off(dsv); /* one-shot flag */
3824 GvLINE(dsv) = CopLINE(PL_curcop);
3825 GvEGV(dsv) = MUTABLE_GV(dsv);
3830 location = (SV **) &(GvGP(dsv)->gp_cv); /* XXX bypassing GvCV_set */
3831 import_flag = GVf_IMPORTED_CV;
3834 location = (SV **) &GvHV(dsv);
3835 import_flag = GVf_IMPORTED_HV;
3838 location = (SV **) &GvAV(dsv);
3839 import_flag = GVf_IMPORTED_AV;
3842 location = (SV **) &GvIOp(dsv);
3845 location = (SV **) &GvFORM(dsv);
3848 location = &GvSV(dsv);
3849 import_flag = GVf_IMPORTED_SV;
3852 if (stype == SVt_PVCV) {
3853 /*if (GvCVGEN(dsv) && (GvCV(dsv) != (const CV *)sref || GvCVGEN(dsv))) {*/
3855 SvREFCNT_dec(GvCV(dsv));
3856 GvCV_set(dsv, NULL);
3857 GvCVGEN(dsv) = 0; /* Switch off cacheness. */
3860 /* SAVEt_GVSLOT takes more room on the savestack and has more
3861 overhead in leave_scope than SAVEt_GENERIC_SV. But for CVs
3862 leave_scope needs access to the GV so it can reset method
3863 caches. We must use SAVEt_GVSLOT whenever the type is
3864 SVt_PVCV, even if the stash is anonymous, as the stash may
3865 gain a name somehow before leave_scope. */
3866 if (stype == SVt_PVCV) {
3867 /* There is no save_pushptrptrptr. Creating it for this
3868 one call site would be overkill. So inline the ss add
3872 SS_ADD_PTR(location);
3873 SS_ADD_PTR(SvREFCNT_inc(*location));
3874 SS_ADD_UV(SAVEt_GVSLOT);
3877 else SAVEGENERICSV(*location);
3880 if (stype == SVt_PVCV && (*location != sref || GvCVGEN(dsv))) {
3881 CV* const cv = MUTABLE_CV(*location);
3883 if (!GvCVGEN((const GV *)dsv) &&
3884 (CvROOT(cv) || CvXSUB(cv)) &&
3885 /* redundant check that avoids creating the extra SV
3886 most of the time: */
3887 (CvCONST(cv) || (ckWARN(WARN_REDEFINE) && !intro)))
3889 SV * const new_const_sv =
3890 CvCONST((const CV *)sref)
3891 ? cv_const_sv((const CV *)sref)
3893 HV * const stash = GvSTASH((const GV *)dsv);
3894 report_redefined_cv(
3897 ? Perl_newSVpvf(aTHX_
3898 "%" HEKf "::%" HEKf,
3899 HEKfARG(HvNAME_HEK(stash)),
3900 HEKfARG(GvENAME_HEK(MUTABLE_GV(dsv))))
3901 : Perl_newSVpvf(aTHX_
3903 HEKfARG(GvENAME_HEK(MUTABLE_GV(dsv))))
3906 CvCONST((const CV *)sref) ? &new_const_sv : NULL
3910 cv_ckproto_len_flags(cv, (const GV *)dsv,
3911 SvPOK(sref) ? CvPROTO(sref) : NULL,
3912 SvPOK(sref) ? CvPROTOLEN(sref) : 0,
3913 SvPOK(sref) ? SvUTF8(sref) : 0);
3915 GvCVGEN(dsv) = 0; /* Switch off cacheness. */
3917 if(GvSTASH(dsv)) { /* sub foo { 1 } sub bar { 2 } *bar = \&foo */
3918 if (intro && GvREFCNT(dsv) > 1) {
3919 /* temporary remove extra savestack's ref */
3921 gv_method_changed(dsv);
3924 else gv_method_changed(dsv);
3927 *location = SvREFCNT_inc_simple_NN(sref);
3928 if (import_flag && !(GvFLAGS(dsv) & import_flag)
3929 && CopSTASH_ne(PL_curcop, GvSTASH(dsv))) {
3930 GvFLAGS(dsv) |= import_flag;
3933 if (stype == SVt_PVHV) {
3934 const char * const name = GvNAME((GV*)dsv);
3935 const STRLEN len = GvNAMELEN(dsv);
3938 (len > 1 && name[len-2] == ':' && name[len-1] == ':')
3939 || (len == 1 && name[0] == ':')
3941 && (!dref || HvENAME_get(dref))
3944 (HV *)sref, (HV *)dref,
3950 stype == SVt_PVAV && sref != dref
3951 && memEQs(GvNAME((GV*)dsv), GvNAMELEN((GV*)dsv), "ISA")
3952 /* The stash may have been detached from the symbol table, so
3953 check its name before doing anything. */
3954 && GvSTASH(dsv) && HvENAME(GvSTASH(dsv))
3957 MAGIC * const omg = dref && SvSMAGICAL(dref)
3958 ? mg_find(dref, PERL_MAGIC_isa)
3960 if (SvSMAGICAL(sref) && (mg = mg_find(sref, PERL_MAGIC_isa))) {
3961 if (SvTYPE(mg->mg_obj) != SVt_PVAV) {
3962 AV * const ary = newAV_alloc_xz(4);
3963 av_push_simple(ary, mg->mg_obj); /* takes the refcount */
3964 mg->mg_obj = (SV *)ary;
3967 if (SvTYPE(omg->mg_obj) == SVt_PVAV) {
3968 SV **svp = AvARRAY((AV *)omg->mg_obj);
3969 I32 items = AvFILLp((AV *)omg->mg_obj) + 1;
3973 SvREFCNT_inc_simple_NN(*svp++)
3979 SvREFCNT_inc_simple_NN(omg->mg_obj)
3983 av_push((AV *)mg->mg_obj,SvREFCNT_inc_simple_NN(dsv));
3989 sref, omg ? omg->mg_obj : dsv, PERL_MAGIC_isa, NULL, 0
3991 for (i = 0; i <= AvFILL(sref); ++i) {
3992 SV **elem = av_fetch ((AV*)sref, i, 0);
3995 *elem, sref, PERL_MAGIC_isaelem, NULL, i
3999 mg = mg_find(sref, PERL_MAGIC_isa);
4001 /* Since the *ISA assignment could have affected more than
4002 one stash, don't call mro_isa_changed_in directly, but let
4003 magic_clearisa do it for us, as it already has the logic for
4004 dealing with globs vs arrays of globs. */
4006 Perl_magic_clearisa(aTHX_ NULL, mg);
4008 else if (stype == SVt_PVIO) {
4009 DEBUG_o(Perl_deb(aTHX_ "gv_setref clearing PL_stashcache\n"));
4010 /* It's a cache. It will rebuild itself quite happily.
4011 It's a lot of effort to work out exactly which key (or keys)
4012 might be invalidated by the creation of the this file handle.
4014 hv_clear(PL_stashcache);
4018 if (!intro) SvREFCNT_dec(dref);
4027 #ifdef PERL_DEBUG_READONLY_COW
4028 # include <sys/mman.h>
4030 # ifndef PERL_MEMORY_DEBUG_HEADER_SIZE
4031 # define PERL_MEMORY_DEBUG_HEADER_SIZE 0
4035 Perl_sv_buf_to_ro(pTHX_ SV *sv)
4037 struct perl_memory_debug_header * const header =
4038 (struct perl_memory_debug_header *)(SvPVX(sv)-PERL_MEMORY_DEBUG_HEADER_SIZE);
4039 const MEM_SIZE len = header->size;
4040 PERL_ARGS_ASSERT_SV_BUF_TO_RO;
4041 # ifdef PERL_TRACK_MEMPOOL
4042 if (!header->readonly) header->readonly = 1;
4044 if (mprotect(header, len, PROT_READ))
4045 Perl_warn(aTHX_ "mprotect RW for COW string %p %lu failed with %d",
4046 header, len, errno);
4050 S_sv_buf_to_rw(pTHX_ SV *sv)
4052 struct perl_memory_debug_header * const header =
4053 (struct perl_memory_debug_header *)(SvPVX(sv)-PERL_MEMORY_DEBUG_HEADER_SIZE);
4054 const MEM_SIZE len = header->size;
4055 PERL_ARGS_ASSERT_SV_BUF_TO_RW;
4056 if (mprotect(header, len, PROT_READ|PROT_WRITE))
4057 Perl_warn(aTHX_ "mprotect for COW string %p %lu failed with %d",
4058 header, len, errno);
4059 # ifdef PERL_TRACK_MEMPOOL
4060 header->readonly = 0;
4065 # define sv_buf_to_ro(sv) NOOP
4066 # define sv_buf_to_rw(sv) NOOP
4070 Perl_sv_setsv_flags(pTHX_ SV *dsv, SV* ssv, const I32 flags)
4075 unsigned int both_type;
4077 PERL_ARGS_ASSERT_SV_SETSV_FLAGS;
4079 if (UNLIKELY( ssv == dsv ))
4082 if (UNLIKELY( !ssv ))
4085 stype = SvTYPE(ssv);
4086 dtype = SvTYPE(dsv);
4087 both_type = (stype | dtype);
4089 /* with these values, we can check that both SVs are NULL/IV (and not
4090 * freed) just by testing the or'ed types */
4091 STATIC_ASSERT_STMT(SVt_NULL == 0);
4092 STATIC_ASSERT_STMT(SVt_IV == 1);
4093 if (both_type <= 1) {
4094 /* both src and dst are UNDEF/IV/RV, so we can do a lot of
4100 /* minimal subset of SV_CHECK_THINKFIRST_COW_DROP(dsv) */
4101 if (SvREADONLY(dsv))
4102 Perl_croak_no_modify();
4105 sv_unref_flags(dsv, 0);
4110 assert(!SvGMAGICAL(ssv));
4111 assert(!SvGMAGICAL(dsv));
4113 sflags = SvFLAGS(ssv);
4114 if (sflags & (SVf_IOK|SVf_ROK)) {
4115 SET_SVANY_FOR_BODYLESS_IV(dsv);
4116 new_dflags = SVt_IV;
4118 if (sflags & SVf_ROK) {
4119 dsv->sv_u.svu_rv = SvREFCNT_inc(SvRV(ssv));
4120 new_dflags |= SVf_ROK;
4123 /* both src and dst are <= SVt_IV, so sv_any points to the
4124 * head; so access the head directly
4126 assert( &(ssv->sv_u.svu_iv)
4127 == &(((XPVIV*) SvANY(ssv))->xiv_iv));
4128 assert( &(dsv->sv_u.svu_iv)
4129 == &(((XPVIV*) SvANY(dsv))->xiv_iv));
4130 dsv->sv_u.svu_iv = ssv->sv_u.svu_iv;
4131 new_dflags |= (SVf_IOK|SVp_IOK|(sflags & SVf_IVisUV));
4135 new_dflags = dtype; /* turn off everything except the type */
4137 SvFLAGS(dsv) = new_dflags;
4138 SvREFCNT_dec(old_rv);
4143 if (UNLIKELY(both_type == SVTYPEMASK)) {
4144 if (SvIS_FREED(dsv)) {
4145 Perl_croak(aTHX_ "panic: attempt to copy value %" SVf
4146 " to a freed scalar %p", SVfARG(ssv), (void *)dsv);
4148 if (SvIS_FREED(ssv)) {
4149 Perl_croak(aTHX_ "panic: attempt to copy freed scalar %p to %p",
4150 (void*)ssv, (void*)dsv);
4156 SV_CHECK_THINKFIRST_COW_DROP(dsv);
4157 dtype = SvTYPE(dsv); /* THINKFIRST may have changed type */
4159 /* There's a lot of redundancy below but we're going for speed here
4160 * Note: some of the cases below do return; rather than break; so the
4161 * if-elseif-else logic below this switch does not see all cases. */
4166 if (LIKELY( dtype != SVt_PVGV && dtype != SVt_PVLV )) {
4167 (void)SvOK_off(dsv);
4175 /* For performance, we inline promoting to type SVt_IV. */
4176 /* We're starting from SVt_NULL, so provided that define is
4177 * actual 0, we don't have to unset any SV type flags
4178 * to promote to SVt_IV. */
4179 STATIC_ASSERT_STMT(SVt_NULL == 0);
4180 SET_SVANY_FOR_BODYLESS_IV(dsv);
4181 SvFLAGS(dsv) |= SVt_IV;
4185 sv_upgrade(dsv, SVt_PVIV);
4189 goto end_of_first_switch;
4191 (void)SvIOK_only(dsv);
4192 SvIV_set(dsv, SvIVX(ssv));
4195 /* SvTAINTED can only be true if the SV has taint magic, which in
4196 turn means that the SV type is PVMG (or greater). This is the
4197 case statement for SVt_IV, so this cannot be true (whatever gcov
4199 assert(!SvTAINTED(ssv));
4204 if (dtype < SVt_PV && dtype != SVt_IV)
4205 sv_upgrade(dsv, SVt_IV);
4209 if (LIKELY( SvNOK(ssv) )) {
4213 sv_upgrade(dsv, SVt_NV);
4217 sv_upgrade(dsv, SVt_PVNV);
4221 goto end_of_first_switch;
4223 SvNV_set(dsv, SvNVX(ssv));
4224 (void)SvNOK_only(dsv);
4225 /* SvTAINTED can only be true if the SV has taint magic, which in
4226 turn means that the SV type is PVMG (or greater). This is the
4227 case statement for SVt_NV, so this cannot be true (whatever gcov
4229 assert(!SvTAINTED(ssv));
4236 sv_upgrade(dsv, SVt_PV);
4239 if (dtype < SVt_PVIV)
4240 sv_upgrade(dsv, SVt_PVIV);
4243 if (dtype < SVt_PVNV)
4244 sv_upgrade(dsv, SVt_PVNV);
4248 invlist_clone(ssv, dsv);
4252 const char * const type = sv_reftype(ssv,0);
4254 /* diag_listed_as: Bizarre copy of %s */
4255 Perl_croak(aTHX_ "Bizarre copy of %s in %s", type, OP_DESC(PL_op));
4257 Perl_croak(aTHX_ "Bizarre copy of %s", type);
4259 NOT_REACHED; /* NOTREACHED */
4263 if (dtype < SVt_REGEXP)
4264 sv_upgrade(dsv, SVt_REGEXP);
4270 if (SvGMAGICAL(ssv) && (flags & SV_GMAGIC)) {
4272 if (SvTYPE(ssv) != stype)
4273 stype = SvTYPE(ssv);
4275 if (isGV_with_GP(ssv) && dtype <= SVt_PVLV) {
4276 glob_assign_glob(dsv, ssv, dtype);
4279 if (stype == SVt_PVLV)
4281 if (isREGEXP(ssv)) goto upgregexp;
4282 SvUPGRADE(dsv, SVt_PVNV);
4285 SvUPGRADE(dsv, (svtype)stype);
4287 end_of_first_switch:
4289 /* dsv may have been upgraded. */
4290 dtype = SvTYPE(dsv);
4291 sflags = SvFLAGS(ssv);
4293 if (UNLIKELY( dtype == SVt_PVCV )) {
4294 /* Assigning to a subroutine sets the prototype. */
4297 const char *const ptr = SvPV_const(ssv, len);
4299 SvGROW(dsv, len + 1);
4300 Copy(ptr, SvPVX(dsv), len + 1, char);
4301 SvCUR_set(dsv, len);
4303 SvFLAGS(dsv) |= sflags & SVf_UTF8;
4304 CvAUTOLOAD_off(dsv);
4309 else if (UNLIKELY(dtype == SVt_PVAV || dtype == SVt_PVHV
4310 || dtype == SVt_PVFM))
4312 const char * const type = sv_reftype(dsv,0);
4314 /* diag_listed_as: Cannot copy to %s */
4315 Perl_croak(aTHX_ "Cannot copy to %s in %s", type, OP_DESC(PL_op));
4317 Perl_croak(aTHX_ "Cannot copy to %s", type);
4318 } else if (sflags & SVf_ROK) {
4319 if (isGV_with_GP(dsv)
4320 && SvTYPE(SvRV(ssv)) == SVt_PVGV && isGV_with_GP(SvRV(ssv))) {
4323 if (GvIMPORTED(dsv) != GVf_IMPORTED
4324 && CopSTASH_ne(PL_curcop, GvSTASH(dsv)))
4331 glob_assign_glob(dsv, ssv, dtype);
4335 if (dtype >= SVt_PV) {
4336 if (isGV_with_GP(dsv)) {
4337 gv_setref(dsv, ssv);
4340 if (SvPVX_const(dsv)) {
4346 (void)SvOK_off(dsv);
4347 SvRV_set(dsv, SvREFCNT_inc(SvRV(ssv)));
4348 SvFLAGS(dsv) |= sflags & SVf_ROK;
4349 assert(!(sflags & SVp_NOK));
4350 assert(!(sflags & SVp_IOK));
4351 assert(!(sflags & SVf_NOK));
4352 assert(!(sflags & SVf_IOK));
4354 else if (isGV_with_GP(dsv)) {
4355 if (!(sflags & SVf_OK)) {
4356 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
4357 "Undefined value assigned to typeglob");
4360 GV *gv = gv_fetchsv_nomg(ssv, GV_ADD, SVt_PVGV);
4361 if (dsv != (const SV *)gv) {
4362 const char * const name = GvNAME((const GV *)dsv);
4363 const STRLEN len = GvNAMELEN(dsv);
4364 HV *old_stash = NULL;
4365 bool reset_isa = FALSE;
4366 if ((len > 1 && name[len-2] == ':' && name[len-1] == ':')
4367 || (len == 1 && name[0] == ':')) {
4368 /* Set aside the old stash, so we can reset isa caches
4369 on its subclasses. */
4370 if((old_stash = GvHV(dsv))) {
4371 /* Make sure we do not lose it early. */
4372 SvREFCNT_inc_simple_void_NN(
4373 sv_2mortal((SV *)old_stash)
4380 SvREFCNT_inc_simple_void_NN(sv_2mortal(dsv));
4381 gp_free(MUTABLE_GV(dsv));
4383 GvGP_set(dsv, gp_ref(GvGP(gv)));
4386 HV * const stash = GvHV(dsv);
4388 old_stash ? (HV *)HvENAME_get(old_stash) : stash
4398 else if ((dtype == SVt_REGEXP || dtype == SVt_PVLV)
4399 && (stype == SVt_REGEXP || isREGEXP(ssv))) {
4400 reg_temp_copy((REGEXP*)dsv, (REGEXP*)ssv);
4402 else if (sflags & SVp_POK) {
4403 const STRLEN cur = SvCUR(ssv);
4404 const STRLEN len = SvLEN(ssv);
4407 * We have three basic ways to copy the string:
4413 * Which we choose is based on various factors. The following
4414 * things are listed in order of speed, fastest to slowest:
4416 * - Copying a short string
4417 * - Copy-on-write bookkeeping
4419 * - Copying a long string
4421 * We swipe the string (steal the string buffer) if the SV on the
4422 * rhs is about to be freed anyway (TEMP and refcnt==1). This is a
4423 * big win on long strings. It should be a win on short strings if
4424 * SvPVX_const(dsv) has to be allocated. If not, it should not
4425 * slow things down, as SvPVX_const(ssv) would have been freed
4428 * We also steal the buffer from a PADTMP (operator target) if it
4429 * is ‘long enough’. For short strings, a swipe does not help
4430 * here, as it causes more malloc calls the next time the target
4431 * is used. Benchmarks show that even if SvPVX_const(dsv) has to
4432 * be allocated it is still not worth swiping PADTMPs for short
4433 * strings, as the savings here are small.
4435 * If swiping is not an option, then we see whether it is worth using
4436 * copy-on-write. If the lhs already has a buffer big enough and the
4437 * string is short, we skip it and fall back to method 3, since memcpy
4438 * is faster for short strings than the later bookkeeping overhead that
4439 * copy-on-write entails.
4441 * If the rhs is not a copy-on-write string yet, then we also
4442 * consider whether the buffer is too large relative to the string
4443 * it holds. Some operations such as readline allocate a large
4444 * buffer in the expectation of reusing it. But turning such into
4445 * a COW buffer is counter-productive because it increases memory
4446 * usage by making readline allocate a new large buffer the sec-
4447 * ond time round. So, if the buffer is too large, again, we use
4450 * Finally, if there is no buffer on the left, or the buffer is too
4451 * small, then we use copy-on-write and make both SVs share the
4456 /* Whichever path we take through the next code, we want this true,
4457 and doing it now facilitates the COW check. */
4458 (void)SvPOK_only(dsv);
4462 /* slated for free anyway (and not COW)? */
4463 (sflags & (SVs_TEMP|SVf_IsCOW)) == SVs_TEMP
4464 /* or a swipable TARG */
4466 (SVs_PADTMP|SVf_READONLY|SVf_PROTECT|SVf_IsCOW))
4468 /* whose buffer is worth stealing */
4469 && CHECK_COWBUF_THRESHOLD(cur,len)
4472 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
4473 (!(flags & SV_NOSTEAL)) &&
4474 /* and we're allowed to steal temps */
4475 SvREFCNT(ssv) == 1 && /* and no other references to it? */
4476 len) /* and really is a string */
4477 { /* Passes the swipe test. */
4478 if (SvPVX_const(dsv)) /* we know that dtype >= SVt_PV */
4480 SvPV_set(dsv, SvPVX_mutable(ssv));
4481 SvLEN_set(dsv, SvLEN(ssv));
4482 SvCUR_set(dsv, SvCUR(ssv));
4485 (void)SvOK_off(ssv); /* NOTE: nukes most SvFLAGS on ssv */
4486 SvPV_set(ssv, NULL);
4491 /* We must check for SvIsCOW_static() even without
4492 * SV_COW_SHARED_HASH_KEYS being set or else we'll break SvIsBOOL()
4494 else if (SvIsCOW_static(ssv)) {
4495 if (SvPVX_const(dsv)) { /* we know that dtype >= SVt_PV */
4498 SvPV_set(dsv, SvPVX(ssv));
4500 SvCUR_set(dsv, cur);
4501 SvFLAGS(dsv) |= (SVf_IsCOW|SVppv_STATIC);
4503 else if (flags & SV_COW_SHARED_HASH_KEYS
4505 #ifdef PERL_COPY_ON_WRITE
4508 ( (CHECK_COWBUF_THRESHOLD(cur,len) || SvLEN(dsv) < cur+1)
4509 /* If this is a regular (non-hek) COW, only so
4510 many COW "copies" are possible. */
4511 && CowREFCNT(ssv) != SV_COW_REFCNT_MAX ))
4512 : ( (sflags & CAN_COW_MASK) == CAN_COW_FLAGS
4513 && !(SvFLAGS(dsv) & SVf_BREAK)
4514 && CHECK_COW_THRESHOLD(cur,len) && cur+1 < len
4515 && (CHECK_COWBUF_THRESHOLD(cur,len) || SvLEN(dsv) < cur+1)
4519 && !(SvFLAGS(dsv) & SVf_BREAK)
4522 /* Either it's a shared hash key, or it's suitable for
4526 PerlIO_printf(Perl_debug_log, "Copy on write: ssv --> dsv\n");
4532 if (!(sflags & SVf_IsCOW)) {
4537 if (SvPVX_const(dsv)) { /* we know that dtype >= SVt_PV */
4543 if (sflags & SVf_IsCOW) {
4547 SvPV_set(dsv, SvPVX_mutable(ssv));
4552 /* SvIsCOW_shared_hash */
4553 DEBUG_C(PerlIO_printf(Perl_debug_log,
4554 "Copy on write: Sharing hash\n"));
4556 assert (SvTYPE(dsv) >= SVt_PV);
4558 HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(ssv)))));
4560 SvLEN_set(dsv, len);
4561 SvCUR_set(dsv, cur);
4564 /* Failed the swipe test, and we cannot do copy-on-write either.
4565 Have to copy the string. */
4566 SvGROW(dsv, cur + 1); /* inlined from sv_setpvn */
4567 Move(SvPVX_const(ssv),SvPVX(dsv),cur,char);
4568 SvCUR_set(dsv, cur);
4571 if (sflags & SVp_NOK) {
4572 SvNV_set(dsv, SvNVX(ssv));
4573 if ((sflags & SVf_NOK) && !(sflags & SVf_POK)) {
4574 /* Source was SVf_NOK|SVp_NOK|SVp_POK but not SVf_POK, meaning
4575 a value set as floating point and later stringified, where
4576 the value happens to be one of the few that we know aren't
4577 affected by the numeric locale, hence we can cache the
4578 stringification. Currently that's +Inf, -Inf and NaN, but
4579 conceivably we might extend this to -9 .. +9 (excluding -0).
4580 So mark destination the same: */
4581 SvFLAGS(dsv) &= ~SVf_POK;
4584 if (sflags & SVp_IOK) {
4585 SvIV_set(dsv, SvIVX(ssv));
4586 if (sflags & SVf_IVisUV)
4588 if ((sflags & SVf_IOK) && !(sflags & SVf_POK)) {
4589 /* Source was SVf_IOK|SVp_IOK|SVp_POK but not SVf_POK, meaning
4590 a value set as an integer and later stringified. So mark
4591 destination the same: */
4592 SvFLAGS(dsv) &= ~SVf_POK;
4595 SvFLAGS(dsv) |= sflags & (SVf_IOK|SVp_IOK|SVf_NOK|SVp_NOK|SVf_UTF8);
4597 const MAGIC * const smg = SvVSTRING_mg(ssv);
4599 sv_magic(dsv, NULL, PERL_MAGIC_vstring,
4600 smg->mg_ptr, smg->mg_len);
4605 else if (sflags & (SVp_IOK|SVp_NOK)) {
4606 (void)SvOK_off(dsv);
4607 SvFLAGS(dsv) |= sflags & (SVf_IOK|SVp_IOK|SVf_IVisUV|SVf_NOK|SVp_NOK);
4608 if (sflags & SVp_IOK) {
4609 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
4610 SvIV_set(dsv, SvIVX(ssv));
4612 if (sflags & SVp_NOK) {
4613 SvNV_set(dsv, SvNVX(ssv));
4617 if (isGV_with_GP(ssv)) {
4618 gv_efullname3(dsv, MUTABLE_GV(ssv), "*");
4621 (void)SvOK_off(dsv);
4629 =for apidoc sv_set_undef
4631 Equivalent to C<sv_setsv(sv, &PL_sv_undef)>, but more efficient.
4632 Doesn't handle set magic.
4634 The perl equivalent is C<$sv = undef;>. Note that it doesn't free any string
4635 buffer, unlike C<undef $sv>.
4637 Introduced in perl 5.25.12.
4643 Perl_sv_set_undef(pTHX_ SV *sv)
4645 U32 type = SvTYPE(sv);
4647 PERL_ARGS_ASSERT_SV_SET_UNDEF;
4649 /* shortcut, NULL, IV, RV */
4651 if (type <= SVt_IV) {
4652 assert(!SvGMAGICAL(sv));
4653 if (SvREADONLY(sv)) {
4654 /* does undeffing PL_sv_undef count as modifying a read-only
4655 * variable? Some XS code does this */
4656 if (sv == &PL_sv_undef)
4658 Perl_croak_no_modify();
4663 sv_unref_flags(sv, 0);
4666 SvFLAGS(sv) = type; /* quickly turn off all flags */
4667 SvREFCNT_dec_NN(rv);
4671 SvFLAGS(sv) = type; /* quickly turn off all flags */
4676 Perl_croak(aTHX_ "panic: attempt to undefine a freed scalar %p",
4679 SV_CHECK_THINKFIRST_COW_DROP(sv);
4681 if (isGV_with_GP(sv))
4682 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
4683 "Undefined value assigned to typeglob");
4689 =for apidoc sv_set_true
4691 Equivalent to C<sv_setsv(sv, &PL_sv_yes)>, but may be made more
4692 efficient in the future. Doesn't handle set magic.
4694 The perl equivalent is C<$sv = !0;>.
4696 Introduced in perl 5.35.11.
4702 Perl_sv_set_true(pTHX_ SV *sv)
4704 PERL_ARGS_ASSERT_SV_SET_TRUE;
4705 sv_setsv(sv, &PL_sv_yes);
4709 =for apidoc sv_set_false
4711 Equivalent to C<sv_setsv(sv, &PL_sv_no)>, but may be made more
4712 efficient in the future. Doesn't handle set magic.
4714 The perl equivalent is C<$sv = !1;>.
4716 Introduced in perl 5.35.11.
4722 Perl_sv_set_false(pTHX_ SV *sv)
4724 PERL_ARGS_ASSERT_SV_SET_FALSE;
4725 sv_setsv(sv, &PL_sv_no);
4729 =for apidoc sv_set_bool
4731 Equivalent to C<sv_setsv(sv, bool_val ? &Pl_sv_yes : &PL_sv_no)>, but
4732 may be made more efficient in the future. Doesn't handle set magic.
4734 The perl equivalent is C<$sv = !!$expr;>.
4736 Introduced in perl 5.35.11.
4742 Perl_sv_set_bool(pTHX_ SV *sv, const bool bool_val)
4744 PERL_ARGS_ASSERT_SV_SET_BOOL;
4745 sv_setsv(sv, bool_val ? &PL_sv_yes : &PL_sv_no);
4750 Perl_sv_setsv_mg(pTHX_ SV *const dsv, SV *const ssv)
4752 PERL_ARGS_ASSERT_SV_SETSV_MG;
4759 # define SVt_COW SVt_PV
4761 Perl_sv_setsv_cow(pTHX_ SV *dsv, SV *ssv)
4763 STRLEN cur = SvCUR(ssv);
4764 STRLEN len = SvLEN(ssv);
4766 U32 new_flags = (SVt_COW|SVf_POK|SVp_POK|SVf_IsCOW);
4767 #if defined(PERL_DEBUG_READONLY_COW) && defined(PERL_COPY_ON_WRITE)
4768 const bool already = cBOOL(SvIsCOW(ssv));
4771 PERL_ARGS_ASSERT_SV_SETSV_COW;
4774 PerlIO_printf(Perl_debug_log, "Fast copy on write: %p -> %p\n",
4775 (void*)ssv, (void*)dsv);
4782 if (SvTHINKFIRST(dsv))
4783 sv_force_normal_flags(dsv, SV_COW_DROP_PV);
4784 else if (SvPVX_const(dsv))
4785 Safefree(SvPVX_mutable(dsv));
4786 SvUPGRADE(dsv, SVt_COW);
4789 dsv = newSV_type(SVt_COW);
4791 assert (SvPOK(ssv));
4792 assert (SvPOKp(ssv));
4795 if (SvIsCOW_shared_hash(ssv)) {
4796 /* source is a COW shared hash key. */
4797 DEBUG_C(PerlIO_printf(Perl_debug_log,
4798 "Fast copy on write: Sharing hash\n"));
4799 new_pv = HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(ssv))));
4802 else if (SvIsCOW_static(ssv)) {
4803 /* source is static constant; preserve this */
4804 new_pv = SvPVX(ssv);
4805 new_flags |= SVppv_STATIC;
4808 assert(SvCUR(ssv)+1 < SvLEN(ssv));
4809 assert(CowREFCNT(ssv) < SV_COW_REFCNT_MAX);
4811 assert ((SvFLAGS(ssv) & CAN_COW_MASK) == CAN_COW_FLAGS);
4812 SvUPGRADE(ssv, SVt_COW);
4814 DEBUG_C(PerlIO_printf(Perl_debug_log,
4815 "Fast copy on write: Converting ssv to COW\n"));
4818 # ifdef PERL_DEBUG_READONLY_COW
4819 if (already) sv_buf_to_rw(ssv);
4822 new_pv = SvPVX_mutable(ssv);
4826 SvPV_set(dsv, new_pv);
4827 SvFLAGS(dsv) = new_flags;
4830 SvLEN_set(dsv, len);
4831 SvCUR_set(dsv, cur);
4841 =for apidoc sv_setpv_bufsize
4843 Sets the SV to be a string of cur bytes length, with at least
4844 len bytes available. Ensures that there is a null byte at SvEND.
4845 Returns a char * pointer to the SvPV buffer.
4851 Perl_sv_setpv_bufsize(pTHX_ SV *const sv, const STRLEN cur, const STRLEN len)
4855 PERL_ARGS_ASSERT_SV_SETPV_BUFSIZE;
4857 SV_CHECK_THINKFIRST_COW_DROP(sv);
4858 SvUPGRADE(sv, SVt_PV);
4859 pv = SvGROW(sv, len + 1);
4862 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4865 if (SvTYPE(sv) == SVt_PVCV) CvAUTOLOAD_off(sv);
4870 =for apidoc sv_setpv
4871 =for apidoc_item sv_setpv_mg
4872 =for apidoc_item sv_setpvn
4873 =for apidoc_item sv_setpvn_fresh
4874 =for apidoc_item sv_setpvn_mg
4875 =for apidoc_item |void|sv_setpvs|SV* sv|"literal string"
4876 =for apidoc_item |void|sv_setpvs_mg|SV* sv|"literal string"
4878 These copy a string into the SV C<sv>, making sure it is C<L</SvPOK_only>>.
4880 In the C<pvs> forms, the string must be a C literal string, enclosed in double
4883 In the C<pvn> forms, the first byte of the string is pointed to by C<ptr>, and
4884 C<len> indicates the number of bytes to be copied, potentially including
4885 embedded C<NUL> characters.
4887 In the plain C<pv> forms, C<ptr> points to a NUL-terminated C string. That is,
4888 it points to the first byte of the string, and the copy proceeds up through the
4889 first enountered C<NUL> byte.
4891 In the forms that take a C<ptr> argument, if it is NULL, the SV will become
4894 The UTF-8 flag is not changed by these functions. A terminating NUL byte is
4895 guaranteed in the result.
4897 The C<_mg> forms handle 'set' magic; the other forms skip all magic.
4899 C<sv_setpvn_fresh> is a cut-down alternative to C<sv_setpvn>, intended ONLY
4900 to be used with a fresh sv that has been upgraded to a SVt_PV, SVt_PVIV,
4901 SVt_PVNV, or SVt_PVMG.
4907 Perl_sv_setpvn(pTHX_ SV *const sv, const char *const ptr, const STRLEN len)
4911 PERL_ARGS_ASSERT_SV_SETPVN;
4913 SV_CHECK_THINKFIRST_COW_DROP(sv);
4914 if (isGV_with_GP(sv))
4915 Perl_croak_no_modify();
4921 /* len is STRLEN which is unsigned, need to copy to signed */
4924 Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen %"
4927 SvUPGRADE(sv, SVt_PV);
4929 dptr = SvGROW(sv, len + 1);
4930 Move(ptr,dptr,len,char);
4933 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4935 if (SvTYPE(sv) == SVt_PVCV) CvAUTOLOAD_off(sv);
4939 Perl_sv_setpvn_mg(pTHX_ SV *const sv, const char *const ptr, const STRLEN len)
4941 PERL_ARGS_ASSERT_SV_SETPVN_MG;
4943 sv_setpvn(sv,ptr,len);
4948 Perl_sv_setpvn_fresh(pTHX_ SV *const sv, const char *const ptr, const STRLEN len)
4952 PERL_ARGS_ASSERT_SV_SETPVN_FRESH;
4953 assert(SvTYPE(sv) >= SVt_PV && SvTYPE(sv) <= SVt_PVMG);
4954 assert(!SvTHINKFIRST(sv));
4955 assert(!isGV_with_GP(sv));
4959 /* len is STRLEN which is unsigned, need to copy to signed */
4961 Perl_croak(aTHX_ "panic: sv_setpvn_fresh called with negative strlen %"
4964 dptr = sv_grow_fresh(sv, len + 1);
4965 Move(ptr,dptr,len,char);
4974 Perl_sv_setpv(pTHX_ SV *const sv, const char *const ptr)
4978 PERL_ARGS_ASSERT_SV_SETPV;
4980 SV_CHECK_THINKFIRST_COW_DROP(sv);
4986 SvUPGRADE(sv, SVt_PV);
4988 SvGROW(sv, len + 1);
4989 Move(ptr,SvPVX(sv),len+1,char);
4991 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4993 if (SvTYPE(sv) == SVt_PVCV) CvAUTOLOAD_off(sv);
4997 Perl_sv_setpv_mg(pTHX_ SV *const sv, const char *const ptr)
4999 PERL_ARGS_ASSERT_SV_SETPV_MG;
5006 Perl_sv_sethek(pTHX_ SV *const sv, const HEK *const hek)
5008 PERL_ARGS_ASSERT_SV_SETHEK;
5014 if (HEK_LEN(hek) == HEf_SVKEY) {
5015 sv_setsv(sv, *(SV**)HEK_KEY(hek));
5018 const int flags = HEK_FLAGS(hek);
5019 if (flags & HVhek_WASUTF8) {
5020 STRLEN utf8_len = HEK_LEN(hek);
5021 char *as_utf8 = (char *)bytes_to_utf8((U8*)HEK_KEY(hek), &utf8_len);
5022 sv_usepvn_flags(sv, as_utf8, utf8_len, SV_HAS_TRAILING_NUL);
5025 } else if (flags & HVhek_NOTSHARED) {
5026 sv_setpvn(sv, HEK_KEY(hek), HEK_LEN(hek));
5029 else SvUTF8_off(sv);
5033 SV_CHECK_THINKFIRST_COW_DROP(sv);
5034 SvUPGRADE(sv, SVt_PV);
5036 SvPV_set(sv,(char *)HEK_KEY(share_hek_hek(hek)));
5037 SvCUR_set(sv, HEK_LEN(hek));
5043 else SvUTF8_off(sv);
5051 =for apidoc sv_usepvn
5052 =for apidoc_item sv_usepvn_flags
5053 =for apidoc_item sv_usepvn_mg
5055 These tell an SV to use C<ptr> for its string value. Normally SVs have
5056 their string stored inside the SV, but these tell the SV to use an
5057 external string instead.
5059 C<ptr> should point to memory that was allocated
5060 by L</C<Newx>>. It must be
5061 the start of a C<Newx>-ed block of memory, and not a pointer to the
5062 middle of it (beware of L<C<OOK>|perlguts/Offsets> and copy-on-write),
5063 and not be from a non-C<Newx> memory allocator like C<malloc>. The
5064 string length, C<len>, must be supplied. By default this function
5065 will L</C<Renew>> (i.e. realloc, move) the memory pointed to by C<ptr>,
5066 so that the pointer should not be freed or used by the programmer after giving
5067 it to C<sv_usepvn>, and neither should any pointers from "behind" that pointer
5068 (I<e.g.>, S<C<ptr> + 1>) be used.
5070 In the C<sv_usepvn_flags> form, if S<C<flags & SV_SMAGIC>> is true,
5071 C<SvSETMAGIC> is called before returning.
5072 And if S<C<flags & SV_HAS_TRAILING_NUL>> is true, then C<ptr[len]> must be
5073 C<NUL>, and the realloc will be skipped (I<i.e.>, the buffer is actually at
5074 least 1 byte longer than C<len>, and already meets the requirements for storing
5077 C<sv_usepvn> is merely C<sv_usepvn_flags> with C<flags> set to 0, so 'set'
5080 C<sv_usepvn_mg> is merely C<sv_usepvn_flags> with C<flags> set to C<SV_SMAGIC>,
5081 so 'set' magic is performed.
5083 =for apidoc Amnh||SV_SMAGIC
5084 =for apidoc Amnh||SV_HAS_TRAILING_NUL
5090 Perl_sv_usepvn_flags(pTHX_ SV *const sv, char *ptr, const STRLEN len, const U32 flags)
5094 PERL_ARGS_ASSERT_SV_USEPVN_FLAGS;
5096 SV_CHECK_THINKFIRST_COW_DROP(sv);
5097 SvUPGRADE(sv, SVt_PV);
5100 if (flags & SV_SMAGIC)
5104 if (SvPVX_const(sv))
5108 if (flags & SV_HAS_TRAILING_NUL)
5109 assert(ptr[len] == '\0');
5112 allocate = (flags & SV_HAS_TRAILING_NUL)
5114 #ifdef Perl_safesysmalloc_size
5117 PERL_STRLEN_ROUNDUP(len + 1);
5119 if (flags & SV_HAS_TRAILING_NUL) {
5120 /* It's long enough - do nothing.
5121 Specifically Perl_newCONSTSUB is relying on this. */
5124 /* Force a move to shake out bugs in callers. */
5125 char *new_ptr = (char*)safemalloc(allocate);
5126 Copy(ptr, new_ptr, len, char);
5127 PoisonFree(ptr,len,char);
5131 ptr = (char*) saferealloc (ptr, allocate);
5134 #ifdef Perl_safesysmalloc_size
5135 SvLEN_set(sv, Perl_safesysmalloc_size(ptr));
5137 SvLEN_set(sv, allocate);
5141 if (!(flags & SV_HAS_TRAILING_NUL)) {
5144 (void)SvPOK_only_UTF8(sv); /* validate pointer */
5146 if (flags & SV_SMAGIC)
5152 S_sv_uncow(pTHX_ SV * const sv, const U32 flags)
5154 assert(SvIsCOW(sv));
5157 const char * const pvx = SvPVX_const(sv);
5158 const STRLEN len = SvLEN(sv);
5159 const STRLEN cur = SvCUR(sv);
5160 const bool was_shared_hek = SvIsCOW_shared_hash(sv);
5164 PerlIO_printf(Perl_debug_log,
5165 "Copy on write: Force normal %ld\n",
5171 # ifdef PERL_COPY_ON_WRITE
5173 /* Must do this first, since the CowREFCNT uses SvPVX and
5174 we need to write to CowREFCNT, or de-RO the whole buffer if we are
5175 the only owner left of the buffer. */
5176 sv_buf_to_rw(sv); /* NOOP if RO-ing not supported */
5178 U8 cowrefcnt = CowREFCNT(sv);
5179 if(cowrefcnt != 0) {
5181 CowREFCNT(sv) = cowrefcnt;
5186 /* Else we are the only owner of the buffer. */
5191 /* This SV doesn't own the buffer, so need to Newx() a new one: */
5196 if (flags & SV_COW_DROP_PV) {
5197 /* OK, so we don't need to copy our buffer. */
5200 SvGROW(sv, cur + 1);
5201 Move(pvx,SvPVX(sv),cur,char);
5205 if (was_shared_hek) {
5206 unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
5214 const char * const pvx = SvPVX_const(sv);
5215 const STRLEN len = SvCUR(sv);
5219 if (flags & SV_COW_DROP_PV) {
5220 /* OK, so we don't need to copy our buffer. */
5223 SvGROW(sv, len + 1);
5224 Move(pvx,SvPVX(sv),len,char);
5227 unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
5234 =for apidoc sv_force_normal_flags
5236 Undo various types of fakery on an SV, where fakery means
5237 "more than" a string: if the PV is a shared string, make
5238 a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
5239 an C<xpvmg>; if we're a copy-on-write scalar, this is the on-write time when
5240 we do the copy, and is also used locally; if this is a
5241 vstring, drop the vstring magic. If C<SV_COW_DROP_PV> is set
5242 then a copy-on-write scalar drops its PV buffer (if any) and becomes
5243 C<SvPOK_off> rather than making a copy. (Used where this
5244 scalar is about to be set to some other value.) In addition,
5245 the C<flags> parameter gets passed to C<sv_unref_flags()>
5246 when unreffing. C<sv_force_normal> calls this function
5247 with flags set to 0.
5249 This function is expected to be used to signal to perl that this SV is
5250 about to be written to, and any extra book-keeping needs to be taken care
5251 of. Hence, it croaks on read-only values.
5253 =for apidoc Amnh||SV_COW_DROP_PV
5259 Perl_sv_force_normal_flags(pTHX_ SV *const sv, const U32 flags)
5261 PERL_ARGS_ASSERT_SV_FORCE_NORMAL_FLAGS;
5264 Perl_croak_no_modify();
5265 else if (SvIsCOW(sv) && LIKELY(SvTYPE(sv) != SVt_PVHV))
5266 S_sv_uncow(aTHX_ sv, flags);
5268 sv_unref_flags(sv, flags);
5269 else if (SvFAKE(sv) && isGV_with_GP(sv))
5270 sv_unglob(sv, flags);
5271 else if (SvFAKE(sv) && isREGEXP(sv)) {
5272 /* Need to downgrade the REGEXP to a simple(r) scalar. This is analogous
5273 to sv_unglob. We only need it here, so inline it. */
5274 const bool islv = SvTYPE(sv) == SVt_PVLV;
5275 const svtype new_type =
5276 islv ? SVt_NULL : SvMAGIC(sv) || SvSTASH(sv) ? SVt_PVMG : SVt_PV;
5277 SV *const temp = newSV_type(new_type);
5278 regexp *old_rx_body;
5280 if (new_type == SVt_PVMG) {
5281 SvMAGIC_set(temp, SvMAGIC(sv));
5282 SvMAGIC_set(sv, NULL);
5283 SvSTASH_set(temp, SvSTASH(sv));
5284 SvSTASH_set(sv, NULL);
5287 SvCUR_set(temp, SvCUR(sv));
5288 /* Remember that SvPVX is in the head, not the body. */
5289 assert(ReANY((REGEXP *)sv)->mother_re);
5292 /* LV-as-regex has sv->sv_any pointing to an XPVLV body,
5293 * whose xpvlenu_rx field points to the regex body */
5294 XPV *xpv = (XPV*)(SvANY(sv));
5295 old_rx_body = xpv->xpv_len_u.xpvlenu_rx;
5296 xpv->xpv_len_u.xpvlenu_rx = NULL;
5299 old_rx_body = ReANY((REGEXP *)sv);
5301 /* Their buffer is already owned by someone else. */
5302 if (flags & SV_COW_DROP_PV) {
5303 /* SvLEN is already 0. For SVt_REGEXP, we have a brand new
5304 zeroed body. For SVt_PVLV, we zeroed it above (len field
5305 a union with xpvlenu_rx) */
5306 assert(!SvLEN(islv ? sv : temp));
5307 sv->sv_u.svu_pv = 0;
5310 sv->sv_u.svu_pv = savepvn(RX_WRAPPED((REGEXP *)sv), SvCUR(sv));
5311 SvLEN_set(islv ? sv : temp, SvCUR(sv)+1);
5315 /* Now swap the rest of the bodies. */
5319 SvFLAGS(sv) &= ~SVTYPEMASK;
5320 SvFLAGS(sv) |= new_type;
5321 SvANY(sv) = SvANY(temp);
5324 SvFLAGS(temp) &= ~(SVTYPEMASK);
5325 SvFLAGS(temp) |= SVt_REGEXP|SVf_FAKE;
5326 SvANY(temp) = old_rx_body;
5328 /* temp is now rebuilt as a correctly structured SVt_REGEXP, so this
5329 * will trigger a call to sv_clear() which will correctly free the
5331 SvREFCNT_dec_NN(temp);
5333 else if (SvVOK(sv)) sv_unmagic(sv, PERL_MAGIC_vstring);
5339 Efficient removal of characters from the beginning of the string buffer.
5340 C<SvPOK(sv)>, or at least C<SvPOKp(sv)>, must be true and C<ptr> must be a
5341 pointer to somewhere inside the string buffer. C<ptr> becomes the first
5342 character of the adjusted string. Uses the C<OOK> hack. On return, only
5343 C<SvPOK(sv)> and C<SvPOKp(sv)> among the C<OK> flags will be true.
5345 Beware: after this function returns, C<ptr> and SvPVX_const(sv) may no longer
5346 refer to the same chunk of data.
5348 The unfortunate similarity of this function's name to that of Perl's C<chop>
5349 operator is strictly coincidental. This function works from the left;
5350 C<chop> works from the right.
5356 Perl_sv_chop(pTHX_ SV *const sv, const char *const ptr)
5367 PERL_ARGS_ASSERT_SV_CHOP;
5369 if (!ptr || !SvPOKp(sv))
5371 delta = ptr - SvPVX_const(sv);
5373 /* Nothing to do. */
5376 max_delta = SvLEN(sv) ? SvLEN(sv) : SvCUR(sv);
5377 if (delta > max_delta)
5378 Perl_croak(aTHX_ "panic: sv_chop ptr=%p, start=%p, end=%p",
5379 ptr, SvPVX_const(sv), SvPVX_const(sv) + max_delta);
5380 /* SvPVX(sv) may move in SV_CHECK_THINKFIRST(sv), so don't use ptr any more */
5381 SV_CHECK_THINKFIRST(sv);
5382 SvPOK_only_UTF8(sv);
5385 if (!SvLEN(sv)) { /* make copy of shared string */
5386 const char *pvx = SvPVX_const(sv);
5387 const STRLEN len = SvCUR(sv);
5388 SvGROW(sv, len + 1);
5389 Move(pvx,SvPVX(sv),len,char);
5395 SvOOK_offset(sv, old_delta);
5397 SvLEN_set(sv, SvLEN(sv) - delta);
5398 SvCUR_set(sv, SvCUR(sv) - delta);
5399 SvPV_set(sv, SvPVX(sv) + delta);
5401 p = (U8 *)SvPVX_const(sv);
5404 /* how many bytes were evacuated? we will fill them with sentinel
5405 bytes, except for the part holding the new offset of course. */
5408 evacn += (old_delta < 0x100 ? 1 : 1 + sizeof(STRLEN));
5410 assert(evacn <= delta + old_delta);
5414 /* This sets 'delta' to the accumulated value of all deltas so far */
5418 /* If 'delta' fits in a byte, store it just prior to the new beginning of
5419 * the string; otherwise store a 0 byte there and store 'delta' just prior
5420 * to that, using as many bytes as a STRLEN occupies. Thus it overwrites a
5421 * portion of the chopped part of the string */
5422 if (delta < 0x100) {
5426 p -= sizeof(STRLEN);
5427 Copy((U8*)&delta, p, sizeof(STRLEN), U8);
5431 /* Fill the preceding buffer with sentinals to verify that no-one is
5441 =for apidoc sv_catpvn
5442 =for apidoc_item sv_catpvn_flags
5443 =for apidoc_item sv_catpvn_mg
5444 =for apidoc_item sv_catpvn_nomg
5446 These concatenate the C<len> bytes of the string beginning at C<ptr> onto the
5447 end of the string which is in C<dsv>. The caller must make sure C<ptr>
5448 contains at least C<len> bytes.
5450 For all but C<sv_catpvn_flags>, the string appended is assumed to be valid
5451 UTF-8 if the SV has the UTF-8 status set, and a string of bytes otherwise.
5453 They differ in that:
5455 C<sv_catpvn_mg> performs both 'get' and 'set' magic on C<dsv>.
5457 C<sv_catpvn> performs only 'get' magic.
5459 C<sv_catpvn_nomg> skips all magic.
5461 C<sv_catpvn_flags> has an extra C<flags> parameter which allows you to specify
5462 any combination of magic handling (using C<SV_GMAGIC> and/or C<SV_SMAGIC>) and
5463 to also override the UTF-8 handling. By supplying the C<SV_CATBYTES> flag, the
5464 appended string is interpreted as plain bytes; by supplying instead the
5465 C<SV_CATUTF8> flag, it will be interpreted as UTF-8, and the C<dsv> will be
5466 upgraded to UTF-8 if necessary.
5468 C<sv_catpvn>, C<sv_catpvn_mg>, and C<sv_catpvn_nomg> are implemented
5469 in terms of C<sv_catpvn_flags>.
5471 =for apidoc Amnh||SV_CATUTF8
5472 =for apidoc Amnh||SV_CATBYTES
5478 Perl_sv_catpvn_flags(pTHX_ SV *const dsv, const char *sstr, const STRLEN slen, const I32 flags)
5481 const char * const dstr = SvPV_force_flags(dsv, dlen, flags);
5483 PERL_ARGS_ASSERT_SV_CATPVN_FLAGS;
5484 assert((flags & (SV_CATBYTES|SV_CATUTF8)) != (SV_CATBYTES|SV_CATUTF8));
5486 if (!(flags & SV_CATBYTES) || !SvUTF8(dsv)) {
5487 if (flags & SV_CATUTF8 && !SvUTF8(dsv)) {
5488 sv_utf8_upgrade_flags_grow(dsv, 0, slen + 1);
5491 else SvGROW(dsv, dlen + slen + 3);
5493 sstr = SvPVX_const(dsv);
5494 Move(sstr, SvPVX(dsv) + dlen, slen, char);
5495 SvCUR_set(dsv, SvCUR(dsv) + slen);
5498 /* We inline bytes_to_utf8, to avoid an extra malloc. */
5499 const char * const send = sstr + slen;
5502 /* Something this code does not account for, which I think is
5503 impossible; it would require the same pv to be treated as
5504 bytes *and* utf8, which would indicate a bug elsewhere. */
5505 assert(sstr != dstr);
5507 SvGROW(dsv, dlen + slen * 2 + 3);
5508 d = (U8 *)SvPVX(dsv) + dlen;
5510 while (sstr < send) {
5511 append_utf8_from_native_byte(*sstr, &d);
5514 SvCUR_set(dsv, d-(const U8 *)SvPVX(dsv));
5517 (void)SvPOK_only_UTF8(dsv); /* validate pointer */
5519 if (flags & SV_SMAGIC)
5524 =for apidoc sv_catsv
5525 =for apidoc_item sv_catsv_flags
5526 =for apidoc_item sv_catsv_mg
5527 =for apidoc_item sv_catsv_nomg
5529 These concatenate the string from SV C<sstr> onto the end of the string in SV
5530 C<dsv>. If C<sstr> is null, these are no-ops; otherwise only C<dsv> is
5533 They differ only in what magic they perform:
5535 C<sv_catsv_mg> performs 'get' magic on both SVs before the copy, and 'set' magic
5536 on C<dsv> afterwards.
5538 C<sv_catsv> performs just 'get' magic, on both SVs.
5540 C<sv_catsv_nomg> skips all magic.
5542 C<sv_catsv_flags> has an extra C<flags> parameter which allows you to use
5543 C<SV_GMAGIC> and/or C<SV_SMAGIC> to specify any combination of magic handling
5544 (although either both or neither SV will have 'get' magic applied to it.)
5546 C<sv_catsv>, C<sv_catsv_mg>, and C<sv_catsv_nomg> are implemented
5547 in terms of C<sv_catsv_flags>.
5552 Perl_sv_catsv_flags(pTHX_ SV *const dsv, SV *const sstr, const I32 flags)
5554 PERL_ARGS_ASSERT_SV_CATSV_FLAGS;
5558 const char *spv = SvPV_flags_const(sstr, slen, flags);
5559 if (flags & SV_GMAGIC)
5561 sv_catpvn_flags(dsv, spv, slen,
5562 DO_UTF8(sstr) ? SV_CATUTF8 : SV_CATBYTES);
5563 if (flags & SV_SMAGIC)
5569 =for apidoc sv_catpv
5570 =for apidoc_item sv_catpv_flags
5571 =for apidoc_item sv_catpv_mg
5572 =for apidoc_item sv_catpv_nomg
5574 These concatenate the C<NUL>-terminated string C<sstr> onto the end of the
5575 string which is in the SV.
5576 If the SV has the UTF-8 status set, then the bytes appended should be
5579 They differ only in how they handle magic:
5581 C<sv_catpv_mg> performs both 'get' and 'set' magic.
5583 C<sv_catpv> performs only 'get' magic.
5585 C<sv_catpv_nomg> skips all magic.
5587 C<sv_catpv_flags> has an extra C<flags> parameter which allows you to specify
5588 any combination of magic handling (using C<SV_GMAGIC> and/or C<SV_SMAGIC>), and
5589 to also override the UTF-8 handling. By supplying the C<SV_CATUTF8> flag, the
5590 appended string is forced to be interpreted as UTF-8; by supplying instead the
5591 C<SV_CATBYTES> flag, it will be interpreted as just bytes. Either the SV or
5592 the string appended will be upgraded to UTF-8 if necessary.
5598 Perl_sv_catpv(pTHX_ SV *const dsv, const char *sstr)
5604 PERL_ARGS_ASSERT_SV_CATPV;
5608 junk = SvPV_force(dsv, tlen);
5610 SvGROW(dsv, tlen + len + 1);
5612 sstr = SvPVX_const(dsv);
5613 Move(sstr,SvPVX(dsv)+tlen,len+1,char);
5614 SvCUR_set(dsv, SvCUR(dsv) + len);
5615 (void)SvPOK_only_UTF8(dsv); /* validate pointer */
5620 Perl_sv_catpv_flags(pTHX_ SV *dsv, const char *sstr, const I32 flags)
5622 PERL_ARGS_ASSERT_SV_CATPV_FLAGS;
5623 sv_catpvn_flags(dsv, sstr, strlen(sstr), flags);
5627 Perl_sv_catpv_mg(pTHX_ SV *const dsv, const char *const sstr)
5629 PERL_ARGS_ASSERT_SV_CATPV_MG;
5638 Creates a new SV. A non-zero C<len> parameter indicates the number of
5639 bytes of preallocated string space the SV should have. An extra byte for a
5640 trailing C<NUL> is also reserved. (C<SvPOK> is not set for the SV even if string
5641 space is allocated.) The reference count for the new SV is set to 1.
5643 In 5.9.3, C<newSV()> replaces the older C<NEWSV()> API, and drops the first
5644 parameter, I<x>, a debug aid which allowed callers to identify themselves.
5645 This aid has been superseded by a new build option, C<PERL_MEM_LOG> (see
5646 L<perlhacktips/PERL_MEM_LOG>). The older API is still there for use in XS
5647 modules supporting older perls.
5653 Perl_newSV(pTHX_ const STRLEN len)
5660 sv = newSV_type(SVt_PV);
5661 sv_grow_fresh(sv, len + 1);
5666 =for apidoc sv_magicext
5668 Adds magic to an SV, upgrading it if necessary. Applies the
5669 supplied C<vtable> and returns a pointer to the magic added.
5671 Note that C<sv_magicext> will allow things that C<sv_magic> will not.
5672 In particular, you can add magic to C<SvREADONLY> SVs, and add more than
5673 one instance of the same C<how>.
5675 If C<namlen> is greater than zero then a C<savepvn> I<copy> of C<name> is
5676 stored, if C<namlen> is zero then C<name> is stored as-is and - as another
5677 special case - if C<(name && namlen == HEf_SVKEY)> then C<name> is assumed
5678 to contain an SV* and is stored as-is with its C<REFCNT> incremented.
5680 (This is now used as a subroutine by C<sv_magic>.)
5685 Perl_sv_magicext(pTHX_ SV *const sv, SV *const obj, const int how,
5686 const MGVTBL *const vtable, const char *const name, const I32 namlen)
5690 PERL_ARGS_ASSERT_SV_MAGICEXT;
5692 SvUPGRADE(sv, SVt_PVMG);
5693 Newxz(mg, 1, MAGIC);
5694 mg->mg_moremagic = SvMAGIC(sv);
5695 SvMAGIC_set(sv, mg);
5697 /* Sometimes a magic contains a reference loop, where the sv and
5698 object refer to each other. To prevent a reference loop that
5699 would prevent such objects being freed, we look for such loops
5700 and if we find one we avoid incrementing the object refcount.
5702 Note we cannot do this to avoid self-tie loops as intervening RV must
5703 have its REFCNT incremented to keep it in existence.
5706 if (!obj || obj == sv ||
5707 how == PERL_MAGIC_arylen ||
5708 how == PERL_MAGIC_regdata ||
5709 how == PERL_MAGIC_regdatum ||
5710 how == PERL_MAGIC_symtab ||
5711 (SvTYPE(obj) == SVt_PVGV &&
5712 (GvSV(obj) == sv || GvHV(obj) == (const HV *)sv
5713 || GvAV(obj) == (const AV *)sv || GvCV(obj) == (const CV *)sv
5714 || GvIOp(obj) == (const IO *)sv || GvFORM(obj) == (const CV *)sv)))
5719 mg->mg_obj = SvREFCNT_inc_simple(obj);
5720 mg->mg_flags |= MGf_REFCOUNTED;
5723 /* Normal self-ties simply pass a null object, and instead of
5724 using mg_obj directly, use the SvTIED_obj macro to produce a
5725 new RV as needed. For glob "self-ties", we are tieing the PVIO
5726 with an RV obj pointing to the glob containing the PVIO. In
5727 this case, to avoid a reference loop, we need to weaken the
5731 if (how == PERL_MAGIC_tiedscalar && SvTYPE(sv) == SVt_PVIO &&
5732 obj && SvROK(obj) && GvIO(SvRV(obj)) == (const IO *)sv)
5738 mg->mg_len = namlen;
5741 mg->mg_ptr = savepvn(name, namlen);
5742 else if (namlen == HEf_SVKEY) {
5743 /* Yes, this is casting away const. This is only for the case of
5744 HEf_SVKEY. I think we need to document this aberation of the
5745 constness of the API, rather than making name non-const, as
5746 that change propagating outwards a long way. */
5747 mg->mg_ptr = (char*)SvREFCNT_inc_simple_NN((SV *)name);
5749 mg->mg_ptr = (char *) name;
5751 mg->mg_virtual = (MGVTBL *) vtable;
5758 Perl_sv_magicext_mglob(pTHX_ SV *sv)
5760 PERL_ARGS_ASSERT_SV_MAGICEXT_MGLOB;
5761 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
5762 /* This sv is only a delegate. //g magic must be attached to
5767 return sv_magicext(sv, NULL, PERL_MAGIC_regex_global,
5768 &PL_vtbl_mglob, 0, 0);
5772 =for apidoc sv_magic
5774 Adds magic to an SV. First upgrades C<sv> to type C<SVt_PVMG> if
5775 necessary, then adds a new magic item of type C<how> to the head of the
5778 See C<L</sv_magicext>> (which C<sv_magic> now calls) for a description of the
5779 handling of the C<name> and C<namlen> arguments.
5781 You need to use C<sv_magicext> to add magic to C<SvREADONLY> SVs and also
5782 to add more than one instance of the same C<how>.
5788 Perl_sv_magic(pTHX_ SV *const sv, SV *const obj, const int how,
5789 const char *const name, const I32 namlen)
5791 const MGVTBL *vtable;
5794 unsigned int vtable_index;
5796 PERL_ARGS_ASSERT_SV_MAGIC;
5798 if (how < 0 || (unsigned)how >= C_ARRAY_LENGTH(PL_magic_data)
5799 || ((flags = PL_magic_data[how]),
5800 (vtable_index = flags & PERL_MAGIC_VTABLE_MASK)
5801 > magic_vtable_max))
5802 Perl_croak(aTHX_ "Don't know how to handle magic of type \\%o", how);
5804 /* PERL_MAGIC_ext is reserved for use by extensions not perl internals.
5805 Useful for attaching extension internal data to perl vars.
5806 Note that multiple extensions may clash if magical scalars
5807 etc holding private data from one are passed to another. */
5809 vtable = (vtable_index == magic_vtable_max)
5810 ? NULL : PL_magic_vtables + vtable_index;
5812 if (SvREADONLY(sv)) {
5814 !PERL_MAGIC_TYPE_READONLY_ACCEPTABLE(how)
5817 Perl_croak_no_modify();
5820 if (SvMAGICAL(sv) || (how == PERL_MAGIC_taint && SvTYPE(sv) >= SVt_PVMG)) {
5821 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
5822 /* sv_magic() refuses to add a magic of the same 'how' as an
5825 if (how == PERL_MAGIC_taint)
5831 /* Rest of work is done else where */
5832 mg = sv_magicext(sv,obj,how,vtable,name,namlen);
5835 case PERL_MAGIC_taint:
5838 case PERL_MAGIC_ext:
5839 case PERL_MAGIC_dbfile:
5846 S_sv_unmagicext_flags(pTHX_ SV *const sv, const int type, MGVTBL *vtbl, const U32 flags)
5853 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
5855 mgp = &(((XPVMG*) SvANY(sv))->xmg_u.xmg_magic);
5856 for (mg = *mgp; mg; mg = *mgp) {
5857 const MGVTBL* const virt = mg->mg_virtual;
5858 if (mg->mg_type == type && (!flags || virt == vtbl)) {
5859 *mgp = mg->mg_moremagic;
5860 if (virt && virt->svt_free)
5861 virt->svt_free(aTHX_ sv, mg);
5862 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
5864 Safefree(mg->mg_ptr);
5865 else if (mg->mg_len == HEf_SVKEY)
5866 SvREFCNT_dec(MUTABLE_SV(mg->mg_ptr));
5867 else if (mg->mg_type == PERL_MAGIC_utf8)
5868 Safefree(mg->mg_ptr);
5870 if (mg->mg_flags & MGf_REFCOUNTED)
5871 SvREFCNT_dec(mg->mg_obj);
5875 mgp = &mg->mg_moremagic;
5878 if (SvMAGICAL(sv)) /* if we're under save_magic, wait for restore_magic; */
5879 mg_magical(sv); /* else fix the flags now */
5888 =for apidoc sv_unmagic
5890 Removes all magic of type C<type> from an SV.
5896 Perl_sv_unmagic(pTHX_ SV *const sv, const int type)
5898 PERL_ARGS_ASSERT_SV_UNMAGIC;
5899 return S_sv_unmagicext_flags(aTHX_ sv, type, NULL, 0);
5903 =for apidoc sv_unmagicext
5905 Removes all magic of type C<type> with the specified C<vtbl> from an SV.
5911 Perl_sv_unmagicext(pTHX_ SV *const sv, const int type, MGVTBL *vtbl)
5913 PERL_ARGS_ASSERT_SV_UNMAGICEXT;
5914 return S_sv_unmagicext_flags(aTHX_ sv, type, vtbl, 1);
5918 =for apidoc sv_rvweaken
5920 Weaken a reference: set the C<SvWEAKREF> flag on this RV; give the
5921 referred-to SV C<PERL_MAGIC_backref> magic if it hasn't already; and
5922 push a back-reference to this RV onto the array of backreferences
5923 associated with that magic. If the RV is magical, set magic will be
5924 called after the RV is cleared. Silently ignores C<undef> and warns
5925 on already-weak references.
5931 Perl_sv_rvweaken(pTHX_ SV *const sv)
5935 PERL_ARGS_ASSERT_SV_RVWEAKEN;
5937 if (!SvOK(sv)) /* let undefs pass */
5940 Perl_croak(aTHX_ "Can't weaken a nonreference");
5941 else if (SvWEAKREF(sv)) {
5942 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Reference is already weak");
5945 else if (SvREADONLY(sv)) croak_no_modify();
5947 Perl_sv_add_backref(aTHX_ tsv, sv);
5949 SvREFCNT_dec_NN(tsv);
5954 =for apidoc sv_rvunweaken
5956 Unweaken a reference: Clear the C<SvWEAKREF> flag on this RV; remove
5957 the backreference to this RV from the array of backreferences
5958 associated with the target SV, increment the refcount of the target.
5959 Silently ignores C<undef> and warns on non-weak references.
5965 Perl_sv_rvunweaken(pTHX_ SV *const sv)
5969 PERL_ARGS_ASSERT_SV_RVUNWEAKEN;
5971 if (!SvOK(sv)) /* let undefs pass */
5974 Perl_croak(aTHX_ "Can't unweaken a nonreference");
5975 else if (!SvWEAKREF(sv)) {
5976 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Reference is not weak");
5979 else if (SvREADONLY(sv)) croak_no_modify();
5984 SvREFCNT_inc_NN(tsv);
5985 Perl_sv_del_backref(aTHX_ tsv, sv);
5990 =for apidoc sv_get_backrefs
5992 If C<sv> is the target of a weak reference then it returns the back
5993 references structure associated with the sv; otherwise return C<NULL>.
5995 When returning a non-null result the type of the return is relevant. If it
5996 is an AV then the elements of the AV are the weak reference RVs which
5997 point at this item. If it is any other type then the item itself is the
6000 See also C<Perl_sv_add_backref()>, C<Perl_sv_del_backref()>,
6001 C<Perl_sv_kill_backrefs()>
6007 Perl_sv_get_backrefs(SV *const sv)
6011 PERL_ARGS_ASSERT_SV_GET_BACKREFS;
6013 /* find slot to store array or singleton backref */
6015 if (SvTYPE(sv) == SVt_PVHV) {
6017 struct xpvhv_aux * const iter = HvAUX((HV *)sv);
6018 backrefs = (SV *)iter->xhv_backreferences;
6020 } else if (SvMAGICAL(sv)) {
6021 MAGIC *mg = mg_find(sv, PERL_MAGIC_backref);
6023 backrefs = mg->mg_obj;
6028 /* Give tsv backref magic if it hasn't already got it, then push a
6029 * back-reference to sv onto the array associated with the backref magic.
6031 * As an optimisation, if there's only one backref and it's not an AV,
6032 * store it directly in the HvAUX or mg_obj slot, avoiding the need to
6033 * allocate an AV. (Whether the slot holds an AV tells us whether this is
6037 /* A discussion about the backreferences array and its refcount:
6039 * The AV holding the backreferences is pointed to either as the mg_obj of
6040 * PERL_MAGIC_backref, or in the specific case of a HV, from the
6041 * xhv_backreferences field. The array is created with a refcount
6042 * of 2. This means that if during global destruction the array gets
6043 * picked on before its parent to have its refcount decremented by the
6044 * random zapper, it won't actually be freed, meaning it's still there for
6045 * when its parent gets freed.
6047 * When the parent SV is freed, the extra ref is killed by
6048 * Perl_sv_kill_backrefs. The other ref is killed, in the case of magic,
6049 * by mg_free() / MGf_REFCOUNTED, or for a hash, by Perl_hv_kill_backrefs.
6051 * When a single backref SV is stored directly, it is not reference
6056 Perl_sv_add_backref(pTHX_ SV *const tsv, SV *const sv)
6062 PERL_ARGS_ASSERT_SV_ADD_BACKREF;
6064 /* find slot to store array or singleton backref */
6066 if (SvTYPE(tsv) == SVt_PVHV) {
6067 svp = (SV**)Perl_hv_backreferences_p(aTHX_ MUTABLE_HV(tsv));
6070 mg = mg_find(tsv, PERL_MAGIC_backref);
6072 mg = sv_magicext(tsv, NULL, PERL_MAGIC_backref, &PL_vtbl_backref, NULL, 0);
6073 svp = &(mg->mg_obj);
6076 /* create or retrieve the array */
6078 if ( (!*svp && SvTYPE(sv) == SVt_PVAV)
6079 || (*svp && SvTYPE(*svp) != SVt_PVAV)
6083 mg->mg_flags |= MGf_REFCOUNTED;
6086 SvREFCNT_inc_simple_void_NN(av);
6087 /* av now has a refcnt of 2; see discussion above */
6088 av_extend(av, *svp ? 2 : 1);
6090 /* move single existing backref to the array */
6091 AvARRAY(av)[++AvFILLp(av)] = *svp; /* av_push() */
6096 av = MUTABLE_AV(*svp);
6098 /* optimisation: store single backref directly in HvAUX or mg_obj */
6102 assert(SvTYPE(av) == SVt_PVAV);
6103 if (AvFILLp(av) >= AvMAX(av)) {
6104 av_extend(av, AvFILLp(av)+1);
6107 /* push new backref */
6108 AvARRAY(av)[++AvFILLp(av)] = sv; /* av_push() */
6111 /* delete a back-reference to ourselves from the backref magic associated
6112 * with the SV we point to.
6116 Perl_sv_del_backref(pTHX_ SV *const tsv, SV *const sv)
6120 PERL_ARGS_ASSERT_SV_DEL_BACKREF;
6122 if (SvTYPE(tsv) == SVt_PVHV) {
6124 svp = (SV**)Perl_hv_backreferences_p(aTHX_ MUTABLE_HV(tsv));
6126 else if (SvIS_FREED(tsv) && PL_phase == PERL_PHASE_DESTRUCT) {
6127 /* It's possible for the the last (strong) reference to tsv to have
6128 become freed *before* the last thing holding a weak reference.
6129 If both survive longer than the backreferences array, then when
6130 the referent's reference count drops to 0 and it is freed, it's
6131 not able to chase the backreferences, so they aren't NULLed.
6133 For example, a CV holds a weak reference to its stash. If both the
6134 CV and the stash survive longer than the backreferences array,
6135 and the CV gets picked for the SvBREAK() treatment first,
6136 *and* it turns out that the stash is only being kept alive because
6137 of an our variable in the pad of the CV, then midway during CV
6138 destruction the stash gets freed, but CvSTASH() isn't set to NULL.
6139 It ends up pointing to the freed HV. Hence it's chased in here, and
6140 if this block wasn't here, it would hit the !svp panic just below.
6142 I don't believe that "better" destruction ordering is going to help
6143 here - during global destruction there's always going to be the
6144 chance that something goes out of order. We've tried to make it
6145 foolproof before, and it only resulted in evolutionary pressure on
6146 fools. Which made us look foolish for our hubris. :-(
6152 = SvMAGICAL(tsv) ? mg_find(tsv, PERL_MAGIC_backref) : NULL;
6153 svp = mg ? &(mg->mg_obj) : NULL;
6157 Perl_croak(aTHX_ "panic: del_backref, svp=0");
6159 /* It's possible that sv is being freed recursively part way through the
6160 freeing of tsv. If this happens, the backreferences array of tsv has
6161 already been freed, and so svp will be NULL. If this is the case,
6162 we should not panic. Instead, nothing needs doing, so return. */
6163 if (PL_phase == PERL_PHASE_DESTRUCT && SvREFCNT(tsv) == 0)
6165 Perl_croak(aTHX_ "panic: del_backref, *svp=%p phase=%s refcnt=%" UVuf,
6166 (void*)*svp, PL_phase_names[PL_phase], (UV)SvREFCNT(tsv));
6169 if (SvTYPE(*svp) == SVt_PVAV) {
6173 AV * const av = (AV*)*svp;
6175 assert(!SvIS_FREED(av));
6179 /* for an SV with N weak references to it, if all those
6180 * weak refs are deleted, then sv_del_backref will be called
6181 * N times and O(N^2) compares will be done within the backref
6182 * array. To ameliorate this potential slowness, we:
6183 * 1) make sure this code is as tight as possible;
6184 * 2) when looking for SV, look for it at both the head and tail of the
6185 * array first before searching the rest, since some create/destroy
6186 * patterns will cause the backrefs to be freed in order.
6193 SV **p = &svp[fill];
6194 SV *const topsv = *p;
6201 /* We weren't the last entry.
6202 An unordered list has this property that you
6203 can take the last element off the end to fill
6204 the hole, and it's still an unordered list :-)
6210 break; /* should only be one */
6217 AvFILLp(av) = fill-1;
6219 else if (SvIS_FREED(*svp) && PL_phase == PERL_PHASE_DESTRUCT) {
6220 /* freed AV; skip */
6223 /* optimisation: only a single backref, stored directly */
6225 Perl_croak(aTHX_ "panic: del_backref, *svp=%p, sv=%p",
6226 (void*)*svp, (void*)sv);
6233 Perl_sv_kill_backrefs(pTHX_ SV *const sv, AV *const av)
6239 PERL_ARGS_ASSERT_SV_KILL_BACKREFS;
6244 /* after multiple passes through Perl_sv_clean_all() for a thingy
6245 * that has badly leaked, the backref array may have gotten freed,
6246 * since we only protect it against 1 round of cleanup */
6247 if (SvIS_FREED(av)) {
6248 if (PL_in_clean_all) /* All is fair */
6251 "panic: magic_killbackrefs (freed backref AV/SV)");
6255 is_array = (SvTYPE(av) == SVt_PVAV);
6257 assert(!SvIS_FREED(av));
6260 last = svp + AvFILLp(av);
6263 /* optimisation: only a single backref, stored directly */
6269 while (svp <= last) {
6271 SV *const referrer = *svp;
6272 if (SvWEAKREF(referrer)) {
6273 /* XXX Should we check that it hasn't changed? */
6274 assert(SvROK(referrer));
6275 SvRV_set(referrer, 0);
6277 SvWEAKREF_off(referrer);
6278 SvSETMAGIC(referrer);
6279 } else if (SvTYPE(referrer) == SVt_PVGV ||
6280 SvTYPE(referrer) == SVt_PVLV) {
6281 assert(SvTYPE(sv) == SVt_PVHV); /* stash backref */
6282 /* You lookin' at me? */
6283 assert(GvSTASH(referrer));
6284 assert(GvSTASH(referrer) == (const HV *)sv);
6285 GvSTASH(referrer) = 0;
6286 } else if (SvTYPE(referrer) == SVt_PVCV ||
6287 SvTYPE(referrer) == SVt_PVFM) {
6288 if (SvTYPE(sv) == SVt_PVHV) { /* stash backref */
6289 /* You lookin' at me? */
6290 assert(CvSTASH(referrer));
6291 assert(CvSTASH(referrer) == (const HV *)sv);
6292 SvANY(MUTABLE_CV(referrer))->xcv_stash = 0;
6295 assert(SvTYPE(sv) == SVt_PVGV);
6296 /* You lookin' at me? */
6297 assert(CvGV(referrer));
6298 assert(CvGV(referrer) == (const GV *)sv);
6299 anonymise_cv_maybe(MUTABLE_GV(sv),
6300 MUTABLE_CV(referrer));
6305 "panic: magic_killbackrefs (flags=%" UVxf ")",
6306 (UV)SvFLAGS(referrer));
6317 SvREFCNT_dec_NN(av); /* remove extra count added by sv_add_backref() */
6323 =for apidoc sv_insert
6325 Inserts and/or replaces a string at the specified offset/length within the SV.
6326 Similar to the Perl C<substr()> function, with C<littlelen> bytes starting at
6327 C<little> replacing C<len> bytes of the string in C<bigstr> starting at
6328 C<offset>. Handles get magic.
6330 =for apidoc sv_insert_flags
6332 Same as C<sv_insert>, but the extra C<flags> are passed to the
6333 C<SvPV_force_flags> that applies to C<bigstr>.
6339 Perl_sv_insert_flags(pTHX_ SV *const bigstr, const STRLEN offset, const STRLEN len, const char *little, const STRLEN littlelen, const U32 flags)
6345 SSize_t i; /* better be sizeof(STRLEN) or bad things happen */
6348 PERL_ARGS_ASSERT_SV_INSERT_FLAGS;
6350 SvPV_force_flags(bigstr, curlen, flags);
6351 (void)SvPOK_only_UTF8(bigstr);
6353 if (little >= SvPVX(bigstr) &&
6354 little < SvPVX(bigstr) + (SvLEN(bigstr) ? SvLEN(bigstr) : SvCUR(bigstr))) {
6355 /* little is a pointer to within bigstr, since we can reallocate bigstr,
6356 or little...little+littlelen might overlap offset...offset+len we make a copy
6358 little = savepvn(little, littlelen);
6362 if (offset + len > curlen) {
6363 SvGROW(bigstr, offset+len+1);
6364 Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
6365 SvCUR_set(bigstr, offset+len);
6369 i = littlelen - len;
6370 if (i > 0) { /* string might grow */
6371 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
6372 mid = big + offset + len;
6373 midend = bigend = big + SvCUR(bigstr);
6376 while (midend > mid) /* shove everything down */
6377 *--bigend = *--midend;
6378 Move(little,big+offset,littlelen,char);
6379 SvCUR_set(bigstr, SvCUR(bigstr) + i);
6384 Move(little,SvPVX(bigstr)+offset,len,char);
6389 big = SvPVX(bigstr);
6392 bigend = big + SvCUR(bigstr);
6394 if (midend > bigend)
6395 Perl_croak(aTHX_ "panic: sv_insert, midend=%p, bigend=%p",
6398 if (mid - big > bigend - midend) { /* faster to shorten from end */
6400 Move(little, mid, littlelen,char);
6403 i = bigend - midend;
6405 Move(midend, mid, i,char);
6409 SvCUR_set(bigstr, mid - big);
6411 else if ((i = mid - big)) { /* faster from front */
6412 midend -= littlelen;
6414 Move(big, midend - i, i, char);
6415 sv_chop(bigstr,midend-i);
6417 Move(little, mid, littlelen,char);
6419 else if (littlelen) {
6420 midend -= littlelen;
6421 sv_chop(bigstr,midend);
6422 Move(little,midend,littlelen,char);
6425 sv_chop(bigstr,midend);
6431 =for apidoc sv_replace
6433 Make the first argument a copy of the second, then delete the original.
6434 The target SV physically takes over ownership of the body of the source SV
6435 and inherits its flags; however, the target keeps any magic it owns,
6436 and any magic in the source is discarded.
6437 Note that this is a rather specialist SV copying operation; most of the
6438 time you'll want to use C<sv_setsv> or one of its many macro front-ends.
6444 Perl_sv_replace(pTHX_ SV *const sv, SV *const nsv)
6446 const U32 refcnt = SvREFCNT(sv);
6448 PERL_ARGS_ASSERT_SV_REPLACE;
6450 SV_CHECK_THINKFIRST_COW_DROP(sv);
6451 if (SvREFCNT(nsv) != 1) {
6452 Perl_croak(aTHX_ "panic: reference miscount on nsv in sv_replace()"
6453 " (%" UVuf " != 1)", (UV) SvREFCNT(nsv));
6455 if (SvMAGICAL(sv)) {
6459 sv_upgrade(nsv, SVt_PVMG);
6460 SvMAGIC_set(nsv, SvMAGIC(sv));
6461 SvFLAGS(nsv) |= SvMAGICAL(sv);
6463 SvMAGIC_set(sv, NULL);
6467 assert(!SvREFCNT(sv));
6468 #ifdef DEBUG_LEAKING_SCALARS
6469 sv->sv_flags = nsv->sv_flags;
6470 sv->sv_any = nsv->sv_any;
6471 sv->sv_refcnt = nsv->sv_refcnt;
6472 sv->sv_u = nsv->sv_u;
6474 StructCopy(nsv,sv,SV);
6476 if(SvTYPE(sv) == SVt_IV) {
6477 SET_SVANY_FOR_BODYLESS_IV(sv);
6481 SvREFCNT(sv) = refcnt;
6482 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
6487 /* We're about to free a GV which has a CV that refers back to us.
6488 * If that CV will outlive us, make it anonymous (i.e. fix up its CvGV
6492 S_anonymise_cv_maybe(pTHX_ GV *gv, CV* cv)
6497 PERL_ARGS_ASSERT_ANONYMISE_CV_MAYBE;
6500 assert(SvREFCNT(gv) == 0);
6501 assert(isGV(gv) && isGV_with_GP(gv));
6503 assert(!CvANON(cv));
6504 assert(CvGV(cv) == gv);
6505 assert(!CvNAMED(cv));
6507 /* will the CV shortly be freed by gp_free() ? */
6508 if (GvCV(gv) == cv && GvGP(gv)->gp_refcnt < 2 && SvREFCNT(cv) < 2) {
6509 SvANY(cv)->xcv_gv_u.xcv_gv = NULL;
6513 /* if not, anonymise: */
6514 gvname = (GvSTASH(gv) && HvNAME(GvSTASH(gv)) && HvENAME(GvSTASH(gv)))
6515 ? newSVhek(HvENAME_HEK(GvSTASH(gv)))
6516 : newSVpvn_flags( "__ANON__", 8, 0 );
6517 sv_catpvs(gvname, "::__ANON__");
6518 anongv = gv_fetchsv(gvname, GV_ADDMULTI, SVt_PVCV);
6519 SvREFCNT_dec_NN(gvname);
6523 SvANY(cv)->xcv_gv_u.xcv_gv = MUTABLE_GV(SvREFCNT_inc(anongv));
6528 =for apidoc sv_clear
6530 Clear an SV: call any destructors, free up any memory used by the body,
6531 and free the body itself. The SV's head is I<not> freed, although
6532 its type is set to all 1's so that it won't inadvertently be assumed
6533 to be live during global destruction etc.
6534 This function should only be called when C<REFCNT> is zero. Most of the time
6535 you'll want to call C<sv_free()> (or its macro wrapper C<SvREFCNT_dec>)
6542 Perl_sv_clear(pTHX_ SV *const orig_sv)
6547 STRLEN hash_index = 0; /* initialise to make Coverity et al happy.
6548 Not strictly necessary */
6550 PERL_ARGS_ASSERT_SV_CLEAR;
6552 /* within this loop, sv is the SV currently being freed, and
6553 * iter_sv is the most recent AV or whatever that's being iterated
6554 * over to provide more SVs */
6557 U32 type = SvTYPE(sv);
6560 assert(SvREFCNT(sv) == 0);
6561 assert(SvTYPE(sv) != (svtype)SVTYPEMASK);
6563 if (type <= SVt_IV) {
6564 /* Historically this check on type was needed so that the code to
6565 * free bodies wasn't reached for these types, because the arena
6566 * slots were re-used for HEs and pointer table entries. The
6567 * metadata table `bodies_by_type` had the information for the sizes
6568 * for HEs and PTEs, hence the code here had to have a special-case
6569 * check to ensure that the "regular" body freeing code wasn't
6570 * reached, and get confused by the "lies" in `bodies_by_type`.
6572 * However, it hasn't actually been needed for that reason since
6573 * Aug 2010 (commit 829cd18aa7f45221), because `bodies_by_type` was
6574 * changed to always hold the accurate metadata for the SV types.
6575 * This was possible because PTEs were no longer allocated from the
6576 * "SVt_IV" arena, and the code to allocate HEs from the "SVt_NULL"
6577 * arena is entirely in hv.c, so doesn't access the table.
6579 * Some sort of check is still needed to handle SVt_IVs - pure RVs
6580 * need to take one code path which is common with RVs stored in
6581 * SVt_PV (or larger), but pure IVs mustn't take the "PV but not RV"
6582 * path, as SvPVX() doesn't point to valid memory.
6584 * Hence this code is still the most efficient way to handle this.
6589 SvFLAGS(sv) &= SVf_BREAK;
6590 SvFLAGS(sv) |= SVTYPEMASK;
6594 /* objs are always >= MG, but pad names use the SVs_OBJECT flag
6595 for another purpose */
6596 assert(!SvOBJECT(sv) || type >= SVt_PVMG);
6598 if (type >= SVt_PVMG) {
6600 if (!curse(sv, 1)) goto get_next_sv;
6601 type = SvTYPE(sv); /* destructor may have changed it */
6603 /* Free back-references before magic, in case the magic calls
6604 * Perl code that has weak references to sv. */
6605 if (type == SVt_PVHV) {
6606 Perl_hv_kill_backrefs(aTHX_ MUTABLE_HV(sv));
6610 else if (SvMAGIC(sv)) {
6611 /* Free back-references before other types of magic. */
6612 sv_unmagic(sv, PERL_MAGIC_backref);
6618 /* case SVt_INVLIST: */
6621 IoIFP(sv) != PerlIO_stdin() &&
6622 IoIFP(sv) != PerlIO_stdout() &&
6623 IoIFP(sv) != PerlIO_stderr() &&
6624 !(IoFLAGS(sv) & IOf_FAKE_DIRP))
6626 io_close(MUTABLE_IO(sv), NULL, FALSE,
6627 (IoTYPE(sv) == IoTYPE_WRONLY ||
6628 IoTYPE(sv) == IoTYPE_RDWR ||
6629 IoTYPE(sv) == IoTYPE_APPEND));
6631 if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP))
6632 PerlDir_close(IoDIRP(sv));
6633 IoDIRP(sv) = (DIR*)NULL;
6634 Safefree(IoTOP_NAME(sv));
6635 Safefree(IoFMT_NAME(sv));
6636 Safefree(IoBOTTOM_NAME(sv));
6637 if ((const GV *)sv == PL_statgv)
6641 /* FIXME for plugins */
6642 pregfree2((REGEXP*) sv);
6646 cv_undef(MUTABLE_CV(sv));
6647 /* If we're in a stash, we don't own a reference to it.
6648 * However it does have a back reference to us, which needs to
6650 if ((stash = CvSTASH(sv)))
6651 sv_del_backref(MUTABLE_SV(stash), sv);
6654 if (HvTOTALKEYS((HV*)sv) > 0) {
6656 /* this statement should match the one at the beginning of
6657 * hv_undef_flags() */
6658 if ( PL_phase != PERL_PHASE_DESTRUCT
6659 && (hek = HvNAME_HEK((HV*)sv)))
6661 if (PL_stashcache) {
6662 DEBUG_o(Perl_deb(aTHX_
6663 "sv_clear clearing PL_stashcache for '%" HEKf
6666 (void)hv_deletehek(PL_stashcache,
6669 hv_name_set((HV*)sv, NULL, 0, 0);
6672 /* save old iter_sv in unused SvSTASH field */
6673 assert(!SvOBJECT(sv));
6674 SvSTASH(sv) = (HV*)iter_sv;
6677 /* save old hash_index in unused SvMAGIC field */
6678 assert(!SvMAGICAL(sv));
6679 assert(!SvMAGIC(sv));
6680 ((XPVMG*) SvANY(sv))->xmg_u.xmg_hash_index = hash_index;
6683 next_sv = Perl_hfree_next_entry(aTHX_ (HV*)sv, &hash_index);
6684 goto get_next_sv; /* process this new sv */
6686 /* free empty hash */
6687 Perl_hv_undef_flags(aTHX_ MUTABLE_HV(sv), HV_NAME_SETALL);
6688 assert(!HvARRAY((HV*)sv));
6692 AV* av = MUTABLE_AV(sv);
6693 if (PL_comppad == av) {
6697 if (AvREAL(av) && AvFILLp(av) > -1) {
6698 next_sv = AvARRAY(av)[AvFILLp(av)--];
6699 /* save old iter_sv in top-most slot of AV,
6700 * and pray that it doesn't get wiped in the meantime */
6701 AvARRAY(av)[AvMAX(av)] = iter_sv;
6703 goto get_next_sv; /* process this new sv */
6705 Safefree(AvALLOC(av));
6710 if (LvTYPE(sv) == 'T') { /* for tie: return HE to pool */
6711 SvREFCNT_dec(HeKEY_sv((HE*)LvTARG(sv)));
6712 HeNEXT((HE*)LvTARG(sv)) = PL_hv_fetch_ent_mh;
6713 PL_hv_fetch_ent_mh = (HE*)LvTARG(sv);
6715 else if (LvTYPE(sv) != 't') /* unless tie: unrefcnted fake SV** */
6716 SvREFCNT_dec(LvTARG(sv));
6718 /* This PVLV has had a REGEXP assigned to it - the memory
6719 * normally used to store SvLEN instead points to a regex body.
6720 * Retrieving the pointer to the regex body from the correct
6721 * location is normally abstracted by ReANY(), which handles
6722 * both SVt_PVLV and SVt_REGEXP
6724 * This code is unwinding the storage specific to SVt_PVLV.
6725 * We get the body pointer directly from the union, free it,
6726 * then set SvLEN to whatever value was in the now-freed regex
6727 * body. The PVX buffer is shared by multiple re's and only
6728 * freed once, by the re whose SvLEN is non-null.
6730 * Perl_sv_force_normal_flags() also has code to free this
6731 * hidden body - it swaps the body into a temporary SV it has
6732 * just allocated, then frees that SV. That causes execution
6733 * to reach the SVt_REGEXP: case about 60 lines earlier in this
6736 * See Perl_reg_temp_copy() for the code that sets up this
6737 * REGEXP body referenced by the PVLV. */
6738 struct regexp *r = ((XPV*)SvANY(sv))->xpv_len_u.xpvlenu_rx;
6739 STRLEN len = r->xpv_len;
6740 pregfree2((REGEXP*) sv);
6741 del_body_by_type(r, SVt_REGEXP);
6742 SvLEN_set((sv), len);
6747 if (isGV_with_GP(sv)) {
6748 if(GvCVu((const GV *)sv) && (stash = GvSTASH(MUTABLE_GV(sv)))
6749 && HvENAME_get(stash))
6750 mro_method_changed_in(stash);
6751 gp_free(MUTABLE_GV(sv));
6753 unshare_hek(GvNAME_HEK(sv));
6754 /* If we're in a stash, we don't own a reference to it.
6755 * However it does have a back reference to us, which
6756 * needs to be cleared. */
6757 if ((stash = GvSTASH(sv)))
6758 sv_del_backref(MUTABLE_SV(stash), sv);
6760 /* FIXME. There are probably more unreferenced pointers to SVs
6761 * in the interpreter struct that we should check and tidy in
6762 * a similar fashion to this: */
6763 /* See also S_sv_unglob, which does the same thing. */
6764 if ((const GV *)sv == PL_last_in_gv)
6765 PL_last_in_gv = NULL;
6766 else if ((const GV *)sv == PL_statgv)
6768 else if ((const GV *)sv == PL_stderrgv)
6777 /* Don't bother with SvOOK_off(sv); as we're only going to
6781 SvOOK_offset(sv, offset);
6782 SvPV_set(sv, SvPVX_mutable(sv) - offset);
6783 /* Don't even bother with turning off the OOK flag. */
6788 SV * const target = SvRV(sv);
6790 sv_del_backref(target, sv);
6796 else if (SvPVX_const(sv)
6797 && !(SvTYPE(sv) == SVt_PVIO
6798 && !(IoFLAGS(sv) & IOf_FAKE_DIRP)))
6803 PerlIO_printf(Perl_debug_log, "Copy on write: clear\n");
6807 if (SvIsCOW_static(sv)) {
6810 else if (SvIsCOW_shared_hash(sv)) {
6811 unshare_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sv)));
6814 if (CowREFCNT(sv)) {
6823 Safefree(SvPVX_mutable(sv));
6827 else if (SvPVX_const(sv) && SvLEN(sv)
6828 && !(SvTYPE(sv) == SVt_PVIO
6829 && !(IoFLAGS(sv) & IOf_FAKE_DIRP)))
6830 Safefree(SvPVX_mutable(sv));
6831 else if (SvPVX_const(sv) && SvIsCOW(sv)) {
6832 unshare_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sv)));
6844 const struct body_details *sv_type_details;
6846 if (type == SVt_PVHV && HvHasAUX(sv)) {
6847 arena_index = HVAUX_ARENA_ROOT_IX;
6848 sv_type_details = &fake_hv_with_aux;
6852 sv_type_details = bodies_by_type + arena_index;
6855 SvFLAGS(sv) &= SVf_BREAK;
6856 SvFLAGS(sv) |= SVTYPEMASK;
6858 if (sv_type_details->arena) {
6859 del_body(((char *)SvANY(sv) + sv_type_details->offset),
6860 &PL_body_roots[arena_index]);
6862 else if (sv_type_details->body_size) {
6863 safefree(SvANY(sv));
6868 /* caller is responsible for freeing the head of the original sv */
6869 if (sv != orig_sv && !SvREFCNT(sv))
6872 /* grab and free next sv, if any */
6880 else if (!iter_sv) {
6882 } else if (SvTYPE(iter_sv) == SVt_PVAV) {
6883 AV *const av = (AV*)iter_sv;
6884 if (AvFILLp(av) > -1) {
6885 sv = AvARRAY(av)[AvFILLp(av)--];
6887 else { /* no more elements of current AV to free */
6890 /* restore previous value, squirrelled away */
6891 iter_sv = AvARRAY(av)[AvMAX(av)];
6892 Safefree(AvALLOC(av));
6895 } else if (SvTYPE(iter_sv) == SVt_PVHV) {
6896 sv = Perl_hfree_next_entry(aTHX_ (HV*)iter_sv, &hash_index);
6897 if (!sv && !HvTOTALKEYS((HV *)iter_sv)) {
6898 /* no more elements of current HV to free */
6901 /* Restore previous values of iter_sv and hash_index,
6902 * squirrelled away */
6903 assert(!SvOBJECT(sv));
6904 iter_sv = (SV*)SvSTASH(sv);
6905 assert(!SvMAGICAL(sv));
6906 hash_index = ((XPVMG*) SvANY(sv))->xmg_u.xmg_hash_index;
6908 /* perl -DA does not like rubbish in SvMAGIC. */
6912 /* free any remaining detritus from the hash struct */
6913 Perl_hv_undef_flags(aTHX_ MUTABLE_HV(sv), HV_NAME_SETALL);
6914 assert(!HvARRAY((HV*)sv));
6919 /* unrolled SvREFCNT_dec and sv_free2 follows: */
6923 if (!SvREFCNT(sv)) {
6927 if (--(SvREFCNT(sv)))
6931 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEBUGGING),
6932 "Attempt to free temp prematurely: SV 0x%" UVxf
6933 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
6937 if (SvIMMORTAL(sv)) {
6938 /* make sure SvREFCNT(sv)==0 happens very seldom */
6939 SvREFCNT(sv) = SvREFCNT_IMMORTAL;
6948 /* This routine curses the sv itself, not the object referenced by sv. So
6949 sv does not have to be ROK. */
6952 S_curse(pTHX_ SV * const sv, const bool check_refcnt) {
6953 PERL_ARGS_ASSERT_CURSE;
6954 assert(SvOBJECT(sv));
6956 if (PL_defstash && /* Still have a symbol table? */
6962 stash = SvSTASH(sv);
6963 assert(SvTYPE(stash) == SVt_PVHV);
6964 if (HvNAME(stash)) {
6965 CV* destructor = NULL;
6966 struct mro_meta *meta;
6968 assert (HvHasAUX(stash));
6970 DEBUG_o( Perl_deb(aTHX_ "Looking for DESTROY method for %s\n",
6973 /* don't make this an initialization above the assert, since it needs
6975 meta = HvMROMETA(stash);
6976 if (meta->destroy_gen && meta->destroy_gen == PL_sub_generation) {
6977 destructor = meta->destroy;
6978 DEBUG_o( Perl_deb(aTHX_ "Using cached DESTROY method %p for %s\n",
6979 (void *)destructor, HvNAME(stash)) );
6982 bool autoload = FALSE;
6984 gv_fetchmeth_pvn(stash, S_destroy, S_destroy_len, -1, 0);
6986 destructor = GvCV(gv);
6988 gv = gv_autoload_pvn(stash, S_destroy, S_destroy_len,
6989 GV_AUTOLOAD_ISMETHOD);
6991 destructor = GvCV(gv);
6995 /* we don't cache AUTOLOAD for DESTROY, since this code
6996 would then need to set $__PACKAGE__::AUTOLOAD, or the
6997 equivalent for XS AUTOLOADs */
6999 meta->destroy_gen = PL_sub_generation;
7000 meta->destroy = destructor;
7002 DEBUG_o( Perl_deb(aTHX_ "Set cached DESTROY method %p for %s\n",
7003 (void *)destructor, HvNAME(stash)) );
7006 DEBUG_o( Perl_deb(aTHX_ "Not caching AUTOLOAD for DESTROY method for %s\n",
7010 assert(!destructor || SvTYPE(destructor) == SVt_PVCV);
7012 /* A constant subroutine can have no side effects, so
7013 don't bother calling it. */
7014 && !CvCONST(destructor)
7015 /* Don't bother calling an empty destructor or one that
7016 returns immediately. */
7017 && (CvISXSUB(destructor)
7018 || (CvSTART(destructor)
7019 && (CvSTART(destructor)->op_next->op_type
7021 && (CvSTART(destructor)->op_next->op_type
7023 || CvSTART(destructor)->op_next->op_next->op_type
7029 SV* const tmpref = newRV(sv);
7030 SvREADONLY_on(tmpref); /* DESTROY() could be naughty */
7032 PUSHSTACKi(PERLSI_DESTROY);
7037 call_sv(MUTABLE_SV(destructor),
7038 G_DISCARD|G_EVAL|G_KEEPERR|G_VOID);
7042 if(SvREFCNT(tmpref) < 2) {
7043 /* tmpref is not kept alive! */
7045 SvRV_set(tmpref, NULL);
7048 SvREFCNT_dec_NN(tmpref);
7051 } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
7054 if (check_refcnt && SvREFCNT(sv)) {
7055 if (PL_in_clean_objs)
7057 "DESTROY created new reference to dead object '%" HEKf "'",
7058 HEKfARG(HvNAME_HEK(stash)));
7059 /* DESTROY gave object new lease on life */
7065 HV * const stash = SvSTASH(sv);
7066 /* Curse before freeing the stash, as freeing the stash could cause
7067 a recursive call into S_curse. */
7068 SvOBJECT_off(sv); /* Curse the object. */
7069 SvSTASH_set(sv,0); /* SvREFCNT_dec may try to read this */
7070 SvREFCNT_dec(stash); /* possibly of changed persuasion */
7076 =for apidoc sv_newref
7078 Increment an SV's reference count. Use the C<SvREFCNT_inc()> wrapper
7085 Perl_sv_newref(pTHX_ SV *const sv)
7087 PERL_UNUSED_CONTEXT;
7096 Decrement an SV's reference count, and if it drops to zero, call
7097 C<sv_clear> to invoke destructors and free up any memory used by
7098 the body; finally, deallocating the SV's head itself.
7099 Normally called via a wrapper macro C<SvREFCNT_dec>.
7105 Perl_sv_free(pTHX_ SV *const sv)
7111 /* Private helper function for SvREFCNT_dec().
7112 * Called with rc set to original SvREFCNT(sv), where rc == 0 or 1 */
7115 Perl_sv_free2(pTHX_ SV *const sv, const U32 rc)
7118 PERL_ARGS_ASSERT_SV_FREE2;
7120 if (LIKELY( rc == 1 )) {
7126 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEBUGGING),
7127 "Attempt to free temp prematurely: SV 0x%" UVxf
7128 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
7132 if (SvIMMORTAL(sv)) {
7133 /* make sure SvREFCNT(sv)==0 happens very seldom */
7134 SvREFCNT(sv) = SvREFCNT_IMMORTAL;
7138 if (! SvREFCNT(sv)) /* may have have been resurrected */
7143 /* handle exceptional cases */
7147 if (SvFLAGS(sv) & SVf_BREAK)
7148 /* this SV's refcnt has been artificially decremented to
7149 * trigger cleanup */
7151 if (PL_in_clean_all) /* All is fair */
7153 if (SvIMMORTAL(sv)) {
7154 /* make sure SvREFCNT(sv)==0 happens very seldom */
7155 SvREFCNT(sv) = SvREFCNT_IMMORTAL;
7158 if (ckWARN_d(WARN_INTERNAL)) {
7159 #ifdef DEBUG_LEAKING_SCALARS_FORK_DUMP
7160 Perl_dump_sv_child(aTHX_ sv);
7162 #ifdef DEBUG_LEAKING_SCALARS
7165 #ifdef DEBUG_LEAKING_SCALARS_ABORT
7166 if (PL_warnhook == PERL_WARNHOOK_FATAL
7167 || ckDEAD(packWARN(WARN_INTERNAL))) {
7168 /* Don't let Perl_warner cause us to escape our fate: */
7172 /* This may not return: */
7173 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
7174 "Attempt to free unreferenced scalar: SV 0x%" UVxf
7175 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
7178 #ifdef DEBUG_LEAKING_SCALARS_ABORT
7188 Returns the length of the string in the SV. Handles magic and type
7189 coercion and sets the UTF8 flag appropriately. See also C<L</SvCUR>>, which
7190 gives raw access to the C<xpv_cur> slot.
7196 Perl_sv_len(pTHX_ SV *const sv)
7203 (void)SvPV_const(sv, len);
7208 =for apidoc sv_len_utf8
7209 =for apidoc_item sv_len_utf8_nomg
7211 These return the number of characters in the string in an SV, counting wide
7212 UTF-8 bytes as a single character. Both handle type coercion.
7213 They differ only in that C<sv_len_utf8> performs 'get' magic;
7214 C<sv_len_utf8_nomg> skips any magic.
7220 * The length is cached in PERL_MAGIC_utf8, in the mg_len field. Also the
7221 * mg_ptr is used, by sv_pos_u2b() and sv_pos_b2u() - see the comments below.
7222 * (Note that the mg_len is not the length of the mg_ptr field.
7223 * This allows the cache to store the character length of the string without
7224 * needing to malloc() extra storage to attach to the mg_ptr.)
7229 Perl_sv_len_utf8(pTHX_ SV *const sv)
7235 return sv_len_utf8_nomg(sv);
7239 Perl_sv_len_utf8_nomg(pTHX_ SV * const sv)
7242 const U8 *s = (U8*)SvPV_nomg_const(sv, len);
7244 PERL_ARGS_ASSERT_SV_LEN_UTF8_NOMG;
7246 if (PL_utf8cache && SvUTF8(sv)) {
7248 MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : NULL;
7250 if (mg && (mg->mg_len != -1 || mg->mg_ptr)) {
7251 if (mg->mg_len != -1)
7254 /* We can use the offset cache for a headstart.
7255 The longer value is stored in the first pair. */
7256 STRLEN *cache = (STRLEN *) mg->mg_ptr;
7258 ulen = cache[0] + Perl_utf8_length(aTHX_ s + cache[1],
7262 if (PL_utf8cache < 0) {
7263 const STRLEN real = Perl_utf8_length(aTHX_ s, s + len);
7264 assert_uft8_cache_coherent("sv_len_utf8", ulen, real, sv);
7268 ulen = Perl_utf8_length(aTHX_ s, s + len);
7269 utf8_mg_len_cache_update(sv, &mg, ulen);
7273 return SvUTF8(sv) ? Perl_utf8_length(aTHX_ s, s + len) : len;
7276 /* Walk forwards to find the byte corresponding to the passed in UTF-8
7279 S_sv_pos_u2b_forwards(const U8 *const start, const U8 *const send,
7280 STRLEN *const uoffset_p, bool *const at_end,
7281 bool* canonical_position)
7283 const U8 *s = start;
7284 STRLEN uoffset = *uoffset_p;
7286 PERL_ARGS_ASSERT_SV_POS_U2B_FORWARDS;
7288 while (s < send && uoffset) {
7295 else if (s > send) {
7297 /* This is the existing behaviour. Possibly it should be a croak, as
7298 it's actually a bounds error */
7301 /* If the unicode position is beyond the end, we return the end but
7302 shouldn't cache that position */
7303 *canonical_position = (uoffset == 0);
7304 *uoffset_p -= uoffset;
7308 /* Given the length of the string in both bytes and UTF-8 characters, decide
7309 whether to walk forwards or backwards to find the byte corresponding to
7310 the passed in UTF-8 offset. */
7312 S_sv_pos_u2b_midway(const U8 *const start, const U8 *send,
7313 STRLEN uoffset, const STRLEN uend)
7315 STRLEN backw = uend - uoffset;
7317 PERL_ARGS_ASSERT_SV_POS_U2B_MIDWAY;
7319 if (uoffset < 2 * backw) {
7320 /* The assumption is that going forwards is twice the speed of going
7321 forward (that's where the 2 * backw comes from).
7322 (The real figure of course depends on the UTF-8 data.) */
7323 const U8 *s = start;
7325 while (s < send && uoffset--)
7335 while (UTF8_IS_CONTINUATION(*send))
7338 return send - start;
7341 /* For the string representation of the given scalar, find the byte
7342 corresponding to the passed in UTF-8 offset. uoffset0 and boffset0
7343 give another position in the string, *before* the sought offset, which
7344 (which is always true, as 0, 0 is a valid pair of positions), which should
7345 help reduce the amount of linear searching.
7346 If *mgp is non-NULL, it should point to the UTF-8 cache magic, which
7347 will be used to reduce the amount of linear searching. The cache will be
7348 created if necessary, and the found value offered to it for update. */
7350 S_sv_pos_u2b_cached(pTHX_ SV *const sv, MAGIC **const mgp, const U8 *const start,
7351 const U8 *const send, STRLEN uoffset,
7352 STRLEN uoffset0, STRLEN boffset0)
7354 STRLEN boffset = 0; /* Actually always set, but let's keep gcc happy. */
7356 bool at_end = FALSE;
7357 bool canonical_position = FALSE;
7359 PERL_ARGS_ASSERT_SV_POS_U2B_CACHED;
7361 assert (uoffset >= uoffset0);
7366 if (!SvREADONLY(sv) && !SvGMAGICAL(sv) && SvPOK(sv)
7368 && (*mgp || (SvTYPE(sv) >= SVt_PVMG &&
7369 (*mgp = mg_find(sv, PERL_MAGIC_utf8))))) {
7370 if ((*mgp)->mg_ptr) {
7371 STRLEN *cache = (STRLEN *) (*mgp)->mg_ptr;
7372 if (cache[0] == uoffset) {
7373 /* An exact match. */
7376 if (cache[2] == uoffset) {
7377 /* An exact match. */
7381 if (cache[0] < uoffset) {
7382 /* The cache already knows part of the way. */
7383 if (cache[0] > uoffset0) {
7384 /* The cache knows more than the passed in pair */
7385 uoffset0 = cache[0];
7386 boffset0 = cache[1];
7388 if ((*mgp)->mg_len != -1) {
7389 /* And we know the end too. */
7391 + sv_pos_u2b_midway(start + boffset0, send,
7393 (*mgp)->mg_len - uoffset0);
7395 uoffset -= uoffset0;
7397 + sv_pos_u2b_forwards(start + boffset0,
7398 send, &uoffset, &at_end,
7399 &canonical_position);
7400 uoffset += uoffset0;
7403 else if (cache[2] < uoffset) {
7404 /* We're between the two cache entries. */
7405 if (cache[2] > uoffset0) {
7406 /* and the cache knows more than the passed in pair */
7407 uoffset0 = cache[2];
7408 boffset0 = cache[3];
7412 + sv_pos_u2b_midway(start + boffset0,
7415 cache[0] - uoffset0);
7418 + sv_pos_u2b_midway(start + boffset0,
7421 cache[2] - uoffset0);
7425 else if ((*mgp)->mg_len != -1) {
7426 /* If we can take advantage of a passed in offset, do so. */
7427 /* In fact, offset0 is either 0, or less than offset, so don't
7428 need to worry about the other possibility. */
7430 + sv_pos_u2b_midway(start + boffset0, send,
7432 (*mgp)->mg_len - uoffset0);
7437 if (!found || PL_utf8cache < 0) {
7438 STRLEN real_boffset;
7439 uoffset -= uoffset0;
7440 real_boffset = boffset0 + sv_pos_u2b_forwards(start + boffset0,
7441 send, &uoffset, &at_end,
7442 &canonical_position);
7443 uoffset += uoffset0;
7445 if (found && PL_utf8cache < 0)
7446 assert_uft8_cache_coherent("sv_pos_u2b_cache", boffset,
7448 boffset = real_boffset;
7451 if (PL_utf8cache && canonical_position && !SvGMAGICAL(sv) && SvPOK(sv)) {
7453 utf8_mg_len_cache_update(sv, mgp, uoffset);
7455 utf8_mg_pos_cache_update(sv, mgp, boffset, uoffset, send - start);
7462 =for apidoc sv_pos_u2b_flags
7464 Converts the offset from a count of UTF-8 chars from
7465 the start of the string, to a count of the equivalent number of bytes; if
7466 C<lenp> is non-zero, it does the same to C<lenp>, but this time starting from
7467 C<offset>, rather than from the start
7468 of the string. Handles type coercion.
7469 C<flags> is passed to C<SvPV_flags>, and usually should be
7470 C<SV_GMAGIC|SV_CONST_RETURN> to handle magic.
7476 * sv_pos_u2b_flags() uses, like sv_pos_b2u(), the mg_ptr of the potential
7477 * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
7478 * byte offsets. See also the comments of S_utf8_mg_pos_cache_update().
7483 Perl_sv_pos_u2b_flags(pTHX_ SV *const sv, STRLEN uoffset, STRLEN *const lenp,
7490 PERL_ARGS_ASSERT_SV_POS_U2B_FLAGS;
7492 start = (U8*)SvPV_flags(sv, len, flags);
7494 const U8 * const send = start + len;
7496 boffset = sv_pos_u2b_cached(sv, &mg, start, send, uoffset, 0, 0);
7499 && *lenp /* don't bother doing work for 0, as its bytes equivalent
7500 is 0, and *lenp is already set to that. */) {
7501 /* Convert the relative offset to absolute. */
7502 const STRLEN uoffset2 = uoffset + *lenp;
7503 const STRLEN boffset2
7504 = sv_pos_u2b_cached(sv, &mg, start, send, uoffset2,
7505 uoffset, boffset) - boffset;
7519 =for apidoc sv_pos_u2b
7521 Converts the value pointed to by C<offsetp> from a count of UTF-8 chars from
7522 the start of the string, to a count of the equivalent number of bytes; if
7523 C<lenp> is non-zero, it does the same to C<lenp>, but this time starting from
7524 the offset, rather than from the start of the string. Handles magic and
7527 Use C<sv_pos_u2b_flags> in preference, which correctly handles strings longer
7534 * sv_pos_u2b() uses, like sv_pos_b2u(), the mg_ptr of the potential
7535 * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
7536 * byte offsets. See also the comments of S_utf8_mg_pos_cache_update().
7540 /* This function is subject to size and sign problems */
7543 Perl_sv_pos_u2b(pTHX_ SV *const sv, I32 *const offsetp, I32 *const lenp)
7545 PERL_ARGS_ASSERT_SV_POS_U2B;
7548 STRLEN ulen = (STRLEN)*lenp;
7549 *offsetp = (I32)sv_pos_u2b_flags(sv, (STRLEN)*offsetp, &ulen,
7550 SV_GMAGIC|SV_CONST_RETURN);
7553 *offsetp = (I32)sv_pos_u2b_flags(sv, (STRLEN)*offsetp, NULL,
7554 SV_GMAGIC|SV_CONST_RETURN);
7559 S_utf8_mg_len_cache_update(pTHX_ SV *const sv, MAGIC **const mgp,
7562 PERL_ARGS_ASSERT_UTF8_MG_LEN_CACHE_UPDATE;
7563 if (SvREADONLY(sv) || SvGMAGICAL(sv) || !SvPOK(sv))
7566 if (!*mgp && (SvTYPE(sv) < SVt_PVMG ||
7567 !(*mgp = mg_find(sv, PERL_MAGIC_utf8)))) {
7568 *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, &PL_vtbl_utf8, 0, 0);
7572 (*mgp)->mg_len = ulen;
7575 /* Create and update the UTF8 magic offset cache, with the proffered utf8/
7576 byte length pairing. The (byte) length of the total SV is passed in too,
7577 as blen, because for some (more esoteric) SVs, the call to SvPV_const()
7578 may not have updated SvCUR, so we can't rely on reading it directly.
7580 The proffered utf8/byte length pairing isn't used if the cache already has
7581 two pairs, and swapping either for the proffered pair would increase the
7582 RMS of the intervals between known byte offsets.
7584 The cache itself consists of 4 STRLEN values
7585 0: larger UTF-8 offset
7586 1: corresponding byte offset
7587 2: smaller UTF-8 offset
7588 3: corresponding byte offset
7590 Unused cache pairs have the value 0, 0.
7591 Keeping the cache "backwards" means that the invariant of
7592 cache[0] >= cache[2] is maintained even with empty slots, which means that
7593 the code that uses it doesn't need to worry if only 1 entry has actually
7594 been set to non-zero. It also makes the "position beyond the end of the
7595 cache" logic much simpler, as the first slot is always the one to start
7599 S_utf8_mg_pos_cache_update(pTHX_ SV *const sv, MAGIC **const mgp, const STRLEN byte,
7600 const STRLEN utf8, const STRLEN blen)
7604 PERL_ARGS_ASSERT_UTF8_MG_POS_CACHE_UPDATE;
7609 if (!*mgp && (SvTYPE(sv) < SVt_PVMG ||
7610 !(*mgp = mg_find(sv, PERL_MAGIC_utf8)))) {
7611 *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, (MGVTBL*)&PL_vtbl_utf8, 0,
7613 (*mgp)->mg_len = -1;
7617 if (!(cache = (STRLEN *)(*mgp)->mg_ptr)) {
7618 Newxz(cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
7619 (*mgp)->mg_ptr = (char *) cache;
7623 if (PL_utf8cache < 0 && SvPOKp(sv)) {
7624 /* SvPOKp() because, if sv is a reference, then SvPVX() is actually
7625 a pointer. Note that we no longer cache utf8 offsets on refer-
7626 ences, but this check is still a good idea, for robustness. */
7627 const U8 *start = (const U8 *) SvPVX_const(sv);
7628 const STRLEN realutf8 = utf8_length(start, start + byte);
7630 assert_uft8_cache_coherent("utf8_mg_pos_cache_update", utf8, realutf8,
7634 /* Cache is held with the later position first, to simplify the code
7635 that deals with unbounded ends. */
7637 ASSERT_UTF8_CACHE(cache);
7638 if (cache[1] == 0) {
7639 /* Cache is totally empty */
7642 } else if (cache[3] == 0) {
7643 if (byte > cache[1]) {
7644 /* New one is larger, so goes first. */
7645 cache[2] = cache[0];
7646 cache[3] = cache[1];
7654 /* float casts necessary? XXX */
7655 #define THREEWAY_SQUARE(a,b,c,d) \
7656 ((float)((d) - (c))) * ((float)((d) - (c))) \
7657 + ((float)((c) - (b))) * ((float)((c) - (b))) \
7658 + ((float)((b) - (a))) * ((float)((b) - (a)))
7660 /* Cache has 2 slots in use, and we know three potential pairs.
7661 Keep the two that give the lowest RMS distance. Do the
7662 calculation in bytes simply because we always know the byte
7663 length. squareroot has the same ordering as the positive value,
7664 so don't bother with the actual square root. */
7665 if (byte > cache[1]) {
7666 /* New position is after the existing pair of pairs. */
7667 const float keep_earlier
7668 = THREEWAY_SQUARE(0, cache[3], byte, blen);
7669 const float keep_later
7670 = THREEWAY_SQUARE(0, cache[1], byte, blen);
7672 if (keep_later < keep_earlier) {
7673 cache[2] = cache[0];
7674 cache[3] = cache[1];
7680 const float keep_later = THREEWAY_SQUARE(0, byte, cache[1], blen);
7681 float b, c, keep_earlier;
7682 if (byte > cache[3]) {
7683 /* New position is between the existing pair of pairs. */
7684 b = (float)cache[3];
7687 /* New position is before the existing pair of pairs. */
7689 c = (float)cache[3];
7691 keep_earlier = THREEWAY_SQUARE(0, b, c, blen);
7692 if (byte > cache[3]) {
7693 if (keep_later < keep_earlier) {
7703 if (! (keep_later < keep_earlier)) {
7704 cache[0] = cache[2];
7705 cache[1] = cache[3];
7712 ASSERT_UTF8_CACHE(cache);
7715 /* We already know all of the way, now we may be able to walk back. The same
7716 assumption is made as in S_sv_pos_u2b_midway(), namely that walking
7717 backward is half the speed of walking forward. */
7719 S_sv_pos_b2u_midway(pTHX_ const U8 *const s, const U8 *const target,
7720 const U8 *end, STRLEN endu)
7722 const STRLEN forw = target - s;
7723 STRLEN backw = end - target;
7725 PERL_ARGS_ASSERT_SV_POS_B2U_MIDWAY;
7727 if (forw < 2 * backw) {
7728 return utf8_length(s, target);
7731 while (end > target) {
7733 while (UTF8_IS_CONTINUATION(*end)) {
7742 =for apidoc sv_pos_b2u_flags
7744 Converts C<offset> from a count of bytes from the start of the string, to
7745 a count of the equivalent number of UTF-8 chars. Handles type coercion.
7746 C<flags> is passed to C<SvPV_flags>, and usually should be
7747 C<SV_GMAGIC|SV_CONST_RETURN> to handle magic.
7753 * sv_pos_b2u_flags() uses, like sv_pos_u2b_flags(), the mg_ptr of the
7754 * potential PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8
7759 Perl_sv_pos_b2u_flags(pTHX_ SV *const sv, STRLEN const offset, U32 flags)
7762 STRLEN len = 0; /* Actually always set, but let's keep gcc happy. */
7768 PERL_ARGS_ASSERT_SV_POS_B2U_FLAGS;
7770 s = (const U8*)SvPV_flags(sv, blen, flags);
7773 Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset, blen=%" UVuf
7774 ", byte=%" UVuf, (UV)blen, (UV)offset);
7780 && SvTYPE(sv) >= SVt_PVMG
7781 && (mg = mg_find(sv, PERL_MAGIC_utf8)))
7784 STRLEN * const cache = (STRLEN *) mg->mg_ptr;
7785 if (cache[1] == offset) {
7786 /* An exact match. */
7789 if (cache[3] == offset) {
7790 /* An exact match. */
7794 if (cache[1] < offset) {
7795 /* We already know part of the way. */
7796 if (mg->mg_len != -1) {
7797 /* Actually, we know the end too. */
7799 + S_sv_pos_b2u_midway(aTHX_ s + cache[1], send,
7800 s + blen, mg->mg_len - cache[0]);
7802 len = cache[0] + utf8_length(s + cache[1], send);
7805 else if (cache[3] < offset) {
7806 /* We're between the two cached pairs, so we do the calculation
7807 offset by the byte/utf-8 positions for the earlier pair,
7808 then add the utf-8 characters from the string start to
7810 len = S_sv_pos_b2u_midway(aTHX_ s + cache[3], send,
7811 s + cache[1], cache[0] - cache[2])
7815 else { /* cache[3] > offset */
7816 len = S_sv_pos_b2u_midway(aTHX_ s, send, s + cache[3],
7820 ASSERT_UTF8_CACHE(cache);
7822 } else if (mg->mg_len != -1) {
7823 len = S_sv_pos_b2u_midway(aTHX_ s, send, s + blen, mg->mg_len);
7827 if (!found || PL_utf8cache < 0) {
7828 const STRLEN real_len = utf8_length(s, send);
7830 if (found && PL_utf8cache < 0)
7831 assert_uft8_cache_coherent("sv_pos_b2u", len, real_len, sv);
7837 utf8_mg_len_cache_update(sv, &mg, len);
7839 utf8_mg_pos_cache_update(sv, &mg, offset, len, blen);
7846 =for apidoc sv_pos_b2u
7848 Converts the value pointed to by C<offsetp> from a count of bytes from the
7849 start of the string, to a count of the equivalent number of UTF-8 chars.
7850 Handles magic and type coercion.
7852 Use C<sv_pos_b2u_flags> in preference, which correctly handles strings
7859 * sv_pos_b2u() uses, like sv_pos_u2b(), the mg_ptr of the potential
7860 * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
7865 Perl_sv_pos_b2u(pTHX_ SV *const sv, I32 *const offsetp)
7867 PERL_ARGS_ASSERT_SV_POS_B2U;
7872 *offsetp = (I32)sv_pos_b2u_flags(sv, (STRLEN)*offsetp,
7873 SV_GMAGIC|SV_CONST_RETURN);
7877 S_assert_uft8_cache_coherent(pTHX_ const char *const func, STRLEN from_cache,
7878 STRLEN real, SV *const sv)
7880 PERL_ARGS_ASSERT_ASSERT_UFT8_CACHE_COHERENT;
7882 /* As this is debugging only code, save space by keeping this test here,
7883 rather than inlining it in all the callers. */
7884 if (from_cache == real)
7887 /* Need to turn the assertions off otherwise we may recurse infinitely
7888 while printing error messages. */
7889 SAVEI8(PL_utf8cache);
7891 Perl_croak(aTHX_ "panic: %s cache %" UVuf " real %" UVuf " for %" SVf,
7892 func, (UV) from_cache, (UV) real, SVfARG(sv));
7898 Returns a boolean indicating whether the strings in the two SVs are
7899 identical. Is UTF-8 and S<C<'use bytes'>> aware, handles get magic, and will
7900 coerce its args to strings if necessary.
7902 This function does not handle operator overloading. For a version that does,
7903 see instead C<sv_streq>.
7905 =for apidoc sv_eq_flags
7907 Returns a boolean indicating whether the strings in the two SVs are
7908 identical. Is UTF-8 and S<C<'use bytes'>> aware and coerces its args to strings
7909 if necessary. If the flags has the C<SV_GMAGIC> bit set, it handles get-magic, too.
7911 This function does not handle operator overloading. For a version that does,
7912 see instead C<sv_streq_flags>.
7918 Perl_sv_eq_flags(pTHX_ SV *sv1, SV *sv2, const U32 flags)
7930 /* if pv1 and pv2 are the same, second SvPV_const call may
7931 * invalidate pv1 (if we are handling magic), so we may need to
7933 if (sv1 == sv2 && flags & SV_GMAGIC
7934 && (SvTHINKFIRST(sv1) || SvGMAGICAL(sv1))) {
7935 pv1 = SvPV_const(sv1, cur1);
7936 sv1 = newSVpvn_flags(pv1, cur1, SVs_TEMP | SvUTF8(sv2));
7938 pv1 = SvPV_flags_const(sv1, cur1, flags);
7946 pv2 = SvPV_flags_const(sv2, cur2, flags);
7948 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
7949 /* Differing utf8ness. */
7951 /* sv1 is the UTF-8 one */
7952 return bytes_cmp_utf8((const U8*)pv2, cur2,
7953 (const U8*)pv1, cur1) == 0;
7956 /* sv2 is the UTF-8 one */
7957 return bytes_cmp_utf8((const U8*)pv1, cur1,
7958 (const U8*)pv2, cur2) == 0;
7963 return (pv1 == pv2) || memEQ(pv1, pv2, cur1);
7969 =for apidoc sv_streq_flags
7971 Returns a boolean indicating whether the strings in the two SVs are
7972 identical. If the flags argument has the C<SV_GMAGIC> bit set, it handles
7973 get-magic too. Will coerce its args to strings if necessary. Treats
7974 C<NULL> as undef. Correctly handles the UTF8 flag.
7976 If flags does not have the C<SV_SKIP_OVERLOAD> bit set, an attempt to use
7977 C<eq> overloading will be made. If such overloading does not exist or the
7978 flag is set, then regular string comparison will be used instead.
7980 =for apidoc sv_streq
7982 A convenient shortcut for calling C<sv_streq_flags> with the C<SV_GMAGIC>
7983 flag. This function basically behaves like the Perl code C<$sv1 eq $sv2>.
7989 Perl_sv_streq_flags(pTHX_ SV *sv1, SV *sv2, const U32 flags)
7991 PERL_ARGS_ASSERT_SV_STREQ_FLAGS;
7993 if(flags & SV_GMAGIC) {
8000 /* Treat NULL as undef */
8006 if(!(flags & SV_SKIP_OVERLOAD) &&
8007 (SvAMAGIC(sv1) || SvAMAGIC(sv2))) {
8008 SV *ret = amagic_call(sv1, sv2, seq_amg, 0);
8013 return sv_eq_flags(sv1, sv2, 0);
8017 =for apidoc sv_numeq_flags
8019 Returns a boolean indicating whether the numbers in the two SVs are
8020 identical. If the flags argument has the C<SV_GMAGIC> bit set, it handles
8021 get-magic too. Will coerce its args to numbers if necessary. Treats
8024 If flags does not have the C<SV_SKIP_OVERLOAD> bit set, an attempt to use
8025 C<==> overloading will be made. If such overloading does not exist or the
8026 flag is set, then regular numerical comparison will be used instead.
8028 =for apidoc sv_numeq
8030 A convenient shortcut for calling C<sv_numeq_flags> with the C<SV_GMAGIC>
8031 flag. This function basically behaves like the Perl code C<$sv1 == $sv2>.
8037 Perl_sv_numeq_flags(pTHX_ SV *sv1, SV *sv2, const U32 flags)
8039 PERL_ARGS_ASSERT_SV_NUMEQ_FLAGS;
8041 if(flags & SV_GMAGIC) {
8048 /* Treat NULL as undef */
8054 if(!(flags & SV_SKIP_OVERLOAD) &&
8055 (SvAMAGIC(sv1) || SvAMAGIC(sv2))) {
8056 SV *ret = amagic_call(sv1, sv2, eq_amg, 0);
8061 return do_ncmp(sv1, sv2) == 0;
8067 Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
8068 string in C<sv1> is less than, equal to, or greater than the string in
8069 C<sv2>. Is UTF-8 and S<C<'use bytes'>> aware, handles get magic, and will
8070 coerce its args to strings if necessary. See also C<L</sv_cmp_locale>>.
8072 =for apidoc sv_cmp_flags
8074 Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
8075 string in C<sv1> is less than, equal to, or greater than the string in
8076 C<sv2>. Is UTF-8 and S<C<'use bytes'>> aware and will coerce its args to strings
8077 if necessary. If the flags has the C<SV_GMAGIC> bit set, it handles get magic. See
8078 also C<L</sv_cmp_locale_flags>>.
8084 Perl_sv_cmp(pTHX_ SV *const sv1, SV *const sv2)
8086 return sv_cmp_flags(sv1, sv2, SV_GMAGIC);
8090 Perl_sv_cmp_flags(pTHX_ SV *const sv1, SV *const sv2,
8094 const char *pv1, *pv2;
8096 SV *svrecode = NULL;
8103 pv1 = SvPV_flags_const(sv1, cur1, flags);
8110 pv2 = SvPV_flags_const(sv2, cur2, flags);
8112 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
8113 /* Differing utf8ness. */
8115 const int retval = -bytes_cmp_utf8((const U8*)pv2, cur2,
8116 (const U8*)pv1, cur1);
8117 return retval ? retval < 0 ? -1 : +1 : 0;
8120 const int retval = bytes_cmp_utf8((const U8*)pv1, cur1,
8121 (const U8*)pv2, cur2);
8122 return retval ? retval < 0 ? -1 : +1 : 0;
8126 /* Here, if both are non-NULL, then they have the same UTF8ness. */
8129 cmp = cur2 ? -1 : 0;
8133 STRLEN shortest_len = cur1 < cur2 ? cur1 : cur2;
8136 if (! DO_UTF8(sv1)) {
8138 const I32 retval = memcmp((const void*)pv1,
8142 cmp = retval < 0 ? -1 : 1;
8143 } else if (cur1 == cur2) {
8146 cmp = cur1 < cur2 ? -1 : 1;
8150 else { /* Both are to be treated as UTF-EBCDIC */
8152 /* EBCDIC UTF-8 is complicated by the fact that it is based on I8
8153 * which remaps code points 0-255. We therefore generally have to
8154 * unmap back to the original values to get an accurate comparison.
8155 * But we don't have to do that for UTF-8 invariants, as by
8156 * definition, they aren't remapped, nor do we have to do it for
8157 * above-latin1 code points, as they also aren't remapped. (This
8158 * code also works on ASCII platforms, but the memcmp() above is
8161 const char *e = pv1 + shortest_len;
8163 /* Find the first bytes that differ between the two strings */
8164 while (pv1 < e && *pv1 == *pv2) {
8170 if (pv1 == e) { /* Are the same all the way to the end */
8174 cmp = cur1 < cur2 ? -1 : 1;
8177 else /* Here *pv1 and *pv2 are not equal, but all bytes earlier
8178 * in the strings were. The current bytes may or may not be
8179 * at the beginning of a character. But neither or both are
8180 * (or else earlier bytes would have been different). And
8181 * if we are in the middle of a character, the two
8182 * characters are comprised of the same number of bytes
8183 * (because in this case the start bytes are the same, and
8184 * the start bytes encode the character's length). */
8185 if (UTF8_IS_INVARIANT(*pv1))
8187 /* If both are invariants; can just compare directly */
8188 if (UTF8_IS_INVARIANT(*pv2)) {
8189 cmp = ((U8) *pv1 < (U8) *pv2) ? -1 : 1;
8191 else /* Since *pv1 is invariant, it is the whole character,
8192 which means it is at the beginning of a character.
8193 That means pv2 is also at the beginning of a
8194 character (see earlier comment). Since it isn't
8195 invariant, it must be a start byte. If it starts a
8196 character whose code point is above 255, that
8197 character is greater than any single-byte char, which
8199 if (UTF8_IS_ABOVE_LATIN1_START(*pv2))
8204 /* Here, pv2 points to a character composed of 2 bytes
8205 * whose code point is < 256. Get its code point and
8206 * compare with *pv1 */
8207 cmp = ((U8) *pv1 < EIGHT_BIT_UTF8_TO_NATIVE(*pv2, *(pv2 + 1)))
8212 else /* The code point starting at pv1 isn't a single byte */
8213 if (UTF8_IS_INVARIANT(*pv2))
8215 /* But here, the code point starting at *pv2 is a single byte,
8216 * and so *pv1 must begin a character, hence is a start byte.
8217 * If that character is above 255, it is larger than any
8218 * single-byte char, which *pv2 is */
8219 if (UTF8_IS_ABOVE_LATIN1_START(*pv1)) {
8223 /* Here, pv1 points to a character composed of 2 bytes
8224 * whose code point is < 256. Get its code point and
8225 * compare with the single byte character *pv2 */
8226 cmp = (EIGHT_BIT_UTF8_TO_NATIVE(*pv1, *(pv1 + 1)) < (U8) *pv2)
8231 else /* Here, we've ruled out either *pv1 and *pv2 being
8232 invariant. That means both are part of variants, but not
8233 necessarily at the start of a character */
8234 if ( UTF8_IS_ABOVE_LATIN1_START(*pv1)
8235 || UTF8_IS_ABOVE_LATIN1_START(*pv2))
8237 /* Here, at least one is the start of a character, which means
8238 * the other is also a start byte. And the code point of at
8239 * least one of the characters is above 255. It is a
8240 * characteristic of UTF-EBCDIC that all start bytes for
8241 * above-latin1 code points are well behaved as far as code
8242 * point comparisons go, and all are larger than all other
8243 * start bytes, so the comparison with those is also well
8245 cmp = ((U8) *pv1 < (U8) *pv2) ? -1 : 1;
8248 /* Here both *pv1 and *pv2 are part of variant characters.
8249 * They could be both continuations, or both start characters.
8250 * (One or both could even be an illegal start character (for
8251 * an overlong) which for the purposes of sorting we treat as
8253 if (UTF8_IS_CONTINUATION(*pv1)) {
8255 /* If they are continuations for code points above 255,
8256 * then comparing the current byte is sufficient, as there
8257 * is no remapping of these and so the comparison is
8258 * well-behaved. We determine if they are such
8259 * continuations by looking at the preceding byte. It
8260 * could be a start byte, from which we can tell if it is
8261 * for an above 255 code point. Or it could be a
8262 * continuation, which means the character occupies at
8263 * least 3 bytes, so must be above 255. */
8264 if ( UTF8_IS_CONTINUATION(*(pv2 - 1))
8265 || UTF8_IS_ABOVE_LATIN1_START(*(pv2 -1)))
8267 cmp = ((U8) *pv1 < (U8) *pv2) ? -1 : 1;
8271 /* Here, the continuations are for code points below 256;
8272 * back up one to get to the start byte */
8277 /* We need to get the actual native code point of each of these
8278 * variants in order to compare them */
8279 cmp = ( EIGHT_BIT_UTF8_TO_NATIVE(*pv1, *(pv1 + 1))
8280 < EIGHT_BIT_UTF8_TO_NATIVE(*pv2, *(pv2 + 1)))
8289 SvREFCNT_dec(svrecode);
8295 =for apidoc sv_cmp_locale
8297 Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and
8298 S<C<'use bytes'>> aware, handles get magic, and will coerce its args to strings
8299 if necessary. See also C<L</sv_cmp>>.
8301 =for apidoc sv_cmp_locale_flags
8303 Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and
8304 S<C<'use bytes'>> aware and will coerce its args to strings if necessary. If
8305 the flags contain C<SV_GMAGIC>, it handles get magic. See also
8306 C<L</sv_cmp_flags>>.
8312 Perl_sv_cmp_locale(pTHX_ SV *const sv1, SV *const sv2)
8314 return sv_cmp_locale_flags(sv1, sv2, SV_GMAGIC);
8318 Perl_sv_cmp_locale_flags(pTHX_ SV *const sv1, SV *const sv2,
8321 #ifdef USE_LOCALE_COLLATE
8327 if (PL_collation_standard)
8332 /* Revert to using raw compare if both operands exist, but either one
8333 * doesn't transform properly for collation */
8335 pv1 = sv_collxfrm_flags(sv1, &len1, flags);
8339 pv2 = sv_collxfrm_flags(sv2, &len2, flags);
8345 pv1 = sv1 ? sv_collxfrm_flags(sv1, &len1, flags) : (char *) NULL;
8346 pv2 = sv2 ? sv_collxfrm_flags(sv2, &len2, flags) : (char *) NULL;
8349 if (!pv1 || !len1) {
8360 retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
8363 return retval < 0 ? -1 : 1;
8366 * When the result of collation is equality, that doesn't mean
8367 * that there are no differences -- some locales exclude some
8368 * characters from consideration. So to avoid false equalities,
8369 * we use the raw string as a tiebreaker.
8376 PERL_UNUSED_ARG(flags);
8377 #endif /* USE_LOCALE_COLLATE */
8379 return sv_cmp(sv1, sv2);
8383 #ifdef USE_LOCALE_COLLATE
8386 =for apidoc sv_collxfrm
8388 This calls C<sv_collxfrm_flags> with the SV_GMAGIC flag. See
8389 C<L</sv_collxfrm_flags>>.
8391 =for apidoc sv_collxfrm_flags
8393 Add Collate Transform magic to an SV if it doesn't already have it. If the
8394 flags contain C<SV_GMAGIC>, it handles get-magic.
8396 Any scalar variable may carry C<PERL_MAGIC_collxfrm> magic that contains the
8397 scalar data of the variable, but transformed to such a format that a normal
8398 memory comparison can be used to compare the data according to the locale
8405 Perl_sv_collxfrm_flags(pTHX_ SV *const sv, STRLEN *const nxp, const I32 flags)
8409 PERL_ARGS_ASSERT_SV_COLLXFRM_FLAGS;
8411 mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_collxfrm) : (MAGIC *) NULL;
8413 /* If we don't have collation magic on 'sv', or the locale has changed
8414 * since the last time we calculated it, get it and save it now */
8415 if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
8420 /* Free the old space */
8422 Safefree(mg->mg_ptr);
8424 s = SvPV_flags_const(sv, len, flags);
8425 if ((xf = mem_collxfrm_(s, len, &xlen, cBOOL(SvUTF8(sv))))) {
8427 mg = sv_magicext(sv, 0, PERL_MAGIC_collxfrm, &PL_vtbl_collxfrm,
8442 if (mg && mg->mg_ptr) {
8444 return mg->mg_ptr + sizeof(PL_collation_ix);
8452 #endif /* USE_LOCALE_COLLATE */
8455 S_sv_gets_append_to_utf8(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
8457 SV * const tsv = newSV_type(SVt_NULL);
8460 sv_gets(tsv, fp, 0);
8461 sv_utf8_upgrade_nomg(tsv);
8462 SvCUR_set(sv,append);
8465 return (SvCUR(sv) - append) ? SvPVX(sv) : NULL;
8469 S_sv_gets_read_record(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
8472 const STRLEN recsize = SvUV(SvRV(PL_rs)); /* RsRECORD() guarantees > 0. */
8473 /* Grab the size of the record we're getting */
8474 char *buffer = SvGROW(sv, (STRLEN)(recsize + append + 1)) + append;
8481 /* With a true, record-oriented file on VMS, we need to use read directly
8482 * to ensure that we respect RMS record boundaries. The user is responsible
8483 * for providing a PL_rs value that corresponds to the FAB$W_MRS (maximum
8484 * record size) field. N.B. This is likely to produce invalid results on
8485 * varying-width character data when a record ends mid-character.
8487 fd = PerlIO_fileno(fp);
8489 && PerlLIO_fstat(fd, &st) == 0
8490 && (st.st_fab_rfm == FAB$C_VAR
8491 || st.st_fab_rfm == FAB$C_VFC
8492 || st.st_fab_rfm == FAB$C_FIX)) {
8494 bytesread = PerlLIO_read(fd, buffer, recsize);
8496 else /* in-memory file from PerlIO::Scalar
8497 * or not a record-oriented file
8501 bytesread = PerlIO_read(fp, buffer, recsize);
8503 /* At this point, the logic in sv_get() means that sv will
8504 be treated as utf-8 if the handle is utf8.
8506 if (PerlIO_isutf8(fp) && bytesread > 0) {
8507 char *bend = buffer + bytesread;
8508 char *bufp = buffer;
8509 size_t charcount = 0;
8510 bool charstart = TRUE;
8513 while (charcount < recsize) {
8514 /* count accumulated characters */
8515 while (bufp < bend) {
8517 skip = UTF8SKIP(bufp);
8519 if (bufp + skip > bend) {
8520 /* partial at the end */
8531 if (charcount < recsize) {
8533 STRLEN bufp_offset = bufp - buffer;
8534 SSize_t morebytesread;
8536 /* originally I read enough to fill any incomplete
8537 character and the first byte of the next
8538 character if needed, but if there's many
8539 multi-byte encoded characters we're going to be
8540 making a read call for every character beyond
8541 the original read size.
8543 So instead, read the rest of the character if
8544 any, and enough bytes to match at least the
8545 start bytes for each character we're going to
8549 readsize = recsize - charcount;
8551 readsize = skip - (bend - bufp) + recsize - charcount - 1;
8552 buffer = SvGROW(sv, append + bytesread + readsize + 1) + append;
8553 bend = buffer + bytesread;
8554 morebytesread = PerlIO_read(fp, bend, readsize);
8555 if (morebytesread <= 0) {
8556 /* we're done, if we still have incomplete
8557 characters the check code in sv_gets() will
8560 I'd originally considered doing
8561 PerlIO_ungetc() on all but the lead
8562 character of the incomplete character, but
8563 read() doesn't do that, so I don't.
8568 /* prepare to scan some more */
8569 bytesread += morebytesread;
8570 bend = buffer + bytesread;
8571 bufp = buffer + bufp_offset;
8579 SvCUR_set(sv, bytesread + append);
8580 buffer[bytesread] = '\0';
8581 return (SvCUR(sv) - append) ? SvPVX(sv) : NULL;
8587 Get a line from the filehandle and store it into the SV, optionally
8588 appending to the currently-stored string. If C<append> is not 0, the
8589 line is appended to the SV instead of overwriting it. C<append> should
8590 be set to the byte offset that the appended string should start at
8591 in the SV (typically, C<SvCUR(sv)> is a suitable choice).
8597 Perl_sv_gets(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
8607 PERL_ARGS_ASSERT_SV_GETS;
8609 if (SvTHINKFIRST(sv))
8610 sv_force_normal_flags(sv, append ? 0 : SV_COW_DROP_PV);
8611 /* XXX. If you make this PVIV, then copy on write can copy scalars read
8613 However, perlbench says it's slower, because the existing swipe code
8614 is faster than copy on write.
8615 Swings and roundabouts. */
8616 SvUPGRADE(sv, SVt_PV);
8619 /* line is going to be appended to the existing buffer in the sv */
8620 if (PerlIO_isutf8(fp)) {
8622 sv_utf8_upgrade_nomg(sv);
8623 sv_pos_u2b(sv,&append,0);
8625 } else if (SvUTF8(sv)) {
8626 return S_sv_gets_append_to_utf8(aTHX_ sv, fp, append);
8632 /* not appending - "clear" the string by setting SvCUR to 0,
8633 * the pv is still avaiable. */
8636 if (PerlIO_isutf8(fp))
8639 if (IN_PERL_COMPILETIME) {
8640 /* we always read code in line mode */
8644 else if (RsSNARF(PL_rs)) {
8645 /* If it is a regular disk file use size from stat() as estimate
8646 of amount we are going to read -- may result in mallocing
8647 more memory than we really need if the layers below reduce
8648 the size we read (e.g. CRLF or a gzip layer).
8651 int fd = PerlIO_fileno(fp);
8652 if (fd >= 0 && (PerlLIO_fstat(fd, &st) == 0) && S_ISREG(st.st_mode)) {
8653 const Off_t offset = PerlIO_tell(fp);
8654 if (offset != (Off_t) -1 && st.st_size + append > offset) {
8655 #ifdef PERL_COPY_ON_WRITE
8656 /* Add an extra byte for the sake of copy-on-write's
8657 * buffer reference count. */
8658 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 2));
8660 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1));
8667 else if (RsRECORD(PL_rs)) {
8668 return S_sv_gets_read_record(aTHX_ sv, fp, append);
8670 else if (RsPARA(PL_rs)) {
8676 /* Get $/ i.e. PL_rs into same encoding as stream wants */
8677 if (PerlIO_isutf8(fp)) {
8678 rsptr = SvPVutf8(PL_rs, rslen);
8681 if (SvUTF8(PL_rs)) {
8682 if (!sv_utf8_downgrade(PL_rs, TRUE)) {
8683 Perl_croak(aTHX_ "Wide character in $/");
8686 /* extract the raw pointer to the record separator */
8687 rsptr = SvPV_const(PL_rs, rslen);
8691 /* rslast is the last character in the record separator
8692 * note we don't use rslast except when rslen is true, so the
8693 * null assign is a placeholder. */
8694 rslast = rslen ? rsptr[rslen - 1] : '\0';
8696 if (rspara) { /* have to do this both before and after */
8697 /* to make sure file boundaries work right */
8701 i = PerlIO_getc(fp);
8705 PerlIO_ungetc(fp,i);
8711 /* See if we know enough about I/O mechanism to cheat it ! */
8713 /* This used to be #ifdef test - it is made run-time test for ease
8714 of abstracting out stdio interface. One call should be cheap
8715 enough here - and may even be a macro allowing compile
8719 if (PerlIO_fast_gets(fp)) {
8721 * We can do buffer based IO operations on this filehandle.
8723 * This means we can bypass a lot of subcalls and process
8724 * the buffer directly, it also means we know the upper bound
8725 * on the amount of data we might read of the current buffer
8726 * into our sv. Knowing this allows us to preallocate the pv
8727 * to be able to hold that maximum, which allows us to simplify
8728 * a lot of logic. */
8731 * We're going to steal some values from the stdio struct
8732 * and put EVERYTHING in the innermost loop into registers.
8734 STDCHAR *ptr; /* pointer into fp's read-ahead buffer */
8735 STRLEN bpx; /* length of the data in the target sv
8736 used to fix pointers after a SvGROW */
8737 I32 shortbuffered; /* If the pv buffer is shorter than the amount
8738 of data left in the read-ahead buffer.
8739 If 0 then the pv buffer can hold the full
8740 amount left, otherwise this is the amount it
8743 /* Here is some breathtakingly efficient cheating */
8745 /* When you read the following logic resist the urge to think
8746 * of record separators that are 1 byte long. They are an
8747 * uninteresting special (simple) case.
8749 * Instead think of record separators which are at least 2 bytes
8750 * long, and keep in mind that we need to deal with such
8751 * separators when they cross a read-ahead buffer boundary.
8753 * Also consider that we need to gracefully deal with separators
8754 * that may be longer than a single read ahead buffer.
8756 * Lastly do not forget we want to copy the delimiter as well. We
8757 * are copying all data in the file _up_to_and_including_ the separator
8760 * Now that you have all that in mind here is what is happening below:
8762 * 1. When we first enter the loop we do some memory book keeping to see
8763 * how much free space there is in the target SV. (This sub assumes that
8764 * it is operating on the same SV most of the time via $_ and that it is
8765 * going to be able to reuse the same pv buffer each call.) If there is
8766 * "enough" room then we set "shortbuffered" to how much space there is
8767 * and start reading forward.
8769 * 2. When we scan forward we copy from the read-ahead buffer to the target
8770 * SV's pv buffer. While we go we watch for the end of the read-ahead buffer,
8771 * and the end of the of pv, as well as for the "rslast", which is the last
8772 * char of the separator.
8774 * 3. When scanning forward if we see rslast then we jump backwards in *pv*
8775 * (which has a "complete" record up to the point we saw rslast) and check
8776 * it to see if it matches the separator. If it does we are done. If it doesn't
8777 * we continue on with the scan/copy.
8779 * 4. If we run out of read-ahead buffer (cnt goes to 0) then we have to get
8780 * the IO system to read the next buffer. We do this by doing a getc(), which
8781 * returns a single char read (or EOF), and prefills the buffer, and also
8782 * allows us to find out how full the buffer is. We use this information to
8783 * SvGROW() the sv to the size remaining in the buffer, after which we copy
8784 * the returned single char into the target sv, and then go back into scan
8787 * 5. If we run out of write-buffer then we SvGROW() it by the size of the
8788 * remaining space in the read-buffer.
8790 * Note that this code despite its twisty-turny nature is pretty darn slick.
8791 * It manages single byte separators, multi-byte cross boundary separators,
8792 * and cross-read-buffer separators cleanly and efficiently at the cost
8793 * of potentially greatly overallocating the target SV.
8799 /* get the number of bytes remaining in the read-ahead buffer
8800 * on first call on a given fp this will return 0.*/
8801 cnt = PerlIO_get_cnt(fp);
8803 /* make sure we have the room */
8804 if ((I32)(SvLEN(sv) - append) <= cnt + 1) {
8805 /* Not room for all of it
8806 if we are looking for a separator and room for some
8808 if (rslen && cnt > 80 && (I32)SvLEN(sv) > append) {
8809 /* just process what we have room for */
8810 shortbuffered = cnt - SvLEN(sv) + append + 1;
8811 cnt -= shortbuffered;
8814 /* ensure that the target sv has enough room to hold
8815 * the rest of the read-ahead buffer */
8817 /* remember that cnt can be negative */
8818 SvGROW(sv, (STRLEN)(append + (cnt <= 0 ? 2 : (cnt + 1))));
8822 /* we have enough room to hold the full buffer, lets scream */
8826 /* extract the pointer to sv's string buffer, offset by append as necessary */
8827 bp = (STDCHAR*)SvPVX_const(sv) + append; /* move these two too to registers */
8828 /* extract the point to the read-ahead buffer */
8829 ptr = (STDCHAR*)PerlIO_get_ptr(fp);
8831 /* some trace debug output */
8832 DEBUG_P(PerlIO_printf(Perl_debug_log,
8833 "Screamer: entering, ptr=%" UVuf ", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
8834 DEBUG_P(PerlIO_printf(Perl_debug_log,
8835 "Screamer: entering: PerlIO * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%"
8837 PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
8838 PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
8842 /* if there is stuff left in the read-ahead buffer */
8844 /* if there is a separator */
8846 /* find next rslast */
8849 /* shortcut common case of blank line */
8851 if ((*bp++ = *ptr++) == rslast)
8852 goto thats_all_folks;
8854 p = (STDCHAR *)memchr(ptr, rslast, cnt);
8856 SSize_t got = p - ptr + 1;
8857 Copy(ptr, bp, got, STDCHAR);
8861 goto thats_all_folks;
8863 Copy(ptr, bp, cnt, STDCHAR);
8869 /* no separator, slurp the full buffer */
8870 Copy(ptr, bp, cnt, char); /* this | eat */
8871 bp += cnt; /* screams | dust */
8872 ptr += cnt; /* louder | sed :-) */
8874 assert (!shortbuffered);
8875 goto cannot_be_shortbuffered;
8879 if (shortbuffered) { /* oh well, must extend */
8880 /* we didnt have enough room to fit the line into the target buffer
8881 * so we must extend the target buffer and keep going */
8882 cnt = shortbuffered;
8884 bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */
8886 /* extned the target sv's buffer so it can hold the full read-ahead buffer */
8887 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
8888 bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */
8892 cannot_be_shortbuffered:
8893 /* we need to refill the read-ahead buffer if possible */
8895 DEBUG_P(PerlIO_printf(Perl_debug_log,
8896 "Screamer: going to getc, ptr=%" UVuf ", cnt=%" IVdf "\n",
8897 PTR2UV(ptr),(IV)cnt));
8898 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* deregisterize cnt and ptr */
8900 DEBUG_Pv(PerlIO_printf(Perl_debug_log,
8901 "Screamer: pre: FILE * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%" UVuf "\n",
8902 PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
8903 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
8906 call PerlIO_getc() to let it prefill the lookahead buffer
8908 This used to call 'filbuf' in stdio form, but as that behaves like
8909 getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
8910 another abstraction.
8912 Note we have to deal with the char in 'i' if we are not at EOF
8914 bpx = bp - (STDCHAR*)SvPVX_const(sv);
8915 /* signals might be called here, possibly modifying sv */
8916 i = PerlIO_getc(fp); /* get more characters */
8917 bp = (STDCHAR*)SvPVX_const(sv) + bpx;
8919 DEBUG_Pv(PerlIO_printf(Perl_debug_log,
8920 "Screamer: post: FILE * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%" UVuf "\n",
8921 PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
8922 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
8924 /* find out how much is left in the read-ahead buffer, and rextract its pointer */
8925 cnt = PerlIO_get_cnt(fp);
8926 ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */
8927 DEBUG_P(PerlIO_printf(Perl_debug_log,
8928 "Screamer: after getc, ptr=%" UVuf ", cnt=%" IVdf "\n",
8929 PTR2UV(ptr),(IV)cnt));
8931 if (i == EOF) /* all done for ever? */
8932 goto thats_really_all_folks;
8934 /* make sure we have enough space in the target sv */
8935 bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */
8937 SvGROW(sv, bpx + cnt + 2);
8938 bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */
8940 /* copy of the char we got from getc() */
8941 *bp++ = (STDCHAR)i; /* store character from PerlIO_getc */
8943 /* make sure we deal with the i being the last character of a separator */
8944 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
8945 goto thats_all_folks;
8949 /* check if we have actually found the separator - only really applies
8951 if ((rslen > 1 && (STRLEN)(bp - (STDCHAR*)SvPVX_const(sv)) < rslen) ||
8952 memNE((char*)bp - rslen, rsptr, rslen))
8953 goto screamer; /* go back to the fray */
8954 thats_really_all_folks:
8956 cnt += shortbuffered;
8957 DEBUG_P(PerlIO_printf(Perl_debug_log,
8958 "Screamer: quitting, ptr=%" UVuf ", cnt=%" IVdf "\n",PTR2UV(ptr),(IV)cnt));
8959 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* put these back or we're in trouble */
8960 DEBUG_P(PerlIO_printf(Perl_debug_log,
8961 "Screamer: end: FILE * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%" UVuf
8963 PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
8964 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
8966 SvCUR_set(sv, bp - (STDCHAR*)SvPVX_const(sv)); /* set length */
8967 DEBUG_P(PerlIO_printf(Perl_debug_log,
8968 "Screamer: done, len=%ld, string=|%.*s|\n",
8969 (long)SvCUR(sv),(int)SvCUR(sv),SvPVX_const(sv)));
8973 /*The big, slow, and stupid way. */
8978 const STDCHAR * const bpe = buf + sizeof(buf);
8980 while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = (STDCHAR)i) != rslast && bp < bpe)
8981 ; /* keep reading */
8985 cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
8986 /* Accommodate broken VAXC compiler, which applies U8 cast to
8987 * both args of ?: operator, causing EOF to change into 255
8990 i = (U8)buf[cnt - 1];
8996 cnt = 0; /* we do need to re-set the sv even when cnt <= 0 */
8998 sv_catpvn_nomg(sv, (char *) buf, cnt);
9000 sv_setpvn(sv, (char *) buf, cnt); /* "nomg" is implied */
9002 if (i != EOF && /* joy */
9004 SvCUR(sv) < rslen ||
9005 memNE(SvPVX_const(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
9009 * If we're reading from a TTY and we get a short read,
9010 * indicating that the user hit his EOF character, we need
9011 * to notice it now, because if we try to read from the TTY
9012 * again, the EOF condition will disappear.
9014 * The comparison of cnt to sizeof(buf) is an optimization
9015 * that prevents unnecessary calls to feof().
9019 if (!(cnt < (I32)sizeof(buf) && PerlIO_eof(fp)))
9025 if (rspara) { /* have to do this both before and after */
9026 while (i != EOF) { /* to make sure file boundaries work right */
9027 i = PerlIO_getc(fp);
9029 PerlIO_ungetc(fp,i);
9035 return (SvCUR(sv) - append) ? SvPVX(sv) : NULL;
9040 =for apidoc_item sv_inc_nomg
9042 These auto-increment the value in the SV, doing string to numeric conversion
9043 if necessary. They both handle operator overloading.
9045 They differ only in that C<sv_inc> performs 'get' magic; C<sv_inc_nomg> skips
9052 Perl_sv_inc(pTHX_ SV *const sv)
9061 Perl_sv_inc_nomg(pTHX_ SV *const sv)
9068 if (SvTHINKFIRST(sv)) {
9069 if (SvREADONLY(sv)) {
9070 Perl_croak_no_modify();
9074 if (SvAMAGIC(sv) && AMG_CALLunary(sv, inc_amg))
9076 i = PTR2IV(SvRV(sv));
9080 else sv_force_normal_flags(sv, 0);
9082 flags = SvFLAGS(sv);
9083 if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) {
9084 /* It's (privately or publicly) a float, but not tested as an
9085 integer, so test it to see. */
9087 flags = SvFLAGS(sv);
9089 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
9090 /* It's publicly an integer, or privately an integer-not-float */
9091 #ifdef PERL_PRESERVE_IVUV
9095 if (SvUVX(sv) == UV_MAX)
9096 sv_setnv(sv, UV_MAX_P1);
9098 (void)SvIOK_only_UV(sv);
9099 SvUV_set(sv, SvUVX(sv) + 1);
9102 if (SvIVX(sv) == IV_MAX)
9103 sv_setuv(sv, (UV)IV_MAX + 1);
9105 (void)SvIOK_only(sv);
9106 SvIV_set(sv, SvIVX(sv) + 1);
9111 if (flags & SVp_NOK) {
9112 const NV was = SvNVX(sv);
9113 if (NV_OVERFLOWS_INTEGERS_AT != 0.0 &&
9114 /* If NVX was NaN, the following comparisons return always false */
9115 UNLIKELY(was >= NV_OVERFLOWS_INTEGERS_AT ||
9116 was < -NV_OVERFLOWS_INTEGERS_AT) &&
9117 #if defined(NAN_COMPARE_BROKEN)
9118 LIKELY(!Perl_isinfnan(was))
9120 LIKELY(!Perl_isinf(was))
9123 /* diag_listed_as: Lost precision when %s %f by 1 */
9124 Perl_ck_warner(aTHX_ packWARN(WARN_IMPRECISION),
9125 "Lost precision when incrementing %" NVff " by 1",
9128 (void)SvNOK_only(sv);
9129 SvNV_set(sv, was + 1.0);
9133 /* treat AV/HV/CV/FM/IO and non-fake GVs as immutable */
9134 if (SvTYPE(sv) >= SVt_PVAV || (isGV_with_GP(sv) && !SvFAKE(sv)))
9135 Perl_croak_no_modify();
9137 if (!(flags & SVp_POK) || !*SvPVX_const(sv)) {
9138 if ((flags & SVTYPEMASK) < SVt_PVIV)
9139 sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV ? SVt_PVIV : SVt_IV));
9140 (void)SvIOK_only(sv);
9145 while (isALPHA(*d)) d++;
9146 while (isDIGIT(*d)) d++;
9147 if (d < SvEND(sv)) {
9148 const int numtype = grok_number_flags(SvPVX_const(sv), SvCUR(sv), NULL, PERL_SCAN_TRAILING);
9149 #ifdef PERL_PRESERVE_IVUV
9150 /* Got to punt this as an integer if needs be, but we don't issue
9151 warnings. Probably ought to make the sv_iv_please() that does
9152 the conversion if possible, and silently. */
9153 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
9154 /* Need to try really hard to see if it's an integer.
9155 9.22337203685478e+18 is an integer.
9156 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
9157 so $a="9.22337203685478e+18"; $a+0; $a++
9158 needs to be the same as $a="9.22337203685478e+18"; $a++
9165 /* sv_2iv *should* have made this an NV */
9166 if (flags & SVp_NOK) {
9167 (void)SvNOK_only(sv);
9168 SvNV_set(sv, SvNVX(sv) + 1.0);
9171 /* I don't think we can get here. Maybe I should assert this
9172 And if we do get here I suspect that sv_setnv will croak. NWC
9174 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%" UVxf " NV=%" NVgf "\n",
9175 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
9177 #endif /* PERL_PRESERVE_IVUV */
9178 if (!numtype && ckWARN(WARN_NUMERIC))
9179 not_incrementable(sv);
9180 sv_setnv(sv,Atof(SvPVX_const(sv)) + 1.0);
9184 while (d >= SvPVX_const(sv)) {
9192 /* MKS: The original code here died if letters weren't consecutive.
9193 * at least it didn't have to worry about non-C locales. The
9194 * new code assumes that ('z'-'a')==('Z'-'A'), letters are
9195 * arranged in order (although not consecutively) and that only
9196 * [A-Za-z] are accepted by isALPHA in the C locale.
9198 if (isALPHA_FOLD_NE(*d, 'z')) {
9199 do { ++*d; } while (!isALPHA(*d));
9202 *(d--) -= 'z' - 'a';
9207 *(d--) -= 'z' - 'a' + 1;
9211 /* oh,oh, the number grew */
9212 SvGROW(sv, SvCUR(sv) + 2);
9213 SvCUR_set(sv, SvCUR(sv) + 1);
9214 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX_const(sv); d--)
9224 =for apidoc_item sv_dec_nomg
9226 These auto-decrement the value in the SV, doing string to numeric conversion
9227 if necessary. They both handle operator overloading.
9229 They differ only in that:
9231 C<sv_dec> handles 'get' magic; C<sv_dec_nomg> skips 'get' magic.
9237 Perl_sv_dec(pTHX_ SV *const sv)
9246 Perl_sv_dec_nomg(pTHX_ SV *const sv)
9252 if (SvTHINKFIRST(sv)) {
9253 if (SvREADONLY(sv)) {
9254 Perl_croak_no_modify();
9258 if (SvAMAGIC(sv) && AMG_CALLunary(sv, dec_amg))
9260 i = PTR2IV(SvRV(sv));
9264 else sv_force_normal_flags(sv, 0);
9266 /* Unlike sv_inc we don't have to worry about string-never-numbers
9267 and keeping them magic. But we mustn't warn on punting */
9268 flags = SvFLAGS(sv);
9269 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
9270 /* It's publicly an integer, or privately an integer-not-float */
9271 #ifdef PERL_PRESERVE_IVUV
9275 if (SvUVX(sv) == 0) {
9276 (void)SvIOK_only(sv);
9280 (void)SvIOK_only_UV(sv);
9281 SvUV_set(sv, SvUVX(sv) - 1);
9284 if (SvIVX(sv) == IV_MIN) {
9285 sv_setnv(sv, (NV)IV_MIN);
9289 (void)SvIOK_only(sv);
9290 SvIV_set(sv, SvIVX(sv) - 1);
9295 if (flags & SVp_NOK) {
9298 const NV was = SvNVX(sv);
9299 if (NV_OVERFLOWS_INTEGERS_AT != 0.0 &&
9300 /* If NVX was NaN, these comparisons return always false */
9301 UNLIKELY(was <= -NV_OVERFLOWS_INTEGERS_AT ||
9302 was > NV_OVERFLOWS_INTEGERS_AT) &&
9303 #if defined(NAN_COMPARE_BROKEN)
9304 LIKELY(!Perl_isinfnan(was))
9306 LIKELY(!Perl_isinf(was))
9309 /* diag_listed_as: Lost precision when %s %f by 1 */
9310 Perl_ck_warner(aTHX_ packWARN(WARN_IMPRECISION),
9311 "Lost precision when decrementing %" NVff " by 1",
9314 (void)SvNOK_only(sv);
9315 SvNV_set(sv, was - 1.0);
9320 /* treat AV/HV/CV/FM/IO and non-fake GVs as immutable */
9321 if (SvTYPE(sv) >= SVt_PVAV || (isGV_with_GP(sv) && !SvFAKE(sv)))
9322 Perl_croak_no_modify();
9324 if (!(flags & SVp_POK)) {
9325 if ((flags & SVTYPEMASK) < SVt_PVIV)
9326 sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV) ? SVt_PVIV : SVt_IV);
9328 (void)SvIOK_only(sv);
9331 #ifdef PERL_PRESERVE_IVUV
9333 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), NULL);
9334 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
9335 /* Need to try really hard to see if it's an integer.
9336 9.22337203685478e+18 is an integer.
9337 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
9338 so $a="9.22337203685478e+18"; $a+0; $a--
9339 needs to be the same as $a="9.22337203685478e+18"; $a--
9346 /* sv_2iv *should* have made this an NV */
9347 if (flags & SVp_NOK) {
9348 (void)SvNOK_only(sv);
9349 SvNV_set(sv, SvNVX(sv) - 1.0);
9352 /* I don't think we can get here. Maybe I should assert this
9353 And if we do get here I suspect that sv_setnv will croak. NWC
9355 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%" UVxf " NV=%" NVgf "\n",
9356 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
9359 #endif /* PERL_PRESERVE_IVUV */
9360 sv_setnv(sv,Atof(SvPVX_const(sv)) - 1.0); /* punt */
9363 /* this define is used to eliminate a chunk of duplicated but shared logic
9364 * it has the suffix __SV_C to signal that it isnt API, and isnt meant to be
9365 * used anywhere but here - yves
9367 #define PUSH_EXTEND_MORTAL__SV_C(AnSv) \
9369 SSize_t ix = ++PL_tmps_ix; \
9370 if (UNLIKELY(ix >= PL_tmps_max)) \
9371 ix = tmps_grow_p(ix); \
9372 PL_tmps_stack[ix] = (AnSv); \
9376 =for apidoc sv_mortalcopy
9378 Creates a new SV which is a copy of the original SV (using C<sv_setsv>).
9379 The new SV is marked as mortal. It will be destroyed "soon", either by an
9380 explicit call to C<FREETMPS>, or by an implicit call at places such as
9381 statement boundaries. See also C<L</sv_newmortal>> and C<L</sv_2mortal>>.
9383 =for apidoc sv_mortalcopy_flags
9385 Like C<sv_mortalcopy>, but the extra C<flags> are passed to the
9391 /* Make a string that will exist for the duration of the expression
9392 * evaluation. Actually, it may have to last longer than that, but
9393 * hopefully we won't free it until it has been assigned to a
9394 * permanent location. */
9397 Perl_sv_mortalcopy_flags(pTHX_ SV *const oldstr, U32 flags)
9401 if (flags & SV_GMAGIC)
9402 SvGETMAGIC(oldstr); /* before new_SV, in case it dies */
9404 sv_setsv_flags(sv,oldstr,flags & ~SV_GMAGIC);
9405 PUSH_EXTEND_MORTAL__SV_C(sv);
9411 =for apidoc sv_newmortal
9413 Creates a new null SV which is mortal. The reference count of the SV is
9414 set to 1. It will be destroyed "soon", either by an explicit call to
9415 C<FREETMPS>, or by an implicit call at places such as statement boundaries.
9416 See also C<L</sv_mortalcopy>> and C<L</sv_2mortal>>.
9422 Perl_sv_newmortal(pTHX)
9427 SvFLAGS(sv) = SVs_TEMP;
9428 PUSH_EXTEND_MORTAL__SV_C(sv);
9434 =for apidoc newSVpvn_flags
9436 Creates a new SV and copies a string (which may contain C<NUL> (C<\0>)
9437 characters) into it. The reference count for the
9438 SV is set to 1. Note that if C<len> is zero, Perl will create a zero length
9439 string. You are responsible for ensuring that the source string is at least
9440 C<len> bytes long. If the C<s> argument is NULL the new SV will be undefined.
9441 Currently the only flag bits accepted are C<SVf_UTF8> and C<SVs_TEMP>.
9442 If C<SVs_TEMP> is set, then C<sv_2mortal()> is called on the result before
9443 returning. If C<SVf_UTF8> is set, C<s>
9444 is considered to be in UTF-8 and the
9445 C<SVf_UTF8> flag will be set on the new SV.
9446 C<newSVpvn_utf8()> is a convenience wrapper for this function, defined as
9448 #define newSVpvn_utf8(s, len, u) \
9449 newSVpvn_flags((s), (len), (u) ? SVf_UTF8 : 0)
9451 =for apidoc Amnh||SVs_TEMP
9457 Perl_newSVpvn_flags(pTHX_ const char *const s, const STRLEN len, const U32 flags)
9461 /* All the flags we don't support must be zero.
9462 And we're new code so I'm going to assert this from the start. */
9463 assert(!(flags & ~(SVf_UTF8|SVs_TEMP)));
9464 sv = newSV_type(SVt_PV);
9465 sv_setpvn_fresh(sv,s,len);
9467 /* This code used to do a sv_2mortal(), however we now unroll the call to
9468 * sv_2mortal() and do what it does ourselves here. Since we have asserted
9469 * that flags can only have the SVf_UTF8 and/or SVs_TEMP flags set above we
9470 * can use it to enable the sv flags directly (bypassing SvTEMP_on), which
9471 * in turn means we dont need to mask out the SVf_UTF8 flag below, which
9472 * means that we eliminate quite a few steps than it looks - Yves
9473 * (explaining patch by gfx) */
9475 SvFLAGS(sv) |= flags;
9477 if(flags & SVs_TEMP){
9478 PUSH_EXTEND_MORTAL__SV_C(sv);
9485 =for apidoc sv_2mortal
9487 Marks an existing SV as mortal. The SV will be destroyed "soon", either
9488 by an explicit call to C<FREETMPS>, or by an implicit call at places such as
9489 statement boundaries. C<SvTEMP()> is turned on which means that the SV's
9490 string buffer can be "stolen" if this SV is copied. See also
9491 C<L</sv_newmortal>> and C<L</sv_mortalcopy>>.
9497 Perl_sv_2mortal(pTHX_ SV *const sv)
9503 PUSH_EXTEND_MORTAL__SV_C(sv);
9511 Creates a new SV and copies a string (which may contain C<NUL> (C<\0>)
9512 characters) into it. The reference count for the
9513 SV is set to 1. If C<len> is zero, Perl will compute the length using
9514 C<strlen()>, (which means if you use this option, that C<s> can't have embedded
9515 C<NUL> characters and has to have a terminating C<NUL> byte).
9517 This function can cause reliability issues if you are likely to pass in
9518 empty strings that are not null terminated, because it will run
9519 strlen on the string and potentially run past valid memory.
9521 Using L</newSVpvn> is a safer alternative for non C<NUL> terminated strings.
9522 For string literals use L</newSVpvs> instead. This function will work fine for
9523 C<NUL> terminated strings, but if you want to avoid the if statement on whether
9524 to call C<strlen> use C<newSVpvn> instead (calling C<strlen> yourself).
9530 Perl_newSVpv(pTHX_ const char *const s, const STRLEN len)
9532 SV *sv = newSV_type(SVt_PV);
9533 sv_setpvn_fresh(sv, s, len || s == NULL ? len : strlen(s));
9538 =for apidoc newSVpvn
9540 Creates a new SV and copies a string into it, which may contain C<NUL> characters
9541 (C<\0>) and other binary data. The reference count for the SV is set to 1.
9542 Note that if C<len> is zero, Perl will create a zero length (Perl) string. You
9543 are responsible for ensuring that the source buffer is at least
9544 C<len> bytes long. If the C<buffer> argument is NULL the new SV will be
9551 Perl_newSVpvn(pTHX_ const char *const buffer, const STRLEN len)
9553 SV *sv = newSV_type(SVt_PV);
9554 sv_setpvn_fresh(sv,buffer,len);
9559 =for apidoc newSVhek_mortal
9561 Creates a new mortal SV from the hash key structure. It will generate
9562 scalars that point to the shared string table where possible. Returns
9563 a new (undefined) SV if C<hek> is NULL.
9565 This is more efficient than using sv_2mortal(newSVhek( ... ))
9571 Perl_newSVhek_mortal(pTHX_ const HEK *const hek)
9573 SV * const sv = newSVhek(hek);
9575 assert(!SvIMMORTAL(sv));
9577 PUSH_EXTEND_MORTAL__SV_C(sv);
9583 =for apidoc newSVhek
9585 Creates a new SV from the hash key structure. It will generate scalars that
9586 point to the shared string table where possible. Returns a new (undefined)
9587 SV if C<hek> is NULL.
9593 Perl_newSVhek(pTHX_ const HEK *const hek)
9602 if (HEK_LEN(hek) == HEf_SVKEY) {
9603 return newSVsv(*(SV**)HEK_KEY(hek));
9605 const int flags = HEK_FLAGS(hek);
9606 if (flags & HVhek_WASUTF8) {
9608 Andreas would like keys he put in as utf8 to come back as utf8
9610 STRLEN utf8_len = HEK_LEN(hek);
9611 SV * const sv = newSV_type(SVt_PV);
9612 char *as_utf8 = (char *)bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
9613 /* bytes_to_utf8() allocates a new string, which we can repurpose: */
9614 sv_usepvn_flags(sv, as_utf8, utf8_len, SV_HAS_TRAILING_NUL);
9617 } else if (flags & HVhek_NOTSHARED) {
9618 /* A hash that isn't using shared hash keys has to have
9619 the flag in every key so that we know not to try to call
9620 share_hek_hek on it. */
9622 SV * const sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
9627 /* This will be overwhelminly the most common case. */
9629 /* Inline most of newSVpvn_share(), because share_hek_hek() is far
9630 more efficient than sharepvn(). */
9631 SV *sv = newSV_type(SVt_PV);
9633 SvPV_set(sv, (char *)HEK_KEY(share_hek_hek(hek)));
9634 SvCUR_set(sv, HEK_LEN(hek));
9646 =for apidoc newSVpvn_share
9648 Creates a new SV with its C<SvPVX_const> pointing to a shared string in the string
9649 table. If the string does not already exist in the table, it is
9650 created first. Turns on the C<SvIsCOW> flag (or C<READONLY>
9651 and C<FAKE> in 5.16 and earlier). If the C<hash> parameter
9652 is non-zero, that value is used; otherwise the hash is computed.
9653 The string's hash can later be retrieved from the SV
9654 with the C<L</SvSHARED_HASH>> macro. The idea here is
9655 that as the string table is used for shared hash keys these strings will have
9656 C<SvPVX_const == HeKEY> and hash lookup will avoid string compare.
9662 Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash)
9665 bool is_utf8 = FALSE;
9666 const char *const orig_src = src;
9669 STRLEN tmplen = -len;
9671 /* See the note in hv.c:hv_fetch() --jhi */
9672 src = (char*)bytes_from_utf8((const U8*)src, &tmplen, &is_utf8);
9676 PERL_HASH(hash, src, len);
9677 sv = newSV_type(SVt_PV);
9678 /* The logic for this is inlined in S_mro_get_linear_isa_dfs(), so if it
9679 changes here, update it there too. */
9680 SvPV_set(sv, sharepvn(src, is_utf8?-len:len, hash));
9687 if (src != orig_src)
9693 =for apidoc newSVpv_share
9695 Like C<newSVpvn_share>, but takes a C<NUL>-terminated string instead of a
9702 Perl_newSVpv_share(pTHX_ const char *src, U32 hash)
9704 return newSVpvn_share(src, strlen(src), hash);
9707 #if defined(MULTIPLICITY)
9709 /* pTHX_ magic can't cope with varargs, so this is a no-context
9710 * version of the main function, (which may itself be aliased to us).
9711 * Don't access this version directly.
9715 Perl_newSVpvf_nocontext(const char *const pat, ...)
9721 PERL_ARGS_ASSERT_NEWSVPVF_NOCONTEXT;
9723 va_start(args, pat);
9724 sv = vnewSVpvf(pat, &args);
9731 =for apidoc newSVpvf
9733 Creates a new SV and initializes it with the string formatted like
9736 =for apidoc newSVpvf_nocontext
9737 Like C<L</newSVpvf>> but does not take a thread context (C<aTHX>) parameter,
9738 so is used in situations where the caller doesn't already have the thread
9741 =for apidoc vnewSVpvf
9742 Like C<L</newSVpvf>> but the arguments are an encapsulated argument list.
9748 Perl_newSVpvf(pTHX_ const char *const pat, ...)
9753 PERL_ARGS_ASSERT_NEWSVPVF;
9755 va_start(args, pat);
9756 sv = vnewSVpvf(pat, &args);
9761 /* backend for newSVpvf() and newSVpvf_nocontext() */
9764 Perl_vnewSVpvf(pTHX_ const char *const pat, va_list *const args)
9768 PERL_ARGS_ASSERT_VNEWSVPVF;
9771 SvPVCLEAR_FRESH(sv);
9772 sv_vcatpvfn_flags(sv, pat, strlen(pat), args, NULL, 0, NULL, 0);
9779 Creates a new SV and copies a floating point value into it.
9780 The reference count for the SV is set to 1.
9786 Perl_newSVnv(pTHX_ const NV n)
9788 SV *sv = newSV_type(SVt_NV);
9800 Creates a new SV and copies an integer into it. The reference count for the
9807 Perl_newSViv(pTHX_ const IV i)
9809 SV *sv = newSV_type(SVt_IV);
9821 Creates a new SV and copies an unsigned integer into it.
9822 The reference count for the SV is set to 1.
9828 Perl_newSVuv(pTHX_ const UV u)
9832 /* Inlining ONLY the small relevant subset of sv_setuv here
9833 * for performance. Makes a significant difference. */
9835 /* Using ivs is more efficient than using uvs - see sv_setuv */
9836 if (u <= (UV)IV_MAX) {
9837 return newSViv((IV)u);
9842 /* We're starting from SVt_FIRST, so provided that's
9843 * actual 0, we don't have to unset any SV type flags
9844 * to promote to SVt_IV. */
9845 STATIC_ASSERT_STMT(SVt_FIRST == 0);
9847 SET_SVANY_FOR_BODYLESS_IV(sv);
9848 SvFLAGS(sv) |= SVt_IV;
9850 (void)SvIsUV_on(sv);
9859 =for apidoc newSVbool
9861 Creates a new SV boolean.
9867 Perl_newSVbool(pTHX_ bool bool_val)
9869 PERL_ARGS_ASSERT_NEWSVBOOL;
9870 SV *sv = newSVsv(bool_val ? &PL_sv_yes : &PL_sv_no);
9876 =for apidoc newSV_true
9878 Creates a new SV that is a boolean true.
9883 Perl_newSV_true(pTHX)
9885 PERL_ARGS_ASSERT_NEWSV_TRUE;
9886 SV *sv = newSVsv(&PL_sv_yes);
9892 =for apidoc newSV_false
9894 Creates a new SV that is a boolean false.
9900 Perl_newSV_false(pTHX)
9902 PERL_ARGS_ASSERT_NEWSV_FALSE;
9903 SV *sv = newSVsv(&PL_sv_no);
9908 /* newRV_inc is the official function name to use now.
9909 * newRV_inc is in fact #defined to newRV in sv.h
9913 Perl_newRV(pTHX_ SV *const sv)
9915 PERL_ARGS_ASSERT_NEWRV;
9917 return newRV_noinc(SvREFCNT_inc_simple_NN(sv));
9922 =for apidoc_item newSVsv_flags
9923 =for apidoc_item newSVsv_nomg
9925 These create a new SV which is an exact duplicate of the original SV
9926 (using C<sv_setsv>.)
9928 They differ only in that C<newSVsv> performs 'get' magic; C<newSVsv_nomg> skips
9929 any magic; and C<newSVsv_flags> allows you to explicitly set a C<flags>
9936 Perl_newSVsv_flags(pTHX_ SV *const old, I32 flags)
9942 if (SvTYPE(old) == (svtype)SVTYPEMASK) {
9943 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "semi-panic: attempt to dup freed string");
9946 /* Do this here, otherwise we leak the new SV if this croaks. */
9947 if (flags & SV_GMAGIC)
9950 sv_setsv_flags(sv, old, flags & ~SV_GMAGIC);
9955 =for apidoc sv_reset
9957 Underlying implementation for the C<reset> Perl function.
9958 Note that the perl-level function is vaguely deprecated.
9964 Perl_sv_reset(pTHX_ const char *s, HV *const stash)
9966 PERL_ARGS_ASSERT_SV_RESET;
9968 sv_resetpvn(*s ? s : NULL, strlen(s), stash);
9972 Perl_sv_resetpvn(pTHX_ const char *s, STRLEN len, HV * const stash)
9974 char todo[PERL_UCHAR_MAX+1];
9977 if (!stash || SvTYPE(stash) != SVt_PVHV)
9980 if (!s) { /* reset ?? searches */
9981 MAGIC * const mg = mg_find((const SV *)stash, PERL_MAGIC_symtab);
9982 if (mg && mg->mg_len) {
9983 const U32 count = mg->mg_len / sizeof(PMOP**);
9984 PMOP **pmp = (PMOP**) mg->mg_ptr;
9985 PMOP *const *const end = pmp + count;
9989 SvREADONLY_off(PL_regex_pad[(*pmp)->op_pmoffset]);
9991 (*pmp)->op_pmflags &= ~PMf_USED;
9999 /* reset variables */
10001 if (!HvTOTALKEYS(stash))
10004 Zero(todo, 256, char);
10008 I32 i = (unsigned char)*s;
10012 max = (unsigned char)*s++;
10013 for ( ; i <= max; i++) {
10016 for (i = 0; i <= (I32) HvMAX(stash); i++) {
10018 for (entry = HvARRAY(stash)[i];
10020 entry = HeNEXT(entry))
10025 if (!todo[(U8)*HeKEY(entry)])
10027 gv = MUTABLE_GV(HeVAL(entry));
10031 if (sv && !SvREADONLY(sv)) {
10032 SV_CHECK_THINKFIRST_COW_DROP(sv);
10033 if (!isGV(sv)) SvOK_off(sv);
10036 av_clear(GvAV(gv));
10038 if (GvHV(gv) && !HvNAME_get(GvHV(gv))) {
10039 hv_clear(GvHV(gv));
10049 Using various gambits, try to get an IO from an SV: the IO slot if its a
10050 GV; or the recursive result if we're an RV; or the IO slot of the symbol
10051 named after the PV if we're a string.
10053 'Get' magic is ignored on the C<sv> passed in, but will be called on
10054 C<SvRV(sv)> if C<sv> is an RV.
10060 Perl_sv_2io(pTHX_ SV *const sv)
10065 PERL_ARGS_ASSERT_SV_2IO;
10067 switch (SvTYPE(sv)) {
10069 io = MUTABLE_IO(sv);
10073 if (isGV_with_GP(sv)) {
10074 gv = MUTABLE_GV(sv);
10077 Perl_croak(aTHX_ "Bad filehandle: %" HEKf,
10078 HEKfARG(GvNAME_HEK(gv)));
10084 Perl_croak(aTHX_ PL_no_usym, "filehandle");
10086 SvGETMAGIC(SvRV(sv));
10087 return sv_2io(SvRV(sv));
10089 gv = gv_fetchsv_nomg(sv, 0, SVt_PVIO);
10096 if (SvGMAGICAL(sv)) {
10097 newsv = sv_newmortal();
10098 sv_setsv_nomg(newsv, sv);
10100 Perl_croak(aTHX_ "Bad filehandle: %" SVf, SVfARG(newsv));
10110 Using various gambits, try to get a CV from an SV; in addition, try if
10111 possible to set C<*st> and C<*gvp> to the stash and GV associated with it.
10112 The flags in C<lref> are passed to C<gv_fetchsv>.
10118 Perl_sv_2cv(pTHX_ SV *sv, HV **const st, GV **const gvp, const I32 lref)
10123 PERL_ARGS_ASSERT_SV_2CV;
10130 switch (SvTYPE(sv)) {
10134 return MUTABLE_CV(sv);
10144 sv = amagic_deref_call(sv, to_cv_amg);
10147 if (SvTYPE(sv) == SVt_PVCV) {
10148 cv = MUTABLE_CV(sv);
10153 else if(SvGETMAGIC(sv), isGV_with_GP(sv))
10154 gv = MUTABLE_GV(sv);
10156 Perl_croak(aTHX_ "Not a subroutine reference");
10158 else if (isGV_with_GP(sv)) {
10159 gv = MUTABLE_GV(sv);
10162 gv = gv_fetchsv_nomg(sv, lref, SVt_PVCV);
10169 /* Some flags to gv_fetchsv mean don't really create the GV */
10170 if (!isGV_with_GP(gv)) {
10174 *st = GvESTASH(gv);
10175 if (lref & ~GV_ADDMG && !GvCVu(gv)) {
10176 /* XXX this is probably not what they think they're getting.
10177 * It has the same effect as "sub name;", i.e. just a forward
10186 =for apidoc sv_true
10188 Returns true if the SV has a true value by Perl's rules.
10189 Use the C<SvTRUE> macro instead, which may call C<sv_true()> or may
10190 instead use an in-line version.
10196 Perl_sv_true(pTHX_ SV *const sv)
10201 const XPV* const tXpv = (XPV*)SvANY(sv);
10203 (tXpv->xpv_cur > 1 ||
10204 (tXpv->xpv_cur && *sv->sv_u.svu_pv != '0')))
10211 return SvIVX(sv) != 0;
10214 return SvNVX(sv) != 0.0;
10216 return sv_2bool(sv);
10222 =for apidoc sv_pvn_force
10224 Get a sensible string out of the SV somehow.
10225 A private implementation of the C<SvPV_force> macro for compilers which
10226 can't cope with complex macro expressions. Always use the macro instead.
10228 =for apidoc sv_pvn_force_flags
10230 Get a sensible string out of the SV somehow.
10231 If C<flags> has the C<SV_GMAGIC> bit set, will C<L</mg_get>> on C<sv> if
10232 appropriate, else not. C<sv_pvn_force> and C<sv_pvn_force_nomg> are
10233 implemented in terms of this function.
10234 You normally want to use the various wrapper macros instead: see
10235 C<L</SvPV_force>> and C<L</SvPV_force_nomg>>.
10241 Perl_sv_pvn_force_flags(pTHX_ SV *const sv, STRLEN *const lp, const U32 flags)
10243 PERL_ARGS_ASSERT_SV_PVN_FORCE_FLAGS;
10245 if (flags & SV_GMAGIC) SvGETMAGIC(sv);
10246 if (SvTHINKFIRST(sv) && (!SvROK(sv) || SvREADONLY(sv)))
10247 sv_force_normal_flags(sv, 0);
10257 if (SvTYPE(sv) > SVt_PVLV
10258 || isGV_with_GP(sv))
10259 /* diag_listed_as: Can't coerce %s to %s in %s */
10260 Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0),
10262 s = sv_2pv_flags(sv, &len, flags &~ SV_GMAGIC);
10269 if (SvTYPE(sv) < SVt_PV ||
10270 s != SvPVX_const(sv)) { /* Almost, but not quite, sv_setpvn() */
10273 SvUPGRADE(sv, SVt_PV); /* Never FALSE */
10274 SvGROW(sv, len + 1);
10275 Move(s,SvPVX(sv),len,char);
10276 SvCUR_set(sv, len);
10277 SvPVX(sv)[len] = '\0';
10280 SvPOK_on(sv); /* validate pointer */
10282 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2pv(%s)\n",
10283 PTR2UV(sv),SvPVX_const(sv)));
10286 (void)SvPOK_only_UTF8(sv);
10287 return SvPVX_mutable(sv);
10291 =for apidoc sv_pvbyten_force
10293 The backend for the C<SvPVbytex_force> macro. Always use the macro
10294 instead. If the SV cannot be downgraded from UTF-8, this croaks.
10300 Perl_sv_pvbyten_force(pTHX_ SV *const sv, STRLEN *const lp)
10302 PERL_ARGS_ASSERT_SV_PVBYTEN_FORCE;
10304 sv_pvn_force(sv,lp);
10305 (void)sv_utf8_downgrade(sv,0);
10311 =for apidoc sv_pvutf8n_force
10313 The backend for the C<SvPVutf8x_force> macro. Always use the macro
10320 Perl_sv_pvutf8n_force(pTHX_ SV *const sv, STRLEN *const lp)
10322 PERL_ARGS_ASSERT_SV_PVUTF8N_FORCE;
10324 sv_pvn_force(sv,0);
10325 sv_utf8_upgrade_nomg(sv);
10331 =for apidoc sv_reftype
10333 Returns a string describing what the SV is a reference to.
10335 If ob is true and the SV is blessed, the string is the class name,
10336 otherwise it is the type of the SV, "SCALAR", "ARRAY" etc.
10342 Perl_sv_reftype(pTHX_ const SV *const sv, const int ob)
10344 PERL_ARGS_ASSERT_SV_REFTYPE;
10345 if (ob && SvOBJECT(sv)) {
10346 return SvPV_nolen_const(sv_ref(NULL, sv, ob));
10349 /* WARNING - There is code, for instance in mg.c, that assumes that
10350 * the only reason that sv_reftype(sv,0) would return a string starting
10351 * with 'L' or 'S' is that it is a LVALUE or a SCALAR.
10352 * Yes this a dodgy way to do type checking, but it saves practically reimplementing
10353 * this routine inside other subs, and it saves time.
10354 * Do not change this assumption without searching for "dodgy type check" in
10357 switch (SvTYPE(sv)) {
10372 case SVt_PVLV: return (char *) (SvROK(sv) ? "REF"
10373 /* tied lvalues should appear to be
10374 * scalars for backwards compatibility */
10375 : (isALPHA_FOLD_EQ(LvTYPE(sv), 't'))
10376 ? "SCALAR" : "LVALUE");
10377 case SVt_PVAV: return "ARRAY";
10378 case SVt_PVHV: return "HASH";
10379 case SVt_PVCV: return "CODE";
10380 case SVt_PVGV: return (char *) (isGV_with_GP(sv)
10381 ? "GLOB" : "SCALAR");
10382 case SVt_PVFM: return "FORMAT";
10383 case SVt_PVIO: return "IO";
10384 case SVt_INVLIST: return "INVLIST";
10385 case SVt_REGEXP: return "REGEXP";
10386 default: return "UNKNOWN";
10394 Returns a SV describing what the SV passed in is a reference to.
10396 dst can be a SV to be set to the description or NULL, in which case a
10397 mortal SV is returned.
10399 If ob is true and the SV is blessed, the description is the class
10400 name, otherwise it is the type of the SV, "SCALAR", "ARRAY" etc.
10406 Perl_sv_ref(pTHX_ SV *dst, const SV *const sv, const int ob)
10408 PERL_ARGS_ASSERT_SV_REF;
10411 dst = sv_newmortal();
10413 if (ob && SvOBJECT(sv)) {
10414 HvNAME_get(SvSTASH(sv))
10415 ? sv_sethek(dst, HvNAME_HEK(SvSTASH(sv)))
10416 : sv_setpvs(dst, "__ANON__");
10419 const char * reftype = sv_reftype(sv, 0);
10420 sv_setpv(dst, reftype);
10426 =for apidoc sv_isobject
10428 Returns a boolean indicating whether the SV is an RV pointing to a blessed
10429 object. If the SV is not an RV, or if the object is not blessed, then this
10436 Perl_sv_isobject(pTHX_ SV *sv)
10452 Returns a boolean indicating whether the SV is blessed into the specified
10455 This does not check for subtypes or method overloading. Use C<sv_isa_sv> to
10456 verify an inheritance relationship in the same way as the C<isa> operator by
10457 respecting any C<isa()> method overloading; or C<sv_derived_from_sv> to test
10458 directly on the actual object type.
10464 Perl_sv_isa(pTHX_ SV *sv, const char *const name)
10466 const char *hvname;
10468 PERL_ARGS_ASSERT_SV_ISA;
10478 hvname = HvNAME_get(SvSTASH(sv));
10482 return strEQ(hvname, name);
10486 =for apidoc newSVrv
10488 Creates a new SV for the existing RV, C<rv>, to point to. If C<rv> is not an
10489 RV then it will be upgraded to one. If C<classname> is non-null then the new
10490 SV will be blessed in the specified package. The new SV is returned and its
10491 reference count is 1. The reference count 1 is owned by C<rv>. See also
10492 newRV_inc() and newRV_noinc() for creating a new RV properly.
10498 Perl_newSVrv(pTHX_ SV *const rv, const char *const classname)
10502 PERL_ARGS_ASSERT_NEWSVRV;
10506 SV_CHECK_THINKFIRST_COW_DROP(rv);
10508 if (UNLIKELY( SvTYPE(rv) >= SVt_PVMG )) {
10509 const U32 refcnt = SvREFCNT(rv);
10513 SvREFCNT(rv) = refcnt;
10515 sv_upgrade(rv, SVt_IV);
10516 } else if (SvROK(rv)) {
10517 SvREFCNT_dec(SvRV(rv));
10519 prepare_SV_for_RV(rv);
10527 HV* const stash = gv_stashpv(classname, GV_ADD);
10528 (void)sv_bless(rv, stash);
10534 Perl_newSVavdefelem(pTHX_ AV *av, SSize_t ix, bool extendible)
10536 SV * const lv = newSV_type(SVt_PVLV);
10537 PERL_ARGS_ASSERT_NEWSVAVDEFELEM;
10539 sv_magic(lv, NULL, PERL_MAGIC_defelem, NULL, 0);
10540 LvTARG(lv) = SvREFCNT_inc_simple_NN(av);
10541 LvSTARGOFF(lv) = ix;
10542 LvTARGLEN(lv) = extendible ? 1 : (STRLEN)UV_MAX;
10547 =for apidoc sv_setref_pv
10549 Copies a pointer into a new SV, optionally blessing the SV. The C<rv>
10550 argument will be upgraded to an RV. That RV will be modified to point to
10551 the new SV. If the C<pv> argument is C<NULL>, then C<PL_sv_undef> will be placed
10552 into the SV. The C<classname> argument indicates the package for the
10553 blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
10554 will have a reference count of 1, and the RV will be returned.
10556 Do not use with other Perl types such as HV, AV, SV, CV, because those
10557 objects will become corrupted by the pointer copy process.
10559 Note that C<sv_setref_pvn> copies the string while this copies the pointer.
10565 Perl_sv_setref_pv(pTHX_ SV *const rv, const char *const classname, void *const pv)
10567 PERL_ARGS_ASSERT_SV_SETREF_PV;
10574 sv_setiv(newSVrv(rv,classname), PTR2IV(pv));
10579 =for apidoc sv_setref_iv
10581 Copies an integer into a new SV, optionally blessing the SV. The C<rv>
10582 argument will be upgraded to an RV. That RV will be modified to point to
10583 the new SV. The C<classname> argument indicates the package for the
10584 blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
10585 will have a reference count of 1, and the RV will be returned.
10591 Perl_sv_setref_iv(pTHX_ SV *const rv, const char *const classname, const IV iv)
10593 PERL_ARGS_ASSERT_SV_SETREF_IV;
10595 sv_setiv(newSVrv(rv,classname), iv);
10600 =for apidoc sv_setref_uv
10602 Copies an unsigned integer into a new SV, optionally blessing the SV. The C<rv>
10603 argument will be upgraded to an RV. That RV will be modified to point to
10604 the new SV. The C<classname> argument indicates the package for the
10605 blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
10606 will have a reference count of 1, and the RV will be returned.
10612 Perl_sv_setref_uv(pTHX_ SV *const rv, const char *const classname, const UV uv)
10614 PERL_ARGS_ASSERT_SV_SETREF_UV;
10616 sv_setuv(newSVrv(rv,classname), uv);
10621 =for apidoc sv_setref_nv
10623 Copies a double into a new SV, optionally blessing the SV. The C<rv>
10624 argument will be upgraded to an RV. That RV will be modified to point to
10625 the new SV. The C<classname> argument indicates the package for the
10626 blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
10627 will have a reference count of 1, and the RV will be returned.
10633 Perl_sv_setref_nv(pTHX_ SV *const rv, const char *const classname, const NV nv)
10635 PERL_ARGS_ASSERT_SV_SETREF_NV;
10637 sv_setnv(newSVrv(rv,classname), nv);
10642 =for apidoc sv_setref_pvn
10644 Copies a string into a new SV, optionally blessing the SV. The length of the
10645 string must be specified with C<n>. The C<rv> argument will be upgraded to
10646 an RV. That RV will be modified to point to the new SV. The C<classname>
10647 argument indicates the package for the blessing. Set C<classname> to
10648 C<NULL> to avoid the blessing. The new SV will have a reference count
10649 of 1, and the RV will be returned.
10651 Note that C<sv_setref_pv> copies the pointer while this copies the string.
10657 Perl_sv_setref_pvn(pTHX_ SV *const rv, const char *const classname,
10658 const char *const pv, const STRLEN n)
10660 PERL_ARGS_ASSERT_SV_SETREF_PVN;
10662 sv_setpvn(newSVrv(rv,classname), pv, n);
10667 =for apidoc sv_bless
10669 Blesses an SV into a specified package. The SV must be an RV. The package
10670 must be designated by its stash (see C<L</gv_stashpv>>). The reference count
10671 of the SV is unaffected.
10677 Perl_sv_bless(pTHX_ SV *const sv, HV *const stash)
10680 HV *oldstash = NULL;
10682 PERL_ARGS_ASSERT_SV_BLESS;
10686 Perl_croak(aTHX_ "Can't bless non-reference value");
10688 if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY|SVf_PROTECT)) {
10689 if (SvREADONLY(tmpRef))
10690 Perl_croak_no_modify();
10691 if (SvOBJECT(tmpRef)) {
10692 oldstash = SvSTASH(tmpRef);
10695 SvOBJECT_on(tmpRef);
10696 SvUPGRADE(tmpRef, SVt_PVMG);
10697 SvSTASH_set(tmpRef, MUTABLE_HV(SvREFCNT_inc_simple(stash)));
10698 SvREFCNT_dec(oldstash);
10700 if(SvSMAGICAL(tmpRef))
10701 if(mg_find(tmpRef, PERL_MAGIC_ext) || mg_find(tmpRef, PERL_MAGIC_uvar))
10709 /* Downgrades a PVGV to a PVMG. If it's actually a PVLV, we leave the type
10710 * as it is after unglobbing it.
10713 PERL_STATIC_INLINE void
10714 S_sv_unglob(pTHX_ SV *const sv, U32 flags)
10718 SV * const temp = flags & SV_COW_DROP_PV ? NULL : sv_newmortal();
10720 PERL_ARGS_ASSERT_SV_UNGLOB;
10722 assert(SvTYPE(sv) == SVt_PVGV || SvTYPE(sv) == SVt_PVLV);
10724 if (!(flags & SV_COW_DROP_PV))
10725 gv_efullname3(temp, MUTABLE_GV(sv), "*");
10727 SvREFCNT_inc_simple_void_NN(sv_2mortal(sv));
10729 if(GvCVu((const GV *)sv) && (stash = GvSTASH(MUTABLE_GV(sv)))
10730 && HvNAME_get(stash))
10731 mro_method_changed_in(stash);
10732 gp_free(MUTABLE_GV(sv));
10735 sv_del_backref(MUTABLE_SV(GvSTASH(sv)), sv);
10736 GvSTASH(sv) = NULL;
10739 if (GvNAME_HEK(sv)) {
10740 unshare_hek(GvNAME_HEK(sv));
10742 isGV_with_GP_off(sv);
10744 if(SvTYPE(sv) == SVt_PVGV) {
10745 /* need to keep SvANY(sv) in the right arena */
10746 xpvmg = new_XPVMG();
10747 StructCopy(SvANY(sv), xpvmg, XPVMG);
10748 del_body_by_type(SvANY(sv), SVt_PVGV);
10751 SvFLAGS(sv) &= ~SVTYPEMASK;
10752 SvFLAGS(sv) |= SVt_PVMG;
10755 /* Intentionally not calling any local SET magic, as this isn't so much a
10756 set operation as merely an internal storage change. */
10757 if (flags & SV_COW_DROP_PV) SvOK_off(sv);
10758 else sv_setsv_flags(sv, temp, 0);
10760 if ((const GV *)sv == PL_last_in_gv)
10761 PL_last_in_gv = NULL;
10762 else if ((const GV *)sv == PL_statgv)
10767 =for apidoc sv_unref_flags
10769 Unsets the RV status of the SV, and decrements the reference count of
10770 whatever was being referenced by the RV. This can almost be thought of
10771 as a reversal of C<newSVrv>. The C<cflags> argument can contain
10772 C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented
10773 (otherwise the decrementing is conditional on the reference count being
10774 different from one or the reference being a readonly SV).
10775 See C<L</SvROK_off>>.
10777 =for apidoc Amnh||SV_IMMEDIATE_UNREF
10783 Perl_sv_unref_flags(pTHX_ SV *const ref, const U32 flags)
10785 SV* const target = SvRV(ref);
10787 PERL_ARGS_ASSERT_SV_UNREF_FLAGS;
10789 if (SvWEAKREF(ref)) {
10790 sv_del_backref(target, ref);
10791 SvWEAKREF_off(ref);
10792 SvRV_set(ref, NULL);
10795 SvRV_set(ref, NULL);
10797 /* You can't have a || SvREADONLY(target) here, as $a = $$a, where $a was
10798 assigned to as BEGIN {$a = \"Foo"} will fail. */
10799 if (SvREFCNT(target) != 1 || (flags & SV_IMMEDIATE_UNREF))
10800 SvREFCNT_dec_NN(target);
10801 else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */
10802 sv_2mortal(target); /* Schedule for freeing later */
10806 =for apidoc sv_untaint
10808 Untaint an SV. Use C<SvTAINTED_off> instead.
10814 Perl_sv_untaint(pTHX_ SV *const sv)
10816 PERL_ARGS_ASSERT_SV_UNTAINT;
10817 PERL_UNUSED_CONTEXT;
10819 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
10820 MAGIC * const mg = mg_find(sv, PERL_MAGIC_taint);
10827 =for apidoc sv_tainted
10829 Test an SV for taintedness. Use C<SvTAINTED> instead.
10835 Perl_sv_tainted(pTHX_ SV *const sv)
10837 PERL_ARGS_ASSERT_SV_TAINTED;
10838 PERL_UNUSED_CONTEXT;
10840 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
10841 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_taint);
10842 if (mg && (mg->mg_len & 1) )
10848 #if defined(MULTIPLICITY)
10850 /* pTHX_ magic can't cope with varargs, so this is a no-context
10851 * version of the main function, (which may itself be aliased to us).
10852 * Don't access this version directly.
10856 Perl_sv_setpvf_nocontext(SV *const sv, const char *const pat, ...)
10861 PERL_ARGS_ASSERT_SV_SETPVF_NOCONTEXT;
10863 va_start(args, pat);
10864 sv_vsetpvf(sv, pat, &args);
10868 /* pTHX_ magic can't cope with varargs, so this is a no-context
10869 * version of the main function, (which may itself be aliased to us).
10870 * Don't access this version directly.
10874 Perl_sv_setpvf_mg_nocontext(SV *const sv, const char *const pat, ...)
10879 PERL_ARGS_ASSERT_SV_SETPVF_MG_NOCONTEXT;
10881 va_start(args, pat);
10882 sv_vsetpvf_mg(sv, pat, &args);
10888 =for apidoc sv_setpvf
10889 =for apidoc_item sv_setpvf_mg
10890 =for apidoc_item sv_setpvf_mg_nocontext
10891 =for apidoc_item sv_setpvf_nocontext
10893 These work like C<L</sv_catpvf>> but copy the text into the SV instead of
10896 The differences between these are:
10898 C<sv_setpvf_mg> and C<sv_setpvf_mg_nocontext> perform 'set' magic; C<sv_setpvf>
10899 and C<sv_setpvf_nocontext> skip all magic.
10901 C<sv_setpvf_nocontext> and C<sv_setpvf_mg_nocontext> do not take a thread
10902 context (C<aTHX>) parameter, so are used in situations where the caller
10903 doesn't already have the thread context.
10909 Perl_sv_setpvf(pTHX_ SV *const sv, const char *const pat, ...)
10913 PERL_ARGS_ASSERT_SV_SETPVF;
10915 va_start(args, pat);
10916 sv_vsetpvf(sv, pat, &args);
10921 =for apidoc sv_vsetpvf
10922 =for apidoc_item sv_vsetpvf_mg
10924 These work like C<L</sv_vcatpvf>> but copy the text into the SV instead of
10927 They differ only in that C<sv_vsetpvf_mg> performs 'set' magic;
10928 C<sv_vsetpvf> skips all magic.
10930 They are usually used via their frontends, C<L</sv_setpvf>> and
10931 C<L</sv_setpvf_mg>>.
10937 Perl_sv_vsetpvf(pTHX_ SV *const sv, const char *const pat, va_list *const args)
10939 PERL_ARGS_ASSERT_SV_VSETPVF;
10941 sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
10945 Perl_sv_setpvf_mg(pTHX_ SV *const sv, const char *const pat, ...)
10949 PERL_ARGS_ASSERT_SV_SETPVF_MG;
10951 va_start(args, pat);
10952 sv_vsetpvf_mg(sv, pat, &args);
10957 Perl_sv_vsetpvf_mg(pTHX_ SV *const sv, const char *const pat, va_list *const args)
10959 PERL_ARGS_ASSERT_SV_VSETPVF_MG;
10961 sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
10965 #if defined(MULTIPLICITY)
10967 /* pTHX_ magic can't cope with varargs, so this is a no-context
10968 * version of the main function, (which may itself be aliased to us).
10969 * Don't access this version directly.
10973 Perl_sv_catpvf_nocontext(SV *const sv, const char *const pat, ...)
10978 PERL_ARGS_ASSERT_SV_CATPVF_NOCONTEXT;
10980 va_start(args, pat);
10981 sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
10985 /* pTHX_ magic can't cope with varargs, so this is a no-context
10986 * version of the main function, (which may itself be aliased to us).
10987 * Don't access this version directly.
10991 Perl_sv_catpvf_mg_nocontext(SV *const sv, const char *const pat, ...)
10996 PERL_ARGS_ASSERT_SV_CATPVF_MG_NOCONTEXT;
10998 va_start(args, pat);
10999 sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11006 =for apidoc sv_catpvf
11007 =for apidoc_item sv_catpvf_mg
11008 =for apidoc_item sv_catpvf_mg_nocontext
11009 =for apidoc_item sv_catpvf_nocontext
11011 These process their arguments like C<sprintf>, and append the formatted
11012 output to an SV. As with C<sv_vcatpvfn>, argument reordering is not supporte
11013 when called with a non-null C-style variable argument list.
11015 If the appended data contains "wide" characters
11016 (including, but not limited to, SVs with a UTF-8 PV formatted with C<%s>,
11017 and characters >255 formatted with C<%c>), the original SV might get
11020 If the original SV was UTF-8, the pattern should be
11021 valid UTF-8; if the original SV was bytes, the pattern should be too.
11023 All perform 'get' magic, but only C<sv_catpvf_mg> and C<sv_catpvf_mg_nocontext>
11024 perform 'set' magic.
11026 C<sv_catpvf_nocontext> and C<sv_catpvf_mg_nocontext> do not take a thread
11027 context (C<aTHX>) parameter, so are used in situations where the caller
11028 doesn't already have the thread context.
11034 Perl_sv_catpvf(pTHX_ SV *const sv, const char *const pat, ...)
11038 PERL_ARGS_ASSERT_SV_CATPVF;
11040 va_start(args, pat);
11041 sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11046 =for apidoc sv_vcatpvf
11047 =for apidoc_item sv_vcatpvf_mg
11049 These process their arguments like C<sv_vcatpvfn> called with a non-null
11050 C-style variable argument list, and append the formatted output to C<sv>.
11052 They differ only in that C<sv_vcatpvf_mg> performs 'set' magic;
11053 C<sv_vcatpvf> skips 'set' magic.
11055 Both perform 'get' magic.
11057 They are usually accessed via their frontends C<L</sv_catpvf>> and
11058 C<L</sv_catpvf_mg>>.
11064 Perl_sv_vcatpvf(pTHX_ SV *const sv, const char *const pat, va_list *const args)
11066 PERL_ARGS_ASSERT_SV_VCATPVF;
11068 sv_vcatpvfn_flags(sv, pat, strlen(pat), args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11072 Perl_sv_catpvf_mg(pTHX_ SV *const sv, const char *const pat, ...)
11076 PERL_ARGS_ASSERT_SV_CATPVF_MG;
11078 va_start(args, pat);
11079 sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11085 Perl_sv_vcatpvf_mg(pTHX_ SV *const sv, const char *const pat, va_list *const args)
11087 PERL_ARGS_ASSERT_SV_VCATPVF_MG;
11089 sv_vcatpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
11094 =for apidoc sv_vsetpvfn
11096 Works like C<sv_vcatpvfn> but copies the text into the SV instead of
11099 Usually used via one of its frontends L</C<sv_vsetpvf>> and
11100 L</C<sv_vsetpvf_mg>>.
11106 Perl_sv_vsetpvfn(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
11107 va_list *const args, SV **const svargs, const Size_t sv_count, bool *const maybe_tainted)
11109 PERL_ARGS_ASSERT_SV_VSETPVFN;
11112 sv_vcatpvfn_flags(sv, pat, patlen, args, svargs, sv_count, maybe_tainted, 0);
11116 /* simplified inline Perl_sv_catpvn_nomg() when you know the SV's SvPOK */
11118 PERL_STATIC_INLINE void
11119 S_sv_catpvn_simple(pTHX_ SV *const sv, const char* const buf, const STRLEN len)
11121 STRLEN const need = len + SvCUR(sv) + 1;
11124 /* can't wrap as both len and SvCUR() are allocated in
11125 * memory and together can't consume all the address space
11127 assert(need > len);
11132 Copy(buf, end, len, char);
11135 SvCUR_set(sv, need - 1);
11140 * Warn of missing argument to sprintf. The value used in place of such
11141 * arguments should be &PL_sv_no; an undefined value would yield
11142 * inappropriate "use of uninit" warnings [perl #71000].
11145 S_warn_vcatpvfn_missing_argument(pTHX) {
11146 if (ckWARN(WARN_MISSING)) {
11147 Perl_warner(aTHX_ packWARN(WARN_MISSING), "Missing argument in %s",
11148 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
11157 Perl_croak(aTHX_ "Integer overflow in format string for %s",
11158 (PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn"));
11162 /* Given an int i from the next arg (if args is true) or an sv from an arg
11163 * (if args is false), try to extract a STRLEN-ranged value from the arg,
11164 * with overflow checking.
11165 * Sets *neg to true if the value was negative (untouched otherwise.
11166 * Returns the absolute value.
11167 * As an extra margin of safety, it croaks if the returned value would
11168 * exceed the maximum value of a STRLEN / 4.
11172 S_sprintf_arg_num_val(pTHX_ va_list *const args, int i, SV *sv, bool *neg)
11186 if (UNLIKELY(SvIsUV(sv))) {
11187 UV uv = SvUV_nomg(sv);
11189 S_croak_overflow();
11193 iv = SvIV_nomg(sv);
11197 S_croak_overflow();
11203 if (iv > (IV)(((STRLEN)~0) / 4))
11204 S_croak_overflow();
11209 /* Read in and return a number. Updates *pattern to point to the char
11210 * following the number. Expects the first char to 1..9.
11211 * Croaks if the number exceeds 1/4 of the maximum value of STRLEN.
11212 * This is a belt-and-braces safety measure to complement any
11213 * overflow/wrap checks done in the main body of sv_vcatpvfn_flags.
11214 * It means that e.g. on a 32-bit system the width/precision can't be more
11215 * than 1G, which seems reasonable.
11219 S_expect_number(pTHX_ const char **const pattern)
11223 PERL_ARGS_ASSERT_EXPECT_NUMBER;
11225 assert(inRANGE(**pattern, '1', '9'));
11227 var = *(*pattern)++ - '0';
11228 while (isDIGIT(**pattern)) {
11229 /* if var * 10 + 9 would exceed 1/4 max strlen, croak */
11230 if (var > ((((STRLEN)~0) / 4 - 9) / 10))
11231 S_croak_overflow();
11232 var = var * 10 + (*(*pattern)++ - '0');
11237 /* Implement a fast "%.0f": given a pointer to the end of a buffer (caller
11238 * ensures it's big enough), back fill it with the rounded integer part of
11239 * nv. Returns ptr to start of string, and sets *len to its length.
11240 * Returns NULL if not convertible.
11244 S_F0convert(NV nv, char *const endbuf, STRLEN *const len)
11246 const int neg = nv < 0;
11249 PERL_ARGS_ASSERT_F0CONVERT;
11251 assert(!Perl_isinfnan(nv));
11254 if (nv != 0.0 && nv < (NV) UV_MAX) {
11260 if (uv & 1 && uv == nv)
11261 uv--; /* Round to even */
11264 const unsigned dig = uv % 10;
11266 } while (uv /= 10);
11276 /* XXX maybe_tainted is never assigned to, so the doc above is lying. */
11279 Perl_sv_vcatpvfn(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
11280 va_list *const args, SV **const svargs, const Size_t sv_count, bool *const maybe_tainted)
11282 PERL_ARGS_ASSERT_SV_VCATPVFN;
11284 sv_vcatpvfn_flags(sv, pat, patlen, args, svargs, sv_count, maybe_tainted, SV_GMAGIC|SV_SMAGIC);
11288 /* For the vcatpvfn code, we need a long double target in case
11289 * HAS_LONG_DOUBLE, even without USE_LONG_DOUBLE, so that we can printf
11290 * with long double formats, even without NV being long double. But we
11291 * call the target 'fv' instead of 'nv', since most of the time it is not
11292 * (most compilers these days recognize "long double", even if only as a
11293 * synonym for "double").
11295 #if defined(HAS_LONG_DOUBLE) && LONG_DOUBLESIZE > DOUBLESIZE && \
11296 defined(PERL_PRIgldbl) && !defined(USE_QUADMATH)
11297 # define VCATPVFN_FV_GF PERL_PRIgldbl
11298 # if defined(__VMS) && defined(__ia64) && defined(__IEEE_FLOAT)
11299 /* Work around breakage in OTS$CVT_FLOAT_T_X */
11300 # define VCATPVFN_NV_TO_FV(nv,fv) \
11303 fv = Perl_isnan(_dv) ? LDBL_QNAN : _dv; \
11306 # define VCATPVFN_NV_TO_FV(nv,fv) (fv)=(nv)
11308 typedef long double vcatpvfn_long_double_t;
11310 # define VCATPVFN_FV_GF NVgf
11311 # define VCATPVFN_NV_TO_FV(nv,fv) (fv)=(nv)
11312 typedef NV vcatpvfn_long_double_t;
11315 #ifdef LONGDOUBLE_DOUBLEDOUBLE
11316 /* The first double can be as large as 2**1023, or '1' x '0' x 1023.
11317 * The second double can be as small as 2**-1074, or '0' x 1073 . '1'.
11318 * The sum of them can be '1' . '0' x 2096 . '1', with implied radix point
11319 * after the first 1023 zero bits.
11321 * XXX The 2098 is quite large (262.25 bytes) and therefore some sort
11322 * of dynamically growing buffer might be better, start at just 16 bytes
11323 * (for example) and grow only when necessary. Or maybe just by looking
11324 * at the exponents of the two doubles? */
11325 # define DOUBLEDOUBLE_MAXBITS 2098
11328 /* vhex will contain the values (0..15) of the hex digits ("nybbles"
11329 * of 4 bits); 1 for the implicit 1, and the mantissa bits, four bits
11330 * per xdigit. For the double-double case, this can be rather many.
11331 * The non-double-double-long-double overshoots since all bits of NV
11332 * are not mantissa bits, there are also exponent bits. */
11333 #ifdef LONGDOUBLE_DOUBLEDOUBLE
11334 # define VHEX_SIZE (3+DOUBLEDOUBLE_MAXBITS/4)
11336 # define VHEX_SIZE (1+(NVSIZE * 8)/4)
11339 /* If we do not have a known long double format, (including not using
11340 * long doubles, or long doubles being equal to doubles) then we will
11341 * fall back to the ldexp/frexp route, with which we can retrieve at
11342 * most as many bits as our widest unsigned integer type is. We try
11343 * to get a 64-bit unsigned integer even if we are not using a 64-bit UV.
11345 * (If you want to test the case of UVSIZE == 4, NVSIZE == 8,
11346 * set the MANTISSATYPE to int and the MANTISSASIZE to 4.)
11348 #if defined(HAS_QUAD) && defined(Uquad_t)
11349 # define MANTISSATYPE Uquad_t
11350 # define MANTISSASIZE 8
11352 # define MANTISSATYPE UV
11353 # define MANTISSASIZE UVSIZE
11356 #if defined(DOUBLE_LITTLE_ENDIAN) || defined(LONGDOUBLE_LITTLE_ENDIAN)
11357 # define HEXTRACT_LITTLE_ENDIAN
11358 #elif defined(DOUBLE_BIG_ENDIAN) || defined(LONGDOUBLE_BIG_ENDIAN)
11359 # define HEXTRACT_BIG_ENDIAN
11361 # define HEXTRACT_MIX_ENDIAN
11364 /* S_hextract() is a helper for S_format_hexfp, for extracting
11365 * the hexadecimal values (for %a/%A). The nv is the NV where the value
11366 * are being extracted from (either directly from the long double in-memory
11367 * presentation, or from the uquad computed via frexp+ldexp). frexp also
11368 * is used to update the exponent. The subnormal is set to true
11369 * for IEEE 754 subnormals/denormals (including the x86 80-bit format).
11370 * The vhex is the pointer to the beginning of the output buffer of VHEX_SIZE.
11372 * The tricky part is that S_hextract() needs to be called twice:
11373 * the first time with vend as NULL, and the second time with vend as
11374 * the pointer returned by the first call. What happens is that on
11375 * the first round the output size is computed, and the intended
11376 * extraction sanity checked. On the second round the actual output
11377 * (the extraction of the hexadecimal values) takes place.
11378 * Sanity failures cause fatal failures during both rounds. */
11380 S_hextract(pTHX_ const NV nv, int* exponent, bool *subnormal,
11381 U8* vhex, U8* vend)
11385 int ixmin = 0, ixmax = 0;
11387 /* XXX Inf/NaN are not handled here, since it is
11388 * assumed they are to be output as "Inf" and "NaN". */
11390 /* These macros are just to reduce typos, they have multiple
11391 * repetitions below, but usually only one (or sometimes two)
11392 * of them is really being used. */
11393 /* HEXTRACT_OUTPUT() extracts the high nybble first. */
11394 #define HEXTRACT_OUTPUT_HI(ix) (*v++ = nvp[ix] >> 4)
11395 #define HEXTRACT_OUTPUT_LO(ix) (*v++ = nvp[ix] & 0xF)
11396 #define HEXTRACT_OUTPUT(ix) \
11398 HEXTRACT_OUTPUT_HI(ix); HEXTRACT_OUTPUT_LO(ix); \
11400 #define HEXTRACT_COUNT(ix, c) \
11402 v += c; if (ix < ixmin) ixmin = ix; else if (ix > ixmax) ixmax = ix; \
11404 #define HEXTRACT_BYTE(ix) \
11406 if (vend) HEXTRACT_OUTPUT(ix); else HEXTRACT_COUNT(ix, 2); \
11408 #define HEXTRACT_LO_NYBBLE(ix) \
11410 if (vend) HEXTRACT_OUTPUT_LO(ix); else HEXTRACT_COUNT(ix, 1); \
11412 /* HEXTRACT_TOP_NYBBLE is just convenience disguise,
11413 * to make it look less odd when the top bits of a NV
11414 * are extracted using HEXTRACT_LO_NYBBLE: the highest
11415 * order bits can be in the "low nybble" of a byte. */
11416 #define HEXTRACT_TOP_NYBBLE(ix) HEXTRACT_LO_NYBBLE(ix)
11417 #define HEXTRACT_BYTES_LE(a, b) \
11418 for (ix = a; ix >= b; ix--) { HEXTRACT_BYTE(ix); }
11419 #define HEXTRACT_BYTES_BE(a, b) \
11420 for (ix = a; ix <= b; ix++) { HEXTRACT_BYTE(ix); }
11421 #define HEXTRACT_GET_SUBNORMAL(nv) *subnormal = Perl_fp_class_denorm(nv)
11422 #define HEXTRACT_IMPLICIT_BIT(nv) \
11424 if (!*subnormal) { \
11425 if (vend) *v++ = ((nv) == 0.0) ? 0 : 1; else v++; \
11429 /* Most formats do. Those which don't should undef this.
11431 * But also note that IEEE 754 subnormals do not have it, or,
11432 * expressed alternatively, their implicit bit is zero. */
11433 #define HEXTRACT_HAS_IMPLICIT_BIT
11435 /* Many formats do. Those which don't should undef this. */
11436 #define HEXTRACT_HAS_TOP_NYBBLE
11438 /* HEXTRACTSIZE is the maximum number of xdigits. */
11439 #if defined(USE_LONG_DOUBLE) && defined(LONGDOUBLE_DOUBLEDOUBLE)
11440 # define HEXTRACTSIZE (2+DOUBLEDOUBLE_MAXBITS/4)
11442 # define HEXTRACTSIZE 2 * NVSIZE
11445 const U8* vmaxend = vhex + HEXTRACTSIZE;
11447 assert(HEXTRACTSIZE <= VHEX_SIZE);
11449 PERL_UNUSED_VAR(ix); /* might happen */
11450 (void)Perl_frexp(PERL_ABS(nv), exponent);
11451 *subnormal = FALSE;
11452 if (vend && (vend <= vhex || vend > vmaxend)) {
11453 /* diag_listed_as: Hexadecimal float: internal error (%s) */
11454 Perl_croak(aTHX_ "Hexadecimal float: internal error (entry)");
11457 /* First check if using long doubles. */
11458 #if defined(USE_LONG_DOUBLE) && (NVSIZE > DOUBLESIZE)
11459 # if LONG_DOUBLEKIND == LONG_DOUBLE_IS_IEEE_754_128_BIT_LITTLE_ENDIAN
11460 /* Used in e.g. VMS and HP-UX IA-64, e.g. -0.1L:
11461 * 9a 99 99 99 99 99 99 99 99 99 99 99 99 99 fb bf */
11462 /* The bytes 13..0 are the mantissa/fraction,
11463 * the 15,14 are the sign+exponent. */
11464 const U8* nvp = (const U8*)(&nv);
11465 HEXTRACT_GET_SUBNORMAL(nv);
11466 HEXTRACT_IMPLICIT_BIT(nv);
11467 # undef HEXTRACT_HAS_TOP_NYBBLE
11468 HEXTRACT_BYTES_LE(13, 0);
11469 # elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_IEEE_754_128_BIT_BIG_ENDIAN
11470 /* Used in e.g. Solaris Sparc and HP-UX PA-RISC, e.g. -0.1L:
11471 * bf fb 99 99 99 99 99 99 99 99 99 99 99 99 99 9a */
11472 /* The bytes 2..15 are the mantissa/fraction,
11473 * the 0,1 are the sign+exponent. */
11474 const U8* nvp = (const U8*)(&nv);
11475 HEXTRACT_GET_SUBNORMAL(nv);
11476 HEXTRACT_IMPLICIT_BIT(nv);
11477 # undef HEXTRACT_HAS_TOP_NYBBLE
11478 HEXTRACT_BYTES_BE(2, 15);
11479 # elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_LITTLE_ENDIAN
11480 /* x86 80-bit "extended precision", 64 bits of mantissa / fraction /
11481 * significand, 15 bits of exponent, 1 bit of sign. No implicit bit.
11482 * NVSIZE can be either 12 (ILP32, Solaris x86) or 16 (LP64, Linux
11483 * and OS X), meaning that 2 or 6 bytes are empty padding. */
11484 /* The bytes 0..1 are the sign+exponent,
11485 * the bytes 2..9 are the mantissa/fraction. */
11486 const U8* nvp = (const U8*)(&nv);
11487 # undef HEXTRACT_HAS_IMPLICIT_BIT
11488 # undef HEXTRACT_HAS_TOP_NYBBLE
11489 HEXTRACT_GET_SUBNORMAL(nv);
11490 HEXTRACT_BYTES_LE(7, 0);
11491 # elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_BIG_ENDIAN
11492 /* Does this format ever happen? (Wikipedia says the Motorola
11493 * 6888x math coprocessors used format _like_ this but padded
11494 * to 96 bits with 16 unused bits between the exponent and the
11496 const U8* nvp = (const U8*)(&nv);
11497 # undef HEXTRACT_HAS_IMPLICIT_BIT
11498 # undef HEXTRACT_HAS_TOP_NYBBLE
11499 HEXTRACT_GET_SUBNORMAL(nv);
11500 HEXTRACT_BYTES_BE(0, 7);
11502 # define HEXTRACT_FALLBACK
11503 /* Double-double format: two doubles next to each other.
11504 * The first double is the high-order one, exactly like
11505 * it would be for a "lone" double. The second double
11506 * is shifted down using the exponent so that that there
11507 * are no common bits. The tricky part is that the value
11508 * of the double-double is the SUM of the two doubles and
11509 * the second one can be also NEGATIVE.
11511 * Because of this tricky construction the bytewise extraction we
11512 * use for the other long double formats doesn't work, we must
11513 * extract the values bit by bit.
11515 * The little-endian double-double is used .. somewhere?
11517 * The big endian double-double is used in e.g. PPC/Power (AIX)
11520 * The mantissa bits are in two separate stretches, e.g. for -0.1L:
11521 * 9a 99 99 99 99 99 59 bc 9a 99 99 99 99 99 b9 3f (LE)
11522 * 3f b9 99 99 99 99 99 9a bc 59 99 99 99 99 99 9a (BE)
11525 #else /* #if defined(USE_LONG_DOUBLE) && (NVSIZE > DOUBLESIZE) */
11526 /* Using normal doubles, not long doubles.
11528 * We generate 4-bit xdigits (nybble/nibble) instead of 8-bit
11529 * bytes, since we might need to handle printf precision, and
11530 * also need to insert the radix. */
11532 # ifdef HEXTRACT_LITTLE_ENDIAN
11533 /* 0 1 2 3 4 5 6 7 (MSB = 7, LSB = 0, 6+7 = exponent+sign) */
11534 const U8* nvp = (const U8*)(&nv);
11535 HEXTRACT_GET_SUBNORMAL(nv);
11536 HEXTRACT_IMPLICIT_BIT(nv);
11537 HEXTRACT_TOP_NYBBLE(6);
11538 HEXTRACT_BYTES_LE(5, 0);
11539 # elif defined(HEXTRACT_BIG_ENDIAN)
11540 /* 7 6 5 4 3 2 1 0 (MSB = 7, LSB = 0, 6+7 = exponent+sign) */
11541 const U8* nvp = (const U8*)(&nv);
11542 HEXTRACT_GET_SUBNORMAL(nv);
11543 HEXTRACT_IMPLICIT_BIT(nv);
11544 HEXTRACT_TOP_NYBBLE(1);
11545 HEXTRACT_BYTES_BE(2, 7);
11546 # elif DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_MIXED_ENDIAN_LE_BE
11547 /* 4 5 6 7 0 1 2 3 (MSB = 7, LSB = 0, 6:7 = nybble:exponent:sign) */
11548 const U8* nvp = (const U8*)(&nv);
11549 HEXTRACT_GET_SUBNORMAL(nv);
11550 HEXTRACT_IMPLICIT_BIT(nv);
11551 HEXTRACT_TOP_NYBBLE(2); /* 6 */
11552 HEXTRACT_BYTE(1); /* 5 */
11553 HEXTRACT_BYTE(0); /* 4 */
11554 HEXTRACT_BYTE(7); /* 3 */
11555 HEXTRACT_BYTE(6); /* 2 */
11556 HEXTRACT_BYTE(5); /* 1 */
11557 HEXTRACT_BYTE(4); /* 0 */
11558 # elif DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_MIXED_ENDIAN_BE_LE
11559 /* 3 2 1 0 7 6 5 4 (MSB = 7, LSB = 0, 7:6 = sign:exponent:nybble) */
11560 const U8* nvp = (const U8*)(&nv);
11561 HEXTRACT_GET_SUBNORMAL(nv);
11562 HEXTRACT_IMPLICIT_BIT(nv);
11563 HEXTRACT_TOP_NYBBLE(5); /* 6 */
11564 HEXTRACT_BYTE(6); /* 5 */
11565 HEXTRACT_BYTE(7); /* 4 */
11566 HEXTRACT_BYTE(0); /* 3 */
11567 HEXTRACT_BYTE(1); /* 2 */
11568 HEXTRACT_BYTE(2); /* 1 */
11569 HEXTRACT_BYTE(3); /* 0 */
11571 # define HEXTRACT_FALLBACK
11574 # define HEXTRACT_FALLBACK
11576 #endif /* #if defined(USE_LONG_DOUBLE) && (NVSIZE > DOUBLESIZE) #else */
11578 #ifdef HEXTRACT_FALLBACK
11579 HEXTRACT_GET_SUBNORMAL(nv);
11580 # undef HEXTRACT_HAS_TOP_NYBBLE /* Meaningless, but consistent. */
11581 /* The fallback is used for the double-double format, and
11582 * for unknown long double formats, and for unknown double
11583 * formats, or in general unknown NV formats. */
11584 if (nv == (NV)0.0) {
11592 NV d = nv < 0 ? -nv : nv;
11594 U8 ha = 0x0; /* hexvalue accumulator */
11595 U8 hd = 0x8; /* hexvalue digit */
11597 /* Shift d and e (and update exponent) so that e <= d < 2*e,
11598 * this is essentially manual frexp(). Multiplying by 0.5 and
11599 * doubling should be lossless in binary floating point. */
11609 while (d >= e + e) {
11613 /* Now e <= d < 2*e */
11615 /* First extract the leading hexdigit (the implicit bit). */
11631 /* Then extract the remaining hexdigits. */
11632 while (d > (NV)0.0) {
11638 /* Output or count in groups of four bits,
11639 * that is, when the hexdigit is down to one. */
11644 /* Reset the hexvalue. */
11653 /* Flush possible pending hexvalue. */
11663 /* Croak for various reasons: if the output pointer escaped the
11664 * output buffer, if the extraction index escaped the extraction
11665 * buffer, or if the ending output pointer didn't match the
11666 * previously computed value. */
11667 if (v <= vhex || v - vhex >= VHEX_SIZE ||
11668 /* For double-double the ixmin and ixmax stay at zero,
11669 * which is convenient since the HEXTRACTSIZE is tricky
11670 * for double-double. */
11671 ixmin < 0 || ixmax >= NVSIZE ||
11672 (vend && v != vend)) {
11673 /* diag_listed_as: Hexadecimal float: internal error (%s) */
11674 Perl_croak(aTHX_ "Hexadecimal float: internal error (overflow)");
11680 /* S_format_hexfp(): helper function for Perl_sv_vcatpvfn_flags().
11682 * Processes the %a/%A hexadecimal floating-point format, since the
11683 * built-in snprintf()s which are used for most of the f/p formats, don't
11684 * universally handle %a/%A.
11685 * Populates buf of length bufsize, and returns the length of the created
11687 * The rest of the args have the same meaning as the local vars of the
11688 * same name within Perl_sv_vcatpvfn_flags().
11690 * The caller's determination of IN_LC(LC_NUMERIC), passed as in_lc_numeric,
11691 * is used to ensure we do the right thing when we need to access the locale's
11694 * It requires the caller to make buf large enough.
11698 S_format_hexfp(pTHX_ char * const buf, const STRLEN bufsize, const char c,
11699 const NV nv, const vcatpvfn_long_double_t fv,
11700 bool has_precis, STRLEN precis, STRLEN width,
11701 bool alt, char plus, bool left, bool fill, bool in_lc_numeric)
11703 /* Hexadecimal floating point. */
11705 U8 vhex[VHEX_SIZE];
11706 U8* v = vhex; /* working pointer to vhex */
11707 U8* vend; /* pointer to one beyond last digit of vhex */
11708 U8* vfnz = NULL; /* first non-zero */
11709 U8* vlnz = NULL; /* last non-zero */
11710 U8* v0 = NULL; /* first output */
11711 const bool lower = (c == 'a');
11712 /* At output the values of vhex (up to vend) will
11713 * be mapped through the xdig to get the actual
11714 * human-readable xdigits. */
11715 const char* xdig = PL_hexdigit;
11716 STRLEN zerotail = 0; /* how many extra zeros to append */
11717 int exponent = 0; /* exponent of the floating point input */
11718 bool hexradix = FALSE; /* should we output the radix */
11719 bool subnormal = FALSE; /* IEEE 754 subnormal/denormal */
11720 bool negative = FALSE;
11723 /* XXX: NaN, Inf -- though they are printed as "NaN" and "Inf".
11725 * For example with denormals, (assuming the vanilla
11726 * 64-bit double): the exponent is zero. 1xp-1074 is
11727 * the smallest denormal and the smallest double, it
11728 * could be output also as 0x0.0000000000001p-1022 to
11729 * match its internal structure. */
11731 vend = S_hextract(aTHX_ nv, &exponent, &subnormal, vhex, NULL);
11732 S_hextract(aTHX_ nv, &exponent, &subnormal, vhex, vend);
11734 #if NVSIZE > DOUBLESIZE
11735 # ifdef HEXTRACT_HAS_IMPLICIT_BIT
11736 /* In this case there is an implicit bit,
11737 * and therefore the exponent is shifted by one. */
11739 # elif defined(NV_X86_80_BIT)
11741 /* The subnormals of the x86-80 have a base exponent of -16382,
11742 * (while the physical exponent bits are zero) but the frexp()
11743 * returned the scientific-style floating exponent. We want
11744 * to map the last one as:
11745 * -16831..-16384 -> -16382 (the last normal is 0x1p-16382)
11746 * -16835..-16388 -> -16384
11747 * since we want to keep the first hexdigit
11748 * as one of the [8421]. */
11749 exponent = -4 * ( (exponent + 1) / -4) - 2;
11753 /* TBD: other non-implicit-bit platforms than the x86-80. */
11757 negative = fv < 0 || Perl_signbit(nv);
11768 xdig += 16; /* Use uppercase hex. */
11771 /* Find the first non-zero xdigit. */
11772 for (v = vhex; v < vend; v++) {
11780 /* Find the last non-zero xdigit. */
11781 for (v = vend - 1; v >= vhex; v--) {
11788 #if NVSIZE == DOUBLESIZE
11794 #ifndef NV_X86_80_BIT
11796 /* IEEE 754 subnormals (but not the x86 80-bit):
11797 * we want "normalize" the subnormal,
11798 * so we need to right shift the hex nybbles
11799 * so that the output of the subnormal starts
11800 * from the first true bit. (Another, equally
11801 * valid, policy would be to dump the subnormal
11802 * nybbles as-is, to display the "physical" layout.) */
11805 /* Find the ceil(log2(v[0])) of
11806 * the top non-zero nybble. */
11807 for (i = vfnz[0], n = 0; i > 1; i >>= 1, n++) { }
11811 for (vshr = vlnz; vshr >= vfnz; vshr--) {
11812 vshr[1] |= (vshr[0] & (0xF >> (4 - n))) << (4 - n);
11826 U8* ve = (subnormal ? vlnz + 1 : vend);
11827 SSize_t vn = ve - v0;
11829 if (precis < (Size_t)(vn - 1)) {
11830 bool overflow = FALSE;
11831 if (v0[precis + 1] < 0x8) {
11832 /* Round down, nothing to do. */
11833 } else if (v0[precis + 1] > 0x8) {
11836 overflow = v0[precis] > 0xF;
11838 } else { /* v0[precis] == 0x8 */
11839 /* Half-point: round towards the one
11840 * with the even least-significant digit:
11848 * 78 -> 8 f8 -> 10 */
11849 if ((v0[precis] & 0x1)) {
11852 overflow = v0[precis] > 0xF;
11857 for (v = v0 + precis - 1; v >= v0; v--) {
11859 overflow = *v > 0xF;
11865 if (v == v0 - 1 && overflow) {
11866 /* If the overflow goes all the
11867 * way to the front, we need to
11868 * insert 0x1 in front, and adjust
11870 Move(v0, v0 + 1, vn - 1, char);
11876 /* The new effective "last non zero". */
11877 vlnz = v0 + precis;
11881 subnormal ? precis - vn + 1 :
11882 precis - (vlnz - vhex);
11889 /* If there are non-zero xdigits, the radix
11890 * is output after the first one. */
11898 zerotail = has_precis ? precis : 0;
11901 /* The radix is always output if precis, or if alt. */
11902 if ((has_precis && precis > 0) || alt) {
11907 #ifndef USE_LOCALE_NUMERIC
11908 PERL_UNUSED_ARG(in_lc_numeric);
11912 if (in_lc_numeric) {
11914 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(TRUE, {
11915 const char* r = SvPV(PL_numeric_radix_sv, n);
11916 Copy(r, p, n, char);
11931 if (zerotail > 0) {
11932 while (zerotail--) {
11939 /* sanity checks */
11940 if (elen >= bufsize || width >= bufsize)
11941 /* diag_listed_as: Hexadecimal float: internal error (%s) */
11942 Perl_croak(aTHX_ "Hexadecimal float: internal error (overflow)");
11944 elen += my_snprintf(p, bufsize - elen,
11945 "%c%+d", lower ? 'p' : 'P',
11948 if (elen < width) {
11949 STRLEN gap = (STRLEN)(width - elen);
11951 /* Pad the back with spaces. */
11952 memset(buf + elen, ' ', gap);
11955 /* Insert the zeros after the "0x" and the
11956 * the potential sign, but before the digits,
11957 * otherwise we end up with "0000xH.HHH...",
11958 * when we want "0x000H.HHH..." */
11959 STRLEN nzero = gap;
11960 char* zerox = buf + 2;
11961 STRLEN nmove = elen - 2;
11962 if (negative || plus) {
11966 Move(zerox, zerox + nzero, nmove, char);
11967 memset(zerox, fill ? '0' : ' ', nzero);
11970 /* Move it to the right. */
11971 Move(buf, buf + gap,
11973 /* Pad the front with spaces. */
11974 memset(buf, ' ', gap);
11982 =for apidoc sv_vcatpvfn
11983 =for apidoc_item sv_vcatpvfn_flags
11985 These process their arguments like C<L<vsprintf(3)>> and append the formatted output
11986 to an SV. They use an array of SVs if the C-style variable argument list is
11987 missing (C<NULL>). Argument reordering (using format specifiers like C<%2$d> or
11988 C<%*2$d>) is supported only when using an array of SVs; using a C-style
11989 C<va_list> argument list with a format string that uses argument reordering
11990 will yield an exception.
11992 When running with taint checks enabled, they indicate via C<maybe_tainted> if
11993 results are untrustworthy (often due to the use of locales).
11995 They assume that C<pat> has the same utf8-ness as C<sv>. It's the caller's
11996 responsibility to ensure that this is so.
11998 They differ in that C<sv_vcatpvfn_flags> has a C<flags> parameter in which you
11999 can set or clear the C<SV_GMAGIC> and/or S<SV_SMAGIC> flags, to specify which
12000 magic to handle or not handle; whereas plain C<sv_vcatpvfn> always specifies
12001 both 'get' and 'set' magic.
12003 They are usually used via one of the frontends L</C<sv_vcatpvf>> and
12004 L</C<sv_vcatpvf_mg>>.
12011 Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
12012 va_list *const args, SV **const svargs, const Size_t sv_count, bool *const maybe_tainted,
12015 const char *fmtstart; /* character following the current '%' */
12016 const char *q; /* current position within format */
12017 const char *patend;
12020 static const char nullstr[] = "(null)";
12021 bool has_utf8 = DO_UTF8(sv); /* has the result utf8? */
12022 const bool pat_utf8 = has_utf8; /* the pattern is in utf8? */
12023 /* Times 4: a decimal digit takes more than 3 binary digits.
12024 * NV_DIG: mantissa takes that many decimal digits.
12025 * Plus 32: Playing safe. */
12026 char ebuf[IV_DIG * 4 + NV_DIG + 32];
12027 bool no_redundant_warning = FALSE; /* did we use any explicit format parameter index? */
12028 #ifdef USE_LOCALE_NUMERIC
12029 bool have_in_lc_numeric = FALSE;
12031 /* we never change this unless USE_LOCALE_NUMERIC */
12032 bool in_lc_numeric = FALSE;
12035 PERL_ARGS_ASSERT_SV_VCATPVFN_FLAGS;
12036 PERL_UNUSED_ARG(maybe_tainted);
12038 if (flags & SV_GMAGIC)
12041 /* no matter what, this is a string now */
12042 (void)SvPV_force_nomg(sv, origlen);
12044 /* the code that scans for flags etc following a % relies on
12045 * a '\0' being present to avoid falling off the end. Ideally that
12046 * should be fixed */
12047 assert(pat[patlen] == '\0');
12050 /* Special-case "", "%s", "%-p" (SVf - see below) and "%.0f".
12051 * In each case, if there isn't the correct number of args, instead
12052 * fall through to the main code to handle the issuing of any
12056 if (patlen == 0 && (args || sv_count == 0))
12059 if (patlen <= 4 && pat[0] == '%' && (args || sv_count == 1)) {
12062 if (patlen == 2 && pat[1] == 's') {
12064 const char * const s = va_arg(*args, char*);
12065 sv_catpv_nomg(sv, s ? s : nullstr);
12068 /* we want get magic on the source but not the target.
12069 * sv_catsv can't do that, though */
12070 SvGETMAGIC(*svargs);
12071 sv_catsv_nomg(sv, *svargs);
12078 if (patlen == 3 && pat[1] == '-' && pat[2] == 'p') {
12079 SV *asv = MUTABLE_SV(va_arg(*args, void*));
12080 sv_catsv_nomg(sv, asv);
12084 #if !defined(USE_LONG_DOUBLE) && !defined(USE_QUADMATH)
12085 /* special-case "%.0f" */
12086 else if ( patlen == 4
12087 && pat[1] == '.' && pat[2] == '0' && pat[3] == 'f')
12089 const NV nv = SvNV(*svargs);
12090 if (LIKELY(!Perl_isinfnan(nv))) {
12094 if ((p = F0convert(nv, ebuf + sizeof ebuf, &l))) {
12095 sv_catpvn_nomg(sv, p, l);
12100 #endif /* !USE_LONG_DOUBLE */
12104 patend = (char*)pat + patlen;
12105 for (fmtstart = pat; fmtstart < patend; fmtstart = q) {
12106 char intsize = 0; /* size qualifier in "%hi..." etc */
12107 bool alt = FALSE; /* has "%#..." */
12108 bool left = FALSE; /* has "%-..." */
12109 bool fill = FALSE; /* has "%0..." */
12110 char plus = 0; /* has "%+..." */
12111 STRLEN width = 0; /* value of "%NNN..." */
12112 bool has_precis = FALSE; /* has "%.NNN..." */
12113 STRLEN precis = 0; /* value of "%.NNN..." */
12114 int base = 0; /* base to print in, e.g. 8 for %o */
12115 UV uv = 0; /* the value to print of int-ish args */
12117 bool vectorize = FALSE; /* has "%v..." */
12118 bool vec_utf8 = FALSE; /* SvUTF8(vec arg) */
12119 const U8 *vecstr = NULL; /* SvPVX(vec arg) */
12120 STRLEN veclen = 0; /* SvCUR(vec arg) */
12121 const char *dotstr = NULL; /* separator string for %v */
12122 STRLEN dotstrlen; /* length of separator string for %v */
12124 Size_t efix = 0; /* explicit format parameter index */
12125 const Size_t osvix = svix; /* original index in case of bad fmt */
12128 bool is_utf8 = FALSE; /* is this item utf8? */
12129 bool arg_missing = FALSE; /* give "Missing argument" warning */
12130 char esignbuf[4]; /* holds sign prefix, e.g. "-0x" */
12131 STRLEN esignlen = 0; /* length of e.g. "-0x" */
12132 STRLEN zeros = 0; /* how many '0' to prepend */
12134 const char *eptr = NULL; /* the address of the element string */
12135 STRLEN elen = 0; /* the length of the element string */
12137 char c; /* the actual format ('d', s' etc) */
12139 bool escape_it = FALSE; /* if this is a string should we quote and escape it? */
12142 /* echo everything up to the next format specification */
12143 for (q = fmtstart; q < patend && *q != '%'; ++q)
12146 if (q > fmtstart) {
12147 if (has_utf8 && !pat_utf8) {
12148 /* upgrade and copy the bytes of fmtstart..q-1 to utf8 on
12152 STRLEN need = SvCUR(sv) + (q - fmtstart) + 1;
12154 for (p = fmtstart; p < q; p++)
12155 if (!NATIVE_BYTE_IS_INVARIANT(*p))
12160 for (p = fmtstart; p < q; p++)
12161 append_utf8_from_native_byte((U8)*p, (U8**)&dst);
12163 SvCUR_set(sv, need - 1);
12166 S_sv_catpvn_simple(aTHX_ sv, fmtstart, q - fmtstart);
12171 fmtstart = q; /* fmtstart is char following the '%' */
12174 We allow format specification elements in this order:
12175 \d+\$ explicit format parameter index
12177 v|\*(\d+\$)?v vector with optional (optionally specified) arg
12178 0 flag (as above): repeated to allow "v02"
12179 \d+|\*(\d+\$)? width using optional (optionally specified) arg
12180 \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg
12182 [%bcdefginopsuxDFOUX] format (mandatory)
12185 if (inRANGE(*q, '1', '9')) {
12186 width = expect_number(&q);
12189 Perl_croak_nocontext(
12190 "Cannot yet reorder sv_vcatpvfn() arguments from va_list");
12192 efix = (Size_t)width;
12194 no_redundant_warning = TRUE;
12206 if (plus == '+' && *q == ' ') /* '+' over ' ' */
12233 /* at this point we can expect one of:
12235 * 123 an explicit width
12236 * * width taken from next arg
12237 * *12$ width taken from 12th arg
12240 * But any width specification may be preceded by a v, in one of its
12245 * So an asterisk may be either a width specifier or a vector
12246 * separator arg specifier, and we don't know which initially
12251 STRLEN ix; /* explicit width/vector separator index */
12253 if (inRANGE(*q, '1', '9')) {
12254 ix = expect_number(&q);
12257 Perl_croak_nocontext(
12258 "Cannot yet reorder sv_vcatpvfn() arguments from va_list");
12259 no_redundant_warning = TRUE;
12268 /* The asterisk was for *v, *NNN$v: vectorizing, but not
12269 * with the default "." */
12274 vecsv = va_arg(*args, SV*);
12276 ix = ix ? ix - 1 : svix++;
12277 vecsv = ix < sv_count ? svargs[ix]
12278 : (arg_missing = TRUE, &PL_sv_no);
12280 dotstr = SvPV_const(vecsv, dotstrlen);
12281 /* Keep the DO_UTF8 test *after* the SvPV call, else things go
12282 bad with tied or overloaded values that return UTF8. */
12283 if (DO_UTF8(vecsv))
12285 else if (has_utf8) {
12286 vecsv = sv_mortalcopy(vecsv);
12287 sv_utf8_upgrade(vecsv);
12288 dotstr = SvPV_const(vecsv, dotstrlen);
12295 /* the asterisk specified a width */
12298 SV *width_sv = NULL;
12300 i = va_arg(*args, int);
12302 ix = ix ? ix - 1 : svix++;
12303 width_sv = (ix < sv_count) ? svargs[ix]
12304 : (arg_missing = TRUE, (SV*)NULL);
12306 width = S_sprintf_arg_num_val(aTHX_ args, i, width_sv, &left);
12309 else if (*q == 'v') {
12320 /* explicit width? */
12325 if (inRANGE(*q, '1', '9'))
12326 width = expect_number(&q);
12336 STRLEN ix; /* explicit precision index */
12338 if (inRANGE(*q, '1', '9')) {
12339 ix = expect_number(&q);
12342 Perl_croak_nocontext(
12343 "Cannot yet reorder sv_vcatpvfn() arguments from va_list");
12344 no_redundant_warning = TRUE;
12353 SV *width_sv = NULL;
12357 i = va_arg(*args, int);
12359 ix = ix ? ix - 1 : svix++;
12360 width_sv = (ix < sv_count) ? svargs[ix]
12361 : (arg_missing = TRUE, (SV*)NULL);
12363 precis = S_sprintf_arg_num_val(aTHX_ args, i, width_sv, &neg);
12365 /* ignore negative precision */
12371 /* although it doesn't seem documented, this code has long
12373 * no digits following the '.' is treated like '.0'
12374 * the number may be preceded by any number of zeroes,
12375 * e.g. "%.0001f", which is the same as "%.1f"
12376 * so I've kept that behaviour. DAPM May 2017
12380 precis = inRANGE(*q, '1', '9') ? expect_number(&q) : 0;
12389 case 'I': /* Ix, I32x, and I64x */
12390 # ifdef USE_64_BIT_INT
12391 if (q[1] == '6' && q[2] == '4') {
12397 if (q[1] == '3' && q[2] == '2') {
12401 # ifdef USE_64_BIT_INT
12407 #if (IVSIZE >= 8 || defined(HAS_LONG_DOUBLE)) || \
12408 (IVSIZE == 4 && !defined(HAS_LONG_DOUBLE))
12420 #if (IVSIZE >= 8 || defined(HAS_LONG_DOUBLE)) || \
12421 (IVSIZE == 4 && !defined(HAS_LONG_DOUBLE))
12422 if (*q == 'l') { /* lld, llf */
12431 if (*++q == 'h') { /* hhd, hhu */
12438 #ifdef USE_QUADMATH
12451 c = *q++; /* c now holds the conversion type */
12453 /* '%' doesn't have an arg, so skip arg processing */
12462 if (vectorize && !memCHRs("BbDdiOouUXx", c))
12465 /* get next arg (individual branches do their own va_arg()
12466 * handling for the args case) */
12469 efix = efix ? efix - 1 : svix++;
12470 argsv = efix < sv_count ? svargs[efix]
12471 : (arg_missing = TRUE, &PL_sv_no);
12481 eptr = va_arg(*args, char*);
12484 elen = my_strnlen(eptr, precis);
12486 elen = strlen(eptr);
12488 eptr = (char *)nullstr;
12489 elen = sizeof nullstr - 1;
12493 eptr = SvPV_const(argsv, elen);
12494 if (DO_UTF8(argsv)) {
12495 STRLEN old_precis = precis;
12496 if (has_precis && precis < elen) {
12497 STRLEN ulen = sv_or_pv_len_utf8(argsv, eptr, elen);
12498 STRLEN p = precis > ulen ? ulen : precis;
12499 precis = sv_or_pv_pos_u2b(argsv, eptr, p, 0);
12500 /* sticks at end */
12502 if (width) { /* fudge width (can't fudge elen) */
12503 if (has_precis && precis < elen)
12504 width += precis - old_precis;
12507 elen - sv_or_pv_len_utf8(argsv,eptr,elen);
12515 U32 flags = PERL_PV_PRETTY_QUOTEDPREFIX;
12517 flags |= PERL_PV_ESCAPE_UNI;
12520 /* "blah"... where blah might be made up
12521 * of characters like \x{1234} */
12522 tmp_sv = newSV(1 + (PERL_QUOTEDPREFIX_LEN * 8) + 1 + 3);
12523 sv_2mortal(tmp_sv);
12525 pv_pretty(tmp_sv, eptr, elen, PERL_QUOTEDPREFIX_LEN,
12526 NULL, NULL, flags);
12527 eptr = SvPV_const(tmp_sv, elen);
12529 if (has_precis && precis < elen)
12539 * We want to extend the C level sprintf format API with
12540 * custom formats for specific types (eg SV*) and behavior.
12541 * However some C compilers are "sprintf aware" and will
12542 * throw compile time exceptions when an illegal sprintf is
12543 * encountered, so we can't just add new format letters.
12545 * However it turns out the length argument to the %p format
12546 * is more or less useless (the size of a pointer does not
12547 * change over time) and is not really used in the C level
12548 * code. Accordingly we can map our special behavior to
12549 * specific "length" options to the %p format. We hide these
12550 * mappings behind defines anyway, so nobody needs to know
12551 * that HEKf is actually %2p. This keeps the C compiler
12552 * happy while allowing us to add new formats.
12554 * Note the existing logic for which number is used for what
12555 * is torturous. All negative values are used for SVf, and
12556 * non-negative values have arbitrary meanings with no
12557 * structure to them. This may change in the future.
12559 * NEVER use the raw %p values directly. Always use the define
12560 * as the underlying mapping may change in the future.
12566 * "%...p" is normally treated like "%...x", except that the
12567 * number to print is the SV's address (or a pointer address
12568 * for C-ish sprintf).
12570 * However, the C-ish sprintf variant allows a few special
12571 * extensions. These are currently:
12573 * %-p (SVf) Like %s, but gets the string from an SV*
12574 * arg rather than a char* arg. Use C<SVfARG()>
12575 * to set up the argument properly.
12576 * (This was previously %_).
12578 * %-<num>p Ditto but like %.<num>s (i.e. num is max
12579 * width), there is no escaped and quoted version
12582 * %1p (PVf_QUOTEDPREFIX). Like raw %s, but it is escaped
12585 * %5p (SVf_QUOTEDPREFIX) Like SVf, but length restricted,
12586 * escaped and quoted with pv_pretty. Intended
12587 * for error messages.
12589 * %2p (HEKf) Like %s, but using the key string in a HEK
12590 * %7p (HEKf_QUOTEDPREFIX) ... but escaped and quoted.
12592 * %3p (HEKf256) Ditto but like %.256s
12593 * %8p (HEKf256_QUOTEDPREFIX) ... but escaped and quoted
12595 * %d%lu%4p (UTF8f) A utf8 string. Consumes 3 args:
12596 * (cBOOL(utf8), len, string_buf).
12597 * It's handled by the "case 'd'" branch
12598 * rather than here.
12599 * %d%lu%9p (UTF8f_QUOTEDPREFIX) .. but escaped and quoted.
12602 * %<num>p where num is > 9: reserved for future
12603 * extensions. Warns, but then is treated as a
12604 * general %p (print hex address) format.
12606 * NOTE: If you add a new magic %p value you will
12607 * need to update F<t/porting/diag.t> to be aware of it
12608 * on top of adding the various defines and etc. Do not
12609 * forget to add it to F<pod/perlguts.pod> as well.
12617 /* not %*p or %*1$p - any width was explicit */
12621 if (left || width == 5) { /* %-p (SVf), %-NNNp, %5p */
12622 if (left && width) {
12625 } else if (width == 5) {
12628 argsv = MUTABLE_SV(va_arg(*args, void*));
12629 eptr = SvPV_const(argsv, elen);
12630 if (DO_UTF8(argsv))
12635 else if (width == 2 || width == 3 ||
12636 width == 7 || width == 8)
12637 { /* HEKf, HEKf256, HEKf_QUOTEDPREFIX, HEKf256_QUOTEDPREFIX */
12638 HEK * const hek = va_arg(*args, HEK *);
12639 eptr = HEK_KEY(hek);
12640 elen = HEK_LEN(hek);
12652 else if (width == 1) {
12653 eptr = va_arg(*args,char *);
12654 elen = strlen(eptr);
12660 /* note width=4 or width=9 is handled under %d */
12661 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
12662 "internal %%<num>p might conflict with future printf extensions");
12666 /* treat as normal %...p */
12668 uv = PTR2UV(args ? va_arg(*args, void*) : argsv);
12670 c = 'x'; /* in case the format string contains '#' */
12674 /* Ignore any size specifiers, since they're not documented as
12675 * being allowed for %c (ideally we should warn on e.g. '%hc').
12676 * Setting a default intsize, along with a positive
12677 * (which signals unsigned) base, causes, for C-ish use, the
12678 * va_arg to be interpreted as an unsigned int, when it's
12679 * actually signed, which will convert -ve values to high +ve
12680 * values. Note that unlike the libc %c, values > 255 will
12681 * convert to high unicode points rather than being truncated
12682 * to 8 bits. For perlish use, it will do SvUV(argsv), which
12683 * will again convert -ve args to high -ve values.
12686 base = 1; /* special value that indicates we're doing a 'c' */
12687 goto get_int_arg_val;
12696 goto get_int_arg_val;
12699 /* probably just a plain %d, but it might be the start of the
12700 * special UTF8f format, which usually looks something like
12701 * "%d%lu%4p" (the lu may vary by platform) or
12702 * "%d%lu%9p" for an escaped version.
12704 assert((UTF8f)[0] == 'd');
12705 assert((UTF8f)[1] == '%');
12707 if ( args /* UTF8f only valid for C-ish sprintf */
12708 && q == fmtstart + 1 /* plain %d, not %....d */
12709 && patend >= fmtstart + sizeof(UTF8f) - 1 /* long enough */
12711 && strnEQ(q + 1, (UTF8f) + 2, sizeof(UTF8f) - 5)
12712 && q[sizeof(UTF8f)-3] == 'p'
12713 && (q[sizeof(UTF8f)-4] == '4' ||
12714 q[sizeof(UTF8f)-4] == '9'))
12716 /* The argument has already gone through cBOOL, so the cast
12718 if (q[sizeof(UTF8f)-4] == '9')
12720 is_utf8 = (bool)va_arg(*args, int);
12721 elen = va_arg(*args, UV);
12722 /* if utf8 length is larger than 0x7ffff..., then it might
12723 * have been a signed value that wrapped */
12724 if (elen > ((~(STRLEN)0) >> 1)) {
12725 assert(0); /* in DEBUGGING build we want to crash */
12726 elen = 0; /* otherwise we want to treat this as an empty string */
12728 eptr = va_arg(*args, char *);
12729 q += sizeof(UTF8f) - 2;
12736 goto get_int_arg_val;
12747 goto get_int_arg_val;
12752 goto get_int_arg_val;
12763 goto get_int_arg_val;
12778 esignbuf[esignlen++] = plus;
12781 /* initialise the vector string to iterate over */
12783 vecsv = args ? va_arg(*args, SV*) : argsv;
12785 /* if this is a version object, we need to convert
12786 * back into v-string notation and then let the
12787 * vectorize happen normally
12789 if (sv_isobject(vecsv) && sv_derived_from(vecsv, "version")) {
12790 if ( hv_existss(MUTABLE_HV(SvRV(vecsv)), "alpha") ) {
12791 Perl_ck_warner_d(aTHX_ packWARN(WARN_PRINTF),
12792 "vector argument not supported with alpha versions");
12796 vecstr = (U8*)SvPV_const(vecsv,veclen);
12797 vecsv = sv_newmortal();
12798 scan_vstring((char *)vecstr, (char *)vecstr + veclen,
12802 vecstr = (U8*)SvPV_const(vecsv, veclen);
12803 vec_utf8 = DO_UTF8(vecsv);
12805 /* This is the re-entry point for when we're iterating
12806 * over the individual characters of a vector arg */
12809 goto done_valid_conversion;
12811 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
12821 /* test arg for inf/nan. This can trigger an unwanted
12822 * 'str' overload, so manually force 'num' overload first
12826 if (UNLIKELY(SvAMAGIC(argsv)))
12827 argsv = sv_2num(argsv);
12828 if (UNLIKELY(isinfnansv(argsv)))
12829 goto handle_infnan_argsv;
12833 /* signed int type */
12838 case 'c': iv = (char)va_arg(*args, int); break;
12839 case 'h': iv = (short)va_arg(*args, int); break;
12840 case 'l': iv = va_arg(*args, long); break;
12841 case 'V': iv = va_arg(*args, IV); break;
12842 case 'z': iv = va_arg(*args, SSize_t); break;
12843 #ifdef HAS_PTRDIFF_T
12844 case 't': iv = va_arg(*args, ptrdiff_t); break;
12846 default: iv = va_arg(*args, int); break;
12847 case 'j': iv = (IV) va_arg(*args, PERL_INTMAX_T); break;
12850 iv = va_arg(*args, Quad_t); break;
12857 /* assign to tiv then cast to iv to work around
12858 * 2003 GCC cast bug (gnu.org bugzilla #13488) */
12859 IV tiv = SvIV_nomg(argsv);
12861 case 'c': iv = (char)tiv; break;
12862 case 'h': iv = (short)tiv; break;
12863 case 'l': iv = (long)tiv; break;
12865 default: iv = tiv; break;
12868 iv = (Quad_t)tiv; break;
12875 /* now convert iv to uv */
12879 esignbuf[esignlen++] = plus;
12882 /* Using 0- here to silence bogus warning from MS VC */
12883 uv = (UV) (0 - (UV) iv);
12884 esignbuf[esignlen++] = '-';
12888 /* unsigned int type */
12891 case 'c': uv = (unsigned char)va_arg(*args, unsigned);
12893 case 'h': uv = (unsigned short)va_arg(*args, unsigned);
12895 case 'l': uv = va_arg(*args, unsigned long); break;
12896 case 'V': uv = va_arg(*args, UV); break;
12897 case 'z': uv = va_arg(*args, Size_t); break;
12898 #ifdef HAS_PTRDIFF_T
12899 /* will sign extend, but there is no
12900 * uptrdiff_t, so oh well */
12901 case 't': uv = va_arg(*args, ptrdiff_t); break;
12903 case 'j': uv = (UV) va_arg(*args, PERL_UINTMAX_T); break;
12904 default: uv = va_arg(*args, unsigned); break;
12907 uv = va_arg(*args, Uquad_t); break;
12914 /* assign to tiv then cast to iv to work around
12915 * 2003 GCC cast bug (gnu.org bugzilla #13488) */
12916 UV tuv = SvUV_nomg(argsv);
12918 case 'c': uv = (unsigned char)tuv; break;
12919 case 'h': uv = (unsigned short)tuv; break;
12920 case 'l': uv = (unsigned long)tuv; break;
12922 default: uv = tuv; break;
12925 uv = (Uquad_t)tuv; break;
12936 char *ptr = ebuf + sizeof ebuf;
12943 const char * const p =
12944 (c == 'X') ? PL_hexdigit + 16 : PL_hexdigit;
12949 } while (uv >>= 4);
12950 if (alt && *ptr != '0') {
12951 esignbuf[esignlen++] = '0';
12952 esignbuf[esignlen++] = c; /* 'x' or 'X' */
12959 *--ptr = '0' + dig;
12960 } while (uv >>= 3);
12961 if (alt && *ptr != '0')
12967 *--ptr = '0' + dig;
12968 } while (uv >>= 1);
12969 if (alt && *ptr != '0') {
12970 esignbuf[esignlen++] = '0';
12971 esignbuf[esignlen++] = c; /* 'b' or 'B' */
12976 /* special-case: base 1 indicates a 'c' format:
12977 * we use the common code for extracting a uv,
12978 * but handle that value differently here than
12979 * all the other int types */
12981 (!UVCHR_IS_INVARIANT(uv) && SvUTF8(sv)))
12984 STATIC_ASSERT_STMT(sizeof(ebuf) >= UTF8_MAXBYTES + 1);
12986 elen = uvchr_to_utf8((U8*)eptr, uv) - (U8*)ebuf;
12991 ebuf[0] = (char)uv;
12996 default: /* it had better be ten or less */
12999 *--ptr = '0' + dig;
13000 } while (uv /= base);
13003 elen = (ebuf + sizeof ebuf) - ptr;
13007 zeros = precis - elen;
13008 else if (precis == 0 && elen == 1 && *eptr == '0'
13009 && !(base == 8 && alt)) /* "%#.0o" prints "0" */
13012 /* a precision nullifies the 0 flag. */
13018 /* FLOATING POINT */
13021 c = 'f'; /* maybe %F isn't supported here */
13023 case 'e': case 'E':
13025 case 'g': case 'G':
13026 case 'a': case 'A':
13029 STRLEN float_need; /* what PL_efloatsize needs to become */
13030 bool hexfp; /* hexadecimal floating point? */
13032 vcatpvfn_long_double_t fv;
13035 /* This is evil, but floating point is even more evil */
13037 /* for SV-style calling, we can only get NV
13038 for C-style calling, we assume %f is double;
13039 for simplicity we allow any of %Lf, %llf, %qf for long double
13042 #if defined(USE_QUADMATH)
13047 #if defined(USE_LONG_DOUBLE) || defined(USE_QUADMATH)
13051 /* [perl #20339] - we should accept and ignore %lf rather than die */
13055 #if defined(USE_LONG_DOUBLE) || defined(USE_QUADMATH)
13056 intsize = args ? 0 : 'q';
13060 #if defined(HAS_LONG_DOUBLE)
13073 /* Now we need (long double) if intsize == 'q', else (double). */
13075 /* Note: do not pull NVs off the va_list with va_arg()
13076 * (pull doubles instead) because if you have a build
13077 * with long doubles, you would always be pulling long
13078 * doubles, which would badly break anyone using only
13079 * doubles (i.e. the majority of builds). In other
13080 * words, you cannot mix doubles and long doubles.
13081 * The only case where you can pull off long doubles
13082 * is when the format specifier explicitly asks so with
13084 #ifdef USE_QUADMATH
13085 nv = intsize == 'Q' ? va_arg(*args, NV) :
13086 intsize == 'q' ? va_arg(*args, long double) :
13087 va_arg(*args, double);
13089 #elif LONG_DOUBLESIZE > DOUBLESIZE
13090 if (intsize == 'q') {
13091 fv = va_arg(*args, long double);
13094 nv = va_arg(*args, double);
13095 VCATPVFN_NV_TO_FV(nv, fv);
13098 nv = va_arg(*args, double);
13105 /* we jump here if an int-ish format encountered an
13106 * infinite/Nan argsv. After setting nv/fv, it falls
13107 * into the isinfnan block which follows */
13108 handle_infnan_argsv:
13109 nv = SvNV_nomg(argsv);
13110 VCATPVFN_NV_TO_FV(nv, fv);
13113 if (Perl_isinfnan(nv)) {
13115 Perl_croak(aTHX_ "Cannot printf %" NVgf " with '%c'",
13118 elen = S_infnan_2pv(nv, ebuf, sizeof(ebuf), plus);
13127 /* special-case "%.0f" */
13131 && !(width || left || plus || alt)
13134 && ((eptr = F0convert(nv, ebuf + sizeof ebuf, &elen)))
13138 /* Determine the buffer size needed for the various
13139 * floating-point formats.
13141 * The basic possibilities are:
13144 * %f 1111111.123456789
13145 * %e 1.111111123e+06
13146 * %a 0x1.0f4471f9bp+20
13148 * %g 1.11111112e+15
13150 * where P is the value of the precision in the format, or 6
13151 * if not specified. Note the two possible output formats of
13152 * %g; in both cases the number of significant digits is <=
13155 * For most of the format types the maximum buffer size needed
13156 * is precision, plus: any leading 1 or 0x1, the radix
13157 * point, and an exponent. The difficult one is %f: for a
13158 * large positive exponent it can have many leading digits,
13159 * which needs to be calculated specially. Also %a is slightly
13160 * different in that in the absence of a specified precision,
13161 * it uses as many digits as necessary to distinguish
13162 * different values.
13164 * First, here are the constant bits. For ease of calculation
13165 * we over-estimate the needed buffer size, for example by
13166 * assuming all formats have an exponent and a leading 0x1.
13168 * Also for production use, add a little extra overhead for
13169 * safety's sake. Under debugging don't, as it means we're
13170 * more likely to quickly spot issues during development.
13173 float_need = 1 /* possible unary minus */
13174 + 4 /* "0x1" plus very unlikely carry */
13175 + 1 /* default radix point '.' */
13176 + 2 /* "e-", "p+" etc */
13177 + 6 /* exponent: up to 16383 (quad fp) */
13179 + 20 /* safety net */
13184 /* determine the radix point len, e.g. length(".") in "1.2" */
13185 #ifdef USE_LOCALE_NUMERIC
13186 /* note that we may either explicitly use PL_numeric_radix_sv
13187 * below, or implicitly, via an snprintf() variant.
13188 * Note also things like ps_AF.utf8 which has
13189 * "\N{ARABIC DECIMAL SEPARATOR} as a radix point */
13190 if (! have_in_lc_numeric) {
13191 in_lc_numeric = IN_LC(LC_NUMERIC);
13192 have_in_lc_numeric = TRUE;
13195 if (in_lc_numeric) {
13196 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(TRUE, {
13197 /* this can't wrap unless PL_numeric_radix_sv is a string
13198 * consuming virtually all the 32-bit or 64-bit address
13201 float_need += (SvCUR(PL_numeric_radix_sv) - 1);
13203 /* floating-point formats only get utf8 if the radix point
13204 * is utf8. All other characters in the string are < 128
13205 * and so can be safely appended to both a non-utf8 and utf8
13207 * Note that this will convert the output to utf8 even if
13208 * the radix point didn't get output.
13210 if (SvUTF8(PL_numeric_radix_sv) && !has_utf8) {
13211 sv_utf8_upgrade(sv);
13220 if (isALPHA_FOLD_EQ(c, 'f')) {
13221 /* Determine how many digits before the radix point
13222 * might be emitted. frexp() (or frexpl) has some
13223 * unspecified behaviour for nan/inf/-inf, so lucky we've
13224 * already handled them above */
13226 int i = PERL_INT_MIN;
13227 (void)Perl_frexp((NV)fv, &i);
13228 if (i == PERL_INT_MIN)
13229 Perl_die(aTHX_ "panic: frexp: %" VCATPVFN_FV_GF, fv);
13232 digits = BIT_DIGITS(i);
13233 /* this can't overflow. 'digits' will only be a few
13234 * thousand even for the largest floating-point types.
13235 * And up until now float_need is just some small
13236 * constants plus radix len, which can't be in
13237 * overflow territory unless the radix SV is consuming
13238 * over 1/2 the address space */
13239 assert(float_need < ((STRLEN)~0) - digits);
13240 float_need += digits;
13243 else if (UNLIKELY(isALPHA_FOLD_EQ(c, 'a'))) {
13246 /* %a in the absence of precision may print as many
13247 * digits as needed to represent the entire mantissa
13249 * This estimate seriously overshoots in most cases,
13250 * but better the undershooting. Firstly, all bytes
13251 * of the NV are not mantissa, some of them are
13252 * exponent. Secondly, for the reasonably common
13253 * long doubles case, the "80-bit extended", two
13254 * or six bytes of the NV are unused. Also, we'll
13255 * still pick up an extra +6 from the default
13256 * precision calculation below. */
13258 #ifdef LONGDOUBLE_DOUBLEDOUBLE
13259 /* For the "double double", we need more.
13260 * Since each double has their own exponent, the
13261 * doubles may float (haha) rather far from each
13262 * other, and the number of required bits is much
13263 * larger, up to total of DOUBLEDOUBLE_MAXBITS bits.
13264 * See the definition of DOUBLEDOUBLE_MAXBITS.
13266 * Need 2 hexdigits for each byte. */
13267 (DOUBLEDOUBLE_MAXBITS/8 + 1) * 2;
13269 NVSIZE * 2; /* 2 hexdigits for each byte */
13271 /* see "this can't overflow" comment above */
13272 assert(float_need < ((STRLEN)~0) - digits);
13273 float_need += digits;
13276 /* special-case "%.<number>g" if it will fit in ebuf */
13278 && precis /* See earlier comment about buggy Gconvert
13279 when digits, aka precis, is 0 */
13281 /* check that "%.<number>g" formatting will fit in ebuf */
13282 && sizeof(ebuf) - float_need > precis
13283 /* sizeof(ebuf) - float_need will have wrapped if float_need > sizeof(ebuf). *
13284 * Therefore we should check that float_need < sizeof(ebuf). Normally, we would *
13285 * have run this check first, but that triggers incorrect -Wformat-overflow *
13286 * compilation warnings with some versions of gcc if Gconvert invokes sprintf(). *
13287 * ( See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89161 ) *
13288 * So, instead, we check it next: */
13289 && float_need < sizeof(ebuf)
13290 && !(width || left || plus || alt)
13294 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13295 SNPRINTF_G(fv, ebuf, sizeof(ebuf), precis)
13297 elen = strlen(ebuf);
13304 STRLEN pr = has_precis ? precis : 6; /* known default */
13305 /* this probably can't wrap, since precis is limited
13306 * to 1/4 address space size, but better safe than sorry
13308 if (float_need >= ((STRLEN)~0) - pr)
13309 croak_memory_wrap();
13313 if (float_need < width)
13314 float_need = width;
13316 if (float_need > INT_MAX) {
13317 /* snprintf() returns an int, and we use that return value,
13318 so die horribly if the expected size is too large for int
13320 Perl_croak(aTHX_ "Numeric format result too large");
13323 if (PL_efloatsize <= float_need) {
13324 /* PL_efloatbuf should be at least 1 greater than
13325 * float_need to allow a trailing \0 to be returned by
13326 * snprintf(). If we need to grow, overgrow for the
13327 * benefit of future generations */
13328 const STRLEN extra = 0x20;
13329 if (float_need >= ((STRLEN)~0) - extra)
13330 croak_memory_wrap();
13331 float_need += extra;
13332 Safefree(PL_efloatbuf);
13333 PL_efloatsize = float_need;
13334 Newx(PL_efloatbuf, PL_efloatsize, char);
13335 PL_efloatbuf[0] = '\0';
13338 if (UNLIKELY(hexfp)) {
13339 elen = S_format_hexfp(aTHX_ PL_efloatbuf, PL_efloatsize, c,
13340 nv, fv, has_precis, precis, width,
13341 alt, plus, left, fill, in_lc_numeric);
13344 char *ptr = ebuf + sizeof ebuf;
13347 #if defined(USE_QUADMATH)
13348 /* always use Q here. my_snprint() throws an exception if we
13349 fallthrough to the double/long double code, even when the
13350 format is correct, presumably to avoid any accidentally
13354 /* FIXME: what to do if HAS_LONG_DOUBLE but not PERL_PRIfldbl? */
13355 #elif defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl)
13356 /* Note that this is HAS_LONG_DOUBLE and PERL_PRIfldbl,
13357 * not USE_LONG_DOUBLE and NVff. In other words,
13358 * this needs to work without USE_LONG_DOUBLE. */
13359 if (intsize == 'q') {
13360 /* Copy the one or more characters in a long double
13361 * format before the 'base' ([efgEFG]) character to
13362 * the format string. */
13363 static char const ldblf[] = PERL_PRIfldbl;
13364 char const *p = ldblf + sizeof(ldblf) - 3;
13365 while (p >= ldblf) { *--ptr = *p--; }
13370 do { *--ptr = '0' + (base % 10); } while (base /= 10);
13375 do { *--ptr = '0' + (base % 10); } while (base /= 10);
13387 /* No taint. Otherwise we are in the strange situation
13388 * where printf() taints but print($float) doesn't.
13391 /* hopefully the above makes ptr a very constrained format
13392 * that is safe to use, even though it's not literal */
13393 GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral);
13394 #ifdef USE_QUADMATH
13396 if (!quadmath_format_valid(ptr))
13397 Perl_croak_nocontext("panic: quadmath invalid format \"%s\"", ptr);
13398 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13399 elen = quadmath_snprintf(PL_efloatbuf, PL_efloatsize,
13402 if ((IV)elen == -1) {
13403 Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", ptr);
13406 #elif defined(HAS_LONG_DOUBLE)
13407 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13408 elen = ((intsize == 'q')
13409 ? my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, fv)
13410 : my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, (double)fv))
13413 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13414 elen = my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, fv)
13417 GCC_DIAG_RESTORE_STMT;
13420 eptr = PL_efloatbuf;
13424 /* Since floating-point formats do their own formatting and
13425 * padding, we skip the main block of code at the end of this
13426 * loop which handles appending eptr to sv, and do our own
13427 * stripped-down version */
13432 assert(elen >= width);
13434 S_sv_catpvn_simple(aTHX_ sv, eptr, elen);
13436 goto done_valid_conversion;
13444 /* XXX ideally we should warn if any flags etc have been
13445 * set, e.g. "%-4.5n" */
13446 /* XXX if sv was originally non-utf8 with a char in the
13447 * range 0x80-0xff, then if it got upgraded, we should
13448 * calculate char len rather than byte len here */
13449 len = SvCUR(sv) - origlen;
13451 int i = (len > PERL_INT_MAX) ? PERL_INT_MAX : (int)len;
13454 case 'c': *(va_arg(*args, char*)) = i; break;
13455 case 'h': *(va_arg(*args, short*)) = i; break;
13456 default: *(va_arg(*args, int*)) = i; break;
13457 case 'l': *(va_arg(*args, long*)) = i; break;
13458 case 'V': *(va_arg(*args, IV*)) = i; break;
13459 case 'z': *(va_arg(*args, SSize_t*)) = i; break;
13460 #ifdef HAS_PTRDIFF_T
13461 case 't': *(va_arg(*args, ptrdiff_t*)) = i; break;
13463 case 'j': *(va_arg(*args, PERL_INTMAX_T*)) = i; break;
13466 *(va_arg(*args, Quad_t*)) = i; break;
13474 Perl_croak_nocontext(
13475 "Missing argument for %%n in %s",
13476 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
13477 sv_setuv_mg(argsv, has_utf8
13478 ? (UV)utf8_length((U8*)SvPVX(sv), (U8*)SvEND(sv))
13481 goto done_valid_conversion;
13489 && (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)
13490 && ckWARN(WARN_PRINTF))
13492 SV * const msg = sv_newmortal();
13493 Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %sprintf: ",
13494 (PL_op->op_type == OP_PRTF) ? "" : "s");
13495 if (fmtstart < patend) {
13496 const char * const fmtend = q < patend ? q : patend;
13498 sv_catpvs(msg, "\"%");
13499 for (f = fmtstart; f < fmtend; f++) {
13501 sv_catpvn_nomg(msg, f, 1);
13503 Perl_sv_catpvf(aTHX_ msg, "\\%03o", (U8) *f);
13506 sv_catpvs(msg, "\"");
13508 sv_catpvs(msg, "end of string");
13510 Perl_warner(aTHX_ packWARN(WARN_PRINTF), "%" SVf, SVfARG(msg)); /* yes, this is reentrant */
13513 /* mangled format: output the '%', then continue from the
13514 * character following that */
13515 sv_catpvn_nomg(sv, fmtstart-1, 1);
13518 /* Any "redundant arg" warning from now onwards will probably
13519 * just be misleading, so don't bother. */
13520 no_redundant_warning = TRUE;
13521 continue; /* not "break" */
13524 if (is_utf8 != has_utf8) {
13527 sv_utf8_upgrade(sv);
13530 const STRLEN old_elen = elen;
13531 SV * const nsv = newSVpvn_flags(eptr, elen, SVs_TEMP);
13532 sv_utf8_upgrade(nsv);
13533 eptr = SvPVX_const(nsv);
13536 if (width) { /* fudge width (can't fudge elen) */
13537 width += elen - old_elen;
13544 /* append esignbuf, filler, zeros, eptr and dotstr to sv */
13547 STRLEN need, have, gap;
13551 /* signed value that's wrapped? */
13552 assert(elen <= ((~(STRLEN)0) >> 1));
13554 /* if zeros is non-zero, then it represents filler between
13555 * elen and precis. So adding elen and zeros together will
13556 * always be <= precis, and the addition can never wrap */
13557 assert(!zeros || (precis > elen && precis - elen == zeros));
13558 have = elen + zeros;
13560 if (have >= (((STRLEN)~0) - esignlen))
13561 croak_memory_wrap();
13564 need = (have > width ? have : width);
13567 if (need >= (((STRLEN)~0) - (SvCUR(sv) + 1)))
13568 croak_memory_wrap();
13569 need += (SvCUR(sv) + 1);
13576 for (i = 0; i < esignlen; i++)
13577 *s++ = esignbuf[i];
13578 for (i = zeros; i; i--)
13580 Copy(eptr, s, elen, char);
13582 for (i = gap; i; i--)
13587 for (i = 0; i < esignlen; i++)
13588 *s++ = esignbuf[i];
13593 for (i = gap; i; i--)
13595 for (i = 0; i < esignlen; i++)
13596 *s++ = esignbuf[i];
13599 for (i = zeros; i; i--)
13601 Copy(eptr, s, elen, char);
13606 SvCUR_set(sv, s - SvPVX_const(sv));
13614 if (vectorize && veclen) {
13615 /* we append the vector separator separately since %v isn't
13616 * very common: don't slow down the general case by adding
13617 * dotstrlen to need etc */
13618 sv_catpvn_nomg(sv, dotstr, dotstrlen);
13620 goto vector; /* do next iteration */
13623 done_valid_conversion:
13626 S_warn_vcatpvfn_missing_argument(aTHX);
13629 /* Now that we've consumed all our printf format arguments (svix)
13630 * do we have things left on the stack that we didn't use?
13632 if (!no_redundant_warning && sv_count >= svix + 1 && ckWARN(WARN_REDUNDANT)) {
13633 Perl_warner(aTHX_ packWARN(WARN_REDUNDANT), "Redundant argument in %s",
13634 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
13637 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
13638 /* while we shouldn't set the cache, it may have been previously
13639 set in the caller, so clear it */
13640 MAGIC *mg = mg_find(sv, PERL_MAGIC_utf8);
13642 magic_setutf8(sv,mg); /* clear UTF8 cache */
13647 /* =========================================================================
13649 =for apidoc_section $embedding
13653 All the macros and functions in this section are for the private use of
13654 the main function, perl_clone().
13656 The foo_dup() functions make an exact copy of an existing foo thingy.
13657 During the course of a cloning, a hash table is used to map old addresses
13658 to new addresses. The table is created and manipulated with the
13659 ptr_table_* functions.
13661 * =========================================================================*/
13664 #if defined(USE_ITHREADS)
13666 /* XXX Remove this so it doesn't have to go thru the macro and return for nothing */
13667 #ifndef GpREFCNT_inc
13668 # define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL)
13672 /* Certain cases in Perl_ss_dup have been merged, by relying on the fact
13673 that currently av_dup, gv_dup and hv_dup are the same as sv_dup.
13674 If this changes, please unmerge ss_dup.
13675 Likewise, sv_dup_inc_multiple() relies on this fact. */
13676 #define sv_dup_inc_NN(s,t) SvREFCNT_inc_NN(sv_dup_inc(s,t))
13677 #define av_dup(s,t) MUTABLE_AV(sv_dup((const SV *)s,t))
13678 #define av_dup_inc(s,t) MUTABLE_AV(sv_dup_inc((const SV *)s,t))
13679 #define hv_dup(s,t) MUTABLE_HV(sv_dup((const SV *)s,t))
13680 #define hv_dup_inc(s,t) MUTABLE_HV(sv_dup_inc((const SV *)s,t))
13681 #define cv_dup(s,t) MUTABLE_CV(sv_dup((const SV *)s,t))
13682 #define cv_dup_inc(s,t) MUTABLE_CV(sv_dup_inc((const SV *)s,t))
13683 #define io_dup(s,t) MUTABLE_IO(sv_dup((const SV *)s,t))
13684 #define io_dup_inc(s,t) MUTABLE_IO(sv_dup_inc((const SV *)s,t))
13685 #define gv_dup(s,t) MUTABLE_GV(sv_dup((const SV *)s,t))
13686 #define gv_dup_inc(s,t) MUTABLE_GV(sv_dup_inc((const SV *)s,t))
13687 #define SAVEPV(p) ((p) ? savepv(p) : NULL)
13688 #define SAVEPVN(p,n) ((p) ? savepvn(p,n) : NULL)
13690 /* clone a parser */
13693 Perl_parser_dup(pTHX_ const yy_parser *const proto, CLONE_PARAMS *const param)
13697 PERL_ARGS_ASSERT_PARSER_DUP;
13702 /* look for it in the table first */
13703 parser = (yy_parser *)ptr_table_fetch(PL_ptr_table, proto);
13707 /* create anew and remember what it is */
13708 Newxz(parser, 1, yy_parser);
13709 ptr_table_store(PL_ptr_table, proto, parser);
13711 /* XXX eventually, just Copy() most of the parser struct ? */
13713 parser->lex_brackets = proto->lex_brackets;
13714 parser->lex_casemods = proto->lex_casemods;
13715 parser->lex_brackstack = savepvn(proto->lex_brackstack,
13716 (proto->lex_brackets < 120 ? 120 : proto->lex_brackets));
13717 parser->lex_casestack = savepvn(proto->lex_casestack,
13718 (proto->lex_casemods < 12 ? 12 : proto->lex_casemods));
13719 parser->lex_defer = proto->lex_defer;
13720 parser->lex_dojoin = proto->lex_dojoin;
13721 parser->lex_formbrack = proto->lex_formbrack;
13722 parser->lex_inpat = proto->lex_inpat;
13723 parser->lex_inwhat = proto->lex_inwhat;
13724 parser->lex_op = proto->lex_op;
13725 parser->lex_repl = sv_dup_inc(proto->lex_repl, param);
13726 parser->lex_starts = proto->lex_starts;
13727 parser->lex_stuff = sv_dup_inc(proto->lex_stuff, param);
13728 parser->multi_close = proto->multi_close;
13729 parser->multi_open = proto->multi_open;
13730 parser->multi_start = proto->multi_start;
13731 parser->multi_end = proto->multi_end;
13732 parser->preambled = proto->preambled;
13733 parser->lex_super_state = proto->lex_super_state;
13734 parser->lex_sub_inwhat = proto->lex_sub_inwhat;
13735 parser->lex_sub_op = proto->lex_sub_op;
13736 parser->lex_sub_repl= sv_dup_inc(proto->lex_sub_repl, param);
13737 parser->linestr = sv_dup_inc(proto->linestr, param);
13738 parser->expect = proto->expect;
13739 parser->copline = proto->copline;
13740 parser->last_lop_op = proto->last_lop_op;
13741 parser->lex_state = proto->lex_state;
13742 parser->rsfp = fp_dup(proto->rsfp, '<', param);
13743 /* rsfp_filters entries have fake IoDIRP() */
13744 parser->rsfp_filters= av_dup_inc(proto->rsfp_filters, param);
13745 parser->in_my = proto->in_my;
13746 parser->in_my_stash = hv_dup(proto->in_my_stash, param);
13747 parser->error_count = proto->error_count;
13748 parser->sig_elems = proto->sig_elems;
13749 parser->sig_optelems= proto->sig_optelems;
13750 parser->sig_slurpy = proto->sig_slurpy;
13751 parser->recheck_utf8_validity = proto->recheck_utf8_validity;
13754 char * const ols = SvPVX(proto->linestr);
13755 char * const ls = SvPVX(parser->linestr);
13757 parser->bufptr = ls + (proto->bufptr >= ols ?
13758 proto->bufptr - ols : 0);
13759 parser->oldbufptr = ls + (proto->oldbufptr >= ols ?
13760 proto->oldbufptr - ols : 0);
13761 parser->oldoldbufptr= ls + (proto->oldoldbufptr >= ols ?
13762 proto->oldoldbufptr - ols : 0);
13763 parser->linestart = ls + (proto->linestart >= ols ?
13764 proto->linestart - ols : 0);
13765 parser->last_uni = ls + (proto->last_uni >= ols ?
13766 proto->last_uni - ols : 0);
13767 parser->last_lop = ls + (proto->last_lop >= ols ?
13768 proto->last_lop - ols : 0);
13770 parser->bufend = ls + SvCUR(parser->linestr);
13773 Copy(proto->tokenbuf, parser->tokenbuf, 256, char);
13776 Copy(proto->nextval, parser->nextval, 5, YYSTYPE);
13777 Copy(proto->nexttype, parser->nexttype, 5, I32);
13778 parser->nexttoke = proto->nexttoke;
13780 /* XXX should clone saved_curcop here, but we aren't passed
13781 * proto_perl; so do it in perl_clone_using instead */
13787 =for apidoc_section $io
13790 Duplicate a file handle, returning a pointer to the cloned object.
13796 Perl_fp_dup(pTHX_ PerlIO *const fp, const char type, CLONE_PARAMS *const param)
13800 PERL_ARGS_ASSERT_FP_DUP;
13801 PERL_UNUSED_ARG(type);
13804 return (PerlIO*)NULL;
13806 /* look for it in the table first */
13807 ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp);
13811 /* create anew and remember what it is */
13812 #ifdef __amigaos4__
13813 ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE|PERLIO_DUP_FD);
13815 ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE);
13817 ptr_table_store(PL_ptr_table, fp, ret);
13822 =for apidoc_section $io
13823 =for apidoc dirp_dup
13825 Duplicate a directory handle, returning a pointer to the cloned object.
13831 Perl_dirp_dup(pTHX_ DIR *const dp, CLONE_PARAMS *const param)
13835 #if defined(HAS_FCHDIR) && defined(HAS_TELLDIR) && defined(HAS_SEEKDIR)
13837 const Direntry_t *dirent;
13838 char smallbuf[256]; /* XXX MAXPATHLEN, surely? */
13844 PERL_UNUSED_CONTEXT;
13845 PERL_ARGS_ASSERT_DIRP_DUP;
13850 /* look for it in the table first */
13851 ret = (DIR*)ptr_table_fetch(PL_ptr_table, dp);
13855 #if defined(HAS_FCHDIR) && defined(HAS_TELLDIR) && defined(HAS_SEEKDIR)
13857 PERL_UNUSED_ARG(param);
13861 /* open the current directory (so we can switch back) */
13862 if (!(pwd = PerlDir_open("."))) return (DIR *)NULL;
13864 /* chdir to our dir handle and open the present working directory */
13865 if (fchdir(my_dirfd(dp)) < 0 || !(ret = PerlDir_open("."))) {
13866 PerlDir_close(pwd);
13867 return (DIR *)NULL;
13869 /* Now we should have two dir handles pointing to the same dir. */
13871 /* Be nice to the calling code and chdir back to where we were. */
13872 /* XXX If this fails, then what? */
13873 PERL_UNUSED_RESULT(fchdir(my_dirfd(pwd)));
13875 /* We have no need of the pwd handle any more. */
13876 PerlDir_close(pwd);
13879 # define d_namlen(d) (d)->d_namlen
13881 # define d_namlen(d) strlen((d)->d_name)
13883 /* Iterate once through dp, to get the file name at the current posi-
13884 tion. Then step back. */
13885 pos = PerlDir_tell(dp);
13886 if ((dirent = PerlDir_read(dp))) {
13887 len = d_namlen(dirent);
13888 if (len > sizeof(dirent->d_name) && sizeof(dirent->d_name) > PTRSIZE) {
13889 /* If the len is somehow magically longer than the
13890 * maximum length of the directory entry, even though
13891 * we could fit it in a buffer, we could not copy it
13892 * from the dirent. Bail out. */
13893 PerlDir_close(ret);
13896 if (len <= sizeof smallbuf) name = smallbuf;
13897 else Newx(name, len, char);
13898 Move(dirent->d_name, name, len, char);
13900 PerlDir_seek(dp, pos);
13902 /* Iterate through the new dir handle, till we find a file with the
13904 if (!dirent) /* just before the end */
13906 pos = PerlDir_tell(ret);
13907 if (PerlDir_read(ret)) continue; /* not there yet */
13908 PerlDir_seek(ret, pos); /* step back */
13912 const long pos0 = PerlDir_tell(ret);
13914 pos = PerlDir_tell(ret);
13915 if ((dirent = PerlDir_read(ret))) {
13916 if (len == (STRLEN)d_namlen(dirent)
13917 && memEQ(name, dirent->d_name, len)) {
13919 PerlDir_seek(ret, pos); /* step back */
13922 /* else we are not there yet; keep iterating */
13924 else { /* This is not meant to happen. The best we can do is
13925 reset the iterator to the beginning. */
13926 PerlDir_seek(ret, pos0);
13933 if (name && name != smallbuf)
13938 ret = win32_dirp_dup(dp, param);
13941 /* pop it in the pointer table */
13943 ptr_table_store(PL_ptr_table, dp, ret);
13949 =for apidoc_section $GV
13952 Duplicate a typeglob, returning a pointer to the cloned object.
13958 Perl_gp_dup(pTHX_ GP *const gp, CLONE_PARAMS *const param)
13962 PERL_ARGS_ASSERT_GP_DUP;
13966 /* look for it in the table first */
13967 ret = (GP*)ptr_table_fetch(PL_ptr_table, gp);
13971 /* create anew and remember what it is */
13973 ptr_table_store(PL_ptr_table, gp, ret);
13976 /* ret->gp_refcnt must be 0 before any other dups are called. We're relying
13977 on Newxz() to do this for us. */
13978 ret->gp_sv = sv_dup_inc(gp->gp_sv, param);
13979 ret->gp_io = io_dup_inc(gp->gp_io, param);
13980 ret->gp_form = cv_dup_inc(gp->gp_form, param);
13981 ret->gp_av = av_dup_inc(gp->gp_av, param);
13982 ret->gp_hv = hv_dup_inc(gp->gp_hv, param);
13983 ret->gp_egv = gv_dup(gp->gp_egv, param);/* GvEGV is not refcounted */
13984 ret->gp_cv = cv_dup_inc(gp->gp_cv, param);
13985 ret->gp_cvgen = gp->gp_cvgen;
13986 ret->gp_line = gp->gp_line;
13987 ret->gp_file_hek = hek_dup(gp->gp_file_hek, param);
13993 =for apidoc_section $magic
13996 Duplicate a chain of magic, returning a pointer to the cloned object.
14002 Perl_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *const param)
14004 MAGIC *mgret = NULL;
14005 MAGIC **mgprev_p = &mgret;
14007 PERL_ARGS_ASSERT_MG_DUP;
14009 for (; mg; mg = mg->mg_moremagic) {
14012 if ((param->flags & CLONEf_JOIN_IN)
14013 && mg->mg_type == PERL_MAGIC_backref)
14014 /* when joining, we let the individual SVs add themselves to
14015 * backref as needed. */
14018 Newx(nmg, 1, MAGIC);
14020 mgprev_p = &(nmg->mg_moremagic);
14022 /* There was a comment "XXX copy dynamic vtable?" but as we don't have
14023 dynamic vtables, I'm not sure why Sarathy wrote it. The comment dates
14024 from the original commit adding Perl_mg_dup() - revision 4538.
14025 Similarly there is the annotation "XXX random ptr?" next to the
14026 assignment to nmg->mg_ptr. */
14029 /* FIXME for plugins
14030 if (nmg->mg_type == PERL_MAGIC_qr) {
14031 nmg->mg_obj = MUTABLE_SV(CALLREGDUPE((REGEXP*)nmg->mg_obj, param));
14035 nmg->mg_obj = (nmg->mg_flags & MGf_REFCOUNTED)
14036 ? nmg->mg_type == PERL_MAGIC_backref
14037 /* The backref AV has its reference
14038 * count deliberately bumped by 1 */
14039 ? SvREFCNT_inc(av_dup_inc((const AV *)
14040 nmg->mg_obj, param))
14041 : sv_dup_inc(nmg->mg_obj, param)
14042 : (nmg->mg_type == PERL_MAGIC_regdatum ||
14043 nmg->mg_type == PERL_MAGIC_regdata)
14045 : sv_dup(nmg->mg_obj, param);
14047 if (nmg->mg_ptr && nmg->mg_type != PERL_MAGIC_regex_global) {
14048 if (nmg->mg_len > 0) {
14049 nmg->mg_ptr = SAVEPVN(nmg->mg_ptr, nmg->mg_len);
14050 if (nmg->mg_type == PERL_MAGIC_overload_table &&
14051 AMT_AMAGIC((AMT*)nmg->mg_ptr))
14053 AMT * const namtp = (AMT*)nmg->mg_ptr;
14054 sv_dup_inc_multiple((SV**)(namtp->table),
14055 (SV**)(namtp->table), NofAMmeth, param);
14058 else if (nmg->mg_len == HEf_SVKEY)
14059 nmg->mg_ptr = (char*)sv_dup_inc((const SV *)nmg->mg_ptr, param);
14061 if ((nmg->mg_flags & MGf_DUP) && nmg->mg_virtual && nmg->mg_virtual->svt_dup) {
14062 nmg->mg_virtual->svt_dup(aTHX_ nmg, param);
14068 #endif /* USE_ITHREADS */
14070 struct ptr_tbl_arena {
14071 struct ptr_tbl_arena *next;
14072 struct ptr_tbl_ent array[1023/3]; /* as ptr_tbl_ent has 3 pointers. */
14076 =for apidoc ptr_table_new
14078 Create a new pointer-mapping table
14084 Perl_ptr_table_new(pTHX)
14087 PERL_UNUSED_CONTEXT;
14089 Newx(tbl, 1, PTR_TBL_t);
14090 tbl->tbl_max = 511;
14091 tbl->tbl_items = 0;
14092 tbl->tbl_arena = NULL;
14093 tbl->tbl_arena_next = NULL;
14094 tbl->tbl_arena_end = NULL;
14095 Newxz(tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*);
14099 #define PTR_TABLE_HASH(ptr) \
14100 ((PTR2UV(ptr) >> 3) ^ (PTR2UV(ptr) >> (3 + 7)) ^ (PTR2UV(ptr) >> (3 + 17)))
14102 /* map an existing pointer using a table */
14104 STATIC PTR_TBL_ENT_t *
14105 S_ptr_table_find(PTR_TBL_t *const tbl, const void *const sv)
14107 PTR_TBL_ENT_t *tblent;
14108 const UV hash = PTR_TABLE_HASH(sv);
14110 PERL_ARGS_ASSERT_PTR_TABLE_FIND;
14112 tblent = tbl->tbl_ary[hash & tbl->tbl_max];
14113 for (; tblent; tblent = tblent->next) {
14114 if (tblent->oldval == sv)
14121 =for apidoc ptr_table_fetch
14123 Look for C<sv> in the pointer-mapping table C<tbl>, returning its value, or
14130 Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *const tbl, const void *const sv)
14132 PTR_TBL_ENT_t const *const tblent = ptr_table_find(tbl, sv);
14134 PERL_ARGS_ASSERT_PTR_TABLE_FETCH;
14135 PERL_UNUSED_CONTEXT;
14137 return tblent ? tblent->newval : NULL;
14141 =for apidoc ptr_table_store
14143 Add a new entry to a pointer-mapping table C<tbl>.
14144 In hash terms, C<oldsv> is the key; Cnewsv> is the value.
14146 The names "old" and "new" are specific to the core's typical use of ptr_tables
14153 Perl_ptr_table_store(pTHX_ PTR_TBL_t *const tbl, const void *const oldsv, void *const newsv)
14155 PTR_TBL_ENT_t *tblent = ptr_table_find(tbl, oldsv);
14157 PERL_ARGS_ASSERT_PTR_TABLE_STORE;
14158 PERL_UNUSED_CONTEXT;
14161 tblent->newval = newsv;
14163 const UV entry = PTR_TABLE_HASH(oldsv) & tbl->tbl_max;
14165 if (tbl->tbl_arena_next == tbl->tbl_arena_end) {
14166 struct ptr_tbl_arena *new_arena;
14168 Newx(new_arena, 1, struct ptr_tbl_arena);
14169 new_arena->next = tbl->tbl_arena;
14170 tbl->tbl_arena = new_arena;
14171 tbl->tbl_arena_next = new_arena->array;
14172 tbl->tbl_arena_end = C_ARRAY_END(new_arena->array);
14175 tblent = tbl->tbl_arena_next++;
14177 tblent->oldval = oldsv;
14178 tblent->newval = newsv;
14179 tblent->next = tbl->tbl_ary[entry];
14180 tbl->tbl_ary[entry] = tblent;
14182 if (tblent->next && tbl->tbl_items > tbl->tbl_max)
14183 ptr_table_split(tbl);
14188 =for apidoc ptr_table_split
14190 Double the hash bucket size of an existing ptr table
14196 Perl_ptr_table_split(pTHX_ PTR_TBL_t *const tbl)
14198 PTR_TBL_ENT_t **ary = tbl->tbl_ary;
14199 const UV oldsize = tbl->tbl_max + 1;
14200 UV newsize = oldsize * 2;
14203 PERL_ARGS_ASSERT_PTR_TABLE_SPLIT;
14204 PERL_UNUSED_CONTEXT;
14206 Renew(ary, newsize, PTR_TBL_ENT_t*);
14207 Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*);
14208 tbl->tbl_max = --newsize;
14209 tbl->tbl_ary = ary;
14210 for (i=0; i < oldsize; i++, ary++) {
14211 PTR_TBL_ENT_t **entp = ary;
14212 PTR_TBL_ENT_t *ent = *ary;
14213 PTR_TBL_ENT_t **curentp;
14216 curentp = ary + oldsize;
14218 if ((newsize & PTR_TABLE_HASH(ent->oldval)) != i) {
14220 ent->next = *curentp;
14231 =for apidoc ptr_table_free
14233 Clear and free a ptr table
14239 Perl_ptr_table_free(pTHX_ PTR_TBL_t *const tbl)
14241 struct ptr_tbl_arena *arena;
14243 PERL_UNUSED_CONTEXT;
14249 arena = tbl->tbl_arena;
14252 struct ptr_tbl_arena *next = arena->next;
14258 Safefree(tbl->tbl_ary);
14262 #if defined(USE_ITHREADS)
14265 Perl_rvpv_dup(pTHX_ SV *const dsv, const SV *const ssv, CLONE_PARAMS *const param)
14267 PERL_ARGS_ASSERT_RVPV_DUP;
14269 assert(!isREGEXP(ssv));
14271 if (SvWEAKREF(ssv)) {
14272 SvRV_set(dsv, sv_dup(SvRV_const(ssv), param));
14273 if (param->flags & CLONEf_JOIN_IN) {
14274 /* if joining, we add any back references individually rather
14275 * than copying the whole backref array */
14276 Perl_sv_add_backref(aTHX_ SvRV(dsv), dsv);
14280 SvRV_set(dsv, sv_dup_inc(SvRV_const(ssv), param));
14282 else if (SvPVX_const(ssv)) {
14283 /* Has something there */
14285 /* Normal PV - clone whole allocated space */
14286 SvPV_set(dsv, SAVEPVN(SvPVX_const(ssv), SvLEN(ssv)-1));
14287 /* ssv may not be that normal, but actually copy on write.
14288 But we are a true, independent SV, so: */
14292 /* Special case - not normally malloced for some reason */
14293 if (isGV_with_GP(ssv)) {
14294 /* Don't need to do anything here. */
14296 else if ((SvIsCOW_shared_hash(ssv))) {
14297 /* A "shared" PV - clone it as "shared" PV */
14299 HEK_KEY(hek_dup(SvSHARED_HEK_FROM_PV(SvPVX_const(ssv)),
14303 /* Some other special case - random pointer */
14304 SvPV_set(dsv, (char *) SvPVX_const(ssv));
14309 /* Copy the NULL */
14310 SvPV_set(dsv, NULL);
14314 /* duplicate a list of SVs. source and dest may point to the same memory. */
14316 S_sv_dup_inc_multiple(pTHX_ SV *const *source, SV **dest,
14317 SSize_t items, CLONE_PARAMS *const param)
14319 PERL_ARGS_ASSERT_SV_DUP_INC_MULTIPLE;
14321 while (items-- > 0) {
14322 *dest++ = sv_dup_inc(*source++, param);
14328 /* duplicate an SV of any type (including AV, HV etc) */
14331 S_sv_dup_common(pTHX_ const SV *const ssv, CLONE_PARAMS *const param)
14335 PERL_ARGS_ASSERT_SV_DUP_COMMON;
14337 if (SvTYPE(ssv) == (svtype)SVTYPEMASK) {
14338 #ifdef DEBUG_LEAKING_SCALARS_ABORT
14343 /* look for it in the table first */
14344 dsv = MUTABLE_SV(ptr_table_fetch(PL_ptr_table, ssv));
14348 if(param->flags & CLONEf_JOIN_IN) {
14349 /** We are joining here so we don't want do clone
14350 something that is bad **/
14351 if (SvTYPE(ssv) == SVt_PVHV) {
14352 const HEK * const hvname = HvNAME_HEK(ssv);
14354 /** don't clone stashes if they already exist **/
14355 dsv = MUTABLE_SV(gv_stashpvn(HEK_KEY(hvname), HEK_LEN(hvname),
14356 HEK_UTF8(hvname) ? SVf_UTF8 : 0));
14357 ptr_table_store(PL_ptr_table, ssv, dsv);
14361 else if (SvTYPE(ssv) == SVt_PVGV && !SvFAKE(ssv)) {
14362 HV *stash = GvSTASH(ssv);
14363 const HEK * hvname;
14364 if (stash && (hvname = HvNAME_HEK(stash))) {
14365 /** don't clone GVs if they already exist **/
14367 stash = gv_stashpvn(HEK_KEY(hvname), HEK_LEN(hvname),
14368 HEK_UTF8(hvname) ? SVf_UTF8 : 0);
14370 stash, GvNAME(ssv),
14376 if (svp && *svp && SvTYPE(*svp) == SVt_PVGV) {
14377 ptr_table_store(PL_ptr_table, ssv, *svp);
14384 /* create anew and remember what it is */
14387 #ifdef DEBUG_LEAKING_SCALARS
14388 dsv->sv_debug_optype = ssv->sv_debug_optype;
14389 dsv->sv_debug_line = ssv->sv_debug_line;
14390 dsv->sv_debug_inpad = ssv->sv_debug_inpad;
14391 dsv->sv_debug_parent = (SV*)ssv;
14392 FREE_SV_DEBUG_FILE(dsv);
14393 dsv->sv_debug_file = savesharedpv(ssv->sv_debug_file);
14396 ptr_table_store(PL_ptr_table, ssv, dsv);
14399 SvFLAGS(dsv) = SvFLAGS(ssv);
14400 SvFLAGS(dsv) &= ~SVf_OOK; /* don't propagate OOK hack */
14401 SvREFCNT(dsv) = 0; /* must be before any other dups! */
14404 if (SvANY(ssv) && PL_watch_pvx && SvPVX_const(ssv) == PL_watch_pvx)
14405 PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n",
14406 (void*)PL_watch_pvx, SvPVX_const(ssv));
14409 /* don't clone objects whose class has asked us not to */
14411 && ! (SvFLAGS(SvSTASH(ssv)) & SVphv_CLONEABLE))
14417 switch (SvTYPE(ssv)) {
14422 SET_SVANY_FOR_BODYLESS_IV(dsv);
14424 Perl_rvpv_dup(aTHX_ dsv, ssv, param);
14426 SvIV_set(dsv, SvIVX(ssv));
14430 #if NVSIZE <= IVSIZE
14431 SET_SVANY_FOR_BODYLESS_NV(dsv);
14433 SvANY(dsv) = new_XNV();
14435 SvNV_set(dsv, SvNVX(ssv));
14439 /* These are all the types that need complex bodies allocating. */
14441 const svtype sv_type = SvTYPE(ssv);
14442 const struct body_details *sv_type_details
14443 = bodies_by_type + sv_type;
14447 Perl_croak(aTHX_ "Bizarre SvTYPE [%" IVdf "]", (IV)SvTYPE(ssv));
14448 NOT_REACHED; /* NOTREACHED */
14452 if (HvHasAUX(ssv)) {
14453 sv_type_details = &fake_hv_with_aux;
14455 new_body = new_NOARENA(sv_type_details);
14457 new_body_from_arena(new_body, HVAUX_ARENA_ROOT_IX, fake_hv_with_aux);
14474 assert(sv_type_details->body_size);
14476 if (sv_type_details->arena) {
14477 new_body = S_new_body(aTHX_ sv_type);
14479 = (void*)((char*)new_body - sv_type_details->offset);
14483 new_body = new_NOARENA(sv_type_details);
14488 SvANY(dsv) = new_body;
14491 Copy(((char*)SvANY(ssv)) + sv_type_details->offset,
14492 ((char*)SvANY(dsv)) + sv_type_details->offset,
14493 sv_type_details->copy, char);
14495 Copy(((char*)SvANY(ssv)),
14496 ((char*)SvANY(dsv)),
14497 sv_type_details->body_size + sv_type_details->offset, char);
14500 if (sv_type != SVt_PVAV && sv_type != SVt_PVHV
14501 && !isGV_with_GP(dsv)
14503 && !(sv_type == SVt_PVIO && !(IoFLAGS(dsv) & IOf_FAKE_DIRP)))
14504 Perl_rvpv_dup(aTHX_ dsv, ssv, param);
14506 /* The Copy above means that all the source (unduplicated) pointers
14507 are now in the destination. We can check the flags and the
14508 pointers in either, but it's possible that there's less cache
14509 missing by always going for the destination.
14510 FIXME - instrument and check that assumption */
14511 if (sv_type >= SVt_PVMG) {
14513 SvMAGIC_set(dsv, mg_dup(SvMAGIC(dsv), param));
14514 if (SvOBJECT(dsv) && SvSTASH(dsv))
14515 SvSTASH_set(dsv, hv_dup_inc(SvSTASH(dsv), param));
14516 else SvSTASH_set(dsv, 0); /* don't copy DESTROY cache */
14519 /* The cast silences a GCC warning about unhandled types. */
14520 switch ((int)sv_type) {
14531 /* FIXME for plugins */
14532 re_dup_guts((REGEXP*) ssv, (REGEXP*) dsv, param);
14535 /* XXX LvTARGOFF sometimes holds PMOP* when DEBUGGING */
14536 if (LvTYPE(dsv) == 't') /* for tie: unrefcnted fake (SV**) */
14538 else if (LvTYPE(dsv) == 'T') /* for tie: fake HE */
14539 LvTARG(dsv) = MUTABLE_SV(he_dup((HE*)LvTARG(dsv), FALSE, param));
14541 LvTARG(dsv) = sv_dup_inc(LvTARG(dsv), param);
14542 if (isREGEXP(ssv)) goto duprex;
14545 /* non-GP case already handled above */
14546 if(isGV_with_GP(ssv)) {
14547 GvNAME_HEK(dsv) = hek_dup(GvNAME_HEK(dsv), param);
14548 /* Don't call sv_add_backref here as it's going to be
14549 created as part of the magic cloning of the symbol
14550 table--unless this is during a join and the stash
14551 is not actually being cloned. */
14552 /* Danger Will Robinson - GvGP(dsv) isn't initialised
14553 at the point of this comment. */
14554 GvSTASH(dsv) = hv_dup(GvSTASH(dsv), param);
14555 if (param->flags & CLONEf_JOIN_IN)
14556 Perl_sv_add_backref(aTHX_ MUTABLE_SV(GvSTASH(dsv)), dsv);
14557 GvGP_set(dsv, gp_dup(GvGP(ssv), param));
14558 (void)GpREFCNT_inc(GvGP(dsv));
14562 /* PL_parser->rsfp_filters entries have fake IoDIRP() */
14563 if(IoFLAGS(dsv) & IOf_FAKE_DIRP) {
14564 /* I have no idea why fake dirp (rsfps)
14565 should be treated differently but otherwise
14566 we end up with leaks -- sky*/
14567 IoTOP_GV(dsv) = gv_dup_inc(IoTOP_GV(dsv), param);
14568 IoFMT_GV(dsv) = gv_dup_inc(IoFMT_GV(dsv), param);
14569 IoBOTTOM_GV(dsv) = gv_dup_inc(IoBOTTOM_GV(dsv), param);
14571 IoTOP_GV(dsv) = gv_dup(IoTOP_GV(dsv), param);
14572 IoFMT_GV(dsv) = gv_dup(IoFMT_GV(dsv), param);
14573 IoBOTTOM_GV(dsv) = gv_dup(IoBOTTOM_GV(dsv), param);
14575 IoDIRP(dsv) = dirp_dup(IoDIRP(dsv), param);
14578 /* IoDIRP(dsv) is already a copy of IoDIRP(ssv) */
14580 IoIFP(dsv) = fp_dup(IoIFP(ssv), IoTYPE(dsv), param);
14582 if (IoOFP(dsv) == IoIFP(ssv))
14583 IoOFP(dsv) = IoIFP(dsv);
14585 IoOFP(dsv) = fp_dup(IoOFP(dsv), IoTYPE(dsv), param);
14586 IoTOP_NAME(dsv) = SAVEPV(IoTOP_NAME(dsv));
14587 IoFMT_NAME(dsv) = SAVEPV(IoFMT_NAME(dsv));
14588 IoBOTTOM_NAME(dsv) = SAVEPV(IoBOTTOM_NAME(dsv));
14591 /* avoid cloning an empty array */
14592 if (AvARRAY((const AV *)ssv) && AvFILLp((const AV *)ssv) >= 0) {
14593 SV **dst_ary, **src_ary;
14594 SSize_t items = AvFILLp((const AV *)ssv) + 1;
14596 src_ary = AvARRAY((const AV *)ssv);
14597 Newx(dst_ary, AvMAX((const AV *)ssv)+1, SV*);
14598 ptr_table_store(PL_ptr_table, src_ary, dst_ary);
14599 AvARRAY(MUTABLE_AV(dsv)) = dst_ary;
14600 AvALLOC((const AV *)dsv) = dst_ary;
14601 if (AvREAL((const AV *)ssv)) {
14602 dst_ary = sv_dup_inc_multiple(src_ary, dst_ary, items,
14606 while (items-- > 0)
14607 *dst_ary++ = sv_dup(*src_ary++, param);
14609 items = AvMAX((const AV *)ssv) - AvFILLp((const AV *)ssv);
14610 while (items-- > 0) {
14615 AvARRAY(MUTABLE_AV(dsv)) = NULL;
14616 AvALLOC((const AV *)dsv) = (SV**)NULL;
14617 AvMAX( (const AV *)dsv) = -1;
14618 AvFILLp((const AV *)dsv) = -1;
14622 if (HvARRAY((const HV *)ssv)) {
14624 XPVHV * const dxhv = (XPVHV*)SvANY(dsv);
14625 XPVHV * const sxhv = (XPVHV*)SvANY(ssv);
14627 Newx(darray, PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1),
14629 HvARRAY(dsv) = (HE**)darray;
14630 while (i <= sxhv->xhv_max) {
14631 const HE * const source = HvARRAY(ssv)[i];
14632 HvARRAY(dsv)[i] = source
14633 ? he_dup(source, FALSE, param) : 0;
14636 if (HvHasAUX(ssv)) {
14637 const struct xpvhv_aux * const saux = HvAUX(ssv);
14638 struct xpvhv_aux * const daux = HvAUX(dsv);
14639 /* This flag isn't copied. */
14642 if (saux->xhv_name_count) {
14643 HEK ** const sname = saux->xhv_name_u.xhvnameu_names;
14645 = saux->xhv_name_count < 0
14646 ? -saux->xhv_name_count
14647 : saux->xhv_name_count;
14648 HEK **shekp = sname + count;
14650 Newx(daux->xhv_name_u.xhvnameu_names, count, HEK *);
14651 dhekp = daux->xhv_name_u.xhvnameu_names + count;
14652 while (shekp-- > sname) {
14654 *dhekp = hek_dup(*shekp, param);
14658 daux->xhv_name_u.xhvnameu_name
14659 = hek_dup(saux->xhv_name_u.xhvnameu_name,
14662 daux->xhv_name_count = saux->xhv_name_count;
14664 daux->xhv_aux_flags = saux->xhv_aux_flags;
14665 #ifdef PERL_HASH_RANDOMIZE_KEYS
14666 daux->xhv_rand = saux->xhv_rand;
14667 daux->xhv_last_rand = saux->xhv_last_rand;
14669 daux->xhv_riter = saux->xhv_riter;
14670 daux->xhv_eiter = saux->xhv_eiter
14671 ? he_dup(saux->xhv_eiter, FALSE, param) : 0;
14672 /* backref array needs refcnt=2; see sv_add_backref */
14673 daux->xhv_backreferences =
14674 (param->flags & CLONEf_JOIN_IN)
14675 /* when joining, we let the individual GVs and
14676 * CVs add themselves to backref as
14677 * needed. This avoids pulling in stuff
14678 * that isn't required, and simplifies the
14679 * case where stashes aren't cloned back
14680 * if they already exist in the parent
14683 : saux->xhv_backreferences
14684 ? (SvTYPE(saux->xhv_backreferences) == SVt_PVAV)
14685 ? MUTABLE_AV(SvREFCNT_inc(
14686 sv_dup_inc((const SV *)
14687 saux->xhv_backreferences, param)))
14688 : MUTABLE_AV(sv_dup((const SV *)
14689 saux->xhv_backreferences, param))
14692 daux->xhv_mro_meta = saux->xhv_mro_meta
14693 ? mro_meta_dup(saux->xhv_mro_meta, param)
14696 /* Record stashes for possible cloning in Perl_clone(). */
14698 av_push(param->stashes, dsv);
14702 HvARRAY(MUTABLE_HV(dsv)) = NULL;
14705 if (!(param->flags & CLONEf_COPY_STACKS)) {
14710 /* NOTE: not refcounted */
14711 SvANY(MUTABLE_CV(dsv))->xcv_stash =
14712 hv_dup(CvSTASH(dsv), param);
14713 if ((param->flags & CLONEf_JOIN_IN) && CvSTASH(dsv))
14714 Perl_sv_add_backref(aTHX_ MUTABLE_SV(CvSTASH(dsv)), dsv);
14715 if (!CvISXSUB(dsv)) {
14717 CvROOT(dsv) = OpREFCNT_inc(CvROOT(dsv));
14719 CvSLABBED_off(dsv);
14720 } else if (CvCONST(dsv)) {
14721 CvXSUBANY(dsv).any_ptr =
14722 sv_dup_inc((const SV *)CvXSUBANY(dsv).any_ptr, param);
14723 } else if (CvREFCOUNTED_ANYSV(dsv)) {
14724 CvXSUBANY(dsv).any_sv =
14725 sv_dup_inc((const SV *)CvXSUBANY(dsv).any_sv, param);
14727 assert(!CvSLABBED(dsv));
14728 if (CvDYNFILE(dsv)) CvFILE(dsv) = SAVEPV(CvFILE(dsv));
14730 SvANY((CV *)dsv)->xcv_gv_u.xcv_hek =
14731 hek_dup(CvNAME_HEK((CV *)ssv), param);
14732 /* don't dup if copying back - CvGV isn't refcounted, so the
14733 * duped GV may never be freed. A bit of a hack! DAPM */
14735 SvANY(MUTABLE_CV(dsv))->xcv_gv_u.xcv_gv =
14737 ? gv_dup_inc(CvGV(ssv), param)
14738 : (param->flags & CLONEf_JOIN_IN)
14740 : gv_dup(CvGV(ssv), param);
14742 if (!CvISXSUB(ssv)) {
14743 PADLIST * padlist = CvPADLIST(ssv);
14745 padlist = padlist_dup(padlist, param);
14746 CvPADLIST_set(dsv, padlist);
14748 /* unthreaded perl can't sv_dup so we dont support unthreaded's CvHSCXT */
14749 PoisonPADLIST(dsv);
14753 ? cv_dup( CvOUTSIDE(dsv), param)
14754 : cv_dup_inc(CvOUTSIDE(dsv), param);
14764 Perl_sv_dup_inc(pTHX_ const SV *const ssv, CLONE_PARAMS *const param)
14766 PERL_ARGS_ASSERT_SV_DUP_INC;
14767 return ssv ? SvREFCNT_inc(sv_dup_common(ssv, param)) : NULL;
14771 Perl_sv_dup(pTHX_ const SV *const ssv, CLONE_PARAMS *const param)
14773 SV *dsv = ssv ? sv_dup_common(ssv, param) : NULL;
14774 PERL_ARGS_ASSERT_SV_DUP;
14776 /* Track every SV that (at least initially) had a reference count of 0.
14777 We need to do this by holding an actual reference to it in this array.
14778 If we attempt to cheat, turn AvREAL_off(), and store only pointers
14779 (akin to the stashes hash, and the perl stack), we come unstuck if
14780 a weak reference (or other SV legitimately SvREFCNT() == 0 for this
14781 thread) is manipulated in a CLONE method, because CLONE runs before the
14782 unreferenced array is walked to find SVs still with SvREFCNT() == 0
14783 (and fix things up by giving each a reference via the temps stack).
14784 Instead, during CLONE, if the 0-referenced SV has SvREFCNT_inc() and
14785 then SvREFCNT_dec(), it will be cleaned up (and added to the free list)
14786 before the walk of unreferenced happens and a reference to that is SV
14787 added to the temps stack. At which point we have the same SV considered
14788 to be in use, and free to be re-used. Not good.
14790 if (dsv && !(param->flags & CLONEf_COPY_STACKS) && !SvREFCNT(dsv)) {
14791 assert(param->unreferenced);
14792 av_push(param->unreferenced, SvREFCNT_inc(dsv));
14798 /* duplicate a context */
14801 Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max, CLONE_PARAMS* param)
14803 PERL_CONTEXT *ncxs;
14805 PERL_ARGS_ASSERT_CX_DUP;
14808 return (PERL_CONTEXT*)NULL;
14810 /* look for it in the table first */
14811 ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs);
14815 /* create anew and remember what it is */
14816 Newx(ncxs, max + 1, PERL_CONTEXT);
14817 ptr_table_store(PL_ptr_table, cxs, ncxs);
14818 Copy(cxs, ncxs, max + 1, PERL_CONTEXT);
14821 PERL_CONTEXT * const ncx = &ncxs[ix];
14822 if (CxTYPE(ncx) == CXt_SUBST) {
14823 Perl_croak(aTHX_ "Cloning substitution context is unimplemented");
14826 ncx->blk_oldcop = (COP*)any_dup(ncx->blk_oldcop, param->proto_perl);
14827 switch (CxTYPE(ncx)) {
14829 ncx->blk_sub.cv = cv_dup_inc(ncx->blk_sub.cv, param);
14830 if(CxHASARGS(ncx)){
14831 ncx->blk_sub.savearray = av_dup_inc(ncx->blk_sub.savearray,param);
14833 ncx->blk_sub.savearray = NULL;
14835 ncx->blk_sub.prevcomppad = (PAD*)ptr_table_fetch(PL_ptr_table,
14836 ncx->blk_sub.prevcomppad);
14839 ncx->blk_eval.old_namesv = sv_dup_inc(ncx->blk_eval.old_namesv,
14841 /* XXX should this sv_dup_inc? Or only if CxEVAL_TXT_REFCNTED ???? */
14842 ncx->blk_eval.cur_text = sv_dup(ncx->blk_eval.cur_text, param);
14843 ncx->blk_eval.cv = cv_dup(ncx->blk_eval.cv, param);
14844 /* XXX what to do with cur_top_env ???? */
14846 case CXt_LOOP_LAZYSV:
14847 ncx->blk_loop.state_u.lazysv.end
14848 = sv_dup_inc(ncx->blk_loop.state_u.lazysv.end, param);
14849 /* Fallthrough: duplicate lazysv.cur by using the ary.ary
14850 duplication code instead.
14851 We are taking advantage of (1) av_dup_inc and sv_dup_inc
14852 actually being the same function, and (2) order
14853 equivalence of the two unions.
14854 We can assert the later [but only at run time :-(] */
14855 assert ((void *) &ncx->blk_loop.state_u.ary.ary ==
14856 (void *) &ncx->blk_loop.state_u.lazysv.cur);
14859 ncx->blk_loop.state_u.ary.ary
14860 = av_dup_inc(ncx->blk_loop.state_u.ary.ary, param);
14862 case CXt_LOOP_LIST:
14863 case CXt_LOOP_LAZYIV:
14864 /* code common to all 'for' CXt_LOOP_* types */
14865 ncx->blk_loop.itersave =
14866 sv_dup_inc(ncx->blk_loop.itersave, param);
14867 if (CxPADLOOP(ncx)) {
14868 PADOFFSET off = ncx->blk_loop.itervar_u.svp
14869 - &CX_CURPAD_SV(ncx->blk_loop, 0);
14870 ncx->blk_loop.oldcomppad =
14871 (PAD*)ptr_table_fetch(PL_ptr_table,
14872 ncx->blk_loop.oldcomppad);
14873 ncx->blk_loop.itervar_u.svp =
14874 &CX_CURPAD_SV(ncx->blk_loop, off);
14877 /* this copies the GV if CXp_FOR_GV, or the SV for an
14878 * alias (for \$x (...)) - relies on gv_dup being the
14879 * same as sv_dup */
14880 ncx->blk_loop.itervar_u.gv
14881 = gv_dup((const GV *)ncx->blk_loop.itervar_u.gv,
14885 case CXt_LOOP_PLAIN:
14888 ncx->blk_format.prevcomppad =
14889 (PAD*)ptr_table_fetch(PL_ptr_table,
14890 ncx->blk_format.prevcomppad);
14891 ncx->blk_format.cv = cv_dup_inc(ncx->blk_format.cv, param);
14892 ncx->blk_format.gv = gv_dup(ncx->blk_format.gv, param);
14893 ncx->blk_format.dfoutgv = gv_dup_inc(ncx->blk_format.dfoutgv,
14897 ncx->blk_givwhen.defsv_save =
14898 sv_dup_inc(ncx->blk_givwhen.defsv_save, param);
14915 Duplicate a stack info structure, returning a pointer to the cloned object.
14921 Perl_si_dup(pTHX_ PERL_SI *si, CLONE_PARAMS* param)
14925 PERL_ARGS_ASSERT_SI_DUP;
14928 return (PERL_SI*)NULL;
14930 /* look for it in the table first */
14931 nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si);
14935 /* create anew and remember what it is */
14936 Newx(nsi, 1, PERL_SI);
14937 ptr_table_store(PL_ptr_table, si, nsi);
14939 nsi->si_stack = av_dup_inc(si->si_stack, param);
14940 nsi->si_cxix = si->si_cxix;
14941 nsi->si_cxsubix = si->si_cxsubix;
14942 nsi->si_cxmax = si->si_cxmax;
14943 nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax, param);
14944 nsi->si_type = si->si_type;
14945 nsi->si_prev = si_dup(si->si_prev, param);
14946 nsi->si_next = si_dup(si->si_next, param);
14947 nsi->si_markoff = si->si_markoff;
14948 #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
14949 nsi->si_stack_hwm = 0;
14955 #define POPINT(ss,ix) ((ss)[--(ix)].any_i32)
14956 #define TOPINT(ss,ix) ((ss)[ix].any_i32)
14957 #define POPLONG(ss,ix) ((ss)[--(ix)].any_long)
14958 #define TOPLONG(ss,ix) ((ss)[ix].any_long)
14959 #define POPIV(ss,ix) ((ss)[--(ix)].any_iv)
14960 #define TOPIV(ss,ix) ((ss)[ix].any_iv)
14961 #define POPUV(ss,ix) ((ss)[--(ix)].any_uv)
14962 #define TOPUV(ss,ix) ((ss)[ix].any_uv)
14963 #define POPBOOL(ss,ix) ((ss)[--(ix)].any_bool)
14964 #define TOPBOOL(ss,ix) ((ss)[ix].any_bool)
14965 #define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr)
14966 #define TOPPTR(ss,ix) ((ss)[ix].any_ptr)
14967 #define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr)
14968 #define TOPDPTR(ss,ix) ((ss)[ix].any_dptr)
14969 #define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr)
14970 #define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr)
14973 #define pv_dup_inc(p) SAVEPV(p)
14974 #define pv_dup(p) SAVEPV(p)
14975 #define svp_dup_inc(p,pp) any_dup(p,pp)
14977 /* map any object to the new equivent - either something in the
14978 * ptr table, or something in the interpreter structure
14982 Perl_any_dup(pTHX_ void *v, const PerlInterpreter *proto_perl)
14986 PERL_ARGS_ASSERT_ANY_DUP;
14989 return (void*)NULL;
14991 /* look for it in the table first */
14992 ret = ptr_table_fetch(PL_ptr_table, v);
14996 /* see if it is part of the interpreter structure */
14997 if (v >= (void*)proto_perl && v < (void*)(proto_perl+1))
14998 ret = (void*)(((char*)aTHX) + (((char*)v) - (char*)proto_perl));
15009 Duplicate the save stack, returning a pointer to the cloned object.
15015 Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl, CLONE_PARAMS* param)
15017 ANY * const ss = proto_perl->Isavestack;
15018 const I32 max = proto_perl->Isavestack_max + SS_MAXPUSH;
15019 I32 ix = proto_perl->Isavestack_ix;
15032 void (*dptr) (void*);
15033 void (*dxptr) (pTHX_ void*);
15035 PERL_ARGS_ASSERT_SS_DUP;
15037 Newx(nss, max, ANY);
15040 const UV uv = POPUV(ss,ix);
15041 const U8 type = (U8)uv & SAVE_MASK;
15043 TOPUV(nss,ix) = uv;
15045 case SAVEt_CLEARSV:
15046 case SAVEt_CLEARPADRANGE:
15048 case SAVEt_HELEM: /* hash element */
15049 case SAVEt_SV: /* scalar reference */
15050 sv = (const SV *)POPPTR(ss,ix);
15051 TOPPTR(nss,ix) = SvREFCNT_inc(sv_dup_inc(sv, param));
15053 case SAVEt_ITEM: /* normal string */
15054 case SAVEt_GVSV: /* scalar slot in GV */
15055 sv = (const SV *)POPPTR(ss,ix);
15056 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15057 if (type == SAVEt_SV)
15061 case SAVEt_MORTALIZESV:
15062 case SAVEt_READONLY_OFF:
15063 sv = (const SV *)POPPTR(ss,ix);
15064 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15066 case SAVEt_FREEPADNAME:
15067 ptr = POPPTR(ss,ix);
15068 TOPPTR(nss,ix) = padname_dup((PADNAME *)ptr, param);
15069 PadnameREFCNT((PADNAME *)TOPPTR(nss,ix))++;
15071 case SAVEt_SHARED_PVREF: /* char* in shared space */
15072 c = (char*)POPPTR(ss,ix);
15073 TOPPTR(nss,ix) = savesharedpv(c);
15074 ptr = POPPTR(ss,ix);
15075 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15077 case SAVEt_GENERIC_SVREF: /* generic sv */
15078 case SAVEt_SVREF: /* scalar reference */
15079 sv = (const SV *)POPPTR(ss,ix);
15080 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15081 if (type == SAVEt_SVREF)
15082 SvREFCNT_inc_simple_void((SV *)TOPPTR(nss,ix));
15083 ptr = POPPTR(ss,ix);
15084 TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
15086 case SAVEt_GVSLOT: /* any slot in GV */
15087 sv = (const SV *)POPPTR(ss,ix);
15088 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15089 ptr = POPPTR(ss,ix);
15090 TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
15091 sv = (const SV *)POPPTR(ss,ix);
15092 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15094 case SAVEt_HV: /* hash reference */
15095 case SAVEt_AV: /* array reference */
15096 sv = (const SV *) POPPTR(ss,ix);
15097 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15099 case SAVEt_COMPPAD:
15101 sv = (const SV *) POPPTR(ss,ix);
15102 TOPPTR(nss,ix) = sv_dup(sv, param);
15104 case SAVEt_INT: /* int reference */
15105 ptr = POPPTR(ss,ix);
15106 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15107 intval = (int)POPINT(ss,ix);
15108 TOPINT(nss,ix) = intval;
15110 case SAVEt_LONG: /* long reference */
15111 ptr = POPPTR(ss,ix);
15112 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15113 longval = (long)POPLONG(ss,ix);
15114 TOPLONG(nss,ix) = longval;
15116 case SAVEt_I32: /* I32 reference */
15117 ptr = POPPTR(ss,ix);
15118 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15120 TOPINT(nss,ix) = i;
15122 case SAVEt_IV: /* IV reference */
15123 case SAVEt_STRLEN: /* STRLEN/size_t ref */
15124 ptr = POPPTR(ss,ix);
15125 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15127 TOPIV(nss,ix) = iv;
15129 case SAVEt_TMPSFLOOR:
15131 TOPIV(nss,ix) = iv;
15133 case SAVEt_HPTR: /* HV* reference */
15134 case SAVEt_APTR: /* AV* reference */
15135 case SAVEt_SPTR: /* SV* reference */
15136 ptr = POPPTR(ss,ix);
15137 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15138 sv = (const SV *)POPPTR(ss,ix);
15139 TOPPTR(nss,ix) = sv_dup(sv, param);
15141 case SAVEt_VPTR: /* random* reference */
15142 ptr = POPPTR(ss,ix);
15143 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15145 case SAVEt_STRLEN_SMALL:
15146 case SAVEt_INT_SMALL:
15147 case SAVEt_I32_SMALL:
15148 case SAVEt_I16: /* I16 reference */
15149 case SAVEt_I8: /* I8 reference */
15151 ptr = POPPTR(ss,ix);
15152 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15154 case SAVEt_GENERIC_PVREF: /* generic char* */
15155 case SAVEt_PPTR: /* char* reference */
15156 ptr = POPPTR(ss,ix);
15157 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15158 c = (char*)POPPTR(ss,ix);
15159 TOPPTR(nss,ix) = pv_dup(c);
15161 case SAVEt_GP: /* scalar reference */
15162 gp = (GP*)POPPTR(ss,ix);
15163 TOPPTR(nss,ix) = gp = gp_dup(gp, param);
15164 (void)GpREFCNT_inc(gp);
15165 gv = (const GV *)POPPTR(ss,ix);
15166 TOPPTR(nss,ix) = gv_dup_inc(gv, param);
15169 ptr = POPPTR(ss,ix);
15170 if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) {
15171 /* these are assumed to be refcounted properly */
15173 switch (((OP*)ptr)->op_type) {
15175 case OP_LEAVESUBLV:
15179 case OP_LEAVEWRITE:
15180 TOPPTR(nss,ix) = ptr;
15183 (void) OpREFCNT_inc(o);
15187 TOPPTR(nss,ix) = NULL;
15192 TOPPTR(nss,ix) = NULL;
15194 case SAVEt_FREECOPHH:
15195 ptr = POPPTR(ss,ix);
15196 TOPPTR(nss,ix) = cophh_copy((COPHH *)ptr);
15198 case SAVEt_ADELETE:
15199 av = (const AV *)POPPTR(ss,ix);
15200 TOPPTR(nss,ix) = av_dup_inc(av, param);
15202 TOPINT(nss,ix) = i;
15205 hv = (const HV *)POPPTR(ss,ix);
15206 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
15208 TOPINT(nss,ix) = i;
15211 c = (char*)POPPTR(ss,ix);
15212 TOPPTR(nss,ix) = pv_dup_inc(c);
15214 case SAVEt_STACK_POS: /* Position on Perl stack */
15216 TOPINT(nss,ix) = i;
15218 case SAVEt_DESTRUCTOR:
15219 ptr = POPPTR(ss,ix);
15220 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
15221 dptr = POPDPTR(ss,ix);
15222 TOPDPTR(nss,ix) = DPTR2FPTR(void (*)(void*),
15223 any_dup(FPTR2DPTR(void *, dptr),
15226 case SAVEt_DESTRUCTOR_X:
15227 ptr = POPPTR(ss,ix);
15228 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
15229 dxptr = POPDXPTR(ss,ix);
15230 TOPDXPTR(nss,ix) = DPTR2FPTR(void (*)(pTHX_ void*),
15231 any_dup(FPTR2DPTR(void *, dxptr),
15234 case SAVEt_REGCONTEXT:
15236 ix -= uv >> SAVE_TIGHT_SHIFT;
15238 case SAVEt_AELEM: /* array element */
15239 sv = (const SV *)POPPTR(ss,ix);
15240 TOPPTR(nss,ix) = SvREFCNT_inc(sv_dup_inc(sv, param));
15242 TOPIV(nss,ix) = iv;
15243 av = (const AV *)POPPTR(ss,ix);
15244 TOPPTR(nss,ix) = av_dup_inc(av, param);
15247 ptr = POPPTR(ss,ix);
15248 TOPPTR(nss,ix) = ptr;
15250 case SAVEt_HINTS_HH:
15251 hv = (const HV *)POPPTR(ss,ix);
15252 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
15255 ptr = POPPTR(ss,ix);
15256 ptr = cophh_copy((COPHH*)ptr);
15257 TOPPTR(nss,ix) = ptr;
15259 TOPINT(nss,ix) = i;
15261 case SAVEt_PADSV_AND_MORTALIZE:
15262 longval = (long)POPLONG(ss,ix);
15263 TOPLONG(nss,ix) = longval;
15264 ptr = POPPTR(ss,ix);
15265 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15266 sv = (const SV *)POPPTR(ss,ix);
15267 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15269 case SAVEt_SET_SVFLAGS:
15271 TOPINT(nss,ix) = i;
15273 TOPINT(nss,ix) = i;
15274 sv = (const SV *)POPPTR(ss,ix);
15275 TOPPTR(nss,ix) = sv_dup(sv, param);
15277 case SAVEt_COMPILE_WARNINGS:
15278 ptr = POPPTR(ss,ix);
15279 TOPPTR(nss,ix) = DUP_WARNINGS((STRLEN*)ptr);
15282 ptr = POPPTR(ss,ix);
15283 TOPPTR(nss,ix) = parser_dup((const yy_parser*)ptr, param);
15287 "panic: ss_dup inconsistency (%" IVdf ")", (IV) type);
15295 /* if sv is a stash, call $class->CLONE_SKIP(), and set the SVphv_CLONEABLE
15296 * flag to the result. This is done for each stash before cloning starts,
15297 * so we know which stashes want their objects cloned */
15300 do_mark_cloneable_stash(pTHX_ SV *const sv)
15302 const HEK * const hvname = HvNAME_HEK((const HV *)sv);
15304 GV* const cloner = gv_fetchmethod_autoload(MUTABLE_HV(sv), "CLONE_SKIP", 0);
15305 SvFLAGS(sv) |= SVphv_CLONEABLE; /* clone objects by default */
15306 if (cloner && GvCV(cloner)) {
15313 mXPUSHs(newSVhek(hvname));
15315 call_sv(MUTABLE_SV(GvCV(cloner)), G_SCALAR);
15322 SvFLAGS(sv) &= ~SVphv_CLONEABLE;
15330 =for apidoc perl_clone
15332 Create and return a new interpreter by cloning the current one.
15334 C<perl_clone> takes these flags as parameters:
15336 C<CLONEf_COPY_STACKS> - is used to, well, copy the stacks also,
15337 without it we only clone the data and zero the stacks,
15338 with it we copy the stacks and the new perl interpreter is
15339 ready to run at the exact same point as the previous one.
15340 The pseudo-fork code uses C<COPY_STACKS> while the
15341 threads->create doesn't.
15343 C<CLONEf_KEEP_PTR_TABLE> -
15344 C<perl_clone> keeps a ptr_table with the pointer of the old
15345 variable as a key and the new variable as a value,
15346 this allows it to check if something has been cloned and not
15347 clone it again, but rather just use the value and increase the
15349 If C<KEEP_PTR_TABLE> is not set then C<perl_clone> will kill the ptr_table
15350 using the function S<C<ptr_table_free(PL_ptr_table); PL_ptr_table = NULL;>>.
15351 A reason to keep it around is if you want to dup some of your own
15352 variables which are outside the graph that perl scans.
15354 C<CLONEf_CLONE_HOST> -
15355 This is a win32 thing, it is ignored on unix, it tells perl's
15356 win32host code (which is c++) to clone itself, this is needed on
15357 win32 if you want to run two threads at the same time,
15358 if you just want to do some stuff in a separate perl interpreter
15359 and then throw it away and return to the original one,
15360 you don't need to do anything.
15365 /* XXX the above needs expanding by someone who actually understands it ! */
15366 EXTERN_C PerlInterpreter *
15367 perl_clone_host(PerlInterpreter* proto_perl, UV flags);
15370 perl_clone(PerlInterpreter *proto_perl, UV flags)
15372 #ifdef PERL_IMPLICIT_SYS
15374 PERL_ARGS_ASSERT_PERL_CLONE;
15376 /* perlhost.h so we need to call into it
15377 to clone the host, CPerlHost should have a c interface, sky */
15379 #ifndef __amigaos4__
15380 if (flags & CLONEf_CLONE_HOST) {
15381 return perl_clone_host(proto_perl,flags);
15384 return perl_clone_using(proto_perl, flags,
15386 proto_perl->IMemShared,
15387 proto_perl->IMemParse,
15389 proto_perl->IStdIO,
15393 proto_perl->IProc);
15397 perl_clone_using(PerlInterpreter *proto_perl, UV flags,
15398 struct IPerlMem* ipM, struct IPerlMem* ipMS,
15399 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
15400 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
15401 struct IPerlDir* ipD, struct IPerlSock* ipS,
15402 struct IPerlProc* ipP)
15404 /* XXX many of the string copies here can be optimized if they're
15405 * constants; they need to be allocated as common memory and just
15406 * their pointers copied. */
15409 CLONE_PARAMS clone_params;
15410 CLONE_PARAMS* const param = &clone_params;
15412 PerlInterpreter * const my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
15414 PERL_ARGS_ASSERT_PERL_CLONE_USING;
15415 #else /* !PERL_IMPLICIT_SYS */
15417 CLONE_PARAMS clone_params;
15418 CLONE_PARAMS* param = &clone_params;
15419 PerlInterpreter * const my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
15421 PERL_ARGS_ASSERT_PERL_CLONE;
15422 #endif /* PERL_IMPLICIT_SYS */
15424 /* for each stash, determine whether its objects should be cloned */
15425 S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
15426 PERL_SET_THX(my_perl);
15429 PoisonNew(my_perl, 1, PerlInterpreter);
15432 PL_defstash = NULL; /* may be used by perl malloc() */
15435 PL_scopestack_name = 0;
15437 PL_savestack_ix = 0;
15438 PL_savestack_max = -1;
15439 PL_sig_pending = 0;
15441 PL_eval_begin_nest_depth = proto_perl->Ieval_begin_nest_depth;
15442 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
15443 Zero(&PL_padname_undef, 1, PADNAME);
15444 Zero(&PL_padname_const, 1, PADNAME);
15445 # ifdef DEBUG_LEAKING_SCALARS
15446 PL_sv_serial = (((UV)my_perl >> 2) & 0xfff) * 1000000;
15448 # ifdef PERL_TRACE_OPS
15449 Zero(PL_op_exec_cnt, OP_max+2, UV);
15451 #else /* !DEBUGGING */
15452 Zero(my_perl, 1, PerlInterpreter);
15453 #endif /* DEBUGGING */
15455 #ifdef PERL_IMPLICIT_SYS
15456 /* host pointers */
15458 PL_MemShared = ipMS;
15459 PL_MemParse = ipMP;
15466 #endif /* PERL_IMPLICIT_SYS */
15469 param->flags = flags;
15470 /* Nothing in the core code uses this, but we make it available to
15471 extensions (using mg_dup). */
15472 param->proto_perl = proto_perl;
15473 /* Likely nothing will use this, but it is initialised to be consistent
15474 with Perl_clone_params_new(). */
15475 param->new_perl = my_perl;
15476 param->unreferenced = NULL;
15479 INIT_TRACK_MEMPOOL(my_perl->Imemory_debug_header, my_perl);
15481 PL_body_arenas = NULL;
15482 Zero(&PL_body_roots, 1, PL_body_roots);
15486 PL_sv_arenaroot = NULL;
15488 PL_debug = proto_perl->Idebug;
15490 /* dbargs array probably holds garbage */
15493 PL_compiling = proto_perl->Icompiling;
15495 /* pseudo environmental stuff */
15496 PL_origargc = proto_perl->Iorigargc;
15497 PL_origargv = proto_perl->Iorigargv;
15499 #ifndef NO_TAINT_SUPPORT
15500 /* Set tainting stuff before PerlIO_debug can possibly get called */
15501 PL_tainting = proto_perl->Itainting;
15502 PL_taint_warn = proto_perl->Itaint_warn;
15504 PL_tainting = FALSE;
15505 PL_taint_warn = FALSE;
15508 PL_minus_c = proto_perl->Iminus_c;
15510 PL_localpatches = proto_perl->Ilocalpatches;
15511 PL_splitstr = proto_perl->Isplitstr;
15512 PL_minus_n = proto_perl->Iminus_n;
15513 PL_minus_p = proto_perl->Iminus_p;
15514 PL_minus_l = proto_perl->Iminus_l;
15515 PL_minus_a = proto_perl->Iminus_a;
15516 PL_minus_E = proto_perl->Iminus_E;
15517 PL_minus_F = proto_perl->Iminus_F;
15518 PL_doswitches = proto_perl->Idoswitches;
15519 PL_dowarn = proto_perl->Idowarn;
15520 #ifdef PERL_SAWAMPERSAND
15521 PL_sawampersand = proto_perl->Isawampersand;
15523 PL_unsafe = proto_perl->Iunsafe;
15524 PL_perldb = proto_perl->Iperldb;
15525 PL_perl_destruct_level = proto_perl->Iperl_destruct_level;
15526 PL_exit_flags = proto_perl->Iexit_flags;
15528 /* XXX time(&PL_basetime) when asked for? */
15529 PL_basetime = proto_perl->Ibasetime;
15531 PL_maxsysfd = proto_perl->Imaxsysfd;
15532 PL_statusvalue = proto_perl->Istatusvalue;
15534 PL_statusvalue_vms = proto_perl->Istatusvalue_vms;
15536 PL_statusvalue_posix = proto_perl->Istatusvalue_posix;
15539 /* RE engine related */
15540 PL_regmatch_slab = NULL;
15541 PL_reg_curpm = NULL;
15543 PL_sub_generation = proto_perl->Isub_generation;
15545 /* funky return mechanisms */
15546 PL_forkprocess = proto_perl->Iforkprocess;
15548 /* internal state */
15549 PL_main_start = proto_perl->Imain_start;
15550 PL_eval_root = proto_perl->Ieval_root;
15551 PL_eval_start = proto_perl->Ieval_start;
15553 PL_filemode = proto_perl->Ifilemode;
15554 PL_lastfd = proto_perl->Ilastfd;
15555 PL_oldname = proto_perl->Ioldname; /* XXX not quite right */
15556 PL_gensym = proto_perl->Igensym;
15558 PL_laststatval = proto_perl->Ilaststatval;
15559 PL_laststype = proto_perl->Ilaststype;
15562 PL_profiledata = NULL;
15564 PL_generation = proto_perl->Igeneration;
15566 PL_in_clean_objs = proto_perl->Iin_clean_objs;
15567 PL_in_clean_all = proto_perl->Iin_clean_all;
15569 PL_delaymagic_uid = proto_perl->Idelaymagic_uid;
15570 PL_delaymagic_euid = proto_perl->Idelaymagic_euid;
15571 PL_delaymagic_gid = proto_perl->Idelaymagic_gid;
15572 PL_delaymagic_egid = proto_perl->Idelaymagic_egid;
15573 PL_nomemok = proto_perl->Inomemok;
15574 PL_an = proto_perl->Ian;
15575 PL_evalseq = proto_perl->Ievalseq;
15576 PL_origalen = proto_perl->Iorigalen;
15578 PL_sighandlerp = proto_perl->Isighandlerp;
15579 PL_sighandler1p = proto_perl->Isighandler1p;
15580 PL_sighandler3p = proto_perl->Isighandler3p;
15582 PL_runops = proto_perl->Irunops;
15584 PL_subline = proto_perl->Isubline;
15586 PL_cv_has_eval = proto_perl->Icv_has_eval;
15588 #ifdef USE_LOCALE_COLLATE
15589 PL_collation_ix = proto_perl->Icollation_ix;
15590 PL_collation_standard = proto_perl->Icollation_standard;
15591 PL_collxfrm_base = proto_perl->Icollxfrm_base;
15592 PL_collxfrm_mult = proto_perl->Icollxfrm_mult;
15593 PL_strxfrm_max_cp = proto_perl->Istrxfrm_max_cp;
15594 PL_strxfrm_is_behaved = proto_perl->Istrxfrm_is_behaved;
15595 PL_strxfrm_NUL_replacement = proto_perl->Istrxfrm_NUL_replacement;
15596 #endif /* USE_LOCALE_COLLATE */
15598 #ifdef USE_LOCALE_NUMERIC
15599 PL_numeric_standard = proto_perl->Inumeric_standard;
15600 PL_numeric_underlying = proto_perl->Inumeric_underlying;
15601 PL_numeric_underlying_is_standard = proto_perl->Inumeric_underlying_is_standard;
15602 #endif /* !USE_LOCALE_NUMERIC */
15604 /* Did the locale setup indicate UTF-8? */
15605 PL_utf8locale = proto_perl->Iutf8locale;
15607 #ifdef USE_LOCALE_THREADS
15608 assert(PL_locale_mutex_depth <= 0);
15609 PL_locale_mutex_depth = 0;
15611 /* Unicode features (see perlrun/-C) */
15612 PL_unicode = proto_perl->Iunicode;
15614 /* Pre-5.8 signals control */
15615 PL_signals = proto_perl->Isignals;
15617 /* times() ticks per second */
15618 PL_clocktick = proto_perl->Iclocktick;
15620 /* Recursion stopper for PerlIO_find_layer */
15621 PL_in_load_module = proto_perl->Iin_load_module;
15623 /* Not really needed/useful since the reenrant_retint is "volatile",
15624 * but do it for consistency's sake. */
15625 PL_reentrant_retint = proto_perl->Ireentrant_retint;
15627 /* Hooks to shared SVs and locks. */
15628 PL_sharehook = proto_perl->Isharehook;
15629 PL_lockhook = proto_perl->Ilockhook;
15630 PL_unlockhook = proto_perl->Iunlockhook;
15631 PL_threadhook = proto_perl->Ithreadhook;
15632 PL_destroyhook = proto_perl->Idestroyhook;
15633 PL_signalhook = proto_perl->Isignalhook;
15635 PL_globhook = proto_perl->Iglobhook;
15637 PL_srand_called = proto_perl->Isrand_called;
15638 Copy(&(proto_perl->Irandom_state), &PL_random_state, 1, PL_RANDOM_STATE_TYPE);
15639 PL_srand_override = proto_perl->Isrand_override;
15640 PL_srand_override_next = proto_perl->Isrand_override_next;
15642 if (flags & CLONEf_COPY_STACKS) {
15643 /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */
15644 PL_tmps_ix = proto_perl->Itmps_ix;
15645 PL_tmps_max = proto_perl->Itmps_max;
15646 PL_tmps_floor = proto_perl->Itmps_floor;
15648 /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
15649 * NOTE: unlike the others! */
15650 PL_scopestack_ix = proto_perl->Iscopestack_ix;
15651 PL_scopestack_max = proto_perl->Iscopestack_max;
15653 /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix]
15654 * NOTE: unlike the others! */
15655 PL_savestack_ix = proto_perl->Isavestack_ix;
15656 PL_savestack_max = proto_perl->Isavestack_max;
15659 PL_start_env = proto_perl->Istart_env; /* XXXXXX */
15660 PL_top_env = &PL_start_env;
15662 PL_op = proto_perl->Iop;
15665 PL_Xpv = (XPV*)NULL;
15666 my_perl->Ina = proto_perl->Ina;
15668 PL_statcache = proto_perl->Istatcache;
15670 #ifndef NO_TAINT_SUPPORT
15671 PL_tainted = proto_perl->Itainted;
15673 PL_tainted = FALSE;
15675 PL_curpm = proto_perl->Icurpm; /* XXX No PMOP ref count */
15677 PL_chopset = proto_perl->Ichopset; /* XXX never deallocated */
15679 PL_restartjmpenv = proto_perl->Irestartjmpenv;
15680 PL_restartop = proto_perl->Irestartop;
15681 PL_in_eval = proto_perl->Iin_eval;
15682 PL_delaymagic = proto_perl->Idelaymagic;
15683 PL_phase = proto_perl->Iphase;
15684 PL_localizing = proto_perl->Ilocalizing;
15686 PL_hv_fetch_ent_mh = NULL;
15687 PL_modcount = proto_perl->Imodcount;
15688 PL_lastgotoprobe = NULL;
15689 PL_dumpindent = proto_perl->Idumpindent;
15691 PL_efloatbuf = NULL; /* reinits on demand */
15692 PL_efloatsize = 0; /* reinits on demand */
15696 PL_colorset = 0; /* reinits PL_colors[] */
15697 /*PL_colors[6] = {0,0,0,0,0,0};*/
15699 /* Pluggable optimizer */
15700 PL_peepp = proto_perl->Ipeepp;
15701 PL_rpeepp = proto_perl->Irpeepp;
15702 /* op_free() hook */
15703 PL_opfreehook = proto_perl->Iopfreehook;
15705 # ifdef PERL_MEM_LOG
15706 Zero(PL_mem_log, sizeof(PL_mem_log), char);
15709 #ifdef USE_REENTRANT_API
15710 /* XXX: things like -Dm will segfault here in perlio, but doing
15711 * PERL_SET_CONTEXT(proto_perl);
15712 * breaks too many other things
15714 Perl_reentrant_init(aTHX);
15717 /* create SV map for pointer relocation */
15718 PL_ptr_table = ptr_table_new();
15720 /* initialize these special pointers as early as possible */
15722 ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef);
15723 ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no);
15724 ptr_table_store(PL_ptr_table, &proto_perl->Isv_zero, &PL_sv_zero);
15725 ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes);
15726 ptr_table_store(PL_ptr_table, &proto_perl->Ipadname_const,
15727 &PL_padname_const);
15729 /* create (a non-shared!) shared string table */
15730 PL_strtab = newHV();
15731 HvSHAREKEYS_off(PL_strtab);
15732 hv_ksplit(PL_strtab, HvTOTALKEYS(proto_perl->Istrtab));
15733 ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab);
15735 Zero(PL_sv_consts, SV_CONSTS_COUNT, SV*);
15737 /* This PV will be free'd special way so must set it same way op.c does */
15738 PL_compiling.cop_file = savesharedpv(PL_compiling.cop_file);
15739 ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_file, PL_compiling.cop_file);
15741 ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling);
15742 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
15743 CopHINTHASH_set(&PL_compiling, cophh_copy(CopHINTHASH_get(&PL_compiling)));
15744 PL_curcop = (COP*)any_dup(proto_perl->Icurcop, proto_perl);
15746 param->stashes = newAV(); /* Setup array of objects to call clone on */
15747 /* This makes no difference to the implementation, as it always pushes
15748 and shifts pointers to other SVs without changing their reference
15749 count, with the array becoming empty before it is freed. However, it
15750 makes it conceptually clear what is going on, and will avoid some
15751 work inside av.c, filling slots between AvFILL() and AvMAX() with
15752 &PL_sv_undef, and SvREFCNT_dec()ing those. */
15753 AvREAL_off(param->stashes);
15755 if (!(flags & CLONEf_COPY_STACKS)) {
15756 param->unreferenced = newAV();
15759 #ifdef PERLIO_LAYERS
15760 /* Clone PerlIO tables as soon as we can handle general xx_dup() */
15761 PerlIO_clone(aTHX_ proto_perl, param);
15764 PL_envgv = gv_dup_inc(proto_perl->Ienvgv, param);
15765 PL_incgv = gv_dup_inc(proto_perl->Iincgv, param);
15766 PL_hintgv = gv_dup_inc(proto_perl->Ihintgv, param);
15767 PL_origfilename = SAVEPV(proto_perl->Iorigfilename);
15768 PL_xsubfilename = proto_perl->Ixsubfilename;
15769 PL_diehook = sv_dup_inc(proto_perl->Idiehook, param);
15770 PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook, param);
15773 PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel, param);
15774 PL_inplace = SAVEPV(proto_perl->Iinplace);
15775 PL_e_script = sv_dup_inc(proto_perl->Ie_script, param);
15777 /* magical thingies */
15779 SvPVCLEAR(PERL_DEBUG_PAD(0)); /* For regex debugging. */
15780 SvPVCLEAR(PERL_DEBUG_PAD(1)); /* ext/re needs these */
15781 SvPVCLEAR(PERL_DEBUG_PAD(2)); /* even without DEBUGGING. */
15784 /* Clone the regex array */
15785 /* ORANGE FIXME for plugins, probably in the SV dup code.
15786 newSViv(PTR2IV(CALLREGDUPE(
15787 INT2PTR(REGEXP *, SvIVX(regex)), param))))
15789 PL_regex_padav = av_dup_inc(proto_perl->Iregex_padav, param);
15790 PL_regex_pad = AvARRAY(PL_regex_padav);
15792 PL_stashpadmax = proto_perl->Istashpadmax;
15793 PL_stashpadix = proto_perl->Istashpadix ;
15794 Newx(PL_stashpad, PL_stashpadmax, HV *);
15797 for (; o < PL_stashpadmax; ++o)
15798 PL_stashpad[o] = hv_dup(proto_perl->Istashpad[o], param);
15801 /* shortcuts to various I/O objects */
15802 PL_ofsgv = gv_dup_inc(proto_perl->Iofsgv, param);
15803 PL_stdingv = gv_dup(proto_perl->Istdingv, param);
15804 PL_stderrgv = gv_dup(proto_perl->Istderrgv, param);
15805 PL_defgv = gv_dup(proto_perl->Idefgv, param);
15806 PL_argvgv = gv_dup_inc(proto_perl->Iargvgv, param);
15807 PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv, param);
15808 PL_argvout_stack = av_dup_inc(proto_perl->Iargvout_stack, param);
15810 /* shortcuts to regexp stuff */
15811 PL_replgv = gv_dup_inc(proto_perl->Ireplgv, param);
15813 /* shortcuts to misc objects */
15814 PL_errgv = gv_dup(proto_perl->Ierrgv, param);
15816 /* shortcuts to debugging objects */
15817 PL_DBgv = gv_dup_inc(proto_perl->IDBgv, param);
15818 PL_DBline = gv_dup_inc(proto_perl->IDBline, param);
15819 PL_DBsub = gv_dup_inc(proto_perl->IDBsub, param);
15820 PL_DBsingle = sv_dup(proto_perl->IDBsingle, param);
15821 PL_DBtrace = sv_dup(proto_perl->IDBtrace, param);
15822 PL_DBsignal = sv_dup(proto_perl->IDBsignal, param);
15823 Copy(proto_perl->IDBcontrol, PL_DBcontrol, DBVARMG_COUNT, IV);
15825 /* symbol tables */
15826 PL_defstash = hv_dup_inc(proto_perl->Idefstash, param);
15827 PL_curstash = hv_dup_inc(proto_perl->Icurstash, param);
15828 PL_debstash = hv_dup(proto_perl->Idebstash, param);
15829 PL_globalstash = hv_dup(proto_perl->Iglobalstash, param);
15830 PL_curstname = sv_dup_inc(proto_perl->Icurstname, param);
15832 PL_beginav = av_dup_inc(proto_perl->Ibeginav, param);
15833 PL_beginav_save = av_dup_inc(proto_perl->Ibeginav_save, param);
15834 PL_checkav_save = av_dup_inc(proto_perl->Icheckav_save, param);
15835 PL_unitcheckav = av_dup_inc(proto_perl->Iunitcheckav, param);
15836 PL_unitcheckav_save = av_dup_inc(proto_perl->Iunitcheckav_save, param);
15837 PL_endav = av_dup_inc(proto_perl->Iendav, param);
15838 PL_checkav = av_dup_inc(proto_perl->Icheckav, param);
15839 PL_initav = av_dup_inc(proto_perl->Iinitav, param);
15840 PL_savebegin = proto_perl->Isavebegin;
15842 PL_isarev = hv_dup_inc(proto_perl->Iisarev, param);
15844 /* subprocess state */
15845 PL_fdpid = av_dup_inc(proto_perl->Ifdpid, param);
15847 if (proto_perl->Iop_mask)
15848 PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo);
15851 /* PL_asserting = proto_perl->Iasserting; */
15853 /* current interpreter roots */
15854 PL_main_cv = cv_dup_inc(proto_perl->Imain_cv, param);
15856 PL_main_root = OpREFCNT_inc(proto_perl->Imain_root);
15859 /* runtime control stuff */
15860 PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl);
15862 PL_preambleav = av_dup_inc(proto_perl->Ipreambleav, param);
15864 PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv, param);
15866 /* interpreter atexit processing */
15867 PL_exitlistlen = proto_perl->Iexitlistlen;
15868 if (PL_exitlistlen) {
15869 Newx(PL_exitlist, PL_exitlistlen, PerlExitListEntry);
15870 Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
15873 PL_exitlist = (PerlExitListEntry*)NULL;
15875 PL_my_cxt_size = proto_perl->Imy_cxt_size;
15876 if (PL_my_cxt_size) {
15877 Newx(PL_my_cxt_list, PL_my_cxt_size, void *);
15878 Copy(proto_perl->Imy_cxt_list, PL_my_cxt_list, PL_my_cxt_size, void *);
15881 PL_my_cxt_list = (void**)NULL;
15883 PL_modglobal = hv_dup_inc(proto_perl->Imodglobal, param);
15884 PL_custom_op_names = hv_dup_inc(proto_perl->Icustom_op_names,param);
15885 PL_custom_op_descs = hv_dup_inc(proto_perl->Icustom_op_descs,param);
15886 PL_custom_ops = hv_dup_inc(proto_perl->Icustom_ops, param);
15888 PL_compcv = cv_dup(proto_perl->Icompcv, param);
15890 PAD_CLONE_VARS(proto_perl, param);
15892 #ifdef HAVE_INTERP_INTERN
15893 sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern);
15896 PL_DBcv = cv_dup(proto_perl->IDBcv, param);
15898 #ifdef PERL_USES_PL_PIDSTATUS
15899 PL_pidstatus = newHV(); /* XXX flag for cloning? */
15901 PL_osname = SAVEPV(proto_perl->Iosname);
15902 PL_parser = parser_dup(proto_perl->Iparser, param);
15904 /* XXX this only works if the saved cop has already been cloned */
15905 if (proto_perl->Iparser) {
15906 PL_parser->saved_curcop = (COP*)any_dup(
15907 proto_perl->Iparser->saved_curcop,
15911 PL_subname = sv_dup_inc(proto_perl->Isubname, param);
15913 #ifdef USE_PL_CURLOCALES
15914 for (i = 0; i < (int) C_ARRAY_LENGTH(PL_curlocales); i++) {
15915 PL_curlocales[i] = SAVEPV(proto_perl->Icurlocales[i]);
15918 #ifdef USE_LOCALE_CTYPE
15919 Copy(proto_perl->Ifold_locale, PL_fold_locale, 256, U8);
15920 /* Should we warn if uses locale? */
15921 PL_ctype_name = SAVEPV(proto_perl->Ictype_name);
15922 PL_warn_locale = sv_dup_inc(proto_perl->Iwarn_locale, param);
15923 PL_utf8locale = proto_perl->Iutf8locale;
15924 PL_in_utf8_CTYPE_locale = proto_perl->Iin_utf8_CTYPE_locale;
15925 PL_in_utf8_turkic_locale = proto_perl->Iin_utf8_turkic_locale;
15928 #ifdef USE_LOCALE_COLLATE
15929 PL_in_utf8_COLLATE_locale = proto_perl->Iin_utf8_COLLATE_locale;
15930 PL_collation_name = SAVEPV(proto_perl->Icollation_name);
15931 #endif /* USE_LOCALE_COLLATE */
15933 #ifdef USE_LOCALE_NUMERIC
15934 PL_numeric_name = SAVEPV(proto_perl->Inumeric_name);
15935 PL_numeric_radix_sv = sv_dup_inc(proto_perl->Inumeric_radix_sv, param);
15936 PL_underlying_radix_sv = sv_dup_inc(proto_perl->Iunderlying_radix_sv, param);
15938 # if defined(USE_POSIX_2008_LOCALE)
15939 PL_underlying_numeric_obj = NULL;
15941 #endif /* !USE_LOCALE_NUMERIC */
15942 #if defined(USE_POSIX_2008_LOCALE)
15943 PL_scratch_locale_obj = NULL;
15947 PL_mbrlen_ps = proto_perl->Imbrlen_ps;
15950 PL_mbrtowc_ps = proto_perl->Imbrtowc_ps;
15953 PL_wcrtomb_ps = proto_perl->Iwcrtomb_ps;
15956 PL_langinfo_buf = NULL;
15957 PL_langinfo_bufsize = 0;
15959 PL_setlocale_buf = NULL;
15960 PL_setlocale_bufsize = 0;
15962 PL_stdize_locale_buf = NULL;
15963 PL_stdize_locale_bufsize = 0;
15965 /* Unicode inversion lists */
15967 PL_AboveLatin1 = sv_dup_inc(proto_perl->IAboveLatin1, param);
15968 PL_Assigned_invlist = sv_dup_inc(proto_perl->IAssigned_invlist, param);
15969 PL_GCB_invlist = sv_dup_inc(proto_perl->IGCB_invlist, param);
15970 PL_HasMultiCharFold = sv_dup_inc(proto_perl->IHasMultiCharFold, param);
15971 PL_InMultiCharFold = sv_dup_inc(proto_perl->IInMultiCharFold, param);
15972 PL_Latin1 = sv_dup_inc(proto_perl->ILatin1, param);
15973 PL_LB_invlist = sv_dup_inc(proto_perl->ILB_invlist, param);
15974 PL_SB_invlist = sv_dup_inc(proto_perl->ISB_invlist, param);
15975 PL_SCX_invlist = sv_dup_inc(proto_perl->ISCX_invlist, param);
15976 PL_UpperLatin1 = sv_dup_inc(proto_perl->IUpperLatin1, param);
15977 PL_in_some_fold = sv_dup_inc(proto_perl->Iin_some_fold, param);
15978 PL_utf8_foldclosures = sv_dup_inc(proto_perl->Iutf8_foldclosures, param);
15979 PL_utf8_idcont = sv_dup_inc(proto_perl->Iutf8_idcont, param);
15980 PL_utf8_idstart = sv_dup_inc(proto_perl->Iutf8_idstart, param);
15981 PL_utf8_perl_idcont = sv_dup_inc(proto_perl->Iutf8_perl_idcont, param);
15982 PL_utf8_perl_idstart = sv_dup_inc(proto_perl->Iutf8_perl_idstart, param);
15983 PL_utf8_xidcont = sv_dup_inc(proto_perl->Iutf8_xidcont, param);
15984 PL_utf8_xidstart = sv_dup_inc(proto_perl->Iutf8_xidstart, param);
15985 PL_WB_invlist = sv_dup_inc(proto_perl->IWB_invlist, param);
15986 for (i = 0; i < POSIX_CC_COUNT; i++) {
15987 PL_XPosix_ptrs[i] = sv_dup_inc(proto_perl->IXPosix_ptrs[i], param);
15988 if (i != CC_CASED_ && i != CC_VERTSPACE_) {
15989 PL_Posix_ptrs[i] = sv_dup_inc(proto_perl->IPosix_ptrs[i], param);
15992 PL_Posix_ptrs[CC_CASED_] = PL_Posix_ptrs[CC_ALPHA_];
15993 PL_Posix_ptrs[CC_VERTSPACE_] = NULL;
15995 PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper, param);
15996 PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle, param);
15997 PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower, param);
15998 PL_utf8_tofold = sv_dup_inc(proto_perl->Iutf8_tofold, param);
15999 PL_utf8_tosimplefold = sv_dup_inc(proto_perl->Iutf8_tosimplefold, param);
16000 PL_utf8_charname_begin = sv_dup_inc(proto_perl->Iutf8_charname_begin, param);
16001 PL_utf8_charname_continue = sv_dup_inc(proto_perl->Iutf8_charname_continue, param);
16002 PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark, param);
16003 PL_InBitmap = sv_dup_inc(proto_perl->IInBitmap, param);
16004 PL_CCC_non0_non230 = sv_dup_inc(proto_perl->ICCC_non0_non230, param);
16005 PL_Private_Use = sv_dup_inc(proto_perl->IPrivate_Use, param);
16008 PL_seen_deprecated_macro = hv_dup_inc(proto_perl->Iseen_deprecated_macro, param);
16011 if (proto_perl->Ipsig_pend) {
16012 Newxz(PL_psig_pend, SIG_SIZE, int);
16015 PL_psig_pend = (int*)NULL;
16018 if (proto_perl->Ipsig_name) {
16019 Newx(PL_psig_name, 2 * SIG_SIZE, SV*);
16020 sv_dup_inc_multiple(proto_perl->Ipsig_name, PL_psig_name, 2 * SIG_SIZE,
16022 PL_psig_ptr = PL_psig_name + SIG_SIZE;
16025 PL_psig_ptr = (SV**)NULL;
16026 PL_psig_name = (SV**)NULL;
16029 if (flags & CLONEf_COPY_STACKS) {
16030 Newx(PL_tmps_stack, PL_tmps_max, SV*);
16031 sv_dup_inc_multiple(proto_perl->Itmps_stack, PL_tmps_stack,
16032 PL_tmps_ix+1, param);
16034 /* next PUSHMARK() sets *(PL_markstack_ptr+1) */
16035 i = proto_perl->Imarkstack_max - proto_perl->Imarkstack;
16036 Newx(PL_markstack, i, I32);
16037 PL_markstack_max = PL_markstack + (proto_perl->Imarkstack_max
16038 - proto_perl->Imarkstack);
16039 PL_markstack_ptr = PL_markstack + (proto_perl->Imarkstack_ptr
16040 - proto_perl->Imarkstack);
16041 Copy(proto_perl->Imarkstack, PL_markstack,
16042 PL_markstack_ptr - PL_markstack + 1, I32);
16044 /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
16045 * NOTE: unlike the others! */
16046 Newx(PL_scopestack, PL_scopestack_max, I32);
16047 Copy(proto_perl->Iscopestack, PL_scopestack, PL_scopestack_ix, I32);
16050 Newx(PL_scopestack_name, PL_scopestack_max, const char *);
16051 Copy(proto_perl->Iscopestack_name, PL_scopestack_name, PL_scopestack_ix, const char *);
16053 /* reset stack AV to correct length before its duped via
16054 * PL_curstackinfo */
16055 AvFILLp(proto_perl->Icurstack) =
16056 proto_perl->Istack_sp - proto_perl->Istack_base;
16058 /* NOTE: si_dup() looks at PL_markstack */
16059 PL_curstackinfo = si_dup(proto_perl->Icurstackinfo, param);
16061 /* PL_curstack = PL_curstackinfo->si_stack; */
16062 PL_curstack = av_dup(proto_perl->Icurstack, param);
16063 PL_mainstack = av_dup(proto_perl->Imainstack, param);
16065 /* next PUSHs() etc. set *(PL_stack_sp+1) */
16066 PL_stack_base = AvARRAY(PL_curstack);
16067 PL_stack_sp = PL_stack_base + (proto_perl->Istack_sp
16068 - proto_perl->Istack_base);
16069 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
16071 /*Newxz(PL_savestack, PL_savestack_max, ANY);*/
16072 PL_savestack = ss_dup(proto_perl, param);
16076 ENTER; /* perl_destruct() wants to LEAVE; */
16079 PL_statgv = gv_dup(proto_perl->Istatgv, param);
16080 PL_statname = sv_dup_inc(proto_perl->Istatname, param);
16082 PL_rs = sv_dup_inc(proto_perl->Irs, param);
16083 PL_last_in_gv = gv_dup(proto_perl->Ilast_in_gv, param);
16084 PL_defoutgv = gv_dup_inc(proto_perl->Idefoutgv, param);
16085 PL_toptarget = sv_dup_inc(proto_perl->Itoptarget, param);
16086 PL_bodytarget = sv_dup_inc(proto_perl->Ibodytarget, param);
16087 PL_formtarget = sv_dup(proto_perl->Iformtarget, param);
16089 PL_errors = sv_dup_inc(proto_perl->Ierrors, param);
16091 PL_sortcop = (OP*)any_dup(proto_perl->Isortcop, proto_perl);
16092 PL_firstgv = gv_dup_inc(proto_perl->Ifirstgv, param);
16093 PL_secondgv = gv_dup_inc(proto_perl->Isecondgv, param);
16095 PL_stashcache = newHV();
16097 PL_watchaddr = (char **) ptr_table_fetch(PL_ptr_table,
16098 proto_perl->Iwatchaddr);
16099 PL_watchok = PL_watchaddr ? * PL_watchaddr : NULL;
16100 if (PL_debug && PL_watchaddr) {
16101 PerlIO_printf(Perl_debug_log,
16102 "WATCHING: %" UVxf " cloned as %" UVxf " with value %" UVxf "\n",
16103 PTR2UV(proto_perl->Iwatchaddr), PTR2UV(PL_watchaddr),
16104 PTR2UV(PL_watchok));
16107 PL_registered_mros = hv_dup_inc(proto_perl->Iregistered_mros, param);
16108 PL_blockhooks = av_dup_inc(proto_perl->Iblockhooks, param);
16110 /* Call the ->CLONE method, if it exists, for each of the stashes
16111 identified by sv_dup() above.
16113 while(av_count(param->stashes) != 0) {
16114 HV* const stash = MUTABLE_HV(av_shift(param->stashes));
16115 GV* const cloner = gv_fetchmethod_autoload(stash, "CLONE", 0);
16116 if (cloner && GvCV(cloner)) {
16121 mXPUSHs(newSVhek(HvNAME_HEK(stash)));
16123 call_sv(MUTABLE_SV(GvCV(cloner)), G_DISCARD);
16129 if (!(flags & CLONEf_KEEP_PTR_TABLE)) {
16130 ptr_table_free(PL_ptr_table);
16131 PL_ptr_table = NULL;
16134 if (!(flags & CLONEf_COPY_STACKS)) {
16135 unreferenced_to_tmp_stack(param->unreferenced);
16138 SvREFCNT_dec(param->stashes);
16140 /* orphaned? eg threads->new inside BEGIN or use */
16141 if (PL_compcv && ! SvREFCNT(PL_compcv)) {
16142 SvREFCNT_inc_simple_void(PL_compcv);
16143 SAVEFREESV(PL_compcv);
16150 S_unreferenced_to_tmp_stack(pTHX_ AV *const unreferenced)
16152 PERL_ARGS_ASSERT_UNREFERENCED_TO_TMP_STACK;
16154 if (AvFILLp(unreferenced) > -1) {
16155 SV **svp = AvARRAY(unreferenced);
16156 SV **const last = svp + AvFILLp(unreferenced);
16160 if (SvREFCNT(*svp) == 1)
16162 } while (++svp <= last);
16164 EXTEND_MORTAL(count);
16165 svp = AvARRAY(unreferenced);
16168 if (SvREFCNT(*svp) == 1) {
16169 /* Our reference is the only one to this SV. This means that
16170 in this thread, the scalar effectively has a 0 reference.
16171 That doesn't work (cleanup never happens), so donate our
16172 reference to it onto the save stack. */
16173 PL_tmps_stack[++PL_tmps_ix] = *svp;
16175 /* As an optimisation, because we are already walking the
16176 entire array, instead of above doing either
16177 SvREFCNT_inc(*svp) or *svp = &PL_sv_undef, we can instead
16178 release our reference to the scalar, so that at the end of
16179 the array owns zero references to the scalars it happens to
16180 point to. We are effectively converting the array from
16181 AvREAL() on to AvREAL() off. This saves the av_clear()
16182 (triggered by the SvREFCNT_dec(unreferenced) below) from
16183 walking the array a second time. */
16184 SvREFCNT_dec(*svp);
16187 } while (++svp <= last);
16188 AvREAL_off(unreferenced);
16190 SvREFCNT_dec_NN(unreferenced);
16194 Perl_clone_params_del(CLONE_PARAMS *param)
16196 PerlInterpreter *const was = PERL_GET_THX;
16197 PerlInterpreter *const to = param->new_perl;
16200 PERL_ARGS_ASSERT_CLONE_PARAMS_DEL;
16206 SvREFCNT_dec(param->stashes);
16207 if (param->unreferenced)
16208 unreferenced_to_tmp_stack(param->unreferenced);
16218 Perl_clone_params_new(PerlInterpreter *const from, PerlInterpreter *const to)
16220 /* Need to play this game, as newAV() can call safesysmalloc(), and that
16221 does a dTHX; to get the context from thread local storage.
16222 FIXME - under PERL_CORE Newx(), Safefree() and friends should expand to
16223 a version that passes in my_perl. */
16224 PerlInterpreter *const was = PERL_GET_THX;
16225 CLONE_PARAMS *param;
16227 PERL_ARGS_ASSERT_CLONE_PARAMS_NEW;
16233 /* Given that we've set the context, we can do this unshared. */
16234 Newx(param, 1, CLONE_PARAMS);
16237 param->proto_perl = from;
16238 param->new_perl = to;
16239 param->stashes = (AV *)Perl_newSV_type(to, SVt_PVAV);
16240 AvREAL_off(param->stashes);
16241 param->unreferenced = (AV *)Perl_newSV_type(to, SVt_PVAV);
16249 #endif /* USE_ITHREADS */
16252 Perl_init_constants(pTHX)
16255 SvREFCNT(&PL_sv_undef) = SvREFCNT_IMMORTAL;
16256 SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVf_PROTECT|SVt_NULL;
16257 SvANY(&PL_sv_undef) = NULL;
16259 SvANY(&PL_sv_no) = new_XPVNV();
16260 SvREFCNT(&PL_sv_no) = SvREFCNT_IMMORTAL;
16261 SvFLAGS(&PL_sv_no) = SVt_PVNV|SVf_READONLY|SVf_PROTECT
16262 |SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
16263 |SVp_POK|SVf_POK|SVf_IsCOW|SVppv_STATIC;
16265 SvANY(&PL_sv_yes) = new_XPVNV();
16266 SvREFCNT(&PL_sv_yes) = SvREFCNT_IMMORTAL;
16267 SvFLAGS(&PL_sv_yes) = SVt_PVNV|SVf_READONLY|SVf_PROTECT
16268 |SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
16269 |SVp_POK|SVf_POK|SVf_IsCOW|SVppv_STATIC;
16271 SvANY(&PL_sv_zero) = new_XPVNV();
16272 SvREFCNT(&PL_sv_zero) = SvREFCNT_IMMORTAL;
16273 SvFLAGS(&PL_sv_zero) = SVt_PVNV|SVf_READONLY|SVf_PROTECT
16274 |SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
16278 SvPV_set(&PL_sv_no, (char*)PL_No);
16279 SvCUR_set(&PL_sv_no, 0);
16280 SvLEN_set(&PL_sv_no, 0);
16281 SvIV_set(&PL_sv_no, 0);
16282 SvNV_set(&PL_sv_no, 0);
16284 SvPV_set(&PL_sv_yes, (char*)PL_Yes);
16285 SvCUR_set(&PL_sv_yes, 1);
16286 SvLEN_set(&PL_sv_yes, 0);
16287 SvIV_set(&PL_sv_yes, 1);
16288 SvNV_set(&PL_sv_yes, 1);
16290 SvPV_set(&PL_sv_zero, (char*)PL_Zero);
16291 SvCUR_set(&PL_sv_zero, 1);
16292 SvLEN_set(&PL_sv_zero, 0);
16293 SvIV_set(&PL_sv_zero, 0);
16294 SvNV_set(&PL_sv_zero, 0);
16296 PadnamePV(&PL_padname_const) = (char *)PL_No;
16298 assert(SvIMMORTAL_INTERP(&PL_sv_yes));
16299 assert(SvIMMORTAL_INTERP(&PL_sv_undef));
16300 assert(SvIMMORTAL_INTERP(&PL_sv_no));
16301 assert(SvIMMORTAL_INTERP(&PL_sv_zero));
16303 assert(SvIMMORTAL(&PL_sv_yes));
16304 assert(SvIMMORTAL(&PL_sv_undef));
16305 assert(SvIMMORTAL(&PL_sv_no));
16306 assert(SvIMMORTAL(&PL_sv_zero));
16308 assert( SvIMMORTAL_TRUE(&PL_sv_yes));
16309 assert(!SvIMMORTAL_TRUE(&PL_sv_undef));
16310 assert(!SvIMMORTAL_TRUE(&PL_sv_no));
16311 assert(!SvIMMORTAL_TRUE(&PL_sv_zero));
16313 assert( SvTRUE_nomg_NN(&PL_sv_yes));
16314 assert(!SvTRUE_nomg_NN(&PL_sv_undef));
16315 assert(!SvTRUE_nomg_NN(&PL_sv_no));
16316 assert(!SvTRUE_nomg_NN(&PL_sv_zero));
16320 =for apidoc_section $unicode
16322 =for apidoc sv_recode_to_utf8
16324 C<encoding> is assumed to be an C<Encode> object, on entry the PV
16325 of C<sv> is assumed to be octets in that encoding, and C<sv>
16326 will be converted into Unicode (and UTF-8).
16328 If C<sv> already is UTF-8 (or if it is not C<POK>), or if C<encoding>
16329 is not a reference, nothing is done to C<sv>. If C<encoding> is not
16330 an C<Encode::XS> Encoding object, bad things will happen.
16331 (See L<encoding> and L<Encode>.)
16333 The PV of C<sv> is returned.
16338 Perl_sv_recode_to_utf8(pTHX_ SV *sv, SV *encoding)
16340 PERL_ARGS_ASSERT_SV_RECODE_TO_UTF8;
16342 if (SvPOK(sv) && !SvUTF8(sv) && !IN_BYTES && SvROK(encoding)) {
16351 if (SvPADTMP(nsv)) {
16352 nsv = sv_newmortal();
16353 SvSetSV_nosteal(nsv, sv);
16362 Passing sv_yes is wrong - it needs to be or'ed set of constants
16363 for Encode::XS, while UTf-8 decode (currently) assumes a true value means
16364 remove converted chars from source.
16366 Both will default the value - let them.
16368 XPUSHs(&PL_sv_yes);
16371 call_method("decode", G_SCALAR);
16375 s = SvPV_const(uni, len);
16376 if (s != SvPVX_const(sv)) {
16377 SvGROW(sv, len + 1);
16378 Move(s, SvPVX(sv), len + 1, char);
16379 SvCUR_set(sv, len);
16384 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
16385 /* clear pos and any utf8 cache */
16386 MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
16389 if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
16390 magic_setutf8(sv,mg); /* clear UTF8 cache */
16395 return SvPOKp(sv) ? SvPVX(sv) : NULL;
16399 =for apidoc sv_cat_decode
16401 C<encoding> is assumed to be an C<Encode> object, the PV of C<ssv> is
16402 assumed to be octets in that encoding and decoding the input starts
16403 from the position which S<C<(PV + *offset)>> pointed to. C<dsv> will be
16404 concatenated with the decoded UTF-8 string from C<ssv>. Decoding will terminate
16405 when the string C<tstr> appears in decoding output or the input ends on
16406 the PV of C<ssv>. The value which C<offset> points will be modified
16407 to the last input position on C<ssv>.
16409 Returns TRUE if the terminator was found, else returns FALSE.
16414 Perl_sv_cat_decode(pTHX_ SV *dsv, SV *encoding,
16415 SV *ssv, int *offset, char *tstr, int tlen)
16419 PERL_ARGS_ASSERT_SV_CAT_DECODE;
16421 if (SvPOK(ssv) && SvPOK(dsv) && SvROK(encoding)) {
16432 offsv = newSViv(*offset);
16434 mPUSHp(tstr, tlen);
16436 call_method("cat_decode", G_SCALAR);
16438 ret = SvTRUE(TOPs);
16439 *offset = SvIV(offsv);
16445 Perl_croak(aTHX_ "Invalid argument to sv_cat_decode");
16450 /* ---------------------------------------------------------------------
16452 * support functions for report_uninit()
16455 /* the maxiumum size of array or hash where we will scan looking
16456 * for the undefined element that triggered the warning */
16458 #define FUV_MAX_SEARCH_SIZE 1000
16460 /* Look for an entry in the hash whose value has the same SV as val;
16461 * If so, return a mortal copy of the key. */
16464 S_find_hash_subscript(pTHX_ const HV *const hv, const SV *const val)
16469 PERL_ARGS_ASSERT_FIND_HASH_SUBSCRIPT;
16471 if (!hv || SvMAGICAL(hv) || !HvTOTALKEYS(hv) ||
16472 (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
16475 if (val == &PL_sv_undef || val == &PL_sv_placeholder)
16478 array = HvARRAY(hv);
16480 for (i=HvMAX(hv); i>=0; i--) {
16482 for (entry = array[i]; entry; entry = HeNEXT(entry)) {
16483 if (HeVAL(entry) == val)
16484 return newSVhek_mortal(HeKEY_hek(entry));
16490 /* Look for an entry in the array whose value has the same SV as val;
16491 * If so, return the index, otherwise return -1. */
16494 S_find_array_subscript(pTHX_ const AV *const av, const SV *const val)
16496 PERL_ARGS_ASSERT_FIND_ARRAY_SUBSCRIPT;
16498 if (!av || SvMAGICAL(av) || !AvARRAY(av) ||
16499 (AvFILLp(av) > FUV_MAX_SEARCH_SIZE))
16502 if (val != &PL_sv_undef) {
16503 SV ** const svp = AvARRAY(av);
16506 for (i=AvFILLp(av); i>=0; i--)
16513 /* varname(): return the name of a variable, optionally with a subscript.
16514 * If gv is non-zero, use the name of that global, along with gvtype (one
16515 * of "$", "@", "%"); otherwise use the name of the lexical at pad offset
16516 * targ. Depending on the value of the subscript_type flag, return:
16519 #define FUV_SUBSCRIPT_NONE 1 /* "@foo" */
16520 #define FUV_SUBSCRIPT_ARRAY 2 /* "$foo[aindex]" */
16521 #define FUV_SUBSCRIPT_HASH 3 /* "$foo{keyname}" */
16522 #define FUV_SUBSCRIPT_WITHIN 4 /* "within @foo" */
16525 Perl_varname(pTHX_ const GV *const gv, const char gvtype, PADOFFSET targ,
16526 const SV *const keyname, SSize_t aindex, int subscript_type)
16529 SV * const name = sv_newmortal();
16530 if (gv && isGV(gv)) {
16532 buffer[0] = gvtype;
16535 /* as gv_fullname4(), but add literal '^' for $^FOO names */
16537 gv_fullname4(name, gv, buffer, 0);
16539 if ((unsigned int)SvPVX(name)[1] <= 26) {
16541 buffer[1] = SvPVX(name)[1] + 'A' - 1;
16543 /* Swap the 1 unprintable control character for the 2 byte pretty
16544 version - ie substr($name, 1, 1) = $buffer; */
16545 sv_insert(name, 1, 1, buffer, 2);
16549 CV * const cv = gv ? ((CV *)gv) : find_runcv(NULL);
16552 assert(!cv || SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM);
16554 if (!cv || !CvPADLIST(cv))
16556 sv = padnamelist_fetch(PadlistNAMES(CvPADLIST(cv)), targ);
16557 sv_setpvn(name, PadnamePV(sv), PadnameLEN(sv));
16561 if (subscript_type == FUV_SUBSCRIPT_HASH) {
16562 SV * const sv = newSV_type(SVt_NULL);
16564 const char * const pv = SvPV_nomg_const((SV*)keyname, len);
16566 *SvPVX(name) = '$';
16567 Perl_sv_catpvf(aTHX_ name, "{%s}",
16568 pv_pretty(sv, pv, len, 32, NULL, NULL,
16569 PERL_PV_PRETTY_DUMP | PERL_PV_ESCAPE_UNI_DETECT ));
16570 SvREFCNT_dec_NN(sv);
16572 else if (subscript_type == FUV_SUBSCRIPT_ARRAY) {
16573 *SvPVX(name) = '$';
16574 Perl_sv_catpvf(aTHX_ name, "[%" IVdf "]", (IV)aindex);
16576 else if (subscript_type == FUV_SUBSCRIPT_WITHIN) {
16577 /* We know that name has no magic, so can use 0 instead of SV_GMAGIC */
16578 Perl_sv_insert_flags(aTHX_ name, 0, 0, STR_WITH_LEN("within "), 0);
16586 =apidoc_section $warning
16587 =for apidoc find_uninit_var
16589 Find the name of the undefined variable (if any) that caused the operator
16590 to issue a "Use of uninitialized value" warning.
16591 If match is true, only return a name if its value matches C<uninit_sv>.
16592 So roughly speaking, if a unary operator (such as C<OP_COS>) generates a
16593 warning, then following the direct child of the op may yield an
16594 C<OP_PADSV> or C<OP_GV> that gives the name of the undefined variable. On the
16595 other hand, with C<OP_ADD> there are two branches to follow, so we only print
16596 the variable name if we get an exact match.
16597 C<desc_p> points to a string pointer holding the description of the op.
16598 This may be updated if needed.
16600 The name is returned as a mortal SV.
16602 Assumes that C<PL_op> is the OP that originally triggered the error, and that
16603 C<PL_comppad>/C<PL_curpad> points to the currently executing pad.
16609 S_find_uninit_var(pTHX_ const OP *const obase, const SV *const uninit_sv,
16610 bool match, const char **desc_p)
16614 const OP *o, *o2, *kid;
16616 PERL_ARGS_ASSERT_FIND_UNINIT_VAR;
16618 if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef ||
16619 uninit_sv == &PL_sv_placeholder)))
16622 switch (obase->op_type) {
16625 /* undef should care if its args are undef - any warnings
16626 * will be from tied/magic vars */
16634 const bool pad = ( obase->op_type == OP_PADAV
16635 || obase->op_type == OP_PADHV
16636 || obase->op_type == OP_PADRANGE
16639 const bool hash = ( obase->op_type == OP_PADHV
16640 || obase->op_type == OP_RV2HV
16641 || (obase->op_type == OP_PADRANGE
16642 && SvTYPE(PAD_SVl(obase->op_targ)) == SVt_PVHV)
16646 int subscript_type = FUV_SUBSCRIPT_WITHIN;
16648 if (pad) { /* @lex, %lex */
16649 sv = PAD_SVl(obase->op_targ);
16653 if (cUNOPx(obase)->op_first->op_type == OP_GV) {
16654 /* @global, %global */
16655 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
16658 sv = hash ? MUTABLE_SV(GvHV(gv)): MUTABLE_SV(GvAV(gv));
16660 else if (obase == PL_op) /* @{expr}, %{expr} */
16661 return find_uninit_var(cUNOPx(obase)->op_first,
16662 uninit_sv, match, desc_p);
16663 else /* @{expr}, %{expr} as a sub-expression */
16667 /* attempt to find a match within the aggregate */
16669 keysv = find_hash_subscript((const HV*)sv, uninit_sv);
16671 subscript_type = FUV_SUBSCRIPT_HASH;
16674 index = find_array_subscript((const AV *)sv, uninit_sv);
16676 subscript_type = FUV_SUBSCRIPT_ARRAY;
16679 if (match && subscript_type == FUV_SUBSCRIPT_WITHIN)
16682 return varname(gv, (char)(hash ? '%' : '@'), obase->op_targ,
16683 keysv, index, subscript_type);
16687 if (cUNOPx(obase)->op_first->op_type == OP_GV) {
16689 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
16690 if (!gv || !GvSTASH(gv))
16692 if (match && (GvSV(gv) != uninit_sv))
16694 return varname(gv, '$', 0, NULL, 0, FUV_SUBSCRIPT_NONE);
16697 return find_uninit_var(cUNOPx(obase)->op_first, uninit_sv, 1, desc_p);
16700 if (match && PAD_SVl(obase->op_targ) != uninit_sv)
16702 return varname(NULL, '$', obase->op_targ,
16703 NULL, 0, FUV_SUBSCRIPT_NONE);
16706 gv = cGVOPx_gv(obase);
16707 if (!gv || (match && GvSV(gv) != uninit_sv) || !GvSTASH(gv))
16709 return varname(gv, '$', 0, NULL, 0, FUV_SUBSCRIPT_NONE);
16711 case OP_AELEMFAST_LEX:
16714 AV *av = MUTABLE_AV(PAD_SV(obase->op_targ));
16715 if (!av || SvRMAGICAL(av))
16717 svp = av_fetch(av, (I8)obase->op_private, FALSE);
16718 if (!svp || *svp != uninit_sv)
16721 return varname(NULL, '$', obase->op_targ,
16722 NULL, (I8)obase->op_private, FUV_SUBSCRIPT_ARRAY);
16725 gv = cGVOPx_gv(obase);
16730 AV *const av = GvAV(gv);
16731 if (!av || SvRMAGICAL(av))
16733 svp = av_fetch(av, (I8)obase->op_private, FALSE);
16734 if (!svp || *svp != uninit_sv)
16737 return varname(gv, '$', 0,
16738 NULL, (I8)obase->op_private, FUV_SUBSCRIPT_ARRAY);
16740 NOT_REACHED; /* NOTREACHED */
16743 o = cUNOPx(obase)->op_first;
16744 if (!o || o->op_type != OP_NULL ||
16745 ! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM))
16747 return find_uninit_var(cBINOPo->op_last, uninit_sv, match, desc_p);
16752 bool negate = FALSE;
16754 if (PL_op == obase)
16755 /* $a[uninit_expr] or $h{uninit_expr} */
16756 return find_uninit_var(cBINOPx(obase)->op_last,
16757 uninit_sv, match, desc_p);
16760 o = cBINOPx(obase)->op_first;
16761 kid = cBINOPx(obase)->op_last;
16763 /* get the av or hv, and optionally the gv */
16765 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) {
16766 sv = PAD_SV(o->op_targ);
16768 else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV)
16769 && cUNOPo->op_first->op_type == OP_GV)
16771 gv = cGVOPx_gv(cUNOPo->op_first);
16775 == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(GvAV(gv));
16780 if (kid && kid->op_type == OP_NEGATE) {
16782 kid = cUNOPx(kid)->op_first;
16785 if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) {
16786 /* index is constant */
16789 kidsv = newSVpvs_flags("-", SVs_TEMP);
16790 sv_catsv(kidsv, cSVOPx_sv(kid));
16793 kidsv = cSVOPx_sv(kid);
16797 if (obase->op_type == OP_HELEM) {
16798 HE* he = hv_fetch_ent(MUTABLE_HV(sv), kidsv, 0, 0);
16799 if (!he || HeVAL(he) != uninit_sv)
16803 SV * const opsv = cSVOPx_sv(kid);
16804 const IV opsviv = SvIV(opsv);
16805 SV * const * const svp = av_fetch(MUTABLE_AV(sv),
16806 negate ? - opsviv : opsviv,
16808 if (!svp || *svp != uninit_sv)
16812 if (obase->op_type == OP_HELEM)
16813 return varname(gv, '%', o->op_targ,
16814 kidsv, 0, FUV_SUBSCRIPT_HASH);
16816 return varname(gv, '@', o->op_targ, NULL,
16817 negate ? - SvIV(cSVOPx_sv(kid)) : SvIV(cSVOPx_sv(kid)),
16818 FUV_SUBSCRIPT_ARRAY);
16821 /* index is an expression;
16822 * attempt to find a match within the aggregate */
16823 if (obase->op_type == OP_HELEM) {
16824 SV * const keysv = find_hash_subscript((const HV*)sv, uninit_sv);
16826 return varname(gv, '%', o->op_targ,
16827 keysv, 0, FUV_SUBSCRIPT_HASH);
16830 const SSize_t index
16831 = find_array_subscript((const AV *)sv, uninit_sv);
16833 return varname(gv, '@', o->op_targ,
16834 NULL, index, FUV_SUBSCRIPT_ARRAY);
16839 (char)((o->op_type == OP_PADAV || o->op_type == OP_RV2AV)
16841 o->op_targ, NULL, 0, FUV_SUBSCRIPT_WITHIN);
16843 NOT_REACHED; /* NOTREACHED */
16846 case OP_MULTIDEREF: {
16847 /* If we were executing OP_MULTIDEREF when the undef warning
16848 * triggered, then it must be one of the index values within
16849 * that triggered it. If not, then the only possibility is that
16850 * the value retrieved by the last aggregate index might be the
16851 * culprit. For the former, we set PL_multideref_pc each time before
16852 * using an index, so work though the item list until we reach
16853 * that point. For the latter, just work through the entire item
16854 * list; the last aggregate retrieved will be the candidate.
16855 * There is a third rare possibility: something triggered
16856 * magic while fetching an array/hash element. Just display
16857 * nothing in this case.
16860 /* the named aggregate, if any */
16861 PADOFFSET agg_targ = 0;
16863 /* the last-seen index */
16865 PADOFFSET index_targ;
16867 IV index_const_iv = 0; /* init for spurious compiler warn */
16868 SV *index_const_sv;
16869 int depth = 0; /* how many array/hash lookups we've done */
16871 UNOP_AUX_item *items = cUNOP_AUXx(obase)->op_aux;
16872 UNOP_AUX_item *last = NULL;
16873 UV actions = items->uv;
16876 if (PL_op == obase) {
16877 last = PL_multideref_pc;
16878 assert(last >= items && last <= items + items[-1].uv);
16885 switch (actions & MDEREF_ACTION_MASK) {
16887 case MDEREF_reload:
16888 actions = (++items)->uv;
16891 case MDEREF_HV_padhv_helem: /* $lex{...} */
16894 case MDEREF_AV_padav_aelem: /* $lex[...] */
16895 agg_targ = (++items)->pad_offset;
16899 case MDEREF_HV_gvhv_helem: /* $pkg{...} */
16902 case MDEREF_AV_gvav_aelem: /* $pkg[...] */
16904 agg_gv = (GV*)UNOP_AUX_item_sv(++items);
16905 assert(isGV_with_GP(agg_gv));
16908 case MDEREF_HV_gvsv_vivify_rv2hv_helem: /* $pkg->{...} */
16909 case MDEREF_HV_padsv_vivify_rv2hv_helem: /* $lex->{...} */
16912 case MDEREF_HV_pop_rv2hv_helem: /* expr->{...} */
16913 case MDEREF_HV_vivify_rv2hv_helem: /* vivify, ->{...} */
16919 case MDEREF_AV_gvsv_vivify_rv2av_aelem: /* $pkg->[...] */
16920 case MDEREF_AV_padsv_vivify_rv2av_aelem: /* $lex->[...] */
16923 case MDEREF_AV_pop_rv2av_aelem: /* expr->[...] */
16924 case MDEREF_AV_vivify_rv2av_aelem: /* vivify, ->[...] */
16931 index_const_sv = NULL;
16933 index_type = (actions & MDEREF_INDEX_MASK);
16934 switch (index_type) {
16935 case MDEREF_INDEX_none:
16937 case MDEREF_INDEX_const:
16939 index_const_sv = UNOP_AUX_item_sv(++items)
16941 index_const_iv = (++items)->iv;
16943 case MDEREF_INDEX_padsv:
16944 index_targ = (++items)->pad_offset;
16946 case MDEREF_INDEX_gvsv:
16947 index_gv = (GV*)UNOP_AUX_item_sv(++items);
16948 assert(isGV_with_GP(index_gv));
16952 if (index_type != MDEREF_INDEX_none)
16955 if ( index_type == MDEREF_INDEX_none
16956 || (actions & MDEREF_FLAG_last)
16957 || (last && items >= last)
16961 actions >>= MDEREF_SHIFT;
16964 if (PL_op == obase) {
16965 /* most likely index was undef */
16967 *desc_p = ( (actions & MDEREF_FLAG_last)
16968 && (obase->op_private
16969 & (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE)))
16971 (obase->op_private & OPpMULTIDEREF_EXISTS)
16974 : is_hv ? "hash element" : "array element";
16975 assert(index_type != MDEREF_INDEX_none);
16977 if (GvSV(index_gv) == uninit_sv)
16978 return varname(index_gv, '$', 0, NULL, 0,
16979 FUV_SUBSCRIPT_NONE);
16984 if (PL_curpad[index_targ] == uninit_sv)
16985 return varname(NULL, '$', index_targ,
16986 NULL, 0, FUV_SUBSCRIPT_NONE);
16990 /* If we got to this point it was undef on a const subscript,
16991 * so magic probably involved, e.g. $ISA[0]. Give up. */
16995 /* the SV returned by pp_multideref() was undef, if anything was */
17001 sv = PAD_SV(agg_targ);
17003 sv = is_hv ? MUTABLE_SV(GvHV(agg_gv)) : MUTABLE_SV(GvAV(agg_gv));
17010 if (index_type == MDEREF_INDEX_const) {
17015 HE* he = hv_fetch_ent(MUTABLE_HV(sv), index_const_sv, 0, 0);
17016 if (!he || HeVAL(he) != uninit_sv)
17020 SV * const * const svp =
17021 av_fetch(MUTABLE_AV(sv), index_const_iv, FALSE);
17022 if (!svp || *svp != uninit_sv)
17027 ? varname(agg_gv, '%', agg_targ,
17028 index_const_sv, 0, FUV_SUBSCRIPT_HASH)
17029 : varname(agg_gv, '@', agg_targ,
17030 NULL, index_const_iv, FUV_SUBSCRIPT_ARRAY);
17033 /* index is an var */
17035 SV * const keysv = find_hash_subscript((const HV*)sv, uninit_sv);
17037 return varname(agg_gv, '%', agg_targ,
17038 keysv, 0, FUV_SUBSCRIPT_HASH);
17041 const SSize_t index
17042 = find_array_subscript((const AV *)sv, uninit_sv);
17044 return varname(agg_gv, '@', agg_targ,
17045 NULL, index, FUV_SUBSCRIPT_ARRAY);
17047 /* look for an element not found */
17048 if (!SvMAGICAL(sv)) {
17049 SV *index_sv = NULL;
17051 index_sv = PL_curpad[index_targ];
17053 else if (index_gv) {
17054 index_sv = GvSV(index_gv);
17056 if (index_sv && !SvMAGICAL(index_sv) && !SvROK(index_sv)) {
17058 SV *report_index_sv = SvOK(index_sv) ? index_sv : &PL_sv_no;
17059 HE *he = hv_fetch_ent(MUTABLE_HV(sv), report_index_sv, 0, 0);
17061 return varname(agg_gv, '%', agg_targ,
17062 report_index_sv, 0, FUV_SUBSCRIPT_HASH);
17066 SSize_t index = SvOK(index_sv) ? SvIV(index_sv) : 0;
17067 SV * const * const svp =
17068 av_fetch(MUTABLE_AV(sv), index, FALSE);
17070 return varname(agg_gv, '@', agg_targ,
17071 NULL, index, FUV_SUBSCRIPT_ARRAY);
17078 return varname(agg_gv,
17080 agg_targ, NULL, 0, FUV_SUBSCRIPT_WITHIN);
17082 NOT_REACHED; /* NOTREACHED */
17086 /* only examine RHS */
17087 return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv,
17091 o = cUNOPx(obase)->op_first;
17092 if ( o->op_type == OP_PUSHMARK
17093 || (o->op_type == OP_NULL && o->op_targ == OP_PUSHMARK)
17097 if (!OpHAS_SIBLING(o)) {
17098 /* one-arg version of open is highly magical */
17100 if (o->op_type == OP_GV) { /* open FOO; */
17102 if (match && GvSV(gv) != uninit_sv)
17104 return varname(gv, '$', 0,
17105 NULL, 0, FUV_SUBSCRIPT_NONE);
17107 /* other possibilities not handled are:
17108 * open $x; or open my $x; should return '${*$x}'
17109 * open expr; should return '$'.expr ideally
17116 /* ops where $_ may be an implicit arg */
17121 if ( !(obase->op_flags & OPf_STACKED)) {
17122 if (uninit_sv == DEFSV)
17123 return newSVpvs_flags("$_", SVs_TEMP);
17124 else if (obase->op_targ
17125 && uninit_sv == PAD_SVl(obase->op_targ))
17126 return varname(NULL, '$', obase->op_targ, NULL, 0,
17127 FUV_SUBSCRIPT_NONE);
17134 match = 1; /* print etc can return undef on defined args */
17135 /* skip filehandle as it can't produce 'undef' warning */
17136 o = cUNOPx(obase)->op_first;
17137 if ((obase->op_flags & OPf_STACKED)
17139 ( o->op_type == OP_PUSHMARK
17140 || (o->op_type == OP_NULL && o->op_targ == OP_PUSHMARK)))
17141 o = OpSIBLING(OpSIBLING(o));
17145 case OP_ENTEREVAL: /* could be eval $undef or $x='$undef'; eval $x */
17146 case OP_CUSTOM: /* XS or custom code could trigger random warnings */
17148 /* the following ops are capable of returning PL_sv_undef even for
17149 * defined arg(s) */
17168 case OP_GETPEERNAME:
17215 case OP_SMARTMATCH:
17224 /* XXX tmp hack: these two may call an XS sub, and currently
17225 XS subs don't have a SUB entry on the context stack, so CV and
17226 pad determination goes wrong, and BAD things happen. So, just
17227 don't try to determine the value under those circumstances.
17228 Need a better fix at dome point. DAPM 11/2007 */
17234 GV * const gv = gv_fetchpvs(".", GV_NOTQUAL, SVt_PV);
17235 if (gv && GvSV(gv) == uninit_sv)
17236 return newSVpvs_flags("$.", SVs_TEMP);
17241 /* def-ness of rval pos() is independent of the def-ness of its arg */
17242 if ( !(obase->op_flags & OPf_MOD))
17248 if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs))
17249 return newSVpvs_flags("${$/}", SVs_TEMP);
17254 if (!(obase->op_flags & OPf_KIDS))
17256 o = cUNOPx(obase)->op_first;
17262 /* This loop checks all the kid ops, skipping any that cannot pos-
17263 * sibly be responsible for the uninitialized value; i.e., defined
17264 * constants and ops that return nothing. If there is only one op
17265 * left that is not skipped, then we *know* it is responsible for
17266 * the uninitialized value. If there is more than one op left, we
17267 * have to look for an exact match in the while() loop below.
17268 * Note that we skip padrange, because the individual pad ops that
17269 * it replaced are still in the tree, so we work on them instead.
17272 for (kid=o; kid; kid = OpSIBLING(kid)) {
17273 const OPCODE type = kid->op_type;
17274 if ( (type == OP_CONST && SvOK(cSVOPx_sv(kid)))
17275 || (type == OP_NULL && ! (kid->op_flags & OPf_KIDS))
17276 || (type == OP_PUSHMARK)
17277 || (type == OP_PADRANGE)
17281 if (o2) { /* more than one found */
17288 return find_uninit_var(o2, uninit_sv, match, desc_p);
17290 /* scan all args */
17292 sv = find_uninit_var(o, uninit_sv, 1, desc_p);
17304 =for apidoc_section $warning
17305 =for apidoc report_uninit
17307 Print appropriate "Use of uninitialized variable" warning.
17313 Perl_report_uninit(pTHX_ const SV *uninit_sv)
17315 const char *desc = NULL;
17316 SV* varname = NULL;
17319 desc = PL_op->op_type == OP_STRINGIFY && PL_op->op_folded
17321 : PL_op->op_type == OP_MULTICONCAT
17322 && (PL_op->op_private & OPpMULTICONCAT_FAKE)
17325 if (uninit_sv && PL_curpad) {
17326 varname = find_uninit_var(PL_op, uninit_sv, 0, &desc);
17328 sv_insert(varname, 0, 0, " ", 1);
17331 else if (PL_curstackinfo->si_type == PERLSI_SORT && cxstack_ix == 0)
17332 /* we've reached the end of a sort block or sub,
17333 * and the uninit value is probably what that code returned */
17336 /* PL_warn_uninit_sv is constant */
17337 GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral);
17339 /* diag_listed_as: Use of uninitialized value%s */
17340 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit_sv,
17341 SVfARG(varname ? varname : &PL_sv_no),
17344 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
17346 GCC_DIAG_RESTORE_STMT;
17350 * ex: set ts=8 sts=4 sw=4 et: