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 */
4164 if (LIKELY( dtype != SVt_PVGV && dtype != SVt_PVLV )) {
4165 (void)SvOK_off(dsv);
4173 /* For performance, we inline promoting to type SVt_IV. */
4174 /* We're starting from SVt_NULL, so provided that define is
4175 * actual 0, we don't have to unset any SV type flags
4176 * to promote to SVt_IV. */
4177 STATIC_ASSERT_STMT(SVt_NULL == 0);
4178 SET_SVANY_FOR_BODYLESS_IV(dsv);
4179 SvFLAGS(dsv) |= SVt_IV;
4183 sv_upgrade(dsv, SVt_PVIV);
4187 goto end_of_first_switch;
4189 (void)SvIOK_only(dsv);
4190 SvIV_set(dsv, SvIVX(ssv));
4193 /* SvTAINTED can only be true if the SV has taint magic, which in
4194 turn means that the SV type is PVMG (or greater). This is the
4195 case statement for SVt_IV, so this cannot be true (whatever gcov
4197 assert(!SvTAINTED(ssv));
4202 if (dtype < SVt_PV && dtype != SVt_IV)
4203 sv_upgrade(dsv, SVt_IV);
4207 if (LIKELY( SvNOK(ssv) )) {
4211 sv_upgrade(dsv, SVt_NV);
4215 sv_upgrade(dsv, SVt_PVNV);
4219 goto end_of_first_switch;
4221 SvNV_set(dsv, SvNVX(ssv));
4222 (void)SvNOK_only(dsv);
4223 /* SvTAINTED can only be true if the SV has taint magic, which in
4224 turn means that the SV type is PVMG (or greater). This is the
4225 case statement for SVt_NV, so this cannot be true (whatever gcov
4227 assert(!SvTAINTED(ssv));
4234 sv_upgrade(dsv, SVt_PV);
4237 if (dtype < SVt_PVIV)
4238 sv_upgrade(dsv, SVt_PVIV);
4241 if (dtype < SVt_PVNV)
4242 sv_upgrade(dsv, SVt_PVNV);
4246 invlist_clone(ssv, dsv);
4250 const char * const type = sv_reftype(ssv,0);
4252 /* diag_listed_as: Bizarre copy of %s */
4253 Perl_croak(aTHX_ "Bizarre copy of %s in %s", type, OP_DESC(PL_op));
4255 Perl_croak(aTHX_ "Bizarre copy of %s", type);
4257 NOT_REACHED; /* NOTREACHED */
4261 if (dtype < SVt_REGEXP)
4262 sv_upgrade(dsv, SVt_REGEXP);
4268 if (SvGMAGICAL(ssv) && (flags & SV_GMAGIC)) {
4270 if (SvTYPE(ssv) != stype)
4271 stype = SvTYPE(ssv);
4273 if (isGV_with_GP(ssv) && dtype <= SVt_PVLV) {
4274 glob_assign_glob(dsv, ssv, dtype);
4277 if (stype == SVt_PVLV)
4279 if (isREGEXP(ssv)) goto upgregexp;
4280 SvUPGRADE(dsv, SVt_PVNV);
4283 SvUPGRADE(dsv, (svtype)stype);
4285 end_of_first_switch:
4287 /* dsv may have been upgraded. */
4288 dtype = SvTYPE(dsv);
4289 sflags = SvFLAGS(ssv);
4291 if (UNLIKELY( dtype == SVt_PVCV )) {
4292 /* Assigning to a subroutine sets the prototype. */
4295 const char *const ptr = SvPV_const(ssv, len);
4297 SvGROW(dsv, len + 1);
4298 Copy(ptr, SvPVX(dsv), len + 1, char);
4299 SvCUR_set(dsv, len);
4301 SvFLAGS(dsv) |= sflags & SVf_UTF8;
4302 CvAUTOLOAD_off(dsv);
4307 else if (UNLIKELY(dtype == SVt_PVAV || dtype == SVt_PVHV
4308 || dtype == SVt_PVFM))
4310 const char * const type = sv_reftype(dsv,0);
4312 /* diag_listed_as: Cannot copy to %s */
4313 Perl_croak(aTHX_ "Cannot copy to %s in %s", type, OP_DESC(PL_op));
4315 Perl_croak(aTHX_ "Cannot copy to %s", type);
4316 } else if (sflags & SVf_ROK) {
4317 if (isGV_with_GP(dsv)
4318 && SvTYPE(SvRV(ssv)) == SVt_PVGV && isGV_with_GP(SvRV(ssv))) {
4321 if (GvIMPORTED(dsv) != GVf_IMPORTED
4322 && CopSTASH_ne(PL_curcop, GvSTASH(dsv)))
4329 glob_assign_glob(dsv, ssv, dtype);
4333 if (dtype >= SVt_PV) {
4334 if (isGV_with_GP(dsv)) {
4335 gv_setref(dsv, ssv);
4338 if (SvPVX_const(dsv)) {
4344 (void)SvOK_off(dsv);
4345 SvRV_set(dsv, SvREFCNT_inc(SvRV(ssv)));
4346 SvFLAGS(dsv) |= sflags & SVf_ROK;
4347 assert(!(sflags & SVp_NOK));
4348 assert(!(sflags & SVp_IOK));
4349 assert(!(sflags & SVf_NOK));
4350 assert(!(sflags & SVf_IOK));
4352 else if (isGV_with_GP(dsv)) {
4353 if (!(sflags & SVf_OK)) {
4354 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
4355 "Undefined value assigned to typeglob");
4358 GV *gv = gv_fetchsv_nomg(ssv, GV_ADD, SVt_PVGV);
4359 if (dsv != (const SV *)gv) {
4360 const char * const name = GvNAME((const GV *)dsv);
4361 const STRLEN len = GvNAMELEN(dsv);
4362 HV *old_stash = NULL;
4363 bool reset_isa = FALSE;
4364 if ((len > 1 && name[len-2] == ':' && name[len-1] == ':')
4365 || (len == 1 && name[0] == ':')) {
4366 /* Set aside the old stash, so we can reset isa caches
4367 on its subclasses. */
4368 if((old_stash = GvHV(dsv))) {
4369 /* Make sure we do not lose it early. */
4370 SvREFCNT_inc_simple_void_NN(
4371 sv_2mortal((SV *)old_stash)
4378 SvREFCNT_inc_simple_void_NN(sv_2mortal(dsv));
4379 gp_free(MUTABLE_GV(dsv));
4381 GvGP_set(dsv, gp_ref(GvGP(gv)));
4384 HV * const stash = GvHV(dsv);
4386 old_stash ? (HV *)HvENAME_get(old_stash) : stash
4396 else if ((dtype == SVt_REGEXP || dtype == SVt_PVLV)
4397 && (stype == SVt_REGEXP || isREGEXP(ssv))) {
4398 reg_temp_copy((REGEXP*)dsv, (REGEXP*)ssv);
4400 else if (sflags & SVp_POK) {
4401 const STRLEN cur = SvCUR(ssv);
4402 const STRLEN len = SvLEN(ssv);
4405 * We have three basic ways to copy the string:
4411 * Which we choose is based on various factors. The following
4412 * things are listed in order of speed, fastest to slowest:
4414 * - Copying a short string
4415 * - Copy-on-write bookkeeping
4417 * - Copying a long string
4419 * We swipe the string (steal the string buffer) if the SV on the
4420 * rhs is about to be freed anyway (TEMP and refcnt==1). This is a
4421 * big win on long strings. It should be a win on short strings if
4422 * SvPVX_const(dsv) has to be allocated. If not, it should not
4423 * slow things down, as SvPVX_const(ssv) would have been freed
4426 * We also steal the buffer from a PADTMP (operator target) if it
4427 * is ‘long enough’. For short strings, a swipe does not help
4428 * here, as it causes more malloc calls the next time the target
4429 * is used. Benchmarks show that even if SvPVX_const(dsv) has to
4430 * be allocated it is still not worth swiping PADTMPs for short
4431 * strings, as the savings here are small.
4433 * If swiping is not an option, then we see whether it is
4434 * worth using copy-on-write. If the lhs already has a buf-
4435 * fer big enough and the string is short, we skip it and fall back
4436 * to method 3, since memcpy is faster for short strings than the
4437 * later bookkeeping overhead that copy-on-write entails.
4439 * If the rhs is not a copy-on-write string yet, then we also
4440 * consider whether the buffer is too large relative to the string
4441 * it holds. Some operations such as readline allocate a large
4442 * buffer in the expectation of reusing it. But turning such into
4443 * a COW buffer is counter-productive because it increases memory
4444 * usage by making readline allocate a new large buffer the sec-
4445 * ond time round. So, if the buffer is too large, again, we use
4448 * Finally, if there is no buffer on the left, or the buffer is too
4449 * small, then we use copy-on-write and make both SVs share the
4454 /* Whichever path we take through the next code, we want this true,
4455 and doing it now facilitates the COW check. */
4456 (void)SvPOK_only(dsv);
4460 /* slated for free anyway (and not COW)? */
4461 (sflags & (SVs_TEMP|SVf_IsCOW)) == SVs_TEMP
4462 /* or a swipable TARG */
4464 (SVs_PADTMP|SVf_READONLY|SVf_PROTECT|SVf_IsCOW))
4466 /* whose buffer is worth stealing */
4467 && CHECK_COWBUF_THRESHOLD(cur,len)
4470 !(sflags & SVf_OOK) && /* and not involved in OOK hack? */
4471 (!(flags & SV_NOSTEAL)) &&
4472 /* and we're allowed to steal temps */
4473 SvREFCNT(ssv) == 1 && /* and no other references to it? */
4474 len) /* and really is a string */
4475 { /* Passes the swipe test. */
4476 if (SvPVX_const(dsv)) /* we know that dtype >= SVt_PV */
4478 SvPV_set(dsv, SvPVX_mutable(ssv));
4479 SvLEN_set(dsv, SvLEN(ssv));
4480 SvCUR_set(dsv, SvCUR(ssv));
4483 (void)SvOK_off(ssv); /* NOTE: nukes most SvFLAGS on ssv */
4484 SvPV_set(ssv, NULL);
4489 /* We must check for SvIsCOW_static() even without
4490 * SV_COW_SHARED_HASH_KEYS being set or else we'll break SvIsBOOL()
4492 else if (SvIsCOW_static(ssv)) {
4493 if (SvPVX_const(dsv)) { /* we know that dtype >= SVt_PV */
4496 SvPV_set(dsv, SvPVX(ssv));
4498 SvCUR_set(dsv, cur);
4499 SvFLAGS(dsv) |= (SVf_IsCOW|SVppv_STATIC);
4501 else if (flags & SV_COW_SHARED_HASH_KEYS
4503 #ifdef PERL_COPY_ON_WRITE
4506 ( (CHECK_COWBUF_THRESHOLD(cur,len) || SvLEN(dsv) < cur+1)
4507 /* If this is a regular (non-hek) COW, only so
4508 many COW "copies" are possible. */
4509 && CowREFCNT(ssv) != SV_COW_REFCNT_MAX ))
4510 : ( (sflags & CAN_COW_MASK) == CAN_COW_FLAGS
4511 && !(SvFLAGS(dsv) & SVf_BREAK)
4512 && CHECK_COW_THRESHOLD(cur,len) && cur+1 < len
4513 && (CHECK_COWBUF_THRESHOLD(cur,len) || SvLEN(dsv) < cur+1)
4517 && !(SvFLAGS(dsv) & SVf_BREAK)
4520 /* Either it's a shared hash key, or it's suitable for
4524 PerlIO_printf(Perl_debug_log, "Copy on write: ssv --> dsv\n");
4530 if (!(sflags & SVf_IsCOW)) {
4535 if (SvPVX_const(dsv)) { /* we know that dtype >= SVt_PV */
4541 if (sflags & SVf_IsCOW) {
4545 SvPV_set(dsv, SvPVX_mutable(ssv));
4550 /* SvIsCOW_shared_hash */
4551 DEBUG_C(PerlIO_printf(Perl_debug_log,
4552 "Copy on write: Sharing hash\n"));
4554 assert (SvTYPE(dsv) >= SVt_PV);
4556 HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(ssv)))));
4558 SvLEN_set(dsv, len);
4559 SvCUR_set(dsv, cur);
4562 /* Failed the swipe test, and we cannot do copy-on-write either.
4563 Have to copy the string. */
4564 SvGROW(dsv, cur + 1); /* inlined from sv_setpvn */
4565 Move(SvPVX_const(ssv),SvPVX(dsv),cur,char);
4566 SvCUR_set(dsv, cur);
4569 if (sflags & SVp_NOK) {
4570 SvNV_set(dsv, SvNVX(ssv));
4571 if ((sflags & SVf_NOK) && !(sflags & SVf_POK)) {
4572 /* Source was SVf_NOK|SVp_NOK|SVp_POK but not SVf_POK, meaning
4573 a value set as floating point and later stringified, where
4574 the value happens to be one of the few that we know aren't
4575 affected by the numeric locale, hence we can cache the
4576 stringification. Currently that's +Inf, -Inf and NaN, but
4577 conceivably we might extend this to -9 .. +9 (excluding -0).
4578 So mark destination the same: */
4579 SvFLAGS(dsv) &= ~SVf_POK;
4582 if (sflags & SVp_IOK) {
4583 SvIV_set(dsv, SvIVX(ssv));
4584 if (sflags & SVf_IVisUV)
4586 if ((sflags & SVf_IOK) && !(sflags & SVf_POK)) {
4587 /* Source was SVf_IOK|SVp_IOK|SVp_POK but not SVf_POK, meaning
4588 a value set as an integer and later stringified. So mark
4589 destination the same: */
4590 SvFLAGS(dsv) &= ~SVf_POK;
4593 SvFLAGS(dsv) |= sflags & (SVf_IOK|SVp_IOK|SVf_NOK|SVp_NOK|SVf_UTF8);
4595 const MAGIC * const smg = SvVSTRING_mg(ssv);
4597 sv_magic(dsv, NULL, PERL_MAGIC_vstring,
4598 smg->mg_ptr, smg->mg_len);
4603 else if (sflags & (SVp_IOK|SVp_NOK)) {
4604 (void)SvOK_off(dsv);
4605 SvFLAGS(dsv) |= sflags & (SVf_IOK|SVp_IOK|SVf_IVisUV|SVf_NOK|SVp_NOK);
4606 if (sflags & SVp_IOK) {
4607 /* XXXX Do we want to set IsUV for IV(ROK)? Be extra safe... */
4608 SvIV_set(dsv, SvIVX(ssv));
4610 if (sflags & SVp_NOK) {
4611 SvNV_set(dsv, SvNVX(ssv));
4615 if (isGV_with_GP(ssv)) {
4616 gv_efullname3(dsv, MUTABLE_GV(ssv), "*");
4619 (void)SvOK_off(dsv);
4627 =for apidoc sv_set_undef
4629 Equivalent to C<sv_setsv(sv, &PL_sv_undef)>, but more efficient.
4630 Doesn't handle set magic.
4632 The perl equivalent is C<$sv = undef;>. Note that it doesn't free any string
4633 buffer, unlike C<undef $sv>.
4635 Introduced in perl 5.25.12.
4641 Perl_sv_set_undef(pTHX_ SV *sv)
4643 U32 type = SvTYPE(sv);
4645 PERL_ARGS_ASSERT_SV_SET_UNDEF;
4647 /* shortcut, NULL, IV, RV */
4649 if (type <= SVt_IV) {
4650 assert(!SvGMAGICAL(sv));
4651 if (SvREADONLY(sv)) {
4652 /* does undeffing PL_sv_undef count as modifying a read-only
4653 * variable? Some XS code does this */
4654 if (sv == &PL_sv_undef)
4656 Perl_croak_no_modify();
4661 sv_unref_flags(sv, 0);
4664 SvFLAGS(sv) = type; /* quickly turn off all flags */
4665 SvREFCNT_dec_NN(rv);
4669 SvFLAGS(sv) = type; /* quickly turn off all flags */
4674 Perl_croak(aTHX_ "panic: attempt to undefine a freed scalar %p",
4677 SV_CHECK_THINKFIRST_COW_DROP(sv);
4679 if (isGV_with_GP(sv))
4680 Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
4681 "Undefined value assigned to typeglob");
4687 =for apidoc sv_set_true
4689 Equivalent to C<sv_setsv(sv, &PL_sv_yes)>, but may be made more
4690 efficient in the future. Doesn't handle set magic.
4692 The perl equivalent is C<$sv = !0;>.
4694 Introduced in perl 5.35.11.
4700 Perl_sv_set_true(pTHX_ SV *sv)
4702 PERL_ARGS_ASSERT_SV_SET_TRUE;
4703 sv_setsv(sv, &PL_sv_yes);
4707 =for apidoc sv_set_false
4709 Equivalent to C<sv_setsv(sv, &PL_sv_no)>, but may be made more
4710 efficient in the future. Doesn't handle set magic.
4712 The perl equivalent is C<$sv = !1;>.
4714 Introduced in perl 5.35.11.
4720 Perl_sv_set_false(pTHX_ SV *sv)
4722 PERL_ARGS_ASSERT_SV_SET_FALSE;
4723 sv_setsv(sv, &PL_sv_no);
4727 =for apidoc sv_set_bool
4729 Equivalent to C<sv_setsv(sv, bool_val ? &Pl_sv_yes : &PL_sv_no)>, but
4730 may be made more efficient in the future. Doesn't handle set magic.
4732 The perl equivalent is C<$sv = !!$expr;>.
4734 Introduced in perl 5.35.11.
4740 Perl_sv_set_bool(pTHX_ SV *sv, const bool bool_val)
4742 PERL_ARGS_ASSERT_SV_SET_BOOL;
4743 sv_setsv(sv, bool_val ? &PL_sv_yes : &PL_sv_no);
4748 Perl_sv_setsv_mg(pTHX_ SV *const dsv, SV *const ssv)
4750 PERL_ARGS_ASSERT_SV_SETSV_MG;
4757 # define SVt_COW SVt_PV
4759 Perl_sv_setsv_cow(pTHX_ SV *dsv, SV *ssv)
4761 STRLEN cur = SvCUR(ssv);
4762 STRLEN len = SvLEN(ssv);
4764 U32 new_flags = (SVt_COW|SVf_POK|SVp_POK|SVf_IsCOW);
4765 #if defined(PERL_DEBUG_READONLY_COW) && defined(PERL_COPY_ON_WRITE)
4766 const bool already = cBOOL(SvIsCOW(ssv));
4769 PERL_ARGS_ASSERT_SV_SETSV_COW;
4772 PerlIO_printf(Perl_debug_log, "Fast copy on write: %p -> %p\n",
4773 (void*)ssv, (void*)dsv);
4780 if (SvTHINKFIRST(dsv))
4781 sv_force_normal_flags(dsv, SV_COW_DROP_PV);
4782 else if (SvPVX_const(dsv))
4783 Safefree(SvPVX_mutable(dsv));
4787 SvUPGRADE(dsv, SVt_COW);
4789 assert (SvPOK(ssv));
4790 assert (SvPOKp(ssv));
4793 if (SvIsCOW_shared_hash(ssv)) {
4794 /* source is a COW shared hash key. */
4795 DEBUG_C(PerlIO_printf(Perl_debug_log,
4796 "Fast copy on write: Sharing hash\n"));
4797 new_pv = HEK_KEY(share_hek_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(ssv))));
4800 else if (SvIsCOW_static(ssv)) {
4801 /* source is static constant; preserve this */
4802 new_pv = SvPVX(ssv);
4803 new_flags |= SVppv_STATIC;
4806 assert(SvCUR(ssv)+1 < SvLEN(ssv));
4807 assert(CowREFCNT(ssv) < SV_COW_REFCNT_MAX);
4809 assert ((SvFLAGS(ssv) & CAN_COW_MASK) == CAN_COW_FLAGS);
4810 SvUPGRADE(ssv, SVt_COW);
4812 DEBUG_C(PerlIO_printf(Perl_debug_log,
4813 "Fast copy on write: Converting ssv to COW\n"));
4816 # ifdef PERL_DEBUG_READONLY_COW
4817 if (already) sv_buf_to_rw(ssv);
4820 new_pv = SvPVX_mutable(ssv);
4824 SvPV_set(dsv, new_pv);
4825 SvFLAGS(dsv) = new_flags;
4828 SvLEN_set(dsv, len);
4829 SvCUR_set(dsv, cur);
4839 =for apidoc sv_setpv_bufsize
4841 Sets the SV to be a string of cur bytes length, with at least
4842 len bytes available. Ensures that there is a null byte at SvEND.
4843 Returns a char * pointer to the SvPV buffer.
4849 Perl_sv_setpv_bufsize(pTHX_ SV *const sv, const STRLEN cur, const STRLEN len)
4853 PERL_ARGS_ASSERT_SV_SETPV_BUFSIZE;
4855 SV_CHECK_THINKFIRST_COW_DROP(sv);
4856 SvUPGRADE(sv, SVt_PV);
4857 pv = SvGROW(sv, len + 1);
4860 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4863 if (SvTYPE(sv) == SVt_PVCV) CvAUTOLOAD_off(sv);
4868 =for apidoc sv_setpv
4869 =for apidoc_item sv_setpv_mg
4870 =for apidoc_item sv_setpvn
4871 =for apidoc_item sv_setpvn_fresh
4872 =for apidoc_item sv_setpvn_mg
4873 =for apidoc_item |void|sv_setpvs|SV* sv|"literal string"
4874 =for apidoc_item |void|sv_setpvs_mg|SV* sv|"literal string"
4876 These copy a string into the SV C<sv>, making sure it is C<L</SvPOK_only>>.
4878 In the C<pvs> forms, the string must be a C literal string, enclosed in double
4881 In the C<pvn> forms, the first byte of the string is pointed to by C<ptr>, and
4882 C<len> indicates the number of bytes to be copied, potentially including
4883 embedded C<NUL> characters.
4885 In the plain C<pv> forms, C<ptr> points to a NUL-terminated C string. That is,
4886 it points to the first byte of the string, and the copy proceeds up through the
4887 first enountered C<NUL> byte.
4889 In the forms that take a C<ptr> argument, if it is NULL, the SV will become
4892 The UTF-8 flag is not changed by these functions. A terminating NUL byte is
4893 guaranteed in the result.
4895 The C<_mg> forms handle 'set' magic; the other forms skip all magic.
4897 C<sv_setpvn_fresh> is a cut-down alternative to C<sv_setpvn>, intended ONLY
4898 to be used with a fresh sv that has been upgraded to a SVt_PV, SVt_PVIV,
4899 SVt_PVNV, or SVt_PVMG.
4905 Perl_sv_setpvn(pTHX_ SV *const sv, const char *const ptr, const STRLEN len)
4909 PERL_ARGS_ASSERT_SV_SETPVN;
4911 SV_CHECK_THINKFIRST_COW_DROP(sv);
4912 if (isGV_with_GP(sv))
4913 Perl_croak_no_modify();
4919 /* len is STRLEN which is unsigned, need to copy to signed */
4922 Perl_croak(aTHX_ "panic: sv_setpvn called with negative strlen %"
4925 SvUPGRADE(sv, SVt_PV);
4927 dptr = SvGROW(sv, len + 1);
4928 Move(ptr,dptr,len,char);
4931 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4933 if (SvTYPE(sv) == SVt_PVCV) CvAUTOLOAD_off(sv);
4937 Perl_sv_setpvn_mg(pTHX_ SV *const sv, const char *const ptr, const STRLEN len)
4939 PERL_ARGS_ASSERT_SV_SETPVN_MG;
4941 sv_setpvn(sv,ptr,len);
4946 Perl_sv_setpvn_fresh(pTHX_ SV *const sv, const char *const ptr, const STRLEN len)
4950 PERL_ARGS_ASSERT_SV_SETPVN_FRESH;
4951 assert(SvTYPE(sv) >= SVt_PV && SvTYPE(sv) <= SVt_PVMG);
4952 assert(!SvTHINKFIRST(sv));
4953 assert(!isGV_with_GP(sv));
4957 /* len is STRLEN which is unsigned, need to copy to signed */
4959 Perl_croak(aTHX_ "panic: sv_setpvn_fresh called with negative strlen %"
4962 dptr = sv_grow_fresh(sv, len + 1);
4963 Move(ptr,dptr,len,char);
4972 Perl_sv_setpv(pTHX_ SV *const sv, const char *const ptr)
4976 PERL_ARGS_ASSERT_SV_SETPV;
4978 SV_CHECK_THINKFIRST_COW_DROP(sv);
4984 SvUPGRADE(sv, SVt_PV);
4986 SvGROW(sv, len + 1);
4987 Move(ptr,SvPVX(sv),len+1,char);
4989 (void)SvPOK_only_UTF8(sv); /* validate pointer */
4991 if (SvTYPE(sv) == SVt_PVCV) CvAUTOLOAD_off(sv);
4995 Perl_sv_setpv_mg(pTHX_ SV *const sv, const char *const ptr)
4997 PERL_ARGS_ASSERT_SV_SETPV_MG;
5004 Perl_sv_sethek(pTHX_ SV *const sv, const HEK *const hek)
5006 PERL_ARGS_ASSERT_SV_SETHEK;
5012 if (HEK_LEN(hek) == HEf_SVKEY) {
5013 sv_setsv(sv, *(SV**)HEK_KEY(hek));
5016 const int flags = HEK_FLAGS(hek);
5017 if (flags & HVhek_WASUTF8) {
5018 STRLEN utf8_len = HEK_LEN(hek);
5019 char *as_utf8 = (char *)bytes_to_utf8((U8*)HEK_KEY(hek), &utf8_len);
5020 sv_usepvn_flags(sv, as_utf8, utf8_len, SV_HAS_TRAILING_NUL);
5023 } else if (flags & HVhek_NOTSHARED) {
5024 sv_setpvn(sv, HEK_KEY(hek), HEK_LEN(hek));
5027 else SvUTF8_off(sv);
5031 SV_CHECK_THINKFIRST_COW_DROP(sv);
5032 SvUPGRADE(sv, SVt_PV);
5034 SvPV_set(sv,(char *)HEK_KEY(share_hek_hek(hek)));
5035 SvCUR_set(sv, HEK_LEN(hek));
5041 else SvUTF8_off(sv);
5049 =for apidoc sv_usepvn
5050 =for apidoc_item sv_usepvn_flags
5051 =for apidoc_item sv_usepvn_mg
5053 These tell an SV to use C<ptr> for its string value. Normally SVs have
5054 their string stored inside the SV, but these tell the SV to use an
5055 external string instead.
5057 C<ptr> should point to memory that was allocated
5058 by L</C<Newx>>. It must be
5059 the start of a C<Newx>-ed block of memory, and not a pointer to the
5060 middle of it (beware of L<C<OOK>|perlguts/Offsets> and copy-on-write),
5061 and not be from a non-C<Newx> memory allocator like C<malloc>. The
5062 string length, C<len>, must be supplied. By default this function
5063 will L</C<Renew>> (i.e. realloc, move) the memory pointed to by C<ptr>,
5064 so that the pointer should not be freed or used by the programmer after giving
5065 it to C<sv_usepvn>, and neither should any pointers from "behind" that pointer
5066 (I<e.g.>, S<C<ptr> + 1>) be used.
5068 In the C<sv_usepvn_flags> form, if S<C<flags & SV_SMAGIC>> is true,
5069 C<SvSETMAGIC> is called before returning.
5070 And if S<C<flags & SV_HAS_TRAILING_NUL>> is true, then C<ptr[len]> must be
5071 C<NUL>, and the realloc will be skipped (I<i.e.>, the buffer is actually at
5072 least 1 byte longer than C<len>, and already meets the requirements for storing
5075 C<sv_usepvn> is merely C<sv_usepvn_flags> with C<flags> set to 0, so 'set'
5078 C<sv_usepvn_mg> is merely C<sv_usepvn_flags> with C<flags> set to C<SV_SMAGIC>,
5079 so 'set' magic is performed.
5081 =for apidoc Amnh||SV_SMAGIC
5082 =for apidoc Amnh||SV_HAS_TRAILING_NUL
5088 Perl_sv_usepvn_flags(pTHX_ SV *const sv, char *ptr, const STRLEN len, const U32 flags)
5092 PERL_ARGS_ASSERT_SV_USEPVN_FLAGS;
5094 SV_CHECK_THINKFIRST_COW_DROP(sv);
5095 SvUPGRADE(sv, SVt_PV);
5098 if (flags & SV_SMAGIC)
5102 if (SvPVX_const(sv))
5106 if (flags & SV_HAS_TRAILING_NUL)
5107 assert(ptr[len] == '\0');
5110 allocate = (flags & SV_HAS_TRAILING_NUL)
5112 #ifdef Perl_safesysmalloc_size
5115 PERL_STRLEN_ROUNDUP(len + 1);
5117 if (flags & SV_HAS_TRAILING_NUL) {
5118 /* It's long enough - do nothing.
5119 Specifically Perl_newCONSTSUB is relying on this. */
5122 /* Force a move to shake out bugs in callers. */
5123 char *new_ptr = (char*)safemalloc(allocate);
5124 Copy(ptr, new_ptr, len, char);
5125 PoisonFree(ptr,len,char);
5129 ptr = (char*) saferealloc (ptr, allocate);
5132 #ifdef Perl_safesysmalloc_size
5133 SvLEN_set(sv, Perl_safesysmalloc_size(ptr));
5135 SvLEN_set(sv, allocate);
5139 if (!(flags & SV_HAS_TRAILING_NUL)) {
5142 (void)SvPOK_only_UTF8(sv); /* validate pointer */
5144 if (flags & SV_SMAGIC)
5150 S_sv_uncow(pTHX_ SV * const sv, const U32 flags)
5152 assert(SvIsCOW(sv));
5155 const char * const pvx = SvPVX_const(sv);
5156 const STRLEN len = SvLEN(sv);
5157 const STRLEN cur = SvCUR(sv);
5158 const bool was_shared_hek = SvIsCOW_shared_hash(sv);
5162 PerlIO_printf(Perl_debug_log,
5163 "Copy on write: Force normal %ld\n",
5169 # ifdef PERL_COPY_ON_WRITE
5171 /* Must do this first, since the CowREFCNT uses SvPVX and
5172 we need to write to CowREFCNT, or de-RO the whole buffer if we are
5173 the only owner left of the buffer. */
5174 sv_buf_to_rw(sv); /* NOOP if RO-ing not supported */
5176 U8 cowrefcnt = CowREFCNT(sv);
5177 if(cowrefcnt != 0) {
5179 CowREFCNT(sv) = cowrefcnt;
5184 /* Else we are the only owner of the buffer. */
5189 /* This SV doesn't own the buffer, so need to Newx() a new one: */
5194 if (flags & SV_COW_DROP_PV) {
5195 /* OK, so we don't need to copy our buffer. */
5198 SvGROW(sv, cur + 1);
5199 Move(pvx,SvPVX(sv),cur,char);
5203 if (was_shared_hek) {
5204 unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
5212 const char * const pvx = SvPVX_const(sv);
5213 const STRLEN len = SvCUR(sv);
5217 if (flags & SV_COW_DROP_PV) {
5218 /* OK, so we don't need to copy our buffer. */
5221 SvGROW(sv, len + 1);
5222 Move(pvx,SvPVX(sv),len,char);
5225 unshare_hek(SvSHARED_HEK_FROM_PV(pvx));
5232 =for apidoc sv_force_normal_flags
5234 Undo various types of fakery on an SV, where fakery means
5235 "more than" a string: if the PV is a shared string, make
5236 a private copy; if we're a ref, stop refing; if we're a glob, downgrade to
5237 an C<xpvmg>; if we're a copy-on-write scalar, this is the on-write time when
5238 we do the copy, and is also used locally; if this is a
5239 vstring, drop the vstring magic. If C<SV_COW_DROP_PV> is set
5240 then a copy-on-write scalar drops its PV buffer (if any) and becomes
5241 C<SvPOK_off> rather than making a copy. (Used where this
5242 scalar is about to be set to some other value.) In addition,
5243 the C<flags> parameter gets passed to C<sv_unref_flags()>
5244 when unreffing. C<sv_force_normal> calls this function
5245 with flags set to 0.
5247 This function is expected to be used to signal to perl that this SV is
5248 about to be written to, and any extra book-keeping needs to be taken care
5249 of. Hence, it croaks on read-only values.
5251 =for apidoc Amnh||SV_COW_DROP_PV
5257 Perl_sv_force_normal_flags(pTHX_ SV *const sv, const U32 flags)
5259 PERL_ARGS_ASSERT_SV_FORCE_NORMAL_FLAGS;
5262 Perl_croak_no_modify();
5263 else if (SvIsCOW(sv) && LIKELY(SvTYPE(sv) != SVt_PVHV))
5264 S_sv_uncow(aTHX_ sv, flags);
5266 sv_unref_flags(sv, flags);
5267 else if (SvFAKE(sv) && isGV_with_GP(sv))
5268 sv_unglob(sv, flags);
5269 else if (SvFAKE(sv) && isREGEXP(sv)) {
5270 /* Need to downgrade the REGEXP to a simple(r) scalar. This is analogous
5271 to sv_unglob. We only need it here, so inline it. */
5272 const bool islv = SvTYPE(sv) == SVt_PVLV;
5273 const svtype new_type =
5274 islv ? SVt_NULL : SvMAGIC(sv) || SvSTASH(sv) ? SVt_PVMG : SVt_PV;
5275 SV *const temp = newSV_type(new_type);
5276 regexp *old_rx_body;
5278 if (new_type == SVt_PVMG) {
5279 SvMAGIC_set(temp, SvMAGIC(sv));
5280 SvMAGIC_set(sv, NULL);
5281 SvSTASH_set(temp, SvSTASH(sv));
5282 SvSTASH_set(sv, NULL);
5285 SvCUR_set(temp, SvCUR(sv));
5286 /* Remember that SvPVX is in the head, not the body. */
5287 assert(ReANY((REGEXP *)sv)->mother_re);
5290 /* LV-as-regex has sv->sv_any pointing to an XPVLV body,
5291 * whose xpvlenu_rx field points to the regex body */
5292 XPV *xpv = (XPV*)(SvANY(sv));
5293 old_rx_body = xpv->xpv_len_u.xpvlenu_rx;
5294 xpv->xpv_len_u.xpvlenu_rx = NULL;
5297 old_rx_body = ReANY((REGEXP *)sv);
5299 /* Their buffer is already owned by someone else. */
5300 if (flags & SV_COW_DROP_PV) {
5301 /* SvLEN is already 0. For SVt_REGEXP, we have a brand new
5302 zeroed body. For SVt_PVLV, we zeroed it above (len field
5303 a union with xpvlenu_rx) */
5304 assert(!SvLEN(islv ? sv : temp));
5305 sv->sv_u.svu_pv = 0;
5308 sv->sv_u.svu_pv = savepvn(RX_WRAPPED((REGEXP *)sv), SvCUR(sv));
5309 SvLEN_set(islv ? sv : temp, SvCUR(sv)+1);
5313 /* Now swap the rest of the bodies. */
5317 SvFLAGS(sv) &= ~SVTYPEMASK;
5318 SvFLAGS(sv) |= new_type;
5319 SvANY(sv) = SvANY(temp);
5322 SvFLAGS(temp) &= ~(SVTYPEMASK);
5323 SvFLAGS(temp) |= SVt_REGEXP|SVf_FAKE;
5324 SvANY(temp) = old_rx_body;
5326 /* temp is now rebuilt as a correctly structured SVt_REGEXP, so this
5327 * will trigger a call to sv_clear() which will correctly free the
5329 SvREFCNT_dec_NN(temp);
5331 else if (SvVOK(sv)) sv_unmagic(sv, PERL_MAGIC_vstring);
5337 Efficient removal of characters from the beginning of the string buffer.
5338 C<SvPOK(sv)>, or at least C<SvPOKp(sv)>, must be true and C<ptr> must be a
5339 pointer to somewhere inside the string buffer. C<ptr> becomes the first
5340 character of the adjusted string. Uses the C<OOK> hack. On return, only
5341 C<SvPOK(sv)> and C<SvPOKp(sv)> among the C<OK> flags will be true.
5343 Beware: after this function returns, C<ptr> and SvPVX_const(sv) may no longer
5344 refer to the same chunk of data.
5346 The unfortunate similarity of this function's name to that of Perl's C<chop>
5347 operator is strictly coincidental. This function works from the left;
5348 C<chop> works from the right.
5354 Perl_sv_chop(pTHX_ SV *const sv, const char *const ptr)
5365 PERL_ARGS_ASSERT_SV_CHOP;
5367 if (!ptr || !SvPOKp(sv))
5369 delta = ptr - SvPVX_const(sv);
5371 /* Nothing to do. */
5374 max_delta = SvLEN(sv) ? SvLEN(sv) : SvCUR(sv);
5375 if (delta > max_delta)
5376 Perl_croak(aTHX_ "panic: sv_chop ptr=%p, start=%p, end=%p",
5377 ptr, SvPVX_const(sv), SvPVX_const(sv) + max_delta);
5378 /* SvPVX(sv) may move in SV_CHECK_THINKFIRST(sv), so don't use ptr any more */
5379 SV_CHECK_THINKFIRST(sv);
5380 SvPOK_only_UTF8(sv);
5383 if (!SvLEN(sv)) { /* make copy of shared string */
5384 const char *pvx = SvPVX_const(sv);
5385 const STRLEN len = SvCUR(sv);
5386 SvGROW(sv, len + 1);
5387 Move(pvx,SvPVX(sv),len,char);
5393 SvOOK_offset(sv, old_delta);
5395 SvLEN_set(sv, SvLEN(sv) - delta);
5396 SvCUR_set(sv, SvCUR(sv) - delta);
5397 SvPV_set(sv, SvPVX(sv) + delta);
5399 p = (U8 *)SvPVX_const(sv);
5402 /* how many bytes were evacuated? we will fill them with sentinel
5403 bytes, except for the part holding the new offset of course. */
5406 evacn += (old_delta < 0x100 ? 1 : 1 + sizeof(STRLEN));
5408 assert(evacn <= delta + old_delta);
5412 /* This sets 'delta' to the accumulated value of all deltas so far */
5416 /* If 'delta' fits in a byte, store it just prior to the new beginning of
5417 * the string; otherwise store a 0 byte there and store 'delta' just prior
5418 * to that, using as many bytes as a STRLEN occupies. Thus it overwrites a
5419 * portion of the chopped part of the string */
5420 if (delta < 0x100) {
5424 p -= sizeof(STRLEN);
5425 Copy((U8*)&delta, p, sizeof(STRLEN), U8);
5429 /* Fill the preceding buffer with sentinals to verify that no-one is
5439 =for apidoc sv_catpvn
5440 =for apidoc_item sv_catpvn_flags
5441 =for apidoc_item sv_catpvn_mg
5442 =for apidoc_item sv_catpvn_nomg
5444 These concatenate the C<len> bytes of the string beginning at C<ptr> onto the
5445 end of the string which is in C<dsv>. The caller must make sure C<ptr>
5446 contains at least C<len> bytes.
5448 For all but C<sv_catpvn_flags>, the string appended is assumed to be valid
5449 UTF-8 if the SV has the UTF-8 status set, and a string of bytes otherwise.
5451 They differ in that:
5453 C<sv_catpvn_mg> performs both 'get' and 'set' magic on C<dsv>.
5455 C<sv_catpvn> performs only 'get' magic.
5457 C<sv_catpvn_nomg> skips all magic.
5459 C<sv_catpvn_flags> has an extra C<flags> parameter which allows you to specify
5460 any combination of magic handling (using C<SV_GMAGIC> and/or C<SV_SMAGIC>) and
5461 to also override the UTF-8 handling. By supplying the C<SV_CATBYTES> flag, the
5462 appended string is interpreted as plain bytes; by supplying instead the
5463 C<SV_CATUTF8> flag, it will be interpreted as UTF-8, and the C<dsv> will be
5464 upgraded to UTF-8 if necessary.
5466 C<sv_catpvn>, C<sv_catpvn_mg>, and C<sv_catpvn_nomg> are implemented
5467 in terms of C<sv_catpvn_flags>.
5469 =for apidoc Amnh||SV_CATUTF8
5470 =for apidoc Amnh||SV_CATBYTES
5476 Perl_sv_catpvn_flags(pTHX_ SV *const dsv, const char *sstr, const STRLEN slen, const I32 flags)
5479 const char * const dstr = SvPV_force_flags(dsv, dlen, flags);
5481 PERL_ARGS_ASSERT_SV_CATPVN_FLAGS;
5482 assert((flags & (SV_CATBYTES|SV_CATUTF8)) != (SV_CATBYTES|SV_CATUTF8));
5484 if (!(flags & SV_CATBYTES) || !SvUTF8(dsv)) {
5485 if (flags & SV_CATUTF8 && !SvUTF8(dsv)) {
5486 sv_utf8_upgrade_flags_grow(dsv, 0, slen + 1);
5489 else SvGROW(dsv, dlen + slen + 3);
5491 sstr = SvPVX_const(dsv);
5492 Move(sstr, SvPVX(dsv) + dlen, slen, char);
5493 SvCUR_set(dsv, SvCUR(dsv) + slen);
5496 /* We inline bytes_to_utf8, to avoid an extra malloc. */
5497 const char * const send = sstr + slen;
5500 /* Something this code does not account for, which I think is
5501 impossible; it would require the same pv to be treated as
5502 bytes *and* utf8, which would indicate a bug elsewhere. */
5503 assert(sstr != dstr);
5505 SvGROW(dsv, dlen + slen * 2 + 3);
5506 d = (U8 *)SvPVX(dsv) + dlen;
5508 while (sstr < send) {
5509 append_utf8_from_native_byte(*sstr, &d);
5512 SvCUR_set(dsv, d-(const U8 *)SvPVX(dsv));
5515 (void)SvPOK_only_UTF8(dsv); /* validate pointer */
5517 if (flags & SV_SMAGIC)
5522 =for apidoc sv_catsv
5523 =for apidoc_item sv_catsv_flags
5524 =for apidoc_item sv_catsv_mg
5525 =for apidoc_item sv_catsv_nomg
5527 These concatenate the string from SV C<sstr> onto the end of the string in SV
5528 C<dsv>. If C<sstr> is null, these are no-ops; otherwise only C<dsv> is
5531 They differ only in what magic they perform:
5533 C<sv_catsv_mg> performs 'get' magic on both SVs before the copy, and 'set' magic
5534 on C<dsv> afterwards.
5536 C<sv_catsv> performs just 'get' magic, on both SVs.
5538 C<sv_catsv_nomg> skips all magic.
5540 C<sv_catsv_flags> has an extra C<flags> parameter which allows you to use
5541 C<SV_GMAGIC> and/or C<SV_SMAGIC> to specify any combination of magic handling
5542 (although either both or neither SV will have 'get' magic applied to it.)
5544 C<sv_catsv>, C<sv_catsv_mg>, and C<sv_catsv_nomg> are implemented
5545 in terms of C<sv_catsv_flags>.
5550 Perl_sv_catsv_flags(pTHX_ SV *const dsv, SV *const sstr, const I32 flags)
5552 PERL_ARGS_ASSERT_SV_CATSV_FLAGS;
5556 const char *spv = SvPV_flags_const(sstr, slen, flags);
5557 if (flags & SV_GMAGIC)
5559 sv_catpvn_flags(dsv, spv, slen,
5560 DO_UTF8(sstr) ? SV_CATUTF8 : SV_CATBYTES);
5561 if (flags & SV_SMAGIC)
5567 =for apidoc sv_catpv
5568 =for apidoc_item sv_catpv_flags
5569 =for apidoc_item sv_catpv_mg
5570 =for apidoc_item sv_catpv_nomg
5572 These concatenate the C<NUL>-terminated string C<sstr> onto the end of the
5573 string which is in the SV.
5574 If the SV has the UTF-8 status set, then the bytes appended should be
5577 They differ only in how they handle magic:
5579 C<sv_catpv_mg> performs both 'get' and 'set' magic.
5581 C<sv_catpv> performs only 'get' magic.
5583 C<sv_catpv_nomg> skips all magic.
5585 C<sv_catpv_flags> has an extra C<flags> parameter which allows you to specify
5586 any combination of magic handling (using C<SV_GMAGIC> and/or C<SV_SMAGIC>), and
5587 to also override the UTF-8 handling. By supplying the C<SV_CATUTF8> flag, the
5588 appended string is forced to be interpreted as UTF-8; by supplying instead the
5589 C<SV_CATBYTES> flag, it will be interpreted as just bytes. Either the SV or
5590 the string appended will be upgraded to UTF-8 if necessary.
5596 Perl_sv_catpv(pTHX_ SV *const dsv, const char *sstr)
5602 PERL_ARGS_ASSERT_SV_CATPV;
5606 junk = SvPV_force(dsv, tlen);
5608 SvGROW(dsv, tlen + len + 1);
5610 sstr = SvPVX_const(dsv);
5611 Move(sstr,SvPVX(dsv)+tlen,len+1,char);
5612 SvCUR_set(dsv, SvCUR(dsv) + len);
5613 (void)SvPOK_only_UTF8(dsv); /* validate pointer */
5618 Perl_sv_catpv_flags(pTHX_ SV *dsv, const char *sstr, const I32 flags)
5620 PERL_ARGS_ASSERT_SV_CATPV_FLAGS;
5621 sv_catpvn_flags(dsv, sstr, strlen(sstr), flags);
5625 Perl_sv_catpv_mg(pTHX_ SV *const dsv, const char *const sstr)
5627 PERL_ARGS_ASSERT_SV_CATPV_MG;
5636 Creates a new SV. A non-zero C<len> parameter indicates the number of
5637 bytes of preallocated string space the SV should have. An extra byte for a
5638 trailing C<NUL> is also reserved. (C<SvPOK> is not set for the SV even if string
5639 space is allocated.) The reference count for the new SV is set to 1.
5641 In 5.9.3, C<newSV()> replaces the older C<NEWSV()> API, and drops the first
5642 parameter, I<x>, a debug aid which allowed callers to identify themselves.
5643 This aid has been superseded by a new build option, C<PERL_MEM_LOG> (see
5644 L<perlhacktips/PERL_MEM_LOG>). The older API is still there for use in XS
5645 modules supporting older perls.
5651 Perl_newSV(pTHX_ const STRLEN len)
5658 sv = newSV_type(SVt_PV);
5659 sv_grow_fresh(sv, len + 1);
5664 =for apidoc sv_magicext
5666 Adds magic to an SV, upgrading it if necessary. Applies the
5667 supplied C<vtable> and returns a pointer to the magic added.
5669 Note that C<sv_magicext> will allow things that C<sv_magic> will not.
5670 In particular, you can add magic to C<SvREADONLY> SVs, and add more than
5671 one instance of the same C<how>.
5673 If C<namlen> is greater than zero then a C<savepvn> I<copy> of C<name> is
5674 stored, if C<namlen> is zero then C<name> is stored as-is and - as another
5675 special case - if C<(name && namlen == HEf_SVKEY)> then C<name> is assumed
5676 to contain an SV* and is stored as-is with its C<REFCNT> incremented.
5678 (This is now used as a subroutine by C<sv_magic>.)
5683 Perl_sv_magicext(pTHX_ SV *const sv, SV *const obj, const int how,
5684 const MGVTBL *const vtable, const char *const name, const I32 namlen)
5688 PERL_ARGS_ASSERT_SV_MAGICEXT;
5690 SvUPGRADE(sv, SVt_PVMG);
5691 Newxz(mg, 1, MAGIC);
5692 mg->mg_moremagic = SvMAGIC(sv);
5693 SvMAGIC_set(sv, mg);
5695 /* Sometimes a magic contains a reference loop, where the sv and
5696 object refer to each other. To prevent a reference loop that
5697 would prevent such objects being freed, we look for such loops
5698 and if we find one we avoid incrementing the object refcount.
5700 Note we cannot do this to avoid self-tie loops as intervening RV must
5701 have its REFCNT incremented to keep it in existence.
5704 if (!obj || obj == sv ||
5705 how == PERL_MAGIC_arylen ||
5706 how == PERL_MAGIC_regdata ||
5707 how == PERL_MAGIC_regdatum ||
5708 how == PERL_MAGIC_symtab ||
5709 (SvTYPE(obj) == SVt_PVGV &&
5710 (GvSV(obj) == sv || GvHV(obj) == (const HV *)sv
5711 || GvAV(obj) == (const AV *)sv || GvCV(obj) == (const CV *)sv
5712 || GvIOp(obj) == (const IO *)sv || GvFORM(obj) == (const CV *)sv)))
5717 mg->mg_obj = SvREFCNT_inc_simple(obj);
5718 mg->mg_flags |= MGf_REFCOUNTED;
5721 /* Normal self-ties simply pass a null object, and instead of
5722 using mg_obj directly, use the SvTIED_obj macro to produce a
5723 new RV as needed. For glob "self-ties", we are tieing the PVIO
5724 with an RV obj pointing to the glob containing the PVIO. In
5725 this case, to avoid a reference loop, we need to weaken the
5729 if (how == PERL_MAGIC_tiedscalar && SvTYPE(sv) == SVt_PVIO &&
5730 obj && SvROK(obj) && GvIO(SvRV(obj)) == (const IO *)sv)
5736 mg->mg_len = namlen;
5739 mg->mg_ptr = savepvn(name, namlen);
5740 else if (namlen == HEf_SVKEY) {
5741 /* Yes, this is casting away const. This is only for the case of
5742 HEf_SVKEY. I think we need to document this aberation of the
5743 constness of the API, rather than making name non-const, as
5744 that change propagating outwards a long way. */
5745 mg->mg_ptr = (char*)SvREFCNT_inc_simple_NN((SV *)name);
5747 mg->mg_ptr = (char *) name;
5749 mg->mg_virtual = (MGVTBL *) vtable;
5756 Perl_sv_magicext_mglob(pTHX_ SV *sv)
5758 PERL_ARGS_ASSERT_SV_MAGICEXT_MGLOB;
5759 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
5760 /* This sv is only a delegate. //g magic must be attached to
5765 return sv_magicext(sv, NULL, PERL_MAGIC_regex_global,
5766 &PL_vtbl_mglob, 0, 0);
5770 =for apidoc sv_magic
5772 Adds magic to an SV. First upgrades C<sv> to type C<SVt_PVMG> if
5773 necessary, then adds a new magic item of type C<how> to the head of the
5776 See C<L</sv_magicext>> (which C<sv_magic> now calls) for a description of the
5777 handling of the C<name> and C<namlen> arguments.
5779 You need to use C<sv_magicext> to add magic to C<SvREADONLY> SVs and also
5780 to add more than one instance of the same C<how>.
5786 Perl_sv_magic(pTHX_ SV *const sv, SV *const obj, const int how,
5787 const char *const name, const I32 namlen)
5789 const MGVTBL *vtable;
5792 unsigned int vtable_index;
5794 PERL_ARGS_ASSERT_SV_MAGIC;
5796 if (how < 0 || (unsigned)how >= C_ARRAY_LENGTH(PL_magic_data)
5797 || ((flags = PL_magic_data[how]),
5798 (vtable_index = flags & PERL_MAGIC_VTABLE_MASK)
5799 > magic_vtable_max))
5800 Perl_croak(aTHX_ "Don't know how to handle magic of type \\%o", how);
5802 /* PERL_MAGIC_ext is reserved for use by extensions not perl internals.
5803 Useful for attaching extension internal data to perl vars.
5804 Note that multiple extensions may clash if magical scalars
5805 etc holding private data from one are passed to another. */
5807 vtable = (vtable_index == magic_vtable_max)
5808 ? NULL : PL_magic_vtables + vtable_index;
5810 if (SvREADONLY(sv)) {
5812 !PERL_MAGIC_TYPE_READONLY_ACCEPTABLE(how)
5815 Perl_croak_no_modify();
5818 if (SvMAGICAL(sv) || (how == PERL_MAGIC_taint && SvTYPE(sv) >= SVt_PVMG)) {
5819 if (SvMAGIC(sv) && (mg = mg_find(sv, how))) {
5820 /* sv_magic() refuses to add a magic of the same 'how' as an
5823 if (how == PERL_MAGIC_taint)
5829 /* Rest of work is done else where */
5830 mg = sv_magicext(sv,obj,how,vtable,name,namlen);
5833 case PERL_MAGIC_taint:
5836 case PERL_MAGIC_ext:
5837 case PERL_MAGIC_dbfile:
5844 S_sv_unmagicext_flags(pTHX_ SV *const sv, const int type, MGVTBL *vtbl, const U32 flags)
5851 if (SvTYPE(sv) < SVt_PVMG || !SvMAGIC(sv))
5853 mgp = &(((XPVMG*) SvANY(sv))->xmg_u.xmg_magic);
5854 for (mg = *mgp; mg; mg = *mgp) {
5855 const MGVTBL* const virt = mg->mg_virtual;
5856 if (mg->mg_type == type && (!flags || virt == vtbl)) {
5857 *mgp = mg->mg_moremagic;
5858 if (virt && virt->svt_free)
5859 virt->svt_free(aTHX_ sv, mg);
5860 if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global) {
5862 Safefree(mg->mg_ptr);
5863 else if (mg->mg_len == HEf_SVKEY)
5864 SvREFCNT_dec(MUTABLE_SV(mg->mg_ptr));
5865 else if (mg->mg_type == PERL_MAGIC_utf8)
5866 Safefree(mg->mg_ptr);
5868 if (mg->mg_flags & MGf_REFCOUNTED)
5869 SvREFCNT_dec(mg->mg_obj);
5873 mgp = &mg->mg_moremagic;
5876 if (SvMAGICAL(sv)) /* if we're under save_magic, wait for restore_magic; */
5877 mg_magical(sv); /* else fix the flags now */
5886 =for apidoc sv_unmagic
5888 Removes all magic of type C<type> from an SV.
5894 Perl_sv_unmagic(pTHX_ SV *const sv, const int type)
5896 PERL_ARGS_ASSERT_SV_UNMAGIC;
5897 return S_sv_unmagicext_flags(aTHX_ sv, type, NULL, 0);
5901 =for apidoc sv_unmagicext
5903 Removes all magic of type C<type> with the specified C<vtbl> from an SV.
5909 Perl_sv_unmagicext(pTHX_ SV *const sv, const int type, MGVTBL *vtbl)
5911 PERL_ARGS_ASSERT_SV_UNMAGICEXT;
5912 return S_sv_unmagicext_flags(aTHX_ sv, type, vtbl, 1);
5916 =for apidoc sv_rvweaken
5918 Weaken a reference: set the C<SvWEAKREF> flag on this RV; give the
5919 referred-to SV C<PERL_MAGIC_backref> magic if it hasn't already; and
5920 push a back-reference to this RV onto the array of backreferences
5921 associated with that magic. If the RV is magical, set magic will be
5922 called after the RV is cleared. Silently ignores C<undef> and warns
5923 on already-weak references.
5929 Perl_sv_rvweaken(pTHX_ SV *const sv)
5933 PERL_ARGS_ASSERT_SV_RVWEAKEN;
5935 if (!SvOK(sv)) /* let undefs pass */
5938 Perl_croak(aTHX_ "Can't weaken a nonreference");
5939 else if (SvWEAKREF(sv)) {
5940 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Reference is already weak");
5943 else if (SvREADONLY(sv)) croak_no_modify();
5945 Perl_sv_add_backref(aTHX_ tsv, sv);
5947 SvREFCNT_dec_NN(tsv);
5952 =for apidoc sv_rvunweaken
5954 Unweaken a reference: Clear the C<SvWEAKREF> flag on this RV; remove
5955 the backreference to this RV from the array of backreferences
5956 associated with the target SV, increment the refcount of the target.
5957 Silently ignores C<undef> and warns on non-weak references.
5963 Perl_sv_rvunweaken(pTHX_ SV *const sv)
5967 PERL_ARGS_ASSERT_SV_RVUNWEAKEN;
5969 if (!SvOK(sv)) /* let undefs pass */
5972 Perl_croak(aTHX_ "Can't unweaken a nonreference");
5973 else if (!SvWEAKREF(sv)) {
5974 Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Reference is not weak");
5977 else if (SvREADONLY(sv)) croak_no_modify();
5982 SvREFCNT_inc_NN(tsv);
5983 Perl_sv_del_backref(aTHX_ tsv, sv);
5988 =for apidoc sv_get_backrefs
5990 If C<sv> is the target of a weak reference then it returns the back
5991 references structure associated with the sv; otherwise return C<NULL>.
5993 When returning a non-null result the type of the return is relevant. If it
5994 is an AV then the elements of the AV are the weak reference RVs which
5995 point at this item. If it is any other type then the item itself is the
5998 See also C<Perl_sv_add_backref()>, C<Perl_sv_del_backref()>,
5999 C<Perl_sv_kill_backrefs()>
6005 Perl_sv_get_backrefs(SV *const sv)
6009 PERL_ARGS_ASSERT_SV_GET_BACKREFS;
6011 /* find slot to store array or singleton backref */
6013 if (SvTYPE(sv) == SVt_PVHV) {
6015 struct xpvhv_aux * const iter = HvAUX((HV *)sv);
6016 backrefs = (SV *)iter->xhv_backreferences;
6018 } else if (SvMAGICAL(sv)) {
6019 MAGIC *mg = mg_find(sv, PERL_MAGIC_backref);
6021 backrefs = mg->mg_obj;
6026 /* Give tsv backref magic if it hasn't already got it, then push a
6027 * back-reference to sv onto the array associated with the backref magic.
6029 * As an optimisation, if there's only one backref and it's not an AV,
6030 * store it directly in the HvAUX or mg_obj slot, avoiding the need to
6031 * allocate an AV. (Whether the slot holds an AV tells us whether this is
6035 /* A discussion about the backreferences array and its refcount:
6037 * The AV holding the backreferences is pointed to either as the mg_obj of
6038 * PERL_MAGIC_backref, or in the specific case of a HV, from the
6039 * xhv_backreferences field. The array is created with a refcount
6040 * of 2. This means that if during global destruction the array gets
6041 * picked on before its parent to have its refcount decremented by the
6042 * random zapper, it won't actually be freed, meaning it's still there for
6043 * when its parent gets freed.
6045 * When the parent SV is freed, the extra ref is killed by
6046 * Perl_sv_kill_backrefs. The other ref is killed, in the case of magic,
6047 * by mg_free() / MGf_REFCOUNTED, or for a hash, by Perl_hv_kill_backrefs.
6049 * When a single backref SV is stored directly, it is not reference
6054 Perl_sv_add_backref(pTHX_ SV *const tsv, SV *const sv)
6060 PERL_ARGS_ASSERT_SV_ADD_BACKREF;
6062 /* find slot to store array or singleton backref */
6064 if (SvTYPE(tsv) == SVt_PVHV) {
6065 svp = (SV**)Perl_hv_backreferences_p(aTHX_ MUTABLE_HV(tsv));
6068 mg = mg_find(tsv, PERL_MAGIC_backref);
6070 mg = sv_magicext(tsv, NULL, PERL_MAGIC_backref, &PL_vtbl_backref, NULL, 0);
6071 svp = &(mg->mg_obj);
6074 /* create or retrieve the array */
6076 if ( (!*svp && SvTYPE(sv) == SVt_PVAV)
6077 || (*svp && SvTYPE(*svp) != SVt_PVAV)
6081 mg->mg_flags |= MGf_REFCOUNTED;
6084 SvREFCNT_inc_simple_void_NN(av);
6085 /* av now has a refcnt of 2; see discussion above */
6086 av_extend(av, *svp ? 2 : 1);
6088 /* move single existing backref to the array */
6089 AvARRAY(av)[++AvFILLp(av)] = *svp; /* av_push() */
6094 av = MUTABLE_AV(*svp);
6096 /* optimisation: store single backref directly in HvAUX or mg_obj */
6100 assert(SvTYPE(av) == SVt_PVAV);
6101 if (AvFILLp(av) >= AvMAX(av)) {
6102 av_extend(av, AvFILLp(av)+1);
6105 /* push new backref */
6106 AvARRAY(av)[++AvFILLp(av)] = sv; /* av_push() */
6109 /* delete a back-reference to ourselves from the backref magic associated
6110 * with the SV we point to.
6114 Perl_sv_del_backref(pTHX_ SV *const tsv, SV *const sv)
6118 PERL_ARGS_ASSERT_SV_DEL_BACKREF;
6120 if (SvTYPE(tsv) == SVt_PVHV) {
6122 svp = (SV**)Perl_hv_backreferences_p(aTHX_ MUTABLE_HV(tsv));
6124 else if (SvIS_FREED(tsv) && PL_phase == PERL_PHASE_DESTRUCT) {
6125 /* It's possible for the the last (strong) reference to tsv to have
6126 become freed *before* the last thing holding a weak reference.
6127 If both survive longer than the backreferences array, then when
6128 the referent's reference count drops to 0 and it is freed, it's
6129 not able to chase the backreferences, so they aren't NULLed.
6131 For example, a CV holds a weak reference to its stash. If both the
6132 CV and the stash survive longer than the backreferences array,
6133 and the CV gets picked for the SvBREAK() treatment first,
6134 *and* it turns out that the stash is only being kept alive because
6135 of an our variable in the pad of the CV, then midway during CV
6136 destruction the stash gets freed, but CvSTASH() isn't set to NULL.
6137 It ends up pointing to the freed HV. Hence it's chased in here, and
6138 if this block wasn't here, it would hit the !svp panic just below.
6140 I don't believe that "better" destruction ordering is going to help
6141 here - during global destruction there's always going to be the
6142 chance that something goes out of order. We've tried to make it
6143 foolproof before, and it only resulted in evolutionary pressure on
6144 fools. Which made us look foolish for our hubris. :-(
6150 = SvMAGICAL(tsv) ? mg_find(tsv, PERL_MAGIC_backref) : NULL;
6151 svp = mg ? &(mg->mg_obj) : NULL;
6155 Perl_croak(aTHX_ "panic: del_backref, svp=0");
6157 /* It's possible that sv is being freed recursively part way through the
6158 freeing of tsv. If this happens, the backreferences array of tsv has
6159 already been freed, and so svp will be NULL. If this is the case,
6160 we should not panic. Instead, nothing needs doing, so return. */
6161 if (PL_phase == PERL_PHASE_DESTRUCT && SvREFCNT(tsv) == 0)
6163 Perl_croak(aTHX_ "panic: del_backref, *svp=%p phase=%s refcnt=%" UVuf,
6164 (void*)*svp, PL_phase_names[PL_phase], (UV)SvREFCNT(tsv));
6167 if (SvTYPE(*svp) == SVt_PVAV) {
6171 AV * const av = (AV*)*svp;
6173 assert(!SvIS_FREED(av));
6177 /* for an SV with N weak references to it, if all those
6178 * weak refs are deleted, then sv_del_backref will be called
6179 * N times and O(N^2) compares will be done within the backref
6180 * array. To ameliorate this potential slowness, we:
6181 * 1) make sure this code is as tight as possible;
6182 * 2) when looking for SV, look for it at both the head and tail of the
6183 * array first before searching the rest, since some create/destroy
6184 * patterns will cause the backrefs to be freed in order.
6191 SV **p = &svp[fill];
6192 SV *const topsv = *p;
6199 /* We weren't the last entry.
6200 An unordered list has this property that you
6201 can take the last element off the end to fill
6202 the hole, and it's still an unordered list :-)
6208 break; /* should only be one */
6215 AvFILLp(av) = fill-1;
6217 else if (SvIS_FREED(*svp) && PL_phase == PERL_PHASE_DESTRUCT) {
6218 /* freed AV; skip */
6221 /* optimisation: only a single backref, stored directly */
6223 Perl_croak(aTHX_ "panic: del_backref, *svp=%p, sv=%p",
6224 (void*)*svp, (void*)sv);
6231 Perl_sv_kill_backrefs(pTHX_ SV *const sv, AV *const av)
6237 PERL_ARGS_ASSERT_SV_KILL_BACKREFS;
6242 /* after multiple passes through Perl_sv_clean_all() for a thingy
6243 * that has badly leaked, the backref array may have gotten freed,
6244 * since we only protect it against 1 round of cleanup */
6245 if (SvIS_FREED(av)) {
6246 if (PL_in_clean_all) /* All is fair */
6249 "panic: magic_killbackrefs (freed backref AV/SV)");
6253 is_array = (SvTYPE(av) == SVt_PVAV);
6255 assert(!SvIS_FREED(av));
6258 last = svp + AvFILLp(av);
6261 /* optimisation: only a single backref, stored directly */
6267 while (svp <= last) {
6269 SV *const referrer = *svp;
6270 if (SvWEAKREF(referrer)) {
6271 /* XXX Should we check that it hasn't changed? */
6272 assert(SvROK(referrer));
6273 SvRV_set(referrer, 0);
6275 SvWEAKREF_off(referrer);
6276 SvSETMAGIC(referrer);
6277 } else if (SvTYPE(referrer) == SVt_PVGV ||
6278 SvTYPE(referrer) == SVt_PVLV) {
6279 assert(SvTYPE(sv) == SVt_PVHV); /* stash backref */
6280 /* You lookin' at me? */
6281 assert(GvSTASH(referrer));
6282 assert(GvSTASH(referrer) == (const HV *)sv);
6283 GvSTASH(referrer) = 0;
6284 } else if (SvTYPE(referrer) == SVt_PVCV ||
6285 SvTYPE(referrer) == SVt_PVFM) {
6286 if (SvTYPE(sv) == SVt_PVHV) { /* stash backref */
6287 /* You lookin' at me? */
6288 assert(CvSTASH(referrer));
6289 assert(CvSTASH(referrer) == (const HV *)sv);
6290 SvANY(MUTABLE_CV(referrer))->xcv_stash = 0;
6293 assert(SvTYPE(sv) == SVt_PVGV);
6294 /* You lookin' at me? */
6295 assert(CvGV(referrer));
6296 assert(CvGV(referrer) == (const GV *)sv);
6297 anonymise_cv_maybe(MUTABLE_GV(sv),
6298 MUTABLE_CV(referrer));
6303 "panic: magic_killbackrefs (flags=%" UVxf ")",
6304 (UV)SvFLAGS(referrer));
6315 SvREFCNT_dec_NN(av); /* remove extra count added by sv_add_backref() */
6321 =for apidoc sv_insert
6323 Inserts and/or replaces a string at the specified offset/length within the SV.
6324 Similar to the Perl C<substr()> function, with C<littlelen> bytes starting at
6325 C<little> replacing C<len> bytes of the string in C<bigstr> starting at
6326 C<offset>. Handles get magic.
6328 =for apidoc sv_insert_flags
6330 Same as C<sv_insert>, but the extra C<flags> are passed to the
6331 C<SvPV_force_flags> that applies to C<bigstr>.
6337 Perl_sv_insert_flags(pTHX_ SV *const bigstr, const STRLEN offset, const STRLEN len, const char *little, const STRLEN littlelen, const U32 flags)
6343 SSize_t i; /* better be sizeof(STRLEN) or bad things happen */
6346 PERL_ARGS_ASSERT_SV_INSERT_FLAGS;
6348 SvPV_force_flags(bigstr, curlen, flags);
6349 (void)SvPOK_only_UTF8(bigstr);
6351 if (little >= SvPVX(bigstr) &&
6352 little < SvPVX(bigstr) + (SvLEN(bigstr) ? SvLEN(bigstr) : SvCUR(bigstr))) {
6353 /* little is a pointer to within bigstr, since we can reallocate bigstr,
6354 or little...little+littlelen might overlap offset...offset+len we make a copy
6356 little = savepvn(little, littlelen);
6360 if (offset + len > curlen) {
6361 SvGROW(bigstr, offset+len+1);
6362 Zero(SvPVX(bigstr)+curlen, offset+len-curlen, char);
6363 SvCUR_set(bigstr, offset+len);
6367 i = littlelen - len;
6368 if (i > 0) { /* string might grow */
6369 big = SvGROW(bigstr, SvCUR(bigstr) + i + 1);
6370 mid = big + offset + len;
6371 midend = bigend = big + SvCUR(bigstr);
6374 while (midend > mid) /* shove everything down */
6375 *--bigend = *--midend;
6376 Move(little,big+offset,littlelen,char);
6377 SvCUR_set(bigstr, SvCUR(bigstr) + i);
6382 Move(little,SvPVX(bigstr)+offset,len,char);
6387 big = SvPVX(bigstr);
6390 bigend = big + SvCUR(bigstr);
6392 if (midend > bigend)
6393 Perl_croak(aTHX_ "panic: sv_insert, midend=%p, bigend=%p",
6396 if (mid - big > bigend - midend) { /* faster to shorten from end */
6398 Move(little, mid, littlelen,char);
6401 i = bigend - midend;
6403 Move(midend, mid, i,char);
6407 SvCUR_set(bigstr, mid - big);
6409 else if ((i = mid - big)) { /* faster from front */
6410 midend -= littlelen;
6412 Move(big, midend - i, i, char);
6413 sv_chop(bigstr,midend-i);
6415 Move(little, mid, littlelen,char);
6417 else if (littlelen) {
6418 midend -= littlelen;
6419 sv_chop(bigstr,midend);
6420 Move(little,midend,littlelen,char);
6423 sv_chop(bigstr,midend);
6429 =for apidoc sv_replace
6431 Make the first argument a copy of the second, then delete the original.
6432 The target SV physically takes over ownership of the body of the source SV
6433 and inherits its flags; however, the target keeps any magic it owns,
6434 and any magic in the source is discarded.
6435 Note that this is a rather specialist SV copying operation; most of the
6436 time you'll want to use C<sv_setsv> or one of its many macro front-ends.
6442 Perl_sv_replace(pTHX_ SV *const sv, SV *const nsv)
6444 const U32 refcnt = SvREFCNT(sv);
6446 PERL_ARGS_ASSERT_SV_REPLACE;
6448 SV_CHECK_THINKFIRST_COW_DROP(sv);
6449 if (SvREFCNT(nsv) != 1) {
6450 Perl_croak(aTHX_ "panic: reference miscount on nsv in sv_replace()"
6451 " (%" UVuf " != 1)", (UV) SvREFCNT(nsv));
6453 if (SvMAGICAL(sv)) {
6457 sv_upgrade(nsv, SVt_PVMG);
6458 SvMAGIC_set(nsv, SvMAGIC(sv));
6459 SvFLAGS(nsv) |= SvMAGICAL(sv);
6461 SvMAGIC_set(sv, NULL);
6465 assert(!SvREFCNT(sv));
6466 #ifdef DEBUG_LEAKING_SCALARS
6467 sv->sv_flags = nsv->sv_flags;
6468 sv->sv_any = nsv->sv_any;
6469 sv->sv_refcnt = nsv->sv_refcnt;
6470 sv->sv_u = nsv->sv_u;
6472 StructCopy(nsv,sv,SV);
6474 if(SvTYPE(sv) == SVt_IV) {
6475 SET_SVANY_FOR_BODYLESS_IV(sv);
6479 SvREFCNT(sv) = refcnt;
6480 SvFLAGS(nsv) |= SVTYPEMASK; /* Mark as freed */
6485 /* We're about to free a GV which has a CV that refers back to us.
6486 * If that CV will outlive us, make it anonymous (i.e. fix up its CvGV
6490 S_anonymise_cv_maybe(pTHX_ GV *gv, CV* cv)
6495 PERL_ARGS_ASSERT_ANONYMISE_CV_MAYBE;
6498 assert(SvREFCNT(gv) == 0);
6499 assert(isGV(gv) && isGV_with_GP(gv));
6501 assert(!CvANON(cv));
6502 assert(CvGV(cv) == gv);
6503 assert(!CvNAMED(cv));
6505 /* will the CV shortly be freed by gp_free() ? */
6506 if (GvCV(gv) == cv && GvGP(gv)->gp_refcnt < 2 && SvREFCNT(cv) < 2) {
6507 SvANY(cv)->xcv_gv_u.xcv_gv = NULL;
6511 /* if not, anonymise: */
6512 gvname = (GvSTASH(gv) && HvNAME(GvSTASH(gv)) && HvENAME(GvSTASH(gv)))
6513 ? newSVhek(HvENAME_HEK(GvSTASH(gv)))
6514 : newSVpvn_flags( "__ANON__", 8, 0 );
6515 sv_catpvs(gvname, "::__ANON__");
6516 anongv = gv_fetchsv(gvname, GV_ADDMULTI, SVt_PVCV);
6517 SvREFCNT_dec_NN(gvname);
6521 SvANY(cv)->xcv_gv_u.xcv_gv = MUTABLE_GV(SvREFCNT_inc(anongv));
6526 =for apidoc sv_clear
6528 Clear an SV: call any destructors, free up any memory used by the body,
6529 and free the body itself. The SV's head is I<not> freed, although
6530 its type is set to all 1's so that it won't inadvertently be assumed
6531 to be live during global destruction etc.
6532 This function should only be called when C<REFCNT> is zero. Most of the time
6533 you'll want to call C<sv_free()> (or its macro wrapper C<SvREFCNT_dec>)
6540 Perl_sv_clear(pTHX_ SV *const orig_sv)
6545 STRLEN hash_index = 0; /* initialise to make Coverity et al happy.
6546 Not strictly necessary */
6548 PERL_ARGS_ASSERT_SV_CLEAR;
6550 /* within this loop, sv is the SV currently being freed, and
6551 * iter_sv is the most recent AV or whatever that's being iterated
6552 * over to provide more SVs */
6555 U32 type = SvTYPE(sv);
6558 assert(SvREFCNT(sv) == 0);
6559 assert(SvTYPE(sv) != (svtype)SVTYPEMASK);
6561 if (type <= SVt_IV) {
6562 /* Historically this check on type was needed so that the code to
6563 * free bodies wasn't reached for these types, because the arena
6564 * slots were re-used for HEs and pointer table entries. The
6565 * metadata table `bodies_by_type` had the information for the sizes
6566 * for HEs and PTEs, hence the code here had to have a special-case
6567 * check to ensure that the "regular" body freeing code wasn't
6568 * reached, and get confused by the "lies" in `bodies_by_type`.
6570 * However, it hasn't actually been needed for that reason since
6571 * Aug 2010 (commit 829cd18aa7f45221), because `bodies_by_type` was
6572 * changed to always hold the accurate metadata for the SV types.
6573 * This was possible because PTEs were no longer allocated from the
6574 * "SVt_IV" arena, and the code to allocate HEs from the "SVt_NULL"
6575 * arena is entirely in hv.c, so doesn't access the table.
6577 * Some sort of check is still needed to handle SVt_IVs - pure RVs
6578 * need to take one code path which is common with RVs stored in
6579 * SVt_PV (or larger), but pure IVs mustn't take the "PV but not RV"
6580 * path, as SvPVX() doesn't point to valid memory.
6582 * Hence this code is still the most efficient way to handle this.
6587 SvFLAGS(sv) &= SVf_BREAK;
6588 SvFLAGS(sv) |= SVTYPEMASK;
6592 /* objs are always >= MG, but pad names use the SVs_OBJECT flag
6593 for another purpose */
6594 assert(!SvOBJECT(sv) || type >= SVt_PVMG);
6596 if (type >= SVt_PVMG) {
6598 if (!curse(sv, 1)) goto get_next_sv;
6599 type = SvTYPE(sv); /* destructor may have changed it */
6601 /* Free back-references before magic, in case the magic calls
6602 * Perl code that has weak references to sv. */
6603 if (type == SVt_PVHV) {
6604 Perl_hv_kill_backrefs(aTHX_ MUTABLE_HV(sv));
6608 else if (SvMAGIC(sv)) {
6609 /* Free back-references before other types of magic. */
6610 sv_unmagic(sv, PERL_MAGIC_backref);
6616 /* case SVt_INVLIST: */
6619 IoIFP(sv) != PerlIO_stdin() &&
6620 IoIFP(sv) != PerlIO_stdout() &&
6621 IoIFP(sv) != PerlIO_stderr() &&
6622 !(IoFLAGS(sv) & IOf_FAKE_DIRP))
6624 io_close(MUTABLE_IO(sv), NULL, FALSE,
6625 (IoTYPE(sv) == IoTYPE_WRONLY ||
6626 IoTYPE(sv) == IoTYPE_RDWR ||
6627 IoTYPE(sv) == IoTYPE_APPEND));
6629 if (IoDIRP(sv) && !(IoFLAGS(sv) & IOf_FAKE_DIRP))
6630 PerlDir_close(IoDIRP(sv));
6631 IoDIRP(sv) = (DIR*)NULL;
6632 Safefree(IoTOP_NAME(sv));
6633 Safefree(IoFMT_NAME(sv));
6634 Safefree(IoBOTTOM_NAME(sv));
6635 if ((const GV *)sv == PL_statgv)
6639 /* FIXME for plugins */
6640 pregfree2((REGEXP*) sv);
6644 cv_undef(MUTABLE_CV(sv));
6645 /* If we're in a stash, we don't own a reference to it.
6646 * However it does have a back reference to us, which needs to
6648 if ((stash = CvSTASH(sv)))
6649 sv_del_backref(MUTABLE_SV(stash), sv);
6652 if (HvTOTALKEYS((HV*)sv) > 0) {
6654 /* this statement should match the one at the beginning of
6655 * hv_undef_flags() */
6656 if ( PL_phase != PERL_PHASE_DESTRUCT
6657 && (hek = HvNAME_HEK((HV*)sv)))
6659 if (PL_stashcache) {
6660 DEBUG_o(Perl_deb(aTHX_
6661 "sv_clear clearing PL_stashcache for '%" HEKf
6664 (void)hv_deletehek(PL_stashcache,
6667 hv_name_set((HV*)sv, NULL, 0, 0);
6670 /* save old iter_sv in unused SvSTASH field */
6671 assert(!SvOBJECT(sv));
6672 SvSTASH(sv) = (HV*)iter_sv;
6675 /* save old hash_index in unused SvMAGIC field */
6676 assert(!SvMAGICAL(sv));
6677 assert(!SvMAGIC(sv));
6678 ((XPVMG*) SvANY(sv))->xmg_u.xmg_hash_index = hash_index;
6681 next_sv = Perl_hfree_next_entry(aTHX_ (HV*)sv, &hash_index);
6682 goto get_next_sv; /* process this new sv */
6684 /* free empty hash */
6685 Perl_hv_undef_flags(aTHX_ MUTABLE_HV(sv), HV_NAME_SETALL);
6686 assert(!HvARRAY((HV*)sv));
6690 AV* av = MUTABLE_AV(sv);
6691 if (PL_comppad == av) {
6695 if (AvREAL(av) && AvFILLp(av) > -1) {
6696 next_sv = AvARRAY(av)[AvFILLp(av)--];
6697 /* save old iter_sv in top-most slot of AV,
6698 * and pray that it doesn't get wiped in the meantime */
6699 AvARRAY(av)[AvMAX(av)] = iter_sv;
6701 goto get_next_sv; /* process this new sv */
6703 Safefree(AvALLOC(av));
6708 if (LvTYPE(sv) == 'T') { /* for tie: return HE to pool */
6709 SvREFCNT_dec(HeKEY_sv((HE*)LvTARG(sv)));
6710 HeNEXT((HE*)LvTARG(sv)) = PL_hv_fetch_ent_mh;
6711 PL_hv_fetch_ent_mh = (HE*)LvTARG(sv);
6713 else if (LvTYPE(sv) != 't') /* unless tie: unrefcnted fake SV** */
6714 SvREFCNT_dec(LvTARG(sv));
6716 /* This PVLV has had a REGEXP assigned to it - the memory
6717 * normally used to store SvLEN instead points to a regex body.
6718 * Retrieving the pointer to the regex body from the correct
6719 * location is normally abstracted by ReANY(), which handles
6720 * both SVt_PVLV and SVt_REGEXP
6722 * This code is unwinding the storage specific to SVt_PVLV.
6723 * We get the body pointer directly from the union, free it,
6724 * then set SvLEN to whatever value was in the now-freed regex
6725 * body. The PVX buffer is shared by multiple re's and only
6726 * freed once, by the re whose SvLEN is non-null.
6728 * Perl_sv_force_normal_flags() also has code to free this
6729 * hidden body - it swaps the body into a temporary SV it has
6730 * just allocated, then frees that SV. That causes execution
6731 * to reach the SVt_REGEXP: case about 60 lines earlier in this
6734 * See Perl_reg_temp_copy() for the code that sets up this
6735 * REGEXP body referenced by the PVLV. */
6736 struct regexp *r = ((XPV*)SvANY(sv))->xpv_len_u.xpvlenu_rx;
6737 STRLEN len = r->xpv_len;
6738 pregfree2((REGEXP*) sv);
6739 del_body_by_type(r, SVt_REGEXP);
6740 SvLEN_set((sv), len);
6745 if (isGV_with_GP(sv)) {
6746 if(GvCVu((const GV *)sv) && (stash = GvSTASH(MUTABLE_GV(sv)))
6747 && HvENAME_get(stash))
6748 mro_method_changed_in(stash);
6749 gp_free(MUTABLE_GV(sv));
6751 unshare_hek(GvNAME_HEK(sv));
6752 /* If we're in a stash, we don't own a reference to it.
6753 * However it does have a back reference to us, which
6754 * needs to be cleared. */
6755 if ((stash = GvSTASH(sv)))
6756 sv_del_backref(MUTABLE_SV(stash), sv);
6758 /* FIXME. There are probably more unreferenced pointers to SVs
6759 * in the interpreter struct that we should check and tidy in
6760 * a similar fashion to this: */
6761 /* See also S_sv_unglob, which does the same thing. */
6762 if ((const GV *)sv == PL_last_in_gv)
6763 PL_last_in_gv = NULL;
6764 else if ((const GV *)sv == PL_statgv)
6766 else if ((const GV *)sv == PL_stderrgv)
6775 /* Don't bother with SvOOK_off(sv); as we're only going to
6779 SvOOK_offset(sv, offset);
6780 SvPV_set(sv, SvPVX_mutable(sv) - offset);
6781 /* Don't even bother with turning off the OOK flag. */
6786 SV * const target = SvRV(sv);
6788 sv_del_backref(target, sv);
6794 else if (SvPVX_const(sv)
6795 && !(SvTYPE(sv) == SVt_PVIO
6796 && !(IoFLAGS(sv) & IOf_FAKE_DIRP)))
6801 PerlIO_printf(Perl_debug_log, "Copy on write: clear\n");
6805 if (SvIsCOW_static(sv)) {
6808 else if (SvIsCOW_shared_hash(sv)) {
6809 unshare_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sv)));
6812 if (CowREFCNT(sv)) {
6821 Safefree(SvPVX_mutable(sv));
6825 else if (SvPVX_const(sv) && SvLEN(sv)
6826 && !(SvTYPE(sv) == SVt_PVIO
6827 && !(IoFLAGS(sv) & IOf_FAKE_DIRP)))
6828 Safefree(SvPVX_mutable(sv));
6829 else if (SvPVX_const(sv) && SvIsCOW(sv)) {
6830 unshare_hek(SvSHARED_HEK_FROM_PV(SvPVX_const(sv)));
6842 const struct body_details *sv_type_details;
6844 if (type == SVt_PVHV && HvHasAUX(sv)) {
6845 arena_index = HVAUX_ARENA_ROOT_IX;
6846 sv_type_details = &fake_hv_with_aux;
6850 sv_type_details = bodies_by_type + arena_index;
6853 SvFLAGS(sv) &= SVf_BREAK;
6854 SvFLAGS(sv) |= SVTYPEMASK;
6856 if (sv_type_details->arena) {
6857 del_body(((char *)SvANY(sv) + sv_type_details->offset),
6858 &PL_body_roots[arena_index]);
6860 else if (sv_type_details->body_size) {
6861 safefree(SvANY(sv));
6866 /* caller is responsible for freeing the head of the original sv */
6867 if (sv != orig_sv && !SvREFCNT(sv))
6870 /* grab and free next sv, if any */
6878 else if (!iter_sv) {
6880 } else if (SvTYPE(iter_sv) == SVt_PVAV) {
6881 AV *const av = (AV*)iter_sv;
6882 if (AvFILLp(av) > -1) {
6883 sv = AvARRAY(av)[AvFILLp(av)--];
6885 else { /* no more elements of current AV to free */
6888 /* restore previous value, squirrelled away */
6889 iter_sv = AvARRAY(av)[AvMAX(av)];
6890 Safefree(AvALLOC(av));
6893 } else if (SvTYPE(iter_sv) == SVt_PVHV) {
6894 sv = Perl_hfree_next_entry(aTHX_ (HV*)iter_sv, &hash_index);
6895 if (!sv && !HvTOTALKEYS((HV *)iter_sv)) {
6896 /* no more elements of current HV to free */
6899 /* Restore previous values of iter_sv and hash_index,
6900 * squirrelled away */
6901 assert(!SvOBJECT(sv));
6902 iter_sv = (SV*)SvSTASH(sv);
6903 assert(!SvMAGICAL(sv));
6904 hash_index = ((XPVMG*) SvANY(sv))->xmg_u.xmg_hash_index;
6906 /* perl -DA does not like rubbish in SvMAGIC. */
6910 /* free any remaining detritus from the hash struct */
6911 Perl_hv_undef_flags(aTHX_ MUTABLE_HV(sv), HV_NAME_SETALL);
6912 assert(!HvARRAY((HV*)sv));
6917 /* unrolled SvREFCNT_dec and sv_free2 follows: */
6921 if (!SvREFCNT(sv)) {
6925 if (--(SvREFCNT(sv)))
6929 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEBUGGING),
6930 "Attempt to free temp prematurely: SV 0x%" UVxf
6931 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
6935 if (SvIMMORTAL(sv)) {
6936 /* make sure SvREFCNT(sv)==0 happens very seldom */
6937 SvREFCNT(sv) = SvREFCNT_IMMORTAL;
6946 /* This routine curses the sv itself, not the object referenced by sv. So
6947 sv does not have to be ROK. */
6950 S_curse(pTHX_ SV * const sv, const bool check_refcnt) {
6951 PERL_ARGS_ASSERT_CURSE;
6952 assert(SvOBJECT(sv));
6954 if (PL_defstash && /* Still have a symbol table? */
6960 stash = SvSTASH(sv);
6961 assert(SvTYPE(stash) == SVt_PVHV);
6962 if (HvNAME(stash)) {
6963 CV* destructor = NULL;
6964 struct mro_meta *meta;
6966 assert (HvHasAUX(stash));
6968 DEBUG_o( Perl_deb(aTHX_ "Looking for DESTROY method for %s\n",
6971 /* don't make this an initialization above the assert, since it needs
6973 meta = HvMROMETA(stash);
6974 if (meta->destroy_gen && meta->destroy_gen == PL_sub_generation) {
6975 destructor = meta->destroy;
6976 DEBUG_o( Perl_deb(aTHX_ "Using cached DESTROY method %p for %s\n",
6977 (void *)destructor, HvNAME(stash)) );
6980 bool autoload = FALSE;
6982 gv_fetchmeth_pvn(stash, S_destroy, S_destroy_len, -1, 0);
6984 destructor = GvCV(gv);
6986 gv = gv_autoload_pvn(stash, S_destroy, S_destroy_len,
6987 GV_AUTOLOAD_ISMETHOD);
6989 destructor = GvCV(gv);
6993 /* we don't cache AUTOLOAD for DESTROY, since this code
6994 would then need to set $__PACKAGE__::AUTOLOAD, or the
6995 equivalent for XS AUTOLOADs */
6997 meta->destroy_gen = PL_sub_generation;
6998 meta->destroy = destructor;
7000 DEBUG_o( Perl_deb(aTHX_ "Set cached DESTROY method %p for %s\n",
7001 (void *)destructor, HvNAME(stash)) );
7004 DEBUG_o( Perl_deb(aTHX_ "Not caching AUTOLOAD for DESTROY method for %s\n",
7008 assert(!destructor || SvTYPE(destructor) == SVt_PVCV);
7010 /* A constant subroutine can have no side effects, so
7011 don't bother calling it. */
7012 && !CvCONST(destructor)
7013 /* Don't bother calling an empty destructor or one that
7014 returns immediately. */
7015 && (CvISXSUB(destructor)
7016 || (CvSTART(destructor)
7017 && (CvSTART(destructor)->op_next->op_type
7019 && (CvSTART(destructor)->op_next->op_type
7021 || CvSTART(destructor)->op_next->op_next->op_type
7027 SV* const tmpref = newRV(sv);
7028 SvREADONLY_on(tmpref); /* DESTROY() could be naughty */
7030 PUSHSTACKi(PERLSI_DESTROY);
7035 call_sv(MUTABLE_SV(destructor),
7036 G_DISCARD|G_EVAL|G_KEEPERR|G_VOID);
7040 if(SvREFCNT(tmpref) < 2) {
7041 /* tmpref is not kept alive! */
7043 SvRV_set(tmpref, NULL);
7046 SvREFCNT_dec_NN(tmpref);
7049 } while (SvOBJECT(sv) && SvSTASH(sv) != stash);
7052 if (check_refcnt && SvREFCNT(sv)) {
7053 if (PL_in_clean_objs)
7055 "DESTROY created new reference to dead object '%" HEKf "'",
7056 HEKfARG(HvNAME_HEK(stash)));
7057 /* DESTROY gave object new lease on life */
7063 HV * const stash = SvSTASH(sv);
7064 /* Curse before freeing the stash, as freeing the stash could cause
7065 a recursive call into S_curse. */
7066 SvOBJECT_off(sv); /* Curse the object. */
7067 SvSTASH_set(sv,0); /* SvREFCNT_dec may try to read this */
7068 SvREFCNT_dec(stash); /* possibly of changed persuasion */
7074 =for apidoc sv_newref
7076 Increment an SV's reference count. Use the C<SvREFCNT_inc()> wrapper
7083 Perl_sv_newref(pTHX_ SV *const sv)
7085 PERL_UNUSED_CONTEXT;
7094 Decrement an SV's reference count, and if it drops to zero, call
7095 C<sv_clear> to invoke destructors and free up any memory used by
7096 the body; finally, deallocating the SV's head itself.
7097 Normally called via a wrapper macro C<SvREFCNT_dec>.
7103 Perl_sv_free(pTHX_ SV *const sv)
7109 /* Private helper function for SvREFCNT_dec().
7110 * Called with rc set to original SvREFCNT(sv), where rc == 0 or 1 */
7113 Perl_sv_free2(pTHX_ SV *const sv, const U32 rc)
7116 PERL_ARGS_ASSERT_SV_FREE2;
7118 if (LIKELY( rc == 1 )) {
7124 Perl_ck_warner_d(aTHX_ packWARN(WARN_DEBUGGING),
7125 "Attempt to free temp prematurely: SV 0x%" UVxf
7126 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
7130 if (SvIMMORTAL(sv)) {
7131 /* make sure SvREFCNT(sv)==0 happens very seldom */
7132 SvREFCNT(sv) = SvREFCNT_IMMORTAL;
7136 if (! SvREFCNT(sv)) /* may have have been resurrected */
7141 /* handle exceptional cases */
7145 if (SvFLAGS(sv) & SVf_BREAK)
7146 /* this SV's refcnt has been artificially decremented to
7147 * trigger cleanup */
7149 if (PL_in_clean_all) /* All is fair */
7151 if (SvIMMORTAL(sv)) {
7152 /* make sure SvREFCNT(sv)==0 happens very seldom */
7153 SvREFCNT(sv) = SvREFCNT_IMMORTAL;
7156 if (ckWARN_d(WARN_INTERNAL)) {
7157 #ifdef DEBUG_LEAKING_SCALARS_FORK_DUMP
7158 Perl_dump_sv_child(aTHX_ sv);
7160 #ifdef DEBUG_LEAKING_SCALARS
7163 #ifdef DEBUG_LEAKING_SCALARS_ABORT
7164 if (PL_warnhook == PERL_WARNHOOK_FATAL
7165 || ckDEAD(packWARN(WARN_INTERNAL))) {
7166 /* Don't let Perl_warner cause us to escape our fate: */
7170 /* This may not return: */
7171 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
7172 "Attempt to free unreferenced scalar: SV 0x%" UVxf
7173 pTHX__FORMAT, PTR2UV(sv) pTHX__VALUE);
7176 #ifdef DEBUG_LEAKING_SCALARS_ABORT
7186 Returns the length of the string in the SV. Handles magic and type
7187 coercion and sets the UTF8 flag appropriately. See also C<L</SvCUR>>, which
7188 gives raw access to the C<xpv_cur> slot.
7194 Perl_sv_len(pTHX_ SV *const sv)
7201 (void)SvPV_const(sv, len);
7206 =for apidoc sv_len_utf8
7207 =for apidoc_item sv_len_utf8_nomg
7209 These return the number of characters in the string in an SV, counting wide
7210 UTF-8 bytes as a single character. Both handle type coercion.
7211 They differ only in that C<sv_len_utf8> performs 'get' magic;
7212 C<sv_len_utf8_nomg> skips any magic.
7218 * The length is cached in PERL_MAGIC_utf8, in the mg_len field. Also the
7219 * mg_ptr is used, by sv_pos_u2b() and sv_pos_b2u() - see the comments below.
7220 * (Note that the mg_len is not the length of the mg_ptr field.
7221 * This allows the cache to store the character length of the string without
7222 * needing to malloc() extra storage to attach to the mg_ptr.)
7227 Perl_sv_len_utf8(pTHX_ SV *const sv)
7233 return sv_len_utf8_nomg(sv);
7237 Perl_sv_len_utf8_nomg(pTHX_ SV * const sv)
7240 const U8 *s = (U8*)SvPV_nomg_const(sv, len);
7242 PERL_ARGS_ASSERT_SV_LEN_UTF8_NOMG;
7244 if (PL_utf8cache && SvUTF8(sv)) {
7246 MAGIC *mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_utf8) : NULL;
7248 if (mg && (mg->mg_len != -1 || mg->mg_ptr)) {
7249 if (mg->mg_len != -1)
7252 /* We can use the offset cache for a headstart.
7253 The longer value is stored in the first pair. */
7254 STRLEN *cache = (STRLEN *) mg->mg_ptr;
7256 ulen = cache[0] + Perl_utf8_length(aTHX_ s + cache[1],
7260 if (PL_utf8cache < 0) {
7261 const STRLEN real = Perl_utf8_length(aTHX_ s, s + len);
7262 assert_uft8_cache_coherent("sv_len_utf8", ulen, real, sv);
7266 ulen = Perl_utf8_length(aTHX_ s, s + len);
7267 utf8_mg_len_cache_update(sv, &mg, ulen);
7271 return SvUTF8(sv) ? Perl_utf8_length(aTHX_ s, s + len) : len;
7274 /* Walk forwards to find the byte corresponding to the passed in UTF-8
7277 S_sv_pos_u2b_forwards(const U8 *const start, const U8 *const send,
7278 STRLEN *const uoffset_p, bool *const at_end,
7279 bool* canonical_position)
7281 const U8 *s = start;
7282 STRLEN uoffset = *uoffset_p;
7284 PERL_ARGS_ASSERT_SV_POS_U2B_FORWARDS;
7286 while (s < send && uoffset) {
7293 else if (s > send) {
7295 /* This is the existing behaviour. Possibly it should be a croak, as
7296 it's actually a bounds error */
7299 /* If the unicode position is beyond the end, we return the end but
7300 shouldn't cache that position */
7301 *canonical_position = (uoffset == 0);
7302 *uoffset_p -= uoffset;
7306 /* Given the length of the string in both bytes and UTF-8 characters, decide
7307 whether to walk forwards or backwards to find the byte corresponding to
7308 the passed in UTF-8 offset. */
7310 S_sv_pos_u2b_midway(const U8 *const start, const U8 *send,
7311 STRLEN uoffset, const STRLEN uend)
7313 STRLEN backw = uend - uoffset;
7315 PERL_ARGS_ASSERT_SV_POS_U2B_MIDWAY;
7317 if (uoffset < 2 * backw) {
7318 /* The assumption is that going forwards is twice the speed of going
7319 forward (that's where the 2 * backw comes from).
7320 (The real figure of course depends on the UTF-8 data.) */
7321 const U8 *s = start;
7323 while (s < send && uoffset--)
7333 while (UTF8_IS_CONTINUATION(*send))
7336 return send - start;
7339 /* For the string representation of the given scalar, find the byte
7340 corresponding to the passed in UTF-8 offset. uoffset0 and boffset0
7341 give another position in the string, *before* the sought offset, which
7342 (which is always true, as 0, 0 is a valid pair of positions), which should
7343 help reduce the amount of linear searching.
7344 If *mgp is non-NULL, it should point to the UTF-8 cache magic, which
7345 will be used to reduce the amount of linear searching. The cache will be
7346 created if necessary, and the found value offered to it for update. */
7348 S_sv_pos_u2b_cached(pTHX_ SV *const sv, MAGIC **const mgp, const U8 *const start,
7349 const U8 *const send, STRLEN uoffset,
7350 STRLEN uoffset0, STRLEN boffset0)
7352 STRLEN boffset = 0; /* Actually always set, but let's keep gcc happy. */
7354 bool at_end = FALSE;
7355 bool canonical_position = FALSE;
7357 PERL_ARGS_ASSERT_SV_POS_U2B_CACHED;
7359 assert (uoffset >= uoffset0);
7364 if (!SvREADONLY(sv) && !SvGMAGICAL(sv) && SvPOK(sv)
7366 && (*mgp || (SvTYPE(sv) >= SVt_PVMG &&
7367 (*mgp = mg_find(sv, PERL_MAGIC_utf8))))) {
7368 if ((*mgp)->mg_ptr) {
7369 STRLEN *cache = (STRLEN *) (*mgp)->mg_ptr;
7370 if (cache[0] == uoffset) {
7371 /* An exact match. */
7374 if (cache[2] == uoffset) {
7375 /* An exact match. */
7379 if (cache[0] < uoffset) {
7380 /* The cache already knows part of the way. */
7381 if (cache[0] > uoffset0) {
7382 /* The cache knows more than the passed in pair */
7383 uoffset0 = cache[0];
7384 boffset0 = cache[1];
7386 if ((*mgp)->mg_len != -1) {
7387 /* And we know the end too. */
7389 + sv_pos_u2b_midway(start + boffset0, send,
7391 (*mgp)->mg_len - uoffset0);
7393 uoffset -= uoffset0;
7395 + sv_pos_u2b_forwards(start + boffset0,
7396 send, &uoffset, &at_end,
7397 &canonical_position);
7398 uoffset += uoffset0;
7401 else if (cache[2] < uoffset) {
7402 /* We're between the two cache entries. */
7403 if (cache[2] > uoffset0) {
7404 /* and the cache knows more than the passed in pair */
7405 uoffset0 = cache[2];
7406 boffset0 = cache[3];
7410 + sv_pos_u2b_midway(start + boffset0,
7413 cache[0] - uoffset0);
7416 + sv_pos_u2b_midway(start + boffset0,
7419 cache[2] - uoffset0);
7423 else if ((*mgp)->mg_len != -1) {
7424 /* If we can take advantage of a passed in offset, do so. */
7425 /* In fact, offset0 is either 0, or less than offset, so don't
7426 need to worry about the other possibility. */
7428 + sv_pos_u2b_midway(start + boffset0, send,
7430 (*mgp)->mg_len - uoffset0);
7435 if (!found || PL_utf8cache < 0) {
7436 STRLEN real_boffset;
7437 uoffset -= uoffset0;
7438 real_boffset = boffset0 + sv_pos_u2b_forwards(start + boffset0,
7439 send, &uoffset, &at_end,
7440 &canonical_position);
7441 uoffset += uoffset0;
7443 if (found && PL_utf8cache < 0)
7444 assert_uft8_cache_coherent("sv_pos_u2b_cache", boffset,
7446 boffset = real_boffset;
7449 if (PL_utf8cache && canonical_position && !SvGMAGICAL(sv) && SvPOK(sv)) {
7451 utf8_mg_len_cache_update(sv, mgp, uoffset);
7453 utf8_mg_pos_cache_update(sv, mgp, boffset, uoffset, send - start);
7460 =for apidoc sv_pos_u2b_flags
7462 Converts the offset from a count of UTF-8 chars from
7463 the start of the string, to a count of the equivalent number of bytes; if
7464 C<lenp> is non-zero, it does the same to C<lenp>, but this time starting from
7465 C<offset>, rather than from the start
7466 of the string. Handles type coercion.
7467 C<flags> is passed to C<SvPV_flags>, and usually should be
7468 C<SV_GMAGIC|SV_CONST_RETURN> to handle magic.
7474 * sv_pos_u2b_flags() uses, like sv_pos_b2u(), the mg_ptr of the potential
7475 * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
7476 * byte offsets. See also the comments of S_utf8_mg_pos_cache_update().
7481 Perl_sv_pos_u2b_flags(pTHX_ SV *const sv, STRLEN uoffset, STRLEN *const lenp,
7488 PERL_ARGS_ASSERT_SV_POS_U2B_FLAGS;
7490 start = (U8*)SvPV_flags(sv, len, flags);
7492 const U8 * const send = start + len;
7494 boffset = sv_pos_u2b_cached(sv, &mg, start, send, uoffset, 0, 0);
7497 && *lenp /* don't bother doing work for 0, as its bytes equivalent
7498 is 0, and *lenp is already set to that. */) {
7499 /* Convert the relative offset to absolute. */
7500 const STRLEN uoffset2 = uoffset + *lenp;
7501 const STRLEN boffset2
7502 = sv_pos_u2b_cached(sv, &mg, start, send, uoffset2,
7503 uoffset, boffset) - boffset;
7517 =for apidoc sv_pos_u2b
7519 Converts the value pointed to by C<offsetp> from a count of UTF-8 chars from
7520 the start of the string, to a count of the equivalent number of bytes; if
7521 C<lenp> is non-zero, it does the same to C<lenp>, but this time starting from
7522 the offset, rather than from the start of the string. Handles magic and
7525 Use C<sv_pos_u2b_flags> in preference, which correctly handles strings longer
7532 * sv_pos_u2b() uses, like sv_pos_b2u(), the mg_ptr of the potential
7533 * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
7534 * byte offsets. See also the comments of S_utf8_mg_pos_cache_update().
7538 /* This function is subject to size and sign problems */
7541 Perl_sv_pos_u2b(pTHX_ SV *const sv, I32 *const offsetp, I32 *const lenp)
7543 PERL_ARGS_ASSERT_SV_POS_U2B;
7546 STRLEN ulen = (STRLEN)*lenp;
7547 *offsetp = (I32)sv_pos_u2b_flags(sv, (STRLEN)*offsetp, &ulen,
7548 SV_GMAGIC|SV_CONST_RETURN);
7551 *offsetp = (I32)sv_pos_u2b_flags(sv, (STRLEN)*offsetp, NULL,
7552 SV_GMAGIC|SV_CONST_RETURN);
7557 S_utf8_mg_len_cache_update(pTHX_ SV *const sv, MAGIC **const mgp,
7560 PERL_ARGS_ASSERT_UTF8_MG_LEN_CACHE_UPDATE;
7561 if (SvREADONLY(sv) || SvGMAGICAL(sv) || !SvPOK(sv))
7564 if (!*mgp && (SvTYPE(sv) < SVt_PVMG ||
7565 !(*mgp = mg_find(sv, PERL_MAGIC_utf8)))) {
7566 *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, &PL_vtbl_utf8, 0, 0);
7570 (*mgp)->mg_len = ulen;
7573 /* Create and update the UTF8 magic offset cache, with the proffered utf8/
7574 byte length pairing. The (byte) length of the total SV is passed in too,
7575 as blen, because for some (more esoteric) SVs, the call to SvPV_const()
7576 may not have updated SvCUR, so we can't rely on reading it directly.
7578 The proffered utf8/byte length pairing isn't used if the cache already has
7579 two pairs, and swapping either for the proffered pair would increase the
7580 RMS of the intervals between known byte offsets.
7582 The cache itself consists of 4 STRLEN values
7583 0: larger UTF-8 offset
7584 1: corresponding byte offset
7585 2: smaller UTF-8 offset
7586 3: corresponding byte offset
7588 Unused cache pairs have the value 0, 0.
7589 Keeping the cache "backwards" means that the invariant of
7590 cache[0] >= cache[2] is maintained even with empty slots, which means that
7591 the code that uses it doesn't need to worry if only 1 entry has actually
7592 been set to non-zero. It also makes the "position beyond the end of the
7593 cache" logic much simpler, as the first slot is always the one to start
7597 S_utf8_mg_pos_cache_update(pTHX_ SV *const sv, MAGIC **const mgp, const STRLEN byte,
7598 const STRLEN utf8, const STRLEN blen)
7602 PERL_ARGS_ASSERT_UTF8_MG_POS_CACHE_UPDATE;
7607 if (!*mgp && (SvTYPE(sv) < SVt_PVMG ||
7608 !(*mgp = mg_find(sv, PERL_MAGIC_utf8)))) {
7609 *mgp = sv_magicext(sv, 0, PERL_MAGIC_utf8, (MGVTBL*)&PL_vtbl_utf8, 0,
7611 (*mgp)->mg_len = -1;
7615 if (!(cache = (STRLEN *)(*mgp)->mg_ptr)) {
7616 Newxz(cache, PERL_MAGIC_UTF8_CACHESIZE * 2, STRLEN);
7617 (*mgp)->mg_ptr = (char *) cache;
7621 if (PL_utf8cache < 0 && SvPOKp(sv)) {
7622 /* SvPOKp() because, if sv is a reference, then SvPVX() is actually
7623 a pointer. Note that we no longer cache utf8 offsets on refer-
7624 ences, but this check is still a good idea, for robustness. */
7625 const U8 *start = (const U8 *) SvPVX_const(sv);
7626 const STRLEN realutf8 = utf8_length(start, start + byte);
7628 assert_uft8_cache_coherent("utf8_mg_pos_cache_update", utf8, realutf8,
7632 /* Cache is held with the later position first, to simplify the code
7633 that deals with unbounded ends. */
7635 ASSERT_UTF8_CACHE(cache);
7636 if (cache[1] == 0) {
7637 /* Cache is totally empty */
7640 } else if (cache[3] == 0) {
7641 if (byte > cache[1]) {
7642 /* New one is larger, so goes first. */
7643 cache[2] = cache[0];
7644 cache[3] = cache[1];
7652 /* float casts necessary? XXX */
7653 #define THREEWAY_SQUARE(a,b,c,d) \
7654 ((float)((d) - (c))) * ((float)((d) - (c))) \
7655 + ((float)((c) - (b))) * ((float)((c) - (b))) \
7656 + ((float)((b) - (a))) * ((float)((b) - (a)))
7658 /* Cache has 2 slots in use, and we know three potential pairs.
7659 Keep the two that give the lowest RMS distance. Do the
7660 calculation in bytes simply because we always know the byte
7661 length. squareroot has the same ordering as the positive value,
7662 so don't bother with the actual square root. */
7663 if (byte > cache[1]) {
7664 /* New position is after the existing pair of pairs. */
7665 const float keep_earlier
7666 = THREEWAY_SQUARE(0, cache[3], byte, blen);
7667 const float keep_later
7668 = THREEWAY_SQUARE(0, cache[1], byte, blen);
7670 if (keep_later < keep_earlier) {
7671 cache[2] = cache[0];
7672 cache[3] = cache[1];
7678 const float keep_later = THREEWAY_SQUARE(0, byte, cache[1], blen);
7679 float b, c, keep_earlier;
7680 if (byte > cache[3]) {
7681 /* New position is between the existing pair of pairs. */
7682 b = (float)cache[3];
7685 /* New position is before the existing pair of pairs. */
7687 c = (float)cache[3];
7689 keep_earlier = THREEWAY_SQUARE(0, b, c, blen);
7690 if (byte > cache[3]) {
7691 if (keep_later < keep_earlier) {
7701 if (! (keep_later < keep_earlier)) {
7702 cache[0] = cache[2];
7703 cache[1] = cache[3];
7710 ASSERT_UTF8_CACHE(cache);
7713 /* We already know all of the way, now we may be able to walk back. The same
7714 assumption is made as in S_sv_pos_u2b_midway(), namely that walking
7715 backward is half the speed of walking forward. */
7717 S_sv_pos_b2u_midway(pTHX_ const U8 *const s, const U8 *const target,
7718 const U8 *end, STRLEN endu)
7720 const STRLEN forw = target - s;
7721 STRLEN backw = end - target;
7723 PERL_ARGS_ASSERT_SV_POS_B2U_MIDWAY;
7725 if (forw < 2 * backw) {
7726 return utf8_length(s, target);
7729 while (end > target) {
7731 while (UTF8_IS_CONTINUATION(*end)) {
7740 =for apidoc sv_pos_b2u_flags
7742 Converts C<offset> from a count of bytes from the start of the string, to
7743 a count of the equivalent number of UTF-8 chars. Handles type coercion.
7744 C<flags> is passed to C<SvPV_flags>, and usually should be
7745 C<SV_GMAGIC|SV_CONST_RETURN> to handle magic.
7751 * sv_pos_b2u_flags() uses, like sv_pos_u2b_flags(), the mg_ptr of the
7752 * potential PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8
7757 Perl_sv_pos_b2u_flags(pTHX_ SV *const sv, STRLEN const offset, U32 flags)
7760 STRLEN len = 0; /* Actually always set, but let's keep gcc happy. */
7766 PERL_ARGS_ASSERT_SV_POS_B2U_FLAGS;
7768 s = (const U8*)SvPV_flags(sv, blen, flags);
7771 Perl_croak(aTHX_ "panic: sv_pos_b2u: bad byte offset, blen=%" UVuf
7772 ", byte=%" UVuf, (UV)blen, (UV)offset);
7778 && SvTYPE(sv) >= SVt_PVMG
7779 && (mg = mg_find(sv, PERL_MAGIC_utf8)))
7782 STRLEN * const cache = (STRLEN *) mg->mg_ptr;
7783 if (cache[1] == offset) {
7784 /* An exact match. */
7787 if (cache[3] == offset) {
7788 /* An exact match. */
7792 if (cache[1] < offset) {
7793 /* We already know part of the way. */
7794 if (mg->mg_len != -1) {
7795 /* Actually, we know the end too. */
7797 + S_sv_pos_b2u_midway(aTHX_ s + cache[1], send,
7798 s + blen, mg->mg_len - cache[0]);
7800 len = cache[0] + utf8_length(s + cache[1], send);
7803 else if (cache[3] < offset) {
7804 /* We're between the two cached pairs, so we do the calculation
7805 offset by the byte/utf-8 positions for the earlier pair,
7806 then add the utf-8 characters from the string start to
7808 len = S_sv_pos_b2u_midway(aTHX_ s + cache[3], send,
7809 s + cache[1], cache[0] - cache[2])
7813 else { /* cache[3] > offset */
7814 len = S_sv_pos_b2u_midway(aTHX_ s, send, s + cache[3],
7818 ASSERT_UTF8_CACHE(cache);
7820 } else if (mg->mg_len != -1) {
7821 len = S_sv_pos_b2u_midway(aTHX_ s, send, s + blen, mg->mg_len);
7825 if (!found || PL_utf8cache < 0) {
7826 const STRLEN real_len = utf8_length(s, send);
7828 if (found && PL_utf8cache < 0)
7829 assert_uft8_cache_coherent("sv_pos_b2u", len, real_len, sv);
7835 utf8_mg_len_cache_update(sv, &mg, len);
7837 utf8_mg_pos_cache_update(sv, &mg, offset, len, blen);
7844 =for apidoc sv_pos_b2u
7846 Converts the value pointed to by C<offsetp> from a count of bytes from the
7847 start of the string, to a count of the equivalent number of UTF-8 chars.
7848 Handles magic and type coercion.
7850 Use C<sv_pos_b2u_flags> in preference, which correctly handles strings
7857 * sv_pos_b2u() uses, like sv_pos_u2b(), the mg_ptr of the potential
7858 * PERL_MAGIC_utf8 of the sv to store the mapping between UTF-8 and
7863 Perl_sv_pos_b2u(pTHX_ SV *const sv, I32 *const offsetp)
7865 PERL_ARGS_ASSERT_SV_POS_B2U;
7870 *offsetp = (I32)sv_pos_b2u_flags(sv, (STRLEN)*offsetp,
7871 SV_GMAGIC|SV_CONST_RETURN);
7875 S_assert_uft8_cache_coherent(pTHX_ const char *const func, STRLEN from_cache,
7876 STRLEN real, SV *const sv)
7878 PERL_ARGS_ASSERT_ASSERT_UFT8_CACHE_COHERENT;
7880 /* As this is debugging only code, save space by keeping this test here,
7881 rather than inlining it in all the callers. */
7882 if (from_cache == real)
7885 /* Need to turn the assertions off otherwise we may recurse infinitely
7886 while printing error messages. */
7887 SAVEI8(PL_utf8cache);
7889 Perl_croak(aTHX_ "panic: %s cache %" UVuf " real %" UVuf " for %" SVf,
7890 func, (UV) from_cache, (UV) real, SVfARG(sv));
7896 Returns a boolean indicating whether the strings in the two SVs are
7897 identical. Is UTF-8 and S<C<'use bytes'>> aware, handles get magic, and will
7898 coerce its args to strings if necessary.
7900 This function does not handle operator overloading. For a version that does,
7901 see instead C<sv_streq>.
7903 =for apidoc sv_eq_flags
7905 Returns a boolean indicating whether the strings in the two SVs are
7906 identical. Is UTF-8 and S<C<'use bytes'>> aware and coerces its args to strings
7907 if necessary. If the flags has the C<SV_GMAGIC> bit set, it handles get-magic, too.
7909 This function does not handle operator overloading. For a version that does,
7910 see instead C<sv_streq_flags>.
7916 Perl_sv_eq_flags(pTHX_ SV *sv1, SV *sv2, const U32 flags)
7928 /* if pv1 and pv2 are the same, second SvPV_const call may
7929 * invalidate pv1 (if we are handling magic), so we may need to
7931 if (sv1 == sv2 && flags & SV_GMAGIC
7932 && (SvTHINKFIRST(sv1) || SvGMAGICAL(sv1))) {
7933 pv1 = SvPV_const(sv1, cur1);
7934 sv1 = newSVpvn_flags(pv1, cur1, SVs_TEMP | SvUTF8(sv2));
7936 pv1 = SvPV_flags_const(sv1, cur1, flags);
7944 pv2 = SvPV_flags_const(sv2, cur2, flags);
7946 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
7947 /* Differing utf8ness. */
7949 /* sv1 is the UTF-8 one */
7950 return bytes_cmp_utf8((const U8*)pv2, cur2,
7951 (const U8*)pv1, cur1) == 0;
7954 /* sv2 is the UTF-8 one */
7955 return bytes_cmp_utf8((const U8*)pv1, cur1,
7956 (const U8*)pv2, cur2) == 0;
7961 return (pv1 == pv2) || memEQ(pv1, pv2, cur1);
7967 =for apidoc sv_streq_flags
7969 Returns a boolean indicating whether the strings in the two SVs are
7970 identical. If the flags argument has the C<SV_GMAGIC> bit set, it handles
7971 get-magic too. Will coerce its args to strings if necessary. Treats
7972 C<NULL> as undef. Correctly handles the UTF8 flag.
7974 If flags does not have the C<SV_SKIP_OVERLOAD> bit set, an attempt to use
7975 C<eq> overloading will be made. If such overloading does not exist or the
7976 flag is set, then regular string comparison will be used instead.
7978 =for apidoc sv_streq
7980 A convenient shortcut for calling C<sv_streq_flags> with the C<SV_GMAGIC>
7981 flag. This function basically behaves like the Perl code C<$sv1 eq $sv2>.
7987 Perl_sv_streq_flags(pTHX_ SV *sv1, SV *sv2, const U32 flags)
7989 PERL_ARGS_ASSERT_SV_STREQ_FLAGS;
7991 if(flags & SV_GMAGIC) {
7998 /* Treat NULL as undef */
8004 if(!(flags & SV_SKIP_OVERLOAD) &&
8005 (SvAMAGIC(sv1) || SvAMAGIC(sv2))) {
8006 SV *ret = amagic_call(sv1, sv2, seq_amg, 0);
8011 return sv_eq_flags(sv1, sv2, 0);
8015 =for apidoc sv_numeq_flags
8017 Returns a boolean indicating whether the numbers in the two SVs are
8018 identical. If the flags argument has the C<SV_GMAGIC> bit set, it handles
8019 get-magic too. Will coerce its args to numbers if necessary. Treats
8022 If flags does not have the C<SV_SKIP_OVERLOAD> bit set, an attempt to use
8023 C<==> overloading will be made. If such overloading does not exist or the
8024 flag is set, then regular numerical comparison will be used instead.
8026 =for apidoc sv_numeq
8028 A convenient shortcut for calling C<sv_numeq_flags> with the C<SV_GMAGIC>
8029 flag. This function basically behaves like the Perl code C<$sv1 == $sv2>.
8035 Perl_sv_numeq_flags(pTHX_ SV *sv1, SV *sv2, const U32 flags)
8037 PERL_ARGS_ASSERT_SV_NUMEQ_FLAGS;
8039 if(flags & SV_GMAGIC) {
8046 /* Treat NULL as undef */
8052 if(!(flags & SV_SKIP_OVERLOAD) &&
8053 (SvAMAGIC(sv1) || SvAMAGIC(sv2))) {
8054 SV *ret = amagic_call(sv1, sv2, eq_amg, 0);
8059 return do_ncmp(sv1, sv2) == 0;
8065 Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
8066 string in C<sv1> is less than, equal to, or greater than the string in
8067 C<sv2>. Is UTF-8 and S<C<'use bytes'>> aware, handles get magic, and will
8068 coerce its args to strings if necessary. See also C<L</sv_cmp_locale>>.
8070 =for apidoc sv_cmp_flags
8072 Compares the strings in two SVs. Returns -1, 0, or 1 indicating whether the
8073 string in C<sv1> is less than, equal to, or greater than the string in
8074 C<sv2>. Is UTF-8 and S<C<'use bytes'>> aware and will coerce its args to strings
8075 if necessary. If the flags has the C<SV_GMAGIC> bit set, it handles get magic. See
8076 also C<L</sv_cmp_locale_flags>>.
8082 Perl_sv_cmp(pTHX_ SV *const sv1, SV *const sv2)
8084 return sv_cmp_flags(sv1, sv2, SV_GMAGIC);
8088 Perl_sv_cmp_flags(pTHX_ SV *const sv1, SV *const sv2,
8092 const char *pv1, *pv2;
8094 SV *svrecode = NULL;
8101 pv1 = SvPV_flags_const(sv1, cur1, flags);
8108 pv2 = SvPV_flags_const(sv2, cur2, flags);
8110 if (cur1 && cur2 && SvUTF8(sv1) != SvUTF8(sv2) && !IN_BYTES) {
8111 /* Differing utf8ness. */
8113 const int retval = -bytes_cmp_utf8((const U8*)pv2, cur2,
8114 (const U8*)pv1, cur1);
8115 return retval ? retval < 0 ? -1 : +1 : 0;
8118 const int retval = bytes_cmp_utf8((const U8*)pv1, cur1,
8119 (const U8*)pv2, cur2);
8120 return retval ? retval < 0 ? -1 : +1 : 0;
8124 /* Here, if both are non-NULL, then they have the same UTF8ness. */
8127 cmp = cur2 ? -1 : 0;
8131 STRLEN shortest_len = cur1 < cur2 ? cur1 : cur2;
8134 if (! DO_UTF8(sv1)) {
8136 const I32 retval = memcmp((const void*)pv1,
8140 cmp = retval < 0 ? -1 : 1;
8141 } else if (cur1 == cur2) {
8144 cmp = cur1 < cur2 ? -1 : 1;
8148 else { /* Both are to be treated as UTF-EBCDIC */
8150 /* EBCDIC UTF-8 is complicated by the fact that it is based on I8
8151 * which remaps code points 0-255. We therefore generally have to
8152 * unmap back to the original values to get an accurate comparison.
8153 * But we don't have to do that for UTF-8 invariants, as by
8154 * definition, they aren't remapped, nor do we have to do it for
8155 * above-latin1 code points, as they also aren't remapped. (This
8156 * code also works on ASCII platforms, but the memcmp() above is
8159 const char *e = pv1 + shortest_len;
8161 /* Find the first bytes that differ between the two strings */
8162 while (pv1 < e && *pv1 == *pv2) {
8168 if (pv1 == e) { /* Are the same all the way to the end */
8172 cmp = cur1 < cur2 ? -1 : 1;
8175 else /* Here *pv1 and *pv2 are not equal, but all bytes earlier
8176 * in the strings were. The current bytes may or may not be
8177 * at the beginning of a character. But neither or both are
8178 * (or else earlier bytes would have been different). And
8179 * if we are in the middle of a character, the two
8180 * characters are comprised of the same number of bytes
8181 * (because in this case the start bytes are the same, and
8182 * the start bytes encode the character's length). */
8183 if (UTF8_IS_INVARIANT(*pv1))
8185 /* If both are invariants; can just compare directly */
8186 if (UTF8_IS_INVARIANT(*pv2)) {
8187 cmp = ((U8) *pv1 < (U8) *pv2) ? -1 : 1;
8189 else /* Since *pv1 is invariant, it is the whole character,
8190 which means it is at the beginning of a character.
8191 That means pv2 is also at the beginning of a
8192 character (see earlier comment). Since it isn't
8193 invariant, it must be a start byte. If it starts a
8194 character whose code point is above 255, that
8195 character is greater than any single-byte char, which
8197 if (UTF8_IS_ABOVE_LATIN1_START(*pv2))
8202 /* Here, pv2 points to a character composed of 2 bytes
8203 * whose code point is < 256. Get its code point and
8204 * compare with *pv1 */
8205 cmp = ((U8) *pv1 < EIGHT_BIT_UTF8_TO_NATIVE(*pv2, *(pv2 + 1)))
8210 else /* The code point starting at pv1 isn't a single byte */
8211 if (UTF8_IS_INVARIANT(*pv2))
8213 /* But here, the code point starting at *pv2 is a single byte,
8214 * and so *pv1 must begin a character, hence is a start byte.
8215 * If that character is above 255, it is larger than any
8216 * single-byte char, which *pv2 is */
8217 if (UTF8_IS_ABOVE_LATIN1_START(*pv1)) {
8221 /* Here, pv1 points to a character composed of 2 bytes
8222 * whose code point is < 256. Get its code point and
8223 * compare with the single byte character *pv2 */
8224 cmp = (EIGHT_BIT_UTF8_TO_NATIVE(*pv1, *(pv1 + 1)) < (U8) *pv2)
8229 else /* Here, we've ruled out either *pv1 and *pv2 being
8230 invariant. That means both are part of variants, but not
8231 necessarily at the start of a character */
8232 if ( UTF8_IS_ABOVE_LATIN1_START(*pv1)
8233 || UTF8_IS_ABOVE_LATIN1_START(*pv2))
8235 /* Here, at least one is the start of a character, which means
8236 * the other is also a start byte. And the code point of at
8237 * least one of the characters is above 255. It is a
8238 * characteristic of UTF-EBCDIC that all start bytes for
8239 * above-latin1 code points are well behaved as far as code
8240 * point comparisons go, and all are larger than all other
8241 * start bytes, so the comparison with those is also well
8243 cmp = ((U8) *pv1 < (U8) *pv2) ? -1 : 1;
8246 /* Here both *pv1 and *pv2 are part of variant characters.
8247 * They could be both continuations, or both start characters.
8248 * (One or both could even be an illegal start character (for
8249 * an overlong) which for the purposes of sorting we treat as
8251 if (UTF8_IS_CONTINUATION(*pv1)) {
8253 /* If they are continuations for code points above 255,
8254 * then comparing the current byte is sufficient, as there
8255 * is no remapping of these and so the comparison is
8256 * well-behaved. We determine if they are such
8257 * continuations by looking at the preceding byte. It
8258 * could be a start byte, from which we can tell if it is
8259 * for an above 255 code point. Or it could be a
8260 * continuation, which means the character occupies at
8261 * least 3 bytes, so must be above 255. */
8262 if ( UTF8_IS_CONTINUATION(*(pv2 - 1))
8263 || UTF8_IS_ABOVE_LATIN1_START(*(pv2 -1)))
8265 cmp = ((U8) *pv1 < (U8) *pv2) ? -1 : 1;
8269 /* Here, the continuations are for code points below 256;
8270 * back up one to get to the start byte */
8275 /* We need to get the actual native code point of each of these
8276 * variants in order to compare them */
8277 cmp = ( EIGHT_BIT_UTF8_TO_NATIVE(*pv1, *(pv1 + 1))
8278 < EIGHT_BIT_UTF8_TO_NATIVE(*pv2, *(pv2 + 1)))
8287 SvREFCNT_dec(svrecode);
8293 =for apidoc sv_cmp_locale
8295 Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and
8296 S<C<'use bytes'>> aware, handles get magic, and will coerce its args to strings
8297 if necessary. See also C<L</sv_cmp>>.
8299 =for apidoc sv_cmp_locale_flags
8301 Compares the strings in two SVs in a locale-aware manner. Is UTF-8 and
8302 S<C<'use bytes'>> aware and will coerce its args to strings if necessary. If
8303 the flags contain C<SV_GMAGIC>, it handles get magic. See also
8304 C<L</sv_cmp_flags>>.
8310 Perl_sv_cmp_locale(pTHX_ SV *const sv1, SV *const sv2)
8312 return sv_cmp_locale_flags(sv1, sv2, SV_GMAGIC);
8316 Perl_sv_cmp_locale_flags(pTHX_ SV *const sv1, SV *const sv2,
8319 #ifdef USE_LOCALE_COLLATE
8325 if (PL_collation_standard)
8330 /* Revert to using raw compare if both operands exist, but either one
8331 * doesn't transform properly for collation */
8333 pv1 = sv_collxfrm_flags(sv1, &len1, flags);
8337 pv2 = sv_collxfrm_flags(sv2, &len2, flags);
8343 pv1 = sv1 ? sv_collxfrm_flags(sv1, &len1, flags) : (char *) NULL;
8344 pv2 = sv2 ? sv_collxfrm_flags(sv2, &len2, flags) : (char *) NULL;
8347 if (!pv1 || !len1) {
8358 retval = memcmp((void*)pv1, (void*)pv2, len1 < len2 ? len1 : len2);
8361 return retval < 0 ? -1 : 1;
8364 * When the result of collation is equality, that doesn't mean
8365 * that there are no differences -- some locales exclude some
8366 * characters from consideration. So to avoid false equalities,
8367 * we use the raw string as a tiebreaker.
8374 PERL_UNUSED_ARG(flags);
8375 #endif /* USE_LOCALE_COLLATE */
8377 return sv_cmp(sv1, sv2);
8381 #ifdef USE_LOCALE_COLLATE
8384 =for apidoc sv_collxfrm
8386 This calls C<sv_collxfrm_flags> with the SV_GMAGIC flag. See
8387 C<L</sv_collxfrm_flags>>.
8389 =for apidoc sv_collxfrm_flags
8391 Add Collate Transform magic to an SV if it doesn't already have it. If the
8392 flags contain C<SV_GMAGIC>, it handles get-magic.
8394 Any scalar variable may carry C<PERL_MAGIC_collxfrm> magic that contains the
8395 scalar data of the variable, but transformed to such a format that a normal
8396 memory comparison can be used to compare the data according to the locale
8403 Perl_sv_collxfrm_flags(pTHX_ SV *const sv, STRLEN *const nxp, const I32 flags)
8407 PERL_ARGS_ASSERT_SV_COLLXFRM_FLAGS;
8409 mg = SvMAGICAL(sv) ? mg_find(sv, PERL_MAGIC_collxfrm) : (MAGIC *) NULL;
8411 /* If we don't have collation magic on 'sv', or the locale has changed
8412 * since the last time we calculated it, get it and save it now */
8413 if (!mg || !mg->mg_ptr || *(U32*)mg->mg_ptr != PL_collation_ix) {
8418 /* Free the old space */
8420 Safefree(mg->mg_ptr);
8422 s = SvPV_flags_const(sv, len, flags);
8423 if ((xf = _mem_collxfrm(s, len, &xlen, cBOOL(SvUTF8(sv))))) {
8425 mg = sv_magicext(sv, 0, PERL_MAGIC_collxfrm, &PL_vtbl_collxfrm,
8440 if (mg && mg->mg_ptr) {
8442 return mg->mg_ptr + sizeof(PL_collation_ix);
8450 #endif /* USE_LOCALE_COLLATE */
8453 S_sv_gets_append_to_utf8(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
8455 SV * const tsv = newSV_type(SVt_NULL);
8458 sv_gets(tsv, fp, 0);
8459 sv_utf8_upgrade_nomg(tsv);
8460 SvCUR_set(sv,append);
8463 return (SvCUR(sv) - append) ? SvPVX(sv) : NULL;
8467 S_sv_gets_read_record(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
8470 const STRLEN recsize = SvUV(SvRV(PL_rs)); /* RsRECORD() guarantees > 0. */
8471 /* Grab the size of the record we're getting */
8472 char *buffer = SvGROW(sv, (STRLEN)(recsize + append + 1)) + append;
8479 /* With a true, record-oriented file on VMS, we need to use read directly
8480 * to ensure that we respect RMS record boundaries. The user is responsible
8481 * for providing a PL_rs value that corresponds to the FAB$W_MRS (maximum
8482 * record size) field. N.B. This is likely to produce invalid results on
8483 * varying-width character data when a record ends mid-character.
8485 fd = PerlIO_fileno(fp);
8487 && PerlLIO_fstat(fd, &st) == 0
8488 && (st.st_fab_rfm == FAB$C_VAR
8489 || st.st_fab_rfm == FAB$C_VFC
8490 || st.st_fab_rfm == FAB$C_FIX)) {
8492 bytesread = PerlLIO_read(fd, buffer, recsize);
8494 else /* in-memory file from PerlIO::Scalar
8495 * or not a record-oriented file
8499 bytesread = PerlIO_read(fp, buffer, recsize);
8501 /* At this point, the logic in sv_get() means that sv will
8502 be treated as utf-8 if the handle is utf8.
8504 if (PerlIO_isutf8(fp) && bytesread > 0) {
8505 char *bend = buffer + bytesread;
8506 char *bufp = buffer;
8507 size_t charcount = 0;
8508 bool charstart = TRUE;
8511 while (charcount < recsize) {
8512 /* count accumulated characters */
8513 while (bufp < bend) {
8515 skip = UTF8SKIP(bufp);
8517 if (bufp + skip > bend) {
8518 /* partial at the end */
8529 if (charcount < recsize) {
8531 STRLEN bufp_offset = bufp - buffer;
8532 SSize_t morebytesread;
8534 /* originally I read enough to fill any incomplete
8535 character and the first byte of the next
8536 character if needed, but if there's many
8537 multi-byte encoded characters we're going to be
8538 making a read call for every character beyond
8539 the original read size.
8541 So instead, read the rest of the character if
8542 any, and enough bytes to match at least the
8543 start bytes for each character we're going to
8547 readsize = recsize - charcount;
8549 readsize = skip - (bend - bufp) + recsize - charcount - 1;
8550 buffer = SvGROW(sv, append + bytesread + readsize + 1) + append;
8551 bend = buffer + bytesread;
8552 morebytesread = PerlIO_read(fp, bend, readsize);
8553 if (morebytesread <= 0) {
8554 /* we're done, if we still have incomplete
8555 characters the check code in sv_gets() will
8558 I'd originally considered doing
8559 PerlIO_ungetc() on all but the lead
8560 character of the incomplete character, but
8561 read() doesn't do that, so I don't.
8566 /* prepare to scan some more */
8567 bytesread += morebytesread;
8568 bend = buffer + bytesread;
8569 bufp = buffer + bufp_offset;
8577 SvCUR_set(sv, bytesread + append);
8578 buffer[bytesread] = '\0';
8579 return (SvCUR(sv) - append) ? SvPVX(sv) : NULL;
8585 Get a line from the filehandle and store it into the SV, optionally
8586 appending to the currently-stored string. If C<append> is not 0, the
8587 line is appended to the SV instead of overwriting it. C<append> should
8588 be set to the byte offset that the appended string should start at
8589 in the SV (typically, C<SvCUR(sv)> is a suitable choice).
8595 Perl_sv_gets(pTHX_ SV *const sv, PerlIO *const fp, I32 append)
8605 PERL_ARGS_ASSERT_SV_GETS;
8607 if (SvTHINKFIRST(sv))
8608 sv_force_normal_flags(sv, append ? 0 : SV_COW_DROP_PV);
8609 /* XXX. If you make this PVIV, then copy on write can copy scalars read
8611 However, perlbench says it's slower, because the existing swipe code
8612 is faster than copy on write.
8613 Swings and roundabouts. */
8614 SvUPGRADE(sv, SVt_PV);
8617 /* line is going to be appended to the existing buffer in the sv */
8618 if (PerlIO_isutf8(fp)) {
8620 sv_utf8_upgrade_nomg(sv);
8621 sv_pos_u2b(sv,&append,0);
8623 } else if (SvUTF8(sv)) {
8624 return S_sv_gets_append_to_utf8(aTHX_ sv, fp, append);
8630 /* not appending - "clear" the string by setting SvCUR to 0,
8631 * the pv is still avaiable. */
8634 if (PerlIO_isutf8(fp))
8637 if (IN_PERL_COMPILETIME) {
8638 /* we always read code in line mode */
8642 else if (RsSNARF(PL_rs)) {
8643 /* If it is a regular disk file use size from stat() as estimate
8644 of amount we are going to read -- may result in mallocing
8645 more memory than we really need if the layers below reduce
8646 the size we read (e.g. CRLF or a gzip layer).
8649 int fd = PerlIO_fileno(fp);
8650 if (fd >= 0 && (PerlLIO_fstat(fd, &st) == 0) && S_ISREG(st.st_mode)) {
8651 const Off_t offset = PerlIO_tell(fp);
8652 if (offset != (Off_t) -1 && st.st_size + append > offset) {
8653 #ifdef PERL_COPY_ON_WRITE
8654 /* Add an extra byte for the sake of copy-on-write's
8655 * buffer reference count. */
8656 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 2));
8658 (void) SvGROW(sv, (STRLEN)((st.st_size - offset) + append + 1));
8665 else if (RsRECORD(PL_rs)) {
8666 return S_sv_gets_read_record(aTHX_ sv, fp, append);
8668 else if (RsPARA(PL_rs)) {
8674 /* Get $/ i.e. PL_rs into same encoding as stream wants */
8675 if (PerlIO_isutf8(fp)) {
8676 rsptr = SvPVutf8(PL_rs, rslen);
8679 if (SvUTF8(PL_rs)) {
8680 if (!sv_utf8_downgrade(PL_rs, TRUE)) {
8681 Perl_croak(aTHX_ "Wide character in $/");
8684 /* extract the raw pointer to the record separator */
8685 rsptr = SvPV_const(PL_rs, rslen);
8689 /* rslast is the last character in the record separator
8690 * note we don't use rslast except when rslen is true, so the
8691 * null assign is a placeholder. */
8692 rslast = rslen ? rsptr[rslen - 1] : '\0';
8694 if (rspara) { /* have to do this both before and after */
8695 /* to make sure file boundaries work right */
8699 i = PerlIO_getc(fp);
8703 PerlIO_ungetc(fp,i);
8709 /* See if we know enough about I/O mechanism to cheat it ! */
8711 /* This used to be #ifdef test - it is made run-time test for ease
8712 of abstracting out stdio interface. One call should be cheap
8713 enough here - and may even be a macro allowing compile
8717 if (PerlIO_fast_gets(fp)) {
8719 * We can do buffer based IO operations on this filehandle.
8721 * This means we can bypass a lot of subcalls and process
8722 * the buffer directly, it also means we know the upper bound
8723 * on the amount of data we might read of the current buffer
8724 * into our sv. Knowing this allows us to preallocate the pv
8725 * to be able to hold that maximum, which allows us to simplify
8726 * a lot of logic. */
8729 * We're going to steal some values from the stdio struct
8730 * and put EVERYTHING in the innermost loop into registers.
8732 STDCHAR *ptr; /* pointer into fp's read-ahead buffer */
8733 STRLEN bpx; /* length of the data in the target sv
8734 used to fix pointers after a SvGROW */
8735 I32 shortbuffered; /* If the pv buffer is shorter than the amount
8736 of data left in the read-ahead buffer.
8737 If 0 then the pv buffer can hold the full
8738 amount left, otherwise this is the amount it
8741 /* Here is some breathtakingly efficient cheating */
8743 /* When you read the following logic resist the urge to think
8744 * of record separators that are 1 byte long. They are an
8745 * uninteresting special (simple) case.
8747 * Instead think of record separators which are at least 2 bytes
8748 * long, and keep in mind that we need to deal with such
8749 * separators when they cross a read-ahead buffer boundary.
8751 * Also consider that we need to gracefully deal with separators
8752 * that may be longer than a single read ahead buffer.
8754 * Lastly do not forget we want to copy the delimiter as well. We
8755 * are copying all data in the file _up_to_and_including_ the separator
8758 * Now that you have all that in mind here is what is happening below:
8760 * 1. When we first enter the loop we do some memory book keeping to see
8761 * how much free space there is in the target SV. (This sub assumes that
8762 * it is operating on the same SV most of the time via $_ and that it is
8763 * going to be able to reuse the same pv buffer each call.) If there is
8764 * "enough" room then we set "shortbuffered" to how much space there is
8765 * and start reading forward.
8767 * 2. When we scan forward we copy from the read-ahead buffer to the target
8768 * SV's pv buffer. While we go we watch for the end of the read-ahead buffer,
8769 * and the end of the of pv, as well as for the "rslast", which is the last
8770 * char of the separator.
8772 * 3. When scanning forward if we see rslast then we jump backwards in *pv*
8773 * (which has a "complete" record up to the point we saw rslast) and check
8774 * it to see if it matches the separator. If it does we are done. If it doesn't
8775 * we continue on with the scan/copy.
8777 * 4. If we run out of read-ahead buffer (cnt goes to 0) then we have to get
8778 * the IO system to read the next buffer. We do this by doing a getc(), which
8779 * returns a single char read (or EOF), and prefills the buffer, and also
8780 * allows us to find out how full the buffer is. We use this information to
8781 * SvGROW() the sv to the size remaining in the buffer, after which we copy
8782 * the returned single char into the target sv, and then go back into scan
8785 * 5. If we run out of write-buffer then we SvGROW() it by the size of the
8786 * remaining space in the read-buffer.
8788 * Note that this code despite its twisty-turny nature is pretty darn slick.
8789 * It manages single byte separators, multi-byte cross boundary separators,
8790 * and cross-read-buffer separators cleanly and efficiently at the cost
8791 * of potentially greatly overallocating the target SV.
8797 /* get the number of bytes remaining in the read-ahead buffer
8798 * on first call on a given fp this will return 0.*/
8799 cnt = PerlIO_get_cnt(fp);
8801 /* make sure we have the room */
8802 if ((I32)(SvLEN(sv) - append) <= cnt + 1) {
8803 /* Not room for all of it
8804 if we are looking for a separator and room for some
8806 if (rslen && cnt > 80 && (I32)SvLEN(sv) > append) {
8807 /* just process what we have room for */
8808 shortbuffered = cnt - SvLEN(sv) + append + 1;
8809 cnt -= shortbuffered;
8812 /* ensure that the target sv has enough room to hold
8813 * the rest of the read-ahead buffer */
8815 /* remember that cnt can be negative */
8816 SvGROW(sv, (STRLEN)(append + (cnt <= 0 ? 2 : (cnt + 1))));
8820 /* we have enough room to hold the full buffer, lets scream */
8824 /* extract the pointer to sv's string buffer, offset by append as necessary */
8825 bp = (STDCHAR*)SvPVX_const(sv) + append; /* move these two too to registers */
8826 /* extract the point to the read-ahead buffer */
8827 ptr = (STDCHAR*)PerlIO_get_ptr(fp);
8829 /* some trace debug output */
8830 DEBUG_P(PerlIO_printf(Perl_debug_log,
8831 "Screamer: entering, ptr=%" UVuf ", cnt=%ld\n",PTR2UV(ptr),(long)cnt));
8832 DEBUG_P(PerlIO_printf(Perl_debug_log,
8833 "Screamer: entering: PerlIO * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%"
8835 PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
8836 PTR2UV(PerlIO_has_base(fp) ? PerlIO_get_base(fp) : 0)));
8840 /* if there is stuff left in the read-ahead buffer */
8842 /* if there is a separator */
8844 /* find next rslast */
8847 /* shortcut common case of blank line */
8849 if ((*bp++ = *ptr++) == rslast)
8850 goto thats_all_folks;
8852 p = (STDCHAR *)memchr(ptr, rslast, cnt);
8854 SSize_t got = p - ptr + 1;
8855 Copy(ptr, bp, got, STDCHAR);
8859 goto thats_all_folks;
8861 Copy(ptr, bp, cnt, STDCHAR);
8867 /* no separator, slurp the full buffer */
8868 Copy(ptr, bp, cnt, char); /* this | eat */
8869 bp += cnt; /* screams | dust */
8870 ptr += cnt; /* louder | sed :-) */
8872 assert (!shortbuffered);
8873 goto cannot_be_shortbuffered;
8877 if (shortbuffered) { /* oh well, must extend */
8878 /* we didnt have enough room to fit the line into the target buffer
8879 * so we must extend the target buffer and keep going */
8880 cnt = shortbuffered;
8882 bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */
8884 /* extned the target sv's buffer so it can hold the full read-ahead buffer */
8885 SvGROW(sv, SvLEN(sv) + append + cnt + 2);
8886 bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */
8890 cannot_be_shortbuffered:
8891 /* we need to refill the read-ahead buffer if possible */
8893 DEBUG_P(PerlIO_printf(Perl_debug_log,
8894 "Screamer: going to getc, ptr=%" UVuf ", cnt=%" IVdf "\n",
8895 PTR2UV(ptr),(IV)cnt));
8896 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* deregisterize cnt and ptr */
8898 DEBUG_Pv(PerlIO_printf(Perl_debug_log,
8899 "Screamer: pre: FILE * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%" UVuf "\n",
8900 PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
8901 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
8904 call PerlIO_getc() to let it prefill the lookahead buffer
8906 This used to call 'filbuf' in stdio form, but as that behaves like
8907 getc when cnt <= 0 we use PerlIO_getc here to avoid introducing
8908 another abstraction.
8910 Note we have to deal with the char in 'i' if we are not at EOF
8912 bpx = bp - (STDCHAR*)SvPVX_const(sv);
8913 /* signals might be called here, possibly modifying sv */
8914 i = PerlIO_getc(fp); /* get more characters */
8915 bp = (STDCHAR*)SvPVX_const(sv) + bpx;
8917 DEBUG_Pv(PerlIO_printf(Perl_debug_log,
8918 "Screamer: post: FILE * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%" UVuf "\n",
8919 PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
8920 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
8922 /* find out how much is left in the read-ahead buffer, and rextract its pointer */
8923 cnt = PerlIO_get_cnt(fp);
8924 ptr = (STDCHAR*)PerlIO_get_ptr(fp); /* reregisterize cnt and ptr */
8925 DEBUG_P(PerlIO_printf(Perl_debug_log,
8926 "Screamer: after getc, ptr=%" UVuf ", cnt=%" IVdf "\n",
8927 PTR2UV(ptr),(IV)cnt));
8929 if (i == EOF) /* all done for ever? */
8930 goto thats_really_all_folks;
8932 /* make sure we have enough space in the target sv */
8933 bpx = bp - (STDCHAR*)SvPVX_const(sv); /* box up before relocation */
8935 SvGROW(sv, bpx + cnt + 2);
8936 bp = (STDCHAR*)SvPVX_const(sv) + bpx; /* unbox after relocation */
8938 /* copy of the char we got from getc() */
8939 *bp++ = (STDCHAR)i; /* store character from PerlIO_getc */
8941 /* make sure we deal with the i being the last character of a separator */
8942 if (rslen && (STDCHAR)i == rslast) /* all done for now? */
8943 goto thats_all_folks;
8947 /* check if we have actually found the separator - only really applies
8949 if ((rslen > 1 && (STRLEN)(bp - (STDCHAR*)SvPVX_const(sv)) < rslen) ||
8950 memNE((char*)bp - rslen, rsptr, rslen))
8951 goto screamer; /* go back to the fray */
8952 thats_really_all_folks:
8954 cnt += shortbuffered;
8955 DEBUG_P(PerlIO_printf(Perl_debug_log,
8956 "Screamer: quitting, ptr=%" UVuf ", cnt=%" IVdf "\n",PTR2UV(ptr),(IV)cnt));
8957 PerlIO_set_ptrcnt(fp, (STDCHAR*)ptr, cnt); /* put these back or we're in trouble */
8958 DEBUG_P(PerlIO_printf(Perl_debug_log,
8959 "Screamer: end: FILE * thinks ptr=%" UVuf ", cnt=%" IVdf ", base=%" UVuf
8961 PTR2UV(PerlIO_get_ptr(fp)), (IV)PerlIO_get_cnt(fp),
8962 PTR2UV(PerlIO_has_base (fp) ? PerlIO_get_base(fp) : 0)));
8964 SvCUR_set(sv, bp - (STDCHAR*)SvPVX_const(sv)); /* set length */
8965 DEBUG_P(PerlIO_printf(Perl_debug_log,
8966 "Screamer: done, len=%ld, string=|%.*s|\n",
8967 (long)SvCUR(sv),(int)SvCUR(sv),SvPVX_const(sv)));
8971 /*The big, slow, and stupid way. */
8976 const STDCHAR * const bpe = buf + sizeof(buf);
8978 while ((i = PerlIO_getc(fp)) != EOF && (*bp++ = (STDCHAR)i) != rslast && bp < bpe)
8979 ; /* keep reading */
8983 cnt = PerlIO_read(fp,(char*)buf, sizeof(buf));
8984 /* Accommodate broken VAXC compiler, which applies U8 cast to
8985 * both args of ?: operator, causing EOF to change into 255
8988 i = (U8)buf[cnt - 1];
8994 cnt = 0; /* we do need to re-set the sv even when cnt <= 0 */
8996 sv_catpvn_nomg(sv, (char *) buf, cnt);
8998 sv_setpvn(sv, (char *) buf, cnt); /* "nomg" is implied */
9000 if (i != EOF && /* joy */
9002 SvCUR(sv) < rslen ||
9003 memNE(SvPVX_const(sv) + SvCUR(sv) - rslen, rsptr, rslen)))
9007 * If we're reading from a TTY and we get a short read,
9008 * indicating that the user hit his EOF character, we need
9009 * to notice it now, because if we try to read from the TTY
9010 * again, the EOF condition will disappear.
9012 * The comparison of cnt to sizeof(buf) is an optimization
9013 * that prevents unnecessary calls to feof().
9017 if (!(cnt < (I32)sizeof(buf) && PerlIO_eof(fp)))
9023 if (rspara) { /* have to do this both before and after */
9024 while (i != EOF) { /* to make sure file boundaries work right */
9025 i = PerlIO_getc(fp);
9027 PerlIO_ungetc(fp,i);
9033 return (SvCUR(sv) - append) ? SvPVX(sv) : NULL;
9038 =for apidoc_item sv_inc_nomg
9040 These auto-increment the value in the SV, doing string to numeric conversion
9041 if necessary. They both handle operator overloading.
9043 They differ only in that C<sv_inc> performs 'get' magic; C<sv_inc_nomg> skips
9050 Perl_sv_inc(pTHX_ SV *const sv)
9059 Perl_sv_inc_nomg(pTHX_ SV *const sv)
9066 if (SvTHINKFIRST(sv)) {
9067 if (SvREADONLY(sv)) {
9068 Perl_croak_no_modify();
9072 if (SvAMAGIC(sv) && AMG_CALLunary(sv, inc_amg))
9074 i = PTR2IV(SvRV(sv));
9078 else sv_force_normal_flags(sv, 0);
9080 flags = SvFLAGS(sv);
9081 if ((flags & (SVp_NOK|SVp_IOK)) == SVp_NOK) {
9082 /* It's (privately or publicly) a float, but not tested as an
9083 integer, so test it to see. */
9085 flags = SvFLAGS(sv);
9087 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
9088 /* It's publicly an integer, or privately an integer-not-float */
9089 #ifdef PERL_PRESERVE_IVUV
9093 if (SvUVX(sv) == UV_MAX)
9094 sv_setnv(sv, UV_MAX_P1);
9096 (void)SvIOK_only_UV(sv);
9097 SvUV_set(sv, SvUVX(sv) + 1);
9100 if (SvIVX(sv) == IV_MAX)
9101 sv_setuv(sv, (UV)IV_MAX + 1);
9103 (void)SvIOK_only(sv);
9104 SvIV_set(sv, SvIVX(sv) + 1);
9109 if (flags & SVp_NOK) {
9110 const NV was = SvNVX(sv);
9111 if (NV_OVERFLOWS_INTEGERS_AT != 0.0 &&
9112 /* If NVX was NaN, the following comparisons return always false */
9113 UNLIKELY(was >= NV_OVERFLOWS_INTEGERS_AT ||
9114 was < -NV_OVERFLOWS_INTEGERS_AT) &&
9115 #if defined(NAN_COMPARE_BROKEN)
9116 LIKELY(!Perl_isinfnan(was))
9118 LIKELY(!Perl_isinf(was))
9121 /* diag_listed_as: Lost precision when %s %f by 1 */
9122 Perl_ck_warner(aTHX_ packWARN(WARN_IMPRECISION),
9123 "Lost precision when incrementing %" NVff " by 1",
9126 (void)SvNOK_only(sv);
9127 SvNV_set(sv, was + 1.0);
9131 /* treat AV/HV/CV/FM/IO and non-fake GVs as immutable */
9132 if (SvTYPE(sv) >= SVt_PVAV || (isGV_with_GP(sv) && !SvFAKE(sv)))
9133 Perl_croak_no_modify();
9135 if (!(flags & SVp_POK) || !*SvPVX_const(sv)) {
9136 if ((flags & SVTYPEMASK) < SVt_PVIV)
9137 sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV ? SVt_PVIV : SVt_IV));
9138 (void)SvIOK_only(sv);
9143 while (isALPHA(*d)) d++;
9144 while (isDIGIT(*d)) d++;
9145 if (d < SvEND(sv)) {
9146 const int numtype = grok_number_flags(SvPVX_const(sv), SvCUR(sv), NULL, PERL_SCAN_TRAILING);
9147 #ifdef PERL_PRESERVE_IVUV
9148 /* Got to punt this as an integer if needs be, but we don't issue
9149 warnings. Probably ought to make the sv_iv_please() that does
9150 the conversion if possible, and silently. */
9151 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
9152 /* Need to try really hard to see if it's an integer.
9153 9.22337203685478e+18 is an integer.
9154 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
9155 so $a="9.22337203685478e+18"; $a+0; $a++
9156 needs to be the same as $a="9.22337203685478e+18"; $a++
9163 /* sv_2iv *should* have made this an NV */
9164 if (flags & SVp_NOK) {
9165 (void)SvNOK_only(sv);
9166 SvNV_set(sv, SvNVX(sv) + 1.0);
9169 /* I don't think we can get here. Maybe I should assert this
9170 And if we do get here I suspect that sv_setnv will croak. NWC
9172 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_inc punt failed to convert '%s' to IOK or NOKp, UV=0x%" UVxf " NV=%" NVgf "\n",
9173 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
9175 #endif /* PERL_PRESERVE_IVUV */
9176 if (!numtype && ckWARN(WARN_NUMERIC))
9177 not_incrementable(sv);
9178 sv_setnv(sv,Atof(SvPVX_const(sv)) + 1.0);
9182 while (d >= SvPVX_const(sv)) {
9190 /* MKS: The original code here died if letters weren't consecutive.
9191 * at least it didn't have to worry about non-C locales. The
9192 * new code assumes that ('z'-'a')==('Z'-'A'), letters are
9193 * arranged in order (although not consecutively) and that only
9194 * [A-Za-z] are accepted by isALPHA in the C locale.
9196 if (isALPHA_FOLD_NE(*d, 'z')) {
9197 do { ++*d; } while (!isALPHA(*d));
9200 *(d--) -= 'z' - 'a';
9205 *(d--) -= 'z' - 'a' + 1;
9209 /* oh,oh, the number grew */
9210 SvGROW(sv, SvCUR(sv) + 2);
9211 SvCUR_set(sv, SvCUR(sv) + 1);
9212 for (d = SvPVX(sv) + SvCUR(sv); d > SvPVX_const(sv); d--)
9222 =for apidoc_item sv_dec_nomg
9224 These auto-decrement the value in the SV, doing string to numeric conversion
9225 if necessary. They both handle operator overloading.
9227 They differ only in that:
9229 C<sv_dec> handles 'get' magic; C<sv_dec_nomg> skips 'get' magic.
9235 Perl_sv_dec(pTHX_ SV *const sv)
9244 Perl_sv_dec_nomg(pTHX_ SV *const sv)
9250 if (SvTHINKFIRST(sv)) {
9251 if (SvREADONLY(sv)) {
9252 Perl_croak_no_modify();
9256 if (SvAMAGIC(sv) && AMG_CALLunary(sv, dec_amg))
9258 i = PTR2IV(SvRV(sv));
9262 else sv_force_normal_flags(sv, 0);
9264 /* Unlike sv_inc we don't have to worry about string-never-numbers
9265 and keeping them magic. But we mustn't warn on punting */
9266 flags = SvFLAGS(sv);
9267 if ((flags & SVf_IOK) || ((flags & (SVp_IOK | SVp_NOK)) == SVp_IOK)) {
9268 /* It's publicly an integer, or privately an integer-not-float */
9269 #ifdef PERL_PRESERVE_IVUV
9273 if (SvUVX(sv) == 0) {
9274 (void)SvIOK_only(sv);
9278 (void)SvIOK_only_UV(sv);
9279 SvUV_set(sv, SvUVX(sv) - 1);
9282 if (SvIVX(sv) == IV_MIN) {
9283 sv_setnv(sv, (NV)IV_MIN);
9287 (void)SvIOK_only(sv);
9288 SvIV_set(sv, SvIVX(sv) - 1);
9293 if (flags & SVp_NOK) {
9296 const NV was = SvNVX(sv);
9297 if (NV_OVERFLOWS_INTEGERS_AT != 0.0 &&
9298 /* If NVX was NaN, these comparisons return always false */
9299 UNLIKELY(was <= -NV_OVERFLOWS_INTEGERS_AT ||
9300 was > NV_OVERFLOWS_INTEGERS_AT) &&
9301 #if defined(NAN_COMPARE_BROKEN)
9302 LIKELY(!Perl_isinfnan(was))
9304 LIKELY(!Perl_isinf(was))
9307 /* diag_listed_as: Lost precision when %s %f by 1 */
9308 Perl_ck_warner(aTHX_ packWARN(WARN_IMPRECISION),
9309 "Lost precision when decrementing %" NVff " by 1",
9312 (void)SvNOK_only(sv);
9313 SvNV_set(sv, was - 1.0);
9318 /* treat AV/HV/CV/FM/IO and non-fake GVs as immutable */
9319 if (SvTYPE(sv) >= SVt_PVAV || (isGV_with_GP(sv) && !SvFAKE(sv)))
9320 Perl_croak_no_modify();
9322 if (!(flags & SVp_POK)) {
9323 if ((flags & SVTYPEMASK) < SVt_PVIV)
9324 sv_upgrade(sv, ((flags & SVTYPEMASK) > SVt_IV) ? SVt_PVIV : SVt_IV);
9326 (void)SvIOK_only(sv);
9329 #ifdef PERL_PRESERVE_IVUV
9331 const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), NULL);
9332 if (numtype && !(numtype & IS_NUMBER_INFINITY)) {
9333 /* Need to try really hard to see if it's an integer.
9334 9.22337203685478e+18 is an integer.
9335 but "9.22337203685478e+18" + 0 is UV=9223372036854779904
9336 so $a="9.22337203685478e+18"; $a+0; $a--
9337 needs to be the same as $a="9.22337203685478e+18"; $a--
9344 /* sv_2iv *should* have made this an NV */
9345 if (flags & SVp_NOK) {
9346 (void)SvNOK_only(sv);
9347 SvNV_set(sv, SvNVX(sv) - 1.0);
9350 /* I don't think we can get here. Maybe I should assert this
9351 And if we do get here I suspect that sv_setnv will croak. NWC
9353 DEBUG_c(PerlIO_printf(Perl_debug_log,"sv_dec punt failed to convert '%s' to IOK or NOKp, UV=0x%" UVxf " NV=%" NVgf "\n",
9354 SvPVX_const(sv), SvIVX(sv), SvNVX(sv)));
9357 #endif /* PERL_PRESERVE_IVUV */
9358 sv_setnv(sv,Atof(SvPVX_const(sv)) - 1.0); /* punt */
9361 /* this define is used to eliminate a chunk of duplicated but shared logic
9362 * it has the suffix __SV_C to signal that it isnt API, and isnt meant to be
9363 * used anywhere but here - yves
9365 #define PUSH_EXTEND_MORTAL__SV_C(AnSv) \
9367 SSize_t ix = ++PL_tmps_ix; \
9368 if (UNLIKELY(ix >= PL_tmps_max)) \
9369 ix = tmps_grow_p(ix); \
9370 PL_tmps_stack[ix] = (AnSv); \
9374 =for apidoc sv_mortalcopy
9376 Creates a new SV which is a copy of the original SV (using C<sv_setsv>).
9377 The new SV is marked as mortal. It will be destroyed "soon", either by an
9378 explicit call to C<FREETMPS>, or by an implicit call at places such as
9379 statement boundaries. See also C<L</sv_newmortal>> and C<L</sv_2mortal>>.
9381 =for apidoc sv_mortalcopy_flags
9383 Like C<sv_mortalcopy>, but the extra C<flags> are passed to the
9389 /* Make a string that will exist for the duration of the expression
9390 * evaluation. Actually, it may have to last longer than that, but
9391 * hopefully we won't free it until it has been assigned to a
9392 * permanent location. */
9395 Perl_sv_mortalcopy_flags(pTHX_ SV *const oldstr, U32 flags)
9399 if (flags & SV_GMAGIC)
9400 SvGETMAGIC(oldstr); /* before new_SV, in case it dies */
9402 sv_setsv_flags(sv,oldstr,flags & ~SV_GMAGIC);
9403 PUSH_EXTEND_MORTAL__SV_C(sv);
9409 =for apidoc sv_newmortal
9411 Creates a new null SV which is mortal. The reference count of the SV is
9412 set to 1. It will be destroyed "soon", either by an explicit call to
9413 C<FREETMPS>, or by an implicit call at places such as statement boundaries.
9414 See also C<L</sv_mortalcopy>> and C<L</sv_2mortal>>.
9420 Perl_sv_newmortal(pTHX)
9425 SvFLAGS(sv) = SVs_TEMP;
9426 PUSH_EXTEND_MORTAL__SV_C(sv);
9432 =for apidoc newSVpvn_flags
9434 Creates a new SV and copies a string (which may contain C<NUL> (C<\0>)
9435 characters) into it. The reference count for the
9436 SV is set to 1. Note that if C<len> is zero, Perl will create a zero length
9437 string. You are responsible for ensuring that the source string is at least
9438 C<len> bytes long. If the C<s> argument is NULL the new SV will be undefined.
9439 Currently the only flag bits accepted are C<SVf_UTF8> and C<SVs_TEMP>.
9440 If C<SVs_TEMP> is set, then C<sv_2mortal()> is called on the result before
9441 returning. If C<SVf_UTF8> is set, C<s>
9442 is considered to be in UTF-8 and the
9443 C<SVf_UTF8> flag will be set on the new SV.
9444 C<newSVpvn_utf8()> is a convenience wrapper for this function, defined as
9446 #define newSVpvn_utf8(s, len, u) \
9447 newSVpvn_flags((s), (len), (u) ? SVf_UTF8 : 0)
9449 =for apidoc Amnh||SVs_TEMP
9455 Perl_newSVpvn_flags(pTHX_ const char *const s, const STRLEN len, const U32 flags)
9459 /* All the flags we don't support must be zero.
9460 And we're new code so I'm going to assert this from the start. */
9461 assert(!(flags & ~(SVf_UTF8|SVs_TEMP)));
9462 sv = newSV_type(SVt_PV);
9463 sv_setpvn_fresh(sv,s,len);
9465 /* This code used to do a sv_2mortal(), however we now unroll the call to
9466 * sv_2mortal() and do what it does ourselves here. Since we have asserted
9467 * that flags can only have the SVf_UTF8 and/or SVs_TEMP flags set above we
9468 * can use it to enable the sv flags directly (bypassing SvTEMP_on), which
9469 * in turn means we dont need to mask out the SVf_UTF8 flag below, which
9470 * means that we eliminate quite a few steps than it looks - Yves
9471 * (explaining patch by gfx) */
9473 SvFLAGS(sv) |= flags;
9475 if(flags & SVs_TEMP){
9476 PUSH_EXTEND_MORTAL__SV_C(sv);
9483 =for apidoc sv_2mortal
9485 Marks an existing SV as mortal. The SV will be destroyed "soon", either
9486 by an explicit call to C<FREETMPS>, or by an implicit call at places such as
9487 statement boundaries. C<SvTEMP()> is turned on which means that the SV's
9488 string buffer can be "stolen" if this SV is copied. See also
9489 C<L</sv_newmortal>> and C<L</sv_mortalcopy>>.
9495 Perl_sv_2mortal(pTHX_ SV *const sv)
9501 PUSH_EXTEND_MORTAL__SV_C(sv);
9509 Creates a new SV and copies a string (which may contain C<NUL> (C<\0>)
9510 characters) into it. The reference count for the
9511 SV is set to 1. If C<len> is zero, Perl will compute the length using
9512 C<strlen()>, (which means if you use this option, that C<s> can't have embedded
9513 C<NUL> characters and has to have a terminating C<NUL> byte).
9515 This function can cause reliability issues if you are likely to pass in
9516 empty strings that are not null terminated, because it will run
9517 strlen on the string and potentially run past valid memory.
9519 Using L</newSVpvn> is a safer alternative for non C<NUL> terminated strings.
9520 For string literals use L</newSVpvs> instead. This function will work fine for
9521 C<NUL> terminated strings, but if you want to avoid the if statement on whether
9522 to call C<strlen> use C<newSVpvn> instead (calling C<strlen> yourself).
9528 Perl_newSVpv(pTHX_ const char *const s, const STRLEN len)
9530 SV *sv = newSV_type(SVt_PV);
9531 sv_setpvn_fresh(sv, s, len || s == NULL ? len : strlen(s));
9536 =for apidoc newSVpvn
9538 Creates a new SV and copies a string into it, which may contain C<NUL> characters
9539 (C<\0>) and other binary data. The reference count for the SV is set to 1.
9540 Note that if C<len> is zero, Perl will create a zero length (Perl) string. You
9541 are responsible for ensuring that the source buffer is at least
9542 C<len> bytes long. If the C<buffer> argument is NULL the new SV will be
9549 Perl_newSVpvn(pTHX_ const char *const buffer, const STRLEN len)
9551 SV *sv = newSV_type(SVt_PV);
9552 sv_setpvn_fresh(sv,buffer,len);
9557 =for apidoc newSVhek_mortal
9559 Creates a new mortal SV from the hash key structure. It will generate
9560 scalars that point to the shared string table where possible. Returns
9561 a new (undefined) SV if C<hek> is NULL.
9563 This is more efficient than using sv_2mortal(newSVhek( ... ))
9569 Perl_newSVhek_mortal(pTHX_ const HEK *const hek)
9571 SV * const sv = newSVhek(hek);
9573 assert(!SvIMMORTAL(sv));
9575 PUSH_EXTEND_MORTAL__SV_C(sv);
9581 =for apidoc newSVhek
9583 Creates a new SV from the hash key structure. It will generate scalars that
9584 point to the shared string table where possible. Returns a new (undefined)
9585 SV if C<hek> is NULL.
9591 Perl_newSVhek(pTHX_ const HEK *const hek)
9600 if (HEK_LEN(hek) == HEf_SVKEY) {
9601 return newSVsv(*(SV**)HEK_KEY(hek));
9603 const int flags = HEK_FLAGS(hek);
9604 if (flags & HVhek_WASUTF8) {
9606 Andreas would like keys he put in as utf8 to come back as utf8
9608 STRLEN utf8_len = HEK_LEN(hek);
9609 SV * const sv = newSV_type(SVt_PV);
9610 char *as_utf8 = (char *)bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
9611 /* bytes_to_utf8() allocates a new string, which we can repurpose: */
9612 sv_usepvn_flags(sv, as_utf8, utf8_len, SV_HAS_TRAILING_NUL);
9615 } else if (flags & HVhek_NOTSHARED) {
9616 /* A hash that isn't using shared hash keys has to have
9617 the flag in every key so that we know not to try to call
9618 share_hek_hek on it. */
9620 SV * const sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
9625 /* This will be overwhelminly the most common case. */
9627 /* Inline most of newSVpvn_share(), because share_hek_hek() is far
9628 more efficient than sharepvn(). */
9629 SV *sv = newSV_type(SVt_PV);
9631 SvPV_set(sv, (char *)HEK_KEY(share_hek_hek(hek)));
9632 SvCUR_set(sv, HEK_LEN(hek));
9644 =for apidoc newSVpvn_share
9646 Creates a new SV with its C<SvPVX_const> pointing to a shared string in the string
9647 table. If the string does not already exist in the table, it is
9648 created first. Turns on the C<SvIsCOW> flag (or C<READONLY>
9649 and C<FAKE> in 5.16 and earlier). If the C<hash> parameter
9650 is non-zero, that value is used; otherwise the hash is computed.
9651 The string's hash can later be retrieved from the SV
9652 with the C<L</SvSHARED_HASH>> macro. The idea here is
9653 that as the string table is used for shared hash keys these strings will have
9654 C<SvPVX_const == HeKEY> and hash lookup will avoid string compare.
9660 Perl_newSVpvn_share(pTHX_ const char *src, I32 len, U32 hash)
9663 bool is_utf8 = FALSE;
9664 const char *const orig_src = src;
9667 STRLEN tmplen = -len;
9669 /* See the note in hv.c:hv_fetch() --jhi */
9670 src = (char*)bytes_from_utf8((const U8*)src, &tmplen, &is_utf8);
9674 PERL_HASH(hash, src, len);
9675 sv = newSV_type(SVt_PV);
9676 /* The logic for this is inlined in S_mro_get_linear_isa_dfs(), so if it
9677 changes here, update it there too. */
9678 SvPV_set(sv, sharepvn(src, is_utf8?-len:len, hash));
9685 if (src != orig_src)
9691 =for apidoc newSVpv_share
9693 Like C<newSVpvn_share>, but takes a C<NUL>-terminated string instead of a
9700 Perl_newSVpv_share(pTHX_ const char *src, U32 hash)
9702 return newSVpvn_share(src, strlen(src), hash);
9705 #if defined(MULTIPLICITY)
9707 /* pTHX_ magic can't cope with varargs, so this is a no-context
9708 * version of the main function, (which may itself be aliased to us).
9709 * Don't access this version directly.
9713 Perl_newSVpvf_nocontext(const char *const pat, ...)
9719 PERL_ARGS_ASSERT_NEWSVPVF_NOCONTEXT;
9721 va_start(args, pat);
9722 sv = vnewSVpvf(pat, &args);
9729 =for apidoc newSVpvf
9731 Creates a new SV and initializes it with the string formatted like
9734 =for apidoc newSVpvf_nocontext
9735 Like C<L</newSVpvf>> but does not take a thread context (C<aTHX>) parameter,
9736 so is used in situations where the caller doesn't already have the thread
9739 =for apidoc vnewSVpvf
9740 Like C<L</newSVpvf>> but the arguments are an encapsulated argument list.
9746 Perl_newSVpvf(pTHX_ const char *const pat, ...)
9751 PERL_ARGS_ASSERT_NEWSVPVF;
9753 va_start(args, pat);
9754 sv = vnewSVpvf(pat, &args);
9759 /* backend for newSVpvf() and newSVpvf_nocontext() */
9762 Perl_vnewSVpvf(pTHX_ const char *const pat, va_list *const args)
9766 PERL_ARGS_ASSERT_VNEWSVPVF;
9769 sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
9776 Creates a new SV and copies a floating point value into it.
9777 The reference count for the SV is set to 1.
9783 Perl_newSVnv(pTHX_ const NV n)
9785 SV *sv = newSV_type(SVt_NV);
9797 Creates a new SV and copies an integer into it. The reference count for the
9804 Perl_newSViv(pTHX_ const IV i)
9806 SV *sv = newSV_type(SVt_IV);
9818 Creates a new SV and copies an unsigned integer into it.
9819 The reference count for the SV is set to 1.
9825 Perl_newSVuv(pTHX_ const UV u)
9829 /* Inlining ONLY the small relevant subset of sv_setuv here
9830 * for performance. Makes a significant difference. */
9832 /* Using ivs is more efficient than using uvs - see sv_setuv */
9833 if (u <= (UV)IV_MAX) {
9834 return newSViv((IV)u);
9839 /* We're starting from SVt_FIRST, so provided that's
9840 * actual 0, we don't have to unset any SV type flags
9841 * to promote to SVt_IV. */
9842 STATIC_ASSERT_STMT(SVt_FIRST == 0);
9844 SET_SVANY_FOR_BODYLESS_IV(sv);
9845 SvFLAGS(sv) |= SVt_IV;
9847 (void)SvIsUV_on(sv);
9856 =for apidoc newSVbool
9858 Creates a new SV boolean.
9864 Perl_newSVbool(pTHX_ bool bool_val)
9866 PERL_ARGS_ASSERT_NEWSVBOOL;
9867 SV *sv = newSVsv(bool_val ? &PL_sv_yes : &PL_sv_no);
9873 =for apidoc newSV_true
9875 Creates a new SV that is a boolean true.
9880 Perl_newSV_true(pTHX)
9882 PERL_ARGS_ASSERT_NEWSV_TRUE;
9883 SV *sv = newSVsv(&PL_sv_yes);
9889 =for apidoc newSV_false
9891 Creates a new SV that is a boolean false.
9897 Perl_newSV_false(pTHX)
9899 PERL_ARGS_ASSERT_NEWSV_FALSE;
9900 SV *sv = newSVsv(&PL_sv_no);
9905 /* newRV_inc is the official function name to use now.
9906 * newRV_inc is in fact #defined to newRV in sv.h
9910 Perl_newRV(pTHX_ SV *const sv)
9912 PERL_ARGS_ASSERT_NEWRV;
9914 return newRV_noinc(SvREFCNT_inc_simple_NN(sv));
9919 =for apidoc_item newSVsv_flags
9920 =for apidoc_item newSVsv_nomg
9922 These create a new SV which is an exact duplicate of the original SV
9923 (using C<sv_setsv>.)
9925 They differ only in that C<newSVsv> performs 'get' magic; C<newSVsv_nomg> skips
9926 any magic; and C<newSVsv_flags> allows you to explicitly set a C<flags>
9933 Perl_newSVsv_flags(pTHX_ SV *const old, I32 flags)
9939 if (SvTYPE(old) == (svtype)SVTYPEMASK) {
9940 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), "semi-panic: attempt to dup freed string");
9943 /* Do this here, otherwise we leak the new SV if this croaks. */
9944 if (flags & SV_GMAGIC)
9947 sv_setsv_flags(sv, old, flags & ~SV_GMAGIC);
9952 =for apidoc sv_reset
9954 Underlying implementation for the C<reset> Perl function.
9955 Note that the perl-level function is vaguely deprecated.
9961 Perl_sv_reset(pTHX_ const char *s, HV *const stash)
9963 PERL_ARGS_ASSERT_SV_RESET;
9965 sv_resetpvn(*s ? s : NULL, strlen(s), stash);
9969 Perl_sv_resetpvn(pTHX_ const char *s, STRLEN len, HV * const stash)
9971 char todo[PERL_UCHAR_MAX+1];
9974 if (!stash || SvTYPE(stash) != SVt_PVHV)
9977 if (!s) { /* reset ?? searches */
9978 MAGIC * const mg = mg_find((const SV *)stash, PERL_MAGIC_symtab);
9979 if (mg && mg->mg_len) {
9980 const U32 count = mg->mg_len / sizeof(PMOP**);
9981 PMOP **pmp = (PMOP**) mg->mg_ptr;
9982 PMOP *const *const end = pmp + count;
9986 SvREADONLY_off(PL_regex_pad[(*pmp)->op_pmoffset]);
9988 (*pmp)->op_pmflags &= ~PMf_USED;
9996 /* reset variables */
9998 if (!HvTOTALKEYS(stash))
10001 Zero(todo, 256, char);
10005 I32 i = (unsigned char)*s;
10009 max = (unsigned char)*s++;
10010 for ( ; i <= max; i++) {
10013 for (i = 0; i <= (I32) HvMAX(stash); i++) {
10015 for (entry = HvARRAY(stash)[i];
10017 entry = HeNEXT(entry))
10022 if (!todo[(U8)*HeKEY(entry)])
10024 gv = MUTABLE_GV(HeVAL(entry));
10028 if (sv && !SvREADONLY(sv)) {
10029 SV_CHECK_THINKFIRST_COW_DROP(sv);
10030 if (!isGV(sv)) SvOK_off(sv);
10033 av_clear(GvAV(gv));
10035 if (GvHV(gv) && !HvNAME_get(GvHV(gv))) {
10036 hv_clear(GvHV(gv));
10046 Using various gambits, try to get an IO from an SV: the IO slot if its a
10047 GV; or the recursive result if we're an RV; or the IO slot of the symbol
10048 named after the PV if we're a string.
10050 'Get' magic is ignored on the C<sv> passed in, but will be called on
10051 C<SvRV(sv)> if C<sv> is an RV.
10057 Perl_sv_2io(pTHX_ SV *const sv)
10062 PERL_ARGS_ASSERT_SV_2IO;
10064 switch (SvTYPE(sv)) {
10066 io = MUTABLE_IO(sv);
10070 if (isGV_with_GP(sv)) {
10071 gv = MUTABLE_GV(sv);
10074 Perl_croak(aTHX_ "Bad filehandle: %" HEKf,
10075 HEKfARG(GvNAME_HEK(gv)));
10081 Perl_croak(aTHX_ PL_no_usym, "filehandle");
10083 SvGETMAGIC(SvRV(sv));
10084 return sv_2io(SvRV(sv));
10086 gv = gv_fetchsv_nomg(sv, 0, SVt_PVIO);
10093 if (SvGMAGICAL(sv)) {
10094 newsv = sv_newmortal();
10095 sv_setsv_nomg(newsv, sv);
10097 Perl_croak(aTHX_ "Bad filehandle: %" SVf, SVfARG(newsv));
10107 Using various gambits, try to get a CV from an SV; in addition, try if
10108 possible to set C<*st> and C<*gvp> to the stash and GV associated with it.
10109 The flags in C<lref> are passed to C<gv_fetchsv>.
10115 Perl_sv_2cv(pTHX_ SV *sv, HV **const st, GV **const gvp, const I32 lref)
10120 PERL_ARGS_ASSERT_SV_2CV;
10127 switch (SvTYPE(sv)) {
10131 return MUTABLE_CV(sv);
10141 sv = amagic_deref_call(sv, to_cv_amg);
10144 if (SvTYPE(sv) == SVt_PVCV) {
10145 cv = MUTABLE_CV(sv);
10150 else if(SvGETMAGIC(sv), isGV_with_GP(sv))
10151 gv = MUTABLE_GV(sv);
10153 Perl_croak(aTHX_ "Not a subroutine reference");
10155 else if (isGV_with_GP(sv)) {
10156 gv = MUTABLE_GV(sv);
10159 gv = gv_fetchsv_nomg(sv, lref, SVt_PVCV);
10166 /* Some flags to gv_fetchsv mean don't really create the GV */
10167 if (!isGV_with_GP(gv)) {
10171 *st = GvESTASH(gv);
10172 if (lref & ~GV_ADDMG && !GvCVu(gv)) {
10173 /* XXX this is probably not what they think they're getting.
10174 * It has the same effect as "sub name;", i.e. just a forward
10183 =for apidoc sv_true
10185 Returns true if the SV has a true value by Perl's rules.
10186 Use the C<SvTRUE> macro instead, which may call C<sv_true()> or may
10187 instead use an in-line version.
10193 Perl_sv_true(pTHX_ SV *const sv)
10198 const XPV* const tXpv = (XPV*)SvANY(sv);
10200 (tXpv->xpv_cur > 1 ||
10201 (tXpv->xpv_cur && *sv->sv_u.svu_pv != '0')))
10208 return SvIVX(sv) != 0;
10211 return SvNVX(sv) != 0.0;
10213 return sv_2bool(sv);
10219 =for apidoc sv_pvn_force
10221 Get a sensible string out of the SV somehow.
10222 A private implementation of the C<SvPV_force> macro for compilers which
10223 can't cope with complex macro expressions. Always use the macro instead.
10225 =for apidoc sv_pvn_force_flags
10227 Get a sensible string out of the SV somehow.
10228 If C<flags> has the C<SV_GMAGIC> bit set, will C<L</mg_get>> on C<sv> if
10229 appropriate, else not. C<sv_pvn_force> and C<sv_pvn_force_nomg> are
10230 implemented in terms of this function.
10231 You normally want to use the various wrapper macros instead: see
10232 C<L</SvPV_force>> and C<L</SvPV_force_nomg>>.
10238 Perl_sv_pvn_force_flags(pTHX_ SV *const sv, STRLEN *const lp, const U32 flags)
10240 PERL_ARGS_ASSERT_SV_PVN_FORCE_FLAGS;
10242 if (flags & SV_GMAGIC) SvGETMAGIC(sv);
10243 if (SvTHINKFIRST(sv) && (!SvROK(sv) || SvREADONLY(sv)))
10244 sv_force_normal_flags(sv, 0);
10254 if (SvTYPE(sv) > SVt_PVLV
10255 || isGV_with_GP(sv))
10256 /* diag_listed_as: Can't coerce %s to %s in %s */
10257 Perl_croak(aTHX_ "Can't coerce %s to string in %s", sv_reftype(sv,0),
10259 s = sv_2pv_flags(sv, &len, flags &~ SV_GMAGIC);
10266 if (SvTYPE(sv) < SVt_PV ||
10267 s != SvPVX_const(sv)) { /* Almost, but not quite, sv_setpvn() */
10270 SvUPGRADE(sv, SVt_PV); /* Never FALSE */
10271 SvGROW(sv, len + 1);
10272 Move(s,SvPVX(sv),len,char);
10273 SvCUR_set(sv, len);
10274 SvPVX(sv)[len] = '\0';
10277 SvPOK_on(sv); /* validate pointer */
10279 DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%" UVxf " 2pv(%s)\n",
10280 PTR2UV(sv),SvPVX_const(sv)));
10283 (void)SvPOK_only_UTF8(sv);
10284 return SvPVX_mutable(sv);
10288 =for apidoc sv_pvbyten_force
10290 The backend for the C<SvPVbytex_force> macro. Always use the macro
10291 instead. If the SV cannot be downgraded from UTF-8, this croaks.
10297 Perl_sv_pvbyten_force(pTHX_ SV *const sv, STRLEN *const lp)
10299 PERL_ARGS_ASSERT_SV_PVBYTEN_FORCE;
10301 sv_pvn_force(sv,lp);
10302 (void)sv_utf8_downgrade(sv,0);
10308 =for apidoc sv_pvutf8n_force
10310 The backend for the C<SvPVutf8x_force> macro. Always use the macro
10317 Perl_sv_pvutf8n_force(pTHX_ SV *const sv, STRLEN *const lp)
10319 PERL_ARGS_ASSERT_SV_PVUTF8N_FORCE;
10321 sv_pvn_force(sv,0);
10322 sv_utf8_upgrade_nomg(sv);
10328 =for apidoc sv_reftype
10330 Returns a string describing what the SV is a reference to.
10332 If ob is true and the SV is blessed, the string is the class name,
10333 otherwise it is the type of the SV, "SCALAR", "ARRAY" etc.
10339 Perl_sv_reftype(pTHX_ const SV *const sv, const int ob)
10341 PERL_ARGS_ASSERT_SV_REFTYPE;
10342 if (ob && SvOBJECT(sv)) {
10343 return SvPV_nolen_const(sv_ref(NULL, sv, ob));
10346 /* WARNING - There is code, for instance in mg.c, that assumes that
10347 * the only reason that sv_reftype(sv,0) would return a string starting
10348 * with 'L' or 'S' is that it is a LVALUE or a SCALAR.
10349 * Yes this a dodgy way to do type checking, but it saves practically reimplementing
10350 * this routine inside other subs, and it saves time.
10351 * Do not change this assumption without searching for "dodgy type check" in
10354 switch (SvTYPE(sv)) {
10369 case SVt_PVLV: return (char *) (SvROK(sv) ? "REF"
10370 /* tied lvalues should appear to be
10371 * scalars for backwards compatibility */
10372 : (isALPHA_FOLD_EQ(LvTYPE(sv), 't'))
10373 ? "SCALAR" : "LVALUE");
10374 case SVt_PVAV: return "ARRAY";
10375 case SVt_PVHV: return "HASH";
10376 case SVt_PVCV: return "CODE";
10377 case SVt_PVGV: return (char *) (isGV_with_GP(sv)
10378 ? "GLOB" : "SCALAR");
10379 case SVt_PVFM: return "FORMAT";
10380 case SVt_PVIO: return "IO";
10381 case SVt_INVLIST: return "INVLIST";
10382 case SVt_REGEXP: return "REGEXP";
10383 default: return "UNKNOWN";
10391 Returns a SV describing what the SV passed in is a reference to.
10393 dst can be a SV to be set to the description or NULL, in which case a
10394 mortal SV is returned.
10396 If ob is true and the SV is blessed, the description is the class
10397 name, otherwise it is the type of the SV, "SCALAR", "ARRAY" etc.
10403 Perl_sv_ref(pTHX_ SV *dst, const SV *const sv, const int ob)
10405 PERL_ARGS_ASSERT_SV_REF;
10408 dst = sv_newmortal();
10410 if (ob && SvOBJECT(sv)) {
10411 HvNAME_get(SvSTASH(sv))
10412 ? sv_sethek(dst, HvNAME_HEK(SvSTASH(sv)))
10413 : sv_setpvs(dst, "__ANON__");
10416 const char * reftype = sv_reftype(sv, 0);
10417 sv_setpv(dst, reftype);
10423 =for apidoc sv_isobject
10425 Returns a boolean indicating whether the SV is an RV pointing to a blessed
10426 object. If the SV is not an RV, or if the object is not blessed, then this
10433 Perl_sv_isobject(pTHX_ SV *sv)
10449 Returns a boolean indicating whether the SV is blessed into the specified
10452 This does not check for subtypes or method overloading. Use C<sv_isa_sv> to
10453 verify an inheritance relationship in the same way as the C<isa> operator by
10454 respecting any C<isa()> method overloading; or C<sv_derived_from_sv> to test
10455 directly on the actual object type.
10461 Perl_sv_isa(pTHX_ SV *sv, const char *const name)
10463 const char *hvname;
10465 PERL_ARGS_ASSERT_SV_ISA;
10475 hvname = HvNAME_get(SvSTASH(sv));
10479 return strEQ(hvname, name);
10483 =for apidoc newSVrv
10485 Creates a new SV for the existing RV, C<rv>, to point to. If C<rv> is not an
10486 RV then it will be upgraded to one. If C<classname> is non-null then the new
10487 SV will be blessed in the specified package. The new SV is returned and its
10488 reference count is 1. The reference count 1 is owned by C<rv>. See also
10489 newRV_inc() and newRV_noinc() for creating a new RV properly.
10495 Perl_newSVrv(pTHX_ SV *const rv, const char *const classname)
10499 PERL_ARGS_ASSERT_NEWSVRV;
10503 SV_CHECK_THINKFIRST_COW_DROP(rv);
10505 if (UNLIKELY( SvTYPE(rv) >= SVt_PVMG )) {
10506 const U32 refcnt = SvREFCNT(rv);
10510 SvREFCNT(rv) = refcnt;
10512 sv_upgrade(rv, SVt_IV);
10513 } else if (SvROK(rv)) {
10514 SvREFCNT_dec(SvRV(rv));
10516 prepare_SV_for_RV(rv);
10524 HV* const stash = gv_stashpv(classname, GV_ADD);
10525 (void)sv_bless(rv, stash);
10531 Perl_newSVavdefelem(pTHX_ AV *av, SSize_t ix, bool extendible)
10533 SV * const lv = newSV_type(SVt_PVLV);
10534 PERL_ARGS_ASSERT_NEWSVAVDEFELEM;
10536 sv_magic(lv, NULL, PERL_MAGIC_defelem, NULL, 0);
10537 LvTARG(lv) = SvREFCNT_inc_simple_NN(av);
10538 LvSTARGOFF(lv) = ix;
10539 LvTARGLEN(lv) = extendible ? 1 : (STRLEN)UV_MAX;
10544 =for apidoc sv_setref_pv
10546 Copies a pointer into a new SV, optionally blessing the SV. The C<rv>
10547 argument will be upgraded to an RV. That RV will be modified to point to
10548 the new SV. If the C<pv> argument is C<NULL>, then C<PL_sv_undef> will be placed
10549 into the SV. The C<classname> argument indicates the package for the
10550 blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
10551 will have a reference count of 1, and the RV will be returned.
10553 Do not use with other Perl types such as HV, AV, SV, CV, because those
10554 objects will become corrupted by the pointer copy process.
10556 Note that C<sv_setref_pvn> copies the string while this copies the pointer.
10562 Perl_sv_setref_pv(pTHX_ SV *const rv, const char *const classname, void *const pv)
10564 PERL_ARGS_ASSERT_SV_SETREF_PV;
10571 sv_setiv(newSVrv(rv,classname), PTR2IV(pv));
10576 =for apidoc sv_setref_iv
10578 Copies an integer into a new SV, optionally blessing the SV. The C<rv>
10579 argument will be upgraded to an RV. That RV will be modified to point to
10580 the new SV. The C<classname> argument indicates the package for the
10581 blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
10582 will have a reference count of 1, and the RV will be returned.
10588 Perl_sv_setref_iv(pTHX_ SV *const rv, const char *const classname, const IV iv)
10590 PERL_ARGS_ASSERT_SV_SETREF_IV;
10592 sv_setiv(newSVrv(rv,classname), iv);
10597 =for apidoc sv_setref_uv
10599 Copies an unsigned integer into a new SV, optionally blessing the SV. The C<rv>
10600 argument will be upgraded to an RV. That RV will be modified to point to
10601 the new SV. The C<classname> argument indicates the package for the
10602 blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
10603 will have a reference count of 1, and the RV will be returned.
10609 Perl_sv_setref_uv(pTHX_ SV *const rv, const char *const classname, const UV uv)
10611 PERL_ARGS_ASSERT_SV_SETREF_UV;
10613 sv_setuv(newSVrv(rv,classname), uv);
10618 =for apidoc sv_setref_nv
10620 Copies a double into a new SV, optionally blessing the SV. The C<rv>
10621 argument will be upgraded to an RV. That RV will be modified to point to
10622 the new SV. The C<classname> argument indicates the package for the
10623 blessing. Set C<classname> to C<NULL> to avoid the blessing. The new SV
10624 will have a reference count of 1, and the RV will be returned.
10630 Perl_sv_setref_nv(pTHX_ SV *const rv, const char *const classname, const NV nv)
10632 PERL_ARGS_ASSERT_SV_SETREF_NV;
10634 sv_setnv(newSVrv(rv,classname), nv);
10639 =for apidoc sv_setref_pvn
10641 Copies a string into a new SV, optionally blessing the SV. The length of the
10642 string must be specified with C<n>. The C<rv> argument will be upgraded to
10643 an RV. That RV will be modified to point to the new SV. The C<classname>
10644 argument indicates the package for the blessing. Set C<classname> to
10645 C<NULL> to avoid the blessing. The new SV will have a reference count
10646 of 1, and the RV will be returned.
10648 Note that C<sv_setref_pv> copies the pointer while this copies the string.
10654 Perl_sv_setref_pvn(pTHX_ SV *const rv, const char *const classname,
10655 const char *const pv, const STRLEN n)
10657 PERL_ARGS_ASSERT_SV_SETREF_PVN;
10659 sv_setpvn(newSVrv(rv,classname), pv, n);
10664 =for apidoc sv_bless
10666 Blesses an SV into a specified package. The SV must be an RV. The package
10667 must be designated by its stash (see C<L</gv_stashpv>>). The reference count
10668 of the SV is unaffected.
10674 Perl_sv_bless(pTHX_ SV *const sv, HV *const stash)
10677 HV *oldstash = NULL;
10679 PERL_ARGS_ASSERT_SV_BLESS;
10683 Perl_croak(aTHX_ "Can't bless non-reference value");
10685 if (SvFLAGS(tmpRef) & (SVs_OBJECT|SVf_READONLY|SVf_PROTECT)) {
10686 if (SvREADONLY(tmpRef))
10687 Perl_croak_no_modify();
10688 if (SvOBJECT(tmpRef)) {
10689 oldstash = SvSTASH(tmpRef);
10692 SvOBJECT_on(tmpRef);
10693 SvUPGRADE(tmpRef, SVt_PVMG);
10694 SvSTASH_set(tmpRef, MUTABLE_HV(SvREFCNT_inc_simple(stash)));
10695 SvREFCNT_dec(oldstash);
10697 if(SvSMAGICAL(tmpRef))
10698 if(mg_find(tmpRef, PERL_MAGIC_ext) || mg_find(tmpRef, PERL_MAGIC_uvar))
10706 /* Downgrades a PVGV to a PVMG. If it's actually a PVLV, we leave the type
10707 * as it is after unglobbing it.
10710 PERL_STATIC_INLINE void
10711 S_sv_unglob(pTHX_ SV *const sv, U32 flags)
10715 SV * const temp = flags & SV_COW_DROP_PV ? NULL : sv_newmortal();
10717 PERL_ARGS_ASSERT_SV_UNGLOB;
10719 assert(SvTYPE(sv) == SVt_PVGV || SvTYPE(sv) == SVt_PVLV);
10721 if (!(flags & SV_COW_DROP_PV))
10722 gv_efullname3(temp, MUTABLE_GV(sv), "*");
10724 SvREFCNT_inc_simple_void_NN(sv_2mortal(sv));
10726 if(GvCVu((const GV *)sv) && (stash = GvSTASH(MUTABLE_GV(sv)))
10727 && HvNAME_get(stash))
10728 mro_method_changed_in(stash);
10729 gp_free(MUTABLE_GV(sv));
10732 sv_del_backref(MUTABLE_SV(GvSTASH(sv)), sv);
10733 GvSTASH(sv) = NULL;
10736 if (GvNAME_HEK(sv)) {
10737 unshare_hek(GvNAME_HEK(sv));
10739 isGV_with_GP_off(sv);
10741 if(SvTYPE(sv) == SVt_PVGV) {
10742 /* need to keep SvANY(sv) in the right arena */
10743 xpvmg = new_XPVMG();
10744 StructCopy(SvANY(sv), xpvmg, XPVMG);
10745 del_body_by_type(SvANY(sv), SVt_PVGV);
10748 SvFLAGS(sv) &= ~SVTYPEMASK;
10749 SvFLAGS(sv) |= SVt_PVMG;
10752 /* Intentionally not calling any local SET magic, as this isn't so much a
10753 set operation as merely an internal storage change. */
10754 if (flags & SV_COW_DROP_PV) SvOK_off(sv);
10755 else sv_setsv_flags(sv, temp, 0);
10757 if ((const GV *)sv == PL_last_in_gv)
10758 PL_last_in_gv = NULL;
10759 else if ((const GV *)sv == PL_statgv)
10764 =for apidoc sv_unref_flags
10766 Unsets the RV status of the SV, and decrements the reference count of
10767 whatever was being referenced by the RV. This can almost be thought of
10768 as a reversal of C<newSVrv>. The C<cflags> argument can contain
10769 C<SV_IMMEDIATE_UNREF> to force the reference count to be decremented
10770 (otherwise the decrementing is conditional on the reference count being
10771 different from one or the reference being a readonly SV).
10772 See C<L</SvROK_off>>.
10774 =for apidoc Amnh||SV_IMMEDIATE_UNREF
10780 Perl_sv_unref_flags(pTHX_ SV *const ref, const U32 flags)
10782 SV* const target = SvRV(ref);
10784 PERL_ARGS_ASSERT_SV_UNREF_FLAGS;
10786 if (SvWEAKREF(ref)) {
10787 sv_del_backref(target, ref);
10788 SvWEAKREF_off(ref);
10789 SvRV_set(ref, NULL);
10792 SvRV_set(ref, NULL);
10794 /* You can't have a || SvREADONLY(target) here, as $a = $$a, where $a was
10795 assigned to as BEGIN {$a = \"Foo"} will fail. */
10796 if (SvREFCNT(target) != 1 || (flags & SV_IMMEDIATE_UNREF))
10797 SvREFCNT_dec_NN(target);
10798 else /* XXX Hack, but hard to make $a=$a->[1] work otherwise */
10799 sv_2mortal(target); /* Schedule for freeing later */
10803 =for apidoc sv_untaint
10805 Untaint an SV. Use C<SvTAINTED_off> instead.
10811 Perl_sv_untaint(pTHX_ SV *const sv)
10813 PERL_ARGS_ASSERT_SV_UNTAINT;
10814 PERL_UNUSED_CONTEXT;
10816 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
10817 MAGIC * const mg = mg_find(sv, PERL_MAGIC_taint);
10824 =for apidoc sv_tainted
10826 Test an SV for taintedness. Use C<SvTAINTED> instead.
10832 Perl_sv_tainted(pTHX_ SV *const sv)
10834 PERL_ARGS_ASSERT_SV_TAINTED;
10835 PERL_UNUSED_CONTEXT;
10837 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
10838 const MAGIC * const mg = mg_find(sv, PERL_MAGIC_taint);
10839 if (mg && (mg->mg_len & 1) )
10845 #if defined(MULTIPLICITY)
10847 /* pTHX_ magic can't cope with varargs, so this is a no-context
10848 * version of the main function, (which may itself be aliased to us).
10849 * Don't access this version directly.
10853 Perl_sv_setpvf_nocontext(SV *const sv, const char *const pat, ...)
10858 PERL_ARGS_ASSERT_SV_SETPVF_NOCONTEXT;
10860 va_start(args, pat);
10861 sv_vsetpvf(sv, pat, &args);
10865 /* pTHX_ magic can't cope with varargs, so this is a no-context
10866 * version of the main function, (which may itself be aliased to us).
10867 * Don't access this version directly.
10871 Perl_sv_setpvf_mg_nocontext(SV *const sv, const char *const pat, ...)
10876 PERL_ARGS_ASSERT_SV_SETPVF_MG_NOCONTEXT;
10878 va_start(args, pat);
10879 sv_vsetpvf_mg(sv, pat, &args);
10885 =for apidoc sv_setpvf
10886 =for apidoc_item sv_setpvf_mg
10887 =for apidoc_item sv_setpvf_mg_nocontext
10888 =for apidoc_item sv_setpvf_nocontext
10890 These work like C<L</sv_catpvf>> but copy the text into the SV instead of
10893 The differences between these are:
10895 C<sv_setpvf_mg> and C<sv_setpvf_mg_nocontext> perform 'set' magic; C<sv_setpvf>
10896 and C<sv_setpvf_nocontext> skip all magic.
10898 C<sv_setpvf_nocontext> and C<sv_setpvf_mg_nocontext> do not take a thread
10899 context (C<aTHX>) parameter, so are used in situations where the caller
10900 doesn't already have the thread context.
10906 Perl_sv_setpvf(pTHX_ SV *const sv, const char *const pat, ...)
10910 PERL_ARGS_ASSERT_SV_SETPVF;
10912 va_start(args, pat);
10913 sv_vsetpvf(sv, pat, &args);
10918 =for apidoc sv_vsetpvf
10919 =for apidoc_item sv_vsetpvf_mg
10921 These work like C<L</sv_vcatpvf>> but copy the text into the SV instead of
10924 They differ only in that C<sv_vsetpvf_mg> performs 'set' magic;
10925 C<sv_vsetpvf> skips all magic.
10927 They are usually used via their frontends, C<L</sv_setpvf>> and
10928 C<L</sv_setpvf_mg>>.
10934 Perl_sv_vsetpvf(pTHX_ SV *const sv, const char *const pat, va_list *const args)
10936 PERL_ARGS_ASSERT_SV_VSETPVF;
10938 sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
10942 Perl_sv_setpvf_mg(pTHX_ SV *const sv, const char *const pat, ...)
10946 PERL_ARGS_ASSERT_SV_SETPVF_MG;
10948 va_start(args, pat);
10949 sv_vsetpvf_mg(sv, pat, &args);
10954 Perl_sv_vsetpvf_mg(pTHX_ SV *const sv, const char *const pat, va_list *const args)
10956 PERL_ARGS_ASSERT_SV_VSETPVF_MG;
10958 sv_vsetpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
10962 #if defined(MULTIPLICITY)
10964 /* pTHX_ magic can't cope with varargs, so this is a no-context
10965 * version of the main function, (which may itself be aliased to us).
10966 * Don't access this version directly.
10970 Perl_sv_catpvf_nocontext(SV *const sv, const char *const pat, ...)
10975 PERL_ARGS_ASSERT_SV_CATPVF_NOCONTEXT;
10977 va_start(args, pat);
10978 sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
10982 /* pTHX_ magic can't cope with varargs, so this is a no-context
10983 * version of the main function, (which may itself be aliased to us).
10984 * Don't access this version directly.
10988 Perl_sv_catpvf_mg_nocontext(SV *const sv, const char *const pat, ...)
10993 PERL_ARGS_ASSERT_SV_CATPVF_MG_NOCONTEXT;
10995 va_start(args, pat);
10996 sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11003 =for apidoc sv_catpvf
11004 =for apidoc_item sv_catpvf_mg
11005 =for apidoc_item sv_catpvf_mg_nocontext
11006 =for apidoc_item sv_catpvf_nocontext
11008 These process their arguments like C<sprintf>, and append the formatted
11009 output to an SV. As with C<sv_vcatpvfn>, argument reordering is not supporte
11010 when called with a non-null C-style variable argument list.
11012 If the appended data contains "wide" characters
11013 (including, but not limited to, SVs with a UTF-8 PV formatted with C<%s>,
11014 and characters >255 formatted with C<%c>), the original SV might get
11017 If the original SV was UTF-8, the pattern should be
11018 valid UTF-8; if the original SV was bytes, the pattern should be too.
11020 All perform 'get' magic, but only C<sv_catpvf_mg> and C<sv_catpvf_mg_nocontext>
11021 perform 'set' magic.
11023 C<sv_catpvf_nocontext> and C<sv_catpvf_mg_nocontext> do not take a thread
11024 context (C<aTHX>) parameter, so are used in situations where the caller
11025 doesn't already have the thread context.
11031 Perl_sv_catpvf(pTHX_ SV *const sv, const char *const pat, ...)
11035 PERL_ARGS_ASSERT_SV_CATPVF;
11037 va_start(args, pat);
11038 sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11043 =for apidoc sv_vcatpvf
11044 =for apidoc_item sv_vcatpvf_mg
11046 These process their arguments like C<sv_vcatpvfn> called with a non-null
11047 C-style variable argument list, and append the formatted output to C<sv>.
11049 They differ only in that C<sv_vcatpvf_mg> performs 'set' magic;
11050 C<sv_vcatpvf> skips 'set' magic.
11052 Both perform 'get' magic.
11054 They are usually accessed via their frontends C<L</sv_catpvf>> and
11055 C<L</sv_catpvf_mg>>.
11061 Perl_sv_vcatpvf(pTHX_ SV *const sv, const char *const pat, va_list *const args)
11063 PERL_ARGS_ASSERT_SV_VCATPVF;
11065 sv_vcatpvfn_flags(sv, pat, strlen(pat), args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11069 Perl_sv_catpvf_mg(pTHX_ SV *const sv, const char *const pat, ...)
11073 PERL_ARGS_ASSERT_SV_CATPVF_MG;
11075 va_start(args, pat);
11076 sv_vcatpvfn_flags(sv, pat, strlen(pat), &args, NULL, 0, NULL, SV_GMAGIC|SV_SMAGIC);
11082 Perl_sv_vcatpvf_mg(pTHX_ SV *const sv, const char *const pat, va_list *const args)
11084 PERL_ARGS_ASSERT_SV_VCATPVF_MG;
11086 sv_vcatpvfn(sv, pat, strlen(pat), args, NULL, 0, NULL);
11091 =for apidoc sv_vsetpvfn
11093 Works like C<sv_vcatpvfn> but copies the text into the SV instead of
11096 Usually used via one of its frontends L</C<sv_vsetpvf>> and
11097 L</C<sv_vsetpvf_mg>>.
11103 Perl_sv_vsetpvfn(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
11104 va_list *const args, SV **const svargs, const Size_t sv_count, bool *const maybe_tainted)
11106 PERL_ARGS_ASSERT_SV_VSETPVFN;
11109 sv_vcatpvfn_flags(sv, pat, patlen, args, svargs, sv_count, maybe_tainted, 0);
11113 /* simplified inline Perl_sv_catpvn_nomg() when you know the SV's SvPOK */
11115 PERL_STATIC_INLINE void
11116 S_sv_catpvn_simple(pTHX_ SV *const sv, const char* const buf, const STRLEN len)
11118 STRLEN const need = len + SvCUR(sv) + 1;
11121 /* can't wrap as both len and SvCUR() are allocated in
11122 * memory and together can't consume all the address space
11124 assert(need > len);
11129 Copy(buf, end, len, char);
11132 SvCUR_set(sv, need - 1);
11137 * Warn of missing argument to sprintf. The value used in place of such
11138 * arguments should be &PL_sv_no; an undefined value would yield
11139 * inappropriate "use of uninit" warnings [perl #71000].
11142 S_warn_vcatpvfn_missing_argument(pTHX) {
11143 if (ckWARN(WARN_MISSING)) {
11144 Perl_warner(aTHX_ packWARN(WARN_MISSING), "Missing argument in %s",
11145 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
11154 Perl_croak(aTHX_ "Integer overflow in format string for %s",
11155 (PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn"));
11159 /* Given an int i from the next arg (if args is true) or an sv from an arg
11160 * (if args is false), try to extract a STRLEN-ranged value from the arg,
11161 * with overflow checking.
11162 * Sets *neg to true if the value was negative (untouched otherwise.
11163 * Returns the absolute value.
11164 * As an extra margin of safety, it croaks if the returned value would
11165 * exceed the maximum value of a STRLEN / 4.
11169 S_sprintf_arg_num_val(pTHX_ va_list *const args, int i, SV *sv, bool *neg)
11183 if (UNLIKELY(SvIsUV(sv))) {
11184 UV uv = SvUV_nomg(sv);
11186 S_croak_overflow();
11190 iv = SvIV_nomg(sv);
11194 S_croak_overflow();
11200 if (iv > (IV)(((STRLEN)~0) / 4))
11201 S_croak_overflow();
11206 /* Read in and return a number. Updates *pattern to point to the char
11207 * following the number. Expects the first char to 1..9.
11208 * Croaks if the number exceeds 1/4 of the maximum value of STRLEN.
11209 * This is a belt-and-braces safety measure to complement any
11210 * overflow/wrap checks done in the main body of sv_vcatpvfn_flags.
11211 * It means that e.g. on a 32-bit system the width/precision can't be more
11212 * than 1G, which seems reasonable.
11216 S_expect_number(pTHX_ const char **const pattern)
11220 PERL_ARGS_ASSERT_EXPECT_NUMBER;
11222 assert(inRANGE(**pattern, '1', '9'));
11224 var = *(*pattern)++ - '0';
11225 while (isDIGIT(**pattern)) {
11226 /* if var * 10 + 9 would exceed 1/4 max strlen, croak */
11227 if (var > ((((STRLEN)~0) / 4 - 9) / 10))
11228 S_croak_overflow();
11229 var = var * 10 + (*(*pattern)++ - '0');
11234 /* Implement a fast "%.0f": given a pointer to the end of a buffer (caller
11235 * ensures it's big enough), back fill it with the rounded integer part of
11236 * nv. Returns ptr to start of string, and sets *len to its length.
11237 * Returns NULL if not convertible.
11241 S_F0convert(NV nv, char *const endbuf, STRLEN *const len)
11243 const int neg = nv < 0;
11246 PERL_ARGS_ASSERT_F0CONVERT;
11248 assert(!Perl_isinfnan(nv));
11251 if (nv != 0.0 && nv < (NV) UV_MAX) {
11257 if (uv & 1 && uv == nv)
11258 uv--; /* Round to even */
11261 const unsigned dig = uv % 10;
11263 } while (uv /= 10);
11273 /* XXX maybe_tainted is never assigned to, so the doc above is lying. */
11276 Perl_sv_vcatpvfn(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
11277 va_list *const args, SV **const svargs, const Size_t sv_count, bool *const maybe_tainted)
11279 PERL_ARGS_ASSERT_SV_VCATPVFN;
11281 sv_vcatpvfn_flags(sv, pat, patlen, args, svargs, sv_count, maybe_tainted, SV_GMAGIC|SV_SMAGIC);
11285 /* For the vcatpvfn code, we need a long double target in case
11286 * HAS_LONG_DOUBLE, even without USE_LONG_DOUBLE, so that we can printf
11287 * with long double formats, even without NV being long double. But we
11288 * call the target 'fv' instead of 'nv', since most of the time it is not
11289 * (most compilers these days recognize "long double", even if only as a
11290 * synonym for "double").
11292 #if defined(HAS_LONG_DOUBLE) && LONG_DOUBLESIZE > DOUBLESIZE && \
11293 defined(PERL_PRIgldbl) && !defined(USE_QUADMATH)
11294 # define VCATPVFN_FV_GF PERL_PRIgldbl
11295 # if defined(__VMS) && defined(__ia64) && defined(__IEEE_FLOAT)
11296 /* Work around breakage in OTS$CVT_FLOAT_T_X */
11297 # define VCATPVFN_NV_TO_FV(nv,fv) \
11300 fv = Perl_isnan(_dv) ? LDBL_QNAN : _dv; \
11303 # define VCATPVFN_NV_TO_FV(nv,fv) (fv)=(nv)
11305 typedef long double vcatpvfn_long_double_t;
11307 # define VCATPVFN_FV_GF NVgf
11308 # define VCATPVFN_NV_TO_FV(nv,fv) (fv)=(nv)
11309 typedef NV vcatpvfn_long_double_t;
11312 #ifdef LONGDOUBLE_DOUBLEDOUBLE
11313 /* The first double can be as large as 2**1023, or '1' x '0' x 1023.
11314 * The second double can be as small as 2**-1074, or '0' x 1073 . '1'.
11315 * The sum of them can be '1' . '0' x 2096 . '1', with implied radix point
11316 * after the first 1023 zero bits.
11318 * XXX The 2098 is quite large (262.25 bytes) and therefore some sort
11319 * of dynamically growing buffer might be better, start at just 16 bytes
11320 * (for example) and grow only when necessary. Or maybe just by looking
11321 * at the exponents of the two doubles? */
11322 # define DOUBLEDOUBLE_MAXBITS 2098
11325 /* vhex will contain the values (0..15) of the hex digits ("nybbles"
11326 * of 4 bits); 1 for the implicit 1, and the mantissa bits, four bits
11327 * per xdigit. For the double-double case, this can be rather many.
11328 * The non-double-double-long-double overshoots since all bits of NV
11329 * are not mantissa bits, there are also exponent bits. */
11330 #ifdef LONGDOUBLE_DOUBLEDOUBLE
11331 # define VHEX_SIZE (3+DOUBLEDOUBLE_MAXBITS/4)
11333 # define VHEX_SIZE (1+(NVSIZE * 8)/4)
11336 /* If we do not have a known long double format, (including not using
11337 * long doubles, or long doubles being equal to doubles) then we will
11338 * fall back to the ldexp/frexp route, with which we can retrieve at
11339 * most as many bits as our widest unsigned integer type is. We try
11340 * to get a 64-bit unsigned integer even if we are not using a 64-bit UV.
11342 * (If you want to test the case of UVSIZE == 4, NVSIZE == 8,
11343 * set the MANTISSATYPE to int and the MANTISSASIZE to 4.)
11345 #if defined(HAS_QUAD) && defined(Uquad_t)
11346 # define MANTISSATYPE Uquad_t
11347 # define MANTISSASIZE 8
11349 # define MANTISSATYPE UV
11350 # define MANTISSASIZE UVSIZE
11353 #if defined(DOUBLE_LITTLE_ENDIAN) || defined(LONGDOUBLE_LITTLE_ENDIAN)
11354 # define HEXTRACT_LITTLE_ENDIAN
11355 #elif defined(DOUBLE_BIG_ENDIAN) || defined(LONGDOUBLE_BIG_ENDIAN)
11356 # define HEXTRACT_BIG_ENDIAN
11358 # define HEXTRACT_MIX_ENDIAN
11361 /* S_hextract() is a helper for S_format_hexfp, for extracting
11362 * the hexadecimal values (for %a/%A). The nv is the NV where the value
11363 * are being extracted from (either directly from the long double in-memory
11364 * presentation, or from the uquad computed via frexp+ldexp). frexp also
11365 * is used to update the exponent. The subnormal is set to true
11366 * for IEEE 754 subnormals/denormals (including the x86 80-bit format).
11367 * The vhex is the pointer to the beginning of the output buffer of VHEX_SIZE.
11369 * The tricky part is that S_hextract() needs to be called twice:
11370 * the first time with vend as NULL, and the second time with vend as
11371 * the pointer returned by the first call. What happens is that on
11372 * the first round the output size is computed, and the intended
11373 * extraction sanity checked. On the second round the actual output
11374 * (the extraction of the hexadecimal values) takes place.
11375 * Sanity failures cause fatal failures during both rounds. */
11377 S_hextract(pTHX_ const NV nv, int* exponent, bool *subnormal,
11378 U8* vhex, U8* vend)
11382 int ixmin = 0, ixmax = 0;
11384 /* XXX Inf/NaN are not handled here, since it is
11385 * assumed they are to be output as "Inf" and "NaN". */
11387 /* These macros are just to reduce typos, they have multiple
11388 * repetitions below, but usually only one (or sometimes two)
11389 * of them is really being used. */
11390 /* HEXTRACT_OUTPUT() extracts the high nybble first. */
11391 #define HEXTRACT_OUTPUT_HI(ix) (*v++ = nvp[ix] >> 4)
11392 #define HEXTRACT_OUTPUT_LO(ix) (*v++ = nvp[ix] & 0xF)
11393 #define HEXTRACT_OUTPUT(ix) \
11395 HEXTRACT_OUTPUT_HI(ix); HEXTRACT_OUTPUT_LO(ix); \
11397 #define HEXTRACT_COUNT(ix, c) \
11399 v += c; if (ix < ixmin) ixmin = ix; else if (ix > ixmax) ixmax = ix; \
11401 #define HEXTRACT_BYTE(ix) \
11403 if (vend) HEXTRACT_OUTPUT(ix); else HEXTRACT_COUNT(ix, 2); \
11405 #define HEXTRACT_LO_NYBBLE(ix) \
11407 if (vend) HEXTRACT_OUTPUT_LO(ix); else HEXTRACT_COUNT(ix, 1); \
11409 /* HEXTRACT_TOP_NYBBLE is just convenience disguise,
11410 * to make it look less odd when the top bits of a NV
11411 * are extracted using HEXTRACT_LO_NYBBLE: the highest
11412 * order bits can be in the "low nybble" of a byte. */
11413 #define HEXTRACT_TOP_NYBBLE(ix) HEXTRACT_LO_NYBBLE(ix)
11414 #define HEXTRACT_BYTES_LE(a, b) \
11415 for (ix = a; ix >= b; ix--) { HEXTRACT_BYTE(ix); }
11416 #define HEXTRACT_BYTES_BE(a, b) \
11417 for (ix = a; ix <= b; ix++) { HEXTRACT_BYTE(ix); }
11418 #define HEXTRACT_GET_SUBNORMAL(nv) *subnormal = Perl_fp_class_denorm(nv)
11419 #define HEXTRACT_IMPLICIT_BIT(nv) \
11421 if (!*subnormal) { \
11422 if (vend) *v++ = ((nv) == 0.0) ? 0 : 1; else v++; \
11426 /* Most formats do. Those which don't should undef this.
11428 * But also note that IEEE 754 subnormals do not have it, or,
11429 * expressed alternatively, their implicit bit is zero. */
11430 #define HEXTRACT_HAS_IMPLICIT_BIT
11432 /* Many formats do. Those which don't should undef this. */
11433 #define HEXTRACT_HAS_TOP_NYBBLE
11435 /* HEXTRACTSIZE is the maximum number of xdigits. */
11436 #if defined(USE_LONG_DOUBLE) && defined(LONGDOUBLE_DOUBLEDOUBLE)
11437 # define HEXTRACTSIZE (2+DOUBLEDOUBLE_MAXBITS/4)
11439 # define HEXTRACTSIZE 2 * NVSIZE
11442 const U8* vmaxend = vhex + HEXTRACTSIZE;
11444 assert(HEXTRACTSIZE <= VHEX_SIZE);
11446 PERL_UNUSED_VAR(ix); /* might happen */
11447 (void)Perl_frexp(PERL_ABS(nv), exponent);
11448 *subnormal = FALSE;
11449 if (vend && (vend <= vhex || vend > vmaxend)) {
11450 /* diag_listed_as: Hexadecimal float: internal error (%s) */
11451 Perl_croak(aTHX_ "Hexadecimal float: internal error (entry)");
11454 /* First check if using long doubles. */
11455 #if defined(USE_LONG_DOUBLE) && (NVSIZE > DOUBLESIZE)
11456 # if LONG_DOUBLEKIND == LONG_DOUBLE_IS_IEEE_754_128_BIT_LITTLE_ENDIAN
11457 /* Used in e.g. VMS and HP-UX IA-64, e.g. -0.1L:
11458 * 9a 99 99 99 99 99 99 99 99 99 99 99 99 99 fb bf */
11459 /* The bytes 13..0 are the mantissa/fraction,
11460 * the 15,14 are the sign+exponent. */
11461 const U8* nvp = (const U8*)(&nv);
11462 HEXTRACT_GET_SUBNORMAL(nv);
11463 HEXTRACT_IMPLICIT_BIT(nv);
11464 # undef HEXTRACT_HAS_TOP_NYBBLE
11465 HEXTRACT_BYTES_LE(13, 0);
11466 # elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_IEEE_754_128_BIT_BIG_ENDIAN
11467 /* Used in e.g. Solaris Sparc and HP-UX PA-RISC, e.g. -0.1L:
11468 * bf fb 99 99 99 99 99 99 99 99 99 99 99 99 99 9a */
11469 /* The bytes 2..15 are the mantissa/fraction,
11470 * the 0,1 are the sign+exponent. */
11471 const U8* nvp = (const U8*)(&nv);
11472 HEXTRACT_GET_SUBNORMAL(nv);
11473 HEXTRACT_IMPLICIT_BIT(nv);
11474 # undef HEXTRACT_HAS_TOP_NYBBLE
11475 HEXTRACT_BYTES_BE(2, 15);
11476 # elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_LITTLE_ENDIAN
11477 /* x86 80-bit "extended precision", 64 bits of mantissa / fraction /
11478 * significand, 15 bits of exponent, 1 bit of sign. No implicit bit.
11479 * NVSIZE can be either 12 (ILP32, Solaris x86) or 16 (LP64, Linux
11480 * and OS X), meaning that 2 or 6 bytes are empty padding. */
11481 /* The bytes 0..1 are the sign+exponent,
11482 * the bytes 2..9 are the mantissa/fraction. */
11483 const U8* nvp = (const U8*)(&nv);
11484 # undef HEXTRACT_HAS_IMPLICIT_BIT
11485 # undef HEXTRACT_HAS_TOP_NYBBLE
11486 HEXTRACT_GET_SUBNORMAL(nv);
11487 HEXTRACT_BYTES_LE(7, 0);
11488 # elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_BIG_ENDIAN
11489 /* Does this format ever happen? (Wikipedia says the Motorola
11490 * 6888x math coprocessors used format _like_ this but padded
11491 * to 96 bits with 16 unused bits between the exponent and the
11493 const U8* nvp = (const U8*)(&nv);
11494 # undef HEXTRACT_HAS_IMPLICIT_BIT
11495 # undef HEXTRACT_HAS_TOP_NYBBLE
11496 HEXTRACT_GET_SUBNORMAL(nv);
11497 HEXTRACT_BYTES_BE(0, 7);
11499 # define HEXTRACT_FALLBACK
11500 /* Double-double format: two doubles next to each other.
11501 * The first double is the high-order one, exactly like
11502 * it would be for a "lone" double. The second double
11503 * is shifted down using the exponent so that that there
11504 * are no common bits. The tricky part is that the value
11505 * of the double-double is the SUM of the two doubles and
11506 * the second one can be also NEGATIVE.
11508 * Because of this tricky construction the bytewise extraction we
11509 * use for the other long double formats doesn't work, we must
11510 * extract the values bit by bit.
11512 * The little-endian double-double is used .. somewhere?
11514 * The big endian double-double is used in e.g. PPC/Power (AIX)
11517 * The mantissa bits are in two separate stretches, e.g. for -0.1L:
11518 * 9a 99 99 99 99 99 59 bc 9a 99 99 99 99 99 b9 3f (LE)
11519 * 3f b9 99 99 99 99 99 9a bc 59 99 99 99 99 99 9a (BE)
11522 #else /* #if defined(USE_LONG_DOUBLE) && (NVSIZE > DOUBLESIZE) */
11523 /* Using normal doubles, not long doubles.
11525 * We generate 4-bit xdigits (nybble/nibble) instead of 8-bit
11526 * bytes, since we might need to handle printf precision, and
11527 * also need to insert the radix. */
11529 # ifdef HEXTRACT_LITTLE_ENDIAN
11530 /* 0 1 2 3 4 5 6 7 (MSB = 7, LSB = 0, 6+7 = exponent+sign) */
11531 const U8* nvp = (const U8*)(&nv);
11532 HEXTRACT_GET_SUBNORMAL(nv);
11533 HEXTRACT_IMPLICIT_BIT(nv);
11534 HEXTRACT_TOP_NYBBLE(6);
11535 HEXTRACT_BYTES_LE(5, 0);
11536 # elif defined(HEXTRACT_BIG_ENDIAN)
11537 /* 7 6 5 4 3 2 1 0 (MSB = 7, LSB = 0, 6+7 = exponent+sign) */
11538 const U8* nvp = (const U8*)(&nv);
11539 HEXTRACT_GET_SUBNORMAL(nv);
11540 HEXTRACT_IMPLICIT_BIT(nv);
11541 HEXTRACT_TOP_NYBBLE(1);
11542 HEXTRACT_BYTES_BE(2, 7);
11543 # elif DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_MIXED_ENDIAN_LE_BE
11544 /* 4 5 6 7 0 1 2 3 (MSB = 7, LSB = 0, 6:7 = nybble:exponent:sign) */
11545 const U8* nvp = (const U8*)(&nv);
11546 HEXTRACT_GET_SUBNORMAL(nv);
11547 HEXTRACT_IMPLICIT_BIT(nv);
11548 HEXTRACT_TOP_NYBBLE(2); /* 6 */
11549 HEXTRACT_BYTE(1); /* 5 */
11550 HEXTRACT_BYTE(0); /* 4 */
11551 HEXTRACT_BYTE(7); /* 3 */
11552 HEXTRACT_BYTE(6); /* 2 */
11553 HEXTRACT_BYTE(5); /* 1 */
11554 HEXTRACT_BYTE(4); /* 0 */
11555 # elif DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_MIXED_ENDIAN_BE_LE
11556 /* 3 2 1 0 7 6 5 4 (MSB = 7, LSB = 0, 7:6 = sign:exponent:nybble) */
11557 const U8* nvp = (const U8*)(&nv);
11558 HEXTRACT_GET_SUBNORMAL(nv);
11559 HEXTRACT_IMPLICIT_BIT(nv);
11560 HEXTRACT_TOP_NYBBLE(5); /* 6 */
11561 HEXTRACT_BYTE(6); /* 5 */
11562 HEXTRACT_BYTE(7); /* 4 */
11563 HEXTRACT_BYTE(0); /* 3 */
11564 HEXTRACT_BYTE(1); /* 2 */
11565 HEXTRACT_BYTE(2); /* 1 */
11566 HEXTRACT_BYTE(3); /* 0 */
11568 # define HEXTRACT_FALLBACK
11571 # define HEXTRACT_FALLBACK
11573 #endif /* #if defined(USE_LONG_DOUBLE) && (NVSIZE > DOUBLESIZE) #else */
11575 #ifdef HEXTRACT_FALLBACK
11576 HEXTRACT_GET_SUBNORMAL(nv);
11577 # undef HEXTRACT_HAS_TOP_NYBBLE /* Meaningless, but consistent. */
11578 /* The fallback is used for the double-double format, and
11579 * for unknown long double formats, and for unknown double
11580 * formats, or in general unknown NV formats. */
11581 if (nv == (NV)0.0) {
11589 NV d = nv < 0 ? -nv : nv;
11591 U8 ha = 0x0; /* hexvalue accumulator */
11592 U8 hd = 0x8; /* hexvalue digit */
11594 /* Shift d and e (and update exponent) so that e <= d < 2*e,
11595 * this is essentially manual frexp(). Multiplying by 0.5 and
11596 * doubling should be lossless in binary floating point. */
11606 while (d >= e + e) {
11610 /* Now e <= d < 2*e */
11612 /* First extract the leading hexdigit (the implicit bit). */
11628 /* Then extract the remaining hexdigits. */
11629 while (d > (NV)0.0) {
11635 /* Output or count in groups of four bits,
11636 * that is, when the hexdigit is down to one. */
11641 /* Reset the hexvalue. */
11650 /* Flush possible pending hexvalue. */
11660 /* Croak for various reasons: if the output pointer escaped the
11661 * output buffer, if the extraction index escaped the extraction
11662 * buffer, or if the ending output pointer didn't match the
11663 * previously computed value. */
11664 if (v <= vhex || v - vhex >= VHEX_SIZE ||
11665 /* For double-double the ixmin and ixmax stay at zero,
11666 * which is convenient since the HEXTRACTSIZE is tricky
11667 * for double-double. */
11668 ixmin < 0 || ixmax >= NVSIZE ||
11669 (vend && v != vend)) {
11670 /* diag_listed_as: Hexadecimal float: internal error (%s) */
11671 Perl_croak(aTHX_ "Hexadecimal float: internal error (overflow)");
11677 /* S_format_hexfp(): helper function for Perl_sv_vcatpvfn_flags().
11679 * Processes the %a/%A hexadecimal floating-point format, since the
11680 * built-in snprintf()s which are used for most of the f/p formats, don't
11681 * universally handle %a/%A.
11682 * Populates buf of length bufsize, and returns the length of the created
11684 * The rest of the args have the same meaning as the local vars of the
11685 * same name within Perl_sv_vcatpvfn_flags().
11687 * The caller's determination of IN_LC(LC_NUMERIC), passed as in_lc_numeric,
11688 * is used to ensure we do the right thing when we need to access the locale's
11691 * It requires the caller to make buf large enough.
11695 S_format_hexfp(pTHX_ char * const buf, const STRLEN bufsize, const char c,
11696 const NV nv, const vcatpvfn_long_double_t fv,
11697 bool has_precis, STRLEN precis, STRLEN width,
11698 bool alt, char plus, bool left, bool fill, bool in_lc_numeric)
11700 /* Hexadecimal floating point. */
11702 U8 vhex[VHEX_SIZE];
11703 U8* v = vhex; /* working pointer to vhex */
11704 U8* vend; /* pointer to one beyond last digit of vhex */
11705 U8* vfnz = NULL; /* first non-zero */
11706 U8* vlnz = NULL; /* last non-zero */
11707 U8* v0 = NULL; /* first output */
11708 const bool lower = (c == 'a');
11709 /* At output the values of vhex (up to vend) will
11710 * be mapped through the xdig to get the actual
11711 * human-readable xdigits. */
11712 const char* xdig = PL_hexdigit;
11713 STRLEN zerotail = 0; /* how many extra zeros to append */
11714 int exponent = 0; /* exponent of the floating point input */
11715 bool hexradix = FALSE; /* should we output the radix */
11716 bool subnormal = FALSE; /* IEEE 754 subnormal/denormal */
11717 bool negative = FALSE;
11720 /* XXX: NaN, Inf -- though they are printed as "NaN" and "Inf".
11722 * For example with denormals, (assuming the vanilla
11723 * 64-bit double): the exponent is zero. 1xp-1074 is
11724 * the smallest denormal and the smallest double, it
11725 * could be output also as 0x0.0000000000001p-1022 to
11726 * match its internal structure. */
11728 vend = S_hextract(aTHX_ nv, &exponent, &subnormal, vhex, NULL);
11729 S_hextract(aTHX_ nv, &exponent, &subnormal, vhex, vend);
11731 #if NVSIZE > DOUBLESIZE
11732 # ifdef HEXTRACT_HAS_IMPLICIT_BIT
11733 /* In this case there is an implicit bit,
11734 * and therefore the exponent is shifted by one. */
11736 # elif defined(NV_X86_80_BIT)
11738 /* The subnormals of the x86-80 have a base exponent of -16382,
11739 * (while the physical exponent bits are zero) but the frexp()
11740 * returned the scientific-style floating exponent. We want
11741 * to map the last one as:
11742 * -16831..-16384 -> -16382 (the last normal is 0x1p-16382)
11743 * -16835..-16388 -> -16384
11744 * since we want to keep the first hexdigit
11745 * as one of the [8421]. */
11746 exponent = -4 * ( (exponent + 1) / -4) - 2;
11750 /* TBD: other non-implicit-bit platforms than the x86-80. */
11754 negative = fv < 0 || Perl_signbit(nv);
11765 xdig += 16; /* Use uppercase hex. */
11768 /* Find the first non-zero xdigit. */
11769 for (v = vhex; v < vend; v++) {
11777 /* Find the last non-zero xdigit. */
11778 for (v = vend - 1; v >= vhex; v--) {
11785 #if NVSIZE == DOUBLESIZE
11791 #ifndef NV_X86_80_BIT
11793 /* IEEE 754 subnormals (but not the x86 80-bit):
11794 * we want "normalize" the subnormal,
11795 * so we need to right shift the hex nybbles
11796 * so that the output of the subnormal starts
11797 * from the first true bit. (Another, equally
11798 * valid, policy would be to dump the subnormal
11799 * nybbles as-is, to display the "physical" layout.) */
11802 /* Find the ceil(log2(v[0])) of
11803 * the top non-zero nybble. */
11804 for (i = vfnz[0], n = 0; i > 1; i >>= 1, n++) { }
11808 for (vshr = vlnz; vshr >= vfnz; vshr--) {
11809 vshr[1] |= (vshr[0] & (0xF >> (4 - n))) << (4 - n);
11823 U8* ve = (subnormal ? vlnz + 1 : vend);
11824 SSize_t vn = ve - v0;
11826 if (precis < (Size_t)(vn - 1)) {
11827 bool overflow = FALSE;
11828 if (v0[precis + 1] < 0x8) {
11829 /* Round down, nothing to do. */
11830 } else if (v0[precis + 1] > 0x8) {
11833 overflow = v0[precis] > 0xF;
11835 } else { /* v0[precis] == 0x8 */
11836 /* Half-point: round towards the one
11837 * with the even least-significant digit:
11845 * 78 -> 8 f8 -> 10 */
11846 if ((v0[precis] & 0x1)) {
11849 overflow = v0[precis] > 0xF;
11854 for (v = v0 + precis - 1; v >= v0; v--) {
11856 overflow = *v > 0xF;
11862 if (v == v0 - 1 && overflow) {
11863 /* If the overflow goes all the
11864 * way to the front, we need to
11865 * insert 0x1 in front, and adjust
11867 Move(v0, v0 + 1, vn - 1, char);
11873 /* The new effective "last non zero". */
11874 vlnz = v0 + precis;
11878 subnormal ? precis - vn + 1 :
11879 precis - (vlnz - vhex);
11886 /* If there are non-zero xdigits, the radix
11887 * is output after the first one. */
11895 zerotail = has_precis ? precis : 0;
11898 /* The radix is always output if precis, or if alt. */
11899 if ((has_precis && precis > 0) || alt) {
11904 #ifndef USE_LOCALE_NUMERIC
11905 PERL_UNUSED_ARG(in_lc_numeric);
11909 if (in_lc_numeric) {
11911 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(TRUE, {
11912 const char* r = SvPV(PL_numeric_radix_sv, n);
11913 Copy(r, p, n, char);
11928 if (zerotail > 0) {
11929 while (zerotail--) {
11936 /* sanity checks */
11937 if (elen >= bufsize || width >= bufsize)
11938 /* diag_listed_as: Hexadecimal float: internal error (%s) */
11939 Perl_croak(aTHX_ "Hexadecimal float: internal error (overflow)");
11941 elen += my_snprintf(p, bufsize - elen,
11942 "%c%+d", lower ? 'p' : 'P',
11945 if (elen < width) {
11946 STRLEN gap = (STRLEN)(width - elen);
11948 /* Pad the back with spaces. */
11949 memset(buf + elen, ' ', gap);
11952 /* Insert the zeros after the "0x" and the
11953 * the potential sign, but before the digits,
11954 * otherwise we end up with "0000xH.HHH...",
11955 * when we want "0x000H.HHH..." */
11956 STRLEN nzero = gap;
11957 char* zerox = buf + 2;
11958 STRLEN nmove = elen - 2;
11959 if (negative || plus) {
11963 Move(zerox, zerox + nzero, nmove, char);
11964 memset(zerox, fill ? '0' : ' ', nzero);
11967 /* Move it to the right. */
11968 Move(buf, buf + gap,
11970 /* Pad the front with spaces. */
11971 memset(buf, ' ', gap);
11979 =for apidoc sv_vcatpvfn
11980 =for apidoc_item sv_vcatpvfn_flags
11982 These process their arguments like C<L<vsprintf(3)>> and append the formatted output
11983 to an SV. They use an array of SVs if the C-style variable argument list is
11984 missing (C<NULL>). Argument reordering (using format specifiers like C<%2$d> or
11985 C<%*2$d>) is supported only when using an array of SVs; using a C-style
11986 C<va_list> argument list with a format string that uses argument reordering
11987 will yield an exception.
11989 When running with taint checks enabled, they indicate via C<maybe_tainted> if
11990 results are untrustworthy (often due to the use of locales).
11992 They assume that C<pat> has the same utf8-ness as C<sv>. It's the caller's
11993 responsibility to ensure that this is so.
11995 They differ in that C<sv_vcatpvfn_flags> has a C<flags> parameter in which you
11996 can set or clear the C<SV_GMAGIC> and/or S<SV_SMAGIC> flags, to specify which
11997 magic to handle or not handle; whereas plain C<sv_vcatpvfn> always specifies
11998 both 'get' and 'set' magic.
12000 They are usually used via one of the frontends L</C<sv_vcatpvf>> and
12001 L</C<sv_vcatpvf_mg>>.
12008 Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN patlen,
12009 va_list *const args, SV **const svargs, const Size_t sv_count, bool *const maybe_tainted,
12012 const char *fmtstart; /* character following the current '%' */
12013 const char *q; /* current position within format */
12014 const char *patend;
12017 static const char nullstr[] = "(null)";
12018 bool has_utf8 = DO_UTF8(sv); /* has the result utf8? */
12019 const bool pat_utf8 = has_utf8; /* the pattern is in utf8? */
12020 /* Times 4: a decimal digit takes more than 3 binary digits.
12021 * NV_DIG: mantissa takes that many decimal digits.
12022 * Plus 32: Playing safe. */
12023 char ebuf[IV_DIG * 4 + NV_DIG + 32];
12024 bool no_redundant_warning = FALSE; /* did we use any explicit format parameter index? */
12025 #ifdef USE_LOCALE_NUMERIC
12026 bool have_in_lc_numeric = FALSE;
12028 /* we never change this unless USE_LOCALE_NUMERIC */
12029 bool in_lc_numeric = FALSE;
12032 PERL_ARGS_ASSERT_SV_VCATPVFN_FLAGS;
12033 PERL_UNUSED_ARG(maybe_tainted);
12035 if (flags & SV_GMAGIC)
12038 /* no matter what, this is a string now */
12039 (void)SvPV_force_nomg(sv, origlen);
12041 /* the code that scans for flags etc following a % relies on
12042 * a '\0' being present to avoid falling off the end. Ideally that
12043 * should be fixed */
12044 assert(pat[patlen] == '\0');
12047 /* Special-case "", "%s", "%-p" (SVf - see below) and "%.0f".
12048 * In each case, if there isn't the correct number of args, instead
12049 * fall through to the main code to handle the issuing of any
12053 if (patlen == 0 && (args || sv_count == 0))
12056 if (patlen <= 4 && pat[0] == '%' && (args || sv_count == 1)) {
12059 if (patlen == 2 && pat[1] == 's') {
12061 const char * const s = va_arg(*args, char*);
12062 sv_catpv_nomg(sv, s ? s : nullstr);
12065 /* we want get magic on the source but not the target.
12066 * sv_catsv can't do that, though */
12067 SvGETMAGIC(*svargs);
12068 sv_catsv_nomg(sv, *svargs);
12075 if (patlen == 3 && pat[1] == '-' && pat[2] == 'p') {
12076 SV *asv = MUTABLE_SV(va_arg(*args, void*));
12077 sv_catsv_nomg(sv, asv);
12081 #if !defined(USE_LONG_DOUBLE) && !defined(USE_QUADMATH)
12082 /* special-case "%.0f" */
12083 else if ( patlen == 4
12084 && pat[1] == '.' && pat[2] == '0' && pat[3] == 'f')
12086 const NV nv = SvNV(*svargs);
12087 if (LIKELY(!Perl_isinfnan(nv))) {
12091 if ((p = F0convert(nv, ebuf + sizeof ebuf, &l))) {
12092 sv_catpvn_nomg(sv, p, l);
12097 #endif /* !USE_LONG_DOUBLE */
12101 patend = (char*)pat + patlen;
12102 for (fmtstart = pat; fmtstart < patend; fmtstart = q) {
12103 char intsize = 0; /* size qualifier in "%hi..." etc */
12104 bool alt = FALSE; /* has "%#..." */
12105 bool left = FALSE; /* has "%-..." */
12106 bool fill = FALSE; /* has "%0..." */
12107 char plus = 0; /* has "%+..." */
12108 STRLEN width = 0; /* value of "%NNN..." */
12109 bool has_precis = FALSE; /* has "%.NNN..." */
12110 STRLEN precis = 0; /* value of "%.NNN..." */
12111 int base = 0; /* base to print in, e.g. 8 for %o */
12112 UV uv = 0; /* the value to print of int-ish args */
12114 bool vectorize = FALSE; /* has "%v..." */
12115 bool vec_utf8 = FALSE; /* SvUTF8(vec arg) */
12116 const U8 *vecstr = NULL; /* SvPVX(vec arg) */
12117 STRLEN veclen = 0; /* SvCUR(vec arg) */
12118 const char *dotstr = NULL; /* separator string for %v */
12119 STRLEN dotstrlen; /* length of separator string for %v */
12121 Size_t efix = 0; /* explicit format parameter index */
12122 const Size_t osvix = svix; /* original index in case of bad fmt */
12125 bool is_utf8 = FALSE; /* is this item utf8? */
12126 bool arg_missing = FALSE; /* give "Missing argument" warning */
12127 char esignbuf[4]; /* holds sign prefix, e.g. "-0x" */
12128 STRLEN esignlen = 0; /* length of e.g. "-0x" */
12129 STRLEN zeros = 0; /* how many '0' to prepend */
12131 const char *eptr = NULL; /* the address of the element string */
12132 STRLEN elen = 0; /* the length of the element string */
12134 char c; /* the actual format ('d', s' etc) */
12136 bool escape_it = FALSE; /* if this is a string should we quote and escape it? */
12139 /* echo everything up to the next format specification */
12140 for (q = fmtstart; q < patend && *q != '%'; ++q)
12143 if (q > fmtstart) {
12144 if (has_utf8 && !pat_utf8) {
12145 /* upgrade and copy the bytes of fmtstart..q-1 to utf8 on
12149 STRLEN need = SvCUR(sv) + (q - fmtstart) + 1;
12151 for (p = fmtstart; p < q; p++)
12152 if (!NATIVE_BYTE_IS_INVARIANT(*p))
12157 for (p = fmtstart; p < q; p++)
12158 append_utf8_from_native_byte((U8)*p, (U8**)&dst);
12160 SvCUR_set(sv, need - 1);
12163 S_sv_catpvn_simple(aTHX_ sv, fmtstart, q - fmtstart);
12168 fmtstart = q; /* fmtstart is char following the '%' */
12171 We allow format specification elements in this order:
12172 \d+\$ explicit format parameter index
12174 v|\*(\d+\$)?v vector with optional (optionally specified) arg
12175 0 flag (as above): repeated to allow "v02"
12176 \d+|\*(\d+\$)? width using optional (optionally specified) arg
12177 \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg
12179 [%bcdefginopsuxDFOUX] format (mandatory)
12182 if (inRANGE(*q, '1', '9')) {
12183 width = expect_number(&q);
12186 Perl_croak_nocontext(
12187 "Cannot yet reorder sv_vcatpvfn() arguments from va_list");
12189 efix = (Size_t)width;
12191 no_redundant_warning = TRUE;
12203 if (plus == '+' && *q == ' ') /* '+' over ' ' */
12230 /* at this point we can expect one of:
12232 * 123 an explicit width
12233 * * width taken from next arg
12234 * *12$ width taken from 12th arg
12237 * But any width specification may be preceded by a v, in one of its
12242 * So an asterisk may be either a width specifier or a vector
12243 * separator arg specifier, and we don't know which initially
12248 STRLEN ix; /* explicit width/vector separator index */
12250 if (inRANGE(*q, '1', '9')) {
12251 ix = expect_number(&q);
12254 Perl_croak_nocontext(
12255 "Cannot yet reorder sv_vcatpvfn() arguments from va_list");
12256 no_redundant_warning = TRUE;
12265 /* The asterisk was for *v, *NNN$v: vectorizing, but not
12266 * with the default "." */
12271 vecsv = va_arg(*args, SV*);
12273 ix = ix ? ix - 1 : svix++;
12274 vecsv = ix < sv_count ? svargs[ix]
12275 : (arg_missing = TRUE, &PL_sv_no);
12277 dotstr = SvPV_const(vecsv, dotstrlen);
12278 /* Keep the DO_UTF8 test *after* the SvPV call, else things go
12279 bad with tied or overloaded values that return UTF8. */
12280 if (DO_UTF8(vecsv))
12282 else if (has_utf8) {
12283 vecsv = sv_mortalcopy(vecsv);
12284 sv_utf8_upgrade(vecsv);
12285 dotstr = SvPV_const(vecsv, dotstrlen);
12292 /* the asterisk specified a width */
12295 SV *width_sv = NULL;
12297 i = va_arg(*args, int);
12299 ix = ix ? ix - 1 : svix++;
12300 width_sv = (ix < sv_count) ? svargs[ix]
12301 : (arg_missing = TRUE, (SV*)NULL);
12303 width = S_sprintf_arg_num_val(aTHX_ args, i, width_sv, &left);
12306 else if (*q == 'v') {
12317 /* explicit width? */
12322 if (inRANGE(*q, '1', '9'))
12323 width = expect_number(&q);
12333 STRLEN ix; /* explicit precision index */
12335 if (inRANGE(*q, '1', '9')) {
12336 ix = expect_number(&q);
12339 Perl_croak_nocontext(
12340 "Cannot yet reorder sv_vcatpvfn() arguments from va_list");
12341 no_redundant_warning = TRUE;
12350 SV *width_sv = NULL;
12354 i = va_arg(*args, int);
12356 ix = ix ? ix - 1 : svix++;
12357 width_sv = (ix < sv_count) ? svargs[ix]
12358 : (arg_missing = TRUE, (SV*)NULL);
12360 precis = S_sprintf_arg_num_val(aTHX_ args, i, width_sv, &neg);
12362 /* ignore negative precision */
12368 /* although it doesn't seem documented, this code has long
12370 * no digits following the '.' is treated like '.0'
12371 * the number may be preceded by any number of zeroes,
12372 * e.g. "%.0001f", which is the same as "%.1f"
12373 * so I've kept that behaviour. DAPM May 2017
12377 precis = inRANGE(*q, '1', '9') ? expect_number(&q) : 0;
12386 case 'I': /* Ix, I32x, and I64x */
12387 # ifdef USE_64_BIT_INT
12388 if (q[1] == '6' && q[2] == '4') {
12394 if (q[1] == '3' && q[2] == '2') {
12398 # ifdef USE_64_BIT_INT
12404 #if (IVSIZE >= 8 || defined(HAS_LONG_DOUBLE)) || \
12405 (IVSIZE == 4 && !defined(HAS_LONG_DOUBLE))
12417 #if (IVSIZE >= 8 || defined(HAS_LONG_DOUBLE)) || \
12418 (IVSIZE == 4 && !defined(HAS_LONG_DOUBLE))
12419 if (*q == 'l') { /* lld, llf */
12428 if (*++q == 'h') { /* hhd, hhu */
12435 #ifdef USE_QUADMATH
12448 c = *q++; /* c now holds the conversion type */
12450 /* '%' doesn't have an arg, so skip arg processing */
12459 if (vectorize && !memCHRs("BbDdiOouUXx", c))
12462 /* get next arg (individual branches do their own va_arg()
12463 * handling for the args case) */
12466 efix = efix ? efix - 1 : svix++;
12467 argsv = efix < sv_count ? svargs[efix]
12468 : (arg_missing = TRUE, &PL_sv_no);
12478 eptr = va_arg(*args, char*);
12481 elen = my_strnlen(eptr, precis);
12483 elen = strlen(eptr);
12485 eptr = (char *)nullstr;
12486 elen = sizeof nullstr - 1;
12490 eptr = SvPV_const(argsv, elen);
12491 if (DO_UTF8(argsv)) {
12492 STRLEN old_precis = precis;
12493 if (has_precis && precis < elen) {
12494 STRLEN ulen = sv_or_pv_len_utf8(argsv, eptr, elen);
12495 STRLEN p = precis > ulen ? ulen : precis;
12496 precis = sv_or_pv_pos_u2b(argsv, eptr, p, 0);
12497 /* sticks at end */
12499 if (width) { /* fudge width (can't fudge elen) */
12500 if (has_precis && precis < elen)
12501 width += precis - old_precis;
12504 elen - sv_or_pv_len_utf8(argsv,eptr,elen);
12512 U32 flags = PERL_PV_PRETTY_QUOTEDPREFIX;
12514 flags |= PERL_PV_ESCAPE_UNI;
12517 /* "blah"... where blah might be made up
12518 * of characters like \x{1234} */
12519 tmp_sv = newSV(1 + (PERL_QUOTEDPREFIX_LEN * 8) + 1 + 3);
12520 sv_2mortal(tmp_sv);
12522 pv_pretty(tmp_sv, eptr, elen, PERL_QUOTEDPREFIX_LEN,
12523 NULL, NULL, flags);
12524 eptr = SvPV_const(tmp_sv, elen);
12526 if (has_precis && precis < elen)
12536 * We want to extend the C level sprintf format API with
12537 * custom formats for specific types (eg SV*) and behavior.
12538 * However some C compilers are "sprintf aware" and will
12539 * throw compile time exceptions when an illegal sprintf is
12540 * encountered, so we can't just add new format letters.
12542 * However it turns out the length argument to the %p format
12543 * is more or less useless (the size of a pointer does not
12544 * change over time) and is not really used in the C level
12545 * code. Accordingly we can map our special behavior to
12546 * specific "length" options to the %p format. We hide these
12547 * mappings behind defines anyway, so nobody needs to know
12548 * that HEKf is actually %2p. This keeps the C compiler
12549 * happy while allowing us to add new formats.
12551 * Note the existing logic for which number is used for what
12552 * is torturous. All negative values are used for SVf, and
12553 * non-negative values have arbitrary meanings with no
12554 * structure to them. This may change in the future.
12556 * NEVER use the raw %p values directly. Always use the define
12557 * as the underlying mapping may change in the future.
12563 * "%...p" is normally treated like "%...x", except that the
12564 * number to print is the SV's address (or a pointer address
12565 * for C-ish sprintf).
12567 * However, the C-ish sprintf variant allows a few special
12568 * extensions. These are currently:
12570 * %-p (SVf) Like %s, but gets the string from an SV*
12571 * arg rather than a char* arg. Use C<SVfARG()>
12572 * to set up the argument properly.
12573 * (This was previously %_).
12575 * %-<num>p Ditto but like %.<num>s (i.e. num is max
12576 * width), there is no escaped and quoted version
12579 * %1p (PVf_QUOTEDPREFIX). Like raw %s, but it is escaped
12582 * %5p (SVf_QUOTEDPREFIX) Like SVf, but length restricted,
12583 * escaped and quoted with pv_pretty. Intended
12584 * for error messages.
12586 * %2p (HEKf) Like %s, but using the key string in a HEK
12587 * %7p (HEKf_QUOTEDPREFIX) ... but escaped and quoted.
12589 * %3p (HEKf256) Ditto but like %.256s
12590 * %8p (HEKf256_QUOTEDPREFIX) ... but escaped and quoted
12592 * %d%lu%4p (UTF8f) A utf8 string. Consumes 3 args:
12593 * (cBOOL(utf8), len, string_buf).
12594 * It's handled by the "case 'd'" branch
12595 * rather than here.
12596 * %d%lu%9p (UTF8f_QUOTEDPREFIX) .. but escaped and quoted.
12599 * %<num>p where num is > 9: reserved for future
12600 * extensions. Warns, but then is treated as a
12601 * general %p (print hex address) format.
12603 * NOTE: If you add a new magic %p value you will
12604 * need to update F<t/porting/diag.t> to be aware of it
12605 * on top of adding the various defines and etc. Do not
12606 * forget to add it to F<pod/perlguts.pod> as well.
12614 /* not %*p or %*1$p - any width was explicit */
12618 if (left || width == 5) { /* %-p (SVf), %-NNNp, %5p */
12619 if (left && width) {
12622 } else if (width == 5) {
12625 argsv = MUTABLE_SV(va_arg(*args, void*));
12626 eptr = SvPV_const(argsv, elen);
12627 if (DO_UTF8(argsv))
12632 else if (width == 2 || width == 3 ||
12633 width == 7 || width == 8)
12634 { /* HEKf, HEKf256, HEKf_QUOTEDPREFIX, HEKf256_QUOTEDPREFIX */
12635 HEK * const hek = va_arg(*args, HEK *);
12636 eptr = HEK_KEY(hek);
12637 elen = HEK_LEN(hek);
12649 else if (width == 1) {
12650 eptr = va_arg(*args,char *);
12651 elen = strlen(eptr);
12657 /* note width=4 or width=9 is handled under %d */
12658 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
12659 "internal %%<num>p might conflict with future printf extensions");
12663 /* treat as normal %...p */
12665 uv = PTR2UV(args ? va_arg(*args, void*) : argsv);
12667 c = 'x'; /* in case the format string contains '#' */
12671 /* Ignore any size specifiers, since they're not documented as
12672 * being allowed for %c (ideally we should warn on e.g. '%hc').
12673 * Setting a default intsize, along with a positive
12674 * (which signals unsigned) base, causes, for C-ish use, the
12675 * va_arg to be interpreted as an unsigned int, when it's
12676 * actually signed, which will convert -ve values to high +ve
12677 * values. Note that unlike the libc %c, values > 255 will
12678 * convert to high unicode points rather than being truncated
12679 * to 8 bits. For perlish use, it will do SvUV(argsv), which
12680 * will again convert -ve args to high -ve values.
12683 base = 1; /* special value that indicates we're doing a 'c' */
12684 goto get_int_arg_val;
12693 goto get_int_arg_val;
12696 /* probably just a plain %d, but it might be the start of the
12697 * special UTF8f format, which usually looks something like
12698 * "%d%lu%4p" (the lu may vary by platform) or
12699 * "%d%lu%9p" for an escaped version.
12701 assert((UTF8f)[0] == 'd');
12702 assert((UTF8f)[1] == '%');
12704 if ( args /* UTF8f only valid for C-ish sprintf */
12705 && q == fmtstart + 1 /* plain %d, not %....d */
12706 && patend >= fmtstart + sizeof(UTF8f) - 1 /* long enough */
12708 && strnEQ(q + 1, (UTF8f) + 2, sizeof(UTF8f) - 5)
12709 && q[sizeof(UTF8f)-3] == 'p'
12710 && (q[sizeof(UTF8f)-4] == '4' ||
12711 q[sizeof(UTF8f)-4] == '9'))
12713 /* The argument has already gone through cBOOL, so the cast
12715 if (q[sizeof(UTF8f)-4] == '9')
12717 is_utf8 = (bool)va_arg(*args, int);
12718 elen = va_arg(*args, UV);
12719 /* if utf8 length is larger than 0x7ffff..., then it might
12720 * have been a signed value that wrapped */
12721 if (elen > ((~(STRLEN)0) >> 1)) {
12722 assert(0); /* in DEBUGGING build we want to crash */
12723 elen = 0; /* otherwise we want to treat this as an empty string */
12725 eptr = va_arg(*args, char *);
12726 q += sizeof(UTF8f) - 2;
12733 goto get_int_arg_val;
12744 goto get_int_arg_val;
12749 goto get_int_arg_val;
12760 goto get_int_arg_val;
12775 esignbuf[esignlen++] = plus;
12778 /* initialise the vector string to iterate over */
12780 vecsv = args ? va_arg(*args, SV*) : argsv;
12782 /* if this is a version object, we need to convert
12783 * back into v-string notation and then let the
12784 * vectorize happen normally
12786 if (sv_isobject(vecsv) && sv_derived_from(vecsv, "version")) {
12787 if ( hv_existss(MUTABLE_HV(SvRV(vecsv)), "alpha") ) {
12788 Perl_ck_warner_d(aTHX_ packWARN(WARN_PRINTF),
12789 "vector argument not supported with alpha versions");
12793 vecstr = (U8*)SvPV_const(vecsv,veclen);
12794 vecsv = sv_newmortal();
12795 scan_vstring((char *)vecstr, (char *)vecstr + veclen,
12799 vecstr = (U8*)SvPV_const(vecsv, veclen);
12800 vec_utf8 = DO_UTF8(vecsv);
12802 /* This is the re-entry point for when we're iterating
12803 * over the individual characters of a vector arg */
12806 goto done_valid_conversion;
12808 uv = utf8n_to_uvchr(vecstr, veclen, &ulen,
12818 /* test arg for inf/nan. This can trigger an unwanted
12819 * 'str' overload, so manually force 'num' overload first
12823 if (UNLIKELY(SvAMAGIC(argsv)))
12824 argsv = sv_2num(argsv);
12825 if (UNLIKELY(isinfnansv(argsv)))
12826 goto handle_infnan_argsv;
12830 /* signed int type */
12835 case 'c': iv = (char)va_arg(*args, int); break;
12836 case 'h': iv = (short)va_arg(*args, int); break;
12837 case 'l': iv = va_arg(*args, long); break;
12838 case 'V': iv = va_arg(*args, IV); break;
12839 case 'z': iv = va_arg(*args, SSize_t); break;
12840 #ifdef HAS_PTRDIFF_T
12841 case 't': iv = va_arg(*args, ptrdiff_t); break;
12843 default: iv = va_arg(*args, int); break;
12844 case 'j': iv = (IV) va_arg(*args, PERL_INTMAX_T); break;
12847 iv = va_arg(*args, Quad_t); break;
12854 /* assign to tiv then cast to iv to work around
12855 * 2003 GCC cast bug (gnu.org bugzilla #13488) */
12856 IV tiv = SvIV_nomg(argsv);
12858 case 'c': iv = (char)tiv; break;
12859 case 'h': iv = (short)tiv; break;
12860 case 'l': iv = (long)tiv; break;
12862 default: iv = tiv; break;
12865 iv = (Quad_t)tiv; break;
12872 /* now convert iv to uv */
12876 esignbuf[esignlen++] = plus;
12879 /* Using 0- here to silence bogus warning from MS VC */
12880 uv = (UV) (0 - (UV) iv);
12881 esignbuf[esignlen++] = '-';
12885 /* unsigned int type */
12888 case 'c': uv = (unsigned char)va_arg(*args, unsigned);
12890 case 'h': uv = (unsigned short)va_arg(*args, unsigned);
12892 case 'l': uv = va_arg(*args, unsigned long); break;
12893 case 'V': uv = va_arg(*args, UV); break;
12894 case 'z': uv = va_arg(*args, Size_t); break;
12895 #ifdef HAS_PTRDIFF_T
12896 /* will sign extend, but there is no
12897 * uptrdiff_t, so oh well */
12898 case 't': uv = va_arg(*args, ptrdiff_t); break;
12900 case 'j': uv = (UV) va_arg(*args, PERL_UINTMAX_T); break;
12901 default: uv = va_arg(*args, unsigned); break;
12904 uv = va_arg(*args, Uquad_t); break;
12911 /* assign to tiv then cast to iv to work around
12912 * 2003 GCC cast bug (gnu.org bugzilla #13488) */
12913 UV tuv = SvUV_nomg(argsv);
12915 case 'c': uv = (unsigned char)tuv; break;
12916 case 'h': uv = (unsigned short)tuv; break;
12917 case 'l': uv = (unsigned long)tuv; break;
12919 default: uv = tuv; break;
12922 uv = (Uquad_t)tuv; break;
12933 char *ptr = ebuf + sizeof ebuf;
12940 const char * const p =
12941 (c == 'X') ? PL_hexdigit + 16 : PL_hexdigit;
12946 } while (uv >>= 4);
12947 if (alt && *ptr != '0') {
12948 esignbuf[esignlen++] = '0';
12949 esignbuf[esignlen++] = c; /* 'x' or 'X' */
12956 *--ptr = '0' + dig;
12957 } while (uv >>= 3);
12958 if (alt && *ptr != '0')
12964 *--ptr = '0' + dig;
12965 } while (uv >>= 1);
12966 if (alt && *ptr != '0') {
12967 esignbuf[esignlen++] = '0';
12968 esignbuf[esignlen++] = c; /* 'b' or 'B' */
12973 /* special-case: base 1 indicates a 'c' format:
12974 * we use the common code for extracting a uv,
12975 * but handle that value differently here than
12976 * all the other int types */
12978 (!UVCHR_IS_INVARIANT(uv) && SvUTF8(sv)))
12981 STATIC_ASSERT_STMT(sizeof(ebuf) >= UTF8_MAXBYTES + 1);
12983 elen = uvchr_to_utf8((U8*)eptr, uv) - (U8*)ebuf;
12988 ebuf[0] = (char)uv;
12993 default: /* it had better be ten or less */
12996 *--ptr = '0' + dig;
12997 } while (uv /= base);
13000 elen = (ebuf + sizeof ebuf) - ptr;
13004 zeros = precis - elen;
13005 else if (precis == 0 && elen == 1 && *eptr == '0'
13006 && !(base == 8 && alt)) /* "%#.0o" prints "0" */
13009 /* a precision nullifies the 0 flag. */
13015 /* FLOATING POINT */
13018 c = 'f'; /* maybe %F isn't supported here */
13020 case 'e': case 'E':
13022 case 'g': case 'G':
13023 case 'a': case 'A':
13026 STRLEN float_need; /* what PL_efloatsize needs to become */
13027 bool hexfp; /* hexadecimal floating point? */
13029 vcatpvfn_long_double_t fv;
13032 /* This is evil, but floating point is even more evil */
13034 /* for SV-style calling, we can only get NV
13035 for C-style calling, we assume %f is double;
13036 for simplicity we allow any of %Lf, %llf, %qf for long double
13039 #if defined(USE_QUADMATH)
13044 #if defined(USE_LONG_DOUBLE) || defined(USE_QUADMATH)
13048 /* [perl #20339] - we should accept and ignore %lf rather than die */
13052 #if defined(USE_LONG_DOUBLE) || defined(USE_QUADMATH)
13053 intsize = args ? 0 : 'q';
13057 #if defined(HAS_LONG_DOUBLE)
13070 /* Now we need (long double) if intsize == 'q', else (double). */
13072 /* Note: do not pull NVs off the va_list with va_arg()
13073 * (pull doubles instead) because if you have a build
13074 * with long doubles, you would always be pulling long
13075 * doubles, which would badly break anyone using only
13076 * doubles (i.e. the majority of builds). In other
13077 * words, you cannot mix doubles and long doubles.
13078 * The only case where you can pull off long doubles
13079 * is when the format specifier explicitly asks so with
13081 #ifdef USE_QUADMATH
13082 nv = intsize == 'Q' ? va_arg(*args, NV) :
13083 intsize == 'q' ? va_arg(*args, long double) :
13084 va_arg(*args, double);
13086 #elif LONG_DOUBLESIZE > DOUBLESIZE
13087 if (intsize == 'q') {
13088 fv = va_arg(*args, long double);
13091 nv = va_arg(*args, double);
13092 VCATPVFN_NV_TO_FV(nv, fv);
13095 nv = va_arg(*args, double);
13102 /* we jump here if an int-ish format encountered an
13103 * infinite/Nan argsv. After setting nv/fv, it falls
13104 * into the isinfnan block which follows */
13105 handle_infnan_argsv:
13106 nv = SvNV_nomg(argsv);
13107 VCATPVFN_NV_TO_FV(nv, fv);
13110 if (Perl_isinfnan(nv)) {
13112 Perl_croak(aTHX_ "Cannot printf %" NVgf " with '%c'",
13115 elen = S_infnan_2pv(nv, ebuf, sizeof(ebuf), plus);
13124 /* special-case "%.0f" */
13128 && !(width || left || plus || alt)
13131 && ((eptr = F0convert(nv, ebuf + sizeof ebuf, &elen)))
13135 /* Determine the buffer size needed for the various
13136 * floating-point formats.
13138 * The basic possibilities are:
13141 * %f 1111111.123456789
13142 * %e 1.111111123e+06
13143 * %a 0x1.0f4471f9bp+20
13145 * %g 1.11111112e+15
13147 * where P is the value of the precision in the format, or 6
13148 * if not specified. Note the two possible output formats of
13149 * %g; in both cases the number of significant digits is <=
13152 * For most of the format types the maximum buffer size needed
13153 * is precision, plus: any leading 1 or 0x1, the radix
13154 * point, and an exponent. The difficult one is %f: for a
13155 * large positive exponent it can have many leading digits,
13156 * which needs to be calculated specially. Also %a is slightly
13157 * different in that in the absence of a specified precision,
13158 * it uses as many digits as necessary to distinguish
13159 * different values.
13161 * First, here are the constant bits. For ease of calculation
13162 * we over-estimate the needed buffer size, for example by
13163 * assuming all formats have an exponent and a leading 0x1.
13165 * Also for production use, add a little extra overhead for
13166 * safety's sake. Under debugging don't, as it means we're
13167 * more likely to quickly spot issues during development.
13170 float_need = 1 /* possible unary minus */
13171 + 4 /* "0x1" plus very unlikely carry */
13172 + 1 /* default radix point '.' */
13173 + 2 /* "e-", "p+" etc */
13174 + 6 /* exponent: up to 16383 (quad fp) */
13176 + 20 /* safety net */
13181 /* determine the radix point len, e.g. length(".") in "1.2" */
13182 #ifdef USE_LOCALE_NUMERIC
13183 /* note that we may either explicitly use PL_numeric_radix_sv
13184 * below, or implicitly, via an snprintf() variant.
13185 * Note also things like ps_AF.utf8 which has
13186 * "\N{ARABIC DECIMAL SEPARATOR} as a radix point */
13187 if (! have_in_lc_numeric) {
13188 in_lc_numeric = IN_LC(LC_NUMERIC);
13189 have_in_lc_numeric = TRUE;
13192 if (in_lc_numeric) {
13193 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(TRUE, {
13194 /* this can't wrap unless PL_numeric_radix_sv is a string
13195 * consuming virtually all the 32-bit or 64-bit address
13198 float_need += (SvCUR(PL_numeric_radix_sv) - 1);
13200 /* floating-point formats only get utf8 if the radix point
13201 * is utf8. All other characters in the string are < 128
13202 * and so can be safely appended to both a non-utf8 and utf8
13204 * Note that this will convert the output to utf8 even if
13205 * the radix point didn't get output.
13207 if (SvUTF8(PL_numeric_radix_sv) && !has_utf8) {
13208 sv_utf8_upgrade(sv);
13217 if (isALPHA_FOLD_EQ(c, 'f')) {
13218 /* Determine how many digits before the radix point
13219 * might be emitted. frexp() (or frexpl) has some
13220 * unspecified behaviour for nan/inf/-inf, so lucky we've
13221 * already handled them above */
13223 int i = PERL_INT_MIN;
13224 (void)Perl_frexp((NV)fv, &i);
13225 if (i == PERL_INT_MIN)
13226 Perl_die(aTHX_ "panic: frexp: %" VCATPVFN_FV_GF, fv);
13229 digits = BIT_DIGITS(i);
13230 /* this can't overflow. 'digits' will only be a few
13231 * thousand even for the largest floating-point types.
13232 * And up until now float_need is just some small
13233 * constants plus radix len, which can't be in
13234 * overflow territory unless the radix SV is consuming
13235 * over 1/2 the address space */
13236 assert(float_need < ((STRLEN)~0) - digits);
13237 float_need += digits;
13240 else if (UNLIKELY(isALPHA_FOLD_EQ(c, 'a'))) {
13243 /* %a in the absence of precision may print as many
13244 * digits as needed to represent the entire mantissa
13246 * This estimate seriously overshoots in most cases,
13247 * but better the undershooting. Firstly, all bytes
13248 * of the NV are not mantissa, some of them are
13249 * exponent. Secondly, for the reasonably common
13250 * long doubles case, the "80-bit extended", two
13251 * or six bytes of the NV are unused. Also, we'll
13252 * still pick up an extra +6 from the default
13253 * precision calculation below. */
13255 #ifdef LONGDOUBLE_DOUBLEDOUBLE
13256 /* For the "double double", we need more.
13257 * Since each double has their own exponent, the
13258 * doubles may float (haha) rather far from each
13259 * other, and the number of required bits is much
13260 * larger, up to total of DOUBLEDOUBLE_MAXBITS bits.
13261 * See the definition of DOUBLEDOUBLE_MAXBITS.
13263 * Need 2 hexdigits for each byte. */
13264 (DOUBLEDOUBLE_MAXBITS/8 + 1) * 2;
13266 NVSIZE * 2; /* 2 hexdigits for each byte */
13268 /* see "this can't overflow" comment above */
13269 assert(float_need < ((STRLEN)~0) - digits);
13270 float_need += digits;
13273 /* special-case "%.<number>g" if it will fit in ebuf */
13275 && precis /* See earlier comment about buggy Gconvert
13276 when digits, aka precis, is 0 */
13278 /* check that "%.<number>g" formatting will fit in ebuf */
13279 && sizeof(ebuf) - float_need > precis
13280 /* sizeof(ebuf) - float_need will have wrapped if float_need > sizeof(ebuf). *
13281 * Therefore we should check that float_need < sizeof(ebuf). Normally, we would *
13282 * have run this check first, but that triggers incorrect -Wformat-overflow *
13283 * compilation warnings with some versions of gcc if Gconvert invokes sprintf(). *
13284 * ( See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89161 ) *
13285 * So, instead, we check it next: */
13286 && float_need < sizeof(ebuf)
13287 && !(width || left || plus || alt)
13291 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13292 SNPRINTF_G(fv, ebuf, sizeof(ebuf), precis)
13294 elen = strlen(ebuf);
13301 STRLEN pr = has_precis ? precis : 6; /* known default */
13302 /* this probably can't wrap, since precis is limited
13303 * to 1/4 address space size, but better safe than sorry
13305 if (float_need >= ((STRLEN)~0) - pr)
13306 croak_memory_wrap();
13310 if (float_need < width)
13311 float_need = width;
13313 if (float_need > INT_MAX) {
13314 /* snprintf() returns an int, and we use that return value,
13315 so die horribly if the expected size is too large for int
13317 Perl_croak(aTHX_ "Numeric format result too large");
13320 if (PL_efloatsize <= float_need) {
13321 /* PL_efloatbuf should be at least 1 greater than
13322 * float_need to allow a trailing \0 to be returned by
13323 * snprintf(). If we need to grow, overgrow for the
13324 * benefit of future generations */
13325 const STRLEN extra = 0x20;
13326 if (float_need >= ((STRLEN)~0) - extra)
13327 croak_memory_wrap();
13328 float_need += extra;
13329 Safefree(PL_efloatbuf);
13330 PL_efloatsize = float_need;
13331 Newx(PL_efloatbuf, PL_efloatsize, char);
13332 PL_efloatbuf[0] = '\0';
13335 if (UNLIKELY(hexfp)) {
13336 elen = S_format_hexfp(aTHX_ PL_efloatbuf, PL_efloatsize, c,
13337 nv, fv, has_precis, precis, width,
13338 alt, plus, left, fill, in_lc_numeric);
13341 char *ptr = ebuf + sizeof ebuf;
13344 #if defined(USE_QUADMATH)
13345 /* always use Q here. my_snprint() throws an exception if we
13346 fallthrough to the double/long double code, even when the
13347 format is correct, presumably to avoid any accidentally
13351 /* FIXME: what to do if HAS_LONG_DOUBLE but not PERL_PRIfldbl? */
13352 #elif defined(HAS_LONG_DOUBLE) && defined(PERL_PRIfldbl)
13353 /* Note that this is HAS_LONG_DOUBLE and PERL_PRIfldbl,
13354 * not USE_LONG_DOUBLE and NVff. In other words,
13355 * this needs to work without USE_LONG_DOUBLE. */
13356 if (intsize == 'q') {
13357 /* Copy the one or more characters in a long double
13358 * format before the 'base' ([efgEFG]) character to
13359 * the format string. */
13360 static char const ldblf[] = PERL_PRIfldbl;
13361 char const *p = ldblf + sizeof(ldblf) - 3;
13362 while (p >= ldblf) { *--ptr = *p--; }
13367 do { *--ptr = '0' + (base % 10); } while (base /= 10);
13372 do { *--ptr = '0' + (base % 10); } while (base /= 10);
13384 /* No taint. Otherwise we are in the strange situation
13385 * where printf() taints but print($float) doesn't.
13388 /* hopefully the above makes ptr a very constrained format
13389 * that is safe to use, even though it's not literal */
13390 GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral);
13391 #ifdef USE_QUADMATH
13393 if (!quadmath_format_valid(ptr))
13394 Perl_croak_nocontext("panic: quadmath invalid format \"%s\"", ptr);
13395 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13396 elen = quadmath_snprintf(PL_efloatbuf, PL_efloatsize,
13399 if ((IV)elen == -1) {
13400 Perl_croak_nocontext("panic: quadmath_snprintf failed, format \"%s\"", ptr);
13403 #elif defined(HAS_LONG_DOUBLE)
13404 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13405 elen = ((intsize == 'q')
13406 ? my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, fv)
13407 : my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, (double)fv))
13410 WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric,
13411 elen = my_snprintf(PL_efloatbuf, PL_efloatsize, ptr, fv)
13414 GCC_DIAG_RESTORE_STMT;
13417 eptr = PL_efloatbuf;
13421 /* Since floating-point formats do their own formatting and
13422 * padding, we skip the main block of code at the end of this
13423 * loop which handles appending eptr to sv, and do our own
13424 * stripped-down version */
13429 assert(elen >= width);
13431 S_sv_catpvn_simple(aTHX_ sv, eptr, elen);
13433 goto done_valid_conversion;
13441 /* XXX ideally we should warn if any flags etc have been
13442 * set, e.g. "%-4.5n" */
13443 /* XXX if sv was originally non-utf8 with a char in the
13444 * range 0x80-0xff, then if it got upgraded, we should
13445 * calculate char len rather than byte len here */
13446 len = SvCUR(sv) - origlen;
13448 int i = (len > PERL_INT_MAX) ? PERL_INT_MAX : (int)len;
13451 case 'c': *(va_arg(*args, char*)) = i; break;
13452 case 'h': *(va_arg(*args, short*)) = i; break;
13453 default: *(va_arg(*args, int*)) = i; break;
13454 case 'l': *(va_arg(*args, long*)) = i; break;
13455 case 'V': *(va_arg(*args, IV*)) = i; break;
13456 case 'z': *(va_arg(*args, SSize_t*)) = i; break;
13457 #ifdef HAS_PTRDIFF_T
13458 case 't': *(va_arg(*args, ptrdiff_t*)) = i; break;
13460 case 'j': *(va_arg(*args, PERL_INTMAX_T*)) = i; break;
13463 *(va_arg(*args, Quad_t*)) = i; break;
13471 Perl_croak_nocontext(
13472 "Missing argument for %%n in %s",
13473 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
13474 sv_setuv_mg(argsv, has_utf8
13475 ? (UV)utf8_length((U8*)SvPVX(sv), (U8*)SvEND(sv))
13478 goto done_valid_conversion;
13486 && (PL_op->op_type == OP_PRTF || PL_op->op_type == OP_SPRINTF)
13487 && ckWARN(WARN_PRINTF))
13489 SV * const msg = sv_newmortal();
13490 Perl_sv_setpvf(aTHX_ msg, "Invalid conversion in %sprintf: ",
13491 (PL_op->op_type == OP_PRTF) ? "" : "s");
13492 if (fmtstart < patend) {
13493 const char * const fmtend = q < patend ? q : patend;
13495 sv_catpvs(msg, "\"%");
13496 for (f = fmtstart; f < fmtend; f++) {
13498 sv_catpvn_nomg(msg, f, 1);
13500 Perl_sv_catpvf(aTHX_ msg, "\\%03o", (U8) *f);
13503 sv_catpvs(msg, "\"");
13505 sv_catpvs(msg, "end of string");
13507 Perl_warner(aTHX_ packWARN(WARN_PRINTF), "%" SVf, SVfARG(msg)); /* yes, this is reentrant */
13510 /* mangled format: output the '%', then continue from the
13511 * character following that */
13512 sv_catpvn_nomg(sv, fmtstart-1, 1);
13515 /* Any "redundant arg" warning from now onwards will probably
13516 * just be misleading, so don't bother. */
13517 no_redundant_warning = TRUE;
13518 continue; /* not "break" */
13521 if (is_utf8 != has_utf8) {
13524 sv_utf8_upgrade(sv);
13527 const STRLEN old_elen = elen;
13528 SV * const nsv = newSVpvn_flags(eptr, elen, SVs_TEMP);
13529 sv_utf8_upgrade(nsv);
13530 eptr = SvPVX_const(nsv);
13533 if (width) { /* fudge width (can't fudge elen) */
13534 width += elen - old_elen;
13541 /* append esignbuf, filler, zeros, eptr and dotstr to sv */
13544 STRLEN need, have, gap;
13548 /* signed value that's wrapped? */
13549 assert(elen <= ((~(STRLEN)0) >> 1));
13551 /* if zeros is non-zero, then it represents filler between
13552 * elen and precis. So adding elen and zeros together will
13553 * always be <= precis, and the addition can never wrap */
13554 assert(!zeros || (precis > elen && precis - elen == zeros));
13555 have = elen + zeros;
13557 if (have >= (((STRLEN)~0) - esignlen))
13558 croak_memory_wrap();
13561 need = (have > width ? have : width);
13564 if (need >= (((STRLEN)~0) - (SvCUR(sv) + 1)))
13565 croak_memory_wrap();
13566 need += (SvCUR(sv) + 1);
13573 for (i = 0; i < esignlen; i++)
13574 *s++ = esignbuf[i];
13575 for (i = zeros; i; i--)
13577 Copy(eptr, s, elen, char);
13579 for (i = gap; i; i--)
13584 for (i = 0; i < esignlen; i++)
13585 *s++ = esignbuf[i];
13590 for (i = gap; i; i--)
13592 for (i = 0; i < esignlen; i++)
13593 *s++ = esignbuf[i];
13596 for (i = zeros; i; i--)
13598 Copy(eptr, s, elen, char);
13603 SvCUR_set(sv, s - SvPVX_const(sv));
13611 if (vectorize && veclen) {
13612 /* we append the vector separator separately since %v isn't
13613 * very common: don't slow down the general case by adding
13614 * dotstrlen to need etc */
13615 sv_catpvn_nomg(sv, dotstr, dotstrlen);
13617 goto vector; /* do next iteration */
13620 done_valid_conversion:
13623 S_warn_vcatpvfn_missing_argument(aTHX);
13626 /* Now that we've consumed all our printf format arguments (svix)
13627 * do we have things left on the stack that we didn't use?
13629 if (!no_redundant_warning && sv_count >= svix + 1 && ckWARN(WARN_REDUNDANT)) {
13630 Perl_warner(aTHX_ packWARN(WARN_REDUNDANT), "Redundant argument in %s",
13631 PL_op ? OP_DESC(PL_op) : "sv_vcatpvfn()");
13634 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
13635 /* while we shouldn't set the cache, it may have been previously
13636 set in the caller, so clear it */
13637 MAGIC *mg = mg_find(sv, PERL_MAGIC_utf8);
13639 magic_setutf8(sv,mg); /* clear UTF8 cache */
13644 /* =========================================================================
13646 =for apidoc_section $embedding
13650 All the macros and functions in this section are for the private use of
13651 the main function, perl_clone().
13653 The foo_dup() functions make an exact copy of an existing foo thingy.
13654 During the course of a cloning, a hash table is used to map old addresses
13655 to new addresses. The table is created and manipulated with the
13656 ptr_table_* functions.
13658 * =========================================================================*/
13661 #if defined(USE_ITHREADS)
13663 /* XXX Remove this so it doesn't have to go thru the macro and return for nothing */
13664 #ifndef GpREFCNT_inc
13665 # define GpREFCNT_inc(gp) ((gp) ? (++(gp)->gp_refcnt, (gp)) : (GP*)NULL)
13669 /* Certain cases in Perl_ss_dup have been merged, by relying on the fact
13670 that currently av_dup, gv_dup and hv_dup are the same as sv_dup.
13671 If this changes, please unmerge ss_dup.
13672 Likewise, sv_dup_inc_multiple() relies on this fact. */
13673 #define sv_dup_inc_NN(s,t) SvREFCNT_inc_NN(sv_dup_inc(s,t))
13674 #define av_dup(s,t) MUTABLE_AV(sv_dup((const SV *)s,t))
13675 #define av_dup_inc(s,t) MUTABLE_AV(sv_dup_inc((const SV *)s,t))
13676 #define hv_dup(s,t) MUTABLE_HV(sv_dup((const SV *)s,t))
13677 #define hv_dup_inc(s,t) MUTABLE_HV(sv_dup_inc((const SV *)s,t))
13678 #define cv_dup(s,t) MUTABLE_CV(sv_dup((const SV *)s,t))
13679 #define cv_dup_inc(s,t) MUTABLE_CV(sv_dup_inc((const SV *)s,t))
13680 #define io_dup(s,t) MUTABLE_IO(sv_dup((const SV *)s,t))
13681 #define io_dup_inc(s,t) MUTABLE_IO(sv_dup_inc((const SV *)s,t))
13682 #define gv_dup(s,t) MUTABLE_GV(sv_dup((const SV *)s,t))
13683 #define gv_dup_inc(s,t) MUTABLE_GV(sv_dup_inc((const SV *)s,t))
13684 #define SAVEPV(p) ((p) ? savepv(p) : NULL)
13685 #define SAVEPVN(p,n) ((p) ? savepvn(p,n) : NULL)
13687 /* clone a parser */
13690 Perl_parser_dup(pTHX_ const yy_parser *const proto, CLONE_PARAMS *const param)
13694 PERL_ARGS_ASSERT_PARSER_DUP;
13699 /* look for it in the table first */
13700 parser = (yy_parser *)ptr_table_fetch(PL_ptr_table, proto);
13704 /* create anew and remember what it is */
13705 Newxz(parser, 1, yy_parser);
13706 ptr_table_store(PL_ptr_table, proto, parser);
13708 /* XXX eventually, just Copy() most of the parser struct ? */
13710 parser->lex_brackets = proto->lex_brackets;
13711 parser->lex_casemods = proto->lex_casemods;
13712 parser->lex_brackstack = savepvn(proto->lex_brackstack,
13713 (proto->lex_brackets < 120 ? 120 : proto->lex_brackets));
13714 parser->lex_casestack = savepvn(proto->lex_casestack,
13715 (proto->lex_casemods < 12 ? 12 : proto->lex_casemods));
13716 parser->lex_defer = proto->lex_defer;
13717 parser->lex_dojoin = proto->lex_dojoin;
13718 parser->lex_formbrack = proto->lex_formbrack;
13719 parser->lex_inpat = proto->lex_inpat;
13720 parser->lex_inwhat = proto->lex_inwhat;
13721 parser->lex_op = proto->lex_op;
13722 parser->lex_repl = sv_dup_inc(proto->lex_repl, param);
13723 parser->lex_starts = proto->lex_starts;
13724 parser->lex_stuff = sv_dup_inc(proto->lex_stuff, param);
13725 parser->multi_close = proto->multi_close;
13726 parser->multi_open = proto->multi_open;
13727 parser->multi_start = proto->multi_start;
13728 parser->multi_end = proto->multi_end;
13729 parser->preambled = proto->preambled;
13730 parser->lex_super_state = proto->lex_super_state;
13731 parser->lex_sub_inwhat = proto->lex_sub_inwhat;
13732 parser->lex_sub_op = proto->lex_sub_op;
13733 parser->lex_sub_repl= sv_dup_inc(proto->lex_sub_repl, param);
13734 parser->linestr = sv_dup_inc(proto->linestr, param);
13735 parser->expect = proto->expect;
13736 parser->copline = proto->copline;
13737 parser->last_lop_op = proto->last_lop_op;
13738 parser->lex_state = proto->lex_state;
13739 parser->rsfp = fp_dup(proto->rsfp, '<', param);
13740 /* rsfp_filters entries have fake IoDIRP() */
13741 parser->rsfp_filters= av_dup_inc(proto->rsfp_filters, param);
13742 parser->in_my = proto->in_my;
13743 parser->in_my_stash = hv_dup(proto->in_my_stash, param);
13744 parser->error_count = proto->error_count;
13745 parser->sig_elems = proto->sig_elems;
13746 parser->sig_optelems= proto->sig_optelems;
13747 parser->sig_slurpy = proto->sig_slurpy;
13748 parser->recheck_utf8_validity = proto->recheck_utf8_validity;
13751 char * const ols = SvPVX(proto->linestr);
13752 char * const ls = SvPVX(parser->linestr);
13754 parser->bufptr = ls + (proto->bufptr >= ols ?
13755 proto->bufptr - ols : 0);
13756 parser->oldbufptr = ls + (proto->oldbufptr >= ols ?
13757 proto->oldbufptr - ols : 0);
13758 parser->oldoldbufptr= ls + (proto->oldoldbufptr >= ols ?
13759 proto->oldoldbufptr - ols : 0);
13760 parser->linestart = ls + (proto->linestart >= ols ?
13761 proto->linestart - ols : 0);
13762 parser->last_uni = ls + (proto->last_uni >= ols ?
13763 proto->last_uni - ols : 0);
13764 parser->last_lop = ls + (proto->last_lop >= ols ?
13765 proto->last_lop - ols : 0);
13767 parser->bufend = ls + SvCUR(parser->linestr);
13770 Copy(proto->tokenbuf, parser->tokenbuf, 256, char);
13773 Copy(proto->nextval, parser->nextval, 5, YYSTYPE);
13774 Copy(proto->nexttype, parser->nexttype, 5, I32);
13775 parser->nexttoke = proto->nexttoke;
13777 /* XXX should clone saved_curcop here, but we aren't passed
13778 * proto_perl; so do it in perl_clone_using instead */
13784 =for apidoc_section $io
13787 Duplicate a file handle, returning a pointer to the cloned object.
13793 Perl_fp_dup(pTHX_ PerlIO *const fp, const char type, CLONE_PARAMS *const param)
13797 PERL_ARGS_ASSERT_FP_DUP;
13798 PERL_UNUSED_ARG(type);
13801 return (PerlIO*)NULL;
13803 /* look for it in the table first */
13804 ret = (PerlIO*)ptr_table_fetch(PL_ptr_table, fp);
13808 /* create anew and remember what it is */
13809 #ifdef __amigaos4__
13810 ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE|PERLIO_DUP_FD);
13812 ret = PerlIO_fdupopen(aTHX_ fp, param, PERLIO_DUP_CLONE);
13814 ptr_table_store(PL_ptr_table, fp, ret);
13819 =for apidoc_section $io
13820 =for apidoc dirp_dup
13822 Duplicate a directory handle, returning a pointer to the cloned object.
13828 Perl_dirp_dup(pTHX_ DIR *const dp, CLONE_PARAMS *const param)
13832 #if defined(HAS_FCHDIR) && defined(HAS_TELLDIR) && defined(HAS_SEEKDIR)
13834 const Direntry_t *dirent;
13835 char smallbuf[256]; /* XXX MAXPATHLEN, surely? */
13841 PERL_UNUSED_CONTEXT;
13842 PERL_ARGS_ASSERT_DIRP_DUP;
13847 /* look for it in the table first */
13848 ret = (DIR*)ptr_table_fetch(PL_ptr_table, dp);
13852 #if defined(HAS_FCHDIR) && defined(HAS_TELLDIR) && defined(HAS_SEEKDIR)
13854 PERL_UNUSED_ARG(param);
13858 /* open the current directory (so we can switch back) */
13859 if (!(pwd = PerlDir_open("."))) return (DIR *)NULL;
13861 /* chdir to our dir handle and open the present working directory */
13862 if (fchdir(my_dirfd(dp)) < 0 || !(ret = PerlDir_open("."))) {
13863 PerlDir_close(pwd);
13864 return (DIR *)NULL;
13866 /* Now we should have two dir handles pointing to the same dir. */
13868 /* Be nice to the calling code and chdir back to where we were. */
13869 /* XXX If this fails, then what? */
13870 PERL_UNUSED_RESULT(fchdir(my_dirfd(pwd)));
13872 /* We have no need of the pwd handle any more. */
13873 PerlDir_close(pwd);
13876 # define d_namlen(d) (d)->d_namlen
13878 # define d_namlen(d) strlen((d)->d_name)
13880 /* Iterate once through dp, to get the file name at the current posi-
13881 tion. Then step back. */
13882 pos = PerlDir_tell(dp);
13883 if ((dirent = PerlDir_read(dp))) {
13884 len = d_namlen(dirent);
13885 if (len > sizeof(dirent->d_name) && sizeof(dirent->d_name) > PTRSIZE) {
13886 /* If the len is somehow magically longer than the
13887 * maximum length of the directory entry, even though
13888 * we could fit it in a buffer, we could not copy it
13889 * from the dirent. Bail out. */
13890 PerlDir_close(ret);
13893 if (len <= sizeof smallbuf) name = smallbuf;
13894 else Newx(name, len, char);
13895 Move(dirent->d_name, name, len, char);
13897 PerlDir_seek(dp, pos);
13899 /* Iterate through the new dir handle, till we find a file with the
13901 if (!dirent) /* just before the end */
13903 pos = PerlDir_tell(ret);
13904 if (PerlDir_read(ret)) continue; /* not there yet */
13905 PerlDir_seek(ret, pos); /* step back */
13909 const long pos0 = PerlDir_tell(ret);
13911 pos = PerlDir_tell(ret);
13912 if ((dirent = PerlDir_read(ret))) {
13913 if (len == (STRLEN)d_namlen(dirent)
13914 && memEQ(name, dirent->d_name, len)) {
13916 PerlDir_seek(ret, pos); /* step back */
13919 /* else we are not there yet; keep iterating */
13921 else { /* This is not meant to happen. The best we can do is
13922 reset the iterator to the beginning. */
13923 PerlDir_seek(ret, pos0);
13930 if (name && name != smallbuf)
13935 ret = win32_dirp_dup(dp, param);
13938 /* pop it in the pointer table */
13940 ptr_table_store(PL_ptr_table, dp, ret);
13946 =for apidoc_section $GV
13949 Duplicate a typeglob, returning a pointer to the cloned object.
13955 Perl_gp_dup(pTHX_ GP *const gp, CLONE_PARAMS *const param)
13959 PERL_ARGS_ASSERT_GP_DUP;
13963 /* look for it in the table first */
13964 ret = (GP*)ptr_table_fetch(PL_ptr_table, gp);
13968 /* create anew and remember what it is */
13970 ptr_table_store(PL_ptr_table, gp, ret);
13973 /* ret->gp_refcnt must be 0 before any other dups are called. We're relying
13974 on Newxz() to do this for us. */
13975 ret->gp_sv = sv_dup_inc(gp->gp_sv, param);
13976 ret->gp_io = io_dup_inc(gp->gp_io, param);
13977 ret->gp_form = cv_dup_inc(gp->gp_form, param);
13978 ret->gp_av = av_dup_inc(gp->gp_av, param);
13979 ret->gp_hv = hv_dup_inc(gp->gp_hv, param);
13980 ret->gp_egv = gv_dup(gp->gp_egv, param);/* GvEGV is not refcounted */
13981 ret->gp_cv = cv_dup_inc(gp->gp_cv, param);
13982 ret->gp_cvgen = gp->gp_cvgen;
13983 ret->gp_line = gp->gp_line;
13984 ret->gp_file_hek = hek_dup(gp->gp_file_hek, param);
13990 =for apidoc_section $magic
13993 Duplicate a chain of magic, returning a pointer to the cloned object.
13999 Perl_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *const param)
14001 MAGIC *mgret = NULL;
14002 MAGIC **mgprev_p = &mgret;
14004 PERL_ARGS_ASSERT_MG_DUP;
14006 for (; mg; mg = mg->mg_moremagic) {
14009 if ((param->flags & CLONEf_JOIN_IN)
14010 && mg->mg_type == PERL_MAGIC_backref)
14011 /* when joining, we let the individual SVs add themselves to
14012 * backref as needed. */
14015 Newx(nmg, 1, MAGIC);
14017 mgprev_p = &(nmg->mg_moremagic);
14019 /* There was a comment "XXX copy dynamic vtable?" but as we don't have
14020 dynamic vtables, I'm not sure why Sarathy wrote it. The comment dates
14021 from the original commit adding Perl_mg_dup() - revision 4538.
14022 Similarly there is the annotation "XXX random ptr?" next to the
14023 assignment to nmg->mg_ptr. */
14026 /* FIXME for plugins
14027 if (nmg->mg_type == PERL_MAGIC_qr) {
14028 nmg->mg_obj = MUTABLE_SV(CALLREGDUPE((REGEXP*)nmg->mg_obj, param));
14032 nmg->mg_obj = (nmg->mg_flags & MGf_REFCOUNTED)
14033 ? nmg->mg_type == PERL_MAGIC_backref
14034 /* The backref AV has its reference
14035 * count deliberately bumped by 1 */
14036 ? SvREFCNT_inc(av_dup_inc((const AV *)
14037 nmg->mg_obj, param))
14038 : sv_dup_inc(nmg->mg_obj, param)
14039 : (nmg->mg_type == PERL_MAGIC_regdatum ||
14040 nmg->mg_type == PERL_MAGIC_regdata)
14042 : sv_dup(nmg->mg_obj, param);
14044 if (nmg->mg_ptr && nmg->mg_type != PERL_MAGIC_regex_global) {
14045 if (nmg->mg_len > 0) {
14046 nmg->mg_ptr = SAVEPVN(nmg->mg_ptr, nmg->mg_len);
14047 if (nmg->mg_type == PERL_MAGIC_overload_table &&
14048 AMT_AMAGIC((AMT*)nmg->mg_ptr))
14050 AMT * const namtp = (AMT*)nmg->mg_ptr;
14051 sv_dup_inc_multiple((SV**)(namtp->table),
14052 (SV**)(namtp->table), NofAMmeth, param);
14055 else if (nmg->mg_len == HEf_SVKEY)
14056 nmg->mg_ptr = (char*)sv_dup_inc((const SV *)nmg->mg_ptr, param);
14058 if ((nmg->mg_flags & MGf_DUP) && nmg->mg_virtual && nmg->mg_virtual->svt_dup) {
14059 nmg->mg_virtual->svt_dup(aTHX_ nmg, param);
14065 #endif /* USE_ITHREADS */
14067 struct ptr_tbl_arena {
14068 struct ptr_tbl_arena *next;
14069 struct ptr_tbl_ent array[1023/3]; /* as ptr_tbl_ent has 3 pointers. */
14073 =for apidoc ptr_table_new
14075 Create a new pointer-mapping table
14081 Perl_ptr_table_new(pTHX)
14084 PERL_UNUSED_CONTEXT;
14086 Newx(tbl, 1, PTR_TBL_t);
14087 tbl->tbl_max = 511;
14088 tbl->tbl_items = 0;
14089 tbl->tbl_arena = NULL;
14090 tbl->tbl_arena_next = NULL;
14091 tbl->tbl_arena_end = NULL;
14092 Newxz(tbl->tbl_ary, tbl->tbl_max + 1, PTR_TBL_ENT_t*);
14096 #define PTR_TABLE_HASH(ptr) \
14097 ((PTR2UV(ptr) >> 3) ^ (PTR2UV(ptr) >> (3 + 7)) ^ (PTR2UV(ptr) >> (3 + 17)))
14099 /* map an existing pointer using a table */
14101 STATIC PTR_TBL_ENT_t *
14102 S_ptr_table_find(PTR_TBL_t *const tbl, const void *const sv)
14104 PTR_TBL_ENT_t *tblent;
14105 const UV hash = PTR_TABLE_HASH(sv);
14107 PERL_ARGS_ASSERT_PTR_TABLE_FIND;
14109 tblent = tbl->tbl_ary[hash & tbl->tbl_max];
14110 for (; tblent; tblent = tblent->next) {
14111 if (tblent->oldval == sv)
14118 =for apidoc ptr_table_fetch
14120 Look for C<sv> in the pointer-mapping table C<tbl>, returning its value, or
14127 Perl_ptr_table_fetch(pTHX_ PTR_TBL_t *const tbl, const void *const sv)
14129 PTR_TBL_ENT_t const *const tblent = ptr_table_find(tbl, sv);
14131 PERL_ARGS_ASSERT_PTR_TABLE_FETCH;
14132 PERL_UNUSED_CONTEXT;
14134 return tblent ? tblent->newval : NULL;
14138 =for apidoc ptr_table_store
14140 Add a new entry to a pointer-mapping table C<tbl>.
14141 In hash terms, C<oldsv> is the key; Cnewsv> is the value.
14143 The names "old" and "new" are specific to the core's typical use of ptr_tables
14150 Perl_ptr_table_store(pTHX_ PTR_TBL_t *const tbl, const void *const oldsv, void *const newsv)
14152 PTR_TBL_ENT_t *tblent = ptr_table_find(tbl, oldsv);
14154 PERL_ARGS_ASSERT_PTR_TABLE_STORE;
14155 PERL_UNUSED_CONTEXT;
14158 tblent->newval = newsv;
14160 const UV entry = PTR_TABLE_HASH(oldsv) & tbl->tbl_max;
14162 if (tbl->tbl_arena_next == tbl->tbl_arena_end) {
14163 struct ptr_tbl_arena *new_arena;
14165 Newx(new_arena, 1, struct ptr_tbl_arena);
14166 new_arena->next = tbl->tbl_arena;
14167 tbl->tbl_arena = new_arena;
14168 tbl->tbl_arena_next = new_arena->array;
14169 tbl->tbl_arena_end = C_ARRAY_END(new_arena->array);
14172 tblent = tbl->tbl_arena_next++;
14174 tblent->oldval = oldsv;
14175 tblent->newval = newsv;
14176 tblent->next = tbl->tbl_ary[entry];
14177 tbl->tbl_ary[entry] = tblent;
14179 if (tblent->next && tbl->tbl_items > tbl->tbl_max)
14180 ptr_table_split(tbl);
14185 =for apidoc ptr_table_split
14187 Double the hash bucket size of an existing ptr table
14193 Perl_ptr_table_split(pTHX_ PTR_TBL_t *const tbl)
14195 PTR_TBL_ENT_t **ary = tbl->tbl_ary;
14196 const UV oldsize = tbl->tbl_max + 1;
14197 UV newsize = oldsize * 2;
14200 PERL_ARGS_ASSERT_PTR_TABLE_SPLIT;
14201 PERL_UNUSED_CONTEXT;
14203 Renew(ary, newsize, PTR_TBL_ENT_t*);
14204 Zero(&ary[oldsize], newsize-oldsize, PTR_TBL_ENT_t*);
14205 tbl->tbl_max = --newsize;
14206 tbl->tbl_ary = ary;
14207 for (i=0; i < oldsize; i++, ary++) {
14208 PTR_TBL_ENT_t **entp = ary;
14209 PTR_TBL_ENT_t *ent = *ary;
14210 PTR_TBL_ENT_t **curentp;
14213 curentp = ary + oldsize;
14215 if ((newsize & PTR_TABLE_HASH(ent->oldval)) != i) {
14217 ent->next = *curentp;
14228 =for apidoc ptr_table_free
14230 Clear and free a ptr table
14236 Perl_ptr_table_free(pTHX_ PTR_TBL_t *const tbl)
14238 struct ptr_tbl_arena *arena;
14240 PERL_UNUSED_CONTEXT;
14246 arena = tbl->tbl_arena;
14249 struct ptr_tbl_arena *next = arena->next;
14255 Safefree(tbl->tbl_ary);
14259 #if defined(USE_ITHREADS)
14262 Perl_rvpv_dup(pTHX_ SV *const dsv, const SV *const ssv, CLONE_PARAMS *const param)
14264 PERL_ARGS_ASSERT_RVPV_DUP;
14266 assert(!isREGEXP(ssv));
14268 if (SvWEAKREF(ssv)) {
14269 SvRV_set(dsv, sv_dup(SvRV_const(ssv), param));
14270 if (param->flags & CLONEf_JOIN_IN) {
14271 /* if joining, we add any back references individually rather
14272 * than copying the whole backref array */
14273 Perl_sv_add_backref(aTHX_ SvRV(dsv), dsv);
14277 SvRV_set(dsv, sv_dup_inc(SvRV_const(ssv), param));
14279 else if (SvPVX_const(ssv)) {
14280 /* Has something there */
14282 /* Normal PV - clone whole allocated space */
14283 SvPV_set(dsv, SAVEPVN(SvPVX_const(ssv), SvLEN(ssv)-1));
14284 /* ssv may not be that normal, but actually copy on write.
14285 But we are a true, independent SV, so: */
14289 /* Special case - not normally malloced for some reason */
14290 if (isGV_with_GP(ssv)) {
14291 /* Don't need to do anything here. */
14293 else if ((SvIsCOW_shared_hash(ssv))) {
14294 /* A "shared" PV - clone it as "shared" PV */
14296 HEK_KEY(hek_dup(SvSHARED_HEK_FROM_PV(SvPVX_const(ssv)),
14300 /* Some other special case - random pointer */
14301 SvPV_set(dsv, (char *) SvPVX_const(ssv));
14306 /* Copy the NULL */
14307 SvPV_set(dsv, NULL);
14311 /* duplicate a list of SVs. source and dest may point to the same memory. */
14313 S_sv_dup_inc_multiple(pTHX_ SV *const *source, SV **dest,
14314 SSize_t items, CLONE_PARAMS *const param)
14316 PERL_ARGS_ASSERT_SV_DUP_INC_MULTIPLE;
14318 while (items-- > 0) {
14319 *dest++ = sv_dup_inc(*source++, param);
14325 /* duplicate an SV of any type (including AV, HV etc) */
14328 S_sv_dup_common(pTHX_ const SV *const ssv, CLONE_PARAMS *const param)
14332 PERL_ARGS_ASSERT_SV_DUP_COMMON;
14334 if (SvTYPE(ssv) == (svtype)SVTYPEMASK) {
14335 #ifdef DEBUG_LEAKING_SCALARS_ABORT
14340 /* look for it in the table first */
14341 dsv = MUTABLE_SV(ptr_table_fetch(PL_ptr_table, ssv));
14345 if(param->flags & CLONEf_JOIN_IN) {
14346 /** We are joining here so we don't want do clone
14347 something that is bad **/
14348 if (SvTYPE(ssv) == SVt_PVHV) {
14349 const HEK * const hvname = HvNAME_HEK(ssv);
14351 /** don't clone stashes if they already exist **/
14352 dsv = MUTABLE_SV(gv_stashpvn(HEK_KEY(hvname), HEK_LEN(hvname),
14353 HEK_UTF8(hvname) ? SVf_UTF8 : 0));
14354 ptr_table_store(PL_ptr_table, ssv, dsv);
14358 else if (SvTYPE(ssv) == SVt_PVGV && !SvFAKE(ssv)) {
14359 HV *stash = GvSTASH(ssv);
14360 const HEK * hvname;
14361 if (stash && (hvname = HvNAME_HEK(stash))) {
14362 /** don't clone GVs if they already exist **/
14364 stash = gv_stashpvn(HEK_KEY(hvname), HEK_LEN(hvname),
14365 HEK_UTF8(hvname) ? SVf_UTF8 : 0);
14367 stash, GvNAME(ssv),
14373 if (svp && *svp && SvTYPE(*svp) == SVt_PVGV) {
14374 ptr_table_store(PL_ptr_table, ssv, *svp);
14381 /* create anew and remember what it is */
14384 #ifdef DEBUG_LEAKING_SCALARS
14385 dsv->sv_debug_optype = ssv->sv_debug_optype;
14386 dsv->sv_debug_line = ssv->sv_debug_line;
14387 dsv->sv_debug_inpad = ssv->sv_debug_inpad;
14388 dsv->sv_debug_parent = (SV*)ssv;
14389 FREE_SV_DEBUG_FILE(dsv);
14390 dsv->sv_debug_file = savesharedpv(ssv->sv_debug_file);
14393 ptr_table_store(PL_ptr_table, ssv, dsv);
14396 SvFLAGS(dsv) = SvFLAGS(ssv);
14397 SvFLAGS(dsv) &= ~SVf_OOK; /* don't propagate OOK hack */
14398 SvREFCNT(dsv) = 0; /* must be before any other dups! */
14401 if (SvANY(ssv) && PL_watch_pvx && SvPVX_const(ssv) == PL_watch_pvx)
14402 PerlIO_printf(Perl_debug_log, "watch at %p hit, found string \"%s\"\n",
14403 (void*)PL_watch_pvx, SvPVX_const(ssv));
14406 /* don't clone objects whose class has asked us not to */
14408 && ! (SvFLAGS(SvSTASH(ssv)) & SVphv_CLONEABLE))
14414 switch (SvTYPE(ssv)) {
14419 SET_SVANY_FOR_BODYLESS_IV(dsv);
14421 Perl_rvpv_dup(aTHX_ dsv, ssv, param);
14423 SvIV_set(dsv, SvIVX(ssv));
14427 #if NVSIZE <= IVSIZE
14428 SET_SVANY_FOR_BODYLESS_NV(dsv);
14430 SvANY(dsv) = new_XNV();
14432 SvNV_set(dsv, SvNVX(ssv));
14436 /* These are all the types that need complex bodies allocating. */
14438 const svtype sv_type = SvTYPE(ssv);
14439 const struct body_details *sv_type_details
14440 = bodies_by_type + sv_type;
14444 Perl_croak(aTHX_ "Bizarre SvTYPE [%" IVdf "]", (IV)SvTYPE(ssv));
14445 NOT_REACHED; /* NOTREACHED */
14449 if (HvHasAUX(ssv)) {
14450 sv_type_details = &fake_hv_with_aux;
14452 new_body = new_NOARENA(sv_type_details);
14454 new_body_from_arena(new_body, HVAUX_ARENA_ROOT_IX, fake_hv_with_aux);
14471 assert(sv_type_details->body_size);
14473 if (sv_type_details->arena) {
14474 new_body = S_new_body(aTHX_ sv_type);
14476 = (void*)((char*)new_body - sv_type_details->offset);
14480 new_body = new_NOARENA(sv_type_details);
14485 SvANY(dsv) = new_body;
14488 Copy(((char*)SvANY(ssv)) + sv_type_details->offset,
14489 ((char*)SvANY(dsv)) + sv_type_details->offset,
14490 sv_type_details->copy, char);
14492 Copy(((char*)SvANY(ssv)),
14493 ((char*)SvANY(dsv)),
14494 sv_type_details->body_size + sv_type_details->offset, char);
14497 if (sv_type != SVt_PVAV && sv_type != SVt_PVHV
14498 && !isGV_with_GP(dsv)
14500 && !(sv_type == SVt_PVIO && !(IoFLAGS(dsv) & IOf_FAKE_DIRP)))
14501 Perl_rvpv_dup(aTHX_ dsv, ssv, param);
14503 /* The Copy above means that all the source (unduplicated) pointers
14504 are now in the destination. We can check the flags and the
14505 pointers in either, but it's possible that there's less cache
14506 missing by always going for the destination.
14507 FIXME - instrument and check that assumption */
14508 if (sv_type >= SVt_PVMG) {
14510 SvMAGIC_set(dsv, mg_dup(SvMAGIC(dsv), param));
14511 if (SvOBJECT(dsv) && SvSTASH(dsv))
14512 SvSTASH_set(dsv, hv_dup_inc(SvSTASH(dsv), param));
14513 else SvSTASH_set(dsv, 0); /* don't copy DESTROY cache */
14516 /* The cast silences a GCC warning about unhandled types. */
14517 switch ((int)sv_type) {
14528 /* FIXME for plugins */
14529 re_dup_guts((REGEXP*) ssv, (REGEXP*) dsv, param);
14532 /* XXX LvTARGOFF sometimes holds PMOP* when DEBUGGING */
14533 if (LvTYPE(dsv) == 't') /* for tie: unrefcnted fake (SV**) */
14535 else if (LvTYPE(dsv) == 'T') /* for tie: fake HE */
14536 LvTARG(dsv) = MUTABLE_SV(he_dup((HE*)LvTARG(dsv), FALSE, param));
14538 LvTARG(dsv) = sv_dup_inc(LvTARG(dsv), param);
14539 if (isREGEXP(ssv)) goto duprex;
14542 /* non-GP case already handled above */
14543 if(isGV_with_GP(ssv)) {
14544 GvNAME_HEK(dsv) = hek_dup(GvNAME_HEK(dsv), param);
14545 /* Don't call sv_add_backref here as it's going to be
14546 created as part of the magic cloning of the symbol
14547 table--unless this is during a join and the stash
14548 is not actually being cloned. */
14549 /* Danger Will Robinson - GvGP(dsv) isn't initialised
14550 at the point of this comment. */
14551 GvSTASH(dsv) = hv_dup(GvSTASH(dsv), param);
14552 if (param->flags & CLONEf_JOIN_IN)
14553 Perl_sv_add_backref(aTHX_ MUTABLE_SV(GvSTASH(dsv)), dsv);
14554 GvGP_set(dsv, gp_dup(GvGP(ssv), param));
14555 (void)GpREFCNT_inc(GvGP(dsv));
14559 /* PL_parser->rsfp_filters entries have fake IoDIRP() */
14560 if(IoFLAGS(dsv) & IOf_FAKE_DIRP) {
14561 /* I have no idea why fake dirp (rsfps)
14562 should be treated differently but otherwise
14563 we end up with leaks -- sky*/
14564 IoTOP_GV(dsv) = gv_dup_inc(IoTOP_GV(dsv), param);
14565 IoFMT_GV(dsv) = gv_dup_inc(IoFMT_GV(dsv), param);
14566 IoBOTTOM_GV(dsv) = gv_dup_inc(IoBOTTOM_GV(dsv), param);
14568 IoTOP_GV(dsv) = gv_dup(IoTOP_GV(dsv), param);
14569 IoFMT_GV(dsv) = gv_dup(IoFMT_GV(dsv), param);
14570 IoBOTTOM_GV(dsv) = gv_dup(IoBOTTOM_GV(dsv), param);
14572 IoDIRP(dsv) = dirp_dup(IoDIRP(dsv), param);
14575 /* IoDIRP(dsv) is already a copy of IoDIRP(ssv) */
14577 IoIFP(dsv) = fp_dup(IoIFP(ssv), IoTYPE(dsv), param);
14579 if (IoOFP(dsv) == IoIFP(ssv))
14580 IoOFP(dsv) = IoIFP(dsv);
14582 IoOFP(dsv) = fp_dup(IoOFP(dsv), IoTYPE(dsv), param);
14583 IoTOP_NAME(dsv) = SAVEPV(IoTOP_NAME(dsv));
14584 IoFMT_NAME(dsv) = SAVEPV(IoFMT_NAME(dsv));
14585 IoBOTTOM_NAME(dsv) = SAVEPV(IoBOTTOM_NAME(dsv));
14588 /* avoid cloning an empty array */
14589 if (AvARRAY((const AV *)ssv) && AvFILLp((const AV *)ssv) >= 0) {
14590 SV **dst_ary, **src_ary;
14591 SSize_t items = AvFILLp((const AV *)ssv) + 1;
14593 src_ary = AvARRAY((const AV *)ssv);
14594 Newx(dst_ary, AvMAX((const AV *)ssv)+1, SV*);
14595 ptr_table_store(PL_ptr_table, src_ary, dst_ary);
14596 AvARRAY(MUTABLE_AV(dsv)) = dst_ary;
14597 AvALLOC((const AV *)dsv) = dst_ary;
14598 if (AvREAL((const AV *)ssv)) {
14599 dst_ary = sv_dup_inc_multiple(src_ary, dst_ary, items,
14603 while (items-- > 0)
14604 *dst_ary++ = sv_dup(*src_ary++, param);
14606 items = AvMAX((const AV *)ssv) - AvFILLp((const AV *)ssv);
14607 while (items-- > 0) {
14612 AvARRAY(MUTABLE_AV(dsv)) = NULL;
14613 AvALLOC((const AV *)dsv) = (SV**)NULL;
14614 AvMAX( (const AV *)dsv) = -1;
14615 AvFILLp((const AV *)dsv) = -1;
14619 if (HvARRAY((const HV *)ssv)) {
14621 XPVHV * const dxhv = (XPVHV*)SvANY(dsv);
14622 XPVHV * const sxhv = (XPVHV*)SvANY(ssv);
14624 Newx(darray, PERL_HV_ARRAY_ALLOC_BYTES(dxhv->xhv_max+1),
14626 HvARRAY(dsv) = (HE**)darray;
14627 while (i <= sxhv->xhv_max) {
14628 const HE * const source = HvARRAY(ssv)[i];
14629 HvARRAY(dsv)[i] = source
14630 ? he_dup(source, FALSE, param) : 0;
14633 if (HvHasAUX(ssv)) {
14634 const struct xpvhv_aux * const saux = HvAUX(ssv);
14635 struct xpvhv_aux * const daux = HvAUX(dsv);
14636 /* This flag isn't copied. */
14639 if (saux->xhv_name_count) {
14640 HEK ** const sname = saux->xhv_name_u.xhvnameu_names;
14642 = saux->xhv_name_count < 0
14643 ? -saux->xhv_name_count
14644 : saux->xhv_name_count;
14645 HEK **shekp = sname + count;
14647 Newx(daux->xhv_name_u.xhvnameu_names, count, HEK *);
14648 dhekp = daux->xhv_name_u.xhvnameu_names + count;
14649 while (shekp-- > sname) {
14651 *dhekp = hek_dup(*shekp, param);
14655 daux->xhv_name_u.xhvnameu_name
14656 = hek_dup(saux->xhv_name_u.xhvnameu_name,
14659 daux->xhv_name_count = saux->xhv_name_count;
14661 daux->xhv_aux_flags = saux->xhv_aux_flags;
14662 #ifdef PERL_HASH_RANDOMIZE_KEYS
14663 daux->xhv_rand = saux->xhv_rand;
14664 daux->xhv_last_rand = saux->xhv_last_rand;
14666 daux->xhv_riter = saux->xhv_riter;
14667 daux->xhv_eiter = saux->xhv_eiter
14668 ? he_dup(saux->xhv_eiter, FALSE, param) : 0;
14669 /* backref array needs refcnt=2; see sv_add_backref */
14670 daux->xhv_backreferences =
14671 (param->flags & CLONEf_JOIN_IN)
14672 /* when joining, we let the individual GVs and
14673 * CVs add themselves to backref as
14674 * needed. This avoids pulling in stuff
14675 * that isn't required, and simplifies the
14676 * case where stashes aren't cloned back
14677 * if they already exist in the parent
14680 : saux->xhv_backreferences
14681 ? (SvTYPE(saux->xhv_backreferences) == SVt_PVAV)
14682 ? MUTABLE_AV(SvREFCNT_inc(
14683 sv_dup_inc((const SV *)
14684 saux->xhv_backreferences, param)))
14685 : MUTABLE_AV(sv_dup((const SV *)
14686 saux->xhv_backreferences, param))
14689 daux->xhv_mro_meta = saux->xhv_mro_meta
14690 ? mro_meta_dup(saux->xhv_mro_meta, param)
14693 /* Record stashes for possible cloning in Perl_clone(). */
14695 av_push(param->stashes, dsv);
14699 HvARRAY(MUTABLE_HV(dsv)) = NULL;
14702 if (!(param->flags & CLONEf_COPY_STACKS)) {
14707 /* NOTE: not refcounted */
14708 SvANY(MUTABLE_CV(dsv))->xcv_stash =
14709 hv_dup(CvSTASH(dsv), param);
14710 if ((param->flags & CLONEf_JOIN_IN) && CvSTASH(dsv))
14711 Perl_sv_add_backref(aTHX_ MUTABLE_SV(CvSTASH(dsv)), dsv);
14712 if (!CvISXSUB(dsv)) {
14714 CvROOT(dsv) = OpREFCNT_inc(CvROOT(dsv));
14716 CvSLABBED_off(dsv);
14717 } else if (CvCONST(dsv)) {
14718 CvXSUBANY(dsv).any_ptr =
14719 sv_dup_inc((const SV *)CvXSUBANY(dsv).any_ptr, param);
14720 } else if (CvREFCOUNTED_ANYSV(dsv)) {
14721 CvXSUBANY(dsv).any_sv =
14722 sv_dup_inc((const SV *)CvXSUBANY(dsv).any_sv, param);
14724 assert(!CvSLABBED(dsv));
14725 if (CvDYNFILE(dsv)) CvFILE(dsv) = SAVEPV(CvFILE(dsv));
14727 SvANY((CV *)dsv)->xcv_gv_u.xcv_hek =
14728 hek_dup(CvNAME_HEK((CV *)ssv), param);
14729 /* don't dup if copying back - CvGV isn't refcounted, so the
14730 * duped GV may never be freed. A bit of a hack! DAPM */
14732 SvANY(MUTABLE_CV(dsv))->xcv_gv_u.xcv_gv =
14734 ? gv_dup_inc(CvGV(ssv), param)
14735 : (param->flags & CLONEf_JOIN_IN)
14737 : gv_dup(CvGV(ssv), param);
14739 if (!CvISXSUB(ssv)) {
14740 PADLIST * padlist = CvPADLIST(ssv);
14742 padlist = padlist_dup(padlist, param);
14743 CvPADLIST_set(dsv, padlist);
14745 /* unthreaded perl can't sv_dup so we dont support unthreaded's CvHSCXT */
14746 PoisonPADLIST(dsv);
14750 ? cv_dup( CvOUTSIDE(dsv), param)
14751 : cv_dup_inc(CvOUTSIDE(dsv), param);
14761 Perl_sv_dup_inc(pTHX_ const SV *const ssv, CLONE_PARAMS *const param)
14763 PERL_ARGS_ASSERT_SV_DUP_INC;
14764 return ssv ? SvREFCNT_inc(sv_dup_common(ssv, param)) : NULL;
14768 Perl_sv_dup(pTHX_ const SV *const ssv, CLONE_PARAMS *const param)
14770 SV *dsv = ssv ? sv_dup_common(ssv, param) : NULL;
14771 PERL_ARGS_ASSERT_SV_DUP;
14773 /* Track every SV that (at least initially) had a reference count of 0.
14774 We need to do this by holding an actual reference to it in this array.
14775 If we attempt to cheat, turn AvREAL_off(), and store only pointers
14776 (akin to the stashes hash, and the perl stack), we come unstuck if
14777 a weak reference (or other SV legitimately SvREFCNT() == 0 for this
14778 thread) is manipulated in a CLONE method, because CLONE runs before the
14779 unreferenced array is walked to find SVs still with SvREFCNT() == 0
14780 (and fix things up by giving each a reference via the temps stack).
14781 Instead, during CLONE, if the 0-referenced SV has SvREFCNT_inc() and
14782 then SvREFCNT_dec(), it will be cleaned up (and added to the free list)
14783 before the walk of unreferenced happens and a reference to that is SV
14784 added to the temps stack. At which point we have the same SV considered
14785 to be in use, and free to be re-used. Not good.
14787 if (dsv && !(param->flags & CLONEf_COPY_STACKS) && !SvREFCNT(dsv)) {
14788 assert(param->unreferenced);
14789 av_push(param->unreferenced, SvREFCNT_inc(dsv));
14795 /* duplicate a context */
14798 Perl_cx_dup(pTHX_ PERL_CONTEXT *cxs, I32 ix, I32 max, CLONE_PARAMS* param)
14800 PERL_CONTEXT *ncxs;
14802 PERL_ARGS_ASSERT_CX_DUP;
14805 return (PERL_CONTEXT*)NULL;
14807 /* look for it in the table first */
14808 ncxs = (PERL_CONTEXT*)ptr_table_fetch(PL_ptr_table, cxs);
14812 /* create anew and remember what it is */
14813 Newx(ncxs, max + 1, PERL_CONTEXT);
14814 ptr_table_store(PL_ptr_table, cxs, ncxs);
14815 Copy(cxs, ncxs, max + 1, PERL_CONTEXT);
14818 PERL_CONTEXT * const ncx = &ncxs[ix];
14819 if (CxTYPE(ncx) == CXt_SUBST) {
14820 Perl_croak(aTHX_ "Cloning substitution context is unimplemented");
14823 ncx->blk_oldcop = (COP*)any_dup(ncx->blk_oldcop, param->proto_perl);
14824 switch (CxTYPE(ncx)) {
14826 ncx->blk_sub.cv = cv_dup_inc(ncx->blk_sub.cv, param);
14827 if(CxHASARGS(ncx)){
14828 ncx->blk_sub.savearray = av_dup_inc(ncx->blk_sub.savearray,param);
14830 ncx->blk_sub.savearray = NULL;
14832 ncx->blk_sub.prevcomppad = (PAD*)ptr_table_fetch(PL_ptr_table,
14833 ncx->blk_sub.prevcomppad);
14836 ncx->blk_eval.old_namesv = sv_dup_inc(ncx->blk_eval.old_namesv,
14838 /* XXX should this sv_dup_inc? Or only if CxEVAL_TXT_REFCNTED ???? */
14839 ncx->blk_eval.cur_text = sv_dup(ncx->blk_eval.cur_text, param);
14840 ncx->blk_eval.cv = cv_dup(ncx->blk_eval.cv, param);
14841 /* XXX what to do with cur_top_env ???? */
14843 case CXt_LOOP_LAZYSV:
14844 ncx->blk_loop.state_u.lazysv.end
14845 = sv_dup_inc(ncx->blk_loop.state_u.lazysv.end, param);
14846 /* Fallthrough: duplicate lazysv.cur by using the ary.ary
14847 duplication code instead.
14848 We are taking advantage of (1) av_dup_inc and sv_dup_inc
14849 actually being the same function, and (2) order
14850 equivalence of the two unions.
14851 We can assert the later [but only at run time :-(] */
14852 assert ((void *) &ncx->blk_loop.state_u.ary.ary ==
14853 (void *) &ncx->blk_loop.state_u.lazysv.cur);
14856 ncx->blk_loop.state_u.ary.ary
14857 = av_dup_inc(ncx->blk_loop.state_u.ary.ary, param);
14859 case CXt_LOOP_LIST:
14860 case CXt_LOOP_LAZYIV:
14861 /* code common to all 'for' CXt_LOOP_* types */
14862 ncx->blk_loop.itersave =
14863 sv_dup_inc(ncx->blk_loop.itersave, param);
14864 if (CxPADLOOP(ncx)) {
14865 PADOFFSET off = ncx->blk_loop.itervar_u.svp
14866 - &CX_CURPAD_SV(ncx->blk_loop, 0);
14867 ncx->blk_loop.oldcomppad =
14868 (PAD*)ptr_table_fetch(PL_ptr_table,
14869 ncx->blk_loop.oldcomppad);
14870 ncx->blk_loop.itervar_u.svp =
14871 &CX_CURPAD_SV(ncx->blk_loop, off);
14874 /* this copies the GV if CXp_FOR_GV, or the SV for an
14875 * alias (for \$x (...)) - relies on gv_dup being the
14876 * same as sv_dup */
14877 ncx->blk_loop.itervar_u.gv
14878 = gv_dup((const GV *)ncx->blk_loop.itervar_u.gv,
14882 case CXt_LOOP_PLAIN:
14885 ncx->blk_format.prevcomppad =
14886 (PAD*)ptr_table_fetch(PL_ptr_table,
14887 ncx->blk_format.prevcomppad);
14888 ncx->blk_format.cv = cv_dup_inc(ncx->blk_format.cv, param);
14889 ncx->blk_format.gv = gv_dup(ncx->blk_format.gv, param);
14890 ncx->blk_format.dfoutgv = gv_dup_inc(ncx->blk_format.dfoutgv,
14894 ncx->blk_givwhen.defsv_save =
14895 sv_dup_inc(ncx->blk_givwhen.defsv_save, param);
14912 Duplicate a stack info structure, returning a pointer to the cloned object.
14918 Perl_si_dup(pTHX_ PERL_SI *si, CLONE_PARAMS* param)
14922 PERL_ARGS_ASSERT_SI_DUP;
14925 return (PERL_SI*)NULL;
14927 /* look for it in the table first */
14928 nsi = (PERL_SI*)ptr_table_fetch(PL_ptr_table, si);
14932 /* create anew and remember what it is */
14933 Newx(nsi, 1, PERL_SI);
14934 ptr_table_store(PL_ptr_table, si, nsi);
14936 nsi->si_stack = av_dup_inc(si->si_stack, param);
14937 nsi->si_cxix = si->si_cxix;
14938 nsi->si_cxsubix = si->si_cxsubix;
14939 nsi->si_cxmax = si->si_cxmax;
14940 nsi->si_cxstack = cx_dup(si->si_cxstack, si->si_cxix, si->si_cxmax, param);
14941 nsi->si_type = si->si_type;
14942 nsi->si_prev = si_dup(si->si_prev, param);
14943 nsi->si_next = si_dup(si->si_next, param);
14944 nsi->si_markoff = si->si_markoff;
14945 #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY
14946 nsi->si_stack_hwm = 0;
14952 #define POPINT(ss,ix) ((ss)[--(ix)].any_i32)
14953 #define TOPINT(ss,ix) ((ss)[ix].any_i32)
14954 #define POPLONG(ss,ix) ((ss)[--(ix)].any_long)
14955 #define TOPLONG(ss,ix) ((ss)[ix].any_long)
14956 #define POPIV(ss,ix) ((ss)[--(ix)].any_iv)
14957 #define TOPIV(ss,ix) ((ss)[ix].any_iv)
14958 #define POPUV(ss,ix) ((ss)[--(ix)].any_uv)
14959 #define TOPUV(ss,ix) ((ss)[ix].any_uv)
14960 #define POPBOOL(ss,ix) ((ss)[--(ix)].any_bool)
14961 #define TOPBOOL(ss,ix) ((ss)[ix].any_bool)
14962 #define POPPTR(ss,ix) ((ss)[--(ix)].any_ptr)
14963 #define TOPPTR(ss,ix) ((ss)[ix].any_ptr)
14964 #define POPDPTR(ss,ix) ((ss)[--(ix)].any_dptr)
14965 #define TOPDPTR(ss,ix) ((ss)[ix].any_dptr)
14966 #define POPDXPTR(ss,ix) ((ss)[--(ix)].any_dxptr)
14967 #define TOPDXPTR(ss,ix) ((ss)[ix].any_dxptr)
14970 #define pv_dup_inc(p) SAVEPV(p)
14971 #define pv_dup(p) SAVEPV(p)
14972 #define svp_dup_inc(p,pp) any_dup(p,pp)
14974 /* map any object to the new equivent - either something in the
14975 * ptr table, or something in the interpreter structure
14979 Perl_any_dup(pTHX_ void *v, const PerlInterpreter *proto_perl)
14983 PERL_ARGS_ASSERT_ANY_DUP;
14986 return (void*)NULL;
14988 /* look for it in the table first */
14989 ret = ptr_table_fetch(PL_ptr_table, v);
14993 /* see if it is part of the interpreter structure */
14994 if (v >= (void*)proto_perl && v < (void*)(proto_perl+1))
14995 ret = (void*)(((char*)aTHX) + (((char*)v) - (char*)proto_perl));
15006 Duplicate the save stack, returning a pointer to the cloned object.
15012 Perl_ss_dup(pTHX_ PerlInterpreter *proto_perl, CLONE_PARAMS* param)
15014 ANY * const ss = proto_perl->Isavestack;
15015 const I32 max = proto_perl->Isavestack_max + SS_MAXPUSH;
15016 I32 ix = proto_perl->Isavestack_ix;
15029 void (*dptr) (void*);
15030 void (*dxptr) (pTHX_ void*);
15032 PERL_ARGS_ASSERT_SS_DUP;
15034 Newx(nss, max, ANY);
15037 const UV uv = POPUV(ss,ix);
15038 const U8 type = (U8)uv & SAVE_MASK;
15040 TOPUV(nss,ix) = uv;
15042 case SAVEt_CLEARSV:
15043 case SAVEt_CLEARPADRANGE:
15045 case SAVEt_HELEM: /* hash element */
15046 case SAVEt_SV: /* scalar reference */
15047 sv = (const SV *)POPPTR(ss,ix);
15048 TOPPTR(nss,ix) = SvREFCNT_inc(sv_dup_inc(sv, param));
15050 case SAVEt_ITEM: /* normal string */
15051 case SAVEt_GVSV: /* scalar slot in GV */
15052 sv = (const SV *)POPPTR(ss,ix);
15053 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15054 if (type == SAVEt_SV)
15058 case SAVEt_MORTALIZESV:
15059 case SAVEt_READONLY_OFF:
15060 sv = (const SV *)POPPTR(ss,ix);
15061 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15063 case SAVEt_FREEPADNAME:
15064 ptr = POPPTR(ss,ix);
15065 TOPPTR(nss,ix) = padname_dup((PADNAME *)ptr, param);
15066 PadnameREFCNT((PADNAME *)TOPPTR(nss,ix))++;
15068 case SAVEt_SHARED_PVREF: /* char* in shared space */
15069 c = (char*)POPPTR(ss,ix);
15070 TOPPTR(nss,ix) = savesharedpv(c);
15071 ptr = POPPTR(ss,ix);
15072 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15074 case SAVEt_GENERIC_SVREF: /* generic sv */
15075 case SAVEt_SVREF: /* scalar reference */
15076 sv = (const SV *)POPPTR(ss,ix);
15077 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15078 if (type == SAVEt_SVREF)
15079 SvREFCNT_inc_simple_void((SV *)TOPPTR(nss,ix));
15080 ptr = POPPTR(ss,ix);
15081 TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
15083 case SAVEt_GVSLOT: /* any slot in GV */
15084 sv = (const SV *)POPPTR(ss,ix);
15085 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15086 ptr = POPPTR(ss,ix);
15087 TOPPTR(nss,ix) = svp_dup_inc((SV**)ptr, proto_perl);/* XXXXX */
15088 sv = (const SV *)POPPTR(ss,ix);
15089 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15091 case SAVEt_HV: /* hash reference */
15092 case SAVEt_AV: /* array reference */
15093 sv = (const SV *) POPPTR(ss,ix);
15094 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15096 case SAVEt_COMPPAD:
15098 sv = (const SV *) POPPTR(ss,ix);
15099 TOPPTR(nss,ix) = sv_dup(sv, param);
15101 case SAVEt_INT: /* int reference */
15102 ptr = POPPTR(ss,ix);
15103 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15104 intval = (int)POPINT(ss,ix);
15105 TOPINT(nss,ix) = intval;
15107 case SAVEt_LONG: /* long reference */
15108 ptr = POPPTR(ss,ix);
15109 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15110 longval = (long)POPLONG(ss,ix);
15111 TOPLONG(nss,ix) = longval;
15113 case SAVEt_I32: /* I32 reference */
15114 ptr = POPPTR(ss,ix);
15115 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15117 TOPINT(nss,ix) = i;
15119 case SAVEt_IV: /* IV reference */
15120 case SAVEt_STRLEN: /* STRLEN/size_t ref */
15121 ptr = POPPTR(ss,ix);
15122 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15124 TOPIV(nss,ix) = iv;
15126 case SAVEt_TMPSFLOOR:
15128 TOPIV(nss,ix) = iv;
15130 case SAVEt_HPTR: /* HV* reference */
15131 case SAVEt_APTR: /* AV* reference */
15132 case SAVEt_SPTR: /* SV* reference */
15133 ptr = POPPTR(ss,ix);
15134 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15135 sv = (const SV *)POPPTR(ss,ix);
15136 TOPPTR(nss,ix) = sv_dup(sv, param);
15138 case SAVEt_VPTR: /* random* reference */
15139 ptr = POPPTR(ss,ix);
15140 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15142 case SAVEt_STRLEN_SMALL:
15143 case SAVEt_INT_SMALL:
15144 case SAVEt_I32_SMALL:
15145 case SAVEt_I16: /* I16 reference */
15146 case SAVEt_I8: /* I8 reference */
15148 ptr = POPPTR(ss,ix);
15149 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15151 case SAVEt_GENERIC_PVREF: /* generic char* */
15152 case SAVEt_PPTR: /* char* reference */
15153 ptr = POPPTR(ss,ix);
15154 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15155 c = (char*)POPPTR(ss,ix);
15156 TOPPTR(nss,ix) = pv_dup(c);
15158 case SAVEt_GP: /* scalar reference */
15159 gp = (GP*)POPPTR(ss,ix);
15160 TOPPTR(nss,ix) = gp = gp_dup(gp, param);
15161 (void)GpREFCNT_inc(gp);
15162 gv = (const GV *)POPPTR(ss,ix);
15163 TOPPTR(nss,ix) = gv_dup_inc(gv, param);
15166 ptr = POPPTR(ss,ix);
15167 if (ptr && (((OP*)ptr)->op_private & OPpREFCOUNTED)) {
15168 /* these are assumed to be refcounted properly */
15170 switch (((OP*)ptr)->op_type) {
15172 case OP_LEAVESUBLV:
15176 case OP_LEAVEWRITE:
15177 TOPPTR(nss,ix) = ptr;
15180 (void) OpREFCNT_inc(o);
15184 TOPPTR(nss,ix) = NULL;
15189 TOPPTR(nss,ix) = NULL;
15191 case SAVEt_FREECOPHH:
15192 ptr = POPPTR(ss,ix);
15193 TOPPTR(nss,ix) = cophh_copy((COPHH *)ptr);
15195 case SAVEt_ADELETE:
15196 av = (const AV *)POPPTR(ss,ix);
15197 TOPPTR(nss,ix) = av_dup_inc(av, param);
15199 TOPINT(nss,ix) = i;
15202 hv = (const HV *)POPPTR(ss,ix);
15203 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
15205 TOPINT(nss,ix) = i;
15208 c = (char*)POPPTR(ss,ix);
15209 TOPPTR(nss,ix) = pv_dup_inc(c);
15211 case SAVEt_STACK_POS: /* Position on Perl stack */
15213 TOPINT(nss,ix) = i;
15215 case SAVEt_DESTRUCTOR:
15216 ptr = POPPTR(ss,ix);
15217 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
15218 dptr = POPDPTR(ss,ix);
15219 TOPDPTR(nss,ix) = DPTR2FPTR(void (*)(void*),
15220 any_dup(FPTR2DPTR(void *, dptr),
15223 case SAVEt_DESTRUCTOR_X:
15224 ptr = POPPTR(ss,ix);
15225 TOPPTR(nss,ix) = any_dup(ptr, proto_perl); /* XXX quite arbitrary */
15226 dxptr = POPDXPTR(ss,ix);
15227 TOPDXPTR(nss,ix) = DPTR2FPTR(void (*)(pTHX_ void*),
15228 any_dup(FPTR2DPTR(void *, dxptr),
15231 case SAVEt_REGCONTEXT:
15233 ix -= uv >> SAVE_TIGHT_SHIFT;
15235 case SAVEt_AELEM: /* array element */
15236 sv = (const SV *)POPPTR(ss,ix);
15237 TOPPTR(nss,ix) = SvREFCNT_inc(sv_dup_inc(sv, param));
15239 TOPIV(nss,ix) = iv;
15240 av = (const AV *)POPPTR(ss,ix);
15241 TOPPTR(nss,ix) = av_dup_inc(av, param);
15244 ptr = POPPTR(ss,ix);
15245 TOPPTR(nss,ix) = ptr;
15247 case SAVEt_HINTS_HH:
15248 hv = (const HV *)POPPTR(ss,ix);
15249 TOPPTR(nss,ix) = hv_dup_inc(hv, param);
15252 ptr = POPPTR(ss,ix);
15253 ptr = cophh_copy((COPHH*)ptr);
15254 TOPPTR(nss,ix) = ptr;
15256 TOPINT(nss,ix) = i;
15258 case SAVEt_PADSV_AND_MORTALIZE:
15259 longval = (long)POPLONG(ss,ix);
15260 TOPLONG(nss,ix) = longval;
15261 ptr = POPPTR(ss,ix);
15262 TOPPTR(nss,ix) = any_dup(ptr, proto_perl);
15263 sv = (const SV *)POPPTR(ss,ix);
15264 TOPPTR(nss,ix) = sv_dup_inc(sv, param);
15266 case SAVEt_SET_SVFLAGS:
15268 TOPINT(nss,ix) = i;
15270 TOPINT(nss,ix) = i;
15271 sv = (const SV *)POPPTR(ss,ix);
15272 TOPPTR(nss,ix) = sv_dup(sv, param);
15274 case SAVEt_COMPILE_WARNINGS:
15275 ptr = POPPTR(ss,ix);
15276 TOPPTR(nss,ix) = DUP_WARNINGS((STRLEN*)ptr);
15279 ptr = POPPTR(ss,ix);
15280 TOPPTR(nss,ix) = parser_dup((const yy_parser*)ptr, param);
15284 "panic: ss_dup inconsistency (%" IVdf ")", (IV) type);
15292 /* if sv is a stash, call $class->CLONE_SKIP(), and set the SVphv_CLONEABLE
15293 * flag to the result. This is done for each stash before cloning starts,
15294 * so we know which stashes want their objects cloned */
15297 do_mark_cloneable_stash(pTHX_ SV *const sv)
15299 const HEK * const hvname = HvNAME_HEK((const HV *)sv);
15301 GV* const cloner = gv_fetchmethod_autoload(MUTABLE_HV(sv), "CLONE_SKIP", 0);
15302 SvFLAGS(sv) |= SVphv_CLONEABLE; /* clone objects by default */
15303 if (cloner && GvCV(cloner)) {
15310 mXPUSHs(newSVhek(hvname));
15312 call_sv(MUTABLE_SV(GvCV(cloner)), G_SCALAR);
15319 SvFLAGS(sv) &= ~SVphv_CLONEABLE;
15327 =for apidoc perl_clone
15329 Create and return a new interpreter by cloning the current one.
15331 C<perl_clone> takes these flags as parameters:
15333 C<CLONEf_COPY_STACKS> - is used to, well, copy the stacks also,
15334 without it we only clone the data and zero the stacks,
15335 with it we copy the stacks and the new perl interpreter is
15336 ready to run at the exact same point as the previous one.
15337 The pseudo-fork code uses C<COPY_STACKS> while the
15338 threads->create doesn't.
15340 C<CLONEf_KEEP_PTR_TABLE> -
15341 C<perl_clone> keeps a ptr_table with the pointer of the old
15342 variable as a key and the new variable as a value,
15343 this allows it to check if something has been cloned and not
15344 clone it again, but rather just use the value and increase the
15346 If C<KEEP_PTR_TABLE> is not set then C<perl_clone> will kill the ptr_table
15347 using the function S<C<ptr_table_free(PL_ptr_table); PL_ptr_table = NULL;>>.
15348 A reason to keep it around is if you want to dup some of your own
15349 variables which are outside the graph that perl scans.
15351 C<CLONEf_CLONE_HOST> -
15352 This is a win32 thing, it is ignored on unix, it tells perl's
15353 win32host code (which is c++) to clone itself, this is needed on
15354 win32 if you want to run two threads at the same time,
15355 if you just want to do some stuff in a separate perl interpreter
15356 and then throw it away and return to the original one,
15357 you don't need to do anything.
15362 /* XXX the above needs expanding by someone who actually understands it ! */
15363 EXTERN_C PerlInterpreter *
15364 perl_clone_host(PerlInterpreter* proto_perl, UV flags);
15367 perl_clone(PerlInterpreter *proto_perl, UV flags)
15369 #ifdef PERL_IMPLICIT_SYS
15371 PERL_ARGS_ASSERT_PERL_CLONE;
15373 /* perlhost.h so we need to call into it
15374 to clone the host, CPerlHost should have a c interface, sky */
15376 #ifndef __amigaos4__
15377 if (flags & CLONEf_CLONE_HOST) {
15378 return perl_clone_host(proto_perl,flags);
15381 return perl_clone_using(proto_perl, flags,
15383 proto_perl->IMemShared,
15384 proto_perl->IMemParse,
15386 proto_perl->IStdIO,
15390 proto_perl->IProc);
15394 perl_clone_using(PerlInterpreter *proto_perl, UV flags,
15395 struct IPerlMem* ipM, struct IPerlMem* ipMS,
15396 struct IPerlMem* ipMP, struct IPerlEnv* ipE,
15397 struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
15398 struct IPerlDir* ipD, struct IPerlSock* ipS,
15399 struct IPerlProc* ipP)
15401 /* XXX many of the string copies here can be optimized if they're
15402 * constants; they need to be allocated as common memory and just
15403 * their pointers copied. */
15406 CLONE_PARAMS clone_params;
15407 CLONE_PARAMS* const param = &clone_params;
15409 PerlInterpreter * const my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
15411 PERL_ARGS_ASSERT_PERL_CLONE_USING;
15412 #else /* !PERL_IMPLICIT_SYS */
15414 CLONE_PARAMS clone_params;
15415 CLONE_PARAMS* param = &clone_params;
15416 PerlInterpreter * const my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
15418 PERL_ARGS_ASSERT_PERL_CLONE;
15419 #endif /* PERL_IMPLICIT_SYS */
15421 /* for each stash, determine whether its objects should be cloned */
15422 S_visit(proto_perl, do_mark_cloneable_stash, SVt_PVHV, SVTYPEMASK);
15423 PERL_SET_THX(my_perl);
15426 PoisonNew(my_perl, 1, PerlInterpreter);
15429 PL_defstash = NULL; /* may be used by perl malloc() */
15432 PL_scopestack_name = 0;
15434 PL_savestack_ix = 0;
15435 PL_savestack_max = -1;
15436 PL_sig_pending = 0;
15438 Zero(&PL_debug_pad, 1, struct perl_debug_pad);
15439 Zero(&PL_padname_undef, 1, PADNAME);
15440 Zero(&PL_padname_const, 1, PADNAME);
15441 # ifdef DEBUG_LEAKING_SCALARS
15442 PL_sv_serial = (((UV)my_perl >> 2) & 0xfff) * 1000000;
15444 # ifdef PERL_TRACE_OPS
15445 Zero(PL_op_exec_cnt, OP_max+2, UV);
15447 #else /* !DEBUGGING */
15448 Zero(my_perl, 1, PerlInterpreter);
15449 #endif /* DEBUGGING */
15451 #ifdef PERL_IMPLICIT_SYS
15452 /* host pointers */
15454 PL_MemShared = ipMS;
15455 PL_MemParse = ipMP;
15462 #endif /* PERL_IMPLICIT_SYS */
15465 param->flags = flags;
15466 /* Nothing in the core code uses this, but we make it available to
15467 extensions (using mg_dup). */
15468 param->proto_perl = proto_perl;
15469 /* Likely nothing will use this, but it is initialised to be consistent
15470 with Perl_clone_params_new(). */
15471 param->new_perl = my_perl;
15472 param->unreferenced = NULL;
15475 INIT_TRACK_MEMPOOL(my_perl->Imemory_debug_header, my_perl);
15477 PL_body_arenas = NULL;
15478 Zero(&PL_body_roots, 1, PL_body_roots);
15482 PL_sv_arenaroot = NULL;
15484 PL_debug = proto_perl->Idebug;
15486 /* dbargs array probably holds garbage */
15489 PL_compiling = proto_perl->Icompiling;
15491 /* pseudo environmental stuff */
15492 PL_origargc = proto_perl->Iorigargc;
15493 PL_origargv = proto_perl->Iorigargv;
15495 #ifndef NO_TAINT_SUPPORT
15496 /* Set tainting stuff before PerlIO_debug can possibly get called */
15497 PL_tainting = proto_perl->Itainting;
15498 PL_taint_warn = proto_perl->Itaint_warn;
15500 PL_tainting = FALSE;
15501 PL_taint_warn = FALSE;
15504 PL_minus_c = proto_perl->Iminus_c;
15506 PL_localpatches = proto_perl->Ilocalpatches;
15507 PL_splitstr = proto_perl->Isplitstr;
15508 PL_minus_n = proto_perl->Iminus_n;
15509 PL_minus_p = proto_perl->Iminus_p;
15510 PL_minus_l = proto_perl->Iminus_l;
15511 PL_minus_a = proto_perl->Iminus_a;
15512 PL_minus_E = proto_perl->Iminus_E;
15513 PL_minus_F = proto_perl->Iminus_F;
15514 PL_doswitches = proto_perl->Idoswitches;
15515 PL_dowarn = proto_perl->Idowarn;
15516 #ifdef PERL_SAWAMPERSAND
15517 PL_sawampersand = proto_perl->Isawampersand;
15519 PL_unsafe = proto_perl->Iunsafe;
15520 PL_perldb = proto_perl->Iperldb;
15521 PL_perl_destruct_level = proto_perl->Iperl_destruct_level;
15522 PL_exit_flags = proto_perl->Iexit_flags;
15524 /* XXX time(&PL_basetime) when asked for? */
15525 PL_basetime = proto_perl->Ibasetime;
15527 PL_maxsysfd = proto_perl->Imaxsysfd;
15528 PL_statusvalue = proto_perl->Istatusvalue;
15530 PL_statusvalue_vms = proto_perl->Istatusvalue_vms;
15532 PL_statusvalue_posix = proto_perl->Istatusvalue_posix;
15535 /* RE engine related */
15536 PL_regmatch_slab = NULL;
15537 PL_reg_curpm = NULL;
15539 PL_sub_generation = proto_perl->Isub_generation;
15541 /* funky return mechanisms */
15542 PL_forkprocess = proto_perl->Iforkprocess;
15544 /* internal state */
15545 PL_main_start = proto_perl->Imain_start;
15546 PL_eval_root = proto_perl->Ieval_root;
15547 PL_eval_start = proto_perl->Ieval_start;
15549 PL_filemode = proto_perl->Ifilemode;
15550 PL_lastfd = proto_perl->Ilastfd;
15551 PL_oldname = proto_perl->Ioldname; /* XXX not quite right */
15552 PL_gensym = proto_perl->Igensym;
15554 PL_laststatval = proto_perl->Ilaststatval;
15555 PL_laststype = proto_perl->Ilaststype;
15558 PL_profiledata = NULL;
15560 PL_generation = proto_perl->Igeneration;
15562 PL_in_clean_objs = proto_perl->Iin_clean_objs;
15563 PL_in_clean_all = proto_perl->Iin_clean_all;
15565 PL_delaymagic_uid = proto_perl->Idelaymagic_uid;
15566 PL_delaymagic_euid = proto_perl->Idelaymagic_euid;
15567 PL_delaymagic_gid = proto_perl->Idelaymagic_gid;
15568 PL_delaymagic_egid = proto_perl->Idelaymagic_egid;
15569 PL_nomemok = proto_perl->Inomemok;
15570 PL_an = proto_perl->Ian;
15571 PL_evalseq = proto_perl->Ievalseq;
15572 PL_origalen = proto_perl->Iorigalen;
15574 PL_sighandlerp = proto_perl->Isighandlerp;
15575 PL_sighandler1p = proto_perl->Isighandler1p;
15576 PL_sighandler3p = proto_perl->Isighandler3p;
15578 PL_runops = proto_perl->Irunops;
15580 PL_subline = proto_perl->Isubline;
15582 PL_cv_has_eval = proto_perl->Icv_has_eval;
15584 #ifdef USE_LOCALE_COLLATE
15585 PL_collation_ix = proto_perl->Icollation_ix;
15586 PL_collation_standard = proto_perl->Icollation_standard;
15587 PL_collxfrm_base = proto_perl->Icollxfrm_base;
15588 PL_collxfrm_mult = proto_perl->Icollxfrm_mult;
15589 PL_strxfrm_max_cp = proto_perl->Istrxfrm_max_cp;
15590 PL_strxfrm_is_behaved = proto_perl->Istrxfrm_is_behaved;
15591 PL_strxfrm_NUL_replacement = proto_perl->Istrxfrm_NUL_replacement;
15592 #endif /* USE_LOCALE_COLLATE */
15594 #ifdef USE_LOCALE_NUMERIC
15595 PL_numeric_standard = proto_perl->Inumeric_standard;
15596 PL_numeric_underlying = proto_perl->Inumeric_underlying;
15597 PL_numeric_underlying_is_standard = proto_perl->Inumeric_underlying_is_standard;
15598 #endif /* !USE_LOCALE_NUMERIC */
15600 /* Did the locale setup indicate UTF-8? */
15601 PL_utf8locale = proto_perl->Iutf8locale;
15602 my_strlcpy(PL_locale_utf8ness, proto_perl->Ilocale_utf8ness, sizeof(PL_locale_utf8ness));
15603 #if defined(USE_ITHREADS) && ! defined(USE_THREAD_SAFE_LOCALE)
15604 PL_lc_numeric_mutex_depth = 0;
15606 /* Unicode features (see perlrun/-C) */
15607 PL_unicode = proto_perl->Iunicode;
15609 /* Pre-5.8 signals control */
15610 PL_signals = proto_perl->Isignals;
15612 /* times() ticks per second */
15613 PL_clocktick = proto_perl->Iclocktick;
15615 /* Recursion stopper for PerlIO_find_layer */
15616 PL_in_load_module = proto_perl->Iin_load_module;
15618 /* Not really needed/useful since the reenrant_retint is "volatile",
15619 * but do it for consistency's sake. */
15620 PL_reentrant_retint = proto_perl->Ireentrant_retint;
15622 /* Hooks to shared SVs and locks. */
15623 PL_sharehook = proto_perl->Isharehook;
15624 PL_lockhook = proto_perl->Ilockhook;
15625 PL_unlockhook = proto_perl->Iunlockhook;
15626 PL_threadhook = proto_perl->Ithreadhook;
15627 PL_destroyhook = proto_perl->Idestroyhook;
15628 PL_signalhook = proto_perl->Isignalhook;
15630 PL_globhook = proto_perl->Iglobhook;
15632 PL_srand_called = proto_perl->Isrand_called;
15633 Copy(&(proto_perl->Irandom_state), &PL_random_state, 1, PL_RANDOM_STATE_TYPE);
15634 PL_srand_override = proto_perl->Isrand_override;
15635 PL_srand_override_next = proto_perl->Isrand_override_next;
15637 if (flags & CLONEf_COPY_STACKS) {
15638 /* next allocation will be PL_tmps_stack[PL_tmps_ix+1] */
15639 PL_tmps_ix = proto_perl->Itmps_ix;
15640 PL_tmps_max = proto_perl->Itmps_max;
15641 PL_tmps_floor = proto_perl->Itmps_floor;
15643 /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
15644 * NOTE: unlike the others! */
15645 PL_scopestack_ix = proto_perl->Iscopestack_ix;
15646 PL_scopestack_max = proto_perl->Iscopestack_max;
15648 /* next SSPUSHFOO() sets PL_savestack[PL_savestack_ix]
15649 * NOTE: unlike the others! */
15650 PL_savestack_ix = proto_perl->Isavestack_ix;
15651 PL_savestack_max = proto_perl->Isavestack_max;
15654 PL_start_env = proto_perl->Istart_env; /* XXXXXX */
15655 PL_top_env = &PL_start_env;
15657 PL_op = proto_perl->Iop;
15660 PL_Xpv = (XPV*)NULL;
15661 my_perl->Ina = proto_perl->Ina;
15663 PL_statcache = proto_perl->Istatcache;
15665 #ifndef NO_TAINT_SUPPORT
15666 PL_tainted = proto_perl->Itainted;
15668 PL_tainted = FALSE;
15670 PL_curpm = proto_perl->Icurpm; /* XXX No PMOP ref count */
15672 PL_chopset = proto_perl->Ichopset; /* XXX never deallocated */
15674 PL_restartjmpenv = proto_perl->Irestartjmpenv;
15675 PL_restartop = proto_perl->Irestartop;
15676 PL_in_eval = proto_perl->Iin_eval;
15677 PL_delaymagic = proto_perl->Idelaymagic;
15678 PL_phase = proto_perl->Iphase;
15679 PL_localizing = proto_perl->Ilocalizing;
15681 PL_hv_fetch_ent_mh = NULL;
15682 PL_modcount = proto_perl->Imodcount;
15683 PL_lastgotoprobe = NULL;
15684 PL_dumpindent = proto_perl->Idumpindent;
15686 PL_efloatbuf = NULL; /* reinits on demand */
15687 PL_efloatsize = 0; /* reinits on demand */
15691 PL_colorset = 0; /* reinits PL_colors[] */
15692 /*PL_colors[6] = {0,0,0,0,0,0};*/
15694 /* Pluggable optimizer */
15695 PL_peepp = proto_perl->Ipeepp;
15696 PL_rpeepp = proto_perl->Irpeepp;
15697 /* op_free() hook */
15698 PL_opfreehook = proto_perl->Iopfreehook;
15700 # ifdef PERL_MEM_LOG
15701 Zero(PL_mem_log, sizeof(PL_mem_log), char);
15704 #ifdef USE_REENTRANT_API
15705 /* XXX: things like -Dm will segfault here in perlio, but doing
15706 * PERL_SET_CONTEXT(proto_perl);
15707 * breaks too many other things
15709 Perl_reentrant_init(aTHX);
15712 /* create SV map for pointer relocation */
15713 PL_ptr_table = ptr_table_new();
15715 /* initialize these special pointers as early as possible */
15717 ptr_table_store(PL_ptr_table, &proto_perl->Isv_undef, &PL_sv_undef);
15718 ptr_table_store(PL_ptr_table, &proto_perl->Isv_no, &PL_sv_no);
15719 ptr_table_store(PL_ptr_table, &proto_perl->Isv_zero, &PL_sv_zero);
15720 ptr_table_store(PL_ptr_table, &proto_perl->Isv_yes, &PL_sv_yes);
15721 ptr_table_store(PL_ptr_table, &proto_perl->Ipadname_const,
15722 &PL_padname_const);
15724 /* create (a non-shared!) shared string table */
15725 PL_strtab = newHV();
15726 HvSHAREKEYS_off(PL_strtab);
15727 hv_ksplit(PL_strtab, HvTOTALKEYS(proto_perl->Istrtab));
15728 ptr_table_store(PL_ptr_table, proto_perl->Istrtab, PL_strtab);
15730 Zero(PL_sv_consts, SV_CONSTS_COUNT, SV*);
15732 /* This PV will be free'd special way so must set it same way op.c does */
15733 PL_compiling.cop_file = savesharedpv(PL_compiling.cop_file);
15734 ptr_table_store(PL_ptr_table, proto_perl->Icompiling.cop_file, PL_compiling.cop_file);
15736 ptr_table_store(PL_ptr_table, &proto_perl->Icompiling, &PL_compiling);
15737 PL_compiling.cop_warnings = DUP_WARNINGS(PL_compiling.cop_warnings);
15738 CopHINTHASH_set(&PL_compiling, cophh_copy(CopHINTHASH_get(&PL_compiling)));
15739 PL_curcop = (COP*)any_dup(proto_perl->Icurcop, proto_perl);
15741 param->stashes = newAV(); /* Setup array of objects to call clone on */
15742 /* This makes no difference to the implementation, as it always pushes
15743 and shifts pointers to other SVs without changing their reference
15744 count, with the array becoming empty before it is freed. However, it
15745 makes it conceptually clear what is going on, and will avoid some
15746 work inside av.c, filling slots between AvFILL() and AvMAX() with
15747 &PL_sv_undef, and SvREFCNT_dec()ing those. */
15748 AvREAL_off(param->stashes);
15750 if (!(flags & CLONEf_COPY_STACKS)) {
15751 param->unreferenced = newAV();
15754 #ifdef PERLIO_LAYERS
15755 /* Clone PerlIO tables as soon as we can handle general xx_dup() */
15756 PerlIO_clone(aTHX_ proto_perl, param);
15759 PL_envgv = gv_dup_inc(proto_perl->Ienvgv, param);
15760 PL_incgv = gv_dup_inc(proto_perl->Iincgv, param);
15761 PL_hintgv = gv_dup_inc(proto_perl->Ihintgv, param);
15762 PL_origfilename = SAVEPV(proto_perl->Iorigfilename);
15763 PL_xsubfilename = proto_perl->Ixsubfilename;
15764 PL_diehook = sv_dup_inc(proto_perl->Idiehook, param);
15765 PL_warnhook = sv_dup_inc(proto_perl->Iwarnhook, param);
15768 PL_patchlevel = sv_dup_inc(proto_perl->Ipatchlevel, param);
15769 PL_inplace = SAVEPV(proto_perl->Iinplace);
15770 PL_e_script = sv_dup_inc(proto_perl->Ie_script, param);
15772 /* magical thingies */
15774 SvPVCLEAR(PERL_DEBUG_PAD(0)); /* For regex debugging. */
15775 SvPVCLEAR(PERL_DEBUG_PAD(1)); /* ext/re needs these */
15776 SvPVCLEAR(PERL_DEBUG_PAD(2)); /* even without DEBUGGING. */
15779 /* Clone the regex array */
15780 /* ORANGE FIXME for plugins, probably in the SV dup code.
15781 newSViv(PTR2IV(CALLREGDUPE(
15782 INT2PTR(REGEXP *, SvIVX(regex)), param))))
15784 PL_regex_padav = av_dup_inc(proto_perl->Iregex_padav, param);
15785 PL_regex_pad = AvARRAY(PL_regex_padav);
15787 PL_stashpadmax = proto_perl->Istashpadmax;
15788 PL_stashpadix = proto_perl->Istashpadix ;
15789 Newx(PL_stashpad, PL_stashpadmax, HV *);
15792 for (; o < PL_stashpadmax; ++o)
15793 PL_stashpad[o] = hv_dup(proto_perl->Istashpad[o], param);
15796 /* shortcuts to various I/O objects */
15797 PL_ofsgv = gv_dup_inc(proto_perl->Iofsgv, param);
15798 PL_stdingv = gv_dup(proto_perl->Istdingv, param);
15799 PL_stderrgv = gv_dup(proto_perl->Istderrgv, param);
15800 PL_defgv = gv_dup(proto_perl->Idefgv, param);
15801 PL_argvgv = gv_dup_inc(proto_perl->Iargvgv, param);
15802 PL_argvoutgv = gv_dup(proto_perl->Iargvoutgv, param);
15803 PL_argvout_stack = av_dup_inc(proto_perl->Iargvout_stack, param);
15805 /* shortcuts to regexp stuff */
15806 PL_replgv = gv_dup_inc(proto_perl->Ireplgv, param);
15808 /* shortcuts to misc objects */
15809 PL_errgv = gv_dup(proto_perl->Ierrgv, param);
15811 /* shortcuts to debugging objects */
15812 PL_DBgv = gv_dup_inc(proto_perl->IDBgv, param);
15813 PL_DBline = gv_dup_inc(proto_perl->IDBline, param);
15814 PL_DBsub = gv_dup_inc(proto_perl->IDBsub, param);
15815 PL_DBsingle = sv_dup(proto_perl->IDBsingle, param);
15816 PL_DBtrace = sv_dup(proto_perl->IDBtrace, param);
15817 PL_DBsignal = sv_dup(proto_perl->IDBsignal, param);
15818 Copy(proto_perl->IDBcontrol, PL_DBcontrol, DBVARMG_COUNT, IV);
15820 /* symbol tables */
15821 PL_defstash = hv_dup_inc(proto_perl->Idefstash, param);
15822 PL_curstash = hv_dup_inc(proto_perl->Icurstash, param);
15823 PL_debstash = hv_dup(proto_perl->Idebstash, param);
15824 PL_globalstash = hv_dup(proto_perl->Iglobalstash, param);
15825 PL_curstname = sv_dup_inc(proto_perl->Icurstname, param);
15827 PL_beginav = av_dup_inc(proto_perl->Ibeginav, param);
15828 PL_beginav_save = av_dup_inc(proto_perl->Ibeginav_save, param);
15829 PL_checkav_save = av_dup_inc(proto_perl->Icheckav_save, param);
15830 PL_unitcheckav = av_dup_inc(proto_perl->Iunitcheckav, param);
15831 PL_unitcheckav_save = av_dup_inc(proto_perl->Iunitcheckav_save, param);
15832 PL_endav = av_dup_inc(proto_perl->Iendav, param);
15833 PL_checkav = av_dup_inc(proto_perl->Icheckav, param);
15834 PL_initav = av_dup_inc(proto_perl->Iinitav, param);
15835 PL_savebegin = proto_perl->Isavebegin;
15837 PL_isarev = hv_dup_inc(proto_perl->Iisarev, param);
15839 /* subprocess state */
15840 PL_fdpid = av_dup_inc(proto_perl->Ifdpid, param);
15842 if (proto_perl->Iop_mask)
15843 PL_op_mask = SAVEPVN(proto_perl->Iop_mask, PL_maxo);
15846 /* PL_asserting = proto_perl->Iasserting; */
15848 /* current interpreter roots */
15849 PL_main_cv = cv_dup_inc(proto_perl->Imain_cv, param);
15851 PL_main_root = OpREFCNT_inc(proto_perl->Imain_root);
15854 /* runtime control stuff */
15855 PL_curcopdb = (COP*)any_dup(proto_perl->Icurcopdb, proto_perl);
15857 PL_preambleav = av_dup_inc(proto_perl->Ipreambleav, param);
15859 PL_ors_sv = sv_dup_inc(proto_perl->Iors_sv, param);
15861 /* interpreter atexit processing */
15862 PL_exitlistlen = proto_perl->Iexitlistlen;
15863 if (PL_exitlistlen) {
15864 Newx(PL_exitlist, PL_exitlistlen, PerlExitListEntry);
15865 Copy(proto_perl->Iexitlist, PL_exitlist, PL_exitlistlen, PerlExitListEntry);
15868 PL_exitlist = (PerlExitListEntry*)NULL;
15870 PL_my_cxt_size = proto_perl->Imy_cxt_size;
15871 if (PL_my_cxt_size) {
15872 Newx(PL_my_cxt_list, PL_my_cxt_size, void *);
15873 Copy(proto_perl->Imy_cxt_list, PL_my_cxt_list, PL_my_cxt_size, void *);
15876 PL_my_cxt_list = (void**)NULL;
15878 PL_modglobal = hv_dup_inc(proto_perl->Imodglobal, param);
15879 PL_custom_op_names = hv_dup_inc(proto_perl->Icustom_op_names,param);
15880 PL_custom_op_descs = hv_dup_inc(proto_perl->Icustom_op_descs,param);
15881 PL_custom_ops = hv_dup_inc(proto_perl->Icustom_ops, param);
15883 PL_compcv = cv_dup(proto_perl->Icompcv, param);
15885 PAD_CLONE_VARS(proto_perl, param);
15887 #ifdef HAVE_INTERP_INTERN
15888 sys_intern_dup(&proto_perl->Isys_intern, &PL_sys_intern);
15891 PL_DBcv = cv_dup(proto_perl->IDBcv, param);
15893 #ifdef PERL_USES_PL_PIDSTATUS
15894 PL_pidstatus = newHV(); /* XXX flag for cloning? */
15896 PL_osname = SAVEPV(proto_perl->Iosname);
15897 PL_parser = parser_dup(proto_perl->Iparser, param);
15899 /* XXX this only works if the saved cop has already been cloned */
15900 if (proto_perl->Iparser) {
15901 PL_parser->saved_curcop = (COP*)any_dup(
15902 proto_perl->Iparser->saved_curcop,
15906 PL_subname = sv_dup_inc(proto_perl->Isubname, param);
15908 #if defined(USE_POSIX_2008_LOCALE) \
15909 && defined(USE_THREAD_SAFE_LOCALE) \
15910 && ! defined(HAS_QUERYLOCALE)
15911 for (i = 0; i < (int) C_ARRAY_LENGTH(PL_curlocales); i++) {
15912 PL_curlocales[i] = SAVEPV(proto_perl->Icurlocales[i]);
15915 #ifdef USE_LOCALE_CTYPE
15916 Copy(proto_perl->Ifold_locale, PL_fold_locale, 256, U8);
15917 /* Should we warn if uses locale? */
15918 PL_warn_locale = sv_dup_inc(proto_perl->Iwarn_locale, param);
15919 PL_utf8locale = proto_perl->Iutf8locale;
15920 PL_in_utf8_CTYPE_locale = proto_perl->Iin_utf8_CTYPE_locale;
15921 PL_in_utf8_turkic_locale = proto_perl->Iin_utf8_turkic_locale;
15924 #ifdef USE_LOCALE_COLLATE
15925 PL_in_utf8_COLLATE_locale = proto_perl->Iin_utf8_COLLATE_locale;
15926 PL_collation_name = SAVEPV(proto_perl->Icollation_name);
15927 #endif /* USE_LOCALE_COLLATE */
15929 #ifdef USE_LOCALE_NUMERIC
15930 PL_numeric_name = SAVEPV(proto_perl->Inumeric_name);
15931 PL_numeric_radix_sv = sv_dup_inc(proto_perl->Inumeric_radix_sv, param);
15933 # if defined(USE_POSIX_2008_LOCALE)
15934 PL_underlying_numeric_obj = NULL;
15936 #endif /* !USE_LOCALE_NUMERIC */
15937 #if defined(USE_POSIX_2008_LOCALE)
15938 PL_scratch_locale_obj = NULL;
15942 PL_mbrlen_ps = proto_perl->Imbrlen_ps;
15945 PL_mbrtowc_ps = proto_perl->Imbrtowc_ps;
15948 PL_wcrtomb_ps = proto_perl->Iwcrtomb_ps;
15951 PL_langinfo_buf = NULL;
15952 PL_langinfo_bufsize = 0;
15954 PL_setlocale_buf = NULL;
15955 PL_setlocale_bufsize = 0;
15957 PL_stdize_locale_buf = NULL;
15958 PL_stdize_locale_bufsize = 0;
15960 /* Unicode inversion lists */
15962 PL_AboveLatin1 = sv_dup_inc(proto_perl->IAboveLatin1, param);
15963 PL_Assigned_invlist = sv_dup_inc(proto_perl->IAssigned_invlist, param);
15964 PL_GCB_invlist = sv_dup_inc(proto_perl->IGCB_invlist, param);
15965 PL_HasMultiCharFold = sv_dup_inc(proto_perl->IHasMultiCharFold, param);
15966 PL_InMultiCharFold = sv_dup_inc(proto_perl->IInMultiCharFold, param);
15967 PL_Latin1 = sv_dup_inc(proto_perl->ILatin1, param);
15968 PL_LB_invlist = sv_dup_inc(proto_perl->ILB_invlist, param);
15969 PL_SB_invlist = sv_dup_inc(proto_perl->ISB_invlist, param);
15970 PL_SCX_invlist = sv_dup_inc(proto_perl->ISCX_invlist, param);
15971 PL_UpperLatin1 = sv_dup_inc(proto_perl->IUpperLatin1, param);
15972 PL_in_some_fold = sv_dup_inc(proto_perl->Iin_some_fold, param);
15973 PL_utf8_foldclosures = sv_dup_inc(proto_perl->Iutf8_foldclosures, param);
15974 PL_utf8_idcont = sv_dup_inc(proto_perl->Iutf8_idcont, param);
15975 PL_utf8_idstart = sv_dup_inc(proto_perl->Iutf8_idstart, param);
15976 PL_utf8_perl_idcont = sv_dup_inc(proto_perl->Iutf8_perl_idcont, param);
15977 PL_utf8_perl_idstart = sv_dup_inc(proto_perl->Iutf8_perl_idstart, param);
15978 PL_utf8_xidcont = sv_dup_inc(proto_perl->Iutf8_xidcont, param);
15979 PL_utf8_xidstart = sv_dup_inc(proto_perl->Iutf8_xidstart, param);
15980 PL_WB_invlist = sv_dup_inc(proto_perl->IWB_invlist, param);
15981 for (i = 0; i < POSIX_CC_COUNT; i++) {
15982 PL_XPosix_ptrs[i] = sv_dup_inc(proto_perl->IXPosix_ptrs[i], param);
15983 if (i != CC_CASED_ && i != CC_VERTSPACE_) {
15984 PL_Posix_ptrs[i] = sv_dup_inc(proto_perl->IPosix_ptrs[i], param);
15987 PL_Posix_ptrs[CC_CASED_] = PL_Posix_ptrs[CC_ALPHA_];
15988 PL_Posix_ptrs[CC_VERTSPACE_] = NULL;
15990 PL_utf8_toupper = sv_dup_inc(proto_perl->Iutf8_toupper, param);
15991 PL_utf8_totitle = sv_dup_inc(proto_perl->Iutf8_totitle, param);
15992 PL_utf8_tolower = sv_dup_inc(proto_perl->Iutf8_tolower, param);
15993 PL_utf8_tofold = sv_dup_inc(proto_perl->Iutf8_tofold, param);
15994 PL_utf8_tosimplefold = sv_dup_inc(proto_perl->Iutf8_tosimplefold, param);
15995 PL_utf8_charname_begin = sv_dup_inc(proto_perl->Iutf8_charname_begin, param);
15996 PL_utf8_charname_continue = sv_dup_inc(proto_perl->Iutf8_charname_continue, param);
15997 PL_utf8_mark = sv_dup_inc(proto_perl->Iutf8_mark, param);
15998 PL_InBitmap = sv_dup_inc(proto_perl->IInBitmap, param);
15999 PL_CCC_non0_non230 = sv_dup_inc(proto_perl->ICCC_non0_non230, param);
16000 PL_Private_Use = sv_dup_inc(proto_perl->IPrivate_Use, param);
16003 PL_seen_deprecated_macro = hv_dup_inc(proto_perl->Iseen_deprecated_macro, param);
16006 if (proto_perl->Ipsig_pend) {
16007 Newxz(PL_psig_pend, SIG_SIZE, int);
16010 PL_psig_pend = (int*)NULL;
16013 if (proto_perl->Ipsig_name) {
16014 Newx(PL_psig_name, 2 * SIG_SIZE, SV*);
16015 sv_dup_inc_multiple(proto_perl->Ipsig_name, PL_psig_name, 2 * SIG_SIZE,
16017 PL_psig_ptr = PL_psig_name + SIG_SIZE;
16020 PL_psig_ptr = (SV**)NULL;
16021 PL_psig_name = (SV**)NULL;
16024 if (flags & CLONEf_COPY_STACKS) {
16025 Newx(PL_tmps_stack, PL_tmps_max, SV*);
16026 sv_dup_inc_multiple(proto_perl->Itmps_stack, PL_tmps_stack,
16027 PL_tmps_ix+1, param);
16029 /* next PUSHMARK() sets *(PL_markstack_ptr+1) */
16030 i = proto_perl->Imarkstack_max - proto_perl->Imarkstack;
16031 Newx(PL_markstack, i, I32);
16032 PL_markstack_max = PL_markstack + (proto_perl->Imarkstack_max
16033 - proto_perl->Imarkstack);
16034 PL_markstack_ptr = PL_markstack + (proto_perl->Imarkstack_ptr
16035 - proto_perl->Imarkstack);
16036 Copy(proto_perl->Imarkstack, PL_markstack,
16037 PL_markstack_ptr - PL_markstack + 1, I32);
16039 /* next push_scope()/ENTER sets PL_scopestack[PL_scopestack_ix]
16040 * NOTE: unlike the others! */
16041 Newx(PL_scopestack, PL_scopestack_max, I32);
16042 Copy(proto_perl->Iscopestack, PL_scopestack, PL_scopestack_ix, I32);
16045 Newx(PL_scopestack_name, PL_scopestack_max, const char *);
16046 Copy(proto_perl->Iscopestack_name, PL_scopestack_name, PL_scopestack_ix, const char *);
16048 /* reset stack AV to correct length before its duped via
16049 * PL_curstackinfo */
16050 AvFILLp(proto_perl->Icurstack) =
16051 proto_perl->Istack_sp - proto_perl->Istack_base;
16053 /* NOTE: si_dup() looks at PL_markstack */
16054 PL_curstackinfo = si_dup(proto_perl->Icurstackinfo, param);
16056 /* PL_curstack = PL_curstackinfo->si_stack; */
16057 PL_curstack = av_dup(proto_perl->Icurstack, param);
16058 PL_mainstack = av_dup(proto_perl->Imainstack, param);
16060 /* next PUSHs() etc. set *(PL_stack_sp+1) */
16061 PL_stack_base = AvARRAY(PL_curstack);
16062 PL_stack_sp = PL_stack_base + (proto_perl->Istack_sp
16063 - proto_perl->Istack_base);
16064 PL_stack_max = PL_stack_base + AvMAX(PL_curstack);
16066 /*Newxz(PL_savestack, PL_savestack_max, ANY);*/
16067 PL_savestack = ss_dup(proto_perl, param);
16071 ENTER; /* perl_destruct() wants to LEAVE; */
16074 PL_statgv = gv_dup(proto_perl->Istatgv, param);
16075 PL_statname = sv_dup_inc(proto_perl->Istatname, param);
16077 PL_rs = sv_dup_inc(proto_perl->Irs, param);
16078 PL_last_in_gv = gv_dup(proto_perl->Ilast_in_gv, param);
16079 PL_defoutgv = gv_dup_inc(proto_perl->Idefoutgv, param);
16080 PL_toptarget = sv_dup_inc(proto_perl->Itoptarget, param);
16081 PL_bodytarget = sv_dup_inc(proto_perl->Ibodytarget, param);
16082 PL_formtarget = sv_dup(proto_perl->Iformtarget, param);
16084 PL_errors = sv_dup_inc(proto_perl->Ierrors, param);
16086 PL_sortcop = (OP*)any_dup(proto_perl->Isortcop, proto_perl);
16087 PL_firstgv = gv_dup_inc(proto_perl->Ifirstgv, param);
16088 PL_secondgv = gv_dup_inc(proto_perl->Isecondgv, param);
16090 PL_stashcache = newHV();
16092 PL_watchaddr = (char **) ptr_table_fetch(PL_ptr_table,
16093 proto_perl->Iwatchaddr);
16094 PL_watchok = PL_watchaddr ? * PL_watchaddr : NULL;
16095 if (PL_debug && PL_watchaddr) {
16096 PerlIO_printf(Perl_debug_log,
16097 "WATCHING: %" UVxf " cloned as %" UVxf " with value %" UVxf "\n",
16098 PTR2UV(proto_perl->Iwatchaddr), PTR2UV(PL_watchaddr),
16099 PTR2UV(PL_watchok));
16102 PL_registered_mros = hv_dup_inc(proto_perl->Iregistered_mros, param);
16103 PL_blockhooks = av_dup_inc(proto_perl->Iblockhooks, param);
16105 /* Call the ->CLONE method, if it exists, for each of the stashes
16106 identified by sv_dup() above.
16108 while(av_count(param->stashes) != 0) {
16109 HV* const stash = MUTABLE_HV(av_shift(param->stashes));
16110 GV* const cloner = gv_fetchmethod_autoload(stash, "CLONE", 0);
16111 if (cloner && GvCV(cloner)) {
16116 mXPUSHs(newSVhek(HvNAME_HEK(stash)));
16118 call_sv(MUTABLE_SV(GvCV(cloner)), G_DISCARD);
16124 if (!(flags & CLONEf_KEEP_PTR_TABLE)) {
16125 ptr_table_free(PL_ptr_table);
16126 PL_ptr_table = NULL;
16129 if (!(flags & CLONEf_COPY_STACKS)) {
16130 unreferenced_to_tmp_stack(param->unreferenced);
16133 SvREFCNT_dec(param->stashes);
16135 /* orphaned? eg threads->new inside BEGIN or use */
16136 if (PL_compcv && ! SvREFCNT(PL_compcv)) {
16137 SvREFCNT_inc_simple_void(PL_compcv);
16138 SAVEFREESV(PL_compcv);
16145 S_unreferenced_to_tmp_stack(pTHX_ AV *const unreferenced)
16147 PERL_ARGS_ASSERT_UNREFERENCED_TO_TMP_STACK;
16149 if (AvFILLp(unreferenced) > -1) {
16150 SV **svp = AvARRAY(unreferenced);
16151 SV **const last = svp + AvFILLp(unreferenced);
16155 if (SvREFCNT(*svp) == 1)
16157 } while (++svp <= last);
16159 EXTEND_MORTAL(count);
16160 svp = AvARRAY(unreferenced);
16163 if (SvREFCNT(*svp) == 1) {
16164 /* Our reference is the only one to this SV. This means that
16165 in this thread, the scalar effectively has a 0 reference.
16166 That doesn't work (cleanup never happens), so donate our
16167 reference to it onto the save stack. */
16168 PL_tmps_stack[++PL_tmps_ix] = *svp;
16170 /* As an optimisation, because we are already walking the
16171 entire array, instead of above doing either
16172 SvREFCNT_inc(*svp) or *svp = &PL_sv_undef, we can instead
16173 release our reference to the scalar, so that at the end of
16174 the array owns zero references to the scalars it happens to
16175 point to. We are effectively converting the array from
16176 AvREAL() on to AvREAL() off. This saves the av_clear()
16177 (triggered by the SvREFCNT_dec(unreferenced) below) from
16178 walking the array a second time. */
16179 SvREFCNT_dec(*svp);
16182 } while (++svp <= last);
16183 AvREAL_off(unreferenced);
16185 SvREFCNT_dec_NN(unreferenced);
16189 Perl_clone_params_del(CLONE_PARAMS *param)
16191 PerlInterpreter *const was = PERL_GET_THX;
16192 PerlInterpreter *const to = param->new_perl;
16195 PERL_ARGS_ASSERT_CLONE_PARAMS_DEL;
16201 SvREFCNT_dec(param->stashes);
16202 if (param->unreferenced)
16203 unreferenced_to_tmp_stack(param->unreferenced);
16213 Perl_clone_params_new(PerlInterpreter *const from, PerlInterpreter *const to)
16215 /* Need to play this game, as newAV() can call safesysmalloc(), and that
16216 does a dTHX; to get the context from thread local storage.
16217 FIXME - under PERL_CORE Newx(), Safefree() and friends should expand to
16218 a version that passes in my_perl. */
16219 PerlInterpreter *const was = PERL_GET_THX;
16220 CLONE_PARAMS *param;
16222 PERL_ARGS_ASSERT_CLONE_PARAMS_NEW;
16228 /* Given that we've set the context, we can do this unshared. */
16229 Newx(param, 1, CLONE_PARAMS);
16232 param->proto_perl = from;
16233 param->new_perl = to;
16234 param->stashes = (AV *)Perl_newSV_type(to, SVt_PVAV);
16235 AvREAL_off(param->stashes);
16236 param->unreferenced = (AV *)Perl_newSV_type(to, SVt_PVAV);
16244 #endif /* USE_ITHREADS */
16247 Perl_init_constants(pTHX)
16250 SvREFCNT(&PL_sv_undef) = SvREFCNT_IMMORTAL;
16251 SvFLAGS(&PL_sv_undef) = SVf_READONLY|SVf_PROTECT|SVt_NULL;
16252 SvANY(&PL_sv_undef) = NULL;
16254 SvANY(&PL_sv_no) = new_XPVNV();
16255 SvREFCNT(&PL_sv_no) = SvREFCNT_IMMORTAL;
16256 SvFLAGS(&PL_sv_no) = SVt_PVNV|SVf_READONLY|SVf_PROTECT
16257 |SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
16258 |SVp_POK|SVf_POK|SVf_IsCOW|SVppv_STATIC;
16260 SvANY(&PL_sv_yes) = new_XPVNV();
16261 SvREFCNT(&PL_sv_yes) = SvREFCNT_IMMORTAL;
16262 SvFLAGS(&PL_sv_yes) = SVt_PVNV|SVf_READONLY|SVf_PROTECT
16263 |SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
16264 |SVp_POK|SVf_POK|SVf_IsCOW|SVppv_STATIC;
16266 SvANY(&PL_sv_zero) = new_XPVNV();
16267 SvREFCNT(&PL_sv_zero) = SvREFCNT_IMMORTAL;
16268 SvFLAGS(&PL_sv_zero) = SVt_PVNV|SVf_READONLY|SVf_PROTECT
16269 |SVp_IOK|SVf_IOK|SVp_NOK|SVf_NOK
16273 SvPV_set(&PL_sv_no, (char*)PL_No);
16274 SvCUR_set(&PL_sv_no, 0);
16275 SvLEN_set(&PL_sv_no, 0);
16276 SvIV_set(&PL_sv_no, 0);
16277 SvNV_set(&PL_sv_no, 0);
16279 SvPV_set(&PL_sv_yes, (char*)PL_Yes);
16280 SvCUR_set(&PL_sv_yes, 1);
16281 SvLEN_set(&PL_sv_yes, 0);
16282 SvIV_set(&PL_sv_yes, 1);
16283 SvNV_set(&PL_sv_yes, 1);
16285 SvPV_set(&PL_sv_zero, (char*)PL_Zero);
16286 SvCUR_set(&PL_sv_zero, 1);
16287 SvLEN_set(&PL_sv_zero, 0);
16288 SvIV_set(&PL_sv_zero, 0);
16289 SvNV_set(&PL_sv_zero, 0);
16291 PadnamePV(&PL_padname_const) = (char *)PL_No;
16293 assert(SvIMMORTAL_INTERP(&PL_sv_yes));
16294 assert(SvIMMORTAL_INTERP(&PL_sv_undef));
16295 assert(SvIMMORTAL_INTERP(&PL_sv_no));
16296 assert(SvIMMORTAL_INTERP(&PL_sv_zero));
16298 assert(SvIMMORTAL(&PL_sv_yes));
16299 assert(SvIMMORTAL(&PL_sv_undef));
16300 assert(SvIMMORTAL(&PL_sv_no));
16301 assert(SvIMMORTAL(&PL_sv_zero));
16303 assert( SvIMMORTAL_TRUE(&PL_sv_yes));
16304 assert(!SvIMMORTAL_TRUE(&PL_sv_undef));
16305 assert(!SvIMMORTAL_TRUE(&PL_sv_no));
16306 assert(!SvIMMORTAL_TRUE(&PL_sv_zero));
16308 assert( SvTRUE_nomg_NN(&PL_sv_yes));
16309 assert(!SvTRUE_nomg_NN(&PL_sv_undef));
16310 assert(!SvTRUE_nomg_NN(&PL_sv_no));
16311 assert(!SvTRUE_nomg_NN(&PL_sv_zero));
16315 =for apidoc_section $unicode
16317 =for apidoc sv_recode_to_utf8
16319 C<encoding> is assumed to be an C<Encode> object, on entry the PV
16320 of C<sv> is assumed to be octets in that encoding, and C<sv>
16321 will be converted into Unicode (and UTF-8).
16323 If C<sv> already is UTF-8 (or if it is not C<POK>), or if C<encoding>
16324 is not a reference, nothing is done to C<sv>. If C<encoding> is not
16325 an C<Encode::XS> Encoding object, bad things will happen.
16326 (See L<encoding> and L<Encode>.)
16328 The PV of C<sv> is returned.
16333 Perl_sv_recode_to_utf8(pTHX_ SV *sv, SV *encoding)
16335 PERL_ARGS_ASSERT_SV_RECODE_TO_UTF8;
16337 if (SvPOK(sv) && !SvUTF8(sv) && !IN_BYTES && SvROK(encoding)) {
16346 if (SvPADTMP(nsv)) {
16347 nsv = sv_newmortal();
16348 SvSetSV_nosteal(nsv, sv);
16357 Passing sv_yes is wrong - it needs to be or'ed set of constants
16358 for Encode::XS, while UTf-8 decode (currently) assumes a true value means
16359 remove converted chars from source.
16361 Both will default the value - let them.
16363 XPUSHs(&PL_sv_yes);
16366 call_method("decode", G_SCALAR);
16370 s = SvPV_const(uni, len);
16371 if (s != SvPVX_const(sv)) {
16372 SvGROW(sv, len + 1);
16373 Move(s, SvPVX(sv), len + 1, char);
16374 SvCUR_set(sv, len);
16379 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
16380 /* clear pos and any utf8 cache */
16381 MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
16384 if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
16385 magic_setutf8(sv,mg); /* clear UTF8 cache */
16390 return SvPOKp(sv) ? SvPVX(sv) : NULL;
16394 =for apidoc sv_cat_decode
16396 C<encoding> is assumed to be an C<Encode> object, the PV of C<ssv> is
16397 assumed to be octets in that encoding and decoding the input starts
16398 from the position which S<C<(PV + *offset)>> pointed to. C<dsv> will be
16399 concatenated with the decoded UTF-8 string from C<ssv>. Decoding will terminate
16400 when the string C<tstr> appears in decoding output or the input ends on
16401 the PV of C<ssv>. The value which C<offset> points will be modified
16402 to the last input position on C<ssv>.
16404 Returns TRUE if the terminator was found, else returns FALSE.
16409 Perl_sv_cat_decode(pTHX_ SV *dsv, SV *encoding,
16410 SV *ssv, int *offset, char *tstr, int tlen)
16414 PERL_ARGS_ASSERT_SV_CAT_DECODE;
16416 if (SvPOK(ssv) && SvPOK(dsv) && SvROK(encoding)) {
16427 offsv = newSViv(*offset);
16429 mPUSHp(tstr, tlen);
16431 call_method("cat_decode", G_SCALAR);
16433 ret = SvTRUE(TOPs);
16434 *offset = SvIV(offsv);
16440 Perl_croak(aTHX_ "Invalid argument to sv_cat_decode");
16445 /* ---------------------------------------------------------------------
16447 * support functions for report_uninit()
16450 /* the maxiumum size of array or hash where we will scan looking
16451 * for the undefined element that triggered the warning */
16453 #define FUV_MAX_SEARCH_SIZE 1000
16455 /* Look for an entry in the hash whose value has the same SV as val;
16456 * If so, return a mortal copy of the key. */
16459 S_find_hash_subscript(pTHX_ const HV *const hv, const SV *const val)
16464 PERL_ARGS_ASSERT_FIND_HASH_SUBSCRIPT;
16466 if (!hv || SvMAGICAL(hv) || !HvTOTALKEYS(hv) ||
16467 (HvTOTALKEYS(hv) > FUV_MAX_SEARCH_SIZE))
16470 if (val == &PL_sv_undef || val == &PL_sv_placeholder)
16473 array = HvARRAY(hv);
16475 for (i=HvMAX(hv); i>=0; i--) {
16477 for (entry = array[i]; entry; entry = HeNEXT(entry)) {
16478 if (HeVAL(entry) == val)
16479 return newSVhek_mortal(HeKEY_hek(entry));
16485 /* Look for an entry in the array whose value has the same SV as val;
16486 * If so, return the index, otherwise return -1. */
16489 S_find_array_subscript(pTHX_ const AV *const av, const SV *const val)
16491 PERL_ARGS_ASSERT_FIND_ARRAY_SUBSCRIPT;
16493 if (!av || SvMAGICAL(av) || !AvARRAY(av) ||
16494 (AvFILLp(av) > FUV_MAX_SEARCH_SIZE))
16497 if (val != &PL_sv_undef) {
16498 SV ** const svp = AvARRAY(av);
16501 for (i=AvFILLp(av); i>=0; i--)
16508 /* varname(): return the name of a variable, optionally with a subscript.
16509 * If gv is non-zero, use the name of that global, along with gvtype (one
16510 * of "$", "@", "%"); otherwise use the name of the lexical at pad offset
16511 * targ. Depending on the value of the subscript_type flag, return:
16514 #define FUV_SUBSCRIPT_NONE 1 /* "@foo" */
16515 #define FUV_SUBSCRIPT_ARRAY 2 /* "$foo[aindex]" */
16516 #define FUV_SUBSCRIPT_HASH 3 /* "$foo{keyname}" */
16517 #define FUV_SUBSCRIPT_WITHIN 4 /* "within @foo" */
16520 Perl_varname(pTHX_ const GV *const gv, const char gvtype, PADOFFSET targ,
16521 const SV *const keyname, SSize_t aindex, int subscript_type)
16524 SV * const name = sv_newmortal();
16525 if (gv && isGV(gv)) {
16527 buffer[0] = gvtype;
16530 /* as gv_fullname4(), but add literal '^' for $^FOO names */
16532 gv_fullname4(name, gv, buffer, 0);
16534 if ((unsigned int)SvPVX(name)[1] <= 26) {
16536 buffer[1] = SvPVX(name)[1] + 'A' - 1;
16538 /* Swap the 1 unprintable control character for the 2 byte pretty
16539 version - ie substr($name, 1, 1) = $buffer; */
16540 sv_insert(name, 1, 1, buffer, 2);
16544 CV * const cv = gv ? ((CV *)gv) : find_runcv(NULL);
16547 assert(!cv || SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM);
16549 if (!cv || !CvPADLIST(cv))
16551 sv = padnamelist_fetch(PadlistNAMES(CvPADLIST(cv)), targ);
16552 sv_setpvn(name, PadnamePV(sv), PadnameLEN(sv));
16556 if (subscript_type == FUV_SUBSCRIPT_HASH) {
16557 SV * const sv = newSV_type(SVt_NULL);
16559 const char * const pv = SvPV_nomg_const((SV*)keyname, len);
16561 *SvPVX(name) = '$';
16562 Perl_sv_catpvf(aTHX_ name, "{%s}",
16563 pv_pretty(sv, pv, len, 32, NULL, NULL,
16564 PERL_PV_PRETTY_DUMP | PERL_PV_ESCAPE_UNI_DETECT ));
16565 SvREFCNT_dec_NN(sv);
16567 else if (subscript_type == FUV_SUBSCRIPT_ARRAY) {
16568 *SvPVX(name) = '$';
16569 Perl_sv_catpvf(aTHX_ name, "[%" IVdf "]", (IV)aindex);
16571 else if (subscript_type == FUV_SUBSCRIPT_WITHIN) {
16572 /* We know that name has no magic, so can use 0 instead of SV_GMAGIC */
16573 Perl_sv_insert_flags(aTHX_ name, 0, 0, STR_WITH_LEN("within "), 0);
16581 =apidoc_section $warning
16582 =for apidoc find_uninit_var
16584 Find the name of the undefined variable (if any) that caused the operator
16585 to issue a "Use of uninitialized value" warning.
16586 If match is true, only return a name if its value matches C<uninit_sv>.
16587 So roughly speaking, if a unary operator (such as C<OP_COS>) generates a
16588 warning, then following the direct child of the op may yield an
16589 C<OP_PADSV> or C<OP_GV> that gives the name of the undefined variable. On the
16590 other hand, with C<OP_ADD> there are two branches to follow, so we only print
16591 the variable name if we get an exact match.
16592 C<desc_p> points to a string pointer holding the description of the op.
16593 This may be updated if needed.
16595 The name is returned as a mortal SV.
16597 Assumes that C<PL_op> is the OP that originally triggered the error, and that
16598 C<PL_comppad>/C<PL_curpad> points to the currently executing pad.
16604 S_find_uninit_var(pTHX_ const OP *const obase, const SV *const uninit_sv,
16605 bool match, const char **desc_p)
16609 const OP *o, *o2, *kid;
16611 PERL_ARGS_ASSERT_FIND_UNINIT_VAR;
16613 if (!obase || (match && (!uninit_sv || uninit_sv == &PL_sv_undef ||
16614 uninit_sv == &PL_sv_placeholder)))
16617 switch (obase->op_type) {
16620 /* undef should care if its args are undef - any warnings
16621 * will be from tied/magic vars */
16629 const bool pad = ( obase->op_type == OP_PADAV
16630 || obase->op_type == OP_PADHV
16631 || obase->op_type == OP_PADRANGE
16634 const bool hash = ( obase->op_type == OP_PADHV
16635 || obase->op_type == OP_RV2HV
16636 || (obase->op_type == OP_PADRANGE
16637 && SvTYPE(PAD_SVl(obase->op_targ)) == SVt_PVHV)
16641 int subscript_type = FUV_SUBSCRIPT_WITHIN;
16643 if (pad) { /* @lex, %lex */
16644 sv = PAD_SVl(obase->op_targ);
16648 if (cUNOPx(obase)->op_first->op_type == OP_GV) {
16649 /* @global, %global */
16650 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
16653 sv = hash ? MUTABLE_SV(GvHV(gv)): MUTABLE_SV(GvAV(gv));
16655 else if (obase == PL_op) /* @{expr}, %{expr} */
16656 return find_uninit_var(cUNOPx(obase)->op_first,
16657 uninit_sv, match, desc_p);
16658 else /* @{expr}, %{expr} as a sub-expression */
16662 /* attempt to find a match within the aggregate */
16664 keysv = find_hash_subscript((const HV*)sv, uninit_sv);
16666 subscript_type = FUV_SUBSCRIPT_HASH;
16669 index = find_array_subscript((const AV *)sv, uninit_sv);
16671 subscript_type = FUV_SUBSCRIPT_ARRAY;
16674 if (match && subscript_type == FUV_SUBSCRIPT_WITHIN)
16677 return varname(gv, (char)(hash ? '%' : '@'), obase->op_targ,
16678 keysv, index, subscript_type);
16682 if (cUNOPx(obase)->op_first->op_type == OP_GV) {
16684 gv = cGVOPx_gv(cUNOPx(obase)->op_first);
16685 if (!gv || !GvSTASH(gv))
16687 if (match && (GvSV(gv) != uninit_sv))
16689 return varname(gv, '$', 0, NULL, 0, FUV_SUBSCRIPT_NONE);
16692 return find_uninit_var(cUNOPx(obase)->op_first, uninit_sv, 1, desc_p);
16695 if (match && PAD_SVl(obase->op_targ) != uninit_sv)
16697 return varname(NULL, '$', obase->op_targ,
16698 NULL, 0, FUV_SUBSCRIPT_NONE);
16701 gv = cGVOPx_gv(obase);
16702 if (!gv || (match && GvSV(gv) != uninit_sv) || !GvSTASH(gv))
16704 return varname(gv, '$', 0, NULL, 0, FUV_SUBSCRIPT_NONE);
16706 case OP_AELEMFAST_LEX:
16709 AV *av = MUTABLE_AV(PAD_SV(obase->op_targ));
16710 if (!av || SvRMAGICAL(av))
16712 svp = av_fetch(av, (I8)obase->op_private, FALSE);
16713 if (!svp || *svp != uninit_sv)
16716 return varname(NULL, '$', obase->op_targ,
16717 NULL, (I8)obase->op_private, FUV_SUBSCRIPT_ARRAY);
16720 gv = cGVOPx_gv(obase);
16725 AV *const av = GvAV(gv);
16726 if (!av || SvRMAGICAL(av))
16728 svp = av_fetch(av, (I8)obase->op_private, FALSE);
16729 if (!svp || *svp != uninit_sv)
16732 return varname(gv, '$', 0,
16733 NULL, (I8)obase->op_private, FUV_SUBSCRIPT_ARRAY);
16735 NOT_REACHED; /* NOTREACHED */
16738 o = cUNOPx(obase)->op_first;
16739 if (!o || o->op_type != OP_NULL ||
16740 ! (o->op_targ == OP_AELEM || o->op_targ == OP_HELEM))
16742 return find_uninit_var(cBINOPo->op_last, uninit_sv, match, desc_p);
16747 bool negate = FALSE;
16749 if (PL_op == obase)
16750 /* $a[uninit_expr] or $h{uninit_expr} */
16751 return find_uninit_var(cBINOPx(obase)->op_last,
16752 uninit_sv, match, desc_p);
16755 o = cBINOPx(obase)->op_first;
16756 kid = cBINOPx(obase)->op_last;
16758 /* get the av or hv, and optionally the gv */
16760 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) {
16761 sv = PAD_SV(o->op_targ);
16763 else if ((o->op_type == OP_RV2AV || o->op_type == OP_RV2HV)
16764 && cUNOPo->op_first->op_type == OP_GV)
16766 gv = cGVOPx_gv(cUNOPo->op_first);
16770 == OP_RV2HV ? MUTABLE_SV(GvHV(gv)) : MUTABLE_SV(GvAV(gv));
16775 if (kid && kid->op_type == OP_NEGATE) {
16777 kid = cUNOPx(kid)->op_first;
16780 if (kid && kid->op_type == OP_CONST && SvOK(cSVOPx_sv(kid))) {
16781 /* index is constant */
16784 kidsv = newSVpvs_flags("-", SVs_TEMP);
16785 sv_catsv(kidsv, cSVOPx_sv(kid));
16788 kidsv = cSVOPx_sv(kid);
16792 if (obase->op_type == OP_HELEM) {
16793 HE* he = hv_fetch_ent(MUTABLE_HV(sv), kidsv, 0, 0);
16794 if (!he || HeVAL(he) != uninit_sv)
16798 SV * const opsv = cSVOPx_sv(kid);
16799 const IV opsviv = SvIV(opsv);
16800 SV * const * const svp = av_fetch(MUTABLE_AV(sv),
16801 negate ? - opsviv : opsviv,
16803 if (!svp || *svp != uninit_sv)
16807 if (obase->op_type == OP_HELEM)
16808 return varname(gv, '%', o->op_targ,
16809 kidsv, 0, FUV_SUBSCRIPT_HASH);
16811 return varname(gv, '@', o->op_targ, NULL,
16812 negate ? - SvIV(cSVOPx_sv(kid)) : SvIV(cSVOPx_sv(kid)),
16813 FUV_SUBSCRIPT_ARRAY);
16816 /* index is an expression;
16817 * attempt to find a match within the aggregate */
16818 if (obase->op_type == OP_HELEM) {
16819 SV * const keysv = find_hash_subscript((const HV*)sv, uninit_sv);
16821 return varname(gv, '%', o->op_targ,
16822 keysv, 0, FUV_SUBSCRIPT_HASH);
16825 const SSize_t index
16826 = find_array_subscript((const AV *)sv, uninit_sv);
16828 return varname(gv, '@', o->op_targ,
16829 NULL, index, FUV_SUBSCRIPT_ARRAY);
16834 (char)((o->op_type == OP_PADAV || o->op_type == OP_RV2AV)
16836 o->op_targ, NULL, 0, FUV_SUBSCRIPT_WITHIN);
16838 NOT_REACHED; /* NOTREACHED */
16841 case OP_MULTIDEREF: {
16842 /* If we were executing OP_MULTIDEREF when the undef warning
16843 * triggered, then it must be one of the index values within
16844 * that triggered it. If not, then the only possibility is that
16845 * the value retrieved by the last aggregate index might be the
16846 * culprit. For the former, we set PL_multideref_pc each time before
16847 * using an index, so work though the item list until we reach
16848 * that point. For the latter, just work through the entire item
16849 * list; the last aggregate retrieved will be the candidate.
16850 * There is a third rare possibility: something triggered
16851 * magic while fetching an array/hash element. Just display
16852 * nothing in this case.
16855 /* the named aggregate, if any */
16856 PADOFFSET agg_targ = 0;
16858 /* the last-seen index */
16860 PADOFFSET index_targ;
16862 IV index_const_iv = 0; /* init for spurious compiler warn */
16863 SV *index_const_sv;
16864 int depth = 0; /* how many array/hash lookups we've done */
16866 UNOP_AUX_item *items = cUNOP_AUXx(obase)->op_aux;
16867 UNOP_AUX_item *last = NULL;
16868 UV actions = items->uv;
16871 if (PL_op == obase) {
16872 last = PL_multideref_pc;
16873 assert(last >= items && last <= items + items[-1].uv);
16880 switch (actions & MDEREF_ACTION_MASK) {
16882 case MDEREF_reload:
16883 actions = (++items)->uv;
16886 case MDEREF_HV_padhv_helem: /* $lex{...} */
16889 case MDEREF_AV_padav_aelem: /* $lex[...] */
16890 agg_targ = (++items)->pad_offset;
16894 case MDEREF_HV_gvhv_helem: /* $pkg{...} */
16897 case MDEREF_AV_gvav_aelem: /* $pkg[...] */
16899 agg_gv = (GV*)UNOP_AUX_item_sv(++items);
16900 assert(isGV_with_GP(agg_gv));
16903 case MDEREF_HV_gvsv_vivify_rv2hv_helem: /* $pkg->{...} */
16904 case MDEREF_HV_padsv_vivify_rv2hv_helem: /* $lex->{...} */
16907 case MDEREF_HV_pop_rv2hv_helem: /* expr->{...} */
16908 case MDEREF_HV_vivify_rv2hv_helem: /* vivify, ->{...} */
16914 case MDEREF_AV_gvsv_vivify_rv2av_aelem: /* $pkg->[...] */
16915 case MDEREF_AV_padsv_vivify_rv2av_aelem: /* $lex->[...] */
16918 case MDEREF_AV_pop_rv2av_aelem: /* expr->[...] */
16919 case MDEREF_AV_vivify_rv2av_aelem: /* vivify, ->[...] */
16926 index_const_sv = NULL;
16928 index_type = (actions & MDEREF_INDEX_MASK);
16929 switch (index_type) {
16930 case MDEREF_INDEX_none:
16932 case MDEREF_INDEX_const:
16934 index_const_sv = UNOP_AUX_item_sv(++items)
16936 index_const_iv = (++items)->iv;
16938 case MDEREF_INDEX_padsv:
16939 index_targ = (++items)->pad_offset;
16941 case MDEREF_INDEX_gvsv:
16942 index_gv = (GV*)UNOP_AUX_item_sv(++items);
16943 assert(isGV_with_GP(index_gv));
16947 if (index_type != MDEREF_INDEX_none)
16950 if ( index_type == MDEREF_INDEX_none
16951 || (actions & MDEREF_FLAG_last)
16952 || (last && items >= last)
16956 actions >>= MDEREF_SHIFT;
16959 if (PL_op == obase) {
16960 /* most likely index was undef */
16962 *desc_p = ( (actions & MDEREF_FLAG_last)
16963 && (obase->op_private
16964 & (OPpMULTIDEREF_EXISTS|OPpMULTIDEREF_DELETE)))
16966 (obase->op_private & OPpMULTIDEREF_EXISTS)
16969 : is_hv ? "hash element" : "array element";
16970 assert(index_type != MDEREF_INDEX_none);
16972 if (GvSV(index_gv) == uninit_sv)
16973 return varname(index_gv, '$', 0, NULL, 0,
16974 FUV_SUBSCRIPT_NONE);
16979 if (PL_curpad[index_targ] == uninit_sv)
16980 return varname(NULL, '$', index_targ,
16981 NULL, 0, FUV_SUBSCRIPT_NONE);
16985 /* If we got to this point it was undef on a const subscript,
16986 * so magic probably involved, e.g. $ISA[0]. Give up. */
16990 /* the SV returned by pp_multideref() was undef, if anything was */
16996 sv = PAD_SV(agg_targ);
16998 sv = is_hv ? MUTABLE_SV(GvHV(agg_gv)) : MUTABLE_SV(GvAV(agg_gv));
17005 if (index_type == MDEREF_INDEX_const) {
17010 HE* he = hv_fetch_ent(MUTABLE_HV(sv), index_const_sv, 0, 0);
17011 if (!he || HeVAL(he) != uninit_sv)
17015 SV * const * const svp =
17016 av_fetch(MUTABLE_AV(sv), index_const_iv, FALSE);
17017 if (!svp || *svp != uninit_sv)
17022 ? varname(agg_gv, '%', agg_targ,
17023 index_const_sv, 0, FUV_SUBSCRIPT_HASH)
17024 : varname(agg_gv, '@', agg_targ,
17025 NULL, index_const_iv, FUV_SUBSCRIPT_ARRAY);
17028 /* index is an var */
17030 SV * const keysv = find_hash_subscript((const HV*)sv, uninit_sv);
17032 return varname(agg_gv, '%', agg_targ,
17033 keysv, 0, FUV_SUBSCRIPT_HASH);
17036 const SSize_t index
17037 = find_array_subscript((const AV *)sv, uninit_sv);
17039 return varname(agg_gv, '@', agg_targ,
17040 NULL, index, FUV_SUBSCRIPT_ARRAY);
17042 /* look for an element not found */
17043 if (!SvMAGICAL(sv)) {
17044 SV *index_sv = NULL;
17046 index_sv = PL_curpad[index_targ];
17048 else if (index_gv) {
17049 index_sv = GvSV(index_gv);
17051 if (index_sv && !SvMAGICAL(index_sv) && !SvROK(index_sv)) {
17053 SV *report_index_sv = SvOK(index_sv) ? index_sv : &PL_sv_no;
17054 HE *he = hv_fetch_ent(MUTABLE_HV(sv), report_index_sv, 0, 0);
17056 return varname(agg_gv, '%', agg_targ,
17057 report_index_sv, 0, FUV_SUBSCRIPT_HASH);
17061 SSize_t index = SvOK(index_sv) ? SvIV(index_sv) : 0;
17062 SV * const * const svp =
17063 av_fetch(MUTABLE_AV(sv), index, FALSE);
17065 return varname(agg_gv, '@', agg_targ,
17066 NULL, index, FUV_SUBSCRIPT_ARRAY);
17073 return varname(agg_gv,
17075 agg_targ, NULL, 0, FUV_SUBSCRIPT_WITHIN);
17077 NOT_REACHED; /* NOTREACHED */
17081 /* only examine RHS */
17082 return find_uninit_var(cBINOPx(obase)->op_first, uninit_sv,
17086 o = cUNOPx(obase)->op_first;
17087 if ( o->op_type == OP_PUSHMARK
17088 || (o->op_type == OP_NULL && o->op_targ == OP_PUSHMARK)
17092 if (!OpHAS_SIBLING(o)) {
17093 /* one-arg version of open is highly magical */
17095 if (o->op_type == OP_GV) { /* open FOO; */
17097 if (match && GvSV(gv) != uninit_sv)
17099 return varname(gv, '$', 0,
17100 NULL, 0, FUV_SUBSCRIPT_NONE);
17102 /* other possibilities not handled are:
17103 * open $x; or open my $x; should return '${*$x}'
17104 * open expr; should return '$'.expr ideally
17111 /* ops where $_ may be an implicit arg */
17116 if ( !(obase->op_flags & OPf_STACKED)) {
17117 if (uninit_sv == DEFSV)
17118 return newSVpvs_flags("$_", SVs_TEMP);
17119 else if (obase->op_targ
17120 && uninit_sv == PAD_SVl(obase->op_targ))
17121 return varname(NULL, '$', obase->op_targ, NULL, 0,
17122 FUV_SUBSCRIPT_NONE);
17129 match = 1; /* print etc can return undef on defined args */
17130 /* skip filehandle as it can't produce 'undef' warning */
17131 o = cUNOPx(obase)->op_first;
17132 if ((obase->op_flags & OPf_STACKED)
17134 ( o->op_type == OP_PUSHMARK
17135 || (o->op_type == OP_NULL && o->op_targ == OP_PUSHMARK)))
17136 o = OpSIBLING(OpSIBLING(o));
17140 case OP_ENTEREVAL: /* could be eval $undef or $x='$undef'; eval $x */
17141 case OP_CUSTOM: /* XS or custom code could trigger random warnings */
17143 /* the following ops are capable of returning PL_sv_undef even for
17144 * defined arg(s) */
17163 case OP_GETPEERNAME:
17210 case OP_SMARTMATCH:
17219 /* XXX tmp hack: these two may call an XS sub, and currently
17220 XS subs don't have a SUB entry on the context stack, so CV and
17221 pad determination goes wrong, and BAD things happen. So, just
17222 don't try to determine the value under those circumstances.
17223 Need a better fix at dome point. DAPM 11/2007 */
17229 GV * const gv = gv_fetchpvs(".", GV_NOTQUAL, SVt_PV);
17230 if (gv && GvSV(gv) == uninit_sv)
17231 return newSVpvs_flags("$.", SVs_TEMP);
17236 /* def-ness of rval pos() is independent of the def-ness of its arg */
17237 if ( !(obase->op_flags & OPf_MOD))
17243 if (SvROK(PL_rs) && uninit_sv == SvRV(PL_rs))
17244 return newSVpvs_flags("${$/}", SVs_TEMP);
17249 if (!(obase->op_flags & OPf_KIDS))
17251 o = cUNOPx(obase)->op_first;
17257 /* This loop checks all the kid ops, skipping any that cannot pos-
17258 * sibly be responsible for the uninitialized value; i.e., defined
17259 * constants and ops that return nothing. If there is only one op
17260 * left that is not skipped, then we *know* it is responsible for
17261 * the uninitialized value. If there is more than one op left, we
17262 * have to look for an exact match in the while() loop below.
17263 * Note that we skip padrange, because the individual pad ops that
17264 * it replaced are still in the tree, so we work on them instead.
17267 for (kid=o; kid; kid = OpSIBLING(kid)) {
17268 const OPCODE type = kid->op_type;
17269 if ( (type == OP_CONST && SvOK(cSVOPx_sv(kid)))
17270 || (type == OP_NULL && ! (kid->op_flags & OPf_KIDS))
17271 || (type == OP_PUSHMARK)
17272 || (type == OP_PADRANGE)
17276 if (o2) { /* more than one found */
17283 return find_uninit_var(o2, uninit_sv, match, desc_p);
17285 /* scan all args */
17287 sv = find_uninit_var(o, uninit_sv, 1, desc_p);
17299 =for apidoc_section $warning
17300 =for apidoc report_uninit
17302 Print appropriate "Use of uninitialized variable" warning.
17308 Perl_report_uninit(pTHX_ const SV *uninit_sv)
17310 const char *desc = NULL;
17311 SV* varname = NULL;
17314 desc = PL_op->op_type == OP_STRINGIFY && PL_op->op_folded
17316 : PL_op->op_type == OP_MULTICONCAT
17317 && (PL_op->op_private & OPpMULTICONCAT_FAKE)
17320 if (uninit_sv && PL_curpad) {
17321 varname = find_uninit_var(PL_op, uninit_sv, 0, &desc);
17323 sv_insert(varname, 0, 0, " ", 1);
17326 else if (PL_curstackinfo->si_type == PERLSI_SORT && cxstack_ix == 0)
17327 /* we've reached the end of a sort block or sub,
17328 * and the uninit value is probably what that code returned */
17331 /* PL_warn_uninit_sv is constant */
17332 GCC_DIAG_IGNORE_STMT(-Wformat-nonliteral);
17334 /* diag_listed_as: Use of uninitialized value%s */
17335 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit_sv,
17336 SVfARG(varname ? varname : &PL_sv_no),
17339 Perl_warner(aTHX_ packWARN(WARN_UNINITIALIZED), PL_warn_uninit,
17341 GCC_DIAG_RESTORE_STMT;
17345 * ex: set ts=8 sts=4 sw=4 et: