File Coverage

sv.c
Criterion Covered Total %
statement 3585 3928 91.3
branch 3563 4770 74.7
condition n/a
subroutine n/a
total 7148 8698 82.2


line stmt bran cond sub time code
1           /* sv.c
2           *
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
5           * and others
6           *
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.
9           *
10           */
11            
12           /*
13           * 'I wonder what the Entish is for "yes" and "no",' he thought.
14           * --Pippin
15           *
16           * [p.480 of _The Lord of the Rings_, III/iv: "Treebeard"]
17           */
18            
19           /*
20           *
21           *
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
27           * in the pp*.c files.
28           */
29            
30           #include "EXTERN.h"
31           #define PERL_IN_SV_C
32           #include "perl.h"
33           #include "regcomp.h"
34            
35           #ifndef HAS_C99
36           # if __STDC_VERSION__ >= 199901L && !defined(VMS)
37           # define HAS_C99 1
38           # endif
39           #endif
40           #if HAS_C99
41           # include
42           #endif
43            
44           #define FCALL *f
45            
46           #ifdef __Lynx__
47           /* Missing proto on LynxOS */
48           char *gconvert(double, int, int, char *);
49           #endif
50            
51           #ifdef PERL_UTF8_CACHE_ASSERT
52           /* if adding more checks watch out for the following tests:
53           * t/op/index.t t/op/length.t t/op/pat.t t/op/substr.t
54           * lib/utf8.t lib/Unicode/Collate/t/index.t
55           * --jhi
56           */
57           # define ASSERT_UTF8_CACHE(cache) \
58           STMT_START { if (cache) { assert((cache)[0] <= (cache)[1]); \
59           assert((cache)[2] <= (cache)[3]); \
60           assert((cache)[3] <= (cache)[1]);} \
61           } STMT_END
62           #else
63           # define ASSERT_UTF8_CACHE(cache) NOOP
64           #endif
65            
66           #ifdef PERL_OLD_COPY_ON_WRITE
67           #define SV_COW_NEXT_SV(sv) INT2PTR(SV *,SvUVX(sv))
68           #define SV_COW_NEXT_SV_SET(current,next) SvUV_set(current, PTR2UV(next))
69           #endif
70            
71           /* ============================================================================
72            
73           =head1 Allocation and deallocation of SVs.
74            
75           An SV (or AV, HV, etc.) is allocated in two parts: the head (struct
76           sv, av, hv...) contains type and reference count information, and for
77           many types, a pointer to the body (struct xrv, xpv, xpviv...), which
78           contains fields specific to each type. Some types store all they need
79           in the head, so don't have a body.
80            
81           In all but the most memory-paranoid configurations (ex: PURIFY), heads
82           and bodies are allocated out of arenas, which by default are
83           approximately 4K chunks of memory parcelled up into N heads or bodies.
84           Sv-bodies are allocated by their sv-type, guaranteeing size
85           consistency needed to allocate safely from arrays.
86            
87           For SV-heads, the first slot in each arena is reserved, and holds a
88           link to the next arena, some flags, and a note of the number of slots.
89           Snaked through each arena chain is a linked list of free items; when
90           this becomes empty, an extra arena is allocated and divided up into N
91           items which are threaded into the free list.
92            
93           SV-bodies are similar, but they use arena-sets by default, which
94           separate the link and info from the arena itself, and reclaim the 1st
95           slot in the arena. SV-bodies are further described later.
96            
97           The following global variables are associated with arenas:
98            
99           PL_sv_arenaroot pointer to list of SV arenas
100           PL_sv_root pointer to list of free SV structures
101            
102           PL_body_arenas head of linked-list of body arenas
103           PL_body_roots[] array of pointers to list of free bodies of svtype
104           arrays are indexed by the svtype needed
105            
106           A few special SV heads are not allocated from an arena, but are
107           instead directly created in the interpreter structure, eg PL_sv_undef.
108           The size of arenas can be changed from the default by setting
109           PERL_ARENA_SIZE appropriately at compile time.
110            
111           The SV arena serves the secondary purpose of allowing still-live SVs
112           to be located and destroyed during final cleanup.
113            
114           At the lowest level, the macros new_SV() and del_SV() grab and free
115           an SV head. (If debugging with -DD, del_SV() calls the function S_del_sv()
116           to return the SV to the free list with error checking.) new_SV() calls
117           more_sv() / sv_add_arena() to add an extra arena if the free list is empty.
118           SVs in the free list have their SvTYPE field set to all ones.
119            
120           At the time of very final cleanup, sv_free_arenas() is called from
121           perl_destruct() to physically free all the arenas allocated since the
122           start of the interpreter.
123            
124           The function visit() scans the SV arenas list, and calls a specified
125           function for each SV it finds which is still live - ie which has an SvTYPE
126           other than all 1's, and a non-zero SvREFCNT. visit() is used by the
127           following functions (specified as [function that calls visit()] / [function
128           called by visit() for each SV]):
129            
130           sv_report_used() / do_report_used()
131           dump all remaining SVs (debugging aid)
132            
133           sv_clean_objs() / do_clean_objs(),do_clean_named_objs(),
134           do_clean_named_io_objs(),do_curse()
135           Attempt to free all objects pointed to by RVs,
136           try to do the same for all objects indir-
137           ectly referenced by typeglobs too, and
138           then do a final sweep, cursing any
139           objects that remain. Called once from
140           perl_destruct(), prior to calling sv_clean_all()
141           below.
142            
143           sv_clean_all() / do_clean_all()
144           SvREFCNT_dec(sv) each remaining SV, possibly
145           triggering an sv_free(). It also sets the
146           SVf_BREAK flag on the SV to indicate that the
147           refcnt has been artificially lowered, and thus
148           stopping sv_free() from giving spurious warnings
149           about SVs which unexpectedly have a refcnt
150           of zero. called repeatedly from perl_destruct()
151           until there are no SVs left.
152            
153           =head2 Arena allocator API Summary
154            
155           Private API to rest of sv.c
156            
157           new_SV(), del_SV(),
158            
159           new_XPVNV(), del_XPVGV(),
160           etc
161            
162           Public API:
163            
164           sv_report_used(), sv_clean_objs(), sv_clean_all(), sv_free_arenas()
165            
166           =cut
167            
168           * ========================================================================= */
169            
170           /*
171           * "A time to plant, and a time to uproot what was planted..."
172           */
173            
174           #ifdef PERL_MEM_LOG
175           # define MEM_LOG_NEW_SV(sv, file, line, func) \
176           Perl_mem_log_new_sv(sv, file, line, func)
177           # define MEM_LOG_DEL_SV(sv, file, line, func) \
178           Perl_mem_log_del_sv(sv, file, line, func)
179           #else
180           # define MEM_LOG_NEW_SV(sv, file, line, func) NOOP
181           # define MEM_LOG_DEL_SV(sv, file, line, func) NOOP
182           #endif
183            
184           #ifdef DEBUG_LEAKING_SCALARS
185           # define FREE_SV_DEBUG_FILE(sv) STMT_START { \
186           if ((sv)->sv_debug_file) PerlMemShared_free((sv)->sv_debug_file); \
187           } STMT_END
188           # define DEBUG_SV_SERIAL(sv) \
189           DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) del_SV\n", \
190           PTR2UV(sv), (long)(sv)->sv_debug_serial))
191           #else
192           # define FREE_SV_DEBUG_FILE(sv)
193           # define DEBUG_SV_SERIAL(sv) NOOP
194           #endif
195            
196           #ifdef PERL_POISON
197           # define SvARENA_CHAIN(sv) ((sv)->sv_u.svu_rv)
198           # define SvARENA_CHAIN_SET(sv,val) (sv)->sv_u.svu_rv = MUTABLE_SV((val))
199           /* Whilst I'd love to do this, it seems that things like to check on
200           unreferenced scalars
201           # define POSION_SV_HEAD(sv) PoisonNew(sv, 1, struct STRUCT_SV)
202           */
203           # define POSION_SV_HEAD(sv) PoisonNew(&SvANY(sv), 1, void *), \
204           PoisonNew(&SvREFCNT(sv), 1, U32)
205           #else
206           # define SvARENA_CHAIN(sv) SvANY(sv)
207           # define SvARENA_CHAIN_SET(sv,val) SvANY(sv) = (void *)(val)
208           # define POSION_SV_HEAD(sv)
209           #endif
210            
211           /* Mark an SV head as unused, and add to free list.
212           *
213           * If SVf_BREAK is set, skip adding it to the free list, as this SV had
214           * its refcount artificially decremented during global destruction, so
215           * there may be dangling pointers to it. The last thing we want in that
216           * case is for it to be reused. */
217            
218           #define plant_SV(p) \
219           STMT_START { \
220           const U32 old_flags = SvFLAGS(p); \
221           MEM_LOG_DEL_SV(p, __FILE__, __LINE__, FUNCTION__); \
222           DEBUG_SV_SERIAL(p); \
223           FREE_SV_DEBUG_FILE(p); \
224           POSION_SV_HEAD(p); \
225           SvFLAGS(p) = SVTYPEMASK; \
226           if (!(old_flags & SVf_BREAK)) { \
227           SvARENA_CHAIN_SET(p, PL_sv_root); \
228           PL_sv_root = (p); \
229           } \
230           --PL_sv_count; \
231           } STMT_END
232            
233           #define uproot_SV(p) \
234           STMT_START { \
235           (p) = PL_sv_root; \
236           PL_sv_root = MUTABLE_SV(SvARENA_CHAIN(p)); \
237           ++PL_sv_count; \
238           } STMT_END
239            
240            
241           /* make some more SVs by adding another arena */
242            
243           STATIC SV*
244 4257580         S_more_sv(pTHX)
245           {
246           dVAR;
247           SV* sv;
248           char *chunk; /* must use New here to match call to */
249 4257580         Newx(chunk,PERL_ARENA_SIZE,char); /* Safefree() in sv_free_arenas() */
250           sv_add_arena(chunk, PERL_ARENA_SIZE, 0);
251 4257580         uproot_SV(sv);
252 4257580         return sv;
253           }
254            
255           /* new_SV(): return a new, empty SV head */
256            
257           #ifdef DEBUG_LEAKING_SCALARS
258           /* provide a real function for a debugger to play with */
259           STATIC SV*
260           S_new_SV(pTHX_ const char *file, int line, const char *func)
261           {
262           SV* sv;
263            
264           if (PL_sv_root)
265           uproot_SV(sv);
266           else
267           sv = S_more_sv(aTHX);
268           SvANY(sv) = 0;
269           SvREFCNT(sv) = 1;
270           SvFLAGS(sv) = 0;
271           sv->sv_debug_optype = PL_op ? PL_op->op_type : 0;
272           sv->sv_debug_line = (U16) (PL_parser && PL_parser->copline != NOLINE
273           ? PL_parser->copline
274           : PL_curcop
275           ? CopLINE(PL_curcop)
276           : 0
277           );
278           sv->sv_debug_inpad = 0;
279           sv->sv_debug_parent = NULL;
280           sv->sv_debug_file = PL_curcop ? savesharedpv(CopFILE(PL_curcop)): NULL;
281            
282           sv->sv_debug_serial = PL_sv_serial++;
283            
284           MEM_LOG_NEW_SV(sv, file, line, func);
285           DEBUG_m(PerlIO_printf(Perl_debug_log, "0x%"UVxf": (%05ld) new_SV (from %s:%d [%s])\n",
286           PTR2UV(sv), (long)sv->sv_debug_serial, file, line, func));
287            
288           return sv;
289           }
290           # define new_SV(p) (p)=S_new_SV(aTHX_ __FILE__, __LINE__, FUNCTION__)
291            
292           #else
293           # define new_SV(p) \
294           STMT_START { \
295           if (PL_sv_root) \
296           uproot_SV(p); \
297           else \
298           (p) = S_more_sv(aTHX); \
299           SvANY(p) = 0; \
300           SvREFCNT(p) = 1; \
301           SvFLAGS(p) = 0; \
302           MEM_LOG_NEW_SV(p, __FILE__, __LINE__, FUNCTION__); \
303           } STMT_END
304           #endif
305            
306            
307           /* del_SV(): return an empty SV head to the free list */
308            
309           #ifdef DEBUGGING
310            
311           #define del_SV(p) \
312           STMT_START { \
313           if (DEBUG_D_TEST) \
314           del_sv(p); \
315           else \
316           plant_SV(p); \
317           } STMT_END
318            
319           STATIC void
320           S_del_sv(pTHX_ SV *p)
321           {
322           dVAR;
323            
324           PERL_ARGS_ASSERT_DEL_SV;
325            
326           if (DEBUG_D_TEST) {
327           SV* sva;
328           bool ok = 0;
329           for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) {
330           const SV * const sv = sva + 1;
331           const SV * const svend = &sva[SvREFCNT(sva)];
332           if (p >= sv && p < svend) {
333           ok = 1;
334           break;
335           }
336           }
337           if (!ok) {
338           Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
339           "Attempt to free non-arena SV: 0x%"UVxf
340           pTHX__FORMAT, PTR2UV(p) pTHX__VALUE);
341           return;
342           }
343           }
344           plant_SV(p);
345           }
346            
347           #else /* ! DEBUGGING */
348            
349           #define del_SV(p) plant_SV(p)
350            
351           #endif /* DEBUGGING */
352            
353            
354           /*
355           =head1 SV Manipulation Functions
356            
357           =for apidoc sv_add_arena
358            
359           Given a chunk of memory, link it to the head of the list of arenas,
360           and split it into a list of free SVs.
361            
362           =cut
363           */
364            
365           static void
366           S_sv_add_arena(pTHX_ char *const ptr, const U32 size, const U32 flags)
367           {
368           dVAR;
369           SV *const sva = MUTABLE_SV(ptr);
370           SV* sv;
371           SV* svend;
372            
373           PERL_ARGS_ASSERT_SV_ADD_ARENA;
374            
375           /* The first SV in an arena isn't an SV. */
376 4257580         SvANY(sva) = (void *) PL_sv_arenaroot; /* ptr to next arena */
377 4257580         SvREFCNT(sva) = size / sizeof(SV); /* number of SV slots */
378 4257580         SvFLAGS(sva) = flags; /* FAKE if not to be freed */
379            
380 4257580         PL_sv_arenaroot = sva;
381 4257580         PL_sv_root = sva + 1;
382            
383 4257580         svend = &sva[SvREFCNT(sva) - 1];
384 4257580         sv = sva + 1;
385 719531020 100       while (sv < svend) {
386 715273440         SvARENA_CHAIN_SET(sv, (sv + 1));
387           #ifdef DEBUGGING
388           SvREFCNT(sv) = 0;
389           #endif
390           /* Must always set typemask because it's always checked in on cleanup
391           when the arenas are walked looking for objects. */
392 715273440         SvFLAGS(sv) = SVTYPEMASK;
393 715273440         sv++;
394           }
395 4257580         SvARENA_CHAIN_SET(sv, 0);
396           #ifdef DEBUGGING
397           SvREFCNT(sv) = 0;
398           #endif
399 4257580         SvFLAGS(sv) = SVTYPEMASK;
400           }
401            
402           /* visit(): call the named function for each non-free SV in the arenas
403           * whose flags field matches the flags/mask args. */
404            
405           STATIC I32
406 97372         S_visit(pTHX_ SVFUNC_t f, const U32 flags, const U32 mask)
407           {
408           dVAR;
409           SV* sva;
410           I32 visited = 0;
411            
412           PERL_ARGS_ASSERT_VISIT;
413            
414 17127050 100       for (sva = PL_sv_arenaroot; sva; sva = MUTABLE_SV(SvANY(sva))) {
415 17029682         const SV * const svend = &sva[SvREFCNT(sva)];
416           SV* sv;
417 2895045320 100       for (sv = sva + 1; sv < svend; ++sv) {
418 2878015642 100       if (SvTYPE(sv) != (svtype)SVTYPEMASK
419 1941832460 100       && (sv->sv_flags & mask) == flags
420 95168879 100       && SvREFCNT(sv))
421           {
422 95168875         (FCALL)(aTHX_ sv);
423 95168871         ++visited;
424           }
425           }
426           }
427 97368         return visited;
428           }
429            
430           #ifdef DEBUGGING
431            
432           /* called by sv_report_used() for each live SV */
433            
434           static void
435           do_report_used(pTHX_ SV *const sv)
436           {
437           if (SvTYPE(sv) != (svtype)SVTYPEMASK) {
438           PerlIO_printf(Perl_debug_log, "****\n");
439           sv_dump(sv);
440           }
441           }
442           #endif
443            
444           /*
445           =for apidoc sv_report_used
446            
447           Dump the contents of all SVs not yet freed (debugging aid).
448            
449           =cut
450           */
451            
452           void
453 0         Perl_sv_report_used(pTHX)
454           {
455           #ifdef DEBUGGING
456           visit(do_report_used, 0, 0);
457           #else
458           PERL_UNUSED_CONTEXT;
459           #endif
460 0         }
461            
462           /* called by sv_clean_objs() for each live SV */
463            
464           static void
465 39575099         do_clean_objs(pTHX_ SV *const ref)
466           {
467           dVAR;
468           assert (SvROK(ref));
469           {
470 39575099         SV * const target = SvRV(ref);
471 39575099 100       if (SvOBJECT(target)) {
472           DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning object ref:\n "), sv_dump(ref)));
473 1957680 100       if (SvWEAKREF(ref)) {
474 2         sv_del_backref(target, ref);
475 2         SvWEAKREF_off(ref);
476 2         SvRV_set(ref, NULL);
477           } else {
478 1957678         SvROK_off(ref);
479 1957678         SvRV_set(ref, NULL);
480 1957678         SvREFCNT_dec_NN(target);
481           }
482           }
483           }
484 39575095         }
485            
486            
487           /* clear any slots in a GV which hold objects - except IO;
488           * called by sv_clean_objs() for each live GV */
489            
490           static void
491 27766979         do_clean_named_objs(pTHX_ SV *const sv)
492           {
493           dVAR;
494           SV *obj;
495           assert(SvTYPE(sv) == SVt_PVGV);
496           assert(isGV_with_GP(sv));
497 27766979 50       if (!GvGP(sv))
498 27766979         return;
499            
500           /* freeing GP entries may indirectly free the current GV;
501           * hold onto it while we mess with the GP slots */
502           SvREFCNT_inc(sv);
503            
504 27766979 100       if ( ((obj = GvSV(sv) )) && SvOBJECT(obj)) {
    100        
505           DEBUG_D((PerlIO_printf(Perl_debug_log,
506           "Cleaning named glob SV object:\n "), sv_dump(obj)));
507 330         GvSV(sv) = NULL;
508 330         SvREFCNT_dec_NN(obj);
509           }
510 27766979 100       if ( ((obj = MUTABLE_SV(GvAV(sv)) )) && SvOBJECT(obj)) {
    100        
511           DEBUG_D((PerlIO_printf(Perl_debug_log,
512           "Cleaning named glob AV object:\n "), sv_dump(obj)));
513 4         GvAV(sv) = NULL;
514 4         SvREFCNT_dec_NN(obj);
515           }
516 27766979 100       if ( ((obj = MUTABLE_SV(GvHV(sv)) )) && SvOBJECT(obj)) {
    100        
517           DEBUG_D((PerlIO_printf(Perl_debug_log,
518           "Cleaning named glob HV object:\n "), sv_dump(obj)));
519 64         GvHV(sv) = NULL;
520 64         SvREFCNT_dec_NN(obj);
521           }
522 27766979 100       if ( ((obj = MUTABLE_SV(GvCV(sv)) )) && SvOBJECT(obj)) {
    100        
523           DEBUG_D((PerlIO_printf(Perl_debug_log,
524           "Cleaning named glob CV object:\n "), sv_dump(obj)));
525 8         GvCV_set(sv, NULL);
526 8         SvREFCNT_dec_NN(obj);
527           }
528 27766979         SvREFCNT_dec_NN(sv); /* undo the inc above */
529           }
530            
531           /* clear any IO slots in a GV which hold objects (except stderr, defout);
532           * called by sv_clean_objs() for each live GV */
533            
534           static void
535 27821727         do_clean_named_io_objs(pTHX_ SV *const sv)
536           {
537           dVAR;
538           SV *obj;
539           assert(SvTYPE(sv) == SVt_PVGV);
540           assert(isGV_with_GP(sv));
541 27821727 50       if (!GvGP(sv) || sv == (SV*)PL_stderrgv || sv == (SV*)PL_defoutgv)
    100        
    100        
542 27821727         return;
543            
544           SvREFCNT_inc(sv);
545 27773283 50       if ( ((obj = MUTABLE_SV(GvIO(sv)) )) && SvOBJECT(obj)) {
    50        
    50        
    100        
    50        
546           DEBUG_D((PerlIO_printf(Perl_debug_log,
547           "Cleaning named glob IO object:\n "), sv_dump(obj)));
548 270939         GvIOp(sv) = NULL;
549 270939         SvREFCNT_dec_NN(obj);
550           }
551 27773283         SvREFCNT_dec_NN(sv); /* undo the inc above */
552           }
553            
554           /* Void wrapper to pass to visit() */
555           static void
556 53514         do_curse(pTHX_ SV * const sv) {
557 53514 100       if ((PL_stderrgv && GvGP(PL_stderrgv) && (SV*)GvIO(PL_stderrgv) == sv)
    50        
    50        
    50        
    50        
    100        
558 29310 50       || (PL_defoutgv && GvGP(PL_defoutgv) && (SV*)GvIO(PL_defoutgv) == sv))
    50        
    50        
    50        
    50        
    100        
559 53514         return;
560 9748         (void)curse(sv, 0);
561           }
562            
563           /*
564           =for apidoc sv_clean_objs
565            
566           Attempt to destroy all objects not yet freed.
567            
568           =cut
569           */
570            
571           void
572 24346         Perl_sv_clean_objs(pTHX)
573           {
574           dVAR;
575           GV *olddef, *olderr;
576 24346         PL_in_clean_objs = TRUE;
577 24346         visit(do_clean_objs, SVf_ROK, SVf_ROK);
578           /* Some barnacles may yet remain, clinging to typeglobs.
579           * Run the non-IO destructors first: they may want to output
580           * error messages, close files etc */
581 24342         visit(do_clean_named_objs, SVt_PVGV|SVpgv_GP, SVTYPEMASK|SVp_POK|SVpgv_GP);
582 24342         visit(do_clean_named_io_objs, SVt_PVGV|SVpgv_GP, SVTYPEMASK|SVp_POK|SVpgv_GP);
583           /* And if there are some very tenacious barnacles clinging to arrays,
584           closures, or what have you.... */
585 24342         visit(do_curse, SVs_OBJECT, SVs_OBJECT);
586 24342         olddef = PL_defoutgv;
587 24342         PL_defoutgv = NULL; /* disable skip of PL_defoutgv */
588 24342 100       if (olddef && isGV_with_GP(olddef))
    50        
    50        
589 24224         do_clean_named_io_objs(aTHX_ MUTABLE_SV(olddef));
590 24342         olderr = PL_stderrgv;
591 24342         PL_stderrgv = NULL; /* disable skip of PL_stderrgv */
592 24342 100       if (olderr && isGV_with_GP(olderr))
    50        
    50        
593 24220         do_clean_named_io_objs(aTHX_ MUTABLE_SV(olderr));
594 24342         SvREFCNT_dec(olddef);
595 24342         PL_in_clean_objs = FALSE;
596 24342         }
597            
598           /* called by sv_clean_all() for each live SV */
599            
600           static void
601 0         do_clean_all(pTHX_ SV *const sv)
602           {
603           dVAR;
604 0 0       if (sv == (const SV *) PL_fdpid || sv == (const SV *)PL_strtab) {
    0        
605           /* don't clean pid table and strtab */
606 0         return;
607           }
608           DEBUG_D((PerlIO_printf(Perl_debug_log, "Cleaning loops: SV at 0x%"UVxf"\n", PTR2UV(sv)) ));
609 0         SvFLAGS(sv) |= SVf_BREAK;
610 0         SvREFCNT_dec_NN(sv);
611           }
612            
613           /*
614           =for apidoc sv_clean_all
615            
616           Decrement the refcnt of each remaining SV, possibly triggering a
617           cleanup. This function may have to be called multiple times to free
618           SVs which are in complex self-referential hierarchies.
619            
620           =cut
621           */
622            
623           I32
624 0         Perl_sv_clean_all(pTHX)
625           {
626           dVAR;
627           I32 cleaned;
628 0         PL_in_clean_all = TRUE;
629 0         cleaned = visit(do_clean_all, 0,0);
630 0         return cleaned;
631           }
632            
633           /*
634           ARENASETS: a meta-arena implementation which separates arena-info
635           into struct arena_set, which contains an array of struct
636           arena_descs, each holding info for a single arena. By separating
637           the meta-info from the arena, we recover the 1st slot, formerly
638           borrowed for list management. The arena_set is about the size of an
639           arena, avoiding the needless malloc overhead of a naive linked-list.
640            
641           The cost is 1 arena-set malloc per ~320 arena-mallocs, + the unused
642           memory in the last arena-set (1/2 on average). In trade, we get
643           back the 1st slot in each arena (ie 1.7% of a CV-arena, less for
644           smaller types). The recovery of the wasted space allows use of
645           small arenas for large, rare body types, by changing array* fields
646           in body_details_by_type[] below.
647           */
648           struct arena_desc {
649           char *arena; /* the raw storage, allocated aligned */
650           size_t size; /* its size ~4k typ */
651           svtype utype; /* bodytype stored in arena */
652           };
653            
654           struct arena_set;
655            
656           /* Get the maximum number of elements in set[] such that struct arena_set
657           will fit within PERL_ARENA_SIZE, which is probably just under 4K, and
658           therefore likely to be 1 aligned memory page. */
659            
660           #define ARENAS_PER_SET ((PERL_ARENA_SIZE - sizeof(struct arena_set*) \
661           - 2 * sizeof(int)) / sizeof (struct arena_desc))
662            
663           struct arena_set {
664           struct arena_set* next;
665           unsigned int set_size; /* ie ARENAS_PER_SET */
666           unsigned int curr; /* index of next available arena-desc */
667           struct arena_desc set[ARENAS_PER_SET];
668           };
669            
670           /*
671           =for apidoc sv_free_arenas
672            
673           Deallocate the memory used by all arenas. Note that all the individual SV
674           heads and bodies within the arenas must already have been freed.
675            
676           =cut
677           */
678           void
679 0         Perl_sv_free_arenas(pTHX)
680           {
681           dVAR;
682           SV* sva;
683           SV* svanext;
684           unsigned int i;
685            
686           /* Free arenas here, but be careful about fake ones. (We assume
687           contiguity of the fake ones with the corresponding real ones.) */
688            
689 0 0       for (sva = PL_sv_arenaroot; sva; sva = svanext) {
690 0         svanext = MUTABLE_SV(SvANY(sva));
691 0 0       while (svanext && SvFAKE(svanext))
    0        
692 0         svanext = MUTABLE_SV(SvANY(svanext));
693            
694 0 0       if (!SvFAKE(sva))
695 0         Safefree(sva);
696           }
697            
698           {
699 0         struct arena_set *aroot = (struct arena_set*) PL_body_arenas;
700            
701 0 0       while (aroot) {
702           struct arena_set *current = aroot;
703 0         i = aroot->curr;
704 0 0       while (i--) {
705           assert(aroot->set[i].arena);
706 0         Safefree(aroot->set[i].arena);
707           }
708 0         aroot = aroot->next;
709 0         Safefree(current);
710           }
711           }
712 0         PL_body_arenas = 0;
713            
714           i = PERL_ARENA_ROOTS_SIZE;
715 0 0       while (i--)
716 0         PL_body_roots[i] = 0;
717            
718 0         PL_sv_arenaroot = 0;
719 0         PL_sv_root = 0;
720 0         }
721            
722           /*
723           Here are mid-level routines that manage the allocation of bodies out
724           of the various arenas. There are 5 kinds of arenas:
725            
726           1. SV-head arenas, which are discussed and handled above
727           2. regular body arenas
728           3. arenas for reduced-size bodies
729           4. Hash-Entry arenas
730            
731           Arena types 2 & 3 are chained by body-type off an array of
732           arena-root pointers, which is indexed by svtype. Some of the
733           larger/less used body types are malloced singly, since a large
734           unused block of them is wasteful. Also, several svtypes dont have
735           bodies; the data fits into the sv-head itself. The arena-root
736           pointer thus has a few unused root-pointers (which may be hijacked
737           later for arena types 4,5)
738            
739           3 differs from 2 as an optimization; some body types have several
740           unused fields in the front of the structure (which are kept in-place
741           for consistency). These bodies can be allocated in smaller chunks,
742           because the leading fields arent accessed. Pointers to such bodies
743           are decremented to point at the unused 'ghost' memory, knowing that
744           the pointers are used with offsets to the real memory.
745            
746            
747           =head1 SV-Body Allocation
748            
749           Allocation of SV-bodies is similar to SV-heads, differing as follows;
750           the allocation mechanism is used for many body types, so is somewhat
751           more complicated, it uses arena-sets, and has no need for still-live
752           SV detection.
753            
754           At the outermost level, (new|del)_X*V macros return bodies of the
755           appropriate type. These macros call either (new|del)_body_type or
756           (new|del)_body_allocated macro pairs, depending on specifics of the
757           type. Most body types use the former pair, the latter pair is used to
758           allocate body types with "ghost fields".
759            
760           "ghost fields" are fields that are unused in certain types, and
761           consequently don't need to actually exist. They are declared because
762           they're part of a "base type", which allows use of functions as
763           methods. The simplest examples are AVs and HVs, 2 aggregate types
764           which don't use the fields which support SCALAR semantics.
765            
766           For these types, the arenas are carved up into appropriately sized
767           chunks, we thus avoid wasted memory for those unaccessed members.
768           When bodies are allocated, we adjust the pointer back in memory by the
769           size of the part not allocated, so it's as if we allocated the full
770           structure. (But things will all go boom if you write to the part that
771           is "not there", because you'll be overwriting the last members of the
772           preceding structure in memory.)
773            
774           We calculate the correction using the STRUCT_OFFSET macro on the first
775           member present. If the allocated structure is smaller (no initial NV
776           actually allocated) then the net effect is to subtract the size of the NV
777           from the pointer, to return a new pointer as if an initial NV were actually
778           allocated. (We were using structures named *_allocated for this, but
779           this turned out to be a subtle bug, because a structure without an NV
780           could have a lower alignment constraint, but the compiler is allowed to
781           optimised accesses based on the alignment constraint of the actual pointer
782           to the full structure, for example, using a single 64 bit load instruction
783           because it "knows" that two adjacent 32 bit members will be 8-byte aligned.)
784            
785           This is the same trick as was used for NV and IV bodies. Ironically it
786           doesn't need to be used for NV bodies any more, because NV is now at
787           the start of the structure. IV bodies don't need it either, because
788           they are no longer allocated.
789            
790           In turn, the new_body_* allocators call S_new_body(), which invokes
791           new_body_inline macro, which takes a lock, and takes a body off the
792           linked list at PL_body_roots[sv_type], calling Perl_more_bodies() if
793           necessary to refresh an empty list. Then the lock is released, and
794           the body is returned.
795            
796           Perl_more_bodies allocates a new arena, and carves it up into an array of N
797           bodies, which it strings into a linked list. It looks up arena-size
798           and body-size from the body_details table described below, thus
799           supporting the multiple body-types.
800            
801           If PURIFY is defined, or PERL_ARENA_SIZE=0, arenas are not used, and
802           the (new|del)_X*V macros are mapped directly to malloc/free.
803            
804           For each sv-type, struct body_details bodies_by_type[] carries
805           parameters which control these aspects of SV handling:
806            
807           Arena_size determines whether arenas are used for this body type, and if
808           so, how big they are. PURIFY or PERL_ARENA_SIZE=0 set this field to
809           zero, forcing individual mallocs and frees.
810            
811           Body_size determines how big a body is, and therefore how many fit into
812           each arena. Offset carries the body-pointer adjustment needed for
813           "ghost fields", and is used in *_allocated macros.
814            
815           But its main purpose is to parameterize info needed in
816           Perl_sv_upgrade(). The info here dramatically simplifies the function
817           vs the implementation in 5.8.8, making it table-driven. All fields
818           are used for this, except for arena_size.
819            
820           For the sv-types that have no bodies, arenas are not used, so those
821           PL_body_roots[sv_type] are unused, and can be overloaded. In
822           something of a special case, SVt_NULL is borrowed for HE arenas;
823           PL_body_roots[HE_SVSLOT=SVt_NULL] is filled by S_more_he, but the
824           bodies_by_type[SVt_NULL] slot is not used, as the table is not
825           available in hv.c.
826            
827           */
828            
829           struct body_details {
830           U8 body_size; /* Size to allocate */
831           U8 copy; /* Size of structure to copy (may be shorter) */
832           U8 offset;
833           unsigned int type : 4; /* We have space for a sanity check. */
834           unsigned int cant_upgrade : 1; /* Cannot upgrade this type */
835           unsigned int zero_nv : 1; /* zero the NV when upgrading from this */
836           unsigned int arena : 1; /* Allocated from an arena */
837           size_t arena_size; /* Size of arena to allocate */
838           };
839            
840           #define HADNV FALSE
841           #define NONV TRUE
842            
843            
844           #ifdef PURIFY
845           /* With -DPURFIY we allocate everything directly, and don't use arenas.
846           This seems a rather elegant way to simplify some of the code below. */
847           #define HASARENA FALSE
848           #else
849           #define HASARENA TRUE
850           #endif
851           #define NOARENA FALSE
852            
853           /* Size the arenas to exactly fit a given number of bodies. A count
854           of 0 fits the max number bodies into a PERL_ARENA_SIZE.block,
855           simplifying the default. If count > 0, the arena is sized to fit
856           only that many bodies, allowing arenas to be used for large, rare
857           bodies (XPVFM, XPVIO) without undue waste. The arena size is
858           limited by PERL_ARENA_SIZE, so we can safely oversize the
859           declarations.
860           */
861           #define FIT_ARENA0(body_size) \
862           ((size_t)(PERL_ARENA_SIZE / body_size) * body_size)
863           #define FIT_ARENAn(count,body_size) \
864           ( count * body_size <= PERL_ARENA_SIZE) \
865           ? count * body_size \
866           : FIT_ARENA0 (body_size)
867           #define FIT_ARENA(count,body_size) \
868           count \
869           ? FIT_ARENAn (count, body_size) \
870           : FIT_ARENA0 (body_size)
871            
872           /* Calculate the length to copy. Specifically work out the length less any
873           final padding the compiler needed to add. See the comment in sv_upgrade
874           for why copying the padding proved to be a bug. */
875            
876           #define copy_length(type, last_member) \
877           STRUCT_OFFSET(type, last_member) \
878           + sizeof (((type*)SvANY((const SV *)0))->last_member)
879            
880           static const struct body_details bodies_by_type[] = {
881           /* HEs use this offset for their arena. */
882           { 0, 0, 0, SVt_NULL, FALSE, NONV, NOARENA, 0 },
883            
884           /* IVs are in the head, so the allocation size is 0. */
885           { 0,
886           sizeof(IV), /* This is used to copy out the IV body. */
887           STRUCT_OFFSET(XPVIV, xiv_iv), SVt_IV, FALSE, NONV,
888           NOARENA /* IVS don't need an arena */, 0
889           },
890            
891           { sizeof(NV), sizeof(NV),
892           STRUCT_OFFSET(XPVNV, xnv_u),
893           SVt_NV, FALSE, HADNV, HASARENA, FIT_ARENA(0, sizeof(NV)) },
894            
895           { sizeof(XPV) - STRUCT_OFFSET(XPV, xpv_cur),
896           copy_length(XPV, xpv_len) - STRUCT_OFFSET(XPV, xpv_cur),
897           + STRUCT_OFFSET(XPV, xpv_cur),
898           SVt_PV, FALSE, NONV, HASARENA,
899           FIT_ARENA(0, sizeof(XPV) - STRUCT_OFFSET(XPV, xpv_cur)) },
900            
901           { sizeof(XINVLIST) - STRUCT_OFFSET(XPV, xpv_cur),
902           copy_length(XINVLIST, is_offset) - STRUCT_OFFSET(XPV, xpv_cur),
903           + STRUCT_OFFSET(XPV, xpv_cur),
904           SVt_INVLIST, TRUE, NONV, HASARENA,
905           FIT_ARENA(0, sizeof(XINVLIST) - STRUCT_OFFSET(XPV, xpv_cur)) },
906            
907           { sizeof(XPVIV) - STRUCT_OFFSET(XPV, xpv_cur),
908           copy_length(XPVIV, xiv_u) - STRUCT_OFFSET(XPV, xpv_cur),
909           + STRUCT_OFFSET(XPV, xpv_cur),
910           SVt_PVIV, FALSE, NONV, HASARENA,
911           FIT_ARENA(0, sizeof(XPVIV) - STRUCT_OFFSET(XPV, xpv_cur)) },
912            
913           { sizeof(XPVNV) - STRUCT_OFFSET(XPV, xpv_cur),
914           copy_length(XPVNV, xnv_u) - STRUCT_OFFSET(XPV, xpv_cur),
915           + STRUCT_OFFSET(XPV, xpv_cur),
916           SVt_PVNV, FALSE, HADNV, HASARENA,
917           FIT_ARENA(0, sizeof(XPVNV) - STRUCT_OFFSET(XPV, xpv_cur)) },
918            
919           { sizeof(XPVMG), copy_length(XPVMG, xnv_u), 0, SVt_PVMG, FALSE, HADNV,
920           HASARENA, FIT_ARENA(0, sizeof(XPVMG)) },
921            
922           { sizeof(regexp),
923           sizeof(regexp),
924           0,
925           SVt_REGEXP, TRUE, NONV, HASARENA,
926           FIT_ARENA(0, sizeof(regexp))
927           },
928            
929           { sizeof(XPVGV), sizeof(XPVGV), 0, SVt_PVGV, TRUE, HADNV,
930           HASARENA, FIT_ARENA(0, sizeof(XPVGV)) },
931          
932           { sizeof(XPVLV), sizeof(XPVLV), 0, SVt_PVLV, TRUE, HADNV,
933           HASARENA, FIT_ARENA(0, sizeof(XPVLV)) },
934            
935           { sizeof(XPVAV),
936           copy_length(XPVAV, xav_alloc),
937           0,
938           SVt_PVAV, TRUE, NONV, HASARENA,
939           FIT_ARENA(0, sizeof(XPVAV)) },
940            
941           { sizeof(XPVHV),
942           copy_length(XPVHV, xhv_max),
943           0,
944           SVt_PVHV, TRUE, NONV, HASARENA,
945           FIT_ARENA(0, sizeof(XPVHV)) },
946            
947           { sizeof(XPVCV),
948           sizeof(XPVCV),
949           0,
950           SVt_PVCV, TRUE, NONV, HASARENA,
951           FIT_ARENA(0, sizeof(XPVCV)) },
952            
953           { sizeof(XPVFM),
954           sizeof(XPVFM),
955           0,
956           SVt_PVFM, TRUE, NONV, NOARENA,
957           FIT_ARENA(20, sizeof(XPVFM)) },
958            
959           { sizeof(XPVIO),
960           sizeof(XPVIO),
961           0,
962           SVt_PVIO, TRUE, NONV, HASARENA,
963           FIT_ARENA(24, sizeof(XPVIO)) },
964           };
965            
966           #define new_body_allocated(sv_type) \
967           (void *)((char *)S_new_body(aTHX_ sv_type) \
968           - bodies_by_type[sv_type].offset)
969            
970           /* return a thing to the free list */
971            
972           #define del_body(thing, root) \
973           STMT_START { \
974           void ** const thing_copy = (void **)thing; \
975           *thing_copy = *root; \
976           *root = (void*)thing_copy; \
977           } STMT_END
978            
979           #ifdef PURIFY
980            
981           #define new_XNV() safemalloc(sizeof(XPVNV))
982           #define new_XPVNV() safemalloc(sizeof(XPVNV))
983           #define new_XPVMG() safemalloc(sizeof(XPVMG))
984            
985           #define del_XPVGV(p) safefree(p)
986            
987           #else /* !PURIFY */
988            
989           #define new_XNV() new_body_allocated(SVt_NV)
990           #define new_XPVNV() new_body_allocated(SVt_PVNV)
991           #define new_XPVMG() new_body_allocated(SVt_PVMG)
992            
993           #define del_XPVGV(p) del_body(p + bodies_by_type[SVt_PVGV].offset, \
994           &PL_body_roots[SVt_PVGV])
995            
996           #endif /* PURIFY */
997            
998           /* no arena for you! */
999            
1000           #define new_NOARENA(details) \
1001           safemalloc((details)->body_size + (details)->offset)
1002           #define new_NOARENAZ(details) \
1003           safecalloc((details)->body_size + (details)->offset, 1)
1004            
1005           void *
1006 4609978         Perl_more_bodies (pTHX_ const svtype sv_type, const size_t body_size,
1007           const size_t arena_size)
1008           {
1009           dVAR;
1010 4609978         void ** const root = &PL_body_roots[sv_type];
1011           struct arena_desc *adesc;
1012 4609978         struct arena_set *aroot = (struct arena_set *) PL_body_arenas;
1013           unsigned int curr;
1014           char *start;
1015           const char *end;
1016           const size_t good_arena_size = Perl_malloc_good_size(arena_size);
1017           #if defined(DEBUGGING) && !defined(PERL_GLOBAL_STRUCT_PRIVATE)
1018           static bool done_sanity_check;
1019            
1020           /* PERL_GLOBAL_STRUCT_PRIVATE cannot coexist with global
1021           * variables like done_sanity_check. */
1022           if (!done_sanity_check) {
1023           unsigned int i = SVt_LAST;
1024            
1025           done_sanity_check = TRUE;
1026            
1027           while (i--)
1028           assert (bodies_by_type[i].type == i);
1029           }
1030           #endif
1031            
1032           assert(arena_size);
1033            
1034           /* may need new arena-set to hold new arena */
1035 4609978 100       if (!aroot || aroot->curr >= aroot->set_size) {
    100        
1036           struct arena_set *newroot;
1037 45172         Newxz(newroot, 1, struct arena_set);
1038 45172         newroot->set_size = ARENAS_PER_SET;
1039 45172         newroot->next = aroot;
1040           aroot = newroot;
1041 45172         PL_body_arenas = (void *) newroot;
1042           DEBUG_m(PerlIO_printf(Perl_debug_log, "new arenaset %p\n", (void*)aroot));
1043           }
1044            
1045           /* ok, now have arena-set with at least 1 empty/available arena-desc */
1046 4609978         curr = aroot->curr++;
1047 4609978         adesc = &(aroot->set[curr]);
1048           assert(!adesc->arena);
1049          
1050 4609978         Newx(adesc->arena, good_arena_size, char);
1051 4609978         adesc->size = good_arena_size;
1052 4609978         adesc->utype = sv_type;
1053           DEBUG_m(PerlIO_printf(Perl_debug_log, "arena %d added: %p size %"UVuf"\n",
1054           curr, (void*)adesc->arena, (UV)good_arena_size));
1055            
1056 4609978         start = (char *) adesc->arena;
1057            
1058           /* Get the address of the byte after the end of the last body we can fit.
1059           Remember, this is integer division: */
1060 4609978         end = start + good_arena_size / body_size * body_size;
1061            
1062           /* computed count doesn't reflect the 1st slot reservation */
1063           #if defined(MYMALLOC) || defined(HAS_MALLOC_GOOD_SIZE)
1064           DEBUG_m(PerlIO_printf(Perl_debug_log,
1065           "arena %p end %p arena-size %d (from %d) type %d "
1066           "size %d ct %d\n",
1067           (void*)start, (void*)end, (int)good_arena_size,
1068           (int)arena_size, sv_type, (int)body_size,
1069           (int)good_arena_size / (int)body_size));
1070           #else
1071           DEBUG_m(PerlIO_printf(Perl_debug_log,
1072           "arena %p end %p arena-size %d type %d size %d ct %d\n",
1073           (void*)start, (void*)end,
1074           (int)arena_size, sv_type, (int)body_size,
1075           (int)good_arena_size / (int)body_size));
1076           #endif
1077 4609978         *root = (void *)start;
1078            
1079           while (1) {
1080           /* Where the next body would start: */
1081 590962958         char * const next = start + body_size;
1082            
1083 590962958 100       if (next >= end) {
1084           /* This is the last body: */
1085           assert(next == end);
1086            
1087 4609978         *(void **)start = 0;
1088 4609978         return *root;
1089           }
1090            
1091 586352980         *(void**) start = (void *)next;
1092           start = next;
1093 586352980         }
1094           }
1095            
1096           /* grab a new thing from the free list, allocating more if necessary.
1097           The inline version is used for speed in hot routines, and the
1098           function using it serves the rest (unless PURIFY).
1099           */
1100           #define new_body_inline(xpv, sv_type) \
1101           STMT_START { \
1102           void ** const r3wt = &PL_body_roots[sv_type]; \
1103           xpv = (PTR_TBL_ENT_t*) (*((void **)(r3wt)) \
1104           ? *((void **)(r3wt)) : Perl_more_bodies(aTHX_ sv_type, \
1105           bodies_by_type[sv_type].body_size,\
1106           bodies_by_type[sv_type].arena_size)); \
1107           *(r3wt) = *(void**)(xpv); \
1108           } STMT_END
1109            
1110           #ifndef PURIFY
1111            
1112           STATIC void *
1113 35916050         S_new_body(pTHX_ const svtype sv_type)
1114           {
1115           dVAR;
1116           void *xpv;
1117 35916050 100       new_body_inline(xpv, sv_type);
1118 35916050         return xpv;
1119           }
1120            
1121           #endif
1122            
1123           static const struct body_details fake_rv =
1124           { 0, 0, 0, SVt_IV, FALSE, NONV, NOARENA, 0 };
1125            
1126           /*
1127           =for apidoc sv_upgrade
1128            
1129           Upgrade an SV to a more complex form. Generally adds a new body type to the
1130           SV, then copies across as much information as possible from the old body.
1131           It croaks if the SV is already in a more complex form than requested. You
1132           generally want to use the C macro wrapper, which checks the type
1133           before calling C, and hence does not croak. See also
1134           C.
1135            
1136           =cut
1137           */
1138            
1139           void
1140 4246335395         Perl_sv_upgrade(pTHX_ SV *const sv, svtype new_type)
1141           {
1142           dVAR;
1143           void* old_body;
1144           void* new_body;
1145 4246335395         const svtype old_type = SvTYPE(sv);
1146           const struct body_details *new_type_details;
1147 4246335395         const struct body_details *old_type_details
1148 4246335395         = bodies_by_type + old_type;
1149           SV *referant = NULL;
1150            
1151           PERL_ARGS_ASSERT_SV_UPGRADE;
1152            
1153 4246335395 100       if (old_type == new_type)
1154           return;
1155            
1156           /* This clause was purposefully added ahead of the early return above to
1157           the shared string hackery for (sort {$a <=> $b} keys %hash), with the
1158           inference by Nick I-S that it would fix other troublesome cases. See
1159           changes 7162, 7163 (f130fd4589cf5fbb24149cd4db4137c8326f49c1 and parent)
1160            
1161           Given that shared hash key scalars are no longer PVIV, but PV, there is
1162           no longer need to unshare so as to free up the IVX slot for its proper
1163           purpose. So it's safe to move the early return earlier. */
1164            
1165 4243940283 100       if (new_type > SVt_PVMG && SvIsCOW(sv)) {
    50        
1166 0         sv_force_normal_flags(sv, 0);
1167           }
1168            
1169 4243940283         old_body = SvANY(sv);
1170            
1171           /* Copying structures onto other structures that have been neatly zeroed
1172           has a subtle gotcha. Consider XPVMG
1173            
1174           +------+------+------+------+------+-------+-------+
1175           | NV | CUR | LEN | IV | MAGIC | STASH |
1176           +------+------+------+------+------+-------+-------+
1177           0 4 8 12 16 20 24 28
1178            
1179           where NVs are aligned to 8 bytes, so that sizeof that structure is
1180           actually 32 bytes long, with 4 bytes of padding at the end:
1181            
1182           +------+------+------+------+------+-------+-------+------+
1183           | NV | CUR | LEN | IV | MAGIC | STASH | ??? |
1184           +------+------+------+------+------+-------+-------+------+
1185           0 4 8 12 16 20 24 28 32
1186            
1187           so what happens if you allocate memory for this structure:
1188            
1189           +------+------+------+------+------+-------+-------+------+------+...
1190           | NV | CUR | LEN | IV | MAGIC | STASH | GP | NAME |
1191           +------+------+------+------+------+-------+-------+------+------+...
1192           0 4 8 12 16 20 24 28 32 36
1193            
1194           zero it, then copy sizeof(XPVMG) bytes on top of it? Not quite what you
1195           expect, because you copy the area marked ??? onto GP. Now, ??? may have
1196           started out as zero once, but it's quite possible that it isn't. So now,
1197           rather than a nicely zeroed GP, you have it pointing somewhere random.
1198           Bugs ensue.
1199            
1200           (In fact, GP ends up pointing at a previous GP structure, because the
1201           principle cause of the padding in XPVMG getting garbage is a copy of
1202           sizeof(XPVMG) bytes from a XPVGV structure in sv_unglob. Right now
1203           this happens to be moot because XPVGV has been re-ordered, with GP
1204           no longer after STASH)
1205            
1206           So we are careful and work out the size of used parts of all the
1207           structures. */
1208            
1209 4243940283         switch (old_type) {
1210           case SVt_NULL:
1211           break;
1212           case SVt_IV:
1213 170477263 100       if (SvROK(sv)) {
1214 42598         referant = SvRV(sv);
1215           old_type_details = &fake_rv;
1216 42598 50       if (new_type == SVt_NV)
1217           new_type = SVt_PVNV;
1218           } else {
1219 170434665 100       if (new_type < SVt_PVIV) {
1220 2324856 100       new_type = (new_type == SVt_NV)
1221           ? SVt_PVNV : SVt_PVIV;
1222           }
1223           }
1224           break;
1225           case SVt_NV:
1226 2303163 100       if (new_type < SVt_PVNV) {
1227           new_type = SVt_PVNV;
1228           }
1229           break;
1230           case SVt_PV:
1231           assert(new_type > SVt_PV);
1232           assert(SVt_IV < SVt_PV);
1233           assert(SVt_NV < SVt_PV);
1234           break;
1235           case SVt_PVIV:
1236           break;
1237           case SVt_PVNV:
1238           break;
1239           case SVt_PVMG:
1240           /* Because the XPVMG of PL_mess_sv isn't allocated from the arena,
1241           there's no way that it can be safely upgraded, because perl.c
1242           expects to Safefree(SvANY(PL_mess_sv)) */
1243           assert(sv != PL_mess_sv);
1244           /* This flag bit is used to mean other things in other scalar types.
1245           Given that it only has meaning inside the pad, it shouldn't be set
1246           on anything that can get upgraded. */
1247           assert(!SvPAD_TYPED(sv));
1248           break;
1249           default:
1250 0 0       if (UNLIKELY(old_type_details->cant_upgrade))
1251 0         Perl_croak(aTHX_ "Can't upgrade %s (%" UVuf ") to %" UVuf,
1252           sv_reftype(sv, 0), (UV) old_type, (UV) new_type);
1253           }
1254            
1255 4243940283 50       if (UNLIKELY(old_type > new_type))
1256 0         Perl_croak(aTHX_ "sv_upgrade from type %d down to type %d",
1257           (int)old_type, (int)new_type);
1258            
1259 4243940283         new_type_details = bodies_by_type + new_type;
1260            
1261 4243940283         SvFLAGS(sv) &= ~SVTYPEMASK;
1262 4243940283         SvFLAGS(sv) |= new_type;
1263            
1264           /* This can't happen, as SVt_NULL is <= all values of new_type, so one of
1265           the return statements above will have triggered. */
1266           assert (new_type != SVt_NULL);
1267 4243940283         switch (new_type) {
1268           case SVt_IV:
1269           assert(old_type == SVt_NULL);
1270 1904381913         SvANY(sv) = (XPVIV*)((char*)&(sv->sv_u.svu_iv) - STRUCT_OFFSET(XPVIV, xiv_iv));
1271 1904381913         SvIV_set(sv, 0);
1272 1904381913         return;
1273           case SVt_NV:
1274           assert(old_type == SVt_NULL);
1275 8784664         SvANY(sv) = new_XNV();
1276 8784664         SvNV_set(sv, 0);
1277 8784664         return;
1278           case SVt_PVHV:
1279           case SVt_PVAV:
1280           assert(new_type_details->body_size);
1281            
1282           #ifndef PURIFY
1283           assert(new_type_details->arena);
1284           assert(new_type_details->arena_size);
1285           /* This points to the start of the allocated area. */
1286 301470357 100       new_body_inline(new_body, new_type);
1287 301470357         Zero(new_body, new_type_details->body_size, char);
1288 301470357         new_body = ((char *)new_body) - new_type_details->offset;
1289           #else
1290           /* We always allocated the full length item with PURIFY. To do this
1291           we fake things so that arena is false for all 16 types.. */
1292           new_body = new_NOARENAZ(new_type_details);
1293           #endif
1294 301470357         SvANY(sv) = new_body;
1295 301470357 100       if (new_type == SVt_PVAV) {
1296 185822768         AvMAX(sv) = -1;
1297 185822768         AvFILLp(sv) = -1;
1298 185822768         AvREAL_only(sv);
1299 185822768 50       if (old_type_details->body_size) {
1300 0         AvALLOC(sv) = 0;
1301           } else {
1302           /* It will have been zeroed when the new body was allocated.
1303           Lets not write to it, in case it confuses a write-back
1304           cache. */
1305           }
1306           } else {
1307           assert(!SvOK(sv));
1308 115647589 50       SvOK_off(sv);
1309           #ifndef NODEFAULT_SHAREKEYS
1310 115647589         HvSHAREKEYS_on(sv); /* key-sharing on by default */
1311           #endif
1312           /* start with PERL_HASH_DEFAULT_HvMAX+1 buckets: */
1313 115647589         HvMAX(sv) = PERL_HASH_DEFAULT_HvMAX;
1314           }
1315            
1316           /* SVt_NULL isn't the only thing upgraded to AV or HV.
1317           The target created by newSVrv also is, and it can have magic.
1318           However, it never has SvPVX set.
1319           */
1320           if (old_type == SVt_IV) {
1321           assert(!SvROK(sv));
1322           } else if (old_type >= SVt_PV) {
1323           assert(SvPVX_const(sv) == 0);
1324           }
1325            
1326 301470357 100       if (old_type >= SVt_PVMG) {
1327 1184416         SvMAGIC_set(sv, ((XPVMG*)old_body)->xmg_u.xmg_magic);
1328 1184416         SvSTASH_set(sv, ((XPVMG*)old_body)->xmg_stash);
1329           } else {
1330 300285941         sv->sv_u.svu_array = NULL; /* or svu_hash */
1331           }
1332           break;
1333            
1334           case SVt_PVIV:
1335           /* XXX Is this still needed? Was it ever needed? Surely as there is
1336           no route from NV to PVIV, NOK can never be true */
1337           assert(!SvNOKp(sv));
1338           assert(!SvNOK(sv));
1339           case SVt_PVIO:
1340           case SVt_PVFM:
1341           case SVt_PVGV:
1342           case SVt_PVCV:
1343           case SVt_PVLV:
1344           case SVt_INVLIST:
1345           case SVt_REGEXP:
1346           case SVt_PVMG:
1347           case SVt_PVNV:
1348           case SVt_PV:
1349            
1350           assert(new_type_details->body_size);
1351           /* We always allocated the full length item with PURIFY. To do this
1352           we fake things so that arena is false for all 16 types.. */
1353 2029303349 100       if(new_type_details->arena) {
1354           /* This points to the start of the allocated area. */
1355 2029302717 100       new_body_inline(new_body, new_type);
1356 2029302717         Zero(new_body, new_type_details->body_size, char);
1357 2029302717         new_body = ((char *)new_body) - new_type_details->offset;
1358           } else {
1359 632         new_body = new_NOARENAZ(new_type_details);
1360           }
1361 2029303349         SvANY(sv) = new_body;
1362            
1363 2029303349 100       if (old_type_details->copy) {
1364           /* There is now the potential for an upgrade from something without
1365           an offset (PVNV or PVMG) to something with one (PVCV, PVFM) */
1366 219765160         int offset = old_type_details->offset;
1367 219765160         int length = old_type_details->copy;
1368            
1369 219765160 50       if (new_type_details->offset > old_type_details->offset) {
1370 0         const int difference
1371 0         = new_type_details->offset - old_type_details->offset;
1372 0         offset += difference;
1373 0         length -= difference;
1374           }
1375           assert (length >= 0);
1376          
1377 219765160         Copy((char *)old_body + offset, (char *)new_body + offset, length,
1378           char);
1379           }
1380            
1381           #ifndef NV_ZERO_IS_ALLBITS_ZERO
1382           /* If NV 0.0 is stores as all bits 0 then Zero() already creates a
1383           * correct 0.0 for us. Otherwise, if the old body didn't have an
1384           * NV slot, but the new one does, then we need to initialise the
1385           * freshly created NV slot with whatever the correct bit pattern is
1386           * for 0.0 */
1387           if (old_type_details->zero_nv && !new_type_details->zero_nv
1388           && !isGV_with_GP(sv))
1389           SvNV_set(sv, 0);
1390           #endif
1391            
1392 2029303349 100       if (UNLIKELY(new_type == SVt_PVIO)) {
1393           IO * const io = MUTABLE_IO(sv);
1394 5097139         GV *iogv = gv_fetchpvs("IO::File::", GV_ADD, SVt_PVHV);
1395            
1396 5097139         SvOBJECT_on(io);
1397           /* Clear the stashcache because a new IO could overrule a package
1398           name */
1399           DEBUG_o(Perl_deb(aTHX_ "sv_upgrade clearing PL_stashcache\n"));
1400 5097139         hv_clear(PL_stashcache);
1401            
1402 10194278         SvSTASH_set(io, MUTABLE_HV(SvREFCNT_inc(GvHV(iogv))));
1403 5097139         IoPAGE_LEN(sv) = 60;
1404           }
1405 2029303349 100       if (UNLIKELY(new_type == SVt_REGEXP))
1406 24189608         sv->sv_u.svu_rx = (regexp *)new_body;
1407 2005113741 100       else if (old_type < SVt_PV) {
1408           /* referant will be NULL unless the old type was SVt_IV emulating
1409           SVt_RV */
1410 1958086435         sv->sv_u.svu_rv = referant;
1411           }
1412           break;
1413           default:
1414 0         Perl_croak(aTHX_ "panic: sv_upgrade to unknown type %lu",
1415           (unsigned long)new_type);
1416           }
1417            
1418 2330773706 100       if (old_type > SVt_IV) {
1419           #ifdef PURIFY
1420           safefree(old_body);
1421           #else
1422           /* Note that there is an assumption that all bodies of types that
1423           can be upgraded came from arenas. Only the more complex non-
1424           upgradable types are allowed to be directly malloc()ed. */
1425           assert(old_type_details->arena);
1426 2158102480         del_body((void*)((char*)old_body + old_type_details->offset),
1427           &PL_body_roots[old_type]);
1428           #endif
1429           }
1430           }
1431            
1432           /*
1433           =for apidoc sv_backoff
1434            
1435           Remove any string offset. You should normally use the C macro
1436           wrapper instead.
1437            
1438           =cut
1439           */
1440            
1441           int
1442 77326         Perl_sv_backoff(pTHX_ SV *const sv)
1443           {
1444           STRLEN delta;
1445 77326         const char * const s = SvPVX_const(sv);
1446            
1447           PERL_ARGS_ASSERT_SV_BACKOFF;
1448           PERL_UNUSED_CONTEXT;
1449            
1450           assert(SvOOK(sv));
1451           assert(SvTYPE(sv) != SVt_PVHV);
1452           assert(SvTYPE(sv) != SVt_PVAV);
1453            
1454 77326 50       SvOOK_offset(sv, delta);
    100        
1455          
1456 77326         SvLEN_set(sv, SvLEN(sv) + delta);
1457 77326         SvPV_set(sv, SvPVX(sv) - delta);
1458 77326         Move(s, SvPVX(sv), SvCUR(sv)+1, char);
1459 77326         SvFLAGS(sv) &= ~SVf_OOK;
1460 77326         return 0;
1461           }
1462            
1463           /*
1464           =for apidoc sv_grow
1465            
1466           Expands the character buffer in the SV. If necessary, uses C and
1467           upgrades the SV to C. Returns a pointer to the character buffer.
1468           Use the C wrapper instead.
1469            
1470           =cut
1471           */
1472            
1473           static void S_sv_uncow(pTHX_ SV * const sv, const U32 flags);
1474            
1475           char *
1476 1046735576         Perl_sv_grow(pTHX_ SV *const sv, STRLEN newlen)
1477           {
1478           char *s;
1479            
1480           PERL_ARGS_ASSERT_SV_GROW;
1481            
1482           #ifdef HAS_64K_LIMIT
1483           if (newlen >= 0x10000) {
1484           PerlIO_printf(Perl_debug_log,
1485           "Allocation too large: %"UVxf"\n", (UV)newlen);
1486           my_exit(1);
1487           }
1488           #endif /* HAS_64K_LIMIT */
1489 1046735576 50       if (SvROK(sv))
1490 0         sv_unref(sv);
1491 1046735576 100       if (SvTYPE(sv) < SVt_PV) {
1492 24346         sv_upgrade(sv, SVt_PV);
1493 24346         s = SvPVX_mutable(sv);
1494           }
1495 1046711230 100       else if (SvOOK(sv)) { /* pv is offset? */
1496 14562         sv_backoff(sv);
1497 14562         s = SvPVX_mutable(sv);
1498 14562 100       if (newlen > SvLEN(sv))
1499 6344         newlen += 10 * (newlen - SvCUR(sv)); /* avoid copy each time */
1500           #ifdef HAS_64K_LIMIT
1501           if (newlen >= 0x10000)
1502           newlen = 0xFFFF;
1503           #endif
1504           }
1505           else
1506           {
1507 1046696668 100       if (SvIsCOW(sv)) S_sv_uncow(aTHX_ sv, 0);
1508 1046696668         s = SvPVX_mutable(sv);
1509           }
1510            
1511           #ifdef PERL_NEW_COPY_ON_WRITE
1512           /* the new COW scheme uses SvPVX(sv)[SvLEN(sv)-1] (if spare)
1513           * to store the COW count. So in general, allocate one more byte than
1514           * asked for, to make it likely this byte is always spare: and thus
1515           * make more strings COW-able.
1516           * If the new size is a big power of two, don't bother: we assume the
1517           * caller wanted a nice 2^N sized block and will be annoyed at getting
1518           * 2^N+1 */
1519 1046735576 100       if (newlen & 0xff)
1520 1046623352         newlen++;
1521           #endif
1522            
1523 1046735576 100       if (newlen > SvLEN(sv)) { /* need more room? */
1524 1037760184         STRLEN minlen = SvCUR(sv);
1525 1037760184         minlen += (minlen >> PERL_STRLEN_EXPAND_SHIFT) + 10;
1526 1037760184 100       if (newlen < minlen)
1527           newlen = minlen;
1528           #ifndef Perl_safesysmalloc_size
1529 1037760184 50       newlen = PERL_STRLEN_ROUNDUP(newlen);
1530           #endif
1531 1037760184 100       if (SvLEN(sv) && s) {
1532 37573464         s = (char*)saferealloc(s, newlen);
1533           }
1534           else {
1535 1000186720         s = (char*)safemalloc(newlen);
1536 1000186720 100       if (SvPVX_const(sv) && SvCUR(sv)) {
    50        
1537 72         Move(SvPVX_const(sv), s, (newlen < SvCUR(sv)) ? newlen : SvCUR(sv), char);
1538           }
1539           }
1540 1037760184         SvPV_set(sv, s);
1541           #ifdef Perl_safesysmalloc_size
1542           /* Do this here, do it once, do it right, and then we will never get
1543           called back into sv_grow() unless there really is some growing
1544           needed. */
1545           SvLEN_set(sv, Perl_safesysmalloc_size(s));
1546           #else
1547 1037760184         SvLEN_set(sv, newlen);
1548           #endif
1549           }
1550 1046735576         return s;
1551           }
1552            
1553           /*
1554           =for apidoc sv_setiv
1555            
1556           Copies an integer into the given SV, upgrading first if necessary.
1557           Does not handle 'set' magic. See also C.
1558            
1559           =cut
1560           */
1561            
1562           void
1563 1792820924         Perl_sv_setiv(pTHX_ SV *const sv, const IV i)
1564           {
1565           dVAR;
1566            
1567           PERL_ARGS_ASSERT_SV_SETIV;
1568            
1569 1792820924 100       SV_CHECK_THINKFIRST_COW_DROP(sv);
1570 1792820922         switch (SvTYPE(sv)) {
1571           case SVt_NULL:
1572           case SVt_NV:
1573 770672422         sv_upgrade(sv, SVt_IV);
1574 770672422         break;
1575           case SVt_PV:
1576 8926         sv_upgrade(sv, SVt_PVIV);
1577 8926         break;
1578            
1579           case SVt_PVGV:
1580 2 50       if (!isGV_with_GP(sv))
    50        
1581           break;
1582           case SVt_PVAV:
1583           case SVt_PVHV:
1584           case SVt_PVCV:
1585           case SVt_PVFM:
1586           case SVt_PVIO:
1587           /* diag_listed_as: Can't coerce %s to %s in %s */
1588 3 50       Perl_croak(aTHX_ "Can't coerce %s to integer in %s", sv_reftype(sv,0),
1589 1 0       OP_DESC(PL_op));
1590           default: NOOP;
1591           }
1592 1792820920 50       (void)SvIOK_only(sv); /* validate number */
1593 1792820920         SvIV_set(sv, i);
1594 1792820920 100       SvTAINT(sv);
    100        
    50        
1595 1792820920         }
1596            
1597           /*
1598           =for apidoc sv_setiv_mg
1599            
1600           Like C, but also handles 'set' magic.
1601            
1602           =cut
1603           */
1604            
1605           void
1606 108         Perl_sv_setiv_mg(pTHX_ SV *const sv, const IV i)
1607           {
1608           PERL_ARGS_ASSERT_SV_SETIV_MG;
1609            
1610 108         sv_setiv(sv,i);
1611 108 100       SvSETMAGIC(sv);
1612 108         }
1613            
1614           /*
1615           =for apidoc sv_setuv
1616            
1617           Copies an unsigned integer into the given SV, upgrading first if necessary.
1618           Does not handle 'set' magic. See also C.
1619            
1620           =cut
1621           */
1622            
1623           void
1624 250556095         Perl_sv_setuv(pTHX_ SV *const sv, const UV u)
1625           {
1626           PERL_ARGS_ASSERT_SV_SETUV;
1627            
1628           /* With the if statement to ensure that integers are stored as IVs whenever
1629           possible:
1630           u=1.49 s=0.52 cu=72.49 cs=10.64 scripts=270 tests=20865
1631            
1632           without
1633           u=1.35 s=0.47 cu=73.45 cs=11.43 scripts=270 tests=20865
1634            
1635           If you wish to remove the following if statement, so that this routine
1636           (and its callers) always return UVs, please benchmark to see what the
1637           effect is. Modern CPUs may be different. Or may not :-)
1638           */
1639 250556095 100       if (u <= (UV)IV_MAX) {
1640 249527411         sv_setiv(sv, (IV)u);
1641 374971525         return;
1642           }
1643 1028684         sv_setiv(sv, 0);
1644 1028684         SvIsUV_on(sv);
1645 1028684         SvUV_set(sv, u);
1646           }
1647            
1648           /*
1649           =for apidoc sv_setuv_mg
1650            
1651           Like C, but also handles 'set' magic.
1652            
1653           =cut
1654           */
1655            
1656           void
1657 12         Perl_sv_setuv_mg(pTHX_ SV *const sv, const UV u)
1658           {
1659           PERL_ARGS_ASSERT_SV_SETUV_MG;
1660            
1661 12         sv_setuv(sv,u);
1662 10 100       SvSETMAGIC(sv);
1663 10         }
1664            
1665           /*
1666           =for apidoc sv_setnv
1667            
1668           Copies a double into the given SV, upgrading first if necessary.
1669           Does not handle 'set' magic. See also C.
1670            
1671           =cut
1672           */
1673            
1674           void
1675 106016696         Perl_sv_setnv(pTHX_ SV *const sv, const NV num)
1676           {
1677           dVAR;
1678            
1679           PERL_ARGS_ASSERT_SV_SETNV;
1680            
1681 106016696 100       SV_CHECK_THINKFIRST_COW_DROP(sv);
1682 106016696         switch (SvTYPE(sv)) {
1683           case SVt_NULL:
1684           case SVt_IV:
1685 4942782         sv_upgrade(sv, SVt_NV);
1686 4942782         break;
1687           case SVt_PV:
1688           case SVt_PVIV:
1689 36         sv_upgrade(sv, SVt_PVNV);
1690 36         break;
1691            
1692           case SVt_PVGV:
1693 2 50       if (!isGV_with_GP(sv))
    50        
1694           break;
1695           case SVt_PVAV:
1696           case SVt_PVHV:
1697           case SVt_PVCV:
1698           case SVt_PVFM:
1699           case SVt_PVIO:
1700           /* diag_listed_as: Can't coerce %s to %s in %s */
1701 3 50       Perl_croak(aTHX_ "Can't coerce %s to number in %s", sv_reftype(sv,0),
1702 1 0       OP_DESC(PL_op));
1703           default: NOOP;
1704           }
1705 106016694         SvNV_set(sv, num);
1706 106016694 50       (void)SvNOK_only(sv); /* validate number */
1707 106016694 100       SvTAINT(sv);
    50        
    0        
1708 106016694         }
1709            
1710           /*
1711           =for apidoc sv_setnv_mg
1712            
1713           Like C, but also handles 'set' magic.
1714            
1715           =cut
1716           */
1717            
1718           void
1719 1536621         Perl_sv_setnv_mg(pTHX_ SV *const sv, const NV num)
1720           {
1721           PERL_ARGS_ASSERT_SV_SETNV_MG;
1722            
1723 1536621         sv_setnv(sv,num);
1724 1536621 100       SvSETMAGIC(sv);
1725 1536621         }
1726            
1727           /* Return a cleaned-up, printable version of sv, for non-numeric, or
1728           * not incrementable warning display.
1729           * Originally part of S_not_a_number().
1730           * The return value may be != tmpbuf.
1731           */
1732            
1733           STATIC const char *
1734 92         S_sv_display(pTHX_ SV *const sv, char *tmpbuf, STRLEN tmpbuf_size) {
1735           const char *pv;
1736            
1737           PERL_ARGS_ASSERT_SV_DISPLAY;
1738            
1739 110 100       if (DO_UTF8(sv)) {
    50        
1740 18         SV *dsv = newSVpvs_flags("", SVs_TEMP);
1741 18         pv = sv_uni_display(dsv, sv, 10, UNI_DISPLAY_ISPRINT);
1742           } else {
1743           char *d = tmpbuf;
1744 74         const char * const limit = tmpbuf + tmpbuf_size - 8;
1745           /* each *s can expand to 4 chars + "...\0",
1746           i.e. need room for 8 chars */
1747          
1748 74         const char *s = SvPVX_const(sv);
1749 74         const char * const end = s + SvCUR(sv);
1750 522 100       for ( ; s < end && d < limit; s++ ) {
1751 448         int ch = *s & 0xFF;
1752 448 50       if (! isASCII(ch) && !isPRINT_LC(ch)) {
    0        
1753 0         *d++ = 'M';
1754 0         *d++ = '-';
1755            
1756           /* Map to ASCII "equivalent" of Latin1 */
1757 0         ch = LATIN1_TO_NATIVE(NATIVE_TO_LATIN1(ch) & 127);
1758           }
1759 448 50       if (ch == '\n') {
1760 0         *d++ = '\\';
1761 0         *d++ = 'n';
1762           }
1763 448 50       else if (ch == '\r') {
1764 0         *d++ = '\\';
1765 0         *d++ = 'r';
1766           }
1767 448 50       else if (ch == '\f') {
1768 0         *d++ = '\\';
1769 0         *d++ = 'f';
1770           }
1771 448 50       else if (ch == '\\') {
1772 0         *d++ = '\\';
1773 0         *d++ = '\\';
1774           }
1775 448 100       else if (ch == '\0') {
1776 22         *d++ = '\\';
1777 22         *d++ = '0';
1778           }
1779 426 50       else if (isPRINT_LC(ch))
1780 426         *d++ = ch;
1781           else {
1782 0         *d++ = '^';
1783 0 0       *d++ = toCTRL(ch);
1784           }
1785           }
1786 74 50       if (s < end) {
1787 0         *d++ = '.';
1788 0         *d++ = '.';
1789 0         *d++ = '.';
1790           }
1791 74         *d = '\0';
1792           pv = tmpbuf;
1793           }
1794            
1795 92         return pv;
1796           }
1797            
1798           /* Print an "isn't numeric" warning, using a cleaned-up,
1799           * printable version of the offending string
1800           */
1801            
1802           STATIC void
1803 88         S_not_a_number(pTHX_ SV *const sv)
1804           {
1805           dVAR;
1806           char tmpbuf[64];
1807           const char *pv;
1808            
1809           PERL_ARGS_ASSERT_NOT_A_NUMBER;
1810            
1811 88         pv = sv_display(sv, tmpbuf, sizeof(tmpbuf));
1812            
1813 88 50       if (PL_op)
1814 132 50       Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1815           /* diag_listed_as: Argument "%s" isn't numeric%s */
1816           "Argument \"%s\" isn't numeric in %s", pv,
1817 44 0       OP_DESC(PL_op));
1818           else
1819 0         Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1820           /* diag_listed_as: Argument "%s" isn't numeric%s */
1821           "Argument \"%s\" isn't numeric", pv);
1822 74         }
1823            
1824           STATIC void
1825           S_not_incrementable(pTHX_ SV *const sv) {
1826           dVAR;
1827           char tmpbuf[64];
1828           const char *pv;
1829            
1830           PERL_ARGS_ASSERT_NOT_INCREMENTABLE;
1831            
1832 4         pv = sv_display(sv, tmpbuf, sizeof(tmpbuf));
1833            
1834 4         Perl_warner(aTHX_ packWARN(WARN_NUMERIC),
1835           "Argument \"%s\" treated as 0 in increment (++)", pv);
1836           }
1837            
1838           /*
1839           =for apidoc looks_like_number
1840            
1841           Test if the content of an SV looks like a number (or is a number).
1842           C and C are treated as numbers (so will not issue a
1843           non-numeric warning), even if your atof() doesn't grok them. Get-magic is
1844           ignored.
1845            
1846           =cut
1847           */
1848            
1849           I32
1850 3586         Perl_looks_like_number(pTHX_ SV *const sv)
1851           {
1852           const char *sbegin;
1853           STRLEN len;
1854            
1855           PERL_ARGS_ASSERT_LOOKS_LIKE_NUMBER;
1856            
1857 3586 100       if (SvPOK(sv) || SvPOKp(sv)) {
1858 3518 100       sbegin = SvPV_nomg_const(sv, len);
1859           }
1860           else
1861 1827         return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
1862 3518         return grok_number(sbegin, len, NULL);
1863           }
1864            
1865           STATIC bool
1866 50         S_glob_2number(pTHX_ GV * const gv)
1867           {
1868           PERL_ARGS_ASSERT_GLOB_2NUMBER;
1869            
1870           /* We know that all GVs stringify to something that is not-a-number,
1871           so no need to test that. */
1872 50 100       if (ckWARN(WARN_NUMERIC))
1873           {
1874 28         SV *const buffer = sv_newmortal();
1875 28         gv_efullname3(buffer, gv, "*");
1876 28         not_a_number(buffer);
1877           }
1878           /* We just want something true to return, so that S_sv_2iuv_common
1879           can tail call us and return true. */
1880 50         return TRUE;
1881           }
1882            
1883           /* Actually, ISO C leaves conversion of UV to IV undefined, but
1884           until proven guilty, assume that things are not that bad... */
1885            
1886           /*
1887           NV_PRESERVES_UV:
1888            
1889           As 64 bit platforms often have an NV that doesn't preserve all bits of
1890           an IV (an assumption perl has been based on to date) it becomes necessary
1891           to remove the assumption that the NV always carries enough precision to
1892           recreate the IV whenever needed, and that the NV is the canonical form.
1893           Instead, IV/UV and NV need to be given equal rights. So as to not lose
1894           precision as a side effect of conversion (which would lead to insanity
1895           and the dragon(s) in t/op/numconvert.t getting very angry) the intent is
1896           1) to distinguish between IV/UV/NV slots that have cached a valid
1897           conversion where precision was lost and IV/UV/NV slots that have a
1898           valid conversion which has lost no precision
1899           2) to ensure that if a numeric conversion to one form is requested that
1900           would lose precision, the precise conversion (or differently
1901           imprecise conversion) is also performed and cached, to prevent
1902           requests for different numeric formats on the same SV causing
1903           lossy conversion chains. (lossless conversion chains are perfectly
1904           acceptable (still))
1905            
1906            
1907           flags are used:
1908           SvIOKp is true if the IV slot contains a valid value
1909           SvIOK is true only if the IV value is accurate (UV if SvIOK_UV true)
1910           SvNOKp is true if the NV slot contains a valid value
1911           SvNOK is true only if the NV value is accurate
1912            
1913           so
1914           while converting from PV to NV, check to see if converting that NV to an
1915           IV(or UV) would lose accuracy over a direct conversion from PV to
1916           IV(or UV). If it would, cache both conversions, return NV, but mark
1917           SV as IOK NOKp (ie not NOK).
1918            
1919           While converting from PV to IV, check to see if converting that IV to an
1920           NV would lose accuracy over a direct conversion from PV to NV. If it
1921           would, cache both conversions, flag similarly.
1922            
1923           Before, the SV value "3.2" could become NV=3.2 IV=3 NOK, IOK quite
1924           correctly because if IV & NV were set NV *always* overruled.
1925           Now, "3.2" will become NV=3.2 IV=3 NOK, IOKp, because the flag's meaning
1926           changes - now IV and NV together means that the two are interchangeable:
1927           SvIVX == (IV) SvNVX && SvNVX == (NV) SvIVX;
1928            
1929           The benefit of this is that operations such as pp_add know that if
1930           SvIOK is true for both left and right operands, then integer addition
1931           can be used instead of floating point (for cases where the result won't
1932           overflow). Before, floating point was always used, which could lead to
1933           loss of precision compared with integer addition.
1934            
1935           * making IV and NV equal status should make maths accurate on 64 bit
1936           platforms
1937           * may speed up maths somewhat if pp_add and friends start to use
1938           integers when possible instead of fp. (Hopefully the overhead in
1939           looking for SvIOK and checking for overflow will not outweigh the
1940           fp to integer speedup)
1941           * will slow down integer operations (callers of SvIV) on "inaccurate"
1942           values, as the change from SvIOK to SvIOKp will cause a call into
1943           sv_2iv each time rather than a macro access direct to the IV slot
1944           * should speed up number->string conversion on integers as IV is
1945           favoured when IV and NV are equally accurate
1946            
1947           ####################################################################
1948           You had better be using SvIOK_notUV if you want an IV for arithmetic:
1949           SvIOK is true if (IV or UV), so you might be getting (IV)SvUV.
1950           On the other hand, SvUOK is true iff UV.
1951           ####################################################################
1952            
1953           Your mileage will vary depending your CPU's relative fp to integer
1954           performance ratio.
1955           */
1956            
1957           #ifndef NV_PRESERVES_UV
1958           # define IS_NUMBER_UNDERFLOW_IV 1
1959           # define IS_NUMBER_UNDERFLOW_UV 2
1960           # define IS_NUMBER_IV_AND_UV 2
1961           # define IS_NUMBER_OVERFLOW_IV 4
1962           # define IS_NUMBER_OVERFLOW_UV 5
1963            
1964           /* sv_2iuv_non_preserve(): private routine for use by sv_2iv() and sv_2uv() */
1965            
1966           /* For sv_2nv these three cases are "SvNOK and don't bother casting" */
1967           STATIC int
1968 22976         S_sv_2iuv_non_preserve(pTHX_ SV *const sv
1969           # ifdef DEBUGGING
1970           , I32 numtype
1971           # endif
1972           )
1973           {
1974           dVAR;
1975            
1976           PERL_ARGS_ASSERT_SV_2IUV_NON_PRESERVE;
1977            
1978           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));
1979 22976 100       if (SvNVX(sv) < (NV)IV_MIN) {
1980 17118         (void)SvIOKp_on(sv);
1981 17118         (void)SvNOK_on(sv);
1982 17118         SvIV_set(sv, IV_MIN);
1983 17118         return IS_NUMBER_UNDERFLOW_IV;
1984           }
1985 5858 100       if (SvNVX(sv) > (NV)UV_MAX) {
1986 5744         (void)SvIOKp_on(sv);
1987 5744         (void)SvNOK_on(sv);
1988 5744         SvIsUV_on(sv);
1989 5744         SvUV_set(sv, UV_MAX);
1990 5744         return IS_NUMBER_OVERFLOW_UV;
1991           }
1992 114         (void)SvIOKp_on(sv);
1993 114         (void)SvNOK_on(sv);
1994           /* Can't use strtol etc to convert this string. (See truth table in
1995           sv_2iv */
1996 114 50       if (SvNVX(sv) <= (UV)IV_MAX) {
1997 0         SvIV_set(sv, I_V(SvNVX(sv)));
1998 0 0       if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
1999 0         SvIOK_on(sv); /* Integer is precise. NOK, IOK */
2000           } else {
2001           /* Integer is imprecise. NOK, IOKp */
2002           }
2003           return SvNVX(sv) < 0 ? IS_NUMBER_UNDERFLOW_UV : IS_NUMBER_IV_AND_UV;
2004           }
2005 114         SvIsUV_on(sv);
2006 114         SvUV_set(sv, U_V(SvNVX(sv)));
2007 114 50       if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2008 114 50       if (SvUVX(sv) == UV_MAX) {
2009           /* As we know that NVs don't preserve UVs, UV_MAX cannot
2010           possibly be preserved by NV. Hence, it must be overflow.
2011           NOK, IOKp */
2012           return IS_NUMBER_OVERFLOW_UV;
2013           }
2014 11488         SvIOK_on(sv); /* Integer is precise. NOK, UOK */
2015           } else {
2016           /* Integer is imprecise. NOK, IOKp */
2017           }
2018           return IS_NUMBER_OVERFLOW_IV;
2019           }
2020           #endif /* !NV_PRESERVES_UV*/
2021            
2022           STATIC bool
2023 28046981         S_sv_2iuv_common(pTHX_ SV *const sv)
2024           {
2025           dVAR;
2026            
2027           PERL_ARGS_ASSERT_SV_2IUV_COMMON;
2028            
2029 28046981 100       if (SvNOKp(sv)) {
2030           /* erm. not sure. *should* never get NOKp (without NOK) from sv_2nv
2031           * without also getting a cached IV/UV from it at the same time
2032           * (ie PV->NV conversion should detect loss of accuracy and cache
2033           * IV or UV at same time to avoid this. */
2034           /* IV-over-UV optimisation - choose to cache IV if possible */
2035            
2036 16304457 100       if (SvTYPE(sv) == SVt_NV)
2037 714435         sv_upgrade(sv, SVt_PVNV);
2038            
2039 16304457         (void)SvIOKp_on(sv); /* Must do this first, to clear any SvOOK */
2040           /* < not <= as for NV doesn't preserve UV, ((NV)IV_MAX+1) will almost
2041           certainly cast into the IV range at IV_MAX, whereas the correct
2042           answer is the UV IV_MAX +1. Hence < ensures that dodgy boundary
2043           cases go to UV */
2044           #if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
2045           if (Perl_isnan(SvNVX(sv))) {
2046           SvUV_set(sv, 0);
2047           SvIsUV_on(sv);
2048           return FALSE;
2049           }
2050           #endif
2051 16304457 100       if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2052 16289819         SvIV_set(sv, I_V(SvNVX(sv)));
2053 16289819 100       if (SvNVX(sv) == (NV) SvIVX(sv)
2054           #ifndef NV_PRESERVES_UV
2055 6386230 100       && (((UV)1 << NV_PRESERVES_UV_BITS) >
2056 6386230         (UV)(SvIVX(sv) > 0 ? SvIVX(sv) : -SvIVX(sv)))
2057           /* Don't flag it as "accurately an integer" if the number
2058           came from a (by definition imprecise) NV operation, and
2059           we're outside the range of NV integer precision */
2060           #endif
2061           ) {
2062 6384030 100       if (SvNOK(sv))
2063 6384022         SvIOK_on(sv); /* Can this go wrong with rounding? NWC */
2064           else {
2065           /* scalar has trailing garbage, eg "42a" */
2066           }
2067           DEBUG_c(PerlIO_printf(Perl_debug_log,
2068           "0x%"UVxf" iv(%"NVgf" => %"IVdf") (precise)\n",
2069           PTR2UV(sv),
2070           SvNVX(sv),
2071           SvIVX(sv)));
2072            
2073           } else {
2074           /* IV not precise. No need to convert from PV, as NV
2075           conversion would already have cached IV if it detected
2076           that PV->IV would be better than PV->NV->IV
2077           flags already correct - don't set public IOK. */
2078           DEBUG_c(PerlIO_printf(Perl_debug_log,
2079           "0x%"UVxf" iv(%"NVgf" => %"IVdf") (imprecise)\n",
2080           PTR2UV(sv),
2081           SvNVX(sv),
2082           SvIVX(sv)));
2083           }
2084           /* Can the above go wrong if SvIVX == IV_MIN and SvNVX < IV_MIN,
2085           but the cast (NV)IV_MIN rounds to a the value less (more
2086           negative) than IV_MIN which happens to be equal to SvNVX ??
2087           Analogous to 0xFFFFFFFFFFFFFFFF rounding up to NV (2**64) and
2088           NV rounding back to 0xFFFFFFFFFFFFFFFF, so UVX == UV(NVX) and
2089           (NV)UVX == NVX are both true, but the values differ. :-(
2090           Hopefully for 2s complement IV_MIN is something like
2091           0x8000000000000000 which will be exact. NWC */
2092           }
2093           else {
2094 14638         SvUV_set(sv, U_V(SvNVX(sv)));
2095 14638 100       if (
2096 14638         (SvNVX(sv) == (NV) SvUVX(sv))
2097           #ifndef NV_PRESERVES_UV
2098           /* Make sure it's not 0xFFFFFFFFFFFFFFFF */
2099           /*&& (SvUVX(sv) != UV_MAX) irrelevant with code below */
2100 1160 50       && (((UV)1 << NV_PRESERVES_UV_BITS) > SvUVX(sv))
2101           /* Don't flag it as "accurately an integer" if the number
2102           came from a (by definition imprecise) NV operation, and
2103           we're outside the range of NV integer precision */
2104           #endif
2105 0 0       && SvNOK(sv)
2106           )
2107 0         SvIOK_on(sv);
2108 14638         SvIsUV_on(sv);
2109           DEBUG_c(PerlIO_printf(Perl_debug_log,
2110           "0x%"UVxf" 2iv(%"UVuf" => %"IVdf") (as unsigned)\n",
2111           PTR2UV(sv),
2112           SvUVX(sv),
2113           SvUVX(sv)));
2114           }
2115           }
2116 11742524 100       else if (SvPOKp(sv)) {
2117           UV value;
2118 3423444         const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2119           /* We want to avoid a possible problem when we cache an IV/ a UV which
2120           may be later translated to an NV, and the resulting NV is not
2121           the same as the direct translation of the initial string
2122           (eg 123.456 can shortcut to the IV 123 with atol(), but we must
2123           be careful to ensure that the value with the .456 is around if the
2124           NV value is requested in the future).
2125          
2126           This means that if we cache such an IV/a UV, we need to cache the
2127           NV as well. Moreover, we trade speed for space, and do not
2128           cache the NV if we are sure it's not needed.
2129           */
2130            
2131           /* SVt_PVNV is one higher than SVt_PVIV, hence this order */
2132 3423444 100       if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2133           == IS_NUMBER_IN_UV) {
2134           /* It's definitely an integer, only upgrade to PVIV */
2135 3348820 100       if (SvTYPE(sv) < SVt_PVIV)
2136 812452         sv_upgrade(sv, SVt_PVIV);
2137 3348820         (void)SvIOK_on(sv);
2138 74624 100       } else if (SvTYPE(sv) < SVt_PVNV)
2139 39240         sv_upgrade(sv, SVt_PVNV);
2140            
2141           /* If NVs preserve UVs then we only use the UV value if we know that
2142           we aren't going to call atof() below. If NVs don't preserve UVs
2143           then the value returned may have more precision than atof() will
2144           return, even though value isn't perfectly accurate. */
2145 3423444 100       if ((numtype & (IS_NUMBER_IN_UV
2146           #ifdef NV_PRESERVES_UV
2147           | IS_NUMBER_NOT_INT
2148           #endif
2149 3423444         )) == IS_NUMBER_IN_UV) {
2150           /* This won't turn off the public IOK flag if it was set above */
2151 3391576         (void)SvIOKp_on(sv);
2152            
2153 3391576 100       if (!(numtype & IS_NUMBER_NEG)) {
2154           /* positive */;
2155 3361862 100       if (value <= (UV)IV_MAX) {
2156 3343770         SvIV_set(sv, (IV)value);
2157           } else {
2158           /* it didn't overflow, and it was positive. */
2159 18092         SvUV_set(sv, value);
2160 18092         SvIsUV_on(sv);
2161           }
2162           } else {
2163           /* 2s complement assumption */
2164 29714 50       if (value <= (UV)IV_MIN) {
2165 29714         SvIV_set(sv, -(IV)value);
2166           } else {
2167           /* Too negative for an IV. This is a double upgrade, but
2168           I'm assuming it will be rare. */
2169 0 0       if (SvTYPE(sv) < SVt_PVNV)
2170 0         sv_upgrade(sv, SVt_PVNV);
2171 0         SvNOK_on(sv);
2172 0         SvIOK_off(sv);
2173 0         SvIOKp_on(sv);
2174 0         SvNV_set(sv, -(NV)value);
2175 0         SvIV_set(sv, IV_MIN);
2176           }
2177           }
2178           }
2179           /* For !NV_PRESERVES_UV and IS_NUMBER_IN_UV and IS_NUMBER_NOT_INT we
2180           will be in the previous block to set the IV slot, and the next
2181           block to set the NV slot. So no else here. */
2182          
2183 3423444 100       if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2184           != IS_NUMBER_IN_UV) {
2185           /* It wasn't an (integer that doesn't overflow the UV). */
2186 74624         SvNV_set(sv, Atof(SvPVX_const(sv)));
2187            
2188 74624 100       if (! numtype && ckWARN(WARN_NUMERIC))
    100        
2189 54         not_a_number(sv);
2190            
2191           #if defined(USE_LONG_DOUBLE)
2192           DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%" PERL_PRIgldbl ")\n",
2193           PTR2UV(sv), SvNVX(sv)));
2194           #else
2195           DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"NVgf")\n",
2196           PTR2UV(sv), SvNVX(sv)));
2197           #endif
2198            
2199           #ifdef NV_PRESERVES_UV
2200           (void)SvIOKp_on(sv);
2201           (void)SvNOK_on(sv);
2202           if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2203           SvIV_set(sv, I_V(SvNVX(sv)));
2204           if ((NV)(SvIVX(sv)) == SvNVX(sv)) {
2205           SvIOK_on(sv);
2206           } else {
2207           NOOP; /* Integer is imprecise. NOK, IOKp */
2208           }
2209           /* UV will not work better than IV */
2210           } else {
2211           if (SvNVX(sv) > (NV)UV_MAX) {
2212           SvIsUV_on(sv);
2213           /* Integer is inaccurate. NOK, IOKp, is UV */
2214           SvUV_set(sv, UV_MAX);
2215           } else {
2216           SvUV_set(sv, U_V(SvNVX(sv)));
2217           /* 0xFFFFFFFFFFFFFFFF not an issue in here, NVs
2218           NV preservse UV so can do correct comparison. */
2219           if ((NV)(SvUVX(sv)) == SvNVX(sv)) {
2220           SvIOK_on(sv);
2221           } else {
2222           NOOP; /* Integer is imprecise. NOK, IOKp, is UV */
2223           }
2224           }
2225           SvIsUV_on(sv);
2226           }
2227           #else /* NV_PRESERVES_UV */
2228 74612 100       if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2229           == (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT)) {
2230           /* The IV/UV slot will have been set from value returned by
2231           grok_number above. The NV slot has just been set using
2232           Atof. */
2233 42756         SvNOK_on(sv);
2234           assert (SvIOKp(sv));
2235           } else {
2236 31856 100       if (((UV)1 << NV_PRESERVES_UV_BITS) >
2237 31856 100       U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2238           /* Small enough to preserve all bits. */
2239 8880         (void)SvIOKp_on(sv);
2240 8880         SvNOK_on(sv);
2241 8880         SvIV_set(sv, I_V(SvNVX(sv)));
2242 8880 100       if ((NV)(SvIVX(sv)) == SvNVX(sv))
2243 8374         SvIOK_on(sv);
2244           /* Assumption: first non-preserved integer is < IV_MAX,
2245           this NV is in the preserved range, therefore: */
2246 8880 100       if (!(U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))
    50        
2247           < (UV)IV_MAX)) {
2248 0         Perl_croak(aTHX_ "sv_2iv assumed (U_V(fabs((double)SvNVX(sv))) < (UV)IV_MAX) but SvNVX(sv)=%"NVgf" U_V is 0x%"UVxf", IV_MAX is 0x%"UVxf"\n", SvNVX(sv), U_V(SvNVX(sv)), (UV)IV_MAX);
2249           }
2250           } else {
2251           /* IN_UV NOT_INT
2252           0 0 already failed to read UV.
2253           0 1 already failed to read UV.
2254           1 0 you won't get here in this case. IV/UV
2255           slot set, public IOK, Atof() unneeded.
2256           1 1 already read UV.
2257           so there's no point in sv_2iuv_non_preserve() attempting
2258           to use atol, strtol, strtoul etc. */
2259           # ifdef DEBUGGING
2260           sv_2iuv_non_preserve (sv, numtype);
2261           # else
2262 22976         sv_2iuv_non_preserve (sv);
2263           # endif
2264           }
2265           }
2266           #endif /* NV_PRESERVES_UV */
2267           /* It might be more code efficient to go through the entire logic above
2268           and conditionally set with SvIOKp_on() rather than SvIOK(), but it
2269           gets complex and potentially buggy, so more programmer efficient
2270           to do it this way, by turning off the public flags: */
2271 74612 100       if (!numtype)
2272 6720         SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK);
2273           }
2274           }
2275           else {
2276 8319080 100       if (isGV_with_GP(sv))
    50        
2277 18         return glob_2number(MUTABLE_GV(sv));
2278            
2279 8319062 100       if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
    100        
2280 274         report_uninit(sv);
2281 8319062 100       if (SvTYPE(sv) < SVt_IV)
2282           /* Typically the caller expects that sv_any is not NULL now. */
2283 14047762         sv_upgrade(sv, SVt_IV);
2284           /* Return 0 from the caller. */
2285           return TRUE;
2286           }
2287           return FALSE;
2288           }
2289            
2290           /*
2291           =for apidoc sv_2iv_flags
2292            
2293           Return the integer value of an SV, doing any necessary string
2294           conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2295           Normally used via the C and C macros.
2296            
2297           =cut
2298           */
2299            
2300           IV
2301 27762857         Perl_sv_2iv_flags(pTHX_ SV *const sv, const I32 flags)
2302           {
2303           dVAR;
2304            
2305 27762857 50       if (!sv)
2306           return 0;
2307            
2308           assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
2309           && SvTYPE(sv) != SVt_PVFM);
2310            
2311 27762857 100       if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
    100        
2312 23688         mg_get(sv);
2313            
2314 27762857 100       if (SvROK(sv)) {
2315 70282 50       if (SvAMAGIC(sv)) {
    100        
    50        
2316           SV * tmpstr;
2317 498 50       if (flags & SV_SKIP_OVERLOAD)
2318           return 0;
2319 498         tmpstr = AMG_CALLunary(sv, numer_amg);
2320 498 50       if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
    50        
    0        
2321 498 100       return SvIV(tmpstr);
2322           }
2323           }
2324 69784         return PTR2IV(SvRV(sv));
2325           }
2326            
2327 27692575 50       if (SvVALID(sv) || isREGEXP(sv)) {
    100        
    50        
2328           /* FBMs use the space for SvIVX and SvNVX for other purposes, and use
2329           the same flag bit as SVf_IVisUV, so must not let them cache IVs.
2330           In practice they are extremely unlikely to actually get anywhere
2331           accessible by user Perl code - the only way that I'm aware of is when
2332           a constant subroutine which is used as the second argument to index.
2333            
2334           Regexps have no SvIVX and SvNVX fields.
2335           */
2336           assert(isREGEXP(sv) || SvPOKp(sv));
2337           {
2338           UV value;
2339           const char * const ptr =
2340 3 50       isREGEXP(sv) ? RX_WRAPPED((REGEXP*)sv) : SvPVX_const(sv);
    0        
2341 2         const int numtype
2342 2         = grok_number(ptr, SvCUR(sv), &value);
2343            
2344 2 50       if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2345           == IS_NUMBER_IN_UV) {
2346           /* It's definitely an integer */
2347 0 0       if (numtype & IS_NUMBER_NEG) {
2348 0 0       if (value < (UV)IV_MIN)
2349 0         return -(IV)value;
2350           } else {
2351 0 0       if (value < (UV)IV_MAX)
2352 0         return (IV)value;
2353           }
2354           }
2355 2 50       if (!numtype) {
2356 2 50       if (ckWARN(WARN_NUMERIC))
2357 0         not_a_number(sv);
2358           }
2359 2         return I_V(Atof(ptr));
2360           }
2361           }
2362            
2363 27692573 100       if (SvTHINKFIRST(sv)) {
2364           #ifdef PERL_OLD_COPY_ON_WRITE
2365           if (SvIsCOW(sv)) {
2366           sv_force_normal_flags(sv, 0);
2367           }
2368           #endif
2369 2097016 100       if (SvREADONLY(sv) && !SvOK(sv)) {
    100        
    50        
    50        
2370 74 100       if (ckWARN(WARN_UNINITIALIZED))
2371 32         report_uninit(sv);
2372           return 0;
2373           }
2374           }
2375            
2376 27692499 100       if (!SvIOKp(sv)) {
2377 27668001 100       if (S_sv_2iuv_common(aTHX_ sv))
2378           return 0;
2379           }
2380            
2381           DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2iv(%"IVdf")\n",
2382           PTR2UV(sv),SvIVX(sv)));
2383 23569127 100       return SvIsUV(sv) ? (IV)SvUVX(sv) : SvIVX(sv);
2384           }
2385            
2386           /*
2387           =for apidoc sv_2uv_flags
2388            
2389           Return the unsigned integer value of an SV, doing any necessary string
2390           conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2391           Normally used via the C and C macros.
2392            
2393           =cut
2394           */
2395            
2396           UV
2397 28676011         Perl_sv_2uv_flags(pTHX_ SV *const sv, const I32 flags)
2398           {
2399           dVAR;
2400            
2401 28676011 50       if (!sv)
2402           return 0;
2403            
2404 28676011 100       if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
    100        
2405 12         mg_get(sv);
2406            
2407 28676011 100       if (SvROK(sv)) {
2408 12498064 50       if (SvAMAGIC(sv)) {
    100        
    100        
2409           SV *tmpstr;
2410 12495574 50       if (flags & SV_SKIP_OVERLOAD)
2411           return 0;
2412 12495574         tmpstr = AMG_CALLunary(sv, numer_amg);
2413 12495574 100       if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
    50        
    0        
2414 6 50       return SvUV(tmpstr);
2415           }
2416           }
2417 12498058         return PTR2UV(SvRV(sv));
2418           }
2419            
2420 16177947 50       if (SvVALID(sv) || isREGEXP(sv)) {
    100        
    50        
2421           /* FBMs use the space for SvIVX and SvNVX for other purposes, and use
2422           the same flag bit as SVf_IVisUV, so must not let them cache IVs.
2423           Regexps have no SvIVX and SvNVX fields. */
2424           assert(isREGEXP(sv) || SvPOKp(sv));
2425           {
2426           UV value;
2427           const char * const ptr =
2428 3 50       isREGEXP(sv) ? RX_WRAPPED((REGEXP*)sv) : SvPVX_const(sv);
    0        
2429 2         const int numtype
2430 2         = grok_number(ptr, SvCUR(sv), &value);
2431            
2432 2 50       if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2433           == IS_NUMBER_IN_UV) {
2434           /* It's definitely an integer */
2435 0 0       if (!(numtype & IS_NUMBER_NEG))
2436 0         return value;
2437           }
2438 2 50       if (!numtype) {
2439 2 50       if (ckWARN(WARN_NUMERIC))
2440 0         not_a_number(sv);
2441           }
2442 2         return U_V(Atof(ptr));
2443           }
2444           }
2445            
2446 16177945 100       if (SvTHINKFIRST(sv)) {
2447           #ifdef PERL_OLD_COPY_ON_WRITE
2448           if (SvIsCOW(sv)) {
2449           sv_force_normal_flags(sv, 0);
2450           }
2451           #endif
2452 2978976 100       if (SvREADONLY(sv) && !SvOK(sv)) {
    100        
    50        
    50        
2453 12 100       if (ckWARN(WARN_UNINITIALIZED))
2454 8         report_uninit(sv);
2455           return 0;
2456           }
2457           }
2458            
2459 16177933 100       if (!SvIOKp(sv)) {
2460 378980 100       if (S_sv_2iuv_common(aTHX_ sv))
2461           return 0;
2462           }
2463            
2464           DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2uv(%"UVuf")\n",
2465           PTR2UV(sv),SvUVX(sv)));
2466 22425969 100       return SvIsUV(sv) ? SvUVX(sv) : (UV)SvIVX(sv);
2467           }
2468            
2469           /*
2470           =for apidoc sv_2nv_flags
2471            
2472           Return the num value of an SV, doing any necessary string or integer
2473           conversion. If flags includes SV_GMAGIC, does an mg_get() first.
2474           Normally used via the C and C macros.
2475            
2476           =cut
2477           */
2478            
2479           NV
2480 8993530         Perl_sv_2nv_flags(pTHX_ SV *const sv, const I32 flags)
2481           {
2482           dVAR;
2483 8993530 50       if (!sv)
2484           return 0.0;
2485           assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
2486           && SvTYPE(sv) != SVt_PVFM);
2487 8993530 100       if (SvGMAGICAL(sv) || SvVALID(sv) || isREGEXP(sv)) {
    100        
    50        
2488           /* FBMs use the space for SvIVX and SvNVX for other purposes, and use
2489           the same flag bit as SVf_IVisUV, so must not let them cache NVs.
2490           Regexps have no SvIVX and SvNVX fields. */
2491           const char *ptr;
2492 696 100       if (flags & SV_GMAGIC)
2493 460         mg_get(sv);
2494 696 100       if (SvNOKp(sv))
2495 8         return SvNVX(sv);
2496 688 100       if (SvPOKp(sv) && !SvIOKp(sv)) {
2497 32         ptr = SvPVX_const(sv);
2498           grokpv:
2499 54 50       if (!SvIOKp(sv) && ckWARN(WARN_NUMERIC) &&
2500 12         !grok_number(ptr, SvCUR(sv), NULL))
2501 4         not_a_number(sv);
2502 42         return Atof(ptr);
2503           }
2504 656 100       if (SvIOKp(sv)) {
2505 602 50       if (SvIsUV(sv))
2506 0         return (NV)SvUVX(sv);
2507           else
2508 602         return (NV)SvIVX(sv);
2509           }
2510 54 50       if (SvROK(sv)) {
2511           goto return_rok;
2512           }
2513 54 100       if (isREGEXP(sv)) {
    50        
2514 10         ptr = RX_WRAPPED((REGEXP *)sv);
2515 10         goto grokpv;
2516           }
2517           assert(SvTYPE(sv) >= SVt_PVMG);
2518           /* This falls through to the report_uninit near the end of the
2519           function. */
2520 8992834 100       } else if (SvTHINKFIRST(sv)) {
2521 893742 100       if (SvROK(sv)) {
2522           return_rok:
2523 52 50       if (SvAMAGIC(sv)) {
    50        
    50        
2524           SV *tmpstr;
2525 52 50       if (flags & SV_SKIP_OVERLOAD)
2526           return 0;
2527 52         tmpstr = AMG_CALLunary(sv, numer_amg);
2528 52 100       if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
    50        
    0        
2529 44 100       return SvNV(tmpstr);
2530           }
2531           }
2532 8         return PTR2NV(SvRV(sv));
2533           }
2534           #ifdef PERL_OLD_COPY_ON_WRITE
2535           if (SvIsCOW(sv)) {
2536           sv_force_normal_flags(sv, 0);
2537           }
2538           #endif
2539 893690 100       if (SvREADONLY(sv) && !SvOK(sv)) {
    100        
    50        
    50        
2540 405766 100       if (ckWARN(WARN_UNINITIALIZED))
2541 174         report_uninit(sv);
2542           return 0.0;
2543           }
2544           }
2545 8587060 100       if (SvTYPE(sv) < SVt_NV) {
2546           /* The logic to use SVt_PVNV if necessary is in sv_upgrade. */
2547 1934284         sv_upgrade(sv, SVt_NV);
2548           #ifdef USE_LONG_DOUBLE
2549           DEBUG_c({
2550           STORE_NUMERIC_LOCAL_SET_STANDARD();
2551           PerlIO_printf(Perl_debug_log,
2552           "0x%"UVxf" num(%" PERL_PRIgldbl ")\n",
2553           PTR2UV(sv), SvNVX(sv));
2554           RESTORE_NUMERIC_LOCAL();
2555           });
2556           #else
2557           DEBUG_c({
2558           STORE_NUMERIC_LOCAL_SET_STANDARD();
2559           PerlIO_printf(Perl_debug_log, "0x%"UVxf" num(%"NVgf")\n",
2560           PTR2UV(sv), SvNVX(sv));
2561           RESTORE_NUMERIC_LOCAL();
2562           });
2563           #endif
2564           }
2565 6652776 100       else if (SvTYPE(sv) < SVt_PVNV)
2566 3070706         sv_upgrade(sv, SVt_PVNV);
2567 8587060 100       if (SvNOKp(sv)) {
2568 23814         return SvNVX(sv);
2569           }
2570 8563246 100       if (SvIOKp(sv)) {
2571 8222466 100       SvNV_set(sv, SvIsUV(sv) ? (NV)SvUVX(sv) : (NV)SvIVX(sv));
2572           #ifdef NV_PRESERVES_UV
2573           if (SvIOK(sv))
2574           SvNOK_on(sv);
2575           else
2576           SvNOKp_on(sv);
2577           #else
2578           /* Only set the public NV OK flag if this NV preserves the IV */
2579           /* Check it's not 0xFFFFFFFFFFFFFFFF */
2580 20511392 50       if (SvIOK(sv) &&
    100        
    100        
    100        
2581 4072435 100       SvIsUV(sv) ? ((SvUVX(sv) != UV_MAX)&&(SvUVX(sv) == U_V(SvNVX(sv))))
2582 8215650         : (SvIVX(sv) == I_V(SvNVX(sv))))
2583 8211708         SvNOK_on(sv);
2584           else
2585 10758         SvNOKp_on(sv);
2586           #endif
2587           }
2588 340780 100       else if (SvPOKp(sv)) {
2589           UV value;
2590 336584         const int numtype = grok_number(SvPVX_const(sv), SvCUR(sv), &value);
2591 336584 100       if (!SvIOKp(sv) && !numtype && ckWARN(WARN_NUMERIC))
    100        
2592 2         not_a_number(sv);
2593           #ifdef NV_PRESERVES_UV
2594           if ((numtype & (IS_NUMBER_IN_UV | IS_NUMBER_NOT_INT))
2595           == IS_NUMBER_IN_UV) {
2596           /* It's definitely an integer */
2597           SvNV_set(sv, (numtype & IS_NUMBER_NEG) ? -(NV)value : (NV)value);
2598           } else
2599           SvNV_set(sv, Atof(SvPVX_const(sv)));
2600           if (numtype)
2601           SvNOK_on(sv);
2602           else
2603           SvNOKp_on(sv);
2604           #else
2605 336582         SvNV_set(sv, Atof(SvPVX_const(sv)));
2606           /* Only set the public NV OK flag if this NV preserves the value in
2607           the PV at least as well as an IV/UV would.
2608           Not sure how to do this 100% reliably. */
2609           /* if that shift count is out of range then Configure's test is
2610           wonky. We shouldn't be in here with NV_PRESERVES_UV_BITS ==
2611           UV_BITS */
2612 336582 100       if (((UV)1 << NV_PRESERVES_UV_BITS) >
2613 336582 100       U_V(SvNVX(sv) > 0 ? SvNVX(sv) : -SvNVX(sv))) {
2614 334058         SvNOK_on(sv); /* Definitely small enough to preserve all bits */
2615 2524 100       } else if (!(numtype & IS_NUMBER_IN_UV)) {
2616           /* Can't use strtol etc to convert this string, so don't try.
2617           sv_2iv and sv_2uv will use the NV to convert, not the PV. */
2618 718         SvNOK_on(sv);
2619           } else {
2620           /* value has been set. It may not be precise. */
2621 1806 100       if ((numtype & IS_NUMBER_NEG) && (value > (UV)IV_MIN)) {
    50        
2622           /* 2s complement assumption for (UV)IV_MIN */
2623 0         SvNOK_on(sv); /* Integer is too negative. */
2624           } else {
2625 1806         SvNOKp_on(sv);
2626 1806         SvIOKp_on(sv);
2627            
2628 1806 100       if (numtype & IS_NUMBER_NEG) {
2629 594         SvIV_set(sv, -(IV)value);
2630 1212 100       } else if (value <= (UV)IV_MAX) {
2631 462         SvIV_set(sv, (IV)value);
2632           } else {
2633 750         SvUV_set(sv, value);
2634 750         SvIsUV_on(sv);
2635           }
2636            
2637 1806 50       if (numtype & IS_NUMBER_NOT_INT) {
2638           /* I believe that even if the original PV had decimals,
2639           they are lost beyond the limit of the FP precision.
2640           However, neither is canonical, so both only get p
2641           flags. NWC, 2000/11/25 */
2642           /* Both already have p flags, so do nothing */
2643           } else {
2644 1806         const NV nv = SvNVX(sv);
2645 1806 100       if (SvNVX(sv) < (NV)IV_MAX + 0.5) {
2646 1042 100       if (SvIVX(sv) == I_V(nv)) {
2647 246         SvNOK_on(sv);
2648           } else {
2649           /* It had no "." so it must be integer. */
2650           }
2651 1042         SvIOK_on(sv);
2652           } else {
2653           /* between IV_MAX and NV(UV_MAX).
2654           Could be slightly > UV_MAX */
2655            
2656 764 50       if (numtype & IS_NUMBER_NOT_INT) {
2657           /* UV and NV both imprecise. */
2658           } else {
2659 764         const UV nv_as_uv = U_V(nv);
2660            
2661 764 100       if (value == nv_as_uv && SvUVX(sv) != UV_MAX) {
    100        
2662 246         SvNOK_on(sv);
2663           }
2664 764         SvIOK_on(sv);
2665           }
2666           }
2667           }
2668           }
2669           }
2670           /* It might be more code efficient to go through the entire logic above
2671           and conditionally set with SvNOKp_on() rather than SvNOK(), but it
2672           gets complex and potentially buggy, so more programmer efficient
2673           to do it this way, by turning off the public flags: */
2674 336582 100       if (!numtype)
2675 1656         SvFLAGS(sv) &= ~(SVf_IOK|SVf_NOK);
2676           #endif /* NV_PRESERVES_UV */
2677           }
2678           else {
2679 4196 100       if (isGV_with_GP(sv)) {
    50        
2680 32         glob_2number(MUTABLE_GV(sv));
2681 32         return 0.0;
2682           }
2683            
2684 4164 50       if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
    100        
2685 306         report_uninit(sv);
2686           assert (SvTYPE(sv) >= SVt_NV);
2687           /* Typically the caller expects that sv_any is not NULL now. */
2688           /* XXX Ilya implies that this is a bug in callers that assume this
2689           and ideally should be fixed. */
2690           return 0.0;
2691           }
2692           #if defined(USE_LONG_DOUBLE)
2693           DEBUG_c({
2694           STORE_NUMERIC_LOCAL_SET_STANDARD();
2695           PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2nv(%" PERL_PRIgldbl ")\n",
2696           PTR2UV(sv), SvNVX(sv));
2697           RESTORE_NUMERIC_LOCAL();
2698           });
2699           #else
2700           DEBUG_c({
2701           STORE_NUMERIC_LOCAL_SET_STANDARD();
2702           PerlIO_printf(Perl_debug_log, "0x%"UVxf" 1nv(%"NVgf")\n",
2703           PTR2UV(sv), SvNVX(sv));
2704           RESTORE_NUMERIC_LOCAL();
2705           });
2706           #endif
2707 8776288         return SvNVX(sv);
2708           }
2709            
2710           /*
2711           =for apidoc sv_2num
2712            
2713           Return an SV with the numeric value of the source SV, doing any necessary
2714           reference or overload conversion. You must use the C macro to
2715           access this function.
2716            
2717           =cut
2718           */
2719            
2720           SV *
2721 4668699         Perl_sv_2num(pTHX_ SV *const sv)
2722           {
2723           PERL_ARGS_ASSERT_SV_2NUM;
2724            
2725 4669624 100       if (!SvROK(sv))
2726           return sv;
2727 4667774 50       if (SvAMAGIC(sv)) {
    100        
    100        
2728 563436         SV * const tmpsv = AMG_CALLunary(sv, numer_amg);
2729 563436 100       TAINT_IF(tmpsv && SvTAINTED(tmpsv));
    50        
    0        
    50        
2730 563436 100       if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
    50        
    0        
2731           return sv_2num(tmpsv);
2732           }
2733 4666849         return sv_2mortal(newSVuv(PTR2UV(SvRV(sv))));
2734           }
2735            
2736           /* uiv_2buf(): private routine for use by sv_2pv_flags(): print an IV or
2737           * UV as a string towards the end of buf, and return pointers to start and
2738           * end of it.
2739           *
2740           * We assume that buf is at least TYPE_CHARS(UV) long.
2741           */
2742            
2743           static char *
2744           S_uiv_2buf(char *const buf, const IV iv, UV uv, const int is_uv, char **const peob)
2745           {
2746           char *ptr = buf + TYPE_CHARS(UV);
2747           char * const ebuf = ptr;
2748           int sign;
2749            
2750           PERL_ARGS_ASSERT_UIV_2BUF;
2751            
2752 190248255 100       if (is_uv)
2753           sign = 0;
2754 190230923 0       else if (iv >= 0) {
    100        
2755 190132539         uv = iv;
2756           sign = 0;
2757           } else {
2758 95213454         uv = -iv;
2759           sign = 1;
2760           }
2761           do {
2762 1429742796         *--ptr = '0' + (char)(uv % 10);
2763 1429742796 0       } while (uv /= 10);
    100        
2764 190248255 0       if (sign)
    100        
2765 98384         *--ptr = '-';
2766           *peob = ebuf;
2767           return ptr;
2768           }
2769            
2770           /*
2771           =for apidoc sv_2pv_flags
2772            
2773           Returns a pointer to the string value of an SV, and sets *lp to its length.
2774           If flags includes SV_GMAGIC, does an mg_get() first. Coerces sv to a
2775           string if necessary. Normally invoked via the C macro.
2776           C and C usually end up here too.
2777            
2778           =cut
2779           */
2780            
2781           char *
2782 223911065         Perl_sv_2pv_flags(pTHX_ SV *const sv, STRLEN *const lp, const I32 flags)
2783           {
2784           dVAR;
2785           char *s;
2786            
2787 223911065 50       if (!sv) {
2788 0 0       if (lp)
2789 0         *lp = 0;
2790           return (char *)"";
2791           }
2792           assert (SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV
2793           && SvTYPE(sv) != SVt_PVFM);
2794 223911065 100       if (SvGMAGICAL(sv) && (flags & SV_GMAGIC))
    100        
2795 14085628         mg_get(sv);
2796 223911059 100       if (SvROK(sv)) {
2797 4191280 50       if (SvAMAGIC(sv)) {
    100        
    100        
2798           SV *tmpstr;
2799 2008748 100       if (flags & SV_SKIP_OVERLOAD)
2800           return NULL;
2801 2008516         tmpstr = AMG_CALLunary(sv, string_amg);
2802 2008516 100       TAINT_IF(tmpstr && SvTAINTED(tmpstr));
    50        
    0        
    50        
2803 2008516 100       if (tmpstr && (!SvROK(tmpstr) || (SvRV(tmpstr) != SvRV(sv)))) {
    100        
    50        
2804           /* Unwrap this: */
2805           /* char *pv = lp ? SvPV(tmpstr, *lp) : SvPV_nolen(tmpstr);
2806           */
2807            
2808           char *pv;
2809 68268 100       if ((SvFLAGS(tmpstr) & (SVf_POK)) == SVf_POK) {
2810 65352 100       if (flags & SV_CONST_RETURN) {
2811 61848         pv = (char *) SvPVX_const(tmpstr);
2812           } else {
2813 3504         pv = (flags & SV_MUTABLE_RETURN)
2814           ? SvPVX_mutable(tmpstr) : SvPVX(tmpstr);
2815           }
2816 65352 100       if (lp)
2817 65312         *lp = SvCUR(tmpstr);
2818           } else {
2819 2916         pv = sv_2pv_flags(tmpstr, lp, flags);
2820           }
2821 68268 100       if (SvUTF8(tmpstr))
2822 334         SvUTF8_on(sv);
2823           else
2824 67934         SvUTF8_off(sv);
2825           return pv;
2826           }
2827           }
2828           {
2829           STRLEN len;
2830           char *retval;
2831           char *buffer;
2832 4122780         SV *const referent = SvRV(sv);
2833            
2834 4122780 50       if (!referent) {
2835           len = 7;
2836 0         retval = buffer = savepvn("NULLREF", len);
2837 4329372 100       } else if (SvTYPE(referent) == SVt_REGEXP &&
    100        
2838 206620 100       (!(PL_curcop->cop_hints & HINT_NO_AMAGIC) ||
2839 28         amagic_is_enabled(string_amg))) {
2840           REGEXP * const re = (REGEXP *)MUTABLE_PTR(referent);
2841            
2842           assert(re);
2843          
2844           /* If the regex is UTF-8 we want the containing scalar to
2845           have an UTF-8 flag too */
2846 413162 100       if (RX_UTF8(re))
2847 40         SvUTF8_on(sv);
2848           else
2849 413122         SvUTF8_off(sv);
2850            
2851 413162 50       if (lp)
2852 413162         *lp = RX_WRAPLEN(re);
2853          
2854 413162         return RX_WRAPPED(re);
2855           } else {
2856 3709618         const char *const typestr = sv_reftype(referent, 0);
2857 3709618         const STRLEN typelen = strlen(typestr);
2858 3709618         UV addr = PTR2UV(referent);
2859           const char *stashname = NULL;
2860           STRLEN stashnamelen = 0; /* hush, gcc */
2861           const char *buffer_end;
2862            
2863 3709618 100       if (SvOBJECT(referent)) {
2864 2624002 50       const HEK *const name = HvNAME_HEK(SvSTASH(referent));
    50        
    100        
2865            
2866 2624002 100       if (name) {
2867 2623998         stashname = HEK_KEY(name);
2868 2623998         stashnamelen = HEK_LEN(name);
2869            
2870 2623998 100       if (HEK_UTF8(name)) {
2871 174         SvUTF8_on(sv);
2872           } else {
2873 2623824         SvUTF8_off(sv);
2874           }
2875           } else {
2876           stashname = "__ANON__";
2877           stashnamelen = 8;
2878           }
2879 2624002         len = stashnamelen + 1 /* = */ + typelen + 3 /* (0x */
2880           + 2 * sizeof(UV) + 2 /* )\0 */;
2881           } else {
2882 1085616         len = typelen + 3 /* (0x */
2883           + 2 * sizeof(UV) + 2 /* )\0 */;
2884           }
2885            
2886 3709618         Newx(buffer, len, char);
2887 3709618         buffer_end = retval = buffer + len;
2888            
2889           /* Working backwards */
2890 3709618         *--retval = '\0';
2891 3709618         *--retval = ')';
2892           do {
2893 25961388         *--retval = PL_hexdigit[addr & 15];
2894 25961388 100       } while (addr >>= 4);
2895 3709618         *--retval = 'x';
2896 3709618         *--retval = '0';
2897 3709618         *--retval = '(';
2898            
2899 3709618         retval -= typelen;
2900 3709618         memcpy(retval, typestr, typelen);
2901            
2902 3709618 100       if (stashname) {
2903 2624002         *--retval = '=';
2904 2624002         retval -= stashnamelen;
2905 2624002         memcpy(retval, stashname, stashnamelen);
2906           }
2907           /* retval may not necessarily have reached the start of the
2908           buffer here. */
2909           assert (retval >= buffer);
2910            
2911 3709618         len = buffer_end - retval - 1; /* -1 for that \0 */
2912           }
2913 3709618 100       if (lp)
2914 3683200         *lp = len;
2915 3709618         SAVEFREEPV(buffer);
2916 3709618         return retval;
2917           }
2918           }
2919            
2920 219719779 100       if (SvPOKp(sv)) {
2921 27682226 100       if (lp)
2922 27680776         *lp = SvCUR(sv);
2923 27682226 100       if (flags & SV_MUTABLE_RETURN)
2924 650         return SvPVX_mutable(sv);
2925 27681576 100       if (flags & SV_CONST_RETURN)
2926 27656214         return (char *)SvPVX_const(sv);
2927 25362         return SvPVX(sv);
2928           }
2929            
2930 192037553 100       if (SvIOK(sv)) {
2931           /* I'm assuming that if both IV and NV are equally valid then
2932           converting the IV is going to be more efficient */
2933 190248255         const U32 isUIOK = SvIsUV(sv);
2934           char buf[TYPE_CHARS(UV)];
2935           char *ebuf, *ptr;
2936           STRLEN len;
2937            
2938 190248255 100       if (SvTYPE(sv) < SVt_PVIV)
2939 162469671         sv_upgrade(sv, SVt_PVIV);
2940 190248255         ptr = uiv_2buf(buf, SvIVX(sv), SvUVX(sv), isUIOK, &ebuf);
2941 190248255         len = ebuf - ptr;
2942           /* inlined from sv_setpvn */
2943 190248255 100       s = SvGROW_mutable(sv, len + 1);
2944           Move(ptr, s, len, char);
2945 190248255         s += len;
2946 190248255         *s = '\0';
2947 190248255         SvPOK_on(sv);
2948           }
2949 1789298 100       else if (SvNOK(sv)) {
2950 125340 100       if (SvTYPE(sv) < SVt_PVNV)
2951 15410         sv_upgrade(sv, SVt_PVNV);
2952 125340 100       if (SvNVX(sv) == 0.0) {
2953 4042 100       s = SvGROW_mutable(sv, 2);
2954 4042         *s++ = '0';
2955 4042         *s = '\0';
2956           } else {
2957 121298         dSAVE_ERRNO;
2958           /* The +20 is pure guesswork. Configure test needed. --jhi */
2959 121298 100       s = SvGROW_mutable(sv, NV_DIG + 20);
2960           /* some Xenix systems wipe out errno here */
2961            
2962           #ifndef USE_LOCALE_NUMERIC
2963           Gconvert(SvNVX(sv), NV_DIG, 0, s);
2964           SvPOK_on(sv);
2965           #else
2966           /* Gconvert always uses the current locale. That's the right thing
2967           * to do if we're supposed to be using locales. But otherwise, we
2968           * want the result to be based on the C locale, so we need to
2969           * change to the C locale during the Gconvert and then change back.
2970           * But if we're already in the C locale (PL_numeric_standard is
2971           * TRUE in that case), no need to do any changing */
2972 121298 100       if (PL_numeric_standard || IN_SOME_LOCALE_FORM_RUNTIME) {
    50        
2973 121294         Gconvert(SvNVX(sv), NV_DIG, 0, s);
2974            
2975           /* If the radix character is UTF-8, and actually is in the
2976           * output, turn on the UTF-8 flag for the scalar */
2977 121294 50       if (! PL_numeric_standard
2978 0 0       && PL_numeric_radix_sv && SvUTF8(PL_numeric_radix_sv)
    0        
2979 0 0       && instr(s, SvPVX_const(PL_numeric_radix_sv)))
2980           {
2981 0         SvUTF8_on(sv);
2982           }
2983           }
2984           else {
2985 4         char *loc = savepv(setlocale(LC_NUMERIC, NULL));
2986 4         setlocale(LC_NUMERIC, "C");
2987 4         Gconvert(SvNVX(sv), NV_DIG, 0, s);
2988 4         setlocale(LC_NUMERIC, loc);
2989 4         Safefree(loc);
2990            
2991           }
2992            
2993           /* We don't call SvPOK_on(), because it may come to pass that the
2994           * locale changes so that the stringification we just did is no
2995           * longer correct. We will have to re-stringify every time it is
2996           * needed */
2997           #endif
2998 121298         RESTORE_ERRNO;
2999 821082 100       while (*s) s++;
3000           }
3001           #ifdef hcx
3002           if (s[-1] == '.')
3003           *--s = '\0';
3004           #endif
3005           }
3006 1663958 100       else if (isGV_with_GP(sv)) {
    50        
3007           GV *const gv = MUTABLE_GV(sv);
3008 1666         SV *const buffer = sv_newmortal();
3009            
3010 1666         gv_efullname3(buffer, gv, "*");
3011            
3012           assert(SvPOK(buffer));
3013 1666 100       if (SvUTF8(buffer))
3014 134         SvUTF8_on(sv);
3015 1666 100       if (lp)
3016 1646         *lp = SvCUR(buffer);
3017 1666         return SvPVX(buffer);
3018           }
3019 1662292 100       else if (isREGEXP(sv)) {
    100        
3020 1593132 50       if (lp) *lp = RX_WRAPLEN((REGEXP *)sv);
3021 1593132         return RX_WRAPPED((REGEXP *)sv);
3022           }
3023           else {
3024 69160 100       if (lp)
3025 69092         *lp = 0;
3026 69160 100       if (flags & SV_UNDEF_RETURNS_NULL)
3027           return NULL;
3028 69136 100       if (!PL_localizing && ckWARN(WARN_UNINITIALIZED))
    100        
3029 2264         report_uninit(sv);
3030           /* Typically the caller expects that sv_any is not NULL now. */
3031 69098 100       if (!SvREADONLY(sv) && SvTYPE(sv) < SVt_PV)
    100        
3032 2092         sv_upgrade(sv, SVt_PV);
3033           return (char *)"";
3034           }
3035            
3036           {
3037 190373595         const STRLEN len = s - SvPVX_const(sv);
3038 190373595 100       if (lp)
3039 190371357         *lp = len;
3040 190373595         SvCUR_set(sv, len);
3041           }
3042           DEBUG_c(PerlIO_printf(Perl_debug_log, "0x%"UVxf" 2pv(%s)\n",
3043           PTR2UV(sv),SvPVX_const(sv)));
3044 190373595 100       if (flags & SV_CONST_RETURN)
3045 190085597         return (char *)SvPVX_const(sv);
3046 287998 100       if (flags & SV_MUTABLE_RETURN)
3047 8         return SvPVX_mutable(sv);
3048 112144318         return SvPVX(sv);
3049           }
3050            
3051           /*
3052           =for apidoc sv_copypv
3053            
3054           Copies a stringified representation of the source SV into the
3055           destination SV. Automatically performs any necessary mg_get and
3056           coercion of numeric values into strings. Guaranteed to preserve
3057           UTF8 flag even from overloaded objects. Similar in nature to
3058           sv_2pv[_flags] but operates directly on an SV instead of just the
3059           string. Mostly uses sv_2pv_flags to do its work, except when that
3060           would lose the UTF-8'ness of the PV.
3061            
3062           =for apidoc sv_copypv_nomg
3063            
3064           Like sv_copypv, but doesn't invoke get magic first.
3065            
3066           =for apidoc sv_copypv_flags
3067            
3068           Implementation of sv_copypv and sv_copypv_nomg. Calls get magic iff flags
3069           include SV_GMAGIC.
3070            
3071           =cut
3072           */
3073            
3074           void
3075 0         Perl_sv_copypv(pTHX_ SV *const dsv, SV *const ssv)
3076           {
3077           PERL_ARGS_ASSERT_SV_COPYPV;
3078            
3079 0         sv_copypv_flags(dsv, ssv, 0);
3080 0         }
3081            
3082           void
3083 6811770         Perl_sv_copypv_flags(pTHX_ SV *const dsv, SV *const ssv, const I32 flags)
3084           {
3085           STRLEN len;
3086           const char *s;
3087            
3088           PERL_ARGS_ASSERT_SV_COPYPV_FLAGS;
3089            
3090 6811770 100       if ((flags & SV_GMAGIC) && SvGMAGICAL(ssv))
    100        
3091 40500         mg_get(ssv);
3092 6811768 100       s = SvPV_nomg_const(ssv,len);
3093 6811768         sv_setpvn(dsv,s,len);
3094 6811768 100       if (SvUTF8(ssv))
3095 361862         SvUTF8_on(dsv);
3096           else
3097 6449906         SvUTF8_off(dsv);
3098 6811768         }
3099            
3100           /*
3101           =for apidoc sv_2pvbyte
3102            
3103           Return a pointer to the byte-encoded representation of the SV, and set *lp
3104           to its length. May cause the SV to be downgraded from UTF-8 as a
3105           side-effect.
3106            
3107           Usually accessed via the C macro.
3108            
3109           =cut
3110           */
3111            
3112           char *
3113 1780         Perl_sv_2pvbyte(pTHX_ SV *sv, STRLEN *const lp)
3114 1780 100       {
3115           PERL_ARGS_ASSERT_SV_2PVBYTE;
3116            
3117 904         SvGETMAGIC(sv);
3118 1780 100       if (((SvREADONLY(sv) || SvFAKE(sv)) && !SvIsCOW(sv))
    50        
3119 1768 100       || isGV_with_GP(sv) || SvROK(sv)) {
    50        
    100        
3120 22         SV *sv2 = sv_newmortal();
3121 22         sv_copypv_nomg(sv2,sv);
3122           sv = sv2;
3123           }
3124 1780         sv_utf8_downgrade(sv,0);
3125 1772 100       return lp ? SvPV_nomg(sv,*lp) : SvPV_nomg_nolen(sv);
    100        
    100        
3126           }
3127            
3128           /*
3129           =for apidoc sv_2pvutf8
3130            
3131           Return a pointer to the UTF-8-encoded representation of the SV, and set *lp
3132           to its length. May cause the SV to be upgraded to UTF-8 as a side-effect.
3133            
3134           Usually accessed via the C macro.
3135            
3136           =cut
3137           */
3138            
3139           char *
3140 392         Perl_sv_2pvutf8(pTHX_ SV *sv, STRLEN *const lp)
3141           {
3142           PERL_ARGS_ASSERT_SV_2PVUTF8;
3143            
3144 776 100       if (((SvREADONLY(sv) || SvFAKE(sv)) && !SvIsCOW(sv))
    50        
    100        
3145 388 100       || isGV_with_GP(sv) || SvROK(sv))
    50        
    100        
3146 8         sv = sv_mortalcopy(sv);
3147           else
3148 198         SvGETMAGIC(sv);
3149 392         sv_utf8_upgrade_nomg(sv);
3150 392 100       return lp ? SvPV_nomg(sv,*lp) : SvPV_nomg_nolen(sv);
    100        
    100        
3151           }
3152            
3153            
3154           /*
3155           =for apidoc sv_2bool
3156            
3157           This macro is only used by sv_true() or its macro equivalent, and only if
3158           the latter's argument is neither SvPOK, SvIOK nor SvNOK.
3159           It calls sv_2bool_flags with the SV_GMAGIC flag.
3160            
3161           =for apidoc sv_2bool_flags
3162            
3163           This function is only used by sv_true() and friends, and only if
3164           the latter's argument is neither SvPOK, SvIOK nor SvNOK. If the flags
3165           contain SV_GMAGIC, then it does an mg_get() first.
3166            
3167            
3168           =cut
3169           */
3170            
3171           bool
3172 117332049         Perl_sv_2bool_flags(pTHX_ SV *const sv, const I32 flags)
3173           {
3174           dVAR;
3175            
3176           PERL_ARGS_ASSERT_SV_2BOOL_FLAGS;
3177            
3178 117332049 100       if(flags & SV_GMAGIC) SvGETMAGIC(sv);
    50        
3179            
3180 117332049 100       if (!SvOK(sv))
    100        
    50        
3181           return 0;
3182 116897113 100       if (SvROK(sv)) {
3183 114118443 50       if (SvAMAGIC(sv)) {
    100        
    100        
3184 122850         SV * const tmpsv = AMG_CALLunary(sv, bool__amg);
3185 122850 100       if (tmpsv && (!SvROK(tmpsv) || (SvRV(tmpsv) != SvRV(sv))))
    50        
    0        
3186 23542 50       return cBOOL(SvTRUE(tmpsv));
    50        
    0        
    50        
    0        
    0        
    100        
    50        
    100        
    100        
    50        
    100        
    50        
    100        
    50        
    50        
    50        
    50        
    0        
3187           }
3188 114094901         return SvRV(sv) != 0;
3189           }
3190 2778670 100       if (isREGEXP(sv))
    50        
3191 2         return
3192 2 50       RX_WRAPLEN(sv) > 1 || (RX_WRAPLEN(sv) && *RX_WRAPPED(sv) != '0');
    0        
    0        
3193 60072277 50       return SvTRUE_common(sv, isGV_with_GP(sv) ? 1 : 0);
    0        
    0        
    100        
    50        
    100        
    100        
    100        
    100        
    50        
    100        
    50        
    0        
    50        
    50        
3194           }
3195            
3196           /*
3197           =for apidoc sv_utf8_upgrade
3198            
3199           Converts the PV of an SV to its UTF-8-encoded form.
3200           Forces the SV to string form if it is not already.
3201           Will C on C if appropriate.
3202           Always sets the SvUTF8 flag to avoid future validity checks even
3203           if the whole string is the same in UTF-8 as not.
3204           Returns the number of bytes in the converted string
3205            
3206           This is not a general purpose byte encoding to Unicode interface:
3207           use the Encode extension for that.
3208            
3209           =for apidoc sv_utf8_upgrade_nomg
3210            
3211           Like sv_utf8_upgrade, but doesn't do magic on C.
3212            
3213           =for apidoc sv_utf8_upgrade_flags
3214            
3215           Converts the PV of an SV to its UTF-8-encoded form.
3216           Forces the SV to string form if it is not already.
3217           Always sets the SvUTF8 flag to avoid future validity checks even
3218           if all the bytes are invariant in UTF-8.
3219           If C has C bit set,
3220           will C on C if appropriate, else not.
3221           Returns the number of bytes in the converted string
3222           C and
3223           C are implemented in terms of this function.
3224            
3225           This is not a general purpose byte encoding to Unicode interface:
3226           use the Encode extension for that.
3227            
3228           =cut
3229            
3230           The grow version is currently not externally documented. It adds a parameter,
3231           extra, which is the number of unused bytes the string of 'sv' is guaranteed to
3232           have free after it upon return. This allows the caller to reserve extra space
3233           that it intends to fill, to avoid extra grows.
3234            
3235           Also externally undocumented for the moment is the flag SV_FORCE_UTF8_UPGRADE,
3236           which can be used to tell this function to not first check to see if there are
3237           any characters that are different in UTF-8 (variant characters) which would
3238           force it to allocate a new string to sv, but to assume there are. Typically
3239           this flag is used by a routine that has already parsed the string to find that
3240           there are such characters, and passes this information on so that the work
3241           doesn't have to be repeated.
3242            
3243           (One might think that the calling routine could pass in the position of the
3244           first such variant, so it wouldn't have to be found again. But that is not the
3245           case, because typically when the caller is likely to use this flag, it won't be
3246           calling this routine unless it finds something that won't fit into a byte.
3247           Otherwise it tries to not upgrade and just use bytes. But some things that
3248           do fit into a byte are variants in utf8, and the caller may not have been
3249           keeping track of these.)
3250            
3251           If the routine itself changes the string, it adds a trailing NUL. Such a NUL
3252           isn't guaranteed due to having other routines do the work in some input cases,
3253           or if the input is already flagged as being in utf8.
3254            
3255           The speed of this could perhaps be improved for many cases if someone wanted to
3256           write a fast function that counts the number of variant characters in a string,
3257           especially if it could return the position of the first one.
3258            
3259           */
3260            
3261           STRLEN
3262 5384668         Perl_sv_utf8_upgrade_flags_grow(pTHX_ SV *const sv, const I32 flags, STRLEN extra)
3263           {
3264           dVAR;
3265            
3266           PERL_ARGS_ASSERT_SV_UTF8_UPGRADE_FLAGS_GROW;
3267            
3268 5384668 50       if (sv == &PL_sv_undef)
3269           return 0;
3270 5384668 100       if (!SvPOK_nog(sv)) {
3271 350202         STRLEN len = 0;
3272 350202 50       if (SvREADONLY(sv) && (SvPOKp(sv) || SvIOKp(sv) || SvNOKp(sv))) {
    0        
3273 0         (void) sv_2pv_flags(sv,&len, flags);
3274 0 0       if (SvUTF8(sv)) {
3275 0 0       if (extra) SvGROW(sv, SvCUR(sv) + extra);
    0        
    0        
3276 0         return len;
3277           }
3278           } else {
3279 350202 50       (void) SvPV_force_flags(sv,len,flags & SV_GMAGIC);
3280           }
3281           }
3282            
3283 5384668 100       if (SvUTF8(sv)) {
3284 855316 50       if (extra) SvGROW(sv, SvCUR(sv) + extra);
    0        
    0        
3285 855316         return SvCUR(sv);
3286           }
3287            
3288 4529352 100       if (SvIsCOW(sv)) {
3289 517012         S_sv_uncow(aTHX_ sv, 0);
3290           }
3291            
3292 4529352 100       if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING)) {
    50        
3293 55058         sv_recode_to_utf8(sv, PL_encoding);
3294 55054 100       if (extra) SvGROW(sv, SvCUR(sv) + extra);
    50        
    100        
3295 55054         return SvCUR(sv);
3296           }
3297            
3298 4474294 100       if (SvCUR(sv) == 0) {
3299 947628 100       if (extra) SvGROW(sv, extra);
    50        
    100        
3300           } else { /* Assume Latin-1/EBCDIC */
3301           /* This function could be much more efficient if we
3302           * had a FLAG in SVs to signal if there are any variant
3303           * chars in the PV. Given that there isn't such a flag
3304           * make the loop as fast as possible (although there are certainly ways
3305           * to speed this up, eg. through vectorization) */
3306 3526666         U8 * s = (U8 *) SvPVX_const(sv);
3307 3526666         U8 * e = (U8 *) SvEND(sv);
3308           U8 *t = s;
3309           STRLEN two_byte_count = 0;
3310          
3311 3526666 100       if (flags & SV_FORCE_UTF8_UPGRADE) goto must_be_utf8;
3312            
3313           /* See if really will need to convert to utf8. We mustn't rely on our
3314           * incoming SV being well formed and having a trailing '\0', as certain
3315           * code in pp_formline can send us partially built SVs. */
3316            
3317 40563332 100       while (t < e) {
3318 37381256         const U8 ch = *t++;
3319 37381256 100       if (NATIVE_IS_INVARIANT(ch)) continue;
3320            
3321           t--; /* t already incremented; re-point to first variant */
3322           two_byte_count = 1;
3323           goto must_be_utf8;
3324           }
3325            
3326           /* utf8 conversion not needed because all are invariants. Mark as
3327           * UTF-8 even if no variant - saves scanning loop */
3328 3182076         SvUTF8_on(sv);
3329 3182076 100       if (extra) SvGROW(sv, SvCUR(sv) + extra);
    50        
    100        
3330 3182076         return SvCUR(sv);
3331            
3332           must_be_utf8:
3333            
3334           /* Here, the string should be converted to utf8, either because of an
3335           * input flag (two_byte_count = 0), or because a character that
3336           * requires 2 bytes was found (two_byte_count = 1). t points either to
3337           * the beginning of the string (if we didn't examine anything), or to
3338           * the first variant. In either case, everything from s to t - 1 will
3339           * occupy only 1 byte each on output.
3340           *
3341           * There are two main ways to convert. One is to create a new string
3342           * and go through the input starting from the beginning, appending each
3343           * converted value onto the new string as we go along. It's probably
3344           * best to allocate enough space in the string for the worst possible
3345           * case rather than possibly running out of space and having to
3346           * reallocate and then copy what we've done so far. Since everything
3347           * from s to t - 1 is invariant, the destination can be initialized
3348           * with these using a fast memory copy
3349           *
3350           * The other way is to figure out exactly how big the string should be
3351           * by parsing the entire input. Then you don't have to make it big
3352           * enough to handle the worst possible case, and more importantly, if
3353           * the string you already have is large enough, you don't have to
3354           * allocate a new string, you can copy the last character in the input
3355           * string to the final position(s) that will be occupied by the
3356           * converted string and go backwards, stopping at t, since everything
3357           * before that is invariant.
3358           *
3359           * There are advantages and disadvantages to each method.
3360           *
3361           * In the first method, we can allocate a new string, do the memory
3362           * copy from the s to t - 1, and then proceed through the rest of the
3363           * string byte-by-byte.
3364           *
3365           * In the second method, we proceed through the rest of the input
3366           * string just calculating how big the converted string will be. Then
3367           * there are two cases:
3368           * 1) if the string has enough extra space to handle the converted
3369           * value. We go backwards through the string, converting until we
3370           * get to the position we are at now, and then stop. If this
3371           * position is far enough along in the string, this method is
3372           * faster than the other method. If the memory copy were the same
3373           * speed as the byte-by-byte loop, that position would be about
3374           * half-way, as at the half-way mark, parsing to the end and back
3375           * is one complete string's parse, the same amount as starting
3376           * over and going all the way through. Actually, it would be
3377           * somewhat less than half-way, as it's faster to just count bytes
3378           * than to also copy, and we don't have the overhead of allocating
3379           * a new string, changing the scalar to use it, and freeing the
3380           * existing one. But if the memory copy is fast, the break-even
3381           * point is somewhere after half way. The counting loop could be
3382           * sped up by vectorization, etc, to move the break-even point
3383           * further towards the beginning.
3384           * 2) if the string doesn't have enough space to handle the converted
3385           * value. A new string will have to be allocated, and one might
3386           * as well, given that, start from the beginning doing the first
3387           * method. We've spent extra time parsing the string and in
3388           * exchange all we've gotten is that we know precisely how big to
3389           * make the new one. Perl is more optimized for time than space,
3390           * so this case is a loser.
3391           * So what I've decided to do is not use the 2nd method unless it is
3392           * guaranteed that a new string won't have to be allocated, assuming
3393           * the worst case. I also decided not to put any more conditions on it
3394           * than this, for now. It seems likely that, since the worst case is
3395           * twice as big as the unknown portion of the string (plus 1), we won't
3396           * be guaranteed enough space, causing us to go to the first method,
3397           * unless the string is short, or the first variant character is near
3398           * the end of it. In either of these cases, it seems best to use the
3399           * 2nd method. The only circumstance I can think of where this would
3400           * be really slower is if the string had once had much more data in it
3401           * than it does now, but there is still a substantial amount in it */
3402            
3403           {
3404 344590         STRLEN invariant_head = t - s;
3405 344590         STRLEN size = invariant_head + (e - t) * 2 + 1 + extra;
3406 344590 100       if (SvLEN(sv) < size) {
3407            
3408           /* Here, have decided to allocate a new string */
3409            
3410           U8 *dst;
3411           U8 *d;
3412            
3413 4134         Newx(dst, size, U8);
3414            
3415           /* If no known invariants at the beginning of the input string,
3416           * set so starts from there. Otherwise, can use memory copy to
3417           * get up to where we are now, and then start from here */
3418            
3419 4404 100       if (invariant_head <= 0) {
3420           d = dst;
3421           } else {
3422 540         Copy(s, dst, invariant_head, char);
3423 540         d = dst + invariant_head;
3424           }
3425            
3426 205176 100       while (t < e) {
3427 201042         append_utf8_from_native_byte(*t, &d);
3428 201042         t++;
3429           }
3430 4134         *d = '\0';
3431 4134 50       SvPV_free(sv); /* No longer using pre-existing string */
    50        
    0        
    0        
3432 4134         SvPV_set(sv, (char*)dst);
3433 4134         SvCUR_set(sv, d - dst);
3434 4134         SvLEN_set(sv, size);
3435           } else {
3436            
3437           /* Here, have decided to get the exact size of the string.
3438           * Currently this happens only when we know that there is
3439           * guaranteed enough space to fit the converted string, so
3440           * don't have to worry about growing. If two_byte_count is 0,
3441           * then t points to the first byte of the string which hasn't
3442           * been examined yet. Otherwise two_byte_count is 1, and t
3443           * points to the first byte in the string that will expand to
3444           * two. Depending on this, start examining at t or 1 after t.
3445           * */
3446            
3447 340456         U8 *d = t + two_byte_count;
3448            
3449            
3450           /* Count up the remaining bytes that expand to two */
3451            
3452 871014 100       while (d < e) {
3453 360330         const U8 chr = *d++;
3454 360330 100       if (! NATIVE_IS_INVARIANT(chr)) two_byte_count++;
3455           }
3456            
3457           /* The string will expand by just the number of bytes that
3458           * occupy two positions. But we are one afterwards because of
3459           * the increment just above. This is the place to put the
3460           * trailing NUL, and to set the length before we decrement */
3461            
3462 340456         d += two_byte_count;
3463 340456         SvCUR_set(sv, d - s);
3464 340456         *d-- = '\0';
3465            
3466            
3467           /* Having decremented d, it points to the position to put the
3468           * very last byte of the expanded string. Go backwards through
3469           * the string, copying and expanding as we go, stopping when we
3470           * get to the part that is invariant the rest of the way down */
3471            
3472 340456         e--;
3473 1138618 100       while (e >= t) {
3474 627934 100       if (NATIVE_IS_INVARIANT(*e)) {
3475 218304         *d-- = *e;
3476           } else {
3477 409630         *d-- = UTF8_EIGHT_BIT_LO(*e);
3478 409630         *d-- = UTF8_EIGHT_BIT_HI(*e);
3479           }
3480 627934         e--;
3481           }
3482           }
3483            
3484 344590 100       if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
    100        
3485           /* Update pos. We do it at the end rather than during
3486           * the upgrade, to avoid slowing down the common case
3487           * (upgrade without pos).
3488           * pos can be stored as either bytes or characters. Since
3489           * this was previously a byte string we can just turn off
3490           * the bytes flag. */
3491 540         MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
3492 540 100       if (mg) {
3493 328         mg->mg_flags &= ~MGf_BYTES;
3494           }
3495 540 100       if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
3496 220         magic_setutf8(sv,mg); /* clear UTF8 cache */
3497           }
3498           }
3499           }
3500            
3501           /* Mark as UTF-8 even if no variant - saves scanning loop */
3502 1292218         SvUTF8_on(sv);
3503 3338441         return SvCUR(sv);
3504           }
3505            
3506           /*
3507           =for apidoc sv_utf8_downgrade
3508            
3509           Attempts to convert the PV of an SV from characters to bytes.
3510           If the PV contains a character that cannot fit
3511           in a byte, this conversion will fail;
3512           in this case, either returns false or, if C is not
3513           true, croaks.
3514            
3515           This is not a general purpose Unicode to byte encoding interface:
3516           use the Encode extension for that.
3517            
3518           =cut
3519           */
3520            
3521           bool
3522 877692         Perl_sv_utf8_downgrade(pTHX_ SV *const sv, const bool fail_ok)
3523           {
3524           dVAR;
3525            
3526           PERL_ARGS_ASSERT_SV_UTF8_DOWNGRADE;
3527            
3528 877692 100       if (SvPOKp(sv) && SvUTF8(sv)) {
3529 170456 100       if (SvCUR(sv)) {
3530           U8 *s;
3531           STRLEN len;
3532           int mg_flags = SV_GMAGIC;
3533            
3534 170454 100       if (SvIsCOW(sv)) {
3535 860         S_sv_uncow(aTHX_ sv, 0);
3536           }
3537 170454 100       if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
    100        
3538           /* update pos */
3539 416         MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
3540 416 100       if (mg && mg->mg_len > 0 && mg->mg_flags & MGf_BYTES) {
    100        
    50        
3541 0         mg->mg_len = sv_pos_b2u_flags(sv, mg->mg_len,
3542           SV_GMAGIC|SV_CONST_RETURN);
3543           mg_flags = 0; /* sv_pos_b2u does get magic */
3544           }
3545 416 100       if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
3546 380         magic_setutf8(sv,mg); /* clear UTF8 cache */
3547            
3548           }
3549 170454 50       s = (U8 *) SvPV_flags(sv, len, mg_flags);
3550            
3551 170454 100       if (!utf8_to_bytes(s, &len)) {
3552 766 100       if (fail_ok)
3553           return FALSE;
3554           else {
3555 30 50       if (PL_op)
3556 45 50       Perl_croak(aTHX_ "Wide character in %s",
3557 15 0       OP_DESC(PL_op));
3558           else
3559 0         Perl_croak(aTHX_ "Wide character");
3560           }
3561           }
3562 169688         SvCUR_set(sv, len);
3563           }
3564           }
3565 876926         SvUTF8_off(sv);
3566 877294         return TRUE;
3567           }
3568            
3569           /*
3570           =for apidoc sv_utf8_encode
3571            
3572           Converts the PV of an SV to UTF-8, but then turns the C
3573           flag off so that it looks like octets again.
3574            
3575           =cut
3576           */
3577            
3578           void
3579 931500         Perl_sv_utf8_encode(pTHX_ SV *const sv)
3580           {
3581           PERL_ARGS_ASSERT_SV_UTF8_ENCODE;
3582            
3583 931500 100       if (SvREADONLY(sv)) {
3584 2         sv_force_normal_flags(sv, 0);
3585           }
3586 931498         (void) sv_utf8_upgrade(sv);
3587 931498         SvUTF8_off(sv);
3588 931498         }
3589            
3590           /*
3591           =for apidoc sv_utf8_decode
3592            
3593           If the PV of the SV is an octet sequence in UTF-8
3594           and contains a multiple-byte character, the C flag is turned on
3595           so that it looks like a character. If the PV contains only single-byte
3596           characters, the C flag stays off.
3597           Scans PV for validity and returns false if the PV is invalid UTF-8.
3598            
3599           =cut
3600           */
3601            
3602           bool
3603 4930         Perl_sv_utf8_decode(pTHX_ SV *const sv)
3604           {
3605           PERL_ARGS_ASSERT_SV_UTF8_DECODE;
3606            
3607 4930 50       if (SvPOKp(sv)) {
3608           const U8 *start, *c;
3609           const U8 *e;
3610            
3611           /* The octets may have got themselves encoded - get them back as
3612           * bytes
3613           */
3614 4930 100       if (!sv_utf8_downgrade(sv, TRUE))
3615           return FALSE;
3616            
3617           /* it is actually just a matter of turning the utf8 flag on, but
3618           * we want to make sure everything inside is valid utf8 first.
3619           */
3620 4926         c = start = (const U8 *) SvPVX_const(sv);
3621 4926 100       if (!is_utf8_string(c, SvCUR(sv)))
3622           return FALSE;
3623 4920         e = (const U8 *) SvEND(sv);
3624 350950 100       while (c < e) {
3625 347834         const U8 ch = *c++;
3626 347834 100       if (!UTF8_IS_INVARIANT(ch)) {
3627 4264         SvUTF8_on(sv);
3628 4264         break;
3629           }
3630           }
3631 4920 100       if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
    50        
3632           /* XXX Is this dead code? XS_utf8_decode calls SvSETMAGIC
3633           after this, clearing pos. Does anything on CPAN
3634           need this? */
3635           /* adjust pos to the start of a UTF8 char sequence */
3636 26         MAGIC * mg = mg_find(sv, PERL_MAGIC_regex_global);
3637 26 100       if (mg) {
3638 24         I32 pos = mg->mg_len;
3639 24 100       if (pos > 0) {
3640 36 50       for (c = start + pos; c > start; c--) {
3641 36 100       if (UTF8_IS_START(*c))
3642           break;
3643           }
3644 20         mg->mg_len = c - start;
3645           }
3646           }
3647 26 100       if ((mg = mg_find(sv, PERL_MAGIC_utf8)))
3648 2477         magic_setutf8(sv,mg); /* clear UTF8 cache */
3649           }
3650           }
3651           return TRUE;
3652           }
3653            
3654           /*
3655           =for apidoc sv_setsv
3656            
3657           Copies the contents of the source SV C into the destination SV
3658           C. The source SV may be destroyed if it is mortal, so don't use this
3659           function if the source SV needs to be reused. Does not handle 'set' magic.
3660           Loosely speaking, it performs a copy-by-value, obliterating any previous
3661           content of the destination.
3662            
3663           You probably want to use one of the assortment of wrappers, such as
3664           C, C, C and
3665           C.
3666            
3667           =for apidoc sv_setsv_flags
3668            
3669           Copies the contents of the source SV C into the destination SV
3670           C. The source SV may be destroyed if it is mortal, so don't use this
3671           function if the source SV needs to be reused. Does not handle 'set' magic.
3672           Loosely speaking, it performs a copy-by-value, obliterating any previous
3673           content of the destination.
3674           If the C parameter has the C bit set, will C on
3675           C if appropriate, else not. If the C
3676           parameter has the C bit set then the
3677           buffers of temps will not be stolen.
3678           and C are implemented in terms of this function.
3679            
3680           You probably want to use one of the assortment of wrappers, such as
3681           C, C, C and
3682           C.
3683            
3684           This is the primary function for copying scalars, and most other
3685           copy-ish functions and macros use this underneath.
3686            
3687           =cut
3688           */
3689            
3690           static void
3691 27508108         S_glob_assign_glob(pTHX_ SV *const dstr, SV *const sstr, const int dtype)
3692           {
3693           I32 mro_changes = 0; /* 1 = method, 2 = isa, 3 = recursive isa */
3694           HV *old_stash = NULL;
3695            
3696           PERL_ARGS_ASSERT_GLOB_ASSIGN_GLOB;
3697            
3698 54919784 100       if (dtype != SVt_PVGV && !isGV_with_GP(dstr)) {
    50        
    0        
    100        
3699 27411676         const char * const name = GvNAME(sstr);
3700 27411676         const STRLEN len = GvNAMELEN(sstr);
3701           {
3702 27411676 100       if (dtype >= SVt_PV) {
3703 27054806 100       SvPV_free(dstr);
    50        
    0        
    0        
3704 27054806         SvPV_set(dstr, 0);
3705 27054806         SvLEN_set(dstr, 0);
3706 27054806         SvCUR_set(dstr, 0);
3707           }
3708 41117378         SvUPGRADE(dstr, SVt_PVGV);
3709 27411676 50       (void)SvOK_off(dstr);
3710           /* We have to turn this on here, even though we turn it off
3711           below, as GvSTASH will fail an assertion otherwise. */
3712 27411676         isGV_with_GP_on(dstr);
3713           }
3714 27411676         GvSTASH(dstr) = GvSTASH(sstr);
3715 27411676 100       if (GvSTASH(dstr))
3716 27411624         Perl_sv_add_backref(aTHX_ MUTABLE_SV(GvSTASH(dstr)), dstr);
3717 27411676 100       gv_name_set(MUTABLE_GV(dstr), name, len,
3718           GV_ADD | (GvNAMEUTF8(sstr) ? SVf_UTF8 : 0 ));
3719 27411676         SvFAKE_on(dstr); /* can coerce to non-glob */
3720           }
3721            
3722 27508108 50       if(GvGP(MUTABLE_GV(sstr))) {
3723           /* If source has method cache entry, clear it */
3724 27508108 100       if(GvCVGEN(sstr)) {
3725 749856         SvREFCNT_dec(GvCV(sstr));
3726 749856         GvCV_set(sstr, NULL);
3727 749856         GvCVGEN(sstr) = 0;
3728           }
3729           /* If source has a real method, then a method is
3730           going to change */
3731 26758252 100       else if(
3732 32982001 100       GvCV((const GV *)sstr) && GvSTASH(dstr) && HvENAME(GvSTASH(dstr))
    50        
    50        
    50        
    100        
    100        
    50        
    50        
3733           ) {
3734           mro_changes = 1;
3735           }
3736           }
3737            
3738           /* If dest already had a real method, that's a change as well */
3739 27508108 100       if(
3740 7904170 100       !mro_changes && GvGP(MUTABLE_GV(dstr)) && GvCVu((const GV *)dstr)
    50        
    100        
3741 46 100       && GvSTASH(dstr) && HvENAME(GvSTASH(dstr))
    50        
    50        
    50        
    50        
    50        
    50        
    50        
3742           ) {
3743           mro_changes = 1;
3744           }
3745            
3746           /* We don't need to check the name of the destination if it was not a
3747           glob to begin with. */
3748 27508108 100       if(dtype == SVt_PVGV) {
3749 96432         const char * const name = GvNAME((const GV *)dstr);
3750 96432 100       if(
3751 96432 100       strEQ(name,"ISA")
    50        
    50        
3752           /* The stash may have been detached from the symbol table, so
3753           check its name. */
3754 16 100       && GvSTASH(dstr) && HvENAME(GvSTASH(dstr))
    50        
    50        
    50        
    50        
    50        
    50        
    50        
3755           )
3756           mro_changes = 2;
3757           else {
3758 96420         const STRLEN len = GvNAMELEN(dstr);
3759 96420 100       if ((len > 1 && name[len-2] == ':' && name[len-1] == ':')
    100        
    50        
3760 96288 100       || (len == 1 && name[0] == ':')) {
    50        
3761           mro_changes = 3;
3762            
3763           /* Set aside the old stash, so we can reset isa caches on
3764           its subclasses. */
3765 132 50       if((old_stash = GvHV(dstr)))
3766           /* Make sure we do not lose it early. */
3767 132         SvREFCNT_inc_simple_void_NN(
3768           sv_2mortal((SV *)old_stash)
3769           );
3770           }
3771           }
3772           }
3773            
3774 27508108         gp_free(MUTABLE_GV(dstr));
3775 27508108         isGV_with_GP_off(dstr); /* SvOK_off does not like globs. */
3776 27508108 50       (void)SvOK_off(dstr);
3777 27508108         isGV_with_GP_on(dstr);
3778 27508108         GvINTRO_off(dstr); /* one-shot flag */
3779 27508108         GvGP_set(dstr, gp_ref(GvGP(sstr)));
3780 27508108 100       if (SvTAINTED(sstr))
    50        
3781 0 0       SvTAINT(dstr);
    0        
    0        
3782 27508108 100       if (GvIMPORTED(dstr) != GVf_IMPORTED
3783 27507988 100       && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
3784           {
3785 24987980         GvIMPORTED_on(dstr);
3786           }
3787 27508108         GvMULTI_on(dstr);
3788 27508108 100       if(mro_changes == 2) {
3789 12 100       if (GvAV((const GV *)sstr)) {
3790           MAGIC *mg;
3791 10         SV * const sref = (SV *)GvAV((const GV *)dstr);
3792 10 50       if (SvSMAGICAL(sref) && (mg = mg_find(sref, PERL_MAGIC_isa))) {
    50        
3793 10 50       if (SvTYPE(mg->mg_obj) != SVt_PVAV) {
3794 10         AV * const ary = newAV();
3795 10         av_push(ary, mg->mg_obj); /* takes the refcount */
3796 10         mg->mg_obj = (SV *)ary;
3797           }
3798 10         av_push((AV *)mg->mg_obj, SvREFCNT_inc_simple_NN(dstr));
3799           }
3800 0         else sv_magic(sref, dstr, PERL_MAGIC_isa, NULL, 0);
3801           }
3802 12         mro_isa_changed_in(GvSTASH(dstr));
3803           }
3804 27508096 100       else if(mro_changes == 3) {
3805 132         HV * const stash = GvHV(dstr);
3806 132 50       if(old_stash ? (HV *)HvENAME_get(old_stash) : stash)
    50        
    50        
    50        
    100        
    100        
    50        
    50        
    50        
3807 132         mro_package_moved(
3808           stash, old_stash,
3809           (GV *)dstr, 0
3810           );
3811           }
3812 27507964 100       else if(mro_changes) mro_method_changed_in(GvSTASH(dstr));
3813 27508108 50       if (GvIO(dstr) && dtype == SVt_PVGV) {
    50        
    50        
    100        
3814           DEBUG_o(Perl_deb(aTHX_
3815           "glob_assign_glob clearing PL_stashcache\n"));
3816           /* It's a cache. It will rebuild itself quite happily.
3817           It's a lot of effort to work out exactly which key (or keys)
3818           might be invalidated by the creation of the this file handle.
3819           */
3820 6480         hv_clear(PL_stashcache);
3821           }
3822 27508108         return;
3823           }
3824            
3825           static void
3826 2270345         S_glob_assign_ref(pTHX_ SV *const dstr, SV *const sstr)
3827           {
3828 2270345         SV * const sref = SvRV(sstr);
3829           SV *dref;
3830 2270345         const int intro = GvINTRO(dstr);
3831           SV **location;
3832           U8 import_flag = 0;
3833 2270345         const U32 stype = SvTYPE(sref);
3834            
3835           PERL_ARGS_ASSERT_GLOB_ASSIGN_REF;
3836            
3837 2270345 100       if (intro) {
3838 311899         GvINTRO_off(dstr); /* one-shot flag */
3839 311899         GvLINE(dstr) = CopLINE(PL_curcop);
3840 311899         GvEGV(dstr) = MUTABLE_GV(dstr);
3841           }
3842 2270345         GvMULTI_on(dstr);
3843 2270345         switch (stype) {
3844           case SVt_PVCV:
3845 1768343         location = (SV **) &(GvGP(dstr)->gp_cv); /* XXX bypassing GvCV_set */
3846           import_flag = GVf_IMPORTED_CV;
3847 1768343         goto common;
3848           case SVt_PVHV:
3849 101700         location = (SV **) &GvHV(dstr);
3850           import_flag = GVf_IMPORTED_HV;
3851 101700         goto common;
3852           case SVt_PVAV:
3853 125240         location = (SV **) &GvAV(dstr);
3854           import_flag = GVf_IMPORTED_AV;
3855 125240         goto common;
3856           case SVt_PVIO:
3857 20         location = (SV **) &GvIOp(dstr);
3858 20         goto common;
3859           case SVt_PVFM:
3860 24         location = (SV **) &GvFORM(dstr);
3861 24         goto common;
3862           default:
3863 275018         location = &GvSV(dstr);
3864           import_flag = GVf_IMPORTED_SV;
3865           common:
3866 2270345 100       if (intro) {
3867 311899 100       if (stype == SVt_PVCV) {
3868           /*if (GvCVGEN(dstr) && (GvCV(dstr) != (const CV *)sref || GvCVGEN(dstr))) {*/
3869 297598 100       if (GvCVGEN(dstr)) {
3870 18         SvREFCNT_dec(GvCV(dstr));
3871 18         GvCV_set(dstr, NULL);
3872 18         GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3873           }
3874           }
3875           /* SAVEt_GVSLOT takes more room on the savestack and has more
3876           overhead in leave_scope than SAVEt_GENERIC_SV. But for CVs
3877           leave_scope needs access to the GV so it can reset method
3878           caches. We must use SAVEt_GVSLOT whenever the type is
3879           SVt_PVCV, even if the stash is anonymous, as the stash may
3880           gain a name somehow before leave_scope. */
3881 311899 100       if (stype == SVt_PVCV) {
3882           /* There is no save_pushptrptrptr. Creating it for this
3883           one call site would be overkill. So inline the ss add
3884           routines here. */
3885 297598         dSS_ADD;
3886 297598         SS_ADD_PTR(dstr);
3887 297598         SS_ADD_PTR(location);
3888 595196         SS_ADD_PTR(SvREFCNT_inc(*location));
3889 297598         SS_ADD_UV(SAVEt_GVSLOT);
3890 297598 50       SS_ADD_END(4);
3891           }
3892 14301         else SAVEGENERICSV(*location);
3893           }
3894 2270345         dref = *location;
3895 2270345 100       if (stype == SVt_PVCV && (*location != sref || GvCVGEN(dstr))) {
    100        
    100        
3896 1738169         CV* const cv = MUTABLE_CV(*location);
3897 1738169 100       if (cv) {
3898 476808 100       if (!GvCVGEN((const GV *)dstr) &&
    100        
3899 476802 50       (CvROOT(cv) || CvXSUB(cv)) &&
    100        
3900           /* redundant check that avoids creating the extra SV
3901           most of the time: */
3902 446446 100       (CvCONST(cv) || ckWARN(WARN_REDEFINE)))
3903           {
3904 18574         SV * const new_const_sv =
3905 18574         CvCONST((const CV *)sref)
3906           ? cv_const_sv((const CV *)sref)
3907 18574 100       : NULL;
3908 18574 100       report_redefined_cv(
    50        
    50        
    50        
    50        
3909           sv_2mortal(Perl_newSVpvf(aTHX_
3910           "%"HEKf"::%"HEKf,
3911           HEKfARG(
3912           HvNAME_HEK(GvSTASH((const GV *)dstr))
3913           ),
3914           HEKfARG(GvENAME_HEK(MUTABLE_GV(dstr)))
3915           )),
3916           cv,
3917           CvCONST((const CV *)sref) ? &new_const_sv : NULL
3918           );
3919           }
3920 317868 100       if (!intro)
3921 27352 100       cv_ckproto_len_flags(cv, (const GV *)dstr,
    100        
    50        
    50        
    100        
    100        
    50        
    50        
    100        
3922           SvPOK(sref) ? CvPROTO(sref) : NULL,
3923           SvPOK(sref) ? CvPROTOLEN(sref) : 0,
3924           SvPOK(sref) ? SvUTF8(sref) : 0);
3925           }
3926 1738163         GvCVGEN(dstr) = 0; /* Switch off cacheness. */
3927 1738163         GvASSUMECV_on(dstr);
3928 1738163 50       if(GvSTASH(dstr)) gv_method_changed(dstr); /* sub foo { 1 } sub bar { 2 } *bar = \&foo */
    100        
3929           }
3930 2270339         *location = SvREFCNT_inc_simple_NN(sref);
3931 2270339 100       if (import_flag && !(GvFLAGS(dstr) & import_flag)
    100        
3932 1927310 100       && CopSTASH_ne(PL_curcop, GvSTASH(dstr))) {
3933 1503927         GvFLAGS(dstr) |= import_flag;
3934           }
3935 2270339 100       if (stype == SVt_PVHV) {
3936 101700         const char * const name = GvNAME((GV*)dstr);
3937 101700         const STRLEN len = GvNAMELEN(dstr);
3938 101700 100       if (
3939           (
3940 101694 100       (len > 1 && name[len-2] == ':' && name[len-1] == ':')
    50        
3941 101652 100       || (len == 1 && name[0] == ':')
    50        
3942           )
3943 48 50       && (!dref || HvENAME_get(dref))
    50        
    50        
    50        
    50        
    100        
    50        
    50        
3944           ) {
3945 48         mro_package_moved(
3946           (HV *)sref, (HV *)dref,
3947           (GV *)dstr, 0
3948           );
3949           }
3950           }
3951 2168639 100       else if (
3952 2168639         stype == SVt_PVAV && sref != dref
3953 9944 100       && strEQ(GvNAME((GV*)dstr), "ISA")
    50        
    50        
    100        
3954           /* The stash may have been detached from the symbol table, so
3955           check its name before doing anything. */
3956 78 100       && GvSTASH(dstr) && HvENAME(GvSTASH(dstr))
    50        
    50        
    50        
    50        
    50        
    50        
    50        
3957 74         ) {
3958           MAGIC *mg;
3959 74 50       MAGIC * const omg = dref && SvSMAGICAL(dref)
3960           ? mg_find(dref, PERL_MAGIC_isa)
3961 148 50       : NULL;
3962 74 100       if (SvSMAGICAL(sref) && (mg = mg_find(sref, PERL_MAGIC_isa))) {
    50        
3963 54 100       if (SvTYPE(mg->mg_obj) != SVt_PVAV) {
3964 24         AV * const ary = newAV();
3965 24         av_push(ary, mg->mg_obj); /* takes the refcount */
3966 24         mg->mg_obj = (SV *)ary;
3967           }
3968 54 50       if (omg) {
3969 54 50       if (SvTYPE(omg->mg_obj) == SVt_PVAV) {
3970 0         SV **svp = AvARRAY((AV *)omg->mg_obj);
3971 0         I32 items = AvFILLp((AV *)omg->mg_obj) + 1;
3972 0 0       while (items--)
3973 0         av_push(
3974           (AV *)mg->mg_obj,
3975           SvREFCNT_inc_simple_NN(*svp++)
3976           );
3977           }
3978           else
3979 54         av_push(
3980           (AV *)mg->mg_obj,
3981           SvREFCNT_inc_simple_NN(omg->mg_obj)
3982           );
3983           }
3984           else
3985 0         av_push((AV *)mg->mg_obj,SvREFCNT_inc_simple_NN(dstr));
3986           }
3987           else
3988           {
3989 20 50       sv_magic(
3990           sref, omg ? omg->mg_obj : dstr, PERL_MAGIC_isa, NULL, 0
3991           );
3992 20         mg = mg_find(sref, PERL_MAGIC_isa);
3993           }
3994           /* Since the *ISA assignment could have affected more than
3995           one stash, don't call mro_isa_changed_in directly, but let
3996           magic_clearisa do it for us, as it already has the logic for
3997           dealing with globs vs arrays of globs. */
3998           assert(mg);
3999 74         Perl_magic_clearisa(aTHX_ NULL, mg);
4000           }
4001 2168565 100       else if (stype == SVt_PVIO) {
4002           DEBUG_o(Perl_deb(aTHX_ "glob_assign_ref clearing PL_stashcache\n"));
4003           /* It's a cache. It will rebuild itself quite happily.
4004           It's a lot of effort to work out exactly which key (or keys)
4005           might be invalidated by the creation of the this file handle.
4006           */
4007 20         hv_clear(PL_stashcache);
4008           }
4009           break;
4010           }
4011 2270339 100       if (!intro) SvREFCNT_dec(dref);
4012 2270339 50       if (SvTAINTED(sstr))
    0        
4013 0 0       SvTAINT(dstr);
    0        
    0        
4014 2270339         return;
4015           }
4016            
4017           /* Work around compiler warnings about unsigned >= THRESHOLD when thres-
4018           hold is 0. */
4019           #if SV_COW_THRESHOLD
4020           # define GE_COW_THRESHOLD(len) ((len) >= SV_COW_THRESHOLD)
4021           #else
4022           # define GE_COW_THRESHOLD(len) 1
4023           #endif
4024           #if SV_COWBUF_THRESHOLD
4025           # define GE_COWBUF_THRESHOLD(len) ((len) >= SV_COWBUF_THRESHOLD)
4026           #else
4027           # define GE_COWBUF_THRESHOLD(len) 1
4028           #endif
4029            
4030           void
4031 2006120706         Perl_sv_setsv_flags(pTHX_ SV *dstr, SV* sstr, const I32 flags)
4032           {
4033           dVAR;
4034           U32 sflags;
4035           int dtype;
4036           svtype stype;
4037            
4038           PERL_ARGS_ASSERT_SV_SETSV_FLAGS;
4039            
4040 2006120706 100       if (sstr == dstr)
4041           return;
4042            
4043 2006118310 50       if (SvIS_FREED(dstr)) {
4044 0         Perl_croak(aTHX_ "panic: attempt to copy value %" SVf
4045           " to a freed scalar %p", SVfARG(sstr), (void *)dstr);
4046           }
4047 2006118310 100       SV_CHECK_THINKFIRST_COW_DROP(dstr);
4048 2006118248 100       if (!sstr)
4049           sstr = &PL_sv_undef;
4050 2006118248 100       if (SvIS_FREED(sstr)) {
4051 2         Perl_croak(aTHX_ "panic: attempt to copy freed scalar %p to %p",
4052           (void*)sstr, (void*)dstr);
4053           }
4054 2006118246         stype = SvTYPE(sstr);
4055 2006118246         dtype = SvTYPE(dstr);
4056            
4057           /* There's a lot of redundancy below but we're going for speed here */
4058            
4059 2006118246         switch (stype) {
4060           case SVt_NULL:
4061           undef_sstr:
4062 100088469 100       if (dtype != SVt_PVGV && dtype != SVt_PVLV) {
4063 99946025 50       (void)SvOK_off(dstr);
4064           return;
4065           }
4066           break;
4067           case SVt_IV:
4068 943707383 100       if (SvIOK(sstr)) {
4069 444678728         switch (dtype) {
4070           case SVt_NULL:
4071 234767721         sv_upgrade(dstr, SVt_IV);
4072 234767721         break;
4073           case SVt_NV:
4074           case SVt_PV:
4075 321834         sv_upgrade(dstr, SVt_PVIV);
4076 321834         break;
4077           case SVt_PVGV:
4078           case SVt_PVLV:
4079           goto end_of_first_switch;
4080           }
4081 439777188 50       (void)SvIOK_only(dstr);
4082 439777188         SvIV_set(dstr, SvIVX(sstr));
4083 439777188 100       if (SvIsUV(sstr))
4084 53936         SvIsUV_on(dstr);
4085           /* SvTAINTED can only be true if the SV has taint magic, which in
4086           turn means that the SV type is PVMG (or greater). This is the
4087           case statement for SVt_IV, so this cannot be true (whatever gcov
4088           may say). */
4089           assert(!SvTAINTED(sstr));
4090           return;
4091           }
4092 499028655 100       if (!SvROK(sstr))
4093           goto undef_sstr;
4094 487511553 100       if (dtype < SVt_PV && dtype != SVt_IV)
4095 83497489         sv_upgrade(dstr, SVt_IV);
4096           break;
4097            
4098           case SVt_NV:
4099 9360370 100       if (SvNOK(sstr)) {
4100 9360368         switch (dtype) {
4101           case SVt_NULL:
4102           case SVt_IV:
4103 3905078         sv_upgrade(dstr, SVt_NV);
4104 3905078         break;
4105           case SVt_PV:
4106           case SVt_PVIV:
4107 49314         sv_upgrade(dstr, SVt_PVNV);
4108 49314         break;
4109           case SVt_PVGV:
4110           case SVt_PVLV:
4111           goto end_of_first_switch;
4112           }
4113 9342822         SvNV_set(dstr, SvNVX(sstr));
4114 9342822 50       (void)SvNOK_only(dstr);
4115           /* SvTAINTED can only be true if the SV has taint magic, which in
4116           turn means that the SV type is PVMG (or greater). This is the
4117           case statement for SVt_NV, so this cannot be true (whatever gcov
4118           may say). */
4119           assert(!SvTAINTED(sstr));
4120 9342822         return;
4121           }
4122           goto undef_sstr;
4123            
4124           case SVt_PV:
4125 607288604 100       if (dtype < SVt_PV)
4126 241946151         sv_upgrade(dstr, SVt_PV);
4127           break;
4128           case SVt_PVIV:
4129 42444802 100       if (dtype < SVt_PVIV)
4130 14761326         sv_upgrade(dstr, SVt_PVIV);
4131           break;
4132           case SVt_PVNV:
4133 131918240 100       if (dtype < SVt_PVNV)
4134 81943090         sv_upgrade(dstr, SVt_PVNV);
4135           break;
4136           default:
4137           {
4138 0         const char * const type = sv_reftype(sstr,0);
4139 0 0       if (PL_op)
4140           /* diag_listed_as: Bizarre copy of %s */
4141 0 0       Perl_croak(aTHX_ "Bizarre copy of %s in %s", type, OP_DESC(PL_op));
    0        
4142           else
4143 0         Perl_croak(aTHX_ "Bizarre copy of %s", type);
4144           }
4145           break;
4146            
4147           case SVt_REGEXP:
4148           upgregexp:
4149 66 100       if (dtype < SVt_REGEXP)
4150           {
4151 56 100       if (dtype >= SVt_PV) {
4152 26 100       SvPV_free(dstr);
    50        
    0        
    0        
4153 26         SvPV_set(dstr, 0);
4154 26         SvLEN_set(dstr, 0);
4155 26         SvCUR_set(dstr, 0);
4156           }
4157 56         sv_upgrade(dstr, SVt_REGEXP);
4158           }
4159           break;
4160            
4161           case SVt_INVLIST:
4162           case SVt_PVLV:
4163           case SVt_PVGV:
4164           case SVt_PVMG:
4165 182827422 100       if (SvGMAGICAL(sstr) && (flags & SV_GMAGIC)) {
    100        
4166 16885509         mg_get(sstr);
4167 16885507 100       if (SvTYPE(sstr) != stype)
4168 4         stype = SvTYPE(sstr);
4169           }
4170 182827420 100       if (isGV_with_GP(sstr) && dtype <= SVt_PVLV) {
    50        
4171 27476775         glob_assign_glob(dstr, sstr, dtype);
4172 27476775         return;
4173           }
4174 232551168 100       if (stype == SVt_PVLV)
    100        
    100        
4175           {
4176 450119 50       if (isREGEXP(sstr)) goto upgregexp;
    100        
4177 464056         SvUPGRADE(dstr, SVt_PVNV);
4178           }
4179           else
4180 146750178         SvUPGRADE(dstr, (svtype)stype);
4181           }
4182           end_of_first_switch:
4183            
4184           /* dstr may have been upgraded. */
4185 1429575434         dtype = SvTYPE(dstr);
4186 1429575434         sflags = SvFLAGS(sstr);
4187            
4188 1429575434 100       if (dtype == SVt_PVCV) {
4189           /* Assigning to a subroutine sets the prototype. */
4190 32 50       if (SvOK(sstr)) {
    0        
    0        
4191           STRLEN len;
4192 16 50       const char *const ptr = SvPV_const(sstr, len);
4193            
4194 16 50       SvGROW(dstr, len + 1);
    100        
4195 16         Copy(ptr, SvPVX(dstr), len + 1, char);
4196 16         SvCUR_set(dstr, len);
4197 16         SvPOK_only(dstr);
4198 16         SvFLAGS(dstr) |= sflags & SVf_UTF8;
4199 16         CvAUTOLOAD_off(dstr);
4200           } else {
4201 0 0       SvOK_off(dstr);
4202           }
4203           }
4204 1429575418 50       else if (dtype == SVt_PVAV || dtype == SVt_PVHV || dtype == SVt_PVFM) {
4205 0         const char * const type = sv_reftype(dstr,0);
4206 0 0       if (PL_op)
4207           /* diag_listed_as: Cannot copy to %s */
4208 0 0       Perl_croak(aTHX_ "Cannot copy to %s in %s", type, OP_DESC(PL_op));
    0        
4209           else
4210 0         Perl_croak(aTHX_ "Cannot copy to %s", type);
4211 1429575418 100       } else if (sflags & SVf_ROK) {
4212 548545323 100       if (isGV_with_GP(dstr)
    50        
4213 2304772 100       && SvTYPE(SvRV(sstr)) == SVt_PVGV && isGV_with_GP(SvRV(sstr))) {
    50        
    50        
4214 34427         sstr = SvRV(sstr);
4215 34427 100       if (sstr == dstr) {
4216 3094 50       if (GvIMPORTED(dstr) != GVf_IMPORTED
4217 3094 50       && CopSTASH_ne(PL_curcop, GvSTASH(dstr)))
4218           {
4219 3094         GvIMPORTED_on(dstr);
4220           }
4221 3094         GvMULTI_on(dstr);
4222 3094         return;
4223           }
4224 31333         glob_assign_glob(dstr, sstr, dtype);
4225 31333         return;
4226           }
4227            
4228 548510896 100       if (dtype >= SVt_PV) {
4229 118957336 100       if (isGV_with_GP(dstr)) {
    50        
4230 2270345         glob_assign_ref(dstr, sstr);
4231 2270339         return;
4232           }
4233 116686991 100       if (SvPVX_const(dstr)) {
4234 1667224 50       SvPV_free(dstr);
    100        
    50        
    50        
4235 1667224         SvLEN_set(dstr, 0);
4236 1667224         SvCUR_set(dstr, 0);
4237           }
4238           }
4239 546240551 50       (void)SvOK_off(dstr);
4240 1092481102         SvRV_set(dstr, SvREFCNT_inc(SvRV(sstr)));
4241 546240551         SvFLAGS(dstr) |= sflags & SVf_ROK;
4242           assert(!(sflags & SVp_NOK));
4243           assert(!(sflags & SVp_IOK));
4244           assert(!(sflags & SVf_NOK));
4245           assert(!(sflags & SVf_IOK));
4246           }
4247 881030095 100       else if (isGV_with_GP(dstr)) {
    50        
4248 106 100       if (!(sflags & SVf_OK)) {
4249 28         Perl_ck_warner(aTHX_ packWARN(WARN_MISC),
4250           "Undefined value assigned to typeglob");
4251           }
4252           else {
4253 78         GV *gv = gv_fetchsv_nomg(sstr, GV_ADD, SVt_PVGV);
4254 78 50       if (dstr != (const SV *)gv) {
4255 78         const char * const name = GvNAME((const GV *)dstr);
4256